blob: bece882f99ec44084ed3c0447f96238ef9b81c1b [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{
229 log->l_grant_write_bytes += bytes;
230 if (log->l_grant_write_bytes > log->l_logsize) {
231 log->l_grant_write_bytes -= log->l_logsize;
232 log->l_grant_write_cycle++;
233 }
234}
235
236static void
237xlog_grant_add_space_reserve(struct log *log, int bytes)
238{
239 log->l_grant_reserve_bytes += bytes;
240 if (log->l_grant_reserve_bytes > log->l_logsize) {
241 log->l_grant_reserve_bytes -= log->l_logsize;
242 log->l_grant_reserve_cycle++;
243 }
244}
245
246static inline void
247xlog_grant_add_space(struct log *log, int bytes)
248{
249 xlog_grant_add_space_write(log, bytes);
250 xlog_grant_add_space_reserve(log, bytes);
251}
252
Christoph Hellwig0adba532007-08-30 17:21:46 +1000253static void
254xlog_tic_reset_res(xlog_ticket_t *tic)
255{
256 tic->t_res_num = 0;
257 tic->t_res_arr_sum = 0;
258 tic->t_res_num_ophdrs = 0;
259}
260
261static void
262xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
263{
264 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
265 /* add to overflow and start again */
266 tic->t_res_o_flow += tic->t_res_arr_sum;
267 tic->t_res_num = 0;
268 tic->t_res_arr_sum = 0;
269 }
270
271 tic->t_res_arr[tic->t_res_num].r_len = len;
272 tic->t_res_arr[tic->t_res_num].r_type = type;
273 tic->t_res_arr_sum += len;
274 tic->t_res_num++;
275}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * NOTES:
279 *
280 * 1. currblock field gets updated at startup and after in-core logs
281 * marked as with WANT_SYNC.
282 */
283
284/*
285 * This routine is called when a user of a log manager ticket is done with
286 * the reservation. If the ticket was ever used, then a commit record for
287 * the associated transaction is written out as a log operation header with
288 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
289 * a given ticket. If the ticket was one with a permanent reservation, then
290 * a few operations are done differently. Permanent reservation tickets by
291 * default don't release the reservation. They just commit the current
292 * transaction with the belief that the reservation is still needed. A flag
293 * must be passed in before permanent reservations are actually released.
294 * When these type of tickets are not released, they need to be set into
295 * the inited state again. By doing this, a start record will be written
296 * out when the next write occurs.
297 */
298xfs_lsn_t
299xfs_log_done(xfs_mount_t *mp,
300 xfs_log_ticket_t xtic,
301 void **iclog,
302 uint flags)
303{
304 xlog_t *log = mp->m_log;
305 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
306 xfs_lsn_t lsn = 0;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (XLOG_FORCED_SHUTDOWN(log) ||
309 /*
310 * If nothing was ever written, don't write out commit record.
311 * If we get an error, just continue and give back the log ticket.
312 */
313 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
314 (xlog_commit_record(mp, ticket,
315 (xlog_in_core_t **)iclog, &lsn)))) {
316 lsn = (xfs_lsn_t) -1;
317 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
318 flags |= XFS_LOG_REL_PERM_RESERV;
319 }
320 }
321
322
323 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
324 (flags & XFS_LOG_REL_PERM_RESERV)) {
325 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000326 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * request has been made to release a permanent reservation.
328 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000329 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 xlog_ungrant_log_space(log, ticket);
David Chinnereb01c9c2008-04-10 12:18:46 +1000331 xlog_ticket_put(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000333 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xlog_regrant_reserve_log_space(log, ticket);
335 }
336
337 /* If this ticket was a permanent reservation and we aren't
338 * trying to release it, reset the inited flags; so next time
339 * we write, a start record will be written out.
340 */
341 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
342 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
343 ticket->t_flags |= XLOG_TIC_INITED;
344
345 return lsn;
346} /* xfs_log_done */
347
348
349/*
350 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
351 * the force is done synchronously.
352 *
353 * Asynchronous forces are implemented by setting the WANT_SYNC
354 * bit in the appropriate in-core log and then returning.
355 *
356 * Synchronous forces are implemented with a semaphore. All callers
357 * to force a given lsn to disk will wait on a semaphore attached to the
358 * specific in-core log. When given in-core log finally completes its
359 * write to disk, that thread will wake up all threads waiting on the
360 * semaphore.
361 */
362int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100363_xfs_log_force(
364 xfs_mount_t *mp,
365 xfs_lsn_t lsn,
366 uint flags,
367 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100369 xlog_t *log = mp->m_log;
370 int dummy;
371
372 if (!log_flushed)
373 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ASSERT(flags & XFS_LOG_FORCE);
376
377 XFS_STATS_INC(xs_log_force);
378
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100379 if (log->l_flags & XLOG_IO_ERROR)
380 return XFS_ERROR(EIO);
381 if (lsn == 0)
382 return xlog_state_sync_all(log, flags, log_flushed);
383 else
384 return xlog_state_sync(log, lsn, flags, log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385} /* xfs_log_force */
386
387/*
388 * Attaches a new iclog I/O completion callback routine during
389 * transaction commit. If the log is in error state, a non-zero
390 * return code is handed back and the caller is responsible for
391 * executing the callback at an appropriate time.
392 */
393int
394xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
395 void *iclog_hndl, /* iclog to hang callback off */
396 xfs_log_callback_t *cb)
397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000399 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Chinner114d23a2008-04-10 12:18:39 +1000401 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
403 if (!abortflg) {
404 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
405 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
406 cb->cb_next = NULL;
407 *(iclog->ic_callback_tail) = cb;
408 iclog->ic_callback_tail = &(cb->cb_next);
409 }
David Chinner114d23a2008-04-10 12:18:39 +1000410 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return abortflg;
412} /* xfs_log_notify */
413
414int
415xfs_log_release_iclog(xfs_mount_t *mp,
416 void *iclog_hndl)
417{
418 xlog_t *log = mp->m_log;
419 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
420
421 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000422 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100423 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 return 0;
427}
428
429/*
430 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
431 * to the reservation.
432 * 2. Potentially, push buffers at tail of log to disk.
433 *
434 * Each reservation is going to reserve extra space for a log record header.
435 * When writes happen to the on-disk log, we don't subtract the length of the
436 * log record header from any reservation. By wasting space in each
437 * reservation, we prevent over allocation problems.
438 */
439int
440xfs_log_reserve(xfs_mount_t *mp,
441 int unit_bytes,
442 int cnt,
443 xfs_log_ticket_t *ticket,
444 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000445 uint flags,
446 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 xlog_t *log = mp->m_log;
449 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100450 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
453 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
454
455 if (XLOG_FORCED_SHUTDOWN(log))
456 return XFS_ERROR(EIO);
457
458 XFS_STATS_INC(xs_try_logspace);
459
460 if (*ticket != NULL) {
461 ASSERT(flags & XFS_LOG_PERM_RESERV);
462 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000463 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
465 retval = xlog_regrant_write_log_space(log, internal_ticket);
466 } else {
467 /* may sleep if need to allocate more tickets */
468 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
469 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000470 if (!internal_ticket)
471 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000472 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000474 xlog_trace_loggrant(log, internal_ticket,
475 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
476 "xfs_log_reserve: create new ticket (permanent trans)" :
477 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 xlog_grant_push_ail(mp,
479 (internal_ticket->t_unit_res *
480 internal_ticket->t_cnt));
481 retval = xlog_grant_log_space(log, internal_ticket);
482 }
483
484 return retval;
485} /* xfs_log_reserve */
486
487
488/*
489 * Mount a log filesystem
490 *
491 * mp - ubiquitous xfs mount point structure
492 * log_target - buftarg of on-disk log device
493 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
494 * num_bblocks - Number of BBSIZE blocks in on-disk log
495 *
496 * Return error or zero.
497 */
498int
David Chinner249a8c12008-02-05 12:13:32 +1100499xfs_log_mount(
500 xfs_mount_t *mp,
501 xfs_buftarg_t *log_target,
502 xfs_daddr_t blk_offset,
503 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
David Chinner249a8c12008-02-05 12:13:32 +1100505 int error;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
508 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
509 else {
510 cmn_err(CE_NOTE,
511 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
512 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000513 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
David Chinner249a8c12008-02-05 12:13:32 +1100519 * Initialize the AIL now we have a log.
520 */
521 spin_lock_init(&mp->m_ail_lock);
522 error = xfs_trans_ail_init(mp);
523 if (error) {
524 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
525 goto error;
526 }
527
528 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * skip log recovery on a norecovery mount. pretend it all
530 * just worked.
531 */
532 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100533 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000536 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Eric Sandeen65be6052006-01-11 15:34:19 +1100538 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000541 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (error) {
543 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
David Chinner249a8c12008-02-05 12:13:32 +1100544 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 }
547
548 /* Normal transactions can now occur */
549 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
550
551 /* End mounting message in xfs_log_mount_finish */
552 return 0;
David Chinner249a8c12008-02-05 12:13:32 +1100553error:
554 xfs_log_unmount_dealloc(mp);
555 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556} /* xfs_log_mount */
557
558/*
559 * Finish the recovery of the file system. This is separate from
560 * the xfs_log_mount() call, because it depends on the code in
561 * xfs_mountfs() to read in the root and real-time bitmap inodes
562 * between calling xfs_log_mount() and here.
563 *
564 * mp - ubiquitous xfs mount point structure
565 */
566int
567xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
568{
569 int error;
570
571 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
572 error = xlog_recover_finish(mp->m_log, mfsi_flags);
573 else {
574 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000575 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 return error;
579}
580
581/*
582 * Unmount processing for the log.
583 */
584int
585xfs_log_unmount(xfs_mount_t *mp)
586{
587 int error;
588
589 error = xfs_log_unmount_write(mp);
590 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100591 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/*
595 * Final log writes as part of unmount.
596 *
597 * Mark the filesystem clean as unmount happens. Note that during relocation
598 * this routine needs to be executed as part of source-bag while the
599 * deallocation must not be done until source-end.
600 */
601
602/*
603 * Unmount record used to have a string "Unmount filesystem--" in the
604 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
605 * We just write the magic number now since that particular field isn't
606 * currently architecture converted and "nUmount" is a bit foo.
607 * As far as I know, there weren't any dependencies on the old behaviour.
608 */
609
610int
611xfs_log_unmount_write(xfs_mount_t *mp)
612{
613 xlog_t *log = mp->m_log;
614 xlog_in_core_t *iclog;
615#ifdef DEBUG
616 xlog_in_core_t *first_iclog;
617#endif
618 xfs_log_iovec_t reg[1];
619 xfs_log_ticket_t tic = NULL;
620 xfs_lsn_t lsn;
621 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* the data section must be 32 bit size aligned */
624 struct {
625 __uint16_t magic;
626 __uint16_t pad1;
627 __uint32_t pad2; /* may as well make it 64 bits */
628 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * Don't write out unmount record on read-only mounts.
632 * Or, if we are doing a forced umount (typically because of IO errors).
633 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000634 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636
637 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
638
639#ifdef DEBUG
640 first_iclog = iclog = log->l_iclog;
641 do {
642 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
643 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
644 ASSERT(iclog->ic_offset == 0);
645 }
646 iclog = iclog->ic_next;
647 } while (iclog != first_iclog);
648#endif
649 if (! (XLOG_FORCED_SHUTDOWN(log))) {
650 reg[0].i_addr = (void*)&magic;
651 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000652 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Tim Shimmin955e47a2006-09-28 11:04:16 +1000654 error = xfs_log_reserve(mp, 600, 1, &tic,
655 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!error) {
657 /* remove inited flag */
658 ((xlog_ticket_t *)tic)->t_flags = 0;
659 error = xlog_write(mp, reg, 1, tic, &lsn,
660 NULL, XLOG_UNMOUNT_TRANS);
661 /*
662 * At this point, we're umounting anyway,
663 * so there's no point in transitioning log state
664 * to IOERROR. Just continue...
665 */
666 }
667
668 if (error) {
669 xfs_fs_cmn_err(CE_ALERT, mp,
670 "xfs_log_unmount: unmount record failed");
671 }
672
673
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000674 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100676 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000677 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xlog_state_want_sync(log, iclog);
679 (void) xlog_state_release_iclog(log, iclog);
680
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000681 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
683 iclog->ic_state == XLOG_STATE_DIRTY)) {
684 if (!XLOG_FORCED_SHUTDOWN(log)) {
685 sv_wait(&iclog->ic_forcesema, PMEM,
686 &log->l_icloglock, s);
687 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000688 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000691 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000693 if (tic) {
694 xlog_trace_loggrant(log, tic, "unmount rec");
695 xlog_ungrant_log_space(log, tic);
David Chinnereb01c9c2008-04-10 12:18:46 +1000696 xlog_ticket_put(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 } else {
699 /*
700 * We're already in forced_shutdown mode, couldn't
701 * even attempt to write out the unmount transaction.
702 *
703 * Go through the motions of sync'ing and releasing
704 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000705 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * be in progress. Do this as a separate section of
707 * code so we'll know if we ever get stuck here that
708 * we're in this odd situation of trying to unmount
709 * a file system that went into forced_shutdown as
710 * the result of an unmount..
711 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000712 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100714 atomic_inc(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000715 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 xlog_state_want_sync(log, iclog);
718 (void) xlog_state_release_iclog(log, iclog);
719
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000720 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
723 || iclog->ic_state == XLOG_STATE_DIRTY
724 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
725
726 sv_wait(&iclog->ic_forcesema, PMEM,
727 &log->l_icloglock, s);
728 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000729 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 }
732
733 return 0;
734} /* xfs_log_unmount_write */
735
736/*
737 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100738 *
739 * We need to stop the aild from running before we destroy
740 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
742void
743xfs_log_unmount_dealloc(xfs_mount_t *mp)
744{
David Chinner249a8c12008-02-05 12:13:32 +1100745 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000746 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * Write region vectors to log. The write happens using the space reservation
751 * of the ticket (tic). It is not a requirement that all writes for a given
752 * transaction occur with one call to xfs_log_write().
753 */
754int
755xfs_log_write(xfs_mount_t * mp,
756 xfs_log_iovec_t reg[],
757 int nentries,
758 xfs_log_ticket_t tic,
759 xfs_lsn_t *start_lsn)
760{
761 int error;
762 xlog_t *log = mp->m_log;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (XLOG_FORCED_SHUTDOWN(log))
765 return XFS_ERROR(EIO);
766
767 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000768 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100770 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771} /* xfs_log_write */
772
773
774void
775xfs_log_move_tail(xfs_mount_t *mp,
776 xfs_lsn_t tail_lsn)
777{
778 xlog_ticket_t *tic;
779 xlog_t *log = mp->m_log;
780 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (XLOG_FORCED_SHUTDOWN(log))
783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (tail_lsn == 0) {
786 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000787 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000789 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000792 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
795 * tail_lsn.
796 */
797 if (tail_lsn != 1) {
798 log->l_tail_lsn = tail_lsn;
799 }
800
801 if ((tic = log->l_write_headq)) {
802#ifdef DEBUG
803 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
804 panic("Recovery problem");
805#endif
806 cycle = log->l_grant_write_cycle;
807 bytes = log->l_grant_write_bytes;
808 free_bytes = xlog_space_left(log, cycle, bytes);
809 do {
810 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
811
812 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
813 break;
814 tail_lsn = 0;
815 free_bytes -= tic->t_unit_res;
816 sv_signal(&tic->t_sema);
817 tic = tic->t_next;
818 } while (tic != log->l_write_headq);
819 }
820 if ((tic = log->l_reserve_headq)) {
821#ifdef DEBUG
822 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
823 panic("Recovery problem");
824#endif
825 cycle = log->l_grant_reserve_cycle;
826 bytes = log->l_grant_reserve_bytes;
827 free_bytes = xlog_space_left(log, cycle, bytes);
828 do {
829 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
830 need_bytes = tic->t_unit_res*tic->t_cnt;
831 else
832 need_bytes = tic->t_unit_res;
833 if (free_bytes < need_bytes && tail_lsn != 1)
834 break;
835 tail_lsn = 0;
836 free_bytes -= need_bytes;
837 sv_signal(&tic->t_sema);
838 tic = tic->t_next;
839 } while (tic != log->l_reserve_headq);
840 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000841 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842} /* xfs_log_move_tail */
843
844/*
845 * Determine if we have a transaction that has gone to disk
846 * that needs to be covered. Log activity needs to be idle (no AIL and
847 * nothing in the iclogs). And, we need to be in the right state indicating
848 * something has gone out.
849 */
850int
851xfs_log_need_covered(xfs_mount_t *mp)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int needed = 0, gen;
854 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
David Chinner92821e22007-05-24 15:26:31 +1000856 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return 0;
858
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000859 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
861 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
862 && !xfs_trans_first_ail(mp, &gen)
863 && xlog_iclogs_empty(log)) {
864 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
865 log->l_covered_state = XLOG_STATE_COVER_DONE;
866 else {
867 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
868 log->l_covered_state = XLOG_STATE_COVER_DONE2;
869 }
870 needed = 1;
871 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000872 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100873 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/******************************************************************************
877 *
878 * local routines
879 *
880 ******************************************************************************
881 */
882
883/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
884 * The log manager must keep track of the last LR which was committed
885 * to disk. The lsn of this LR will become the new tail_lsn whenever
886 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
887 * the situation where stuff could be written into the log but nothing
888 * was ever in the AIL when asked. Eventually, we panic since the
889 * tail hits the head.
890 *
891 * We may be holding the log iclog lock upon entering this routine.
892 */
893xfs_lsn_t
894xlog_assign_tail_lsn(xfs_mount_t *mp)
895{
896 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xlog_t *log = mp->m_log;
898
899 tail_lsn = xfs_trans_tail_ail(mp);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000900 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (tail_lsn != 0) {
902 log->l_tail_lsn = tail_lsn;
903 } else {
904 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
905 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000906 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 return tail_lsn;
909} /* xlog_assign_tail_lsn */
910
911
912/*
913 * Return the space in the log between the tail and the head. The head
914 * is passed in the cycle/bytes formal parms. In the special case where
915 * the reserve head has wrapped passed the tail, this calculation is no
916 * longer valid. In this case, just return 0 which means there is no space
917 * in the log. This works for all places where this function is called
918 * with the reserve head. Of course, if the write head were to ever
919 * wrap the tail, we should blow up. Rather than catch this case here,
920 * we depend on other ASSERTions in other parts of the code. XXXmiken
921 *
922 * This code also handles the case where the reservation head is behind
923 * the tail. The details of this case are described below, but the end
924 * result is that we return the size of the log as the amount of space left.
925 */
David Chinnera8272ce2007-11-23 16:28:09 +1100926STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927xlog_space_left(xlog_t *log, int cycle, int bytes)
928{
929 int free_bytes;
930 int tail_bytes;
931 int tail_cycle;
932
933 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
934 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
935 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
936 free_bytes = log->l_logsize - (bytes - tail_bytes);
937 } else if ((tail_cycle + 1) < cycle) {
938 return 0;
939 } else if (tail_cycle < cycle) {
940 ASSERT(tail_cycle == (cycle - 1));
941 free_bytes = tail_bytes - bytes;
942 } else {
943 /*
944 * The reservation head is behind the tail.
945 * In this case we just want to return the size of the
946 * log as the amount of space left.
947 */
948 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
949 "xlog_space_left: head behind tail\n"
950 " tail_cycle = %d, tail_bytes = %d\n"
951 " GH cycle = %d, GH bytes = %d",
952 tail_cycle, tail_bytes, cycle, bytes);
953 ASSERT(0);
954 free_bytes = log->l_logsize;
955 }
956 return free_bytes;
957} /* xlog_space_left */
958
959
960/*
961 * Log function which is called when an io completes.
962 *
963 * The log manager needs its own routine, in order to control what
964 * happens with the buffer after the write completes.
965 */
966void
967xlog_iodone(xfs_buf_t *bp)
968{
969 xlog_in_core_t *iclog;
970 xlog_t *l;
971 int aborted;
972
973 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
974 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
975 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
976 aborted = 0;
977
978 /*
979 * Some versions of cpp barf on the recursive definition of
980 * ic_log -> hic_fields.ic_log and expand ic_log twice when
981 * it is passed through two macros. Workaround broken cpp.
982 */
983 l = iclog->ic_log;
984
985 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000986 * If the ordered flag has been removed by a lower
987 * layer, it means the underlyin device no longer supports
988 * barrier I/O. Warn loudly and turn off barriers.
989 */
990 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
991 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
992 xfs_fs_cmn_err(CE_WARN, l->l_mp,
993 "xlog_iodone: Barriers are no longer supported"
994 " by device. Disabling barriers\n");
995 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
996 }
997
998 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * Race to shutdown the filesystem if we see an error.
1000 */
1001 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
1002 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
1003 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
1004 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001005 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /*
1007 * This flag will be propagated to the trans-committed
1008 * callback routines to let them know that the log-commit
1009 * didn't succeed.
1010 */
1011 aborted = XFS_LI_ABORTED;
1012 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1013 aborted = XFS_LI_ABORTED;
1014 }
David Chinner3db296f2007-05-14 18:24:16 +10001015
1016 /* log I/O is always issued ASYNC */
1017 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +10001019 /*
1020 * do not reference the buffer (bp) here as we could race
1021 * with it being freed after writing the unmount record to the
1022 * log.
1023 */
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025} /* xlog_iodone */
1026
1027/*
1028 * The bdstrat callback function for log bufs. This gives us a central
1029 * place to trap bufs in case we get hit by a log I/O error and need to
1030 * shutdown. Actually, in practice, even when we didn't get a log error,
1031 * we transition the iclogs to IOERROR state *after* flushing all existing
1032 * iclogs to disk. This is because we don't want anymore new transactions to be
1033 * started or completed afterwards.
1034 */
1035STATIC int
1036xlog_bdstrat_cb(struct xfs_buf *bp)
1037{
1038 xlog_in_core_t *iclog;
1039
1040 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1041
1042 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1043 /* note for irix bstrat will need struct bdevsw passed
1044 * Fix the following macro if the code ever is merged
1045 */
1046 XFS_bdstrat(bp);
1047 return 0;
1048 }
1049
1050 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1051 XFS_BUF_ERROR(bp, EIO);
1052 XFS_BUF_STALE(bp);
1053 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001054 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056
1057}
1058
1059/*
1060 * Return size of each in-core log record buffer.
1061 *
Eric Sandeen1cb51252007-08-16 16:24:43 +10001062 * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 *
1064 * If the filesystem blocksize is too large, we may need to choose a
1065 * larger size since the directory code currently logs entire blocks.
1066 */
1067
1068STATIC void
1069xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1070 xlog_t *log)
1071{
1072 int size;
1073 int xhdrs;
1074
Eric Sandeen1cb51252007-08-16 16:24:43 +10001075 if (mp->m_logbufs <= 0)
1076 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1077 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001078 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /*
1081 * Buffer size passed in from mount system call.
1082 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001083 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 size = log->l_iclog_size = mp->m_logbsize;
1085 log->l_iclog_size_log = 0;
1086 while (size != 1) {
1087 log->l_iclog_size_log++;
1088 size >>= 1;
1089 }
1090
Eric Sandeen62118702008-03-06 13:44:28 +11001091 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* # headers = size / 32K
1093 * one header holds cycles from 32K of data
1094 */
1095
1096 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1097 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1098 xhdrs++;
1099 log->l_iclog_hsize = xhdrs << BBSHIFT;
1100 log->l_iclog_heads = xhdrs;
1101 } else {
1102 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1103 log->l_iclog_hsize = BBSIZE;
1104 log->l_iclog_heads = 1;
1105 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001106 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
Eric Sandeen1cb51252007-08-16 16:24:43 +10001109 /* All machines use 32KB buffers by default. */
1110 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1111 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /* the default log size is 16k or 32k which is one header sector */
1114 log->l_iclog_hsize = BBSIZE;
1115 log->l_iclog_heads = 1;
1116
1117 /*
1118 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1119 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1120 */
1121 if (mp->m_sb.sb_blocksize >= 16*1024) {
1122 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1123 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001124 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 switch (mp->m_sb.sb_blocksize) {
1126 case 16*1024: /* 16 KB */
1127 log->l_iclog_bufs = 3;
1128 break;
1129 case 32*1024: /* 32 KB */
1130 log->l_iclog_bufs = 4;
1131 break;
1132 case 64*1024: /* 64 KB */
1133 log->l_iclog_bufs = 8;
1134 break;
1135 default:
1136 xlog_panic("XFS: Invalid blocksize");
1137 break;
1138 }
1139 }
1140 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001141
1142done: /* are we being asked to make the sizes selected above visible? */
1143 if (mp->m_logbufs == 0)
1144 mp->m_logbufs = log->l_iclog_bufs;
1145 if (mp->m_logbsize == 0)
1146 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147} /* xlog_get_iclog_buffer_size */
1148
1149
1150/*
1151 * This routine initializes some of the log structure for a given mount point.
1152 * Its primary purpose is to fill in enough, so recovery can occur. However,
1153 * some other stuff may be filled in too.
1154 */
1155STATIC xlog_t *
1156xlog_alloc_log(xfs_mount_t *mp,
1157 xfs_buftarg_t *log_target,
1158 xfs_daddr_t blk_offset,
1159 int num_bblks)
1160{
1161 xlog_t *log;
1162 xlog_rec_header_t *head;
1163 xlog_in_core_t **iclogp;
1164 xlog_in_core_t *iclog, *prev_iclog=NULL;
1165 xfs_buf_t *bp;
1166 int i;
1167 int iclogsize;
1168
1169 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1170
1171 log->l_mp = mp;
1172 log->l_targ = log_target;
1173 log->l_logsize = BBTOB(num_bblks);
1174 log->l_logBBstart = blk_offset;
1175 log->l_logBBsize = num_bblks;
1176 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1177 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1178
1179 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001180 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1182 log->l_last_sync_lsn = log->l_tail_lsn;
1183 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1184 log->l_grant_reserve_cycle = 1;
1185 log->l_grant_write_cycle = 1;
1186
Eric Sandeen62118702008-03-06 13:44:28 +11001187 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1189 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1190 /* for larger sector sizes, must have v2 or external log */
1191 ASSERT(log->l_sectbb_log == 0 ||
1192 log->l_logBBstart == 0 ||
Eric Sandeen62118702008-03-06 13:44:28 +11001193 xfs_sb_version_haslogv2(&mp->m_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1195 }
1196 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1197
1198 xlog_get_iclog_buffer_size(mp, log);
1199
1200 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1201 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1202 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1203 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1204 ASSERT(XFS_BUF_ISBUSY(bp));
1205 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1206 log->l_xbuf = bp;
1207
Eric Sandeen007c61c2007-10-11 17:43:56 +10001208 spin_lock_init(&log->l_icloglock);
1209 spin_lock_init(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 initnsema(&log->l_flushsema, 0, "ic-flush");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1213 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1214
1215 iclogp = &log->l_iclog;
1216 /*
1217 * The amount of memory to allocate for the iclog structure is
1218 * rather funky due to the way the structure is defined. It is
1219 * done this way so that we can use different sizes for machines
1220 * with different amounts of memory. See the definition of
1221 * xlog_in_core_t in xfs_log_priv.h for details.
1222 */
1223 iclogsize = log->l_iclog_size;
1224 ASSERT(log->l_iclog_size >= 4096);
1225 for (i=0; i < log->l_iclog_bufs; i++) {
1226 *iclogp = (xlog_in_core_t *)
1227 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1228 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 iclog->ic_prev = prev_iclog;
1230 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001231
1232 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1233 if (!XFS_BUF_CPSEMA(bp))
1234 ASSERT(0);
1235 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1236 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1237 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1238 iclog->ic_bp = bp;
1239 iclog->hic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001240#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001242#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 head = &iclog->ic_header;
1244 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001245 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1246 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001247 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001248 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001250 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1254 iclog->ic_state = XLOG_STATE_ACTIVE;
1255 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001256 atomic_set(&iclog->ic_refcnt, 0);
1257 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 iclog->ic_callback_tail = &(iclog->ic_callback);
1259 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1260
1261 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1262 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1263 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1264 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1265
1266 iclogp = &iclog->ic_next;
1267 }
1268 *iclogp = log->l_iclog; /* complete ring */
1269 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1270
1271 return log;
1272} /* xlog_alloc_log */
1273
1274
1275/*
1276 * Write out the commit record of a transaction associated with the given
1277 * ticket. Return the lsn of the commit record.
1278 */
1279STATIC int
1280xlog_commit_record(xfs_mount_t *mp,
1281 xlog_ticket_t *ticket,
1282 xlog_in_core_t **iclog,
1283 xfs_lsn_t *commitlsnp)
1284{
1285 int error;
1286 xfs_log_iovec_t reg[1];
1287
1288 reg[0].i_addr = NULL;
1289 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001290 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 ASSERT_ALWAYS(iclog);
1293 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1294 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001295 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298} /* xlog_commit_record */
1299
1300
1301/*
1302 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1303 * log space. This code pushes on the lsn which would supposedly free up
1304 * the 25% which we want to leave free. We may need to adopt a policy which
1305 * pushes on an lsn which is further along in the log once we reach the high
1306 * water mark. In this manner, we would be creating a low water mark.
1307 */
David Chinnera8272ce2007-11-23 16:28:09 +11001308STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309xlog_grant_push_ail(xfs_mount_t *mp,
1310 int need_bytes)
1311{
1312 xlog_t *log = mp->m_log; /* pointer to the log */
1313 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1314 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1315 int free_blocks; /* free blocks left to write to */
1316 int free_bytes; /* free bytes left to write to */
1317 int threshold_block; /* block in lsn we'd like to be at */
1318 int threshold_cycle; /* lsn cycle we'd like to be at */
1319 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1322
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001323 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 free_bytes = xlog_space_left(log,
1325 log->l_grant_reserve_cycle,
1326 log->l_grant_reserve_bytes);
1327 tail_lsn = log->l_tail_lsn;
1328 free_blocks = BTOBBT(free_bytes);
1329
1330 /*
1331 * Set the threshold for the minimum number of free blocks in the
1332 * log to the maximum of what the caller needs, one quarter of the
1333 * log, and 256 blocks.
1334 */
1335 free_threshold = BTOBB(need_bytes);
1336 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1337 free_threshold = MAX(free_threshold, 256);
1338 if (free_blocks < free_threshold) {
1339 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1340 threshold_cycle = CYCLE_LSN(tail_lsn);
1341 if (threshold_block >= log->l_logBBsize) {
1342 threshold_block -= log->l_logBBsize;
1343 threshold_cycle += 1;
1344 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001345 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /* Don't pass in an lsn greater than the lsn of the last
1348 * log record known to be on disk.
1349 */
1350 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1351 threshold_lsn = log->l_last_sync_lsn;
1352 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001353 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /*
1356 * Get the transaction layer to kick the dirty buffers out to
1357 * disk asynchronously. No point in trying to do this if
1358 * the filesystem is shutting down.
1359 */
1360 if (threshold_lsn &&
1361 !XLOG_FORCED_SHUTDOWN(log))
1362 xfs_trans_push_ail(mp, threshold_lsn);
1363} /* xlog_grant_push_ail */
1364
1365
1366/*
1367 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1368 * fashion. Previously, we should have moved the current iclog
1369 * ptr in the log to point to the next available iclog. This allows further
1370 * write to continue while this code syncs out an iclog ready to go.
1371 * Before an in-core log can be written out, the data section must be scanned
1372 * to save away the 1st word of each BBSIZE block into the header. We replace
1373 * it with the current cycle count. Each BBSIZE block is tagged with the
1374 * cycle count because there in an implicit assumption that drives will
1375 * guarantee that entire 512 byte blocks get written at once. In other words,
1376 * we can't have part of a 512 byte block written and part not written. By
1377 * tagging each block, we will know which blocks are valid when recovering
1378 * after an unclean shutdown.
1379 *
1380 * This routine is single threaded on the iclog. No other thread can be in
1381 * this routine with the same iclog. Changing contents of iclog can there-
1382 * fore be done without grabbing the state machine lock. Updating the global
1383 * log will require grabbing the lock though.
1384 *
1385 * The entire log manager uses a logical block numbering scheme. Only
1386 * log_sync (and then only bwrite()) know about the fact that the log may
1387 * not start with block zero on a given device. The log block start offset
1388 * is added immediately before calling bwrite().
1389 */
1390
David Chinnera8272ce2007-11-23 16:28:09 +11001391STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392xlog_sync(xlog_t *log,
1393 xlog_in_core_t *iclog)
1394{
1395 xfs_caddr_t dptr; /* pointer to byte sized element */
1396 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001397 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 uint count; /* byte count of bwrite */
1399 uint count_init; /* initial count before roundup */
1400 int roundoff; /* roundoff to BB or stripe */
1401 int split = 0; /* split write into two regions */
1402 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001403 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001406 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /* Add for LR header */
1409 count_init = log->l_iclog_hsize + iclog->ic_offset;
1410
1411 /* Round out the log write size */
1412 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1413 /* we have a v2 stripe unit to use */
1414 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1415 } else {
1416 count = BBTOB(BTOBB(count_init));
1417 }
1418 roundoff = count - count_init;
1419 ASSERT(roundoff >= 0);
1420 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1421 roundoff < log->l_mp->m_sb.sb_logsunit)
1422 ||
1423 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1424 roundoff < BBTOB(1)));
1425
1426 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001427 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001428 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001429 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* put cycle number in every block */
1432 xlog_pack_data(log, iclog, roundoff);
1433
1434 /* real byte length */
1435 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001436 iclog->ic_header.h_len =
1437 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001439 iclog->ic_header.h_len =
1440 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442
Nathan Scottf5faad72006-07-28 17:04:44 +10001443 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1445 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001446 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1449
1450 /* Do we need to split this write into 2 parts? */
1451 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1452 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1453 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1454 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1455 } else {
1456 iclog->ic_bwritecnt = 1;
1457 }
David Chinner511105b2007-05-24 15:21:57 +10001458 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001460 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 XFS_BUF_BUSY(bp);
1462 XFS_BUF_ASYNC(bp);
1463 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001464 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001465 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001467 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001468 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1471 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1472
1473 xlog_verify_iclog(log, iclog, count, B_TRUE);
1474
1475 /* account for log which doesn't start at block #0 */
1476 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1477 /*
1478 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1479 * is shutting down.
1480 */
1481 XFS_BUF_WRITE(bp);
1482
1483 if ((error = XFS_bwrite(bp))) {
1484 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1485 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001486 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001489 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1491 (unsigned long)1);
1492 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1493 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1494 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1495 (__psint_t)count), split);
1496 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001497 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 XFS_BUF_BUSY(bp);
1499 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001500 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1501 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 dptr = XFS_BUF_PTR(bp);
1503 /*
1504 * Bump the cycle numbers at the start of each block
1505 * since this part of the buffer is at the start of
1506 * a new cycle. Watch out for the header magic number
1507 * case, though.
1508 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001509 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001510 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001511 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001512 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 dptr += BBSIZE;
1514 }
1515
1516 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1517 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1518
Nathan Scottc41564b2006-03-29 08:55:14 +10001519 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1521 XFS_BUF_WRITE(bp);
1522 if ((error = XFS_bwrite(bp))) {
1523 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1524 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001525 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529} /* xlog_sync */
1530
1531
1532/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001533 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
David Chinnera8272ce2007-11-23 16:28:09 +11001535STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001536xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int i;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 iclog = log->l_iclog;
1542 for (i=0; i<log->l_iclog_bufs; i++) {
1543 sv_destroy(&iclog->ic_forcesema);
1544 sv_destroy(&iclog->ic_writesema);
1545 xfs_buf_free(iclog->ic_bp);
1546#ifdef XFS_LOG_TRACE
1547 if (iclog->ic_trace != NULL) {
1548 ktrace_free(iclog->ic_trace);
1549 }
1550#endif
1551 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 kmem_free(iclog, sizeof(xlog_in_core_t));
1553 iclog = next_iclog;
1554 }
1555 freesema(&log->l_flushsema);
1556 spinlock_destroy(&log->l_icloglock);
1557 spinlock_destroy(&log->l_grant_lock);
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 xfs_buf_free(log->l_xbuf);
1560#ifdef XFS_LOG_TRACE
1561 if (log->l_trace != NULL) {
1562 ktrace_free(log->l_trace);
1563 }
1564 if (log->l_grant_trace != NULL) {
1565 ktrace_free(log->l_grant_trace);
1566 }
1567#endif
1568 log->l_mp->m_log = NULL;
1569 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001570} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572/*
1573 * Update counters atomically now that memcpy is done.
1574 */
1575/* ARGSUSED */
1576static inline void
1577xlog_state_finish_copy(xlog_t *log,
1578 xlog_in_core_t *iclog,
1579 int record_cnt,
1580 int copy_bytes)
1581{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001582 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001584 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 iclog->ic_offset += copy_bytes;
1586
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001587 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588} /* xlog_state_finish_copy */
1589
1590
1591
1592
1593/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001594 * print out info relating to regions written which consume
1595 * the reservation
1596 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001597STATIC void
1598xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1599{
1600 uint i;
1601 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1602
1603 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1604 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1605 "bformat",
1606 "bchunk",
1607 "efi_format",
1608 "efd_format",
1609 "iformat",
1610 "icore",
1611 "iext",
1612 "ibroot",
1613 "ilocal",
1614 "iattr_ext",
1615 "iattr_broot",
1616 "iattr_local",
1617 "qformat",
1618 "dquot",
1619 "quotaoff",
1620 "LR header",
1621 "unmount",
1622 "commit",
1623 "trans header"
1624 };
1625 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1626 "SETATTR_NOT_SIZE",
1627 "SETATTR_SIZE",
1628 "INACTIVE",
1629 "CREATE",
1630 "CREATE_TRUNC",
1631 "TRUNCATE_FILE",
1632 "REMOVE",
1633 "LINK",
1634 "RENAME",
1635 "MKDIR",
1636 "RMDIR",
1637 "SYMLINK",
1638 "SET_DMATTRS",
1639 "GROWFS",
1640 "STRAT_WRITE",
1641 "DIOSTRAT",
1642 "WRITE_SYNC",
1643 "WRITEID",
1644 "ADDAFORK",
1645 "ATTRINVAL",
1646 "ATRUNCATE",
1647 "ATTR_SET",
1648 "ATTR_RM",
1649 "ATTR_FLAG",
1650 "CLEAR_AGI_BUCKET",
1651 "QM_SBCHANGE",
1652 "DUMMY1",
1653 "DUMMY2",
1654 "QM_QUOTAOFF",
1655 "QM_DQALLOC",
1656 "QM_SETQLIM",
1657 "QM_DQCLUSTER",
1658 "QM_QINOCREATE",
1659 "QM_QUOTAOFF_END",
1660 "SB_UNIT",
1661 "FSYNC_TS",
1662 "GROWFSRT_ALLOC",
1663 "GROWFSRT_ZERO",
1664 "GROWFSRT_FREE",
1665 "SWAPEXT"
1666 };
1667
1668 xfs_fs_cmn_err(CE_WARN, mp,
1669 "xfs_log_write: reservation summary:\n"
1670 " trans type = %s (%u)\n"
1671 " unit res = %d bytes\n"
1672 " current res = %d bytes\n"
1673 " total reg = %u bytes (o/flow = %u bytes)\n"
1674 " ophdrs = %u (ophdr space = %u bytes)\n"
1675 " ophdr + reg = %u bytes\n"
1676 " num regions = %u\n",
1677 ((ticket->t_trans_type <= 0 ||
1678 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1679 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1680 ticket->t_trans_type,
1681 ticket->t_unit_res,
1682 ticket->t_curr_res,
1683 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1684 ticket->t_res_num_ophdrs, ophdr_spc,
1685 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001686 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001687 ticket->t_res_num);
1688
1689 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001690 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001691 cmn_err(CE_WARN,
1692 "region[%u]: %s - %u bytes\n",
1693 i,
1694 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1695 "bad-rtype" : res_type_str[r_type-1]),
1696 ticket->t_res_arr[i].r_len);
1697 }
1698}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001699
1700/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * Write some region out to in-core log
1702 *
1703 * This will be called when writing externally provided regions or when
1704 * writing out a commit record for a given transaction.
1705 *
1706 * General algorithm:
1707 * 1. Find total length of this write. This may include adding to the
1708 * lengths passed in.
1709 * 2. Check whether we violate the tickets reservation.
1710 * 3. While writing to this iclog
1711 * A. Reserve as much space in this iclog as can get
1712 * B. If this is first write, save away start lsn
1713 * C. While writing this region:
1714 * 1. If first write of transaction, write start record
1715 * 2. Write log operation header (header per region)
1716 * 3. Find out if we can fit entire region into this iclog
1717 * 4. Potentially, verify destination memcpy ptr
1718 * 5. Memcpy (partial) region
1719 * 6. If partial copy, release iclog; otherwise, continue
1720 * copying more regions into current iclog
1721 * 4. Mark want sync bit (in simulation mode)
1722 * 5. Release iclog for potential flush to on-disk log.
1723 *
1724 * ERRORS:
1725 * 1. Panic if reservation is overrun. This should never happen since
1726 * reservation amounts are generated internal to the filesystem.
1727 * NOTES:
1728 * 1. Tickets are single threaded data structures.
1729 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1730 * syncing routine. When a single log_write region needs to span
1731 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1732 * on all log operation writes which don't contain the end of the
1733 * region. The XLOG_END_TRANS bit is used for the in-core log
1734 * operation which contains the end of the continued log_write region.
1735 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1736 * we don't really know exactly how much space will be used. As a result,
1737 * we don't update ic_offset until the end when we know exactly how many
1738 * bytes have been written out.
1739 */
David Chinnera8272ce2007-11-23 16:28:09 +11001740STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741xlog_write(xfs_mount_t * mp,
1742 xfs_log_iovec_t reg[],
1743 int nentries,
1744 xfs_log_ticket_t tic,
1745 xfs_lsn_t *start_lsn,
1746 xlog_in_core_t **commit_iclog,
1747 uint flags)
1748{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001749 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001751 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 __psint_t ptr; /* copy address into data region */
1754 int len; /* # xlog_write() bytes 2 still copy */
1755 int index; /* region index currently copying */
1756 int log_offset; /* offset (from 0) into data region */
1757 int start_rec_copy; /* # bytes to copy for start record */
1758 int partial_copy; /* did we split a region? */
1759 int partial_copy_len;/* # bytes copied if split region */
1760 int need_copy; /* # bytes need to memcpy this region */
1761 int copy_len; /* # bytes actually memcpy'ing */
1762 int copy_off; /* # bytes from entry start */
1763 int contwr; /* continued write of in-core log? */
1764 int error;
1765 int record_cnt = 0, data_cnt = 0;
1766
1767 partial_copy_len = partial_copy = 0;
1768
1769 /* Calculate potential maximum space. Each region gets its own
1770 * xlog_op_header_t and may need to be double word aligned.
1771 */
1772 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001773 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001775 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 for (index = 0; index < nentries; index++) {
1779 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001780 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001782 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 contwr = *start_lsn = 0;
1785
1786 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001787 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788#ifdef DEBUG
1789 xlog_panic(
1790 "xfs_log_write: reservation ran out. Need to up reservation");
1791#else
1792 /* Customer configurable panic */
1793 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1794 "xfs_log_write: reservation ran out. Need to up reservation");
1795 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001796 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797#endif
1798 } else
1799 ticket->t_curr_res -= len;
1800
1801 for (index = 0; index < nentries; ) {
1802 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1803 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001804 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 ASSERT(log_offset <= iclog->ic_size - 1);
1807 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1808
1809 /* start_lsn is the first lsn written to. That's all we need. */
1810 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001811 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /* This loop writes out as many regions as can fit in the amount
1814 * of space which was allocated by xlog_state_get_iclog_space().
1815 */
1816 while (index < nentries) {
1817 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1818 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1819 start_rec_copy = 0;
1820
1821 /* If first write for transaction, insert start record.
1822 * We can't be trying to commit if we are inited. We can't
1823 * have any "partial_copy" if we are inited.
1824 */
1825 if (ticket->t_flags & XLOG_TIC_INITED) {
1826 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001827 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 logop_head->oh_clientid = ticket->t_clientid;
1829 logop_head->oh_len = 0;
1830 logop_head->oh_flags = XLOG_START_TRANS;
1831 logop_head->oh_res2 = 0;
1832 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1833 record_cnt++;
1834
1835 start_rec_copy = sizeof(xlog_op_header_t);
1836 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1837 }
1838
1839 /* Copy log operation header directly into data section */
1840 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001841 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 logop_head->oh_clientid = ticket->t_clientid;
1843 logop_head->oh_res2 = 0;
1844
1845 /* header copied directly */
1846 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1847
1848 /* are we copying a commit or unmount record? */
1849 logop_head->oh_flags = flags;
1850
1851 /*
1852 * We've seen logs corrupted with bad transaction client
1853 * ids. This makes sure that XFS doesn't generate them on.
1854 * Turn this into an EIO and shut down the filesystem.
1855 */
1856 switch (logop_head->oh_clientid) {
1857 case XFS_TRANSACTION:
1858 case XFS_VOLUME:
1859 case XFS_LOG:
1860 break;
1861 default:
1862 xfs_fs_cmn_err(CE_WARN, mp,
1863 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1864 logop_head->oh_clientid, tic);
1865 return XFS_ERROR(EIO);
1866 }
1867
1868 /* Partial write last time? => (partial_copy != 0)
1869 * need_copy is the amount we'd like to copy if everything could
1870 * fit in the current memcpy.
1871 */
1872 need_copy = reg[index].i_len - partial_copy_len;
1873
1874 copy_off = partial_copy_len;
1875 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001876 copy_len = need_copy;
1877 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (partial_copy)
1879 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1880 partial_copy_len = partial_copy = 0;
1881 } else { /* partial write */
1882 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001883 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1885 if (partial_copy)
1886 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1887 partial_copy_len += copy_len;
1888 partial_copy++;
1889 len += sizeof(xlog_op_header_t); /* from splitting of region */
1890 /* account for new log op header */
1891 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001892 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 xlog_verify_dest_ptr(log, ptr);
1895
1896 /* copy region */
1897 ASSERT(copy_len >= 0);
1898 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1899 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1900
1901 /* make copy_len total bytes copied, including headers */
1902 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1903 record_cnt++;
1904 data_cnt += contwr ? copy_len : 0;
1905 if (partial_copy) { /* copied partial region */
1906 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1907 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1908 record_cnt = data_cnt = 0;
1909 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001910 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 break; /* don't increment index */
1912 } else { /* copied entire region */
1913 index++;
1914 partial_copy_len = partial_copy = 0;
1915
1916 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1917 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1918 record_cnt = data_cnt = 0;
1919 xlog_state_want_sync(log, iclog);
1920 if (commit_iclog) {
1921 ASSERT(flags & XLOG_COMMIT_TRANS);
1922 *commit_iclog = iclog;
1923 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001924 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (index == nentries)
1926 return 0; /* we are done */
1927 else
1928 break;
1929 }
1930 } /* if (partial_copy) */
1931 } /* while (index < nentries) */
1932 } /* for (index = 0; index < nentries; ) */
1933 ASSERT(len == 0);
1934
1935 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1936 if (commit_iclog) {
1937 ASSERT(flags & XLOG_COMMIT_TRANS);
1938 *commit_iclog = iclog;
1939 return 0;
1940 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001941 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942} /* xlog_write */
1943
1944
1945/*****************************************************************************
1946 *
1947 * State Machine functions
1948 *
1949 *****************************************************************************
1950 */
1951
1952/* Clean iclogs starting from the head. This ordering must be
1953 * maintained, so an iclog doesn't become ACTIVE beyond one that
1954 * is SYNCING. This is also required to maintain the notion that we use
1955 * a counting semaphore to hold off would be writers to the log when every
1956 * iclog is trying to sync to disk.
1957 *
1958 * State Change: DIRTY -> ACTIVE
1959 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001960STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961xlog_state_clean_log(xlog_t *log)
1962{
1963 xlog_in_core_t *iclog;
1964 int changed = 0;
1965
1966 iclog = log->l_iclog;
1967 do {
1968 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1969 iclog->ic_state = XLOG_STATE_ACTIVE;
1970 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001971 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
1973 * If the number of ops in this iclog indicate it just
1974 * contains the dummy transaction, we can
1975 * change state into IDLE (the second time around).
1976 * Otherwise we should change the state into
1977 * NEED a dummy.
1978 * We don't need to cover the dummy.
1979 */
1980 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001981 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1982 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 changed = 1;
1984 } else {
1985 /*
1986 * We have two dirty iclogs so start over
1987 * This could also be num of ops indicates
1988 * this is not the dummy going out.
1989 */
1990 changed = 2;
1991 }
1992 iclog->ic_header.h_num_logops = 0;
1993 memset(iclog->ic_header.h_cycle_data, 0,
1994 sizeof(iclog->ic_header.h_cycle_data));
1995 iclog->ic_header.h_lsn = 0;
1996 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1997 /* do nothing */;
1998 else
1999 break; /* stop cleaning */
2000 iclog = iclog->ic_next;
2001 } while (iclog != log->l_iclog);
2002
2003 /* log is locked when we are called */
2004 /*
2005 * Change state for the dummy log recording.
2006 * We usually go to NEED. But we go to NEED2 if the changed indicates
2007 * we are done writing the dummy record.
2008 * If we are done with the second dummy recored (DONE2), then
2009 * we go to IDLE.
2010 */
2011 if (changed) {
2012 switch (log->l_covered_state) {
2013 case XLOG_STATE_COVER_IDLE:
2014 case XLOG_STATE_COVER_NEED:
2015 case XLOG_STATE_COVER_NEED2:
2016 log->l_covered_state = XLOG_STATE_COVER_NEED;
2017 break;
2018
2019 case XLOG_STATE_COVER_DONE:
2020 if (changed == 1)
2021 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2022 else
2023 log->l_covered_state = XLOG_STATE_COVER_NEED;
2024 break;
2025
2026 case XLOG_STATE_COVER_DONE2:
2027 if (changed == 1)
2028 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2029 else
2030 log->l_covered_state = XLOG_STATE_COVER_NEED;
2031 break;
2032
2033 default:
2034 ASSERT(0);
2035 }
2036 }
2037} /* xlog_state_clean_log */
2038
2039STATIC xfs_lsn_t
2040xlog_get_lowest_lsn(
2041 xlog_t *log)
2042{
2043 xlog_in_core_t *lsn_log;
2044 xfs_lsn_t lowest_lsn, lsn;
2045
2046 lsn_log = log->l_iclog;
2047 lowest_lsn = 0;
2048 do {
2049 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002050 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if ((lsn && !lowest_lsn) ||
2052 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2053 lowest_lsn = lsn;
2054 }
2055 }
2056 lsn_log = lsn_log->ic_next;
2057 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002058 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061
2062STATIC void
2063xlog_state_do_callback(
2064 xlog_t *log,
2065 int aborted,
2066 xlog_in_core_t *ciclog)
2067{
2068 xlog_in_core_t *iclog;
2069 xlog_in_core_t *first_iclog; /* used to know when we've
2070 * processed all iclogs once */
2071 xfs_log_callback_t *cb, *cb_next;
2072 int flushcnt = 0;
2073 xfs_lsn_t lowest_lsn;
2074 int ioerrors; /* counter: iclogs with errors */
2075 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2076 int funcdidcallbacks; /* flag: function did callbacks */
2077 int repeats; /* for issuing console warnings if
2078 * looping too many times */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002080 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 first_iclog = iclog = log->l_iclog;
2082 ioerrors = 0;
2083 funcdidcallbacks = 0;
2084 repeats = 0;
2085
2086 do {
2087 /*
2088 * Scan all iclogs starting with the one pointed to by the
2089 * log. Reset this starting point each time the log is
2090 * unlocked (during callbacks).
2091 *
2092 * Keep looping through iclogs until one full pass is made
2093 * without running any callbacks.
2094 */
2095 first_iclog = log->l_iclog;
2096 iclog = log->l_iclog;
2097 loopdidcallbacks = 0;
2098 repeats++;
2099
2100 do {
2101
2102 /* skip all iclogs in the ACTIVE & DIRTY states */
2103 if (iclog->ic_state &
2104 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2105 iclog = iclog->ic_next;
2106 continue;
2107 }
2108
2109 /*
2110 * Between marking a filesystem SHUTDOWN and stopping
2111 * the log, we do flush all iclogs to disk (if there
2112 * wasn't a log I/O error). So, we do want things to
2113 * go smoothly in case of just a SHUTDOWN w/o a
2114 * LOG_IO_ERROR.
2115 */
2116 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2117 /*
2118 * Can only perform callbacks in order. Since
2119 * this iclog is not in the DONE_SYNC/
2120 * DO_CALLBACK state, we skip the rest and
2121 * just try to clean up. If we set our iclog
2122 * to DO_CALLBACK, we will not process it when
2123 * we retry since a previous iclog is in the
2124 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002125 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 */
2127 if (!(iclog->ic_state &
2128 (XLOG_STATE_DONE_SYNC |
2129 XLOG_STATE_DO_CALLBACK))) {
2130 if (ciclog && (ciclog->ic_state ==
2131 XLOG_STATE_DONE_SYNC)) {
2132 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2133 }
2134 break;
2135 }
2136 /*
2137 * We now have an iclog that is in either the
2138 * DO_CALLBACK or DONE_SYNC states. The other
2139 * states (WANT_SYNC, SYNCING, or CALLBACK were
2140 * caught by the above if and are going to
2141 * clean (i.e. we aren't doing their callbacks)
2142 * see the above if.
2143 */
2144
2145 /*
2146 * We will do one more check here to see if we
2147 * have chased our tail around.
2148 */
2149
2150 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002151 if (lowest_lsn &&
2152 XFS_LSN_CMP(lowest_lsn,
2153 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 iclog = iclog->ic_next;
2155 continue; /* Leave this iclog for
2156 * another thread */
2157 }
2158
2159 iclog->ic_state = XLOG_STATE_CALLBACK;
2160
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002161 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002164 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * No one else can be here except us.
2166 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002167 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002168 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2169 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2170 log->l_last_sync_lsn =
2171 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002172 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002175 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 ioerrors++;
2177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
David Chinner114d23a2008-04-10 12:18:39 +10002179 /*
2180 * Keep processing entries in the callback list until
2181 * we come around and it is empty. We need to
2182 * atomically see that the list is empty and change the
2183 * state to DIRTY so that we don't miss any more
2184 * callbacks being added.
2185 */
2186 spin_lock(&iclog->ic_callback_lock);
2187 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002188 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 iclog->ic_callback_tail = &(iclog->ic_callback);
2190 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002191 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002194 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 cb_next = cb->cb_next;
2196 cb->cb_func(cb->cb_arg, aborted);
2197 }
David Chinner114d23a2008-04-10 12:18:39 +10002198 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 cb = iclog->ic_callback;
2200 }
2201
2202 loopdidcallbacks++;
2203 funcdidcallbacks++;
2204
David Chinner114d23a2008-04-10 12:18:39 +10002205 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002206 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002207 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2209 iclog->ic_state = XLOG_STATE_DIRTY;
2210
2211 /*
2212 * Transition from DIRTY to ACTIVE if applicable.
2213 * NOP if STATE_IOERROR.
2214 */
2215 xlog_state_clean_log(log);
2216
2217 /* wake up threads waiting in xfs_log_force() */
2218 sv_broadcast(&iclog->ic_forcesema);
2219
2220 iclog = iclog->ic_next;
2221 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002222
2223 if (repeats > 5000) {
2224 flushcnt += repeats;
2225 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002227 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002228 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230 } while (!ioerrors && loopdidcallbacks);
2231
2232 /*
2233 * make one last gasp attempt to see if iclogs are being left in
2234 * limbo..
2235 */
2236#ifdef DEBUG
2237 if (funcdidcallbacks) {
2238 first_iclog = iclog = log->l_iclog;
2239 do {
2240 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2241 /*
2242 * Terminate the loop if iclogs are found in states
2243 * which will cause other threads to clean up iclogs.
2244 *
2245 * SYNCING - i/o completion will go through logs
2246 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002247 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 * IOERROR - give up hope all ye who enter here
2249 */
2250 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2251 iclog->ic_state == XLOG_STATE_SYNCING ||
2252 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2253 iclog->ic_state == XLOG_STATE_IOERROR )
2254 break;
2255 iclog = iclog->ic_next;
2256 } while (first_iclog != iclog);
2257 }
2258#endif
2259
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002260 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2262 flushcnt = log->l_flushcnt;
2263 log->l_flushcnt = 0;
2264 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002265 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 while (flushcnt--)
2267 vsema(&log->l_flushsema);
2268} /* xlog_state_do_callback */
2269
2270
2271/*
2272 * Finish transitioning this iclog to the dirty state.
2273 *
2274 * Make sure that we completely execute this routine only when this is
2275 * the last call to the iclog. There is a good chance that iclog flushes,
2276 * when we reach the end of the physical log, get turned into 2 separate
2277 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2278 * routine. By using the reference count bwritecnt, we guarantee that only
2279 * the second completion goes through.
2280 *
2281 * Callbacks could take time, so they are done outside the scope of the
2282 * global state machine log lock. Assume that the calls to cvsema won't
2283 * take a long time. At least we know it won't sleep.
2284 */
David Chinnera8272ce2007-11-23 16:28:09 +11002285STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286xlog_state_done_syncing(
2287 xlog_in_core_t *iclog,
2288 int aborted)
2289{
2290 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002292 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2295 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002296 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2298
2299
2300 /*
2301 * If we got an error, either on the first buffer, or in the case of
2302 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2303 * and none should ever be attempted to be written to disk
2304 * again.
2305 */
2306 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2307 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002308 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return;
2310 }
2311 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2312 }
2313
2314 /*
2315 * Someone could be sleeping prior to writing out the next
2316 * iclog buffer, we wake them all, one will get to do the
2317 * I/O, the others get to wait for the result.
2318 */
2319 sv_broadcast(&iclog->ic_writesema);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002320 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2322} /* xlog_state_done_syncing */
2323
2324
2325/*
2326 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2327 * sleep. The flush semaphore is set to the number of in-core buffers and
2328 * decremented around disk syncing. Therefore, if all buffers are syncing,
2329 * this semaphore will cause new writes to sleep until a sync completes.
2330 * Otherwise, this code just does p() followed by v(). This approximates
2331 * a sleep/wakeup except we can't race.
2332 *
2333 * The in-core logs are used in a circular fashion. They are not used
2334 * out-of-order even when an iclog past the head is free.
2335 *
2336 * return:
2337 * * log_offset where xlog_write() can start writing into the in-core
2338 * log's data space.
2339 * * in-core log pointer to which xlog_write() should write.
2340 * * boolean indicating this is a continued write to an in-core log.
2341 * If this is the last write, then the in-core log's offset field
2342 * needs to be incremented, depending on the amount of data which
2343 * is copied.
2344 */
David Chinnera8272ce2007-11-23 16:28:09 +11002345STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346xlog_state_get_iclog_space(xlog_t *log,
2347 int len,
2348 xlog_in_core_t **iclogp,
2349 xlog_ticket_t *ticket,
2350 int *continued_write,
2351 int *logoffsetp)
2352{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int log_offset;
2354 xlog_rec_header_t *head;
2355 xlog_in_core_t *iclog;
2356 int error;
2357
2358restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002359 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002361 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return XFS_ERROR(EIO);
2363 }
2364
2365 iclog = log->l_iclog;
2366 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2367 log->l_flushcnt++;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002368 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2370 XFS_STATS_INC(xs_log_noiclogs);
2371 /* Ensure that log writes happen */
2372 psema(&log->l_flushsema, PINOD);
2373 goto restart;
2374 }
2375 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2376 head = &iclog->ic_header;
2377
David Chinner155cc6b2008-03-06 13:44:14 +11002378 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 log_offset = iclog->ic_offset;
2380
2381 /* On the 1st write to an iclog, figure out lsn. This works
2382 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2383 * committing to. If the offset is set, that's how many blocks
2384 * must be written.
2385 */
2386 if (log_offset == 0) {
2387 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002388 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002389 log->l_iclog_hsize,
2390 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002391 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2392 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002393 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 ASSERT(log->l_curr_block >= 0);
2395 }
2396
2397 /* If there is enough room to write everything, then do it. Otherwise,
2398 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2399 * bit is on, so this will get flushed out. Don't update ic_offset
2400 * until you know exactly how many bytes get copied. Therefore, wait
2401 * until later to update ic_offset.
2402 *
2403 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2404 * can fit into remaining data section.
2405 */
2406 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2407 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2408
2409 /* If I'm the only one writing to this iclog, sync it to disk */
David Chinner155cc6b2008-03-06 13:44:14 +11002410 if (atomic_read(&iclog->ic_refcnt) == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002411 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002413 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002415 atomic_dec(&iclog->ic_refcnt);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002416 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 goto restart;
2419 }
2420
2421 /* Do we have enough room to write the full amount in the remainder
2422 * of this iclog? Or must we continue a write on the next iclog and
2423 * mark this iclog as completely taken? In the case where we switch
2424 * iclogs (to mark it taken), this particular iclog will release/sync
2425 * to disk in xlog_write().
2426 */
2427 if (len <= iclog->ic_size - iclog->ic_offset) {
2428 *continued_write = 0;
2429 iclog->ic_offset += len;
2430 } else {
2431 *continued_write = 1;
2432 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2433 }
2434 *iclogp = iclog;
2435
2436 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002437 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 *logoffsetp = log_offset;
2440 return 0;
2441} /* xlog_state_get_iclog_space */
2442
2443/*
2444 * Atomically get the log space required for a log ticket.
2445 *
2446 * Once a ticket gets put onto the reserveq, it will only return after
2447 * the needed reservation is satisfied.
2448 */
2449STATIC int
2450xlog_grant_log_space(xlog_t *log,
2451 xlog_ticket_t *tic)
2452{
2453 int free_bytes;
2454 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455#ifdef DEBUG
2456 xfs_lsn_t tail_lsn;
2457#endif
2458
2459
2460#ifdef DEBUG
2461 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2462 panic("grant Recovery problem");
2463#endif
2464
2465 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002466 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2468
2469 /* something is already sleeping; insert new transaction at end */
2470 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002471 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 xlog_trace_loggrant(log, tic,
2473 "xlog_grant_log_space: sleep 1");
2474 /*
2475 * Gotta check this before going to sleep, while we're
2476 * holding the grant lock.
2477 */
2478 if (XLOG_FORCED_SHUTDOWN(log))
2479 goto error_return;
2480
2481 XFS_STATS_INC(xs_sleep_logspace);
2482 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2483 /*
2484 * If we got an error, and the filesystem is shutting down,
2485 * we'll catch it down below. So just continue...
2486 */
2487 xlog_trace_loggrant(log, tic,
2488 "xlog_grant_log_space: wake 1");
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002489 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2492 need_bytes = tic->t_unit_res*tic->t_ocnt;
2493 else
2494 need_bytes = tic->t_unit_res;
2495
2496redo:
2497 if (XLOG_FORCED_SHUTDOWN(log))
2498 goto error_return;
2499
2500 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2501 log->l_grant_reserve_bytes);
2502 if (free_bytes < need_bytes) {
2503 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002504 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 xlog_trace_loggrant(log, tic,
2506 "xlog_grant_log_space: sleep 2");
2507 XFS_STATS_INC(xs_sleep_logspace);
2508 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2509
2510 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002511 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 goto error_return;
2513 }
2514
2515 xlog_trace_loggrant(log, tic,
2516 "xlog_grant_log_space: wake 2");
2517 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002518 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 goto redo;
2520 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002521 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002524 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525#ifdef DEBUG
2526 tail_lsn = log->l_tail_lsn;
2527 /*
2528 * Check to make sure the grant write head didn't just over lap the
2529 * tail. If the cycles are the same, we can't be overlapping.
2530 * Otherwise, make sure that the cycles differ by exactly one and
2531 * check the byte count.
2532 */
2533 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2534 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2535 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2536 }
2537#endif
2538 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2539 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002540 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return 0;
2542
2543 error_return:
2544 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002545 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2547 /*
2548 * If we are failing, make sure the ticket doesn't have any
2549 * current reservations. We don't want to add this back when
2550 * the ticket/transaction gets cancelled.
2551 */
2552 tic->t_curr_res = 0;
2553 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002554 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return XFS_ERROR(EIO);
2556} /* xlog_grant_log_space */
2557
2558
2559/*
2560 * Replenish the byte reservation required by moving the grant write head.
2561 *
2562 *
2563 */
2564STATIC int
2565xlog_regrant_write_log_space(xlog_t *log,
2566 xlog_ticket_t *tic)
2567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 int free_bytes, need_bytes;
2569 xlog_ticket_t *ntic;
2570#ifdef DEBUG
2571 xfs_lsn_t tail_lsn;
2572#endif
2573
2574 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002575 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580#ifdef DEBUG
2581 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2582 panic("regrant Recovery problem");
2583#endif
2584
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002585 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2587
2588 if (XLOG_FORCED_SHUTDOWN(log))
2589 goto error_return;
2590
2591 /* If there are other waiters on the queue then give them a
2592 * chance at logspace before us. Wake up the first waiters,
2593 * if we do not wake up all the waiters then go to sleep waiting
2594 * for more free space, otherwise try to get some space for
2595 * this transaction.
2596 */
2597
2598 if ((ntic = log->l_write_headq)) {
2599 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2600 log->l_grant_write_bytes);
2601 do {
2602 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2603
2604 if (free_bytes < ntic->t_unit_res)
2605 break;
2606 free_bytes -= ntic->t_unit_res;
2607 sv_signal(&ntic->t_sema);
2608 ntic = ntic->t_next;
2609 } while (ntic != log->l_write_headq);
2610
2611 if (ntic != log->l_write_headq) {
2612 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002613 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 xlog_trace_loggrant(log, tic,
2616 "xlog_regrant_write_log_space: sleep 1");
2617 XFS_STATS_INC(xs_sleep_logspace);
2618 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2619 &log->l_grant_lock, s);
2620
2621 /* If we're shutting down, this tic is already
2622 * off the queue */
2623 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002624 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto error_return;
2626 }
2627
2628 xlog_trace_loggrant(log, tic,
2629 "xlog_regrant_write_log_space: wake 1");
2630 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002631 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
2633 }
2634
2635 need_bytes = tic->t_unit_res;
2636
2637redo:
2638 if (XLOG_FORCED_SHUTDOWN(log))
2639 goto error_return;
2640
2641 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2642 log->l_grant_write_bytes);
2643 if (free_bytes < need_bytes) {
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 XFS_STATS_INC(xs_sleep_logspace);
2647 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2648
2649 /* If we're shutting down, this tic is already off the queue */
2650 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002651 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 goto error_return;
2653 }
2654
2655 xlog_trace_loggrant(log, tic,
2656 "xlog_regrant_write_log_space: wake 2");
2657 xlog_grant_push_ail(log->l_mp, need_bytes);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002658 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 goto redo;
2660 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002661 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002663 /* we've got enough space */
2664 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665#ifdef DEBUG
2666 tail_lsn = log->l_tail_lsn;
2667 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2668 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2669 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2670 }
2671#endif
2672
2673 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2674 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002675 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678
2679 error_return:
2680 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002681 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2683 /*
2684 * If we are failing, make sure the ticket doesn't have any
2685 * current reservations. We don't want to add this back when
2686 * the ticket/transaction gets cancelled.
2687 */
2688 tic->t_curr_res = 0;
2689 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002690 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return XFS_ERROR(EIO);
2692} /* xlog_regrant_write_log_space */
2693
2694
2695/* The first cnt-1 times through here we don't need to
2696 * move the grant write head because the permanent
2697 * reservation has reserved cnt times the unit amount.
2698 * Release part of current permanent unit reservation and
2699 * reset current reservation to be one units worth. Also
2700 * move grant reservation head forward.
2701 */
2702STATIC void
2703xlog_regrant_reserve_log_space(xlog_t *log,
2704 xlog_ticket_t *ticket)
2705{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 xlog_trace_loggrant(log, ticket,
2707 "xlog_regrant_reserve_log_space: enter");
2708 if (ticket->t_cnt > 0)
2709 ticket->t_cnt--;
2710
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002711 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002712 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002714 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 xlog_trace_loggrant(log, ticket,
2716 "xlog_regrant_reserve_log_space: sub current res");
2717 xlog_verify_grant_head(log, 1);
2718
2719 /* just return if we still have some of the pre-reserved space */
2720 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002721 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return;
2723 }
2724
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002725 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 xlog_trace_loggrant(log, ticket,
2727 "xlog_regrant_reserve_log_space: exit");
2728 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002729 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002731 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732} /* xlog_regrant_reserve_log_space */
2733
2734
2735/*
2736 * Give back the space left from a reservation.
2737 *
2738 * All the information we need to make a correct determination of space left
2739 * is present. For non-permanent reservations, things are quite easy. The
2740 * count should have been decremented to zero. We only need to deal with the
2741 * space remaining in the current reservation part of the ticket. If the
2742 * ticket contains a permanent reservation, there may be left over space which
2743 * needs to be released. A count of N means that N-1 refills of the current
2744 * reservation can be done before we need to ask for more space. The first
2745 * one goes to fill up the first current reservation. Once we run out of
2746 * space, the count will stay at zero and the only space remaining will be
2747 * in the current reservation field.
2748 */
2749STATIC void
2750xlog_ungrant_log_space(xlog_t *log,
2751 xlog_ticket_t *ticket)
2752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (ticket->t_cnt > 0)
2754 ticket->t_cnt--;
2755
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002756 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2758
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002759 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2762
2763 /* If this is a permanent reservation ticket, we may be able to free
2764 * up more space based on the remaining count.
2765 */
2766 if (ticket->t_cnt > 0) {
2767 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002768 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
2770
2771 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2772 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002773 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 xfs_log_move_tail(log->l_mp, 1);
2775} /* xlog_ungrant_log_space */
2776
2777
2778/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 * Flush iclog to disk if this is the last reference to the given iclog and
2780 * the WANT_SYNC bit is set.
2781 *
2782 * When this function is entered, the iclog is not necessarily in the
2783 * WANT_SYNC state. It may be sitting around waiting to get filled.
2784 *
2785 *
2786 */
David Chinnera8272ce2007-11-23 16:28:09 +11002787STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002788xlog_state_release_iclog(
2789 xlog_t *log,
2790 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int sync = 0; /* do we sync? */
2793
David Chinner155cc6b2008-03-06 13:44:14 +11002794 if (iclog->ic_state & XLOG_STATE_IOERROR)
2795 return XFS_ERROR(EIO);
2796
2797 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2798 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2799 return 0;
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002802 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return XFS_ERROR(EIO);
2804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2806 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2807
David Chinner155cc6b2008-03-06 13:44:14 +11002808 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002809 /* update tail before writing to iclog */
2810 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 sync++;
2812 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002813 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2815 /* cycle incremented when incrementing curr_block */
2816 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002817 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 /*
2820 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002821 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2823 * this iclog has consistent data, so we ignore IOERROR
2824 * flags after this point.
2825 */
David Chinnerb5893342008-03-06 13:44:06 +11002826 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829} /* xlog_state_release_iclog */
2830
2831
2832/*
2833 * This routine will mark the current iclog in the ring as WANT_SYNC
2834 * and move the current iclog pointer to the next iclog in the ring.
2835 * When this routine is called from xlog_state_get_iclog_space(), the
2836 * exact size of the iclog has not yet been determined. All we know is
2837 * that every data block. We have run out of space in this log record.
2838 */
2839STATIC void
2840xlog_state_switch_iclogs(xlog_t *log,
2841 xlog_in_core_t *iclog,
2842 int eventual_size)
2843{
2844 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2845 if (!eventual_size)
2846 eventual_size = iclog->ic_offset;
2847 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002848 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 log->l_prev_block = log->l_curr_block;
2850 log->l_prev_cycle = log->l_curr_cycle;
2851
2852 /* roll log?: ic_offset changed later */
2853 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2854
2855 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002856 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 log->l_mp->m_sb.sb_logsunit > 1) {
2858 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2859 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2860 }
2861
2862 if (log->l_curr_block >= log->l_logBBsize) {
2863 log->l_curr_cycle++;
2864 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2865 log->l_curr_cycle++;
2866 log->l_curr_block -= log->l_logBBsize;
2867 ASSERT(log->l_curr_block >= 0);
2868 }
2869 ASSERT(iclog == log->l_iclog);
2870 log->l_iclog = iclog->ic_next;
2871} /* xlog_state_switch_iclogs */
2872
2873
2874/*
2875 * Write out all data in the in-core log as of this exact moment in time.
2876 *
2877 * Data may be written to the in-core log during this call. However,
2878 * we don't guarantee this data will be written out. A change from past
2879 * implementation means this routine will *not* write out zero length LRs.
2880 *
2881 * Basically, we try and perform an intelligent scan of the in-core logs.
2882 * If we determine there is no flushable data, we just return. There is no
2883 * flushable data if:
2884 *
2885 * 1. the current iclog is active and has no data; the previous iclog
2886 * is in the active or dirty state.
2887 * 2. the current iclog is drity, and the previous iclog is in the
2888 * active or dirty state.
2889 *
2890 * We may sleep (call psema) if:
2891 *
2892 * 1. the current iclog is not in the active nor dirty state.
2893 * 2. the current iclog dirty, and the previous iclog is not in the
2894 * active nor dirty state.
2895 * 3. the current iclog is active, and there is another thread writing
2896 * to this particular iclog.
2897 * 4. a) the current iclog is active and has no other writers
2898 * b) when we return from flushing out this iclog, it is still
2899 * not in the active nor dirty state.
2900 */
2901STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002902xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 xlog_in_core_t *iclog;
2905 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002907 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
2909 iclog = log->l_iclog;
2910 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002911 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return XFS_ERROR(EIO);
2913 }
2914
2915 /* If the head iclog is not active nor dirty, we just attach
2916 * ourselves to the head and go to sleep.
2917 */
2918 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2919 iclog->ic_state == XLOG_STATE_DIRTY) {
2920 /*
2921 * If the head is dirty or (active and empty), then
2922 * we need to look at the previous iclog. If the previous
2923 * iclog is active or dirty we are done. There is nothing
2924 * to sync out. Otherwise, we attach ourselves to the
2925 * previous iclog and go to sleep.
2926 */
2927 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002928 (atomic_read(&iclog->ic_refcnt) == 0
2929 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 iclog = iclog->ic_prev;
2931 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2932 iclog->ic_state == XLOG_STATE_DIRTY)
2933 goto no_sleep;
2934 else
2935 goto maybe_sleep;
2936 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002937 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 /* We are the only one with access to this
2939 * iclog. Flush it out now. There should
2940 * be a roundoff of zero to show that someone
2941 * has already taken care of the roundoff from
2942 * the previous sync.
2943 */
David Chinner155cc6b2008-03-06 13:44:14 +11002944 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002945 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002947 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 if (xlog_state_release_iclog(log, iclog))
2950 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002951 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002952 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002953 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 iclog->ic_state != XLOG_STATE_DIRTY)
2955 goto maybe_sleep;
2956 else
2957 goto no_sleep;
2958 } else {
2959 /* Someone else is writing to this iclog.
2960 * Use its call to flush out the data. However,
2961 * the other thread may not force out this LR,
2962 * so we mark it WANT_SYNC.
2963 */
2964 xlog_state_switch_iclogs(log, iclog, 0);
2965 goto maybe_sleep;
2966 }
2967 }
2968 }
2969
2970 /* By the time we come around again, the iclog could've been filled
2971 * which would give it another lsn. If we have a new lsn, just
2972 * return because the relevant data has been flushed.
2973 */
2974maybe_sleep:
2975 if (flags & XFS_LOG_SYNC) {
2976 /*
2977 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002978 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * Then we check again after waking up, in case our
2980 * sleep was disturbed by a bad news.
2981 */
2982 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002983 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return XFS_ERROR(EIO);
2985 }
2986 XFS_STATS_INC(xs_log_force_sleep);
2987 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
2988 /*
2989 * No need to grab the log lock here since we're
2990 * only deciding whether or not to return EIO
2991 * and the memory read should be atomic.
2992 */
2993 if (iclog->ic_state & XLOG_STATE_IOERROR)
2994 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002995 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 } else {
2998
2999no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003000 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002 return 0;
3003} /* xlog_state_sync_all */
3004
3005
3006/*
3007 * Used by code which implements synchronous log forces.
3008 *
3009 * Find in-core log with lsn.
3010 * If it is in the DIRTY state, just return.
3011 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3012 * state and go to sleep or return.
3013 * If it is in any other state, go to sleep or return.
3014 *
3015 * If filesystem activity goes to zero, the iclog will get flushed only by
3016 * bdflush().
3017 */
David Chinnera8272ce2007-11-23 16:28:09 +11003018STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019xlog_state_sync(xlog_t *log,
3020 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003021 uint flags,
3022 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 xlog_in_core_t *iclog;
3025 int already_slept = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027try_again:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003028 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 iclog = log->l_iclog;
3030
3031 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003032 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return XFS_ERROR(EIO);
3034 }
3035
3036 do {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003037 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3038 iclog = iclog->ic_next;
3039 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041
3042 if (iclog->ic_state == XLOG_STATE_DIRTY) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003043 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return 0;
3045 }
3046
3047 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3048 /*
3049 * We sleep here if we haven't already slept (e.g.
3050 * this is the first time we've looked at the correct
3051 * iclog buf) and the buffer before us is going to
3052 * be sync'ed. The reason for this is that if we
3053 * are doing sync transactions here, by waiting for
3054 * the previous I/O to complete, we can allow a few
3055 * more transactions into this iclog before we close
3056 * it down.
3057 *
3058 * Otherwise, we mark the buffer WANT_SYNC, and bump
3059 * up the refcnt so we can release the log (which drops
3060 * the ref count). The state switch keeps new transaction
3061 * commits from using this buffer. When the current commits
3062 * finish writing into the buffer, the refcount will drop to
3063 * zero and the buffer will go out then.
3064 */
3065 if (!already_slept &&
3066 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3067 XLOG_STATE_SYNCING))) {
3068 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3069 XFS_STATS_INC(xs_log_force_sleep);
3070 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3071 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003072 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 already_slept = 1;
3074 goto try_again;
3075 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003076 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003078 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (xlog_state_release_iclog(log, iclog))
3080 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003081 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003082 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084 }
3085
3086 if ((flags & XFS_LOG_SYNC) && /* sleep */
3087 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3088
3089 /*
3090 * Don't wait on the forcesema if we know that we've
3091 * gotten a log write error.
3092 */
3093 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003094 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return XFS_ERROR(EIO);
3096 }
3097 XFS_STATS_INC(xs_log_force_sleep);
3098 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3099 /*
3100 * No need to grab the log lock here since we're
3101 * only deciding whether or not to return EIO
3102 * and the memory read should be atomic.
3103 */
3104 if (iclog->ic_state & XLOG_STATE_IOERROR)
3105 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003106 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003108 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 return 0;
3111
3112 } while (iclog != log->l_iclog);
3113
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003114 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +01003115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116} /* xlog_state_sync */
3117
3118
3119/*
3120 * Called when we want to mark the current iclog as being ready to sync to
3121 * disk.
3122 */
David Chinnera8272ce2007-11-23 16:28:09 +11003123STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3125{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003126 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3129 xlog_state_switch_iclogs(log, iclog, 0);
3130 } else {
3131 ASSERT(iclog->ic_state &
3132 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3133 }
3134
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003135 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136} /* xlog_state_want_sync */
3137
3138
3139
3140/*****************************************************************************
3141 *
3142 * TICKET functions
3143 *
3144 *****************************************************************************
3145 */
3146
3147/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003148 * Free a used ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 */
3150STATIC void
3151xlog_ticket_put(xlog_t *log,
3152 xlog_ticket_t *ticket)
3153{
3154 sv_destroy(&ticket->t_sema);
David Chinnereb01c9c2008-04-10 12:18:46 +10003155 kmem_zone_free(xfs_log_ticket_zone, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156} /* xlog_ticket_put */
3157
3158
3159/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003160 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 */
David Chinnera8272ce2007-11-23 16:28:09 +11003162STATIC xlog_ticket_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163xlog_ticket_get(xlog_t *log,
3164 int unit_bytes,
3165 int cnt,
3166 char client,
3167 uint xflags)
3168{
3169 xlog_ticket_t *tic;
3170 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
David Chinnereb01c9c2008-04-10 12:18:46 +10003172 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3173 if (!tic)
3174 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /*
3177 * Permanent reservations have up to 'cnt'-1 active log operations
3178 * in the log. A unit in this case is the amount of space for one
3179 * of these log operations. Normal reservations have a cnt of 1
3180 * and their unit amount is the total amount of space required.
3181 *
3182 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003183 * which occupy space in the on-disk log.
3184 *
3185 * Normal form of a transaction is:
3186 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3187 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3188 *
3189 * We need to account for all the leadup data and trailer data
3190 * around the transaction data.
3191 * And then we need to account for the worst case in terms of using
3192 * more space.
3193 * The worst case will happen if:
3194 * - the placement of the transaction happens to be such that the
3195 * roundoff is at its maximum
3196 * - the transaction data is synced before the commit record is synced
3197 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3198 * Therefore the commit record is in its own Log Record.
3199 * This can happen as the commit record is called with its
3200 * own region to xlog_write().
3201 * This then means that in the worst case, roundoff can happen for
3202 * the commit-rec as well.
3203 * The commit-rec is smaller than padding in this scenario and so it is
3204 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 */
3206
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003207 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003209 unit_bytes += sizeof(xfs_trans_header_t);
3210
3211 /* for start-rec */
3212 unit_bytes += sizeof(xlog_op_header_t);
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 /* for LR headers */
3215 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3216 unit_bytes += log->l_iclog_hsize * num_headers;
3217
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003218 /* for commit-rec LR header - note: padding will subsume the ophdr */
3219 unit_bytes += log->l_iclog_hsize;
3220
3221 /* for split-recs - ophdrs added when data split over LRs */
3222 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3223
3224 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003225 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003226 log->l_mp->m_sb.sb_logsunit > 1) {
3227 /* log su roundoff */
3228 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3229 } else {
3230 /* BB roundoff */
3231 unit_bytes += 2*BBSIZE;
3232 }
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 tic->t_unit_res = unit_bytes;
3235 tic->t_curr_res = unit_bytes;
3236 tic->t_cnt = cnt;
3237 tic->t_ocnt = cnt;
3238 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3239 tic->t_clientid = client;
3240 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003241 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 if (xflags & XFS_LOG_PERM_RESERV)
3243 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3244 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3245
Christoph Hellwig0adba532007-08-30 17:21:46 +10003246 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return tic;
3249} /* xlog_ticket_get */
3250
3251
3252/******************************************************************************
3253 *
3254 * Log debug routines
3255 *
3256 ******************************************************************************
3257 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003258#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259/*
3260 * Make sure that the destination ptr is within the valid data region of
3261 * one of the iclogs. This uses backup pointers stored in a different
3262 * part of the log in case we trash the log structure.
3263 */
3264void
3265xlog_verify_dest_ptr(xlog_t *log,
3266 __psint_t ptr)
3267{
3268 int i;
3269 int good_ptr = 0;
3270
3271 for (i=0; i < log->l_iclog_bufs; i++) {
3272 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3273 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3274 good_ptr++;
3275 }
3276 if (! good_ptr)
3277 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3278} /* xlog_verify_dest_ptr */
3279
3280STATIC void
3281xlog_verify_grant_head(xlog_t *log, int equals)
3282{
3283 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3284 if (equals)
3285 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3286 else
3287 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3288 } else {
3289 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3290 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3291 }
3292} /* xlog_verify_grant_head */
3293
3294/* check if it will fit */
3295STATIC void
3296xlog_verify_tail_lsn(xlog_t *log,
3297 xlog_in_core_t *iclog,
3298 xfs_lsn_t tail_lsn)
3299{
3300 int blocks;
3301
3302 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3303 blocks =
3304 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3305 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3306 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3307 } else {
3308 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3309
3310 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3311 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3312
3313 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3314 if (blocks < BTOBB(iclog->ic_offset) + 1)
3315 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3316 }
3317} /* xlog_verify_tail_lsn */
3318
3319/*
3320 * Perform a number of checks on the iclog before writing to disk.
3321 *
3322 * 1. Make sure the iclogs are still circular
3323 * 2. Make sure we have a good magic number
3324 * 3. Make sure we don't have magic numbers in the data
3325 * 4. Check fields of each log operation header for:
3326 * A. Valid client identifier
3327 * B. tid ptr value falls in valid ptr space (user space code)
3328 * C. Length in log record header is correct according to the
3329 * individual operation headers within record.
3330 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3331 * log, check the preceding blocks of the physical log to make sure all
3332 * the cycle numbers agree with the current cycle number.
3333 */
3334STATIC void
3335xlog_verify_iclog(xlog_t *log,
3336 xlog_in_core_t *iclog,
3337 int count,
3338 boolean_t syncing)
3339{
3340 xlog_op_header_t *ophead;
3341 xlog_in_core_t *icptr;
3342 xlog_in_core_2_t *xhdr;
3343 xfs_caddr_t ptr;
3344 xfs_caddr_t base_ptr;
3345 __psint_t field_offset;
3346 __uint8_t clientid;
3347 int len, i, j, k, op_len;
3348 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003351 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 icptr = log->l_iclog;
3353 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003354 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 xlog_panic("xlog_verify_iclog: invalid ptr");
3356 icptr = icptr->ic_next;
3357 }
3358 if (icptr != log->l_iclog)
3359 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003360 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003363 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 xlog_panic("xlog_verify_iclog: invalid magic num");
3365
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003366 ptr = (xfs_caddr_t) &iclog->ic_header;
3367 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003369 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 xlog_panic("xlog_verify_iclog: unexpected magic num");
3371 }
3372
3373 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003374 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 ptr = iclog->ic_datap;
3376 base_ptr = ptr;
3377 ophead = (xlog_op_header_t *)ptr;
3378 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3379 for (i = 0; i < len; i++) {
3380 ophead = (xlog_op_header_t *)ptr;
3381
3382 /* clientid is only 1 byte */
3383 field_offset = (__psint_t)
3384 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3385 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3386 clientid = ophead->oh_clientid;
3387 } else {
3388 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3389 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3390 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3391 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003392 clientid = xlog_get_client_id(
3393 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003395 clientid = xlog_get_client_id(
3396 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
3398 }
3399 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003400 cmn_err(CE_WARN, "xlog_verify_iclog: "
3401 "invalid clientid %d op 0x%p offset 0x%lx",
3402 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 /* check length */
3405 field_offset = (__psint_t)
3406 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3407 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003408 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 } else {
3410 idx = BTOBBT((__psint_t)&ophead->oh_len -
3411 (__psint_t)iclog->ic_datap);
3412 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3413 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3414 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003415 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003417 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419 }
3420 ptr += sizeof(xlog_op_header_t) + op_len;
3421 }
3422} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003426 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 */
3428STATIC int
3429xlog_state_ioerror(
3430 xlog_t *log)
3431{
3432 xlog_in_core_t *iclog, *ic;
3433
3434 iclog = log->l_iclog;
3435 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3436 /*
3437 * Mark all the incore logs IOERROR.
3438 * From now on, no log flushes will result.
3439 */
3440 ic = iclog;
3441 do {
3442 ic->ic_state = XLOG_STATE_IOERROR;
3443 ic = ic->ic_next;
3444 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
3447 /*
3448 * Return non-zero, if state transition has already happened.
3449 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003450 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
3453/*
3454 * This is called from xfs_force_shutdown, when we're forcibly
3455 * shutting down the filesystem, typically because of an IO error.
3456 * Our main objectives here are to make sure that:
3457 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3458 * parties to find out, 'atomically'.
3459 * b. those who're sleeping on log reservations, pinned objects and
3460 * other resources get woken up, and be told the bad news.
3461 * c. nothing new gets queued up after (a) and (b) are done.
3462 * d. if !logerror, flush the iclogs to disk, then seal them off
3463 * for business.
3464 */
3465int
3466xfs_log_force_umount(
3467 struct xfs_mount *mp,
3468 int logerror)
3469{
3470 xlog_ticket_t *tic;
3471 xlog_t *log;
3472 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003473 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 log = mp->m_log;
3476
3477 /*
3478 * If this happens during log recovery, don't worry about
3479 * locking; the log isn't open for business yet.
3480 */
3481 if (!log ||
3482 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3483 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3484 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
3487
3488 /*
3489 * Somebody could've already done the hard work for us.
3490 * No need to get locks for this.
3491 */
3492 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3493 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003494 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
3496 retval = 0;
3497 /*
3498 * We must hold both the GRANT lock and the LOG lock,
3499 * before we mark the filesystem SHUTDOWN and wake
3500 * everybody up to tell the bad news.
3501 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003502 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003503 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3505 XFS_BUF_DONE(mp->m_sb_bp);
3506 /*
3507 * This flag is sort of redundant because of the mount flag, but
3508 * it's good to maintain the separation between the log and the rest
3509 * of XFS.
3510 */
3511 log->l_flags |= XLOG_IO_ERROR;
3512
3513 /*
3514 * If we hit a log error, we want to mark all the iclogs IOERROR
3515 * while we're still holding the loglock.
3516 */
3517 if (logerror)
3518 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003519 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 /*
3522 * We don't want anybody waiting for log reservations
3523 * after this. That means we have to wake up everybody
3524 * queued up on reserve_headq as well as write_headq.
3525 * In addition, we make sure in xlog_{re}grant_log_space
3526 * that we don't enqueue anything once the SHUTDOWN flag
3527 * is set, and this action is protected by the GRANTLOCK.
3528 */
3529 if ((tic = log->l_reserve_headq)) {
3530 do {
3531 sv_signal(&tic->t_sema);
3532 tic = tic->t_next;
3533 } while (tic != log->l_reserve_headq);
3534 }
3535
3536 if ((tic = log->l_write_headq)) {
3537 do {
3538 sv_signal(&tic->t_sema);
3539 tic = tic->t_next;
3540 } while (tic != log->l_write_headq);
3541 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003542 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
3544 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3545 ASSERT(!logerror);
3546 /*
3547 * Force the incore logs to disk before shutting the
3548 * log down completely.
3549 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003550 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003551 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003553 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
3555 /*
3556 * Wake up everybody waiting on xfs_log_force.
3557 * Callback all log item committed functions as if the
3558 * log writes were completed.
3559 */
3560 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3561
3562#ifdef XFSERRORDEBUG
3563 {
3564 xlog_in_core_t *iclog;
3565
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003566 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 iclog = log->l_iclog;
3568 do {
3569 ASSERT(iclog->ic_callback == 0);
3570 iclog = iclog->ic_next;
3571 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003572 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
3574#endif
3575 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003576 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003579STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580xlog_iclogs_empty(xlog_t *log)
3581{
3582 xlog_in_core_t *iclog;
3583
3584 iclog = log->l_iclog;
3585 do {
3586 /* endianness does not matter here, zero is zero in
3587 * any language.
3588 */
3589 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 iclog = iclog->ic_next;
3592 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003593 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}