blob: 99c62855432e6fb8f6523681cc49c408ea8bf43a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
28#include "xfs_error.h"
29#include "xfs_log_priv.h"
30#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Chinnereb01c9c2008-04-10 12:18:46 +100041kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Local miscellaneous function prototypes */
Dave Chinner55b66332010-03-23 11:43:17 +110044STATIC int xlog_commit_record(struct log *log, struct xlog_ticket *ticket,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 xlog_in_core_t **, xfs_lsn_t *);
46STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
47 xfs_buftarg_t *log_target,
48 xfs_daddr_t blk_offset,
49 int num_bblks);
50STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
51STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100052STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* local state machine functions */
55STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
56STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
57STATIC int xlog_state_get_iclog_space(xlog_t *log,
58 int len,
59 xlog_in_core_t **iclog,
60 xlog_ticket_t *ticket,
61 int *continued_write,
62 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xlog_state_release_iclog(xlog_t *log,
64 xlog_in_core_t *iclog);
65STATIC void xlog_state_switch_iclogs(xlog_t *log,
66 xlog_in_core_t *iclog,
67 int eventual_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
69
70/* local functions to manipulate grant head */
71STATIC int xlog_grant_log_space(xlog_t *log,
72 xlog_ticket_t *xtic);
73STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
74 int need_bytes);
75STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
76 xlog_ticket_t *ticket);
77STATIC int xlog_regrant_write_log_space(xlog_t *log,
78 xlog_ticket_t *ticket);
79STATIC void xlog_ungrant_log_space(xlog_t *log,
80 xlog_ticket_t *ticket);
81
Nathan Scottcfcbbbd2005-11-02 15:12:04 +110082#if defined(DEBUG)
Christoph Hellwige6b1f272010-03-23 11:47:38 +110083STATIC void xlog_verify_dest_ptr(xlog_t *log, char *ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
Dave Chinner3f336c62010-12-21 12:02:52 +110085STATIC void xlog_verify_grant_tail(struct log *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
87 int count, boolean_t syncing);
88STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
89 xfs_lsn_t tail_lsn);
90#else
91#define xlog_verify_dest_ptr(a,b)
92#define xlog_verify_grant_head(a,b)
Dave Chinner3f336c62010-12-21 12:02:52 +110093#define xlog_verify_grant_tail(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define xlog_verify_iclog(a,b,c,d)
95#define xlog_verify_tail_lsn(a,b,c)
96#endif
97
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100098STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100100static void
101xlog_grant_sub_space(struct log *log, int bytes)
102{
103 log->l_grant_write_bytes -= bytes;
104 if (log->l_grant_write_bytes < 0) {
105 log->l_grant_write_bytes += log->l_logsize;
106 log->l_grant_write_cycle--;
107 }
108
109 log->l_grant_reserve_bytes -= bytes;
110 if ((log)->l_grant_reserve_bytes < 0) {
111 log->l_grant_reserve_bytes += log->l_logsize;
112 log->l_grant_reserve_cycle--;
113 }
114
115}
116
117static void
118xlog_grant_add_space_write(struct log *log, int bytes)
119{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000120 int tmp = log->l_logsize - log->l_grant_write_bytes;
121 if (tmp > bytes)
122 log->l_grant_write_bytes += bytes;
123 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100124 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000125 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100126 }
127}
128
129static void
130xlog_grant_add_space_reserve(struct log *log, int bytes)
131{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000132 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
133 if (tmp > bytes)
134 log->l_grant_reserve_bytes += bytes;
135 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100136 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000137 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100138 }
139}
140
141static inline void
142xlog_grant_add_space(struct log *log, int bytes)
143{
144 xlog_grant_add_space_write(log, bytes);
145 xlog_grant_add_space_reserve(log, bytes);
146}
147
Christoph Hellwig0adba532007-08-30 17:21:46 +1000148static void
149xlog_tic_reset_res(xlog_ticket_t *tic)
150{
151 tic->t_res_num = 0;
152 tic->t_res_arr_sum = 0;
153 tic->t_res_num_ophdrs = 0;
154}
155
156static void
157xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
158{
159 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
160 /* add to overflow and start again */
161 tic->t_res_o_flow += tic->t_res_arr_sum;
162 tic->t_res_num = 0;
163 tic->t_res_arr_sum = 0;
164 }
165
166 tic->t_res_arr[tic->t_res_num].r_len = len;
167 tic->t_res_arr[tic->t_res_num].r_type = type;
168 tic->t_res_arr_sum += len;
169 tic->t_res_num++;
170}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * NOTES:
174 *
175 * 1. currblock field gets updated at startup and after in-core logs
176 * marked as with WANT_SYNC.
177 */
178
179/*
180 * This routine is called when a user of a log manager ticket is done with
181 * the reservation. If the ticket was ever used, then a commit record for
182 * the associated transaction is written out as a log operation header with
183 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
184 * a given ticket. If the ticket was one with a permanent reservation, then
185 * a few operations are done differently. Permanent reservation tickets by
186 * default don't release the reservation. They just commit the current
187 * transaction with the belief that the reservation is still needed. A flag
188 * must be passed in before permanent reservations are actually released.
189 * When these type of tickets are not released, they need to be set into
190 * the inited state again. By doing this, a start record will be written
191 * out when the next write occurs.
192 */
193xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000194xfs_log_done(
195 struct xfs_mount *mp,
196 struct xlog_ticket *ticket,
197 struct xlog_in_core **iclog,
198 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000200 struct log *log = mp->m_log;
201 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (XLOG_FORCED_SHUTDOWN(log) ||
204 /*
205 * If nothing was ever written, don't write out commit record.
206 * If we get an error, just continue and give back the log ticket.
207 */
208 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100209 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 lsn = (xfs_lsn_t) -1;
211 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
212 flags |= XFS_LOG_REL_PERM_RESERV;
213 }
214 }
215
216
217 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
218 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000219 trace_xfs_log_done_nonperm(log, ticket);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000222 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * request has been made to release a permanent reservation.
224 */
225 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100226 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000228 trace_xfs_log_done_perm(log, ticket);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000231 /* If this ticket was a permanent reservation and we aren't
232 * trying to release it, reset the inited flags; so next time
233 * we write, a start record will be written out.
234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*
242 * Attaches a new iclog I/O completion callback routine during
243 * transaction commit. If the log is in error state, a non-zero
244 * return code is handed back and the caller is responsible for
245 * executing the callback at an appropriate time.
246 */
247int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000248xfs_log_notify(
249 struct xfs_mount *mp,
250 struct xlog_in_core *iclog,
251 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000253 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David Chinner114d23a2008-04-10 12:18:39 +1000255 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
257 if (!abortflg) {
258 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
259 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
260 cb->cb_next = NULL;
261 *(iclog->ic_callback_tail) = cb;
262 iclog->ic_callback_tail = &(cb->cb_next);
263 }
David Chinner114d23a2008-04-10 12:18:39 +1000264 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000266}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000269xfs_log_release_iclog(
270 struct xfs_mount *mp,
271 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000273 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000274 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100275 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
278 return 0;
279}
280
281/*
282 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
283 * to the reservation.
284 * 2. Potentially, push buffers at tail of log to disk.
285 *
286 * Each reservation is going to reserve extra space for a log record header.
287 * When writes happen to the on-disk log, we don't subtract the length of the
288 * log record header from any reservation. By wasting space in each
289 * reservation, we prevent over allocation problems.
290 */
291int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000292xfs_log_reserve(
293 struct xfs_mount *mp,
294 int unit_bytes,
295 int cnt,
296 struct xlog_ticket **ticket,
297 __uint8_t client,
298 uint flags,
299 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000301 struct log *log = mp->m_log;
302 struct xlog_ticket *internal_ticket;
303 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (XLOG_FORCED_SHUTDOWN(log))
308 return XFS_ERROR(EIO);
309
310 XFS_STATS_INC(xs_try_logspace);
311
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (*ticket != NULL) {
314 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000315 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000316
Dave Chinner524ee362010-05-07 11:05:05 +1000317 /*
318 * this is a new transaction on the ticket, so we need to
319 * change the transaction ID so that the next transaction has a
320 * different TID in the log. Just add one to the existing tid
321 * so that we can see chains of rolling transactions in the log
322 * easily.
323 */
324 internal_ticket->t_tid++;
325
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000326 trace_xfs_log_reserve(log, internal_ticket);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
329 retval = xlog_regrant_write_log_space(log, internal_ticket);
330 } else {
331 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100332 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Dave Chinner3383ca52010-05-07 11:04:17 +1000333 client, flags,
334 KM_SLEEP|KM_MAYFAIL);
David Chinnereb01c9c2008-04-10 12:18:46 +1000335 if (!internal_ticket)
336 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000337 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000339
340 trace_xfs_log_reserve(log, internal_ticket);
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 xlog_grant_push_ail(mp,
343 (internal_ticket->t_unit_res *
344 internal_ticket->t_cnt));
345 retval = xlog_grant_log_space(log, internal_ticket);
346 }
347
348 return retval;
349} /* xfs_log_reserve */
350
351
352/*
353 * Mount a log filesystem
354 *
355 * mp - ubiquitous xfs mount point structure
356 * log_target - buftarg of on-disk log device
357 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
358 * num_bblocks - Number of BBSIZE blocks in on-disk log
359 *
360 * Return error or zero.
361 */
362int
David Chinner249a8c12008-02-05 12:13:32 +1100363xfs_log_mount(
364 xfs_mount_t *mp,
365 xfs_buftarg_t *log_target,
366 xfs_daddr_t blk_offset,
367 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
David Chinner249a8c12008-02-05 12:13:32 +1100369 int error;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
372 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
373 else {
374 cmn_err(CE_NOTE,
375 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
376 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000377 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
380 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200381 if (IS_ERR(mp->m_log)) {
382 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100383 goto out;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
David Chinner249a8c12008-02-05 12:13:32 +1100387 * Initialize the AIL now we have a log.
388 */
David Chinner249a8c12008-02-05 12:13:32 +1100389 error = xfs_trans_ail_init(mp);
390 if (error) {
391 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100392 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100393 }
David Chinnera9c21c12008-10-30 17:39:35 +1100394 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100395
396 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * skip log recovery on a norecovery mount. pretend it all
398 * just worked.
399 */
400 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100401 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000404 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Eric Sandeen65be6052006-01-11 15:34:19 +1100406 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000409 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (error) {
411 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100412 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415
416 /* Normal transactions can now occur */
417 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
418
Dave Chinner71e330b2010-05-21 14:37:18 +1000419 /*
420 * Now the log has been fully initialised and we know were our
421 * space grant counters are, we can initialise the permanent ticket
422 * needed for delayed logging to work.
423 */
424 xlog_cil_init_post_recovery(mp->m_log);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100427
428out_destroy_ail:
429 xfs_trans_ail_destroy(mp);
430out_free_log:
431 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100432out:
David Chinner249a8c12008-02-05 12:13:32 +1100433 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436/*
437 * Finish the recovery of the file system. This is separate from
438 * the xfs_log_mount() call, because it depends on the code in
439 * xfs_mountfs() to read in the root and real-time bitmap inodes
440 * between calling xfs_log_mount() and here.
441 *
442 * mp - ubiquitous xfs mount point structure
443 */
444int
Christoph Hellwig42490232008-08-13 16:49:32 +1000445xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 int error;
448
449 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000450 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 else {
452 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000453 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 return error;
457}
458
459/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * Final log writes as part of unmount.
461 *
462 * Mark the filesystem clean as unmount happens. Note that during relocation
463 * this routine needs to be executed as part of source-bag while the
464 * deallocation must not be done until source-end.
465 */
466
467/*
468 * Unmount record used to have a string "Unmount filesystem--" in the
469 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
470 * We just write the magic number now since that particular field isn't
471 * currently architecture converted and "nUmount" is a bit foo.
472 * As far as I know, there weren't any dependencies on the old behaviour.
473 */
474
475int
476xfs_log_unmount_write(xfs_mount_t *mp)
477{
478 xlog_t *log = mp->m_log;
479 xlog_in_core_t *iclog;
480#ifdef DEBUG
481 xlog_in_core_t *first_iclog;
482#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000483 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 xfs_lsn_t lsn;
485 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /*
488 * Don't write out unmount record on read-only mounts.
489 * Or, if we are doing a forced umount (typically because of IO errors).
490 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000491 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000494 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000495 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497#ifdef DEBUG
498 first_iclog = iclog = log->l_iclog;
499 do {
500 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
501 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
502 ASSERT(iclog->ic_offset == 0);
503 }
504 iclog = iclog->ic_next;
505 } while (iclog != first_iclog);
506#endif
507 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000508 error = xfs_log_reserve(mp, 600, 1, &tic,
509 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100511 /* the data section must be 32 bit size aligned */
512 struct {
513 __uint16_t magic;
514 __uint16_t pad1;
515 __uint32_t pad2; /* may as well make it 64 bits */
516 } magic = {
517 .magic = XLOG_UNMOUNT_TYPE,
518 };
519 struct xfs_log_iovec reg = {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000520 .i_addr = &magic,
Dave Chinner55b66332010-03-23 11:43:17 +1100521 .i_len = sizeof(magic),
522 .i_type = XLOG_REG_TYPE_UNMOUNT,
523 };
524 struct xfs_log_vec vec = {
525 .lv_niovecs = 1,
526 .lv_iovecp = &reg,
527 };
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100530 tic->t_flags = 0;
531 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 NULL, XLOG_UNMOUNT_TRANS);
533 /*
534 * At this point, we're umounting anyway,
535 * so there's no point in transitioning log state
536 * to IOERROR. Just continue...
537 */
538 }
539
540 if (error) {
541 xfs_fs_cmn_err(CE_ALERT, mp,
542 "xfs_log_unmount: unmount record failed");
543 }
544
545
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000546 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100548 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100550 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000551 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000553 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
555 iclog->ic_state == XLOG_STATE_DIRTY)) {
556 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000557 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 &log->l_icloglock, s);
559 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000560 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000563 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000565 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000566 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000567 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100568 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 } else {
571 /*
572 * We're already in forced_shutdown mode, couldn't
573 * even attempt to write out the unmount transaction.
574 *
575 * Go through the motions of sync'ing and releasing
576 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000577 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * be in progress. Do this as a separate section of
579 * code so we'll know if we ever get stuck here that
580 * we're in this odd situation of trying to unmount
581 * a file system that went into forced_shutdown as
582 * the result of an unmount..
583 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000584 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100586 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100589 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000590 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000592 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
595 || iclog->ic_state == XLOG_STATE_DIRTY
596 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
597
David Chinner12017fa2008-08-13 16:34:31 +1000598 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 &log->l_icloglock, s);
600 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000601 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604
David Chinner1bb7d6b2008-04-10 12:24:38 +1000605 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606} /* xfs_log_unmount_write */
607
608/*
609 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100610 *
611 * We need to stop the aild from running before we destroy
612 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
614void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100615xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
David Chinner249a8c12008-02-05 12:13:32 +1100617 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000618 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Dave Chinner43f5efc2010-03-23 10:10:00 +1100621void
622xfs_log_item_init(
623 struct xfs_mount *mp,
624 struct xfs_log_item *item,
625 int type,
626 struct xfs_item_ops *ops)
627{
628 item->li_mountp = mp;
629 item->li_ailp = mp->m_ail;
630 item->li_type = type;
631 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +1000632 item->li_lv = NULL;
633
634 INIT_LIST_HEAD(&item->li_ail);
635 INIT_LIST_HEAD(&item->li_cil);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/*
639 * Write region vectors to log. The write happens using the space reservation
640 * of the ticket (tic). It is not a requirement that all writes for a given
Dave Chinner9b9fc2b72010-03-23 11:21:11 +1100641 * transaction occur with one call to xfs_log_write(). However, it is important
642 * to note that the transaction reservation code makes an assumption about the
643 * number of log headers a transaction requires that may be violated if you
644 * don't pass all the transaction vectors in one call....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
646int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000647xfs_log_write(
648 struct xfs_mount *mp,
649 struct xfs_log_iovec reg[],
650 int nentries,
651 struct xlog_ticket *tic,
652 xfs_lsn_t *start_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000654 struct log *log = mp->m_log;
655 int error;
Dave Chinner55b66332010-03-23 11:43:17 +1100656 struct xfs_log_vec vec = {
657 .lv_niovecs = nentries,
658 .lv_iovecp = reg,
659 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (XLOG_FORCED_SHUTDOWN(log))
662 return XFS_ERROR(EIO);
663
Dave Chinner55b66332010-03-23 11:43:17 +1100664 error = xlog_write(log, &vec, tic, start_lsn, NULL, 0);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000665 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +1000666 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100667 return error;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000668}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670void
671xfs_log_move_tail(xfs_mount_t *mp,
672 xfs_lsn_t tail_lsn)
673{
674 xlog_ticket_t *tic;
675 xlog_t *log = mp->m_log;
676 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (XLOG_FORCED_SHUTDOWN(log))
679 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (tail_lsn == 0) {
682 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000683 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000685 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000688 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
691 * tail_lsn.
692 */
693 if (tail_lsn != 1) {
694 log->l_tail_lsn = tail_lsn;
695 }
696
Dave Chinner10547942010-12-21 12:02:25 +1100697 if (!list_empty(&log->l_writeq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#ifdef DEBUG
699 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
700 panic("Recovery problem");
701#endif
702 cycle = log->l_grant_write_cycle;
703 bytes = log->l_grant_write_bytes;
704 free_bytes = xlog_space_left(log, cycle, bytes);
Dave Chinner10547942010-12-21 12:02:25 +1100705 list_for_each_entry(tic, &log->l_writeq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
707
708 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
709 break;
710 tail_lsn = 0;
711 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000712 sv_signal(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Dave Chinner10547942010-12-21 12:02:25 +1100715
716 if (!list_empty(&log->l_reserveq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717#ifdef DEBUG
718 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
719 panic("Recovery problem");
720#endif
721 cycle = log->l_grant_reserve_cycle;
722 bytes = log->l_grant_reserve_bytes;
723 free_bytes = xlog_space_left(log, cycle, bytes);
Dave Chinner10547942010-12-21 12:02:25 +1100724 list_for_each_entry(tic, &log->l_reserveq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
726 need_bytes = tic->t_unit_res*tic->t_cnt;
727 else
728 need_bytes = tic->t_unit_res;
729 if (free_bytes < need_bytes && tail_lsn != 1)
730 break;
731 tail_lsn = 0;
732 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000733 sv_signal(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000736 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737} /* xfs_log_move_tail */
738
739/*
740 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000741 * that needs to be covered. To begin the transition to the idle state
742 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
743 * If we are then in a state where covering is needed, the caller is informed
744 * that dummy transactions are required to move the log into the idle state.
745 *
746 * Because this is called as part of the sync process, we should also indicate
747 * that dummy transactions should be issued in anything but the covered or
748 * idle states. This ensures that the log tail is accurately reflected in
749 * the log at the end of the sync, hence if a crash occurrs avoids replay
750 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
752int
753xfs_log_need_covered(xfs_mount_t *mp)
754{
David Chinner27d8d5f2008-10-30 17:38:39 +1100755 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
David Chinner92821e22007-05-24 15:26:31 +1000758 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
760
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000761 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000762 switch (log->l_covered_state) {
763 case XLOG_STATE_COVER_DONE:
764 case XLOG_STATE_COVER_DONE2:
765 case XLOG_STATE_COVER_IDLE:
766 break;
767 case XLOG_STATE_COVER_NEED:
768 case XLOG_STATE_COVER_NEED2:
769 if (!xfs_trans_ail_tail(log->l_ailp) &&
770 xlog_iclogs_empty(log)) {
771 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
772 log->l_covered_state = XLOG_STATE_COVER_DONE;
773 else
774 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000776 /* FALLTHRU */
777 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000779 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000781 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100782 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785/******************************************************************************
786 *
787 * local routines
788 *
789 ******************************************************************************
790 */
791
792/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
793 * The log manager must keep track of the last LR which was committed
794 * to disk. The lsn of this LR will become the new tail_lsn whenever
795 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
796 * the situation where stuff could be written into the log but nothing
797 * was ever in the AIL when asked. Eventually, we panic since the
798 * tail hits the head.
799 *
800 * We may be holding the log iclog lock upon entering this routine.
801 */
802xfs_lsn_t
803xlog_assign_tail_lsn(xfs_mount_t *mp)
804{
805 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xlog_t *log = mp->m_log;
807
David Chinner5b00f142008-10-30 17:39:00 +1100808 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000809 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (tail_lsn != 0) {
811 log->l_tail_lsn = tail_lsn;
812 } else {
813 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
814 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000815 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 return tail_lsn;
818} /* xlog_assign_tail_lsn */
819
820
821/*
822 * Return the space in the log between the tail and the head. The head
823 * is passed in the cycle/bytes formal parms. In the special case where
824 * the reserve head has wrapped passed the tail, this calculation is no
825 * longer valid. In this case, just return 0 which means there is no space
826 * in the log. This works for all places where this function is called
827 * with the reserve head. Of course, if the write head were to ever
828 * wrap the tail, we should blow up. Rather than catch this case here,
829 * we depend on other ASSERTions in other parts of the code. XXXmiken
830 *
831 * This code also handles the case where the reservation head is behind
832 * the tail. The details of this case are described below, but the end
833 * result is that we return the size of the log as the amount of space left.
834 */
David Chinnera8272ce2007-11-23 16:28:09 +1100835STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836xlog_space_left(xlog_t *log, int cycle, int bytes)
837{
838 int free_bytes;
839 int tail_bytes;
840 int tail_cycle;
841
842 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
843 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
844 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
845 free_bytes = log->l_logsize - (bytes - tail_bytes);
846 } else if ((tail_cycle + 1) < cycle) {
847 return 0;
848 } else if (tail_cycle < cycle) {
849 ASSERT(tail_cycle == (cycle - 1));
850 free_bytes = tail_bytes - bytes;
851 } else {
852 /*
853 * The reservation head is behind the tail.
854 * In this case we just want to return the size of the
855 * log as the amount of space left.
856 */
857 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
858 "xlog_space_left: head behind tail\n"
859 " tail_cycle = %d, tail_bytes = %d\n"
860 " GH cycle = %d, GH bytes = %d",
861 tail_cycle, tail_bytes, cycle, bytes);
862 ASSERT(0);
863 free_bytes = log->l_logsize;
864 }
865 return free_bytes;
866} /* xlog_space_left */
867
868
869/*
870 * Log function which is called when an io completes.
871 *
872 * The log manager needs its own routine, in order to control what
873 * happens with the buffer after the write completes.
874 */
875void
876xlog_iodone(xfs_buf_t *bp)
877{
878 xlog_in_core_t *iclog;
879 xlog_t *l;
880 int aborted;
881
882 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
883 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
884 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
885 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 l = iclog->ic_log;
887
888 /*
889 * Race to shutdown the filesystem if we see an error.
890 */
891 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
892 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
893 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
894 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000895 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
897 * This flag will be propagated to the trans-committed
898 * callback routines to let them know that the log-commit
899 * didn't succeed.
900 */
901 aborted = XFS_LI_ABORTED;
902 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
903 aborted = XFS_LI_ABORTED;
904 }
David Chinner3db296f2007-05-14 18:24:16 +1000905
906 /* log I/O is always issued ASYNC */
907 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000909 /*
910 * do not reference the buffer (bp) here as we could race
911 * with it being freed after writing the unmount record to the
912 * log.
913 */
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915} /* xlog_iodone */
916
917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * Return size of each in-core log record buffer.
919 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200920 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
922 * If the filesystem blocksize is too large, we may need to choose a
923 * larger size since the directory code currently logs entire blocks.
924 */
925
926STATIC void
927xlog_get_iclog_buffer_size(xfs_mount_t *mp,
928 xlog_t *log)
929{
930 int size;
931 int xhdrs;
932
Eric Sandeen1cb51252007-08-16 16:24:43 +1000933 if (mp->m_logbufs <= 0)
934 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
935 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100936 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /*
939 * Buffer size passed in from mount system call.
940 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100941 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 size = log->l_iclog_size = mp->m_logbsize;
943 log->l_iclog_size_log = 0;
944 while (size != 1) {
945 log->l_iclog_size_log++;
946 size >>= 1;
947 }
948
Eric Sandeen62118702008-03-06 13:44:28 +1100949 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200950 /* # headers = size / 32k
951 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
953
954 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
955 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
956 xhdrs++;
957 log->l_iclog_hsize = xhdrs << BBSHIFT;
958 log->l_iclog_heads = xhdrs;
959 } else {
960 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
961 log->l_iclog_hsize = BBSIZE;
962 log->l_iclog_heads = 1;
963 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100964 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200967 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +1000968 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
969 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* the default log size is 16k or 32k which is one header sector */
972 log->l_iclog_hsize = BBSIZE;
973 log->l_iclog_heads = 1;
974
Christoph Hellwig7153f8b2009-02-09 08:36:46 +0100975done:
976 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100977 if (mp->m_logbufs == 0)
978 mp->m_logbufs = log->l_iclog_bufs;
979 if (mp->m_logbsize == 0)
980 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981} /* xlog_get_iclog_buffer_size */
982
983
984/*
985 * This routine initializes some of the log structure for a given mount point.
986 * Its primary purpose is to fill in enough, so recovery can occur. However,
987 * some other stuff may be filled in too.
988 */
989STATIC xlog_t *
990xlog_alloc_log(xfs_mount_t *mp,
991 xfs_buftarg_t *log_target,
992 xfs_daddr_t blk_offset,
993 int num_bblks)
994{
995 xlog_t *log;
996 xlog_rec_header_t *head;
997 xlog_in_core_t **iclogp;
998 xlog_in_core_t *iclog, *prev_iclog=NULL;
999 xfs_buf_t *bp;
1000 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001001 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001002 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Dave Chinner644c3562008-11-10 16:50:24 +11001004 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001005 if (!log) {
1006 xlog_warn("XFS: Log allocation failed: No memory!");
1007 goto out;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 log->l_mp = mp;
1011 log->l_targ = log_target;
1012 log->l_logsize = BBTOB(num_bblks);
1013 log->l_logBBstart = blk_offset;
1014 log->l_logBBsize = num_bblks;
1015 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1016 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1017
1018 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001019 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1021 log->l_last_sync_lsn = log->l_tail_lsn;
1022 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1023 log->l_grant_reserve_cycle = 1;
1024 log->l_grant_write_cycle = 1;
Dave Chinner10547942010-12-21 12:02:25 +11001025 INIT_LIST_HEAD(&log->l_reserveq);
1026 INIT_LIST_HEAD(&log->l_writeq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Chinnera6cb7672009-04-06 18:39:27 +02001028 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001029 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001030 log2_size = mp->m_sb.sb_logsectlog;
1031 if (log2_size < BBSHIFT) {
1032 xlog_warn("XFS: Log sector size too small "
1033 "(0x%x < 0x%x)", log2_size, BBSHIFT);
1034 goto out_free_log;
1035 }
1036
1037 log2_size -= BBSHIFT;
1038 if (log2_size > mp->m_sectbb_log) {
1039 xlog_warn("XFS: Log sector size too large "
1040 "(0x%x > 0x%x)", log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001041 goto out_free_log;
1042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001045 if (log2_size && log->l_logBBstart > 0 &&
1046 !xfs_sb_version_haslogv2(&mp->m_sb)) {
1047
Dave Chinnera6cb7672009-04-06 18:39:27 +02001048 xlog_warn("XFS: log sector size (0x%x) invalid "
Alex Elder69ce58f2010-04-20 17:09:59 +10001049 "for configuration.", log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001050 goto out_free_log;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001053 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 xlog_get_iclog_buffer_size(mp, log);
1056
Dave Chinnera6cb7672009-04-06 18:39:27 +02001057 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001059 if (!bp)
1060 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1063 ASSERT(XFS_BUF_ISBUSY(bp));
1064 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1065 log->l_xbuf = bp;
1066
Eric Sandeen007c61c2007-10-11 17:43:56 +10001067 spin_lock_init(&log->l_icloglock);
1068 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001069 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1072 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1073
1074 iclogp = &log->l_iclog;
1075 /*
1076 * The amount of memory to allocate for the iclog structure is
1077 * rather funky due to the way the structure is defined. It is
1078 * done this way so that we can use different sizes for machines
1079 * with different amounts of memory. See the definition of
1080 * xlog_in_core_t in xfs_log_priv.h for details.
1081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ASSERT(log->l_iclog_size >= 4096);
1083 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001084 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1085 if (!*iclogp)
1086 goto out_free_iclog;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 iclog->ic_prev = prev_iclog;
1090 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001091
Dave Chinner686865f2010-09-24 20:07:47 +10001092 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1093 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001094 if (!bp)
1095 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001096 if (!XFS_BUF_CPSEMA(bp))
1097 ASSERT(0);
1098 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001099 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1100 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001101 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001102#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 head = &iclog->ic_header;
1106 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001107 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1108 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001109 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001110 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001112 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1116 iclog->ic_state = XLOG_STATE_ACTIVE;
1117 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001118 atomic_set(&iclog->ic_refcnt, 0);
1119 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001121 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1124 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001125 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1126 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 iclogp = &iclog->ic_next;
1129 }
1130 *iclogp = log->l_iclog; /* complete ring */
1131 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1132
Dave Chinner71e330b2010-05-21 14:37:18 +10001133 error = xlog_cil_init(log);
1134 if (error)
1135 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001137
1138out_free_iclog:
1139 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1140 prev_iclog = iclog->ic_next;
1141 if (iclog->ic_bp) {
1142 sv_destroy(&iclog->ic_force_wait);
1143 sv_destroy(&iclog->ic_write_wait);
1144 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001145 }
1146 kmem_free(iclog);
1147 }
1148 spinlock_destroy(&log->l_icloglock);
1149 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001150 xfs_buf_free(log->l_xbuf);
1151out_free_log:
1152 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001153out:
1154 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155} /* xlog_alloc_log */
1156
1157
1158/*
1159 * Write out the commit record of a transaction associated with the given
1160 * ticket. Return the lsn of the commit record.
1161 */
1162STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001163xlog_commit_record(
1164 struct log *log,
1165 struct xlog_ticket *ticket,
1166 struct xlog_in_core **iclog,
1167 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Dave Chinner55b66332010-03-23 11:43:17 +11001169 struct xfs_mount *mp = log->l_mp;
1170 int error;
1171 struct xfs_log_iovec reg = {
1172 .i_addr = NULL,
1173 .i_len = 0,
1174 .i_type = XLOG_REG_TYPE_COMMIT,
1175 };
1176 struct xfs_log_vec vec = {
1177 .lv_niovecs = 1,
1178 .lv_iovecp = &reg,
1179 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001182 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1183 XLOG_COMMIT_TRANS);
1184 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001185 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001186 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189/*
1190 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1191 * log space. This code pushes on the lsn which would supposedly free up
1192 * the 25% which we want to leave free. We may need to adopt a policy which
1193 * pushes on an lsn which is further along in the log once we reach the high
1194 * water mark. In this manner, we would be creating a low water mark.
1195 */
David Chinnera8272ce2007-11-23 16:28:09 +11001196STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197xlog_grant_push_ail(xfs_mount_t *mp,
1198 int need_bytes)
1199{
1200 xlog_t *log = mp->m_log; /* pointer to the log */
1201 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1202 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1203 int free_blocks; /* free blocks left to write to */
1204 int free_bytes; /* free bytes left to write to */
1205 int threshold_block; /* block in lsn we'd like to be at */
1206 int threshold_cycle; /* lsn cycle we'd like to be at */
1207 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1210
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001211 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 free_bytes = xlog_space_left(log,
1213 log->l_grant_reserve_cycle,
1214 log->l_grant_reserve_bytes);
1215 tail_lsn = log->l_tail_lsn;
1216 free_blocks = BTOBBT(free_bytes);
1217
1218 /*
1219 * Set the threshold for the minimum number of free blocks in the
1220 * log to the maximum of what the caller needs, one quarter of the
1221 * log, and 256 blocks.
1222 */
1223 free_threshold = BTOBB(need_bytes);
1224 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1225 free_threshold = MAX(free_threshold, 256);
1226 if (free_blocks < free_threshold) {
1227 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1228 threshold_cycle = CYCLE_LSN(tail_lsn);
1229 if (threshold_block >= log->l_logBBsize) {
1230 threshold_block -= log->l_logBBsize;
1231 threshold_cycle += 1;
1232 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001233 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* Don't pass in an lsn greater than the lsn of the last
1236 * log record known to be on disk.
1237 */
1238 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1239 threshold_lsn = log->l_last_sync_lsn;
1240 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001241 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /*
1244 * Get the transaction layer to kick the dirty buffers out to
1245 * disk asynchronously. No point in trying to do this if
1246 * the filesystem is shutting down.
1247 */
1248 if (threshold_lsn &&
1249 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001250 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251} /* xlog_grant_push_ail */
1252
Christoph Hellwig873ff552010-01-13 22:17:57 +00001253/*
1254 * The bdstrat callback function for log bufs. This gives us a central
1255 * place to trap bufs in case we get hit by a log I/O error and need to
1256 * shutdown. Actually, in practice, even when we didn't get a log error,
1257 * we transition the iclogs to IOERROR state *after* flushing all existing
1258 * iclogs to disk. This is because we don't want anymore new transactions to be
1259 * started or completed afterwards.
1260 */
1261STATIC int
1262xlog_bdstrat(
1263 struct xfs_buf *bp)
1264{
1265 struct xlog_in_core *iclog;
1266
1267 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1268 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1269 XFS_BUF_ERROR(bp, EIO);
1270 XFS_BUF_STALE(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001271 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001272 /*
1273 * It would seem logical to return EIO here, but we rely on
1274 * the log state machine to propagate I/O errors instead of
1275 * doing it here.
1276 */
1277 return 0;
1278 }
1279
1280 bp->b_flags |= _XBF_RUN_QUEUES;
1281 xfs_buf_iorequest(bp);
1282 return 0;
1283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285/*
1286 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1287 * fashion. Previously, we should have moved the current iclog
1288 * ptr in the log to point to the next available iclog. This allows further
1289 * write to continue while this code syncs out an iclog ready to go.
1290 * Before an in-core log can be written out, the data section must be scanned
1291 * to save away the 1st word of each BBSIZE block into the header. We replace
1292 * it with the current cycle count. Each BBSIZE block is tagged with the
1293 * cycle count because there in an implicit assumption that drives will
1294 * guarantee that entire 512 byte blocks get written at once. In other words,
1295 * we can't have part of a 512 byte block written and part not written. By
1296 * tagging each block, we will know which blocks are valid when recovering
1297 * after an unclean shutdown.
1298 *
1299 * This routine is single threaded on the iclog. No other thread can be in
1300 * this routine with the same iclog. Changing contents of iclog can there-
1301 * fore be done without grabbing the state machine lock. Updating the global
1302 * log will require grabbing the lock though.
1303 *
1304 * The entire log manager uses a logical block numbering scheme. Only
1305 * log_sync (and then only bwrite()) know about the fact that the log may
1306 * not start with block zero on a given device. The log block start offset
1307 * is added immediately before calling bwrite().
1308 */
1309
David Chinnera8272ce2007-11-23 16:28:09 +11001310STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311xlog_sync(xlog_t *log,
1312 xlog_in_core_t *iclog)
1313{
1314 xfs_caddr_t dptr; /* pointer to byte sized element */
1315 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001316 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 uint count; /* byte count of bwrite */
1318 uint count_init; /* initial count before roundup */
1319 int roundoff; /* roundoff to BB or stripe */
1320 int split = 0; /* split write into two regions */
1321 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001322 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001325 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* Add for LR header */
1328 count_init = log->l_iclog_hsize + iclog->ic_offset;
1329
1330 /* Round out the log write size */
1331 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1332 /* we have a v2 stripe unit to use */
1333 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1334 } else {
1335 count = BBTOB(BTOBB(count_init));
1336 }
1337 roundoff = count - count_init;
1338 ASSERT(roundoff >= 0);
1339 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1340 roundoff < log->l_mp->m_sb.sb_logsunit)
1341 ||
1342 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1343 roundoff < BBTOB(1)));
1344
1345 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001346 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001347 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001348 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /* put cycle number in every block */
1351 xlog_pack_data(log, iclog, roundoff);
1352
1353 /* real byte length */
1354 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001355 iclog->ic_header.h_len =
1356 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001358 iclog->ic_header.h_len =
1359 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361
Nathan Scottf5faad72006-07-28 17:04:44 +10001362 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1364 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001365 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1368
1369 /* Do we need to split this write into 2 parts? */
1370 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1371 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1372 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1373 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1374 } else {
1375 iclog->ic_bwritecnt = 1;
1376 }
David Chinner511105b2007-05-24 15:21:57 +10001377 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001379 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 XFS_BUF_BUSY(bp);
1381 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001382 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001383
1384 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001385 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1388 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1389
1390 xlog_verify_iclog(log, iclog, count, B_TRUE);
1391
1392 /* account for log which doesn't start at block #0 */
1393 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1394 /*
1395 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1396 * is shutting down.
1397 */
1398 XFS_BUF_WRITE(bp);
1399
Christoph Hellwig873ff552010-01-13 22:17:57 +00001400 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1402 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001406 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1408 (unsigned long)1);
1409 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1410 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1411 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1412 (__psint_t)count), split);
1413 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001414 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 XFS_BUF_BUSY(bp);
1416 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001417 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001418 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1419 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 dptr = XFS_BUF_PTR(bp);
1421 /*
1422 * Bump the cycle numbers at the start of each block
1423 * since this part of the buffer is at the start of
1424 * a new cycle. Watch out for the header magic number
1425 * case, though.
1426 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001427 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001428 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001429 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001430 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 dptr += BBSIZE;
1432 }
1433
1434 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1435 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1436
Nathan Scottc41564b2006-03-29 08:55:14 +10001437 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1439 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001440 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1442 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447} /* xlog_sync */
1448
1449
1450/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001451 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
David Chinnera8272ce2007-11-23 16:28:09 +11001453STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001454xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int i;
1458
Dave Chinner71e330b2010-05-21 14:37:18 +10001459 xlog_cil_destroy(log);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 iclog = log->l_iclog;
1462 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001463 sv_destroy(&iclog->ic_force_wait);
1464 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001467 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 iclog = next_iclog;
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 spinlock_destroy(&log->l_icloglock);
1471 spinlock_destroy(&log->l_grant_lock);
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001475 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001476} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478/*
1479 * Update counters atomically now that memcpy is done.
1480 */
1481/* ARGSUSED */
1482static inline void
1483xlog_state_finish_copy(xlog_t *log,
1484 xlog_in_core_t *iclog,
1485 int record_cnt,
1486 int copy_bytes)
1487{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001488 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001490 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 iclog->ic_offset += copy_bytes;
1492
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001493 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494} /* xlog_state_finish_copy */
1495
1496
1497
1498
1499/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001500 * print out info relating to regions written which consume
1501 * the reservation
1502 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001503void
1504xlog_print_tic_res(
1505 struct xfs_mount *mp,
1506 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001507{
1508 uint i;
1509 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1510
1511 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1512 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1513 "bformat",
1514 "bchunk",
1515 "efi_format",
1516 "efd_format",
1517 "iformat",
1518 "icore",
1519 "iext",
1520 "ibroot",
1521 "ilocal",
1522 "iattr_ext",
1523 "iattr_broot",
1524 "iattr_local",
1525 "qformat",
1526 "dquot",
1527 "quotaoff",
1528 "LR header",
1529 "unmount",
1530 "commit",
1531 "trans header"
1532 };
1533 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1534 "SETATTR_NOT_SIZE",
1535 "SETATTR_SIZE",
1536 "INACTIVE",
1537 "CREATE",
1538 "CREATE_TRUNC",
1539 "TRUNCATE_FILE",
1540 "REMOVE",
1541 "LINK",
1542 "RENAME",
1543 "MKDIR",
1544 "RMDIR",
1545 "SYMLINK",
1546 "SET_DMATTRS",
1547 "GROWFS",
1548 "STRAT_WRITE",
1549 "DIOSTRAT",
1550 "WRITE_SYNC",
1551 "WRITEID",
1552 "ADDAFORK",
1553 "ATTRINVAL",
1554 "ATRUNCATE",
1555 "ATTR_SET",
1556 "ATTR_RM",
1557 "ATTR_FLAG",
1558 "CLEAR_AGI_BUCKET",
1559 "QM_SBCHANGE",
1560 "DUMMY1",
1561 "DUMMY2",
1562 "QM_QUOTAOFF",
1563 "QM_DQALLOC",
1564 "QM_SETQLIM",
1565 "QM_DQCLUSTER",
1566 "QM_QINOCREATE",
1567 "QM_QUOTAOFF_END",
1568 "SB_UNIT",
1569 "FSYNC_TS",
1570 "GROWFSRT_ALLOC",
1571 "GROWFSRT_ZERO",
1572 "GROWFSRT_FREE",
1573 "SWAPEXT"
1574 };
1575
1576 xfs_fs_cmn_err(CE_WARN, mp,
1577 "xfs_log_write: reservation summary:\n"
1578 " trans type = %s (%u)\n"
1579 " unit res = %d bytes\n"
1580 " current res = %d bytes\n"
1581 " total reg = %u bytes (o/flow = %u bytes)\n"
1582 " ophdrs = %u (ophdr space = %u bytes)\n"
1583 " ophdr + reg = %u bytes\n"
1584 " num regions = %u\n",
1585 ((ticket->t_trans_type <= 0 ||
1586 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1587 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1588 ticket->t_trans_type,
1589 ticket->t_unit_res,
1590 ticket->t_curr_res,
1591 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1592 ticket->t_res_num_ophdrs, ophdr_spc,
1593 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001594 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001595 ticket->t_res_num);
1596
1597 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001598 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001599 cmn_err(CE_WARN,
1600 "region[%u]: %s - %u bytes\n",
1601 i,
1602 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1603 "bad-rtype" : res_type_str[r_type-1]),
1604 ticket->t_res_arr[i].r_len);
1605 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001606
1607 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1608 "xfs_log_write: reservation ran out. Need to up reservation");
1609 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001610}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001611
1612/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001613 * Calculate the potential space needed by the log vector. Each region gets
1614 * its own xlog_op_header_t and may need to be double word aligned.
1615 */
1616static int
1617xlog_write_calc_vec_length(
1618 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001619 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001620{
Dave Chinner55b66332010-03-23 11:43:17 +11001621 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001622 int headers = 0;
1623 int len = 0;
1624 int i;
1625
1626 /* acct for start rec of xact */
1627 if (ticket->t_flags & XLOG_TIC_INITED)
1628 headers++;
1629
Dave Chinner55b66332010-03-23 11:43:17 +11001630 for (lv = log_vector; lv; lv = lv->lv_next) {
1631 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001632
Dave Chinner55b66332010-03-23 11:43:17 +11001633 for (i = 0; i < lv->lv_niovecs; i++) {
1634 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1635
1636 len += vecp->i_len;
1637 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1638 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001639 }
1640
1641 ticket->t_res_num_ophdrs += headers;
1642 len += headers * sizeof(struct xlog_op_header);
1643
1644 return len;
1645}
1646
1647/*
1648 * If first write for transaction, insert start record We can't be trying to
1649 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1650 */
1651static int
1652xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001653 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001654 struct xlog_ticket *ticket)
1655{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001656 if (!(ticket->t_flags & XLOG_TIC_INITED))
1657 return 0;
1658
1659 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1660 ophdr->oh_clientid = ticket->t_clientid;
1661 ophdr->oh_len = 0;
1662 ophdr->oh_flags = XLOG_START_TRANS;
1663 ophdr->oh_res2 = 0;
1664
1665 ticket->t_flags &= ~XLOG_TIC_INITED;
1666
1667 return sizeof(struct xlog_op_header);
1668}
1669
1670static xlog_op_header_t *
1671xlog_write_setup_ophdr(
1672 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001673 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001674 struct xlog_ticket *ticket,
1675 uint flags)
1676{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001677 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1678 ophdr->oh_clientid = ticket->t_clientid;
1679 ophdr->oh_res2 = 0;
1680
1681 /* are we copying a commit or unmount record? */
1682 ophdr->oh_flags = flags;
1683
1684 /*
1685 * We've seen logs corrupted with bad transaction client ids. This
1686 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1687 * and shut down the filesystem.
1688 */
1689 switch (ophdr->oh_clientid) {
1690 case XFS_TRANSACTION:
1691 case XFS_VOLUME:
1692 case XFS_LOG:
1693 break;
1694 default:
1695 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1696 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1697 ophdr->oh_clientid, ticket);
1698 return NULL;
1699 }
1700
1701 return ophdr;
1702}
1703
1704/*
1705 * Set up the parameters of the region copy into the log. This has
1706 * to handle region write split across multiple log buffers - this
1707 * state is kept external to this function so that this code can
1708 * can be written in an obvious, self documenting manner.
1709 */
1710static int
1711xlog_write_setup_copy(
1712 struct xlog_ticket *ticket,
1713 struct xlog_op_header *ophdr,
1714 int space_available,
1715 int space_required,
1716 int *copy_off,
1717 int *copy_len,
1718 int *last_was_partial_copy,
1719 int *bytes_consumed)
1720{
1721 int still_to_copy;
1722
1723 still_to_copy = space_required - *bytes_consumed;
1724 *copy_off = *bytes_consumed;
1725
1726 if (still_to_copy <= space_available) {
1727 /* write of region completes here */
1728 *copy_len = still_to_copy;
1729 ophdr->oh_len = cpu_to_be32(*copy_len);
1730 if (*last_was_partial_copy)
1731 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1732 *last_was_partial_copy = 0;
1733 *bytes_consumed = 0;
1734 return 0;
1735 }
1736
1737 /* partial write of region, needs extra log op header reservation */
1738 *copy_len = space_available;
1739 ophdr->oh_len = cpu_to_be32(*copy_len);
1740 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1741 if (*last_was_partial_copy)
1742 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1743 *bytes_consumed += *copy_len;
1744 (*last_was_partial_copy)++;
1745
1746 /* account for new log op header */
1747 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1748 ticket->t_res_num_ophdrs++;
1749
1750 return sizeof(struct xlog_op_header);
1751}
1752
1753static int
1754xlog_write_copy_finish(
1755 struct log *log,
1756 struct xlog_in_core *iclog,
1757 uint flags,
1758 int *record_cnt,
1759 int *data_cnt,
1760 int *partial_copy,
1761 int *partial_copy_len,
1762 int log_offset,
1763 struct xlog_in_core **commit_iclog)
1764{
1765 if (*partial_copy) {
1766 /*
1767 * This iclog has already been marked WANT_SYNC by
1768 * xlog_state_get_iclog_space.
1769 */
1770 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1771 *record_cnt = 0;
1772 *data_cnt = 0;
1773 return xlog_state_release_iclog(log, iclog);
1774 }
1775
1776 *partial_copy = 0;
1777 *partial_copy_len = 0;
1778
1779 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1780 /* no more space in this iclog - push it. */
1781 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1782 *record_cnt = 0;
1783 *data_cnt = 0;
1784
1785 spin_lock(&log->l_icloglock);
1786 xlog_state_want_sync(log, iclog);
1787 spin_unlock(&log->l_icloglock);
1788
1789 if (!commit_iclog)
1790 return xlog_state_release_iclog(log, iclog);
1791 ASSERT(flags & XLOG_COMMIT_TRANS);
1792 *commit_iclog = iclog;
1793 }
1794
1795 return 0;
1796}
1797
1798/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * Write some region out to in-core log
1800 *
1801 * This will be called when writing externally provided regions or when
1802 * writing out a commit record for a given transaction.
1803 *
1804 * General algorithm:
1805 * 1. Find total length of this write. This may include adding to the
1806 * lengths passed in.
1807 * 2. Check whether we violate the tickets reservation.
1808 * 3. While writing to this iclog
1809 * A. Reserve as much space in this iclog as can get
1810 * B. If this is first write, save away start lsn
1811 * C. While writing this region:
1812 * 1. If first write of transaction, write start record
1813 * 2. Write log operation header (header per region)
1814 * 3. Find out if we can fit entire region into this iclog
1815 * 4. Potentially, verify destination memcpy ptr
1816 * 5. Memcpy (partial) region
1817 * 6. If partial copy, release iclog; otherwise, continue
1818 * copying more regions into current iclog
1819 * 4. Mark want sync bit (in simulation mode)
1820 * 5. Release iclog for potential flush to on-disk log.
1821 *
1822 * ERRORS:
1823 * 1. Panic if reservation is overrun. This should never happen since
1824 * reservation amounts are generated internal to the filesystem.
1825 * NOTES:
1826 * 1. Tickets are single threaded data structures.
1827 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1828 * syncing routine. When a single log_write region needs to span
1829 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1830 * on all log operation writes which don't contain the end of the
1831 * region. The XLOG_END_TRANS bit is used for the in-core log
1832 * operation which contains the end of the continued log_write region.
1833 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1834 * we don't really know exactly how much space will be used. As a result,
1835 * we don't update ic_offset until the end when we know exactly how many
1836 * bytes have been written out.
1837 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001838int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001839xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001840 struct log *log,
1841 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001842 struct xlog_ticket *ticket,
1843 xfs_lsn_t *start_lsn,
1844 struct xlog_in_core **commit_iclog,
1845 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001847 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001848 struct xfs_log_iovec *vecp;
1849 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001850 int len;
1851 int index;
1852 int partial_copy = 0;
1853 int partial_copy_len = 0;
1854 int contwr = 0;
1855 int record_cnt = 0;
1856 int data_cnt = 0;
1857 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001859 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Dave Chinner55b66332010-03-23 11:43:17 +11001861 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001862 if (log->l_cilp) {
1863 /*
1864 * Region headers and bytes are already accounted for.
1865 * We only need to take into account start records and
1866 * split regions in this function.
1867 */
1868 if (ticket->t_flags & XLOG_TIC_INITED)
1869 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1870
1871 /*
1872 * Commit record headers need to be accounted for. These
1873 * come in as separate writes so are easy to detect.
1874 */
1875 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1876 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1877 } else
1878 ticket->t_curr_res -= len;
1879
1880 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001881 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Dave Chinner55b66332010-03-23 11:43:17 +11001883 index = 0;
1884 lv = log_vector;
1885 vecp = lv->lv_iovecp;
1886 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001887 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001888 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001890 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1891 &contwr, &log_offset);
1892 if (error)
1893 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001895 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001896 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001898 /* start_lsn is the first lsn written to. That's all we need. */
1899 if (!*start_lsn)
1900 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001902 /*
1903 * This loop writes out as many regions as can fit in the amount
1904 * of space which was allocated by xlog_state_get_iclog_space().
1905 */
Dave Chinner55b66332010-03-23 11:43:17 +11001906 while (lv && index < lv->lv_niovecs) {
1907 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001908 struct xlog_op_header *ophdr;
1909 int start_rec_copy;
1910 int copy_len;
1911 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Dave Chinner55b66332010-03-23 11:43:17 +11001913 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001914 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001916 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1917 if (start_rec_copy) {
1918 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001919 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001920 start_rec_copy);
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001923 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1924 if (!ophdr)
1925 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001927 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001928 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001930 len += xlog_write_setup_copy(ticket, ophdr,
1931 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001932 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001933 &copy_off, &copy_len,
1934 &partial_copy,
1935 &partial_copy_len);
1936 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001937
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001938 /* copy region */
1939 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001940 memcpy(ptr, reg->i_addr + copy_off, copy_len);
1941 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001942
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001943 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1944 record_cnt++;
1945 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001947 error = xlog_write_copy_finish(log, iclog, flags,
1948 &record_cnt, &data_cnt,
1949 &partial_copy,
1950 &partial_copy_len,
1951 log_offset,
1952 commit_iclog);
1953 if (error)
1954 return error;
1955
1956 /*
1957 * if we had a partial copy, we need to get more iclog
1958 * space but we don't want to increment the region
1959 * index because there is still more is this region to
1960 * write.
1961 *
1962 * If we completed writing this region, and we flushed
1963 * the iclog (indicated by resetting of the record
1964 * count), then we also need to get more log space. If
1965 * this was the last record, though, we are done and
1966 * can just return.
1967 */
1968 if (partial_copy)
1969 break;
1970
Dave Chinner55b66332010-03-23 11:43:17 +11001971 if (++index == lv->lv_niovecs) {
1972 lv = lv->lv_next;
1973 index = 0;
1974 if (lv)
1975 vecp = lv->lv_iovecp;
1976 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001977 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11001978 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001979 return 0;
1980 break;
1981 }
1982 }
1983 }
1984
1985 ASSERT(len == 0);
1986
1987 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1988 if (!commit_iclog)
1989 return xlog_state_release_iclog(log, iclog);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 ASSERT(flags & XLOG_COMMIT_TRANS);
1992 *commit_iclog = iclog;
1993 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996
1997/*****************************************************************************
1998 *
1999 * State Machine functions
2000 *
2001 *****************************************************************************
2002 */
2003
2004/* Clean iclogs starting from the head. This ordering must be
2005 * maintained, so an iclog doesn't become ACTIVE beyond one that
2006 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002007 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 * iclog is trying to sync to disk.
2009 *
2010 * State Change: DIRTY -> ACTIVE
2011 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002012STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013xlog_state_clean_log(xlog_t *log)
2014{
2015 xlog_in_core_t *iclog;
2016 int changed = 0;
2017
2018 iclog = log->l_iclog;
2019 do {
2020 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2021 iclog->ic_state = XLOG_STATE_ACTIVE;
2022 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002023 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /*
2025 * If the number of ops in this iclog indicate it just
2026 * contains the dummy transaction, we can
2027 * change state into IDLE (the second time around).
2028 * Otherwise we should change the state into
2029 * NEED a dummy.
2030 * We don't need to cover the dummy.
2031 */
2032 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002033 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2034 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 changed = 1;
2036 } else {
2037 /*
2038 * We have two dirty iclogs so start over
2039 * This could also be num of ops indicates
2040 * this is not the dummy going out.
2041 */
2042 changed = 2;
2043 }
2044 iclog->ic_header.h_num_logops = 0;
2045 memset(iclog->ic_header.h_cycle_data, 0,
2046 sizeof(iclog->ic_header.h_cycle_data));
2047 iclog->ic_header.h_lsn = 0;
2048 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2049 /* do nothing */;
2050 else
2051 break; /* stop cleaning */
2052 iclog = iclog->ic_next;
2053 } while (iclog != log->l_iclog);
2054
2055 /* log is locked when we are called */
2056 /*
2057 * Change state for the dummy log recording.
2058 * We usually go to NEED. But we go to NEED2 if the changed indicates
2059 * we are done writing the dummy record.
2060 * If we are done with the second dummy recored (DONE2), then
2061 * we go to IDLE.
2062 */
2063 if (changed) {
2064 switch (log->l_covered_state) {
2065 case XLOG_STATE_COVER_IDLE:
2066 case XLOG_STATE_COVER_NEED:
2067 case XLOG_STATE_COVER_NEED2:
2068 log->l_covered_state = XLOG_STATE_COVER_NEED;
2069 break;
2070
2071 case XLOG_STATE_COVER_DONE:
2072 if (changed == 1)
2073 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2074 else
2075 log->l_covered_state = XLOG_STATE_COVER_NEED;
2076 break;
2077
2078 case XLOG_STATE_COVER_DONE2:
2079 if (changed == 1)
2080 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2081 else
2082 log->l_covered_state = XLOG_STATE_COVER_NEED;
2083 break;
2084
2085 default:
2086 ASSERT(0);
2087 }
2088 }
2089} /* xlog_state_clean_log */
2090
2091STATIC xfs_lsn_t
2092xlog_get_lowest_lsn(
2093 xlog_t *log)
2094{
2095 xlog_in_core_t *lsn_log;
2096 xfs_lsn_t lowest_lsn, lsn;
2097
2098 lsn_log = log->l_iclog;
2099 lowest_lsn = 0;
2100 do {
2101 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002102 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if ((lsn && !lowest_lsn) ||
2104 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2105 lowest_lsn = lsn;
2106 }
2107 }
2108 lsn_log = lsn_log->ic_next;
2109 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002110 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113
2114STATIC void
2115xlog_state_do_callback(
2116 xlog_t *log,
2117 int aborted,
2118 xlog_in_core_t *ciclog)
2119{
2120 xlog_in_core_t *iclog;
2121 xlog_in_core_t *first_iclog; /* used to know when we've
2122 * processed all iclogs once */
2123 xfs_log_callback_t *cb, *cb_next;
2124 int flushcnt = 0;
2125 xfs_lsn_t lowest_lsn;
2126 int ioerrors; /* counter: iclogs with errors */
2127 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2128 int funcdidcallbacks; /* flag: function did callbacks */
2129 int repeats; /* for issuing console warnings if
2130 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002131 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002133 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 first_iclog = iclog = log->l_iclog;
2135 ioerrors = 0;
2136 funcdidcallbacks = 0;
2137 repeats = 0;
2138
2139 do {
2140 /*
2141 * Scan all iclogs starting with the one pointed to by the
2142 * log. Reset this starting point each time the log is
2143 * unlocked (during callbacks).
2144 *
2145 * Keep looping through iclogs until one full pass is made
2146 * without running any callbacks.
2147 */
2148 first_iclog = log->l_iclog;
2149 iclog = log->l_iclog;
2150 loopdidcallbacks = 0;
2151 repeats++;
2152
2153 do {
2154
2155 /* skip all iclogs in the ACTIVE & DIRTY states */
2156 if (iclog->ic_state &
2157 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2158 iclog = iclog->ic_next;
2159 continue;
2160 }
2161
2162 /*
2163 * Between marking a filesystem SHUTDOWN and stopping
2164 * the log, we do flush all iclogs to disk (if there
2165 * wasn't a log I/O error). So, we do want things to
2166 * go smoothly in case of just a SHUTDOWN w/o a
2167 * LOG_IO_ERROR.
2168 */
2169 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2170 /*
2171 * Can only perform callbacks in order. Since
2172 * this iclog is not in the DONE_SYNC/
2173 * DO_CALLBACK state, we skip the rest and
2174 * just try to clean up. If we set our iclog
2175 * to DO_CALLBACK, we will not process it when
2176 * we retry since a previous iclog is in the
2177 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002178 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 */
2180 if (!(iclog->ic_state &
2181 (XLOG_STATE_DONE_SYNC |
2182 XLOG_STATE_DO_CALLBACK))) {
2183 if (ciclog && (ciclog->ic_state ==
2184 XLOG_STATE_DONE_SYNC)) {
2185 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2186 }
2187 break;
2188 }
2189 /*
2190 * We now have an iclog that is in either the
2191 * DO_CALLBACK or DONE_SYNC states. The other
2192 * states (WANT_SYNC, SYNCING, or CALLBACK were
2193 * caught by the above if and are going to
2194 * clean (i.e. we aren't doing their callbacks)
2195 * see the above if.
2196 */
2197
2198 /*
2199 * We will do one more check here to see if we
2200 * have chased our tail around.
2201 */
2202
2203 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002204 if (lowest_lsn &&
2205 XFS_LSN_CMP(lowest_lsn,
2206 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 iclog = iclog->ic_next;
2208 continue; /* Leave this iclog for
2209 * another thread */
2210 }
2211
2212 iclog->ic_state = XLOG_STATE_CALLBACK;
2213
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002214 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002217 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * No one else can be here except us.
2219 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002220 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002221 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2222 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2223 log->l_last_sync_lsn =
2224 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002225 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002228 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 ioerrors++;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
David Chinner114d23a2008-04-10 12:18:39 +10002232 /*
2233 * Keep processing entries in the callback list until
2234 * we come around and it is empty. We need to
2235 * atomically see that the list is empty and change the
2236 * state to DIRTY so that we don't miss any more
2237 * callbacks being added.
2238 */
2239 spin_lock(&iclog->ic_callback_lock);
2240 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002241 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 iclog->ic_callback_tail = &(iclog->ic_callback);
2243 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002244 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002247 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 cb_next = cb->cb_next;
2249 cb->cb_func(cb->cb_arg, aborted);
2250 }
David Chinner114d23a2008-04-10 12:18:39 +10002251 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 cb = iclog->ic_callback;
2253 }
2254
2255 loopdidcallbacks++;
2256 funcdidcallbacks++;
2257
David Chinner114d23a2008-04-10 12:18:39 +10002258 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002259 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002260 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2262 iclog->ic_state = XLOG_STATE_DIRTY;
2263
2264 /*
2265 * Transition from DIRTY to ACTIVE if applicable.
2266 * NOP if STATE_IOERROR.
2267 */
2268 xlog_state_clean_log(log);
2269
2270 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002271 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 iclog = iclog->ic_next;
2274 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002275
2276 if (repeats > 5000) {
2277 flushcnt += repeats;
2278 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002280 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002281 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 } while (!ioerrors && loopdidcallbacks);
2284
2285 /*
2286 * make one last gasp attempt to see if iclogs are being left in
2287 * limbo..
2288 */
2289#ifdef DEBUG
2290 if (funcdidcallbacks) {
2291 first_iclog = iclog = log->l_iclog;
2292 do {
2293 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2294 /*
2295 * Terminate the loop if iclogs are found in states
2296 * which will cause other threads to clean up iclogs.
2297 *
2298 * SYNCING - i/o completion will go through logs
2299 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002300 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 * IOERROR - give up hope all ye who enter here
2302 */
2303 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2304 iclog->ic_state == XLOG_STATE_SYNCING ||
2305 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2306 iclog->ic_state == XLOG_STATE_IOERROR )
2307 break;
2308 iclog = iclog->ic_next;
2309 } while (first_iclog != iclog);
2310 }
2311#endif
2312
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002313 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2314 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002315 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002316
2317 if (wake)
2318 sv_broadcast(&log->l_flush_wait);
2319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321
2322/*
2323 * Finish transitioning this iclog to the dirty state.
2324 *
2325 * Make sure that we completely execute this routine only when this is
2326 * the last call to the iclog. There is a good chance that iclog flushes,
2327 * when we reach the end of the physical log, get turned into 2 separate
2328 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2329 * routine. By using the reference count bwritecnt, we guarantee that only
2330 * the second completion goes through.
2331 *
2332 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002333 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 */
David Chinnera8272ce2007-11-23 16:28:09 +11002335STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336xlog_state_done_syncing(
2337 xlog_in_core_t *iclog,
2338 int aborted)
2339{
2340 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002342 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2345 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002346 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2348
2349
2350 /*
2351 * If we got an error, either on the first buffer, or in the case of
2352 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2353 * and none should ever be attempted to be written to disk
2354 * again.
2355 */
2356 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2357 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002358 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return;
2360 }
2361 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2362 }
2363
2364 /*
2365 * Someone could be sleeping prior to writing out the next
2366 * iclog buffer, we wake them all, one will get to do the
2367 * I/O, the others get to wait for the result.
2368 */
David Chinner12017fa2008-08-13 16:34:31 +10002369 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002370 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2372} /* xlog_state_done_syncing */
2373
2374
2375/*
2376 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002377 * sleep. We wait on the flush queue on the head iclog as that should be
2378 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2379 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 *
2381 * The in-core logs are used in a circular fashion. They are not used
2382 * out-of-order even when an iclog past the head is free.
2383 *
2384 * return:
2385 * * log_offset where xlog_write() can start writing into the in-core
2386 * log's data space.
2387 * * in-core log pointer to which xlog_write() should write.
2388 * * boolean indicating this is a continued write to an in-core log.
2389 * If this is the last write, then the in-core log's offset field
2390 * needs to be incremented, depending on the amount of data which
2391 * is copied.
2392 */
David Chinnera8272ce2007-11-23 16:28:09 +11002393STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394xlog_state_get_iclog_space(xlog_t *log,
2395 int len,
2396 xlog_in_core_t **iclogp,
2397 xlog_ticket_t *ticket,
2398 int *continued_write,
2399 int *logoffsetp)
2400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int log_offset;
2402 xlog_rec_header_t *head;
2403 xlog_in_core_t *iclog;
2404 int error;
2405
2406restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002407 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002409 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return XFS_ERROR(EIO);
2411 }
2412
2413 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002414 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002416
2417 /* Wait for log writes to have flushed */
2418 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 goto restart;
2420 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 head = &iclog->ic_header;
2423
David Chinner155cc6b2008-03-06 13:44:14 +11002424 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 log_offset = iclog->ic_offset;
2426
2427 /* On the 1st write to an iclog, figure out lsn. This works
2428 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2429 * committing to. If the offset is set, that's how many blocks
2430 * must be written.
2431 */
2432 if (log_offset == 0) {
2433 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002434 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002435 log->l_iclog_hsize,
2436 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002437 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2438 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002439 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 ASSERT(log->l_curr_block >= 0);
2441 }
2442
2443 /* If there is enough room to write everything, then do it. Otherwise,
2444 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2445 * bit is on, so this will get flushed out. Don't update ic_offset
2446 * until you know exactly how many bytes get copied. Therefore, wait
2447 * until later to update ic_offset.
2448 *
2449 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2450 * can fit into remaining data section.
2451 */
2452 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2453 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2454
Dave Chinner49641f12008-07-11 17:43:55 +10002455 /*
2456 * If I'm the only one writing to this iclog, sync it to disk.
2457 * We need to do an atomic compare and decrement here to avoid
2458 * racing with concurrent atomic_dec_and_lock() calls in
2459 * xlog_state_release_iclog() when there is more than one
2460 * reference to the iclog.
2461 */
2462 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2463 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002464 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002465 error = xlog_state_release_iclog(log, iclog);
2466 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002467 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002469 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471 goto restart;
2472 }
2473
2474 /* Do we have enough room to write the full amount in the remainder
2475 * of this iclog? Or must we continue a write on the next iclog and
2476 * mark this iclog as completely taken? In the case where we switch
2477 * iclogs (to mark it taken), this particular iclog will release/sync
2478 * to disk in xlog_write().
2479 */
2480 if (len <= iclog->ic_size - iclog->ic_offset) {
2481 *continued_write = 0;
2482 iclog->ic_offset += len;
2483 } else {
2484 *continued_write = 1;
2485 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2486 }
2487 *iclogp = iclog;
2488
2489 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002490 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 *logoffsetp = log_offset;
2493 return 0;
2494} /* xlog_state_get_iclog_space */
2495
2496/*
2497 * Atomically get the log space required for a log ticket.
2498 *
2499 * Once a ticket gets put onto the reserveq, it will only return after
2500 * the needed reservation is satisfied.
2501 */
2502STATIC int
2503xlog_grant_log_space(xlog_t *log,
2504 xlog_ticket_t *tic)
2505{
2506 int free_bytes;
2507 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509#ifdef DEBUG
2510 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2511 panic("grant Recovery problem");
2512#endif
2513
2514 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002515 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002516
2517 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /* something is already sleeping; insert new transaction at end */
Dave Chinner10547942010-12-21 12:02:25 +11002520 if (!list_empty(&log->l_reserveq)) {
2521 list_add_tail(&tic->t_queue, &log->l_reserveq);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002522
2523 trace_xfs_log_grant_sleep1(log, tic);
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * Gotta check this before going to sleep, while we're
2527 * holding the grant lock.
2528 */
2529 if (XLOG_FORCED_SHUTDOWN(log))
2530 goto error_return;
2531
2532 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002533 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * If we got an error, and the filesystem is shutting down,
2536 * we'll catch it down below. So just continue...
2537 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002538 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002539 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2542 need_bytes = tic->t_unit_res*tic->t_ocnt;
2543 else
2544 need_bytes = tic->t_unit_res;
2545
2546redo:
2547 if (XLOG_FORCED_SHUTDOWN(log))
2548 goto error_return;
2549
2550 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2551 log->l_grant_reserve_bytes);
2552 if (free_bytes < need_bytes) {
Dave Chinner10547942010-12-21 12:02:25 +11002553 if (list_empty(&tic->t_queue))
2554 list_add_tail(&tic->t_queue, &log->l_reserveq);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002555
2556 trace_xfs_log_grant_sleep2(log, tic);
2557
Dave Chinner9d7fef72009-04-06 18:42:59 +02002558 spin_unlock(&log->l_grant_lock);
2559 xlog_grant_push_ail(log->l_mp, need_bytes);
2560 spin_lock(&log->l_grant_lock);
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002563 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Dave Chinner9d7fef72009-04-06 18:42:59 +02002565 spin_lock(&log->l_grant_lock);
2566 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002569 trace_xfs_log_grant_wake2(log, tic);
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 goto redo;
Dave Chinner10547942010-12-21 12:02:25 +11002572 }
2573
2574 list_del_init(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002577 xlog_grant_add_space(log, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002578 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 xlog_verify_grant_head(log, 1);
Dave Chinner3f336c62010-12-21 12:02:52 +11002580 xlog_verify_grant_tail(log);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002581 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return 0;
2583
2584 error_return:
Dave Chinner10547942010-12-21 12:02:25 +11002585 list_del_init(&tic->t_queue);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002586 trace_xfs_log_grant_error(log, tic);
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 /*
2589 * If we are failing, make sure the ticket doesn't have any
2590 * current reservations. We don't want to add this back when
2591 * the ticket/transaction gets cancelled.
2592 */
2593 tic->t_curr_res = 0;
2594 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002595 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return XFS_ERROR(EIO);
2597} /* xlog_grant_log_space */
2598
2599
2600/*
2601 * Replenish the byte reservation required by moving the grant write head.
2602 *
2603 *
2604 */
2605STATIC int
2606xlog_regrant_write_log_space(xlog_t *log,
2607 xlog_ticket_t *tic)
2608{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 int free_bytes, need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002612 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617#ifdef DEBUG
2618 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2619 panic("regrant Recovery problem");
2620#endif
2621
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002622 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002623
2624 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 if (XLOG_FORCED_SHUTDOWN(log))
2627 goto error_return;
2628
2629 /* If there are other waiters on the queue then give them a
2630 * chance at logspace before us. Wake up the first waiters,
2631 * if we do not wake up all the waiters then go to sleep waiting
2632 * for more free space, otherwise try to get some space for
2633 * this transaction.
2634 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002635 need_bytes = tic->t_unit_res;
Dave Chinner10547942010-12-21 12:02:25 +11002636 if (!list_empty(&log->l_writeq)) {
2637 struct xlog_ticket *ntic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2639 log->l_grant_write_bytes);
Dave Chinner10547942010-12-21 12:02:25 +11002640 list_for_each_entry(ntic, &log->l_writeq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2642
2643 if (free_bytes < ntic->t_unit_res)
2644 break;
2645 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002646 sv_signal(&ntic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +11002647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Dave Chinner10547942010-12-21 12:02:25 +11002649 if (ntic != list_first_entry(&log->l_writeq,
2650 struct xlog_ticket, t_queue)) {
2651 if (list_empty(&tic->t_queue))
2652 list_add_tail(&tic->t_queue, &log->l_writeq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002654 trace_xfs_log_regrant_write_sleep1(log, tic);
2655
Dave Chinner9d7fef72009-04-06 18:42:59 +02002656 spin_unlock(&log->l_grant_lock);
2657 xlog_grant_push_ail(log->l_mp, need_bytes);
2658 spin_lock(&log->l_grant_lock);
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002661 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 &log->l_grant_lock, s);
2663
2664 /* If we're shutting down, this tic is already
2665 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002666 spin_lock(&log->l_grant_lock);
2667 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002670 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
2672 }
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674redo:
2675 if (XLOG_FORCED_SHUTDOWN(log))
2676 goto error_return;
2677
2678 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2679 log->l_grant_write_bytes);
2680 if (free_bytes < need_bytes) {
Dave Chinner10547942010-12-21 12:02:25 +11002681 if (list_empty(&tic->t_queue))
2682 list_add_tail(&tic->t_queue, &log->l_writeq);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002683 spin_unlock(&log->l_grant_lock);
2684 xlog_grant_push_ail(log->l_mp, need_bytes);
2685 spin_lock(&log->l_grant_lock);
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002688 trace_xfs_log_regrant_write_sleep2(log, tic);
2689
David Chinner12017fa2008-08-13 16:34:31 +10002690 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002693 spin_lock(&log->l_grant_lock);
2694 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002697 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 goto redo;
Dave Chinner10547942010-12-21 12:02:25 +11002699 }
2700
2701 list_del_init(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002703 /* we've got enough space */
2704 xlog_grant_add_space_write(log, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002705 trace_xfs_log_regrant_write_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 xlog_verify_grant_head(log, 1);
Dave Chinner3f336c62010-12-21 12:02:52 +11002707 xlog_verify_grant_tail(log);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002708 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711
2712 error_return:
Dave Chinner10547942010-12-21 12:02:25 +11002713 list_del_init(&tic->t_queue);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002714 trace_xfs_log_regrant_write_error(log, tic);
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /*
2717 * If we are failing, make sure the ticket doesn't have any
2718 * current reservations. We don't want to add this back when
2719 * the ticket/transaction gets cancelled.
2720 */
2721 tic->t_curr_res = 0;
2722 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002723 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return XFS_ERROR(EIO);
2725} /* xlog_regrant_write_log_space */
2726
2727
2728/* The first cnt-1 times through here we don't need to
2729 * move the grant write head because the permanent
2730 * reservation has reserved cnt times the unit amount.
2731 * Release part of current permanent unit reservation and
2732 * reset current reservation to be one units worth. Also
2733 * move grant reservation head forward.
2734 */
2735STATIC void
2736xlog_regrant_reserve_log_space(xlog_t *log,
2737 xlog_ticket_t *ticket)
2738{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002739 trace_xfs_log_regrant_reserve_enter(log, ticket);
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (ticket->t_cnt > 0)
2742 ticket->t_cnt--;
2743
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002744 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002745 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002747 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002748
2749 trace_xfs_log_regrant_reserve_sub(log, ticket);
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 xlog_verify_grant_head(log, 1);
2752
2753 /* just return if we still have some of the pre-reserved space */
2754 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002755 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return;
2757 }
2758
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002759 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002760
2761 trace_xfs_log_regrant_reserve_exit(log, ticket);
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002764 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002766 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767} /* xlog_regrant_reserve_log_space */
2768
2769
2770/*
2771 * Give back the space left from a reservation.
2772 *
2773 * All the information we need to make a correct determination of space left
2774 * is present. For non-permanent reservations, things are quite easy. The
2775 * count should have been decremented to zero. We only need to deal with the
2776 * space remaining in the current reservation part of the ticket. If the
2777 * ticket contains a permanent reservation, there may be left over space which
2778 * needs to be released. A count of N means that N-1 refills of the current
2779 * reservation can be done before we need to ask for more space. The first
2780 * one goes to fill up the first current reservation. Once we run out of
2781 * space, the count will stay at zero and the only space remaining will be
2782 * in the current reservation field.
2783 */
2784STATIC void
2785xlog_ungrant_log_space(xlog_t *log,
2786 xlog_ticket_t *ticket)
2787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (ticket->t_cnt > 0)
2789 ticket->t_cnt--;
2790
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002791 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002792 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002794 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002796 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 /* If this is a permanent reservation ticket, we may be able to free
2799 * up more space based on the remaining count.
2800 */
2801 if (ticket->t_cnt > 0) {
2802 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002803 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002806 trace_xfs_log_ungrant_exit(log, ticket);
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002809 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 xfs_log_move_tail(log->l_mp, 1);
2811} /* xlog_ungrant_log_space */
2812
2813
2814/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 * Flush iclog to disk if this is the last reference to the given iclog and
2816 * the WANT_SYNC bit is set.
2817 *
2818 * When this function is entered, the iclog is not necessarily in the
2819 * WANT_SYNC state. It may be sitting around waiting to get filled.
2820 *
2821 *
2822 */
David Chinnera8272ce2007-11-23 16:28:09 +11002823STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002824xlog_state_release_iclog(
2825 xlog_t *log,
2826 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 int sync = 0; /* do we sync? */
2829
David Chinner155cc6b2008-03-06 13:44:14 +11002830 if (iclog->ic_state & XLOG_STATE_IOERROR)
2831 return XFS_ERROR(EIO);
2832
2833 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2834 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2835 return 0;
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002838 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return XFS_ERROR(EIO);
2840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2842 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2843
David Chinner155cc6b2008-03-06 13:44:14 +11002844 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002845 /* update tail before writing to iclog */
2846 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 sync++;
2848 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002849 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2851 /* cycle incremented when incrementing curr_block */
2852 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002853 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855 /*
2856 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002857 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2859 * this iclog has consistent data, so we ignore IOERROR
2860 * flags after this point.
2861 */
David Chinnerb5893342008-03-06 13:44:06 +11002862 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865} /* xlog_state_release_iclog */
2866
2867
2868/*
2869 * This routine will mark the current iclog in the ring as WANT_SYNC
2870 * and move the current iclog pointer to the next iclog in the ring.
2871 * When this routine is called from xlog_state_get_iclog_space(), the
2872 * exact size of the iclog has not yet been determined. All we know is
2873 * that every data block. We have run out of space in this log record.
2874 */
2875STATIC void
2876xlog_state_switch_iclogs(xlog_t *log,
2877 xlog_in_core_t *iclog,
2878 int eventual_size)
2879{
2880 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2881 if (!eventual_size)
2882 eventual_size = iclog->ic_offset;
2883 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002884 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 log->l_prev_block = log->l_curr_block;
2886 log->l_prev_cycle = log->l_curr_cycle;
2887
2888 /* roll log?: ic_offset changed later */
2889 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2890
2891 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002892 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 log->l_mp->m_sb.sb_logsunit > 1) {
2894 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2895 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2896 }
2897
2898 if (log->l_curr_block >= log->l_logBBsize) {
2899 log->l_curr_cycle++;
2900 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2901 log->l_curr_cycle++;
2902 log->l_curr_block -= log->l_logBBsize;
2903 ASSERT(log->l_curr_block >= 0);
2904 }
2905 ASSERT(iclog == log->l_iclog);
2906 log->l_iclog = iclog->ic_next;
2907} /* xlog_state_switch_iclogs */
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909/*
2910 * Write out all data in the in-core log as of this exact moment in time.
2911 *
2912 * Data may be written to the in-core log during this call. However,
2913 * we don't guarantee this data will be written out. A change from past
2914 * implementation means this routine will *not* write out zero length LRs.
2915 *
2916 * Basically, we try and perform an intelligent scan of the in-core logs.
2917 * If we determine there is no flushable data, we just return. There is no
2918 * flushable data if:
2919 *
2920 * 1. the current iclog is active and has no data; the previous iclog
2921 * is in the active or dirty state.
2922 * 2. the current iclog is drity, and the previous iclog is in the
2923 * active or dirty state.
2924 *
David Chinner12017fa2008-08-13 16:34:31 +10002925 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 *
2927 * 1. the current iclog is not in the active nor dirty state.
2928 * 2. the current iclog dirty, and the previous iclog is not in the
2929 * active nor dirty state.
2930 * 3. the current iclog is active, and there is another thread writing
2931 * to this particular iclog.
2932 * 4. a) the current iclog is active and has no other writers
2933 * b) when we return from flushing out this iclog, it is still
2934 * not in the active nor dirty state.
2935 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002936int
2937_xfs_log_force(
2938 struct xfs_mount *mp,
2939 uint flags,
2940 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002942 struct log *log = mp->m_log;
2943 struct xlog_in_core *iclog;
2944 xfs_lsn_t lsn;
2945
2946 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Dave Chinnera44f13e2010-08-24 11:40:03 +10002948 if (log->l_cilp)
2949 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002950
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002951 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 iclog = log->l_iclog;
2954 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002955 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return XFS_ERROR(EIO);
2957 }
2958
2959 /* If the head iclog is not active nor dirty, we just attach
2960 * ourselves to the head and go to sleep.
2961 */
2962 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2963 iclog->ic_state == XLOG_STATE_DIRTY) {
2964 /*
2965 * If the head is dirty or (active and empty), then
2966 * we need to look at the previous iclog. If the previous
2967 * iclog is active or dirty we are done. There is nothing
2968 * to sync out. Otherwise, we attach ourselves to the
2969 * previous iclog and go to sleep.
2970 */
2971 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002972 (atomic_read(&iclog->ic_refcnt) == 0
2973 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 iclog = iclog->ic_prev;
2975 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2976 iclog->ic_state == XLOG_STATE_DIRTY)
2977 goto no_sleep;
2978 else
2979 goto maybe_sleep;
2980 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002981 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 /* We are the only one with access to this
2983 * iclog. Flush it out now. There should
2984 * be a roundoff of zero to show that someone
2985 * has already taken care of the roundoff from
2986 * the previous sync.
2987 */
David Chinner155cc6b2008-03-06 13:44:14 +11002988 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002989 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002991 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 if (xlog_state_release_iclog(log, iclog))
2994 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002995
2996 if (log_flushed)
2997 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002998 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002999 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 iclog->ic_state != XLOG_STATE_DIRTY)
3001 goto maybe_sleep;
3002 else
3003 goto no_sleep;
3004 } else {
3005 /* Someone else is writing to this iclog.
3006 * Use its call to flush out the data. However,
3007 * the other thread may not force out this LR,
3008 * so we mark it WANT_SYNC.
3009 */
3010 xlog_state_switch_iclogs(log, iclog, 0);
3011 goto maybe_sleep;
3012 }
3013 }
3014 }
3015
3016 /* By the time we come around again, the iclog could've been filled
3017 * which would give it another lsn. If we have a new lsn, just
3018 * return because the relevant data has been flushed.
3019 */
3020maybe_sleep:
3021 if (flags & XFS_LOG_SYNC) {
3022 /*
3023 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003024 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 * Then we check again after waking up, in case our
3026 * sleep was disturbed by a bad news.
3027 */
3028 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003029 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return XFS_ERROR(EIO);
3031 }
3032 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10003033 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /*
3035 * No need to grab the log lock here since we're
3036 * only deciding whether or not to return EIO
3037 * and the memory read should be atomic.
3038 */
3039 if (iclog->ic_state & XLOG_STATE_IOERROR)
3040 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003041 if (log_flushed)
3042 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 } else {
3044
3045no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003046 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003052 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
3053 * about errors or whether the log was flushed or not. This is the normal
3054 * interface to use when trying to unpin items or move the log forward.
3055 */
3056void
3057xfs_log_force(
3058 xfs_mount_t *mp,
3059 uint flags)
3060{
3061 int error;
3062
3063 error = _xfs_log_force(mp, flags, NULL);
3064 if (error) {
3065 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3066 "error %d returned.", error);
3067 }
3068}
3069
3070/*
3071 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 *
3073 * Find in-core log with lsn.
3074 * If it is in the DIRTY state, just return.
3075 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3076 * state and go to sleep or return.
3077 * If it is in any other state, go to sleep or return.
3078 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003079 * Synchronous forces are implemented with a signal variable. All callers
3080 * to force a given lsn to disk will wait on a the sv attached to the
3081 * specific in-core log. When given in-core log finally completes its
3082 * write to disk, that thread will wake up all threads waiting on the
3083 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003085int
3086_xfs_log_force_lsn(
3087 struct xfs_mount *mp,
3088 xfs_lsn_t lsn,
3089 uint flags,
3090 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003092 struct log *log = mp->m_log;
3093 struct xlog_in_core *iclog;
3094 int already_slept = 0;
3095
3096 ASSERT(lsn != 0);
3097
3098 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Dave Chinner71e330b2010-05-21 14:37:18 +10003100 if (log->l_cilp) {
Dave Chinnera44f13e2010-08-24 11:40:03 +10003101 lsn = xlog_cil_force_lsn(log, lsn);
Dave Chinner71e330b2010-05-21 14:37:18 +10003102 if (lsn == NULLCOMMITLSN)
3103 return 0;
3104 }
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003107 spin_lock(&log->l_icloglock);
3108 iclog = log->l_iclog;
3109 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003110 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003111 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003114 do {
3115 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3116 iclog = iclog->ic_next;
3117 continue;
3118 }
3119
3120 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3121 spin_unlock(&log->l_icloglock);
3122 return 0;
3123 }
3124
3125 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3126 /*
3127 * We sleep here if we haven't already slept (e.g.
3128 * this is the first time we've looked at the correct
3129 * iclog buf) and the buffer before us is going to
3130 * be sync'ed. The reason for this is that if we
3131 * are doing sync transactions here, by waiting for
3132 * the previous I/O to complete, we can allow a few
3133 * more transactions into this iclog before we close
3134 * it down.
3135 *
3136 * Otherwise, we mark the buffer WANT_SYNC, and bump
3137 * up the refcnt so we can release the log (which
3138 * drops the ref count). The state switch keeps new
3139 * transaction commits from using this buffer. When
3140 * the current commits finish writing into the buffer,
3141 * the refcount will drop to zero and the buffer will
3142 * go out then.
3143 */
3144 if (!already_slept &&
3145 (iclog->ic_prev->ic_state &
3146 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3147 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3148
3149 XFS_STATS_INC(xs_log_force_sleep);
3150
3151 sv_wait(&iclog->ic_prev->ic_write_wait,
3152 PSWP, &log->l_icloglock, s);
3153 if (log_flushed)
3154 *log_flushed = 1;
3155 already_slept = 1;
3156 goto try_again;
3157 }
David Chinner155cc6b2008-03-06 13:44:14 +11003158 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003160 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (xlog_state_release_iclog(log, iclog))
3162 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003163 if (log_flushed)
3164 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003165 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003168 if ((flags & XFS_LOG_SYNC) && /* sleep */
3169 !(iclog->ic_state &
3170 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3171 /*
3172 * Don't wait on completion if we know that we've
3173 * gotten a log write error.
3174 */
3175 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3176 spin_unlock(&log->l_icloglock);
3177 return XFS_ERROR(EIO);
3178 }
3179 XFS_STATS_INC(xs_log_force_sleep);
3180 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3181 /*
3182 * No need to grab the log lock here since we're
3183 * only deciding whether or not to return EIO
3184 * and the memory read should be atomic.
3185 */
3186 if (iclog->ic_state & XLOG_STATE_IOERROR)
3187 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003189 if (log_flushed)
3190 *log_flushed = 1;
3191 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003192 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003194
3195 return 0;
3196 } while (iclog != log->l_iclog);
3197
3198 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003200}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003202/*
3203 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3204 * about errors or whether the log was flushed or not. This is the normal
3205 * interface to use when trying to unpin items or move the log forward.
3206 */
3207void
3208xfs_log_force_lsn(
3209 xfs_mount_t *mp,
3210 xfs_lsn_t lsn,
3211 uint flags)
3212{
3213 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003215 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3216 if (error) {
3217 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3218 "error %d returned.", error);
3219 }
3220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222/*
3223 * Called when we want to mark the current iclog as being ready to sync to
3224 * disk.
3225 */
David Chinnera8272ce2007-11-23 16:28:09 +11003226STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3228{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003229 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3232 xlog_state_switch_iclogs(log, iclog, 0);
3233 } else {
3234 ASSERT(iclog->ic_state &
3235 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3236 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239
3240/*****************************************************************************
3241 *
3242 * TICKET functions
3243 *
3244 *****************************************************************************
3245 */
3246
3247/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003248 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003250void
3251xfs_log_ticket_put(
3252 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003254 ASSERT(atomic_read(&ticket->t_ref) > 0);
3255 if (atomic_dec_and_test(&ticket->t_ref)) {
3256 sv_destroy(&ticket->t_wait);
3257 kmem_zone_free(xfs_log_ticket_zone, ticket);
3258 }
3259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003261xlog_ticket_t *
3262xfs_log_ticket_get(
3263 xlog_ticket_t *ticket)
3264{
3265 ASSERT(atomic_read(&ticket->t_ref) > 0);
3266 atomic_inc(&ticket->t_ref);
3267 return ticket;
3268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Dave Chinner955833c2010-05-14 21:41:46 +10003270xlog_tid_t
3271xfs_log_get_trans_ident(
3272 struct xfs_trans *tp)
3273{
3274 return tp->t_ticket->t_tid;
3275}
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003278 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003280xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003281xlog_ticket_alloc(
3282 struct log *log,
3283 int unit_bytes,
3284 int cnt,
3285 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003286 uint xflags,
3287 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003289 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003291 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Dave Chinner3383ca52010-05-07 11:04:17 +10003293 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003294 if (!tic)
3295 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 /*
3298 * Permanent reservations have up to 'cnt'-1 active log operations
3299 * in the log. A unit in this case is the amount of space for one
3300 * of these log operations. Normal reservations have a cnt of 1
3301 * and their unit amount is the total amount of space required.
3302 *
3303 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003304 * which occupy space in the on-disk log.
3305 *
3306 * Normal form of a transaction is:
3307 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3308 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3309 *
3310 * We need to account for all the leadup data and trailer data
3311 * around the transaction data.
3312 * And then we need to account for the worst case in terms of using
3313 * more space.
3314 * The worst case will happen if:
3315 * - the placement of the transaction happens to be such that the
3316 * roundoff is at its maximum
3317 * - the transaction data is synced before the commit record is synced
3318 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3319 * Therefore the commit record is in its own Log Record.
3320 * This can happen as the commit record is called with its
3321 * own region to xlog_write().
3322 * This then means that in the worst case, roundoff can happen for
3323 * the commit-rec as well.
3324 * The commit-rec is smaller than padding in this scenario and so it is
3325 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 */
3327
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003328 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003330 unit_bytes += sizeof(xfs_trans_header_t);
3331
3332 /* for start-rec */
3333 unit_bytes += sizeof(xlog_op_header_t);
3334
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003335 /*
3336 * for LR headers - the space for data in an iclog is the size minus
3337 * the space used for the headers. If we use the iclog size, then we
3338 * undercalculate the number of headers required.
3339 *
3340 * Furthermore - the addition of op headers for split-recs might
3341 * increase the space required enough to require more log and op
3342 * headers, so take that into account too.
3343 *
3344 * IMPORTANT: This reservation makes the assumption that if this
3345 * transaction is the first in an iclog and hence has the LR headers
3346 * accounted to it, then the remaining space in the iclog is
3347 * exclusively for this transaction. i.e. if the transaction is larger
3348 * than the iclog, it will be the only thing in that iclog.
3349 * Fundamentally, this means we must pass the entire log vector to
3350 * xlog_write to guarantee this.
3351 */
3352 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3353 num_headers = howmany(unit_bytes, iclog_space);
3354
3355 /* for split-recs - ophdrs added when data split over LRs */
3356 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3357
3358 /* add extra header reservations if we overrun */
3359 while (!num_headers ||
3360 howmany(unit_bytes, iclog_space) > num_headers) {
3361 unit_bytes += sizeof(xlog_op_header_t);
3362 num_headers++;
3363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 unit_bytes += log->l_iclog_hsize * num_headers;
3365
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003366 /* for commit-rec LR header - note: padding will subsume the ophdr */
3367 unit_bytes += log->l_iclog_hsize;
3368
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003369 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003370 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003371 log->l_mp->m_sb.sb_logsunit > 1) {
3372 /* log su roundoff */
3373 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3374 } else {
3375 /* BB roundoff */
3376 unit_bytes += 2*BBSIZE;
3377 }
3378
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003379 atomic_set(&tic->t_ref, 1);
Dave Chinner10547942010-12-21 12:02:25 +11003380 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 tic->t_unit_res = unit_bytes;
3382 tic->t_curr_res = unit_bytes;
3383 tic->t_cnt = cnt;
3384 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003385 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 tic->t_clientid = client;
3387 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003388 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (xflags & XFS_LOG_PERM_RESERV)
3390 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003391 sv_init(&tic->t_wait, SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Christoph Hellwig0adba532007-08-30 17:21:46 +10003393 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398
3399/******************************************************************************
3400 *
3401 * Log debug routines
3402 *
3403 ******************************************************************************
3404 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003405#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406/*
3407 * Make sure that the destination ptr is within the valid data region of
3408 * one of the iclogs. This uses backup pointers stored in a different
3409 * part of the log in case we trash the log structure.
3410 */
3411void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003412xlog_verify_dest_ptr(
3413 struct log *log,
3414 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
3416 int i;
3417 int good_ptr = 0;
3418
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003419 for (i = 0; i < log->l_iclog_bufs; i++) {
3420 if (ptr >= log->l_iclog_bak[i] &&
3421 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 good_ptr++;
3423 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003424
3425 if (!good_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003427}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
3429STATIC void
3430xlog_verify_grant_head(xlog_t *log, int equals)
3431{
3432 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3433 if (equals)
3434 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3435 else
3436 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3437 } else {
3438 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3439 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3440 }
3441} /* xlog_verify_grant_head */
3442
Dave Chinner3f336c62010-12-21 12:02:52 +11003443STATIC void
3444xlog_verify_grant_tail(
3445 struct log *log)
3446{
3447 xfs_lsn_t tail_lsn = log->l_tail_lsn;
3448
3449 /*
3450 * Check to make sure the grant write head didn't just over lap the
3451 * tail. If the cycles are the same, we can't be overlapping.
3452 * Otherwise, make sure that the cycles differ by exactly one and
3453 * check the byte count.
3454 */
3455 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
3456 ASSERT(log->l_grant_write_cycle - 1 == CYCLE_LSN(tail_lsn));
3457 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
3458 }
3459}
3460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461/* check if it will fit */
3462STATIC void
3463xlog_verify_tail_lsn(xlog_t *log,
3464 xlog_in_core_t *iclog,
3465 xfs_lsn_t tail_lsn)
3466{
3467 int blocks;
3468
3469 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3470 blocks =
3471 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3472 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3473 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3474 } else {
3475 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3476
3477 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3478 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3479
3480 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3481 if (blocks < BTOBB(iclog->ic_offset) + 1)
3482 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3483 }
3484} /* xlog_verify_tail_lsn */
3485
3486/*
3487 * Perform a number of checks on the iclog before writing to disk.
3488 *
3489 * 1. Make sure the iclogs are still circular
3490 * 2. Make sure we have a good magic number
3491 * 3. Make sure we don't have magic numbers in the data
3492 * 4. Check fields of each log operation header for:
3493 * A. Valid client identifier
3494 * B. tid ptr value falls in valid ptr space (user space code)
3495 * C. Length in log record header is correct according to the
3496 * individual operation headers within record.
3497 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3498 * log, check the preceding blocks of the physical log to make sure all
3499 * the cycle numbers agree with the current cycle number.
3500 */
3501STATIC void
3502xlog_verify_iclog(xlog_t *log,
3503 xlog_in_core_t *iclog,
3504 int count,
3505 boolean_t syncing)
3506{
3507 xlog_op_header_t *ophead;
3508 xlog_in_core_t *icptr;
3509 xlog_in_core_2_t *xhdr;
3510 xfs_caddr_t ptr;
3511 xfs_caddr_t base_ptr;
3512 __psint_t field_offset;
3513 __uint8_t clientid;
3514 int len, i, j, k, op_len;
3515 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003518 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 icptr = log->l_iclog;
3520 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003521 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 xlog_panic("xlog_verify_iclog: invalid ptr");
3523 icptr = icptr->ic_next;
3524 }
3525 if (icptr != log->l_iclog)
3526 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003527 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003530 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 xlog_panic("xlog_verify_iclog: invalid magic num");
3532
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003533 ptr = (xfs_caddr_t) &iclog->ic_header;
3534 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003536 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 xlog_panic("xlog_verify_iclog: unexpected magic num");
3538 }
3539
3540 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003541 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 ptr = iclog->ic_datap;
3543 base_ptr = ptr;
3544 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003545 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 for (i = 0; i < len; i++) {
3547 ophead = (xlog_op_header_t *)ptr;
3548
3549 /* clientid is only 1 byte */
3550 field_offset = (__psint_t)
3551 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3552 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3553 clientid = ophead->oh_clientid;
3554 } else {
3555 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3556 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3557 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3558 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003559 clientid = xlog_get_client_id(
3560 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003562 clientid = xlog_get_client_id(
3563 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565 }
3566 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003567 cmn_err(CE_WARN, "xlog_verify_iclog: "
3568 "invalid clientid %d op 0x%p offset 0x%lx",
3569 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
3571 /* check length */
3572 field_offset = (__psint_t)
3573 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3574 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003575 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 } else {
3577 idx = BTOBBT((__psint_t)&ophead->oh_len -
3578 (__psint_t)iclog->ic_datap);
3579 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3580 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3581 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003582 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003584 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586 }
3587 ptr += sizeof(xlog_op_header_t) + op_len;
3588 }
3589} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003593 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 */
3595STATIC int
3596xlog_state_ioerror(
3597 xlog_t *log)
3598{
3599 xlog_in_core_t *iclog, *ic;
3600
3601 iclog = log->l_iclog;
3602 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3603 /*
3604 * Mark all the incore logs IOERROR.
3605 * From now on, no log flushes will result.
3606 */
3607 ic = iclog;
3608 do {
3609 ic->ic_state = XLOG_STATE_IOERROR;
3610 ic = ic->ic_next;
3611 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 }
3614 /*
3615 * Return non-zero, if state transition has already happened.
3616 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003617 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
3620/*
3621 * This is called from xfs_force_shutdown, when we're forcibly
3622 * shutting down the filesystem, typically because of an IO error.
3623 * Our main objectives here are to make sure that:
3624 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3625 * parties to find out, 'atomically'.
3626 * b. those who're sleeping on log reservations, pinned objects and
3627 * other resources get woken up, and be told the bad news.
3628 * c. nothing new gets queued up after (a) and (b) are done.
3629 * d. if !logerror, flush the iclogs to disk, then seal them off
3630 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003631 *
3632 * Note: for delayed logging the !logerror case needs to flush the regions
3633 * held in memory out to the iclogs before flushing them to disk. This needs
3634 * to be done before the log is marked as shutdown, otherwise the flush to the
3635 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 */
3637int
3638xfs_log_force_umount(
3639 struct xfs_mount *mp,
3640 int logerror)
3641{
3642 xlog_ticket_t *tic;
3643 xlog_t *log;
3644 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646 log = mp->m_log;
3647
3648 /*
3649 * If this happens during log recovery, don't worry about
3650 * locking; the log isn't open for business yet.
3651 */
3652 if (!log ||
3653 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3654 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003655 if (mp->m_sb_bp)
3656 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659
3660 /*
3661 * Somebody could've already done the hard work for us.
3662 * No need to get locks for this.
3663 */
3664 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3665 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003666 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
3668 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003669
3670 /*
3671 * Flush the in memory commit item list before marking the log as
3672 * being shut down. We need to do it in this order to ensure all the
3673 * completed transactions are flushed to disk with the xfs_log_force()
3674 * call below.
3675 */
3676 if (!logerror && (mp->m_flags & XFS_MOUNT_DELAYLOG))
Dave Chinnera44f13e2010-08-24 11:40:03 +10003677 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 /*
3680 * We must hold both the GRANT lock and the LOG lock,
3681 * before we mark the filesystem SHUTDOWN and wake
3682 * everybody up to tell the bad news.
3683 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003684 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003685 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003687 if (mp->m_sb_bp)
3688 XFS_BUF_DONE(mp->m_sb_bp);
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 /*
3691 * This flag is sort of redundant because of the mount flag, but
3692 * it's good to maintain the separation between the log and the rest
3693 * of XFS.
3694 */
3695 log->l_flags |= XLOG_IO_ERROR;
3696
3697 /*
3698 * If we hit a log error, we want to mark all the iclogs IOERROR
3699 * while we're still holding the loglock.
3700 */
3701 if (logerror)
3702 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003703 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
3705 /*
Dave Chinner10547942010-12-21 12:02:25 +11003706 * We don't want anybody waiting for log reservations after this. That
3707 * means we have to wake up everybody queued up on reserveq as well as
3708 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3709 * we don't enqueue anything once the SHUTDOWN flag is set, and this
3710 * action is protected by the GRANTLOCK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 */
Dave Chinner10547942010-12-21 12:02:25 +11003712 list_for_each_entry(tic, &log->l_reserveq, t_queue)
3713 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Dave Chinner10547942010-12-21 12:02:25 +11003715 list_for_each_entry(tic, &log->l_writeq, t_queue)
3716 sv_signal(&tic->t_wait);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003717 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003719 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 ASSERT(!logerror);
3721 /*
3722 * Force the incore logs to disk before shutting the
3723 * log down completely.
3724 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003725 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3726
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003727 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003729 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
3731 /*
3732 * Wake up everybody waiting on xfs_log_force.
3733 * Callback all log item committed functions as if the
3734 * log writes were completed.
3735 */
3736 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3737
3738#ifdef XFSERRORDEBUG
3739 {
3740 xlog_in_core_t *iclog;
3741
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003742 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 iclog = log->l_iclog;
3744 do {
3745 ASSERT(iclog->ic_callback == 0);
3746 iclog = iclog->ic_next;
3747 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003748 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 }
3750#endif
3751 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003752 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003755STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756xlog_iclogs_empty(xlog_t *log)
3757{
3758 xlog_in_core_t *iclog;
3759
3760 iclog = log->l_iclog;
3761 do {
3762 /* endianness does not matter here, zero is zero in
3763 * any language.
3764 */
3765 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 iclog = iclog->ic_next;
3768 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003769 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}