blob: 8556c59628ba7dc8b72b168e539ba6df41c27688 [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
Dave Chinner43f5efc2010-03-23 10:10:00 +1100651void
652xfs_log_item_init(
653 struct xfs_mount *mp,
654 struct xfs_log_item *item,
655 int type,
656 struct xfs_item_ops *ops)
657{
658 item->li_mountp = mp;
659 item->li_ailp = mp->m_ail;
660 item->li_type = type;
661 item->li_ops = ops;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/*
665 * Write region vectors to log. The write happens using the space reservation
666 * of the ticket (tic). It is not a requirement that all writes for a given
667 * transaction occur with one call to xfs_log_write().
668 */
669int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000670xfs_log_write(
671 struct xfs_mount *mp,
672 struct xfs_log_iovec reg[],
673 int nentries,
674 struct xlog_ticket *tic,
675 xfs_lsn_t *start_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000677 struct log *log = mp->m_log;
678 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (XLOG_FORCED_SHUTDOWN(log))
681 return XFS_ERROR(EIO);
682
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000683 error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0);
684 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +1000685 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100686 return error;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000687}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689void
690xfs_log_move_tail(xfs_mount_t *mp,
691 xfs_lsn_t tail_lsn)
692{
693 xlog_ticket_t *tic;
694 xlog_t *log = mp->m_log;
695 int need_bytes, free_bytes, cycle, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (XLOG_FORCED_SHUTDOWN(log))
698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (tail_lsn == 0) {
701 /* needed since sync_lsn is 64 bits */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000702 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 tail_lsn = log->l_last_sync_lsn;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000704 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000707 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* Also an invalid lsn. 1 implies that we aren't passing in a valid
710 * tail_lsn.
711 */
712 if (tail_lsn != 1) {
713 log->l_tail_lsn = tail_lsn;
714 }
715
716 if ((tic = log->l_write_headq)) {
717#ifdef DEBUG
718 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
719 panic("Recovery problem");
720#endif
721 cycle = log->l_grant_write_cycle;
722 bytes = log->l_grant_write_bytes;
723 free_bytes = xlog_space_left(log, cycle, bytes);
724 do {
725 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
726
727 if (free_bytes < tic->t_unit_res && tail_lsn != 1)
728 break;
729 tail_lsn = 0;
730 free_bytes -= tic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +1000731 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 tic = tic->t_next;
733 } while (tic != log->l_write_headq);
734 }
735 if ((tic = log->l_reserve_headq)) {
736#ifdef DEBUG
737 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
738 panic("Recovery problem");
739#endif
740 cycle = log->l_grant_reserve_cycle;
741 bytes = log->l_grant_reserve_bytes;
742 free_bytes = xlog_space_left(log, cycle, bytes);
743 do {
744 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
745 need_bytes = tic->t_unit_res*tic->t_cnt;
746 else
747 need_bytes = tic->t_unit_res;
748 if (free_bytes < need_bytes && tail_lsn != 1)
749 break;
750 tail_lsn = 0;
751 free_bytes -= need_bytes;
David Chinner12017fa2008-08-13 16:34:31 +1000752 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 tic = tic->t_next;
754 } while (tic != log->l_reserve_headq);
755 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000756 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757} /* xfs_log_move_tail */
758
759/*
760 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000761 * that needs to be covered. To begin the transition to the idle state
762 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
763 * If we are then in a state where covering is needed, the caller is informed
764 * that dummy transactions are required to move the log into the idle state.
765 *
766 * Because this is called as part of the sync process, we should also indicate
767 * that dummy transactions should be issued in anything but the covered or
768 * idle states. This ensures that the log tail is accurately reflected in
769 * the log at the end of the sync, hence if a crash occurrs avoids replay
770 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
772int
773xfs_log_need_covered(xfs_mount_t *mp)
774{
David Chinner27d8d5f2008-10-30 17:38:39 +1100775 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David Chinner92821e22007-05-24 15:26:31 +1000778 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return 0;
780
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000781 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000782 switch (log->l_covered_state) {
783 case XLOG_STATE_COVER_DONE:
784 case XLOG_STATE_COVER_DONE2:
785 case XLOG_STATE_COVER_IDLE:
786 break;
787 case XLOG_STATE_COVER_NEED:
788 case XLOG_STATE_COVER_NEED2:
789 if (!xfs_trans_ail_tail(log->l_ailp) &&
790 xlog_iclogs_empty(log)) {
791 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
792 log->l_covered_state = XLOG_STATE_COVER_DONE;
793 else
794 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000796 /* FALLTHRU */
797 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000801 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100802 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805/******************************************************************************
806 *
807 * local routines
808 *
809 ******************************************************************************
810 */
811
812/* xfs_trans_tail_ail returns 0 when there is nothing in the list.
813 * The log manager must keep track of the last LR which was committed
814 * to disk. The lsn of this LR will become the new tail_lsn whenever
815 * xfs_trans_tail_ail returns 0. If we don't do this, we run into
816 * the situation where stuff could be written into the log but nothing
817 * was ever in the AIL when asked. Eventually, we panic since the
818 * tail hits the head.
819 *
820 * We may be holding the log iclog lock upon entering this routine.
821 */
822xfs_lsn_t
823xlog_assign_tail_lsn(xfs_mount_t *mp)
824{
825 xfs_lsn_t tail_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xlog_t *log = mp->m_log;
827
David Chinner5b00f142008-10-30 17:39:00 +1100828 tail_lsn = xfs_trans_ail_tail(mp->m_ail);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000829 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (tail_lsn != 0) {
831 log->l_tail_lsn = tail_lsn;
832 } else {
833 tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn;
834 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +1000835 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 return tail_lsn;
838} /* xlog_assign_tail_lsn */
839
840
841/*
842 * Return the space in the log between the tail and the head. The head
843 * is passed in the cycle/bytes formal parms. In the special case where
844 * the reserve head has wrapped passed the tail, this calculation is no
845 * longer valid. In this case, just return 0 which means there is no space
846 * in the log. This works for all places where this function is called
847 * with the reserve head. Of course, if the write head were to ever
848 * wrap the tail, we should blow up. Rather than catch this case here,
849 * we depend on other ASSERTions in other parts of the code. XXXmiken
850 *
851 * This code also handles the case where the reservation head is behind
852 * the tail. The details of this case are described below, but the end
853 * result is that we return the size of the log as the amount of space left.
854 */
David Chinnera8272ce2007-11-23 16:28:09 +1100855STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856xlog_space_left(xlog_t *log, int cycle, int bytes)
857{
858 int free_bytes;
859 int tail_bytes;
860 int tail_cycle;
861
862 tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn));
863 tail_cycle = CYCLE_LSN(log->l_tail_lsn);
864 if ((tail_cycle == cycle) && (bytes >= tail_bytes)) {
865 free_bytes = log->l_logsize - (bytes - tail_bytes);
866 } else if ((tail_cycle + 1) < cycle) {
867 return 0;
868 } else if (tail_cycle < cycle) {
869 ASSERT(tail_cycle == (cycle - 1));
870 free_bytes = tail_bytes - bytes;
871 } else {
872 /*
873 * The reservation head is behind the tail.
874 * In this case we just want to return the size of the
875 * log as the amount of space left.
876 */
877 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
878 "xlog_space_left: head behind tail\n"
879 " tail_cycle = %d, tail_bytes = %d\n"
880 " GH cycle = %d, GH bytes = %d",
881 tail_cycle, tail_bytes, cycle, bytes);
882 ASSERT(0);
883 free_bytes = log->l_logsize;
884 }
885 return free_bytes;
886} /* xlog_space_left */
887
888
889/*
890 * Log function which is called when an io completes.
891 *
892 * The log manager needs its own routine, in order to control what
893 * happens with the buffer after the write completes.
894 */
895void
896xlog_iodone(xfs_buf_t *bp)
897{
898 xlog_in_core_t *iclog;
899 xlog_t *l;
900 int aborted;
901
902 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
903 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
904 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
905 aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 l = iclog->ic_log;
907
908 /*
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100909 * If the _XFS_BARRIER_FAILED flag was set by a lower
910 * layer, it means the underlying device no longer supports
David Chinner0bfefc42007-05-14 18:24:23 +1000911 * barrier I/O. Warn loudly and turn off barriers.
912 */
Christoph Hellwig73f6aa42008-10-10 17:28:29 +1100913 if (bp->b_flags & _XFS_BARRIER_FAILED) {
914 bp->b_flags &= ~_XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +1000915 l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
916 xfs_fs_cmn_err(CE_WARN, l->l_mp,
917 "xlog_iodone: Barriers are no longer supported"
918 " by device. Disabling barriers\n");
David Chinner0bfefc42007-05-14 18:24:23 +1000919 }
920
921 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Race to shutdown the filesystem if we see an error.
923 */
924 if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp,
925 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
926 xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp));
927 XFS_BUF_STALE(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000928 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /*
930 * This flag will be propagated to the trans-committed
931 * callback routines to let them know that the log-commit
932 * didn't succeed.
933 */
934 aborted = XFS_LI_ABORTED;
935 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
936 aborted = XFS_LI_ABORTED;
937 }
David Chinner3db296f2007-05-14 18:24:16 +1000938
939 /* log I/O is always issued ASYNC */
940 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000942 /*
943 * do not reference the buffer (bp) here as we could race
944 * with it being freed after writing the unmount record to the
945 * log.
946 */
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948} /* xlog_iodone */
949
950/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * Return size of each in-core log record buffer.
952 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200953 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
955 * If the filesystem blocksize is too large, we may need to choose a
956 * larger size since the directory code currently logs entire blocks.
957 */
958
959STATIC void
960xlog_get_iclog_buffer_size(xfs_mount_t *mp,
961 xlog_t *log)
962{
963 int size;
964 int xhdrs;
965
Eric Sandeen1cb51252007-08-16 16:24:43 +1000966 if (mp->m_logbufs <= 0)
967 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
968 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100969 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /*
972 * Buffer size passed in from mount system call.
973 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100974 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 size = log->l_iclog_size = mp->m_logbsize;
976 log->l_iclog_size_log = 0;
977 while (size != 1) {
978 log->l_iclog_size_log++;
979 size >>= 1;
980 }
981
Eric Sandeen62118702008-03-06 13:44:28 +1100982 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200983 /* # headers = size / 32k
984 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
986
987 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
988 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
989 xhdrs++;
990 log->l_iclog_hsize = xhdrs << BBSHIFT;
991 log->l_iclog_heads = xhdrs;
992 } else {
993 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
994 log->l_iclog_hsize = BBSIZE;
995 log->l_iclog_heads = 1;
996 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100997 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001000 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001001 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1002 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /* the default log size is 16k or 32k which is one header sector */
1005 log->l_iclog_hsize = BBSIZE;
1006 log->l_iclog_heads = 1;
1007
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001008done:
1009 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001010 if (mp->m_logbufs == 0)
1011 mp->m_logbufs = log->l_iclog_bufs;
1012 if (mp->m_logbsize == 0)
1013 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014} /* xlog_get_iclog_buffer_size */
1015
1016
1017/*
1018 * This routine initializes some of the log structure for a given mount point.
1019 * Its primary purpose is to fill in enough, so recovery can occur. However,
1020 * some other stuff may be filled in too.
1021 */
1022STATIC xlog_t *
1023xlog_alloc_log(xfs_mount_t *mp,
1024 xfs_buftarg_t *log_target,
1025 xfs_daddr_t blk_offset,
1026 int num_bblks)
1027{
1028 xlog_t *log;
1029 xlog_rec_header_t *head;
1030 xlog_in_core_t **iclogp;
1031 xlog_in_core_t *iclog, *prev_iclog=NULL;
1032 xfs_buf_t *bp;
1033 int i;
1034 int iclogsize;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001035 int error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Dave Chinner644c3562008-11-10 16:50:24 +11001037 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001038 if (!log) {
1039 xlog_warn("XFS: Log allocation failed: No memory!");
1040 goto out;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 log->l_mp = mp;
1044 log->l_targ = log_target;
1045 log->l_logsize = BBTOB(num_bblks);
1046 log->l_logBBstart = blk_offset;
1047 log->l_logBBsize = num_bblks;
1048 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1049 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1050
1051 log->l_prev_block = -1;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001052 log->l_tail_lsn = xlog_assign_lsn(1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
1054 log->l_last_sync_lsn = log->l_tail_lsn;
1055 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
1056 log->l_grant_reserve_cycle = 1;
1057 log->l_grant_write_cycle = 1;
1058
Dave Chinnera6cb7672009-04-06 18:39:27 +02001059 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001060 if (xfs_sb_version_hassector(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001062 if (log->l_sectbb_log < 0 ||
1063 log->l_sectbb_log > mp->m_sectbb_log) {
1064 xlog_warn("XFS: Log sector size (0x%x) out of range.",
1065 log->l_sectbb_log);
1066 goto out_free_log;
1067 }
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* for larger sector sizes, must have v2 or external log */
Dave Chinnera6cb7672009-04-06 18:39:27 +02001070 if (log->l_sectbb_log != 0 &&
1071 (log->l_logBBstart != 0 &&
1072 !xfs_sb_version_haslogv2(&mp->m_sb))) {
1073 xlog_warn("XFS: log sector size (0x%x) invalid "
1074 "for configuration.", log->l_sectbb_log);
1075 goto out_free_log;
1076 }
1077 if (mp->m_sb.sb_logsectlog < BBSHIFT) {
1078 xlog_warn("XFS: Log sector log (0x%x) too small.",
1079 mp->m_sb.sb_logsectlog);
1080 goto out_free_log;
1081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
1084
1085 xlog_get_iclog_buffer_size(mp, log);
1086
Dave Chinnera6cb7672009-04-06 18:39:27 +02001087 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001089 if (!bp)
1090 goto out_free_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1093 ASSERT(XFS_BUF_ISBUSY(bp));
1094 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
1095 log->l_xbuf = bp;
1096
Eric Sandeen007c61c2007-10-11 17:43:56 +10001097 spin_lock_init(&log->l_icloglock);
1098 spin_lock_init(&log->l_grant_lock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10001099 sv_init(&log->l_flush_wait, 0, "flush_wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1102 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1103
1104 iclogp = &log->l_iclog;
1105 /*
1106 * The amount of memory to allocate for the iclog structure is
1107 * rather funky due to the way the structure is defined. It is
1108 * done this way so that we can use different sizes for machines
1109 * with different amounts of memory. See the definition of
1110 * xlog_in_core_t in xfs_log_priv.h for details.
1111 */
1112 iclogsize = log->l_iclog_size;
1113 ASSERT(log->l_iclog_size >= 4096);
1114 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001115 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1116 if (!*iclogp)
1117 goto out_free_iclog;
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 iclog->ic_prev = prev_iclog;
1121 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001122
1123 bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
Dave Chinner644c3562008-11-10 16:50:24 +11001124 if (!bp)
1125 goto out_free_iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001126 if (!XFS_BUF_CPSEMA(bp))
1127 ASSERT(0);
1128 XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001129 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
1130 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001131 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001132#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 head = &iclog->ic_header;
1136 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001137 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1138 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001139 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001140 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001142 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1146 iclog->ic_state = XLOG_STATE_ACTIVE;
1147 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001148 atomic_set(&iclog->ic_refcnt, 0);
1149 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001151 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
1154 ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
David Chinner12017fa2008-08-13 16:34:31 +10001155 sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
1156 sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 iclogp = &iclog->ic_next;
1159 }
1160 *iclogp = log->l_iclog; /* complete ring */
1161 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1162
1163 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001164
1165out_free_iclog:
1166 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1167 prev_iclog = iclog->ic_next;
1168 if (iclog->ic_bp) {
1169 sv_destroy(&iclog->ic_force_wait);
1170 sv_destroy(&iclog->ic_write_wait);
1171 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001172 }
1173 kmem_free(iclog);
1174 }
1175 spinlock_destroy(&log->l_icloglock);
1176 spinlock_destroy(&log->l_grant_lock);
Dave Chinner644c3562008-11-10 16:50:24 +11001177 xfs_buf_free(log->l_xbuf);
1178out_free_log:
1179 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001180out:
1181 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182} /* xlog_alloc_log */
1183
1184
1185/*
1186 * Write out the commit record of a transaction associated with the given
1187 * ticket. Return the lsn of the commit record.
1188 */
1189STATIC int
1190xlog_commit_record(xfs_mount_t *mp,
1191 xlog_ticket_t *ticket,
1192 xlog_in_core_t **iclog,
1193 xfs_lsn_t *commitlsnp)
1194{
1195 int error;
1196 xfs_log_iovec_t reg[1];
1197
1198 reg[0].i_addr = NULL;
1199 reg[0].i_len = 0;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +00001200 reg[0].i_type = XLOG_REG_TYPE_COMMIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 ASSERT_ALWAYS(iclog);
1203 if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
1204 iclog, XLOG_COMMIT_TRANS))) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001205 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001207 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208} /* xlog_commit_record */
1209
1210
1211/*
1212 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1213 * log space. This code pushes on the lsn which would supposedly free up
1214 * the 25% which we want to leave free. We may need to adopt a policy which
1215 * pushes on an lsn which is further along in the log once we reach the high
1216 * water mark. In this manner, we would be creating a low water mark.
1217 */
David Chinnera8272ce2007-11-23 16:28:09 +11001218STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219xlog_grant_push_ail(xfs_mount_t *mp,
1220 int need_bytes)
1221{
1222 xlog_t *log = mp->m_log; /* pointer to the log */
1223 xfs_lsn_t tail_lsn; /* lsn of the log tail */
1224 xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */
1225 int free_blocks; /* free blocks left to write to */
1226 int free_bytes; /* free bytes left to write to */
1227 int threshold_block; /* block in lsn we'd like to be at */
1228 int threshold_cycle; /* lsn cycle we'd like to be at */
1229 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1232
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001233 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 free_bytes = xlog_space_left(log,
1235 log->l_grant_reserve_cycle,
1236 log->l_grant_reserve_bytes);
1237 tail_lsn = log->l_tail_lsn;
1238 free_blocks = BTOBBT(free_bytes);
1239
1240 /*
1241 * Set the threshold for the minimum number of free blocks in the
1242 * log to the maximum of what the caller needs, one quarter of the
1243 * log, and 256 blocks.
1244 */
1245 free_threshold = BTOBB(need_bytes);
1246 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1247 free_threshold = MAX(free_threshold, 256);
1248 if (free_blocks < free_threshold) {
1249 threshold_block = BLOCK_LSN(tail_lsn) + free_threshold;
1250 threshold_cycle = CYCLE_LSN(tail_lsn);
1251 if (threshold_block >= log->l_logBBsize) {
1252 threshold_block -= log->l_logBBsize;
1253 threshold_cycle += 1;
1254 }
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001255 threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* Don't pass in an lsn greater than the lsn of the last
1258 * log record known to be on disk.
1259 */
1260 if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0)
1261 threshold_lsn = log->l_last_sync_lsn;
1262 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001263 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /*
1266 * Get the transaction layer to kick the dirty buffers out to
1267 * disk asynchronously. No point in trying to do this if
1268 * the filesystem is shutting down.
1269 */
1270 if (threshold_lsn &&
1271 !XLOG_FORCED_SHUTDOWN(log))
David Chinner783a2f62008-10-30 17:39:58 +11001272 xfs_trans_ail_push(log->l_ailp, threshold_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273} /* xlog_grant_push_ail */
1274
Christoph Hellwig873ff552010-01-13 22:17:57 +00001275/*
1276 * The bdstrat callback function for log bufs. This gives us a central
1277 * place to trap bufs in case we get hit by a log I/O error and need to
1278 * shutdown. Actually, in practice, even when we didn't get a log error,
1279 * we transition the iclogs to IOERROR state *after* flushing all existing
1280 * iclogs to disk. This is because we don't want anymore new transactions to be
1281 * started or completed afterwards.
1282 */
1283STATIC int
1284xlog_bdstrat(
1285 struct xfs_buf *bp)
1286{
1287 struct xlog_in_core *iclog;
1288
1289 iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
1290 if (iclog->ic_state & XLOG_STATE_IOERROR) {
1291 XFS_BUF_ERROR(bp, EIO);
1292 XFS_BUF_STALE(bp);
1293 xfs_biodone(bp);
1294 /*
1295 * It would seem logical to return EIO here, but we rely on
1296 * the log state machine to propagate I/O errors instead of
1297 * doing it here.
1298 */
1299 return 0;
1300 }
1301
1302 bp->b_flags |= _XBF_RUN_QUEUES;
1303 xfs_buf_iorequest(bp);
1304 return 0;
1305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307/*
1308 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1309 * fashion. Previously, we should have moved the current iclog
1310 * ptr in the log to point to the next available iclog. This allows further
1311 * write to continue while this code syncs out an iclog ready to go.
1312 * Before an in-core log can be written out, the data section must be scanned
1313 * to save away the 1st word of each BBSIZE block into the header. We replace
1314 * it with the current cycle count. Each BBSIZE block is tagged with the
1315 * cycle count because there in an implicit assumption that drives will
1316 * guarantee that entire 512 byte blocks get written at once. In other words,
1317 * we can't have part of a 512 byte block written and part not written. By
1318 * tagging each block, we will know which blocks are valid when recovering
1319 * after an unclean shutdown.
1320 *
1321 * This routine is single threaded on the iclog. No other thread can be in
1322 * this routine with the same iclog. Changing contents of iclog can there-
1323 * fore be done without grabbing the state machine lock. Updating the global
1324 * log will require grabbing the lock though.
1325 *
1326 * The entire log manager uses a logical block numbering scheme. Only
1327 * log_sync (and then only bwrite()) know about the fact that the log may
1328 * not start with block zero on a given device. The log block start offset
1329 * is added immediately before calling bwrite().
1330 */
1331
David Chinnera8272ce2007-11-23 16:28:09 +11001332STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333xlog_sync(xlog_t *log,
1334 xlog_in_core_t *iclog)
1335{
1336 xfs_caddr_t dptr; /* pointer to byte sized element */
1337 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001338 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 uint count; /* byte count of bwrite */
1340 uint count_init; /* initial count before roundup */
1341 int roundoff; /* roundoff to BB or stripe */
1342 int split = 0; /* split write into two regions */
1343 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001344 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001347 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /* Add for LR header */
1350 count_init = log->l_iclog_hsize + iclog->ic_offset;
1351
1352 /* Round out the log write size */
1353 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1354 /* we have a v2 stripe unit to use */
1355 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1356 } else {
1357 count = BBTOB(BTOBB(count_init));
1358 }
1359 roundoff = count - count_init;
1360 ASSERT(roundoff >= 0);
1361 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1362 roundoff < log->l_mp->m_sb.sb_logsunit)
1363 ||
1364 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1365 roundoff < BBTOB(1)));
1366
1367 /* move grant heads by roundoff in sync */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001368 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11001369 xlog_grant_add_space(log, roundoff);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10001370 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* put cycle number in every block */
1373 xlog_pack_data(log, iclog, roundoff);
1374
1375 /* real byte length */
1376 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001377 iclog->ic_header.h_len =
1378 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001380 iclog->ic_header.h_len =
1381 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
Nathan Scottf5faad72006-07-28 17:04:44 +10001384 bp = iclog->ic_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1);
1386 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001387 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1390
1391 /* Do we need to split this write into 2 parts? */
1392 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1393 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1394 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1395 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1396 } else {
1397 iclog->ic_bwritecnt = 1;
1398 }
David Chinner511105b2007-05-24 15:21:57 +10001399 XFS_BUF_SET_COUNT(bp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */
Nathan Scottf5faad72006-07-28 17:04:44 +10001401 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 XFS_BUF_BUSY(bp);
1403 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001404 bp->b_flags |= XBF_LOG_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /*
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001406 * Do an ordered write for the log block.
Nathan Scottf5faad72006-07-28 17:04:44 +10001407 * Its unnecessary to flush the first split block in the log wrap case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 */
Nathan Scottf5faad72006-07-28 17:04:44 +10001409 if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001410 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1413 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1414
1415 xlog_verify_iclog(log, iclog, count, B_TRUE);
1416
1417 /* account for log which doesn't start at block #0 */
1418 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1419 /*
1420 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1421 * is shutting down.
1422 */
1423 XFS_BUF_WRITE(bp);
1424
Christoph Hellwig873ff552010-01-13 22:17:57 +00001425 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
1427 XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001428 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001431 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) ==
1433 (unsigned long)1);
1434 XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2);
1435 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
1436 XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+
1437 (__psint_t)count), split);
1438 XFS_BUF_SET_FSPRIVATE(bp, iclog);
Nathan Scottf5faad72006-07-28 17:04:44 +10001439 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 XFS_BUF_BUSY(bp);
1441 XFS_BUF_ASYNC(bp);
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001442 bp->b_flags |= XBF_LOG_BUFFER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001443 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
1444 XFS_BUF_ORDERED(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 dptr = XFS_BUF_PTR(bp);
1446 /*
1447 * Bump the cycle numbers at the start of each block
1448 * since this part of the buffer is at the start of
1449 * a new cycle. Watch out for the header magic number
1450 * case, though.
1451 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001452 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001453 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001454 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001455 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 dptr += BBSIZE;
1457 }
1458
1459 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1460 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1461
Nathan Scottc41564b2006-03-29 08:55:14 +10001462 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1464 XFS_BUF_WRITE(bp);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001465 if ((error = xlog_bdstrat(bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
1467 bp, XFS_BUF_ADDR(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001468 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472} /* xlog_sync */
1473
1474
1475/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001476 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 */
David Chinnera8272ce2007-11-23 16:28:09 +11001478STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001479xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int i;
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 iclog = log->l_iclog;
1485 for (i=0; i<log->l_iclog_bufs; i++) {
David Chinner12017fa2008-08-13 16:34:31 +10001486 sv_destroy(&iclog->ic_force_wait);
1487 sv_destroy(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001490 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 iclog = next_iclog;
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 spinlock_destroy(&log->l_icloglock);
1494 spinlock_destroy(&log->l_grant_lock);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 xfs_buf_free(log->l_xbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001498 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001499} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501/*
1502 * Update counters atomically now that memcpy is done.
1503 */
1504/* ARGSUSED */
1505static inline void
1506xlog_state_finish_copy(xlog_t *log,
1507 xlog_in_core_t *iclog,
1508 int record_cnt,
1509 int copy_bytes)
1510{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001511 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001513 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 iclog->ic_offset += copy_bytes;
1515
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001516 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517} /* xlog_state_finish_copy */
1518
1519
1520
1521
1522/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001523 * print out info relating to regions written which consume
1524 * the reservation
1525 */
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001526STATIC void
1527xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
1528{
1529 uint i;
1530 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1531
1532 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1533 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1534 "bformat",
1535 "bchunk",
1536 "efi_format",
1537 "efd_format",
1538 "iformat",
1539 "icore",
1540 "iext",
1541 "ibroot",
1542 "ilocal",
1543 "iattr_ext",
1544 "iattr_broot",
1545 "iattr_local",
1546 "qformat",
1547 "dquot",
1548 "quotaoff",
1549 "LR header",
1550 "unmount",
1551 "commit",
1552 "trans header"
1553 };
1554 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1555 "SETATTR_NOT_SIZE",
1556 "SETATTR_SIZE",
1557 "INACTIVE",
1558 "CREATE",
1559 "CREATE_TRUNC",
1560 "TRUNCATE_FILE",
1561 "REMOVE",
1562 "LINK",
1563 "RENAME",
1564 "MKDIR",
1565 "RMDIR",
1566 "SYMLINK",
1567 "SET_DMATTRS",
1568 "GROWFS",
1569 "STRAT_WRITE",
1570 "DIOSTRAT",
1571 "WRITE_SYNC",
1572 "WRITEID",
1573 "ADDAFORK",
1574 "ATTRINVAL",
1575 "ATRUNCATE",
1576 "ATTR_SET",
1577 "ATTR_RM",
1578 "ATTR_FLAG",
1579 "CLEAR_AGI_BUCKET",
1580 "QM_SBCHANGE",
1581 "DUMMY1",
1582 "DUMMY2",
1583 "QM_QUOTAOFF",
1584 "QM_DQALLOC",
1585 "QM_SETQLIM",
1586 "QM_DQCLUSTER",
1587 "QM_QINOCREATE",
1588 "QM_QUOTAOFF_END",
1589 "SB_UNIT",
1590 "FSYNC_TS",
1591 "GROWFSRT_ALLOC",
1592 "GROWFSRT_ZERO",
1593 "GROWFSRT_FREE",
1594 "SWAPEXT"
1595 };
1596
1597 xfs_fs_cmn_err(CE_WARN, mp,
1598 "xfs_log_write: reservation summary:\n"
1599 " trans type = %s (%u)\n"
1600 " unit res = %d bytes\n"
1601 " current res = %d bytes\n"
1602 " total reg = %u bytes (o/flow = %u bytes)\n"
1603 " ophdrs = %u (ophdr space = %u bytes)\n"
1604 " ophdr + reg = %u bytes\n"
1605 " num regions = %u\n",
1606 ((ticket->t_trans_type <= 0 ||
1607 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1608 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1609 ticket->t_trans_type,
1610 ticket->t_unit_res,
1611 ticket->t_curr_res,
1612 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1613 ticket->t_res_num_ophdrs, ophdr_spc,
1614 ticket->t_res_arr_sum +
Tim Shimmin12598452006-01-11 21:02:47 +11001615 ticket->t_res_o_flow + ophdr_spc,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001616 ticket->t_res_num);
1617
1618 for (i = 0; i < ticket->t_res_num; i++) {
Tim Shimmin12598452006-01-11 21:02:47 +11001619 uint r_type = ticket->t_res_arr[i].r_type;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001620 cmn_err(CE_WARN,
1621 "region[%u]: %s - %u bytes\n",
1622 i,
1623 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1624 "bad-rtype" : res_type_str[r_type-1]),
1625 ticket->t_res_arr[i].r_len);
1626 }
1627}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001628
1629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * Write some region out to in-core log
1631 *
1632 * This will be called when writing externally provided regions or when
1633 * writing out a commit record for a given transaction.
1634 *
1635 * General algorithm:
1636 * 1. Find total length of this write. This may include adding to the
1637 * lengths passed in.
1638 * 2. Check whether we violate the tickets reservation.
1639 * 3. While writing to this iclog
1640 * A. Reserve as much space in this iclog as can get
1641 * B. If this is first write, save away start lsn
1642 * C. While writing this region:
1643 * 1. If first write of transaction, write start record
1644 * 2. Write log operation header (header per region)
1645 * 3. Find out if we can fit entire region into this iclog
1646 * 4. Potentially, verify destination memcpy ptr
1647 * 5. Memcpy (partial) region
1648 * 6. If partial copy, release iclog; otherwise, continue
1649 * copying more regions into current iclog
1650 * 4. Mark want sync bit (in simulation mode)
1651 * 5. Release iclog for potential flush to on-disk log.
1652 *
1653 * ERRORS:
1654 * 1. Panic if reservation is overrun. This should never happen since
1655 * reservation amounts are generated internal to the filesystem.
1656 * NOTES:
1657 * 1. Tickets are single threaded data structures.
1658 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1659 * syncing routine. When a single log_write region needs to span
1660 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1661 * on all log operation writes which don't contain the end of the
1662 * region. The XLOG_END_TRANS bit is used for the in-core log
1663 * operation which contains the end of the continued log_write region.
1664 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1665 * we don't really know exactly how much space will be used. As a result,
1666 * we don't update ic_offset until the end when we know exactly how many
1667 * bytes have been written out.
1668 */
David Chinnera8272ce2007-11-23 16:28:09 +11001669STATIC int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001670xlog_write(
1671 struct xfs_mount *mp,
1672 struct xfs_log_iovec reg[],
1673 int nentries,
1674 struct xlog_ticket *ticket,
1675 xfs_lsn_t *start_lsn,
1676 struct xlog_in_core **commit_iclog,
1677 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Nathan Scott5493a0f2006-06-28 11:17:28 +10001679 xlog_t *log = mp->m_log;
Nathan Scott5493a0f2006-06-28 11:17:28 +10001680 xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 xlog_op_header_t *logop_head; /* ptr to log operation header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 __psint_t ptr; /* copy address into data region */
1683 int len; /* # xlog_write() bytes 2 still copy */
1684 int index; /* region index currently copying */
1685 int log_offset; /* offset (from 0) into data region */
1686 int start_rec_copy; /* # bytes to copy for start record */
1687 int partial_copy; /* did we split a region? */
1688 int partial_copy_len;/* # bytes copied if split region */
1689 int need_copy; /* # bytes need to memcpy this region */
1690 int copy_len; /* # bytes actually memcpy'ing */
1691 int copy_off; /* # bytes from entry start */
1692 int contwr; /* continued write of in-core log? */
1693 int error;
1694 int record_cnt = 0, data_cnt = 0;
1695
1696 partial_copy_len = partial_copy = 0;
1697
1698 /* Calculate potential maximum space. Each region gets its own
1699 * xlog_op_header_t and may need to be double word aligned.
1700 */
1701 len = 0;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001702 if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 len += sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001704 ticket->t_res_num_ophdrs++;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 for (index = 0; index < nentries; index++) {
1708 len += sizeof(xlog_op_header_t); /* each region gets >= 1 */
Christoph Hellwig0adba532007-08-30 17:21:46 +10001709 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 len += reg[index].i_len;
Christoph Hellwig0adba532007-08-30 17:21:46 +10001711 xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 contwr = *start_lsn = 0;
1714
1715 if (ticket->t_curr_res < len) {
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001716 xlog_print_tic_res(mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717#ifdef DEBUG
1718 xlog_panic(
1719 "xfs_log_write: reservation ran out. Need to up reservation");
1720#else
1721 /* Customer configurable panic */
1722 xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
1723 "xfs_log_write: reservation ran out. Need to up reservation");
1724 /* If we did not panic, shutdown the filesystem */
Nathan Scott7d04a332006-06-09 14:58:38 +10001725 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726#endif
1727 } else
1728 ticket->t_curr_res -= len;
1729
1730 for (index = 0; index < nentries; ) {
1731 if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1732 &contwr, &log_offset)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001733 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 ASSERT(log_offset <= iclog->ic_size - 1);
1736 ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
1737
1738 /* start_lsn is the first lsn written to. That's all we need. */
1739 if (! *start_lsn)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001740 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 /* This loop writes out as many regions as can fit in the amount
1743 * of space which was allocated by xlog_state_get_iclog_space().
1744 */
1745 while (index < nentries) {
1746 ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
1747 ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
1748 start_rec_copy = 0;
1749
1750 /* If first write for transaction, insert start record.
1751 * We can't be trying to commit if we are inited. We can't
1752 * have any "partial_copy" if we are inited.
1753 */
1754 if (ticket->t_flags & XLOG_TIC_INITED) {
1755 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001756 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 logop_head->oh_clientid = ticket->t_clientid;
1758 logop_head->oh_len = 0;
1759 logop_head->oh_flags = XLOG_START_TRANS;
1760 logop_head->oh_res2 = 0;
1761 ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */
1762 record_cnt++;
1763
1764 start_rec_copy = sizeof(xlog_op_header_t);
1765 xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
1766 }
1767
1768 /* Copy log operation header directly into data section */
1769 logop_head = (xlog_op_header_t *)ptr;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001770 logop_head->oh_tid = cpu_to_be32(ticket->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 logop_head->oh_clientid = ticket->t_clientid;
1772 logop_head->oh_res2 = 0;
1773
1774 /* header copied directly */
1775 xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
1776
1777 /* are we copying a commit or unmount record? */
1778 logop_head->oh_flags = flags;
1779
1780 /*
1781 * We've seen logs corrupted with bad transaction client
1782 * ids. This makes sure that XFS doesn't generate them on.
1783 * Turn this into an EIO and shut down the filesystem.
1784 */
1785 switch (logop_head->oh_clientid) {
1786 case XFS_TRANSACTION:
1787 case XFS_VOLUME:
1788 case XFS_LOG:
1789 break;
1790 default:
1791 xfs_fs_cmn_err(CE_WARN, mp,
1792 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001793 logop_head->oh_clientid, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return XFS_ERROR(EIO);
1795 }
1796
1797 /* Partial write last time? => (partial_copy != 0)
1798 * need_copy is the amount we'd like to copy if everything could
1799 * fit in the current memcpy.
1800 */
1801 need_copy = reg[index].i_len - partial_copy_len;
1802
1803 copy_off = partial_copy_len;
1804 if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001805 copy_len = need_copy;
1806 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (partial_copy)
1808 logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1809 partial_copy_len = partial_copy = 0;
1810 } else { /* partial write */
1811 copy_len = iclog->ic_size - log_offset;
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10001812 logop_head->oh_len = cpu_to_be32(copy_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
1814 if (partial_copy)
1815 logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
1816 partial_copy_len += copy_len;
1817 partial_copy++;
1818 len += sizeof(xlog_op_header_t); /* from splitting of region */
1819 /* account for new log op header */
1820 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Christoph Hellwig0adba532007-08-30 17:21:46 +10001821 ticket->t_res_num_ophdrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 xlog_verify_dest_ptr(log, ptr);
1824
1825 /* copy region */
1826 ASSERT(copy_len >= 0);
1827 memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
1828 xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
1829
1830 /* make copy_len total bytes copied, including headers */
1831 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1832 record_cnt++;
1833 data_cnt += contwr ? copy_len : 0;
1834 if (partial_copy) { /* copied partial region */
1835 /* already marked WANT_SYNC by xlog_state_get_iclog_space */
1836 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1837 record_cnt = data_cnt = 0;
1838 if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001839 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 break; /* don't increment index */
1841 } else { /* copied entire region */
1842 index++;
1843 partial_copy_len = partial_copy = 0;
1844
1845 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1846 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1847 record_cnt = data_cnt = 0;
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001848 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +01001850 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (commit_iclog) {
1852 ASSERT(flags & XLOG_COMMIT_TRANS);
1853 *commit_iclog = iclog;
1854 } else if ((error = xlog_state_release_iclog(log, iclog)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (index == nentries)
1857 return 0; /* we are done */
1858 else
1859 break;
1860 }
1861 } /* if (partial_copy) */
1862 } /* while (index < nentries) */
1863 } /* for (index = 0; index < nentries; ) */
1864 ASSERT(len == 0);
1865
1866 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
1867 if (commit_iclog) {
1868 ASSERT(flags & XLOG_COMMIT_TRANS);
1869 *commit_iclog = iclog;
1870 return 0;
1871 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001872 return xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873} /* xlog_write */
1874
1875
1876/*****************************************************************************
1877 *
1878 * State Machine functions
1879 *
1880 *****************************************************************************
1881 */
1882
1883/* Clean iclogs starting from the head. This ordering must be
1884 * maintained, so an iclog doesn't become ACTIVE beyond one that
1885 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10001886 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * iclog is trying to sync to disk.
1888 *
1889 * State Change: DIRTY -> ACTIVE
1890 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001891STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892xlog_state_clean_log(xlog_t *log)
1893{
1894 xlog_in_core_t *iclog;
1895 int changed = 0;
1896
1897 iclog = log->l_iclog;
1898 do {
1899 if (iclog->ic_state == XLOG_STATE_DIRTY) {
1900 iclog->ic_state = XLOG_STATE_ACTIVE;
1901 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10001902 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /*
1904 * If the number of ops in this iclog indicate it just
1905 * contains the dummy transaction, we can
1906 * change state into IDLE (the second time around).
1907 * Otherwise we should change the state into
1908 * NEED a dummy.
1909 * We don't need to cover the dummy.
1910 */
1911 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001912 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
1913 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 changed = 1;
1915 } else {
1916 /*
1917 * We have two dirty iclogs so start over
1918 * This could also be num of ops indicates
1919 * this is not the dummy going out.
1920 */
1921 changed = 2;
1922 }
1923 iclog->ic_header.h_num_logops = 0;
1924 memset(iclog->ic_header.h_cycle_data, 0,
1925 sizeof(iclog->ic_header.h_cycle_data));
1926 iclog->ic_header.h_lsn = 0;
1927 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
1928 /* do nothing */;
1929 else
1930 break; /* stop cleaning */
1931 iclog = iclog->ic_next;
1932 } while (iclog != log->l_iclog);
1933
1934 /* log is locked when we are called */
1935 /*
1936 * Change state for the dummy log recording.
1937 * We usually go to NEED. But we go to NEED2 if the changed indicates
1938 * we are done writing the dummy record.
1939 * If we are done with the second dummy recored (DONE2), then
1940 * we go to IDLE.
1941 */
1942 if (changed) {
1943 switch (log->l_covered_state) {
1944 case XLOG_STATE_COVER_IDLE:
1945 case XLOG_STATE_COVER_NEED:
1946 case XLOG_STATE_COVER_NEED2:
1947 log->l_covered_state = XLOG_STATE_COVER_NEED;
1948 break;
1949
1950 case XLOG_STATE_COVER_DONE:
1951 if (changed == 1)
1952 log->l_covered_state = XLOG_STATE_COVER_NEED2;
1953 else
1954 log->l_covered_state = XLOG_STATE_COVER_NEED;
1955 break;
1956
1957 case XLOG_STATE_COVER_DONE2:
1958 if (changed == 1)
1959 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1960 else
1961 log->l_covered_state = XLOG_STATE_COVER_NEED;
1962 break;
1963
1964 default:
1965 ASSERT(0);
1966 }
1967 }
1968} /* xlog_state_clean_log */
1969
1970STATIC xfs_lsn_t
1971xlog_get_lowest_lsn(
1972 xlog_t *log)
1973{
1974 xlog_in_core_t *lsn_log;
1975 xfs_lsn_t lowest_lsn, lsn;
1976
1977 lsn_log = log->l_iclog;
1978 lowest_lsn = 0;
1979 do {
1980 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001981 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if ((lsn && !lowest_lsn) ||
1983 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
1984 lowest_lsn = lsn;
1985 }
1986 }
1987 lsn_log = lsn_log->ic_next;
1988 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01001989 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
1992
1993STATIC void
1994xlog_state_do_callback(
1995 xlog_t *log,
1996 int aborted,
1997 xlog_in_core_t *ciclog)
1998{
1999 xlog_in_core_t *iclog;
2000 xlog_in_core_t *first_iclog; /* used to know when we've
2001 * processed all iclogs once */
2002 xfs_log_callback_t *cb, *cb_next;
2003 int flushcnt = 0;
2004 xfs_lsn_t lowest_lsn;
2005 int ioerrors; /* counter: iclogs with errors */
2006 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2007 int funcdidcallbacks; /* flag: function did callbacks */
2008 int repeats; /* for issuing console warnings if
2009 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002010 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002012 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 first_iclog = iclog = log->l_iclog;
2014 ioerrors = 0;
2015 funcdidcallbacks = 0;
2016 repeats = 0;
2017
2018 do {
2019 /*
2020 * Scan all iclogs starting with the one pointed to by the
2021 * log. Reset this starting point each time the log is
2022 * unlocked (during callbacks).
2023 *
2024 * Keep looping through iclogs until one full pass is made
2025 * without running any callbacks.
2026 */
2027 first_iclog = log->l_iclog;
2028 iclog = log->l_iclog;
2029 loopdidcallbacks = 0;
2030 repeats++;
2031
2032 do {
2033
2034 /* skip all iclogs in the ACTIVE & DIRTY states */
2035 if (iclog->ic_state &
2036 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2037 iclog = iclog->ic_next;
2038 continue;
2039 }
2040
2041 /*
2042 * Between marking a filesystem SHUTDOWN and stopping
2043 * the log, we do flush all iclogs to disk (if there
2044 * wasn't a log I/O error). So, we do want things to
2045 * go smoothly in case of just a SHUTDOWN w/o a
2046 * LOG_IO_ERROR.
2047 */
2048 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2049 /*
2050 * Can only perform callbacks in order. Since
2051 * this iclog is not in the DONE_SYNC/
2052 * DO_CALLBACK state, we skip the rest and
2053 * just try to clean up. If we set our iclog
2054 * to DO_CALLBACK, we will not process it when
2055 * we retry since a previous iclog is in the
2056 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002057 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
2059 if (!(iclog->ic_state &
2060 (XLOG_STATE_DONE_SYNC |
2061 XLOG_STATE_DO_CALLBACK))) {
2062 if (ciclog && (ciclog->ic_state ==
2063 XLOG_STATE_DONE_SYNC)) {
2064 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2065 }
2066 break;
2067 }
2068 /*
2069 * We now have an iclog that is in either the
2070 * DO_CALLBACK or DONE_SYNC states. The other
2071 * states (WANT_SYNC, SYNCING, or CALLBACK were
2072 * caught by the above if and are going to
2073 * clean (i.e. we aren't doing their callbacks)
2074 * see the above if.
2075 */
2076
2077 /*
2078 * We will do one more check here to see if we
2079 * have chased our tail around.
2080 */
2081
2082 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002083 if (lowest_lsn &&
2084 XFS_LSN_CMP(lowest_lsn,
2085 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 iclog = iclog->ic_next;
2087 continue; /* Leave this iclog for
2088 * another thread */
2089 }
2090
2091 iclog->ic_state = XLOG_STATE_CALLBACK;
2092
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002093 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 /* l_last_sync_lsn field protected by
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002096 * l_grant_lock. Don't worry about iclog's lsn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 * No one else can be here except us.
2098 */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002099 spin_lock(&log->l_grant_lock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002100 ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn,
2101 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2102 log->l_last_sync_lsn =
2103 be64_to_cpu(iclog->ic_header.h_lsn);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002104 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 } else {
David Chinner114d23a2008-04-10 12:18:39 +10002107 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 ioerrors++;
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
David Chinner114d23a2008-04-10 12:18:39 +10002111 /*
2112 * Keep processing entries in the callback list until
2113 * we come around and it is empty. We need to
2114 * atomically see that the list is empty and change the
2115 * state to DIRTY so that we don't miss any more
2116 * callbacks being added.
2117 */
2118 spin_lock(&iclog->ic_callback_lock);
2119 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002120 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 iclog->ic_callback_tail = &(iclog->ic_callback);
2122 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002123 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002126 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 cb_next = cb->cb_next;
2128 cb->cb_func(cb->cb_arg, aborted);
2129 }
David Chinner114d23a2008-04-10 12:18:39 +10002130 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 cb = iclog->ic_callback;
2132 }
2133
2134 loopdidcallbacks++;
2135 funcdidcallbacks++;
2136
David Chinner114d23a2008-04-10 12:18:39 +10002137 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002138 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002139 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2141 iclog->ic_state = XLOG_STATE_DIRTY;
2142
2143 /*
2144 * Transition from DIRTY to ACTIVE if applicable.
2145 * NOP if STATE_IOERROR.
2146 */
2147 xlog_state_clean_log(log);
2148
2149 /* wake up threads waiting in xfs_log_force() */
David Chinner12017fa2008-08-13 16:34:31 +10002150 sv_broadcast(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 iclog = iclog->ic_next;
2153 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002154
2155 if (repeats > 5000) {
2156 flushcnt += repeats;
2157 repeats = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 xfs_fs_cmn_err(CE_WARN, log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002159 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002160 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162 } while (!ioerrors && loopdidcallbacks);
2163
2164 /*
2165 * make one last gasp attempt to see if iclogs are being left in
2166 * limbo..
2167 */
2168#ifdef DEBUG
2169 if (funcdidcallbacks) {
2170 first_iclog = iclog = log->l_iclog;
2171 do {
2172 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2173 /*
2174 * Terminate the loop if iclogs are found in states
2175 * which will cause other threads to clean up iclogs.
2176 *
2177 * SYNCING - i/o completion will go through logs
2178 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002179 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 * IOERROR - give up hope all ye who enter here
2181 */
2182 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2183 iclog->ic_state == XLOG_STATE_SYNCING ||
2184 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2185 iclog->ic_state == XLOG_STATE_IOERROR )
2186 break;
2187 iclog = iclog->ic_next;
2188 } while (first_iclog != iclog);
2189 }
2190#endif
2191
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002192 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2193 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002194 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002195
2196 if (wake)
2197 sv_broadcast(&log->l_flush_wait);
2198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200
2201/*
2202 * Finish transitioning this iclog to the dirty state.
2203 *
2204 * Make sure that we completely execute this routine only when this is
2205 * the last call to the iclog. There is a good chance that iclog flushes,
2206 * when we reach the end of the physical log, get turned into 2 separate
2207 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2208 * routine. By using the reference count bwritecnt, we guarantee that only
2209 * the second completion goes through.
2210 *
2211 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002212 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 */
David Chinnera8272ce2007-11-23 16:28:09 +11002214STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215xlog_state_done_syncing(
2216 xlog_in_core_t *iclog,
2217 int aborted)
2218{
2219 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002221 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2224 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002225 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2227
2228
2229 /*
2230 * If we got an error, either on the first buffer, or in the case of
2231 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2232 * and none should ever be attempted to be written to disk
2233 * again.
2234 */
2235 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2236 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002237 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return;
2239 }
2240 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2241 }
2242
2243 /*
2244 * Someone could be sleeping prior to writing out the next
2245 * iclog buffer, we wake them all, one will get to do the
2246 * I/O, the others get to wait for the result.
2247 */
David Chinner12017fa2008-08-13 16:34:31 +10002248 sv_broadcast(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002249 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2251} /* xlog_state_done_syncing */
2252
2253
2254/*
2255 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002256 * sleep. We wait on the flush queue on the head iclog as that should be
2257 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2258 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 *
2260 * The in-core logs are used in a circular fashion. They are not used
2261 * out-of-order even when an iclog past the head is free.
2262 *
2263 * return:
2264 * * log_offset where xlog_write() can start writing into the in-core
2265 * log's data space.
2266 * * in-core log pointer to which xlog_write() should write.
2267 * * boolean indicating this is a continued write to an in-core log.
2268 * If this is the last write, then the in-core log's offset field
2269 * needs to be incremented, depending on the amount of data which
2270 * is copied.
2271 */
David Chinnera8272ce2007-11-23 16:28:09 +11002272STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273xlog_state_get_iclog_space(xlog_t *log,
2274 int len,
2275 xlog_in_core_t **iclogp,
2276 xlog_ticket_t *ticket,
2277 int *continued_write,
2278 int *logoffsetp)
2279{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 int log_offset;
2281 xlog_rec_header_t *head;
2282 xlog_in_core_t *iclog;
2283 int error;
2284
2285restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002286 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002288 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return XFS_ERROR(EIO);
2290 }
2291
2292 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002293 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002295
2296 /* Wait for log writes to have flushed */
2297 sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto restart;
2299 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 head = &iclog->ic_header;
2302
David Chinner155cc6b2008-03-06 13:44:14 +11002303 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 log_offset = iclog->ic_offset;
2305
2306 /* On the 1st write to an iclog, figure out lsn. This works
2307 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2308 * committing to. If the offset is set, that's how many blocks
2309 * must be written.
2310 */
2311 if (log_offset == 0) {
2312 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002313 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002314 log->l_iclog_hsize,
2315 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002316 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2317 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002318 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 ASSERT(log->l_curr_block >= 0);
2320 }
2321
2322 /* If there is enough room to write everything, then do it. Otherwise,
2323 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2324 * bit is on, so this will get flushed out. Don't update ic_offset
2325 * until you know exactly how many bytes get copied. Therefore, wait
2326 * until later to update ic_offset.
2327 *
2328 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2329 * can fit into remaining data section.
2330 */
2331 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2332 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2333
Dave Chinner49641f12008-07-11 17:43:55 +10002334 /*
2335 * If I'm the only one writing to this iclog, sync it to disk.
2336 * We need to do an atomic compare and decrement here to avoid
2337 * racing with concurrent atomic_dec_and_lock() calls in
2338 * xlog_state_release_iclog() when there is more than one
2339 * reference to the iclog.
2340 */
2341 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2342 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002343 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002344 error = xlog_state_release_iclog(log, iclog);
2345 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002346 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002348 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
2350 goto restart;
2351 }
2352
2353 /* Do we have enough room to write the full amount in the remainder
2354 * of this iclog? Or must we continue a write on the next iclog and
2355 * mark this iclog as completely taken? In the case where we switch
2356 * iclogs (to mark it taken), this particular iclog will release/sync
2357 * to disk in xlog_write().
2358 */
2359 if (len <= iclog->ic_size - iclog->ic_offset) {
2360 *continued_write = 0;
2361 iclog->ic_offset += len;
2362 } else {
2363 *continued_write = 1;
2364 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2365 }
2366 *iclogp = iclog;
2367
2368 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002369 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 *logoffsetp = log_offset;
2372 return 0;
2373} /* xlog_state_get_iclog_space */
2374
2375/*
2376 * Atomically get the log space required for a log ticket.
2377 *
2378 * Once a ticket gets put onto the reserveq, it will only return after
2379 * the needed reservation is satisfied.
2380 */
2381STATIC int
2382xlog_grant_log_space(xlog_t *log,
2383 xlog_ticket_t *tic)
2384{
2385 int free_bytes;
2386 int need_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387#ifdef DEBUG
2388 xfs_lsn_t tail_lsn;
2389#endif
2390
2391
2392#ifdef DEBUG
2393 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2394 panic("grant Recovery problem");
2395#endif
2396
2397 /* Is there space or do we need to sleep? */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002398 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002399
2400 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 /* something is already sleeping; insert new transaction at end */
2403 if (log->l_reserve_headq) {
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002404 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002405
2406 trace_xfs_log_grant_sleep1(log, tic);
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /*
2409 * Gotta check this before going to sleep, while we're
2410 * holding the grant lock.
2411 */
2412 if (XLOG_FORCED_SHUTDOWN(log))
2413 goto error_return;
2414
2415 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002416 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 /*
2418 * If we got an error, and the filesystem is shutting down,
2419 * we'll catch it down below. So just continue...
2420 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002421 trace_xfs_log_grant_wake1(log, tic);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002422 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
2424 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2425 need_bytes = tic->t_unit_res*tic->t_ocnt;
2426 else
2427 need_bytes = tic->t_unit_res;
2428
2429redo:
2430 if (XLOG_FORCED_SHUTDOWN(log))
2431 goto error_return;
2432
2433 free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle,
2434 log->l_grant_reserve_bytes);
2435 if (free_bytes < need_bytes) {
2436 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002437 xlog_ins_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002438
2439 trace_xfs_log_grant_sleep2(log, tic);
2440
Dave Chinner9d7fef72009-04-06 18:42:59 +02002441 spin_unlock(&log->l_grant_lock);
2442 xlog_grant_push_ail(log->l_mp, need_bytes);
2443 spin_lock(&log->l_grant_lock);
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002446 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Dave Chinner9d7fef72009-04-06 18:42:59 +02002448 spin_lock(&log->l_grant_lock);
2449 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002452 trace_xfs_log_grant_wake2(log, tic);
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto redo;
2455 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002456 xlog_del_ticketq(&log->l_reserve_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 /* we've got enough space */
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002459 xlog_grant_add_space(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460#ifdef DEBUG
2461 tail_lsn = log->l_tail_lsn;
2462 /*
2463 * Check to make sure the grant write head didn't just over lap the
2464 * tail. If the cycles are the same, we can't be overlapping.
2465 * Otherwise, make sure that the cycles differ by exactly one and
2466 * check the byte count.
2467 */
2468 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2469 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2470 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2471 }
2472#endif
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002473 trace_xfs_log_grant_exit(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002475 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return 0;
2477
2478 error_return:
2479 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002480 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002481
2482 trace_xfs_log_grant_error(log, tic);
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /*
2485 * If we are failing, make sure the ticket doesn't have any
2486 * current reservations. We don't want to add this back when
2487 * the ticket/transaction gets cancelled.
2488 */
2489 tic->t_curr_res = 0;
2490 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002491 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return XFS_ERROR(EIO);
2493} /* xlog_grant_log_space */
2494
2495
2496/*
2497 * Replenish the byte reservation required by moving the grant write head.
2498 *
2499 *
2500 */
2501STATIC int
2502xlog_regrant_write_log_space(xlog_t *log,
2503 xlog_ticket_t *tic)
2504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int free_bytes, need_bytes;
2506 xlog_ticket_t *ntic;
2507#ifdef DEBUG
2508 xfs_lsn_t tail_lsn;
2509#endif
2510
2511 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002512 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517#ifdef DEBUG
2518 if (log->l_flags & XLOG_ACTIVE_RECOVERY)
2519 panic("regrant Recovery problem");
2520#endif
2521
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002522 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002523
2524 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 if (XLOG_FORCED_SHUTDOWN(log))
2527 goto error_return;
2528
2529 /* If there are other waiters on the queue then give them a
2530 * chance at logspace before us. Wake up the first waiters,
2531 * if we do not wake up all the waiters then go to sleep waiting
2532 * for more free space, otherwise try to get some space for
2533 * this transaction.
2534 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002535 need_bytes = tic->t_unit_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if ((ntic = log->l_write_headq)) {
2537 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2538 log->l_grant_write_bytes);
2539 do {
2540 ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV);
2541
2542 if (free_bytes < ntic->t_unit_res)
2543 break;
2544 free_bytes -= ntic->t_unit_res;
David Chinner12017fa2008-08-13 16:34:31 +10002545 sv_signal(&ntic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 ntic = ntic->t_next;
2547 } while (ntic != log->l_write_headq);
2548
2549 if (ntic != log->l_write_headq) {
2550 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002551 xlog_ins_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002553 trace_xfs_log_regrant_write_sleep1(log, tic);
2554
Dave Chinner9d7fef72009-04-06 18:42:59 +02002555 spin_unlock(&log->l_grant_lock);
2556 xlog_grant_push_ail(log->l_mp, need_bytes);
2557 spin_lock(&log->l_grant_lock);
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 XFS_STATS_INC(xs_sleep_logspace);
David Chinner12017fa2008-08-13 16:34:31 +10002560 sv_wait(&tic->t_wait, PINOD|PLTWAIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 &log->l_grant_lock, s);
2562
2563 /* If we're shutting down, this tic is already
2564 * off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002565 spin_lock(&log->l_grant_lock);
2566 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002569 trace_xfs_log_regrant_write_wake1(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 }
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573redo:
2574 if (XLOG_FORCED_SHUTDOWN(log))
2575 goto error_return;
2576
2577 free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
2578 log->l_grant_write_bytes);
2579 if (free_bytes < need_bytes) {
2580 if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002581 xlog_ins_ticketq(&log->l_write_headq, tic);
Dave Chinner9d7fef72009-04-06 18:42:59 +02002582 spin_unlock(&log->l_grant_lock);
2583 xlog_grant_push_ail(log->l_mp, need_bytes);
2584 spin_lock(&log->l_grant_lock);
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 XFS_STATS_INC(xs_sleep_logspace);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002587 trace_xfs_log_regrant_write_sleep2(log, tic);
2588
David Chinner12017fa2008-08-13 16:34:31 +10002589 sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 /* If we're shutting down, this tic is already off the queue */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002592 spin_lock(&log->l_grant_lock);
2593 if (XLOG_FORCED_SHUTDOWN(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002596 trace_xfs_log_regrant_write_wake2(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 goto redo;
2598 } else if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002599 xlog_del_ticketq(&log->l_write_headq, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002601 /* we've got enough space */
2602 xlog_grant_add_space_write(log, need_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603#ifdef DEBUG
2604 tail_lsn = log->l_tail_lsn;
2605 if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) {
2606 ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn));
2607 ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
2608 }
2609#endif
2610
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002611 trace_xfs_log_regrant_write_exit(log, tic);
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002614 spin_unlock(&log->l_grant_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01002615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617
2618 error_return:
2619 if (tic->t_flags & XLOG_TIC_IN_Q)
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002620 xlog_del_ticketq(&log->l_reserve_headq, tic);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002621
2622 trace_xfs_log_regrant_write_error(log, tic);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /*
2625 * If we are failing, make sure the ticket doesn't have any
2626 * current reservations. We don't want to add this back when
2627 * the ticket/transaction gets cancelled.
2628 */
2629 tic->t_curr_res = 0;
2630 tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002631 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return XFS_ERROR(EIO);
2633} /* xlog_regrant_write_log_space */
2634
2635
2636/* The first cnt-1 times through here we don't need to
2637 * move the grant write head because the permanent
2638 * reservation has reserved cnt times the unit amount.
2639 * Release part of current permanent unit reservation and
2640 * reset current reservation to be one units worth. Also
2641 * move grant reservation head forward.
2642 */
2643STATIC void
2644xlog_regrant_reserve_log_space(xlog_t *log,
2645 xlog_ticket_t *ticket)
2646{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002647 trace_xfs_log_regrant_reserve_enter(log, ticket);
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (ticket->t_cnt > 0)
2650 ticket->t_cnt--;
2651
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002652 spin_lock(&log->l_grant_lock);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002653 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002655 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002656
2657 trace_xfs_log_regrant_reserve_sub(log, ticket);
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 xlog_verify_grant_head(log, 1);
2660
2661 /* just return if we still have some of the pre-reserved space */
2662 if (ticket->t_cnt > 0) {
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002663 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return;
2665 }
2666
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002667 xlog_grant_add_space_reserve(log, ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002668
2669 trace_xfs_log_regrant_reserve_exit(log, ticket);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 xlog_verify_grant_head(log, 0);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002672 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002674 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675} /* xlog_regrant_reserve_log_space */
2676
2677
2678/*
2679 * Give back the space left from a reservation.
2680 *
2681 * All the information we need to make a correct determination of space left
2682 * is present. For non-permanent reservations, things are quite easy. The
2683 * count should have been decremented to zero. We only need to deal with the
2684 * space remaining in the current reservation part of the ticket. If the
2685 * ticket contains a permanent reservation, there may be left over space which
2686 * needs to be released. A count of N means that N-1 refills of the current
2687 * reservation can be done before we need to ask for more space. The first
2688 * one goes to fill up the first current reservation. Once we run out of
2689 * space, the count will stay at zero and the only space remaining will be
2690 * in the current reservation field.
2691 */
2692STATIC void
2693xlog_ungrant_log_space(xlog_t *log,
2694 xlog_ticket_t *ticket)
2695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (ticket->t_cnt > 0)
2697 ticket->t_cnt--;
2698
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002699 spin_lock(&log->l_grant_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002700 trace_xfs_log_ungrant_enter(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002702 xlog_grant_sub_space(log, ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002704 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 /* If this is a permanent reservation ticket, we may be able to free
2707 * up more space based on the remaining count.
2708 */
2709 if (ticket->t_cnt > 0) {
2710 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Christoph Hellwigdd954c62006-01-11 15:34:50 +11002711 xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
2713
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002714 trace_xfs_log_ungrant_exit(log, ticket);
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 xlog_verify_grant_head(log, 1);
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10002717 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 xfs_log_move_tail(log->l_mp, 1);
2719} /* xlog_ungrant_log_space */
2720
2721
2722/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 * Flush iclog to disk if this is the last reference to the given iclog and
2724 * the WANT_SYNC bit is set.
2725 *
2726 * When this function is entered, the iclog is not necessarily in the
2727 * WANT_SYNC state. It may be sitting around waiting to get filled.
2728 *
2729 *
2730 */
David Chinnera8272ce2007-11-23 16:28:09 +11002731STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002732xlog_state_release_iclog(
2733 xlog_t *log,
2734 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 int sync = 0; /* do we sync? */
2737
David Chinner155cc6b2008-03-06 13:44:14 +11002738 if (iclog->ic_state & XLOG_STATE_IOERROR)
2739 return XFS_ERROR(EIO);
2740
2741 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2742 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2743 return 0;
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002746 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return XFS_ERROR(EIO);
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2750 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2751
David Chinner155cc6b2008-03-06 13:44:14 +11002752 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002753 /* update tail before writing to iclog */
2754 xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 sync++;
2756 iclog->ic_state = XLOG_STATE_SYNCING;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002757 iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
2759 /* cycle incremented when incrementing curr_block */
2760 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002761 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 /*
2764 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002765 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2767 * this iclog has consistent data, so we ignore IOERROR
2768 * flags after this point.
2769 */
David Chinnerb5893342008-03-06 13:44:06 +11002770 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773} /* xlog_state_release_iclog */
2774
2775
2776/*
2777 * This routine will mark the current iclog in the ring as WANT_SYNC
2778 * and move the current iclog pointer to the next iclog in the ring.
2779 * When this routine is called from xlog_state_get_iclog_space(), the
2780 * exact size of the iclog has not yet been determined. All we know is
2781 * that every data block. We have run out of space in this log record.
2782 */
2783STATIC void
2784xlog_state_switch_iclogs(xlog_t *log,
2785 xlog_in_core_t *iclog,
2786 int eventual_size)
2787{
2788 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2789 if (!eventual_size)
2790 eventual_size = iclog->ic_offset;
2791 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002792 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 log->l_prev_block = log->l_curr_block;
2794 log->l_prev_cycle = log->l_curr_cycle;
2795
2796 /* roll log?: ic_offset changed later */
2797 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2798
2799 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002800 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 log->l_mp->m_sb.sb_logsunit > 1) {
2802 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2803 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2804 }
2805
2806 if (log->l_curr_block >= log->l_logBBsize) {
2807 log->l_curr_cycle++;
2808 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2809 log->l_curr_cycle++;
2810 log->l_curr_block -= log->l_logBBsize;
2811 ASSERT(log->l_curr_block >= 0);
2812 }
2813 ASSERT(iclog == log->l_iclog);
2814 log->l_iclog = iclog->ic_next;
2815} /* xlog_state_switch_iclogs */
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817/*
2818 * Write out all data in the in-core log as of this exact moment in time.
2819 *
2820 * Data may be written to the in-core log during this call. However,
2821 * we don't guarantee this data will be written out. A change from past
2822 * implementation means this routine will *not* write out zero length LRs.
2823 *
2824 * Basically, we try and perform an intelligent scan of the in-core logs.
2825 * If we determine there is no flushable data, we just return. There is no
2826 * flushable data if:
2827 *
2828 * 1. the current iclog is active and has no data; the previous iclog
2829 * is in the active or dirty state.
2830 * 2. the current iclog is drity, and the previous iclog is in the
2831 * active or dirty state.
2832 *
David Chinner12017fa2008-08-13 16:34:31 +10002833 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 *
2835 * 1. the current iclog is not in the active nor dirty state.
2836 * 2. the current iclog dirty, and the previous iclog is not in the
2837 * active nor dirty state.
2838 * 3. the current iclog is active, and there is another thread writing
2839 * to this particular iclog.
2840 * 4. a) the current iclog is active and has no other writers
2841 * b) when we return from flushing out this iclog, it is still
2842 * not in the active nor dirty state.
2843 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002844int
2845_xfs_log_force(
2846 struct xfs_mount *mp,
2847 uint flags,
2848 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002850 struct log *log = mp->m_log;
2851 struct xlog_in_core *iclog;
2852 xfs_lsn_t lsn;
2853
2854 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002856 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 iclog = log->l_iclog;
2859 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002860 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return XFS_ERROR(EIO);
2862 }
2863
2864 /* If the head iclog is not active nor dirty, we just attach
2865 * ourselves to the head and go to sleep.
2866 */
2867 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2868 iclog->ic_state == XLOG_STATE_DIRTY) {
2869 /*
2870 * If the head is dirty or (active and empty), then
2871 * we need to look at the previous iclog. If the previous
2872 * iclog is active or dirty we are done. There is nothing
2873 * to sync out. Otherwise, we attach ourselves to the
2874 * previous iclog and go to sleep.
2875 */
2876 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002877 (atomic_read(&iclog->ic_refcnt) == 0
2878 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 iclog = iclog->ic_prev;
2880 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2881 iclog->ic_state == XLOG_STATE_DIRTY)
2882 goto no_sleep;
2883 else
2884 goto maybe_sleep;
2885 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002886 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 /* We are the only one with access to this
2888 * iclog. Flush it out now. There should
2889 * be a roundoff of zero to show that someone
2890 * has already taken care of the roundoff from
2891 * the previous sync.
2892 */
David Chinner155cc6b2008-03-06 13:44:14 +11002893 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002894 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002896 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 if (xlog_state_release_iclog(log, iclog))
2899 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002900
2901 if (log_flushed)
2902 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002903 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002904 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 iclog->ic_state != XLOG_STATE_DIRTY)
2906 goto maybe_sleep;
2907 else
2908 goto no_sleep;
2909 } else {
2910 /* Someone else is writing to this iclog.
2911 * Use its call to flush out the data. However,
2912 * the other thread may not force out this LR,
2913 * so we mark it WANT_SYNC.
2914 */
2915 xlog_state_switch_iclogs(log, iclog, 0);
2916 goto maybe_sleep;
2917 }
2918 }
2919 }
2920
2921 /* By the time we come around again, the iclog could've been filled
2922 * which would give it another lsn. If we have a new lsn, just
2923 * return because the relevant data has been flushed.
2924 */
2925maybe_sleep:
2926 if (flags & XFS_LOG_SYNC) {
2927 /*
2928 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002929 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 * Then we check again after waking up, in case our
2931 * sleep was disturbed by a bad news.
2932 */
2933 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002934 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return XFS_ERROR(EIO);
2936 }
2937 XFS_STATS_INC(xs_log_force_sleep);
David Chinner12017fa2008-08-13 16:34:31 +10002938 sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 /*
2940 * No need to grab the log lock here since we're
2941 * only deciding whether or not to return EIO
2942 * and the memory read should be atomic.
2943 */
2944 if (iclog->ic_state & XLOG_STATE_IOERROR)
2945 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002946 if (log_flushed)
2947 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 } else {
2949
2950no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002951 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
2953 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002954}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002957 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2958 * about errors or whether the log was flushed or not. This is the normal
2959 * interface to use when trying to unpin items or move the log forward.
2960 */
2961void
2962xfs_log_force(
2963 xfs_mount_t *mp,
2964 uint flags)
2965{
2966 int error;
2967
2968 error = _xfs_log_force(mp, flags, NULL);
2969 if (error) {
2970 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
2971 "error %d returned.", error);
2972 }
2973}
2974
2975/*
2976 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 *
2978 * Find in-core log with lsn.
2979 * If it is in the DIRTY state, just return.
2980 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2981 * state and go to sleep or return.
2982 * If it is in any other state, go to sleep or return.
2983 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002984 * Synchronous forces are implemented with a signal variable. All callers
2985 * to force a given lsn to disk will wait on a the sv attached to the
2986 * specific in-core log. When given in-core log finally completes its
2987 * write to disk, that thread will wake up all threads waiting on the
2988 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002990int
2991_xfs_log_force_lsn(
2992 struct xfs_mount *mp,
2993 xfs_lsn_t lsn,
2994 uint flags,
2995 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002997 struct log *log = mp->m_log;
2998 struct xlog_in_core *iclog;
2999 int already_slept = 0;
3000
3001 ASSERT(lsn != 0);
3002
3003 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003006 spin_lock(&log->l_icloglock);
3007 iclog = log->l_iclog;
3008 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003009 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003010 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003013 do {
3014 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3015 iclog = iclog->ic_next;
3016 continue;
3017 }
3018
3019 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3020 spin_unlock(&log->l_icloglock);
3021 return 0;
3022 }
3023
3024 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3025 /*
3026 * We sleep here if we haven't already slept (e.g.
3027 * this is the first time we've looked at the correct
3028 * iclog buf) and the buffer before us is going to
3029 * be sync'ed. The reason for this is that if we
3030 * are doing sync transactions here, by waiting for
3031 * the previous I/O to complete, we can allow a few
3032 * more transactions into this iclog before we close
3033 * it down.
3034 *
3035 * Otherwise, we mark the buffer WANT_SYNC, and bump
3036 * up the refcnt so we can release the log (which
3037 * drops the ref count). The state switch keeps new
3038 * transaction commits from using this buffer. When
3039 * the current commits finish writing into the buffer,
3040 * the refcount will drop to zero and the buffer will
3041 * go out then.
3042 */
3043 if (!already_slept &&
3044 (iclog->ic_prev->ic_state &
3045 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3046 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3047
3048 XFS_STATS_INC(xs_log_force_sleep);
3049
3050 sv_wait(&iclog->ic_prev->ic_write_wait,
3051 PSWP, &log->l_icloglock, s);
3052 if (log_flushed)
3053 *log_flushed = 1;
3054 already_slept = 1;
3055 goto try_again;
3056 }
David Chinner155cc6b2008-03-06 13:44:14 +11003057 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003059 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (xlog_state_release_iclog(log, iclog))
3061 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003062 if (log_flushed)
3063 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003064 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003067 if ((flags & XFS_LOG_SYNC) && /* sleep */
3068 !(iclog->ic_state &
3069 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3070 /*
3071 * Don't wait on completion if we know that we've
3072 * gotten a log write error.
3073 */
3074 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3075 spin_unlock(&log->l_icloglock);
3076 return XFS_ERROR(EIO);
3077 }
3078 XFS_STATS_INC(xs_log_force_sleep);
3079 sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
3080 /*
3081 * No need to grab the log lock here since we're
3082 * only deciding whether or not to return EIO
3083 * and the memory read should be atomic.
3084 */
3085 if (iclog->ic_state & XLOG_STATE_IOERROR)
3086 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003088 if (log_flushed)
3089 *log_flushed = 1;
3090 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003091 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003093
3094 return 0;
3095 } while (iclog != log->l_iclog);
3096
3097 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003101/*
3102 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3103 * about errors or whether the log was flushed or not. This is the normal
3104 * interface to use when trying to unpin items or move the log forward.
3105 */
3106void
3107xfs_log_force_lsn(
3108 xfs_mount_t *mp,
3109 xfs_lsn_t lsn,
3110 uint flags)
3111{
3112 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003114 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
3115 if (error) {
3116 xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
3117 "error %d returned.", error);
3118 }
3119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121/*
3122 * Called when we want to mark the current iclog as being ready to sync to
3123 * disk.
3124 */
David Chinnera8272ce2007-11-23 16:28:09 +11003125STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3127{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003128 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3131 xlog_state_switch_iclogs(log, iclog, 0);
3132 } else {
3133 ASSERT(iclog->ic_state &
3134 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3135 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138
3139/*****************************************************************************
3140 *
3141 * TICKET functions
3142 *
3143 *****************************************************************************
3144 */
3145
3146/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003147 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003149void
3150xfs_log_ticket_put(
3151 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003153 ASSERT(atomic_read(&ticket->t_ref) > 0);
3154 if (atomic_dec_and_test(&ticket->t_ref)) {
3155 sv_destroy(&ticket->t_wait);
3156 kmem_zone_free(xfs_log_ticket_zone, ticket);
3157 }
3158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003160xlog_ticket_t *
3161xfs_log_ticket_get(
3162 xlog_ticket_t *ticket)
3163{
3164 ASSERT(atomic_read(&ticket->t_ref) > 0);
3165 atomic_inc(&ticket->t_ref);
3166 return ticket;
3167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003170 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 */
David Chinnera8272ce2007-11-23 16:28:09 +11003172STATIC xlog_ticket_t *
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003173xlog_ticket_alloc(xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 int unit_bytes,
3175 int cnt,
3176 char client,
3177 uint xflags)
3178{
3179 xlog_ticket_t *tic;
3180 uint num_headers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
David Chinnereb01c9c2008-04-10 12:18:46 +10003182 tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
3183 if (!tic)
3184 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 /*
3187 * Permanent reservations have up to 'cnt'-1 active log operations
3188 * in the log. A unit in this case is the amount of space for one
3189 * of these log operations. Normal reservations have a cnt of 1
3190 * and their unit amount is the total amount of space required.
3191 *
3192 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003193 * which occupy space in the on-disk log.
3194 *
3195 * Normal form of a transaction is:
3196 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3197 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3198 *
3199 * We need to account for all the leadup data and trailer data
3200 * around the transaction data.
3201 * And then we need to account for the worst case in terms of using
3202 * more space.
3203 * The worst case will happen if:
3204 * - the placement of the transaction happens to be such that the
3205 * roundoff is at its maximum
3206 * - the transaction data is synced before the commit record is synced
3207 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3208 * Therefore the commit record is in its own Log Record.
3209 * This can happen as the commit record is called with its
3210 * own region to xlog_write().
3211 * This then means that in the worst case, roundoff can happen for
3212 * the commit-rec as well.
3213 * The commit-rec is smaller than padding in this scenario and so it is
3214 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 */
3216
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003217 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003219 unit_bytes += sizeof(xfs_trans_header_t);
3220
3221 /* for start-rec */
3222 unit_bytes += sizeof(xlog_op_header_t);
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 /* for LR headers */
3225 num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
3226 unit_bytes += log->l_iclog_hsize * num_headers;
3227
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003228 /* for commit-rec LR header - note: padding will subsume the ophdr */
3229 unit_bytes += log->l_iclog_hsize;
3230
3231 /* for split-recs - ophdrs added when data split over LRs */
3232 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3233
3234 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003235 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003236 log->l_mp->m_sb.sb_logsunit > 1) {
3237 /* log su roundoff */
3238 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3239 } else {
3240 /* BB roundoff */
3241 unit_bytes += 2*BBSIZE;
3242 }
3243
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003244 atomic_set(&tic->t_ref, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 tic->t_unit_res = unit_bytes;
3246 tic->t_curr_res = unit_bytes;
3247 tic->t_cnt = cnt;
3248 tic->t_ocnt = cnt;
3249 tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
3250 tic->t_clientid = client;
3251 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003252 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (xflags & XFS_LOG_PERM_RESERV)
3254 tic->t_flags |= XLOG_TIC_PERM_RESERV;
David Chinner12017fa2008-08-13 16:34:31 +10003255 sv_init(&(tic->t_wait), SV_DEFAULT, "logtick");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Christoph Hellwig0adba532007-08-30 17:21:46 +10003257 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262
3263/******************************************************************************
3264 *
3265 * Log debug routines
3266 *
3267 ******************************************************************************
3268 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003269#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270/*
3271 * Make sure that the destination ptr is within the valid data region of
3272 * one of the iclogs. This uses backup pointers stored in a different
3273 * part of the log in case we trash the log structure.
3274 */
3275void
3276xlog_verify_dest_ptr(xlog_t *log,
3277 __psint_t ptr)
3278{
3279 int i;
3280 int good_ptr = 0;
3281
3282 for (i=0; i < log->l_iclog_bufs; i++) {
3283 if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
3284 ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
3285 good_ptr++;
3286 }
3287 if (! good_ptr)
3288 xlog_panic("xlog_verify_dest_ptr: invalid ptr");
3289} /* xlog_verify_dest_ptr */
3290
3291STATIC void
3292xlog_verify_grant_head(xlog_t *log, int equals)
3293{
3294 if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) {
3295 if (equals)
3296 ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes);
3297 else
3298 ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes);
3299 } else {
3300 ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle);
3301 ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes);
3302 }
3303} /* xlog_verify_grant_head */
3304
3305/* check if it will fit */
3306STATIC void
3307xlog_verify_tail_lsn(xlog_t *log,
3308 xlog_in_core_t *iclog,
3309 xfs_lsn_t tail_lsn)
3310{
3311 int blocks;
3312
3313 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3314 blocks =
3315 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3316 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3317 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3318 } else {
3319 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3320
3321 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3322 xlog_panic("xlog_verify_tail_lsn: tail wrapped");
3323
3324 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3325 if (blocks < BTOBB(iclog->ic_offset) + 1)
3326 xlog_panic("xlog_verify_tail_lsn: ran out of log space");
3327 }
3328} /* xlog_verify_tail_lsn */
3329
3330/*
3331 * Perform a number of checks on the iclog before writing to disk.
3332 *
3333 * 1. Make sure the iclogs are still circular
3334 * 2. Make sure we have a good magic number
3335 * 3. Make sure we don't have magic numbers in the data
3336 * 4. Check fields of each log operation header for:
3337 * A. Valid client identifier
3338 * B. tid ptr value falls in valid ptr space (user space code)
3339 * C. Length in log record header is correct according to the
3340 * individual operation headers within record.
3341 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3342 * log, check the preceding blocks of the physical log to make sure all
3343 * the cycle numbers agree with the current cycle number.
3344 */
3345STATIC void
3346xlog_verify_iclog(xlog_t *log,
3347 xlog_in_core_t *iclog,
3348 int count,
3349 boolean_t syncing)
3350{
3351 xlog_op_header_t *ophead;
3352 xlog_in_core_t *icptr;
3353 xlog_in_core_2_t *xhdr;
3354 xfs_caddr_t ptr;
3355 xfs_caddr_t base_ptr;
3356 __psint_t field_offset;
3357 __uint8_t clientid;
3358 int len, i, j, k, op_len;
3359 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
3361 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003362 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 icptr = log->l_iclog;
3364 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003365 if (icptr == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 xlog_panic("xlog_verify_iclog: invalid ptr");
3367 icptr = icptr->ic_next;
3368 }
3369 if (icptr != log->l_iclog)
3370 xlog_panic("xlog_verify_iclog: corrupt iclog ring");
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003371 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 /* check log magic numbers */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003374 if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 xlog_panic("xlog_verify_iclog: invalid magic num");
3376
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003377 ptr = (xfs_caddr_t) &iclog->ic_header;
3378 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 ptr += BBSIZE) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003380 if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 xlog_panic("xlog_verify_iclog: unexpected magic num");
3382 }
3383
3384 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003385 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 ptr = iclog->ic_datap;
3387 base_ptr = ptr;
3388 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003389 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 for (i = 0; i < len; i++) {
3391 ophead = (xlog_op_header_t *)ptr;
3392
3393 /* clientid is only 1 byte */
3394 field_offset = (__psint_t)
3395 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3396 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3397 clientid = ophead->oh_clientid;
3398 } else {
3399 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3400 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3401 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3402 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003403 clientid = xlog_get_client_id(
3404 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003406 clientid = xlog_get_client_id(
3407 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409 }
3410 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Christoph Hellwigda1650a2005-11-02 10:21:35 +11003411 cmn_err(CE_WARN, "xlog_verify_iclog: "
3412 "invalid clientid %d op 0x%p offset 0x%lx",
3413 clientid, ophead, (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 /* check length */
3416 field_offset = (__psint_t)
3417 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3418 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003419 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 } else {
3421 idx = BTOBBT((__psint_t)&ophead->oh_len -
3422 (__psint_t)iclog->ic_datap);
3423 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3424 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3425 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003426 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003428 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
3430 }
3431 ptr += sizeof(xlog_op_header_t) + op_len;
3432 }
3433} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003434#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003437 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 */
3439STATIC int
3440xlog_state_ioerror(
3441 xlog_t *log)
3442{
3443 xlog_in_core_t *iclog, *ic;
3444
3445 iclog = log->l_iclog;
3446 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3447 /*
3448 * Mark all the incore logs IOERROR.
3449 * From now on, no log flushes will result.
3450 */
3451 ic = iclog;
3452 do {
3453 ic->ic_state = XLOG_STATE_IOERROR;
3454 ic = ic->ic_next;
3455 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458 /*
3459 * Return non-zero, if state transition has already happened.
3460 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003461 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
3464/*
3465 * This is called from xfs_force_shutdown, when we're forcibly
3466 * shutting down the filesystem, typically because of an IO error.
3467 * Our main objectives here are to make sure that:
3468 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3469 * parties to find out, 'atomically'.
3470 * b. those who're sleeping on log reservations, pinned objects and
3471 * other resources get woken up, and be told the bad news.
3472 * c. nothing new gets queued up after (a) and (b) are done.
3473 * d. if !logerror, flush the iclogs to disk, then seal them off
3474 * for business.
3475 */
3476int
3477xfs_log_force_umount(
3478 struct xfs_mount *mp,
3479 int logerror)
3480{
3481 xlog_ticket_t *tic;
3482 xlog_t *log;
3483 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 log = mp->m_log;
3486
3487 /*
3488 * If this happens during log recovery, don't worry about
3489 * locking; the log isn't open for business yet.
3490 */
3491 if (!log ||
3492 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3493 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003494 if (mp->m_sb_bp)
3495 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498
3499 /*
3500 * Somebody could've already done the hard work for us.
3501 * No need to get locks for this.
3502 */
3503 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3504 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003505 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507 retval = 0;
3508 /*
3509 * We must hold both the GRANT lock and the LOG lock,
3510 * before we mark the filesystem SHUTDOWN and wake
3511 * everybody up to tell the bad news.
3512 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003513 spin_lock(&log->l_icloglock);
David Chinner6b1d1a72008-04-10 12:19:02 +10003514 spin_lock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003516 if (mp->m_sb_bp)
3517 XFS_BUF_DONE(mp->m_sb_bp);
3518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 /*
3520 * This flag is sort of redundant because of the mount flag, but
3521 * it's good to maintain the separation between the log and the rest
3522 * of XFS.
3523 */
3524 log->l_flags |= XLOG_IO_ERROR;
3525
3526 /*
3527 * If we hit a log error, we want to mark all the iclogs IOERROR
3528 * while we're still holding the loglock.
3529 */
3530 if (logerror)
3531 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003532 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 /*
3535 * We don't want anybody waiting for log reservations
3536 * after this. That means we have to wake up everybody
3537 * queued up on reserve_headq as well as write_headq.
3538 * In addition, we make sure in xlog_{re}grant_log_space
3539 * that we don't enqueue anything once the SHUTDOWN flag
3540 * is set, and this action is protected by the GRANTLOCK.
3541 */
3542 if ((tic = log->l_reserve_headq)) {
3543 do {
David Chinner12017fa2008-08-13 16:34:31 +10003544 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 tic = tic->t_next;
3546 } while (tic != log->l_reserve_headq);
3547 }
3548
3549 if ((tic = log->l_write_headq)) {
3550 do {
David Chinner12017fa2008-08-13 16:34:31 +10003551 sv_signal(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 tic = tic->t_next;
3553 } while (tic != log->l_write_headq);
3554 }
Eric Sandeenc8b5ea22007-10-11 17:37:31 +10003555 spin_unlock(&log->l_grant_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003557 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 ASSERT(!logerror);
3559 /*
3560 * Force the incore logs to disk before shutting the
3561 * log down completely.
3562 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003563 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3564
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003565 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003567 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
3569 /*
3570 * Wake up everybody waiting on xfs_log_force.
3571 * Callback all log item committed functions as if the
3572 * log writes were completed.
3573 */
3574 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3575
3576#ifdef XFSERRORDEBUG
3577 {
3578 xlog_in_core_t *iclog;
3579
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003580 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 iclog = log->l_iclog;
3582 do {
3583 ASSERT(iclog->ic_callback == 0);
3584 iclog = iclog->ic_next;
3585 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003586 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
3588#endif
3589 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003590 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003593STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594xlog_iclogs_empty(xlog_t *log)
3595{
3596 xlog_in_core_t *iclog;
3597
3598 iclog = log->l_iclog;
3599 do {
3600 /* endianness does not matter here, zero is zero in
3601 * any language.
3602 */
3603 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 iclog = iclog->ic_next;
3606 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003607 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}