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