blob: 36e09e362f7fd25e876b06a6ae0b8f4b9f049cfd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
David Chinnereb01c9c2008-04-10 12:18:46 +100045kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* Local miscellaneous function prototypes */
Dave Chinner55b66332010-03-23 11:43:17 +110048STATIC int xlog_commit_record(struct log *log, struct xlog_ticket *ticket,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 xlog_in_core_t **, xfs_lsn_t *);
50STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
51 xfs_buftarg_t *log_target,
52 xfs_daddr_t blk_offset,
53 int num_bblks);
54STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
55STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100056STATIC void xlog_dealloc_log(xlog_t *log);
Dave Chinner55b66332010-03-23 11:43:17 +110057STATIC int xlog_write(struct log *log, struct xfs_log_vec *log_vector,
58 struct xlog_ticket *tic, xfs_lsn_t *start_lsn,
59 xlog_in_core_t **commit_iclog, uint flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* local state machine functions */
62STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
63STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
64STATIC int xlog_state_get_iclog_space(xlog_t *log,
65 int len,
66 xlog_in_core_t **iclog,
67 xlog_ticket_t *ticket,
68 int *continued_write,
69 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070STATIC int xlog_state_release_iclog(xlog_t *log,
71 xlog_in_core_t *iclog);
72STATIC void xlog_state_switch_iclogs(xlog_t *log,
73 xlog_in_core_t *iclog,
74 int eventual_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
76
77/* local functions to manipulate grant head */
78STATIC int xlog_grant_log_space(xlog_t *log,
79 xlog_ticket_t *xtic);
80STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
81 int need_bytes);
82STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
83 xlog_ticket_t *ticket);
84STATIC int xlog_regrant_write_log_space(xlog_t *log,
85 xlog_ticket_t *ticket);
86STATIC void xlog_ungrant_log_space(xlog_t *log,
87 xlog_ticket_t *ticket);
88
89
90/* local ticket functions */
Dave Chinnercc09c0d2008-11-17 17:37:10 +110091STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int unit_bytes,
93 int count,
94 char clientid,
95 uint flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Nathan Scottcfcbbbd2005-11-02 15:12:04 +110097#if defined(DEBUG)
Christoph Hellwige6b1f272010-03-23 11:47:38 +110098STATIC void xlog_verify_dest_ptr(xlog_t *log, char *ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
100STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
101 int count, boolean_t syncing);
102STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
103 xfs_lsn_t tail_lsn);
104#else
105#define xlog_verify_dest_ptr(a,b)
106#define xlog_verify_grant_head(a,b)
107#define xlog_verify_iclog(a,b,c,d)
108#define xlog_verify_tail_lsn(a,b,c)
109#endif
110
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000111STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100113
114static void
115xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
116{
117 if (*qp) {
118 tic->t_next = (*qp);
119 tic->t_prev = (*qp)->t_prev;
120 (*qp)->t_prev->t_next = tic;
121 (*qp)->t_prev = tic;
122 } else {
123 tic->t_prev = tic->t_next = tic;
124 *qp = tic;
125 }
126
127 tic->t_flags |= XLOG_TIC_IN_Q;
128}
129
130static void
131xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
132{
133 if (tic == tic->t_next) {
134 *qp = NULL;
135 } else {
136 *qp = tic->t_next;
137 tic->t_next->t_prev = tic->t_prev;
138 tic->t_prev->t_next = tic->t_next;
139 }
140
141 tic->t_next = tic->t_prev = NULL;
142 tic->t_flags &= ~XLOG_TIC_IN_Q;
143}
144
145static void
146xlog_grant_sub_space(struct log *log, int bytes)
147{
148 log->l_grant_write_bytes -= bytes;
149 if (log->l_grant_write_bytes < 0) {
150 log->l_grant_write_bytes += log->l_logsize;
151 log->l_grant_write_cycle--;
152 }
153
154 log->l_grant_reserve_bytes -= bytes;
155 if ((log)->l_grant_reserve_bytes < 0) {
156 log->l_grant_reserve_bytes += log->l_logsize;
157 log->l_grant_reserve_cycle--;
158 }
159
160}
161
162static void
163xlog_grant_add_space_write(struct log *log, int bytes)
164{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000165 int tmp = log->l_logsize - log->l_grant_write_bytes;
166 if (tmp > bytes)
167 log->l_grant_write_bytes += bytes;
168 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100169 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000170 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100171 }
172}
173
174static void
175xlog_grant_add_space_reserve(struct log *log, int bytes)
176{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000177 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
178 if (tmp > bytes)
179 log->l_grant_reserve_bytes += bytes;
180 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100181 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000182 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100183 }
184}
185
186static inline void
187xlog_grant_add_space(struct log *log, int bytes)
188{
189 xlog_grant_add_space_write(log, bytes);
190 xlog_grant_add_space_reserve(log, bytes);
191}
192
Christoph Hellwig0adba532007-08-30 17:21:46 +1000193static void
194xlog_tic_reset_res(xlog_ticket_t *tic)
195{
196 tic->t_res_num = 0;
197 tic->t_res_arr_sum = 0;
198 tic->t_res_num_ophdrs = 0;
199}
200
201static void
202xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
203{
204 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
205 /* add to overflow and start again */
206 tic->t_res_o_flow += tic->t_res_arr_sum;
207 tic->t_res_num = 0;
208 tic->t_res_arr_sum = 0;
209 }
210
211 tic->t_res_arr[tic->t_res_num].r_len = len;
212 tic->t_res_arr[tic->t_res_num].r_type = type;
213 tic->t_res_arr_sum += len;
214 tic->t_res_num++;
215}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * NOTES:
219 *
220 * 1. currblock field gets updated at startup and after in-core logs
221 * marked as with WANT_SYNC.
222 */
223
224/*
225 * This routine is called when a user of a log manager ticket is done with
226 * the reservation. If the ticket was ever used, then a commit record for
227 * the associated transaction is written out as a log operation header with
228 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
229 * a given ticket. If the ticket was one with a permanent reservation, then
230 * a few operations are done differently. Permanent reservation tickets by
231 * default don't release the reservation. They just commit the current
232 * transaction with the belief that the reservation is still needed. A flag
233 * must be passed in before permanent reservations are actually released.
234 * When these type of tickets are not released, they need to be set into
235 * the inited state again. By doing this, a start record will be written
236 * out when the next write occurs.
237 */
238xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000239xfs_log_done(
240 struct xfs_mount *mp,
241 struct xlog_ticket *ticket,
242 struct xlog_in_core **iclog,
243 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000245 struct log *log = mp->m_log;
246 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (XLOG_FORCED_SHUTDOWN(log) ||
249 /*
250 * If nothing was ever written, don't write out commit record.
251 * If we get an error, just continue and give back the log ticket.
252 */
253 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100254 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 lsn = (xfs_lsn_t) -1;
256 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
257 flags |= XFS_LOG_REL_PERM_RESERV;
258 }
259 }
260
261
262 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
263 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000264 trace_xfs_log_done_nonperm(log, ticket);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000267 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * request has been made to release a permanent reservation.
269 */
270 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100271 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000273 trace_xfs_log_done_perm(log, ticket);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000276 /* If this ticket was a permanent reservation and we aren't
277 * trying to release it, reset the inited flags; so next time
278 * we write, a start record will be written out.
279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * Attaches a new iclog I/O completion callback routine during
288 * transaction commit. If the log is in error state, a non-zero
289 * return code is handed back and the caller is responsible for
290 * executing the callback at an appropriate time.
291 */
292int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000293xfs_log_notify(
294 struct xfs_mount *mp,
295 struct xlog_in_core *iclog,
296 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000298 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
David Chinner114d23a2008-04-10 12:18:39 +1000300 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
302 if (!abortflg) {
303 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
304 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
305 cb->cb_next = NULL;
306 *(iclog->ic_callback_tail) = cb;
307 iclog->ic_callback_tail = &(cb->cb_next);
308 }
David Chinner114d23a2008-04-10 12:18:39 +1000309 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000314xfs_log_release_iclog(
315 struct xfs_mount *mp,
316 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000318 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000319 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100320 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 return 0;
324}
325
326/*
327 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
328 * to the reservation.
329 * 2. Potentially, push buffers at tail of log to disk.
330 *
331 * Each reservation is going to reserve extra space for a log record header.
332 * When writes happen to the on-disk log, we don't subtract the length of the
333 * log record header from any reservation. By wasting space in each
334 * reservation, we prevent over allocation problems.
335 */
336int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000337xfs_log_reserve(
338 struct xfs_mount *mp,
339 int unit_bytes,
340 int cnt,
341 struct xlog_ticket **ticket,
342 __uint8_t client,
343 uint flags,
344 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000346 struct log *log = mp->m_log;
347 struct xlog_ticket *internal_ticket;
348 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
351 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
352
353 if (XLOG_FORCED_SHUTDOWN(log))
354 return XFS_ERROR(EIO);
355
356 XFS_STATS_INC(xs_try_logspace);
357
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (*ticket != NULL) {
360 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000361 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000362
363 trace_xfs_log_reserve(log, internal_ticket);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
366 retval = xlog_regrant_write_log_space(log, internal_ticket);
367 } else {
368 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100369 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000371 if (!internal_ticket)
372 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000373 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000375
376 trace_xfs_log_reserve(log, internal_ticket);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 xlog_grant_push_ail(mp,
379 (internal_ticket->t_unit_res *
380 internal_ticket->t_cnt));
381 retval = xlog_grant_log_space(log, internal_ticket);
382 }
383
384 return retval;
385} /* xfs_log_reserve */
386
387
388/*
389 * Mount a log filesystem
390 *
391 * mp - ubiquitous xfs mount point structure
392 * log_target - buftarg of on-disk log device
393 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
394 * num_bblocks - Number of BBSIZE blocks in on-disk log
395 *
396 * Return error or zero.
397 */
398int
David Chinner249a8c12008-02-05 12:13:32 +1100399xfs_log_mount(
400 xfs_mount_t *mp,
401 xfs_buftarg_t *log_target,
402 xfs_daddr_t blk_offset,
403 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
David Chinner249a8c12008-02-05 12:13:32 +1100405 int error;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
408 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
409 else {
410 cmn_err(CE_NOTE,
411 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
412 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000413 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200417 if (IS_ERR(mp->m_log)) {
418 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100419 goto out;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
David Chinner249a8c12008-02-05 12:13:32 +1100423 * Initialize the AIL now we have a log.
424 */
David Chinner249a8c12008-02-05 12:13:32 +1100425 error = xfs_trans_ail_init(mp);
426 if (error) {
427 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100428 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100429 }
David Chinnera9c21c12008-10-30 17:39:35 +1100430 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100431
432 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * skip log recovery on a norecovery mount. pretend it all
434 * just worked.
435 */
436 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100437 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000440 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Eric Sandeen65be6052006-01-11 15:34:19 +1100442 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000445 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (error) {
447 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100448 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450 }
451
452 /* Normal transactions can now occur */
453 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100456
457out_destroy_ail:
458 xfs_trans_ail_destroy(mp);
459out_free_log:
460 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100461out:
David Chinner249a8c12008-02-05 12:13:32 +1100462 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100463}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465/*
466 * Finish the recovery of the file system. This is separate from
467 * the xfs_log_mount() call, because it depends on the code in
468 * xfs_mountfs() to read in the root and real-time bitmap inodes
469 * between calling xfs_log_mount() and here.
470 *
471 * mp - ubiquitous xfs mount point structure
472 */
473int
Christoph Hellwig42490232008-08-13 16:49:32 +1000474xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 int error;
477
478 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000479 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 else {
481 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000482 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
485 return error;
486}
487
488/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * Final log writes as part of unmount.
490 *
491 * Mark the filesystem clean as unmount happens. Note that during relocation
492 * this routine needs to be executed as part of source-bag while the
493 * deallocation must not be done until source-end.
494 */
495
496/*
497 * Unmount record used to have a string "Unmount filesystem--" in the
498 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
499 * We just write the magic number now since that particular field isn't
500 * currently architecture converted and "nUmount" is a bit foo.
501 * As far as I know, there weren't any dependencies on the old behaviour.
502 */
503
504int
505xfs_log_unmount_write(xfs_mount_t *mp)
506{
507 xlog_t *log = mp->m_log;
508 xlog_in_core_t *iclog;
509#ifdef DEBUG
510 xlog_in_core_t *first_iclog;
511#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000512 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 xfs_lsn_t lsn;
514 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * Don't write out unmount record on read-only mounts.
518 * Or, if we are doing a forced umount (typically because of IO errors).
519 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000520 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000523 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000524 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526#ifdef DEBUG
527 first_iclog = iclog = log->l_iclog;
528 do {
529 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
530 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
531 ASSERT(iclog->ic_offset == 0);
532 }
533 iclog = iclog->ic_next;
534 } while (iclog != first_iclog);
535#endif
536 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000537 error = xfs_log_reserve(mp, 600, 1, &tic,
538 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100540 /* the data section must be 32 bit size aligned */
541 struct {
542 __uint16_t magic;
543 __uint16_t pad1;
544 __uint32_t pad2; /* may as well make it 64 bits */
545 } magic = {
546 .magic = XLOG_UNMOUNT_TYPE,
547 };
548 struct xfs_log_iovec reg = {
549 .i_addr = (void *)&magic,
550 .i_len = sizeof(magic),
551 .i_type = XLOG_REG_TYPE_UNMOUNT,
552 };
553 struct xfs_log_vec vec = {
554 .lv_niovecs = 1,
555 .lv_iovecp = &reg,
556 };
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100559 tic->t_flags = 0;
560 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 NULL, XLOG_UNMOUNT_TRANS);
562 /*
563 * At this point, we're umounting anyway,
564 * so there's no point in transitioning log state
565 * to IOERROR. Just continue...
566 */
567 }
568
569 if (error) {
570 xfs_fs_cmn_err(CE_ALERT, mp,
571 "xfs_log_unmount: unmount record failed");
572 }
573
574
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000575 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100577 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100579 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000580 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000582 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
584 iclog->ic_state == XLOG_STATE_DIRTY)) {
585 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000586 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 &log->l_icloglock, s);
588 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000589 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000592 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000594 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000595 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000596 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100597 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
600 /*
601 * We're already in forced_shutdown mode, couldn't
602 * even attempt to write out the unmount transaction.
603 *
604 * Go through the motions of sync'ing and releasing
605 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000606 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * be in progress. Do this as a separate section of
608 * code so we'll know if we ever get stuck here that
609 * we're in this odd situation of trying to unmount
610 * a file system that went into forced_shutdown as
611 * the result of an unmount..
612 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000613 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100615 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100618 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000619 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000621 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
624 || iclog->ic_state == XLOG_STATE_DIRTY
625 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
626
David Chinner12017fa2008-08-13 16:34:31 +1000627 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 &log->l_icloglock, s);
629 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000630 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632 }
633
David Chinner1bb7d6b2008-04-10 12:24:38 +1000634 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635} /* xfs_log_unmount_write */
636
637/*
638 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100639 *
640 * We need to stop the aild from running before we destroy
641 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100644xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
David Chinner249a8c12008-02-05 12:13:32 +1100646 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000647 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Dave Chinner43f5efc2010-03-23 10:10:00 +1100650void
651xfs_log_item_init(
652 struct xfs_mount *mp,
653 struct xfs_log_item *item,
654 int type,
655 struct xfs_item_ops *ops)
656{
657 item->li_mountp = mp;
658 item->li_ailp = mp->m_ail;
659 item->li_type = type;
660 item->li_ops = ops;
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/*
664 * Write region vectors to log. The write happens using the space reservation
665 * of the ticket (tic). It is not a requirement that all writes for a given
Dave Chinner9b9fc2b72010-03-23 11:21:11 +1100666 * transaction occur with one call to xfs_log_write(). However, it is important
667 * to note that the transaction reservation code makes an assumption about the
668 * number of log headers a transaction requires that may be violated if you
669 * don't pass all the transaction vectors in one call....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
671int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000672xfs_log_write(
673 struct xfs_mount *mp,
674 struct xfs_log_iovec reg[],
675 int nentries,
676 struct xlog_ticket *tic,
677 xfs_lsn_t *start_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000679 struct log *log = mp->m_log;
680 int error;
Dave Chinner55b66332010-03-23 11:43:17 +1100681 struct xfs_log_vec vec = {
682 .lv_niovecs = nentries,
683 .lv_iovecp = reg,
684 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (XLOG_FORCED_SHUTDOWN(log))
687 return XFS_ERROR(EIO);
688
Dave Chinner55b66332010-03-23 11:43:17 +1100689 error = xlog_write(log, &vec, tic, start_lsn, NULL, 0);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000690 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +1000691 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100692 return error;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000693}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695void
696xfs_log_move_tail(xfs_mount_t *mp,
697 xfs_lsn_t tail_lsn)
698{
699 xlog_ticket_t *tic;
700 xlog_t *log = mp->m_log;
701 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (XLOG_FORCED_SHUTDOWN(log))
704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (tail_lsn == 0) {
707 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000708 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000710 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000713 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
716 * tail_lsn.
717 */
718 if (tail_lsn != 1) {
719 log->l_tail_lsn = tail_lsn;
720 }
721
722 if ((tic = log->l_write_headq)) {
723#ifdef DEBUG
724 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
725 panic("Recovery problem");
726#endif
727 cycle = log->l_grant_write_cycle;
728 bytes = log->l_grant_write_bytes;
729 free_bytes = xlog_space_left(log, cycle, bytes);
730 do {
731 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
732
733 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
734 break;
735 tail_lsn = 0;
736 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000737 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 tic = tic->t_next;
739 } while (tic != log->l_write_headq);
740 }
741 if ((tic = log->l_reserve_headq)) {
742#ifdef DEBUG
743 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
744 panic("Recovery problem");
745#endif
746 cycle = log->l_grant_reserve_cycle;
747 bytes = log->l_grant_reserve_bytes;
748 free_bytes = xlog_space_left(log, cycle, bytes);
749 do {
750 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
751 need_bytes = tic->t_unit_res*tic->t_cnt;
752 else
753 need_bytes = tic->t_unit_res;
754 if (free_bytes < need_bytes && tail_lsn != 1)
755 break;
756 tail_lsn = 0;
757 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000758 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 tic = tic->t_next;
760 } while (tic != log->l_reserve_headq);
761 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000762 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763} /* xfs_log_move_tail */
764
765/*
766 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000767 * that needs to be covered. To begin the transition to the idle state
768 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
769 * If we are then in a state where covering is needed, the caller is informed
770 * that dummy transactions are required to move the log into the idle state.
771 *
772 * Because this is called as part of the sync process, we should also indicate
773 * that dummy transactions should be issued in anything but the covered or
774 * idle states. This ensures that the log tail is accurately reflected in
775 * the log at the end of the sync, hence if a crash occurrs avoids replay
776 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778int
779xfs_log_need_covered(xfs_mount_t *mp)
780{
David Chinner27d8d5f2008-10-30 17:38:39 +1100781 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
David Chinner92821e22007-05-24 15:26:31 +1000784 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000787 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000788 switch (log->l_covered_state) {
789 case XLOG_STATE_COVER_DONE:
790 case XLOG_STATE_COVER_DONE2:
791 case XLOG_STATE_COVER_IDLE:
792 break;
793 case XLOG_STATE_COVER_NEED:
794 case XLOG_STATE_COVER_NEED2:
795 if (!xfs_trans_ail_tail(log->l_ailp) &&
796 xlog_iclogs_empty(log)) {
797 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
798 log->l_covered_state = XLOG_STATE_COVER_DONE;
799 else
800 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000802 /* FALLTHRU */
803 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000807 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100808 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/******************************************************************************
812 *
813 * local routines
814 *
815 ******************************************************************************
816 */
817
818/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
819 * The log manager must keep track of the last LR which was committed
820 * to disk. The lsn of this LR will become the new tail_lsn whenever
821 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
822 * the situation where stuff could be written into the log but nothing
823 * was ever in the AIL when asked. Eventually, we panic since the
824 * tail hits the head.
825 *
826 * We may be holding the log iclog lock upon entering this routine.
827 */
828xfs_lsn_t
829xlog_assign_tail_lsn(xfs_mount_t *mp)
830{
831 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 xlog_t *log = mp->m_log;
833
David Chinner5b00f142008-10-30 17:39:00 +1100834 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000835 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (tail_lsn != 0) {
837 log->l_tail_lsn = tail_lsn;
838 } else {
839 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
840 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000841 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 return tail_lsn;
844} /* xlog_assign_tail_lsn */
845
846
847/*
848 * Return the space in the log between the tail and the head. The head
849 * is passed in the cycle/bytes formal parms. In the special case where
850 * the reserve head has wrapped passed the tail, this calculation is no
851 * longer valid. In this case, just return 0 which means there is no space
852 * in the log. This works for all places where this function is called
853 * with the reserve head. Of course, if the write head were to ever
854 * wrap the tail, we should blow up. Rather than catch this case here,
855 * we depend on other ASSERTions in other parts of the code. XXXmiken
856 *
857 * This code also handles the case where the reservation head is behind
858 * the tail. The details of this case are described below, but the end
859 * result is that we return the size of the log as the amount of space left.
860 */
David Chinnera8272ce2007-11-23 16:28:09 +1100861STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862xlog_space_left(xlog_t *log, int cycle, int bytes)
863{
864 int free_bytes;
865 int tail_bytes;
866 int tail_cycle;
867
868 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
869 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
870 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
871 free_bytes = log->l_logsize - (bytes - tail_bytes);
872 } else if ((tail_cycle + 1) < cycle) {
873 return 0;
874 } else if (tail_cycle < cycle) {
875 ASSERT(tail_cycle == (cycle - 1));
876 free_bytes = tail_bytes - bytes;
877 } else {
878 /*
879 * The reservation head is behind the tail.
880 * In this case we just want to return the size of the
881 * log as the amount of space left.
882 */
883 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
884 "xlog_space_left: head behind tail\n"
885 " tail_cycle = %d, tail_bytes = %d\n"
886 " GH cycle = %d, GH bytes = %d",
887 tail_cycle, tail_bytes, cycle, bytes);
888 ASSERT(0);
889 free_bytes = log->l_logsize;
890 }
891 return free_bytes;
892} /* xlog_space_left */
893
894
895/*
896 * Log function which is called when an io completes.
897 *
898 * The log manager needs its own routine, in order to control what
899 * happens with the buffer after the write completes.
900 */
901void
902xlog_iodone(xfs_buf_t *bp)
903{
904 xlog_in_core_t *iclog;
905 xlog_t *l;
906 int aborted;
907
908 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
909 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
910 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
911 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 l = iclog->ic_log;
913
914 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100915 * If the _XFS_BARRIER_FAILED flag was set by a lower
916 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +1000917 * barrier I/O. Warn loudly and turn off barriers.
918 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100919 if (bp->b_flags & _XFS_BARRIER_FAILED) {
920 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000921 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
922 xfs_fs_cmn_err(CE_WARN, l->l_mp,
923 "xlog_iodone: Barriers are no longer supported"
924 " by device. Disabling barriers\n");
David Chinner0bfefc42007-05-14 18:24:23 +1000925 }
926
927 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * Race to shutdown the filesystem if we see an error.
929 */
930 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
931 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
932 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
933 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000934 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /*
936 * This flag will be propagated to the trans-committed
937 * callback routines to let them know that the log-commit
938 * didn't succeed.
939 */
940 aborted = XFS_LI_ABORTED;
941 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
942 aborted = XFS_LI_ABORTED;
943 }
David Chinner3db296f2007-05-14 18:24:16 +1000944
945 /* log I/O is always issued ASYNC */
946 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000948 /*
949 * do not reference the buffer (bp) here as we could race
950 * with it being freed after writing the unmount record to the
951 * log.
952 */
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954} /* xlog_iodone */
955
956/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * Return size of each in-core log record buffer.
958 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200959 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 *
961 * If the filesystem blocksize is too large, we may need to choose a
962 * larger size since the directory code currently logs entire blocks.
963 */
964
965STATIC void
966xlog_get_iclog_buffer_size(xfs_mount_t *mp,
967 xlog_t *log)
968{
969 int size;
970 int xhdrs;
971
Eric Sandeen1cb51252007-08-16 16:24:43 +1000972 if (mp->m_logbufs <= 0)
973 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
974 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100975 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /*
978 * Buffer size passed in from mount system call.
979 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100980 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 size = log->l_iclog_size = mp->m_logbsize;
982 log->l_iclog_size_log = 0;
983 while (size != 1) {
984 log->l_iclog_size_log++;
985 size >>= 1;
986 }
987
Eric Sandeen62118702008-03-06 13:44:28 +1100988 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200989 /* # headers = size / 32k
990 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
992
993 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
994 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
995 xhdrs++;
996 log->l_iclog_hsize = xhdrs << BBSHIFT;
997 log->l_iclog_heads = xhdrs;
998 } else {
999 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1000 log->l_iclog_hsize = BBSIZE;
1001 log->l_iclog_heads = 1;
1002 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001003 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001006 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001007 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1008 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 /* the default log size is 16k or 32k which is one header sector */
1011 log->l_iclog_hsize = BBSIZE;
1012 log->l_iclog_heads = 1;
1013
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001014done:
1015 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001016 if (mp->m_logbufs == 0)
1017 mp->m_logbufs = log->l_iclog_bufs;
1018 if (mp->m_logbsize == 0)
1019 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020} /* xlog_get_iclog_buffer_size */
1021
1022
1023/*
1024 * This routine initializes some of the log structure for a given mount point.
1025 * Its primary purpose is to fill in enough, so recovery can occur. However,
1026 * some other stuff may be filled in too.
1027 */
1028STATIC xlog_t *
1029xlog_alloc_log(xfs_mount_t *mp,
1030 xfs_buftarg_t *log_target,
1031 xfs_daddr_t blk_offset,
1032 int num_bblks)
1033{
1034 xlog_t *log;
1035 xlog_rec_header_t *head;
1036 xlog_in_core_t **iclogp;
1037 xlog_in_core_t *iclog, *prev_iclog=NULL;
1038 xfs_buf_t *bp;
1039 int i;
1040 int iclogsize;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001041 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001042 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Dave Chinner644c3562008-11-10 16:50:24 +11001044 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001045 if (!log) {
1046 xlog_warn("XFS: Log allocation failed: No memory!");
1047 goto out;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 log->l_mp = mp;
1051 log->l_targ = log_target;
1052 log->l_logsize = BBTOB(num_bblks);
1053 log->l_logBBstart = blk_offset;
1054 log->l_logBBsize = num_bblks;
1055 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1056 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1057
1058 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001059 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1061 log->l_last_sync_lsn = log->l_tail_lsn;
1062 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1063 log->l_grant_reserve_cycle = 1;
1064 log->l_grant_write_cycle = 1;
1065
Dave Chinnera6cb7672009-04-06 18:39:27 +02001066 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001067 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001068 log2_size = mp->m_sb.sb_logsectlog;
1069 if (log2_size < BBSHIFT) {
1070 xlog_warn("XFS: Log sector size too small "
1071 "(0x%x < 0x%x)", log2_size, BBSHIFT);
1072 goto out_free_log;
1073 }
1074
1075 log2_size -= BBSHIFT;
1076 if (log2_size > mp->m_sectbb_log) {
1077 xlog_warn("XFS: Log sector size too large "
1078 "(0x%x > 0x%x)", log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001079 goto out_free_log;
1080 }
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001083 if (log2_size && log->l_logBBstart > 0 &&
1084 !xfs_sb_version_haslogv2(&mp->m_sb)) {
1085
Dave Chinnera6cb7672009-04-06 18:39:27 +02001086 xlog_warn("XFS: log sector size (0x%x) invalid "
Alex Elder69ce58f2010-04-20 17:09:59 +10001087 "for configuration.", log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001088 goto out_free_log;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001091 log->l_sectBBsize = 1 << log2_size;
1092 log->l_sectbb_mask = log->l_sectBBsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 xlog_get_iclog_buffer_size(mp, log);
1095
Dave Chinnera6cb7672009-04-06 18:39:27 +02001096 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001098 if (!bp)
1099 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1102 ASSERT(XFS_BUF_ISBUSY(bp));
1103 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1104 log->l_xbuf = bp;
1105
Eric Sandeen007c61c2007-10-11 17:43:56 +10001106 spin_lock_init(&log->l_icloglock);
1107 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001108 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1111 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1112
1113 iclogp = &log->l_iclog;
1114 /*
1115 * The amount of memory to allocate for the iclog structure is
1116 * rather funky due to the way the structure is defined. It is
1117 * done this way so that we can use different sizes for machines
1118 * with different amounts of memory. See the definition of
1119 * xlog_in_core_t in xfs_log_priv.h for details.
1120 */
1121 iclogsize = log->l_iclog_size;
1122 ASSERT(log->l_iclog_size >= 4096);
1123 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001124 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1125 if (!*iclogp)
1126 goto out_free_iclog;
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 iclog->ic_prev = prev_iclog;
1130 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001131
1132 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001133 if (!bp)
1134 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001135 if (!XFS_BUF_CPSEMA(bp))
1136 ASSERT(0);
1137 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001138 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1139 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001140 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001141#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 head = &iclog->ic_header;
1145 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001146 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1147 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001148 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001149 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001151 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1155 iclog->ic_state = XLOG_STATE_ACTIVE;
1156 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001157 atomic_set(&iclog->ic_refcnt, 0);
1158 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001160 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1163 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001164 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1165 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 iclogp = &iclog->ic_next;
1168 }
1169 *iclogp = log->l_iclog; /* complete ring */
1170 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1171
1172 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001173
1174out_free_iclog:
1175 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1176 prev_iclog = iclog->ic_next;
1177 if (iclog->ic_bp) {
1178 sv_destroy(&iclog->ic_force_wait);
1179 sv_destroy(&iclog->ic_write_wait);
1180 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001181 }
1182 kmem_free(iclog);
1183 }
1184 spinlock_destroy(&log->l_icloglock);
1185 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001186 xfs_buf_free(log->l_xbuf);
1187out_free_log:
1188 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001189out:
1190 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191} /* xlog_alloc_log */
1192
1193
1194/*
1195 * Write out the commit record of a transaction associated with the given
1196 * ticket. Return the lsn of the commit record.
1197 */
1198STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001199xlog_commit_record(
1200 struct log *log,
1201 struct xlog_ticket *ticket,
1202 struct xlog_in_core **iclog,
1203 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Dave Chinner55b66332010-03-23 11:43:17 +11001205 struct xfs_mount *mp = log->l_mp;
1206 int error;
1207 struct xfs_log_iovec reg = {
1208 .i_addr = NULL,
1209 .i_len = 0,
1210 .i_type = XLOG_REG_TYPE_COMMIT,
1211 };
1212 struct xfs_log_vec vec = {
1213 .lv_niovecs = 1,
1214 .lv_iovecp = &reg,
1215 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001218 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1219 XLOG_COMMIT_TRANS);
1220 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001221 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001222 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001223}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225/*
1226 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1227 * log space. This code pushes on the lsn which would supposedly free up
1228 * the 25% which we want to leave free. We may need to adopt a policy which
1229 * pushes on an lsn which is further along in the log once we reach the high
1230 * water mark. In this manner, we would be creating a low water mark.
1231 */
David Chinnera8272ce2007-11-23 16:28:09 +11001232STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233xlog_grant_push_ail(xfs_mount_t *mp,
1234 int need_bytes)
1235{
1236 xlog_t *log = mp->m_log; /* pointer to the log */
1237 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1238 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1239 int free_blocks; /* free blocks left to write to */
1240 int free_bytes; /* free bytes left to write to */
1241 int threshold_block; /* block in lsn we'd like to be at */
1242 int threshold_cycle; /* lsn cycle we'd like to be at */
1243 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1246
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001247 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 free_bytes = xlog_space_left(log,
1249 log->l_grant_reserve_cycle,
1250 log->l_grant_reserve_bytes);
1251 tail_lsn = log->l_tail_lsn;
1252 free_blocks = BTOBBT(free_bytes);
1253
1254 /*
1255 * Set the threshold for the minimum number of free blocks in the
1256 * log to the maximum of what the caller needs, one quarter of the
1257 * log, and 256 blocks.
1258 */
1259 free_threshold = BTOBB(need_bytes);
1260 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1261 free_threshold = MAX(free_threshold, 256);
1262 if (free_blocks < free_threshold) {
1263 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1264 threshold_cycle = CYCLE_LSN(tail_lsn);
1265 if (threshold_block >= log->l_logBBsize) {
1266 threshold_block -= log->l_logBBsize;
1267 threshold_cycle += 1;
1268 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001269 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Don't pass in an lsn greater than the lsn of the last
1272 * log record known to be on disk.
1273 */
1274 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1275 threshold_lsn = log->l_last_sync_lsn;
1276 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001277 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /*
1280 * Get the transaction layer to kick the dirty buffers out to
1281 * disk asynchronously. No point in trying to do this if
1282 * the filesystem is shutting down.
1283 */
1284 if (threshold_lsn &&
1285 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001286 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287} /* xlog_grant_push_ail */
1288
Christoph Hellwig873ff552010-01-13 22:17:57 +00001289/*
1290 * The bdstrat callback function for log bufs. This gives us a central
1291 * place to trap bufs in case we get hit by a log I/O error and need to
1292 * shutdown. Actually, in practice, even when we didn't get a log error,
1293 * we transition the iclogs to IOERROR state *after* flushing all existing
1294 * iclogs to disk. This is because we don't want anymore new transactions to be
1295 * started or completed afterwards.
1296 */
1297STATIC int
1298xlog_bdstrat(
1299 struct xfs_buf *bp)
1300{
1301 struct xlog_in_core *iclog;
1302
1303 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1304 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1305 XFS_BUF_ERROR(bp, EIO);
1306 XFS_BUF_STALE(bp);
1307 xfs_biodone(bp);
1308 /*
1309 * It would seem logical to return EIO here, but we rely on
1310 * the log state machine to propagate I/O errors instead of
1311 * doing it here.
1312 */
1313 return 0;
1314 }
1315
1316 bp->b_flags |= _XBF_RUN_QUEUES;
1317 xfs_buf_iorequest(bp);
1318 return 0;
1319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321/*
1322 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1323 * fashion. Previously, we should have moved the current iclog
1324 * ptr in the log to point to the next available iclog. This allows further
1325 * write to continue while this code syncs out an iclog ready to go.
1326 * Before an in-core log can be written out, the data section must be scanned
1327 * to save away the 1st word of each BBSIZE block into the header. We replace
1328 * it with the current cycle count. Each BBSIZE block is tagged with the
1329 * cycle count because there in an implicit assumption that drives will
1330 * guarantee that entire 512 byte blocks get written at once. In other words,
1331 * we can't have part of a 512 byte block written and part not written. By
1332 * tagging each block, we will know which blocks are valid when recovering
1333 * after an unclean shutdown.
1334 *
1335 * This routine is single threaded on the iclog. No other thread can be in
1336 * this routine with the same iclog. Changing contents of iclog can there-
1337 * fore be done without grabbing the state machine lock. Updating the global
1338 * log will require grabbing the lock though.
1339 *
1340 * The entire log manager uses a logical block numbering scheme. Only
1341 * log_sync (and then only bwrite()) know about the fact that the log may
1342 * not start with block zero on a given device. The log block start offset
1343 * is added immediately before calling bwrite().
1344 */
1345
David Chinnera8272ce2007-11-23 16:28:09 +11001346STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347xlog_sync(xlog_t *log,
1348 xlog_in_core_t *iclog)
1349{
1350 xfs_caddr_t dptr; /* pointer to byte sized element */
1351 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001352 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 uint count; /* byte count of bwrite */
1354 uint count_init; /* initial count before roundup */
1355 int roundoff; /* roundoff to BB or stripe */
1356 int split = 0; /* split write into two regions */
1357 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001358 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001361 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /* Add for LR header */
1364 count_init = log->l_iclog_hsize + iclog->ic_offset;
1365
1366 /* Round out the log write size */
1367 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1368 /* we have a v2 stripe unit to use */
1369 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1370 } else {
1371 count = BBTOB(BTOBB(count_init));
1372 }
1373 roundoff = count - count_init;
1374 ASSERT(roundoff >= 0);
1375 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1376 roundoff < log->l_mp->m_sb.sb_logsunit)
1377 ||
1378 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1379 roundoff < BBTOB(1)));
1380
1381 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001382 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001383 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001384 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* put cycle number in every block */
1387 xlog_pack_data(log, iclog, roundoff);
1388
1389 /* real byte length */
1390 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001391 iclog->ic_header.h_len =
1392 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001394 iclog->ic_header.h_len =
1395 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
Nathan Scottf5faad72006-07-28 17:04:44 +10001398 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1400 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001401 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1404
1405 /* Do we need to split this write into 2 parts? */
1406 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1407 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1408 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1409 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1410 } else {
1411 iclog->ic_bwritecnt = 1;
1412 }
David Chinner511105b2007-05-24 15:21:57 +10001413 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001415 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 XFS_BUF_BUSY(bp);
1417 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001418 bp->b_flags |= XBF_LOG_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001420 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001421 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001423 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001424 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1427 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1428
1429 xlog_verify_iclog(log, iclog, count, B_TRUE);
1430
1431 /* account for log which doesn't start at block #0 */
1432 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1433 /*
1434 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1435 * is shutting down.
1436 */
1437 XFS_BUF_WRITE(bp);
1438
Christoph Hellwig873ff552010-01-13 22:17:57 +00001439 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1441 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001442 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001445 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1447 (unsigned long)1);
1448 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1449 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1450 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1451 (__psint_t)count), split);
1452 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001453 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 XFS_BUF_BUSY(bp);
1455 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001456 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001457 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1458 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 dptr = XFS_BUF_PTR(bp);
1460 /*
1461 * Bump the cycle numbers at the start of each block
1462 * since this part of the buffer is at the start of
1463 * a new cycle. Watch out for the header magic number
1464 * case, though.
1465 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001466 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001467 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001468 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001469 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 dptr += BBSIZE;
1471 }
1472
1473 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1474 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1475
Nathan Scottc41564b2006-03-29 08:55:14 +10001476 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1478 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001479 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1481 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001482 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486} /* xlog_sync */
1487
1488
1489/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001490 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
David Chinnera8272ce2007-11-23 16:28:09 +11001492STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001493xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 int i;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 iclog = log->l_iclog;
1499 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001500 sv_destroy(&iclog->ic_force_wait);
1501 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001504 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 iclog = next_iclog;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 spinlock_destroy(&log->l_icloglock);
1508 spinlock_destroy(&log->l_grant_lock);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001512 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001513} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515/*
1516 * Update counters atomically now that memcpy is done.
1517 */
1518/* ARGSUSED */
1519static inline void
1520xlog_state_finish_copy(xlog_t *log,
1521 xlog_in_core_t *iclog,
1522 int record_cnt,
1523 int copy_bytes)
1524{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001525 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001527 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 iclog->ic_offset += copy_bytes;
1529
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001530 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531} /* xlog_state_finish_copy */
1532
1533
1534
1535
1536/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001537 * print out info relating to regions written which consume
1538 * the reservation
1539 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001540STATIC void
1541xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1542{
1543 uint i;
1544 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1545
1546 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1547 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1548 "bformat",
1549 "bchunk",
1550 "efi_format",
1551 "efd_format",
1552 "iformat",
1553 "icore",
1554 "iext",
1555 "ibroot",
1556 "ilocal",
1557 "iattr_ext",
1558 "iattr_broot",
1559 "iattr_local",
1560 "qformat",
1561 "dquot",
1562 "quotaoff",
1563 "LR header",
1564 "unmount",
1565 "commit",
1566 "trans header"
1567 };
1568 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1569 "SETATTR_NOT_SIZE",
1570 "SETATTR_SIZE",
1571 "INACTIVE",
1572 "CREATE",
1573 "CREATE_TRUNC",
1574 "TRUNCATE_FILE",
1575 "REMOVE",
1576 "LINK",
1577 "RENAME",
1578 "MKDIR",
1579 "RMDIR",
1580 "SYMLINK",
1581 "SET_DMATTRS",
1582 "GROWFS",
1583 "STRAT_WRITE",
1584 "DIOSTRAT",
1585 "WRITE_SYNC",
1586 "WRITEID",
1587 "ADDAFORK",
1588 "ATTRINVAL",
1589 "ATRUNCATE",
1590 "ATTR_SET",
1591 "ATTR_RM",
1592 "ATTR_FLAG",
1593 "CLEAR_AGI_BUCKET",
1594 "QM_SBCHANGE",
1595 "DUMMY1",
1596 "DUMMY2",
1597 "QM_QUOTAOFF",
1598 "QM_DQALLOC",
1599 "QM_SETQLIM",
1600 "QM_DQCLUSTER",
1601 "QM_QINOCREATE",
1602 "QM_QUOTAOFF_END",
1603 "SB_UNIT",
1604 "FSYNC_TS",
1605 "GROWFSRT_ALLOC",
1606 "GROWFSRT_ZERO",
1607 "GROWFSRT_FREE",
1608 "SWAPEXT"
1609 };
1610
1611 xfs_fs_cmn_err(CE_WARN, mp,
1612 "xfs_log_write: reservation summary:\n"
1613 " trans type = %s (%u)\n"
1614 " unit res = %d bytes\n"
1615 " current res = %d bytes\n"
1616 " total reg = %u bytes (o/flow = %u bytes)\n"
1617 " ophdrs = %u (ophdr space = %u bytes)\n"
1618 " ophdr + reg = %u bytes\n"
1619 " num regions = %u\n",
1620 ((ticket->t_trans_type <= 0 ||
1621 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1622 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1623 ticket->t_trans_type,
1624 ticket->t_unit_res,
1625 ticket->t_curr_res,
1626 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1627 ticket->t_res_num_ophdrs, ophdr_spc,
1628 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001629 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001630 ticket->t_res_num);
1631
1632 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001633 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001634 cmn_err(CE_WARN,
1635 "region[%u]: %s - %u bytes\n",
1636 i,
1637 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1638 "bad-rtype" : res_type_str[r_type-1]),
1639 ticket->t_res_arr[i].r_len);
1640 }
1641}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001642
1643/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001644 * Calculate the potential space needed by the log vector. Each region gets
1645 * its own xlog_op_header_t and may need to be double word aligned.
1646 */
1647static int
1648xlog_write_calc_vec_length(
1649 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001650 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001651{
Dave Chinner55b66332010-03-23 11:43:17 +11001652 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001653 int headers = 0;
1654 int len = 0;
1655 int i;
1656
1657 /* acct for start rec of xact */
1658 if (ticket->t_flags & XLOG_TIC_INITED)
1659 headers++;
1660
Dave Chinner55b66332010-03-23 11:43:17 +11001661 for (lv = log_vector; lv; lv = lv->lv_next) {
1662 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001663
Dave Chinner55b66332010-03-23 11:43:17 +11001664 for (i = 0; i < lv->lv_niovecs; i++) {
1665 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1666
1667 len += vecp->i_len;
1668 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1669 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001670 }
1671
1672 ticket->t_res_num_ophdrs += headers;
1673 len += headers * sizeof(struct xlog_op_header);
1674
1675 return len;
1676}
1677
1678/*
1679 * If first write for transaction, insert start record We can't be trying to
1680 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1681 */
1682static int
1683xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001684 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001685 struct xlog_ticket *ticket)
1686{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001687 if (!(ticket->t_flags & XLOG_TIC_INITED))
1688 return 0;
1689
1690 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1691 ophdr->oh_clientid = ticket->t_clientid;
1692 ophdr->oh_len = 0;
1693 ophdr->oh_flags = XLOG_START_TRANS;
1694 ophdr->oh_res2 = 0;
1695
1696 ticket->t_flags &= ~XLOG_TIC_INITED;
1697
1698 return sizeof(struct xlog_op_header);
1699}
1700
1701static xlog_op_header_t *
1702xlog_write_setup_ophdr(
1703 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001704 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001705 struct xlog_ticket *ticket,
1706 uint flags)
1707{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001708 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1709 ophdr->oh_clientid = ticket->t_clientid;
1710 ophdr->oh_res2 = 0;
1711
1712 /* are we copying a commit or unmount record? */
1713 ophdr->oh_flags = flags;
1714
1715 /*
1716 * We've seen logs corrupted with bad transaction client ids. This
1717 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1718 * and shut down the filesystem.
1719 */
1720 switch (ophdr->oh_clientid) {
1721 case XFS_TRANSACTION:
1722 case XFS_VOLUME:
1723 case XFS_LOG:
1724 break;
1725 default:
1726 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1727 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1728 ophdr->oh_clientid, ticket);
1729 return NULL;
1730 }
1731
1732 return ophdr;
1733}
1734
1735/*
1736 * Set up the parameters of the region copy into the log. This has
1737 * to handle region write split across multiple log buffers - this
1738 * state is kept external to this function so that this code can
1739 * can be written in an obvious, self documenting manner.
1740 */
1741static int
1742xlog_write_setup_copy(
1743 struct xlog_ticket *ticket,
1744 struct xlog_op_header *ophdr,
1745 int space_available,
1746 int space_required,
1747 int *copy_off,
1748 int *copy_len,
1749 int *last_was_partial_copy,
1750 int *bytes_consumed)
1751{
1752 int still_to_copy;
1753
1754 still_to_copy = space_required - *bytes_consumed;
1755 *copy_off = *bytes_consumed;
1756
1757 if (still_to_copy <= space_available) {
1758 /* write of region completes here */
1759 *copy_len = still_to_copy;
1760 ophdr->oh_len = cpu_to_be32(*copy_len);
1761 if (*last_was_partial_copy)
1762 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1763 *last_was_partial_copy = 0;
1764 *bytes_consumed = 0;
1765 return 0;
1766 }
1767
1768 /* partial write of region, needs extra log op header reservation */
1769 *copy_len = space_available;
1770 ophdr->oh_len = cpu_to_be32(*copy_len);
1771 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1772 if (*last_was_partial_copy)
1773 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1774 *bytes_consumed += *copy_len;
1775 (*last_was_partial_copy)++;
1776
1777 /* account for new log op header */
1778 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1779 ticket->t_res_num_ophdrs++;
1780
1781 return sizeof(struct xlog_op_header);
1782}
1783
1784static int
1785xlog_write_copy_finish(
1786 struct log *log,
1787 struct xlog_in_core *iclog,
1788 uint flags,
1789 int *record_cnt,
1790 int *data_cnt,
1791 int *partial_copy,
1792 int *partial_copy_len,
1793 int log_offset,
1794 struct xlog_in_core **commit_iclog)
1795{
1796 if (*partial_copy) {
1797 /*
1798 * This iclog has already been marked WANT_SYNC by
1799 * xlog_state_get_iclog_space.
1800 */
1801 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1802 *record_cnt = 0;
1803 *data_cnt = 0;
1804 return xlog_state_release_iclog(log, iclog);
1805 }
1806
1807 *partial_copy = 0;
1808 *partial_copy_len = 0;
1809
1810 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1811 /* no more space in this iclog - push it. */
1812 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1813 *record_cnt = 0;
1814 *data_cnt = 0;
1815
1816 spin_lock(&log->l_icloglock);
1817 xlog_state_want_sync(log, iclog);
1818 spin_unlock(&log->l_icloglock);
1819
1820 if (!commit_iclog)
1821 return xlog_state_release_iclog(log, iclog);
1822 ASSERT(flags & XLOG_COMMIT_TRANS);
1823 *commit_iclog = iclog;
1824 }
1825
1826 return 0;
1827}
1828
1829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * Write some region out to in-core log
1831 *
1832 * This will be called when writing externally provided regions or when
1833 * writing out a commit record for a given transaction.
1834 *
1835 * General algorithm:
1836 * 1. Find total length of this write. This may include adding to the
1837 * lengths passed in.
1838 * 2. Check whether we violate the tickets reservation.
1839 * 3. While writing to this iclog
1840 * A. Reserve as much space in this iclog as can get
1841 * B. If this is first write, save away start lsn
1842 * C. While writing this region:
1843 * 1. If first write of transaction, write start record
1844 * 2. Write log operation header (header per region)
1845 * 3. Find out if we can fit entire region into this iclog
1846 * 4. Potentially, verify destination memcpy ptr
1847 * 5. Memcpy (partial) region
1848 * 6. If partial copy, release iclog; otherwise, continue
1849 * copying more regions into current iclog
1850 * 4. Mark want sync bit (in simulation mode)
1851 * 5. Release iclog for potential flush to on-disk log.
1852 *
1853 * ERRORS:
1854 * 1. Panic if reservation is overrun. This should never happen since
1855 * reservation amounts are generated internal to the filesystem.
1856 * NOTES:
1857 * 1. Tickets are single threaded data structures.
1858 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1859 * syncing routine. When a single log_write region needs to span
1860 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1861 * on all log operation writes which don't contain the end of the
1862 * region. The XLOG_END_TRANS bit is used for the in-core log
1863 * operation which contains the end of the continued log_write region.
1864 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1865 * we don't really know exactly how much space will be used. As a result,
1866 * we don't update ic_offset until the end when we know exactly how many
1867 * bytes have been written out.
1868 */
David Chinnera8272ce2007-11-23 16:28:09 +11001869STATIC int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001870xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001871 struct log *log,
1872 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001873 struct xlog_ticket *ticket,
1874 xfs_lsn_t *start_lsn,
1875 struct xlog_in_core **commit_iclog,
1876 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001878 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001879 struct xfs_log_iovec *vecp;
1880 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001881 int len;
1882 int index;
1883 int partial_copy = 0;
1884 int partial_copy_len = 0;
1885 int contwr = 0;
1886 int record_cnt = 0;
1887 int data_cnt = 0;
1888 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001890 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Dave Chinner55b66332010-03-23 11:43:17 +11001892 len = xlog_write_calc_vec_length(ticket, log_vector);
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001893 if (ticket->t_curr_res < len) {
Dave Chinner55b66332010-03-23 11:43:17 +11001894 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895#ifdef DEBUG
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001896 xlog_panic(
1897 "xfs_log_write: reservation ran out. Need to up reservation");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898#else
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001899 /* Customer configurable panic */
Dave Chinner55b66332010-03-23 11:43:17 +11001900 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, log->l_mp,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001901 "xfs_log_write: reservation ran out. Need to up reservation");
1902
1903 /* If we did not panic, shutdown the filesystem */
Dave Chinner55b66332010-03-23 11:43:17 +11001904 xfs_force_shutdown(log->l_mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905#endif
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001906 }
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ticket->t_curr_res -= len;
1909
Dave Chinner55b66332010-03-23 11:43:17 +11001910 index = 0;
1911 lv = log_vector;
1912 vecp = lv->lv_iovecp;
1913 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001914 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001915 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001917 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1918 &contwr, &log_offset);
1919 if (error)
1920 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001922 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001923 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001925 /* start_lsn is the first lsn written to. That's all we need. */
1926 if (!*start_lsn)
1927 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001929 /*
1930 * This loop writes out as many regions as can fit in the amount
1931 * of space which was allocated by xlog_state_get_iclog_space().
1932 */
Dave Chinner55b66332010-03-23 11:43:17 +11001933 while (lv && index < lv->lv_niovecs) {
1934 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001935 struct xlog_op_header *ophdr;
1936 int start_rec_copy;
1937 int copy_len;
1938 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Dave Chinner55b66332010-03-23 11:43:17 +11001940 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001941 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001943 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1944 if (start_rec_copy) {
1945 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001946 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001947 start_rec_copy);
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001950 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1951 if (!ophdr)
1952 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001954 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001955 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001957 len += xlog_write_setup_copy(ticket, ophdr,
1958 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001959 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001960 &copy_off, &copy_len,
1961 &partial_copy,
1962 &partial_copy_len);
1963 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001964
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001965 /* copy region */
1966 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001967 memcpy(ptr, reg->i_addr + copy_off, copy_len);
1968 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001969
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001970 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1971 record_cnt++;
1972 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001974 error = xlog_write_copy_finish(log, iclog, flags,
1975 &record_cnt, &data_cnt,
1976 &partial_copy,
1977 &partial_copy_len,
1978 log_offset,
1979 commit_iclog);
1980 if (error)
1981 return error;
1982
1983 /*
1984 * if we had a partial copy, we need to get more iclog
1985 * space but we don't want to increment the region
1986 * index because there is still more is this region to
1987 * write.
1988 *
1989 * If we completed writing this region, and we flushed
1990 * the iclog (indicated by resetting of the record
1991 * count), then we also need to get more log space. If
1992 * this was the last record, though, we are done and
1993 * can just return.
1994 */
1995 if (partial_copy)
1996 break;
1997
Dave Chinner55b66332010-03-23 11:43:17 +11001998 if (++index == lv->lv_niovecs) {
1999 lv = lv->lv_next;
2000 index = 0;
2001 if (lv)
2002 vecp = lv->lv_iovecp;
2003 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002004 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11002005 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002006 return 0;
2007 break;
2008 }
2009 }
2010 }
2011
2012 ASSERT(len == 0);
2013
2014 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2015 if (!commit_iclog)
2016 return xlog_state_release_iclog(log, iclog);
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 ASSERT(flags & XLOG_COMMIT_TRANS);
2019 *commit_iclog = iclog;
2020 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023
2024/*****************************************************************************
2025 *
2026 * State Machine functions
2027 *
2028 *****************************************************************************
2029 */
2030
2031/* Clean iclogs starting from the head. This ordering must be
2032 * maintained, so an iclog doesn't become ACTIVE beyond one that
2033 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002034 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 * iclog is trying to sync to disk.
2036 *
2037 * State Change: DIRTY -> ACTIVE
2038 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002039STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040xlog_state_clean_log(xlog_t *log)
2041{
2042 xlog_in_core_t *iclog;
2043 int changed = 0;
2044
2045 iclog = log->l_iclog;
2046 do {
2047 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2048 iclog->ic_state = XLOG_STATE_ACTIVE;
2049 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002050 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /*
2052 * If the number of ops in this iclog indicate it just
2053 * contains the dummy transaction, we can
2054 * change state into IDLE (the second time around).
2055 * Otherwise we should change the state into
2056 * NEED a dummy.
2057 * We don't need to cover the dummy.
2058 */
2059 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002060 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2061 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 changed = 1;
2063 } else {
2064 /*
2065 * We have two dirty iclogs so start over
2066 * This could also be num of ops indicates
2067 * this is not the dummy going out.
2068 */
2069 changed = 2;
2070 }
2071 iclog->ic_header.h_num_logops = 0;
2072 memset(iclog->ic_header.h_cycle_data, 0,
2073 sizeof(iclog->ic_header.h_cycle_data));
2074 iclog->ic_header.h_lsn = 0;
2075 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2076 /* do nothing */;
2077 else
2078 break; /* stop cleaning */
2079 iclog = iclog->ic_next;
2080 } while (iclog != log->l_iclog);
2081
2082 /* log is locked when we are called */
2083 /*
2084 * Change state for the dummy log recording.
2085 * We usually go to NEED. But we go to NEED2 if the changed indicates
2086 * we are done writing the dummy record.
2087 * If we are done with the second dummy recored (DONE2), then
2088 * we go to IDLE.
2089 */
2090 if (changed) {
2091 switch (log->l_covered_state) {
2092 case XLOG_STATE_COVER_IDLE:
2093 case XLOG_STATE_COVER_NEED:
2094 case XLOG_STATE_COVER_NEED2:
2095 log->l_covered_state = XLOG_STATE_COVER_NEED;
2096 break;
2097
2098 case XLOG_STATE_COVER_DONE:
2099 if (changed == 1)
2100 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2101 else
2102 log->l_covered_state = XLOG_STATE_COVER_NEED;
2103 break;
2104
2105 case XLOG_STATE_COVER_DONE2:
2106 if (changed == 1)
2107 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2108 else
2109 log->l_covered_state = XLOG_STATE_COVER_NEED;
2110 break;
2111
2112 default:
2113 ASSERT(0);
2114 }
2115 }
2116} /* xlog_state_clean_log */
2117
2118STATIC xfs_lsn_t
2119xlog_get_lowest_lsn(
2120 xlog_t *log)
2121{
2122 xlog_in_core_t *lsn_log;
2123 xfs_lsn_t lowest_lsn, lsn;
2124
2125 lsn_log = log->l_iclog;
2126 lowest_lsn = 0;
2127 do {
2128 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002129 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if ((lsn && !lowest_lsn) ||
2131 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2132 lowest_lsn = lsn;
2133 }
2134 }
2135 lsn_log = lsn_log->ic_next;
2136 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002137 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140
2141STATIC void
2142xlog_state_do_callback(
2143 xlog_t *log,
2144 int aborted,
2145 xlog_in_core_t *ciclog)
2146{
2147 xlog_in_core_t *iclog;
2148 xlog_in_core_t *first_iclog; /* used to know when we've
2149 * processed all iclogs once */
2150 xfs_log_callback_t *cb, *cb_next;
2151 int flushcnt = 0;
2152 xfs_lsn_t lowest_lsn;
2153 int ioerrors; /* counter: iclogs with errors */
2154 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2155 int funcdidcallbacks; /* flag: function did callbacks */
2156 int repeats; /* for issuing console warnings if
2157 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002158 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002160 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 first_iclog = iclog = log->l_iclog;
2162 ioerrors = 0;
2163 funcdidcallbacks = 0;
2164 repeats = 0;
2165
2166 do {
2167 /*
2168 * Scan all iclogs starting with the one pointed to by the
2169 * log. Reset this starting point each time the log is
2170 * unlocked (during callbacks).
2171 *
2172 * Keep looping through iclogs until one full pass is made
2173 * without running any callbacks.
2174 */
2175 first_iclog = log->l_iclog;
2176 iclog = log->l_iclog;
2177 loopdidcallbacks = 0;
2178 repeats++;
2179
2180 do {
2181
2182 /* skip all iclogs in the ACTIVE & DIRTY states */
2183 if (iclog->ic_state &
2184 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2185 iclog = iclog->ic_next;
2186 continue;
2187 }
2188
2189 /*
2190 * Between marking a filesystem SHUTDOWN and stopping
2191 * the log, we do flush all iclogs to disk (if there
2192 * wasn't a log I/O error). So, we do want things to
2193 * go smoothly in case of just a SHUTDOWN w/o a
2194 * LOG_IO_ERROR.
2195 */
2196 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2197 /*
2198 * Can only perform callbacks in order. Since
2199 * this iclog is not in the DONE_SYNC/
2200 * DO_CALLBACK state, we skip the rest and
2201 * just try to clean up. If we set our iclog
2202 * to DO_CALLBACK, we will not process it when
2203 * we retry since a previous iclog is in the
2204 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002205 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 */
2207 if (!(iclog->ic_state &
2208 (XLOG_STATE_DONE_SYNC |
2209 XLOG_STATE_DO_CALLBACK))) {
2210 if (ciclog && (ciclog->ic_state ==
2211 XLOG_STATE_DONE_SYNC)) {
2212 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2213 }
2214 break;
2215 }
2216 /*
2217 * We now have an iclog that is in either the
2218 * DO_CALLBACK or DONE_SYNC states. The other
2219 * states (WANT_SYNC, SYNCING, or CALLBACK were
2220 * caught by the above if and are going to
2221 * clean (i.e. we aren't doing their callbacks)
2222 * see the above if.
2223 */
2224
2225 /*
2226 * We will do one more check here to see if we
2227 * have chased our tail around.
2228 */
2229
2230 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002231 if (lowest_lsn &&
2232 XFS_LSN_CMP(lowest_lsn,
2233 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 iclog = iclog->ic_next;
2235 continue; /* Leave this iclog for
2236 * another thread */
2237 }
2238
2239 iclog->ic_state = XLOG_STATE_CALLBACK;
2240
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002241 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002244 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 * No one else can be here except us.
2246 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002247 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002248 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2249 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2250 log->l_last_sync_lsn =
2251 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002252 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002255 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 ioerrors++;
2257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
David Chinner114d23a2008-04-10 12:18:39 +10002259 /*
2260 * Keep processing entries in the callback list until
2261 * we come around and it is empty. We need to
2262 * atomically see that the list is empty and change the
2263 * state to DIRTY so that we don't miss any more
2264 * callbacks being added.
2265 */
2266 spin_lock(&iclog->ic_callback_lock);
2267 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002268 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 iclog->ic_callback_tail = &(iclog->ic_callback);
2270 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002271 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002274 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 cb_next = cb->cb_next;
2276 cb->cb_func(cb->cb_arg, aborted);
2277 }
David Chinner114d23a2008-04-10 12:18:39 +10002278 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 cb = iclog->ic_callback;
2280 }
2281
2282 loopdidcallbacks++;
2283 funcdidcallbacks++;
2284
David Chinner114d23a2008-04-10 12:18:39 +10002285 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002286 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002287 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2289 iclog->ic_state = XLOG_STATE_DIRTY;
2290
2291 /*
2292 * Transition from DIRTY to ACTIVE if applicable.
2293 * NOP if STATE_IOERROR.
2294 */
2295 xlog_state_clean_log(log);
2296
2297 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002298 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 iclog = iclog->ic_next;
2301 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002302
2303 if (repeats > 5000) {
2304 flushcnt += repeats;
2305 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002307 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002308 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310 } while (!ioerrors && loopdidcallbacks);
2311
2312 /*
2313 * make one last gasp attempt to see if iclogs are being left in
2314 * limbo..
2315 */
2316#ifdef DEBUG
2317 if (funcdidcallbacks) {
2318 first_iclog = iclog = log->l_iclog;
2319 do {
2320 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2321 /*
2322 * Terminate the loop if iclogs are found in states
2323 * which will cause other threads to clean up iclogs.
2324 *
2325 * SYNCING - i/o completion will go through logs
2326 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002327 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 * IOERROR - give up hope all ye who enter here
2329 */
2330 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2331 iclog->ic_state == XLOG_STATE_SYNCING ||
2332 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2333 iclog->ic_state == XLOG_STATE_IOERROR )
2334 break;
2335 iclog = iclog->ic_next;
2336 } while (first_iclog != iclog);
2337 }
2338#endif
2339
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002340 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2341 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002342 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002343
2344 if (wake)
2345 sv_broadcast(&log->l_flush_wait);
2346}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348
2349/*
2350 * Finish transitioning this iclog to the dirty state.
2351 *
2352 * Make sure that we completely execute this routine only when this is
2353 * the last call to the iclog. There is a good chance that iclog flushes,
2354 * when we reach the end of the physical log, get turned into 2 separate
2355 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2356 * routine. By using the reference count bwritecnt, we guarantee that only
2357 * the second completion goes through.
2358 *
2359 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002360 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 */
David Chinnera8272ce2007-11-23 16:28:09 +11002362STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363xlog_state_done_syncing(
2364 xlog_in_core_t *iclog,
2365 int aborted)
2366{
2367 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002369 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2372 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002373 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2375
2376
2377 /*
2378 * If we got an error, either on the first buffer, or in the case of
2379 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2380 * and none should ever be attempted to be written to disk
2381 * again.
2382 */
2383 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2384 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002385 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return;
2387 }
2388 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2389 }
2390
2391 /*
2392 * Someone could be sleeping prior to writing out the next
2393 * iclog buffer, we wake them all, one will get to do the
2394 * I/O, the others get to wait for the result.
2395 */
David Chinner12017fa2008-08-13 16:34:31 +10002396 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002397 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2399} /* xlog_state_done_syncing */
2400
2401
2402/*
2403 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002404 * sleep. We wait on the flush queue on the head iclog as that should be
2405 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2406 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 *
2408 * The in-core logs are used in a circular fashion. They are not used
2409 * out-of-order even when an iclog past the head is free.
2410 *
2411 * return:
2412 * * log_offset where xlog_write() can start writing into the in-core
2413 * log's data space.
2414 * * in-core log pointer to which xlog_write() should write.
2415 * * boolean indicating this is a continued write to an in-core log.
2416 * If this is the last write, then the in-core log's offset field
2417 * needs to be incremented, depending on the amount of data which
2418 * is copied.
2419 */
David Chinnera8272ce2007-11-23 16:28:09 +11002420STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421xlog_state_get_iclog_space(xlog_t *log,
2422 int len,
2423 xlog_in_core_t **iclogp,
2424 xlog_ticket_t *ticket,
2425 int *continued_write,
2426 int *logoffsetp)
2427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 int log_offset;
2429 xlog_rec_header_t *head;
2430 xlog_in_core_t *iclog;
2431 int error;
2432
2433restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002434 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002436 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return XFS_ERROR(EIO);
2438 }
2439
2440 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002441 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002443
2444 /* Wait for log writes to have flushed */
2445 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto restart;
2447 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 head = &iclog->ic_header;
2450
David Chinner155cc6b2008-03-06 13:44:14 +11002451 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 log_offset = iclog->ic_offset;
2453
2454 /* On the 1st write to an iclog, figure out lsn. This works
2455 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2456 * committing to. If the offset is set, that's how many blocks
2457 * must be written.
2458 */
2459 if (log_offset == 0) {
2460 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002461 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002462 log->l_iclog_hsize,
2463 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002464 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2465 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002466 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 ASSERT(log->l_curr_block >= 0);
2468 }
2469
2470 /* If there is enough room to write everything, then do it. Otherwise,
2471 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2472 * bit is on, so this will get flushed out. Don't update ic_offset
2473 * until you know exactly how many bytes get copied. Therefore, wait
2474 * until later to update ic_offset.
2475 *
2476 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2477 * can fit into remaining data section.
2478 */
2479 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2480 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2481
Dave Chinner49641f12008-07-11 17:43:55 +10002482 /*
2483 * If I'm the only one writing to this iclog, sync it to disk.
2484 * We need to do an atomic compare and decrement here to avoid
2485 * racing with concurrent atomic_dec_and_lock() calls in
2486 * xlog_state_release_iclog() when there is more than one
2487 * reference to the iclog.
2488 */
2489 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2490 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002491 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002492 error = xlog_state_release_iclog(log, iclog);
2493 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002494 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002496 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 goto restart;
2499 }
2500
2501 /* Do we have enough room to write the full amount in the remainder
2502 * of this iclog? Or must we continue a write on the next iclog and
2503 * mark this iclog as completely taken? In the case where we switch
2504 * iclogs (to mark it taken), this particular iclog will release/sync
2505 * to disk in xlog_write().
2506 */
2507 if (len <= iclog->ic_size - iclog->ic_offset) {
2508 *continued_write = 0;
2509 iclog->ic_offset += len;
2510 } else {
2511 *continued_write = 1;
2512 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2513 }
2514 *iclogp = iclog;
2515
2516 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002517 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 *logoffsetp = log_offset;
2520 return 0;
2521} /* xlog_state_get_iclog_space */
2522
2523/*
2524 * Atomically get the log space required for a log ticket.
2525 *
2526 * Once a ticket gets put onto the reserveq, it will only return after
2527 * the needed reservation is satisfied.
2528 */
2529STATIC int
2530xlog_grant_log_space(xlog_t *log,
2531 xlog_ticket_t *tic)
2532{
2533 int free_bytes;
2534 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535#ifdef DEBUG
2536 xfs_lsn_t tail_lsn;
2537#endif
2538
2539
2540#ifdef DEBUG
2541 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2542 panic("grant Recovery problem");
2543#endif
2544
2545 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002546 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002547
2548 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 /* something is already sleeping; insert new transaction at end */
2551 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002552 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002553
2554 trace_xfs_log_grant_sleep1(log, tic);
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /*
2557 * Gotta check this before going to sleep, while we're
2558 * holding the grant lock.
2559 */
2560 if (XLOG_FORCED_SHUTDOWN(log))
2561 goto error_return;
2562
2563 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002564 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /*
2566 * If we got an error, and the filesystem is shutting down,
2567 * we'll catch it down below. So just continue...
2568 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002569 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002570 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
2572 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2573 need_bytes = tic->t_unit_res*tic->t_ocnt;
2574 else
2575 need_bytes = tic->t_unit_res;
2576
2577redo:
2578 if (XLOG_FORCED_SHUTDOWN(log))
2579 goto error_return;
2580
2581 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2582 log->l_grant_reserve_bytes);
2583 if (free_bytes < need_bytes) {
2584 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002585 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002586
2587 trace_xfs_log_grant_sleep2(log, tic);
2588
Dave Chinner9d7fef72009-04-06 18:42:59 +02002589 spin_unlock(&log->l_grant_lock);
2590 xlog_grant_push_ail(log->l_mp, need_bytes);
2591 spin_lock(&log->l_grant_lock);
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002594 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Dave Chinner9d7fef72009-04-06 18:42:59 +02002596 spin_lock(&log->l_grant_lock);
2597 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002600 trace_xfs_log_grant_wake2(log, tic);
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 goto redo;
2603 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002604 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002607 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608#ifdef DEBUG
2609 tail_lsn = log->l_tail_lsn;
2610 /*
2611 * Check to make sure the grant write head didn't just over lap the
2612 * tail. If the cycles are the same, we can't be overlapping.
2613 * Otherwise, make sure that the cycles differ by exactly one and
2614 * check the byte count.
2615 */
2616 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2617 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2618 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2619 }
2620#endif
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002621 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002623 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return 0;
2625
2626 error_return:
2627 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002628 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002629
2630 trace_xfs_log_grant_error(log, tic);
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 /*
2633 * If we are failing, make sure the ticket doesn't have any
2634 * current reservations. We don't want to add this back when
2635 * the ticket/transaction gets cancelled.
2636 */
2637 tic->t_curr_res = 0;
2638 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002639 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return XFS_ERROR(EIO);
2641} /* xlog_grant_log_space */
2642
2643
2644/*
2645 * Replenish the byte reservation required by moving the grant write head.
2646 *
2647 *
2648 */
2649STATIC int
2650xlog_regrant_write_log_space(xlog_t *log,
2651 xlog_ticket_t *tic)
2652{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 int free_bytes, need_bytes;
2654 xlog_ticket_t *ntic;
2655#ifdef DEBUG
2656 xfs_lsn_t tail_lsn;
2657#endif
2658
2659 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002660 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665#ifdef DEBUG
2666 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2667 panic("regrant Recovery problem");
2668#endif
2669
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002670 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002671
2672 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 if (XLOG_FORCED_SHUTDOWN(log))
2675 goto error_return;
2676
2677 /* If there are other waiters on the queue then give them a
2678 * chance at logspace before us. Wake up the first waiters,
2679 * if we do not wake up all the waiters then go to sleep waiting
2680 * for more free space, otherwise try to get some space for
2681 * this transaction.
2682 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002683 need_bytes = tic->t_unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if ((ntic = log->l_write_headq)) {
2685 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2686 log->l_grant_write_bytes);
2687 do {
2688 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2689
2690 if (free_bytes < ntic->t_unit_res)
2691 break;
2692 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002693 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 ntic = ntic->t_next;
2695 } while (ntic != log->l_write_headq);
2696
2697 if (ntic != log->l_write_headq) {
2698 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002699 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002701 trace_xfs_log_regrant_write_sleep1(log, tic);
2702
Dave Chinner9d7fef72009-04-06 18:42:59 +02002703 spin_unlock(&log->l_grant_lock);
2704 xlog_grant_push_ail(log->l_mp, need_bytes);
2705 spin_lock(&log->l_grant_lock);
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002708 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 &log->l_grant_lock, s);
2710
2711 /* If we're shutting down, this tic is already
2712 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002713 spin_lock(&log->l_grant_lock);
2714 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002717 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719 }
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721redo:
2722 if (XLOG_FORCED_SHUTDOWN(log))
2723 goto error_return;
2724
2725 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2726 log->l_grant_write_bytes);
2727 if (free_bytes < need_bytes) {
2728 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002729 xlog_ins_ticketq(&log->l_write_headq, tic);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002730 spin_unlock(&log->l_grant_lock);
2731 xlog_grant_push_ail(log->l_mp, need_bytes);
2732 spin_lock(&log->l_grant_lock);
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002735 trace_xfs_log_regrant_write_sleep2(log, tic);
2736
David Chinner12017fa2008-08-13 16:34:31 +10002737 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002740 spin_lock(&log->l_grant_lock);
2741 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002744 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 goto redo;
2746 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002747 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002749 /* we've got enough space */
2750 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751#ifdef DEBUG
2752 tail_lsn = log->l_tail_lsn;
2753 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2754 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2755 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2756 }
2757#endif
2758
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002759 trace_xfs_log_regrant_write_exit(log, tic);
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002762 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765
2766 error_return:
2767 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002768 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002769
2770 trace_xfs_log_regrant_write_error(log, tic);
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 /*
2773 * If we are failing, make sure the ticket doesn't have any
2774 * current reservations. We don't want to add this back when
2775 * the ticket/transaction gets cancelled.
2776 */
2777 tic->t_curr_res = 0;
2778 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002779 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return XFS_ERROR(EIO);
2781} /* xlog_regrant_write_log_space */
2782
2783
2784/* The first cnt-1 times through here we don't need to
2785 * move the grant write head because the permanent
2786 * reservation has reserved cnt times the unit amount.
2787 * Release part of current permanent unit reservation and
2788 * reset current reservation to be one units worth. Also
2789 * move grant reservation head forward.
2790 */
2791STATIC void
2792xlog_regrant_reserve_log_space(xlog_t *log,
2793 xlog_ticket_t *ticket)
2794{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002795 trace_xfs_log_regrant_reserve_enter(log, ticket);
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (ticket->t_cnt > 0)
2798 ticket->t_cnt--;
2799
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002800 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002801 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002803 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002804
2805 trace_xfs_log_regrant_reserve_sub(log, ticket);
2806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 xlog_verify_grant_head(log, 1);
2808
2809 /* just return if we still have some of the pre-reserved space */
2810 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002811 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return;
2813 }
2814
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002815 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002816
2817 trace_xfs_log_regrant_reserve_exit(log, ticket);
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002820 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002822 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823} /* xlog_regrant_reserve_log_space */
2824
2825
2826/*
2827 * Give back the space left from a reservation.
2828 *
2829 * All the information we need to make a correct determination of space left
2830 * is present. For non-permanent reservations, things are quite easy. The
2831 * count should have been decremented to zero. We only need to deal with the
2832 * space remaining in the current reservation part of the ticket. If the
2833 * ticket contains a permanent reservation, there may be left over space which
2834 * needs to be released. A count of N means that N-1 refills of the current
2835 * reservation can be done before we need to ask for more space. The first
2836 * one goes to fill up the first current reservation. Once we run out of
2837 * space, the count will stay at zero and the only space remaining will be
2838 * in the current reservation field.
2839 */
2840STATIC void
2841xlog_ungrant_log_space(xlog_t *log,
2842 xlog_ticket_t *ticket)
2843{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (ticket->t_cnt > 0)
2845 ticket->t_cnt--;
2846
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002847 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002848 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002850 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002852 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 /* If this is a permanent reservation ticket, we may be able to free
2855 * up more space based on the remaining count.
2856 */
2857 if (ticket->t_cnt > 0) {
2858 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002859 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
2861
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002862 trace_xfs_log_ungrant_exit(log, ticket);
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002865 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 xfs_log_move_tail(log->l_mp, 1);
2867} /* xlog_ungrant_log_space */
2868
2869
2870/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 * Flush iclog to disk if this is the last reference to the given iclog and
2872 * the WANT_SYNC bit is set.
2873 *
2874 * When this function is entered, the iclog is not necessarily in the
2875 * WANT_SYNC state. It may be sitting around waiting to get filled.
2876 *
2877 *
2878 */
David Chinnera8272ce2007-11-23 16:28:09 +11002879STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002880xlog_state_release_iclog(
2881 xlog_t *log,
2882 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 int sync = 0; /* do we sync? */
2885
David Chinner155cc6b2008-03-06 13:44:14 +11002886 if (iclog->ic_state & XLOG_STATE_IOERROR)
2887 return XFS_ERROR(EIO);
2888
2889 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2890 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2891 return 0;
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002894 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return XFS_ERROR(EIO);
2896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2898 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2899
David Chinner155cc6b2008-03-06 13:44:14 +11002900 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002901 /* update tail before writing to iclog */
2902 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 sync++;
2904 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002905 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2907 /* cycle incremented when incrementing curr_block */
2908 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002909 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 /*
2912 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002913 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2915 * this iclog has consistent data, so we ignore IOERROR
2916 * flags after this point.
2917 */
David Chinnerb5893342008-03-06 13:44:06 +11002918 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921} /* xlog_state_release_iclog */
2922
2923
2924/*
2925 * This routine will mark the current iclog in the ring as WANT_SYNC
2926 * and move the current iclog pointer to the next iclog in the ring.
2927 * When this routine is called from xlog_state_get_iclog_space(), the
2928 * exact size of the iclog has not yet been determined. All we know is
2929 * that every data block. We have run out of space in this log record.
2930 */
2931STATIC void
2932xlog_state_switch_iclogs(xlog_t *log,
2933 xlog_in_core_t *iclog,
2934 int eventual_size)
2935{
2936 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2937 if (!eventual_size)
2938 eventual_size = iclog->ic_offset;
2939 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002940 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 log->l_prev_block = log->l_curr_block;
2942 log->l_prev_cycle = log->l_curr_cycle;
2943
2944 /* roll log?: ic_offset changed later */
2945 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2946
2947 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002948 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 log->l_mp->m_sb.sb_logsunit > 1) {
2950 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2951 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2952 }
2953
2954 if (log->l_curr_block >= log->l_logBBsize) {
2955 log->l_curr_cycle++;
2956 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2957 log->l_curr_cycle++;
2958 log->l_curr_block -= log->l_logBBsize;
2959 ASSERT(log->l_curr_block >= 0);
2960 }
2961 ASSERT(iclog == log->l_iclog);
2962 log->l_iclog = iclog->ic_next;
2963} /* xlog_state_switch_iclogs */
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965/*
2966 * Write out all data in the in-core log as of this exact moment in time.
2967 *
2968 * Data may be written to the in-core log during this call. However,
2969 * we don't guarantee this data will be written out. A change from past
2970 * implementation means this routine will *not* write out zero length LRs.
2971 *
2972 * Basically, we try and perform an intelligent scan of the in-core logs.
2973 * If we determine there is no flushable data, we just return. There is no
2974 * flushable data if:
2975 *
2976 * 1. the current iclog is active and has no data; the previous iclog
2977 * is in the active or dirty state.
2978 * 2. the current iclog is drity, and the previous iclog is in the
2979 * active or dirty state.
2980 *
David Chinner12017fa2008-08-13 16:34:31 +10002981 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 *
2983 * 1. the current iclog is not in the active nor dirty state.
2984 * 2. the current iclog dirty, and the previous iclog is not in the
2985 * active nor dirty state.
2986 * 3. the current iclog is active, and there is another thread writing
2987 * to this particular iclog.
2988 * 4. a) the current iclog is active and has no other writers
2989 * b) when we return from flushing out this iclog, it is still
2990 * not in the active nor dirty state.
2991 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002992int
2993_xfs_log_force(
2994 struct xfs_mount *mp,
2995 uint flags,
2996 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002998 struct log *log = mp->m_log;
2999 struct xlog_in_core *iclog;
3000 xfs_lsn_t lsn;
3001
3002 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003004 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006 iclog = log->l_iclog;
3007 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003008 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return XFS_ERROR(EIO);
3010 }
3011
3012 /* If the head iclog is not active nor dirty, we just attach
3013 * ourselves to the head and go to sleep.
3014 */
3015 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
3016 iclog->ic_state == XLOG_STATE_DIRTY) {
3017 /*
3018 * If the head is dirty or (active and empty), then
3019 * we need to look at the previous iclog. If the previous
3020 * iclog is active or dirty we are done. There is nothing
3021 * to sync out. Otherwise, we attach ourselves to the
3022 * previous iclog and go to sleep.
3023 */
3024 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11003025 (atomic_read(&iclog->ic_refcnt) == 0
3026 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 iclog = iclog->ic_prev;
3028 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
3029 iclog->ic_state == XLOG_STATE_DIRTY)
3030 goto no_sleep;
3031 else
3032 goto maybe_sleep;
3033 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11003034 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* We are the only one with access to this
3036 * iclog. Flush it out now. There should
3037 * be a roundoff of zero to show that someone
3038 * has already taken care of the roundoff from
3039 * the previous sync.
3040 */
David Chinner155cc6b2008-03-06 13:44:14 +11003041 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003042 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003044 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 if (xlog_state_release_iclog(log, iclog))
3047 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003048
3049 if (log_flushed)
3050 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003051 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003052 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 iclog->ic_state != XLOG_STATE_DIRTY)
3054 goto maybe_sleep;
3055 else
3056 goto no_sleep;
3057 } else {
3058 /* Someone else is writing to this iclog.
3059 * Use its call to flush out the data. However,
3060 * the other thread may not force out this LR,
3061 * so we mark it WANT_SYNC.
3062 */
3063 xlog_state_switch_iclogs(log, iclog, 0);
3064 goto maybe_sleep;
3065 }
3066 }
3067 }
3068
3069 /* By the time we come around again, the iclog could've been filled
3070 * which would give it another lsn. If we have a new lsn, just
3071 * return because the relevant data has been flushed.
3072 */
3073maybe_sleep:
3074 if (flags & XFS_LOG_SYNC) {
3075 /*
3076 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003077 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 * Then we check again after waking up, in case our
3079 * sleep was disturbed by a bad news.
3080 */
3081 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003082 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return XFS_ERROR(EIO);
3084 }
3085 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003086 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 /*
3088 * No need to grab the log lock here since we're
3089 * only deciding whether or not to return EIO
3090 * and the memory read should be atomic.
3091 */
3092 if (iclog->ic_state & XLOG_STATE_IOERROR)
3093 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003094 if (log_flushed)
3095 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 } else {
3097
3098no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003099 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003105 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
3106 * about errors or whether the log was flushed or not. This is the normal
3107 * interface to use when trying to unpin items or move the log forward.
3108 */
3109void
3110xfs_log_force(
3111 xfs_mount_t *mp,
3112 uint flags)
3113{
3114 int error;
3115
3116 error = _xfs_log_force(mp, flags, NULL);
3117 if (error) {
3118 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3119 "error %d returned.", error);
3120 }
3121}
3122
3123/*
3124 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 *
3126 * Find in-core log with lsn.
3127 * If it is in the DIRTY state, just return.
3128 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3129 * state and go to sleep or return.
3130 * If it is in any other state, go to sleep or return.
3131 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003132 * Synchronous forces are implemented with a signal variable. All callers
3133 * to force a given lsn to disk will wait on a the sv attached to the
3134 * specific in-core log. When given in-core log finally completes its
3135 * write to disk, that thread will wake up all threads waiting on the
3136 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003138int
3139_xfs_log_force_lsn(
3140 struct xfs_mount *mp,
3141 xfs_lsn_t lsn,
3142 uint flags,
3143 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003145 struct log *log = mp->m_log;
3146 struct xlog_in_core *iclog;
3147 int already_slept = 0;
3148
3149 ASSERT(lsn != 0);
3150
3151 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003154 spin_lock(&log->l_icloglock);
3155 iclog = log->l_iclog;
3156 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003157 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003158 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003161 do {
3162 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3163 iclog = iclog->ic_next;
3164 continue;
3165 }
3166
3167 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3168 spin_unlock(&log->l_icloglock);
3169 return 0;
3170 }
3171
3172 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3173 /*
3174 * We sleep here if we haven't already slept (e.g.
3175 * this is the first time we've looked at the correct
3176 * iclog buf) and the buffer before us is going to
3177 * be sync'ed. The reason for this is that if we
3178 * are doing sync transactions here, by waiting for
3179 * the previous I/O to complete, we can allow a few
3180 * more transactions into this iclog before we close
3181 * it down.
3182 *
3183 * Otherwise, we mark the buffer WANT_SYNC, and bump
3184 * up the refcnt so we can release the log (which
3185 * drops the ref count). The state switch keeps new
3186 * transaction commits from using this buffer. When
3187 * the current commits finish writing into the buffer,
3188 * the refcount will drop to zero and the buffer will
3189 * go out then.
3190 */
3191 if (!already_slept &&
3192 (iclog->ic_prev->ic_state &
3193 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3194 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3195
3196 XFS_STATS_INC(xs_log_force_sleep);
3197
3198 sv_wait(&iclog->ic_prev->ic_write_wait,
3199 PSWP, &log->l_icloglock, s);
3200 if (log_flushed)
3201 *log_flushed = 1;
3202 already_slept = 1;
3203 goto try_again;
3204 }
David Chinner155cc6b2008-03-06 13:44:14 +11003205 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003207 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (xlog_state_release_iclog(log, iclog))
3209 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003210 if (log_flushed)
3211 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003212 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003215 if ((flags & XFS_LOG_SYNC) && /* sleep */
3216 !(iclog->ic_state &
3217 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3218 /*
3219 * Don't wait on completion if we know that we've
3220 * gotten a log write error.
3221 */
3222 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3223 spin_unlock(&log->l_icloglock);
3224 return XFS_ERROR(EIO);
3225 }
3226 XFS_STATS_INC(xs_log_force_sleep);
3227 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3228 /*
3229 * No need to grab the log lock here since we're
3230 * only deciding whether or not to return EIO
3231 * and the memory read should be atomic.
3232 */
3233 if (iclog->ic_state & XLOG_STATE_IOERROR)
3234 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003236 if (log_flushed)
3237 *log_flushed = 1;
3238 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003239 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003241
3242 return 0;
3243 } while (iclog != log->l_iclog);
3244
3245 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003249/*
3250 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3251 * about errors or whether the log was flushed or not. This is the normal
3252 * interface to use when trying to unpin items or move the log forward.
3253 */
3254void
3255xfs_log_force_lsn(
3256 xfs_mount_t *mp,
3257 xfs_lsn_t lsn,
3258 uint flags)
3259{
3260 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003262 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3263 if (error) {
3264 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3265 "error %d returned.", error);
3266 }
3267}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269/*
3270 * Called when we want to mark the current iclog as being ready to sync to
3271 * disk.
3272 */
David Chinnera8272ce2007-11-23 16:28:09 +11003273STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3275{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003276 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3279 xlog_state_switch_iclogs(log, iclog, 0);
3280 } else {
3281 ASSERT(iclog->ic_state &
3282 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3283 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286
3287/*****************************************************************************
3288 *
3289 * TICKET functions
3290 *
3291 *****************************************************************************
3292 */
3293
3294/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003295 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003297void
3298xfs_log_ticket_put(
3299 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003301 ASSERT(atomic_read(&ticket->t_ref) > 0);
3302 if (atomic_dec_and_test(&ticket->t_ref)) {
3303 sv_destroy(&ticket->t_wait);
3304 kmem_zone_free(xfs_log_ticket_zone, ticket);
3305 }
3306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003308xlog_ticket_t *
3309xfs_log_ticket_get(
3310 xlog_ticket_t *ticket)
3311{
3312 ASSERT(atomic_read(&ticket->t_ref) > 0);
3313 atomic_inc(&ticket->t_ref);
3314 return ticket;
3315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003318 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 */
David Chinnera8272ce2007-11-23 16:28:09 +11003320STATIC xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003321xlog_ticket_alloc(
3322 struct log *log,
3323 int unit_bytes,
3324 int cnt,
3325 char client,
3326 uint xflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003328 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003330 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
David Chinnereb01c9c2008-04-10 12:18:46 +10003332 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3333 if (!tic)
3334 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 /*
3337 * Permanent reservations have up to 'cnt'-1 active log operations
3338 * in the log. A unit in this case is the amount of space for one
3339 * of these log operations. Normal reservations have a cnt of 1
3340 * and their unit amount is the total amount of space required.
3341 *
3342 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003343 * which occupy space in the on-disk log.
3344 *
3345 * Normal form of a transaction is:
3346 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3347 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3348 *
3349 * We need to account for all the leadup data and trailer data
3350 * around the transaction data.
3351 * And then we need to account for the worst case in terms of using
3352 * more space.
3353 * The worst case will happen if:
3354 * - the placement of the transaction happens to be such that the
3355 * roundoff is at its maximum
3356 * - the transaction data is synced before the commit record is synced
3357 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3358 * Therefore the commit record is in its own Log Record.
3359 * This can happen as the commit record is called with its
3360 * own region to xlog_write().
3361 * This then means that in the worst case, roundoff can happen for
3362 * the commit-rec as well.
3363 * The commit-rec is smaller than padding in this scenario and so it is
3364 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 */
3366
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003367 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003369 unit_bytes += sizeof(xfs_trans_header_t);
3370
3371 /* for start-rec */
3372 unit_bytes += sizeof(xlog_op_header_t);
3373
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003374 /*
3375 * for LR headers - the space for data in an iclog is the size minus
3376 * the space used for the headers. If we use the iclog size, then we
3377 * undercalculate the number of headers required.
3378 *
3379 * Furthermore - the addition of op headers for split-recs might
3380 * increase the space required enough to require more log and op
3381 * headers, so take that into account too.
3382 *
3383 * IMPORTANT: This reservation makes the assumption that if this
3384 * transaction is the first in an iclog and hence has the LR headers
3385 * accounted to it, then the remaining space in the iclog is
3386 * exclusively for this transaction. i.e. if the transaction is larger
3387 * than the iclog, it will be the only thing in that iclog.
3388 * Fundamentally, this means we must pass the entire log vector to
3389 * xlog_write to guarantee this.
3390 */
3391 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3392 num_headers = howmany(unit_bytes, iclog_space);
3393
3394 /* for split-recs - ophdrs added when data split over LRs */
3395 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3396
3397 /* add extra header reservations if we overrun */
3398 while (!num_headers ||
3399 howmany(unit_bytes, iclog_space) > num_headers) {
3400 unit_bytes += sizeof(xlog_op_header_t);
3401 num_headers++;
3402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 unit_bytes += log->l_iclog_hsize * num_headers;
3404
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003405 /* for commit-rec LR header - note: padding will subsume the ophdr */
3406 unit_bytes += log->l_iclog_hsize;
3407
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003408 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003409 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003410 log->l_mp->m_sb.sb_logsunit > 1) {
3411 /* log su roundoff */
3412 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3413 } else {
3414 /* BB roundoff */
3415 unit_bytes += 2*BBSIZE;
3416 }
3417
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003418 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 tic->t_unit_res = unit_bytes;
3420 tic->t_curr_res = unit_bytes;
3421 tic->t_cnt = cnt;
3422 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003423 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 tic->t_clientid = client;
3425 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003426 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (xflags & XFS_LOG_PERM_RESERV)
3428 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003429 sv_init(&tic->t_wait, SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Christoph Hellwig0adba532007-08-30 17:21:46 +10003431 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436
3437/******************************************************************************
3438 *
3439 * Log debug routines
3440 *
3441 ******************************************************************************
3442 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003443#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444/*
3445 * Make sure that the destination ptr is within the valid data region of
3446 * one of the iclogs. This uses backup pointers stored in a different
3447 * part of the log in case we trash the log structure.
3448 */
3449void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003450xlog_verify_dest_ptr(
3451 struct log *log,
3452 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453{
3454 int i;
3455 int good_ptr = 0;
3456
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003457 for (i = 0; i < log->l_iclog_bufs; i++) {
3458 if (ptr >= log->l_iclog_bak[i] &&
3459 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 good_ptr++;
3461 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003462
3463 if (!good_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467STATIC void
3468xlog_verify_grant_head(xlog_t *log, int equals)
3469{
3470 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3471 if (equals)
3472 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3473 else
3474 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3475 } else {
3476 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3477 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3478 }
3479} /* xlog_verify_grant_head */
3480
3481/* check if it will fit */
3482STATIC void
3483xlog_verify_tail_lsn(xlog_t *log,
3484 xlog_in_core_t *iclog,
3485 xfs_lsn_t tail_lsn)
3486{
3487 int blocks;
3488
3489 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3490 blocks =
3491 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3492 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3493 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3494 } else {
3495 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3496
3497 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3498 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3499
3500 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3501 if (blocks < BTOBB(iclog->ic_offset) + 1)
3502 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3503 }
3504} /* xlog_verify_tail_lsn */
3505
3506/*
3507 * Perform a number of checks on the iclog before writing to disk.
3508 *
3509 * 1. Make sure the iclogs are still circular
3510 * 2. Make sure we have a good magic number
3511 * 3. Make sure we don't have magic numbers in the data
3512 * 4. Check fields of each log operation header for:
3513 * A. Valid client identifier
3514 * B. tid ptr value falls in valid ptr space (user space code)
3515 * C. Length in log record header is correct according to the
3516 * individual operation headers within record.
3517 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3518 * log, check the preceding blocks of the physical log to make sure all
3519 * the cycle numbers agree with the current cycle number.
3520 */
3521STATIC void
3522xlog_verify_iclog(xlog_t *log,
3523 xlog_in_core_t *iclog,
3524 int count,
3525 boolean_t syncing)
3526{
3527 xlog_op_header_t *ophead;
3528 xlog_in_core_t *icptr;
3529 xlog_in_core_2_t *xhdr;
3530 xfs_caddr_t ptr;
3531 xfs_caddr_t base_ptr;
3532 __psint_t field_offset;
3533 __uint8_t clientid;
3534 int len, i, j, k, op_len;
3535 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003538 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 icptr = log->l_iclog;
3540 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003541 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 xlog_panic("xlog_verify_iclog: invalid ptr");
3543 icptr = icptr->ic_next;
3544 }
3545 if (icptr != log->l_iclog)
3546 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003547 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003550 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 xlog_panic("xlog_verify_iclog: invalid magic num");
3552
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003553 ptr = (xfs_caddr_t) &iclog->ic_header;
3554 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003556 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 xlog_panic("xlog_verify_iclog: unexpected magic num");
3558 }
3559
3560 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003561 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 ptr = iclog->ic_datap;
3563 base_ptr = ptr;
3564 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003565 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 for (i = 0; i < len; i++) {
3567 ophead = (xlog_op_header_t *)ptr;
3568
3569 /* clientid is only 1 byte */
3570 field_offset = (__psint_t)
3571 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3572 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3573 clientid = ophead->oh_clientid;
3574 } else {
3575 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3576 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3577 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3578 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003579 clientid = xlog_get_client_id(
3580 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003582 clientid = xlog_get_client_id(
3583 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
3585 }
3586 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003587 cmn_err(CE_WARN, "xlog_verify_iclog: "
3588 "invalid clientid %d op 0x%p offset 0x%lx",
3589 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 /* check length */
3592 field_offset = (__psint_t)
3593 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3594 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003595 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 } else {
3597 idx = BTOBBT((__psint_t)&ophead->oh_len -
3598 (__psint_t)iclog->ic_datap);
3599 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3600 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3601 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003602 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003604 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
3606 }
3607 ptr += sizeof(xlog_op_header_t) + op_len;
3608 }
3609} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003610#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
3612/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003613 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 */
3615STATIC int
3616xlog_state_ioerror(
3617 xlog_t *log)
3618{
3619 xlog_in_core_t *iclog, *ic;
3620
3621 iclog = log->l_iclog;
3622 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3623 /*
3624 * Mark all the incore logs IOERROR.
3625 * From now on, no log flushes will result.
3626 */
3627 ic = iclog;
3628 do {
3629 ic->ic_state = XLOG_STATE_IOERROR;
3630 ic = ic->ic_next;
3631 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 }
3634 /*
3635 * Return non-zero, if state transition has already happened.
3636 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003637 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
3640/*
3641 * This is called from xfs_force_shutdown, when we're forcibly
3642 * shutting down the filesystem, typically because of an IO error.
3643 * Our main objectives here are to make sure that:
3644 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3645 * parties to find out, 'atomically'.
3646 * b. those who're sleeping on log reservations, pinned objects and
3647 * other resources get woken up, and be told the bad news.
3648 * c. nothing new gets queued up after (a) and (b) are done.
3649 * d. if !logerror, flush the iclogs to disk, then seal them off
3650 * for business.
3651 */
3652int
3653xfs_log_force_umount(
3654 struct xfs_mount *mp,
3655 int logerror)
3656{
3657 xlog_ticket_t *tic;
3658 xlog_t *log;
3659 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
3661 log = mp->m_log;
3662
3663 /*
3664 * If this happens during log recovery, don't worry about
3665 * locking; the log isn't open for business yet.
3666 */
3667 if (!log ||
3668 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3669 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003670 if (mp->m_sb_bp)
3671 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
3674
3675 /*
3676 * Somebody could've already done the hard work for us.
3677 * No need to get locks for this.
3678 */
3679 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3680 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003681 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
3683 retval = 0;
3684 /*
3685 * We must hold both the GRANT lock and the LOG lock,
3686 * before we mark the filesystem SHUTDOWN and wake
3687 * everybody up to tell the bad news.
3688 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003689 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003690 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003692 if (mp->m_sb_bp)
3693 XFS_BUF_DONE(mp->m_sb_bp);
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 /*
3696 * This flag is sort of redundant because of the mount flag, but
3697 * it's good to maintain the separation between the log and the rest
3698 * of XFS.
3699 */
3700 log->l_flags |= XLOG_IO_ERROR;
3701
3702 /*
3703 * If we hit a log error, we want to mark all the iclogs IOERROR
3704 * while we're still holding the loglock.
3705 */
3706 if (logerror)
3707 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003708 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 /*
3711 * We don't want anybody waiting for log reservations
3712 * after this. That means we have to wake up everybody
3713 * queued up on reserve_headq as well as write_headq.
3714 * In addition, we make sure in xlog_{re}grant_log_space
3715 * that we don't enqueue anything once the SHUTDOWN flag
3716 * is set, and this action is protected by the GRANTLOCK.
3717 */
3718 if ((tic = log->l_reserve_headq)) {
3719 do {
David Chinner12017fa2008-08-13 16:34:31 +10003720 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 tic = tic->t_next;
3722 } while (tic != log->l_reserve_headq);
3723 }
3724
3725 if ((tic = log->l_write_headq)) {
3726 do {
David Chinner12017fa2008-08-13 16:34:31 +10003727 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 tic = tic->t_next;
3729 } while (tic != log->l_write_headq);
3730 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003731 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003733 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 ASSERT(!logerror);
3735 /*
3736 * Force the incore logs to disk before shutting the
3737 * log down completely.
3738 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003739 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3740
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003741 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003743 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
3745 /*
3746 * Wake up everybody waiting on xfs_log_force.
3747 * Callback all log item committed functions as if the
3748 * log writes were completed.
3749 */
3750 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3751
3752#ifdef XFSERRORDEBUG
3753 {
3754 xlog_in_core_t *iclog;
3755
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003756 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 iclog = log->l_iclog;
3758 do {
3759 ASSERT(iclog->ic_callback == 0);
3760 iclog = iclog->ic_next;
3761 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003762 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 }
3764#endif
3765 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003766 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003769STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770xlog_iclogs_empty(xlog_t *log)
3771{
3772 xlog_in_core_t *iclog;
3773
3774 iclog = log->l_iclog;
3775 do {
3776 /* endianness does not matter here, zero is zero in
3777 * any language.
3778 */
3779 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 iclog = iclog->ic_next;
3782 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003783 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}