blob: c48bf61f17bd847cdb04a6ca8ad6b6f9f8c7e273 [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
44
45#define xlog_write_adv_cnt(ptr, len, off, bytes) \
46 { (ptr) += (bytes); \
47 (len) -= (bytes); \
48 (off) += (bytes);}
49
50/* Local miscellaneous function prototypes */
51STATIC int xlog_bdstrat_cb(struct xfs_buf *);
52STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
53 xlog_in_core_t **, xfs_lsn_t *);
54STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
55 xfs_buftarg_t *log_target,
56 xfs_daddr_t blk_offset,
57 int num_bblks);
58STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
59STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100060STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
62 int nentries, xfs_log_ticket_t tic,
63 xfs_lsn_t *start_lsn,
64 xlog_in_core_t **commit_iclog,
65 uint flags);
66
67/* local state machine functions */
68STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
69STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
70STATIC int xlog_state_get_iclog_space(xlog_t *log,
71 int len,
72 xlog_in_core_t **iclog,
73 xlog_ticket_t *ticket,
74 int *continued_write,
75 int *logoffsetp);
76STATIC void xlog_state_put_ticket(xlog_t *log,
77 xlog_ticket_t *tic);
78STATIC int xlog_state_release_iclog(xlog_t *log,
79 xlog_in_core_t *iclog);
80STATIC void xlog_state_switch_iclogs(xlog_t *log,
81 xlog_in_core_t *iclog,
82 int eventual_size);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +110083STATIC int xlog_state_sync(xlog_t *log,
84 xfs_lsn_t lsn,
85 uint flags,
86 int *log_flushed);
87STATIC int xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
89
90/* local functions to manipulate grant head */
91STATIC int xlog_grant_log_space(xlog_t *log,
92 xlog_ticket_t *xtic);
93STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
94 int need_bytes);
95STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
96 xlog_ticket_t *ticket);
97STATIC int xlog_regrant_write_log_space(xlog_t *log,
98 xlog_ticket_t *ticket);
99STATIC void xlog_ungrant_log_space(xlog_t *log,
100 xlog_ticket_t *ticket);
101
102
103/* local ticket functions */
104STATIC void xlog_state_ticket_alloc(xlog_t *log);
105STATIC xlog_ticket_t *xlog_ticket_get(xlog_t *log,
106 int unit_bytes,
107 int count,
108 char clientid,
109 uint flags);
110STATIC void xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
111
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100112#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
114STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
115STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
116 int count, boolean_t syncing);
117STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
118 xfs_lsn_t tail_lsn);
119#else
120#define xlog_verify_dest_ptr(a,b)
121#define xlog_verify_grant_head(a,b)
122#define xlog_verify_iclog(a,b,c,d)
123#define xlog_verify_tail_lsn(a,b,c)
124#endif
125
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000126STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#if defined(XFS_LOG_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void
130xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
131{
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000132 unsigned long cnts;
133
134 if (!log->l_grant_trace) {
135 log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
136 if (!log->l_grant_trace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
138 }
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000139 /* ticket counts are 1 byte each */
140 cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 ktrace_enter(log->l_grant_trace,
143 (void *)tic,
144 (void *)log->l_reserve_headq,
145 (void *)log->l_write_headq,
146 (void *)((unsigned long)log->l_grant_reserve_cycle),
147 (void *)((unsigned long)log->l_grant_reserve_bytes),
148 (void *)((unsigned long)log->l_grant_write_cycle),
149 (void *)((unsigned long)log->l_grant_write_bytes),
150 (void *)((unsigned long)log->l_curr_cycle),
151 (void *)((unsigned long)log->l_curr_block),
152 (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
153 (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
154 (void *)string,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000155 (void *)((unsigned long)tic->t_trans_type),
156 (void *)cnts,
157 (void *)((unsigned long)tic->t_curr_res),
158 (void *)((unsigned long)tic->t_unit_res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void
162xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!iclog->ic_trace)
165 iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
166 ktrace_enter(iclog->ic_trace,
167 (void *)((unsigned long)state),
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100168 (void *)((unsigned long)current_pid()),
169 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
170 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
171 (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
172 (void *)NULL, (void *)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#else
175#define xlog_trace_loggrant(log,tic,string)
176#define xlog_trace_iclog(iclog,state)
177#endif /* XFS_LOG_TRACE */
178
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100179
180static void
181xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
182{
183 if (*qp) {
184 tic->t_next = (*qp);
185 tic->t_prev = (*qp)->t_prev;
186 (*qp)->t_prev->t_next = tic;
187 (*qp)->t_prev = tic;
188 } else {
189 tic->t_prev = tic->t_next = tic;
190 *qp = tic;
191 }
192
193 tic->t_flags |= XLOG_TIC_IN_Q;
194}
195
196static void
197xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
198{
199 if (tic == tic->t_next) {
200 *qp = NULL;
201 } else {
202 *qp = tic->t_next;
203 tic->t_next->t_prev = tic->t_prev;
204 tic->t_prev->t_next = tic->t_next;
205 }
206
207 tic->t_next = tic->t_prev = NULL;
208 tic->t_flags &= ~XLOG_TIC_IN_Q;
209}
210
211static void
212xlog_grant_sub_space(struct log *log, int bytes)
213{
214 log->l_grant_write_bytes -= bytes;
215 if (log->l_grant_write_bytes < 0) {
216 log->l_grant_write_bytes += log->l_logsize;
217 log->l_grant_write_cycle--;
218 }
219
220 log->l_grant_reserve_bytes -= bytes;
221 if ((log)->l_grant_reserve_bytes < 0) {
222 log->l_grant_reserve_bytes += log->l_logsize;
223 log->l_grant_reserve_cycle--;
224 }
225
226}
227
228static void
229xlog_grant_add_space_write(struct log *log, int bytes)
230{
231 log->l_grant_write_bytes += bytes;
232 if (log->l_grant_write_bytes > log->l_logsize) {
233 log->l_grant_write_bytes -= log->l_logsize;
234 log->l_grant_write_cycle++;
235 }
236}
237
238static void
239xlog_grant_add_space_reserve(struct log *log, int bytes)
240{
241 log->l_grant_reserve_bytes += bytes;
242 if (log->l_grant_reserve_bytes > log->l_logsize) {
243 log->l_grant_reserve_bytes -= log->l_logsize;
244 log->l_grant_reserve_cycle++;
245 }
246}
247
248static inline void
249xlog_grant_add_space(struct log *log, int bytes)
250{
251 xlog_grant_add_space_write(log, bytes);
252 xlog_grant_add_space_reserve(log, bytes);
253}
254
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
257 * NOTES:
258 *
259 * 1. currblock field gets updated at startup and after in-core logs
260 * marked as with WANT_SYNC.
261 */
262
263/*
264 * This routine is called when a user of a log manager ticket is done with
265 * the reservation. If the ticket was ever used, then a commit record for
266 * the associated transaction is written out as a log operation header with
267 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
268 * a given ticket. If the ticket was one with a permanent reservation, then
269 * a few operations are done differently. Permanent reservation tickets by
270 * default don't release the reservation. They just commit the current
271 * transaction with the belief that the reservation is still needed. A flag
272 * must be passed in before permanent reservations are actually released.
273 * When these type of tickets are not released, they need to be set into
274 * the inited state again. By doing this, a start record will be written
275 * out when the next write occurs.
276 */
277xfs_lsn_t
278xfs_log_done(xfs_mount_t *mp,
279 xfs_log_ticket_t xtic,
280 void **iclog,
281 uint flags)
282{
283 xlog_t *log = mp->m_log;
284 xlog_ticket_t *ticket = (xfs_log_ticket_t) xtic;
285 xfs_lsn_t lsn = 0;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (XLOG_FORCED_SHUTDOWN(log) ||
288 /*
289 * If nothing was ever written, don't write out commit record.
290 * If we get an error, just continue and give back the log ticket.
291 */
292 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
293 (xlog_commit_record(mp, ticket,
294 (xlog_in_core_t **)iclog, &lsn)))) {
295 lsn = (xfs_lsn_t) -1;
296 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
297 flags |= XFS_LOG_REL_PERM_RESERV;
298 }
299 }
300
301
302 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
303 (flags & XFS_LOG_REL_PERM_RESERV)) {
304 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000305 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * request has been made to release a permanent reservation.
307 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000308 xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 xlog_ungrant_log_space(log, ticket);
310 xlog_state_put_ticket(log, ticket);
311 } else {
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000312 xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 xlog_regrant_reserve_log_space(log, ticket);
314 }
315
316 /* If this ticket was a permanent reservation and we aren't
317 * trying to release it, reset the inited flags; so next time
318 * we write, a start record will be written out.
319 */
320 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
321 (flags & XFS_LOG_REL_PERM_RESERV) == 0)
322 ticket->t_flags |= XLOG_TIC_INITED;
323
324 return lsn;
325} /* xfs_log_done */
326
327
328/*
329 * Force the in-core log to disk. If flags == XFS_LOG_SYNC,
330 * the force is done synchronously.
331 *
332 * Asynchronous forces are implemented by setting the WANT_SYNC
333 * bit in the appropriate in-core log and then returning.
334 *
335 * Synchronous forces are implemented with a semaphore. All callers
336 * to force a given lsn to disk will wait on a semaphore attached to the
337 * specific in-core log. When given in-core log finally completes its
338 * write to disk, that thread will wake up all threads waiting on the
339 * semaphore.
340 */
341int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100342_xfs_log_force(
343 xfs_mount_t *mp,
344 xfs_lsn_t lsn,
345 uint flags,
346 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100348 xlog_t *log = mp->m_log;
349 int dummy;
350
351 if (!log_flushed)
352 log_flushed = &dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ASSERT(flags & XFS_LOG_FORCE);
355
356 XFS_STATS_INC(xs_log_force);
357
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100358 if (log->l_flags & XLOG_IO_ERROR)
359 return XFS_ERROR(EIO);
360 if (lsn == 0)
361 return xlog_state_sync_all(log, flags, log_flushed);
362 else
363 return xlog_state_sync(log, lsn, flags, log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364} /* xfs_log_force */
365
366/*
367 * Attaches a new iclog I/O completion callback routine during
368 * transaction commit. If the log is in error state, a non-zero
369 * return code is handed back and the caller is responsible for
370 * executing the callback at an appropriate time.
371 */
372int
373xfs_log_notify(xfs_mount_t *mp, /* mount of partition */
374 void *iclog_hndl, /* iclog to hang callback off */
375 xfs_log_callback_t *cb)
376{
377 xlog_t *log = mp->m_log;
378 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
379 int abortflg, spl;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 cb->cb_next = NULL;
382 spl = LOG_LOCK(log);
383 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
384 if (!abortflg) {
385 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
386 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
387 cb->cb_next = NULL;
388 *(iclog->ic_callback_tail) = cb;
389 iclog->ic_callback_tail = &(cb->cb_next);
390 }
391 LOG_UNLOCK(log, spl);
392 return abortflg;
393} /* xfs_log_notify */
394
395int
396xfs_log_release_iclog(xfs_mount_t *mp,
397 void *iclog_hndl)
398{
399 xlog_t *log = mp->m_log;
400 xlog_in_core_t *iclog = (xlog_in_core_t *)iclog_hndl;
401
402 if (xlog_state_release_iclog(log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000403 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100404 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 return 0;
408}
409
410/*
411 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
412 * to the reservation.
413 * 2. Potentially, push buffers at tail of log to disk.
414 *
415 * Each reservation is going to reserve extra space for a log record header.
416 * When writes happen to the on-disk log, we don't subtract the length of the
417 * log record header from any reservation. By wasting space in each
418 * reservation, we prevent over allocation problems.
419 */
420int
421xfs_log_reserve(xfs_mount_t *mp,
422 int unit_bytes,
423 int cnt,
424 xfs_log_ticket_t *ticket,
425 __uint8_t client,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000426 uint flags,
427 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 xlog_t *log = mp->m_log;
430 xlog_ticket_t *internal_ticket;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100431 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
434 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
435
436 if (XLOG_FORCED_SHUTDOWN(log))
437 return XFS_ERROR(EIO);
438
439 XFS_STATS_INC(xs_try_logspace);
440
441 if (*ticket != NULL) {
442 ASSERT(flags & XFS_LOG_PERM_RESERV);
443 internal_ticket = (xlog_ticket_t *)*ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000444 xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
446 retval = xlog_regrant_write_log_space(log, internal_ticket);
447 } else {
448 /* may sleep if need to allocate more tickets */
449 internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
450 client, flags);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000451 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *ticket = internal_ticket;
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000453 xlog_trace_loggrant(log, internal_ticket,
454 (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
455 "xfs_log_reserve: create new ticket (permanent trans)" :
456 "xfs_log_reserve: create new ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xlog_grant_push_ail(mp,
458 (internal_ticket->t_unit_res *
459 internal_ticket->t_cnt));
460 retval = xlog_grant_log_space(log, internal_ticket);
461 }
462
463 return retval;
464} /* xfs_log_reserve */
465
466
467/*
468 * Mount a log filesystem
469 *
470 * mp - ubiquitous xfs mount point structure
471 * log_target - buftarg of on-disk log device
472 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
473 * num_bblocks - Number of BBSIZE blocks in on-disk log
474 *
475 * Return error or zero.
476 */
477int
478xfs_log_mount(xfs_mount_t *mp,
479 xfs_buftarg_t *log_target,
480 xfs_daddr_t blk_offset,
481 int num_bblks)
482{
483 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
484 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
485 else {
486 cmn_err(CE_NOTE,
487 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
488 mp->m_fsname);
489 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
490 }
491
492 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
495 * skip log recovery on a norecovery mount. pretend it all
496 * just worked.
497 */
498 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
Nathan Scottb83bd132006-06-09 16:48:30 +1000499 bhv_vfs_t *vfsp = XFS_MTOVFS(mp);
500 int error, readonly = (vfsp->vfs_flag & VFS_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (readonly)
503 vfsp->vfs_flag &= ~VFS_RDONLY;
504
Eric Sandeen65be6052006-01-11 15:34:19 +1100505 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (readonly)
508 vfsp->vfs_flag |= VFS_RDONLY;
509 if (error) {
510 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Nathan Scottc41564b2006-03-29 08:55:14 +1000511 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return error;
513 }
514 }
515
516 /* Normal transactions can now occur */
517 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
518
519 /* End mounting message in xfs_log_mount_finish */
520 return 0;
521} /* xfs_log_mount */
522
523/*
524 * Finish the recovery of the file system. This is separate from
525 * the xfs_log_mount() call, because it depends on the code in
526 * xfs_mountfs() to read in the root and real-time bitmap inodes
527 * between calling xfs_log_mount() and here.
528 *
529 * mp - ubiquitous xfs mount point structure
530 */
531int
532xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
533{
534 int error;
535
536 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
537 error = xlog_recover_finish(mp->m_log, mfsi_flags);
538 else {
539 error = 0;
540 ASSERT(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY);
541 }
542
543 return error;
544}
545
546/*
547 * Unmount processing for the log.
548 */
549int
550xfs_log_unmount(xfs_mount_t *mp)
551{
552 int error;
553
554 error = xfs_log_unmount_write(mp);
555 xfs_log_unmount_dealloc(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +0100556 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559/*
560 * Final log writes as part of unmount.
561 *
562 * Mark the filesystem clean as unmount happens. Note that during relocation
563 * this routine needs to be executed as part of source-bag while the
564 * deallocation must not be done until source-end.
565 */
566
567/*
568 * Unmount record used to have a string "Unmount filesystem--" in the
569 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
570 * We just write the magic number now since that particular field isn't
571 * currently architecture converted and "nUmount" is a bit foo.
572 * As far as I know, there weren't any dependencies on the old behaviour.
573 */
574
575int
576xfs_log_unmount_write(xfs_mount_t *mp)
577{
578 xlog_t *log = mp->m_log;
579 xlog_in_core_t *iclog;
580#ifdef DEBUG
581 xlog_in_core_t *first_iclog;
582#endif
583 xfs_log_iovec_t reg[1];
584 xfs_log_ticket_t tic = NULL;
585 xfs_lsn_t lsn;
586 int error;
587 SPLDECL(s);
588
589 /* the data section must be 32 bit size aligned */
590 struct {
591 __uint16_t magic;
592 __uint16_t pad1;
593 __uint32_t pad2; /* may as well make it 64 bits */
594 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Don't write out unmount record on read-only mounts.
598 * Or, if we are doing a forced umount (typically because of IO errors).
599 */
600 if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
601 return 0;
602
603 xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
604
605#ifdef DEBUG
606 first_iclog = iclog = log->l_iclog;
607 do {
608 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
609 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
610 ASSERT(iclog->ic_offset == 0);
611 }
612 iclog = iclog->ic_next;
613 } while (iclog != first_iclog);
614#endif
615 if (! (XLOG_FORCED_SHUTDOWN(log))) {
616 reg[0].i_addr = (void*)&magic;
617 reg[0].i_len = sizeof(magic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000618 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Tim Shimmin955e47a2006-09-28 11:04:16 +1000620 error = xfs_log_reserve(mp, 600, 1, &tic,
621 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!error) {
623 /* remove inited flag */
624 ((xlog_ticket_t *)tic)->t_flags = 0;
625 error = xlog_write(mp, reg, 1, tic, &lsn,
626 NULL, XLOG_UNMOUNT_TRANS);
627 /*
628 * At this point, we're umounting anyway,
629 * so there's no point in transitioning log state
630 * to IOERROR. Just continue...
631 */
632 }
633
634 if (error) {
635 xfs_fs_cmn_err(CE_ALERT, mp,
636 "xfs_log_unmount: unmount record failed");
637 }
638
639
640 s = LOG_LOCK(log);
641 iclog = log->l_iclog;
642 iclog->ic_refcnt++;
643 LOG_UNLOCK(log, s);
644 xlog_state_want_sync(log, iclog);
645 (void) xlog_state_release_iclog(log, iclog);
646
647 s = LOG_LOCK(log);
648 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
649 iclog->ic_state == XLOG_STATE_DIRTY)) {
650 if (!XLOG_FORCED_SHUTDOWN(log)) {
651 sv_wait(&iclog->ic_forcesema, PMEM,
652 &log->l_icloglock, s);
653 } else {
654 LOG_UNLOCK(log, s);
655 }
656 } else {
657 LOG_UNLOCK(log, s);
658 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000659 if (tic) {
660 xlog_trace_loggrant(log, tic, "unmount rec");
661 xlog_ungrant_log_space(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 xlog_state_put_ticket(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else {
665 /*
666 * We're already in forced_shutdown mode, couldn't
667 * even attempt to write out the unmount transaction.
668 *
669 * Go through the motions of sync'ing and releasing
670 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000671 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * be in progress. Do this as a separate section of
673 * code so we'll know if we ever get stuck here that
674 * we're in this odd situation of trying to unmount
675 * a file system that went into forced_shutdown as
676 * the result of an unmount..
677 */
678 s = LOG_LOCK(log);
679 iclog = log->l_iclog;
680 iclog->ic_refcnt++;
681 LOG_UNLOCK(log, s);
682
683 xlog_state_want_sync(log, iclog);
684 (void) xlog_state_release_iclog(log, iclog);
685
686 s = LOG_LOCK(log);
687
688 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
689 || iclog->ic_state == XLOG_STATE_DIRTY
690 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
691
692 sv_wait(&iclog->ic_forcesema, PMEM,
693 &log->l_icloglock, s);
694 } else {
695 LOG_UNLOCK(log, s);
696 }
697 }
698
699 return 0;
700} /* xfs_log_unmount_write */
701
702/*
703 * Deallocate log structures for unmount/relocation.
704 */
705void
706xfs_log_unmount_dealloc(xfs_mount_t *mp)
707{
Nathan Scottc41564b2006-03-29 08:55:14 +1000708 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711/*
712 * Write region vectors to log. The write happens using the space reservation
713 * of the ticket (tic). It is not a requirement that all writes for a given
714 * transaction occur with one call to xfs_log_write().
715 */
716int
717xfs_log_write(xfs_mount_t * mp,
718 xfs_log_iovec_t reg[],
719 int nentries,
720 xfs_log_ticket_t tic,
721 xfs_lsn_t *start_lsn)
722{
723 int error;
724 xlog_t *log = mp->m_log;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (XLOG_FORCED_SHUTDOWN(log))
727 return XFS_ERROR(EIO);
728
729 if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000730 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Jesper Juhl014c2542006-01-15 02:37:08 +0100732 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733} /* xfs_log_write */
734
735
736void
737xfs_log_move_tail(xfs_mount_t *mp,
738 xfs_lsn_t tail_lsn)
739{
740 xlog_ticket_t *tic;
741 xlog_t *log = mp->m_log;
742 int need_bytes, free_bytes, cycle, bytes;
743 SPLDECL(s);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (XLOG_FORCED_SHUTDOWN(log))
746 return;
747 ASSERT(!XFS_FORCED_SHUTDOWN(mp));
748
749 if (tail_lsn == 0) {
750 /* needed since sync_lsn is 64 bits */
751 s = LOG_LOCK(log);
752 tail_lsn = log->l_last_sync_lsn;
753 LOG_UNLOCK(log, s);
754 }
755
756 s = GRANT_LOCK(log);
757
758 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
759 * tail_lsn.
760 */
761 if (tail_lsn != 1) {
762 log->l_tail_lsn = tail_lsn;
763 }
764
765 if ((tic = log->l_write_headq)) {
766#ifdef DEBUG
767 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
768 panic("Recovery problem");
769#endif
770 cycle = log->l_grant_write_cycle;
771 bytes = log->l_grant_write_bytes;
772 free_bytes = xlog_space_left(log, cycle, bytes);
773 do {
774 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
775
776 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
777 break;
778 tail_lsn = 0;
779 free_bytes -= tic->t_unit_res;
780 sv_signal(&tic->t_sema);
781 tic = tic->t_next;
782 } while (tic != log->l_write_headq);
783 }
784 if ((tic = log->l_reserve_headq)) {
785#ifdef DEBUG
786 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
787 panic("Recovery problem");
788#endif
789 cycle = log->l_grant_reserve_cycle;
790 bytes = log->l_grant_reserve_bytes;
791 free_bytes = xlog_space_left(log, cycle, bytes);
792 do {
793 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
794 need_bytes = tic->t_unit_res*tic->t_cnt;
795 else
796 need_bytes = tic->t_unit_res;
797 if (free_bytes < need_bytes && tail_lsn != 1)
798 break;
799 tail_lsn = 0;
800 free_bytes -= need_bytes;
801 sv_signal(&tic->t_sema);
802 tic = tic->t_next;
803 } while (tic != log->l_reserve_headq);
804 }
805 GRANT_UNLOCK(log, s);
806} /* xfs_log_move_tail */
807
808/*
809 * Determine if we have a transaction that has gone to disk
810 * that needs to be covered. Log activity needs to be idle (no AIL and
811 * nothing in the iclogs). And, we need to be in the right state indicating
812 * something has gone out.
813 */
814int
815xfs_log_need_covered(xfs_mount_t *mp)
816{
817 SPLDECL(s);
818 int needed = 0, gen;
819 xlog_t *log = mp->m_log;
Nathan Scottb83bd132006-06-09 16:48:30 +1000820 bhv_vfs_t *vfsp = XFS_MTOVFS(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Nathan Scott34327e12006-06-09 17:11:55 +1000822 if (vfs_test_for_freeze(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 (vfsp->vfs_flag & VFS_RDONLY))
824 return 0;
825
826 s = LOG_LOCK(log);
827 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
828 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
829 && !xfs_trans_first_ail(mp, &gen)
830 && xlog_iclogs_empty(log)) {
831 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
832 log->l_covered_state = XLOG_STATE_COVER_DONE;
833 else {
834 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
835 log->l_covered_state = XLOG_STATE_COVER_DONE2;
836 }
837 needed = 1;
838 }
839 LOG_UNLOCK(log, s);
Jesper Juhl014c2542006-01-15 02:37:08 +0100840 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/******************************************************************************
844 *
845 * local routines
846 *
847 ******************************************************************************
848 */
849
850/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
851 * The log manager must keep track of the last LR which was committed
852 * to disk. The lsn of this LR will become the new tail_lsn whenever
853 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
854 * the situation where stuff could be written into the log but nothing
855 * was ever in the AIL when asked. Eventually, we panic since the
856 * tail hits the head.
857 *
858 * We may be holding the log iclog lock upon entering this routine.
859 */
860xfs_lsn_t
861xlog_assign_tail_lsn(xfs_mount_t *mp)
862{
863 xfs_lsn_t tail_lsn;
864 SPLDECL(s);
865 xlog_t *log = mp->m_log;
866
867 tail_lsn = xfs_trans_tail_ail(mp);
868 s = GRANT_LOCK(log);
869 if (tail_lsn != 0) {
870 log->l_tail_lsn = tail_lsn;
871 } else {
872 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
873 }
874 GRANT_UNLOCK(log, s);
875
876 return tail_lsn;
877} /* xlog_assign_tail_lsn */
878
879
880/*
881 * Return the space in the log between the tail and the head. The head
882 * is passed in the cycle/bytes formal parms. In the special case where
883 * the reserve head has wrapped passed the tail, this calculation is no
884 * longer valid. In this case, just return 0 which means there is no space
885 * in the log. This works for all places where this function is called
886 * with the reserve head. Of course, if the write head were to ever
887 * wrap the tail, we should blow up. Rather than catch this case here,
888 * we depend on other ASSERTions in other parts of the code. XXXmiken
889 *
890 * This code also handles the case where the reservation head is behind
891 * the tail. The details of this case are described below, but the end
892 * result is that we return the size of the log as the amount of space left.
893 */
894int
895xlog_space_left(xlog_t *log, int cycle, int bytes)
896{
897 int free_bytes;
898 int tail_bytes;
899 int tail_cycle;
900
901 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
902 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
903 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
904 free_bytes = log->l_logsize - (bytes - tail_bytes);
905 } else if ((tail_cycle + 1) < cycle) {
906 return 0;
907 } else if (tail_cycle < cycle) {
908 ASSERT(tail_cycle == (cycle - 1));
909 free_bytes = tail_bytes - bytes;
910 } else {
911 /*
912 * The reservation head is behind the tail.
913 * In this case we just want to return the size of the
914 * log as the amount of space left.
915 */
916 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
917 "xlog_space_left: head behind tail\n"
918 " tail_cycle = %d, tail_bytes = %d\n"
919 " GH cycle = %d, GH bytes = %d",
920 tail_cycle, tail_bytes, cycle, bytes);
921 ASSERT(0);
922 free_bytes = log->l_logsize;
923 }
924 return free_bytes;
925} /* xlog_space_left */
926
927
928/*
929 * Log function which is called when an io completes.
930 *
931 * The log manager needs its own routine, in order to control what
932 * happens with the buffer after the write completes.
933 */
934void
935xlog_iodone(xfs_buf_t *bp)
936{
937 xlog_in_core_t *iclog;
938 xlog_t *l;
939 int aborted;
940
941 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
942 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
943 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
944 aborted = 0;
945
946 /*
947 * Some versions of cpp barf on the recursive definition of
948 * ic_log -> hic_fields.ic_log and expand ic_log twice when
949 * it is passed through two macros. Workaround broken cpp.
950 */
951 l = iclog->ic_log;
952
953 /*
954 * Race to shutdown the filesystem if we see an error.
955 */
956 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
957 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
958 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
959 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000960 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /*
962 * This flag will be propagated to the trans-committed
963 * callback routines to let them know that the log-commit
964 * didn't succeed.
965 */
966 aborted = XFS_LI_ABORTED;
967 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
968 aborted = XFS_LI_ABORTED;
969 }
970 xlog_state_done_syncing(iclog, aborted);
971 if (!(XFS_BUF_ISASYNC(bp))) {
972 /*
973 * Corresponding psema() will be done in bwrite(). If we don't
974 * vsema() here, panic.
975 */
976 XFS_BUF_V_IODONESEMA(bp);
977 }
978} /* xlog_iodone */
979
980/*
981 * The bdstrat callback function for log bufs. This gives us a central
982 * place to trap bufs in case we get hit by a log I/O error and need to
983 * shutdown. Actually, in practice, even when we didn't get a log error,
984 * we transition the iclogs to IOERROR state *after* flushing all existing
985 * iclogs to disk. This is because we don't want anymore new transactions to be
986 * started or completed afterwards.
987 */
988STATIC int
989xlog_bdstrat_cb(struct xfs_buf *bp)
990{
991 xlog_in_core_t *iclog;
992
993 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
994
995 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
996 /* note for irix bstrat will need struct bdevsw passed
997 * Fix the following macro if the code ever is merged
998 */
999 XFS_bdstrat(bp);
1000 return 0;
1001 }
1002
1003 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1004 XFS_BUF_ERROR(bp, EIO);
1005 XFS_BUF_STALE(bp);
1006 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001007 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009
1010}
1011
1012/*
1013 * Return size of each in-core log record buffer.
1014 *
1015 * Low memory machines only get 2 16KB buffers. We don't want to waste
1016 * memory here. However, all other machines get at least 2 32KB buffers.
1017 * The number is hard coded because we don't care about the minimum
1018 * memory size, just 32MB systems.
1019 *
1020 * If the filesystem blocksize is too large, we may need to choose a
1021 * larger size since the directory code currently logs entire blocks.
1022 */
1023
1024STATIC void
1025xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1026 xlog_t *log)
1027{
1028 int size;
1029 int xhdrs;
1030
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001031 if (mp->m_logbufs <= 0) {
1032 if (xfs_physmem <= btoc(128*1024*1024)) {
1033 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1034 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1035 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1036 } else { /* 256K with 32K bufs */
1037 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001039 } else {
1040 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 /*
1044 * Buffer size passed in from mount system call.
1045 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001046 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 size = log->l_iclog_size = mp->m_logbsize;
1048 log->l_iclog_size_log = 0;
1049 while (size != 1) {
1050 log->l_iclog_size_log++;
1051 size >>= 1;
1052 }
1053
1054 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1055 /* # headers = size / 32K
1056 * one header holds cycles from 32K of data
1057 */
1058
1059 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1060 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1061 xhdrs++;
1062 log->l_iclog_hsize = xhdrs << BBSHIFT;
1063 log->l_iclog_heads = xhdrs;
1064 } else {
1065 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1066 log->l_iclog_hsize = BBSIZE;
1067 log->l_iclog_heads = 1;
1068 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001069 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
1072 /*
1073 * Special case machines that have less than 32MB of memory.
1074 * All machines with more memory use 32KB buffers.
1075 */
1076 if (xfs_physmem <= btoc(32*1024*1024)) {
1077 /* Don't change; min configuration */
1078 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1079 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1080 } else {
1081 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1082 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1083 }
1084
1085 /* the default log size is 16k or 32k which is one header sector */
1086 log->l_iclog_hsize = BBSIZE;
1087 log->l_iclog_heads = 1;
1088
1089 /*
1090 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1091 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1092 */
1093 if (mp->m_sb.sb_blocksize >= 16*1024) {
1094 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1095 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001096 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 switch (mp->m_sb.sb_blocksize) {
1098 case 16*1024: /* 16 KB */
1099 log->l_iclog_bufs = 3;
1100 break;
1101 case 32*1024: /* 32 KB */
1102 log->l_iclog_bufs = 4;
1103 break;
1104 case 64*1024: /* 64 KB */
1105 log->l_iclog_bufs = 8;
1106 break;
1107 default:
1108 xlog_panic("XFS: Invalid blocksize");
1109 break;
1110 }
1111 }
1112 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001113
1114done: /* are we being asked to make the sizes selected above visible? */
1115 if (mp->m_logbufs == 0)
1116 mp->m_logbufs = log->l_iclog_bufs;
1117 if (mp->m_logbsize == 0)
1118 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119} /* xlog_get_iclog_buffer_size */
1120
1121
1122/*
1123 * This routine initializes some of the log structure for a given mount point.
1124 * Its primary purpose is to fill in enough, so recovery can occur. However,
1125 * some other stuff may be filled in too.
1126 */
1127STATIC xlog_t *
1128xlog_alloc_log(xfs_mount_t *mp,
1129 xfs_buftarg_t *log_target,
1130 xfs_daddr_t blk_offset,
1131 int num_bblks)
1132{
1133 xlog_t *log;
1134 xlog_rec_header_t *head;
1135 xlog_in_core_t **iclogp;
1136 xlog_in_core_t *iclog, *prev_iclog=NULL;
1137 xfs_buf_t *bp;
1138 int i;
1139 int iclogsize;
1140
1141 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1142
1143 log->l_mp = mp;
1144 log->l_targ = log_target;
1145 log->l_logsize = BBTOB(num_bblks);
1146 log->l_logBBstart = blk_offset;
1147 log->l_logBBsize = num_bblks;
1148 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1149 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1150
1151 log->l_prev_block = -1;
1152 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1153 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1154 log->l_last_sync_lsn = log->l_tail_lsn;
1155 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1156 log->l_grant_reserve_cycle = 1;
1157 log->l_grant_write_cycle = 1;
1158
1159 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1160 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1161 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1162 /* for larger sector sizes, must have v2 or external log */
1163 ASSERT(log->l_sectbb_log == 0 ||
1164 log->l_logBBstart == 0 ||
1165 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1166 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1167 }
1168 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1169
1170 xlog_get_iclog_buffer_size(mp, log);
1171
1172 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1173 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1174 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1175 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1176 ASSERT(XFS_BUF_ISBUSY(bp));
1177 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1178 log->l_xbuf = bp;
1179
1180 spinlock_init(&log->l_icloglock, "iclog");
1181 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1182 initnsema(&log->l_flushsema, 0, "ic-flush");
1183 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1184
1185 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1186 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1187
1188 iclogp = &log->l_iclog;
1189 /*
1190 * The amount of memory to allocate for the iclog structure is
1191 * rather funky due to the way the structure is defined. It is
1192 * done this way so that we can use different sizes for machines
1193 * with different amounts of memory. See the definition of
1194 * xlog_in_core_t in xfs_log_priv.h for details.
1195 */
1196 iclogsize = log->l_iclog_size;
1197 ASSERT(log->l_iclog_size >= 4096);
1198 for (i=0; i < log->l_iclog_bufs; i++) {
1199 *iclogp = (xlog_in_core_t *)
1200 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1201 iclog = *iclogp;
1202 iclog->hic_data = (xlog_in_core_2_t *)
Nathan Scottefb8ad72006-09-28 11:03:05 +10001203 kmem_zalloc(iclogsize, KM_SLEEP | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 iclog->ic_prev = prev_iclog;
1206 prev_iclog = iclog;
1207 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1208
1209 head = &iclog->ic_header;
1210 memset(head, 0, sizeof(xlog_rec_header_t));
1211 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1212 INT_SET(head->h_version, ARCH_CONVERT,
1213 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1214 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1215 /* new fields */
1216 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1217 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1218
1219 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1220 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1221 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1222 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1223 iclog->ic_bp = bp;
1224
1225 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1226 iclog->ic_state = XLOG_STATE_ACTIVE;
1227 iclog->ic_log = log;
1228 iclog->ic_callback_tail = &(iclog->ic_callback);
1229 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1230
1231 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1232 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1233 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1234 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1235
1236 iclogp = &iclog->ic_next;
1237 }
1238 *iclogp = log->l_iclog; /* complete ring */
1239 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1240
1241 return log;
1242} /* xlog_alloc_log */
1243
1244
1245/*
1246 * Write out the commit record of a transaction associated with the given
1247 * ticket. Return the lsn of the commit record.
1248 */
1249STATIC int
1250xlog_commit_record(xfs_mount_t *mp,
1251 xlog_ticket_t *ticket,
1252 xlog_in_core_t **iclog,
1253 xfs_lsn_t *commitlsnp)
1254{
1255 int error;
1256 xfs_log_iovec_t reg[1];
1257
1258 reg[0].i_addr = NULL;
1259 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001260 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 ASSERT_ALWAYS(iclog);
1263 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1264 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001265 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001267 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268} /* xlog_commit_record */
1269
1270
1271/*
1272 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1273 * log space. This code pushes on the lsn which would supposedly free up
1274 * the 25% which we want to leave free. We may need to adopt a policy which
1275 * pushes on an lsn which is further along in the log once we reach the high
1276 * water mark. In this manner, we would be creating a low water mark.
1277 */
1278void
1279xlog_grant_push_ail(xfs_mount_t *mp,
1280 int need_bytes)
1281{
1282 xlog_t *log = mp->m_log; /* pointer to the log */
1283 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1284 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1285 int free_blocks; /* free blocks left to write to */
1286 int free_bytes; /* free bytes left to write to */
1287 int threshold_block; /* block in lsn we'd like to be at */
1288 int threshold_cycle; /* lsn cycle we'd like to be at */
1289 int free_threshold;
1290 SPLDECL(s);
1291
1292 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1293
1294 s = GRANT_LOCK(log);
1295 free_bytes = xlog_space_left(log,
1296 log->l_grant_reserve_cycle,
1297 log->l_grant_reserve_bytes);
1298 tail_lsn = log->l_tail_lsn;
1299 free_blocks = BTOBBT(free_bytes);
1300
1301 /*
1302 * Set the threshold for the minimum number of free blocks in the
1303 * log to the maximum of what the caller needs, one quarter of the
1304 * log, and 256 blocks.
1305 */
1306 free_threshold = BTOBB(need_bytes);
1307 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1308 free_threshold = MAX(free_threshold, 256);
1309 if (free_blocks < free_threshold) {
1310 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1311 threshold_cycle = CYCLE_LSN(tail_lsn);
1312 if (threshold_block >= log->l_logBBsize) {
1313 threshold_block -= log->l_logBBsize;
1314 threshold_cycle += 1;
1315 }
1316 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1317 threshold_block);
1318
1319 /* Don't pass in an lsn greater than the lsn of the last
1320 * log record known to be on disk.
1321 */
1322 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1323 threshold_lsn = log->l_last_sync_lsn;
1324 }
1325 GRANT_UNLOCK(log, s);
1326
1327 /*
1328 * Get the transaction layer to kick the dirty buffers out to
1329 * disk asynchronously. No point in trying to do this if
1330 * the filesystem is shutting down.
1331 */
1332 if (threshold_lsn &&
1333 !XLOG_FORCED_SHUTDOWN(log))
1334 xfs_trans_push_ail(mp, threshold_lsn);
1335} /* xlog_grant_push_ail */
1336
1337
1338/*
1339 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1340 * fashion. Previously, we should have moved the current iclog
1341 * ptr in the log to point to the next available iclog. This allows further
1342 * write to continue while this code syncs out an iclog ready to go.
1343 * Before an in-core log can be written out, the data section must be scanned
1344 * to save away the 1st word of each BBSIZE block into the header. We replace
1345 * it with the current cycle count. Each BBSIZE block is tagged with the
1346 * cycle count because there in an implicit assumption that drives will
1347 * guarantee that entire 512 byte blocks get written at once. In other words,
1348 * we can't have part of a 512 byte block written and part not written. By
1349 * tagging each block, we will know which blocks are valid when recovering
1350 * after an unclean shutdown.
1351 *
1352 * This routine is single threaded on the iclog. No other thread can be in
1353 * this routine with the same iclog. Changing contents of iclog can there-
1354 * fore be done without grabbing the state machine lock. Updating the global
1355 * log will require grabbing the lock though.
1356 *
1357 * The entire log manager uses a logical block numbering scheme. Only
1358 * log_sync (and then only bwrite()) know about the fact that the log may
1359 * not start with block zero on a given device. The log block start offset
1360 * is added immediately before calling bwrite().
1361 */
1362
1363int
1364xlog_sync(xlog_t *log,
1365 xlog_in_core_t *iclog)
1366{
1367 xfs_caddr_t dptr; /* pointer to byte sized element */
1368 xfs_buf_t *bp;
1369 int i, ops;
1370 uint count; /* byte count of bwrite */
1371 uint count_init; /* initial count before roundup */
1372 int roundoff; /* roundoff to BB or stripe */
1373 int split = 0; /* split write into two regions */
1374 int error;
1375 SPLDECL(s);
1376 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1377
1378 XFS_STATS_INC(xs_log_writes);
1379 ASSERT(iclog->ic_refcnt == 0);
1380
1381 /* Add for LR header */
1382 count_init = log->l_iclog_hsize + iclog->ic_offset;
1383
1384 /* Round out the log write size */
1385 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1386 /* we have a v2 stripe unit to use */
1387 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1388 } else {
1389 count = BBTOB(BTOBB(count_init));
1390 }
1391 roundoff = count - count_init;
1392 ASSERT(roundoff >= 0);
1393 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1394 roundoff < log->l_mp->m_sb.sb_logsunit)
1395 ||
1396 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1397 roundoff < BBTOB(1)));
1398
1399 /* move grant heads by roundoff in sync */
1400 s = GRANT_LOCK(log);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001401 xlog_grant_add_space(log, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 GRANT_UNLOCK(log, s);
1403
1404 /* put cycle number in every block */
1405 xlog_pack_data(log, iclog, roundoff);
1406
1407 /* real byte length */
1408 if (v2) {
1409 INT_SET(iclog->ic_header.h_len,
1410 ARCH_CONVERT,
1411 iclog->ic_offset + roundoff);
1412 } else {
1413 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1414 }
1415
1416 /* put ops count in correct order */
1417 ops = iclog->ic_header.h_num_logops;
1418 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1419
Nathan Scottf5faad72006-07-28 17:04:44 +10001420 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1422 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1423 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1424
1425 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1426
1427 /* Do we need to split this write into 2 parts? */
1428 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1429 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1430 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1431 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1432 } else {
1433 iclog->ic_bwritecnt = 1;
1434 }
1435 XFS_BUF_SET_PTR(bp, (xfs_caddr_t) &(iclog->ic_header), count);
1436 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001437 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 XFS_BUF_BUSY(bp);
1439 XFS_BUF_ASYNC(bp);
1440 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001441 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001442 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001444 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001445 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1448 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1449
1450 xlog_verify_iclog(log, iclog, count, B_TRUE);
1451
1452 /* account for log which doesn't start at block #0 */
1453 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1454 /*
1455 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1456 * is shutting down.
1457 */
1458 XFS_BUF_WRITE(bp);
1459
1460 if ((error = XFS_bwrite(bp))) {
1461 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1462 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001463 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001466 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1468 (unsigned long)1);
1469 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1470 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1471 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1472 (__psint_t)count), split);
1473 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001474 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 XFS_BUF_BUSY(bp);
1476 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001477 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1478 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 dptr = XFS_BUF_PTR(bp);
1480 /*
1481 * Bump the cycle numbers at the start of each block
1482 * since this part of the buffer is at the start of
1483 * a new cycle. Watch out for the header magic number
1484 * case, though.
1485 */
1486 for (i=0; i<split; i += BBSIZE) {
1487 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1488 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1489 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1490 dptr += BBSIZE;
1491 }
1492
1493 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1494 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1495
Nathan Scottc41564b2006-03-29 08:55:14 +10001496 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1498 XFS_BUF_WRITE(bp);
1499 if ((error = XFS_bwrite(bp))) {
1500 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1501 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001502 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506} /* xlog_sync */
1507
1508
1509/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001510 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512void
Nathan Scottc41564b2006-03-29 08:55:14 +10001513xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 xlog_in_core_t *iclog, *next_iclog;
1516 xlog_ticket_t *tic, *next_tic;
1517 int i;
1518
1519
1520 iclog = log->l_iclog;
1521 for (i=0; i<log->l_iclog_bufs; i++) {
1522 sv_destroy(&iclog->ic_forcesema);
1523 sv_destroy(&iclog->ic_writesema);
1524 xfs_buf_free(iclog->ic_bp);
1525#ifdef XFS_LOG_TRACE
1526 if (iclog->ic_trace != NULL) {
1527 ktrace_free(iclog->ic_trace);
1528 }
1529#endif
1530 next_iclog = iclog->ic_next;
1531 kmem_free(iclog->hic_data, log->l_iclog_size);
1532 kmem_free(iclog, sizeof(xlog_in_core_t));
1533 iclog = next_iclog;
1534 }
1535 freesema(&log->l_flushsema);
1536 spinlock_destroy(&log->l_icloglock);
1537 spinlock_destroy(&log->l_grant_lock);
1538
1539 /* XXXsup take a look at this again. */
1540 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1541 !XLOG_FORCED_SHUTDOWN(log)) {
1542 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001543 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 log->l_ticket_cnt, log->l_ticket_tcnt);
1545 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1546
1547 } else {
1548 tic = log->l_unmount_free;
1549 while (tic) {
1550 next_tic = tic->t_next;
1551 kmem_free(tic, NBPP);
1552 tic = next_tic;
1553 }
1554 }
1555 xfs_buf_free(log->l_xbuf);
1556#ifdef XFS_LOG_TRACE
1557 if (log->l_trace != NULL) {
1558 ktrace_free(log->l_trace);
1559 }
1560 if (log->l_grant_trace != NULL) {
1561 ktrace_free(log->l_grant_trace);
1562 }
1563#endif
1564 log->l_mp->m_log = NULL;
1565 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001566} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/*
1569 * Update counters atomically now that memcpy is done.
1570 */
1571/* ARGSUSED */
1572static inline void
1573xlog_state_finish_copy(xlog_t *log,
1574 xlog_in_core_t *iclog,
1575 int record_cnt,
1576 int copy_bytes)
1577{
1578 SPLDECL(s);
1579
1580 s = LOG_LOCK(log);
1581
1582 iclog->ic_header.h_num_logops += record_cnt;
1583 iclog->ic_offset += copy_bytes;
1584
1585 LOG_UNLOCK(log, s);
1586} /* xlog_state_finish_copy */
1587
1588
1589
1590
1591/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001592 * print out info relating to regions written which consume
1593 * the reservation
1594 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001595STATIC void
1596xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1597{
1598 uint i;
1599 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1600
1601 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1602 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1603 "bformat",
1604 "bchunk",
1605 "efi_format",
1606 "efd_format",
1607 "iformat",
1608 "icore",
1609 "iext",
1610 "ibroot",
1611 "ilocal",
1612 "iattr_ext",
1613 "iattr_broot",
1614 "iattr_local",
1615 "qformat",
1616 "dquot",
1617 "quotaoff",
1618 "LR header",
1619 "unmount",
1620 "commit",
1621 "trans header"
1622 };
1623 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1624 "SETATTR_NOT_SIZE",
1625 "SETATTR_SIZE",
1626 "INACTIVE",
1627 "CREATE",
1628 "CREATE_TRUNC",
1629 "TRUNCATE_FILE",
1630 "REMOVE",
1631 "LINK",
1632 "RENAME",
1633 "MKDIR",
1634 "RMDIR",
1635 "SYMLINK",
1636 "SET_DMATTRS",
1637 "GROWFS",
1638 "STRAT_WRITE",
1639 "DIOSTRAT",
1640 "WRITE_SYNC",
1641 "WRITEID",
1642 "ADDAFORK",
1643 "ATTRINVAL",
1644 "ATRUNCATE",
1645 "ATTR_SET",
1646 "ATTR_RM",
1647 "ATTR_FLAG",
1648 "CLEAR_AGI_BUCKET",
1649 "QM_SBCHANGE",
1650 "DUMMY1",
1651 "DUMMY2",
1652 "QM_QUOTAOFF",
1653 "QM_DQALLOC",
1654 "QM_SETQLIM",
1655 "QM_DQCLUSTER",
1656 "QM_QINOCREATE",
1657 "QM_QUOTAOFF_END",
1658 "SB_UNIT",
1659 "FSYNC_TS",
1660 "GROWFSRT_ALLOC",
1661 "GROWFSRT_ZERO",
1662 "GROWFSRT_FREE",
1663 "SWAPEXT"
1664 };
1665
1666 xfs_fs_cmn_err(CE_WARN, mp,
1667 "xfs_log_write: reservation summary:\n"
1668 " trans type = %s (%u)\n"
1669 " unit res = %d bytes\n"
1670 " current res = %d bytes\n"
1671 " total reg = %u bytes (o/flow = %u bytes)\n"
1672 " ophdrs = %u (ophdr space = %u bytes)\n"
1673 " ophdr + reg = %u bytes\n"
1674 " num regions = %u\n",
1675 ((ticket->t_trans_type <= 0 ||
1676 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1677 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1678 ticket->t_trans_type,
1679 ticket->t_unit_res,
1680 ticket->t_curr_res,
1681 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1682 ticket->t_res_num_ophdrs, ophdr_spc,
1683 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001684 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001685 ticket->t_res_num);
1686
1687 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001688 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001689 cmn_err(CE_WARN,
1690 "region[%u]: %s - %u bytes\n",
1691 i,
1692 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1693 "bad-rtype" : res_type_str[r_type-1]),
1694 ticket->t_res_arr[i].r_len);
1695 }
1696}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001697
1698/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * Write some region out to in-core log
1700 *
1701 * This will be called when writing externally provided regions or when
1702 * writing out a commit record for a given transaction.
1703 *
1704 * General algorithm:
1705 * 1. Find total length of this write. This may include adding to the
1706 * lengths passed in.
1707 * 2. Check whether we violate the tickets reservation.
1708 * 3. While writing to this iclog
1709 * A. Reserve as much space in this iclog as can get
1710 * B. If this is first write, save away start lsn
1711 * C. While writing this region:
1712 * 1. If first write of transaction, write start record
1713 * 2. Write log operation header (header per region)
1714 * 3. Find out if we can fit entire region into this iclog
1715 * 4. Potentially, verify destination memcpy ptr
1716 * 5. Memcpy (partial) region
1717 * 6. If partial copy, release iclog; otherwise, continue
1718 * copying more regions into current iclog
1719 * 4. Mark want sync bit (in simulation mode)
1720 * 5. Release iclog for potential flush to on-disk log.
1721 *
1722 * ERRORS:
1723 * 1. Panic if reservation is overrun. This should never happen since
1724 * reservation amounts are generated internal to the filesystem.
1725 * NOTES:
1726 * 1. Tickets are single threaded data structures.
1727 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1728 * syncing routine. When a single log_write region needs to span
1729 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1730 * on all log operation writes which don't contain the end of the
1731 * region. The XLOG_END_TRANS bit is used for the in-core log
1732 * operation which contains the end of the continued log_write region.
1733 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1734 * we don't really know exactly how much space will be used. As a result,
1735 * we don't update ic_offset until the end when we know exactly how many
1736 * bytes have been written out.
1737 */
1738int
1739xlog_write(xfs_mount_t * mp,
1740 xfs_log_iovec_t reg[],
1741 int nentries,
1742 xfs_log_ticket_t tic,
1743 xfs_lsn_t *start_lsn,
1744 xlog_in_core_t **commit_iclog,
1745 uint flags)
1746{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001747 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001749 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 __psint_t ptr; /* copy address into data region */
1752 int len; /* # xlog_write() bytes 2 still copy */
1753 int index; /* region index currently copying */
1754 int log_offset; /* offset (from 0) into data region */
1755 int start_rec_copy; /* # bytes to copy for start record */
1756 int partial_copy; /* did we split a region? */
1757 int partial_copy_len;/* # bytes copied if split region */
1758 int need_copy; /* # bytes need to memcpy this region */
1759 int copy_len; /* # bytes actually memcpy'ing */
1760 int copy_off; /* # bytes from entry start */
1761 int contwr; /* continued write of in-core log? */
1762 int error;
1763 int record_cnt = 0, data_cnt = 0;
1764
1765 partial_copy_len = partial_copy = 0;
1766
1767 /* Calculate potential maximum space. Each region gets its own
1768 * xlog_op_header_t and may need to be double word aligned.
1769 */
1770 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001771 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 len += sizeof(xlog_op_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001773 XLOG_TIC_ADD_OPHDR(ticket);
1774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 for (index = 0; index < nentries; index++) {
1777 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001778 XLOG_TIC_ADD_OPHDR(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 len += reg[index].i_len;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001780 XLOG_TIC_ADD_REGION(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782 contwr = *start_lsn = 0;
1783
1784 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001785 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786#ifdef DEBUG
1787 xlog_panic(
1788 "xfs_log_write: reservation ran out. Need to up reservation");
1789#else
1790 /* Customer configurable panic */
1791 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1792 "xfs_log_write: reservation ran out. Need to up reservation");
1793 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001794 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795#endif
1796 } else
1797 ticket->t_curr_res -= len;
1798
1799 for (index = 0; index < nentries; ) {
1800 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1801 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001802 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 ASSERT(log_offset <= iclog->ic_size - 1);
1805 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1806
1807 /* start_lsn is the first lsn written to. That's all we need. */
1808 if (! *start_lsn)
1809 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1810
1811 /* This loop writes out as many regions as can fit in the amount
1812 * of space which was allocated by xlog_state_get_iclog_space().
1813 */
1814 while (index < nentries) {
1815 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1816 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1817 start_rec_copy = 0;
1818
1819 /* If first write for transaction, insert start record.
1820 * We can't be trying to commit if we are inited. We can't
1821 * have any "partial_copy" if we are inited.
1822 */
1823 if (ticket->t_flags & XLOG_TIC_INITED) {
1824 logop_head = (xlog_op_header_t *)ptr;
1825 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1826 logop_head->oh_clientid = ticket->t_clientid;
1827 logop_head->oh_len = 0;
1828 logop_head->oh_flags = XLOG_START_TRANS;
1829 logop_head->oh_res2 = 0;
1830 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1831 record_cnt++;
1832
1833 start_rec_copy = sizeof(xlog_op_header_t);
1834 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1835 }
1836
1837 /* Copy log operation header directly into data section */
1838 logop_head = (xlog_op_header_t *)ptr;
1839 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1840 logop_head->oh_clientid = ticket->t_clientid;
1841 logop_head->oh_res2 = 0;
1842
1843 /* header copied directly */
1844 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1845
1846 /* are we copying a commit or unmount record? */
1847 logop_head->oh_flags = flags;
1848
1849 /*
1850 * We've seen logs corrupted with bad transaction client
1851 * ids. This makes sure that XFS doesn't generate them on.
1852 * Turn this into an EIO and shut down the filesystem.
1853 */
1854 switch (logop_head->oh_clientid) {
1855 case XFS_TRANSACTION:
1856 case XFS_VOLUME:
1857 case XFS_LOG:
1858 break;
1859 default:
1860 xfs_fs_cmn_err(CE_WARN, mp,
1861 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1862 logop_head->oh_clientid, tic);
1863 return XFS_ERROR(EIO);
1864 }
1865
1866 /* Partial write last time? => (partial_copy != 0)
1867 * need_copy is the amount we'd like to copy if everything could
1868 * fit in the current memcpy.
1869 */
1870 need_copy = reg[index].i_len - partial_copy_len;
1871
1872 copy_off = partial_copy_len;
1873 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1874 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1875 if (partial_copy)
1876 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1877 partial_copy_len = partial_copy = 0;
1878 } else { /* partial write */
1879 copy_len = iclog->ic_size - log_offset;
1880 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1881 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1882 if (partial_copy)
1883 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1884 partial_copy_len += copy_len;
1885 partial_copy++;
1886 len += sizeof(xlog_op_header_t); /* from splitting of region */
1887 /* account for new log op header */
1888 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001889 XLOG_TIC_ADD_OPHDR(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891 xlog_verify_dest_ptr(log, ptr);
1892
1893 /* copy region */
1894 ASSERT(copy_len >= 0);
1895 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1896 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1897
1898 /* make copy_len total bytes copied, including headers */
1899 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1900 record_cnt++;
1901 data_cnt += contwr ? copy_len : 0;
1902 if (partial_copy) { /* copied partial region */
1903 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1904 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1905 record_cnt = data_cnt = 0;
1906 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001907 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break; /* don't increment index */
1909 } else { /* copied entire region */
1910 index++;
1911 partial_copy_len = partial_copy = 0;
1912
1913 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1914 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1915 record_cnt = data_cnt = 0;
1916 xlog_state_want_sync(log, iclog);
1917 if (commit_iclog) {
1918 ASSERT(flags & XLOG_COMMIT_TRANS);
1919 *commit_iclog = iclog;
1920 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001921 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (index == nentries)
1923 return 0; /* we are done */
1924 else
1925 break;
1926 }
1927 } /* if (partial_copy) */
1928 } /* while (index < nentries) */
1929 } /* for (index = 0; index < nentries; ) */
1930 ASSERT(len == 0);
1931
1932 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1933 if (commit_iclog) {
1934 ASSERT(flags & XLOG_COMMIT_TRANS);
1935 *commit_iclog = iclog;
1936 return 0;
1937 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001938 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939} /* xlog_write */
1940
1941
1942/*****************************************************************************
1943 *
1944 * State Machine functions
1945 *
1946 *****************************************************************************
1947 */
1948
1949/* Clean iclogs starting from the head. This ordering must be
1950 * maintained, so an iclog doesn't become ACTIVE beyond one that
1951 * is SYNCING. This is also required to maintain the notion that we use
1952 * a counting semaphore to hold off would be writers to the log when every
1953 * iclog is trying to sync to disk.
1954 *
1955 * State Change: DIRTY -> ACTIVE
1956 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001957STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958xlog_state_clean_log(xlog_t *log)
1959{
1960 xlog_in_core_t *iclog;
1961 int changed = 0;
1962
1963 iclog = log->l_iclog;
1964 do {
1965 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1966 iclog->ic_state = XLOG_STATE_ACTIVE;
1967 iclog->ic_offset = 0;
1968 iclog->ic_callback = NULL; /* don't need to free */
1969 /*
1970 * If the number of ops in this iclog indicate it just
1971 * contains the dummy transaction, we can
1972 * change state into IDLE (the second time around).
1973 * Otherwise we should change the state into
1974 * NEED a dummy.
1975 * We don't need to cover the dummy.
1976 */
1977 if (!changed &&
1978 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1979 changed = 1;
1980 } else {
1981 /*
1982 * We have two dirty iclogs so start over
1983 * This could also be num of ops indicates
1984 * this is not the dummy going out.
1985 */
1986 changed = 2;
1987 }
1988 iclog->ic_header.h_num_logops = 0;
1989 memset(iclog->ic_header.h_cycle_data, 0,
1990 sizeof(iclog->ic_header.h_cycle_data));
1991 iclog->ic_header.h_lsn = 0;
1992 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1993 /* do nothing */;
1994 else
1995 break; /* stop cleaning */
1996 iclog = iclog->ic_next;
1997 } while (iclog != log->l_iclog);
1998
1999 /* log is locked when we are called */
2000 /*
2001 * Change state for the dummy log recording.
2002 * We usually go to NEED. But we go to NEED2 if the changed indicates
2003 * we are done writing the dummy record.
2004 * If we are done with the second dummy recored (DONE2), then
2005 * we go to IDLE.
2006 */
2007 if (changed) {
2008 switch (log->l_covered_state) {
2009 case XLOG_STATE_COVER_IDLE:
2010 case XLOG_STATE_COVER_NEED:
2011 case XLOG_STATE_COVER_NEED2:
2012 log->l_covered_state = XLOG_STATE_COVER_NEED;
2013 break;
2014
2015 case XLOG_STATE_COVER_DONE:
2016 if (changed == 1)
2017 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2018 else
2019 log->l_covered_state = XLOG_STATE_COVER_NEED;
2020 break;
2021
2022 case XLOG_STATE_COVER_DONE2:
2023 if (changed == 1)
2024 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2025 else
2026 log->l_covered_state = XLOG_STATE_COVER_NEED;
2027 break;
2028
2029 default:
2030 ASSERT(0);
2031 }
2032 }
2033} /* xlog_state_clean_log */
2034
2035STATIC xfs_lsn_t
2036xlog_get_lowest_lsn(
2037 xlog_t *log)
2038{
2039 xlog_in_core_t *lsn_log;
2040 xfs_lsn_t lowest_lsn, lsn;
2041
2042 lsn_log = log->l_iclog;
2043 lowest_lsn = 0;
2044 do {
2045 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2046 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
2047 if ((lsn && !lowest_lsn) ||
2048 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2049 lowest_lsn = lsn;
2050 }
2051 }
2052 lsn_log = lsn_log->ic_next;
2053 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002054 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057
2058STATIC void
2059xlog_state_do_callback(
2060 xlog_t *log,
2061 int aborted,
2062 xlog_in_core_t *ciclog)
2063{
2064 xlog_in_core_t *iclog;
2065 xlog_in_core_t *first_iclog; /* used to know when we've
2066 * processed all iclogs once */
2067 xfs_log_callback_t *cb, *cb_next;
2068 int flushcnt = 0;
2069 xfs_lsn_t lowest_lsn;
2070 int ioerrors; /* counter: iclogs with errors */
2071 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2072 int funcdidcallbacks; /* flag: function did callbacks */
2073 int repeats; /* for issuing console warnings if
2074 * looping too many times */
2075 SPLDECL(s);
2076
2077 s = LOG_LOCK(log);
2078 first_iclog = iclog = log->l_iclog;
2079 ioerrors = 0;
2080 funcdidcallbacks = 0;
2081 repeats = 0;
2082
2083 do {
2084 /*
2085 * Scan all iclogs starting with the one pointed to by the
2086 * log. Reset this starting point each time the log is
2087 * unlocked (during callbacks).
2088 *
2089 * Keep looping through iclogs until one full pass is made
2090 * without running any callbacks.
2091 */
2092 first_iclog = log->l_iclog;
2093 iclog = log->l_iclog;
2094 loopdidcallbacks = 0;
2095 repeats++;
2096
2097 do {
2098
2099 /* skip all iclogs in the ACTIVE & DIRTY states */
2100 if (iclog->ic_state &
2101 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2102 iclog = iclog->ic_next;
2103 continue;
2104 }
2105
2106 /*
2107 * Between marking a filesystem SHUTDOWN and stopping
2108 * the log, we do flush all iclogs to disk (if there
2109 * wasn't a log I/O error). So, we do want things to
2110 * go smoothly in case of just a SHUTDOWN w/o a
2111 * LOG_IO_ERROR.
2112 */
2113 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2114 /*
2115 * Can only perform callbacks in order. Since
2116 * this iclog is not in the DONE_SYNC/
2117 * DO_CALLBACK state, we skip the rest and
2118 * just try to clean up. If we set our iclog
2119 * to DO_CALLBACK, we will not process it when
2120 * we retry since a previous iclog is in the
2121 * CALLBACK and the state cannot change since
2122 * we are holding the LOG_LOCK.
2123 */
2124 if (!(iclog->ic_state &
2125 (XLOG_STATE_DONE_SYNC |
2126 XLOG_STATE_DO_CALLBACK))) {
2127 if (ciclog && (ciclog->ic_state ==
2128 XLOG_STATE_DONE_SYNC)) {
2129 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2130 }
2131 break;
2132 }
2133 /*
2134 * We now have an iclog that is in either the
2135 * DO_CALLBACK or DONE_SYNC states. The other
2136 * states (WANT_SYNC, SYNCING, or CALLBACK were
2137 * caught by the above if and are going to
2138 * clean (i.e. we aren't doing their callbacks)
2139 * see the above if.
2140 */
2141
2142 /*
2143 * We will do one more check here to see if we
2144 * have chased our tail around.
2145 */
2146
2147 lowest_lsn = xlog_get_lowest_lsn(log);
2148 if (lowest_lsn && (
2149 XFS_LSN_CMP(
2150 lowest_lsn,
2151 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2152 )<0)) {
2153 iclog = iclog->ic_next;
2154 continue; /* Leave this iclog for
2155 * another thread */
2156 }
2157
2158 iclog->ic_state = XLOG_STATE_CALLBACK;
2159
2160 LOG_UNLOCK(log, s);
2161
2162 /* l_last_sync_lsn field protected by
2163 * GRANT_LOCK. Don't worry about iclog's lsn.
2164 * No one else can be here except us.
2165 */
2166 s = GRANT_LOCK(log);
2167 ASSERT(XFS_LSN_CMP(
2168 log->l_last_sync_lsn,
2169 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2170 )<=0);
2171 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2172 GRANT_UNLOCK(log, s);
2173
2174 /*
2175 * Keep processing entries in the callback list
2176 * until we come around and it is empty. We
2177 * need to atomically see that the list is
2178 * empty and change the state to DIRTY so that
2179 * we don't miss any more callbacks being added.
2180 */
2181 s = LOG_LOCK(log);
2182 } else {
2183 ioerrors++;
2184 }
2185 cb = iclog->ic_callback;
2186
2187 while (cb != 0) {
2188 iclog->ic_callback_tail = &(iclog->ic_callback);
2189 iclog->ic_callback = NULL;
2190 LOG_UNLOCK(log, s);
2191
2192 /* perform callbacks in the order given */
2193 for (; cb != 0; cb = cb_next) {
2194 cb_next = cb->cb_next;
2195 cb->cb_func(cb->cb_arg, aborted);
2196 }
2197 s = LOG_LOCK(log);
2198 cb = iclog->ic_callback;
2199 }
2200
2201 loopdidcallbacks++;
2202 funcdidcallbacks++;
2203
2204 ASSERT(iclog->ic_callback == 0);
2205 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2206 iclog->ic_state = XLOG_STATE_DIRTY;
2207
2208 /*
2209 * Transition from DIRTY to ACTIVE if applicable.
2210 * NOP if STATE_IOERROR.
2211 */
2212 xlog_state_clean_log(log);
2213
2214 /* wake up threads waiting in xfs_log_force() */
2215 sv_broadcast(&iclog->ic_forcesema);
2216
2217 iclog = iclog->ic_next;
2218 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002219
2220 if (repeats > 5000) {
2221 flushcnt += repeats;
2222 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002224 "%s: possible infinite loop (%d iterations)",
2225 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227 } while (!ioerrors && loopdidcallbacks);
2228
2229 /*
2230 * make one last gasp attempt to see if iclogs are being left in
2231 * limbo..
2232 */
2233#ifdef DEBUG
2234 if (funcdidcallbacks) {
2235 first_iclog = iclog = log->l_iclog;
2236 do {
2237 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2238 /*
2239 * Terminate the loop if iclogs are found in states
2240 * which will cause other threads to clean up iclogs.
2241 *
2242 * SYNCING - i/o completion will go through logs
2243 * DONE_SYNC - interrupt thread should be waiting for
2244 * LOG_LOCK
2245 * IOERROR - give up hope all ye who enter here
2246 */
2247 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2248 iclog->ic_state == XLOG_STATE_SYNCING ||
2249 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2250 iclog->ic_state == XLOG_STATE_IOERROR )
2251 break;
2252 iclog = iclog->ic_next;
2253 } while (first_iclog != iclog);
2254 }
2255#endif
2256
Nathan Scotta3c66852006-09-28 11:02:14 +10002257 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2259 flushcnt = log->l_flushcnt;
2260 log->l_flushcnt = 0;
2261 }
2262 LOG_UNLOCK(log, s);
2263 while (flushcnt--)
2264 vsema(&log->l_flushsema);
2265} /* xlog_state_do_callback */
2266
2267
2268/*
2269 * Finish transitioning this iclog to the dirty state.
2270 *
2271 * Make sure that we completely execute this routine only when this is
2272 * the last call to the iclog. There is a good chance that iclog flushes,
2273 * when we reach the end of the physical log, get turned into 2 separate
2274 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2275 * routine. By using the reference count bwritecnt, we guarantee that only
2276 * the second completion goes through.
2277 *
2278 * Callbacks could take time, so they are done outside the scope of the
2279 * global state machine log lock. Assume that the calls to cvsema won't
2280 * take a long time. At least we know it won't sleep.
2281 */
2282void
2283xlog_state_done_syncing(
2284 xlog_in_core_t *iclog,
2285 int aborted)
2286{
2287 xlog_t *log = iclog->ic_log;
2288 SPLDECL(s);
2289
2290 s = LOG_LOCK(log);
2291
2292 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2293 iclog->ic_state == XLOG_STATE_IOERROR);
2294 ASSERT(iclog->ic_refcnt == 0);
2295 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2296
2297
2298 /*
2299 * If we got an error, either on the first buffer, or in the case of
2300 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2301 * and none should ever be attempted to be written to disk
2302 * again.
2303 */
2304 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2305 if (--iclog->ic_bwritecnt == 1) {
2306 LOG_UNLOCK(log, s);
2307 return;
2308 }
2309 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2310 }
2311
2312 /*
2313 * Someone could be sleeping prior to writing out the next
2314 * iclog buffer, we wake them all, one will get to do the
2315 * I/O, the others get to wait for the result.
2316 */
2317 sv_broadcast(&iclog->ic_writesema);
2318 LOG_UNLOCK(log, s);
2319 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2320} /* xlog_state_done_syncing */
2321
2322
2323/*
2324 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2325 * sleep. The flush semaphore is set to the number of in-core buffers and
2326 * decremented around disk syncing. Therefore, if all buffers are syncing,
2327 * this semaphore will cause new writes to sleep until a sync completes.
2328 * Otherwise, this code just does p() followed by v(). This approximates
2329 * a sleep/wakeup except we can't race.
2330 *
2331 * The in-core logs are used in a circular fashion. They are not used
2332 * out-of-order even when an iclog past the head is free.
2333 *
2334 * return:
2335 * * log_offset where xlog_write() can start writing into the in-core
2336 * log's data space.
2337 * * in-core log pointer to which xlog_write() should write.
2338 * * boolean indicating this is a continued write to an in-core log.
2339 * If this is the last write, then the in-core log's offset field
2340 * needs to be incremented, depending on the amount of data which
2341 * is copied.
2342 */
2343int
2344xlog_state_get_iclog_space(xlog_t *log,
2345 int len,
2346 xlog_in_core_t **iclogp,
2347 xlog_ticket_t *ticket,
2348 int *continued_write,
2349 int *logoffsetp)
2350{
2351 SPLDECL(s);
2352 int log_offset;
2353 xlog_rec_header_t *head;
2354 xlog_in_core_t *iclog;
2355 int error;
2356
2357restart:
2358 s = LOG_LOCK(log);
2359 if (XLOG_FORCED_SHUTDOWN(log)) {
2360 LOG_UNLOCK(log, s);
2361 return XFS_ERROR(EIO);
2362 }
2363
2364 iclog = log->l_iclog;
2365 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2366 log->l_flushcnt++;
2367 LOG_UNLOCK(log, s);
2368 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2369 XFS_STATS_INC(xs_log_noiclogs);
2370 /* Ensure that log writes happen */
2371 psema(&log->l_flushsema, PINOD);
2372 goto restart;
2373 }
2374 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2375 head = &iclog->ic_header;
2376
2377 iclog->ic_refcnt++; /* prevents sync */
2378 log_offset = iclog->ic_offset;
2379
2380 /* On the 1st write to an iclog, figure out lsn. This works
2381 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2382 * committing to. If the offset is set, that's how many blocks
2383 * must be written.
2384 */
2385 if (log_offset == 0) {
2386 ticket->t_curr_res -= log->l_iclog_hsize;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002387 XLOG_TIC_ADD_REGION(ticket,
2388 log->l_iclog_hsize,
2389 XLOG_REG_TYPE_LRHEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2391 ASSIGN_LSN(head->h_lsn, log);
2392 ASSERT(log->l_curr_block >= 0);
2393 }
2394
2395 /* If there is enough room to write everything, then do it. Otherwise,
2396 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2397 * bit is on, so this will get flushed out. Don't update ic_offset
2398 * until you know exactly how many bytes get copied. Therefore, wait
2399 * until later to update ic_offset.
2400 *
2401 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2402 * can fit into remaining data section.
2403 */
2404 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2405 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2406
2407 /* If I'm the only one writing to this iclog, sync it to disk */
2408 if (iclog->ic_refcnt == 1) {
2409 LOG_UNLOCK(log, s);
2410 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002411 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 } else {
2413 iclog->ic_refcnt--;
2414 LOG_UNLOCK(log, s);
2415 }
2416 goto restart;
2417 }
2418
2419 /* Do we have enough room to write the full amount in the remainder
2420 * of this iclog? Or must we continue a write on the next iclog and
2421 * mark this iclog as completely taken? In the case where we switch
2422 * iclogs (to mark it taken), this particular iclog will release/sync
2423 * to disk in xlog_write().
2424 */
2425 if (len <= iclog->ic_size - iclog->ic_offset) {
2426 *continued_write = 0;
2427 iclog->ic_offset += len;
2428 } else {
2429 *continued_write = 1;
2430 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2431 }
2432 *iclogp = iclog;
2433
2434 ASSERT(iclog->ic_offset <= iclog->ic_size);
2435 LOG_UNLOCK(log, s);
2436
2437 *logoffsetp = log_offset;
2438 return 0;
2439} /* xlog_state_get_iclog_space */
2440
2441/*
2442 * Atomically get the log space required for a log ticket.
2443 *
2444 * Once a ticket gets put onto the reserveq, it will only return after
2445 * the needed reservation is satisfied.
2446 */
2447STATIC int
2448xlog_grant_log_space(xlog_t *log,
2449 xlog_ticket_t *tic)
2450{
2451 int free_bytes;
2452 int need_bytes;
2453 SPLDECL(s);
2454#ifdef DEBUG
2455 xfs_lsn_t tail_lsn;
2456#endif
2457
2458
2459#ifdef DEBUG
2460 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2461 panic("grant Recovery problem");
2462#endif
2463
2464 /* Is there space or do we need to sleep? */
2465 s = GRANT_LOCK(log);
2466 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2467
2468 /* something is already sleeping; insert new transaction at end */
2469 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002470 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 xlog_trace_loggrant(log, tic,
2472 "xlog_grant_log_space: sleep 1");
2473 /*
2474 * Gotta check this before going to sleep, while we're
2475 * holding the grant lock.
2476 */
2477 if (XLOG_FORCED_SHUTDOWN(log))
2478 goto error_return;
2479
2480 XFS_STATS_INC(xs_sleep_logspace);
2481 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2482 /*
2483 * If we got an error, and the filesystem is shutting down,
2484 * we'll catch it down below. So just continue...
2485 */
2486 xlog_trace_loggrant(log, tic,
2487 "xlog_grant_log_space: wake 1");
2488 s = GRANT_LOCK(log);
2489 }
2490 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2491 need_bytes = tic->t_unit_res*tic->t_ocnt;
2492 else
2493 need_bytes = tic->t_unit_res;
2494
2495redo:
2496 if (XLOG_FORCED_SHUTDOWN(log))
2497 goto error_return;
2498
2499 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2500 log->l_grant_reserve_bytes);
2501 if (free_bytes < need_bytes) {
2502 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002503 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 xlog_trace_loggrant(log, tic,
2505 "xlog_grant_log_space: sleep 2");
2506 XFS_STATS_INC(xs_sleep_logspace);
2507 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2508
2509 if (XLOG_FORCED_SHUTDOWN(log)) {
2510 s = GRANT_LOCK(log);
2511 goto error_return;
2512 }
2513
2514 xlog_trace_loggrant(log, tic,
2515 "xlog_grant_log_space: wake 2");
2516 xlog_grant_push_ail(log->l_mp, need_bytes);
2517 s = GRANT_LOCK(log);
2518 goto redo;
2519 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002520 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002523 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524#ifdef DEBUG
2525 tail_lsn = log->l_tail_lsn;
2526 /*
2527 * Check to make sure the grant write head didn't just over lap the
2528 * tail. If the cycles are the same, we can't be overlapping.
2529 * Otherwise, make sure that the cycles differ by exactly one and
2530 * check the byte count.
2531 */
2532 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2533 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2534 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2535 }
2536#endif
2537 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2538 xlog_verify_grant_head(log, 1);
2539 GRANT_UNLOCK(log, s);
2540 return 0;
2541
2542 error_return:
2543 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002544 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2546 /*
2547 * If we are failing, make sure the ticket doesn't have any
2548 * current reservations. We don't want to add this back when
2549 * the ticket/transaction gets cancelled.
2550 */
2551 tic->t_curr_res = 0;
2552 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2553 GRANT_UNLOCK(log, s);
2554 return XFS_ERROR(EIO);
2555} /* xlog_grant_log_space */
2556
2557
2558/*
2559 * Replenish the byte reservation required by moving the grant write head.
2560 *
2561 *
2562 */
2563STATIC int
2564xlog_regrant_write_log_space(xlog_t *log,
2565 xlog_ticket_t *tic)
2566{
2567 SPLDECL(s);
2568 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;
Tim Shimmin7e9c6392005-09-02 16:42:05 +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
2585 s = GRANT_LOCK(log);
2586 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)) {
2624 s = GRANT_LOCK(log);
2625 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);
2631 s = GRANT_LOCK(log);
2632 }
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)) {
2651 s = GRANT_LOCK(log);
2652 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);
2658 s = GRANT_LOCK(log);
2659 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);
2675 GRANT_UNLOCK(log, s);
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. */
2690 GRANT_UNLOCK(log, s);
2691 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{
2706 SPLDECL(s);
2707
2708 xlog_trace_loggrant(log, ticket,
2709 "xlog_regrant_reserve_log_space: enter");
2710 if (ticket->t_cnt > 0)
2711 ticket->t_cnt--;
2712
2713 s = GRANT_LOCK(log);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002714 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 ticket->t_curr_res = ticket->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002716 XLOG_TIC_RESET_RES(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 xlog_trace_loggrant(log, ticket,
2718 "xlog_regrant_reserve_log_space: sub current res");
2719 xlog_verify_grant_head(log, 1);
2720
2721 /* just return if we still have some of the pre-reserved space */
2722 if (ticket->t_cnt > 0) {
2723 GRANT_UNLOCK(log, s);
2724 return;
2725 }
2726
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002727 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 xlog_trace_loggrant(log, ticket,
2729 "xlog_regrant_reserve_log_space: exit");
2730 xlog_verify_grant_head(log, 0);
2731 GRANT_UNLOCK(log, s);
2732 ticket->t_curr_res = ticket->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002733 XLOG_TIC_RESET_RES(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734} /* xlog_regrant_reserve_log_space */
2735
2736
2737/*
2738 * Give back the space left from a reservation.
2739 *
2740 * All the information we need to make a correct determination of space left
2741 * is present. For non-permanent reservations, things are quite easy. The
2742 * count should have been decremented to zero. We only need to deal with the
2743 * space remaining in the current reservation part of the ticket. If the
2744 * ticket contains a permanent reservation, there may be left over space which
2745 * needs to be released. A count of N means that N-1 refills of the current
2746 * reservation can be done before we need to ask for more space. The first
2747 * one goes to fill up the first current reservation. Once we run out of
2748 * space, the count will stay at zero and the only space remaining will be
2749 * in the current reservation field.
2750 */
2751STATIC void
2752xlog_ungrant_log_space(xlog_t *log,
2753 xlog_ticket_t *ticket)
2754{
2755 SPLDECL(s);
2756
2757 if (ticket->t_cnt > 0)
2758 ticket->t_cnt--;
2759
2760 s = GRANT_LOCK(log);
2761 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2762
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002763 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2766
2767 /* If this is a permanent reservation ticket, we may be able to free
2768 * up more space based on the remaining count.
2769 */
2770 if (ticket->t_cnt > 0) {
2771 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002772 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
2774
2775 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2776 xlog_verify_grant_head(log, 1);
2777 GRANT_UNLOCK(log, s);
2778 xfs_log_move_tail(log->l_mp, 1);
2779} /* xlog_ungrant_log_space */
2780
2781
2782/*
2783 * Atomically put back used ticket.
2784 */
2785void
2786xlog_state_put_ticket(xlog_t *log,
2787 xlog_ticket_t *tic)
2788{
2789 unsigned long s;
2790
2791 s = LOG_LOCK(log);
2792 xlog_ticket_put(log, tic);
2793 LOG_UNLOCK(log, s);
2794} /* xlog_state_put_ticket */
2795
2796/*
2797 * Flush iclog to disk if this is the last reference to the given iclog and
2798 * the WANT_SYNC bit is set.
2799 *
2800 * When this function is entered, the iclog is not necessarily in the
2801 * WANT_SYNC state. It may be sitting around waiting to get filled.
2802 *
2803 *
2804 */
2805int
2806xlog_state_release_iclog(xlog_t *log,
2807 xlog_in_core_t *iclog)
2808{
2809 SPLDECL(s);
2810 int sync = 0; /* do we sync? */
2811
2812 xlog_assign_tail_lsn(log->l_mp);
2813
2814 s = LOG_LOCK(log);
2815
2816 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2817 LOG_UNLOCK(log, s);
2818 return XFS_ERROR(EIO);
2819 }
2820
2821 ASSERT(iclog->ic_refcnt > 0);
2822 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2823 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2824
2825 if (--iclog->ic_refcnt == 0 &&
2826 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2827 sync++;
2828 iclog->ic_state = XLOG_STATE_SYNCING;
2829 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2830 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2831 /* cycle incremented when incrementing curr_block */
2832 }
2833
2834 LOG_UNLOCK(log, s);
2835
2836 /*
2837 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002838 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2840 * this iclog has consistent data, so we ignore IOERROR
2841 * flags after this point.
2842 */
2843 if (sync) {
2844 return xlog_sync(log, iclog);
2845 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848} /* xlog_state_release_iclog */
2849
2850
2851/*
2852 * This routine will mark the current iclog in the ring as WANT_SYNC
2853 * and move the current iclog pointer to the next iclog in the ring.
2854 * When this routine is called from xlog_state_get_iclog_space(), the
2855 * exact size of the iclog has not yet been determined. All we know is
2856 * that every data block. We have run out of space in this log record.
2857 */
2858STATIC void
2859xlog_state_switch_iclogs(xlog_t *log,
2860 xlog_in_core_t *iclog,
2861 int eventual_size)
2862{
2863 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2864 if (!eventual_size)
2865 eventual_size = iclog->ic_offset;
2866 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2867 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2868 log->l_prev_block = log->l_curr_block;
2869 log->l_prev_cycle = log->l_curr_cycle;
2870
2871 /* roll log?: ic_offset changed later */
2872 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2873
2874 /* Round up to next log-sunit */
2875 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2876 log->l_mp->m_sb.sb_logsunit > 1) {
2877 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2878 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2879 }
2880
2881 if (log->l_curr_block >= log->l_logBBsize) {
2882 log->l_curr_cycle++;
2883 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2884 log->l_curr_cycle++;
2885 log->l_curr_block -= log->l_logBBsize;
2886 ASSERT(log->l_curr_block >= 0);
2887 }
2888 ASSERT(iclog == log->l_iclog);
2889 log->l_iclog = iclog->ic_next;
2890} /* xlog_state_switch_iclogs */
2891
2892
2893/*
2894 * Write out all data in the in-core log as of this exact moment in time.
2895 *
2896 * Data may be written to the in-core log during this call. However,
2897 * we don't guarantee this data will be written out. A change from past
2898 * implementation means this routine will *not* write out zero length LRs.
2899 *
2900 * Basically, we try and perform an intelligent scan of the in-core logs.
2901 * If we determine there is no flushable data, we just return. There is no
2902 * flushable data if:
2903 *
2904 * 1. the current iclog is active and has no data; the previous iclog
2905 * is in the active or dirty state.
2906 * 2. the current iclog is drity, and the previous iclog is in the
2907 * active or dirty state.
2908 *
2909 * We may sleep (call psema) if:
2910 *
2911 * 1. the current iclog is not in the active nor dirty state.
2912 * 2. the current iclog dirty, and the previous iclog is not in the
2913 * active nor dirty state.
2914 * 3. the current iclog is active, and there is another thread writing
2915 * to this particular iclog.
2916 * 4. a) the current iclog is active and has no other writers
2917 * b) when we return from flushing out this iclog, it is still
2918 * not in the active nor dirty state.
2919 */
2920STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002921xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 xlog_in_core_t *iclog;
2924 xfs_lsn_t lsn;
2925 SPLDECL(s);
2926
2927 s = LOG_LOCK(log);
2928
2929 iclog = log->l_iclog;
2930 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2931 LOG_UNLOCK(log, s);
2932 return XFS_ERROR(EIO);
2933 }
2934
2935 /* If the head iclog is not active nor dirty, we just attach
2936 * ourselves to the head and go to sleep.
2937 */
2938 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2939 iclog->ic_state == XLOG_STATE_DIRTY) {
2940 /*
2941 * If the head is dirty or (active and empty), then
2942 * we need to look at the previous iclog. If the previous
2943 * iclog is active or dirty we are done. There is nothing
2944 * to sync out. Otherwise, we attach ourselves to the
2945 * previous iclog and go to sleep.
2946 */
2947 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2948 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2949 iclog = iclog->ic_prev;
2950 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2951 iclog->ic_state == XLOG_STATE_DIRTY)
2952 goto no_sleep;
2953 else
2954 goto maybe_sleep;
2955 } else {
2956 if (iclog->ic_refcnt == 0) {
2957 /* We are the only one with access to this
2958 * iclog. Flush it out now. There should
2959 * be a roundoff of zero to show that someone
2960 * has already taken care of the roundoff from
2961 * the previous sync.
2962 */
2963 iclog->ic_refcnt++;
2964 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2965 xlog_state_switch_iclogs(log, iclog, 0);
2966 LOG_UNLOCK(log, s);
2967
2968 if (xlog_state_release_iclog(log, iclog))
2969 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002970 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 s = LOG_LOCK(log);
2972 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2973 iclog->ic_state != XLOG_STATE_DIRTY)
2974 goto maybe_sleep;
2975 else
2976 goto no_sleep;
2977 } else {
2978 /* Someone else is writing to this iclog.
2979 * Use its call to flush out the data. However,
2980 * the other thread may not force out this LR,
2981 * so we mark it WANT_SYNC.
2982 */
2983 xlog_state_switch_iclogs(log, iclog, 0);
2984 goto maybe_sleep;
2985 }
2986 }
2987 }
2988
2989 /* By the time we come around again, the iclog could've been filled
2990 * which would give it another lsn. If we have a new lsn, just
2991 * return because the relevant data has been flushed.
2992 */
2993maybe_sleep:
2994 if (flags & XFS_LOG_SYNC) {
2995 /*
2996 * We must check if we're shutting down here, before
2997 * we wait, while we're holding the LOG_LOCK.
2998 * Then we check again after waking up, in case our
2999 * sleep was disturbed by a bad news.
3000 */
3001 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3002 LOG_UNLOCK(log, s);
3003 return XFS_ERROR(EIO);
3004 }
3005 XFS_STATS_INC(xs_log_force_sleep);
3006 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3007 /*
3008 * No need to grab the log lock here since we're
3009 * only deciding whether or not to return EIO
3010 * and the memory read should be atomic.
3011 */
3012 if (iclog->ic_state & XLOG_STATE_IOERROR)
3013 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003014 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016 } else {
3017
3018no_sleep:
3019 LOG_UNLOCK(log, s);
3020 }
3021 return 0;
3022} /* xlog_state_sync_all */
3023
3024
3025/*
3026 * Used by code which implements synchronous log forces.
3027 *
3028 * Find in-core log with lsn.
3029 * If it is in the DIRTY state, just return.
3030 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3031 * state and go to sleep or return.
3032 * If it is in any other state, go to sleep or return.
3033 *
3034 * If filesystem activity goes to zero, the iclog will get flushed only by
3035 * bdflush().
3036 */
3037int
3038xlog_state_sync(xlog_t *log,
3039 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003040 uint flags,
3041 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
3043 xlog_in_core_t *iclog;
3044 int already_slept = 0;
3045 SPLDECL(s);
3046
3047
3048try_again:
3049 s = LOG_LOCK(log);
3050 iclog = log->l_iclog;
3051
3052 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3053 LOG_UNLOCK(log, s);
3054 return XFS_ERROR(EIO);
3055 }
3056
3057 do {
3058 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
3059 iclog = iclog->ic_next;
3060 continue;
3061 }
3062
3063 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3064 LOG_UNLOCK(log, s);
3065 return 0;
3066 }
3067
3068 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3069 /*
3070 * We sleep here if we haven't already slept (e.g.
3071 * this is the first time we've looked at the correct
3072 * iclog buf) and the buffer before us is going to
3073 * be sync'ed. The reason for this is that if we
3074 * are doing sync transactions here, by waiting for
3075 * the previous I/O to complete, we can allow a few
3076 * more transactions into this iclog before we close
3077 * it down.
3078 *
3079 * Otherwise, we mark the buffer WANT_SYNC, and bump
3080 * up the refcnt so we can release the log (which drops
3081 * the ref count). The state switch keeps new transaction
3082 * commits from using this buffer. When the current commits
3083 * finish writing into the buffer, the refcount will drop to
3084 * zero and the buffer will go out then.
3085 */
3086 if (!already_slept &&
3087 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3088 XLOG_STATE_SYNCING))) {
3089 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3090 XFS_STATS_INC(xs_log_force_sleep);
3091 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3092 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003093 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 already_slept = 1;
3095 goto try_again;
3096 } else {
3097 iclog->ic_refcnt++;
3098 xlog_state_switch_iclogs(log, iclog, 0);
3099 LOG_UNLOCK(log, s);
3100 if (xlog_state_release_iclog(log, iclog))
3101 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003102 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 s = LOG_LOCK(log);
3104 }
3105 }
3106
3107 if ((flags & XFS_LOG_SYNC) && /* sleep */
3108 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3109
3110 /*
3111 * Don't wait on the forcesema if we know that we've
3112 * gotten a log write error.
3113 */
3114 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3115 LOG_UNLOCK(log, s);
3116 return XFS_ERROR(EIO);
3117 }
3118 XFS_STATS_INC(xs_log_force_sleep);
3119 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3120 /*
3121 * No need to grab the log lock here since we're
3122 * only deciding whether or not to return EIO
3123 * and the memory read should be atomic.
3124 */
3125 if (iclog->ic_state & XLOG_STATE_IOERROR)
3126 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003127 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 } else { /* just return */
3129 LOG_UNLOCK(log, s);
3130 }
3131 return 0;
3132
3133 } while (iclog != log->l_iclog);
3134
3135 LOG_UNLOCK(log, s);
Jesper Juhl014c2542006-01-15 02:37:08 +01003136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137} /* xlog_state_sync */
3138
3139
3140/*
3141 * Called when we want to mark the current iclog as being ready to sync to
3142 * disk.
3143 */
3144void
3145xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3146{
3147 SPLDECL(s);
3148
3149 s = LOG_LOCK(log);
3150
3151 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3152 xlog_state_switch_iclogs(log, iclog, 0);
3153 } else {
3154 ASSERT(iclog->ic_state &
3155 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3156 }
3157
3158 LOG_UNLOCK(log, s);
3159} /* xlog_state_want_sync */
3160
3161
3162
3163/*****************************************************************************
3164 *
3165 * TICKET functions
3166 *
3167 *****************************************************************************
3168 */
3169
3170/*
3171 * Algorithm doesn't take into account page size. ;-(
3172 */
3173STATIC void
3174xlog_state_ticket_alloc(xlog_t *log)
3175{
3176 xlog_ticket_t *t_list;
3177 xlog_ticket_t *next;
3178 xfs_caddr_t buf;
3179 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3180 SPLDECL(s);
3181
3182 /*
3183 * The kmem_zalloc may sleep, so we shouldn't be holding the
3184 * global lock. XXXmiken: may want to use zone allocator.
3185 */
3186 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3187
3188 s = LOG_LOCK(log);
3189
3190 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3191 t_list = (xlog_ticket_t *)buf;
3192 t_list->t_next = log->l_unmount_free;
3193 log->l_unmount_free = t_list++;
3194 log->l_ticket_cnt++;
3195 log->l_ticket_tcnt++;
3196
3197 /* Next ticket becomes first ticket attached to ticket free list */
3198 if (log->l_freelist != NULL) {
3199 ASSERT(log->l_tail != NULL);
3200 log->l_tail->t_next = t_list;
3201 } else {
3202 log->l_freelist = t_list;
3203 }
3204 log->l_ticket_cnt++;
3205 log->l_ticket_tcnt++;
3206
3207 /* Cycle through rest of alloc'ed memory, building up free Q */
3208 for ( ; i > 0; i--) {
3209 next = t_list + 1;
3210 t_list->t_next = next;
3211 t_list = next;
3212 log->l_ticket_cnt++;
3213 log->l_ticket_tcnt++;
3214 }
3215 t_list->t_next = NULL;
3216 log->l_tail = t_list;
3217 LOG_UNLOCK(log, s);
3218} /* xlog_state_ticket_alloc */
3219
3220
3221/*
3222 * Put ticket into free list
3223 *
3224 * Assumption: log lock is held around this call.
3225 */
3226STATIC void
3227xlog_ticket_put(xlog_t *log,
3228 xlog_ticket_t *ticket)
3229{
3230 sv_destroy(&ticket->t_sema);
3231
3232 /*
3233 * Don't think caching will make that much difference. It's
3234 * more important to make debug easier.
3235 */
3236#if 0
3237 /* real code will want to use LIFO for caching */
3238 ticket->t_next = log->l_freelist;
3239 log->l_freelist = ticket;
3240 /* no need to clear fields */
3241#else
3242 /* When we debug, it is easier if tickets are cycled */
3243 ticket->t_next = NULL;
3244 if (log->l_tail != 0) {
3245 log->l_tail->t_next = ticket;
3246 } else {
3247 ASSERT(log->l_freelist == 0);
3248 log->l_freelist = ticket;
3249 }
3250 log->l_tail = ticket;
3251#endif /* DEBUG */
3252 log->l_ticket_cnt++;
3253} /* xlog_ticket_put */
3254
3255
3256/*
3257 * Grab ticket off freelist or allocation some more
3258 */
3259xlog_ticket_t *
3260xlog_ticket_get(xlog_t *log,
3261 int unit_bytes,
3262 int cnt,
3263 char client,
3264 uint xflags)
3265{
3266 xlog_ticket_t *tic;
3267 uint num_headers;
3268 SPLDECL(s);
3269
3270 alloc:
3271 if (log->l_freelist == NULL)
3272 xlog_state_ticket_alloc(log); /* potentially sleep */
3273
3274 s = LOG_LOCK(log);
3275 if (log->l_freelist == NULL) {
3276 LOG_UNLOCK(log, s);
3277 goto alloc;
3278 }
3279 tic = log->l_freelist;
3280 log->l_freelist = tic->t_next;
3281 if (log->l_freelist == NULL)
3282 log->l_tail = NULL;
3283 log->l_ticket_cnt--;
3284 LOG_UNLOCK(log, s);
3285
3286 /*
3287 * Permanent reservations have up to 'cnt'-1 active log operations
3288 * in the log. A unit in this case is the amount of space for one
3289 * of these log operations. Normal reservations have a cnt of 1
3290 * and their unit amount is the total amount of space required.
3291 *
3292 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003293 * which occupy space in the on-disk log.
3294 *
3295 * Normal form of a transaction is:
3296 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3297 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3298 *
3299 * We need to account for all the leadup data and trailer data
3300 * around the transaction data.
3301 * And then we need to account for the worst case in terms of using
3302 * more space.
3303 * The worst case will happen if:
3304 * - the placement of the transaction happens to be such that the
3305 * roundoff is at its maximum
3306 * - the transaction data is synced before the commit record is synced
3307 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3308 * Therefore the commit record is in its own Log Record.
3309 * This can happen as the commit record is called with its
3310 * own region to xlog_write().
3311 * This then means that in the worst case, roundoff can happen for
3312 * the commit-rec as well.
3313 * The commit-rec is smaller than padding in this scenario and so it is
3314 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 */
3316
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003317 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003319 unit_bytes += sizeof(xfs_trans_header_t);
3320
3321 /* for start-rec */
3322 unit_bytes += sizeof(xlog_op_header_t);
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 /* for LR headers */
3325 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3326 unit_bytes += log->l_iclog_hsize * num_headers;
3327
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003328 /* for commit-rec LR header - note: padding will subsume the ophdr */
3329 unit_bytes += log->l_iclog_hsize;
3330
3331 /* for split-recs - ophdrs added when data split over LRs */
3332 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3333
3334 /* for roundoff padding for transaction data and one for commit record */
3335 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3336 log->l_mp->m_sb.sb_logsunit > 1) {
3337 /* log su roundoff */
3338 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3339 } else {
3340 /* BB roundoff */
3341 unit_bytes += 2*BBSIZE;
3342 }
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 tic->t_unit_res = unit_bytes;
3345 tic->t_curr_res = unit_bytes;
3346 tic->t_cnt = cnt;
3347 tic->t_ocnt = cnt;
3348 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3349 tic->t_clientid = client;
3350 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003351 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (xflags & XFS_LOG_PERM_RESERV)
3353 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3354 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3355
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003356 XLOG_TIC_RESET_RES(tic);
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return tic;
3359} /* xlog_ticket_get */
3360
3361
3362/******************************************************************************
3363 *
3364 * Log debug routines
3365 *
3366 ******************************************************************************
3367 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003368#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369/*
3370 * Make sure that the destination ptr is within the valid data region of
3371 * one of the iclogs. This uses backup pointers stored in a different
3372 * part of the log in case we trash the log structure.
3373 */
3374void
3375xlog_verify_dest_ptr(xlog_t *log,
3376 __psint_t ptr)
3377{
3378 int i;
3379 int good_ptr = 0;
3380
3381 for (i=0; i < log->l_iclog_bufs; i++) {
3382 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3383 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3384 good_ptr++;
3385 }
3386 if (! good_ptr)
3387 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3388} /* xlog_verify_dest_ptr */
3389
3390STATIC void
3391xlog_verify_grant_head(xlog_t *log, int equals)
3392{
3393 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3394 if (equals)
3395 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3396 else
3397 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3398 } else {
3399 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3400 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3401 }
3402} /* xlog_verify_grant_head */
3403
3404/* check if it will fit */
3405STATIC void
3406xlog_verify_tail_lsn(xlog_t *log,
3407 xlog_in_core_t *iclog,
3408 xfs_lsn_t tail_lsn)
3409{
3410 int blocks;
3411
3412 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3413 blocks =
3414 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3415 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3416 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3417 } else {
3418 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3419
3420 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3421 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3422
3423 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3424 if (blocks < BTOBB(iclog->ic_offset) + 1)
3425 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3426 }
3427} /* xlog_verify_tail_lsn */
3428
3429/*
3430 * Perform a number of checks on the iclog before writing to disk.
3431 *
3432 * 1. Make sure the iclogs are still circular
3433 * 2. Make sure we have a good magic number
3434 * 3. Make sure we don't have magic numbers in the data
3435 * 4. Check fields of each log operation header for:
3436 * A. Valid client identifier
3437 * B. tid ptr value falls in valid ptr space (user space code)
3438 * C. Length in log record header is correct according to the
3439 * individual operation headers within record.
3440 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3441 * log, check the preceding blocks of the physical log to make sure all
3442 * the cycle numbers agree with the current cycle number.
3443 */
3444STATIC void
3445xlog_verify_iclog(xlog_t *log,
3446 xlog_in_core_t *iclog,
3447 int count,
3448 boolean_t syncing)
3449{
3450 xlog_op_header_t *ophead;
3451 xlog_in_core_t *icptr;
3452 xlog_in_core_2_t *xhdr;
3453 xfs_caddr_t ptr;
3454 xfs_caddr_t base_ptr;
3455 __psint_t field_offset;
3456 __uint8_t clientid;
3457 int len, i, j, k, op_len;
3458 int idx;
3459 SPLDECL(s);
3460
3461 /* check validity of iclog pointers */
3462 s = LOG_LOCK(log);
3463 icptr = log->l_iclog;
3464 for (i=0; i < log->l_iclog_bufs; i++) {
3465 if (icptr == 0)
3466 xlog_panic("xlog_verify_iclog: invalid ptr");
3467 icptr = icptr->ic_next;
3468 }
3469 if (icptr != log->l_iclog)
3470 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3471 LOG_UNLOCK(log, s);
3472
3473 /* check log magic numbers */
3474 ptr = (xfs_caddr_t) &(iclog->ic_header);
3475 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3476 xlog_panic("xlog_verify_iclog: invalid magic num");
3477
3478 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3479 ptr += BBSIZE) {
3480 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3481 xlog_panic("xlog_verify_iclog: unexpected magic num");
3482 }
3483
3484 /* check fields */
3485 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3486 ptr = iclog->ic_datap;
3487 base_ptr = ptr;
3488 ophead = (xlog_op_header_t *)ptr;
3489 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3490 for (i = 0; i < len; i++) {
3491 ophead = (xlog_op_header_t *)ptr;
3492
3493 /* clientid is only 1 byte */
3494 field_offset = (__psint_t)
3495 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3496 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3497 clientid = ophead->oh_clientid;
3498 } else {
3499 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3500 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3501 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3502 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3503 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3504 } else {
3505 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3506 }
3507 }
3508 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003509 cmn_err(CE_WARN, "xlog_verify_iclog: "
3510 "invalid clientid %d op 0x%p offset 0x%lx",
3511 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 /* check length */
3514 field_offset = (__psint_t)
3515 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3516 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3517 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3518 } else {
3519 idx = BTOBBT((__psint_t)&ophead->oh_len -
3520 (__psint_t)iclog->ic_datap);
3521 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3522 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3523 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3524 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3525 } else {
3526 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3527 }
3528 }
3529 ptr += sizeof(xlog_op_header_t) + op_len;
3530 }
3531} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534/*
3535 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3536 */
3537STATIC int
3538xlog_state_ioerror(
3539 xlog_t *log)
3540{
3541 xlog_in_core_t *iclog, *ic;
3542
3543 iclog = log->l_iclog;
3544 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3545 /*
3546 * Mark all the incore logs IOERROR.
3547 * From now on, no log flushes will result.
3548 */
3549 ic = iclog;
3550 do {
3551 ic->ic_state = XLOG_STATE_IOERROR;
3552 ic = ic->ic_next;
3553 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556 /*
3557 * Return non-zero, if state transition has already happened.
3558 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003559 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
3562/*
3563 * This is called from xfs_force_shutdown, when we're forcibly
3564 * shutting down the filesystem, typically because of an IO error.
3565 * Our main objectives here are to make sure that:
3566 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3567 * parties to find out, 'atomically'.
3568 * b. those who're sleeping on log reservations, pinned objects and
3569 * other resources get woken up, and be told the bad news.
3570 * c. nothing new gets queued up after (a) and (b) are done.
3571 * d. if !logerror, flush the iclogs to disk, then seal them off
3572 * for business.
3573 */
3574int
3575xfs_log_force_umount(
3576 struct xfs_mount *mp,
3577 int logerror)
3578{
3579 xlog_ticket_t *tic;
3580 xlog_t *log;
3581 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003582 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 SPLDECL(s);
3584 SPLDECL(s2);
3585
3586 log = mp->m_log;
3587
3588 /*
3589 * If this happens during log recovery, don't worry about
3590 * locking; the log isn't open for business yet.
3591 */
3592 if (!log ||
3593 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3594 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3595 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
3598
3599 /*
3600 * Somebody could've already done the hard work for us.
3601 * No need to get locks for this.
3602 */
3603 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3604 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003605 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
3607 retval = 0;
3608 /*
3609 * We must hold both the GRANT lock and the LOG lock,
3610 * before we mark the filesystem SHUTDOWN and wake
3611 * everybody up to tell the bad news.
3612 */
3613 s = GRANT_LOCK(log);
3614 s2 = LOG_LOCK(log);
3615 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3616 XFS_BUF_DONE(mp->m_sb_bp);
3617 /*
3618 * This flag is sort of redundant because of the mount flag, but
3619 * it's good to maintain the separation between the log and the rest
3620 * of XFS.
3621 */
3622 log->l_flags |= XLOG_IO_ERROR;
3623
3624 /*
3625 * If we hit a log error, we want to mark all the iclogs IOERROR
3626 * while we're still holding the loglock.
3627 */
3628 if (logerror)
3629 retval = xlog_state_ioerror(log);
3630 LOG_UNLOCK(log, s2);
3631
3632 /*
3633 * We don't want anybody waiting for log reservations
3634 * after this. That means we have to wake up everybody
3635 * queued up on reserve_headq as well as write_headq.
3636 * In addition, we make sure in xlog_{re}grant_log_space
3637 * that we don't enqueue anything once the SHUTDOWN flag
3638 * is set, and this action is protected by the GRANTLOCK.
3639 */
3640 if ((tic = log->l_reserve_headq)) {
3641 do {
3642 sv_signal(&tic->t_sema);
3643 tic = tic->t_next;
3644 } while (tic != log->l_reserve_headq);
3645 }
3646
3647 if ((tic = log->l_write_headq)) {
3648 do {
3649 sv_signal(&tic->t_sema);
3650 tic = tic->t_next;
3651 } while (tic != log->l_write_headq);
3652 }
3653 GRANT_UNLOCK(log, s);
3654
3655 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3656 ASSERT(!logerror);
3657 /*
3658 * Force the incore logs to disk before shutting the
3659 * log down completely.
3660 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003661 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 s2 = LOG_LOCK(log);
3663 retval = xlog_state_ioerror(log);
3664 LOG_UNLOCK(log, s2);
3665 }
3666 /*
3667 * Wake up everybody waiting on xfs_log_force.
3668 * Callback all log item committed functions as if the
3669 * log writes were completed.
3670 */
3671 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3672
3673#ifdef XFSERRORDEBUG
3674 {
3675 xlog_in_core_t *iclog;
3676
3677 s = LOG_LOCK(log);
3678 iclog = log->l_iclog;
3679 do {
3680 ASSERT(iclog->ic_callback == 0);
3681 iclog = iclog->ic_next;
3682 } while (iclog != log->l_iclog);
3683 LOG_UNLOCK(log, s);
3684 }
3685#endif
3686 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003687 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003690STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691xlog_iclogs_empty(xlog_t *log)
3692{
3693 xlog_in_core_t *iclog;
3694
3695 iclog = log->l_iclog;
3696 do {
3697 /* endianness does not matter here, zero is zero in
3698 * any language.
3699 */
3700 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 iclog = iclog->ic_next;
3703 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003704 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}