blob: db09e1ea5cf3146f7ffecc74338e25bdc55677ea [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
David Chinner92821e22007-05-24 15:26:31 +1000821 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823
824 s = LOG_LOCK(log);
825 if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
826 (log->l_covered_state == XLOG_STATE_COVER_NEED2))
827 && !xfs_trans_first_ail(mp, &gen)
828 && xlog_iclogs_empty(log)) {
829 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
830 log->l_covered_state = XLOG_STATE_COVER_DONE;
831 else {
832 ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
833 log->l_covered_state = XLOG_STATE_COVER_DONE2;
834 }
835 needed = 1;
836 }
837 LOG_UNLOCK(log, s);
Jesper Juhl014c2542006-01-15 02:37:08 +0100838 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/******************************************************************************
842 *
843 * local routines
844 *
845 ******************************************************************************
846 */
847
848/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
849 * The log manager must keep track of the last LR which was committed
850 * to disk. The lsn of this LR will become the new tail_lsn whenever
851 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
852 * the situation where stuff could be written into the log but nothing
853 * was ever in the AIL when asked. Eventually, we panic since the
854 * tail hits the head.
855 *
856 * We may be holding the log iclog lock upon entering this routine.
857 */
858xfs_lsn_t
859xlog_assign_tail_lsn(xfs_mount_t *mp)
860{
861 xfs_lsn_t tail_lsn;
862 SPLDECL(s);
863 xlog_t *log = mp->m_log;
864
865 tail_lsn = xfs_trans_tail_ail(mp);
866 s = GRANT_LOCK(log);
867 if (tail_lsn != 0) {
868 log->l_tail_lsn = tail_lsn;
869 } else {
870 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
871 }
872 GRANT_UNLOCK(log, s);
873
874 return tail_lsn;
875} /* xlog_assign_tail_lsn */
876
877
878/*
879 * Return the space in the log between the tail and the head. The head
880 * is passed in the cycle/bytes formal parms. In the special case where
881 * the reserve head has wrapped passed the tail, this calculation is no
882 * longer valid. In this case, just return 0 which means there is no space
883 * in the log. This works for all places where this function is called
884 * with the reserve head. Of course, if the write head were to ever
885 * wrap the tail, we should blow up. Rather than catch this case here,
886 * we depend on other ASSERTions in other parts of the code. XXXmiken
887 *
888 * This code also handles the case where the reservation head is behind
889 * the tail. The details of this case are described below, but the end
890 * result is that we return the size of the log as the amount of space left.
891 */
892int
893xlog_space_left(xlog_t *log, int cycle, int bytes)
894{
895 int free_bytes;
896 int tail_bytes;
897 int tail_cycle;
898
899 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
900 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
901 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
902 free_bytes = log->l_logsize - (bytes - tail_bytes);
903 } else if ((tail_cycle + 1) < cycle) {
904 return 0;
905 } else if (tail_cycle < cycle) {
906 ASSERT(tail_cycle == (cycle - 1));
907 free_bytes = tail_bytes - bytes;
908 } else {
909 /*
910 * The reservation head is behind the tail.
911 * In this case we just want to return the size of the
912 * log as the amount of space left.
913 */
914 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
915 "xlog_space_left: head behind tail\n"
916 " tail_cycle = %d, tail_bytes = %d\n"
917 " GH cycle = %d, GH bytes = %d",
918 tail_cycle, tail_bytes, cycle, bytes);
919 ASSERT(0);
920 free_bytes = log->l_logsize;
921 }
922 return free_bytes;
923} /* xlog_space_left */
924
925
926/*
927 * Log function which is called when an io completes.
928 *
929 * The log manager needs its own routine, in order to control what
930 * happens with the buffer after the write completes.
931 */
932void
933xlog_iodone(xfs_buf_t *bp)
934{
935 xlog_in_core_t *iclog;
936 xlog_t *l;
937 int aborted;
938
939 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
940 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
941 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
942 aborted = 0;
943
944 /*
945 * Some versions of cpp barf on the recursive definition of
946 * ic_log -> hic_fields.ic_log and expand ic_log twice when
947 * it is passed through two macros. Workaround broken cpp.
948 */
949 l = iclog->ic_log;
950
951 /*
David Chinner0bfefc42007-05-14 18:24:23 +1000952 * If the ordered flag has been removed by a lower
953 * layer, it means the underlyin device no longer supports
954 * barrier I/O. Warn loudly and turn off barriers.
955 */
956 if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
957 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
958 xfs_fs_cmn_err(CE_WARN, l->l_mp,
959 "xlog_iodone: Barriers are no longer supported"
960 " by device. Disabling barriers\n");
961 xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
962 }
963
964 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * Race to shutdown the filesystem if we see an error.
966 */
967 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
968 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
969 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
970 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000971 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /*
973 * This flag will be propagated to the trans-committed
974 * callback routines to let them know that the log-commit
975 * didn't succeed.
976 */
977 aborted = XFS_LI_ABORTED;
978 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
979 aborted = XFS_LI_ABORTED;
980 }
David Chinner3db296f2007-05-14 18:24:16 +1000981
982 /* log I/O is always issued ASYNC */
983 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000985 /*
986 * do not reference the buffer (bp) here as we could race
987 * with it being freed after writing the unmount record to the
988 * log.
989 */
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991} /* xlog_iodone */
992
993/*
994 * The bdstrat callback function for log bufs. This gives us a central
995 * place to trap bufs in case we get hit by a log I/O error and need to
996 * shutdown. Actually, in practice, even when we didn't get a log error,
997 * we transition the iclogs to IOERROR state *after* flushing all existing
998 * iclogs to disk. This is because we don't want anymore new transactions to be
999 * started or completed afterwards.
1000 */
1001STATIC int
1002xlog_bdstrat_cb(struct xfs_buf *bp)
1003{
1004 xlog_in_core_t *iclog;
1005
1006 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1007
1008 if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
1009 /* note for irix bstrat will need struct bdevsw passed
1010 * Fix the following macro if the code ever is merged
1011 */
1012 XFS_bdstrat(bp);
1013 return 0;
1014 }
1015
1016 xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
1017 XFS_BUF_ERROR(bp, EIO);
1018 XFS_BUF_STALE(bp);
1019 xfs_biodone(bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001020 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022
1023}
1024
1025/*
1026 * Return size of each in-core log record buffer.
1027 *
1028 * Low memory machines only get 2 16KB buffers. We don't want to waste
1029 * memory here. However, all other machines get at least 2 32KB buffers.
1030 * The number is hard coded because we don't care about the minimum
1031 * memory size, just 32MB systems.
1032 *
1033 * If the filesystem blocksize is too large, we may need to choose a
1034 * larger size since the directory code currently logs entire blocks.
1035 */
1036
1037STATIC void
1038xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1039 xlog_t *log)
1040{
1041 int size;
1042 int xhdrs;
1043
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001044 if (mp->m_logbufs <= 0) {
1045 if (xfs_physmem <= btoc(128*1024*1024)) {
1046 log->l_iclog_bufs = XLOG_MIN_ICLOGS;
1047 } else if (xfs_physmem <= btoc(400*1024*1024)) {
1048 log->l_iclog_bufs = XLOG_MED_ICLOGS;
1049 } else { /* 256K with 32K bufs */
1050 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001052 } else {
1053 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
1056 /*
1057 * Buffer size passed in from mount system call.
1058 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001059 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 size = log->l_iclog_size = mp->m_logbsize;
1061 log->l_iclog_size_log = 0;
1062 while (size != 1) {
1063 log->l_iclog_size_log++;
1064 size >>= 1;
1065 }
1066
1067 if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
1068 /* # headers = size / 32K
1069 * one header holds cycles from 32K of data
1070 */
1071
1072 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1073 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1074 xhdrs++;
1075 log->l_iclog_hsize = xhdrs << BBSHIFT;
1076 log->l_iclog_heads = xhdrs;
1077 } else {
1078 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1079 log->l_iclog_hsize = BBSIZE;
1080 log->l_iclog_heads = 1;
1081 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001082 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
1085 /*
1086 * Special case machines that have less than 32MB of memory.
1087 * All machines with more memory use 32KB buffers.
1088 */
1089 if (xfs_physmem <= btoc(32*1024*1024)) {
1090 /* Don't change; min configuration */
1091 log->l_iclog_size = XLOG_RECORD_BSIZE; /* 16k */
1092 log->l_iclog_size_log = XLOG_RECORD_BSHIFT;
1093 } else {
1094 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; /* 32k */
1095 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
1096 }
1097
1098 /* the default log size is 16k or 32k which is one header sector */
1099 log->l_iclog_hsize = BBSIZE;
1100 log->l_iclog_heads = 1;
1101
1102 /*
1103 * For 16KB, we use 3 32KB buffers. For 32KB block sizes, we use
1104 * 4 32KB buffers. For 64KB block sizes, we use 8 32KB buffers.
1105 */
1106 if (mp->m_sb.sb_blocksize >= 16*1024) {
1107 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1108 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001109 if (mp->m_logbufs <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 switch (mp->m_sb.sb_blocksize) {
1111 case 16*1024: /* 16 KB */
1112 log->l_iclog_bufs = 3;
1113 break;
1114 case 32*1024: /* 32 KB */
1115 log->l_iclog_bufs = 4;
1116 break;
1117 case 64*1024: /* 64 KB */
1118 log->l_iclog_bufs = 8;
1119 break;
1120 default:
1121 xlog_panic("XFS: Invalid blocksize");
1122 break;
1123 }
1124 }
1125 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001126
1127done: /* are we being asked to make the sizes selected above visible? */
1128 if (mp->m_logbufs == 0)
1129 mp->m_logbufs = log->l_iclog_bufs;
1130 if (mp->m_logbsize == 0)
1131 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132} /* xlog_get_iclog_buffer_size */
1133
1134
1135/*
1136 * This routine initializes some of the log structure for a given mount point.
1137 * Its primary purpose is to fill in enough, so recovery can occur. However,
1138 * some other stuff may be filled in too.
1139 */
1140STATIC xlog_t *
1141xlog_alloc_log(xfs_mount_t *mp,
1142 xfs_buftarg_t *log_target,
1143 xfs_daddr_t blk_offset,
1144 int num_bblks)
1145{
1146 xlog_t *log;
1147 xlog_rec_header_t *head;
1148 xlog_in_core_t **iclogp;
1149 xlog_in_core_t *iclog, *prev_iclog=NULL;
1150 xfs_buf_t *bp;
1151 int i;
1152 int iclogsize;
1153
1154 log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
1155
1156 log->l_mp = mp;
1157 log->l_targ = log_target;
1158 log->l_logsize = BBTOB(num_bblks);
1159 log->l_logBBstart = blk_offset;
1160 log->l_logBBsize = num_bblks;
1161 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1162 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1163
1164 log->l_prev_block = -1;
1165 ASSIGN_ANY_LSN_HOST(log->l_tail_lsn, 1, 0);
1166 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1167 log->l_last_sync_lsn = log->l_tail_lsn;
1168 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1169 log->l_grant_reserve_cycle = 1;
1170 log->l_grant_write_cycle = 1;
1171
1172 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
1173 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
1174 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
1175 /* for larger sector sizes, must have v2 or external log */
1176 ASSERT(log->l_sectbb_log == 0 ||
1177 log->l_logBBstart == 0 ||
1178 XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
1179 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
1180 }
1181 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1182
1183 xlog_get_iclog_buffer_size(mp, log);
1184
1185 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
1186 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1187 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1188 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1189 ASSERT(XFS_BUF_ISBUSY(bp));
1190 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1191 log->l_xbuf = bp;
1192
1193 spinlock_init(&log->l_icloglock, "iclog");
1194 spinlock_init(&log->l_grant_lock, "grhead_iclog");
1195 initnsema(&log->l_flushsema, 0, "ic-flush");
1196 xlog_state_ticket_alloc(log); /* wait until after icloglock inited */
1197
1198 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1199 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1200
1201 iclogp = &log->l_iclog;
1202 /*
1203 * The amount of memory to allocate for the iclog structure is
1204 * rather funky due to the way the structure is defined. It is
1205 * done this way so that we can use different sizes for machines
1206 * with different amounts of memory. See the definition of
1207 * xlog_in_core_t in xfs_log_priv.h for details.
1208 */
1209 iclogsize = log->l_iclog_size;
1210 ASSERT(log->l_iclog_size >= 4096);
1211 for (i=0; i < log->l_iclog_bufs; i++) {
1212 *iclogp = (xlog_in_core_t *)
1213 kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
1214 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 iclog->ic_prev = prev_iclog;
1216 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001217
1218 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
1219 if (!XFS_BUF_CPSEMA(bp))
1220 ASSERT(0);
1221 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
1222 XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
1223 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1224 iclog->ic_bp = bp;
1225 iclog->hic_data = bp->b_addr;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
1228
1229 head = &iclog->ic_header;
1230 memset(head, 0, sizeof(xlog_rec_header_t));
1231 INT_SET(head->h_magicno, ARCH_CONVERT, XLOG_HEADER_MAGIC_NUM);
1232 INT_SET(head->h_version, ARCH_CONVERT,
1233 XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
1234 INT_SET(head->h_size, ARCH_CONVERT, log->l_iclog_size);
1235 /* new fields */
1236 INT_SET(head->h_fmt, ARCH_CONVERT, XLOG_FMT);
1237 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1241 iclog->ic_state = XLOG_STATE_ACTIVE;
1242 iclog->ic_log = log;
1243 iclog->ic_callback_tail = &(iclog->ic_callback);
1244 iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
1245
1246 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1247 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
1248 sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
1249 sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
1250
1251 iclogp = &iclog->ic_next;
1252 }
1253 *iclogp = log->l_iclog; /* complete ring */
1254 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1255
1256 return log;
1257} /* xlog_alloc_log */
1258
1259
1260/*
1261 * Write out the commit record of a transaction associated with the given
1262 * ticket. Return the lsn of the commit record.
1263 */
1264STATIC int
1265xlog_commit_record(xfs_mount_t *mp,
1266 xlog_ticket_t *ticket,
1267 xlog_in_core_t **iclog,
1268 xfs_lsn_t *commitlsnp)
1269{
1270 int error;
1271 xfs_log_iovec_t reg[1];
1272
1273 reg[0].i_addr = NULL;
1274 reg[0].i_len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001275 XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 ASSERT_ALWAYS(iclog);
1278 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1279 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001280 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001282 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283} /* xlog_commit_record */
1284
1285
1286/*
1287 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1288 * log space. This code pushes on the lsn which would supposedly free up
1289 * the 25% which we want to leave free. We may need to adopt a policy which
1290 * pushes on an lsn which is further along in the log once we reach the high
1291 * water mark. In this manner, we would be creating a low water mark.
1292 */
1293void
1294xlog_grant_push_ail(xfs_mount_t *mp,
1295 int need_bytes)
1296{
1297 xlog_t *log = mp->m_log; /* pointer to the log */
1298 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1299 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1300 int free_blocks; /* free blocks left to write to */
1301 int free_bytes; /* free bytes left to write to */
1302 int threshold_block; /* block in lsn we'd like to be at */
1303 int threshold_cycle; /* lsn cycle we'd like to be at */
1304 int free_threshold;
1305 SPLDECL(s);
1306
1307 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1308
1309 s = GRANT_LOCK(log);
1310 free_bytes = xlog_space_left(log,
1311 log->l_grant_reserve_cycle,
1312 log->l_grant_reserve_bytes);
1313 tail_lsn = log->l_tail_lsn;
1314 free_blocks = BTOBBT(free_bytes);
1315
1316 /*
1317 * Set the threshold for the minimum number of free blocks in the
1318 * log to the maximum of what the caller needs, one quarter of the
1319 * log, and 256 blocks.
1320 */
1321 free_threshold = BTOBB(need_bytes);
1322 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1323 free_threshold = MAX(free_threshold, 256);
1324 if (free_blocks < free_threshold) {
1325 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1326 threshold_cycle = CYCLE_LSN(tail_lsn);
1327 if (threshold_block >= log->l_logBBsize) {
1328 threshold_block -= log->l_logBBsize;
1329 threshold_cycle += 1;
1330 }
1331 ASSIGN_ANY_LSN_HOST(threshold_lsn, threshold_cycle,
1332 threshold_block);
1333
1334 /* Don't pass in an lsn greater than the lsn of the last
1335 * log record known to be on disk.
1336 */
1337 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1338 threshold_lsn = log->l_last_sync_lsn;
1339 }
1340 GRANT_UNLOCK(log, s);
1341
1342 /*
1343 * Get the transaction layer to kick the dirty buffers out to
1344 * disk asynchronously. No point in trying to do this if
1345 * the filesystem is shutting down.
1346 */
1347 if (threshold_lsn &&
1348 !XLOG_FORCED_SHUTDOWN(log))
1349 xfs_trans_push_ail(mp, threshold_lsn);
1350} /* xlog_grant_push_ail */
1351
1352
1353/*
1354 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1355 * fashion. Previously, we should have moved the current iclog
1356 * ptr in the log to point to the next available iclog. This allows further
1357 * write to continue while this code syncs out an iclog ready to go.
1358 * Before an in-core log can be written out, the data section must be scanned
1359 * to save away the 1st word of each BBSIZE block into the header. We replace
1360 * it with the current cycle count. Each BBSIZE block is tagged with the
1361 * cycle count because there in an implicit assumption that drives will
1362 * guarantee that entire 512 byte blocks get written at once. In other words,
1363 * we can't have part of a 512 byte block written and part not written. By
1364 * tagging each block, we will know which blocks are valid when recovering
1365 * after an unclean shutdown.
1366 *
1367 * This routine is single threaded on the iclog. No other thread can be in
1368 * this routine with the same iclog. Changing contents of iclog can there-
1369 * fore be done without grabbing the state machine lock. Updating the global
1370 * log will require grabbing the lock though.
1371 *
1372 * The entire log manager uses a logical block numbering scheme. Only
1373 * log_sync (and then only bwrite()) know about the fact that the log may
1374 * not start with block zero on a given device. The log block start offset
1375 * is added immediately before calling bwrite().
1376 */
1377
1378int
1379xlog_sync(xlog_t *log,
1380 xlog_in_core_t *iclog)
1381{
1382 xfs_caddr_t dptr; /* pointer to byte sized element */
1383 xfs_buf_t *bp;
1384 int i, ops;
1385 uint count; /* byte count of bwrite */
1386 uint count_init; /* initial count before roundup */
1387 int roundoff; /* roundoff to BB or stripe */
1388 int split = 0; /* split write into two regions */
1389 int error;
1390 SPLDECL(s);
1391 int v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
1392
1393 XFS_STATS_INC(xs_log_writes);
1394 ASSERT(iclog->ic_refcnt == 0);
1395
1396 /* Add for LR header */
1397 count_init = log->l_iclog_hsize + iclog->ic_offset;
1398
1399 /* Round out the log write size */
1400 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1401 /* we have a v2 stripe unit to use */
1402 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1403 } else {
1404 count = BBTOB(BTOBB(count_init));
1405 }
1406 roundoff = count - count_init;
1407 ASSERT(roundoff >= 0);
1408 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1409 roundoff < log->l_mp->m_sb.sb_logsunit)
1410 ||
1411 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1412 roundoff < BBTOB(1)));
1413
1414 /* move grant heads by roundoff in sync */
1415 s = GRANT_LOCK(log);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001416 xlog_grant_add_space(log, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 GRANT_UNLOCK(log, s);
1418
1419 /* put cycle number in every block */
1420 xlog_pack_data(log, iclog, roundoff);
1421
1422 /* real byte length */
1423 if (v2) {
1424 INT_SET(iclog->ic_header.h_len,
1425 ARCH_CONVERT,
1426 iclog->ic_offset + roundoff);
1427 } else {
1428 INT_SET(iclog->ic_header.h_len, ARCH_CONVERT, iclog->ic_offset);
1429 }
1430
1431 /* put ops count in correct order */
1432 ops = iclog->ic_header.h_num_logops;
1433 INT_SET(iclog->ic_header.h_num_logops, ARCH_CONVERT, ops);
1434
Nathan Scottf5faad72006-07-28 17:04:44 +10001435 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1437 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1438 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)));
1439
1440 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1441
1442 /* Do we need to split this write into 2 parts? */
1443 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1444 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1445 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1446 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1447 } else {
1448 iclog->ic_bwritecnt = 1;
1449 }
David Chinner511105b2007-05-24 15:21:57 +10001450 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001452 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 XFS_BUF_BUSY(bp);
1454 XFS_BUF_ASYNC(bp);
1455 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001456 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001457 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001459 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001460 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1463 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1464
1465 xlog_verify_iclog(log, iclog, count, B_TRUE);
1466
1467 /* account for log which doesn't start at block #0 */
1468 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1469 /*
1470 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1471 * is shutting down.
1472 */
1473 XFS_BUF_WRITE(bp);
1474
1475 if ((error = XFS_bwrite(bp))) {
1476 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1477 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001481 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1483 (unsigned long)1);
1484 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1485 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1486 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1487 (__psint_t)count), split);
1488 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001489 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 XFS_BUF_BUSY(bp);
1491 XFS_BUF_ASYNC(bp);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001492 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1493 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 dptr = XFS_BUF_PTR(bp);
1495 /*
1496 * Bump the cycle numbers at the start of each block
1497 * since this part of the buffer is at the start of
1498 * a new cycle. Watch out for the header magic number
1499 * case, though.
1500 */
1501 for (i=0; i<split; i += BBSIZE) {
1502 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1503 if (INT_GET(*(uint *)dptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
1504 INT_MOD(*(uint *)dptr, ARCH_CONVERT, +1);
1505 dptr += BBSIZE;
1506 }
1507
1508 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1509 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1510
Nathan Scottc41564b2006-03-29 08:55:14 +10001511 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1513 XFS_BUF_WRITE(bp);
1514 if ((error = XFS_bwrite(bp))) {
1515 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1516 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001517 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521} /* xlog_sync */
1522
1523
1524/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001525 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
1527void
Nathan Scottc41564b2006-03-29 08:55:14 +10001528xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 xlog_in_core_t *iclog, *next_iclog;
1531 xlog_ticket_t *tic, *next_tic;
1532 int i;
1533
1534
1535 iclog = log->l_iclog;
1536 for (i=0; i<log->l_iclog_bufs; i++) {
1537 sv_destroy(&iclog->ic_forcesema);
1538 sv_destroy(&iclog->ic_writesema);
1539 xfs_buf_free(iclog->ic_bp);
1540#ifdef XFS_LOG_TRACE
1541 if (iclog->ic_trace != NULL) {
1542 ktrace_free(iclog->ic_trace);
1543 }
1544#endif
1545 next_iclog = iclog->ic_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 kmem_free(iclog, sizeof(xlog_in_core_t));
1547 iclog = next_iclog;
1548 }
1549 freesema(&log->l_flushsema);
1550 spinlock_destroy(&log->l_icloglock);
1551 spinlock_destroy(&log->l_grant_lock);
1552
1553 /* XXXsup take a look at this again. */
1554 if ((log->l_ticket_cnt != log->l_ticket_tcnt) &&
1555 !XLOG_FORCED_SHUTDOWN(log)) {
1556 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scottc41564b2006-03-29 08:55:14 +10001557 "xlog_dealloc_log: (cnt: %d, total: %d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 log->l_ticket_cnt, log->l_ticket_tcnt);
1559 /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
1560
1561 } else {
1562 tic = log->l_unmount_free;
1563 while (tic) {
1564 next_tic = tic->t_next;
1565 kmem_free(tic, NBPP);
1566 tic = next_tic;
1567 }
1568 }
1569 xfs_buf_free(log->l_xbuf);
1570#ifdef XFS_LOG_TRACE
1571 if (log->l_trace != NULL) {
1572 ktrace_free(log->l_trace);
1573 }
1574 if (log->l_grant_trace != NULL) {
1575 ktrace_free(log->l_grant_trace);
1576 }
1577#endif
1578 log->l_mp->m_log = NULL;
1579 kmem_free(log, sizeof(xlog_t));
Nathan Scottc41564b2006-03-29 08:55:14 +10001580} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582/*
1583 * Update counters atomically now that memcpy is done.
1584 */
1585/* ARGSUSED */
1586static inline void
1587xlog_state_finish_copy(xlog_t *log,
1588 xlog_in_core_t *iclog,
1589 int record_cnt,
1590 int copy_bytes)
1591{
1592 SPLDECL(s);
1593
1594 s = LOG_LOCK(log);
1595
1596 iclog->ic_header.h_num_logops += record_cnt;
1597 iclog->ic_offset += copy_bytes;
1598
1599 LOG_UNLOCK(log, s);
1600} /* xlog_state_finish_copy */
1601
1602
1603
1604
1605/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001606 * print out info relating to regions written which consume
1607 * the reservation
1608 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001609STATIC void
1610xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1611{
1612 uint i;
1613 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1614
1615 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1616 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1617 "bformat",
1618 "bchunk",
1619 "efi_format",
1620 "efd_format",
1621 "iformat",
1622 "icore",
1623 "iext",
1624 "ibroot",
1625 "ilocal",
1626 "iattr_ext",
1627 "iattr_broot",
1628 "iattr_local",
1629 "qformat",
1630 "dquot",
1631 "quotaoff",
1632 "LR header",
1633 "unmount",
1634 "commit",
1635 "trans header"
1636 };
1637 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1638 "SETATTR_NOT_SIZE",
1639 "SETATTR_SIZE",
1640 "INACTIVE",
1641 "CREATE",
1642 "CREATE_TRUNC",
1643 "TRUNCATE_FILE",
1644 "REMOVE",
1645 "LINK",
1646 "RENAME",
1647 "MKDIR",
1648 "RMDIR",
1649 "SYMLINK",
1650 "SET_DMATTRS",
1651 "GROWFS",
1652 "STRAT_WRITE",
1653 "DIOSTRAT",
1654 "WRITE_SYNC",
1655 "WRITEID",
1656 "ADDAFORK",
1657 "ATTRINVAL",
1658 "ATRUNCATE",
1659 "ATTR_SET",
1660 "ATTR_RM",
1661 "ATTR_FLAG",
1662 "CLEAR_AGI_BUCKET",
1663 "QM_SBCHANGE",
1664 "DUMMY1",
1665 "DUMMY2",
1666 "QM_QUOTAOFF",
1667 "QM_DQALLOC",
1668 "QM_SETQLIM",
1669 "QM_DQCLUSTER",
1670 "QM_QINOCREATE",
1671 "QM_QUOTAOFF_END",
1672 "SB_UNIT",
1673 "FSYNC_TS",
1674 "GROWFSRT_ALLOC",
1675 "GROWFSRT_ZERO",
1676 "GROWFSRT_FREE",
1677 "SWAPEXT"
1678 };
1679
1680 xfs_fs_cmn_err(CE_WARN, mp,
1681 "xfs_log_write: reservation summary:\n"
1682 " trans type = %s (%u)\n"
1683 " unit res = %d bytes\n"
1684 " current res = %d bytes\n"
1685 " total reg = %u bytes (o/flow = %u bytes)\n"
1686 " ophdrs = %u (ophdr space = %u bytes)\n"
1687 " ophdr + reg = %u bytes\n"
1688 " num regions = %u\n",
1689 ((ticket->t_trans_type <= 0 ||
1690 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1691 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1692 ticket->t_trans_type,
1693 ticket->t_unit_res,
1694 ticket->t_curr_res,
1695 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1696 ticket->t_res_num_ophdrs, ophdr_spc,
1697 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001698 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001699 ticket->t_res_num);
1700
1701 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001702 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001703 cmn_err(CE_WARN,
1704 "region[%u]: %s - %u bytes\n",
1705 i,
1706 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1707 "bad-rtype" : res_type_str[r_type-1]),
1708 ticket->t_res_arr[i].r_len);
1709 }
1710}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001711
1712/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * Write some region out to in-core log
1714 *
1715 * This will be called when writing externally provided regions or when
1716 * writing out a commit record for a given transaction.
1717 *
1718 * General algorithm:
1719 * 1. Find total length of this write. This may include adding to the
1720 * lengths passed in.
1721 * 2. Check whether we violate the tickets reservation.
1722 * 3. While writing to this iclog
1723 * A. Reserve as much space in this iclog as can get
1724 * B. If this is first write, save away start lsn
1725 * C. While writing this region:
1726 * 1. If first write of transaction, write start record
1727 * 2. Write log operation header (header per region)
1728 * 3. Find out if we can fit entire region into this iclog
1729 * 4. Potentially, verify destination memcpy ptr
1730 * 5. Memcpy (partial) region
1731 * 6. If partial copy, release iclog; otherwise, continue
1732 * copying more regions into current iclog
1733 * 4. Mark want sync bit (in simulation mode)
1734 * 5. Release iclog for potential flush to on-disk log.
1735 *
1736 * ERRORS:
1737 * 1. Panic if reservation is overrun. This should never happen since
1738 * reservation amounts are generated internal to the filesystem.
1739 * NOTES:
1740 * 1. Tickets are single threaded data structures.
1741 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1742 * syncing routine. When a single log_write region needs to span
1743 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1744 * on all log operation writes which don't contain the end of the
1745 * region. The XLOG_END_TRANS bit is used for the in-core log
1746 * operation which contains the end of the continued log_write region.
1747 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1748 * we don't really know exactly how much space will be used. As a result,
1749 * we don't update ic_offset until the end when we know exactly how many
1750 * bytes have been written out.
1751 */
1752int
1753xlog_write(xfs_mount_t * mp,
1754 xfs_log_iovec_t reg[],
1755 int nentries,
1756 xfs_log_ticket_t tic,
1757 xfs_lsn_t *start_lsn,
1758 xlog_in_core_t **commit_iclog,
1759 uint flags)
1760{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001761 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 xlog_ticket_t *ticket = (xlog_ticket_t *)tic;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001763 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 __psint_t ptr; /* copy address into data region */
1766 int len; /* # xlog_write() bytes 2 still copy */
1767 int index; /* region index currently copying */
1768 int log_offset; /* offset (from 0) into data region */
1769 int start_rec_copy; /* # bytes to copy for start record */
1770 int partial_copy; /* did we split a region? */
1771 int partial_copy_len;/* # bytes copied if split region */
1772 int need_copy; /* # bytes need to memcpy this region */
1773 int copy_len; /* # bytes actually memcpy'ing */
1774 int copy_off; /* # bytes from entry start */
1775 int contwr; /* continued write of in-core log? */
1776 int error;
1777 int record_cnt = 0, data_cnt = 0;
1778
1779 partial_copy_len = partial_copy = 0;
1780
1781 /* Calculate potential maximum space. Each region gets its own
1782 * xlog_op_header_t and may need to be double word aligned.
1783 */
1784 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001785 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 len += sizeof(xlog_op_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001787 XLOG_TIC_ADD_OPHDR(ticket);
1788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 for (index = 0; index < nentries; index++) {
1791 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001792 XLOG_TIC_ADD_OPHDR(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 len += reg[index].i_len;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001794 XLOG_TIC_ADD_REGION(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796 contwr = *start_lsn = 0;
1797
1798 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001799 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800#ifdef DEBUG
1801 xlog_panic(
1802 "xfs_log_write: reservation ran out. Need to up reservation");
1803#else
1804 /* Customer configurable panic */
1805 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1806 "xfs_log_write: reservation ran out. Need to up reservation");
1807 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001808 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809#endif
1810 } else
1811 ticket->t_curr_res -= len;
1812
1813 for (index = 0; index < nentries; ) {
1814 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1815 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001816 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 ASSERT(log_offset <= iclog->ic_size - 1);
1819 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1820
1821 /* start_lsn is the first lsn written to. That's all we need. */
1822 if (! *start_lsn)
1823 *start_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
1824
1825 /* This loop writes out as many regions as can fit in the amount
1826 * of space which was allocated by xlog_state_get_iclog_space().
1827 */
1828 while (index < nentries) {
1829 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1830 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1831 start_rec_copy = 0;
1832
1833 /* If first write for transaction, insert start record.
1834 * We can't be trying to commit if we are inited. We can't
1835 * have any "partial_copy" if we are inited.
1836 */
1837 if (ticket->t_flags & XLOG_TIC_INITED) {
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_len = 0;
1842 logop_head->oh_flags = XLOG_START_TRANS;
1843 logop_head->oh_res2 = 0;
1844 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1845 record_cnt++;
1846
1847 start_rec_copy = sizeof(xlog_op_header_t);
1848 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1849 }
1850
1851 /* Copy log operation header directly into data section */
1852 logop_head = (xlog_op_header_t *)ptr;
1853 INT_SET(logop_head->oh_tid, ARCH_CONVERT, ticket->t_tid);
1854 logop_head->oh_clientid = ticket->t_clientid;
1855 logop_head->oh_res2 = 0;
1856
1857 /* header copied directly */
1858 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1859
1860 /* are we copying a commit or unmount record? */
1861 logop_head->oh_flags = flags;
1862
1863 /*
1864 * We've seen logs corrupted with bad transaction client
1865 * ids. This makes sure that XFS doesn't generate them on.
1866 * Turn this into an EIO and shut down the filesystem.
1867 */
1868 switch (logop_head->oh_clientid) {
1869 case XFS_TRANSACTION:
1870 case XFS_VOLUME:
1871 case XFS_LOG:
1872 break;
1873 default:
1874 xfs_fs_cmn_err(CE_WARN, mp,
1875 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1876 logop_head->oh_clientid, tic);
1877 return XFS_ERROR(EIO);
1878 }
1879
1880 /* Partial write last time? => (partial_copy != 0)
1881 * need_copy is the amount we'd like to copy if everything could
1882 * fit in the current memcpy.
1883 */
1884 need_copy = reg[index].i_len - partial_copy_len;
1885
1886 copy_off = partial_copy_len;
1887 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
1888 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len = need_copy);
1889 if (partial_copy)
1890 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1891 partial_copy_len = partial_copy = 0;
1892 } else { /* partial write */
1893 copy_len = iclog->ic_size - log_offset;
1894 INT_SET(logop_head->oh_len, ARCH_CONVERT, copy_len);
1895 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1896 if (partial_copy)
1897 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1898 partial_copy_len += copy_len;
1899 partial_copy++;
1900 len += sizeof(xlog_op_header_t); /* from splitting of region */
1901 /* account for new log op header */
1902 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001903 XLOG_TIC_ADD_OPHDR(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 xlog_verify_dest_ptr(log, ptr);
1906
1907 /* copy region */
1908 ASSERT(copy_len >= 0);
1909 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1910 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1911
1912 /* make copy_len total bytes copied, including headers */
1913 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1914 record_cnt++;
1915 data_cnt += contwr ? copy_len : 0;
1916 if (partial_copy) { /* copied partial region */
1917 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1918 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1919 record_cnt = data_cnt = 0;
1920 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 break; /* don't increment index */
1923 } else { /* copied entire region */
1924 index++;
1925 partial_copy_len = partial_copy = 0;
1926
1927 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1928 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1929 record_cnt = data_cnt = 0;
1930 xlog_state_want_sync(log, iclog);
1931 if (commit_iclog) {
1932 ASSERT(flags & XLOG_COMMIT_TRANS);
1933 *commit_iclog = iclog;
1934 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001935 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (index == nentries)
1937 return 0; /* we are done */
1938 else
1939 break;
1940 }
1941 } /* if (partial_copy) */
1942 } /* while (index < nentries) */
1943 } /* for (index = 0; index < nentries; ) */
1944 ASSERT(len == 0);
1945
1946 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1947 if (commit_iclog) {
1948 ASSERT(flags & XLOG_COMMIT_TRANS);
1949 *commit_iclog = iclog;
1950 return 0;
1951 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001952 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953} /* xlog_write */
1954
1955
1956/*****************************************************************************
1957 *
1958 * State Machine functions
1959 *
1960 *****************************************************************************
1961 */
1962
1963/* Clean iclogs starting from the head. This ordering must be
1964 * maintained, so an iclog doesn't become ACTIVE beyond one that
1965 * is SYNCING. This is also required to maintain the notion that we use
1966 * a counting semaphore to hold off would be writers to the log when every
1967 * iclog is trying to sync to disk.
1968 *
1969 * State Change: DIRTY -> ACTIVE
1970 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001971STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972xlog_state_clean_log(xlog_t *log)
1973{
1974 xlog_in_core_t *iclog;
1975 int changed = 0;
1976
1977 iclog = log->l_iclog;
1978 do {
1979 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1980 iclog->ic_state = XLOG_STATE_ACTIVE;
1981 iclog->ic_offset = 0;
1982 iclog->ic_callback = NULL; /* don't need to free */
1983 /*
1984 * If the number of ops in this iclog indicate it just
1985 * contains the dummy transaction, we can
1986 * change state into IDLE (the second time around).
1987 * Otherwise we should change the state into
1988 * NEED a dummy.
1989 * We don't need to cover the dummy.
1990 */
1991 if (!changed &&
1992 (INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT) == XLOG_COVER_OPS)) {
1993 changed = 1;
1994 } else {
1995 /*
1996 * We have two dirty iclogs so start over
1997 * This could also be num of ops indicates
1998 * this is not the dummy going out.
1999 */
2000 changed = 2;
2001 }
2002 iclog->ic_header.h_num_logops = 0;
2003 memset(iclog->ic_header.h_cycle_data, 0,
2004 sizeof(iclog->ic_header.h_cycle_data));
2005 iclog->ic_header.h_lsn = 0;
2006 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2007 /* do nothing */;
2008 else
2009 break; /* stop cleaning */
2010 iclog = iclog->ic_next;
2011 } while (iclog != log->l_iclog);
2012
2013 /* log is locked when we are called */
2014 /*
2015 * Change state for the dummy log recording.
2016 * We usually go to NEED. But we go to NEED2 if the changed indicates
2017 * we are done writing the dummy record.
2018 * If we are done with the second dummy recored (DONE2), then
2019 * we go to IDLE.
2020 */
2021 if (changed) {
2022 switch (log->l_covered_state) {
2023 case XLOG_STATE_COVER_IDLE:
2024 case XLOG_STATE_COVER_NEED:
2025 case XLOG_STATE_COVER_NEED2:
2026 log->l_covered_state = XLOG_STATE_COVER_NEED;
2027 break;
2028
2029 case XLOG_STATE_COVER_DONE:
2030 if (changed == 1)
2031 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2032 else
2033 log->l_covered_state = XLOG_STATE_COVER_NEED;
2034 break;
2035
2036 case XLOG_STATE_COVER_DONE2:
2037 if (changed == 1)
2038 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2039 else
2040 log->l_covered_state = XLOG_STATE_COVER_NEED;
2041 break;
2042
2043 default:
2044 ASSERT(0);
2045 }
2046 }
2047} /* xlog_state_clean_log */
2048
2049STATIC xfs_lsn_t
2050xlog_get_lowest_lsn(
2051 xlog_t *log)
2052{
2053 xlog_in_core_t *lsn_log;
2054 xfs_lsn_t lowest_lsn, lsn;
2055
2056 lsn_log = log->l_iclog;
2057 lowest_lsn = 0;
2058 do {
2059 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
2060 lsn = INT_GET(lsn_log->ic_header.h_lsn, ARCH_CONVERT);
2061 if ((lsn && !lowest_lsn) ||
2062 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2063 lowest_lsn = lsn;
2064 }
2065 }
2066 lsn_log = lsn_log->ic_next;
2067 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002068 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071
2072STATIC void
2073xlog_state_do_callback(
2074 xlog_t *log,
2075 int aborted,
2076 xlog_in_core_t *ciclog)
2077{
2078 xlog_in_core_t *iclog;
2079 xlog_in_core_t *first_iclog; /* used to know when we've
2080 * processed all iclogs once */
2081 xfs_log_callback_t *cb, *cb_next;
2082 int flushcnt = 0;
2083 xfs_lsn_t lowest_lsn;
2084 int ioerrors; /* counter: iclogs with errors */
2085 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2086 int funcdidcallbacks; /* flag: function did callbacks */
2087 int repeats; /* for issuing console warnings if
2088 * looping too many times */
2089 SPLDECL(s);
2090
2091 s = LOG_LOCK(log);
2092 first_iclog = iclog = log->l_iclog;
2093 ioerrors = 0;
2094 funcdidcallbacks = 0;
2095 repeats = 0;
2096
2097 do {
2098 /*
2099 * Scan all iclogs starting with the one pointed to by the
2100 * log. Reset this starting point each time the log is
2101 * unlocked (during callbacks).
2102 *
2103 * Keep looping through iclogs until one full pass is made
2104 * without running any callbacks.
2105 */
2106 first_iclog = log->l_iclog;
2107 iclog = log->l_iclog;
2108 loopdidcallbacks = 0;
2109 repeats++;
2110
2111 do {
2112
2113 /* skip all iclogs in the ACTIVE & DIRTY states */
2114 if (iclog->ic_state &
2115 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2116 iclog = iclog->ic_next;
2117 continue;
2118 }
2119
2120 /*
2121 * Between marking a filesystem SHUTDOWN and stopping
2122 * the log, we do flush all iclogs to disk (if there
2123 * wasn't a log I/O error). So, we do want things to
2124 * go smoothly in case of just a SHUTDOWN w/o a
2125 * LOG_IO_ERROR.
2126 */
2127 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2128 /*
2129 * Can only perform callbacks in order. Since
2130 * this iclog is not in the DONE_SYNC/
2131 * DO_CALLBACK state, we skip the rest and
2132 * just try to clean up. If we set our iclog
2133 * to DO_CALLBACK, we will not process it when
2134 * we retry since a previous iclog is in the
2135 * CALLBACK and the state cannot change since
2136 * we are holding the LOG_LOCK.
2137 */
2138 if (!(iclog->ic_state &
2139 (XLOG_STATE_DONE_SYNC |
2140 XLOG_STATE_DO_CALLBACK))) {
2141 if (ciclog && (ciclog->ic_state ==
2142 XLOG_STATE_DONE_SYNC)) {
2143 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2144 }
2145 break;
2146 }
2147 /*
2148 * We now have an iclog that is in either the
2149 * DO_CALLBACK or DONE_SYNC states. The other
2150 * states (WANT_SYNC, SYNCING, or CALLBACK were
2151 * caught by the above if and are going to
2152 * clean (i.e. we aren't doing their callbacks)
2153 * see the above if.
2154 */
2155
2156 /*
2157 * We will do one more check here to see if we
2158 * have chased our tail around.
2159 */
2160
2161 lowest_lsn = xlog_get_lowest_lsn(log);
2162 if (lowest_lsn && (
2163 XFS_LSN_CMP(
2164 lowest_lsn,
2165 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2166 )<0)) {
2167 iclog = iclog->ic_next;
2168 continue; /* Leave this iclog for
2169 * another thread */
2170 }
2171
2172 iclog->ic_state = XLOG_STATE_CALLBACK;
2173
2174 LOG_UNLOCK(log, s);
2175
2176 /* l_last_sync_lsn field protected by
2177 * GRANT_LOCK. Don't worry about iclog's lsn.
2178 * No one else can be here except us.
2179 */
2180 s = GRANT_LOCK(log);
2181 ASSERT(XFS_LSN_CMP(
2182 log->l_last_sync_lsn,
2183 INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT)
2184 )<=0);
2185 log->l_last_sync_lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2186 GRANT_UNLOCK(log, s);
2187
2188 /*
2189 * Keep processing entries in the callback list
2190 * until we come around and it is empty. We
2191 * need to atomically see that the list is
2192 * empty and change the state to DIRTY so that
2193 * we don't miss any more callbacks being added.
2194 */
2195 s = LOG_LOCK(log);
2196 } else {
2197 ioerrors++;
2198 }
2199 cb = iclog->ic_callback;
2200
Christoph Hellwig4b809162007-08-16 15:37:36 +10002201 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 iclog->ic_callback_tail = &(iclog->ic_callback);
2203 iclog->ic_callback = NULL;
2204 LOG_UNLOCK(log, s);
2205
2206 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002207 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 cb_next = cb->cb_next;
2209 cb->cb_func(cb->cb_arg, aborted);
2210 }
2211 s = LOG_LOCK(log);
2212 cb = iclog->ic_callback;
2213 }
2214
2215 loopdidcallbacks++;
2216 funcdidcallbacks++;
2217
Christoph Hellwig4b809162007-08-16 15:37:36 +10002218 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2220 iclog->ic_state = XLOG_STATE_DIRTY;
2221
2222 /*
2223 * Transition from DIRTY to ACTIVE if applicable.
2224 * NOP if STATE_IOERROR.
2225 */
2226 xlog_state_clean_log(log);
2227
2228 /* wake up threads waiting in xfs_log_force() */
2229 sv_broadcast(&iclog->ic_forcesema);
2230
2231 iclog = iclog->ic_next;
2232 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002233
2234 if (repeats > 5000) {
2235 flushcnt += repeats;
2236 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002238 "%s: possible infinite loop (%d iterations)",
2239 __FUNCTION__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241 } while (!ioerrors && loopdidcallbacks);
2242
2243 /*
2244 * make one last gasp attempt to see if iclogs are being left in
2245 * limbo..
2246 */
2247#ifdef DEBUG
2248 if (funcdidcallbacks) {
2249 first_iclog = iclog = log->l_iclog;
2250 do {
2251 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2252 /*
2253 * Terminate the loop if iclogs are found in states
2254 * which will cause other threads to clean up iclogs.
2255 *
2256 * SYNCING - i/o completion will go through logs
2257 * DONE_SYNC - interrupt thread should be waiting for
2258 * LOG_LOCK
2259 * IOERROR - give up hope all ye who enter here
2260 */
2261 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2262 iclog->ic_state == XLOG_STATE_SYNCING ||
2263 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2264 iclog->ic_state == XLOG_STATE_IOERROR )
2265 break;
2266 iclog = iclog->ic_next;
2267 } while (first_iclog != iclog);
2268 }
2269#endif
2270
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002271 flushcnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
2273 flushcnt = log->l_flushcnt;
2274 log->l_flushcnt = 0;
2275 }
2276 LOG_UNLOCK(log, s);
2277 while (flushcnt--)
2278 vsema(&log->l_flushsema);
2279} /* xlog_state_do_callback */
2280
2281
2282/*
2283 * Finish transitioning this iclog to the dirty state.
2284 *
2285 * Make sure that we completely execute this routine only when this is
2286 * the last call to the iclog. There is a good chance that iclog flushes,
2287 * when we reach the end of the physical log, get turned into 2 separate
2288 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2289 * routine. By using the reference count bwritecnt, we guarantee that only
2290 * the second completion goes through.
2291 *
2292 * Callbacks could take time, so they are done outside the scope of the
2293 * global state machine log lock. Assume that the calls to cvsema won't
2294 * take a long time. At least we know it won't sleep.
2295 */
2296void
2297xlog_state_done_syncing(
2298 xlog_in_core_t *iclog,
2299 int aborted)
2300{
2301 xlog_t *log = iclog->ic_log;
2302 SPLDECL(s);
2303
2304 s = LOG_LOCK(log);
2305
2306 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2307 iclog->ic_state == XLOG_STATE_IOERROR);
2308 ASSERT(iclog->ic_refcnt == 0);
2309 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2310
2311
2312 /*
2313 * If we got an error, either on the first buffer, or in the case of
2314 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2315 * and none should ever be attempted to be written to disk
2316 * again.
2317 */
2318 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2319 if (--iclog->ic_bwritecnt == 1) {
2320 LOG_UNLOCK(log, s);
2321 return;
2322 }
2323 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2324 }
2325
2326 /*
2327 * Someone could be sleeping prior to writing out the next
2328 * iclog buffer, we wake them all, one will get to do the
2329 * I/O, the others get to wait for the result.
2330 */
2331 sv_broadcast(&iclog->ic_writesema);
2332 LOG_UNLOCK(log, s);
2333 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2334} /* xlog_state_done_syncing */
2335
2336
2337/*
2338 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
2339 * sleep. The flush semaphore is set to the number of in-core buffers and
2340 * decremented around disk syncing. Therefore, if all buffers are syncing,
2341 * this semaphore will cause new writes to sleep until a sync completes.
2342 * Otherwise, this code just does p() followed by v(). This approximates
2343 * a sleep/wakeup except we can't race.
2344 *
2345 * The in-core logs are used in a circular fashion. They are not used
2346 * out-of-order even when an iclog past the head is free.
2347 *
2348 * return:
2349 * * log_offset where xlog_write() can start writing into the in-core
2350 * log's data space.
2351 * * in-core log pointer to which xlog_write() should write.
2352 * * boolean indicating this is a continued write to an in-core log.
2353 * If this is the last write, then the in-core log's offset field
2354 * needs to be incremented, depending on the amount of data which
2355 * is copied.
2356 */
2357int
2358xlog_state_get_iclog_space(xlog_t *log,
2359 int len,
2360 xlog_in_core_t **iclogp,
2361 xlog_ticket_t *ticket,
2362 int *continued_write,
2363 int *logoffsetp)
2364{
2365 SPLDECL(s);
2366 int log_offset;
2367 xlog_rec_header_t *head;
2368 xlog_in_core_t *iclog;
2369 int error;
2370
2371restart:
2372 s = LOG_LOCK(log);
2373 if (XLOG_FORCED_SHUTDOWN(log)) {
2374 LOG_UNLOCK(log, s);
2375 return XFS_ERROR(EIO);
2376 }
2377
2378 iclog = log->l_iclog;
2379 if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
2380 log->l_flushcnt++;
2381 LOG_UNLOCK(log, s);
2382 xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
2383 XFS_STATS_INC(xs_log_noiclogs);
2384 /* Ensure that log writes happen */
2385 psema(&log->l_flushsema, PINOD);
2386 goto restart;
2387 }
2388 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2389 head = &iclog->ic_header;
2390
2391 iclog->ic_refcnt++; /* prevents sync */
2392 log_offset = iclog->ic_offset;
2393
2394 /* On the 1st write to an iclog, figure out lsn. This works
2395 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2396 * committing to. If the offset is set, that's how many blocks
2397 * must be written.
2398 */
2399 if (log_offset == 0) {
2400 ticket->t_curr_res -= log->l_iclog_hsize;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002401 XLOG_TIC_ADD_REGION(ticket,
2402 log->l_iclog_hsize,
2403 XLOG_REG_TYPE_LRHEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 INT_SET(head->h_cycle, ARCH_CONVERT, log->l_curr_cycle);
2405 ASSIGN_LSN(head->h_lsn, log);
2406 ASSERT(log->l_curr_block >= 0);
2407 }
2408
2409 /* If there is enough room to write everything, then do it. Otherwise,
2410 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2411 * bit is on, so this will get flushed out. Don't update ic_offset
2412 * until you know exactly how many bytes get copied. Therefore, wait
2413 * until later to update ic_offset.
2414 *
2415 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2416 * can fit into remaining data section.
2417 */
2418 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2419 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2420
2421 /* If I'm the only one writing to this iclog, sync it to disk */
2422 if (iclog->ic_refcnt == 1) {
2423 LOG_UNLOCK(log, s);
2424 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01002425 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 } else {
2427 iclog->ic_refcnt--;
2428 LOG_UNLOCK(log, s);
2429 }
2430 goto restart;
2431 }
2432
2433 /* Do we have enough room to write the full amount in the remainder
2434 * of this iclog? Or must we continue a write on the next iclog and
2435 * mark this iclog as completely taken? In the case where we switch
2436 * iclogs (to mark it taken), this particular iclog will release/sync
2437 * to disk in xlog_write().
2438 */
2439 if (len <= iclog->ic_size - iclog->ic_offset) {
2440 *continued_write = 0;
2441 iclog->ic_offset += len;
2442 } else {
2443 *continued_write = 1;
2444 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2445 }
2446 *iclogp = iclog;
2447
2448 ASSERT(iclog->ic_offset <= iclog->ic_size);
2449 LOG_UNLOCK(log, s);
2450
2451 *logoffsetp = log_offset;
2452 return 0;
2453} /* xlog_state_get_iclog_space */
2454
2455/*
2456 * Atomically get the log space required for a log ticket.
2457 *
2458 * Once a ticket gets put onto the reserveq, it will only return after
2459 * the needed reservation is satisfied.
2460 */
2461STATIC int
2462xlog_grant_log_space(xlog_t *log,
2463 xlog_ticket_t *tic)
2464{
2465 int free_bytes;
2466 int need_bytes;
2467 SPLDECL(s);
2468#ifdef DEBUG
2469 xfs_lsn_t tail_lsn;
2470#endif
2471
2472
2473#ifdef DEBUG
2474 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2475 panic("grant Recovery problem");
2476#endif
2477
2478 /* Is there space or do we need to sleep? */
2479 s = GRANT_LOCK(log);
2480 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
2481
2482 /* something is already sleeping; insert new transaction at end */
2483 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002484 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 xlog_trace_loggrant(log, tic,
2486 "xlog_grant_log_space: sleep 1");
2487 /*
2488 * Gotta check this before going to sleep, while we're
2489 * holding the grant lock.
2490 */
2491 if (XLOG_FORCED_SHUTDOWN(log))
2492 goto error_return;
2493
2494 XFS_STATS_INC(xs_sleep_logspace);
2495 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2496 /*
2497 * If we got an error, and the filesystem is shutting down,
2498 * we'll catch it down below. So just continue...
2499 */
2500 xlog_trace_loggrant(log, tic,
2501 "xlog_grant_log_space: wake 1");
2502 s = GRANT_LOCK(log);
2503 }
2504 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2505 need_bytes = tic->t_unit_res*tic->t_ocnt;
2506 else
2507 need_bytes = tic->t_unit_res;
2508
2509redo:
2510 if (XLOG_FORCED_SHUTDOWN(log))
2511 goto error_return;
2512
2513 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2514 log->l_grant_reserve_bytes);
2515 if (free_bytes < need_bytes) {
2516 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002517 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 xlog_trace_loggrant(log, tic,
2519 "xlog_grant_log_space: sleep 2");
2520 XFS_STATS_INC(xs_sleep_logspace);
2521 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2522
2523 if (XLOG_FORCED_SHUTDOWN(log)) {
2524 s = GRANT_LOCK(log);
2525 goto error_return;
2526 }
2527
2528 xlog_trace_loggrant(log, tic,
2529 "xlog_grant_log_space: wake 2");
2530 xlog_grant_push_ail(log->l_mp, need_bytes);
2531 s = GRANT_LOCK(log);
2532 goto redo;
2533 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002534 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002537 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538#ifdef DEBUG
2539 tail_lsn = log->l_tail_lsn;
2540 /*
2541 * Check to make sure the grant write head didn't just over lap the
2542 * tail. If the cycles are the same, we can't be overlapping.
2543 * Otherwise, make sure that the cycles differ by exactly one and
2544 * check the byte count.
2545 */
2546 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2547 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2548 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2549 }
2550#endif
2551 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
2552 xlog_verify_grant_head(log, 1);
2553 GRANT_UNLOCK(log, s);
2554 return 0;
2555
2556 error_return:
2557 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002558 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
2560 /*
2561 * If we are failing, make sure the ticket doesn't have any
2562 * current reservations. We don't want to add this back when
2563 * the ticket/transaction gets cancelled.
2564 */
2565 tic->t_curr_res = 0;
2566 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2567 GRANT_UNLOCK(log, s);
2568 return XFS_ERROR(EIO);
2569} /* xlog_grant_log_space */
2570
2571
2572/*
2573 * Replenish the byte reservation required by moving the grant write head.
2574 *
2575 *
2576 */
2577STATIC int
2578xlog_regrant_write_log_space(xlog_t *log,
2579 xlog_ticket_t *tic)
2580{
2581 SPLDECL(s);
2582 int free_bytes, need_bytes;
2583 xlog_ticket_t *ntic;
2584#ifdef DEBUG
2585 xfs_lsn_t tail_lsn;
2586#endif
2587
2588 tic->t_curr_res = tic->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002589 XLOG_TIC_RESET_RES(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594#ifdef DEBUG
2595 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2596 panic("regrant Recovery problem");
2597#endif
2598
2599 s = GRANT_LOCK(log);
2600 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
2601
2602 if (XLOG_FORCED_SHUTDOWN(log))
2603 goto error_return;
2604
2605 /* If there are other waiters on the queue then give them a
2606 * chance at logspace before us. Wake up the first waiters,
2607 * if we do not wake up all the waiters then go to sleep waiting
2608 * for more free space, otherwise try to get some space for
2609 * this transaction.
2610 */
2611
2612 if ((ntic = log->l_write_headq)) {
2613 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2614 log->l_grant_write_bytes);
2615 do {
2616 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2617
2618 if (free_bytes < ntic->t_unit_res)
2619 break;
2620 free_bytes -= ntic->t_unit_res;
2621 sv_signal(&ntic->t_sema);
2622 ntic = ntic->t_next;
2623 } while (ntic != log->l_write_headq);
2624
2625 if (ntic != log->l_write_headq) {
2626 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002627 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 xlog_trace_loggrant(log, tic,
2630 "xlog_regrant_write_log_space: sleep 1");
2631 XFS_STATS_INC(xs_sleep_logspace);
2632 sv_wait(&tic->t_sema, PINOD|PLTWAIT,
2633 &log->l_grant_lock, s);
2634
2635 /* If we're shutting down, this tic is already
2636 * off the queue */
2637 if (XLOG_FORCED_SHUTDOWN(log)) {
2638 s = GRANT_LOCK(log);
2639 goto error_return;
2640 }
2641
2642 xlog_trace_loggrant(log, tic,
2643 "xlog_regrant_write_log_space: wake 1");
2644 xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
2645 s = GRANT_LOCK(log);
2646 }
2647 }
2648
2649 need_bytes = tic->t_unit_res;
2650
2651redo:
2652 if (XLOG_FORCED_SHUTDOWN(log))
2653 goto error_return;
2654
2655 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2656 log->l_grant_write_bytes);
2657 if (free_bytes < need_bytes) {
2658 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002659 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 XFS_STATS_INC(xs_sleep_logspace);
2661 sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
2662
2663 /* If we're shutting down, this tic is already off the queue */
2664 if (XLOG_FORCED_SHUTDOWN(log)) {
2665 s = GRANT_LOCK(log);
2666 goto error_return;
2667 }
2668
2669 xlog_trace_loggrant(log, tic,
2670 "xlog_regrant_write_log_space: wake 2");
2671 xlog_grant_push_ail(log->l_mp, need_bytes);
2672 s = GRANT_LOCK(log);
2673 goto redo;
2674 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002675 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002677 /* we've got enough space */
2678 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679#ifdef DEBUG
2680 tail_lsn = log->l_tail_lsn;
2681 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2682 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2683 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2684 }
2685#endif
2686
2687 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
2688 xlog_verify_grant_head(log, 1);
2689 GRANT_UNLOCK(log, s);
Jesper Juhl014c2542006-01-15 02:37:08 +01002690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692
2693 error_return:
2694 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002695 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
2697 /*
2698 * If we are failing, make sure the ticket doesn't have any
2699 * current reservations. We don't want to add this back when
2700 * the ticket/transaction gets cancelled.
2701 */
2702 tic->t_curr_res = 0;
2703 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
2704 GRANT_UNLOCK(log, s);
2705 return XFS_ERROR(EIO);
2706} /* xlog_regrant_write_log_space */
2707
2708
2709/* The first cnt-1 times through here we don't need to
2710 * move the grant write head because the permanent
2711 * reservation has reserved cnt times the unit amount.
2712 * Release part of current permanent unit reservation and
2713 * reset current reservation to be one units worth. Also
2714 * move grant reservation head forward.
2715 */
2716STATIC void
2717xlog_regrant_reserve_log_space(xlog_t *log,
2718 xlog_ticket_t *ticket)
2719{
2720 SPLDECL(s);
2721
2722 xlog_trace_loggrant(log, ticket,
2723 "xlog_regrant_reserve_log_space: enter");
2724 if (ticket->t_cnt > 0)
2725 ticket->t_cnt--;
2726
2727 s = GRANT_LOCK(log);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002728 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 ticket->t_curr_res = ticket->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002730 XLOG_TIC_RESET_RES(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 xlog_trace_loggrant(log, ticket,
2732 "xlog_regrant_reserve_log_space: sub current res");
2733 xlog_verify_grant_head(log, 1);
2734
2735 /* just return if we still have some of the pre-reserved space */
2736 if (ticket->t_cnt > 0) {
2737 GRANT_UNLOCK(log, s);
2738 return;
2739 }
2740
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002741 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 xlog_trace_loggrant(log, ticket,
2743 "xlog_regrant_reserve_log_space: exit");
2744 xlog_verify_grant_head(log, 0);
2745 GRANT_UNLOCK(log, s);
2746 ticket->t_curr_res = ticket->t_unit_res;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002747 XLOG_TIC_RESET_RES(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748} /* xlog_regrant_reserve_log_space */
2749
2750
2751/*
2752 * Give back the space left from a reservation.
2753 *
2754 * All the information we need to make a correct determination of space left
2755 * is present. For non-permanent reservations, things are quite easy. The
2756 * count should have been decremented to zero. We only need to deal with the
2757 * space remaining in the current reservation part of the ticket. If the
2758 * ticket contains a permanent reservation, there may be left over space which
2759 * needs to be released. A count of N means that N-1 refills of the current
2760 * reservation can be done before we need to ask for more space. The first
2761 * one goes to fill up the first current reservation. Once we run out of
2762 * space, the count will stay at zero and the only space remaining will be
2763 * in the current reservation field.
2764 */
2765STATIC void
2766xlog_ungrant_log_space(xlog_t *log,
2767 xlog_ticket_t *ticket)
2768{
2769 SPLDECL(s);
2770
2771 if (ticket->t_cnt > 0)
2772 ticket->t_cnt--;
2773
2774 s = GRANT_LOCK(log);
2775 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
2776
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002777 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
2780
2781 /* If this is a permanent reservation ticket, we may be able to free
2782 * up more space based on the remaining count.
2783 */
2784 if (ticket->t_cnt > 0) {
2785 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002786 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
2788
2789 xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
2790 xlog_verify_grant_head(log, 1);
2791 GRANT_UNLOCK(log, s);
2792 xfs_log_move_tail(log->l_mp, 1);
2793} /* xlog_ungrant_log_space */
2794
2795
2796/*
2797 * Atomically put back used ticket.
2798 */
2799void
2800xlog_state_put_ticket(xlog_t *log,
2801 xlog_ticket_t *tic)
2802{
2803 unsigned long s;
2804
2805 s = LOG_LOCK(log);
2806 xlog_ticket_put(log, tic);
2807 LOG_UNLOCK(log, s);
2808} /* xlog_state_put_ticket */
2809
2810/*
2811 * Flush iclog to disk if this is the last reference to the given iclog and
2812 * the WANT_SYNC bit is set.
2813 *
2814 * When this function is entered, the iclog is not necessarily in the
2815 * WANT_SYNC state. It may be sitting around waiting to get filled.
2816 *
2817 *
2818 */
2819int
2820xlog_state_release_iclog(xlog_t *log,
2821 xlog_in_core_t *iclog)
2822{
2823 SPLDECL(s);
2824 int sync = 0; /* do we sync? */
2825
2826 xlog_assign_tail_lsn(log->l_mp);
2827
2828 s = LOG_LOCK(log);
2829
2830 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2831 LOG_UNLOCK(log, s);
2832 return XFS_ERROR(EIO);
2833 }
2834
2835 ASSERT(iclog->ic_refcnt > 0);
2836 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2837 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2838
2839 if (--iclog->ic_refcnt == 0 &&
2840 iclog->ic_state == XLOG_STATE_WANT_SYNC) {
2841 sync++;
2842 iclog->ic_state = XLOG_STATE_SYNCING;
2843 INT_SET(iclog->ic_header.h_tail_lsn, ARCH_CONVERT, log->l_tail_lsn);
2844 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2845 /* cycle incremented when incrementing curr_block */
2846 }
2847
2848 LOG_UNLOCK(log, s);
2849
2850 /*
2851 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002852 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2854 * this iclog has consistent data, so we ignore IOERROR
2855 * flags after this point.
2856 */
2857 if (sync) {
2858 return xlog_sync(log, iclog);
2859 }
Jesper Juhl014c2542006-01-15 02:37:08 +01002860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862} /* xlog_state_release_iclog */
2863
2864
2865/*
2866 * This routine will mark the current iclog in the ring as WANT_SYNC
2867 * and move the current iclog pointer to the next iclog in the ring.
2868 * When this routine is called from xlog_state_get_iclog_space(), the
2869 * exact size of the iclog has not yet been determined. All we know is
2870 * that every data block. We have run out of space in this log record.
2871 */
2872STATIC void
2873xlog_state_switch_iclogs(xlog_t *log,
2874 xlog_in_core_t *iclog,
2875 int eventual_size)
2876{
2877 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2878 if (!eventual_size)
2879 eventual_size = iclog->ic_offset;
2880 iclog->ic_state = XLOG_STATE_WANT_SYNC;
2881 INT_SET(iclog->ic_header.h_prev_block, ARCH_CONVERT, log->l_prev_block);
2882 log->l_prev_block = log->l_curr_block;
2883 log->l_prev_cycle = log->l_curr_cycle;
2884
2885 /* roll log?: ic_offset changed later */
2886 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2887
2888 /* Round up to next log-sunit */
2889 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
2890 log->l_mp->m_sb.sb_logsunit > 1) {
2891 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2892 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2893 }
2894
2895 if (log->l_curr_block >= log->l_logBBsize) {
2896 log->l_curr_cycle++;
2897 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2898 log->l_curr_cycle++;
2899 log->l_curr_block -= log->l_logBBsize;
2900 ASSERT(log->l_curr_block >= 0);
2901 }
2902 ASSERT(iclog == log->l_iclog);
2903 log->l_iclog = iclog->ic_next;
2904} /* xlog_state_switch_iclogs */
2905
2906
2907/*
2908 * Write out all data in the in-core log as of this exact moment in time.
2909 *
2910 * Data may be written to the in-core log during this call. However,
2911 * we don't guarantee this data will be written out. A change from past
2912 * implementation means this routine will *not* write out zero length LRs.
2913 *
2914 * Basically, we try and perform an intelligent scan of the in-core logs.
2915 * If we determine there is no flushable data, we just return. There is no
2916 * flushable data if:
2917 *
2918 * 1. the current iclog is active and has no data; the previous iclog
2919 * is in the active or dirty state.
2920 * 2. the current iclog is drity, and the previous iclog is in the
2921 * active or dirty state.
2922 *
2923 * We may sleep (call psema) if:
2924 *
2925 * 1. the current iclog is not in the active nor dirty state.
2926 * 2. the current iclog dirty, and the previous iclog is not in the
2927 * active nor dirty state.
2928 * 3. the current iclog is active, and there is another thread writing
2929 * to this particular iclog.
2930 * 4. a) the current iclog is active and has no other writers
2931 * b) when we return from flushing out this iclog, it is still
2932 * not in the active nor dirty state.
2933 */
2934STATIC int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002935xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
2937 xlog_in_core_t *iclog;
2938 xfs_lsn_t lsn;
2939 SPLDECL(s);
2940
2941 s = LOG_LOCK(log);
2942
2943 iclog = log->l_iclog;
2944 if (iclog->ic_state & XLOG_STATE_IOERROR) {
2945 LOG_UNLOCK(log, s);
2946 return XFS_ERROR(EIO);
2947 }
2948
2949 /* If the head iclog is not active nor dirty, we just attach
2950 * ourselves to the head and go to sleep.
2951 */
2952 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2953 iclog->ic_state == XLOG_STATE_DIRTY) {
2954 /*
2955 * If the head is dirty or (active and empty), then
2956 * we need to look at the previous iclog. If the previous
2957 * iclog is active or dirty we are done. There is nothing
2958 * to sync out. Otherwise, we attach ourselves to the
2959 * previous iclog and go to sleep.
2960 */
2961 if (iclog->ic_state == XLOG_STATE_DIRTY ||
2962 (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
2963 iclog = iclog->ic_prev;
2964 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2965 iclog->ic_state == XLOG_STATE_DIRTY)
2966 goto no_sleep;
2967 else
2968 goto maybe_sleep;
2969 } else {
2970 if (iclog->ic_refcnt == 0) {
2971 /* We are the only one with access to this
2972 * iclog. Flush it out now. There should
2973 * be a roundoff of zero to show that someone
2974 * has already taken care of the roundoff from
2975 * the previous sync.
2976 */
2977 iclog->ic_refcnt++;
2978 lsn = INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT);
2979 xlog_state_switch_iclogs(log, iclog, 0);
2980 LOG_UNLOCK(log, s);
2981
2982 if (xlog_state_release_iclog(log, iclog))
2983 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11002984 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 s = LOG_LOCK(log);
2986 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) == lsn &&
2987 iclog->ic_state != XLOG_STATE_DIRTY)
2988 goto maybe_sleep;
2989 else
2990 goto no_sleep;
2991 } else {
2992 /* Someone else is writing to this iclog.
2993 * Use its call to flush out the data. However,
2994 * the other thread may not force out this LR,
2995 * so we mark it WANT_SYNC.
2996 */
2997 xlog_state_switch_iclogs(log, iclog, 0);
2998 goto maybe_sleep;
2999 }
3000 }
3001 }
3002
3003 /* By the time we come around again, the iclog could've been filled
3004 * which would give it another lsn. If we have a new lsn, just
3005 * return because the relevant data has been flushed.
3006 */
3007maybe_sleep:
3008 if (flags & XFS_LOG_SYNC) {
3009 /*
3010 * We must check if we're shutting down here, before
3011 * we wait, while we're holding the LOG_LOCK.
3012 * Then we check again after waking up, in case our
3013 * sleep was disturbed by a bad news.
3014 */
3015 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3016 LOG_UNLOCK(log, s);
3017 return XFS_ERROR(EIO);
3018 }
3019 XFS_STATS_INC(xs_log_force_sleep);
3020 sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
3021 /*
3022 * No need to grab the log lock here since we're
3023 * only deciding whether or not to return EIO
3024 * and the memory read should be atomic.
3025 */
3026 if (iclog->ic_state & XLOG_STATE_IOERROR)
3027 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003028 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 } else {
3031
3032no_sleep:
3033 LOG_UNLOCK(log, s);
3034 }
3035 return 0;
3036} /* xlog_state_sync_all */
3037
3038
3039/*
3040 * Used by code which implements synchronous log forces.
3041 *
3042 * Find in-core log with lsn.
3043 * If it is in the DIRTY state, just return.
3044 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3045 * state and go to sleep or return.
3046 * If it is in any other state, go to sleep or return.
3047 *
3048 * If filesystem activity goes to zero, the iclog will get flushed only by
3049 * bdflush().
3050 */
3051int
3052xlog_state_sync(xlog_t *log,
3053 xfs_lsn_t lsn,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003054 uint flags,
3055 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 xlog_in_core_t *iclog;
3058 int already_slept = 0;
3059 SPLDECL(s);
3060
3061
3062try_again:
3063 s = LOG_LOCK(log);
3064 iclog = log->l_iclog;
3065
3066 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3067 LOG_UNLOCK(log, s);
3068 return XFS_ERROR(EIO);
3069 }
3070
3071 do {
3072 if (INT_GET(iclog->ic_header.h_lsn, ARCH_CONVERT) != lsn) {
3073 iclog = iclog->ic_next;
3074 continue;
3075 }
3076
3077 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3078 LOG_UNLOCK(log, s);
3079 return 0;
3080 }
3081
3082 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3083 /*
3084 * We sleep here if we haven't already slept (e.g.
3085 * this is the first time we've looked at the correct
3086 * iclog buf) and the buffer before us is going to
3087 * be sync'ed. The reason for this is that if we
3088 * are doing sync transactions here, by waiting for
3089 * the previous I/O to complete, we can allow a few
3090 * more transactions into this iclog before we close
3091 * it down.
3092 *
3093 * Otherwise, we mark the buffer WANT_SYNC, and bump
3094 * up the refcnt so we can release the log (which drops
3095 * the ref count). The state switch keeps new transaction
3096 * commits from using this buffer. When the current commits
3097 * finish writing into the buffer, the refcount will drop to
3098 * zero and the buffer will go out then.
3099 */
3100 if (!already_slept &&
3101 (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
3102 XLOG_STATE_SYNCING))) {
3103 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3104 XFS_STATS_INC(xs_log_force_sleep);
3105 sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
3106 &log->l_icloglock, s);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003107 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 already_slept = 1;
3109 goto try_again;
3110 } else {
3111 iclog->ic_refcnt++;
3112 xlog_state_switch_iclogs(log, iclog, 0);
3113 LOG_UNLOCK(log, s);
3114 if (xlog_state_release_iclog(log, iclog))
3115 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003116 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 s = LOG_LOCK(log);
3118 }
3119 }
3120
3121 if ((flags & XFS_LOG_SYNC) && /* sleep */
3122 !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3123
3124 /*
3125 * Don't wait on the forcesema if we know that we've
3126 * gotten a log write error.
3127 */
3128 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3129 LOG_UNLOCK(log, s);
3130 return XFS_ERROR(EIO);
3131 }
3132 XFS_STATS_INC(xs_log_force_sleep);
3133 sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
3134 /*
3135 * No need to grab the log lock here since we're
3136 * only deciding whether or not to return EIO
3137 * and the memory read should be atomic.
3138 */
3139 if (iclog->ic_state & XLOG_STATE_IOERROR)
3140 return XFS_ERROR(EIO);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003141 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 } else { /* just return */
3143 LOG_UNLOCK(log, s);
3144 }
3145 return 0;
3146
3147 } while (iclog != log->l_iclog);
3148
3149 LOG_UNLOCK(log, s);
Jesper Juhl014c2542006-01-15 02:37:08 +01003150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151} /* xlog_state_sync */
3152
3153
3154/*
3155 * Called when we want to mark the current iclog as being ready to sync to
3156 * disk.
3157 */
3158void
3159xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3160{
3161 SPLDECL(s);
3162
3163 s = LOG_LOCK(log);
3164
3165 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3166 xlog_state_switch_iclogs(log, iclog, 0);
3167 } else {
3168 ASSERT(iclog->ic_state &
3169 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3170 }
3171
3172 LOG_UNLOCK(log, s);
3173} /* xlog_state_want_sync */
3174
3175
3176
3177/*****************************************************************************
3178 *
3179 * TICKET functions
3180 *
3181 *****************************************************************************
3182 */
3183
3184/*
3185 * Algorithm doesn't take into account page size. ;-(
3186 */
3187STATIC void
3188xlog_state_ticket_alloc(xlog_t *log)
3189{
3190 xlog_ticket_t *t_list;
3191 xlog_ticket_t *next;
3192 xfs_caddr_t buf;
3193 uint i = (NBPP / sizeof(xlog_ticket_t)) - 2;
3194 SPLDECL(s);
3195
3196 /*
3197 * The kmem_zalloc may sleep, so we shouldn't be holding the
3198 * global lock. XXXmiken: may want to use zone allocator.
3199 */
3200 buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
3201
3202 s = LOG_LOCK(log);
3203
3204 /* Attach 1st ticket to Q, so we can keep track of allocated memory */
3205 t_list = (xlog_ticket_t *)buf;
3206 t_list->t_next = log->l_unmount_free;
3207 log->l_unmount_free = t_list++;
3208 log->l_ticket_cnt++;
3209 log->l_ticket_tcnt++;
3210
3211 /* Next ticket becomes first ticket attached to ticket free list */
3212 if (log->l_freelist != NULL) {
3213 ASSERT(log->l_tail != NULL);
3214 log->l_tail->t_next = t_list;
3215 } else {
3216 log->l_freelist = t_list;
3217 }
3218 log->l_ticket_cnt++;
3219 log->l_ticket_tcnt++;
3220
3221 /* Cycle through rest of alloc'ed memory, building up free Q */
3222 for ( ; i > 0; i--) {
3223 next = t_list + 1;
3224 t_list->t_next = next;
3225 t_list = next;
3226 log->l_ticket_cnt++;
3227 log->l_ticket_tcnt++;
3228 }
3229 t_list->t_next = NULL;
3230 log->l_tail = t_list;
3231 LOG_UNLOCK(log, s);
3232} /* xlog_state_ticket_alloc */
3233
3234
3235/*
3236 * Put ticket into free list
3237 *
3238 * Assumption: log lock is held around this call.
3239 */
3240STATIC void
3241xlog_ticket_put(xlog_t *log,
3242 xlog_ticket_t *ticket)
3243{
3244 sv_destroy(&ticket->t_sema);
3245
3246 /*
3247 * Don't think caching will make that much difference. It's
3248 * more important to make debug easier.
3249 */
3250#if 0
3251 /* real code will want to use LIFO for caching */
3252 ticket->t_next = log->l_freelist;
3253 log->l_freelist = ticket;
3254 /* no need to clear fields */
3255#else
3256 /* When we debug, it is easier if tickets are cycled */
3257 ticket->t_next = NULL;
Christoph Hellwig4b809162007-08-16 15:37:36 +10003258 if (log->l_tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 log->l_tail->t_next = ticket;
3260 } else {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003261 ASSERT(log->l_freelist == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 log->l_freelist = ticket;
3263 }
3264 log->l_tail = ticket;
3265#endif /* DEBUG */
3266 log->l_ticket_cnt++;
3267} /* xlog_ticket_put */
3268
3269
3270/*
3271 * Grab ticket off freelist or allocation some more
3272 */
3273xlog_ticket_t *
3274xlog_ticket_get(xlog_t *log,
3275 int unit_bytes,
3276 int cnt,
3277 char client,
3278 uint xflags)
3279{
3280 xlog_ticket_t *tic;
3281 uint num_headers;
3282 SPLDECL(s);
3283
3284 alloc:
3285 if (log->l_freelist == NULL)
3286 xlog_state_ticket_alloc(log); /* potentially sleep */
3287
3288 s = LOG_LOCK(log);
3289 if (log->l_freelist == NULL) {
3290 LOG_UNLOCK(log, s);
3291 goto alloc;
3292 }
3293 tic = log->l_freelist;
3294 log->l_freelist = tic->t_next;
3295 if (log->l_freelist == NULL)
3296 log->l_tail = NULL;
3297 log->l_ticket_cnt--;
3298 LOG_UNLOCK(log, s);
3299
3300 /*
3301 * Permanent reservations have up to 'cnt'-1 active log operations
3302 * in the log. A unit in this case is the amount of space for one
3303 * of these log operations. Normal reservations have a cnt of 1
3304 * and their unit amount is the total amount of space required.
3305 *
3306 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003307 * which occupy space in the on-disk log.
3308 *
3309 * Normal form of a transaction is:
3310 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3311 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3312 *
3313 * We need to account for all the leadup data and trailer data
3314 * around the transaction data.
3315 * And then we need to account for the worst case in terms of using
3316 * more space.
3317 * The worst case will happen if:
3318 * - the placement of the transaction happens to be such that the
3319 * roundoff is at its maximum
3320 * - the transaction data is synced before the commit record is synced
3321 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3322 * Therefore the commit record is in its own Log Record.
3323 * This can happen as the commit record is called with its
3324 * own region to xlog_write().
3325 * This then means that in the worst case, roundoff can happen for
3326 * the commit-rec as well.
3327 * The commit-rec is smaller than padding in this scenario and so it is
3328 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 */
3330
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003331 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003333 unit_bytes += sizeof(xfs_trans_header_t);
3334
3335 /* for start-rec */
3336 unit_bytes += sizeof(xlog_op_header_t);
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /* for LR headers */
3339 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3340 unit_bytes += log->l_iclog_hsize * num_headers;
3341
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003342 /* for commit-rec LR header - note: padding will subsume the ophdr */
3343 unit_bytes += log->l_iclog_hsize;
3344
3345 /* for split-recs - ophdrs added when data split over LRs */
3346 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3347
3348 /* for roundoff padding for transaction data and one for commit record */
3349 if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
3350 log->l_mp->m_sb.sb_logsunit > 1) {
3351 /* log su roundoff */
3352 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3353 } else {
3354 /* BB roundoff */
3355 unit_bytes += 2*BBSIZE;
3356 }
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 tic->t_unit_res = unit_bytes;
3359 tic->t_curr_res = unit_bytes;
3360 tic->t_cnt = cnt;
3361 tic->t_ocnt = cnt;
3362 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3363 tic->t_clientid = client;
3364 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003365 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (xflags & XFS_LOG_PERM_RESERV)
3367 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3368 sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
3369
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003370 XLOG_TIC_RESET_RES(tic);
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return tic;
3373} /* xlog_ticket_get */
3374
3375
3376/******************************************************************************
3377 *
3378 * Log debug routines
3379 *
3380 ******************************************************************************
3381 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003382#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383/*
3384 * Make sure that the destination ptr is within the valid data region of
3385 * one of the iclogs. This uses backup pointers stored in a different
3386 * part of the log in case we trash the log structure.
3387 */
3388void
3389xlog_verify_dest_ptr(xlog_t *log,
3390 __psint_t ptr)
3391{
3392 int i;
3393 int good_ptr = 0;
3394
3395 for (i=0; i < log->l_iclog_bufs; i++) {
3396 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3397 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3398 good_ptr++;
3399 }
3400 if (! good_ptr)
3401 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3402} /* xlog_verify_dest_ptr */
3403
3404STATIC void
3405xlog_verify_grant_head(xlog_t *log, int equals)
3406{
3407 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3408 if (equals)
3409 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3410 else
3411 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3412 } else {
3413 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3414 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3415 }
3416} /* xlog_verify_grant_head */
3417
3418/* check if it will fit */
3419STATIC void
3420xlog_verify_tail_lsn(xlog_t *log,
3421 xlog_in_core_t *iclog,
3422 xfs_lsn_t tail_lsn)
3423{
3424 int blocks;
3425
3426 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3427 blocks =
3428 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3429 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3430 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3431 } else {
3432 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3433
3434 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3435 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3436
3437 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3438 if (blocks < BTOBB(iclog->ic_offset) + 1)
3439 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3440 }
3441} /* xlog_verify_tail_lsn */
3442
3443/*
3444 * Perform a number of checks on the iclog before writing to disk.
3445 *
3446 * 1. Make sure the iclogs are still circular
3447 * 2. Make sure we have a good magic number
3448 * 3. Make sure we don't have magic numbers in the data
3449 * 4. Check fields of each log operation header for:
3450 * A. Valid client identifier
3451 * B. tid ptr value falls in valid ptr space (user space code)
3452 * C. Length in log record header is correct according to the
3453 * individual operation headers within record.
3454 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3455 * log, check the preceding blocks of the physical log to make sure all
3456 * the cycle numbers agree with the current cycle number.
3457 */
3458STATIC void
3459xlog_verify_iclog(xlog_t *log,
3460 xlog_in_core_t *iclog,
3461 int count,
3462 boolean_t syncing)
3463{
3464 xlog_op_header_t *ophead;
3465 xlog_in_core_t *icptr;
3466 xlog_in_core_2_t *xhdr;
3467 xfs_caddr_t ptr;
3468 xfs_caddr_t base_ptr;
3469 __psint_t field_offset;
3470 __uint8_t clientid;
3471 int len, i, j, k, op_len;
3472 int idx;
3473 SPLDECL(s);
3474
3475 /* check validity of iclog pointers */
3476 s = LOG_LOCK(log);
3477 icptr = log->l_iclog;
3478 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003479 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 xlog_panic("xlog_verify_iclog: invalid ptr");
3481 icptr = icptr->ic_next;
3482 }
3483 if (icptr != log->l_iclog)
3484 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
3485 LOG_UNLOCK(log, s);
3486
3487 /* check log magic numbers */
3488 ptr = (xfs_caddr_t) &(iclog->ic_header);
3489 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) != XLOG_HEADER_MAGIC_NUM)
3490 xlog_panic("xlog_verify_iclog: invalid magic num");
3491
3492 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&(iclog->ic_header))+count;
3493 ptr += BBSIZE) {
3494 if (INT_GET(*(uint *)ptr, ARCH_CONVERT) == XLOG_HEADER_MAGIC_NUM)
3495 xlog_panic("xlog_verify_iclog: unexpected magic num");
3496 }
3497
3498 /* check fields */
3499 len = INT_GET(iclog->ic_header.h_num_logops, ARCH_CONVERT);
3500 ptr = iclog->ic_datap;
3501 base_ptr = ptr;
3502 ophead = (xlog_op_header_t *)ptr;
3503 xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
3504 for (i = 0; i < len; i++) {
3505 ophead = (xlog_op_header_t *)ptr;
3506
3507 /* clientid is only 1 byte */
3508 field_offset = (__psint_t)
3509 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3510 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3511 clientid = ophead->oh_clientid;
3512 } else {
3513 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3514 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3515 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3516 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3517 clientid = GET_CLIENT_ID(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3518 } else {
3519 clientid = GET_CLIENT_ID(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3520 }
3521 }
3522 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003523 cmn_err(CE_WARN, "xlog_verify_iclog: "
3524 "invalid clientid %d op 0x%p offset 0x%lx",
3525 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 /* check length */
3528 field_offset = (__psint_t)
3529 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3530 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3531 op_len = INT_GET(ophead->oh_len, ARCH_CONVERT);
3532 } else {
3533 idx = BTOBBT((__psint_t)&ophead->oh_len -
3534 (__psint_t)iclog->ic_datap);
3535 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3536 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3537 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3538 op_len = INT_GET(xhdr[j].hic_xheader.xh_cycle_data[k], ARCH_CONVERT);
3539 } else {
3540 op_len = INT_GET(iclog->ic_header.h_cycle_data[idx], ARCH_CONVERT);
3541 }
3542 }
3543 ptr += sizeof(xlog_op_header_t) + op_len;
3544 }
3545} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003546#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548/*
3549 * Mark all iclogs IOERROR. LOG_LOCK is held by the caller.
3550 */
3551STATIC int
3552xlog_state_ioerror(
3553 xlog_t *log)
3554{
3555 xlog_in_core_t *iclog, *ic;
3556
3557 iclog = log->l_iclog;
3558 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3559 /*
3560 * Mark all the incore logs IOERROR.
3561 * From now on, no log flushes will result.
3562 */
3563 ic = iclog;
3564 do {
3565 ic->ic_state = XLOG_STATE_IOERROR;
3566 ic = ic->ic_next;
3567 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570 /*
3571 * Return non-zero, if state transition has already happened.
3572 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003573 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
3576/*
3577 * This is called from xfs_force_shutdown, when we're forcibly
3578 * shutting down the filesystem, typically because of an IO error.
3579 * Our main objectives here are to make sure that:
3580 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3581 * parties to find out, 'atomically'.
3582 * b. those who're sleeping on log reservations, pinned objects and
3583 * other resources get woken up, and be told the bad news.
3584 * c. nothing new gets queued up after (a) and (b) are done.
3585 * d. if !logerror, flush the iclogs to disk, then seal them off
3586 * for business.
3587 */
3588int
3589xfs_log_force_umount(
3590 struct xfs_mount *mp,
3591 int logerror)
3592{
3593 xlog_ticket_t *tic;
3594 xlog_t *log;
3595 int retval;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003596 int dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 SPLDECL(s);
3598 SPLDECL(s2);
3599
3600 log = mp->m_log;
3601
3602 /*
3603 * If this happens during log recovery, don't worry about
3604 * locking; the log isn't open for business yet.
3605 */
3606 if (!log ||
3607 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3608 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3609 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
3612
3613 /*
3614 * Somebody could've already done the hard work for us.
3615 * No need to get locks for this.
3616 */
3617 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3618 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003619 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
3621 retval = 0;
3622 /*
3623 * We must hold both the GRANT lock and the LOG lock,
3624 * before we mark the filesystem SHUTDOWN and wake
3625 * everybody up to tell the bad news.
3626 */
3627 s = GRANT_LOCK(log);
3628 s2 = LOG_LOCK(log);
3629 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3630 XFS_BUF_DONE(mp->m_sb_bp);
3631 /*
3632 * This flag is sort of redundant because of the mount flag, but
3633 * it's good to maintain the separation between the log and the rest
3634 * of XFS.
3635 */
3636 log->l_flags |= XLOG_IO_ERROR;
3637
3638 /*
3639 * If we hit a log error, we want to mark all the iclogs IOERROR
3640 * while we're still holding the loglock.
3641 */
3642 if (logerror)
3643 retval = xlog_state_ioerror(log);
3644 LOG_UNLOCK(log, s2);
3645
3646 /*
3647 * We don't want anybody waiting for log reservations
3648 * after this. That means we have to wake up everybody
3649 * queued up on reserve_headq as well as write_headq.
3650 * In addition, we make sure in xlog_{re}grant_log_space
3651 * that we don't enqueue anything once the SHUTDOWN flag
3652 * is set, and this action is protected by the GRANTLOCK.
3653 */
3654 if ((tic = log->l_reserve_headq)) {
3655 do {
3656 sv_signal(&tic->t_sema);
3657 tic = tic->t_next;
3658 } while (tic != log->l_reserve_headq);
3659 }
3660
3661 if ((tic = log->l_write_headq)) {
3662 do {
3663 sv_signal(&tic->t_sema);
3664 tic = tic->t_next;
3665 } while (tic != log->l_write_headq);
3666 }
3667 GRANT_UNLOCK(log, s);
3668
3669 if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
3670 ASSERT(!logerror);
3671 /*
3672 * Force the incore logs to disk before shutting the
3673 * log down completely.
3674 */
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11003675 xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 s2 = LOG_LOCK(log);
3677 retval = xlog_state_ioerror(log);
3678 LOG_UNLOCK(log, s2);
3679 }
3680 /*
3681 * Wake up everybody waiting on xfs_log_force.
3682 * Callback all log item committed functions as if the
3683 * log writes were completed.
3684 */
3685 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3686
3687#ifdef XFSERRORDEBUG
3688 {
3689 xlog_in_core_t *iclog;
3690
3691 s = LOG_LOCK(log);
3692 iclog = log->l_iclog;
3693 do {
3694 ASSERT(iclog->ic_callback == 0);
3695 iclog = iclog->ic_next;
3696 } while (iclog != log->l_iclog);
3697 LOG_UNLOCK(log, s);
3698 }
3699#endif
3700 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003701 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003704STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705xlog_iclogs_empty(xlog_t *log)
3706{
3707 xlog_in_core_t *iclog;
3708
3709 iclog = log->l_iclog;
3710 do {
3711 /* endianness does not matter here, zero is zero in
3712 * any language.
3713 */
3714 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 iclog = iclog->ic_next;
3717 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003718 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719}