blob: a1d7d12fc51ff99f730f2744fd101ce006bcd958 [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);
Dave Chinnerc8a09ff2010-12-04 00:02:40 +110050STATIC int xlog_space_left(struct log *log, atomic64_t *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051STATIC 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);
Dave Chinner2ced19c2010-12-21 12:09:20 +110073STATIC void xlog_grant_push_ail(struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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
Dave Chinner663e4962010-12-21 12:06:05 +1100101xlog_grant_sub_space(
102 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100103 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100104 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100105{
Dave Chinnera69ed032010-12-21 12:08:20 +1100106 int cycle, space;
107
108 xlog_crack_grant_head(head, &cycle, &space);
109
110 space -= bytes;
111 if (space < 0) {
112 space += log->l_logsize;
113 cycle--;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100114 }
Dave Chinnera69ed032010-12-21 12:08:20 +1100115
116 xlog_assign_grant_head(head, cycle, space);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100117}
118
119static void
Dave Chinner663e4962010-12-21 12:06:05 +1100120xlog_grant_add_space(
121 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100122 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100123 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100124{
Dave Chinnera69ed032010-12-21 12:08:20 +1100125 int tmp;
126 int cycle, space;
127
128 xlog_crack_grant_head(head, &cycle, &space);
129
130 tmp = log->l_logsize - space;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000131 if (tmp > bytes)
Dave Chinnera69ed032010-12-21 12:08:20 +1100132 space += bytes;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000133 else {
Dave Chinnera69ed032010-12-21 12:08:20 +1100134 space = bytes - tmp;
135 cycle++;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100136 }
Dave Chinnera69ed032010-12-21 12:08:20 +1100137
138 xlog_assign_grant_head(head, cycle, space);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100139}
Dave Chinnera69ed032010-12-21 12:08:20 +1100140
Christoph Hellwig0adba532007-08-30 17:21:46 +1000141static void
142xlog_tic_reset_res(xlog_ticket_t *tic)
143{
144 tic->t_res_num = 0;
145 tic->t_res_arr_sum = 0;
146 tic->t_res_num_ophdrs = 0;
147}
148
149static void
150xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
151{
152 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
153 /* add to overflow and start again */
154 tic->t_res_o_flow += tic->t_res_arr_sum;
155 tic->t_res_num = 0;
156 tic->t_res_arr_sum = 0;
157 }
158
159 tic->t_res_arr[tic->t_res_num].r_len = len;
160 tic->t_res_arr[tic->t_res_num].r_type = type;
161 tic->t_res_arr_sum += len;
162 tic->t_res_num++;
163}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * NOTES:
167 *
168 * 1. currblock field gets updated at startup and after in-core logs
169 * marked as with WANT_SYNC.
170 */
171
172/*
173 * This routine is called when a user of a log manager ticket is done with
174 * the reservation. If the ticket was ever used, then a commit record for
175 * the associated transaction is written out as a log operation header with
176 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
177 * a given ticket. If the ticket was one with a permanent reservation, then
178 * a few operations are done differently. Permanent reservation tickets by
179 * default don't release the reservation. They just commit the current
180 * transaction with the belief that the reservation is still needed. A flag
181 * must be passed in before permanent reservations are actually released.
182 * When these type of tickets are not released, they need to be set into
183 * the inited state again. By doing this, a start record will be written
184 * out when the next write occurs.
185 */
186xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000187xfs_log_done(
188 struct xfs_mount *mp,
189 struct xlog_ticket *ticket,
190 struct xlog_in_core **iclog,
191 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000193 struct log *log = mp->m_log;
194 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (XLOG_FORCED_SHUTDOWN(log) ||
197 /*
198 * If nothing was ever written, don't write out commit record.
199 * If we get an error, just continue and give back the log ticket.
200 */
201 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100202 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 lsn = (xfs_lsn_t) -1;
204 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
205 flags |= XFS_LOG_REL_PERM_RESERV;
206 }
207 }
208
209
210 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
211 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000212 trace_xfs_log_done_nonperm(log, ticket);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000215 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * request has been made to release a permanent reservation.
217 */
218 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100219 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000221 trace_xfs_log_done_perm(log, ticket);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000224 /* If this ticket was a permanent reservation and we aren't
225 * trying to release it, reset the inited flags; so next time
226 * we write, a start record will be written out.
227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
235 * Attaches a new iclog I/O completion callback routine during
236 * transaction commit. If the log is in error state, a non-zero
237 * return code is handed back and the caller is responsible for
238 * executing the callback at an appropriate time.
239 */
240int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000241xfs_log_notify(
242 struct xfs_mount *mp,
243 struct xlog_in_core *iclog,
244 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000246 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David Chinner114d23a2008-04-10 12:18:39 +1000248 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
250 if (!abortflg) {
251 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
252 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
253 cb->cb_next = NULL;
254 *(iclog->ic_callback_tail) = cb;
255 iclog->ic_callback_tail = &(cb->cb_next);
256 }
David Chinner114d23a2008-04-10 12:18:39 +1000257 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000262xfs_log_release_iclog(
263 struct xfs_mount *mp,
264 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000266 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000267 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100268 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 return 0;
272}
273
274/*
275 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
276 * to the reservation.
277 * 2. Potentially, push buffers at tail of log to disk.
278 *
279 * Each reservation is going to reserve extra space for a log record header.
280 * When writes happen to the on-disk log, we don't subtract the length of the
281 * log record header from any reservation. By wasting space in each
282 * reservation, we prevent over allocation problems.
283 */
284int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000285xfs_log_reserve(
286 struct xfs_mount *mp,
287 int unit_bytes,
288 int cnt,
289 struct xlog_ticket **ticket,
290 __uint8_t client,
291 uint flags,
292 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000294 struct log *log = mp->m_log;
295 struct xlog_ticket *internal_ticket;
296 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (XLOG_FORCED_SHUTDOWN(log))
301 return XFS_ERROR(EIO);
302
303 XFS_STATS_INC(xs_try_logspace);
304
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (*ticket != NULL) {
307 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000308 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000309
Dave Chinner524ee362010-05-07 11:05:05 +1000310 /*
311 * this is a new transaction on the ticket, so we need to
312 * change the transaction ID so that the next transaction has a
313 * different TID in the log. Just add one to the existing tid
314 * so that we can see chains of rolling transactions in the log
315 * easily.
316 */
317 internal_ticket->t_tid++;
318
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000319 trace_xfs_log_reserve(log, internal_ticket);
320
Dave Chinner2ced19c2010-12-21 12:09:20 +1100321 spin_lock(&log->l_grant_lock);
322 xlog_grant_push_ail(log, internal_ticket->t_unit_res);
323 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 retval = xlog_regrant_write_log_space(log, internal_ticket);
325 } else {
326 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100327 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Dave Chinner3383ca52010-05-07 11:04:17 +1000328 client, flags,
329 KM_SLEEP|KM_MAYFAIL);
David Chinnereb01c9c2008-04-10 12:18:46 +1000330 if (!internal_ticket)
331 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000332 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000334
335 trace_xfs_log_reserve(log, internal_ticket);
336
Dave Chinner2ced19c2010-12-21 12:09:20 +1100337 spin_lock(&log->l_grant_lock);
338 xlog_grant_push_ail(log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 (internal_ticket->t_unit_res *
340 internal_ticket->t_cnt));
Dave Chinner2ced19c2010-12-21 12:09:20 +1100341 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 retval = xlog_grant_log_space(log, internal_ticket);
343 }
344
345 return retval;
346} /* xfs_log_reserve */
347
348
349/*
350 * Mount a log filesystem
351 *
352 * mp - ubiquitous xfs mount point structure
353 * log_target - buftarg of on-disk log device
354 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
355 * num_bblocks - Number of BBSIZE blocks in on-disk log
356 *
357 * Return error or zero.
358 */
359int
David Chinner249a8c12008-02-05 12:13:32 +1100360xfs_log_mount(
361 xfs_mount_t *mp,
362 xfs_buftarg_t *log_target,
363 xfs_daddr_t blk_offset,
364 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
David Chinner249a8c12008-02-05 12:13:32 +1100366 int error;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
369 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
370 else {
371 cmn_err(CE_NOTE,
372 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
373 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000374 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200378 if (IS_ERR(mp->m_log)) {
379 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100380 goto out;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
David Chinner249a8c12008-02-05 12:13:32 +1100384 * Initialize the AIL now we have a log.
385 */
David Chinner249a8c12008-02-05 12:13:32 +1100386 error = xfs_trans_ail_init(mp);
387 if (error) {
388 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100389 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100390 }
David Chinnera9c21c12008-10-30 17:39:35 +1100391 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100392
393 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * skip log recovery on a norecovery mount. pretend it all
395 * just worked.
396 */
397 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100398 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000401 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Eric Sandeen65be6052006-01-11 15:34:19 +1100403 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000406 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (error) {
408 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100409 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 }
412
413 /* Normal transactions can now occur */
414 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
415
Dave Chinner71e330b2010-05-21 14:37:18 +1000416 /*
417 * Now the log has been fully initialised and we know were our
418 * space grant counters are, we can initialise the permanent ticket
419 * needed for delayed logging to work.
420 */
421 xlog_cil_init_post_recovery(mp->m_log);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100424
425out_destroy_ail:
426 xfs_trans_ail_destroy(mp);
427out_free_log:
428 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100429out:
David Chinner249a8c12008-02-05 12:13:32 +1100430 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100431}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/*
434 * Finish the recovery of the file system. This is separate from
435 * the xfs_log_mount() call, because it depends on the code in
436 * xfs_mountfs() to read in the root and real-time bitmap inodes
437 * between calling xfs_log_mount() and here.
438 *
439 * mp - ubiquitous xfs mount point structure
440 */
441int
Christoph Hellwig42490232008-08-13 16:49:32 +1000442xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 int error;
445
446 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000447 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 else {
449 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000450 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
453 return error;
454}
455
456/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * Final log writes as part of unmount.
458 *
459 * Mark the filesystem clean as unmount happens. Note that during relocation
460 * this routine needs to be executed as part of source-bag while the
461 * deallocation must not be done until source-end.
462 */
463
464/*
465 * Unmount record used to have a string "Unmount filesystem--" in the
466 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
467 * We just write the magic number now since that particular field isn't
468 * currently architecture converted and "nUmount" is a bit foo.
469 * As far as I know, there weren't any dependencies on the old behaviour.
470 */
471
472int
473xfs_log_unmount_write(xfs_mount_t *mp)
474{
475 xlog_t *log = mp->m_log;
476 xlog_in_core_t *iclog;
477#ifdef DEBUG
478 xlog_in_core_t *first_iclog;
479#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000480 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 xfs_lsn_t lsn;
482 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Don't write out unmount record on read-only mounts.
486 * Or, if we are doing a forced umount (typically because of IO errors).
487 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000488 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 0;
490
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000491 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000492 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494#ifdef DEBUG
495 first_iclog = iclog = log->l_iclog;
496 do {
497 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
498 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
499 ASSERT(iclog->ic_offset == 0);
500 }
501 iclog = iclog->ic_next;
502 } while (iclog != first_iclog);
503#endif
504 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000505 error = xfs_log_reserve(mp, 600, 1, &tic,
506 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100508 /* the data section must be 32 bit size aligned */
509 struct {
510 __uint16_t magic;
511 __uint16_t pad1;
512 __uint32_t pad2; /* may as well make it 64 bits */
513 } magic = {
514 .magic = XLOG_UNMOUNT_TYPE,
515 };
516 struct xfs_log_iovec reg = {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000517 .i_addr = &magic,
Dave Chinner55b66332010-03-23 11:43:17 +1100518 .i_len = sizeof(magic),
519 .i_type = XLOG_REG_TYPE_UNMOUNT,
520 };
521 struct xfs_log_vec vec = {
522 .lv_niovecs = 1,
523 .lv_iovecp = &reg,
524 };
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100527 tic->t_flags = 0;
528 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 NULL, XLOG_UNMOUNT_TRANS);
530 /*
531 * At this point, we're umounting anyway,
532 * so there's no point in transitioning log state
533 * to IOERROR. Just continue...
534 */
535 }
536
537 if (error) {
538 xfs_fs_cmn_err(CE_ALERT, mp,
539 "xfs_log_unmount: unmount record failed");
540 }
541
542
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000543 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100545 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100547 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000548 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000550 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
552 iclog->ic_state == XLOG_STATE_DIRTY)) {
553 if (!XLOG_FORCED_SHUTDOWN(log)) {
Dave Chinnereb40a872010-12-21 12:09:01 +1100554 xlog_wait(&iclog->ic_force_wait,
555 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000557 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000560 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000562 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000563 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000564 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100565 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 } else {
568 /*
569 * We're already in forced_shutdown mode, couldn't
570 * even attempt to write out the unmount transaction.
571 *
572 * Go through the motions of sync'ing and releasing
573 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000574 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * be in progress. Do this as a separate section of
576 * code so we'll know if we ever get stuck here that
577 * we're in this odd situation of trying to unmount
578 * a file system that went into forced_shutdown as
579 * the result of an unmount..
580 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000581 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100583 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100586 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000587 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000589 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
592 || iclog->ic_state == XLOG_STATE_DIRTY
593 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
594
Dave Chinnereb40a872010-12-21 12:09:01 +1100595 xlog_wait(&iclog->ic_force_wait,
596 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000598 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 }
601
David Chinner1bb7d6b2008-04-10 12:24:38 +1000602 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603} /* xfs_log_unmount_write */
604
605/*
606 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100607 *
608 * We need to stop the aild from running before we destroy
609 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
611void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100612xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
David Chinner249a8c12008-02-05 12:13:32 +1100614 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000615 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Dave Chinner43f5efc2010-03-23 10:10:00 +1100618void
619xfs_log_item_init(
620 struct xfs_mount *mp,
621 struct xfs_log_item *item,
622 int type,
623 struct xfs_item_ops *ops)
624{
625 item->li_mountp = mp;
626 item->li_ailp = mp->m_ail;
627 item->li_type = type;
628 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +1000629 item->li_lv = NULL;
630
631 INIT_LIST_HEAD(&item->li_ail);
632 INIT_LIST_HEAD(&item->li_cil);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/*
636 * Write region vectors to log. The write happens using the space reservation
637 * of the ticket (tic). It is not a requirement that all writes for a given
Dave Chinner9b9fc2b72010-03-23 11:21:11 +1100638 * transaction occur with one call to xfs_log_write(). However, it is important
639 * to note that the transaction reservation code makes an assumption about the
640 * number of log headers a transaction requires that may be violated if you
641 * don't pass all the transaction vectors in one call....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000644xfs_log_write(
645 struct xfs_mount *mp,
646 struct xfs_log_iovec reg[],
647 int nentries,
648 struct xlog_ticket *tic,
649 xfs_lsn_t *start_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000651 struct log *log = mp->m_log;
652 int error;
Dave Chinner55b66332010-03-23 11:43:17 +1100653 struct xfs_log_vec vec = {
654 .lv_niovecs = nentries,
655 .lv_iovecp = reg,
656 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (XLOG_FORCED_SHUTDOWN(log))
659 return XFS_ERROR(EIO);
660
Dave Chinner55b66332010-03-23 11:43:17 +1100661 error = xlog_write(log, &vec, tic, start_lsn, NULL, 0);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000662 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +1000663 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100664 return error;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667void
668xfs_log_move_tail(xfs_mount_t *mp,
669 xfs_lsn_t tail_lsn)
670{
671 xlog_ticket_t *tic;
672 xlog_t *log = mp->m_log;
Dave Chinnera69ed032010-12-21 12:08:20 +1100673 int need_bytes, free_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (XLOG_FORCED_SHUTDOWN(log))
676 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dave Chinner84f3c682010-12-03 22:11:29 +1100678 if (tail_lsn == 0)
679 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100681 /* tail_lsn == 1 implies that we weren't passed a valid value. */
682 if (tail_lsn != 1)
683 atomic64_set(&log->l_tail_lsn, tail_lsn);
684
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000685 spin_lock(&log->l_grant_lock);
Dave Chinner10547942010-12-21 12:02:25 +1100686 if (!list_empty(&log->l_writeq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687#ifdef DEBUG
688 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
689 panic("Recovery problem");
690#endif
Dave Chinnera69ed032010-12-21 12:08:20 +1100691 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Dave Chinner10547942010-12-21 12:02:25 +1100692 list_for_each_entry(tic, &log->l_writeq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
694
695 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
696 break;
697 tail_lsn = 0;
698 free_bytes -= tic->t_unit_res;
Dave Chinnereb40a872010-12-21 12:09:01 +1100699 wake_up(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Dave Chinner10547942010-12-21 12:02:25 +1100702
703 if (!list_empty(&log->l_reserveq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704#ifdef DEBUG
705 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
706 panic("Recovery problem");
707#endif
Dave Chinnera69ed032010-12-21 12:08:20 +1100708 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Dave Chinner10547942010-12-21 12:02:25 +1100709 list_for_each_entry(tic, &log->l_reserveq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
711 need_bytes = tic->t_unit_res*tic->t_cnt;
712 else
713 need_bytes = tic->t_unit_res;
714 if (free_bytes < need_bytes && tail_lsn != 1)
715 break;
716 tail_lsn = 0;
717 free_bytes -= need_bytes;
Dave Chinnereb40a872010-12-21 12:09:01 +1100718 wake_up(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000721 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722} /* xfs_log_move_tail */
723
724/*
725 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000726 * that needs to be covered. To begin the transition to the idle state
727 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
728 * If we are then in a state where covering is needed, the caller is informed
729 * that dummy transactions are required to move the log into the idle state.
730 *
731 * Because this is called as part of the sync process, we should also indicate
732 * that dummy transactions should be issued in anything but the covered or
733 * idle states. This ensures that the log tail is accurately reflected in
734 * the log at the end of the sync, hence if a crash occurrs avoids replay
735 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
737int
738xfs_log_need_covered(xfs_mount_t *mp)
739{
David Chinner27d8d5f2008-10-30 17:38:39 +1100740 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
David Chinner92821e22007-05-24 15:26:31 +1000743 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000746 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000747 switch (log->l_covered_state) {
748 case XLOG_STATE_COVER_DONE:
749 case XLOG_STATE_COVER_DONE2:
750 case XLOG_STATE_COVER_IDLE:
751 break;
752 case XLOG_STATE_COVER_NEED:
753 case XLOG_STATE_COVER_NEED2:
754 if (!xfs_trans_ail_tail(log->l_ailp) &&
755 xlog_iclogs_empty(log)) {
756 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
757 log->l_covered_state = XLOG_STATE_COVER_DONE;
758 else
759 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000761 /* FALLTHRU */
762 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000766 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100767 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/******************************************************************************
771 *
772 * local routines
773 *
774 ******************************************************************************
775 */
776
777/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
778 * The log manager must keep track of the last LR which was committed
779 * to disk. The lsn of this LR will become the new tail_lsn whenever
780 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
781 * the situation where stuff could be written into the log but nothing
782 * was ever in the AIL when asked. Eventually, we panic since the
783 * tail hits the head.
784 *
785 * We may be holding the log iclog lock upon entering this routine.
786 */
787xfs_lsn_t
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100788xlog_assign_tail_lsn(
789 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100791 xfs_lsn_t tail_lsn;
792 struct log *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
David Chinner5b00f142008-10-30 17:39:00 +1100794 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Dave Chinner84f3c682010-12-03 22:11:29 +1100795 if (!tail_lsn)
796 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100798 atomic64_set(&log->l_tail_lsn, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return tail_lsn;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802/*
803 * Return the space in the log between the tail and the head. The head
804 * is passed in the cycle/bytes formal parms. In the special case where
805 * the reserve head has wrapped passed the tail, this calculation is no
806 * longer valid. In this case, just return 0 which means there is no space
807 * in the log. This works for all places where this function is called
808 * with the reserve head. Of course, if the write head were to ever
809 * wrap the tail, we should blow up. Rather than catch this case here,
810 * we depend on other ASSERTions in other parts of the code. XXXmiken
811 *
812 * This code also handles the case where the reservation head is behind
813 * the tail. The details of this case are described below, but the end
814 * result is that we return the size of the log as the amount of space left.
815 */
David Chinnera8272ce2007-11-23 16:28:09 +1100816STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +1100817xlog_space_left(
818 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100819 atomic64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Dave Chinnera69ed032010-12-21 12:08:20 +1100821 int free_bytes;
822 int tail_bytes;
823 int tail_cycle;
824 int head_cycle;
825 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Dave Chinnera69ed032010-12-21 12:08:20 +1100827 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100828 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
829 tail_bytes = BBTOB(tail_bytes);
Dave Chinnera69ed032010-12-21 12:08:20 +1100830 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
831 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
832 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +1100834 else if (tail_cycle < head_cycle) {
835 ASSERT(tail_cycle == (head_cycle - 1));
836 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 } else {
838 /*
839 * The reservation head is behind the tail.
840 * In this case we just want to return the size of the
841 * log as the amount of space left.
842 */
843 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
844 "xlog_space_left: head behind tail\n"
845 " tail_cycle = %d, tail_bytes = %d\n"
846 " GH cycle = %d, GH bytes = %d",
Dave Chinnera69ed032010-12-21 12:08:20 +1100847 tail_cycle, tail_bytes, head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ASSERT(0);
849 free_bytes = log->l_logsize;
850 }
851 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +1100852}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854
855/*
856 * Log function which is called when an io completes.
857 *
858 * The log manager needs its own routine, in order to control what
859 * happens with the buffer after the write completes.
860 */
861void
862xlog_iodone(xfs_buf_t *bp)
863{
864 xlog_in_core_t *iclog;
865 xlog_t *l;
866 int aborted;
867
868 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
869 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
870 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
871 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 l = iclog->ic_log;
873
874 /*
875 * Race to shutdown the filesystem if we see an error.
876 */
877 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
878 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
879 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
880 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000881 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
883 * This flag will be propagated to the trans-committed
884 * callback routines to let them know that the log-commit
885 * didn't succeed.
886 */
887 aborted = XFS_LI_ABORTED;
888 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
889 aborted = XFS_LI_ABORTED;
890 }
David Chinner3db296f2007-05-14 18:24:16 +1000891
892 /* log I/O is always issued ASYNC */
893 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000895 /*
896 * do not reference the buffer (bp) here as we could race
897 * with it being freed after writing the unmount record to the
898 * log.
899 */
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901} /* xlog_iodone */
902
903/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * Return size of each in-core log record buffer.
905 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200906 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 *
908 * If the filesystem blocksize is too large, we may need to choose a
909 * larger size since the directory code currently logs entire blocks.
910 */
911
912STATIC void
913xlog_get_iclog_buffer_size(xfs_mount_t *mp,
914 xlog_t *log)
915{
916 int size;
917 int xhdrs;
918
Eric Sandeen1cb51252007-08-16 16:24:43 +1000919 if (mp->m_logbufs <= 0)
920 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
921 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100922 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /*
925 * Buffer size passed in from mount system call.
926 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100927 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 size = log->l_iclog_size = mp->m_logbsize;
929 log->l_iclog_size_log = 0;
930 while (size != 1) {
931 log->l_iclog_size_log++;
932 size >>= 1;
933 }
934
Eric Sandeen62118702008-03-06 13:44:28 +1100935 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200936 /* # headers = size / 32k
937 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
939
940 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
941 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
942 xhdrs++;
943 log->l_iclog_hsize = xhdrs << BBSHIFT;
944 log->l_iclog_heads = xhdrs;
945 } else {
946 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
947 log->l_iclog_hsize = BBSIZE;
948 log->l_iclog_heads = 1;
949 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100950 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200953 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +1000954 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
955 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /* the default log size is 16k or 32k which is one header sector */
958 log->l_iclog_hsize = BBSIZE;
959 log->l_iclog_heads = 1;
960
Christoph Hellwig7153f8b2009-02-09 08:36:46 +0100961done:
962 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100963 if (mp->m_logbufs == 0)
964 mp->m_logbufs = log->l_iclog_bufs;
965 if (mp->m_logbsize == 0)
966 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967} /* xlog_get_iclog_buffer_size */
968
969
970/*
971 * This routine initializes some of the log structure for a given mount point.
972 * Its primary purpose is to fill in enough, so recovery can occur. However,
973 * some other stuff may be filled in too.
974 */
975STATIC xlog_t *
976xlog_alloc_log(xfs_mount_t *mp,
977 xfs_buftarg_t *log_target,
978 xfs_daddr_t blk_offset,
979 int num_bblks)
980{
981 xlog_t *log;
982 xlog_rec_header_t *head;
983 xlog_in_core_t **iclogp;
984 xlog_in_core_t *iclog, *prev_iclog=NULL;
985 xfs_buf_t *bp;
986 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +0200987 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +1000988 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Dave Chinner644c3562008-11-10 16:50:24 +1100990 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200991 if (!log) {
992 xlog_warn("XFS: Log allocation failed: No memory!");
993 goto out;
994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 log->l_mp = mp;
997 log->l_targ = log_target;
998 log->l_logsize = BBTOB(num_bblks);
999 log->l_logBBstart = blk_offset;
1000 log->l_logBBsize = num_bblks;
1001 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1002 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1003
1004 log->l_prev_block = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001006 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1007 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Dave Chinnera69ed032010-12-21 12:08:20 +11001009 xlog_assign_grant_head(&log->l_grant_reserve_head, 1, 0);
1010 xlog_assign_grant_head(&log->l_grant_write_head, 1, 0);
Dave Chinner10547942010-12-21 12:02:25 +11001011 INIT_LIST_HEAD(&log->l_reserveq);
1012 INIT_LIST_HEAD(&log->l_writeq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Dave Chinnera6cb7672009-04-06 18:39:27 +02001014 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001015 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001016 log2_size = mp->m_sb.sb_logsectlog;
1017 if (log2_size < BBSHIFT) {
1018 xlog_warn("XFS: Log sector size too small "
1019 "(0x%x < 0x%x)", log2_size, BBSHIFT);
1020 goto out_free_log;
1021 }
1022
1023 log2_size -= BBSHIFT;
1024 if (log2_size > mp->m_sectbb_log) {
1025 xlog_warn("XFS: Log sector size too large "
1026 "(0x%x > 0x%x)", log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001027 goto out_free_log;
1028 }
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001031 if (log2_size && log->l_logBBstart > 0 &&
1032 !xfs_sb_version_haslogv2(&mp->m_sb)) {
1033
Dave Chinnera6cb7672009-04-06 18:39:27 +02001034 xlog_warn("XFS: log sector size (0x%x) invalid "
Alex Elder69ce58f2010-04-20 17:09:59 +10001035 "for configuration.", log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001036 goto out_free_log;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001039 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 xlog_get_iclog_buffer_size(mp, log);
1042
Dave Chinnera6cb7672009-04-06 18:39:27 +02001043 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001045 if (!bp)
1046 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1049 ASSERT(XFS_BUF_ISBUSY(bp));
1050 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1051 log->l_xbuf = bp;
1052
Eric Sandeen007c61c2007-10-11 17:43:56 +10001053 spin_lock_init(&log->l_icloglock);
1054 spin_lock_init(&log->l_grant_lock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001055 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1058 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1059
1060 iclogp = &log->l_iclog;
1061 /*
1062 * The amount of memory to allocate for the iclog structure is
1063 * rather funky due to the way the structure is defined. It is
1064 * done this way so that we can use different sizes for machines
1065 * with different amounts of memory. See the definition of
1066 * xlog_in_core_t in xfs_log_priv.h for details.
1067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ASSERT(log->l_iclog_size >= 4096);
1069 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001070 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1071 if (!*iclogp)
1072 goto out_free_iclog;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 iclog->ic_prev = prev_iclog;
1076 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001077
Dave Chinner686865f2010-09-24 20:07:47 +10001078 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1079 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001080 if (!bp)
1081 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001082 if (!XFS_BUF_CPSEMA(bp))
1083 ASSERT(0);
1084 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001085 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1086 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001087 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001088#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 head = &iclog->ic_header;
1092 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001093 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1094 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001095 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001096 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001098 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1102 iclog->ic_state = XLOG_STATE_ACTIVE;
1103 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001104 atomic_set(&iclog->ic_refcnt, 0);
1105 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001107 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1110 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11001111 init_waitqueue_head(&iclog->ic_force_wait);
1112 init_waitqueue_head(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 iclogp = &iclog->ic_next;
1115 }
1116 *iclogp = log->l_iclog; /* complete ring */
1117 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1118
Dave Chinner71e330b2010-05-21 14:37:18 +10001119 error = xlog_cil_init(log);
1120 if (error)
1121 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001123
1124out_free_iclog:
1125 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1126 prev_iclog = iclog->ic_next;
Dave Chinnereb40a872010-12-21 12:09:01 +11001127 if (iclog->ic_bp)
Dave Chinner644c3562008-11-10 16:50:24 +11001128 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001129 kmem_free(iclog);
1130 }
1131 spinlock_destroy(&log->l_icloglock);
1132 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001133 xfs_buf_free(log->l_xbuf);
1134out_free_log:
1135 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001136out:
1137 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138} /* xlog_alloc_log */
1139
1140
1141/*
1142 * Write out the commit record of a transaction associated with the given
1143 * ticket. Return the lsn of the commit record.
1144 */
1145STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001146xlog_commit_record(
1147 struct log *log,
1148 struct xlog_ticket *ticket,
1149 struct xlog_in_core **iclog,
1150 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Dave Chinner55b66332010-03-23 11:43:17 +11001152 struct xfs_mount *mp = log->l_mp;
1153 int error;
1154 struct xfs_log_iovec reg = {
1155 .i_addr = NULL,
1156 .i_len = 0,
1157 .i_type = XLOG_REG_TYPE_COMMIT,
1158 };
1159 struct xfs_log_vec vec = {
1160 .lv_niovecs = 1,
1161 .lv_iovecp = &reg,
1162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001165 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1166 XLOG_COMMIT_TRANS);
1167 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001168 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001169 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172/*
1173 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1174 * log space. This code pushes on the lsn which would supposedly free up
1175 * the 25% which we want to leave free. We may need to adopt a policy which
1176 * pushes on an lsn which is further along in the log once we reach the high
1177 * water mark. In this manner, we would be creating a low water mark.
1178 */
David Chinnera8272ce2007-11-23 16:28:09 +11001179STATIC void
Dave Chinner2ced19c2010-12-21 12:09:20 +11001180xlog_grant_push_ail(
1181 struct log *log,
1182 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001184 xfs_lsn_t threshold_lsn = 0;
Dave Chinner84f3c682010-12-03 22:11:29 +11001185 xfs_lsn_t last_sync_lsn;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001186 int free_blocks;
1187 int free_bytes;
1188 int threshold_block;
1189 int threshold_cycle;
1190 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dave Chinner2ced19c2010-12-21 12:09:20 +11001192 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dave Chinner2ced19c2010-12-21 12:09:20 +11001194 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
1195 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Dave Chinner2ced19c2010-12-21 12:09:20 +11001197 /*
1198 * Set the threshold for the minimum number of free blocks in the
1199 * log to the maximum of what the caller needs, one quarter of the
1200 * log, and 256 blocks.
1201 */
1202 free_threshold = BTOBB(need_bytes);
1203 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1204 free_threshold = MAX(free_threshold, 256);
1205 if (free_blocks >= free_threshold)
1206 return;
1207
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001208 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1209 &threshold_block);
1210 threshold_block += free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001212 threshold_block -= log->l_logBBsize;
1213 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001215 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1216 threshold_block);
1217 /*
1218 * Don't pass in an lsn greater than the lsn of the last
Dave Chinner84f3c682010-12-03 22:11:29 +11001219 * log record known to be on disk. Use a snapshot of the last sync lsn
1220 * so that it doesn't change between the compare and the set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Dave Chinner84f3c682010-12-03 22:11:29 +11001222 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1223 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1224 threshold_lsn = last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dave Chinner2ced19c2010-12-21 12:09:20 +11001226 /*
1227 * Get the transaction layer to kick the dirty buffers out to
1228 * disk asynchronously. No point in trying to do this if
1229 * the filesystem is shutting down.
1230 */
1231 if (!XLOG_FORCED_SHUTDOWN(log))
1232 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
1233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001235/*
1236 * The bdstrat callback function for log bufs. This gives us a central
1237 * place to trap bufs in case we get hit by a log I/O error and need to
1238 * shutdown. Actually, in practice, even when we didn't get a log error,
1239 * we transition the iclogs to IOERROR state *after* flushing all existing
1240 * iclogs to disk. This is because we don't want anymore new transactions to be
1241 * started or completed afterwards.
1242 */
1243STATIC int
1244xlog_bdstrat(
1245 struct xfs_buf *bp)
1246{
1247 struct xlog_in_core *iclog;
1248
1249 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1250 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1251 XFS_BUF_ERROR(bp, EIO);
1252 XFS_BUF_STALE(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001253 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001254 /*
1255 * It would seem logical to return EIO here, but we rely on
1256 * the log state machine to propagate I/O errors instead of
1257 * doing it here.
1258 */
1259 return 0;
1260 }
1261
1262 bp->b_flags |= _XBF_RUN_QUEUES;
1263 xfs_buf_iorequest(bp);
1264 return 0;
1265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267/*
1268 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1269 * fashion. Previously, we should have moved the current iclog
1270 * ptr in the log to point to the next available iclog. This allows further
1271 * write to continue while this code syncs out an iclog ready to go.
1272 * Before an in-core log can be written out, the data section must be scanned
1273 * to save away the 1st word of each BBSIZE block into the header. We replace
1274 * it with the current cycle count. Each BBSIZE block is tagged with the
1275 * cycle count because there in an implicit assumption that drives will
1276 * guarantee that entire 512 byte blocks get written at once. In other words,
1277 * we can't have part of a 512 byte block written and part not written. By
1278 * tagging each block, we will know which blocks are valid when recovering
1279 * after an unclean shutdown.
1280 *
1281 * This routine is single threaded on the iclog. No other thread can be in
1282 * this routine with the same iclog. Changing contents of iclog can there-
1283 * fore be done without grabbing the state machine lock. Updating the global
1284 * log will require grabbing the lock though.
1285 *
1286 * The entire log manager uses a logical block numbering scheme. Only
1287 * log_sync (and then only bwrite()) know about the fact that the log may
1288 * not start with block zero on a given device. The log block start offset
1289 * is added immediately before calling bwrite().
1290 */
1291
David Chinnera8272ce2007-11-23 16:28:09 +11001292STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293xlog_sync(xlog_t *log,
1294 xlog_in_core_t *iclog)
1295{
1296 xfs_caddr_t dptr; /* pointer to byte sized element */
1297 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001298 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 uint count; /* byte count of bwrite */
1300 uint count_init; /* initial count before roundup */
1301 int roundoff; /* roundoff to BB or stripe */
1302 int split = 0; /* split write into two regions */
1303 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001304 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001307 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /* Add for LR header */
1310 count_init = log->l_iclog_hsize + iclog->ic_offset;
1311
1312 /* Round out the log write size */
1313 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1314 /* we have a v2 stripe unit to use */
1315 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1316 } else {
1317 count = BBTOB(BTOBB(count_init));
1318 }
1319 roundoff = count - count_init;
1320 ASSERT(roundoff >= 0);
1321 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1322 roundoff < log->l_mp->m_sb.sb_logsunit)
1323 ||
1324 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1325 roundoff < BBTOB(1)));
1326
1327 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001328 spin_lock(&log->l_grant_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +11001329 xlog_grant_add_space(log, &log->l_grant_reserve_head, roundoff);
1330 xlog_grant_add_space(log, &log->l_grant_write_head, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001331 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* put cycle number in every block */
1334 xlog_pack_data(log, iclog, roundoff);
1335
1336 /* real byte length */
1337 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001338 iclog->ic_header.h_len =
1339 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001341 iclog->ic_header.h_len =
1342 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
Nathan Scottf5faad72006-07-28 17:04:44 +10001345 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1347 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001348 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1351
1352 /* Do we need to split this write into 2 parts? */
1353 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1354 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1355 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1356 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1357 } else {
1358 iclog->ic_bwritecnt = 1;
1359 }
David Chinner511105b2007-05-24 15:21:57 +10001360 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001362 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 XFS_BUF_BUSY(bp);
1364 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001365 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001366
1367 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001368 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1371 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1372
1373 xlog_verify_iclog(log, iclog, count, B_TRUE);
1374
1375 /* account for log which doesn't start at block #0 */
1376 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1377 /*
1378 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1379 * is shutting down.
1380 */
1381 XFS_BUF_WRITE(bp);
1382
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001383 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1385 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001386 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001389 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1391 (unsigned long)1);
1392 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1393 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1394 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1395 (__psint_t)count), split);
1396 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001397 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 XFS_BUF_BUSY(bp);
1399 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001400 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001401 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1402 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 dptr = XFS_BUF_PTR(bp);
1404 /*
1405 * Bump the cycle numbers at the start of each block
1406 * since this part of the buffer is at the start of
1407 * a new cycle. Watch out for the header magic number
1408 * case, though.
1409 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001410 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001411 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001412 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001413 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 dptr += BBSIZE;
1415 }
1416
1417 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1418 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1419
Nathan Scottc41564b2006-03-29 08:55:14 +10001420 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1422 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff5502010-01-13 22:17:57 +00001423 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1425 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001426 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430} /* xlog_sync */
1431
1432
1433/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001434 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
David Chinnera8272ce2007-11-23 16:28:09 +11001436STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001437xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int i;
1441
Dave Chinner71e330b2010-05-21 14:37:18 +10001442 xlog_cil_destroy(log);
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 iclog = log->l_iclog;
1445 for (i=0; i<log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001448 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 iclog = next_iclog;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 spinlock_destroy(&log->l_icloglock);
1452 spinlock_destroy(&log->l_grant_lock);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001456 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001457} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459/*
1460 * Update counters atomically now that memcpy is done.
1461 */
1462/* ARGSUSED */
1463static inline void
1464xlog_state_finish_copy(xlog_t *log,
1465 xlog_in_core_t *iclog,
1466 int record_cnt,
1467 int copy_bytes)
1468{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001469 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001471 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 iclog->ic_offset += copy_bytes;
1473
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001474 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475} /* xlog_state_finish_copy */
1476
1477
1478
1479
1480/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001481 * print out info relating to regions written which consume
1482 * the reservation
1483 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001484void
1485xlog_print_tic_res(
1486 struct xfs_mount *mp,
1487 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001488{
1489 uint i;
1490 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1491
1492 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1493 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1494 "bformat",
1495 "bchunk",
1496 "efi_format",
1497 "efd_format",
1498 "iformat",
1499 "icore",
1500 "iext",
1501 "ibroot",
1502 "ilocal",
1503 "iattr_ext",
1504 "iattr_broot",
1505 "iattr_local",
1506 "qformat",
1507 "dquot",
1508 "quotaoff",
1509 "LR header",
1510 "unmount",
1511 "commit",
1512 "trans header"
1513 };
1514 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1515 "SETATTR_NOT_SIZE",
1516 "SETATTR_SIZE",
1517 "INACTIVE",
1518 "CREATE",
1519 "CREATE_TRUNC",
1520 "TRUNCATE_FILE",
1521 "REMOVE",
1522 "LINK",
1523 "RENAME",
1524 "MKDIR",
1525 "RMDIR",
1526 "SYMLINK",
1527 "SET_DMATTRS",
1528 "GROWFS",
1529 "STRAT_WRITE",
1530 "DIOSTRAT",
1531 "WRITE_SYNC",
1532 "WRITEID",
1533 "ADDAFORK",
1534 "ATTRINVAL",
1535 "ATRUNCATE",
1536 "ATTR_SET",
1537 "ATTR_RM",
1538 "ATTR_FLAG",
1539 "CLEAR_AGI_BUCKET",
1540 "QM_SBCHANGE",
1541 "DUMMY1",
1542 "DUMMY2",
1543 "QM_QUOTAOFF",
1544 "QM_DQALLOC",
1545 "QM_SETQLIM",
1546 "QM_DQCLUSTER",
1547 "QM_QINOCREATE",
1548 "QM_QUOTAOFF_END",
1549 "SB_UNIT",
1550 "FSYNC_TS",
1551 "GROWFSRT_ALLOC",
1552 "GROWFSRT_ZERO",
1553 "GROWFSRT_FREE",
1554 "SWAPEXT"
1555 };
1556
1557 xfs_fs_cmn_err(CE_WARN, mp,
1558 "xfs_log_write: reservation summary:\n"
1559 " trans type = %s (%u)\n"
1560 " unit res = %d bytes\n"
1561 " current res = %d bytes\n"
1562 " total reg = %u bytes (o/flow = %u bytes)\n"
1563 " ophdrs = %u (ophdr space = %u bytes)\n"
1564 " ophdr + reg = %u bytes\n"
1565 " num regions = %u\n",
1566 ((ticket->t_trans_type <= 0 ||
1567 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1568 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1569 ticket->t_trans_type,
1570 ticket->t_unit_res,
1571 ticket->t_curr_res,
1572 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1573 ticket->t_res_num_ophdrs, ophdr_spc,
1574 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001575 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001576 ticket->t_res_num);
1577
1578 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001579 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001580 cmn_err(CE_WARN,
1581 "region[%u]: %s - %u bytes\n",
1582 i,
1583 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1584 "bad-rtype" : res_type_str[r_type-1]),
1585 ticket->t_res_arr[i].r_len);
1586 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001587
1588 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1589 "xfs_log_write: reservation ran out. Need to up reservation");
1590 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001591}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001592
1593/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001594 * Calculate the potential space needed by the log vector. Each region gets
1595 * its own xlog_op_header_t and may need to be double word aligned.
1596 */
1597static int
1598xlog_write_calc_vec_length(
1599 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001600 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001601{
Dave Chinner55b66332010-03-23 11:43:17 +11001602 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001603 int headers = 0;
1604 int len = 0;
1605 int i;
1606
1607 /* acct for start rec of xact */
1608 if (ticket->t_flags & XLOG_TIC_INITED)
1609 headers++;
1610
Dave Chinner55b66332010-03-23 11:43:17 +11001611 for (lv = log_vector; lv; lv = lv->lv_next) {
1612 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001613
Dave Chinner55b66332010-03-23 11:43:17 +11001614 for (i = 0; i < lv->lv_niovecs; i++) {
1615 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1616
1617 len += vecp->i_len;
1618 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1619 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001620 }
1621
1622 ticket->t_res_num_ophdrs += headers;
1623 len += headers * sizeof(struct xlog_op_header);
1624
1625 return len;
1626}
1627
1628/*
1629 * If first write for transaction, insert start record We can't be trying to
1630 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1631 */
1632static int
1633xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001634 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001635 struct xlog_ticket *ticket)
1636{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001637 if (!(ticket->t_flags & XLOG_TIC_INITED))
1638 return 0;
1639
1640 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1641 ophdr->oh_clientid = ticket->t_clientid;
1642 ophdr->oh_len = 0;
1643 ophdr->oh_flags = XLOG_START_TRANS;
1644 ophdr->oh_res2 = 0;
1645
1646 ticket->t_flags &= ~XLOG_TIC_INITED;
1647
1648 return sizeof(struct xlog_op_header);
1649}
1650
1651static xlog_op_header_t *
1652xlog_write_setup_ophdr(
1653 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001654 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001655 struct xlog_ticket *ticket,
1656 uint flags)
1657{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001658 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1659 ophdr->oh_clientid = ticket->t_clientid;
1660 ophdr->oh_res2 = 0;
1661
1662 /* are we copying a commit or unmount record? */
1663 ophdr->oh_flags = flags;
1664
1665 /*
1666 * We've seen logs corrupted with bad transaction client ids. This
1667 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1668 * and shut down the filesystem.
1669 */
1670 switch (ophdr->oh_clientid) {
1671 case XFS_TRANSACTION:
1672 case XFS_VOLUME:
1673 case XFS_LOG:
1674 break;
1675 default:
1676 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1677 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1678 ophdr->oh_clientid, ticket);
1679 return NULL;
1680 }
1681
1682 return ophdr;
1683}
1684
1685/*
1686 * Set up the parameters of the region copy into the log. This has
1687 * to handle region write split across multiple log buffers - this
1688 * state is kept external to this function so that this code can
1689 * can be written in an obvious, self documenting manner.
1690 */
1691static int
1692xlog_write_setup_copy(
1693 struct xlog_ticket *ticket,
1694 struct xlog_op_header *ophdr,
1695 int space_available,
1696 int space_required,
1697 int *copy_off,
1698 int *copy_len,
1699 int *last_was_partial_copy,
1700 int *bytes_consumed)
1701{
1702 int still_to_copy;
1703
1704 still_to_copy = space_required - *bytes_consumed;
1705 *copy_off = *bytes_consumed;
1706
1707 if (still_to_copy <= space_available) {
1708 /* write of region completes here */
1709 *copy_len = still_to_copy;
1710 ophdr->oh_len = cpu_to_be32(*copy_len);
1711 if (*last_was_partial_copy)
1712 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1713 *last_was_partial_copy = 0;
1714 *bytes_consumed = 0;
1715 return 0;
1716 }
1717
1718 /* partial write of region, needs extra log op header reservation */
1719 *copy_len = space_available;
1720 ophdr->oh_len = cpu_to_be32(*copy_len);
1721 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1722 if (*last_was_partial_copy)
1723 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1724 *bytes_consumed += *copy_len;
1725 (*last_was_partial_copy)++;
1726
1727 /* account for new log op header */
1728 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1729 ticket->t_res_num_ophdrs++;
1730
1731 return sizeof(struct xlog_op_header);
1732}
1733
1734static int
1735xlog_write_copy_finish(
1736 struct log *log,
1737 struct xlog_in_core *iclog,
1738 uint flags,
1739 int *record_cnt,
1740 int *data_cnt,
1741 int *partial_copy,
1742 int *partial_copy_len,
1743 int log_offset,
1744 struct xlog_in_core **commit_iclog)
1745{
1746 if (*partial_copy) {
1747 /*
1748 * This iclog has already been marked WANT_SYNC by
1749 * xlog_state_get_iclog_space.
1750 */
1751 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1752 *record_cnt = 0;
1753 *data_cnt = 0;
1754 return xlog_state_release_iclog(log, iclog);
1755 }
1756
1757 *partial_copy = 0;
1758 *partial_copy_len = 0;
1759
1760 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1761 /* no more space in this iclog - push it. */
1762 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1763 *record_cnt = 0;
1764 *data_cnt = 0;
1765
1766 spin_lock(&log->l_icloglock);
1767 xlog_state_want_sync(log, iclog);
1768 spin_unlock(&log->l_icloglock);
1769
1770 if (!commit_iclog)
1771 return xlog_state_release_iclog(log, iclog);
1772 ASSERT(flags & XLOG_COMMIT_TRANS);
1773 *commit_iclog = iclog;
1774 }
1775
1776 return 0;
1777}
1778
1779/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * Write some region out to in-core log
1781 *
1782 * This will be called when writing externally provided regions or when
1783 * writing out a commit record for a given transaction.
1784 *
1785 * General algorithm:
1786 * 1. Find total length of this write. This may include adding to the
1787 * lengths passed in.
1788 * 2. Check whether we violate the tickets reservation.
1789 * 3. While writing to this iclog
1790 * A. Reserve as much space in this iclog as can get
1791 * B. If this is first write, save away start lsn
1792 * C. While writing this region:
1793 * 1. If first write of transaction, write start record
1794 * 2. Write log operation header (header per region)
1795 * 3. Find out if we can fit entire region into this iclog
1796 * 4. Potentially, verify destination memcpy ptr
1797 * 5. Memcpy (partial) region
1798 * 6. If partial copy, release iclog; otherwise, continue
1799 * copying more regions into current iclog
1800 * 4. Mark want sync bit (in simulation mode)
1801 * 5. Release iclog for potential flush to on-disk log.
1802 *
1803 * ERRORS:
1804 * 1. Panic if reservation is overrun. This should never happen since
1805 * reservation amounts are generated internal to the filesystem.
1806 * NOTES:
1807 * 1. Tickets are single threaded data structures.
1808 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1809 * syncing routine. When a single log_write region needs to span
1810 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1811 * on all log operation writes which don't contain the end of the
1812 * region. The XLOG_END_TRANS bit is used for the in-core log
1813 * operation which contains the end of the continued log_write region.
1814 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1815 * we don't really know exactly how much space will be used. As a result,
1816 * we don't update ic_offset until the end when we know exactly how many
1817 * bytes have been written out.
1818 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001819int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001820xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001821 struct log *log,
1822 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001823 struct xlog_ticket *ticket,
1824 xfs_lsn_t *start_lsn,
1825 struct xlog_in_core **commit_iclog,
1826 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001828 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001829 struct xfs_log_iovec *vecp;
1830 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001831 int len;
1832 int index;
1833 int partial_copy = 0;
1834 int partial_copy_len = 0;
1835 int contwr = 0;
1836 int record_cnt = 0;
1837 int data_cnt = 0;
1838 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001840 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Dave Chinner55b66332010-03-23 11:43:17 +11001842 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001843 if (log->l_cilp) {
1844 /*
1845 * Region headers and bytes are already accounted for.
1846 * We only need to take into account start records and
1847 * split regions in this function.
1848 */
1849 if (ticket->t_flags & XLOG_TIC_INITED)
1850 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1851
1852 /*
1853 * Commit record headers need to be accounted for. These
1854 * come in as separate writes so are easy to detect.
1855 */
1856 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1857 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1858 } else
1859 ticket->t_curr_res -= len;
1860
1861 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001862 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Dave Chinner55b66332010-03-23 11:43:17 +11001864 index = 0;
1865 lv = log_vector;
1866 vecp = lv->lv_iovecp;
1867 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001868 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001869 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001871 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1872 &contwr, &log_offset);
1873 if (error)
1874 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001876 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001877 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001879 /* start_lsn is the first lsn written to. That's all we need. */
1880 if (!*start_lsn)
1881 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001883 /*
1884 * This loop writes out as many regions as can fit in the amount
1885 * of space which was allocated by xlog_state_get_iclog_space().
1886 */
Dave Chinner55b66332010-03-23 11:43:17 +11001887 while (lv && index < lv->lv_niovecs) {
1888 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001889 struct xlog_op_header *ophdr;
1890 int start_rec_copy;
1891 int copy_len;
1892 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Dave Chinner55b66332010-03-23 11:43:17 +11001894 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001895 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001897 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1898 if (start_rec_copy) {
1899 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001900 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001901 start_rec_copy);
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001904 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1905 if (!ophdr)
1906 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001908 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001909 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001911 len += xlog_write_setup_copy(ticket, ophdr,
1912 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001913 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001914 &copy_off, &copy_len,
1915 &partial_copy,
1916 &partial_copy_len);
1917 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001918
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001919 /* copy region */
1920 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001921 memcpy(ptr, reg->i_addr + copy_off, copy_len);
1922 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001923
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001924 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1925 record_cnt++;
1926 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001928 error = xlog_write_copy_finish(log, iclog, flags,
1929 &record_cnt, &data_cnt,
1930 &partial_copy,
1931 &partial_copy_len,
1932 log_offset,
1933 commit_iclog);
1934 if (error)
1935 return error;
1936
1937 /*
1938 * if we had a partial copy, we need to get more iclog
1939 * space but we don't want to increment the region
1940 * index because there is still more is this region to
1941 * write.
1942 *
1943 * If we completed writing this region, and we flushed
1944 * the iclog (indicated by resetting of the record
1945 * count), then we also need to get more log space. If
1946 * this was the last record, though, we are done and
1947 * can just return.
1948 */
1949 if (partial_copy)
1950 break;
1951
Dave Chinner55b66332010-03-23 11:43:17 +11001952 if (++index == lv->lv_niovecs) {
1953 lv = lv->lv_next;
1954 index = 0;
1955 if (lv)
1956 vecp = lv->lv_iovecp;
1957 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001958 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11001959 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001960 return 0;
1961 break;
1962 }
1963 }
1964 }
1965
1966 ASSERT(len == 0);
1967
1968 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1969 if (!commit_iclog)
1970 return xlog_state_release_iclog(log, iclog);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 ASSERT(flags & XLOG_COMMIT_TRANS);
1973 *commit_iclog = iclog;
1974 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977
1978/*****************************************************************************
1979 *
1980 * State Machine functions
1981 *
1982 *****************************************************************************
1983 */
1984
1985/* Clean iclogs starting from the head. This ordering must be
1986 * maintained, so an iclog doesn't become ACTIVE beyond one that
1987 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001988 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 * iclog is trying to sync to disk.
1990 *
1991 * State Change: DIRTY -> ACTIVE
1992 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001993STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994xlog_state_clean_log(xlog_t *log)
1995{
1996 xlog_in_core_t *iclog;
1997 int changed = 0;
1998
1999 iclog = log->l_iclog;
2000 do {
2001 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2002 iclog->ic_state = XLOG_STATE_ACTIVE;
2003 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002004 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /*
2006 * If the number of ops in this iclog indicate it just
2007 * contains the dummy transaction, we can
2008 * change state into IDLE (the second time around).
2009 * Otherwise we should change the state into
2010 * NEED a dummy.
2011 * We don't need to cover the dummy.
2012 */
2013 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002014 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2015 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 changed = 1;
2017 } else {
2018 /*
2019 * We have two dirty iclogs so start over
2020 * This could also be num of ops indicates
2021 * this is not the dummy going out.
2022 */
2023 changed = 2;
2024 }
2025 iclog->ic_header.h_num_logops = 0;
2026 memset(iclog->ic_header.h_cycle_data, 0,
2027 sizeof(iclog->ic_header.h_cycle_data));
2028 iclog->ic_header.h_lsn = 0;
2029 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2030 /* do nothing */;
2031 else
2032 break; /* stop cleaning */
2033 iclog = iclog->ic_next;
2034 } while (iclog != log->l_iclog);
2035
2036 /* log is locked when we are called */
2037 /*
2038 * Change state for the dummy log recording.
2039 * We usually go to NEED. But we go to NEED2 if the changed indicates
2040 * we are done writing the dummy record.
2041 * If we are done with the second dummy recored (DONE2), then
2042 * we go to IDLE.
2043 */
2044 if (changed) {
2045 switch (log->l_covered_state) {
2046 case XLOG_STATE_COVER_IDLE:
2047 case XLOG_STATE_COVER_NEED:
2048 case XLOG_STATE_COVER_NEED2:
2049 log->l_covered_state = XLOG_STATE_COVER_NEED;
2050 break;
2051
2052 case XLOG_STATE_COVER_DONE:
2053 if (changed == 1)
2054 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2055 else
2056 log->l_covered_state = XLOG_STATE_COVER_NEED;
2057 break;
2058
2059 case XLOG_STATE_COVER_DONE2:
2060 if (changed == 1)
2061 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2062 else
2063 log->l_covered_state = XLOG_STATE_COVER_NEED;
2064 break;
2065
2066 default:
2067 ASSERT(0);
2068 }
2069 }
2070} /* xlog_state_clean_log */
2071
2072STATIC xfs_lsn_t
2073xlog_get_lowest_lsn(
2074 xlog_t *log)
2075{
2076 xlog_in_core_t *lsn_log;
2077 xfs_lsn_t lowest_lsn, lsn;
2078
2079 lsn_log = log->l_iclog;
2080 lowest_lsn = 0;
2081 do {
2082 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002083 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if ((lsn && !lowest_lsn) ||
2085 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2086 lowest_lsn = lsn;
2087 }
2088 }
2089 lsn_log = lsn_log->ic_next;
2090 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002091 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094
2095STATIC void
2096xlog_state_do_callback(
2097 xlog_t *log,
2098 int aborted,
2099 xlog_in_core_t *ciclog)
2100{
2101 xlog_in_core_t *iclog;
2102 xlog_in_core_t *first_iclog; /* used to know when we've
2103 * processed all iclogs once */
2104 xfs_log_callback_t *cb, *cb_next;
2105 int flushcnt = 0;
2106 xfs_lsn_t lowest_lsn;
2107 int ioerrors; /* counter: iclogs with errors */
2108 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2109 int funcdidcallbacks; /* flag: function did callbacks */
2110 int repeats; /* for issuing console warnings if
2111 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002112 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002114 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 first_iclog = iclog = log->l_iclog;
2116 ioerrors = 0;
2117 funcdidcallbacks = 0;
2118 repeats = 0;
2119
2120 do {
2121 /*
2122 * Scan all iclogs starting with the one pointed to by the
2123 * log. Reset this starting point each time the log is
2124 * unlocked (during callbacks).
2125 *
2126 * Keep looping through iclogs until one full pass is made
2127 * without running any callbacks.
2128 */
2129 first_iclog = log->l_iclog;
2130 iclog = log->l_iclog;
2131 loopdidcallbacks = 0;
2132 repeats++;
2133
2134 do {
2135
2136 /* skip all iclogs in the ACTIVE & DIRTY states */
2137 if (iclog->ic_state &
2138 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2139 iclog = iclog->ic_next;
2140 continue;
2141 }
2142
2143 /*
2144 * Between marking a filesystem SHUTDOWN and stopping
2145 * the log, we do flush all iclogs to disk (if there
2146 * wasn't a log I/O error). So, we do want things to
2147 * go smoothly in case of just a SHUTDOWN w/o a
2148 * LOG_IO_ERROR.
2149 */
2150 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2151 /*
2152 * Can only perform callbacks in order. Since
2153 * this iclog is not in the DONE_SYNC/
2154 * DO_CALLBACK state, we skip the rest and
2155 * just try to clean up. If we set our iclog
2156 * to DO_CALLBACK, we will not process it when
2157 * we retry since a previous iclog is in the
2158 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002159 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
2161 if (!(iclog->ic_state &
2162 (XLOG_STATE_DONE_SYNC |
2163 XLOG_STATE_DO_CALLBACK))) {
2164 if (ciclog && (ciclog->ic_state ==
2165 XLOG_STATE_DONE_SYNC)) {
2166 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2167 }
2168 break;
2169 }
2170 /*
2171 * We now have an iclog that is in either the
2172 * DO_CALLBACK or DONE_SYNC states. The other
2173 * states (WANT_SYNC, SYNCING, or CALLBACK were
2174 * caught by the above if and are going to
2175 * clean (i.e. we aren't doing their callbacks)
2176 * see the above if.
2177 */
2178
2179 /*
2180 * We will do one more check here to see if we
2181 * have chased our tail around.
2182 */
2183
2184 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002185 if (lowest_lsn &&
2186 XFS_LSN_CMP(lowest_lsn,
Dave Chinner84f3c682010-12-03 22:11:29 +11002187 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 iclog = iclog->ic_next;
2189 continue; /* Leave this iclog for
2190 * another thread */
2191 }
2192
2193 iclog->ic_state = XLOG_STATE_CALLBACK;
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Dave Chinner84f3c682010-12-03 22:11:29 +11002196 /*
2197 * update the last_sync_lsn before we drop the
2198 * icloglock to ensure we are the only one that
2199 * can update it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
Dave Chinner84f3c682010-12-03 22:11:29 +11002201 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2202 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2203 atomic64_set(&log->l_last_sync_lsn,
2204 be64_to_cpu(iclog->ic_header.h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Dave Chinner84f3c682010-12-03 22:11:29 +11002206 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 ioerrors++;
Dave Chinner84f3c682010-12-03 22:11:29 +11002208
2209 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
David Chinner114d23a2008-04-10 12:18:39 +10002211 /*
2212 * Keep processing entries in the callback list until
2213 * we come around and it is empty. We need to
2214 * atomically see that the list is empty and change the
2215 * state to DIRTY so that we don't miss any more
2216 * callbacks being added.
2217 */
2218 spin_lock(&iclog->ic_callback_lock);
2219 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002220 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 iclog->ic_callback_tail = &(iclog->ic_callback);
2222 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002223 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002226 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 cb_next = cb->cb_next;
2228 cb->cb_func(cb->cb_arg, aborted);
2229 }
David Chinner114d23a2008-04-10 12:18:39 +10002230 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 cb = iclog->ic_callback;
2232 }
2233
2234 loopdidcallbacks++;
2235 funcdidcallbacks++;
2236
David Chinner114d23a2008-04-10 12:18:39 +10002237 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002238 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002239 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2241 iclog->ic_state = XLOG_STATE_DIRTY;
2242
2243 /*
2244 * Transition from DIRTY to ACTIVE if applicable.
2245 * NOP if STATE_IOERROR.
2246 */
2247 xlog_state_clean_log(log);
2248
2249 /* wake up threads waiting in xfs_log_force() */
Dave Chinnereb40a872010-12-21 12:09:01 +11002250 wake_up_all(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 iclog = iclog->ic_next;
2253 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002254
2255 if (repeats > 5000) {
2256 flushcnt += repeats;
2257 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002259 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002260 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262 } while (!ioerrors && loopdidcallbacks);
2263
2264 /*
2265 * make one last gasp attempt to see if iclogs are being left in
2266 * limbo..
2267 */
2268#ifdef DEBUG
2269 if (funcdidcallbacks) {
2270 first_iclog = iclog = log->l_iclog;
2271 do {
2272 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2273 /*
2274 * Terminate the loop if iclogs are found in states
2275 * which will cause other threads to clean up iclogs.
2276 *
2277 * SYNCING - i/o completion will go through logs
2278 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002279 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 * IOERROR - give up hope all ye who enter here
2281 */
2282 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2283 iclog->ic_state == XLOG_STATE_SYNCING ||
2284 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2285 iclog->ic_state == XLOG_STATE_IOERROR )
2286 break;
2287 iclog = iclog->ic_next;
2288 } while (first_iclog != iclog);
2289 }
2290#endif
2291
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002292 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2293 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002294 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002295
2296 if (wake)
Dave Chinnereb40a872010-12-21 12:09:01 +11002297 wake_up_all(&log->l_flush_wait);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300
2301/*
2302 * Finish transitioning this iclog to the dirty state.
2303 *
2304 * Make sure that we completely execute this routine only when this is
2305 * the last call to the iclog. There is a good chance that iclog flushes,
2306 * when we reach the end of the physical log, get turned into 2 separate
2307 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2308 * routine. By using the reference count bwritecnt, we guarantee that only
2309 * the second completion goes through.
2310 *
2311 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002312 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 */
David Chinnera8272ce2007-11-23 16:28:09 +11002314STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315xlog_state_done_syncing(
2316 xlog_in_core_t *iclog,
2317 int aborted)
2318{
2319 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002321 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2324 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002325 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2327
2328
2329 /*
2330 * If we got an error, either on the first buffer, or in the case of
2331 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2332 * and none should ever be attempted to be written to disk
2333 * again.
2334 */
2335 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2336 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002337 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return;
2339 }
2340 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2341 }
2342
2343 /*
2344 * Someone could be sleeping prior to writing out the next
2345 * iclog buffer, we wake them all, one will get to do the
2346 * I/O, the others get to wait for the result.
2347 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002348 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002349 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2351} /* xlog_state_done_syncing */
2352
2353
2354/*
2355 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002356 * sleep. We wait on the flush queue on the head iclog as that should be
2357 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2358 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 *
2360 * The in-core logs are used in a circular fashion. They are not used
2361 * out-of-order even when an iclog past the head is free.
2362 *
2363 * return:
2364 * * log_offset where xlog_write() can start writing into the in-core
2365 * log's data space.
2366 * * in-core log pointer to which xlog_write() should write.
2367 * * boolean indicating this is a continued write to an in-core log.
2368 * If this is the last write, then the in-core log's offset field
2369 * needs to be incremented, depending on the amount of data which
2370 * is copied.
2371 */
David Chinnera8272ce2007-11-23 16:28:09 +11002372STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373xlog_state_get_iclog_space(xlog_t *log,
2374 int len,
2375 xlog_in_core_t **iclogp,
2376 xlog_ticket_t *ticket,
2377 int *continued_write,
2378 int *logoffsetp)
2379{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int log_offset;
2381 xlog_rec_header_t *head;
2382 xlog_in_core_t *iclog;
2383 int error;
2384
2385restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002386 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002388 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return XFS_ERROR(EIO);
2390 }
2391
2392 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002393 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002395
2396 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002397 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto restart;
2399 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 head = &iclog->ic_header;
2402
David Chinner155cc6b2008-03-06 13:44:14 +11002403 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 log_offset = iclog->ic_offset;
2405
2406 /* On the 1st write to an iclog, figure out lsn. This works
2407 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2408 * committing to. If the offset is set, that's how many blocks
2409 * must be written.
2410 */
2411 if (log_offset == 0) {
2412 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002413 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002414 log->l_iclog_hsize,
2415 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002416 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2417 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002418 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 ASSERT(log->l_curr_block >= 0);
2420 }
2421
2422 /* If there is enough room to write everything, then do it. Otherwise,
2423 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2424 * bit is on, so this will get flushed out. Don't update ic_offset
2425 * until you know exactly how many bytes get copied. Therefore, wait
2426 * until later to update ic_offset.
2427 *
2428 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2429 * can fit into remaining data section.
2430 */
2431 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2432 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2433
Dave Chinner49641f12008-07-11 17:43:55 +10002434 /*
2435 * If I'm the only one writing to this iclog, sync it to disk.
2436 * We need to do an atomic compare and decrement here to avoid
2437 * racing with concurrent atomic_dec_and_lock() calls in
2438 * xlog_state_release_iclog() when there is more than one
2439 * reference to the iclog.
2440 */
2441 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2442 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002443 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002444 error = xlog_state_release_iclog(log, iclog);
2445 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002446 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002448 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450 goto restart;
2451 }
2452
2453 /* Do we have enough room to write the full amount in the remainder
2454 * of this iclog? Or must we continue a write on the next iclog and
2455 * mark this iclog as completely taken? In the case where we switch
2456 * iclogs (to mark it taken), this particular iclog will release/sync
2457 * to disk in xlog_write().
2458 */
2459 if (len <= iclog->ic_size - iclog->ic_offset) {
2460 *continued_write = 0;
2461 iclog->ic_offset += len;
2462 } else {
2463 *continued_write = 1;
2464 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2465 }
2466 *iclogp = iclog;
2467
2468 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002469 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 *logoffsetp = log_offset;
2472 return 0;
2473} /* xlog_state_get_iclog_space */
2474
2475/*
2476 * Atomically get the log space required for a log ticket.
2477 *
2478 * Once a ticket gets put onto the reserveq, it will only return after
2479 * the needed reservation is satisfied.
2480 */
2481STATIC int
2482xlog_grant_log_space(xlog_t *log,
2483 xlog_ticket_t *tic)
2484{
2485 int free_bytes;
2486 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488#ifdef DEBUG
2489 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2490 panic("grant Recovery problem");
2491#endif
2492
2493 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002494 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002495
2496 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* something is already sleeping; insert new transaction at end */
Dave Chinner10547942010-12-21 12:02:25 +11002499 if (!list_empty(&log->l_reserveq)) {
2500 list_add_tail(&tic->t_queue, &log->l_reserveq);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002501
2502 trace_xfs_log_grant_sleep1(log, tic);
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 /*
2505 * Gotta check this before going to sleep, while we're
2506 * holding the grant lock.
2507 */
2508 if (XLOG_FORCED_SHUTDOWN(log))
2509 goto error_return;
2510
2511 XFS_STATS_INC(xs_sleep_logspace);
Dave Chinnereb40a872010-12-21 12:09:01 +11002512 xlog_wait(&tic->t_wait, &log->l_grant_lock);
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /*
2515 * If we got an error, and the filesystem is shutting down,
2516 * we'll catch it down below. So just continue...
2517 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002518 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002519 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2522 need_bytes = tic->t_unit_res*tic->t_ocnt;
2523 else
2524 need_bytes = tic->t_unit_res;
2525
2526redo:
2527 if (XLOG_FORCED_SHUTDOWN(log))
2528 goto error_return;
2529
Dave Chinnera69ed032010-12-21 12:08:20 +11002530 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (free_bytes < need_bytes) {
Dave Chinner10547942010-12-21 12:02:25 +11002532 if (list_empty(&tic->t_queue))
2533 list_add_tail(&tic->t_queue, &log->l_reserveq);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002534
2535 trace_xfs_log_grant_sleep2(log, tic);
2536
Dave Chinner2ced19c2010-12-21 12:09:20 +11002537 xlog_grant_push_ail(log, need_bytes);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 XFS_STATS_INC(xs_sleep_logspace);
Dave Chinnereb40a872010-12-21 12:09:01 +11002540 xlog_wait(&tic->t_wait, &log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Dave Chinner9d7fef72009-04-06 18:42:59 +02002542 spin_lock(&log->l_grant_lock);
2543 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002546 trace_xfs_log_grant_wake2(log, tic);
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 goto redo;
Dave Chinner10547942010-12-21 12:02:25 +11002549 }
2550
2551 list_del_init(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 /* we've got enough space */
Dave Chinnera69ed032010-12-21 12:08:20 +11002554 xlog_grant_add_space(log, &log->l_grant_reserve_head, need_bytes);
2555 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002556 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 xlog_verify_grant_head(log, 1);
Dave Chinner3f336c62010-12-21 12:02:52 +11002558 xlog_verify_grant_tail(log);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002559 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return 0;
2561
2562 error_return:
Dave Chinner10547942010-12-21 12:02:25 +11002563 list_del_init(&tic->t_queue);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002564 trace_xfs_log_grant_error(log, tic);
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /*
2567 * If we are failing, make sure the ticket doesn't have any
2568 * current reservations. We don't want to add this back when
2569 * the ticket/transaction gets cancelled.
2570 */
2571 tic->t_curr_res = 0;
2572 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002573 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return XFS_ERROR(EIO);
2575} /* xlog_grant_log_space */
2576
2577
2578/*
2579 * Replenish the byte reservation required by moving the grant write head.
2580 *
2581 *
2582 */
2583STATIC int
2584xlog_regrant_write_log_space(xlog_t *log,
2585 xlog_ticket_t *tic)
2586{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 int free_bytes, need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002590 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595#ifdef DEBUG
2596 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2597 panic("regrant Recovery problem");
2598#endif
2599
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002600 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002601
2602 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 if (XLOG_FORCED_SHUTDOWN(log))
2605 goto error_return;
2606
2607 /* If there are other waiters on the queue then give them a
2608 * chance at logspace before us. Wake up the first waiters,
2609 * if we do not wake up all the waiters then go to sleep waiting
2610 * for more free space, otherwise try to get some space for
2611 * this transaction.
2612 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002613 need_bytes = tic->t_unit_res;
Dave Chinner10547942010-12-21 12:02:25 +11002614 if (!list_empty(&log->l_writeq)) {
2615 struct xlog_ticket *ntic;
Dave Chinnera69ed032010-12-21 12:08:20 +11002616 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Dave Chinner10547942010-12-21 12:02:25 +11002617 list_for_each_entry(ntic, &log->l_writeq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2619
2620 if (free_bytes < ntic->t_unit_res)
2621 break;
2622 free_bytes -= ntic->t_unit_res;
Dave Chinnereb40a872010-12-21 12:09:01 +11002623 wake_up(&ntic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +11002624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Dave Chinner10547942010-12-21 12:02:25 +11002626 if (ntic != list_first_entry(&log->l_writeq,
2627 struct xlog_ticket, t_queue)) {
2628 if (list_empty(&tic->t_queue))
2629 list_add_tail(&tic->t_queue, &log->l_writeq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002631 trace_xfs_log_regrant_write_sleep1(log, tic);
2632
Dave Chinner2ced19c2010-12-21 12:09:20 +11002633 xlog_grant_push_ail(log, need_bytes);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 XFS_STATS_INC(xs_sleep_logspace);
Dave Chinnereb40a872010-12-21 12:09:01 +11002636 xlog_wait(&tic->t_wait, &log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 /* If we're shutting down, this tic is already
2639 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002640 spin_lock(&log->l_grant_lock);
2641 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002644 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
2646 }
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648redo:
2649 if (XLOG_FORCED_SHUTDOWN(log))
2650 goto error_return;
2651
Dave Chinnera69ed032010-12-21 12:08:20 +11002652 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (free_bytes < need_bytes) {
Dave Chinner10547942010-12-21 12:02:25 +11002654 if (list_empty(&tic->t_queue))
2655 list_add_tail(&tic->t_queue, &log->l_writeq);
Dave Chinner2ced19c2010-12-21 12:09:20 +11002656 xlog_grant_push_ail(log, need_bytes);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002659 trace_xfs_log_regrant_write_sleep2(log, tic);
Dave Chinnereb40a872010-12-21 12:09:01 +11002660 xlog_wait(&tic->t_wait, &log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002663 spin_lock(&log->l_grant_lock);
2664 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002667 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 goto redo;
Dave Chinner10547942010-12-21 12:02:25 +11002669 }
2670
2671 list_del_init(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002673 /* we've got enough space */
Dave Chinnera69ed032010-12-21 12:08:20 +11002674 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002675 trace_xfs_log_regrant_write_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 xlog_verify_grant_head(log, 1);
Dave Chinner3f336c62010-12-21 12:02:52 +11002677 xlog_verify_grant_tail(log);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002678 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681
2682 error_return:
Dave Chinner10547942010-12-21 12:02:25 +11002683 list_del_init(&tic->t_queue);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002684 trace_xfs_log_regrant_write_error(log, tic);
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 /*
2687 * If we are failing, make sure the ticket doesn't have any
2688 * current reservations. We don't want to add this back when
2689 * the ticket/transaction gets cancelled.
2690 */
2691 tic->t_curr_res = 0;
2692 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002693 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return XFS_ERROR(EIO);
2695} /* xlog_regrant_write_log_space */
2696
2697
2698/* The first cnt-1 times through here we don't need to
2699 * move the grant write head because the permanent
2700 * reservation has reserved cnt times the unit amount.
2701 * Release part of current permanent unit reservation and
2702 * reset current reservation to be one units worth. Also
2703 * move grant reservation head forward.
2704 */
2705STATIC void
2706xlog_regrant_reserve_log_space(xlog_t *log,
2707 xlog_ticket_t *ticket)
2708{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002709 trace_xfs_log_regrant_reserve_enter(log, ticket);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (ticket->t_cnt > 0)
2712 ticket->t_cnt--;
2713
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002714 spin_lock(&log->l_grant_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +11002715 xlog_grant_sub_space(log, &log->l_grant_reserve_head,
2716 ticket->t_curr_res);
2717 xlog_grant_sub_space(log, &log->l_grant_write_head,
2718 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002720 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002721
2722 trace_xfs_log_regrant_reserve_sub(log, ticket);
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 xlog_verify_grant_head(log, 1);
2725
2726 /* just return if we still have some of the pre-reserved space */
2727 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002728 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return;
2730 }
2731
Dave Chinnera69ed032010-12-21 12:08:20 +11002732 xlog_grant_add_space(log, &log->l_grant_reserve_head,
2733 ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002734
2735 trace_xfs_log_regrant_reserve_exit(log, ticket);
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002738 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002740 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741} /* xlog_regrant_reserve_log_space */
2742
2743
2744/*
2745 * Give back the space left from a reservation.
2746 *
2747 * All the information we need to make a correct determination of space left
2748 * is present. For non-permanent reservations, things are quite easy. The
2749 * count should have been decremented to zero. We only need to deal with the
2750 * space remaining in the current reservation part of the ticket. If the
2751 * ticket contains a permanent reservation, there may be left over space which
2752 * needs to be released. A count of N means that N-1 refills of the current
2753 * reservation can be done before we need to ask for more space. The first
2754 * one goes to fill up the first current reservation. Once we run out of
2755 * space, the count will stay at zero and the only space remaining will be
2756 * in the current reservation field.
2757 */
2758STATIC void
2759xlog_ungrant_log_space(xlog_t *log,
2760 xlog_ticket_t *ticket)
2761{
Dave Chinner663e4962010-12-21 12:06:05 +11002762 int bytes;
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (ticket->t_cnt > 0)
2765 ticket->t_cnt--;
2766
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002767 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002768 trace_xfs_log_ungrant_enter(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002769 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Dave Chinner663e4962010-12-21 12:06:05 +11002771 /*
2772 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * up more space based on the remaining count.
2774 */
Dave Chinner663e4962010-12-21 12:06:05 +11002775 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (ticket->t_cnt > 0) {
2777 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11002778 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780
Dave Chinnera69ed032010-12-21 12:08:20 +11002781 xlog_grant_sub_space(log, &log->l_grant_reserve_head, bytes);
2782 xlog_grant_sub_space(log, &log->l_grant_write_head, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11002783
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002784 trace_xfs_log_ungrant_exit(log, ticket);
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002787 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 xfs_log_move_tail(log->l_mp, 1);
2789} /* xlog_ungrant_log_space */
2790
2791
2792/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 * Flush iclog to disk if this is the last reference to the given iclog and
2794 * the WANT_SYNC bit is set.
2795 *
2796 * When this function is entered, the iclog is not necessarily in the
2797 * WANT_SYNC state. It may be sitting around waiting to get filled.
2798 *
2799 *
2800 */
David Chinnera8272ce2007-11-23 16:28:09 +11002801STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002802xlog_state_release_iclog(
2803 xlog_t *log,
2804 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 int sync = 0; /* do we sync? */
2807
David Chinner155cc6b2008-03-06 13:44:14 +11002808 if (iclog->ic_state & XLOG_STATE_IOERROR)
2809 return XFS_ERROR(EIO);
2810
2811 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2812 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2813 return 0;
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002816 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return XFS_ERROR(EIO);
2818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2820 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2821
David Chinner155cc6b2008-03-06 13:44:14 +11002822 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002823 /* update tail before writing to iclog */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002824 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 sync++;
2826 iclog->ic_state = XLOG_STATE_SYNCING;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002827 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
2828 xlog_verify_tail_lsn(log, iclog, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /* cycle incremented when incrementing curr_block */
2830 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002831 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 /*
2834 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002835 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2837 * this iclog has consistent data, so we ignore IOERROR
2838 * flags after this point.
2839 */
David Chinnerb5893342008-03-06 13:44:06 +11002840 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843} /* xlog_state_release_iclog */
2844
2845
2846/*
2847 * This routine will mark the current iclog in the ring as WANT_SYNC
2848 * and move the current iclog pointer to the next iclog in the ring.
2849 * When this routine is called from xlog_state_get_iclog_space(), the
2850 * exact size of the iclog has not yet been determined. All we know is
2851 * that every data block. We have run out of space in this log record.
2852 */
2853STATIC void
2854xlog_state_switch_iclogs(xlog_t *log,
2855 xlog_in_core_t *iclog,
2856 int eventual_size)
2857{
2858 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2859 if (!eventual_size)
2860 eventual_size = iclog->ic_offset;
2861 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002862 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 log->l_prev_block = log->l_curr_block;
2864 log->l_prev_cycle = log->l_curr_cycle;
2865
2866 /* roll log?: ic_offset changed later */
2867 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2868
2869 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002870 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 log->l_mp->m_sb.sb_logsunit > 1) {
2872 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2873 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2874 }
2875
2876 if (log->l_curr_block >= log->l_logBBsize) {
2877 log->l_curr_cycle++;
2878 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2879 log->l_curr_cycle++;
2880 log->l_curr_block -= log->l_logBBsize;
2881 ASSERT(log->l_curr_block >= 0);
2882 }
2883 ASSERT(iclog == log->l_iclog);
2884 log->l_iclog = iclog->ic_next;
2885} /* xlog_state_switch_iclogs */
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887/*
2888 * Write out all data in the in-core log as of this exact moment in time.
2889 *
2890 * Data may be written to the in-core log during this call. However,
2891 * we don't guarantee this data will be written out. A change from past
2892 * implementation means this routine will *not* write out zero length LRs.
2893 *
2894 * Basically, we try and perform an intelligent scan of the in-core logs.
2895 * If we determine there is no flushable data, we just return. There is no
2896 * flushable data if:
2897 *
2898 * 1. the current iclog is active and has no data; the previous iclog
2899 * is in the active or dirty state.
2900 * 2. the current iclog is drity, and the previous iclog is in the
2901 * active or dirty state.
2902 *
David Chinner12017fa2008-08-13 16:34:31 +10002903 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 *
2905 * 1. the current iclog is not in the active nor dirty state.
2906 * 2. the current iclog dirty, and the previous iclog is not in the
2907 * active nor dirty state.
2908 * 3. the current iclog is active, and there is another thread writing
2909 * to this particular iclog.
2910 * 4. a) the current iclog is active and has no other writers
2911 * b) when we return from flushing out this iclog, it is still
2912 * not in the active nor dirty state.
2913 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002914int
2915_xfs_log_force(
2916 struct xfs_mount *mp,
2917 uint flags,
2918 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002920 struct log *log = mp->m_log;
2921 struct xlog_in_core *iclog;
2922 xfs_lsn_t lsn;
2923
2924 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Dave Chinnera44f13e2010-08-24 11:40:03 +10002926 if (log->l_cilp)
2927 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002928
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002929 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 iclog = log->l_iclog;
2932 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002933 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return XFS_ERROR(EIO);
2935 }
2936
2937 /* If the head iclog is not active nor dirty, we just attach
2938 * ourselves to the head and go to sleep.
2939 */
2940 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2941 iclog->ic_state == XLOG_STATE_DIRTY) {
2942 /*
2943 * If the head is dirty or (active and empty), then
2944 * we need to look at the previous iclog. If the previous
2945 * iclog is active or dirty we are done. There is nothing
2946 * to sync out. Otherwise, we attach ourselves to the
2947 * previous iclog and go to sleep.
2948 */
2949 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002950 (atomic_read(&iclog->ic_refcnt) == 0
2951 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 iclog = iclog->ic_prev;
2953 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2954 iclog->ic_state == XLOG_STATE_DIRTY)
2955 goto no_sleep;
2956 else
2957 goto maybe_sleep;
2958 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002959 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* We are the only one with access to this
2961 * iclog. Flush it out now. There should
2962 * be a roundoff of zero to show that someone
2963 * has already taken care of the roundoff from
2964 * the previous sync.
2965 */
David Chinner155cc6b2008-03-06 13:44:14 +11002966 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002967 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002969 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 if (xlog_state_release_iclog(log, iclog))
2972 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002973
2974 if (log_flushed)
2975 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002976 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002977 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 iclog->ic_state != XLOG_STATE_DIRTY)
2979 goto maybe_sleep;
2980 else
2981 goto no_sleep;
2982 } else {
2983 /* Someone else is writing to this iclog.
2984 * Use its call to flush out the data. However,
2985 * the other thread may not force out this LR,
2986 * so we mark it WANT_SYNC.
2987 */
2988 xlog_state_switch_iclogs(log, iclog, 0);
2989 goto maybe_sleep;
2990 }
2991 }
2992 }
2993
2994 /* By the time we come around again, the iclog could've been filled
2995 * which would give it another lsn. If we have a new lsn, just
2996 * return because the relevant data has been flushed.
2997 */
2998maybe_sleep:
2999 if (flags & XFS_LOG_SYNC) {
3000 /*
3001 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003002 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 * Then we check again after waking up, in case our
3004 * sleep was disturbed by a bad news.
3005 */
3006 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003007 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return XFS_ERROR(EIO);
3009 }
3010 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003011 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 /*
3013 * No need to grab the log lock here since we're
3014 * only deciding whether or not to return EIO
3015 * and the memory read should be atomic.
3016 */
3017 if (iclog->ic_state & XLOG_STATE_IOERROR)
3018 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003019 if (log_flushed)
3020 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 } else {
3022
3023no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003024 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
3026 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003030 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
3031 * about errors or whether the log was flushed or not. This is the normal
3032 * interface to use when trying to unpin items or move the log forward.
3033 */
3034void
3035xfs_log_force(
3036 xfs_mount_t *mp,
3037 uint flags)
3038{
3039 int error;
3040
3041 error = _xfs_log_force(mp, flags, NULL);
3042 if (error) {
3043 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3044 "error %d returned.", error);
3045 }
3046}
3047
3048/*
3049 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 *
3051 * Find in-core log with lsn.
3052 * If it is in the DIRTY state, just return.
3053 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3054 * state and go to sleep or return.
3055 * If it is in any other state, go to sleep or return.
3056 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003057 * Synchronous forces are implemented with a signal variable. All callers
3058 * to force a given lsn to disk will wait on a the sv attached to the
3059 * specific in-core log. When given in-core log finally completes its
3060 * write to disk, that thread will wake up all threads waiting on the
3061 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003063int
3064_xfs_log_force_lsn(
3065 struct xfs_mount *mp,
3066 xfs_lsn_t lsn,
3067 uint flags,
3068 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003070 struct log *log = mp->m_log;
3071 struct xlog_in_core *iclog;
3072 int already_slept = 0;
3073
3074 ASSERT(lsn != 0);
3075
3076 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Dave Chinner71e330b2010-05-21 14:37:18 +10003078 if (log->l_cilp) {
Dave Chinnera44f13e2010-08-24 11:40:03 +10003079 lsn = xlog_cil_force_lsn(log, lsn);
Dave Chinner71e330b2010-05-21 14:37:18 +10003080 if (lsn == NULLCOMMITLSN)
3081 return 0;
3082 }
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003085 spin_lock(&log->l_icloglock);
3086 iclog = log->l_iclog;
3087 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003088 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003089 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003092 do {
3093 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3094 iclog = iclog->ic_next;
3095 continue;
3096 }
3097
3098 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3099 spin_unlock(&log->l_icloglock);
3100 return 0;
3101 }
3102
3103 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3104 /*
3105 * We sleep here if we haven't already slept (e.g.
3106 * this is the first time we've looked at the correct
3107 * iclog buf) and the buffer before us is going to
3108 * be sync'ed. The reason for this is that if we
3109 * are doing sync transactions here, by waiting for
3110 * the previous I/O to complete, we can allow a few
3111 * more transactions into this iclog before we close
3112 * it down.
3113 *
3114 * Otherwise, we mark the buffer WANT_SYNC, and bump
3115 * up the refcnt so we can release the log (which
3116 * drops the ref count). The state switch keeps new
3117 * transaction commits from using this buffer. When
3118 * the current commits finish writing into the buffer,
3119 * the refcount will drop to zero and the buffer will
3120 * go out then.
3121 */
3122 if (!already_slept &&
3123 (iclog->ic_prev->ic_state &
3124 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3125 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3126
3127 XFS_STATS_INC(xs_log_force_sleep);
3128
Dave Chinnereb40a872010-12-21 12:09:01 +11003129 xlog_wait(&iclog->ic_prev->ic_write_wait,
3130 &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003131 if (log_flushed)
3132 *log_flushed = 1;
3133 already_slept = 1;
3134 goto try_again;
3135 }
David Chinner155cc6b2008-03-06 13:44:14 +11003136 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003138 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (xlog_state_release_iclog(log, iclog))
3140 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003141 if (log_flushed)
3142 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003143 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003146 if ((flags & XFS_LOG_SYNC) && /* sleep */
3147 !(iclog->ic_state &
3148 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3149 /*
3150 * Don't wait on completion if we know that we've
3151 * gotten a log write error.
3152 */
3153 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3154 spin_unlock(&log->l_icloglock);
3155 return XFS_ERROR(EIO);
3156 }
3157 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003158 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003159 /*
3160 * No need to grab the log lock here since we're
3161 * only deciding whether or not to return EIO
3162 * and the memory read should be atomic.
3163 */
3164 if (iclog->ic_state & XLOG_STATE_IOERROR)
3165 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003167 if (log_flushed)
3168 *log_flushed = 1;
3169 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003170 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003172
3173 return 0;
3174 } while (iclog != log->l_iclog);
3175
3176 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003180/*
3181 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3182 * about errors or whether the log was flushed or not. This is the normal
3183 * interface to use when trying to unpin items or move the log forward.
3184 */
3185void
3186xfs_log_force_lsn(
3187 xfs_mount_t *mp,
3188 xfs_lsn_t lsn,
3189 uint flags)
3190{
3191 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003193 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3194 if (error) {
3195 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3196 "error %d returned.", error);
3197 }
3198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200/*
3201 * Called when we want to mark the current iclog as being ready to sync to
3202 * disk.
3203 */
David Chinnera8272ce2007-11-23 16:28:09 +11003204STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3206{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003207 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3210 xlog_state_switch_iclogs(log, iclog, 0);
3211 } else {
3212 ASSERT(iclog->ic_state &
3213 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3214 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003215}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217
3218/*****************************************************************************
3219 *
3220 * TICKET functions
3221 *
3222 *****************************************************************************
3223 */
3224
3225/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003226 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003228void
3229xfs_log_ticket_put(
3230 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003232 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003233 if (atomic_dec_and_test(&ticket->t_ref))
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003234 kmem_zone_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003237xlog_ticket_t *
3238xfs_log_ticket_get(
3239 xlog_ticket_t *ticket)
3240{
3241 ASSERT(atomic_read(&ticket->t_ref) > 0);
3242 atomic_inc(&ticket->t_ref);
3243 return ticket;
3244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Dave Chinner955833c2010-05-14 21:41:46 +10003246xlog_tid_t
3247xfs_log_get_trans_ident(
3248 struct xfs_trans *tp)
3249{
3250 return tp->t_ticket->t_tid;
3251}
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003254 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003256xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003257xlog_ticket_alloc(
3258 struct log *log,
3259 int unit_bytes,
3260 int cnt,
3261 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003262 uint xflags,
3263 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003265 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003267 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Dave Chinner3383ca52010-05-07 11:04:17 +10003269 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003270 if (!tic)
3271 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /*
3274 * Permanent reservations have up to 'cnt'-1 active log operations
3275 * in the log. A unit in this case is the amount of space for one
3276 * of these log operations. Normal reservations have a cnt of 1
3277 * and their unit amount is the total amount of space required.
3278 *
3279 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003280 * which occupy space in the on-disk log.
3281 *
3282 * Normal form of a transaction is:
3283 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3284 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3285 *
3286 * We need to account for all the leadup data and trailer data
3287 * around the transaction data.
3288 * And then we need to account for the worst case in terms of using
3289 * more space.
3290 * The worst case will happen if:
3291 * - the placement of the transaction happens to be such that the
3292 * roundoff is at its maximum
3293 * - the transaction data is synced before the commit record is synced
3294 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3295 * Therefore the commit record is in its own Log Record.
3296 * This can happen as the commit record is called with its
3297 * own region to xlog_write().
3298 * This then means that in the worst case, roundoff can happen for
3299 * the commit-rec as well.
3300 * The commit-rec is smaller than padding in this scenario and so it is
3301 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 */
3303
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003304 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003306 unit_bytes += sizeof(xfs_trans_header_t);
3307
3308 /* for start-rec */
3309 unit_bytes += sizeof(xlog_op_header_t);
3310
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003311 /*
3312 * for LR headers - the space for data in an iclog is the size minus
3313 * the space used for the headers. If we use the iclog size, then we
3314 * undercalculate the number of headers required.
3315 *
3316 * Furthermore - the addition of op headers for split-recs might
3317 * increase the space required enough to require more log and op
3318 * headers, so take that into account too.
3319 *
3320 * IMPORTANT: This reservation makes the assumption that if this
3321 * transaction is the first in an iclog and hence has the LR headers
3322 * accounted to it, then the remaining space in the iclog is
3323 * exclusively for this transaction. i.e. if the transaction is larger
3324 * than the iclog, it will be the only thing in that iclog.
3325 * Fundamentally, this means we must pass the entire log vector to
3326 * xlog_write to guarantee this.
3327 */
3328 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3329 num_headers = howmany(unit_bytes, iclog_space);
3330
3331 /* for split-recs - ophdrs added when data split over LRs */
3332 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3333
3334 /* add extra header reservations if we overrun */
3335 while (!num_headers ||
3336 howmany(unit_bytes, iclog_space) > num_headers) {
3337 unit_bytes += sizeof(xlog_op_header_t);
3338 num_headers++;
3339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 unit_bytes += log->l_iclog_hsize * num_headers;
3341
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003342 /* for commit-rec LR header - note: padding will subsume the ophdr */
3343 unit_bytes += log->l_iclog_hsize;
3344
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003345 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003346 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003347 log->l_mp->m_sb.sb_logsunit > 1) {
3348 /* log su roundoff */
3349 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3350 } else {
3351 /* BB roundoff */
3352 unit_bytes += 2*BBSIZE;
3353 }
3354
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003355 atomic_set(&tic->t_ref, 1);
Dave Chinner10547942010-12-21 12:02:25 +11003356 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 tic->t_unit_res = unit_bytes;
3358 tic->t_curr_res = unit_bytes;
3359 tic->t_cnt = cnt;
3360 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003361 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 tic->t_clientid = client;
3363 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003364 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 if (xflags & XFS_LOG_PERM_RESERV)
3366 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Dave Chinnereb40a872010-12-21 12:09:01 +11003367 init_waitqueue_head(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Christoph Hellwig0adba532007-08-30 17:21:46 +10003369 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
3374
3375/******************************************************************************
3376 *
3377 * Log debug routines
3378 *
3379 ******************************************************************************
3380 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003381#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382/*
3383 * Make sure that the destination ptr is within the valid data region of
3384 * one of the iclogs. This uses backup pointers stored in a different
3385 * part of the log in case we trash the log structure.
3386 */
3387void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003388xlog_verify_dest_ptr(
3389 struct log *log,
3390 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 int i;
3393 int good_ptr = 0;
3394
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003395 for (i = 0; i < log->l_iclog_bufs; i++) {
3396 if (ptr >= log->l_iclog_bak[i] &&
3397 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 good_ptr++;
3399 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003400
3401 if (!good_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
3405STATIC void
3406xlog_verify_grant_head(xlog_t *log, int equals)
3407{
Dave Chinnera69ed032010-12-21 12:08:20 +11003408 int reserve_cycle, reserve_space;
3409 int write_cycle, write_space;
3410
3411 xlog_crack_grant_head(&log->l_grant_reserve_head,
3412 &reserve_cycle, &reserve_space);
3413 xlog_crack_grant_head(&log->l_grant_write_head,
3414 &write_cycle, &write_space);
3415
3416 if (reserve_cycle == write_cycle) {
3417 if (equals)
3418 ASSERT(reserve_space >= write_space);
3419 else
3420 ASSERT(reserve_space > write_space);
3421 } else {
3422 ASSERT(reserve_cycle - 1 == write_cycle);
3423 ASSERT(write_space >= reserve_space);
3424 }
3425}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Dave Chinner3f336c62010-12-21 12:02:52 +11003427STATIC void
3428xlog_verify_grant_tail(
3429 struct log *log)
3430{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003431 int tail_cycle, tail_blocks;
Dave Chinnera69ed032010-12-21 12:08:20 +11003432 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003433
3434 /*
3435 * Check to make sure the grant write head didn't just over lap the
3436 * tail. If the cycles are the same, we can't be overlapping.
3437 * Otherwise, make sure that the cycles differ by exactly one and
3438 * check the byte count.
3439 */
Dave Chinnera69ed032010-12-21 12:08:20 +11003440 xlog_crack_grant_head(&log->l_grant_write_head, &cycle, &space);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003441 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3442 if (tail_cycle != cycle) {
3443 ASSERT(cycle - 1 == tail_cycle);
3444 ASSERT(space <= BBTOB(tail_blocks));
Dave Chinner3f336c62010-12-21 12:02:52 +11003445 }
3446}
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448/* check if it will fit */
3449STATIC void
3450xlog_verify_tail_lsn(xlog_t *log,
3451 xlog_in_core_t *iclog,
3452 xfs_lsn_t tail_lsn)
3453{
3454 int blocks;
3455
3456 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3457 blocks =
3458 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3459 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3460 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3461 } else {
3462 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3463
3464 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3465 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3466
3467 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3468 if (blocks < BTOBB(iclog->ic_offset) + 1)
3469 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3470 }
3471} /* xlog_verify_tail_lsn */
3472
3473/*
3474 * Perform a number of checks on the iclog before writing to disk.
3475 *
3476 * 1. Make sure the iclogs are still circular
3477 * 2. Make sure we have a good magic number
3478 * 3. Make sure we don't have magic numbers in the data
3479 * 4. Check fields of each log operation header for:
3480 * A. Valid client identifier
3481 * B. tid ptr value falls in valid ptr space (user space code)
3482 * C. Length in log record header is correct according to the
3483 * individual operation headers within record.
3484 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3485 * log, check the preceding blocks of the physical log to make sure all
3486 * the cycle numbers agree with the current cycle number.
3487 */
3488STATIC void
3489xlog_verify_iclog(xlog_t *log,
3490 xlog_in_core_t *iclog,
3491 int count,
3492 boolean_t syncing)
3493{
3494 xlog_op_header_t *ophead;
3495 xlog_in_core_t *icptr;
3496 xlog_in_core_2_t *xhdr;
3497 xfs_caddr_t ptr;
3498 xfs_caddr_t base_ptr;
3499 __psint_t field_offset;
3500 __uint8_t clientid;
3501 int len, i, j, k, op_len;
3502 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003505 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 icptr = log->l_iclog;
3507 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003508 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 xlog_panic("xlog_verify_iclog: invalid ptr");
3510 icptr = icptr->ic_next;
3511 }
3512 if (icptr != log->l_iclog)
3513 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003514 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
3516 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003517 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 xlog_panic("xlog_verify_iclog: invalid magic num");
3519
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003520 ptr = (xfs_caddr_t) &iclog->ic_header;
3521 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003523 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 xlog_panic("xlog_verify_iclog: unexpected magic num");
3525 }
3526
3527 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003528 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 ptr = iclog->ic_datap;
3530 base_ptr = ptr;
3531 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003532 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 for (i = 0; i < len; i++) {
3534 ophead = (xlog_op_header_t *)ptr;
3535
3536 /* clientid is only 1 byte */
3537 field_offset = (__psint_t)
3538 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3539 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3540 clientid = ophead->oh_clientid;
3541 } else {
3542 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3543 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3544 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3545 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003546 clientid = xlog_get_client_id(
3547 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003549 clientid = xlog_get_client_id(
3550 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
3552 }
3553 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003554 cmn_err(CE_WARN, "xlog_verify_iclog: "
3555 "invalid clientid %d op 0x%p offset 0x%lx",
3556 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 /* check length */
3559 field_offset = (__psint_t)
3560 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3561 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003562 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 } else {
3564 idx = BTOBBT((__psint_t)&ophead->oh_len -
3565 (__psint_t)iclog->ic_datap);
3566 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3567 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3568 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003569 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003571 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
3573 }
3574 ptr += sizeof(xlog_op_header_t) + op_len;
3575 }
3576} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003580 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 */
3582STATIC int
3583xlog_state_ioerror(
3584 xlog_t *log)
3585{
3586 xlog_in_core_t *iclog, *ic;
3587
3588 iclog = log->l_iclog;
3589 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3590 /*
3591 * Mark all the incore logs IOERROR.
3592 * From now on, no log flushes will result.
3593 */
3594 ic = iclog;
3595 do {
3596 ic->ic_state = XLOG_STATE_IOERROR;
3597 ic = ic->ic_next;
3598 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
3601 /*
3602 * Return non-zero, if state transition has already happened.
3603 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003604 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
3607/*
3608 * This is called from xfs_force_shutdown, when we're forcibly
3609 * shutting down the filesystem, typically because of an IO error.
3610 * Our main objectives here are to make sure that:
3611 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3612 * parties to find out, 'atomically'.
3613 * b. those who're sleeping on log reservations, pinned objects and
3614 * other resources get woken up, and be told the bad news.
3615 * c. nothing new gets queued up after (a) and (b) are done.
3616 * d. if !logerror, flush the iclogs to disk, then seal them off
3617 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003618 *
3619 * Note: for delayed logging the !logerror case needs to flush the regions
3620 * held in memory out to the iclogs before flushing them to disk. This needs
3621 * to be done before the log is marked as shutdown, otherwise the flush to the
3622 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
3624int
3625xfs_log_force_umount(
3626 struct xfs_mount *mp,
3627 int logerror)
3628{
3629 xlog_ticket_t *tic;
3630 xlog_t *log;
3631 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633 log = mp->m_log;
3634
3635 /*
3636 * If this happens during log recovery, don't worry about
3637 * locking; the log isn't open for business yet.
3638 */
3639 if (!log ||
3640 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3641 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003642 if (mp->m_sb_bp)
3643 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 }
3646
3647 /*
3648 * Somebody could've already done the hard work for us.
3649 * No need to get locks for this.
3650 */
3651 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3652 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003653 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
3655 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003656
3657 /*
3658 * Flush the in memory commit item list before marking the log as
3659 * being shut down. We need to do it in this order to ensure all the
3660 * completed transactions are flushed to disk with the xfs_log_force()
3661 * call below.
3662 */
3663 if (!logerror && (mp->m_flags & XFS_MOUNT_DELAYLOG))
Dave Chinnera44f13e2010-08-24 11:40:03 +10003664 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 /*
3667 * We must hold both the GRANT lock and the LOG lock,
3668 * before we mark the filesystem SHUTDOWN and wake
3669 * everybody up to tell the bad news.
3670 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003671 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003672 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003674 if (mp->m_sb_bp)
3675 XFS_BUF_DONE(mp->m_sb_bp);
3676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 /*
3678 * This flag is sort of redundant because of the mount flag, but
3679 * it's good to maintain the separation between the log and the rest
3680 * of XFS.
3681 */
3682 log->l_flags |= XLOG_IO_ERROR;
3683
3684 /*
3685 * If we hit a log error, we want to mark all the iclogs IOERROR
3686 * while we're still holding the loglock.
3687 */
3688 if (logerror)
3689 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003690 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692 /*
Dave Chinner10547942010-12-21 12:02:25 +11003693 * We don't want anybody waiting for log reservations after this. That
3694 * means we have to wake up everybody queued up on reserveq as well as
3695 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3696 * we don't enqueue anything once the SHUTDOWN flag is set, and this
3697 * action is protected by the GRANTLOCK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 */
Dave Chinner10547942010-12-21 12:02:25 +11003699 list_for_each_entry(tic, &log->l_reserveq, t_queue)
Dave Chinnereb40a872010-12-21 12:09:01 +11003700 wake_up(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Dave Chinner10547942010-12-21 12:02:25 +11003702 list_for_each_entry(tic, &log->l_writeq, t_queue)
Dave Chinnereb40a872010-12-21 12:09:01 +11003703 wake_up(&tic->t_wait);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003704 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003706 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 ASSERT(!logerror);
3708 /*
3709 * Force the incore logs to disk before shutting the
3710 * log down completely.
3711 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003712 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3713
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003714 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003716 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 }
3718 /*
3719 * Wake up everybody waiting on xfs_log_force.
3720 * Callback all log item committed functions as if the
3721 * log writes were completed.
3722 */
3723 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3724
3725#ifdef XFSERRORDEBUG
3726 {
3727 xlog_in_core_t *iclog;
3728
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003729 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 iclog = log->l_iclog;
3731 do {
3732 ASSERT(iclog->ic_callback == 0);
3733 iclog = iclog->ic_next;
3734 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003735 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 }
3737#endif
3738 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003739 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
3741
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003742STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743xlog_iclogs_empty(xlog_t *log)
3744{
3745 xlog_in_core_t *iclog;
3746
3747 iclog = log->l_iclog;
3748 do {
3749 /* endianness does not matter here, zero is zero in
3750 * any language.
3751 */
3752 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 iclog = iclog->ic_next;
3755 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003756 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}