blob: 2be019136287666ae84e565093924fe0ba8e76a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_log_priv.h"
32#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_dir2_sf.h"
39#include "xfs_attr_sf.h"
40#include "xfs_dinode.h"
41#include "xfs_inode.h"
42#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
David Chinnereb01c9c2008-04-10 12:18:46 +100045kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define xlog_write_adv_cnt(ptr, len, off, bytes) \
48 { (ptr) += (bytes); \
49 (len) -= (bytes); \
50 (off) += (bytes);}
51
52/* Local miscellaneous function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
54 xlog_in_core_t **, xfs_lsn_t *);
55STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
56 xfs_buftarg_t *log_target,
57 xfs_daddr_t blk_offset,
58 int num_bblks);
59STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes);
60STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100061STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
Christoph Hellwig35a8a722010-02-15 23:34:54 +000063 int nentries, struct xlog_ticket *tic,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 xfs_lsn_t *start_lsn,
65 xlog_in_core_t **commit_iclog,
66 uint flags);
67
68/* local state machine functions */
69STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
70STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
71STATIC int xlog_state_get_iclog_space(xlog_t *log,
72 int len,
73 xlog_in_core_t **iclog,
74 xlog_ticket_t *ticket,
75 int *continued_write,
76 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077STATIC int xlog_state_release_iclog(xlog_t *log,
78 xlog_in_core_t *iclog);
79STATIC void xlog_state_switch_iclogs(xlog_t *log,
80 xlog_in_core_t *iclog,
81 int eventual_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
83
84/* local functions to manipulate grant head */
85STATIC int xlog_grant_log_space(xlog_t *log,
86 xlog_ticket_t *xtic);
87STATIC void xlog_grant_push_ail(xfs_mount_t *mp,
88 int need_bytes);
89STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
90 xlog_ticket_t *ticket);
91STATIC int xlog_regrant_write_log_space(xlog_t *log,
92 xlog_ticket_t *ticket);
93STATIC void xlog_ungrant_log_space(xlog_t *log,
94 xlog_ticket_t *ticket);
95
96
97/* local ticket functions */
Dave Chinnercc09c0d2008-11-17 17:37:10 +110098STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int unit_bytes,
100 int count,
101 char clientid,
102 uint flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100104#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
106STATIC void xlog_verify_grant_head(xlog_t *log, int equals);
107STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
108 int count, boolean_t syncing);
109STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
110 xfs_lsn_t tail_lsn);
111#else
112#define xlog_verify_dest_ptr(a,b)
113#define xlog_verify_grant_head(a,b)
114#define xlog_verify_iclog(a,b,c,d)
115#define xlog_verify_tail_lsn(a,b,c)
116#endif
117
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000118STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100120
121static void
122xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
123{
124 if (*qp) {
125 tic->t_next = (*qp);
126 tic->t_prev = (*qp)->t_prev;
127 (*qp)->t_prev->t_next = tic;
128 (*qp)->t_prev = tic;
129 } else {
130 tic->t_prev = tic->t_next = tic;
131 *qp = tic;
132 }
133
134 tic->t_flags |= XLOG_TIC_IN_Q;
135}
136
137static void
138xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
139{
140 if (tic == tic->t_next) {
141 *qp = NULL;
142 } else {
143 *qp = tic->t_next;
144 tic->t_next->t_prev = tic->t_prev;
145 tic->t_prev->t_next = tic->t_next;
146 }
147
148 tic->t_next = tic->t_prev = NULL;
149 tic->t_flags &= ~XLOG_TIC_IN_Q;
150}
151
152static void
153xlog_grant_sub_space(struct log *log, int bytes)
154{
155 log->l_grant_write_bytes -= bytes;
156 if (log->l_grant_write_bytes < 0) {
157 log->l_grant_write_bytes += log->l_logsize;
158 log->l_grant_write_cycle--;
159 }
160
161 log->l_grant_reserve_bytes -= bytes;
162 if ((log)->l_grant_reserve_bytes < 0) {
163 log->l_grant_reserve_bytes += log->l_logsize;
164 log->l_grant_reserve_cycle--;
165 }
166
167}
168
169static void
170xlog_grant_add_space_write(struct log *log, int bytes)
171{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000172 int tmp = log->l_logsize - log->l_grant_write_bytes;
173 if (tmp > bytes)
174 log->l_grant_write_bytes += bytes;
175 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100176 log->l_grant_write_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000177 log->l_grant_write_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100178 }
179}
180
181static void
182xlog_grant_add_space_reserve(struct log *log, int bytes)
183{
Michael Nishimotod729eae2008-05-19 16:34:20 +1000184 int tmp = log->l_logsize - log->l_grant_reserve_bytes;
185 if (tmp > bytes)
186 log->l_grant_reserve_bytes += bytes;
187 else {
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100188 log->l_grant_reserve_cycle++;
Michael Nishimotod729eae2008-05-19 16:34:20 +1000189 log->l_grant_reserve_bytes = bytes - tmp;
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100190 }
191}
192
193static inline void
194xlog_grant_add_space(struct log *log, int bytes)
195{
196 xlog_grant_add_space_write(log, bytes);
197 xlog_grant_add_space_reserve(log, bytes);
198}
199
Christoph Hellwig0adba532007-08-30 17:21:46 +1000200static void
201xlog_tic_reset_res(xlog_ticket_t *tic)
202{
203 tic->t_res_num = 0;
204 tic->t_res_arr_sum = 0;
205 tic->t_res_num_ophdrs = 0;
206}
207
208static void
209xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
210{
211 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
212 /* add to overflow and start again */
213 tic->t_res_o_flow += tic->t_res_arr_sum;
214 tic->t_res_num = 0;
215 tic->t_res_arr_sum = 0;
216 }
217
218 tic->t_res_arr[tic->t_res_num].r_len = len;
219 tic->t_res_arr[tic->t_res_num].r_type = type;
220 tic->t_res_arr_sum += len;
221 tic->t_res_num++;
222}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * NOTES:
226 *
227 * 1. currblock field gets updated at startup and after in-core logs
228 * marked as with WANT_SYNC.
229 */
230
231/*
232 * This routine is called when a user of a log manager ticket is done with
233 * the reservation. If the ticket was ever used, then a commit record for
234 * the associated transaction is written out as a log operation header with
235 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
236 * a given ticket. If the ticket was one with a permanent reservation, then
237 * a few operations are done differently. Permanent reservation tickets by
238 * default don't release the reservation. They just commit the current
239 * transaction with the belief that the reservation is still needed. A flag
240 * must be passed in before permanent reservations are actually released.
241 * When these type of tickets are not released, they need to be set into
242 * the inited state again. By doing this, a start record will be written
243 * out when the next write occurs.
244 */
245xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000246xfs_log_done(
247 struct xfs_mount *mp,
248 struct xlog_ticket *ticket,
249 struct xlog_in_core **iclog,
250 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000252 struct log *log = mp->m_log;
253 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (XLOG_FORCED_SHUTDOWN(log) ||
256 /*
257 * If nothing was ever written, don't write out commit record.
258 * If we get an error, just continue and give back the log ticket.
259 */
260 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000261 (xlog_commit_record(mp, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 lsn = (xfs_lsn_t) -1;
263 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
264 flags |= XFS_LOG_REL_PERM_RESERV;
265 }
266 }
267
268
269 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
270 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000271 trace_xfs_log_done_nonperm(log, ticket);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000274 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * request has been made to release a permanent reservation.
276 */
277 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100278 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000280 trace_xfs_log_done_perm(log, ticket);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000283 /* If this ticket was a permanent reservation and we aren't
284 * trying to release it, reset the inited flags; so next time
285 * we write, a start record will be written out.
286 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000291}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * Attaches a new iclog I/O completion callback routine during
295 * transaction commit. If the log is in error state, a non-zero
296 * return code is handed back and the caller is responsible for
297 * executing the callback at an appropriate time.
298 */
299int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000300xfs_log_notify(
301 struct xfs_mount *mp,
302 struct xlog_in_core *iclog,
303 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000305 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Chinner114d23a2008-04-10 12:18:39 +1000307 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
309 if (!abortflg) {
310 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
311 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
312 cb->cb_next = NULL;
313 *(iclog->ic_callback_tail) = cb;
314 iclog->ic_callback_tail = &(cb->cb_next);
315 }
David Chinner114d23a2008-04-10 12:18:39 +1000316 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000318}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000321xfs_log_release_iclog(
322 struct xfs_mount *mp,
323 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000325 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000326 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100327 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 return 0;
331}
332
333/*
334 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
335 * to the reservation.
336 * 2. Potentially, push buffers at tail of log to disk.
337 *
338 * Each reservation is going to reserve extra space for a log record header.
339 * When writes happen to the on-disk log, we don't subtract the length of the
340 * log record header from any reservation. By wasting space in each
341 * reservation, we prevent over allocation problems.
342 */
343int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000344xfs_log_reserve(
345 struct xfs_mount *mp,
346 int unit_bytes,
347 int cnt,
348 struct xlog_ticket **ticket,
349 __uint8_t client,
350 uint flags,
351 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000353 struct log *log = mp->m_log;
354 struct xlog_ticket *internal_ticket;
355 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
358 ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
359
360 if (XLOG_FORCED_SHUTDOWN(log))
361 return XFS_ERROR(EIO);
362
363 XFS_STATS_INC(xs_try_logspace);
364
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (*ticket != NULL) {
367 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000368 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000369
370 trace_xfs_log_reserve(log, internal_ticket);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
373 retval = xlog_regrant_write_log_space(log, internal_ticket);
374 } else {
375 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100376 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 client, flags);
David Chinnereb01c9c2008-04-10 12:18:46 +1000378 if (!internal_ticket)
379 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000380 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000382
383 trace_xfs_log_reserve(log, internal_ticket);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 xlog_grant_push_ail(mp,
386 (internal_ticket->t_unit_res *
387 internal_ticket->t_cnt));
388 retval = xlog_grant_log_space(log, internal_ticket);
389 }
390
391 return retval;
392} /* xfs_log_reserve */
393
394
395/*
396 * Mount a log filesystem
397 *
398 * mp - ubiquitous xfs mount point structure
399 * log_target - buftarg of on-disk log device
400 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
401 * num_bblocks - Number of BBSIZE blocks in on-disk log
402 *
403 * Return error or zero.
404 */
405int
David Chinner249a8c12008-02-05 12:13:32 +1100406xfs_log_mount(
407 xfs_mount_t *mp,
408 xfs_buftarg_t *log_target,
409 xfs_daddr_t blk_offset,
410 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
David Chinner249a8c12008-02-05 12:13:32 +1100412 int error;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
415 cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
416 else {
417 cmn_err(CE_NOTE,
418 "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.",
419 mp->m_fsname);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000420 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200424 if (IS_ERR(mp->m_log)) {
425 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100426 goto out;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
David Chinner249a8c12008-02-05 12:13:32 +1100430 * Initialize the AIL now we have a log.
431 */
David Chinner249a8c12008-02-05 12:13:32 +1100432 error = xfs_trans_ail_init(mp);
433 if (error) {
434 cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100435 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100436 }
David Chinnera9c21c12008-10-30 17:39:35 +1100437 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100438
439 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * skip log recovery on a norecovery mount. pretend it all
441 * just worked.
442 */
443 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100444 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000447 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Eric Sandeen65be6052006-01-11 15:34:19 +1100449 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000452 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (error) {
454 cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100455 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 }
458
459 /* Normal transactions can now occur */
460 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100463
464out_destroy_ail:
465 xfs_trans_ail_destroy(mp);
466out_free_log:
467 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100468out:
David Chinner249a8c12008-02-05 12:13:32 +1100469 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472/*
473 * Finish the recovery of the file system. This is separate from
474 * the xfs_log_mount() call, because it depends on the code in
475 * xfs_mountfs() to read in the root and real-time bitmap inodes
476 * between calling xfs_log_mount() and here.
477 *
478 * mp - ubiquitous xfs mount point structure
479 */
480int
Christoph Hellwig42490232008-08-13 16:49:32 +1000481xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 int error;
484
485 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000486 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 else {
488 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000489 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 return error;
493}
494
495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * Final log writes as part of unmount.
497 *
498 * Mark the filesystem clean as unmount happens. Note that during relocation
499 * this routine needs to be executed as part of source-bag while the
500 * deallocation must not be done until source-end.
501 */
502
503/*
504 * Unmount record used to have a string "Unmount filesystem--" in the
505 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
506 * We just write the magic number now since that particular field isn't
507 * currently architecture converted and "nUmount" is a bit foo.
508 * As far as I know, there weren't any dependencies on the old behaviour.
509 */
510
511int
512xfs_log_unmount_write(xfs_mount_t *mp)
513{
514 xlog_t *log = mp->m_log;
515 xlog_in_core_t *iclog;
516#ifdef DEBUG
517 xlog_in_core_t *first_iclog;
518#endif
519 xfs_log_iovec_t reg[1];
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000520 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 xfs_lsn_t lsn;
522 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /* the data section must be 32 bit size aligned */
525 struct {
526 __uint16_t magic;
527 __uint16_t pad1;
528 __uint32_t pad2; /* may as well make it 64 bits */
529 } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
532 * Don't write out unmount record on read-only mounts.
533 * Or, if we are doing a forced umount (typically because of IO errors).
534 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000535 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000538 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000539 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541#ifdef DEBUG
542 first_iclog = iclog = log->l_iclog;
543 do {
544 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
545 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
546 ASSERT(iclog->ic_offset == 0);
547 }
548 iclog = iclog->ic_next;
549 } while (iclog != first_iclog);
550#endif
551 if (! (XLOG_FORCED_SHUTDOWN(log))) {
552 reg[0].i_addr = (void*)&magic;
553 reg[0].i_len = sizeof(magic);
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000554 reg[0].i_type = XLOG_REG_TYPE_UNMOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Tim Shimmin955e47a2006-09-28 11:04:16 +1000556 error = xfs_log_reserve(mp, 600, 1, &tic,
557 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!error) {
559 /* remove inited flag */
560 ((xlog_ticket_t *)tic)->t_flags = 0;
561 error = xlog_write(mp, reg, 1, tic, &lsn,
562 NULL, XLOG_UNMOUNT_TRANS);
563 /*
564 * At this point, we're umounting anyway,
565 * so there's no point in transitioning log state
566 * to IOERROR. Just continue...
567 */
568 }
569
570 if (error) {
571 xfs_fs_cmn_err(CE_ALERT, mp,
572 "xfs_log_unmount: unmount record failed");
573 }
574
575
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000576 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100578 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100580 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000581 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000583 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
585 iclog->ic_state == XLOG_STATE_DIRTY)) {
586 if (!XLOG_FORCED_SHUTDOWN(log)) {
David Chinner12017fa2008-08-13 16:34:31 +1000587 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 &log->l_icloglock, s);
589 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000590 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000593 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000595 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000596 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000597 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100598 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } else {
601 /*
602 * We're already in forced_shutdown mode, couldn't
603 * even attempt to write out the unmount transaction.
604 *
605 * Go through the motions of sync'ing and releasing
606 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000607 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * be in progress. Do this as a separate section of
609 * code so we'll know if we ever get stuck here that
610 * we're in this odd situation of trying to unmount
611 * a file system that went into forced_shutdown as
612 * the result of an unmount..
613 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000614 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100616 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100619 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000620 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000622 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
625 || iclog->ic_state == XLOG_STATE_DIRTY
626 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
627
David Chinner12017fa2008-08-13 16:34:31 +1000628 sv_wait(&iclog->ic_force_wait, PMEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 &log->l_icloglock, s);
630 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000631 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
634
David Chinner1bb7d6b2008-04-10 12:24:38 +1000635 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636} /* xfs_log_unmount_write */
637
638/*
639 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100640 *
641 * We need to stop the aild from running before we destroy
642 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100645xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
David Chinner249a8c12008-02-05 12:13:32 +1100647 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000648 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
652 * Write region vectors to log. The write happens using the space reservation
653 * of the ticket (tic). It is not a requirement that all writes for a given
654 * transaction occur with one call to xfs_log_write().
655 */
656int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000657xfs_log_write(
658 struct xfs_mount *mp,
659 struct xfs_log_iovec reg[],
660 int nentries,
661 struct xlog_ticket *tic,
662 xfs_lsn_t *start_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000664 struct log *log = mp->m_log;
665 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (XLOG_FORCED_SHUTDOWN(log))
668 return XFS_ERROR(EIO);
669
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000670 error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0);
671 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +1000672 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100673 return error;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676void
677xfs_log_move_tail(xfs_mount_t *mp,
678 xfs_lsn_t tail_lsn)
679{
680 xlog_ticket_t *tic;
681 xlog_t *log = mp->m_log;
682 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (XLOG_FORCED_SHUTDOWN(log))
685 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (tail_lsn == 0) {
688 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000689 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000691 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000694 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
697 * tail_lsn.
698 */
699 if (tail_lsn != 1) {
700 log->l_tail_lsn = tail_lsn;
701 }
702
703 if ((tic = log->l_write_headq)) {
704#ifdef DEBUG
705 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
706 panic("Recovery problem");
707#endif
708 cycle = log->l_grant_write_cycle;
709 bytes = log->l_grant_write_bytes;
710 free_bytes = xlog_space_left(log, cycle, bytes);
711 do {
712 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
713
714 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
715 break;
716 tail_lsn = 0;
717 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000718 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 tic = tic->t_next;
720 } while (tic != log->l_write_headq);
721 }
722 if ((tic = log->l_reserve_headq)) {
723#ifdef DEBUG
724 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
725 panic("Recovery problem");
726#endif
727 cycle = log->l_grant_reserve_cycle;
728 bytes = log->l_grant_reserve_bytes;
729 free_bytes = xlog_space_left(log, cycle, bytes);
730 do {
731 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
732 need_bytes = tic->t_unit_res*tic->t_cnt;
733 else
734 need_bytes = tic->t_unit_res;
735 if (free_bytes < need_bytes && tail_lsn != 1)
736 break;
737 tail_lsn = 0;
738 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000739 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 tic = tic->t_next;
741 } while (tic != log->l_reserve_headq);
742 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000743 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744} /* xfs_log_move_tail */
745
746/*
747 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000748 * that needs to be covered. To begin the transition to the idle state
749 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
750 * If we are then in a state where covering is needed, the caller is informed
751 * that dummy transactions are required to move the log into the idle state.
752 *
753 * Because this is called as part of the sync process, we should also indicate
754 * that dummy transactions should be issued in anything but the covered or
755 * idle states. This ensures that the log tail is accurately reflected in
756 * the log at the end of the sync, hence if a crash occurrs avoids replay
757 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
759int
760xfs_log_need_covered(xfs_mount_t *mp)
761{
David Chinner27d8d5f2008-10-30 17:38:39 +1100762 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Chinner92821e22007-05-24 15:26:31 +1000765 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return 0;
767
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000768 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000769 switch (log->l_covered_state) {
770 case XLOG_STATE_COVER_DONE:
771 case XLOG_STATE_COVER_DONE2:
772 case XLOG_STATE_COVER_IDLE:
773 break;
774 case XLOG_STATE_COVER_NEED:
775 case XLOG_STATE_COVER_NEED2:
776 if (!xfs_trans_ail_tail(log->l_ailp) &&
777 xlog_iclogs_empty(log)) {
778 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
779 log->l_covered_state = XLOG_STATE_COVER_DONE;
780 else
781 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000783 /* FALLTHRU */
784 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000788 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100789 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792/******************************************************************************
793 *
794 * local routines
795 *
796 ******************************************************************************
797 */
798
799/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
800 * The log manager must keep track of the last LR which was committed
801 * to disk. The lsn of this LR will become the new tail_lsn whenever
802 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
803 * the situation where stuff could be written into the log but nothing
804 * was ever in the AIL when asked. Eventually, we panic since the
805 * tail hits the head.
806 *
807 * We may be holding the log iclog lock upon entering this routine.
808 */
809xfs_lsn_t
810xlog_assign_tail_lsn(xfs_mount_t *mp)
811{
812 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 xlog_t *log = mp->m_log;
814
David Chinner5b00f142008-10-30 17:39:00 +1100815 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000816 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (tail_lsn != 0) {
818 log->l_tail_lsn = tail_lsn;
819 } else {
820 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
821 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000822 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 return tail_lsn;
825} /* xlog_assign_tail_lsn */
826
827
828/*
829 * Return the space in the log between the tail and the head. The head
830 * is passed in the cycle/bytes formal parms. In the special case where
831 * the reserve head has wrapped passed the tail, this calculation is no
832 * longer valid. In this case, just return 0 which means there is no space
833 * in the log. This works for all places where this function is called
834 * with the reserve head. Of course, if the write head were to ever
835 * wrap the tail, we should blow up. Rather than catch this case here,
836 * we depend on other ASSERTions in other parts of the code. XXXmiken
837 *
838 * This code also handles the case where the reservation head is behind
839 * the tail. The details of this case are described below, but the end
840 * result is that we return the size of the log as the amount of space left.
841 */
David Chinnera8272ce2007-11-23 16:28:09 +1100842STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843xlog_space_left(xlog_t *log, int cycle, int bytes)
844{
845 int free_bytes;
846 int tail_bytes;
847 int tail_cycle;
848
849 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
850 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
851 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
852 free_bytes = log->l_logsize - (bytes - tail_bytes);
853 } else if ((tail_cycle + 1) < cycle) {
854 return 0;
855 } else if (tail_cycle < cycle) {
856 ASSERT(tail_cycle == (cycle - 1));
857 free_bytes = tail_bytes - bytes;
858 } else {
859 /*
860 * The reservation head is behind the tail.
861 * In this case we just want to return the size of the
862 * log as the amount of space left.
863 */
864 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
865 "xlog_space_left: head behind tail\n"
866 " tail_cycle = %d, tail_bytes = %d\n"
867 " GH cycle = %d, GH bytes = %d",
868 tail_cycle, tail_bytes, cycle, bytes);
869 ASSERT(0);
870 free_bytes = log->l_logsize;
871 }
872 return free_bytes;
873} /* xlog_space_left */
874
875
876/*
877 * Log function which is called when an io completes.
878 *
879 * The log manager needs its own routine, in order to control what
880 * happens with the buffer after the write completes.
881 */
882void
883xlog_iodone(xfs_buf_t *bp)
884{
885 xlog_in_core_t *iclog;
886 xlog_t *l;
887 int aborted;
888
889 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
890 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
891 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
892 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 l = iclog->ic_log;
894
895 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100896 * If the _XFS_BARRIER_FAILED flag was set by a lower
897 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +1000898 * barrier I/O. Warn loudly and turn off barriers.
899 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100900 if (bp->b_flags & _XFS_BARRIER_FAILED) {
901 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000902 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
903 xfs_fs_cmn_err(CE_WARN, l->l_mp,
904 "xlog_iodone: Barriers are no longer supported"
905 " by device. Disabling barriers\n");
David Chinner0bfefc42007-05-14 18:24:23 +1000906 }
907
908 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * Race to shutdown the filesystem if we see an error.
910 */
911 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
912 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
913 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
914 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000915 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
917 * This flag will be propagated to the trans-committed
918 * callback routines to let them know that the log-commit
919 * didn't succeed.
920 */
921 aborted = XFS_LI_ABORTED;
922 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
923 aborted = XFS_LI_ABORTED;
924 }
David Chinner3db296f2007-05-14 18:24:16 +1000925
926 /* log I/O is always issued ASYNC */
927 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000929 /*
930 * do not reference the buffer (bp) here as we could race
931 * with it being freed after writing the unmount record to the
932 * log.
933 */
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935} /* xlog_iodone */
936
937/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * Return size of each in-core log record buffer.
939 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200940 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 *
942 * If the filesystem blocksize is too large, we may need to choose a
943 * larger size since the directory code currently logs entire blocks.
944 */
945
946STATIC void
947xlog_get_iclog_buffer_size(xfs_mount_t *mp,
948 xlog_t *log)
949{
950 int size;
951 int xhdrs;
952
Eric Sandeen1cb51252007-08-16 16:24:43 +1000953 if (mp->m_logbufs <= 0)
954 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
955 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100956 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /*
959 * Buffer size passed in from mount system call.
960 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100961 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 size = log->l_iclog_size = mp->m_logbsize;
963 log->l_iclog_size_log = 0;
964 while (size != 1) {
965 log->l_iclog_size_log++;
966 size >>= 1;
967 }
968
Eric Sandeen62118702008-03-06 13:44:28 +1100969 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200970 /* # headers = size / 32k
971 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
973
974 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
975 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
976 xhdrs++;
977 log->l_iclog_hsize = xhdrs << BBSHIFT;
978 log->l_iclog_heads = xhdrs;
979 } else {
980 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
981 log->l_iclog_hsize = BBSIZE;
982 log->l_iclog_heads = 1;
983 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100984 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200987 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +1000988 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
989 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* the default log size is 16k or 32k which is one header sector */
992 log->l_iclog_hsize = BBSIZE;
993 log->l_iclog_heads = 1;
994
Christoph Hellwig7153f8b2009-02-09 08:36:46 +0100995done:
996 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100997 if (mp->m_logbufs == 0)
998 mp->m_logbufs = log->l_iclog_bufs;
999 if (mp->m_logbsize == 0)
1000 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001} /* xlog_get_iclog_buffer_size */
1002
1003
1004/*
1005 * This routine initializes some of the log structure for a given mount point.
1006 * Its primary purpose is to fill in enough, so recovery can occur. However,
1007 * some other stuff may be filled in too.
1008 */
1009STATIC xlog_t *
1010xlog_alloc_log(xfs_mount_t *mp,
1011 xfs_buftarg_t *log_target,
1012 xfs_daddr_t blk_offset,
1013 int num_bblks)
1014{
1015 xlog_t *log;
1016 xlog_rec_header_t *head;
1017 xlog_in_core_t **iclogp;
1018 xlog_in_core_t *iclog, *prev_iclog=NULL;
1019 xfs_buf_t *bp;
1020 int i;
1021 int iclogsize;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001022 int error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Chinner644c3562008-11-10 16:50:24 +11001024 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001025 if (!log) {
1026 xlog_warn("XFS: Log allocation failed: No memory!");
1027 goto out;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 log->l_mp = mp;
1031 log->l_targ = log_target;
1032 log->l_logsize = BBTOB(num_bblks);
1033 log->l_logBBstart = blk_offset;
1034 log->l_logBBsize = num_bblks;
1035 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1036 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1037
1038 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001039 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1041 log->l_last_sync_lsn = log->l_tail_lsn;
1042 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1043 log->l_grant_reserve_cycle = 1;
1044 log->l_grant_write_cycle = 1;
1045
Dave Chinnera6cb7672009-04-06 18:39:27 +02001046 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001047 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001049 if (log->l_sectbb_log < 0 ||
1050 log->l_sectbb_log > mp->m_sectbb_log) {
1051 xlog_warn("XFS: Log sector size (0x%x) out of range.",
1052 log->l_sectbb_log);
1053 goto out_free_log;
1054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* for larger sector sizes, must have v2 or external log */
Dave Chinnera6cb7672009-04-06 18:39:27 +02001057 if (log->l_sectbb_log != 0 &&
1058 (log->l_logBBstart != 0 &&
1059 !xfs_sb_version_haslogv2(&mp->m_sb))) {
1060 xlog_warn("XFS: log sector size (0x%x) invalid "
1061 "for configuration.", log->l_sectbb_log);
1062 goto out_free_log;
1063 }
1064 if (mp->m_sb.sb_logsectlog < BBSHIFT) {
1065 xlog_warn("XFS: Log sector log (0x%x) too small.",
1066 mp->m_sb.sb_logsectlog);
1067 goto out_free_log;
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1071
1072 xlog_get_iclog_buffer_size(mp, log);
1073
Dave Chinnera6cb7672009-04-06 18:39:27 +02001074 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001076 if (!bp)
1077 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1080 ASSERT(XFS_BUF_ISBUSY(bp));
1081 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1082 log->l_xbuf = bp;
1083
Eric Sandeen007c61c2007-10-11 17:43:56 +10001084 spin_lock_init(&log->l_icloglock);
1085 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001086 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1089 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1090
1091 iclogp = &log->l_iclog;
1092 /*
1093 * The amount of memory to allocate for the iclog structure is
1094 * rather funky due to the way the structure is defined. It is
1095 * done this way so that we can use different sizes for machines
1096 * with different amounts of memory. See the definition of
1097 * xlog_in_core_t in xfs_log_priv.h for details.
1098 */
1099 iclogsize = log->l_iclog_size;
1100 ASSERT(log->l_iclog_size >= 4096);
1101 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001102 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1103 if (!*iclogp)
1104 goto out_free_iclog;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 iclog->ic_prev = prev_iclog;
1108 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001109
1110 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001111 if (!bp)
1112 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001113 if (!XFS_BUF_CPSEMA(bp))
1114 ASSERT(0);
1115 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001116 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1117 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001118 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001119#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 head = &iclog->ic_header;
1123 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001124 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1125 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001126 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001127 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001129 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1133 iclog->ic_state = XLOG_STATE_ACTIVE;
1134 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001135 atomic_set(&iclog->ic_refcnt, 0);
1136 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001138 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1141 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001142 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1143 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 iclogp = &iclog->ic_next;
1146 }
1147 *iclogp = log->l_iclog; /* complete ring */
1148 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1149
1150 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001151
1152out_free_iclog:
1153 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1154 prev_iclog = iclog->ic_next;
1155 if (iclog->ic_bp) {
1156 sv_destroy(&iclog->ic_force_wait);
1157 sv_destroy(&iclog->ic_write_wait);
1158 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001159 }
1160 kmem_free(iclog);
1161 }
1162 spinlock_destroy(&log->l_icloglock);
1163 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001164 xfs_buf_free(log->l_xbuf);
1165out_free_log:
1166 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001167out:
1168 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169} /* xlog_alloc_log */
1170
1171
1172/*
1173 * Write out the commit record of a transaction associated with the given
1174 * ticket. Return the lsn of the commit record.
1175 */
1176STATIC int
1177xlog_commit_record(xfs_mount_t *mp,
1178 xlog_ticket_t *ticket,
1179 xlog_in_core_t **iclog,
1180 xfs_lsn_t *commitlsnp)
1181{
1182 int error;
1183 xfs_log_iovec_t reg[1];
1184
1185 reg[0].i_addr = NULL;
1186 reg[0].i_len = 0;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +00001187 reg[0].i_type = XLOG_REG_TYPE_COMMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 ASSERT_ALWAYS(iclog);
1190 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1191 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001192 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001194 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195} /* xlog_commit_record */
1196
1197
1198/*
1199 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1200 * log space. This code pushes on the lsn which would supposedly free up
1201 * the 25% which we want to leave free. We may need to adopt a policy which
1202 * pushes on an lsn which is further along in the log once we reach the high
1203 * water mark. In this manner, we would be creating a low water mark.
1204 */
David Chinnera8272ce2007-11-23 16:28:09 +11001205STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206xlog_grant_push_ail(xfs_mount_t *mp,
1207 int need_bytes)
1208{
1209 xlog_t *log = mp->m_log; /* pointer to the log */
1210 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1211 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1212 int free_blocks; /* free blocks left to write to */
1213 int free_bytes; /* free bytes left to write to */
1214 int threshold_block; /* block in lsn we'd like to be at */
1215 int threshold_cycle; /* lsn cycle we'd like to be at */
1216 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1219
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001220 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 free_bytes = xlog_space_left(log,
1222 log->l_grant_reserve_cycle,
1223 log->l_grant_reserve_bytes);
1224 tail_lsn = log->l_tail_lsn;
1225 free_blocks = BTOBBT(free_bytes);
1226
1227 /*
1228 * Set the threshold for the minimum number of free blocks in the
1229 * log to the maximum of what the caller needs, one quarter of the
1230 * log, and 256 blocks.
1231 */
1232 free_threshold = BTOBB(need_bytes);
1233 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1234 free_threshold = MAX(free_threshold, 256);
1235 if (free_blocks < free_threshold) {
1236 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1237 threshold_cycle = CYCLE_LSN(tail_lsn);
1238 if (threshold_block >= log->l_logBBsize) {
1239 threshold_block -= log->l_logBBsize;
1240 threshold_cycle += 1;
1241 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001242 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /* Don't pass in an lsn greater than the lsn of the last
1245 * log record known to be on disk.
1246 */
1247 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1248 threshold_lsn = log->l_last_sync_lsn;
1249 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001250 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /*
1253 * Get the transaction layer to kick the dirty buffers out to
1254 * disk asynchronously. No point in trying to do this if
1255 * the filesystem is shutting down.
1256 */
1257 if (threshold_lsn &&
1258 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001259 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260} /* xlog_grant_push_ail */
1261
Christoph Hellwig873ff552010-01-13 22:17:57 +00001262/*
1263 * The bdstrat callback function for log bufs. This gives us a central
1264 * place to trap bufs in case we get hit by a log I/O error and need to
1265 * shutdown. Actually, in practice, even when we didn't get a log error,
1266 * we transition the iclogs to IOERROR state *after* flushing all existing
1267 * iclogs to disk. This is because we don't want anymore new transactions to be
1268 * started or completed afterwards.
1269 */
1270STATIC int
1271xlog_bdstrat(
1272 struct xfs_buf *bp)
1273{
1274 struct xlog_in_core *iclog;
1275
1276 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1277 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1278 XFS_BUF_ERROR(bp, EIO);
1279 XFS_BUF_STALE(bp);
1280 xfs_biodone(bp);
1281 /*
1282 * It would seem logical to return EIO here, but we rely on
1283 * the log state machine to propagate I/O errors instead of
1284 * doing it here.
1285 */
1286 return 0;
1287 }
1288
1289 bp->b_flags |= _XBF_RUN_QUEUES;
1290 xfs_buf_iorequest(bp);
1291 return 0;
1292}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294/*
1295 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1296 * fashion. Previously, we should have moved the current iclog
1297 * ptr in the log to point to the next available iclog. This allows further
1298 * write to continue while this code syncs out an iclog ready to go.
1299 * Before an in-core log can be written out, the data section must be scanned
1300 * to save away the 1st word of each BBSIZE block into the header. We replace
1301 * it with the current cycle count. Each BBSIZE block is tagged with the
1302 * cycle count because there in an implicit assumption that drives will
1303 * guarantee that entire 512 byte blocks get written at once. In other words,
1304 * we can't have part of a 512 byte block written and part not written. By
1305 * tagging each block, we will know which blocks are valid when recovering
1306 * after an unclean shutdown.
1307 *
1308 * This routine is single threaded on the iclog. No other thread can be in
1309 * this routine with the same iclog. Changing contents of iclog can there-
1310 * fore be done without grabbing the state machine lock. Updating the global
1311 * log will require grabbing the lock though.
1312 *
1313 * The entire log manager uses a logical block numbering scheme. Only
1314 * log_sync (and then only bwrite()) know about the fact that the log may
1315 * not start with block zero on a given device. The log block start offset
1316 * is added immediately before calling bwrite().
1317 */
1318
David Chinnera8272ce2007-11-23 16:28:09 +11001319STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320xlog_sync(xlog_t *log,
1321 xlog_in_core_t *iclog)
1322{
1323 xfs_caddr_t dptr; /* pointer to byte sized element */
1324 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 uint count; /* byte count of bwrite */
1327 uint count_init; /* initial count before roundup */
1328 int roundoff; /* roundoff to BB or stripe */
1329 int split = 0; /* split write into two regions */
1330 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001331 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001334 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /* Add for LR header */
1337 count_init = log->l_iclog_hsize + iclog->ic_offset;
1338
1339 /* Round out the log write size */
1340 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1341 /* we have a v2 stripe unit to use */
1342 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1343 } else {
1344 count = BBTOB(BTOBB(count_init));
1345 }
1346 roundoff = count - count_init;
1347 ASSERT(roundoff >= 0);
1348 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1349 roundoff < log->l_mp->m_sb.sb_logsunit)
1350 ||
1351 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1352 roundoff < BBTOB(1)));
1353
1354 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001355 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001356 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001357 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* put cycle number in every block */
1360 xlog_pack_data(log, iclog, roundoff);
1361
1362 /* real byte length */
1363 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001364 iclog->ic_header.h_len =
1365 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001367 iclog->ic_header.h_len =
1368 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
Nathan Scottf5faad72006-07-28 17:04:44 +10001371 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1373 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001374 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1377
1378 /* Do we need to split this write into 2 parts? */
1379 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1380 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1381 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1382 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1383 } else {
1384 iclog->ic_bwritecnt = 1;
1385 }
David Chinner511105b2007-05-24 15:21:57 +10001386 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001388 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 XFS_BUF_BUSY(bp);
1390 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001391 bp->b_flags |= XBF_LOG_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001393 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001394 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001396 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001397 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1400 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1401
1402 xlog_verify_iclog(log, iclog, count, B_TRUE);
1403
1404 /* account for log which doesn't start at block #0 */
1405 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1406 /*
1407 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1408 * is shutting down.
1409 */
1410 XFS_BUF_WRITE(bp);
1411
Christoph Hellwig873ff552010-01-13 22:17:57 +00001412 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1414 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001415 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001418 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1420 (unsigned long)1);
1421 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1422 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1423 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1424 (__psint_t)count), split);
1425 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001426 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 XFS_BUF_BUSY(bp);
1428 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001429 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001430 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1431 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 dptr = XFS_BUF_PTR(bp);
1433 /*
1434 * Bump the cycle numbers at the start of each block
1435 * since this part of the buffer is at the start of
1436 * a new cycle. Watch out for the header magic number
1437 * case, though.
1438 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001439 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001440 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001441 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001442 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 dptr += BBSIZE;
1444 }
1445
1446 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1447 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1448
Nathan Scottc41564b2006-03-29 08:55:14 +10001449 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1451 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001452 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1454 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459} /* xlog_sync */
1460
1461
1462/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001463 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
David Chinnera8272ce2007-11-23 16:28:09 +11001465STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001466xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int i;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 iclog = log->l_iclog;
1472 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001473 sv_destroy(&iclog->ic_force_wait);
1474 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001477 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 iclog = next_iclog;
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 spinlock_destroy(&log->l_icloglock);
1481 spinlock_destroy(&log->l_grant_lock);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001485 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001486} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488/*
1489 * Update counters atomically now that memcpy is done.
1490 */
1491/* ARGSUSED */
1492static inline void
1493xlog_state_finish_copy(xlog_t *log,
1494 xlog_in_core_t *iclog,
1495 int record_cnt,
1496 int copy_bytes)
1497{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001498 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001500 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 iclog->ic_offset += copy_bytes;
1502
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001503 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504} /* xlog_state_finish_copy */
1505
1506
1507
1508
1509/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001510 * print out info relating to regions written which consume
1511 * the reservation
1512 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001513STATIC void
1514xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1515{
1516 uint i;
1517 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1518
1519 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1520 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1521 "bformat",
1522 "bchunk",
1523 "efi_format",
1524 "efd_format",
1525 "iformat",
1526 "icore",
1527 "iext",
1528 "ibroot",
1529 "ilocal",
1530 "iattr_ext",
1531 "iattr_broot",
1532 "iattr_local",
1533 "qformat",
1534 "dquot",
1535 "quotaoff",
1536 "LR header",
1537 "unmount",
1538 "commit",
1539 "trans header"
1540 };
1541 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1542 "SETATTR_NOT_SIZE",
1543 "SETATTR_SIZE",
1544 "INACTIVE",
1545 "CREATE",
1546 "CREATE_TRUNC",
1547 "TRUNCATE_FILE",
1548 "REMOVE",
1549 "LINK",
1550 "RENAME",
1551 "MKDIR",
1552 "RMDIR",
1553 "SYMLINK",
1554 "SET_DMATTRS",
1555 "GROWFS",
1556 "STRAT_WRITE",
1557 "DIOSTRAT",
1558 "WRITE_SYNC",
1559 "WRITEID",
1560 "ADDAFORK",
1561 "ATTRINVAL",
1562 "ATRUNCATE",
1563 "ATTR_SET",
1564 "ATTR_RM",
1565 "ATTR_FLAG",
1566 "CLEAR_AGI_BUCKET",
1567 "QM_SBCHANGE",
1568 "DUMMY1",
1569 "DUMMY2",
1570 "QM_QUOTAOFF",
1571 "QM_DQALLOC",
1572 "QM_SETQLIM",
1573 "QM_DQCLUSTER",
1574 "QM_QINOCREATE",
1575 "QM_QUOTAOFF_END",
1576 "SB_UNIT",
1577 "FSYNC_TS",
1578 "GROWFSRT_ALLOC",
1579 "GROWFSRT_ZERO",
1580 "GROWFSRT_FREE",
1581 "SWAPEXT"
1582 };
1583
1584 xfs_fs_cmn_err(CE_WARN, mp,
1585 "xfs_log_write: reservation summary:\n"
1586 " trans type = %s (%u)\n"
1587 " unit res = %d bytes\n"
1588 " current res = %d bytes\n"
1589 " total reg = %u bytes (o/flow = %u bytes)\n"
1590 " ophdrs = %u (ophdr space = %u bytes)\n"
1591 " ophdr + reg = %u bytes\n"
1592 " num regions = %u\n",
1593 ((ticket->t_trans_type <= 0 ||
1594 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1595 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1596 ticket->t_trans_type,
1597 ticket->t_unit_res,
1598 ticket->t_curr_res,
1599 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1600 ticket->t_res_num_ophdrs, ophdr_spc,
1601 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001602 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001603 ticket->t_res_num);
1604
1605 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001606 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001607 cmn_err(CE_WARN,
1608 "region[%u]: %s - %u bytes\n",
1609 i,
1610 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1611 "bad-rtype" : res_type_str[r_type-1]),
1612 ticket->t_res_arr[i].r_len);
1613 }
1614}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001615
1616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * Write some region out to in-core log
1618 *
1619 * This will be called when writing externally provided regions or when
1620 * writing out a commit record for a given transaction.
1621 *
1622 * General algorithm:
1623 * 1. Find total length of this write. This may include adding to the
1624 * lengths passed in.
1625 * 2. Check whether we violate the tickets reservation.
1626 * 3. While writing to this iclog
1627 * A. Reserve as much space in this iclog as can get
1628 * B. If this is first write, save away start lsn
1629 * C. While writing this region:
1630 * 1. If first write of transaction, write start record
1631 * 2. Write log operation header (header per region)
1632 * 3. Find out if we can fit entire region into this iclog
1633 * 4. Potentially, verify destination memcpy ptr
1634 * 5. Memcpy (partial) region
1635 * 6. If partial copy, release iclog; otherwise, continue
1636 * copying more regions into current iclog
1637 * 4. Mark want sync bit (in simulation mode)
1638 * 5. Release iclog for potential flush to on-disk log.
1639 *
1640 * ERRORS:
1641 * 1. Panic if reservation is overrun. This should never happen since
1642 * reservation amounts are generated internal to the filesystem.
1643 * NOTES:
1644 * 1. Tickets are single threaded data structures.
1645 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1646 * syncing routine. When a single log_write region needs to span
1647 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1648 * on all log operation writes which don't contain the end of the
1649 * region. The XLOG_END_TRANS bit is used for the in-core log
1650 * operation which contains the end of the continued log_write region.
1651 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1652 * we don't really know exactly how much space will be used. As a result,
1653 * we don't update ic_offset until the end when we know exactly how many
1654 * bytes have been written out.
1655 */
David Chinnera8272ce2007-11-23 16:28:09 +11001656STATIC int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001657xlog_write(
1658 struct xfs_mount *mp,
1659 struct xfs_log_iovec reg[],
1660 int nentries,
1661 struct xlog_ticket *ticket,
1662 xfs_lsn_t *start_lsn,
1663 struct xlog_in_core **commit_iclog,
1664 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001666 xlog_t *log = mp->m_log;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001667 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 __psint_t ptr; /* copy address into data region */
1670 int len; /* # xlog_write() bytes 2 still copy */
1671 int index; /* region index currently copying */
1672 int log_offset; /* offset (from 0) into data region */
1673 int start_rec_copy; /* # bytes to copy for start record */
1674 int partial_copy; /* did we split a region? */
1675 int partial_copy_len;/* # bytes copied if split region */
1676 int need_copy; /* # bytes need to memcpy this region */
1677 int copy_len; /* # bytes actually memcpy'ing */
1678 int copy_off; /* # bytes from entry start */
1679 int contwr; /* continued write of in-core log? */
1680 int error;
1681 int record_cnt = 0, data_cnt = 0;
1682
1683 partial_copy_len = partial_copy = 0;
1684
1685 /* Calculate potential maximum space. Each region gets its own
1686 * xlog_op_header_t and may need to be double word aligned.
1687 */
1688 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001689 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001691 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 for (index = 0; index < nentries; index++) {
1695 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001696 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001698 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 contwr = *start_lsn = 0;
1701
1702 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001703 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704#ifdef DEBUG
1705 xlog_panic(
1706 "xfs_log_write: reservation ran out. Need to up reservation");
1707#else
1708 /* Customer configurable panic */
1709 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1710 "xfs_log_write: reservation ran out. Need to up reservation");
1711 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001712 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713#endif
1714 } else
1715 ticket->t_curr_res -= len;
1716
1717 for (index = 0; index < nentries; ) {
1718 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1719 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001720 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 ASSERT(log_offset <= iclog->ic_size - 1);
1723 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1724
1725 /* start_lsn is the first lsn written to. That's all we need. */
1726 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001727 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 /* This loop writes out as many regions as can fit in the amount
1730 * of space which was allocated by xlog_state_get_iclog_space().
1731 */
1732 while (index < nentries) {
1733 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1734 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1735 start_rec_copy = 0;
1736
1737 /* If first write for transaction, insert start record.
1738 * We can't be trying to commit if we are inited. We can't
1739 * have any "partial_copy" if we are inited.
1740 */
1741 if (ticket->t_flags & XLOG_TIC_INITED) {
1742 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001743 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 logop_head->oh_clientid = ticket->t_clientid;
1745 logop_head->oh_len = 0;
1746 logop_head->oh_flags = XLOG_START_TRANS;
1747 logop_head->oh_res2 = 0;
1748 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1749 record_cnt++;
1750
1751 start_rec_copy = sizeof(xlog_op_header_t);
1752 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1753 }
1754
1755 /* Copy log operation header directly into data section */
1756 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001757 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 logop_head->oh_clientid = ticket->t_clientid;
1759 logop_head->oh_res2 = 0;
1760
1761 /* header copied directly */
1762 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1763
1764 /* are we copying a commit or unmount record? */
1765 logop_head->oh_flags = flags;
1766
1767 /*
1768 * We've seen logs corrupted with bad transaction client
1769 * ids. This makes sure that XFS doesn't generate them on.
1770 * Turn this into an EIO and shut down the filesystem.
1771 */
1772 switch (logop_head->oh_clientid) {
1773 case XFS_TRANSACTION:
1774 case XFS_VOLUME:
1775 case XFS_LOG:
1776 break;
1777 default:
1778 xfs_fs_cmn_err(CE_WARN, mp,
1779 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001780 logop_head->oh_clientid, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return XFS_ERROR(EIO);
1782 }
1783
1784 /* Partial write last time? => (partial_copy != 0)
1785 * need_copy is the amount we'd like to copy if everything could
1786 * fit in the current memcpy.
1787 */
1788 need_copy = reg[index].i_len - partial_copy_len;
1789
1790 copy_off = partial_copy_len;
1791 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001792 copy_len = need_copy;
1793 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (partial_copy)
1795 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1796 partial_copy_len = partial_copy = 0;
1797 } else { /* partial write */
1798 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001799 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1801 if (partial_copy)
1802 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1803 partial_copy_len += copy_len;
1804 partial_copy++;
1805 len += sizeof(xlog_op_header_t); /* from splitting of region */
1806 /* account for new log op header */
1807 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001808 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810 xlog_verify_dest_ptr(log, ptr);
1811
1812 /* copy region */
1813 ASSERT(copy_len >= 0);
1814 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1815 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1816
1817 /* make copy_len total bytes copied, including headers */
1818 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1819 record_cnt++;
1820 data_cnt += contwr ? copy_len : 0;
1821 if (partial_copy) { /* copied partial region */
1822 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1823 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1824 record_cnt = data_cnt = 0;
1825 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001826 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break; /* don't increment index */
1828 } else { /* copied entire region */
1829 index++;
1830 partial_copy_len = partial_copy = 0;
1831
1832 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1833 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1834 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001835 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001837 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (commit_iclog) {
1839 ASSERT(flags & XLOG_COMMIT_TRANS);
1840 *commit_iclog = iclog;
1841 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001842 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (index == nentries)
1844 return 0; /* we are done */
1845 else
1846 break;
1847 }
1848 } /* if (partial_copy) */
1849 } /* while (index < nentries) */
1850 } /* for (index = 0; index < nentries; ) */
1851 ASSERT(len == 0);
1852
1853 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1854 if (commit_iclog) {
1855 ASSERT(flags & XLOG_COMMIT_TRANS);
1856 *commit_iclog = iclog;
1857 return 0;
1858 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001859 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860} /* xlog_write */
1861
1862
1863/*****************************************************************************
1864 *
1865 * State Machine functions
1866 *
1867 *****************************************************************************
1868 */
1869
1870/* Clean iclogs starting from the head. This ordering must be
1871 * maintained, so an iclog doesn't become ACTIVE beyond one that
1872 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001873 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * iclog is trying to sync to disk.
1875 *
1876 * State Change: DIRTY -> ACTIVE
1877 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001878STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879xlog_state_clean_log(xlog_t *log)
1880{
1881 xlog_in_core_t *iclog;
1882 int changed = 0;
1883
1884 iclog = log->l_iclog;
1885 do {
1886 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1887 iclog->ic_state = XLOG_STATE_ACTIVE;
1888 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001889 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /*
1891 * If the number of ops in this iclog indicate it just
1892 * contains the dummy transaction, we can
1893 * change state into IDLE (the second time around).
1894 * Otherwise we should change the state into
1895 * NEED a dummy.
1896 * We don't need to cover the dummy.
1897 */
1898 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001899 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1900 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 changed = 1;
1902 } else {
1903 /*
1904 * We have two dirty iclogs so start over
1905 * This could also be num of ops indicates
1906 * this is not the dummy going out.
1907 */
1908 changed = 2;
1909 }
1910 iclog->ic_header.h_num_logops = 0;
1911 memset(iclog->ic_header.h_cycle_data, 0,
1912 sizeof(iclog->ic_header.h_cycle_data));
1913 iclog->ic_header.h_lsn = 0;
1914 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1915 /* do nothing */;
1916 else
1917 break; /* stop cleaning */
1918 iclog = iclog->ic_next;
1919 } while (iclog != log->l_iclog);
1920
1921 /* log is locked when we are called */
1922 /*
1923 * Change state for the dummy log recording.
1924 * We usually go to NEED. But we go to NEED2 if the changed indicates
1925 * we are done writing the dummy record.
1926 * If we are done with the second dummy recored (DONE2), then
1927 * we go to IDLE.
1928 */
1929 if (changed) {
1930 switch (log->l_covered_state) {
1931 case XLOG_STATE_COVER_IDLE:
1932 case XLOG_STATE_COVER_NEED:
1933 case XLOG_STATE_COVER_NEED2:
1934 log->l_covered_state = XLOG_STATE_COVER_NEED;
1935 break;
1936
1937 case XLOG_STATE_COVER_DONE:
1938 if (changed == 1)
1939 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1940 else
1941 log->l_covered_state = XLOG_STATE_COVER_NEED;
1942 break;
1943
1944 case XLOG_STATE_COVER_DONE2:
1945 if (changed == 1)
1946 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1947 else
1948 log->l_covered_state = XLOG_STATE_COVER_NEED;
1949 break;
1950
1951 default:
1952 ASSERT(0);
1953 }
1954 }
1955} /* xlog_state_clean_log */
1956
1957STATIC xfs_lsn_t
1958xlog_get_lowest_lsn(
1959 xlog_t *log)
1960{
1961 xlog_in_core_t *lsn_log;
1962 xfs_lsn_t lowest_lsn, lsn;
1963
1964 lsn_log = log->l_iclog;
1965 lowest_lsn = 0;
1966 do {
1967 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001968 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if ((lsn && !lowest_lsn) ||
1970 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
1971 lowest_lsn = lsn;
1972 }
1973 }
1974 lsn_log = lsn_log->ic_next;
1975 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01001976 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979
1980STATIC void
1981xlog_state_do_callback(
1982 xlog_t *log,
1983 int aborted,
1984 xlog_in_core_t *ciclog)
1985{
1986 xlog_in_core_t *iclog;
1987 xlog_in_core_t *first_iclog; /* used to know when we've
1988 * processed all iclogs once */
1989 xfs_log_callback_t *cb, *cb_next;
1990 int flushcnt = 0;
1991 xfs_lsn_t lowest_lsn;
1992 int ioerrors; /* counter: iclogs with errors */
1993 int loopdidcallbacks; /* flag: inner loop did callbacks*/
1994 int funcdidcallbacks; /* flag: function did callbacks */
1995 int repeats; /* for issuing console warnings if
1996 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001997 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001999 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 first_iclog = iclog = log->l_iclog;
2001 ioerrors = 0;
2002 funcdidcallbacks = 0;
2003 repeats = 0;
2004
2005 do {
2006 /*
2007 * Scan all iclogs starting with the one pointed to by the
2008 * log. Reset this starting point each time the log is
2009 * unlocked (during callbacks).
2010 *
2011 * Keep looping through iclogs until one full pass is made
2012 * without running any callbacks.
2013 */
2014 first_iclog = log->l_iclog;
2015 iclog = log->l_iclog;
2016 loopdidcallbacks = 0;
2017 repeats++;
2018
2019 do {
2020
2021 /* skip all iclogs in the ACTIVE & DIRTY states */
2022 if (iclog->ic_state &
2023 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2024 iclog = iclog->ic_next;
2025 continue;
2026 }
2027
2028 /*
2029 * Between marking a filesystem SHUTDOWN and stopping
2030 * the log, we do flush all iclogs to disk (if there
2031 * wasn't a log I/O error). So, we do want things to
2032 * go smoothly in case of just a SHUTDOWN w/o a
2033 * LOG_IO_ERROR.
2034 */
2035 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2036 /*
2037 * Can only perform callbacks in order. Since
2038 * this iclog is not in the DONE_SYNC/
2039 * DO_CALLBACK state, we skip the rest and
2040 * just try to clean up. If we set our iclog
2041 * to DO_CALLBACK, we will not process it when
2042 * we retry since a previous iclog is in the
2043 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002044 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 */
2046 if (!(iclog->ic_state &
2047 (XLOG_STATE_DONE_SYNC |
2048 XLOG_STATE_DO_CALLBACK))) {
2049 if (ciclog && (ciclog->ic_state ==
2050 XLOG_STATE_DONE_SYNC)) {
2051 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2052 }
2053 break;
2054 }
2055 /*
2056 * We now have an iclog that is in either the
2057 * DO_CALLBACK or DONE_SYNC states. The other
2058 * states (WANT_SYNC, SYNCING, or CALLBACK were
2059 * caught by the above if and are going to
2060 * clean (i.e. we aren't doing their callbacks)
2061 * see the above if.
2062 */
2063
2064 /*
2065 * We will do one more check here to see if we
2066 * have chased our tail around.
2067 */
2068
2069 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002070 if (lowest_lsn &&
2071 XFS_LSN_CMP(lowest_lsn,
2072 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 iclog = iclog->ic_next;
2074 continue; /* Leave this iclog for
2075 * another thread */
2076 }
2077
2078 iclog->ic_state = XLOG_STATE_CALLBACK;
2079
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002080 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002083 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 * No one else can be here except us.
2085 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002086 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002087 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2088 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2089 log->l_last_sync_lsn =
2090 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002091 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002094 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 ioerrors++;
2096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
David Chinner114d23a2008-04-10 12:18:39 +10002098 /*
2099 * Keep processing entries in the callback list until
2100 * we come around and it is empty. We need to
2101 * atomically see that the list is empty and change the
2102 * state to DIRTY so that we don't miss any more
2103 * callbacks being added.
2104 */
2105 spin_lock(&iclog->ic_callback_lock);
2106 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002107 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 iclog->ic_callback_tail = &(iclog->ic_callback);
2109 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002110 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002113 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 cb_next = cb->cb_next;
2115 cb->cb_func(cb->cb_arg, aborted);
2116 }
David Chinner114d23a2008-04-10 12:18:39 +10002117 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 cb = iclog->ic_callback;
2119 }
2120
2121 loopdidcallbacks++;
2122 funcdidcallbacks++;
2123
David Chinner114d23a2008-04-10 12:18:39 +10002124 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002125 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002126 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2128 iclog->ic_state = XLOG_STATE_DIRTY;
2129
2130 /*
2131 * Transition from DIRTY to ACTIVE if applicable.
2132 * NOP if STATE_IOERROR.
2133 */
2134 xlog_state_clean_log(log);
2135
2136 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002137 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 iclog = iclog->ic_next;
2140 } while (first_iclog != iclog);
Nathan Scotta3c66852006-09-28 11:02:14 +10002141
2142 if (repeats > 5000) {
2143 flushcnt += repeats;
2144 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c66852006-09-28 11:02:14 +10002146 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002147 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149 } while (!ioerrors && loopdidcallbacks);
2150
2151 /*
2152 * make one last gasp attempt to see if iclogs are being left in
2153 * limbo..
2154 */
2155#ifdef DEBUG
2156 if (funcdidcallbacks) {
2157 first_iclog = iclog = log->l_iclog;
2158 do {
2159 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2160 /*
2161 * Terminate the loop if iclogs are found in states
2162 * which will cause other threads to clean up iclogs.
2163 *
2164 * SYNCING - i/o completion will go through logs
2165 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002166 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 * IOERROR - give up hope all ye who enter here
2168 */
2169 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2170 iclog->ic_state == XLOG_STATE_SYNCING ||
2171 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2172 iclog->ic_state == XLOG_STATE_IOERROR )
2173 break;
2174 iclog = iclog->ic_next;
2175 } while (first_iclog != iclog);
2176 }
2177#endif
2178
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002179 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2180 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002181 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002182
2183 if (wake)
2184 sv_broadcast(&log->l_flush_wait);
2185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187
2188/*
2189 * Finish transitioning this iclog to the dirty state.
2190 *
2191 * Make sure that we completely execute this routine only when this is
2192 * the last call to the iclog. There is a good chance that iclog flushes,
2193 * when we reach the end of the physical log, get turned into 2 separate
2194 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2195 * routine. By using the reference count bwritecnt, we guarantee that only
2196 * the second completion goes through.
2197 *
2198 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002199 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
David Chinnera8272ce2007-11-23 16:28:09 +11002201STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202xlog_state_done_syncing(
2203 xlog_in_core_t *iclog,
2204 int aborted)
2205{
2206 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002208 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2211 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002212 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2214
2215
2216 /*
2217 * If we got an error, either on the first buffer, or in the case of
2218 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2219 * and none should ever be attempted to be written to disk
2220 * again.
2221 */
2222 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2223 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002224 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return;
2226 }
2227 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2228 }
2229
2230 /*
2231 * Someone could be sleeping prior to writing out the next
2232 * iclog buffer, we wake them all, one will get to do the
2233 * I/O, the others get to wait for the result.
2234 */
David Chinner12017fa2008-08-13 16:34:31 +10002235 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002236 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2238} /* xlog_state_done_syncing */
2239
2240
2241/*
2242 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002243 * sleep. We wait on the flush queue on the head iclog as that should be
2244 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2245 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 *
2247 * The in-core logs are used in a circular fashion. They are not used
2248 * out-of-order even when an iclog past the head is free.
2249 *
2250 * return:
2251 * * log_offset where xlog_write() can start writing into the in-core
2252 * log's data space.
2253 * * in-core log pointer to which xlog_write() should write.
2254 * * boolean indicating this is a continued write to an in-core log.
2255 * If this is the last write, then the in-core log's offset field
2256 * needs to be incremented, depending on the amount of data which
2257 * is copied.
2258 */
David Chinnera8272ce2007-11-23 16:28:09 +11002259STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260xlog_state_get_iclog_space(xlog_t *log,
2261 int len,
2262 xlog_in_core_t **iclogp,
2263 xlog_ticket_t *ticket,
2264 int *continued_write,
2265 int *logoffsetp)
2266{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 int log_offset;
2268 xlog_rec_header_t *head;
2269 xlog_in_core_t *iclog;
2270 int error;
2271
2272restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002273 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002275 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return XFS_ERROR(EIO);
2277 }
2278
2279 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002280 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002282
2283 /* Wait for log writes to have flushed */
2284 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto restart;
2286 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 head = &iclog->ic_header;
2289
David Chinner155cc6b2008-03-06 13:44:14 +11002290 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 log_offset = iclog->ic_offset;
2292
2293 /* On the 1st write to an iclog, figure out lsn. This works
2294 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2295 * committing to. If the offset is set, that's how many blocks
2296 * must be written.
2297 */
2298 if (log_offset == 0) {
2299 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002300 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002301 log->l_iclog_hsize,
2302 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002303 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2304 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002305 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 ASSERT(log->l_curr_block >= 0);
2307 }
2308
2309 /* If there is enough room to write everything, then do it. Otherwise,
2310 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2311 * bit is on, so this will get flushed out. Don't update ic_offset
2312 * until you know exactly how many bytes get copied. Therefore, wait
2313 * until later to update ic_offset.
2314 *
2315 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2316 * can fit into remaining data section.
2317 */
2318 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2319 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2320
Dave Chinner49641f12008-07-11 17:43:55 +10002321 /*
2322 * If I'm the only one writing to this iclog, sync it to disk.
2323 * We need to do an atomic compare and decrement here to avoid
2324 * racing with concurrent atomic_dec_and_lock() calls in
2325 * xlog_state_release_iclog() when there is more than one
2326 * reference to the iclog.
2327 */
2328 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2329 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002330 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002331 error = xlog_state_release_iclog(log, iclog);
2332 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002333 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002335 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337 goto restart;
2338 }
2339
2340 /* Do we have enough room to write the full amount in the remainder
2341 * of this iclog? Or must we continue a write on the next iclog and
2342 * mark this iclog as completely taken? In the case where we switch
2343 * iclogs (to mark it taken), this particular iclog will release/sync
2344 * to disk in xlog_write().
2345 */
2346 if (len <= iclog->ic_size - iclog->ic_offset) {
2347 *continued_write = 0;
2348 iclog->ic_offset += len;
2349 } else {
2350 *continued_write = 1;
2351 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2352 }
2353 *iclogp = iclog;
2354
2355 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002356 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 *logoffsetp = log_offset;
2359 return 0;
2360} /* xlog_state_get_iclog_space */
2361
2362/*
2363 * Atomically get the log space required for a log ticket.
2364 *
2365 * Once a ticket gets put onto the reserveq, it will only return after
2366 * the needed reservation is satisfied.
2367 */
2368STATIC int
2369xlog_grant_log_space(xlog_t *log,
2370 xlog_ticket_t *tic)
2371{
2372 int free_bytes;
2373 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374#ifdef DEBUG
2375 xfs_lsn_t tail_lsn;
2376#endif
2377
2378
2379#ifdef DEBUG
2380 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2381 panic("grant Recovery problem");
2382#endif
2383
2384 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002385 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002386
2387 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /* something is already sleeping; insert new transaction at end */
2390 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002391 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002392
2393 trace_xfs_log_grant_sleep1(log, tic);
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /*
2396 * Gotta check this before going to sleep, while we're
2397 * holding the grant lock.
2398 */
2399 if (XLOG_FORCED_SHUTDOWN(log))
2400 goto error_return;
2401
2402 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002403 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /*
2405 * If we got an error, and the filesystem is shutting down,
2406 * we'll catch it down below. So just continue...
2407 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002408 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002409 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2412 need_bytes = tic->t_unit_res*tic->t_ocnt;
2413 else
2414 need_bytes = tic->t_unit_res;
2415
2416redo:
2417 if (XLOG_FORCED_SHUTDOWN(log))
2418 goto error_return;
2419
2420 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2421 log->l_grant_reserve_bytes);
2422 if (free_bytes < need_bytes) {
2423 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002424 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002425
2426 trace_xfs_log_grant_sleep2(log, tic);
2427
Dave Chinner9d7fef72009-04-06 18:42:59 +02002428 spin_unlock(&log->l_grant_lock);
2429 xlog_grant_push_ail(log->l_mp, need_bytes);
2430 spin_lock(&log->l_grant_lock);
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002433 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Dave Chinner9d7fef72009-04-06 18:42:59 +02002435 spin_lock(&log->l_grant_lock);
2436 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002439 trace_xfs_log_grant_wake2(log, tic);
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto redo;
2442 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002443 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002446 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447#ifdef DEBUG
2448 tail_lsn = log->l_tail_lsn;
2449 /*
2450 * Check to make sure the grant write head didn't just over lap the
2451 * tail. If the cycles are the same, we can't be overlapping.
2452 * Otherwise, make sure that the cycles differ by exactly one and
2453 * check the byte count.
2454 */
2455 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2456 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2457 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2458 }
2459#endif
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002460 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002462 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return 0;
2464
2465 error_return:
2466 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002467 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002468
2469 trace_xfs_log_grant_error(log, tic);
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /*
2472 * If we are failing, make sure the ticket doesn't have any
2473 * current reservations. We don't want to add this back when
2474 * the ticket/transaction gets cancelled.
2475 */
2476 tic->t_curr_res = 0;
2477 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002478 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return XFS_ERROR(EIO);
2480} /* xlog_grant_log_space */
2481
2482
2483/*
2484 * Replenish the byte reservation required by moving the grant write head.
2485 *
2486 *
2487 */
2488STATIC int
2489xlog_regrant_write_log_space(xlog_t *log,
2490 xlog_ticket_t *tic)
2491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 int free_bytes, need_bytes;
2493 xlog_ticket_t *ntic;
2494#ifdef DEBUG
2495 xfs_lsn_t tail_lsn;
2496#endif
2497
2498 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002499 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504#ifdef DEBUG
2505 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2506 panic("regrant Recovery problem");
2507#endif
2508
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002509 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002510
2511 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 if (XLOG_FORCED_SHUTDOWN(log))
2514 goto error_return;
2515
2516 /* If there are other waiters on the queue then give them a
2517 * chance at logspace before us. Wake up the first waiters,
2518 * if we do not wake up all the waiters then go to sleep waiting
2519 * for more free space, otherwise try to get some space for
2520 * this transaction.
2521 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002522 need_bytes = tic->t_unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if ((ntic = log->l_write_headq)) {
2524 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2525 log->l_grant_write_bytes);
2526 do {
2527 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2528
2529 if (free_bytes < ntic->t_unit_res)
2530 break;
2531 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002532 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ntic = ntic->t_next;
2534 } while (ntic != log->l_write_headq);
2535
2536 if (ntic != log->l_write_headq) {
2537 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002538 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002540 trace_xfs_log_regrant_write_sleep1(log, tic);
2541
Dave Chinner9d7fef72009-04-06 18:42:59 +02002542 spin_unlock(&log->l_grant_lock);
2543 xlog_grant_push_ail(log->l_mp, need_bytes);
2544 spin_lock(&log->l_grant_lock);
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002547 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 &log->l_grant_lock, s);
2549
2550 /* If we're shutting down, this tic is already
2551 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002552 spin_lock(&log->l_grant_lock);
2553 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002556 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558 }
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560redo:
2561 if (XLOG_FORCED_SHUTDOWN(log))
2562 goto error_return;
2563
2564 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2565 log->l_grant_write_bytes);
2566 if (free_bytes < need_bytes) {
2567 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002568 xlog_ins_ticketq(&log->l_write_headq, tic);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002569 spin_unlock(&log->l_grant_lock);
2570 xlog_grant_push_ail(log->l_mp, need_bytes);
2571 spin_lock(&log->l_grant_lock);
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002574 trace_xfs_log_regrant_write_sleep2(log, tic);
2575
David Chinner12017fa2008-08-13 16:34:31 +10002576 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002579 spin_lock(&log->l_grant_lock);
2580 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002583 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 goto redo;
2585 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002586 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002588 /* we've got enough space */
2589 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590#ifdef DEBUG
2591 tail_lsn = log->l_tail_lsn;
2592 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2593 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2594 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2595 }
2596#endif
2597
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002598 trace_xfs_log_regrant_write_exit(log, tic);
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002601 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604
2605 error_return:
2606 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002607 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002608
2609 trace_xfs_log_regrant_write_error(log, tic);
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /*
2612 * If we are failing, make sure the ticket doesn't have any
2613 * current reservations. We don't want to add this back when
2614 * the ticket/transaction gets cancelled.
2615 */
2616 tic->t_curr_res = 0;
2617 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002618 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return XFS_ERROR(EIO);
2620} /* xlog_regrant_write_log_space */
2621
2622
2623/* The first cnt-1 times through here we don't need to
2624 * move the grant write head because the permanent
2625 * reservation has reserved cnt times the unit amount.
2626 * Release part of current permanent unit reservation and
2627 * reset current reservation to be one units worth. Also
2628 * move grant reservation head forward.
2629 */
2630STATIC void
2631xlog_regrant_reserve_log_space(xlog_t *log,
2632 xlog_ticket_t *ticket)
2633{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002634 trace_xfs_log_regrant_reserve_enter(log, ticket);
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (ticket->t_cnt > 0)
2637 ticket->t_cnt--;
2638
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002639 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002640 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002642 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002643
2644 trace_xfs_log_regrant_reserve_sub(log, ticket);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 xlog_verify_grant_head(log, 1);
2647
2648 /* just return if we still have some of the pre-reserved space */
2649 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002650 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return;
2652 }
2653
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002654 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002655
2656 trace_xfs_log_regrant_reserve_exit(log, ticket);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002659 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002661 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662} /* xlog_regrant_reserve_log_space */
2663
2664
2665/*
2666 * Give back the space left from a reservation.
2667 *
2668 * All the information we need to make a correct determination of space left
2669 * is present. For non-permanent reservations, things are quite easy. The
2670 * count should have been decremented to zero. We only need to deal with the
2671 * space remaining in the current reservation part of the ticket. If the
2672 * ticket contains a permanent reservation, there may be left over space which
2673 * needs to be released. A count of N means that N-1 refills of the current
2674 * reservation can be done before we need to ask for more space. The first
2675 * one goes to fill up the first current reservation. Once we run out of
2676 * space, the count will stay at zero and the only space remaining will be
2677 * in the current reservation field.
2678 */
2679STATIC void
2680xlog_ungrant_log_space(xlog_t *log,
2681 xlog_ticket_t *ticket)
2682{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (ticket->t_cnt > 0)
2684 ticket->t_cnt--;
2685
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002686 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002687 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002689 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002691 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693 /* If this is a permanent reservation ticket, we may be able to free
2694 * up more space based on the remaining count.
2695 */
2696 if (ticket->t_cnt > 0) {
2697 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002698 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
2700
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002701 trace_xfs_log_ungrant_exit(log, ticket);
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002704 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 xfs_log_move_tail(log->l_mp, 1);
2706} /* xlog_ungrant_log_space */
2707
2708
2709/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 * Flush iclog to disk if this is the last reference to the given iclog and
2711 * the WANT_SYNC bit is set.
2712 *
2713 * When this function is entered, the iclog is not necessarily in the
2714 * WANT_SYNC state. It may be sitting around waiting to get filled.
2715 *
2716 *
2717 */
David Chinnera8272ce2007-11-23 16:28:09 +11002718STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002719xlog_state_release_iclog(
2720 xlog_t *log,
2721 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 int sync = 0; /* do we sync? */
2724
David Chinner155cc6b2008-03-06 13:44:14 +11002725 if (iclog->ic_state & XLOG_STATE_IOERROR)
2726 return XFS_ERROR(EIO);
2727
2728 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2729 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2730 return 0;
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002733 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return XFS_ERROR(EIO);
2735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2737 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2738
David Chinner155cc6b2008-03-06 13:44:14 +11002739 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002740 /* update tail before writing to iclog */
2741 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 sync++;
2743 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002744 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2746 /* cycle incremented when incrementing curr_block */
2747 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002748 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 /*
2751 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002752 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2754 * this iclog has consistent data, so we ignore IOERROR
2755 * flags after this point.
2756 */
David Chinnerb5893342008-03-06 13:44:06 +11002757 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760} /* xlog_state_release_iclog */
2761
2762
2763/*
2764 * This routine will mark the current iclog in the ring as WANT_SYNC
2765 * and move the current iclog pointer to the next iclog in the ring.
2766 * When this routine is called from xlog_state_get_iclog_space(), the
2767 * exact size of the iclog has not yet been determined. All we know is
2768 * that every data block. We have run out of space in this log record.
2769 */
2770STATIC void
2771xlog_state_switch_iclogs(xlog_t *log,
2772 xlog_in_core_t *iclog,
2773 int eventual_size)
2774{
2775 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2776 if (!eventual_size)
2777 eventual_size = iclog->ic_offset;
2778 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002779 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 log->l_prev_block = log->l_curr_block;
2781 log->l_prev_cycle = log->l_curr_cycle;
2782
2783 /* roll log?: ic_offset changed later */
2784 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2785
2786 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002787 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 log->l_mp->m_sb.sb_logsunit > 1) {
2789 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2790 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2791 }
2792
2793 if (log->l_curr_block >= log->l_logBBsize) {
2794 log->l_curr_cycle++;
2795 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2796 log->l_curr_cycle++;
2797 log->l_curr_block -= log->l_logBBsize;
2798 ASSERT(log->l_curr_block >= 0);
2799 }
2800 ASSERT(iclog == log->l_iclog);
2801 log->l_iclog = iclog->ic_next;
2802} /* xlog_state_switch_iclogs */
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804/*
2805 * Write out all data in the in-core log as of this exact moment in time.
2806 *
2807 * Data may be written to the in-core log during this call. However,
2808 * we don't guarantee this data will be written out. A change from past
2809 * implementation means this routine will *not* write out zero length LRs.
2810 *
2811 * Basically, we try and perform an intelligent scan of the in-core logs.
2812 * If we determine there is no flushable data, we just return. There is no
2813 * flushable data if:
2814 *
2815 * 1. the current iclog is active and has no data; the previous iclog
2816 * is in the active or dirty state.
2817 * 2. the current iclog is drity, and the previous iclog is in the
2818 * active or dirty state.
2819 *
David Chinner12017fa2008-08-13 16:34:31 +10002820 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 *
2822 * 1. the current iclog is not in the active nor dirty state.
2823 * 2. the current iclog dirty, and the previous iclog is not in the
2824 * active nor dirty state.
2825 * 3. the current iclog is active, and there is another thread writing
2826 * to this particular iclog.
2827 * 4. a) the current iclog is active and has no other writers
2828 * b) when we return from flushing out this iclog, it is still
2829 * not in the active nor dirty state.
2830 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002831int
2832_xfs_log_force(
2833 struct xfs_mount *mp,
2834 uint flags,
2835 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002837 struct log *log = mp->m_log;
2838 struct xlog_in_core *iclog;
2839 xfs_lsn_t lsn;
2840
2841 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002843 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 iclog = log->l_iclog;
2846 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002847 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return XFS_ERROR(EIO);
2849 }
2850
2851 /* If the head iclog is not active nor dirty, we just attach
2852 * ourselves to the head and go to sleep.
2853 */
2854 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2855 iclog->ic_state == XLOG_STATE_DIRTY) {
2856 /*
2857 * If the head is dirty or (active and empty), then
2858 * we need to look at the previous iclog. If the previous
2859 * iclog is active or dirty we are done. There is nothing
2860 * to sync out. Otherwise, we attach ourselves to the
2861 * previous iclog and go to sleep.
2862 */
2863 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002864 (atomic_read(&iclog->ic_refcnt) == 0
2865 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 iclog = iclog->ic_prev;
2867 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2868 iclog->ic_state == XLOG_STATE_DIRTY)
2869 goto no_sleep;
2870 else
2871 goto maybe_sleep;
2872 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002873 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /* We are the only one with access to this
2875 * iclog. Flush it out now. There should
2876 * be a roundoff of zero to show that someone
2877 * has already taken care of the roundoff from
2878 * the previous sync.
2879 */
David Chinner155cc6b2008-03-06 13:44:14 +11002880 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002881 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002883 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 if (xlog_state_release_iclog(log, iclog))
2886 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002887
2888 if (log_flushed)
2889 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002890 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002891 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 iclog->ic_state != XLOG_STATE_DIRTY)
2893 goto maybe_sleep;
2894 else
2895 goto no_sleep;
2896 } else {
2897 /* Someone else is writing to this iclog.
2898 * Use its call to flush out the data. However,
2899 * the other thread may not force out this LR,
2900 * so we mark it WANT_SYNC.
2901 */
2902 xlog_state_switch_iclogs(log, iclog, 0);
2903 goto maybe_sleep;
2904 }
2905 }
2906 }
2907
2908 /* By the time we come around again, the iclog could've been filled
2909 * which would give it another lsn. If we have a new lsn, just
2910 * return because the relevant data has been flushed.
2911 */
2912maybe_sleep:
2913 if (flags & XFS_LOG_SYNC) {
2914 /*
2915 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002916 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 * Then we check again after waking up, in case our
2918 * sleep was disturbed by a bad news.
2919 */
2920 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002921 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return XFS_ERROR(EIO);
2923 }
2924 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10002925 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 /*
2927 * No need to grab the log lock here since we're
2928 * only deciding whether or not to return EIO
2929 * and the memory read should be atomic.
2930 */
2931 if (iclog->ic_state & XLOG_STATE_IOERROR)
2932 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002933 if (log_flushed)
2934 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 } else {
2936
2937no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002938 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002944 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2945 * about errors or whether the log was flushed or not. This is the normal
2946 * interface to use when trying to unpin items or move the log forward.
2947 */
2948void
2949xfs_log_force(
2950 xfs_mount_t *mp,
2951 uint flags)
2952{
2953 int error;
2954
2955 error = _xfs_log_force(mp, flags, NULL);
2956 if (error) {
2957 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
2958 "error %d returned.", error);
2959 }
2960}
2961
2962/*
2963 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 *
2965 * Find in-core log with lsn.
2966 * If it is in the DIRTY state, just return.
2967 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2968 * state and go to sleep or return.
2969 * If it is in any other state, go to sleep or return.
2970 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002971 * Synchronous forces are implemented with a signal variable. All callers
2972 * to force a given lsn to disk will wait on a the sv attached to the
2973 * specific in-core log. When given in-core log finally completes its
2974 * write to disk, that thread will wake up all threads waiting on the
2975 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002977int
2978_xfs_log_force_lsn(
2979 struct xfs_mount *mp,
2980 xfs_lsn_t lsn,
2981 uint flags,
2982 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002984 struct log *log = mp->m_log;
2985 struct xlog_in_core *iclog;
2986 int already_slept = 0;
2987
2988 ASSERT(lsn != 0);
2989
2990 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002993 spin_lock(&log->l_icloglock);
2994 iclog = log->l_iclog;
2995 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002996 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002997 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
2999
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003000 do {
3001 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3002 iclog = iclog->ic_next;
3003 continue;
3004 }
3005
3006 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3007 spin_unlock(&log->l_icloglock);
3008 return 0;
3009 }
3010
3011 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3012 /*
3013 * We sleep here if we haven't already slept (e.g.
3014 * this is the first time we've looked at the correct
3015 * iclog buf) and the buffer before us is going to
3016 * be sync'ed. The reason for this is that if we
3017 * are doing sync transactions here, by waiting for
3018 * the previous I/O to complete, we can allow a few
3019 * more transactions into this iclog before we close
3020 * it down.
3021 *
3022 * Otherwise, we mark the buffer WANT_SYNC, and bump
3023 * up the refcnt so we can release the log (which
3024 * drops the ref count). The state switch keeps new
3025 * transaction commits from using this buffer. When
3026 * the current commits finish writing into the buffer,
3027 * the refcount will drop to zero and the buffer will
3028 * go out then.
3029 */
3030 if (!already_slept &&
3031 (iclog->ic_prev->ic_state &
3032 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3033 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3034
3035 XFS_STATS_INC(xs_log_force_sleep);
3036
3037 sv_wait(&iclog->ic_prev->ic_write_wait,
3038 PSWP, &log->l_icloglock, s);
3039 if (log_flushed)
3040 *log_flushed = 1;
3041 already_slept = 1;
3042 goto try_again;
3043 }
David Chinner155cc6b2008-03-06 13:44:14 +11003044 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003046 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (xlog_state_release_iclog(log, iclog))
3048 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003049 if (log_flushed)
3050 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003051 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003054 if ((flags & XFS_LOG_SYNC) && /* sleep */
3055 !(iclog->ic_state &
3056 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3057 /*
3058 * Don't wait on completion if we know that we've
3059 * gotten a log write error.
3060 */
3061 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3062 spin_unlock(&log->l_icloglock);
3063 return XFS_ERROR(EIO);
3064 }
3065 XFS_STATS_INC(xs_log_force_sleep);
3066 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3067 /*
3068 * No need to grab the log lock here since we're
3069 * only deciding whether or not to return EIO
3070 * and the memory read should be atomic.
3071 */
3072 if (iclog->ic_state & XLOG_STATE_IOERROR)
3073 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003075 if (log_flushed)
3076 *log_flushed = 1;
3077 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003078 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003080
3081 return 0;
3082 } while (iclog != log->l_iclog);
3083
3084 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003088/*
3089 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3090 * about errors or whether the log was flushed or not. This is the normal
3091 * interface to use when trying to unpin items or move the log forward.
3092 */
3093void
3094xfs_log_force_lsn(
3095 xfs_mount_t *mp,
3096 xfs_lsn_t lsn,
3097 uint flags)
3098{
3099 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003101 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3102 if (error) {
3103 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3104 "error %d returned.", error);
3105 }
3106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108/*
3109 * Called when we want to mark the current iclog as being ready to sync to
3110 * disk.
3111 */
David Chinnera8272ce2007-11-23 16:28:09 +11003112STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3114{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003115 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3118 xlog_state_switch_iclogs(log, iclog, 0);
3119 } else {
3120 ASSERT(iclog->ic_state &
3121 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3122 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125
3126/*****************************************************************************
3127 *
3128 * TICKET functions
3129 *
3130 *****************************************************************************
3131 */
3132
3133/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003134 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003136void
3137xfs_log_ticket_put(
3138 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003140 ASSERT(atomic_read(&ticket->t_ref) > 0);
3141 if (atomic_dec_and_test(&ticket->t_ref)) {
3142 sv_destroy(&ticket->t_wait);
3143 kmem_zone_free(xfs_log_ticket_zone, ticket);
3144 }
3145}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003147xlog_ticket_t *
3148xfs_log_ticket_get(
3149 xlog_ticket_t *ticket)
3150{
3151 ASSERT(atomic_read(&ticket->t_ref) > 0);
3152 atomic_inc(&ticket->t_ref);
3153 return ticket;
3154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003157 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 */
David Chinnera8272ce2007-11-23 16:28:09 +11003159STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003160xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 int unit_bytes,
3162 int cnt,
3163 char client,
3164 uint xflags)
3165{
3166 xlog_ticket_t *tic;
3167 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
David Chinnereb01c9c2008-04-10 12:18:46 +10003169 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3170 if (!tic)
3171 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 /*
3174 * Permanent reservations have up to 'cnt'-1 active log operations
3175 * in the log. A unit in this case is the amount of space for one
3176 * of these log operations. Normal reservations have a cnt of 1
3177 * and their unit amount is the total amount of space required.
3178 *
3179 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003180 * which occupy space in the on-disk log.
3181 *
3182 * Normal form of a transaction is:
3183 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3184 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3185 *
3186 * We need to account for all the leadup data and trailer data
3187 * around the transaction data.
3188 * And then we need to account for the worst case in terms of using
3189 * more space.
3190 * The worst case will happen if:
3191 * - the placement of the transaction happens to be such that the
3192 * roundoff is at its maximum
3193 * - the transaction data is synced before the commit record is synced
3194 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3195 * Therefore the commit record is in its own Log Record.
3196 * This can happen as the commit record is called with its
3197 * own region to xlog_write().
3198 * This then means that in the worst case, roundoff can happen for
3199 * the commit-rec as well.
3200 * The commit-rec is smaller than padding in this scenario and so it is
3201 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 */
3203
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003204 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003206 unit_bytes += sizeof(xfs_trans_header_t);
3207
3208 /* for start-rec */
3209 unit_bytes += sizeof(xlog_op_header_t);
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 /* for LR headers */
3212 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3213 unit_bytes += log->l_iclog_hsize * num_headers;
3214
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003215 /* for commit-rec LR header - note: padding will subsume the ophdr */
3216 unit_bytes += log->l_iclog_hsize;
3217
3218 /* for split-recs - ophdrs added when data split over LRs */
3219 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3220
3221 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003222 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003223 log->l_mp->m_sb.sb_logsunit > 1) {
3224 /* log su roundoff */
3225 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3226 } else {
3227 /* BB roundoff */
3228 unit_bytes += 2*BBSIZE;
3229 }
3230
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003231 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 tic->t_unit_res = unit_bytes;
3233 tic->t_curr_res = unit_bytes;
3234 tic->t_cnt = cnt;
3235 tic->t_ocnt = cnt;
3236 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3237 tic->t_clientid = client;
3238 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003239 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (xflags & XFS_LOG_PERM_RESERV)
3241 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003242 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Christoph Hellwig0adba532007-08-30 17:21:46 +10003244 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249
3250/******************************************************************************
3251 *
3252 * Log debug routines
3253 *
3254 ******************************************************************************
3255 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003256#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257/*
3258 * Make sure that the destination ptr is within the valid data region of
3259 * one of the iclogs. This uses backup pointers stored in a different
3260 * part of the log in case we trash the log structure.
3261 */
3262void
3263xlog_verify_dest_ptr(xlog_t *log,
3264 __psint_t ptr)
3265{
3266 int i;
3267 int good_ptr = 0;
3268
3269 for (i=0; i < log->l_iclog_bufs; i++) {
3270 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3271 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3272 good_ptr++;
3273 }
3274 if (! good_ptr)
3275 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3276} /* xlog_verify_dest_ptr */
3277
3278STATIC void
3279xlog_verify_grant_head(xlog_t *log, int equals)
3280{
3281 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3282 if (equals)
3283 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3284 else
3285 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3286 } else {
3287 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3288 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3289 }
3290} /* xlog_verify_grant_head */
3291
3292/* check if it will fit */
3293STATIC void
3294xlog_verify_tail_lsn(xlog_t *log,
3295 xlog_in_core_t *iclog,
3296 xfs_lsn_t tail_lsn)
3297{
3298 int blocks;
3299
3300 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3301 blocks =
3302 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3303 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3304 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3305 } else {
3306 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3307
3308 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3309 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3310
3311 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3312 if (blocks < BTOBB(iclog->ic_offset) + 1)
3313 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3314 }
3315} /* xlog_verify_tail_lsn */
3316
3317/*
3318 * Perform a number of checks on the iclog before writing to disk.
3319 *
3320 * 1. Make sure the iclogs are still circular
3321 * 2. Make sure we have a good magic number
3322 * 3. Make sure we don't have magic numbers in the data
3323 * 4. Check fields of each log operation header for:
3324 * A. Valid client identifier
3325 * B. tid ptr value falls in valid ptr space (user space code)
3326 * C. Length in log record header is correct according to the
3327 * individual operation headers within record.
3328 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3329 * log, check the preceding blocks of the physical log to make sure all
3330 * the cycle numbers agree with the current cycle number.
3331 */
3332STATIC void
3333xlog_verify_iclog(xlog_t *log,
3334 xlog_in_core_t *iclog,
3335 int count,
3336 boolean_t syncing)
3337{
3338 xlog_op_header_t *ophead;
3339 xlog_in_core_t *icptr;
3340 xlog_in_core_2_t *xhdr;
3341 xfs_caddr_t ptr;
3342 xfs_caddr_t base_ptr;
3343 __psint_t field_offset;
3344 __uint8_t clientid;
3345 int len, i, j, k, op_len;
3346 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003349 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 icptr = log->l_iclog;
3351 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003352 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 xlog_panic("xlog_verify_iclog: invalid ptr");
3354 icptr = icptr->ic_next;
3355 }
3356 if (icptr != log->l_iclog)
3357 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003358 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003361 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 xlog_panic("xlog_verify_iclog: invalid magic num");
3363
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003364 ptr = (xfs_caddr_t) &iclog->ic_header;
3365 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003367 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 xlog_panic("xlog_verify_iclog: unexpected magic num");
3369 }
3370
3371 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003372 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 ptr = iclog->ic_datap;
3374 base_ptr = ptr;
3375 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003376 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 for (i = 0; i < len; i++) {
3378 ophead = (xlog_op_header_t *)ptr;
3379
3380 /* clientid is only 1 byte */
3381 field_offset = (__psint_t)
3382 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3383 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3384 clientid = ophead->oh_clientid;
3385 } else {
3386 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3387 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3388 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3389 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003390 clientid = xlog_get_client_id(
3391 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003393 clientid = xlog_get_client_id(
3394 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396 }
3397 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003398 cmn_err(CE_WARN, "xlog_verify_iclog: "
3399 "invalid clientid %d op 0x%p offset 0x%lx",
3400 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
3402 /* check length */
3403 field_offset = (__psint_t)
3404 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3405 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003406 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 } else {
3408 idx = BTOBBT((__psint_t)&ophead->oh_len -
3409 (__psint_t)iclog->ic_datap);
3410 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3411 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3412 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003413 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003415 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
3417 }
3418 ptr += sizeof(xlog_op_header_t) + op_len;
3419 }
3420} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003424 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 */
3426STATIC int
3427xlog_state_ioerror(
3428 xlog_t *log)
3429{
3430 xlog_in_core_t *iclog, *ic;
3431
3432 iclog = log->l_iclog;
3433 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3434 /*
3435 * Mark all the incore logs IOERROR.
3436 * From now on, no log flushes will result.
3437 */
3438 ic = iclog;
3439 do {
3440 ic->ic_state = XLOG_STATE_IOERROR;
3441 ic = ic->ic_next;
3442 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445 /*
3446 * Return non-zero, if state transition has already happened.
3447 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003448 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
3451/*
3452 * This is called from xfs_force_shutdown, when we're forcibly
3453 * shutting down the filesystem, typically because of an IO error.
3454 * Our main objectives here are to make sure that:
3455 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3456 * parties to find out, 'atomically'.
3457 * b. those who're sleeping on log reservations, pinned objects and
3458 * other resources get woken up, and be told the bad news.
3459 * c. nothing new gets queued up after (a) and (b) are done.
3460 * d. if !logerror, flush the iclogs to disk, then seal them off
3461 * for business.
3462 */
3463int
3464xfs_log_force_umount(
3465 struct xfs_mount *mp,
3466 int logerror)
3467{
3468 xlog_ticket_t *tic;
3469 xlog_t *log;
3470 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 log = mp->m_log;
3473
3474 /*
3475 * If this happens during log recovery, don't worry about
3476 * locking; the log isn't open for business yet.
3477 */
3478 if (!log ||
3479 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3480 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003481 if (mp->m_sb_bp)
3482 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
3485
3486 /*
3487 * Somebody could've already done the hard work for us.
3488 * No need to get locks for this.
3489 */
3490 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3491 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003492 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494 retval = 0;
3495 /*
3496 * We must hold both the GRANT lock and the LOG lock,
3497 * before we mark the filesystem SHUTDOWN and wake
3498 * everybody up to tell the bad news.
3499 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003500 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003501 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003503 if (mp->m_sb_bp)
3504 XFS_BUF_DONE(mp->m_sb_bp);
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 /*
3507 * This flag is sort of redundant because of the mount flag, but
3508 * it's good to maintain the separation between the log and the rest
3509 * of XFS.
3510 */
3511 log->l_flags |= XLOG_IO_ERROR;
3512
3513 /*
3514 * If we hit a log error, we want to mark all the iclogs IOERROR
3515 * while we're still holding the loglock.
3516 */
3517 if (logerror)
3518 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003519 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 /*
3522 * We don't want anybody waiting for log reservations
3523 * after this. That means we have to wake up everybody
3524 * queued up on reserve_headq as well as write_headq.
3525 * In addition, we make sure in xlog_{re}grant_log_space
3526 * that we don't enqueue anything once the SHUTDOWN flag
3527 * is set, and this action is protected by the GRANTLOCK.
3528 */
3529 if ((tic = log->l_reserve_headq)) {
3530 do {
David Chinner12017fa2008-08-13 16:34:31 +10003531 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 tic = tic->t_next;
3533 } while (tic != log->l_reserve_headq);
3534 }
3535
3536 if ((tic = log->l_write_headq)) {
3537 do {
David Chinner12017fa2008-08-13 16:34:31 +10003538 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 tic = tic->t_next;
3540 } while (tic != log->l_write_headq);
3541 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003542 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003544 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 ASSERT(!logerror);
3546 /*
3547 * Force the incore logs to disk before shutting the
3548 * log down completely.
3549 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003550 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3551
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003552 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003554 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556 /*
3557 * Wake up everybody waiting on xfs_log_force.
3558 * Callback all log item committed functions as if the
3559 * log writes were completed.
3560 */
3561 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3562
3563#ifdef XFSERRORDEBUG
3564 {
3565 xlog_in_core_t *iclog;
3566
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003567 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 iclog = log->l_iclog;
3569 do {
3570 ASSERT(iclog->ic_callback == 0);
3571 iclog = iclog->ic_next;
3572 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003573 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
3575#endif
3576 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003577 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003580STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581xlog_iclogs_empty(xlog_t *log)
3582{
3583 xlog_in_core_t *iclog;
3584
3585 iclog = log->l_iclog;
3586 do {
3587 /* endianness does not matter here, zero is zero in
3588 * any language.
3589 */
3590 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 iclog = iclog->ic_next;
3593 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003594 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}