blob: 1e2020d5a8b64e850aa9d939e793c694621e09cb [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 Chinnera69ed032010-12-21 12:08:20 +110050STATIC int xlog_space_left(struct log *log, int64_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 Chinnera69ed032010-12-21 12:08:20 +1100103 int64_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 Chinnera69ed032010-12-21 12:08:20 +1100122 int64_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
678 if (tail_lsn == 0) {
679 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000680 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000682 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000685 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
688 * tail_lsn.
689 */
690 if (tail_lsn != 1) {
691 log->l_tail_lsn = tail_lsn;
692 }
693
Dave Chinner10547942010-12-21 12:02:25 +1100694 if (!list_empty(&log->l_writeq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#ifdef DEBUG
696 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
697 panic("Recovery problem");
698#endif
Dave Chinnera69ed032010-12-21 12:08:20 +1100699 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Dave Chinner10547942010-12-21 12:02:25 +1100700 list_for_each_entry(tic, &log->l_writeq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
702
703 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
704 break;
705 tail_lsn = 0;
706 free_bytes -= tic->t_unit_res;
Dave Chinnereb40a872010-12-21 12:09:01 +1100707 wake_up(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Dave Chinner10547942010-12-21 12:02:25 +1100710
711 if (!list_empty(&log->l_reserveq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712#ifdef DEBUG
713 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
714 panic("Recovery problem");
715#endif
Dave Chinnera69ed032010-12-21 12:08:20 +1100716 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Dave Chinner10547942010-12-21 12:02:25 +1100717 list_for_each_entry(tic, &log->l_reserveq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
719 need_bytes = tic->t_unit_res*tic->t_cnt;
720 else
721 need_bytes = tic->t_unit_res;
722 if (free_bytes < need_bytes && tail_lsn != 1)
723 break;
724 tail_lsn = 0;
725 free_bytes -= need_bytes;
Dave Chinnereb40a872010-12-21 12:09:01 +1100726 wake_up(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000729 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730} /* xfs_log_move_tail */
731
732/*
733 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000734 * that needs to be covered. To begin the transition to the idle state
735 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
736 * If we are then in a state where covering is needed, the caller is informed
737 * that dummy transactions are required to move the log into the idle state.
738 *
739 * Because this is called as part of the sync process, we should also indicate
740 * that dummy transactions should be issued in anything but the covered or
741 * idle states. This ensures that the log tail is accurately reflected in
742 * the log at the end of the sync, hence if a crash occurrs avoids replay
743 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
745int
746xfs_log_need_covered(xfs_mount_t *mp)
747{
David Chinner27d8d5f2008-10-30 17:38:39 +1100748 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
David Chinner92821e22007-05-24 15:26:31 +1000751 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000754 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000755 switch (log->l_covered_state) {
756 case XLOG_STATE_COVER_DONE:
757 case XLOG_STATE_COVER_DONE2:
758 case XLOG_STATE_COVER_IDLE:
759 break;
760 case XLOG_STATE_COVER_NEED:
761 case XLOG_STATE_COVER_NEED2:
762 if (!xfs_trans_ail_tail(log->l_ailp) &&
763 xlog_iclogs_empty(log)) {
764 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
765 log->l_covered_state = XLOG_STATE_COVER_DONE;
766 else
767 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000769 /* FALLTHRU */
770 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000774 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100775 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778/******************************************************************************
779 *
780 * local routines
781 *
782 ******************************************************************************
783 */
784
785/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
786 * The log manager must keep track of the last LR which was committed
787 * to disk. The lsn of this LR will become the new tail_lsn whenever
788 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
789 * the situation where stuff could be written into the log but nothing
790 * was ever in the AIL when asked. Eventually, we panic since the
791 * tail hits the head.
792 *
793 * We may be holding the log iclog lock upon entering this routine.
794 */
795xfs_lsn_t
796xlog_assign_tail_lsn(xfs_mount_t *mp)
797{
798 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xlog_t *log = mp->m_log;
800
David Chinner5b00f142008-10-30 17:39:00 +1100801 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000802 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (tail_lsn != 0) {
804 log->l_tail_lsn = tail_lsn;
805 } else {
806 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
807 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000808 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 return tail_lsn;
811} /* xlog_assign_tail_lsn */
812
813
814/*
815 * Return the space in the log between the tail and the head. The head
816 * is passed in the cycle/bytes formal parms. In the special case where
817 * the reserve head has wrapped passed the tail, this calculation is no
818 * longer valid. In this case, just return 0 which means there is no space
819 * in the log. This works for all places where this function is called
820 * with the reserve head. Of course, if the write head were to ever
821 * wrap the tail, we should blow up. Rather than catch this case here,
822 * we depend on other ASSERTions in other parts of the code. XXXmiken
823 *
824 * This code also handles the case where the reservation head is behind
825 * the tail. The details of this case are described below, but the end
826 * result is that we return the size of the log as the amount of space left.
827 */
David Chinnera8272ce2007-11-23 16:28:09 +1100828STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +1100829xlog_space_left(
830 struct log *log,
831 int64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Dave Chinnera69ed032010-12-21 12:08:20 +1100833 int free_bytes;
834 int tail_bytes;
835 int tail_cycle;
836 int head_cycle;
837 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Dave Chinnera69ed032010-12-21 12:08:20 +1100839 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
841 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
Dave Chinnera69ed032010-12-21 12:08:20 +1100842 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
843 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
844 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +1100846 else if (tail_cycle < head_cycle) {
847 ASSERT(tail_cycle == (head_cycle - 1));
848 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 } else {
850 /*
851 * The reservation head is behind the tail.
852 * In this case we just want to return the size of the
853 * log as the amount of space left.
854 */
855 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
856 "xlog_space_left: head behind tail\n"
857 " tail_cycle = %d, tail_bytes = %d\n"
858 " GH cycle = %d, GH bytes = %d",
Dave Chinnera69ed032010-12-21 12:08:20 +1100859 tail_cycle, tail_bytes, head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ASSERT(0);
861 free_bytes = log->l_logsize;
862 }
863 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +1100864}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866
867/*
868 * Log function which is called when an io completes.
869 *
870 * The log manager needs its own routine, in order to control what
871 * happens with the buffer after the write completes.
872 */
873void
874xlog_iodone(xfs_buf_t *bp)
875{
876 xlog_in_core_t *iclog;
877 xlog_t *l;
878 int aborted;
879
880 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
881 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
882 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
883 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 l = iclog->ic_log;
885
886 /*
887 * Race to shutdown the filesystem if we see an error.
888 */
889 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
890 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
891 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
892 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000893 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * This flag will be propagated to the trans-committed
896 * callback routines to let them know that the log-commit
897 * didn't succeed.
898 */
899 aborted = XFS_LI_ABORTED;
900 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
901 aborted = XFS_LI_ABORTED;
902 }
David Chinner3db296f2007-05-14 18:24:16 +1000903
904 /* log I/O is always issued ASYNC */
905 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000907 /*
908 * do not reference the buffer (bp) here as we could race
909 * with it being freed after writing the unmount record to the
910 * log.
911 */
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913} /* xlog_iodone */
914
915/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * Return size of each in-core log record buffer.
917 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200918 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 *
920 * If the filesystem blocksize is too large, we may need to choose a
921 * larger size since the directory code currently logs entire blocks.
922 */
923
924STATIC void
925xlog_get_iclog_buffer_size(xfs_mount_t *mp,
926 xlog_t *log)
927{
928 int size;
929 int xhdrs;
930
Eric Sandeen1cb51252007-08-16 16:24:43 +1000931 if (mp->m_logbufs <= 0)
932 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
933 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100934 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /*
937 * Buffer size passed in from mount system call.
938 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100939 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 size = log->l_iclog_size = mp->m_logbsize;
941 log->l_iclog_size_log = 0;
942 while (size != 1) {
943 log->l_iclog_size_log++;
944 size >>= 1;
945 }
946
Eric Sandeen62118702008-03-06 13:44:28 +1100947 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200948 /* # headers = size / 32k
949 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
951
952 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
953 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
954 xhdrs++;
955 log->l_iclog_hsize = xhdrs << BBSHIFT;
956 log->l_iclog_heads = xhdrs;
957 } else {
958 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
959 log->l_iclog_hsize = BBSIZE;
960 log->l_iclog_heads = 1;
961 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100962 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200965 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +1000966 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
967 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* the default log size is 16k or 32k which is one header sector */
970 log->l_iclog_hsize = BBSIZE;
971 log->l_iclog_heads = 1;
972
Christoph Hellwig7153f8b2009-02-09 08:36:46 +0100973done:
974 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100975 if (mp->m_logbufs == 0)
976 mp->m_logbufs = log->l_iclog_bufs;
977 if (mp->m_logbsize == 0)
978 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979} /* xlog_get_iclog_buffer_size */
980
981
982/*
983 * This routine initializes some of the log structure for a given mount point.
984 * Its primary purpose is to fill in enough, so recovery can occur. However,
985 * some other stuff may be filled in too.
986 */
987STATIC xlog_t *
988xlog_alloc_log(xfs_mount_t *mp,
989 xfs_buftarg_t *log_target,
990 xfs_daddr_t blk_offset,
991 int num_bblks)
992{
993 xlog_t *log;
994 xlog_rec_header_t *head;
995 xlog_in_core_t **iclogp;
996 xlog_in_core_t *iclog, *prev_iclog=NULL;
997 xfs_buf_t *bp;
998 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +0200999 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001000 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Dave Chinner644c3562008-11-10 16:50:24 +11001002 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001003 if (!log) {
1004 xlog_warn("XFS: Log allocation failed: No memory!");
1005 goto out;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 log->l_mp = mp;
1009 log->l_targ = log_target;
1010 log->l_logsize = BBTOB(num_bblks);
1011 log->l_logBBstart = blk_offset;
1012 log->l_logBBsize = num_bblks;
1013 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1014 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1015
1016 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001017 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1019 log->l_last_sync_lsn = log->l_tail_lsn;
1020 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Dave Chinnera69ed032010-12-21 12:08:20 +11001021 xlog_assign_grant_head(&log->l_grant_reserve_head, 1, 0);
1022 xlog_assign_grant_head(&log->l_grant_write_head, 1, 0);
Dave Chinner10547942010-12-21 12:02:25 +11001023 INIT_LIST_HEAD(&log->l_reserveq);
1024 INIT_LIST_HEAD(&log->l_writeq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Dave Chinnera6cb7672009-04-06 18:39:27 +02001026 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001027 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001028 log2_size = mp->m_sb.sb_logsectlog;
1029 if (log2_size < BBSHIFT) {
1030 xlog_warn("XFS: Log sector size too small "
1031 "(0x%x < 0x%x)", log2_size, BBSHIFT);
1032 goto out_free_log;
1033 }
1034
1035 log2_size -= BBSHIFT;
1036 if (log2_size > mp->m_sectbb_log) {
1037 xlog_warn("XFS: Log sector size too large "
1038 "(0x%x > 0x%x)", log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001039 goto out_free_log;
1040 }
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001043 if (log2_size && log->l_logBBstart > 0 &&
1044 !xfs_sb_version_haslogv2(&mp->m_sb)) {
1045
Dave Chinnera6cb7672009-04-06 18:39:27 +02001046 xlog_warn("XFS: log sector size (0x%x) invalid "
Alex Elder69ce58f2010-04-20 17:09:59 +10001047 "for configuration.", log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001048 goto out_free_log;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001051 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 xlog_get_iclog_buffer_size(mp, log);
1054
Dave Chinnera6cb7672009-04-06 18:39:27 +02001055 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001057 if (!bp)
1058 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1061 ASSERT(XFS_BUF_ISBUSY(bp));
1062 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1063 log->l_xbuf = bp;
1064
Eric Sandeen007c61c2007-10-11 17:43:56 +10001065 spin_lock_init(&log->l_icloglock);
1066 spin_lock_init(&log->l_grant_lock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001067 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1070 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1071
1072 iclogp = &log->l_iclog;
1073 /*
1074 * The amount of memory to allocate for the iclog structure is
1075 * rather funky due to the way the structure is defined. It is
1076 * done this way so that we can use different sizes for machines
1077 * with different amounts of memory. See the definition of
1078 * xlog_in_core_t in xfs_log_priv.h for details.
1079 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 ASSERT(log->l_iclog_size >= 4096);
1081 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001082 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1083 if (!*iclogp)
1084 goto out_free_iclog;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 iclog->ic_prev = prev_iclog;
1088 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001089
Dave Chinner686865f2010-09-24 20:07:47 +10001090 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1091 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001092 if (!bp)
1093 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001094 if (!XFS_BUF_CPSEMA(bp))
1095 ASSERT(0);
1096 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001097 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1098 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001099 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001100#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 head = &iclog->ic_header;
1104 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001105 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1106 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001107 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001108 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001110 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1114 iclog->ic_state = XLOG_STATE_ACTIVE;
1115 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001116 atomic_set(&iclog->ic_refcnt, 0);
1117 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001119 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1122 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11001123 init_waitqueue_head(&iclog->ic_force_wait);
1124 init_waitqueue_head(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 iclogp = &iclog->ic_next;
1127 }
1128 *iclogp = log->l_iclog; /* complete ring */
1129 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1130
Dave Chinner71e330b2010-05-21 14:37:18 +10001131 error = xlog_cil_init(log);
1132 if (error)
1133 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001135
1136out_free_iclog:
1137 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1138 prev_iclog = iclog->ic_next;
Dave Chinnereb40a872010-12-21 12:09:01 +11001139 if (iclog->ic_bp)
Dave Chinner644c3562008-11-10 16:50:24 +11001140 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001141 kmem_free(iclog);
1142 }
1143 spinlock_destroy(&log->l_icloglock);
1144 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001145 xfs_buf_free(log->l_xbuf);
1146out_free_log:
1147 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001148out:
1149 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150} /* xlog_alloc_log */
1151
1152
1153/*
1154 * Write out the commit record of a transaction associated with the given
1155 * ticket. Return the lsn of the commit record.
1156 */
1157STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001158xlog_commit_record(
1159 struct log *log,
1160 struct xlog_ticket *ticket,
1161 struct xlog_in_core **iclog,
1162 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Dave Chinner55b66332010-03-23 11:43:17 +11001164 struct xfs_mount *mp = log->l_mp;
1165 int error;
1166 struct xfs_log_iovec reg = {
1167 .i_addr = NULL,
1168 .i_len = 0,
1169 .i_type = XLOG_REG_TYPE_COMMIT,
1170 };
1171 struct xfs_log_vec vec = {
1172 .lv_niovecs = 1,
1173 .lv_iovecp = &reg,
1174 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001177 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1178 XLOG_COMMIT_TRANS);
1179 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001180 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001181 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184/*
1185 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1186 * log space. This code pushes on the lsn which would supposedly free up
1187 * the 25% which we want to leave free. We may need to adopt a policy which
1188 * pushes on an lsn which is further along in the log once we reach the high
1189 * water mark. In this manner, we would be creating a low water mark.
1190 */
David Chinnera8272ce2007-11-23 16:28:09 +11001191STATIC void
Dave Chinner2ced19c2010-12-21 12:09:20 +11001192xlog_grant_push_ail(
1193 struct log *log,
1194 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001196 xfs_lsn_t threshold_lsn = 0;
1197 xfs_lsn_t tail_lsn;
1198 int free_blocks;
1199 int free_bytes;
1200 int threshold_block;
1201 int threshold_cycle;
1202 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Dave Chinner2ced19c2010-12-21 12:09:20 +11001204 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Dave Chinner2ced19c2010-12-21 12:09:20 +11001206 tail_lsn = log->l_tail_lsn;
1207 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
1208 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Dave Chinner2ced19c2010-12-21 12:09:20 +11001210 /*
1211 * Set the threshold for the minimum number of free blocks in the
1212 * log to the maximum of what the caller needs, one quarter of the
1213 * log, and 256 blocks.
1214 */
1215 free_threshold = BTOBB(need_bytes);
1216 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1217 free_threshold = MAX(free_threshold, 256);
1218 if (free_blocks >= free_threshold)
1219 return;
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1222 threshold_cycle = CYCLE_LSN(tail_lsn);
1223 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001224 threshold_block -= log->l_logBBsize;
1225 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001227 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1228 threshold_block);
1229 /*
1230 * Don't pass in an lsn greater than the lsn of the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * log record known to be on disk.
1232 */
1233 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
Dave Chinner2ced19c2010-12-21 12:09:20 +11001234 threshold_lsn = log->l_last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Dave Chinner2ced19c2010-12-21 12:09:20 +11001236 /*
1237 * Get the transaction layer to kick the dirty buffers out to
1238 * disk asynchronously. No point in trying to do this if
1239 * the filesystem is shutting down.
1240 */
1241 if (!XLOG_FORCED_SHUTDOWN(log))
1242 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
1243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Christoph Hellwig873ff552010-01-13 22:17:57 +00001245/*
1246 * The bdstrat callback function for log bufs. This gives us a central
1247 * place to trap bufs in case we get hit by a log I/O error and need to
1248 * shutdown. Actually, in practice, even when we didn't get a log error,
1249 * we transition the iclogs to IOERROR state *after* flushing all existing
1250 * iclogs to disk. This is because we don't want anymore new transactions to be
1251 * started or completed afterwards.
1252 */
1253STATIC int
1254xlog_bdstrat(
1255 struct xfs_buf *bp)
1256{
1257 struct xlog_in_core *iclog;
1258
1259 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1260 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1261 XFS_BUF_ERROR(bp, EIO);
1262 XFS_BUF_STALE(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001263 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001264 /*
1265 * It would seem logical to return EIO here, but we rely on
1266 * the log state machine to propagate I/O errors instead of
1267 * doing it here.
1268 */
1269 return 0;
1270 }
1271
1272 bp->b_flags |= _XBF_RUN_QUEUES;
1273 xfs_buf_iorequest(bp);
1274 return 0;
1275}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277/*
1278 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1279 * fashion. Previously, we should have moved the current iclog
1280 * ptr in the log to point to the next available iclog. This allows further
1281 * write to continue while this code syncs out an iclog ready to go.
1282 * Before an in-core log can be written out, the data section must be scanned
1283 * to save away the 1st word of each BBSIZE block into the header. We replace
1284 * it with the current cycle count. Each BBSIZE block is tagged with the
1285 * cycle count because there in an implicit assumption that drives will
1286 * guarantee that entire 512 byte blocks get written at once. In other words,
1287 * we can't have part of a 512 byte block written and part not written. By
1288 * tagging each block, we will know which blocks are valid when recovering
1289 * after an unclean shutdown.
1290 *
1291 * This routine is single threaded on the iclog. No other thread can be in
1292 * this routine with the same iclog. Changing contents of iclog can there-
1293 * fore be done without grabbing the state machine lock. Updating the global
1294 * log will require grabbing the lock though.
1295 *
1296 * The entire log manager uses a logical block numbering scheme. Only
1297 * log_sync (and then only bwrite()) know about the fact that the log may
1298 * not start with block zero on a given device. The log block start offset
1299 * is added immediately before calling bwrite().
1300 */
1301
David Chinnera8272ce2007-11-23 16:28:09 +11001302STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303xlog_sync(xlog_t *log,
1304 xlog_in_core_t *iclog)
1305{
1306 xfs_caddr_t dptr; /* pointer to byte sized element */
1307 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001308 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 uint count; /* byte count of bwrite */
1310 uint count_init; /* initial count before roundup */
1311 int roundoff; /* roundoff to BB or stripe */
1312 int split = 0; /* split write into two regions */
1313 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001314 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001317 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /* Add for LR header */
1320 count_init = log->l_iclog_hsize + iclog->ic_offset;
1321
1322 /* Round out the log write size */
1323 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1324 /* we have a v2 stripe unit to use */
1325 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1326 } else {
1327 count = BBTOB(BTOBB(count_init));
1328 }
1329 roundoff = count - count_init;
1330 ASSERT(roundoff >= 0);
1331 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1332 roundoff < log->l_mp->m_sb.sb_logsunit)
1333 ||
1334 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1335 roundoff < BBTOB(1)));
1336
1337 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001338 spin_lock(&log->l_grant_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +11001339 xlog_grant_add_space(log, &log->l_grant_reserve_head, roundoff);
1340 xlog_grant_add_space(log, &log->l_grant_write_head, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001341 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /* put cycle number in every block */
1344 xlog_pack_data(log, iclog, roundoff);
1345
1346 /* real byte length */
1347 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001348 iclog->ic_header.h_len =
1349 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001351 iclog->ic_header.h_len =
1352 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
Nathan Scottf5faad72006-07-28 17:04:44 +10001355 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1357 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001358 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1361
1362 /* Do we need to split this write into 2 parts? */
1363 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1364 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1365 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1366 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1367 } else {
1368 iclog->ic_bwritecnt = 1;
1369 }
David Chinner511105b2007-05-24 15:21:57 +10001370 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001372 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 XFS_BUF_BUSY(bp);
1374 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001375 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001376
1377 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001378 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1381 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1382
1383 xlog_verify_iclog(log, iclog, count, B_TRUE);
1384
1385 /* account for log which doesn't start at block #0 */
1386 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1387 /*
1388 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1389 * is shutting down.
1390 */
1391 XFS_BUF_WRITE(bp);
1392
Christoph Hellwig873ff552010-01-13 22:17:57 +00001393 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1395 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001396 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001399 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1401 (unsigned long)1);
1402 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1403 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1404 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1405 (__psint_t)count), split);
1406 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001407 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 XFS_BUF_BUSY(bp);
1409 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001410 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001411 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1412 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 dptr = XFS_BUF_PTR(bp);
1414 /*
1415 * Bump the cycle numbers at the start of each block
1416 * since this part of the buffer is at the start of
1417 * a new cycle. Watch out for the header magic number
1418 * case, though.
1419 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001420 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001421 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001422 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001423 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 dptr += BBSIZE;
1425 }
1426
1427 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1428 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1429
Nathan Scottc41564b2006-03-29 08:55:14 +10001430 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1432 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001433 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1435 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001436 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440} /* xlog_sync */
1441
1442
1443/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001444 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 */
David Chinnera8272ce2007-11-23 16:28:09 +11001446STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001447xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int i;
1451
Dave Chinner71e330b2010-05-21 14:37:18 +10001452 xlog_cil_destroy(log);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 iclog = log->l_iclog;
1455 for (i=0; i<log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001458 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 iclog = next_iclog;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 spinlock_destroy(&log->l_icloglock);
1462 spinlock_destroy(&log->l_grant_lock);
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001466 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001467} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469/*
1470 * Update counters atomically now that memcpy is done.
1471 */
1472/* ARGSUSED */
1473static inline void
1474xlog_state_finish_copy(xlog_t *log,
1475 xlog_in_core_t *iclog,
1476 int record_cnt,
1477 int copy_bytes)
1478{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001479 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001481 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 iclog->ic_offset += copy_bytes;
1483
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001484 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485} /* xlog_state_finish_copy */
1486
1487
1488
1489
1490/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001491 * print out info relating to regions written which consume
1492 * the reservation
1493 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001494void
1495xlog_print_tic_res(
1496 struct xfs_mount *mp,
1497 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001498{
1499 uint i;
1500 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1501
1502 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1503 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1504 "bformat",
1505 "bchunk",
1506 "efi_format",
1507 "efd_format",
1508 "iformat",
1509 "icore",
1510 "iext",
1511 "ibroot",
1512 "ilocal",
1513 "iattr_ext",
1514 "iattr_broot",
1515 "iattr_local",
1516 "qformat",
1517 "dquot",
1518 "quotaoff",
1519 "LR header",
1520 "unmount",
1521 "commit",
1522 "trans header"
1523 };
1524 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1525 "SETATTR_NOT_SIZE",
1526 "SETATTR_SIZE",
1527 "INACTIVE",
1528 "CREATE",
1529 "CREATE_TRUNC",
1530 "TRUNCATE_FILE",
1531 "REMOVE",
1532 "LINK",
1533 "RENAME",
1534 "MKDIR",
1535 "RMDIR",
1536 "SYMLINK",
1537 "SET_DMATTRS",
1538 "GROWFS",
1539 "STRAT_WRITE",
1540 "DIOSTRAT",
1541 "WRITE_SYNC",
1542 "WRITEID",
1543 "ADDAFORK",
1544 "ATTRINVAL",
1545 "ATRUNCATE",
1546 "ATTR_SET",
1547 "ATTR_RM",
1548 "ATTR_FLAG",
1549 "CLEAR_AGI_BUCKET",
1550 "QM_SBCHANGE",
1551 "DUMMY1",
1552 "DUMMY2",
1553 "QM_QUOTAOFF",
1554 "QM_DQALLOC",
1555 "QM_SETQLIM",
1556 "QM_DQCLUSTER",
1557 "QM_QINOCREATE",
1558 "QM_QUOTAOFF_END",
1559 "SB_UNIT",
1560 "FSYNC_TS",
1561 "GROWFSRT_ALLOC",
1562 "GROWFSRT_ZERO",
1563 "GROWFSRT_FREE",
1564 "SWAPEXT"
1565 };
1566
1567 xfs_fs_cmn_err(CE_WARN, mp,
1568 "xfs_log_write: reservation summary:\n"
1569 " trans type = %s (%u)\n"
1570 " unit res = %d bytes\n"
1571 " current res = %d bytes\n"
1572 " total reg = %u bytes (o/flow = %u bytes)\n"
1573 " ophdrs = %u (ophdr space = %u bytes)\n"
1574 " ophdr + reg = %u bytes\n"
1575 " num regions = %u\n",
1576 ((ticket->t_trans_type <= 0 ||
1577 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1578 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1579 ticket->t_trans_type,
1580 ticket->t_unit_res,
1581 ticket->t_curr_res,
1582 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1583 ticket->t_res_num_ophdrs, ophdr_spc,
1584 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001585 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001586 ticket->t_res_num);
1587
1588 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001589 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001590 cmn_err(CE_WARN,
1591 "region[%u]: %s - %u bytes\n",
1592 i,
1593 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1594 "bad-rtype" : res_type_str[r_type-1]),
1595 ticket->t_res_arr[i].r_len);
1596 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001597
1598 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1599 "xfs_log_write: reservation ran out. Need to up reservation");
1600 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001601}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001602
1603/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001604 * Calculate the potential space needed by the log vector. Each region gets
1605 * its own xlog_op_header_t and may need to be double word aligned.
1606 */
1607static int
1608xlog_write_calc_vec_length(
1609 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001610 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001611{
Dave Chinner55b66332010-03-23 11:43:17 +11001612 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001613 int headers = 0;
1614 int len = 0;
1615 int i;
1616
1617 /* acct for start rec of xact */
1618 if (ticket->t_flags & XLOG_TIC_INITED)
1619 headers++;
1620
Dave Chinner55b66332010-03-23 11:43:17 +11001621 for (lv = log_vector; lv; lv = lv->lv_next) {
1622 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001623
Dave Chinner55b66332010-03-23 11:43:17 +11001624 for (i = 0; i < lv->lv_niovecs; i++) {
1625 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1626
1627 len += vecp->i_len;
1628 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1629 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001630 }
1631
1632 ticket->t_res_num_ophdrs += headers;
1633 len += headers * sizeof(struct xlog_op_header);
1634
1635 return len;
1636}
1637
1638/*
1639 * If first write for transaction, insert start record We can't be trying to
1640 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1641 */
1642static int
1643xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001644 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001645 struct xlog_ticket *ticket)
1646{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001647 if (!(ticket->t_flags & XLOG_TIC_INITED))
1648 return 0;
1649
1650 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1651 ophdr->oh_clientid = ticket->t_clientid;
1652 ophdr->oh_len = 0;
1653 ophdr->oh_flags = XLOG_START_TRANS;
1654 ophdr->oh_res2 = 0;
1655
1656 ticket->t_flags &= ~XLOG_TIC_INITED;
1657
1658 return sizeof(struct xlog_op_header);
1659}
1660
1661static xlog_op_header_t *
1662xlog_write_setup_ophdr(
1663 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001664 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001665 struct xlog_ticket *ticket,
1666 uint flags)
1667{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001668 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1669 ophdr->oh_clientid = ticket->t_clientid;
1670 ophdr->oh_res2 = 0;
1671
1672 /* are we copying a commit or unmount record? */
1673 ophdr->oh_flags = flags;
1674
1675 /*
1676 * We've seen logs corrupted with bad transaction client ids. This
1677 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1678 * and shut down the filesystem.
1679 */
1680 switch (ophdr->oh_clientid) {
1681 case XFS_TRANSACTION:
1682 case XFS_VOLUME:
1683 case XFS_LOG:
1684 break;
1685 default:
1686 xfs_fs_cmn_err(CE_WARN, log->l_mp,
1687 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1688 ophdr->oh_clientid, ticket);
1689 return NULL;
1690 }
1691
1692 return ophdr;
1693}
1694
1695/*
1696 * Set up the parameters of the region copy into the log. This has
1697 * to handle region write split across multiple log buffers - this
1698 * state is kept external to this function so that this code can
1699 * can be written in an obvious, self documenting manner.
1700 */
1701static int
1702xlog_write_setup_copy(
1703 struct xlog_ticket *ticket,
1704 struct xlog_op_header *ophdr,
1705 int space_available,
1706 int space_required,
1707 int *copy_off,
1708 int *copy_len,
1709 int *last_was_partial_copy,
1710 int *bytes_consumed)
1711{
1712 int still_to_copy;
1713
1714 still_to_copy = space_required - *bytes_consumed;
1715 *copy_off = *bytes_consumed;
1716
1717 if (still_to_copy <= space_available) {
1718 /* write of region completes here */
1719 *copy_len = still_to_copy;
1720 ophdr->oh_len = cpu_to_be32(*copy_len);
1721 if (*last_was_partial_copy)
1722 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1723 *last_was_partial_copy = 0;
1724 *bytes_consumed = 0;
1725 return 0;
1726 }
1727
1728 /* partial write of region, needs extra log op header reservation */
1729 *copy_len = space_available;
1730 ophdr->oh_len = cpu_to_be32(*copy_len);
1731 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1732 if (*last_was_partial_copy)
1733 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1734 *bytes_consumed += *copy_len;
1735 (*last_was_partial_copy)++;
1736
1737 /* account for new log op header */
1738 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1739 ticket->t_res_num_ophdrs++;
1740
1741 return sizeof(struct xlog_op_header);
1742}
1743
1744static int
1745xlog_write_copy_finish(
1746 struct log *log,
1747 struct xlog_in_core *iclog,
1748 uint flags,
1749 int *record_cnt,
1750 int *data_cnt,
1751 int *partial_copy,
1752 int *partial_copy_len,
1753 int log_offset,
1754 struct xlog_in_core **commit_iclog)
1755{
1756 if (*partial_copy) {
1757 /*
1758 * This iclog has already been marked WANT_SYNC by
1759 * xlog_state_get_iclog_space.
1760 */
1761 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1762 *record_cnt = 0;
1763 *data_cnt = 0;
1764 return xlog_state_release_iclog(log, iclog);
1765 }
1766
1767 *partial_copy = 0;
1768 *partial_copy_len = 0;
1769
1770 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1771 /* no more space in this iclog - push it. */
1772 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1773 *record_cnt = 0;
1774 *data_cnt = 0;
1775
1776 spin_lock(&log->l_icloglock);
1777 xlog_state_want_sync(log, iclog);
1778 spin_unlock(&log->l_icloglock);
1779
1780 if (!commit_iclog)
1781 return xlog_state_release_iclog(log, iclog);
1782 ASSERT(flags & XLOG_COMMIT_TRANS);
1783 *commit_iclog = iclog;
1784 }
1785
1786 return 0;
1787}
1788
1789/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * Write some region out to in-core log
1791 *
1792 * This will be called when writing externally provided regions or when
1793 * writing out a commit record for a given transaction.
1794 *
1795 * General algorithm:
1796 * 1. Find total length of this write. This may include adding to the
1797 * lengths passed in.
1798 * 2. Check whether we violate the tickets reservation.
1799 * 3. While writing to this iclog
1800 * A. Reserve as much space in this iclog as can get
1801 * B. If this is first write, save away start lsn
1802 * C. While writing this region:
1803 * 1. If first write of transaction, write start record
1804 * 2. Write log operation header (header per region)
1805 * 3. Find out if we can fit entire region into this iclog
1806 * 4. Potentially, verify destination memcpy ptr
1807 * 5. Memcpy (partial) region
1808 * 6. If partial copy, release iclog; otherwise, continue
1809 * copying more regions into current iclog
1810 * 4. Mark want sync bit (in simulation mode)
1811 * 5. Release iclog for potential flush to on-disk log.
1812 *
1813 * ERRORS:
1814 * 1. Panic if reservation is overrun. This should never happen since
1815 * reservation amounts are generated internal to the filesystem.
1816 * NOTES:
1817 * 1. Tickets are single threaded data structures.
1818 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1819 * syncing routine. When a single log_write region needs to span
1820 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1821 * on all log operation writes which don't contain the end of the
1822 * region. The XLOG_END_TRANS bit is used for the in-core log
1823 * operation which contains the end of the continued log_write region.
1824 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1825 * we don't really know exactly how much space will be used. As a result,
1826 * we don't update ic_offset until the end when we know exactly how many
1827 * bytes have been written out.
1828 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001829int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001830xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001831 struct log *log,
1832 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001833 struct xlog_ticket *ticket,
1834 xfs_lsn_t *start_lsn,
1835 struct xlog_in_core **commit_iclog,
1836 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001838 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001839 struct xfs_log_iovec *vecp;
1840 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001841 int len;
1842 int index;
1843 int partial_copy = 0;
1844 int partial_copy_len = 0;
1845 int contwr = 0;
1846 int record_cnt = 0;
1847 int data_cnt = 0;
1848 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001850 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Dave Chinner55b66332010-03-23 11:43:17 +11001852 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001853 if (log->l_cilp) {
1854 /*
1855 * Region headers and bytes are already accounted for.
1856 * We only need to take into account start records and
1857 * split regions in this function.
1858 */
1859 if (ticket->t_flags & XLOG_TIC_INITED)
1860 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1861
1862 /*
1863 * Commit record headers need to be accounted for. These
1864 * come in as separate writes so are easy to detect.
1865 */
1866 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1867 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1868 } else
1869 ticket->t_curr_res -= len;
1870
1871 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001872 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Dave Chinner55b66332010-03-23 11:43:17 +11001874 index = 0;
1875 lv = log_vector;
1876 vecp = lv->lv_iovecp;
1877 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001878 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001879 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001881 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1882 &contwr, &log_offset);
1883 if (error)
1884 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001886 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001887 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001889 /* start_lsn is the first lsn written to. That's all we need. */
1890 if (!*start_lsn)
1891 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001893 /*
1894 * This loop writes out as many regions as can fit in the amount
1895 * of space which was allocated by xlog_state_get_iclog_space().
1896 */
Dave Chinner55b66332010-03-23 11:43:17 +11001897 while (lv && index < lv->lv_niovecs) {
1898 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001899 struct xlog_op_header *ophdr;
1900 int start_rec_copy;
1901 int copy_len;
1902 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Dave Chinner55b66332010-03-23 11:43:17 +11001904 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001905 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001907 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1908 if (start_rec_copy) {
1909 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001910 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001911 start_rec_copy);
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001914 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1915 if (!ophdr)
1916 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001918 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001919 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001921 len += xlog_write_setup_copy(ticket, ophdr,
1922 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001923 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001924 &copy_off, &copy_len,
1925 &partial_copy,
1926 &partial_copy_len);
1927 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001928
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001929 /* copy region */
1930 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001931 memcpy(ptr, reg->i_addr + copy_off, copy_len);
1932 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001933
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001934 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1935 record_cnt++;
1936 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001938 error = xlog_write_copy_finish(log, iclog, flags,
1939 &record_cnt, &data_cnt,
1940 &partial_copy,
1941 &partial_copy_len,
1942 log_offset,
1943 commit_iclog);
1944 if (error)
1945 return error;
1946
1947 /*
1948 * if we had a partial copy, we need to get more iclog
1949 * space but we don't want to increment the region
1950 * index because there is still more is this region to
1951 * write.
1952 *
1953 * If we completed writing this region, and we flushed
1954 * the iclog (indicated by resetting of the record
1955 * count), then we also need to get more log space. If
1956 * this was the last record, though, we are done and
1957 * can just return.
1958 */
1959 if (partial_copy)
1960 break;
1961
Dave Chinner55b66332010-03-23 11:43:17 +11001962 if (++index == lv->lv_niovecs) {
1963 lv = lv->lv_next;
1964 index = 0;
1965 if (lv)
1966 vecp = lv->lv_iovecp;
1967 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001968 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11001969 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001970 return 0;
1971 break;
1972 }
1973 }
1974 }
1975
1976 ASSERT(len == 0);
1977
1978 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1979 if (!commit_iclog)
1980 return xlog_state_release_iclog(log, iclog);
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 ASSERT(flags & XLOG_COMMIT_TRANS);
1983 *commit_iclog = iclog;
1984 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001985}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987
1988/*****************************************************************************
1989 *
1990 * State Machine functions
1991 *
1992 *****************************************************************************
1993 */
1994
1995/* Clean iclogs starting from the head. This ordering must be
1996 * maintained, so an iclog doesn't become ACTIVE beyond one that
1997 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001998 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 * iclog is trying to sync to disk.
2000 *
2001 * State Change: DIRTY -> ACTIVE
2002 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002003STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004xlog_state_clean_log(xlog_t *log)
2005{
2006 xlog_in_core_t *iclog;
2007 int changed = 0;
2008
2009 iclog = log->l_iclog;
2010 do {
2011 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2012 iclog->ic_state = XLOG_STATE_ACTIVE;
2013 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002014 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /*
2016 * If the number of ops in this iclog indicate it just
2017 * contains the dummy transaction, we can
2018 * change state into IDLE (the second time around).
2019 * Otherwise we should change the state into
2020 * NEED a dummy.
2021 * We don't need to cover the dummy.
2022 */
2023 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002024 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2025 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 changed = 1;
2027 } else {
2028 /*
2029 * We have two dirty iclogs so start over
2030 * This could also be num of ops indicates
2031 * this is not the dummy going out.
2032 */
2033 changed = 2;
2034 }
2035 iclog->ic_header.h_num_logops = 0;
2036 memset(iclog->ic_header.h_cycle_data, 0,
2037 sizeof(iclog->ic_header.h_cycle_data));
2038 iclog->ic_header.h_lsn = 0;
2039 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2040 /* do nothing */;
2041 else
2042 break; /* stop cleaning */
2043 iclog = iclog->ic_next;
2044 } while (iclog != log->l_iclog);
2045
2046 /* log is locked when we are called */
2047 /*
2048 * Change state for the dummy log recording.
2049 * We usually go to NEED. But we go to NEED2 if the changed indicates
2050 * we are done writing the dummy record.
2051 * If we are done with the second dummy recored (DONE2), then
2052 * we go to IDLE.
2053 */
2054 if (changed) {
2055 switch (log->l_covered_state) {
2056 case XLOG_STATE_COVER_IDLE:
2057 case XLOG_STATE_COVER_NEED:
2058 case XLOG_STATE_COVER_NEED2:
2059 log->l_covered_state = XLOG_STATE_COVER_NEED;
2060 break;
2061
2062 case XLOG_STATE_COVER_DONE:
2063 if (changed == 1)
2064 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2065 else
2066 log->l_covered_state = XLOG_STATE_COVER_NEED;
2067 break;
2068
2069 case XLOG_STATE_COVER_DONE2:
2070 if (changed == 1)
2071 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2072 else
2073 log->l_covered_state = XLOG_STATE_COVER_NEED;
2074 break;
2075
2076 default:
2077 ASSERT(0);
2078 }
2079 }
2080} /* xlog_state_clean_log */
2081
2082STATIC xfs_lsn_t
2083xlog_get_lowest_lsn(
2084 xlog_t *log)
2085{
2086 xlog_in_core_t *lsn_log;
2087 xfs_lsn_t lowest_lsn, lsn;
2088
2089 lsn_log = log->l_iclog;
2090 lowest_lsn = 0;
2091 do {
2092 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002093 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if ((lsn && !lowest_lsn) ||
2095 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2096 lowest_lsn = lsn;
2097 }
2098 }
2099 lsn_log = lsn_log->ic_next;
2100 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002101 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
2104
2105STATIC void
2106xlog_state_do_callback(
2107 xlog_t *log,
2108 int aborted,
2109 xlog_in_core_t *ciclog)
2110{
2111 xlog_in_core_t *iclog;
2112 xlog_in_core_t *first_iclog; /* used to know when we've
2113 * processed all iclogs once */
2114 xfs_log_callback_t *cb, *cb_next;
2115 int flushcnt = 0;
2116 xfs_lsn_t lowest_lsn;
2117 int ioerrors; /* counter: iclogs with errors */
2118 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2119 int funcdidcallbacks; /* flag: function did callbacks */
2120 int repeats; /* for issuing console warnings if
2121 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002122 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002124 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 first_iclog = iclog = log->l_iclog;
2126 ioerrors = 0;
2127 funcdidcallbacks = 0;
2128 repeats = 0;
2129
2130 do {
2131 /*
2132 * Scan all iclogs starting with the one pointed to by the
2133 * log. Reset this starting point each time the log is
2134 * unlocked (during callbacks).
2135 *
2136 * Keep looping through iclogs until one full pass is made
2137 * without running any callbacks.
2138 */
2139 first_iclog = log->l_iclog;
2140 iclog = log->l_iclog;
2141 loopdidcallbacks = 0;
2142 repeats++;
2143
2144 do {
2145
2146 /* skip all iclogs in the ACTIVE & DIRTY states */
2147 if (iclog->ic_state &
2148 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2149 iclog = iclog->ic_next;
2150 continue;
2151 }
2152
2153 /*
2154 * Between marking a filesystem SHUTDOWN and stopping
2155 * the log, we do flush all iclogs to disk (if there
2156 * wasn't a log I/O error). So, we do want things to
2157 * go smoothly in case of just a SHUTDOWN w/o a
2158 * LOG_IO_ERROR.
2159 */
2160 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2161 /*
2162 * Can only perform callbacks in order. Since
2163 * this iclog is not in the DONE_SYNC/
2164 * DO_CALLBACK state, we skip the rest and
2165 * just try to clean up. If we set our iclog
2166 * to DO_CALLBACK, we will not process it when
2167 * we retry since a previous iclog is in the
2168 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002169 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
2171 if (!(iclog->ic_state &
2172 (XLOG_STATE_DONE_SYNC |
2173 XLOG_STATE_DO_CALLBACK))) {
2174 if (ciclog && (ciclog->ic_state ==
2175 XLOG_STATE_DONE_SYNC)) {
2176 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2177 }
2178 break;
2179 }
2180 /*
2181 * We now have an iclog that is in either the
2182 * DO_CALLBACK or DONE_SYNC states. The other
2183 * states (WANT_SYNC, SYNCING, or CALLBACK were
2184 * caught by the above if and are going to
2185 * clean (i.e. we aren't doing their callbacks)
2186 * see the above if.
2187 */
2188
2189 /*
2190 * We will do one more check here to see if we
2191 * have chased our tail around.
2192 */
2193
2194 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002195 if (lowest_lsn &&
2196 XFS_LSN_CMP(lowest_lsn,
2197 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 iclog = iclog->ic_next;
2199 continue; /* Leave this iclog for
2200 * another thread */
2201 }
2202
2203 iclog->ic_state = XLOG_STATE_CALLBACK;
2204
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002205 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002208 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * No one else can be here except us.
2210 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002211 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002212 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2213 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2214 log->l_last_sync_lsn =
2215 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002216 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002219 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 ioerrors++;
2221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
David Chinner114d23a2008-04-10 12:18:39 +10002223 /*
2224 * Keep processing entries in the callback list until
2225 * we come around and it is empty. We need to
2226 * atomically see that the list is empty and change the
2227 * state to DIRTY so that we don't miss any more
2228 * callbacks being added.
2229 */
2230 spin_lock(&iclog->ic_callback_lock);
2231 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002232 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 iclog->ic_callback_tail = &(iclog->ic_callback);
2234 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002235 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002238 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 cb_next = cb->cb_next;
2240 cb->cb_func(cb->cb_arg, aborted);
2241 }
David Chinner114d23a2008-04-10 12:18:39 +10002242 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 cb = iclog->ic_callback;
2244 }
2245
2246 loopdidcallbacks++;
2247 funcdidcallbacks++;
2248
David Chinner114d23a2008-04-10 12:18:39 +10002249 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002250 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002251 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2253 iclog->ic_state = XLOG_STATE_DIRTY;
2254
2255 /*
2256 * Transition from DIRTY to ACTIVE if applicable.
2257 * NOP if STATE_IOERROR.
2258 */
2259 xlog_state_clean_log(log);
2260
2261 /* wake up threads waiting in xfs_log_force() */
Dave Chinnereb40a872010-12-21 12:09:01 +11002262 wake_up_all(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 iclog = iclog->ic_next;
2265 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002266
2267 if (repeats > 5000) {
2268 flushcnt += repeats;
2269 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002271 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002272 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 } while (!ioerrors && loopdidcallbacks);
2275
2276 /*
2277 * make one last gasp attempt to see if iclogs are being left in
2278 * limbo..
2279 */
2280#ifdef DEBUG
2281 if (funcdidcallbacks) {
2282 first_iclog = iclog = log->l_iclog;
2283 do {
2284 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2285 /*
2286 * Terminate the loop if iclogs are found in states
2287 * which will cause other threads to clean up iclogs.
2288 *
2289 * SYNCING - i/o completion will go through logs
2290 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002291 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 * IOERROR - give up hope all ye who enter here
2293 */
2294 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2295 iclog->ic_state == XLOG_STATE_SYNCING ||
2296 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2297 iclog->ic_state == XLOG_STATE_IOERROR )
2298 break;
2299 iclog = iclog->ic_next;
2300 } while (first_iclog != iclog);
2301 }
2302#endif
2303
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002304 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2305 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002306 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002307
2308 if (wake)
Dave Chinnereb40a872010-12-21 12:09:01 +11002309 wake_up_all(&log->l_flush_wait);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002310}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312
2313/*
2314 * Finish transitioning this iclog to the dirty state.
2315 *
2316 * Make sure that we completely execute this routine only when this is
2317 * the last call to the iclog. There is a good chance that iclog flushes,
2318 * when we reach the end of the physical log, get turned into 2 separate
2319 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2320 * routine. By using the reference count bwritecnt, we guarantee that only
2321 * the second completion goes through.
2322 *
2323 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002324 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 */
David Chinnera8272ce2007-11-23 16:28:09 +11002326STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327xlog_state_done_syncing(
2328 xlog_in_core_t *iclog,
2329 int aborted)
2330{
2331 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002333 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2336 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002337 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2339
2340
2341 /*
2342 * If we got an error, either on the first buffer, or in the case of
2343 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2344 * and none should ever be attempted to be written to disk
2345 * again.
2346 */
2347 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2348 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002349 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return;
2351 }
2352 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2353 }
2354
2355 /*
2356 * Someone could be sleeping prior to writing out the next
2357 * iclog buffer, we wake them all, one will get to do the
2358 * I/O, the others get to wait for the result.
2359 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002360 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002361 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2363} /* xlog_state_done_syncing */
2364
2365
2366/*
2367 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002368 * sleep. We wait on the flush queue on the head iclog as that should be
2369 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2370 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 *
2372 * The in-core logs are used in a circular fashion. They are not used
2373 * out-of-order even when an iclog past the head is free.
2374 *
2375 * return:
2376 * * log_offset where xlog_write() can start writing into the in-core
2377 * log's data space.
2378 * * in-core log pointer to which xlog_write() should write.
2379 * * boolean indicating this is a continued write to an in-core log.
2380 * If this is the last write, then the in-core log's offset field
2381 * needs to be incremented, depending on the amount of data which
2382 * is copied.
2383 */
David Chinnera8272ce2007-11-23 16:28:09 +11002384STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385xlog_state_get_iclog_space(xlog_t *log,
2386 int len,
2387 xlog_in_core_t **iclogp,
2388 xlog_ticket_t *ticket,
2389 int *continued_write,
2390 int *logoffsetp)
2391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 int log_offset;
2393 xlog_rec_header_t *head;
2394 xlog_in_core_t *iclog;
2395 int error;
2396
2397restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002398 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002400 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return XFS_ERROR(EIO);
2402 }
2403
2404 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002405 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002407
2408 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002409 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto restart;
2411 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 head = &iclog->ic_header;
2414
David Chinner155cc6b2008-03-06 13:44:14 +11002415 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 log_offset = iclog->ic_offset;
2417
2418 /* On the 1st write to an iclog, figure out lsn. This works
2419 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2420 * committing to. If the offset is set, that's how many blocks
2421 * must be written.
2422 */
2423 if (log_offset == 0) {
2424 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002425 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002426 log->l_iclog_hsize,
2427 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002428 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2429 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002430 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 ASSERT(log->l_curr_block >= 0);
2432 }
2433
2434 /* If there is enough room to write everything, then do it. Otherwise,
2435 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2436 * bit is on, so this will get flushed out. Don't update ic_offset
2437 * until you know exactly how many bytes get copied. Therefore, wait
2438 * until later to update ic_offset.
2439 *
2440 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2441 * can fit into remaining data section.
2442 */
2443 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2444 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2445
Dave Chinner49641f12008-07-11 17:43:55 +10002446 /*
2447 * If I'm the only one writing to this iclog, sync it to disk.
2448 * We need to do an atomic compare and decrement here to avoid
2449 * racing with concurrent atomic_dec_and_lock() calls in
2450 * xlog_state_release_iclog() when there is more than one
2451 * reference to the iclog.
2452 */
2453 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2454 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002455 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002456 error = xlog_state_release_iclog(log, iclog);
2457 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002458 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002460 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462 goto restart;
2463 }
2464
2465 /* Do we have enough room to write the full amount in the remainder
2466 * of this iclog? Or must we continue a write on the next iclog and
2467 * mark this iclog as completely taken? In the case where we switch
2468 * iclogs (to mark it taken), this particular iclog will release/sync
2469 * to disk in xlog_write().
2470 */
2471 if (len <= iclog->ic_size - iclog->ic_offset) {
2472 *continued_write = 0;
2473 iclog->ic_offset += len;
2474 } else {
2475 *continued_write = 1;
2476 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2477 }
2478 *iclogp = iclog;
2479
2480 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002481 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 *logoffsetp = log_offset;
2484 return 0;
2485} /* xlog_state_get_iclog_space */
2486
2487/*
2488 * Atomically get the log space required for a log ticket.
2489 *
2490 * Once a ticket gets put onto the reserveq, it will only return after
2491 * the needed reservation is satisfied.
2492 */
2493STATIC int
2494xlog_grant_log_space(xlog_t *log,
2495 xlog_ticket_t *tic)
2496{
2497 int free_bytes;
2498 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500#ifdef DEBUG
2501 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2502 panic("grant Recovery problem");
2503#endif
2504
2505 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002506 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002507
2508 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 /* something is already sleeping; insert new transaction at end */
Dave Chinner10547942010-12-21 12:02:25 +11002511 if (!list_empty(&log->l_reserveq)) {
2512 list_add_tail(&tic->t_queue, &log->l_reserveq);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002513
2514 trace_xfs_log_grant_sleep1(log, tic);
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 /*
2517 * Gotta check this before going to sleep, while we're
2518 * holding the grant lock.
2519 */
2520 if (XLOG_FORCED_SHUTDOWN(log))
2521 goto error_return;
2522
2523 XFS_STATS_INC(xs_sleep_logspace);
Dave Chinnereb40a872010-12-21 12:09:01 +11002524 xlog_wait(&tic->t_wait, &log->l_grant_lock);
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /*
2527 * If we got an error, and the filesystem is shutting down,
2528 * we'll catch it down below. So just continue...
2529 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002530 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002531 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2534 need_bytes = tic->t_unit_res*tic->t_ocnt;
2535 else
2536 need_bytes = tic->t_unit_res;
2537
2538redo:
2539 if (XLOG_FORCED_SHUTDOWN(log))
2540 goto error_return;
2541
Dave Chinnera69ed032010-12-21 12:08:20 +11002542 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (free_bytes < need_bytes) {
Dave Chinner10547942010-12-21 12:02:25 +11002544 if (list_empty(&tic->t_queue))
2545 list_add_tail(&tic->t_queue, &log->l_reserveq);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002546
2547 trace_xfs_log_grant_sleep2(log, tic);
2548
Dave Chinner2ced19c2010-12-21 12:09:20 +11002549 xlog_grant_push_ail(log, need_bytes);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 XFS_STATS_INC(xs_sleep_logspace);
Dave Chinnereb40a872010-12-21 12:09:01 +11002552 xlog_wait(&tic->t_wait, &log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Dave Chinner9d7fef72009-04-06 18:42:59 +02002554 spin_lock(&log->l_grant_lock);
2555 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002558 trace_xfs_log_grant_wake2(log, tic);
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 goto redo;
Dave Chinner10547942010-12-21 12:02:25 +11002561 }
2562
2563 list_del_init(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 /* we've got enough space */
Dave Chinnera69ed032010-12-21 12:08:20 +11002566 xlog_grant_add_space(log, &log->l_grant_reserve_head, need_bytes);
2567 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002568 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 xlog_verify_grant_head(log, 1);
Dave Chinner3f336c62010-12-21 12:02:52 +11002570 xlog_verify_grant_tail(log);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002571 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return 0;
2573
2574 error_return:
Dave Chinner10547942010-12-21 12:02:25 +11002575 list_del_init(&tic->t_queue);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002576 trace_xfs_log_grant_error(log, tic);
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
2579 * If we are failing, make sure the ticket doesn't have any
2580 * current reservations. We don't want to add this back when
2581 * the ticket/transaction gets cancelled.
2582 */
2583 tic->t_curr_res = 0;
2584 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002585 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return XFS_ERROR(EIO);
2587} /* xlog_grant_log_space */
2588
2589
2590/*
2591 * Replenish the byte reservation required by moving the grant write head.
2592 *
2593 *
2594 */
2595STATIC int
2596xlog_regrant_write_log_space(xlog_t *log,
2597 xlog_ticket_t *tic)
2598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 int free_bytes, need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002602 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607#ifdef DEBUG
2608 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2609 panic("regrant Recovery problem");
2610#endif
2611
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002612 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002613
2614 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (XLOG_FORCED_SHUTDOWN(log))
2617 goto error_return;
2618
2619 /* If there are other waiters on the queue then give them a
2620 * chance at logspace before us. Wake up the first waiters,
2621 * if we do not wake up all the waiters then go to sleep waiting
2622 * for more free space, otherwise try to get some space for
2623 * this transaction.
2624 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002625 need_bytes = tic->t_unit_res;
Dave Chinner10547942010-12-21 12:02:25 +11002626 if (!list_empty(&log->l_writeq)) {
2627 struct xlog_ticket *ntic;
Dave Chinnera69ed032010-12-21 12:08:20 +11002628 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Dave Chinner10547942010-12-21 12:02:25 +11002629 list_for_each_entry(ntic, &log->l_writeq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2631
2632 if (free_bytes < ntic->t_unit_res)
2633 break;
2634 free_bytes -= ntic->t_unit_res;
Dave Chinnereb40a872010-12-21 12:09:01 +11002635 wake_up(&ntic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +11002636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Dave Chinner10547942010-12-21 12:02:25 +11002638 if (ntic != list_first_entry(&log->l_writeq,
2639 struct xlog_ticket, t_queue)) {
2640 if (list_empty(&tic->t_queue))
2641 list_add_tail(&tic->t_queue, &log->l_writeq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002643 trace_xfs_log_regrant_write_sleep1(log, tic);
2644
Dave Chinner2ced19c2010-12-21 12:09:20 +11002645 xlog_grant_push_ail(log, need_bytes);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 XFS_STATS_INC(xs_sleep_logspace);
Dave Chinnereb40a872010-12-21 12:09:01 +11002648 xlog_wait(&tic->t_wait, &log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 /* If we're shutting down, this tic is already
2651 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002652 spin_lock(&log->l_grant_lock);
2653 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002656 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 }
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660redo:
2661 if (XLOG_FORCED_SHUTDOWN(log))
2662 goto error_return;
2663
Dave Chinnera69ed032010-12-21 12:08:20 +11002664 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (free_bytes < need_bytes) {
Dave Chinner10547942010-12-21 12:02:25 +11002666 if (list_empty(&tic->t_queue))
2667 list_add_tail(&tic->t_queue, &log->l_writeq);
Dave Chinner2ced19c2010-12-21 12:09:20 +11002668 xlog_grant_push_ail(log, need_bytes);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002671 trace_xfs_log_regrant_write_sleep2(log, tic);
Dave Chinnereb40a872010-12-21 12:09:01 +11002672 xlog_wait(&tic->t_wait, &log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002675 spin_lock(&log->l_grant_lock);
2676 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002679 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 goto redo;
Dave Chinner10547942010-12-21 12:02:25 +11002681 }
2682
2683 list_del_init(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002685 /* we've got enough space */
Dave Chinnera69ed032010-12-21 12:08:20 +11002686 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002687 trace_xfs_log_regrant_write_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 xlog_verify_grant_head(log, 1);
Dave Chinner3f336c62010-12-21 12:02:52 +11002689 xlog_verify_grant_tail(log);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002690 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693
2694 error_return:
Dave Chinner10547942010-12-21 12:02:25 +11002695 list_del_init(&tic->t_queue);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002696 trace_xfs_log_regrant_write_error(log, tic);
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /*
2699 * If we are failing, make sure the ticket doesn't have any
2700 * current reservations. We don't want to add this back when
2701 * the ticket/transaction gets cancelled.
2702 */
2703 tic->t_curr_res = 0;
2704 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002705 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return XFS_ERROR(EIO);
2707} /* xlog_regrant_write_log_space */
2708
2709
2710/* The first cnt-1 times through here we don't need to
2711 * move the grant write head because the permanent
2712 * reservation has reserved cnt times the unit amount.
2713 * Release part of current permanent unit reservation and
2714 * reset current reservation to be one units worth. Also
2715 * move grant reservation head forward.
2716 */
2717STATIC void
2718xlog_regrant_reserve_log_space(xlog_t *log,
2719 xlog_ticket_t *ticket)
2720{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002721 trace_xfs_log_regrant_reserve_enter(log, ticket);
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (ticket->t_cnt > 0)
2724 ticket->t_cnt--;
2725
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002726 spin_lock(&log->l_grant_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +11002727 xlog_grant_sub_space(log, &log->l_grant_reserve_head,
2728 ticket->t_curr_res);
2729 xlog_grant_sub_space(log, &log->l_grant_write_head,
2730 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002732 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002733
2734 trace_xfs_log_regrant_reserve_sub(log, ticket);
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 xlog_verify_grant_head(log, 1);
2737
2738 /* just return if we still have some of the pre-reserved space */
2739 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002740 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return;
2742 }
2743
Dave Chinnera69ed032010-12-21 12:08:20 +11002744 xlog_grant_add_space(log, &log->l_grant_reserve_head,
2745 ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002746
2747 trace_xfs_log_regrant_reserve_exit(log, ticket);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002750 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002752 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753} /* xlog_regrant_reserve_log_space */
2754
2755
2756/*
2757 * Give back the space left from a reservation.
2758 *
2759 * All the information we need to make a correct determination of space left
2760 * is present. For non-permanent reservations, things are quite easy. The
2761 * count should have been decremented to zero. We only need to deal with the
2762 * space remaining in the current reservation part of the ticket. If the
2763 * ticket contains a permanent reservation, there may be left over space which
2764 * needs to be released. A count of N means that N-1 refills of the current
2765 * reservation can be done before we need to ask for more space. The first
2766 * one goes to fill up the first current reservation. Once we run out of
2767 * space, the count will stay at zero and the only space remaining will be
2768 * in the current reservation field.
2769 */
2770STATIC void
2771xlog_ungrant_log_space(xlog_t *log,
2772 xlog_ticket_t *ticket)
2773{
Dave Chinner663e4962010-12-21 12:06:05 +11002774 int bytes;
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (ticket->t_cnt > 0)
2777 ticket->t_cnt--;
2778
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002779 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002780 trace_xfs_log_ungrant_enter(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002781 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Dave Chinner663e4962010-12-21 12:06:05 +11002783 /*
2784 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 * up more space based on the remaining count.
2786 */
Dave Chinner663e4962010-12-21 12:06:05 +11002787 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (ticket->t_cnt > 0) {
2789 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11002790 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792
Dave Chinnera69ed032010-12-21 12:08:20 +11002793 xlog_grant_sub_space(log, &log->l_grant_reserve_head, bytes);
2794 xlog_grant_sub_space(log, &log->l_grant_write_head, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11002795
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002796 trace_xfs_log_ungrant_exit(log, ticket);
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002799 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 xfs_log_move_tail(log->l_mp, 1);
2801} /* xlog_ungrant_log_space */
2802
2803
2804/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 * Flush iclog to disk if this is the last reference to the given iclog and
2806 * the WANT_SYNC bit is set.
2807 *
2808 * When this function is entered, the iclog is not necessarily in the
2809 * WANT_SYNC state. It may be sitting around waiting to get filled.
2810 *
2811 *
2812 */
David Chinnera8272ce2007-11-23 16:28:09 +11002813STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002814xlog_state_release_iclog(
2815 xlog_t *log,
2816 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 int sync = 0; /* do we sync? */
2819
David Chinner155cc6b2008-03-06 13:44:14 +11002820 if (iclog->ic_state & XLOG_STATE_IOERROR)
2821 return XFS_ERROR(EIO);
2822
2823 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2824 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2825 return 0;
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002828 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return XFS_ERROR(EIO);
2830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2832 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2833
David Chinner155cc6b2008-03-06 13:44:14 +11002834 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002835 /* update tail before writing to iclog */
2836 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 sync++;
2838 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002839 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2841 /* cycle incremented when incrementing curr_block */
2842 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002843 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 /*
2846 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002847 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2849 * this iclog has consistent data, so we ignore IOERROR
2850 * flags after this point.
2851 */
David Chinnerb5893342008-03-06 13:44:06 +11002852 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855} /* xlog_state_release_iclog */
2856
2857
2858/*
2859 * This routine will mark the current iclog in the ring as WANT_SYNC
2860 * and move the current iclog pointer to the next iclog in the ring.
2861 * When this routine is called from xlog_state_get_iclog_space(), the
2862 * exact size of the iclog has not yet been determined. All we know is
2863 * that every data block. We have run out of space in this log record.
2864 */
2865STATIC void
2866xlog_state_switch_iclogs(xlog_t *log,
2867 xlog_in_core_t *iclog,
2868 int eventual_size)
2869{
2870 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2871 if (!eventual_size)
2872 eventual_size = iclog->ic_offset;
2873 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002874 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 log->l_prev_block = log->l_curr_block;
2876 log->l_prev_cycle = log->l_curr_cycle;
2877
2878 /* roll log?: ic_offset changed later */
2879 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2880
2881 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002882 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 log->l_mp->m_sb.sb_logsunit > 1) {
2884 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2885 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2886 }
2887
2888 if (log->l_curr_block >= log->l_logBBsize) {
2889 log->l_curr_cycle++;
2890 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2891 log->l_curr_cycle++;
2892 log->l_curr_block -= log->l_logBBsize;
2893 ASSERT(log->l_curr_block >= 0);
2894 }
2895 ASSERT(iclog == log->l_iclog);
2896 log->l_iclog = iclog->ic_next;
2897} /* xlog_state_switch_iclogs */
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899/*
2900 * Write out all data in the in-core log as of this exact moment in time.
2901 *
2902 * Data may be written to the in-core log during this call. However,
2903 * we don't guarantee this data will be written out. A change from past
2904 * implementation means this routine will *not* write out zero length LRs.
2905 *
2906 * Basically, we try and perform an intelligent scan of the in-core logs.
2907 * If we determine there is no flushable data, we just return. There is no
2908 * flushable data if:
2909 *
2910 * 1. the current iclog is active and has no data; the previous iclog
2911 * is in the active or dirty state.
2912 * 2. the current iclog is drity, and the previous iclog is in the
2913 * active or dirty state.
2914 *
David Chinner12017fa2008-08-13 16:34:31 +10002915 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 *
2917 * 1. the current iclog is not in the active nor dirty state.
2918 * 2. the current iclog dirty, and the previous iclog is not in the
2919 * active nor dirty state.
2920 * 3. the current iclog is active, and there is another thread writing
2921 * to this particular iclog.
2922 * 4. a) the current iclog is active and has no other writers
2923 * b) when we return from flushing out this iclog, it is still
2924 * not in the active nor dirty state.
2925 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002926int
2927_xfs_log_force(
2928 struct xfs_mount *mp,
2929 uint flags,
2930 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002932 struct log *log = mp->m_log;
2933 struct xlog_in_core *iclog;
2934 xfs_lsn_t lsn;
2935
2936 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Dave Chinnera44f13e2010-08-24 11:40:03 +10002938 if (log->l_cilp)
2939 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002940
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002941 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 iclog = log->l_iclog;
2944 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002945 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return XFS_ERROR(EIO);
2947 }
2948
2949 /* If the head iclog is not active nor dirty, we just attach
2950 * ourselves to the head and go to sleep.
2951 */
2952 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2953 iclog->ic_state == XLOG_STATE_DIRTY) {
2954 /*
2955 * If the head is dirty or (active and empty), then
2956 * we need to look at the previous iclog. If the previous
2957 * iclog is active or dirty we are done. There is nothing
2958 * to sync out. Otherwise, we attach ourselves to the
2959 * previous iclog and go to sleep.
2960 */
2961 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002962 (atomic_read(&iclog->ic_refcnt) == 0
2963 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 iclog = iclog->ic_prev;
2965 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2966 iclog->ic_state == XLOG_STATE_DIRTY)
2967 goto no_sleep;
2968 else
2969 goto maybe_sleep;
2970 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002971 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /* We are the only one with access to this
2973 * iclog. Flush it out now. There should
2974 * be a roundoff of zero to show that someone
2975 * has already taken care of the roundoff from
2976 * the previous sync.
2977 */
David Chinner155cc6b2008-03-06 13:44:14 +11002978 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002979 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002981 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 if (xlog_state_release_iclog(log, iclog))
2984 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002985
2986 if (log_flushed)
2987 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002988 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002989 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 iclog->ic_state != XLOG_STATE_DIRTY)
2991 goto maybe_sleep;
2992 else
2993 goto no_sleep;
2994 } else {
2995 /* Someone else is writing to this iclog.
2996 * Use its call to flush out the data. However,
2997 * the other thread may not force out this LR,
2998 * so we mark it WANT_SYNC.
2999 */
3000 xlog_state_switch_iclogs(log, iclog, 0);
3001 goto maybe_sleep;
3002 }
3003 }
3004 }
3005
3006 /* By the time we come around again, the iclog could've been filled
3007 * which would give it another lsn. If we have a new lsn, just
3008 * return because the relevant data has been flushed.
3009 */
3010maybe_sleep:
3011 if (flags & XFS_LOG_SYNC) {
3012 /*
3013 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003014 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 * Then we check again after waking up, in case our
3016 * sleep was disturbed by a bad news.
3017 */
3018 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003019 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return XFS_ERROR(EIO);
3021 }
3022 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003023 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /*
3025 * No need to grab the log lock here since we're
3026 * only deciding whether or not to return EIO
3027 * and the memory read should be atomic.
3028 */
3029 if (iclog->ic_state & XLOG_STATE_IOERROR)
3030 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003031 if (log_flushed)
3032 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 } else {
3034
3035no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003036 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003042 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
3043 * about errors or whether the log was flushed or not. This is the normal
3044 * interface to use when trying to unpin items or move the log forward.
3045 */
3046void
3047xfs_log_force(
3048 xfs_mount_t *mp,
3049 uint flags)
3050{
3051 int error;
3052
3053 error = _xfs_log_force(mp, flags, NULL);
3054 if (error) {
3055 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3056 "error %d returned.", error);
3057 }
3058}
3059
3060/*
3061 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 *
3063 * Find in-core log with lsn.
3064 * If it is in the DIRTY state, just return.
3065 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3066 * state and go to sleep or return.
3067 * If it is in any other state, go to sleep or return.
3068 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003069 * Synchronous forces are implemented with a signal variable. All callers
3070 * to force a given lsn to disk will wait on a the sv attached to the
3071 * specific in-core log. When given in-core log finally completes its
3072 * write to disk, that thread will wake up all threads waiting on the
3073 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003075int
3076_xfs_log_force_lsn(
3077 struct xfs_mount *mp,
3078 xfs_lsn_t lsn,
3079 uint flags,
3080 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003082 struct log *log = mp->m_log;
3083 struct xlog_in_core *iclog;
3084 int already_slept = 0;
3085
3086 ASSERT(lsn != 0);
3087
3088 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Dave Chinner71e330b2010-05-21 14:37:18 +10003090 if (log->l_cilp) {
Dave Chinnera44f13e2010-08-24 11:40:03 +10003091 lsn = xlog_cil_force_lsn(log, lsn);
Dave Chinner71e330b2010-05-21 14:37:18 +10003092 if (lsn == NULLCOMMITLSN)
3093 return 0;
3094 }
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003097 spin_lock(&log->l_icloglock);
3098 iclog = log->l_iclog;
3099 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003100 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003101 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
3103
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003104 do {
3105 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3106 iclog = iclog->ic_next;
3107 continue;
3108 }
3109
3110 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3111 spin_unlock(&log->l_icloglock);
3112 return 0;
3113 }
3114
3115 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3116 /*
3117 * We sleep here if we haven't already slept (e.g.
3118 * this is the first time we've looked at the correct
3119 * iclog buf) and the buffer before us is going to
3120 * be sync'ed. The reason for this is that if we
3121 * are doing sync transactions here, by waiting for
3122 * the previous I/O to complete, we can allow a few
3123 * more transactions into this iclog before we close
3124 * it down.
3125 *
3126 * Otherwise, we mark the buffer WANT_SYNC, and bump
3127 * up the refcnt so we can release the log (which
3128 * drops the ref count). The state switch keeps new
3129 * transaction commits from using this buffer. When
3130 * the current commits finish writing into the buffer,
3131 * the refcount will drop to zero and the buffer will
3132 * go out then.
3133 */
3134 if (!already_slept &&
3135 (iclog->ic_prev->ic_state &
3136 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3137 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3138
3139 XFS_STATS_INC(xs_log_force_sleep);
3140
Dave Chinnereb40a872010-12-21 12:09:01 +11003141 xlog_wait(&iclog->ic_prev->ic_write_wait,
3142 &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003143 if (log_flushed)
3144 *log_flushed = 1;
3145 already_slept = 1;
3146 goto try_again;
3147 }
David Chinner155cc6b2008-03-06 13:44:14 +11003148 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003150 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (xlog_state_release_iclog(log, iclog))
3152 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003153 if (log_flushed)
3154 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003155 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003158 if ((flags & XFS_LOG_SYNC) && /* sleep */
3159 !(iclog->ic_state &
3160 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3161 /*
3162 * Don't wait on completion if we know that we've
3163 * gotten a log write error.
3164 */
3165 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3166 spin_unlock(&log->l_icloglock);
3167 return XFS_ERROR(EIO);
3168 }
3169 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003170 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003171 /*
3172 * No need to grab the log lock here since we're
3173 * only deciding whether or not to return EIO
3174 * and the memory read should be atomic.
3175 */
3176 if (iclog->ic_state & XLOG_STATE_IOERROR)
3177 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003179 if (log_flushed)
3180 *log_flushed = 1;
3181 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003182 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003184
3185 return 0;
3186 } while (iclog != log->l_iclog);
3187
3188 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003192/*
3193 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3194 * about errors or whether the log was flushed or not. This is the normal
3195 * interface to use when trying to unpin items or move the log forward.
3196 */
3197void
3198xfs_log_force_lsn(
3199 xfs_mount_t *mp,
3200 xfs_lsn_t lsn,
3201 uint flags)
3202{
3203 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003205 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3206 if (error) {
3207 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3208 "error %d returned.", error);
3209 }
3210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212/*
3213 * Called when we want to mark the current iclog as being ready to sync to
3214 * disk.
3215 */
David Chinnera8272ce2007-11-23 16:28:09 +11003216STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3218{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003219 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3222 xlog_state_switch_iclogs(log, iclog, 0);
3223 } else {
3224 ASSERT(iclog->ic_state &
3225 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3226 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229
3230/*****************************************************************************
3231 *
3232 * TICKET functions
3233 *
3234 *****************************************************************************
3235 */
3236
3237/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003238 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003240void
3241xfs_log_ticket_put(
3242 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003244 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003245 if (atomic_dec_and_test(&ticket->t_ref))
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003246 kmem_zone_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003249xlog_ticket_t *
3250xfs_log_ticket_get(
3251 xlog_ticket_t *ticket)
3252{
3253 ASSERT(atomic_read(&ticket->t_ref) > 0);
3254 atomic_inc(&ticket->t_ref);
3255 return ticket;
3256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Dave Chinner955833c2010-05-14 21:41:46 +10003258xlog_tid_t
3259xfs_log_get_trans_ident(
3260 struct xfs_trans *tp)
3261{
3262 return tp->t_ticket->t_tid;
3263}
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003266 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003268xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003269xlog_ticket_alloc(
3270 struct log *log,
3271 int unit_bytes,
3272 int cnt,
3273 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003274 uint xflags,
3275 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003277 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003279 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Dave Chinner3383ca52010-05-07 11:04:17 +10003281 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003282 if (!tic)
3283 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 /*
3286 * Permanent reservations have up to 'cnt'-1 active log operations
3287 * in the log. A unit in this case is the amount of space for one
3288 * of these log operations. Normal reservations have a cnt of 1
3289 * and their unit amount is the total amount of space required.
3290 *
3291 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003292 * which occupy space in the on-disk log.
3293 *
3294 * Normal form of a transaction is:
3295 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3296 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3297 *
3298 * We need to account for all the leadup data and trailer data
3299 * around the transaction data.
3300 * And then we need to account for the worst case in terms of using
3301 * more space.
3302 * The worst case will happen if:
3303 * - the placement of the transaction happens to be such that the
3304 * roundoff is at its maximum
3305 * - the transaction data is synced before the commit record is synced
3306 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3307 * Therefore the commit record is in its own Log Record.
3308 * This can happen as the commit record is called with its
3309 * own region to xlog_write().
3310 * This then means that in the worst case, roundoff can happen for
3311 * the commit-rec as well.
3312 * The commit-rec is smaller than padding in this scenario and so it is
3313 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 */
3315
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003316 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003318 unit_bytes += sizeof(xfs_trans_header_t);
3319
3320 /* for start-rec */
3321 unit_bytes += sizeof(xlog_op_header_t);
3322
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003323 /*
3324 * for LR headers - the space for data in an iclog is the size minus
3325 * the space used for the headers. If we use the iclog size, then we
3326 * undercalculate the number of headers required.
3327 *
3328 * Furthermore - the addition of op headers for split-recs might
3329 * increase the space required enough to require more log and op
3330 * headers, so take that into account too.
3331 *
3332 * IMPORTANT: This reservation makes the assumption that if this
3333 * transaction is the first in an iclog and hence has the LR headers
3334 * accounted to it, then the remaining space in the iclog is
3335 * exclusively for this transaction. i.e. if the transaction is larger
3336 * than the iclog, it will be the only thing in that iclog.
3337 * Fundamentally, this means we must pass the entire log vector to
3338 * xlog_write to guarantee this.
3339 */
3340 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3341 num_headers = howmany(unit_bytes, iclog_space);
3342
3343 /* for split-recs - ophdrs added when data split over LRs */
3344 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3345
3346 /* add extra header reservations if we overrun */
3347 while (!num_headers ||
3348 howmany(unit_bytes, iclog_space) > num_headers) {
3349 unit_bytes += sizeof(xlog_op_header_t);
3350 num_headers++;
3351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 unit_bytes += log->l_iclog_hsize * num_headers;
3353
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003354 /* for commit-rec LR header - note: padding will subsume the ophdr */
3355 unit_bytes += log->l_iclog_hsize;
3356
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003357 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003358 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003359 log->l_mp->m_sb.sb_logsunit > 1) {
3360 /* log su roundoff */
3361 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3362 } else {
3363 /* BB roundoff */
3364 unit_bytes += 2*BBSIZE;
3365 }
3366
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003367 atomic_set(&tic->t_ref, 1);
Dave Chinner10547942010-12-21 12:02:25 +11003368 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 tic->t_unit_res = unit_bytes;
3370 tic->t_curr_res = unit_bytes;
3371 tic->t_cnt = cnt;
3372 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003373 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 tic->t_clientid = client;
3375 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003376 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 if (xflags & XFS_LOG_PERM_RESERV)
3378 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Dave Chinnereb40a872010-12-21 12:09:01 +11003379 init_waitqueue_head(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Christoph Hellwig0adba532007-08-30 17:21:46 +10003381 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003382
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
3386
3387/******************************************************************************
3388 *
3389 * Log debug routines
3390 *
3391 ******************************************************************************
3392 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003393#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394/*
3395 * Make sure that the destination ptr is within the valid data region of
3396 * one of the iclogs. This uses backup pointers stored in a different
3397 * part of the log in case we trash the log structure.
3398 */
3399void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003400xlog_verify_dest_ptr(
3401 struct log *log,
3402 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 int i;
3405 int good_ptr = 0;
3406
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003407 for (i = 0; i < log->l_iclog_bufs; i++) {
3408 if (ptr >= log->l_iclog_bak[i] &&
3409 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 good_ptr++;
3411 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003412
3413 if (!good_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417STATIC void
3418xlog_verify_grant_head(xlog_t *log, int equals)
3419{
Dave Chinnera69ed032010-12-21 12:08:20 +11003420 int reserve_cycle, reserve_space;
3421 int write_cycle, write_space;
3422
3423 xlog_crack_grant_head(&log->l_grant_reserve_head,
3424 &reserve_cycle, &reserve_space);
3425 xlog_crack_grant_head(&log->l_grant_write_head,
3426 &write_cycle, &write_space);
3427
3428 if (reserve_cycle == write_cycle) {
3429 if (equals)
3430 ASSERT(reserve_space >= write_space);
3431 else
3432 ASSERT(reserve_space > write_space);
3433 } else {
3434 ASSERT(reserve_cycle - 1 == write_cycle);
3435 ASSERT(write_space >= reserve_space);
3436 }
3437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Dave Chinner3f336c62010-12-21 12:02:52 +11003439STATIC void
3440xlog_verify_grant_tail(
3441 struct log *log)
3442{
3443 xfs_lsn_t tail_lsn = log->l_tail_lsn;
Dave Chinnera69ed032010-12-21 12:08:20 +11003444 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003445
3446 /*
3447 * Check to make sure the grant write head didn't just over lap the
3448 * tail. If the cycles are the same, we can't be overlapping.
3449 * Otherwise, make sure that the cycles differ by exactly one and
3450 * check the byte count.
3451 */
Dave Chinnera69ed032010-12-21 12:08:20 +11003452 xlog_crack_grant_head(&log->l_grant_write_head, &cycle, &space);
3453 if (CYCLE_LSN(tail_lsn) != cycle) {
3454 ASSERT(cycle - 1 == CYCLE_LSN(tail_lsn));
3455 ASSERT(space <= BBTOB(BLOCK_LSN(tail_lsn)));
Dave Chinner3f336c62010-12-21 12:02:52 +11003456 }
3457}
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459/* check if it will fit */
3460STATIC void
3461xlog_verify_tail_lsn(xlog_t *log,
3462 xlog_in_core_t *iclog,
3463 xfs_lsn_t tail_lsn)
3464{
3465 int blocks;
3466
3467 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3468 blocks =
3469 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3470 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3471 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3472 } else {
3473 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3474
3475 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3476 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3477
3478 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3479 if (blocks < BTOBB(iclog->ic_offset) + 1)
3480 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3481 }
3482} /* xlog_verify_tail_lsn */
3483
3484/*
3485 * Perform a number of checks on the iclog before writing to disk.
3486 *
3487 * 1. Make sure the iclogs are still circular
3488 * 2. Make sure we have a good magic number
3489 * 3. Make sure we don't have magic numbers in the data
3490 * 4. Check fields of each log operation header for:
3491 * A. Valid client identifier
3492 * B. tid ptr value falls in valid ptr space (user space code)
3493 * C. Length in log record header is correct according to the
3494 * individual operation headers within record.
3495 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3496 * log, check the preceding blocks of the physical log to make sure all
3497 * the cycle numbers agree with the current cycle number.
3498 */
3499STATIC void
3500xlog_verify_iclog(xlog_t *log,
3501 xlog_in_core_t *iclog,
3502 int count,
3503 boolean_t syncing)
3504{
3505 xlog_op_header_t *ophead;
3506 xlog_in_core_t *icptr;
3507 xlog_in_core_2_t *xhdr;
3508 xfs_caddr_t ptr;
3509 xfs_caddr_t base_ptr;
3510 __psint_t field_offset;
3511 __uint8_t clientid;
3512 int len, i, j, k, op_len;
3513 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
3515 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003516 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 icptr = log->l_iclog;
3518 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003519 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 xlog_panic("xlog_verify_iclog: invalid ptr");
3521 icptr = icptr->ic_next;
3522 }
3523 if (icptr != log->l_iclog)
3524 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003525 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003528 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 xlog_panic("xlog_verify_iclog: invalid magic num");
3530
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003531 ptr = (xfs_caddr_t) &iclog->ic_header;
3532 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003534 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 xlog_panic("xlog_verify_iclog: unexpected magic num");
3536 }
3537
3538 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003539 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 ptr = iclog->ic_datap;
3541 base_ptr = ptr;
3542 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003543 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 for (i = 0; i < len; i++) {
3545 ophead = (xlog_op_header_t *)ptr;
3546
3547 /* clientid is only 1 byte */
3548 field_offset = (__psint_t)
3549 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3550 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3551 clientid = ophead->oh_clientid;
3552 } else {
3553 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3554 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3555 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3556 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003557 clientid = xlog_get_client_id(
3558 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003560 clientid = xlog_get_client_id(
3561 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 }
3563 }
3564 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003565 cmn_err(CE_WARN, "xlog_verify_iclog: "
3566 "invalid clientid %d op 0x%p offset 0x%lx",
3567 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
3569 /* check length */
3570 field_offset = (__psint_t)
3571 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3572 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003573 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 } else {
3575 idx = BTOBBT((__psint_t)&ophead->oh_len -
3576 (__psint_t)iclog->ic_datap);
3577 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3578 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3579 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003580 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003582 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
3584 }
3585 ptr += sizeof(xlog_op_header_t) + op_len;
3586 }
3587} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003588#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003591 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 */
3593STATIC int
3594xlog_state_ioerror(
3595 xlog_t *log)
3596{
3597 xlog_in_core_t *iclog, *ic;
3598
3599 iclog = log->l_iclog;
3600 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3601 /*
3602 * Mark all the incore logs IOERROR.
3603 * From now on, no log flushes will result.
3604 */
3605 ic = iclog;
3606 do {
3607 ic->ic_state = XLOG_STATE_IOERROR;
3608 ic = ic->ic_next;
3609 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
3612 /*
3613 * Return non-zero, if state transition has already happened.
3614 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003615 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
3618/*
3619 * This is called from xfs_force_shutdown, when we're forcibly
3620 * shutting down the filesystem, typically because of an IO error.
3621 * Our main objectives here are to make sure that:
3622 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3623 * parties to find out, 'atomically'.
3624 * b. those who're sleeping on log reservations, pinned objects and
3625 * other resources get woken up, and be told the bad news.
3626 * c. nothing new gets queued up after (a) and (b) are done.
3627 * d. if !logerror, flush the iclogs to disk, then seal them off
3628 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003629 *
3630 * Note: for delayed logging the !logerror case needs to flush the regions
3631 * held in memory out to the iclogs before flushing them to disk. This needs
3632 * to be done before the log is marked as shutdown, otherwise the flush to the
3633 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 */
3635int
3636xfs_log_force_umount(
3637 struct xfs_mount *mp,
3638 int logerror)
3639{
3640 xlog_ticket_t *tic;
3641 xlog_t *log;
3642 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
3644 log = mp->m_log;
3645
3646 /*
3647 * If this happens during log recovery, don't worry about
3648 * locking; the log isn't open for business yet.
3649 */
3650 if (!log ||
3651 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3652 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003653 if (mp->m_sb_bp)
3654 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657
3658 /*
3659 * Somebody could've already done the hard work for us.
3660 * No need to get locks for this.
3661 */
3662 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3663 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003664 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003667
3668 /*
3669 * Flush the in memory commit item list before marking the log as
3670 * being shut down. We need to do it in this order to ensure all the
3671 * completed transactions are flushed to disk with the xfs_log_force()
3672 * call below.
3673 */
3674 if (!logerror && (mp->m_flags & XFS_MOUNT_DELAYLOG))
Dave Chinnera44f13e2010-08-24 11:40:03 +10003675 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 /*
3678 * We must hold both the GRANT lock and the LOG lock,
3679 * before we mark the filesystem SHUTDOWN and wake
3680 * everybody up to tell the bad news.
3681 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003682 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003683 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003685 if (mp->m_sb_bp)
3686 XFS_BUF_DONE(mp->m_sb_bp);
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 /*
3689 * This flag is sort of redundant because of the mount flag, but
3690 * it's good to maintain the separation between the log and the rest
3691 * of XFS.
3692 */
3693 log->l_flags |= XLOG_IO_ERROR;
3694
3695 /*
3696 * If we hit a log error, we want to mark all the iclogs IOERROR
3697 * while we're still holding the loglock.
3698 */
3699 if (logerror)
3700 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003701 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 /*
Dave Chinner10547942010-12-21 12:02:25 +11003704 * We don't want anybody waiting for log reservations after this. That
3705 * means we have to wake up everybody queued up on reserveq as well as
3706 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3707 * we don't enqueue anything once the SHUTDOWN flag is set, and this
3708 * action is protected by the GRANTLOCK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 */
Dave Chinner10547942010-12-21 12:02:25 +11003710 list_for_each_entry(tic, &log->l_reserveq, t_queue)
Dave Chinnereb40a872010-12-21 12:09:01 +11003711 wake_up(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Dave Chinner10547942010-12-21 12:02:25 +11003713 list_for_each_entry(tic, &log->l_writeq, t_queue)
Dave Chinnereb40a872010-12-21 12:09:01 +11003714 wake_up(&tic->t_wait);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003715 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003717 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 ASSERT(!logerror);
3719 /*
3720 * Force the incore logs to disk before shutting the
3721 * log down completely.
3722 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003723 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3724
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003725 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003727 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 }
3729 /*
3730 * Wake up everybody waiting on xfs_log_force.
3731 * Callback all log item committed functions as if the
3732 * log writes were completed.
3733 */
3734 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3735
3736#ifdef XFSERRORDEBUG
3737 {
3738 xlog_in_core_t *iclog;
3739
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003740 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 iclog = log->l_iclog;
3742 do {
3743 ASSERT(iclog->ic_callback == 0);
3744 iclog = iclog->ic_next;
3745 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003746 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 }
3748#endif
3749 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003750 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003753STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754xlog_iclogs_empty(xlog_t *log)
3755{
3756 xlog_in_core_t *iclog;
3757
3758 iclog = log->l_iclog;
3759 do {
3760 /* endianness does not matter here, zero is zero in
3761 * any language.
3762 */
3763 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 iclog = iclog->ic_next;
3766 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003767 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768}