blob: 02a35fba5eaee2974b6f89595e7a29a8fdd80382 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
28#include "xfs_error.h"
29#include "xfs_log_priv.h"
30#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Chinnereb01c9c2008-04-10 12:18:46 +100041kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Local miscellaneous function prototypes */
Dave Chinner55b66332010-03-23 11:43:17 +110044STATIC int xlog_commit_record(struct log *log, struct xlog_ticket *ticket,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 xlog_in_core_t **, xfs_lsn_t *);
46STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
47 xfs_buftarg_t *log_target,
48 xfs_daddr_t blk_offset,
49 int num_bblks);
Dave Chinnerc8a09ff2010-12-04 00:02:40 +110050STATIC int xlog_space_left(struct log *log, atomic64_t *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100052STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* local state machine functions */
55STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
56STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
57STATIC int xlog_state_get_iclog_space(xlog_t *log,
58 int len,
59 xlog_in_core_t **iclog,
60 xlog_ticket_t *ticket,
61 int *continued_write,
62 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xlog_state_release_iclog(xlog_t *log,
64 xlog_in_core_t *iclog);
65STATIC void xlog_state_switch_iclogs(xlog_t *log,
66 xlog_in_core_t *iclog,
67 int eventual_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
69
70/* local functions to manipulate grant head */
71STATIC int xlog_grant_log_space(xlog_t *log,
72 xlog_ticket_t *xtic);
Dave Chinner2ced19c2010-12-21 12:09:20 +110073STATIC void xlog_grant_push_ail(struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int need_bytes);
75STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
76 xlog_ticket_t *ticket);
77STATIC int xlog_regrant_write_log_space(xlog_t *log,
78 xlog_ticket_t *ticket);
79STATIC void xlog_ungrant_log_space(xlog_t *log,
80 xlog_ticket_t *ticket);
81
Nathan Scottcfcbbbd2005-11-02 15:12:04 +110082#if defined(DEBUG)
Christoph Hellwige6b1f272010-03-23 11:47:38 +110083STATIC void xlog_verify_dest_ptr(xlog_t *log, char *ptr);
Dave Chinner3f336c62010-12-21 12:02:52 +110084STATIC void xlog_verify_grant_tail(struct log *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
86 int count, boolean_t syncing);
87STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
88 xfs_lsn_t tail_lsn);
89#else
90#define xlog_verify_dest_ptr(a,b)
Dave Chinner3f336c62010-12-21 12:02:52 +110091#define xlog_verify_grant_tail(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define xlog_verify_iclog(a,b,c,d)
93#define xlog_verify_tail_lsn(a,b,c)
94#endif
95
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100096STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Christoph Hellwigdd954c62006-01-11 15:34:50 +110098static void
Dave Chinner663e4962010-12-21 12:06:05 +110099xlog_grant_sub_space(
100 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100101 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100102 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100103{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100104 int64_t head_val = atomic64_read(head);
105 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100106
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100107 do {
108 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100109
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100110 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100111
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100112 space -= bytes;
113 if (space < 0) {
114 space += log->l_logsize;
115 cycle--;
116 }
117
118 old = head_val;
119 new = xlog_assign_grant_head_val(cycle, space);
120 head_val = atomic64_cmpxchg(head, old, new);
121 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100122}
123
124static void
Dave Chinner663e4962010-12-21 12:06:05 +1100125xlog_grant_add_space(
126 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100127 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100128 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100129{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100130 int64_t head_val = atomic64_read(head);
131 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100132
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100133 do {
134 int tmp;
135 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100136
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100137 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100138
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100139 tmp = log->l_logsize - space;
140 if (tmp > bytes)
141 space += bytes;
142 else {
143 space = bytes - tmp;
144 cycle++;
145 }
146
147 old = head_val;
148 new = xlog_assign_grant_head_val(cycle, space);
149 head_val = atomic64_cmpxchg(head, old, new);
150 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100151}
Dave Chinnera69ed032010-12-21 12:08:20 +1100152
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000153STATIC bool
154xlog_reserveq_wake(
155 struct log *log,
156 int *free_bytes)
157{
158 struct xlog_ticket *tic;
159 int need_bytes;
160
161 list_for_each_entry(tic, &log->l_reserveq, t_queue) {
162 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
163 need_bytes = tic->t_unit_res * tic->t_cnt;
164 else
165 need_bytes = tic->t_unit_res;
166
167 if (*free_bytes < need_bytes)
168 return false;
169 *free_bytes -= need_bytes;
170
171 trace_xfs_log_grant_wake_up(log, tic);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000172 wake_up_process(tic->t_task);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000173 }
174
175 return true;
176}
177
178STATIC bool
179xlog_writeq_wake(
180 struct log *log,
181 int *free_bytes)
182{
183 struct xlog_ticket *tic;
184 int need_bytes;
185
186 list_for_each_entry(tic, &log->l_writeq, t_queue) {
187 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
188
189 need_bytes = tic->t_unit_res;
190
191 if (*free_bytes < need_bytes)
192 return false;
193 *free_bytes -= need_bytes;
194
195 trace_xfs_log_regrant_write_wake_up(log, tic);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000196 wake_up_process(tic->t_task);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000197 }
198
199 return true;
200}
201
202STATIC int
203xlog_reserveq_wait(
204 struct log *log,
205 struct xlog_ticket *tic,
206 int need_bytes)
207{
208 list_add_tail(&tic->t_queue, &log->l_reserveq);
209
210 do {
211 if (XLOG_FORCED_SHUTDOWN(log))
212 goto shutdown;
213 xlog_grant_push_ail(log, need_bytes);
214
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000215 __set_current_state(TASK_UNINTERRUPTIBLE);
216 spin_unlock(&log->l_grant_reserve_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000217
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000218 XFS_STATS_INC(xs_sleep_logspace);
219
220 trace_xfs_log_grant_sleep(log, tic);
221 schedule();
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000222 trace_xfs_log_grant_wake(log, tic);
223
224 spin_lock(&log->l_grant_reserve_lock);
225 if (XLOG_FORCED_SHUTDOWN(log))
226 goto shutdown;
227 } while (xlog_space_left(log, &log->l_grant_reserve_head) < need_bytes);
228
229 list_del_init(&tic->t_queue);
230 return 0;
231shutdown:
232 list_del_init(&tic->t_queue);
233 return XFS_ERROR(EIO);
234}
235
236STATIC int
237xlog_writeq_wait(
238 struct log *log,
239 struct xlog_ticket *tic,
240 int need_bytes)
241{
242 list_add_tail(&tic->t_queue, &log->l_writeq);
243
244 do {
245 if (XLOG_FORCED_SHUTDOWN(log))
246 goto shutdown;
247 xlog_grant_push_ail(log, need_bytes);
248
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000249 __set_current_state(TASK_UNINTERRUPTIBLE);
250 spin_unlock(&log->l_grant_write_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000251
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000252 XFS_STATS_INC(xs_sleep_logspace);
253
254 trace_xfs_log_regrant_write_sleep(log, tic);
255 schedule();
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000256 trace_xfs_log_regrant_write_wake(log, tic);
257
258 spin_lock(&log->l_grant_write_lock);
259 if (XLOG_FORCED_SHUTDOWN(log))
260 goto shutdown;
261 } while (xlog_space_left(log, &log->l_grant_write_head) < need_bytes);
262
263 list_del_init(&tic->t_queue);
264 return 0;
265shutdown:
266 list_del_init(&tic->t_queue);
267 return XFS_ERROR(EIO);
268}
269
Christoph Hellwig0adba532007-08-30 17:21:46 +1000270static void
271xlog_tic_reset_res(xlog_ticket_t *tic)
272{
273 tic->t_res_num = 0;
274 tic->t_res_arr_sum = 0;
275 tic->t_res_num_ophdrs = 0;
276}
277
278static void
279xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
280{
281 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
282 /* add to overflow and start again */
283 tic->t_res_o_flow += tic->t_res_arr_sum;
284 tic->t_res_num = 0;
285 tic->t_res_arr_sum = 0;
286 }
287
288 tic->t_res_arr[tic->t_res_num].r_len = len;
289 tic->t_res_arr[tic->t_res_num].r_type = type;
290 tic->t_res_arr_sum += len;
291 tic->t_res_num++;
292}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
295 * NOTES:
296 *
297 * 1. currblock field gets updated at startup and after in-core logs
298 * marked as with WANT_SYNC.
299 */
300
301/*
302 * This routine is called when a user of a log manager ticket is done with
303 * the reservation. If the ticket was ever used, then a commit record for
304 * the associated transaction is written out as a log operation header with
305 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
306 * a given ticket. If the ticket was one with a permanent reservation, then
307 * a few operations are done differently. Permanent reservation tickets by
308 * default don't release the reservation. They just commit the current
309 * transaction with the belief that the reservation is still needed. A flag
310 * must be passed in before permanent reservations are actually released.
311 * When these type of tickets are not released, they need to be set into
312 * the inited state again. By doing this, a start record will be written
313 * out when the next write occurs.
314 */
315xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000316xfs_log_done(
317 struct xfs_mount *mp,
318 struct xlog_ticket *ticket,
319 struct xlog_in_core **iclog,
320 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000322 struct log *log = mp->m_log;
323 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (XLOG_FORCED_SHUTDOWN(log) ||
326 /*
327 * If nothing was ever written, don't write out commit record.
328 * If we get an error, just continue and give back the log ticket.
329 */
330 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100331 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 lsn = (xfs_lsn_t) -1;
333 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
334 flags |= XFS_LOG_REL_PERM_RESERV;
335 }
336 }
337
338
339 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
340 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000341 trace_xfs_log_done_nonperm(log, ticket);
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000344 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * request has been made to release a permanent reservation.
346 */
347 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100348 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000350 trace_xfs_log_done_perm(log, ticket);
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000353 /* If this ticket was a permanent reservation and we aren't
354 * trying to release it, reset the inited flags; so next time
355 * we write, a start record will be written out.
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000361}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/*
364 * Attaches a new iclog I/O completion callback routine during
365 * transaction commit. If the log is in error state, a non-zero
366 * return code is handed back and the caller is responsible for
367 * executing the callback at an appropriate time.
368 */
369int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000370xfs_log_notify(
371 struct xfs_mount *mp,
372 struct xlog_in_core *iclog,
373 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000375 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David Chinner114d23a2008-04-10 12:18:39 +1000377 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
379 if (!abortflg) {
380 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
381 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
382 cb->cb_next = NULL;
383 *(iclog->ic_callback_tail) = cb;
384 iclog->ic_callback_tail = &(cb->cb_next);
385 }
David Chinner114d23a2008-04-10 12:18:39 +1000386 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000391xfs_log_release_iclog(
392 struct xfs_mount *mp,
393 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000395 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000396 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100397 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 return 0;
401}
402
403/*
404 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
405 * to the reservation.
406 * 2. Potentially, push buffers at tail of log to disk.
407 *
408 * Each reservation is going to reserve extra space for a log record header.
409 * When writes happen to the on-disk log, we don't subtract the length of the
410 * log record header from any reservation. By wasting space in each
411 * reservation, we prevent over allocation problems.
412 */
413int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000414xfs_log_reserve(
415 struct xfs_mount *mp,
416 int unit_bytes,
417 int cnt,
418 struct xlog_ticket **ticket,
419 __uint8_t client,
420 uint flags,
421 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000423 struct log *log = mp->m_log;
424 struct xlog_ticket *internal_ticket;
425 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (XLOG_FORCED_SHUTDOWN(log))
430 return XFS_ERROR(EIO);
431
432 XFS_STATS_INC(xs_try_logspace);
433
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (*ticket != NULL) {
436 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000437 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000438
Dave Chinner524ee362010-05-07 11:05:05 +1000439 /*
440 * this is a new transaction on the ticket, so we need to
441 * change the transaction ID so that the next transaction has a
442 * different TID in the log. Just add one to the existing tid
443 * so that we can see chains of rolling transactions in the log
444 * easily.
445 */
446 internal_ticket->t_tid++;
447
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000448 trace_xfs_log_reserve(log, internal_ticket);
449
Dave Chinner2ced19c2010-12-21 12:09:20 +1100450 xlog_grant_push_ail(log, internal_ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 retval = xlog_regrant_write_log_space(log, internal_ticket);
452 } else {
453 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100454 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Dave Chinner3383ca52010-05-07 11:04:17 +1000455 client, flags,
456 KM_SLEEP|KM_MAYFAIL);
David Chinnereb01c9c2008-04-10 12:18:46 +1000457 if (!internal_ticket)
458 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000459 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000461
462 trace_xfs_log_reserve(log, internal_ticket);
463
Dave Chinner2ced19c2010-12-21 12:09:20 +1100464 xlog_grant_push_ail(log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 (internal_ticket->t_unit_res *
466 internal_ticket->t_cnt));
467 retval = xlog_grant_log_space(log, internal_ticket);
468 }
469
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000470 if (unlikely(retval)) {
471 /*
472 * If we are failing, make sure the ticket doesn't have any
473 * current reservations. We don't want to add this back
474 * when the ticket/ transaction gets cancelled.
475 */
476 internal_ticket->t_curr_res = 0;
477 /* ungrant will give back unit_res * t_cnt. */
478 internal_ticket->t_cnt = 0;
479 }
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return retval;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484
485/*
486 * Mount a log filesystem
487 *
488 * mp - ubiquitous xfs mount point structure
489 * log_target - buftarg of on-disk log device
490 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
491 * num_bblocks - Number of BBSIZE blocks in on-disk log
492 *
493 * Return error or zero.
494 */
495int
David Chinner249a8c12008-02-05 12:13:32 +1100496xfs_log_mount(
497 xfs_mount_t *mp,
498 xfs_buftarg_t *log_target,
499 xfs_daddr_t blk_offset,
500 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
David Chinner249a8c12008-02-05 12:13:32 +1100502 int error;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100505 xfs_notice(mp, "Mounting Filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100507 xfs_notice(mp,
508"Mounting filesystem in no-recovery mode. Filesystem will be inconsistent.");
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000509 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200513 if (IS_ERR(mp->m_log)) {
514 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100515 goto out;
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
David Chinner249a8c12008-02-05 12:13:32 +1100519 * Initialize the AIL now we have a log.
520 */
David Chinner249a8c12008-02-05 12:13:32 +1100521 error = xfs_trans_ail_init(mp);
522 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100523 xfs_warn(mp, "AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100524 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100525 }
David Chinnera9c21c12008-10-30 17:39:35 +1100526 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100527
528 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * skip log recovery on a norecovery mount. pretend it all
530 * just worked.
531 */
532 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100533 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000536 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Eric Sandeen65be6052006-01-11 15:34:19 +1100538 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000541 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100543 xfs_warn(mp, "log mount/recovery failed: error %d",
544 error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100545 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 }
548
549 /* Normal transactions can now occur */
550 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
551
Dave Chinner71e330b2010-05-21 14:37:18 +1000552 /*
553 * Now the log has been fully initialised and we know were our
554 * space grant counters are, we can initialise the permanent ticket
555 * needed for delayed logging to work.
556 */
557 xlog_cil_init_post_recovery(mp->m_log);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100560
561out_destroy_ail:
562 xfs_trans_ail_destroy(mp);
563out_free_log:
564 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100565out:
David Chinner249a8c12008-02-05 12:13:32 +1100566 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/*
570 * Finish the recovery of the file system. This is separate from
571 * the xfs_log_mount() call, because it depends on the code in
572 * xfs_mountfs() to read in the root and real-time bitmap inodes
573 * between calling xfs_log_mount() and here.
574 *
575 * mp - ubiquitous xfs mount point structure
576 */
577int
Christoph Hellwig42490232008-08-13 16:49:32 +1000578xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 int error;
581
582 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000583 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 else {
585 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000586 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
589 return error;
590}
591
592/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * Final log writes as part of unmount.
594 *
595 * Mark the filesystem clean as unmount happens. Note that during relocation
596 * this routine needs to be executed as part of source-bag while the
597 * deallocation must not be done until source-end.
598 */
599
600/*
601 * Unmount record used to have a string "Unmount filesystem--" in the
602 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
603 * We just write the magic number now since that particular field isn't
604 * currently architecture converted and "nUmount" is a bit foo.
605 * As far as I know, there weren't any dependencies on the old behaviour.
606 */
607
608int
609xfs_log_unmount_write(xfs_mount_t *mp)
610{
611 xlog_t *log = mp->m_log;
612 xlog_in_core_t *iclog;
613#ifdef DEBUG
614 xlog_in_core_t *first_iclog;
615#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000616 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xfs_lsn_t lsn;
618 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * Don't write out unmount record on read-only mounts.
622 * Or, if we are doing a forced umount (typically because of IO errors).
623 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000624 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 0;
626
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000627 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000628 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630#ifdef DEBUG
631 first_iclog = iclog = log->l_iclog;
632 do {
633 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
634 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
635 ASSERT(iclog->ic_offset == 0);
636 }
637 iclog = iclog->ic_next;
638 } while (iclog != first_iclog);
639#endif
640 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000641 error = xfs_log_reserve(mp, 600, 1, &tic,
642 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100644 /* the data section must be 32 bit size aligned */
645 struct {
646 __uint16_t magic;
647 __uint16_t pad1;
648 __uint32_t pad2; /* may as well make it 64 bits */
649 } magic = {
650 .magic = XLOG_UNMOUNT_TYPE,
651 };
652 struct xfs_log_iovec reg = {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000653 .i_addr = &magic,
Dave Chinner55b66332010-03-23 11:43:17 +1100654 .i_len = sizeof(magic),
655 .i_type = XLOG_REG_TYPE_UNMOUNT,
656 };
657 struct xfs_log_vec vec = {
658 .lv_niovecs = 1,
659 .lv_iovecp = &reg,
660 };
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100663 tic->t_flags = 0;
664 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 NULL, XLOG_UNMOUNT_TRANS);
666 /*
667 * At this point, we're umounting anyway,
668 * so there's no point in transitioning log state
669 * to IOERROR. Just continue...
670 */
671 }
672
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100673 if (error)
674 xfs_alert(mp, "%s: unmount record failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000677 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100679 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100681 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000682 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000684 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
686 iclog->ic_state == XLOG_STATE_DIRTY)) {
687 if (!XLOG_FORCED_SHUTDOWN(log)) {
Dave Chinnereb40a872010-12-21 12:09:01 +1100688 xlog_wait(&iclog->ic_force_wait,
689 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000691 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000694 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000696 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000697 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000698 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100699 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 } else {
702 /*
703 * We're already in forced_shutdown mode, couldn't
704 * even attempt to write out the unmount transaction.
705 *
706 * Go through the motions of sync'ing and releasing
707 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000708 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * be in progress. Do this as a separate section of
710 * code so we'll know if we ever get stuck here that
711 * we're in this odd situation of trying to unmount
712 * a file system that went into forced_shutdown as
713 * the result of an unmount..
714 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000715 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100717 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100720 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000721 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000723 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
726 || iclog->ic_state == XLOG_STATE_DIRTY
727 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
728
Dave Chinnereb40a872010-12-21 12:09:01 +1100729 xlog_wait(&iclog->ic_force_wait,
730 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000732 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
735
David Chinner1bb7d6b2008-04-10 12:24:38 +1000736 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737} /* xfs_log_unmount_write */
738
739/*
740 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100741 *
742 * We need to stop the aild from running before we destroy
743 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
745void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100746xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
David Chinner249a8c12008-02-05 12:13:32 +1100748 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000749 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Dave Chinner43f5efc2010-03-23 10:10:00 +1100752void
753xfs_log_item_init(
754 struct xfs_mount *mp,
755 struct xfs_log_item *item,
756 int type,
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000757 const struct xfs_item_ops *ops)
Dave Chinner43f5efc2010-03-23 10:10:00 +1100758{
759 item->li_mountp = mp;
760 item->li_ailp = mp->m_ail;
761 item->li_type = type;
762 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +1000763 item->li_lv = NULL;
764
765 INIT_LIST_HEAD(&item->li_ail);
766 INIT_LIST_HEAD(&item->li_cil);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100767}
768
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000769/*
770 * Wake up processes waiting for log space after we have moved the log tail.
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772void
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000773xfs_log_space_wake(
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000774 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000776 struct log *log = mp->m_log;
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000777 int free_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (XLOG_FORCED_SHUTDOWN(log))
780 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dave Chinner3f16b982010-12-21 12:29:01 +1100782 if (!list_empty_careful(&log->l_writeq)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000783 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
784
Dave Chinner3f16b982010-12-21 12:29:01 +1100785 spin_lock(&log->l_grant_write_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +1100786 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000787 xlog_writeq_wake(log, &free_bytes);
Dave Chinner3f16b982010-12-21 12:29:01 +1100788 spin_unlock(&log->l_grant_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Dave Chinner10547942010-12-21 12:02:25 +1100790
Dave Chinner3f16b982010-12-21 12:29:01 +1100791 if (!list_empty_careful(&log->l_reserveq)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000792 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
793
Dave Chinner3f16b982010-12-21 12:29:01 +1100794 spin_lock(&log->l_grant_reserve_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +1100795 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000796 xlog_reserveq_wake(log, &free_bytes);
Dave Chinner3f16b982010-12-21 12:29:01 +1100797 spin_unlock(&log->l_grant_reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Dave Chinner3f16b982010-12-21 12:29:01 +1100799}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801/*
802 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000803 * that needs to be covered. To begin the transition to the idle state
804 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
805 * If we are then in a state where covering is needed, the caller is informed
806 * that dummy transactions are required to move the log into the idle state.
807 *
808 * Because this is called as part of the sync process, we should also indicate
809 * that dummy transactions should be issued in anything but the covered or
810 * idle states. This ensures that the log tail is accurately reflected in
811 * the log at the end of the sync, hence if a crash occurrs avoids replay
812 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
814int
815xfs_log_need_covered(xfs_mount_t *mp)
816{
David Chinner27d8d5f2008-10-30 17:38:39 +1100817 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
David Chinner92821e22007-05-24 15:26:31 +1000820 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000823 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000824 switch (log->l_covered_state) {
825 case XLOG_STATE_COVER_DONE:
826 case XLOG_STATE_COVER_DONE2:
827 case XLOG_STATE_COVER_IDLE:
828 break;
829 case XLOG_STATE_COVER_NEED:
830 case XLOG_STATE_COVER_NEED2:
Dave Chinnerfd074842011-04-08 12:45:07 +1000831 if (!xfs_ail_min_lsn(log->l_ailp) &&
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000832 xlog_iclogs_empty(log)) {
833 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
834 log->l_covered_state = XLOG_STATE_COVER_DONE;
835 else
836 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000838 /* FALLTHRU */
839 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000843 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100844 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000847/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * We may be holding the log iclog lock upon entering this routine.
849 */
850xfs_lsn_t
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100851xlog_assign_tail_lsn(
852 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100854 xfs_lsn_t tail_lsn;
855 struct log *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000857 /*
858 * To make sure we always have a valid LSN for the log tail we keep
859 * track of the last LSN which was committed in log->l_last_sync_lsn,
860 * and use that when the AIL was empty and xfs_ail_min_lsn returns 0.
861 *
862 * If the AIL has been emptied we also need to wake any process
863 * waiting for this condition.
864 */
Dave Chinnerfd074842011-04-08 12:45:07 +1000865 tail_lsn = xfs_ail_min_lsn(mp->m_ail);
Dave Chinner84f3c682010-12-03 22:11:29 +1100866 if (!tail_lsn)
867 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100868 atomic64_set(&log->l_tail_lsn, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return tail_lsn;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100870}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872/*
873 * Return the space in the log between the tail and the head. The head
874 * is passed in the cycle/bytes formal parms. In the special case where
875 * the reserve head has wrapped passed the tail, this calculation is no
876 * longer valid. In this case, just return 0 which means there is no space
877 * in the log. This works for all places where this function is called
878 * with the reserve head. Of course, if the write head were to ever
879 * wrap the tail, we should blow up. Rather than catch this case here,
880 * we depend on other ASSERTions in other parts of the code. XXXmiken
881 *
882 * This code also handles the case where the reservation head is behind
883 * the tail. The details of this case are described below, but the end
884 * result is that we return the size of the log as the amount of space left.
885 */
David Chinnera8272ce2007-11-23 16:28:09 +1100886STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +1100887xlog_space_left(
888 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100889 atomic64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Dave Chinnera69ed032010-12-21 12:08:20 +1100891 int free_bytes;
892 int tail_bytes;
893 int tail_cycle;
894 int head_cycle;
895 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Dave Chinnera69ed032010-12-21 12:08:20 +1100897 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100898 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
899 tail_bytes = BBTOB(tail_bytes);
Dave Chinnera69ed032010-12-21 12:08:20 +1100900 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
901 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
902 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +1100904 else if (tail_cycle < head_cycle) {
905 ASSERT(tail_cycle == (head_cycle - 1));
906 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 } else {
908 /*
909 * The reservation head is behind the tail.
910 * In this case we just want to return the size of the
911 * log as the amount of space left.
912 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100913 xfs_alert(log->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 "xlog_space_left: head behind tail\n"
915 " tail_cycle = %d, tail_bytes = %d\n"
916 " GH cycle = %d, GH bytes = %d",
Dave Chinnera69ed032010-12-21 12:08:20 +1100917 tail_cycle, tail_bytes, head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 ASSERT(0);
919 free_bytes = log->l_logsize;
920 }
921 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +1100922}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924
925/*
926 * Log function which is called when an io completes.
927 *
928 * The log manager needs its own routine, in order to control what
929 * happens with the buffer after the write completes.
930 */
931void
932xlog_iodone(xfs_buf_t *bp)
933{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200934 xlog_in_core_t *iclog = bp->b_fspriv;
935 xlog_t *l = iclog->ic_log;
936 int aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /*
939 * Race to shutdown the filesystem if we see an error.
940 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000941 if (XFS_TEST_ERROR((xfs_buf_geterror(bp)), l->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
Christoph Hellwig901796a2011-10-10 16:52:49 +0000943 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000944 xfs_buf_stale(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000945 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
947 * This flag will be propagated to the trans-committed
948 * callback routines to let them know that the log-commit
949 * didn't succeed.
950 */
951 aborted = XFS_LI_ABORTED;
952 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
953 aborted = XFS_LI_ABORTED;
954 }
David Chinner3db296f2007-05-14 18:24:16 +1000955
956 /* log I/O is always issued ASYNC */
957 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000959 /*
960 * do not reference the buffer (bp) here as we could race
961 * with it being freed after writing the unmount record to the
962 * log.
963 */
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965} /* xlog_iodone */
966
967/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * Return size of each in-core log record buffer.
969 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200970 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 *
972 * If the filesystem blocksize is too large, we may need to choose a
973 * larger size since the directory code currently logs entire blocks.
974 */
975
976STATIC void
977xlog_get_iclog_buffer_size(xfs_mount_t *mp,
978 xlog_t *log)
979{
980 int size;
981 int xhdrs;
982
Eric Sandeen1cb51252007-08-16 16:24:43 +1000983 if (mp->m_logbufs <= 0)
984 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
985 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100986 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /*
989 * Buffer size passed in from mount system call.
990 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100991 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 size = log->l_iclog_size = mp->m_logbsize;
993 log->l_iclog_size_log = 0;
994 while (size != 1) {
995 log->l_iclog_size_log++;
996 size >>= 1;
997 }
998
Eric Sandeen62118702008-03-06 13:44:28 +1100999 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001000 /* # headers = size / 32k
1001 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
1003
1004 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1005 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1006 xhdrs++;
1007 log->l_iclog_hsize = xhdrs << BBSHIFT;
1008 log->l_iclog_heads = xhdrs;
1009 } else {
1010 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1011 log->l_iclog_hsize = BBSIZE;
1012 log->l_iclog_heads = 1;
1013 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001014 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001017 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001018 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1019 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* the default log size is 16k or 32k which is one header sector */
1022 log->l_iclog_hsize = BBSIZE;
1023 log->l_iclog_heads = 1;
1024
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001025done:
1026 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001027 if (mp->m_logbufs == 0)
1028 mp->m_logbufs = log->l_iclog_bufs;
1029 if (mp->m_logbsize == 0)
1030 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031} /* xlog_get_iclog_buffer_size */
1032
1033
1034/*
1035 * This routine initializes some of the log structure for a given mount point.
1036 * Its primary purpose is to fill in enough, so recovery can occur. However,
1037 * some other stuff may be filled in too.
1038 */
1039STATIC xlog_t *
1040xlog_alloc_log(xfs_mount_t *mp,
1041 xfs_buftarg_t *log_target,
1042 xfs_daddr_t blk_offset,
1043 int num_bblks)
1044{
1045 xlog_t *log;
1046 xlog_rec_header_t *head;
1047 xlog_in_core_t **iclogp;
1048 xlog_in_core_t *iclog, *prev_iclog=NULL;
1049 xfs_buf_t *bp;
1050 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001051 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001052 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Dave Chinner644c3562008-11-10 16:50:24 +11001054 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001055 if (!log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001056 xfs_warn(mp, "Log allocation failed: No memory!");
Dave Chinnera6cb7672009-04-06 18:39:27 +02001057 goto out;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 log->l_mp = mp;
1061 log->l_targ = log_target;
1062 log->l_logsize = BBTOB(num_bblks);
1063 log->l_logBBstart = blk_offset;
1064 log->l_logBBsize = num_bblks;
1065 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1066 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1067
1068 log->l_prev_block = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001070 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1071 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Dave Chinnera69ed032010-12-21 12:08:20 +11001073 xlog_assign_grant_head(&log->l_grant_reserve_head, 1, 0);
1074 xlog_assign_grant_head(&log->l_grant_write_head, 1, 0);
Dave Chinner10547942010-12-21 12:02:25 +11001075 INIT_LIST_HEAD(&log->l_reserveq);
1076 INIT_LIST_HEAD(&log->l_writeq);
Dave Chinner3f16b982010-12-21 12:29:01 +11001077 spin_lock_init(&log->l_grant_reserve_lock);
1078 spin_lock_init(&log->l_grant_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Dave Chinnera6cb7672009-04-06 18:39:27 +02001080 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001081 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001082 log2_size = mp->m_sb.sb_logsectlog;
1083 if (log2_size < BBSHIFT) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001084 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1085 log2_size, BBSHIFT);
Alex Elder69ce58f2010-04-20 17:09:59 +10001086 goto out_free_log;
1087 }
1088
1089 log2_size -= BBSHIFT;
1090 if (log2_size > mp->m_sectbb_log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001091 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1092 log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001093 goto out_free_log;
1094 }
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001097 if (log2_size && log->l_logBBstart > 0 &&
1098 !xfs_sb_version_haslogv2(&mp->m_sb)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001099 xfs_warn(mp,
1100 "log sector size (0x%x) invalid for configuration.",
1101 log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001102 goto out_free_log;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001105 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 xlog_get_iclog_buffer_size(mp, log);
1108
Dave Chinnera6cb7672009-04-06 18:39:27 +02001109 error = ENOMEM;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +00001110 bp = xfs_buf_alloc(mp->m_logdev_targp, 0, log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001111 if (!bp)
1112 goto out_free_log;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001113 bp->b_iodone = xlog_iodone;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001114 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 log->l_xbuf = bp;
1116
Eric Sandeen007c61c2007-10-11 17:43:56 +10001117 spin_lock_init(&log->l_icloglock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001118 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1121 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1122
1123 iclogp = &log->l_iclog;
1124 /*
1125 * The amount of memory to allocate for the iclog structure is
1126 * rather funky due to the way the structure is defined. It is
1127 * done this way so that we can use different sizes for machines
1128 * with different amounts of memory. See the definition of
1129 * xlog_in_core_t in xfs_log_priv.h for details.
1130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ASSERT(log->l_iclog_size >= 4096);
1132 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001133 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1134 if (!*iclogp)
1135 goto out_free_iclog;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 iclog->ic_prev = prev_iclog;
1139 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001140
Dave Chinner686865f2010-09-24 20:07:47 +10001141 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1142 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001143 if (!bp)
1144 goto out_free_iclog;
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001145
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001146 bp->b_iodone = xlog_iodone;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001147 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001148 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001149#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 head = &iclog->ic_header;
1153 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001154 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1155 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001156 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001157 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001159 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1163 iclog->ic_state = XLOG_STATE_ACTIVE;
1164 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001165 atomic_set(&iclog->ic_refcnt, 0);
1166 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001168 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001170 ASSERT(xfs_buf_islocked(iclog->ic_bp));
Dave Chinnereb40a872010-12-21 12:09:01 +11001171 init_waitqueue_head(&iclog->ic_force_wait);
1172 init_waitqueue_head(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 iclogp = &iclog->ic_next;
1175 }
1176 *iclogp = log->l_iclog; /* complete ring */
1177 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1178
Dave Chinner71e330b2010-05-21 14:37:18 +10001179 error = xlog_cil_init(log);
1180 if (error)
1181 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001183
1184out_free_iclog:
1185 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1186 prev_iclog = iclog->ic_next;
Dave Chinnereb40a872010-12-21 12:09:01 +11001187 if (iclog->ic_bp)
Dave Chinner644c3562008-11-10 16:50:24 +11001188 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001189 kmem_free(iclog);
1190 }
1191 spinlock_destroy(&log->l_icloglock);
Dave Chinner644c3562008-11-10 16:50:24 +11001192 xfs_buf_free(log->l_xbuf);
1193out_free_log:
1194 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001195out:
1196 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197} /* xlog_alloc_log */
1198
1199
1200/*
1201 * Write out the commit record of a transaction associated with the given
1202 * ticket. Return the lsn of the commit record.
1203 */
1204STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001205xlog_commit_record(
1206 struct log *log,
1207 struct xlog_ticket *ticket,
1208 struct xlog_in_core **iclog,
1209 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Dave Chinner55b66332010-03-23 11:43:17 +11001211 struct xfs_mount *mp = log->l_mp;
1212 int error;
1213 struct xfs_log_iovec reg = {
1214 .i_addr = NULL,
1215 .i_len = 0,
1216 .i_type = XLOG_REG_TYPE_COMMIT,
1217 };
1218 struct xfs_log_vec vec = {
1219 .lv_niovecs = 1,
1220 .lv_iovecp = &reg,
1221 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001224 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1225 XLOG_COMMIT_TRANS);
1226 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001227 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001228 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231/*
1232 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1233 * log space. This code pushes on the lsn which would supposedly free up
1234 * the 25% which we want to leave free. We may need to adopt a policy which
1235 * pushes on an lsn which is further along in the log once we reach the high
1236 * water mark. In this manner, we would be creating a low water mark.
1237 */
David Chinnera8272ce2007-11-23 16:28:09 +11001238STATIC void
Dave Chinner2ced19c2010-12-21 12:09:20 +11001239xlog_grant_push_ail(
1240 struct log *log,
1241 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001243 xfs_lsn_t threshold_lsn = 0;
Dave Chinner84f3c682010-12-03 22:11:29 +11001244 xfs_lsn_t last_sync_lsn;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001245 int free_blocks;
1246 int free_bytes;
1247 int threshold_block;
1248 int threshold_cycle;
1249 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Dave Chinner2ced19c2010-12-21 12:09:20 +11001251 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Dave Chinner2ced19c2010-12-21 12:09:20 +11001253 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
1254 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Dave Chinner2ced19c2010-12-21 12:09:20 +11001256 /*
1257 * Set the threshold for the minimum number of free blocks in the
1258 * log to the maximum of what the caller needs, one quarter of the
1259 * log, and 256 blocks.
1260 */
1261 free_threshold = BTOBB(need_bytes);
1262 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1263 free_threshold = MAX(free_threshold, 256);
1264 if (free_blocks >= free_threshold)
1265 return;
1266
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001267 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1268 &threshold_block);
1269 threshold_block += free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001271 threshold_block -= log->l_logBBsize;
1272 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001274 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1275 threshold_block);
1276 /*
1277 * Don't pass in an lsn greater than the lsn of the last
Dave Chinner84f3c682010-12-03 22:11:29 +11001278 * log record known to be on disk. Use a snapshot of the last sync lsn
1279 * so that it doesn't change between the compare and the set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Dave Chinner84f3c682010-12-03 22:11:29 +11001281 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1282 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1283 threshold_lsn = last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Dave Chinner2ced19c2010-12-21 12:09:20 +11001285 /*
1286 * Get the transaction layer to kick the dirty buffers out to
1287 * disk asynchronously. No point in trying to do this if
1288 * the filesystem is shutting down.
1289 */
1290 if (!XLOG_FORCED_SHUTDOWN(log))
Dave Chinnerfd074842011-04-08 12:45:07 +10001291 xfs_ail_push(log->l_ailp, threshold_lsn);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001292}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Christoph Hellwig873ff552010-01-13 22:17:57 +00001294/*
1295 * The bdstrat callback function for log bufs. This gives us a central
1296 * place to trap bufs in case we get hit by a log I/O error and need to
1297 * shutdown. Actually, in practice, even when we didn't get a log error,
1298 * we transition the iclogs to IOERROR state *after* flushing all existing
1299 * iclogs to disk. This is because we don't want anymore new transactions to be
1300 * started or completed afterwards.
1301 */
1302STATIC int
1303xlog_bdstrat(
1304 struct xfs_buf *bp)
1305{
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001306 struct xlog_in_core *iclog = bp->b_fspriv;
Christoph Hellwig873ff552010-01-13 22:17:57 +00001307
Christoph Hellwig873ff552010-01-13 22:17:57 +00001308 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001309 xfs_buf_ioerror(bp, EIO);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001310 xfs_buf_stale(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001311 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001312 /*
1313 * It would seem logical to return EIO here, but we rely on
1314 * the log state machine to propagate I/O errors instead of
1315 * doing it here.
1316 */
1317 return 0;
1318 }
1319
Christoph Hellwig873ff552010-01-13 22:17:57 +00001320 xfs_buf_iorequest(bp);
1321 return 0;
1322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324/*
1325 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1326 * fashion. Previously, we should have moved the current iclog
1327 * ptr in the log to point to the next available iclog. This allows further
1328 * write to continue while this code syncs out an iclog ready to go.
1329 * Before an in-core log can be written out, the data section must be scanned
1330 * to save away the 1st word of each BBSIZE block into the header. We replace
1331 * it with the current cycle count. Each BBSIZE block is tagged with the
1332 * cycle count because there in an implicit assumption that drives will
1333 * guarantee that entire 512 byte blocks get written at once. In other words,
1334 * we can't have part of a 512 byte block written and part not written. By
1335 * tagging each block, we will know which blocks are valid when recovering
1336 * after an unclean shutdown.
1337 *
1338 * This routine is single threaded on the iclog. No other thread can be in
1339 * this routine with the same iclog. Changing contents of iclog can there-
1340 * fore be done without grabbing the state machine lock. Updating the global
1341 * log will require grabbing the lock though.
1342 *
1343 * The entire log manager uses a logical block numbering scheme. Only
1344 * log_sync (and then only bwrite()) know about the fact that the log may
1345 * not start with block zero on a given device. The log block start offset
1346 * is added immediately before calling bwrite().
1347 */
1348
David Chinnera8272ce2007-11-23 16:28:09 +11001349STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350xlog_sync(xlog_t *log,
1351 xlog_in_core_t *iclog)
1352{
1353 xfs_caddr_t dptr; /* pointer to byte sized element */
1354 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001355 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 uint count; /* byte count of bwrite */
1357 uint count_init; /* initial count before roundup */
1358 int roundoff; /* roundoff to BB or stripe */
1359 int split = 0; /* split write into two regions */
1360 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001361 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001364 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* Add for LR header */
1367 count_init = log->l_iclog_hsize + iclog->ic_offset;
1368
1369 /* Round out the log write size */
1370 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1371 /* we have a v2 stripe unit to use */
1372 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1373 } else {
1374 count = BBTOB(BTOBB(count_init));
1375 }
1376 roundoff = count - count_init;
1377 ASSERT(roundoff >= 0);
1378 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1379 roundoff < log->l_mp->m_sb.sb_logsunit)
1380 ||
1381 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1382 roundoff < BBTOB(1)));
1383
1384 /* move grant heads by roundoff in sync */
Dave Chinnera69ed032010-12-21 12:08:20 +11001385 xlog_grant_add_space(log, &log->l_grant_reserve_head, roundoff);
1386 xlog_grant_add_space(log, &log->l_grant_write_head, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* put cycle number in every block */
1389 xlog_pack_data(log, iclog, roundoff);
1390
1391 /* real byte length */
1392 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001393 iclog->ic_header.h_len =
1394 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001396 iclog->ic_header.h_len =
1397 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Nathan Scottf5faad72006-07-28 17:04:44 +10001400 bp = iclog->ic_bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001401 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1404
1405 /* Do we need to split this write into 2 parts? */
1406 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1407 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1408 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1409 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1410 } else {
1411 iclog->ic_bwritecnt = 1;
1412 }
David Chinner511105b2007-05-24 15:21:57 +10001413 XFS_BUF_SET_COUNT(bp, count);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001414 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001415 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001417 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001418
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001419 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) {
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001420 bp->b_flags |= XBF_FUA;
1421
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001422 /*
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001423 * Flush the data device before flushing the log to make
1424 * sure all meta data written back from the AIL actually made
1425 * it to disk before stamping the new log tail LSN into the
1426 * log buffer. For an external log we need to issue the
1427 * flush explicitly, and unfortunately synchronously here;
1428 * for an internal log we can simply use the block layer
1429 * state machine for preflushes.
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001430 */
1431 if (log->l_mp->m_logdev_targp != log->l_mp->m_ddev_targp)
1432 xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001433 else
1434 bp->b_flags |= XBF_FLUSH;
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1438 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1439
1440 xlog_verify_iclog(log, iclog, count, B_TRUE);
1441
1442 /* account for log which doesn't start at block #0 */
1443 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1444 /*
1445 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1446 * is shutting down.
1447 */
1448 XFS_BUF_WRITE(bp);
1449
Christoph Hellwig901796a2011-10-10 16:52:49 +00001450 error = xlog_bdstrat(bp);
1451 if (error) {
1452 xfs_buf_ioerror_alert(bp, "xlog_sync");
Jesper Juhl014c2542006-01-15 02:37:08 +01001453 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001456 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
Chandra Seetharaman02fe03d2011-07-22 23:40:22 +00001458 xfs_buf_associate_memory(bp,
1459 (char *)&iclog->ic_header + count, split);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001460 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001461 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001463 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001464 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001465 bp->b_flags |= XBF_FUA;
Chandra Seetharaman62926042011-07-22 23:40:15 +00001466 dptr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
1468 * Bump the cycle numbers at the start of each block
1469 * since this part of the buffer is at the start of
1470 * a new cycle. Watch out for the header magic number
1471 * case, though.
1472 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001473 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001474 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001475 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001476 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 dptr += BBSIZE;
1478 }
1479
1480 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1481 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1482
Nathan Scottc41564b2006-03-29 08:55:14 +10001483 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1485 XFS_BUF_WRITE(bp);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001486 error = xlog_bdstrat(bp);
1487 if (error) {
1488 xfs_buf_ioerror_alert(bp, "xlog_sync (split)");
Jesper Juhl014c2542006-01-15 02:37:08 +01001489 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493} /* xlog_sync */
1494
1495
1496/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001497 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
David Chinnera8272ce2007-11-23 16:28:09 +11001499STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001500xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int i;
1504
Dave Chinner71e330b2010-05-21 14:37:18 +10001505 xlog_cil_destroy(log);
1506
Dave Chinner44396472011-04-21 09:34:27 +00001507 /*
1508 * always need to ensure that the extra buffer does not point to memory
1509 * owned by another log buffer before we free it.
1510 */
1511 xfs_buf_set_empty(log->l_xbuf, log->l_iclog_size);
1512 xfs_buf_free(log->l_xbuf);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 iclog = log->l_iclog;
1515 for (i=0; i<log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001518 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 iclog = next_iclog;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 spinlock_destroy(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001524 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001525} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527/*
1528 * Update counters atomically now that memcpy is done.
1529 */
1530/* ARGSUSED */
1531static inline void
1532xlog_state_finish_copy(xlog_t *log,
1533 xlog_in_core_t *iclog,
1534 int record_cnt,
1535 int copy_bytes)
1536{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001537 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001539 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 iclog->ic_offset += copy_bytes;
1541
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001542 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543} /* xlog_state_finish_copy */
1544
1545
1546
1547
1548/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001549 * print out info relating to regions written which consume
1550 * the reservation
1551 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001552void
1553xlog_print_tic_res(
1554 struct xfs_mount *mp,
1555 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001556{
1557 uint i;
1558 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1559
1560 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1561 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1562 "bformat",
1563 "bchunk",
1564 "efi_format",
1565 "efd_format",
1566 "iformat",
1567 "icore",
1568 "iext",
1569 "ibroot",
1570 "ilocal",
1571 "iattr_ext",
1572 "iattr_broot",
1573 "iattr_local",
1574 "qformat",
1575 "dquot",
1576 "quotaoff",
1577 "LR header",
1578 "unmount",
1579 "commit",
1580 "trans header"
1581 };
1582 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1583 "SETATTR_NOT_SIZE",
1584 "SETATTR_SIZE",
1585 "INACTIVE",
1586 "CREATE",
1587 "CREATE_TRUNC",
1588 "TRUNCATE_FILE",
1589 "REMOVE",
1590 "LINK",
1591 "RENAME",
1592 "MKDIR",
1593 "RMDIR",
1594 "SYMLINK",
1595 "SET_DMATTRS",
1596 "GROWFS",
1597 "STRAT_WRITE",
1598 "DIOSTRAT",
1599 "WRITE_SYNC",
1600 "WRITEID",
1601 "ADDAFORK",
1602 "ATTRINVAL",
1603 "ATRUNCATE",
1604 "ATTR_SET",
1605 "ATTR_RM",
1606 "ATTR_FLAG",
1607 "CLEAR_AGI_BUCKET",
1608 "QM_SBCHANGE",
1609 "DUMMY1",
1610 "DUMMY2",
1611 "QM_QUOTAOFF",
1612 "QM_DQALLOC",
1613 "QM_SETQLIM",
1614 "QM_DQCLUSTER",
1615 "QM_QINOCREATE",
1616 "QM_QUOTAOFF_END",
1617 "SB_UNIT",
1618 "FSYNC_TS",
1619 "GROWFSRT_ALLOC",
1620 "GROWFSRT_ZERO",
1621 "GROWFSRT_FREE",
1622 "SWAPEXT"
1623 };
1624
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001625 xfs_warn(mp,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001626 "xlog_write: reservation summary:\n"
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001627 " trans type = %s (%u)\n"
1628 " unit res = %d bytes\n"
1629 " current res = %d bytes\n"
1630 " total reg = %u bytes (o/flow = %u bytes)\n"
1631 " ophdrs = %u (ophdr space = %u bytes)\n"
1632 " ophdr + reg = %u bytes\n"
1633 " num regions = %u\n",
1634 ((ticket->t_trans_type <= 0 ||
1635 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1636 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1637 ticket->t_trans_type,
1638 ticket->t_unit_res,
1639 ticket->t_curr_res,
1640 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1641 ticket->t_res_num_ophdrs, ophdr_spc,
1642 ticket->t_res_arr_sum +
1643 ticket->t_res_o_flow + ophdr_spc,
1644 ticket->t_res_num);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001645
1646 for (i = 0; i < ticket->t_res_num; i++) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001647 uint r_type = ticket->t_res_arr[i].r_type;
1648 xfs_warn(mp, "region[%u]: %s - %u bytes\n", i,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001649 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1650 "bad-rtype" : res_type_str[r_type-1]),
1651 ticket->t_res_arr[i].r_len);
1652 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001653
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001654 xfs_alert_tag(mp, XFS_PTAG_LOGRES,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001655 "xlog_write: reservation ran out. Need to up reservation");
Dave Chinner169a7b02010-05-07 11:05:31 +10001656 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001657}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001658
1659/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001660 * Calculate the potential space needed by the log vector. Each region gets
1661 * its own xlog_op_header_t and may need to be double word aligned.
1662 */
1663static int
1664xlog_write_calc_vec_length(
1665 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001666 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001667{
Dave Chinner55b66332010-03-23 11:43:17 +11001668 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001669 int headers = 0;
1670 int len = 0;
1671 int i;
1672
1673 /* acct for start rec of xact */
1674 if (ticket->t_flags & XLOG_TIC_INITED)
1675 headers++;
1676
Dave Chinner55b66332010-03-23 11:43:17 +11001677 for (lv = log_vector; lv; lv = lv->lv_next) {
1678 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001679
Dave Chinner55b66332010-03-23 11:43:17 +11001680 for (i = 0; i < lv->lv_niovecs; i++) {
1681 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1682
1683 len += vecp->i_len;
1684 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1685 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001686 }
1687
1688 ticket->t_res_num_ophdrs += headers;
1689 len += headers * sizeof(struct xlog_op_header);
1690
1691 return len;
1692}
1693
1694/*
1695 * If first write for transaction, insert start record We can't be trying to
1696 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1697 */
1698static int
1699xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001700 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001701 struct xlog_ticket *ticket)
1702{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001703 if (!(ticket->t_flags & XLOG_TIC_INITED))
1704 return 0;
1705
1706 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1707 ophdr->oh_clientid = ticket->t_clientid;
1708 ophdr->oh_len = 0;
1709 ophdr->oh_flags = XLOG_START_TRANS;
1710 ophdr->oh_res2 = 0;
1711
1712 ticket->t_flags &= ~XLOG_TIC_INITED;
1713
1714 return sizeof(struct xlog_op_header);
1715}
1716
1717static xlog_op_header_t *
1718xlog_write_setup_ophdr(
1719 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001720 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001721 struct xlog_ticket *ticket,
1722 uint flags)
1723{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001724 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1725 ophdr->oh_clientid = ticket->t_clientid;
1726 ophdr->oh_res2 = 0;
1727
1728 /* are we copying a commit or unmount record? */
1729 ophdr->oh_flags = flags;
1730
1731 /*
1732 * We've seen logs corrupted with bad transaction client ids. This
1733 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1734 * and shut down the filesystem.
1735 */
1736 switch (ophdr->oh_clientid) {
1737 case XFS_TRANSACTION:
1738 case XFS_VOLUME:
1739 case XFS_LOG:
1740 break;
1741 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001742 xfs_warn(log->l_mp,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001743 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1744 ophdr->oh_clientid, ticket);
1745 return NULL;
1746 }
1747
1748 return ophdr;
1749}
1750
1751/*
1752 * Set up the parameters of the region copy into the log. This has
1753 * to handle region write split across multiple log buffers - this
1754 * state is kept external to this function so that this code can
1755 * can be written in an obvious, self documenting manner.
1756 */
1757static int
1758xlog_write_setup_copy(
1759 struct xlog_ticket *ticket,
1760 struct xlog_op_header *ophdr,
1761 int space_available,
1762 int space_required,
1763 int *copy_off,
1764 int *copy_len,
1765 int *last_was_partial_copy,
1766 int *bytes_consumed)
1767{
1768 int still_to_copy;
1769
1770 still_to_copy = space_required - *bytes_consumed;
1771 *copy_off = *bytes_consumed;
1772
1773 if (still_to_copy <= space_available) {
1774 /* write of region completes here */
1775 *copy_len = still_to_copy;
1776 ophdr->oh_len = cpu_to_be32(*copy_len);
1777 if (*last_was_partial_copy)
1778 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1779 *last_was_partial_copy = 0;
1780 *bytes_consumed = 0;
1781 return 0;
1782 }
1783
1784 /* partial write of region, needs extra log op header reservation */
1785 *copy_len = space_available;
1786 ophdr->oh_len = cpu_to_be32(*copy_len);
1787 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1788 if (*last_was_partial_copy)
1789 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1790 *bytes_consumed += *copy_len;
1791 (*last_was_partial_copy)++;
1792
1793 /* account for new log op header */
1794 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1795 ticket->t_res_num_ophdrs++;
1796
1797 return sizeof(struct xlog_op_header);
1798}
1799
1800static int
1801xlog_write_copy_finish(
1802 struct log *log,
1803 struct xlog_in_core *iclog,
1804 uint flags,
1805 int *record_cnt,
1806 int *data_cnt,
1807 int *partial_copy,
1808 int *partial_copy_len,
1809 int log_offset,
1810 struct xlog_in_core **commit_iclog)
1811{
1812 if (*partial_copy) {
1813 /*
1814 * This iclog has already been marked WANT_SYNC by
1815 * xlog_state_get_iclog_space.
1816 */
1817 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1818 *record_cnt = 0;
1819 *data_cnt = 0;
1820 return xlog_state_release_iclog(log, iclog);
1821 }
1822
1823 *partial_copy = 0;
1824 *partial_copy_len = 0;
1825
1826 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1827 /* no more space in this iclog - push it. */
1828 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1829 *record_cnt = 0;
1830 *data_cnt = 0;
1831
1832 spin_lock(&log->l_icloglock);
1833 xlog_state_want_sync(log, iclog);
1834 spin_unlock(&log->l_icloglock);
1835
1836 if (!commit_iclog)
1837 return xlog_state_release_iclog(log, iclog);
1838 ASSERT(flags & XLOG_COMMIT_TRANS);
1839 *commit_iclog = iclog;
1840 }
1841
1842 return 0;
1843}
1844
1845/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 * Write some region out to in-core log
1847 *
1848 * This will be called when writing externally provided regions or when
1849 * writing out a commit record for a given transaction.
1850 *
1851 * General algorithm:
1852 * 1. Find total length of this write. This may include adding to the
1853 * lengths passed in.
1854 * 2. Check whether we violate the tickets reservation.
1855 * 3. While writing to this iclog
1856 * A. Reserve as much space in this iclog as can get
1857 * B. If this is first write, save away start lsn
1858 * C. While writing this region:
1859 * 1. If first write of transaction, write start record
1860 * 2. Write log operation header (header per region)
1861 * 3. Find out if we can fit entire region into this iclog
1862 * 4. Potentially, verify destination memcpy ptr
1863 * 5. Memcpy (partial) region
1864 * 6. If partial copy, release iclog; otherwise, continue
1865 * copying more regions into current iclog
1866 * 4. Mark want sync bit (in simulation mode)
1867 * 5. Release iclog for potential flush to on-disk log.
1868 *
1869 * ERRORS:
1870 * 1. Panic if reservation is overrun. This should never happen since
1871 * reservation amounts are generated internal to the filesystem.
1872 * NOTES:
1873 * 1. Tickets are single threaded data structures.
1874 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1875 * syncing routine. When a single log_write region needs to span
1876 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1877 * on all log operation writes which don't contain the end of the
1878 * region. The XLOG_END_TRANS bit is used for the in-core log
1879 * operation which contains the end of the continued log_write region.
1880 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1881 * we don't really know exactly how much space will be used. As a result,
1882 * we don't update ic_offset until the end when we know exactly how many
1883 * bytes have been written out.
1884 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001885int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001886xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001887 struct log *log,
1888 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001889 struct xlog_ticket *ticket,
1890 xfs_lsn_t *start_lsn,
1891 struct xlog_in_core **commit_iclog,
1892 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001894 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001895 struct xfs_log_iovec *vecp;
1896 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001897 int len;
1898 int index;
1899 int partial_copy = 0;
1900 int partial_copy_len = 0;
1901 int contwr = 0;
1902 int record_cnt = 0;
1903 int data_cnt = 0;
1904 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001906 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Dave Chinner55b66332010-03-23 11:43:17 +11001908 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001909
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001910 /*
1911 * Region headers and bytes are already accounted for.
1912 * We only need to take into account start records and
1913 * split regions in this function.
1914 */
1915 if (ticket->t_flags & XLOG_TIC_INITED)
1916 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1917
1918 /*
1919 * Commit record headers need to be accounted for. These
1920 * come in as separate writes so are easy to detect.
1921 */
1922 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1923 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Dave Chinner71e330b2010-05-21 14:37:18 +10001924
1925 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001926 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Dave Chinner55b66332010-03-23 11:43:17 +11001928 index = 0;
1929 lv = log_vector;
1930 vecp = lv->lv_iovecp;
1931 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001932 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001933 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001935 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1936 &contwr, &log_offset);
1937 if (error)
1938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001940 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001941 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001943 /* start_lsn is the first lsn written to. That's all we need. */
1944 if (!*start_lsn)
1945 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001947 /*
1948 * This loop writes out as many regions as can fit in the amount
1949 * of space which was allocated by xlog_state_get_iclog_space().
1950 */
Dave Chinner55b66332010-03-23 11:43:17 +11001951 while (lv && index < lv->lv_niovecs) {
1952 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001953 struct xlog_op_header *ophdr;
1954 int start_rec_copy;
1955 int copy_len;
1956 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Dave Chinner55b66332010-03-23 11:43:17 +11001958 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001959 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001961 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1962 if (start_rec_copy) {
1963 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001964 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001965 start_rec_copy);
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001968 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1969 if (!ophdr)
1970 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001972 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001973 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001975 len += xlog_write_setup_copy(ticket, ophdr,
1976 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001977 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001978 &copy_off, &copy_len,
1979 &partial_copy,
1980 &partial_copy_len);
1981 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001982
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001983 /* copy region */
1984 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001985 memcpy(ptr, reg->i_addr + copy_off, copy_len);
1986 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001987
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001988 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1989 record_cnt++;
1990 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001992 error = xlog_write_copy_finish(log, iclog, flags,
1993 &record_cnt, &data_cnt,
1994 &partial_copy,
1995 &partial_copy_len,
1996 log_offset,
1997 commit_iclog);
1998 if (error)
1999 return error;
2000
2001 /*
2002 * if we had a partial copy, we need to get more iclog
2003 * space but we don't want to increment the region
2004 * index because there is still more is this region to
2005 * write.
2006 *
2007 * If we completed writing this region, and we flushed
2008 * the iclog (indicated by resetting of the record
2009 * count), then we also need to get more log space. If
2010 * this was the last record, though, we are done and
2011 * can just return.
2012 */
2013 if (partial_copy)
2014 break;
2015
Dave Chinner55b66332010-03-23 11:43:17 +11002016 if (++index == lv->lv_niovecs) {
2017 lv = lv->lv_next;
2018 index = 0;
2019 if (lv)
2020 vecp = lv->lv_iovecp;
2021 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002022 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11002023 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002024 return 0;
2025 break;
2026 }
2027 }
2028 }
2029
2030 ASSERT(len == 0);
2031
2032 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2033 if (!commit_iclog)
2034 return xlog_state_release_iclog(log, iclog);
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 ASSERT(flags & XLOG_COMMIT_TRANS);
2037 *commit_iclog = iclog;
2038 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041
2042/*****************************************************************************
2043 *
2044 * State Machine functions
2045 *
2046 *****************************************************************************
2047 */
2048
2049/* Clean iclogs starting from the head. This ordering must be
2050 * maintained, so an iclog doesn't become ACTIVE beyond one that
2051 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002052 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * iclog is trying to sync to disk.
2054 *
2055 * State Change: DIRTY -> ACTIVE
2056 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002057STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058xlog_state_clean_log(xlog_t *log)
2059{
2060 xlog_in_core_t *iclog;
2061 int changed = 0;
2062
2063 iclog = log->l_iclog;
2064 do {
2065 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2066 iclog->ic_state = XLOG_STATE_ACTIVE;
2067 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002068 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /*
2070 * If the number of ops in this iclog indicate it just
2071 * contains the dummy transaction, we can
2072 * change state into IDLE (the second time around).
2073 * Otherwise we should change the state into
2074 * NEED a dummy.
2075 * We don't need to cover the dummy.
2076 */
2077 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002078 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2079 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 changed = 1;
2081 } else {
2082 /*
2083 * We have two dirty iclogs so start over
2084 * This could also be num of ops indicates
2085 * this is not the dummy going out.
2086 */
2087 changed = 2;
2088 }
2089 iclog->ic_header.h_num_logops = 0;
2090 memset(iclog->ic_header.h_cycle_data, 0,
2091 sizeof(iclog->ic_header.h_cycle_data));
2092 iclog->ic_header.h_lsn = 0;
2093 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2094 /* do nothing */;
2095 else
2096 break; /* stop cleaning */
2097 iclog = iclog->ic_next;
2098 } while (iclog != log->l_iclog);
2099
2100 /* log is locked when we are called */
2101 /*
2102 * Change state for the dummy log recording.
2103 * We usually go to NEED. But we go to NEED2 if the changed indicates
2104 * we are done writing the dummy record.
2105 * If we are done with the second dummy recored (DONE2), then
2106 * we go to IDLE.
2107 */
2108 if (changed) {
2109 switch (log->l_covered_state) {
2110 case XLOG_STATE_COVER_IDLE:
2111 case XLOG_STATE_COVER_NEED:
2112 case XLOG_STATE_COVER_NEED2:
2113 log->l_covered_state = XLOG_STATE_COVER_NEED;
2114 break;
2115
2116 case XLOG_STATE_COVER_DONE:
2117 if (changed == 1)
2118 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2119 else
2120 log->l_covered_state = XLOG_STATE_COVER_NEED;
2121 break;
2122
2123 case XLOG_STATE_COVER_DONE2:
2124 if (changed == 1)
2125 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2126 else
2127 log->l_covered_state = XLOG_STATE_COVER_NEED;
2128 break;
2129
2130 default:
2131 ASSERT(0);
2132 }
2133 }
2134} /* xlog_state_clean_log */
2135
2136STATIC xfs_lsn_t
2137xlog_get_lowest_lsn(
2138 xlog_t *log)
2139{
2140 xlog_in_core_t *lsn_log;
2141 xfs_lsn_t lowest_lsn, lsn;
2142
2143 lsn_log = log->l_iclog;
2144 lowest_lsn = 0;
2145 do {
2146 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002147 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if ((lsn && !lowest_lsn) ||
2149 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2150 lowest_lsn = lsn;
2151 }
2152 }
2153 lsn_log = lsn_log->ic_next;
2154 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002155 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158
2159STATIC void
2160xlog_state_do_callback(
2161 xlog_t *log,
2162 int aborted,
2163 xlog_in_core_t *ciclog)
2164{
2165 xlog_in_core_t *iclog;
2166 xlog_in_core_t *first_iclog; /* used to know when we've
2167 * processed all iclogs once */
2168 xfs_log_callback_t *cb, *cb_next;
2169 int flushcnt = 0;
2170 xfs_lsn_t lowest_lsn;
2171 int ioerrors; /* counter: iclogs with errors */
2172 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2173 int funcdidcallbacks; /* flag: function did callbacks */
2174 int repeats; /* for issuing console warnings if
2175 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002176 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002178 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 first_iclog = iclog = log->l_iclog;
2180 ioerrors = 0;
2181 funcdidcallbacks = 0;
2182 repeats = 0;
2183
2184 do {
2185 /*
2186 * Scan all iclogs starting with the one pointed to by the
2187 * log. Reset this starting point each time the log is
2188 * unlocked (during callbacks).
2189 *
2190 * Keep looping through iclogs until one full pass is made
2191 * without running any callbacks.
2192 */
2193 first_iclog = log->l_iclog;
2194 iclog = log->l_iclog;
2195 loopdidcallbacks = 0;
2196 repeats++;
2197
2198 do {
2199
2200 /* skip all iclogs in the ACTIVE & DIRTY states */
2201 if (iclog->ic_state &
2202 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2203 iclog = iclog->ic_next;
2204 continue;
2205 }
2206
2207 /*
2208 * Between marking a filesystem SHUTDOWN and stopping
2209 * the log, we do flush all iclogs to disk (if there
2210 * wasn't a log I/O error). So, we do want things to
2211 * go smoothly in case of just a SHUTDOWN w/o a
2212 * LOG_IO_ERROR.
2213 */
2214 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2215 /*
2216 * Can only perform callbacks in order. Since
2217 * this iclog is not in the DONE_SYNC/
2218 * DO_CALLBACK state, we skip the rest and
2219 * just try to clean up. If we set our iclog
2220 * to DO_CALLBACK, we will not process it when
2221 * we retry since a previous iclog is in the
2222 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002223 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 */
2225 if (!(iclog->ic_state &
2226 (XLOG_STATE_DONE_SYNC |
2227 XLOG_STATE_DO_CALLBACK))) {
2228 if (ciclog && (ciclog->ic_state ==
2229 XLOG_STATE_DONE_SYNC)) {
2230 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2231 }
2232 break;
2233 }
2234 /*
2235 * We now have an iclog that is in either the
2236 * DO_CALLBACK or DONE_SYNC states. The other
2237 * states (WANT_SYNC, SYNCING, or CALLBACK were
2238 * caught by the above if and are going to
2239 * clean (i.e. we aren't doing their callbacks)
2240 * see the above if.
2241 */
2242
2243 /*
2244 * We will do one more check here to see if we
2245 * have chased our tail around.
2246 */
2247
2248 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002249 if (lowest_lsn &&
2250 XFS_LSN_CMP(lowest_lsn,
Dave Chinner84f3c682010-12-03 22:11:29 +11002251 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 iclog = iclog->ic_next;
2253 continue; /* Leave this iclog for
2254 * another thread */
2255 }
2256
2257 iclog->ic_state = XLOG_STATE_CALLBACK;
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Dave Chinner84f3c682010-12-03 22:11:29 +11002260 /*
2261 * update the last_sync_lsn before we drop the
2262 * icloglock to ensure we are the only one that
2263 * can update it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 */
Dave Chinner84f3c682010-12-03 22:11:29 +11002265 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2266 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2267 atomic64_set(&log->l_last_sync_lsn,
2268 be64_to_cpu(iclog->ic_header.h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Dave Chinner84f3c682010-12-03 22:11:29 +11002270 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 ioerrors++;
Dave Chinner84f3c682010-12-03 22:11:29 +11002272
2273 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
David Chinner114d23a2008-04-10 12:18:39 +10002275 /*
2276 * Keep processing entries in the callback list until
2277 * we come around and it is empty. We need to
2278 * atomically see that the list is empty and change the
2279 * state to DIRTY so that we don't miss any more
2280 * callbacks being added.
2281 */
2282 spin_lock(&iclog->ic_callback_lock);
2283 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002284 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 iclog->ic_callback_tail = &(iclog->ic_callback);
2286 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002287 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002290 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 cb_next = cb->cb_next;
2292 cb->cb_func(cb->cb_arg, aborted);
2293 }
David Chinner114d23a2008-04-10 12:18:39 +10002294 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 cb = iclog->ic_callback;
2296 }
2297
2298 loopdidcallbacks++;
2299 funcdidcallbacks++;
2300
David Chinner114d23a2008-04-10 12:18:39 +10002301 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002302 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002303 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2305 iclog->ic_state = XLOG_STATE_DIRTY;
2306
2307 /*
2308 * Transition from DIRTY to ACTIVE if applicable.
2309 * NOP if STATE_IOERROR.
2310 */
2311 xlog_state_clean_log(log);
2312
2313 /* wake up threads waiting in xfs_log_force() */
Dave Chinnereb40a872010-12-21 12:09:01 +11002314 wake_up_all(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 iclog = iclog->ic_next;
2317 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002318
2319 if (repeats > 5000) {
2320 flushcnt += repeats;
2321 repeats = 0;
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002322 xfs_warn(log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002323 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002324 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326 } while (!ioerrors && loopdidcallbacks);
2327
2328 /*
2329 * make one last gasp attempt to see if iclogs are being left in
2330 * limbo..
2331 */
2332#ifdef DEBUG
2333 if (funcdidcallbacks) {
2334 first_iclog = iclog = log->l_iclog;
2335 do {
2336 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2337 /*
2338 * Terminate the loop if iclogs are found in states
2339 * which will cause other threads to clean up iclogs.
2340 *
2341 * SYNCING - i/o completion will go through logs
2342 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002343 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 * IOERROR - give up hope all ye who enter here
2345 */
2346 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2347 iclog->ic_state == XLOG_STATE_SYNCING ||
2348 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2349 iclog->ic_state == XLOG_STATE_IOERROR )
2350 break;
2351 iclog = iclog->ic_next;
2352 } while (first_iclog != iclog);
2353 }
2354#endif
2355
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002356 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2357 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002358 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002359
2360 if (wake)
Dave Chinnereb40a872010-12-21 12:09:01 +11002361 wake_up_all(&log->l_flush_wait);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364
2365/*
2366 * Finish transitioning this iclog to the dirty state.
2367 *
2368 * Make sure that we completely execute this routine only when this is
2369 * the last call to the iclog. There is a good chance that iclog flushes,
2370 * when we reach the end of the physical log, get turned into 2 separate
2371 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2372 * routine. By using the reference count bwritecnt, we guarantee that only
2373 * the second completion goes through.
2374 *
2375 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002376 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 */
David Chinnera8272ce2007-11-23 16:28:09 +11002378STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379xlog_state_done_syncing(
2380 xlog_in_core_t *iclog,
2381 int aborted)
2382{
2383 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002385 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2388 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002389 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2391
2392
2393 /*
2394 * If we got an error, either on the first buffer, or in the case of
2395 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2396 * and none should ever be attempted to be written to disk
2397 * again.
2398 */
2399 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2400 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002401 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return;
2403 }
2404 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2405 }
2406
2407 /*
2408 * Someone could be sleeping prior to writing out the next
2409 * iclog buffer, we wake them all, one will get to do the
2410 * I/O, the others get to wait for the result.
2411 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002412 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002413 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2415} /* xlog_state_done_syncing */
2416
2417
2418/*
2419 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002420 * sleep. We wait on the flush queue on the head iclog as that should be
2421 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2422 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 *
2424 * The in-core logs are used in a circular fashion. They are not used
2425 * out-of-order even when an iclog past the head is free.
2426 *
2427 * return:
2428 * * log_offset where xlog_write() can start writing into the in-core
2429 * log's data space.
2430 * * in-core log pointer to which xlog_write() should write.
2431 * * boolean indicating this is a continued write to an in-core log.
2432 * If this is the last write, then the in-core log's offset field
2433 * needs to be incremented, depending on the amount of data which
2434 * is copied.
2435 */
David Chinnera8272ce2007-11-23 16:28:09 +11002436STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437xlog_state_get_iclog_space(xlog_t *log,
2438 int len,
2439 xlog_in_core_t **iclogp,
2440 xlog_ticket_t *ticket,
2441 int *continued_write,
2442 int *logoffsetp)
2443{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int log_offset;
2445 xlog_rec_header_t *head;
2446 xlog_in_core_t *iclog;
2447 int error;
2448
2449restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002450 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002452 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return XFS_ERROR(EIO);
2454 }
2455
2456 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002457 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002459
2460 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002461 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 goto restart;
2463 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 head = &iclog->ic_header;
2466
David Chinner155cc6b2008-03-06 13:44:14 +11002467 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 log_offset = iclog->ic_offset;
2469
2470 /* On the 1st write to an iclog, figure out lsn. This works
2471 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2472 * committing to. If the offset is set, that's how many blocks
2473 * must be written.
2474 */
2475 if (log_offset == 0) {
2476 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002477 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002478 log->l_iclog_hsize,
2479 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002480 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2481 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002482 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 ASSERT(log->l_curr_block >= 0);
2484 }
2485
2486 /* If there is enough room to write everything, then do it. Otherwise,
2487 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2488 * bit is on, so this will get flushed out. Don't update ic_offset
2489 * until you know exactly how many bytes get copied. Therefore, wait
2490 * until later to update ic_offset.
2491 *
2492 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2493 * can fit into remaining data section.
2494 */
2495 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2496 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2497
Dave Chinner49641f12008-07-11 17:43:55 +10002498 /*
2499 * If I'm the only one writing to this iclog, sync it to disk.
2500 * We need to do an atomic compare and decrement here to avoid
2501 * racing with concurrent atomic_dec_and_lock() calls in
2502 * xlog_state_release_iclog() when there is more than one
2503 * reference to the iclog.
2504 */
2505 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2506 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002507 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002508 error = xlog_state_release_iclog(log, iclog);
2509 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002510 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002512 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514 goto restart;
2515 }
2516
2517 /* Do we have enough room to write the full amount in the remainder
2518 * of this iclog? Or must we continue a write on the next iclog and
2519 * mark this iclog as completely taken? In the case where we switch
2520 * iclogs (to mark it taken), this particular iclog will release/sync
2521 * to disk in xlog_write().
2522 */
2523 if (len <= iclog->ic_size - iclog->ic_offset) {
2524 *continued_write = 0;
2525 iclog->ic_offset += len;
2526 } else {
2527 *continued_write = 1;
2528 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2529 }
2530 *iclogp = iclog;
2531
2532 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002533 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 *logoffsetp = log_offset;
2536 return 0;
2537} /* xlog_state_get_iclog_space */
2538
2539/*
2540 * Atomically get the log space required for a log ticket.
2541 *
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002542 * Once a ticket gets put onto the reserveq, it will only return after the
2543 * needed reservation is satisfied.
Dave Chinner3f16b982010-12-21 12:29:01 +11002544 *
2545 * This function is structured so that it has a lock free fast path. This is
2546 * necessary because every new transaction reservation will come through this
2547 * path. Hence any lock will be globally hot if we take it unconditionally on
2548 * every pass.
2549 *
2550 * As tickets are only ever moved on and off the reserveq under the
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002551 * l_grant_reserve_lock, we only need to take that lock if we are going to add
2552 * the ticket to the queue and sleep. We can avoid taking the lock if the ticket
2553 * was never added to the reserveq because the t_queue list head will be empty
2554 * and we hold the only reference to it so it can safely be checked unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
2556STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002557xlog_grant_log_space(
2558 struct log *log,
2559 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002561 int free_bytes, need_bytes;
2562 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002564 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002566 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002568 /*
2569 * If there are other waiters on the queue then give them a chance at
2570 * logspace before us. Wake up the first waiters, if we do not wake
2571 * up all the waiters then go to sleep waiting for more free space,
2572 * otherwise try to get some space for this transaction.
2573 */
Dave Chinner3f16b982010-12-21 12:29:01 +11002574 need_bytes = tic->t_unit_res;
2575 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2576 need_bytes *= tic->t_ocnt;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002577 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Dave Chinner3f16b982010-12-21 12:29:01 +11002578 if (!list_empty_careful(&log->l_reserveq)) {
2579 spin_lock(&log->l_grant_reserve_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002580 if (!xlog_reserveq_wake(log, &free_bytes) ||
2581 free_bytes < need_bytes)
2582 error = xlog_reserveq_wait(log, tic, need_bytes);
2583 spin_unlock(&log->l_grant_reserve_lock);
2584 } else if (free_bytes < need_bytes) {
Dave Chinner3f16b982010-12-21 12:29:01 +11002585 spin_lock(&log->l_grant_reserve_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002586 error = xlog_reserveq_wait(log, tic, need_bytes);
Dave Chinner3f16b982010-12-21 12:29:01 +11002587 spin_unlock(&log->l_grant_reserve_lock);
2588 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002589 if (error)
2590 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Dave Chinnera69ed032010-12-21 12:08:20 +11002592 xlog_grant_add_space(log, &log->l_grant_reserve_head, need_bytes);
2593 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002594 trace_xfs_log_grant_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002595 xlog_verify_grant_tail(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599/*
2600 * Replenish the byte reservation required by moving the grant write head.
2601 *
Dave Chinner3f16b982010-12-21 12:29:01 +11002602 * Similar to xlog_grant_log_space, the function is structured to have a lock
2603 * free fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
2605STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002606xlog_regrant_write_log_space(
2607 struct log *log,
2608 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002610 int free_bytes, need_bytes;
2611 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002614 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002619 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002621 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002623 /*
2624 * If there are other waiters on the queue then give them a chance at
2625 * logspace before us. Wake up the first waiters, if we do not wake
2626 * up all the waiters then go to sleep waiting for more free space,
2627 * otherwise try to get some space for this transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002629 need_bytes = tic->t_unit_res;
Dave Chinnera69ed032010-12-21 12:08:20 +11002630 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002631 if (!list_empty_careful(&log->l_writeq)) {
Dave Chinner3f16b982010-12-21 12:29:01 +11002632 spin_lock(&log->l_grant_write_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002633 if (!xlog_writeq_wake(log, &free_bytes) ||
2634 free_bytes < need_bytes)
2635 error = xlog_writeq_wait(log, tic, need_bytes);
2636 spin_unlock(&log->l_grant_write_lock);
2637 } else if (free_bytes < need_bytes) {
Dave Chinner3f16b982010-12-21 12:29:01 +11002638 spin_lock(&log->l_grant_write_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002639 error = xlog_writeq_wait(log, tic, need_bytes);
Dave Chinner3f16b982010-12-21 12:29:01 +11002640 spin_unlock(&log->l_grant_write_lock);
2641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002643 if (error)
2644 return error;
2645
Dave Chinnera69ed032010-12-21 12:08:20 +11002646 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002647 trace_xfs_log_regrant_write_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002648 xlog_verify_grant_tail(log);
Jesper Juhl014c2542006-01-15 02:37:08 +01002649 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002650}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652/* The first cnt-1 times through here we don't need to
2653 * move the grant write head because the permanent
2654 * reservation has reserved cnt times the unit amount.
2655 * Release part of current permanent unit reservation and
2656 * reset current reservation to be one units worth. Also
2657 * move grant reservation head forward.
2658 */
2659STATIC void
2660xlog_regrant_reserve_log_space(xlog_t *log,
2661 xlog_ticket_t *ticket)
2662{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002663 trace_xfs_log_regrant_reserve_enter(log, ticket);
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (ticket->t_cnt > 0)
2666 ticket->t_cnt--;
2667
Dave Chinnera69ed032010-12-21 12:08:20 +11002668 xlog_grant_sub_space(log, &log->l_grant_reserve_head,
2669 ticket->t_curr_res);
2670 xlog_grant_sub_space(log, &log->l_grant_write_head,
2671 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002673 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002674
2675 trace_xfs_log_regrant_reserve_sub(log, ticket);
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /* just return if we still have some of the pre-reserved space */
Dave Chinnerd0eb2f32010-12-21 12:29:14 +11002678 if (ticket->t_cnt > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Dave Chinnera69ed032010-12-21 12:08:20 +11002681 xlog_grant_add_space(log, &log->l_grant_reserve_head,
2682 ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002683
2684 trace_xfs_log_regrant_reserve_exit(log, ticket);
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002687 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688} /* xlog_regrant_reserve_log_space */
2689
2690
2691/*
2692 * Give back the space left from a reservation.
2693 *
2694 * All the information we need to make a correct determination of space left
2695 * is present. For non-permanent reservations, things are quite easy. The
2696 * count should have been decremented to zero. We only need to deal with the
2697 * space remaining in the current reservation part of the ticket. If the
2698 * ticket contains a permanent reservation, there may be left over space which
2699 * needs to be released. A count of N means that N-1 refills of the current
2700 * reservation can be done before we need to ask for more space. The first
2701 * one goes to fill up the first current reservation. Once we run out of
2702 * space, the count will stay at zero and the only space remaining will be
2703 * in the current reservation field.
2704 */
2705STATIC void
2706xlog_ungrant_log_space(xlog_t *log,
2707 xlog_ticket_t *ticket)
2708{
Dave Chinner663e4962010-12-21 12:06:05 +11002709 int bytes;
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (ticket->t_cnt > 0)
2712 ticket->t_cnt--;
2713
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002714 trace_xfs_log_ungrant_enter(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002715 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Dave Chinner663e4962010-12-21 12:06:05 +11002717 /*
2718 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 * up more space based on the remaining count.
2720 */
Dave Chinner663e4962010-12-21 12:06:05 +11002721 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (ticket->t_cnt > 0) {
2723 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11002724 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726
Dave Chinnera69ed032010-12-21 12:08:20 +11002727 xlog_grant_sub_space(log, &log->l_grant_reserve_head, bytes);
2728 xlog_grant_sub_space(log, &log->l_grant_write_head, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11002729
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002730 trace_xfs_log_ungrant_exit(log, ticket);
2731
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +00002732 xfs_log_space_wake(log->l_mp);
Christoph Hellwig09a423a2012-02-20 02:31:20 +00002733}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 * Flush iclog to disk if this is the last reference to the given iclog and
2737 * the WANT_SYNC bit is set.
2738 *
2739 * When this function is entered, the iclog is not necessarily in the
2740 * WANT_SYNC state. It may be sitting around waiting to get filled.
2741 *
2742 *
2743 */
David Chinnera8272ce2007-11-23 16:28:09 +11002744STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002745xlog_state_release_iclog(
2746 xlog_t *log,
2747 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 int sync = 0; /* do we sync? */
2750
David Chinner155cc6b2008-03-06 13:44:14 +11002751 if (iclog->ic_state & XLOG_STATE_IOERROR)
2752 return XFS_ERROR(EIO);
2753
2754 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2755 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2756 return 0;
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002759 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return XFS_ERROR(EIO);
2761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2763 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2764
David Chinner155cc6b2008-03-06 13:44:14 +11002765 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002766 /* update tail before writing to iclog */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002767 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 sync++;
2769 iclog->ic_state = XLOG_STATE_SYNCING;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002770 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
2771 xlog_verify_tail_lsn(log, iclog, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 /* cycle incremented when incrementing curr_block */
2773 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002774 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 /*
2777 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002778 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2780 * this iclog has consistent data, so we ignore IOERROR
2781 * flags after this point.
2782 */
David Chinnerb5893342008-03-06 13:44:06 +11002783 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786} /* xlog_state_release_iclog */
2787
2788
2789/*
2790 * This routine will mark the current iclog in the ring as WANT_SYNC
2791 * and move the current iclog pointer to the next iclog in the ring.
2792 * When this routine is called from xlog_state_get_iclog_space(), the
2793 * exact size of the iclog has not yet been determined. All we know is
2794 * that every data block. We have run out of space in this log record.
2795 */
2796STATIC void
2797xlog_state_switch_iclogs(xlog_t *log,
2798 xlog_in_core_t *iclog,
2799 int eventual_size)
2800{
2801 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2802 if (!eventual_size)
2803 eventual_size = iclog->ic_offset;
2804 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002805 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 log->l_prev_block = log->l_curr_block;
2807 log->l_prev_cycle = log->l_curr_cycle;
2808
2809 /* roll log?: ic_offset changed later */
2810 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2811
2812 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002813 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 log->l_mp->m_sb.sb_logsunit > 1) {
2815 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2816 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2817 }
2818
2819 if (log->l_curr_block >= log->l_logBBsize) {
2820 log->l_curr_cycle++;
2821 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2822 log->l_curr_cycle++;
2823 log->l_curr_block -= log->l_logBBsize;
2824 ASSERT(log->l_curr_block >= 0);
2825 }
2826 ASSERT(iclog == log->l_iclog);
2827 log->l_iclog = iclog->ic_next;
2828} /* xlog_state_switch_iclogs */
2829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830/*
2831 * Write out all data in the in-core log as of this exact moment in time.
2832 *
2833 * Data may be written to the in-core log during this call. However,
2834 * we don't guarantee this data will be written out. A change from past
2835 * implementation means this routine will *not* write out zero length LRs.
2836 *
2837 * Basically, we try and perform an intelligent scan of the in-core logs.
2838 * If we determine there is no flushable data, we just return. There is no
2839 * flushable data if:
2840 *
2841 * 1. the current iclog is active and has no data; the previous iclog
2842 * is in the active or dirty state.
2843 * 2. the current iclog is drity, and the previous iclog is in the
2844 * active or dirty state.
2845 *
David Chinner12017fa2008-08-13 16:34:31 +10002846 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 *
2848 * 1. the current iclog is not in the active nor dirty state.
2849 * 2. the current iclog dirty, and the previous iclog is not in the
2850 * active nor dirty state.
2851 * 3. the current iclog is active, and there is another thread writing
2852 * to this particular iclog.
2853 * 4. a) the current iclog is active and has no other writers
2854 * b) when we return from flushing out this iclog, it is still
2855 * not in the active nor dirty state.
2856 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002857int
2858_xfs_log_force(
2859 struct xfs_mount *mp,
2860 uint flags,
2861 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002863 struct log *log = mp->m_log;
2864 struct xlog_in_core *iclog;
2865 xfs_lsn_t lsn;
2866
2867 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002869 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002870
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002871 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 iclog = log->l_iclog;
2874 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002875 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return XFS_ERROR(EIO);
2877 }
2878
2879 /* If the head iclog is not active nor dirty, we just attach
2880 * ourselves to the head and go to sleep.
2881 */
2882 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2883 iclog->ic_state == XLOG_STATE_DIRTY) {
2884 /*
2885 * If the head is dirty or (active and empty), then
2886 * we need to look at the previous iclog. If the previous
2887 * iclog is active or dirty we are done. There is nothing
2888 * to sync out. Otherwise, we attach ourselves to the
2889 * previous iclog and go to sleep.
2890 */
2891 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002892 (atomic_read(&iclog->ic_refcnt) == 0
2893 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 iclog = iclog->ic_prev;
2895 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2896 iclog->ic_state == XLOG_STATE_DIRTY)
2897 goto no_sleep;
2898 else
2899 goto maybe_sleep;
2900 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002901 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 /* We are the only one with access to this
2903 * iclog. Flush it out now. There should
2904 * be a roundoff of zero to show that someone
2905 * has already taken care of the roundoff from
2906 * the previous sync.
2907 */
David Chinner155cc6b2008-03-06 13:44:14 +11002908 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002909 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002911 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 if (xlog_state_release_iclog(log, iclog))
2914 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002915
2916 if (log_flushed)
2917 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002918 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002919 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 iclog->ic_state != XLOG_STATE_DIRTY)
2921 goto maybe_sleep;
2922 else
2923 goto no_sleep;
2924 } else {
2925 /* Someone else is writing to this iclog.
2926 * Use its call to flush out the data. However,
2927 * the other thread may not force out this LR,
2928 * so we mark it WANT_SYNC.
2929 */
2930 xlog_state_switch_iclogs(log, iclog, 0);
2931 goto maybe_sleep;
2932 }
2933 }
2934 }
2935
2936 /* By the time we come around again, the iclog could've been filled
2937 * which would give it another lsn. If we have a new lsn, just
2938 * return because the relevant data has been flushed.
2939 */
2940maybe_sleep:
2941 if (flags & XFS_LOG_SYNC) {
2942 /*
2943 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002944 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 * Then we check again after waking up, in case our
2946 * sleep was disturbed by a bad news.
2947 */
2948 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002949 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return XFS_ERROR(EIO);
2951 }
2952 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11002953 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 /*
2955 * No need to grab the log lock here since we're
2956 * only deciding whether or not to return EIO
2957 * and the memory read should be atomic.
2958 */
2959 if (iclog->ic_state & XLOG_STATE_IOERROR)
2960 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002961 if (log_flushed)
2962 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 } else {
2964
2965no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002966 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
2968 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002972 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2973 * about errors or whether the log was flushed or not. This is the normal
2974 * interface to use when trying to unpin items or move the log forward.
2975 */
2976void
2977xfs_log_force(
2978 xfs_mount_t *mp,
2979 uint flags)
2980{
2981 int error;
2982
2983 error = _xfs_log_force(mp, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002984 if (error)
2985 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002986}
2987
2988/*
2989 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 *
2991 * Find in-core log with lsn.
2992 * If it is in the DIRTY state, just return.
2993 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2994 * state and go to sleep or return.
2995 * If it is in any other state, go to sleep or return.
2996 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002997 * Synchronous forces are implemented with a signal variable. All callers
2998 * to force a given lsn to disk will wait on a the sv attached to the
2999 * specific in-core log. When given in-core log finally completes its
3000 * write to disk, that thread will wake up all threads waiting on the
3001 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003003int
3004_xfs_log_force_lsn(
3005 struct xfs_mount *mp,
3006 xfs_lsn_t lsn,
3007 uint flags,
3008 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003010 struct log *log = mp->m_log;
3011 struct xlog_in_core *iclog;
3012 int already_slept = 0;
3013
3014 ASSERT(lsn != 0);
3015
3016 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003018 lsn = xlog_cil_force_lsn(log, lsn);
3019 if (lsn == NULLCOMMITLSN)
3020 return 0;
Dave Chinner71e330b2010-05-21 14:37:18 +10003021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003023 spin_lock(&log->l_icloglock);
3024 iclog = log->l_iclog;
3025 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003026 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003027 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003030 do {
3031 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3032 iclog = iclog->ic_next;
3033 continue;
3034 }
3035
3036 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3037 spin_unlock(&log->l_icloglock);
3038 return 0;
3039 }
3040
3041 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3042 /*
3043 * We sleep here if we haven't already slept (e.g.
3044 * this is the first time we've looked at the correct
3045 * iclog buf) and the buffer before us is going to
3046 * be sync'ed. The reason for this is that if we
3047 * are doing sync transactions here, by waiting for
3048 * the previous I/O to complete, we can allow a few
3049 * more transactions into this iclog before we close
3050 * it down.
3051 *
3052 * Otherwise, we mark the buffer WANT_SYNC, and bump
3053 * up the refcnt so we can release the log (which
3054 * drops the ref count). The state switch keeps new
3055 * transaction commits from using this buffer. When
3056 * the current commits finish writing into the buffer,
3057 * the refcount will drop to zero and the buffer will
3058 * go out then.
3059 */
3060 if (!already_slept &&
3061 (iclog->ic_prev->ic_state &
3062 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3063 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3064
3065 XFS_STATS_INC(xs_log_force_sleep);
3066
Dave Chinnereb40a872010-12-21 12:09:01 +11003067 xlog_wait(&iclog->ic_prev->ic_write_wait,
3068 &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003069 if (log_flushed)
3070 *log_flushed = 1;
3071 already_slept = 1;
3072 goto try_again;
3073 }
David Chinner155cc6b2008-03-06 13:44:14 +11003074 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003076 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (xlog_state_release_iclog(log, iclog))
3078 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003079 if (log_flushed)
3080 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003081 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003084 if ((flags & XFS_LOG_SYNC) && /* sleep */
3085 !(iclog->ic_state &
3086 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3087 /*
3088 * Don't wait on completion if we know that we've
3089 * gotten a log write error.
3090 */
3091 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3092 spin_unlock(&log->l_icloglock);
3093 return XFS_ERROR(EIO);
3094 }
3095 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003096 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003097 /*
3098 * No need to grab the log lock here since we're
3099 * only deciding whether or not to return EIO
3100 * and the memory read should be atomic.
3101 */
3102 if (iclog->ic_state & XLOG_STATE_IOERROR)
3103 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003105 if (log_flushed)
3106 *log_flushed = 1;
3107 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003108 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003110
3111 return 0;
3112 } while (iclog != log->l_iclog);
3113
3114 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003118/*
3119 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3120 * about errors or whether the log was flushed or not. This is the normal
3121 * interface to use when trying to unpin items or move the log forward.
3122 */
3123void
3124xfs_log_force_lsn(
3125 xfs_mount_t *mp,
3126 xfs_lsn_t lsn,
3127 uint flags)
3128{
3129 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003131 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003132 if (error)
3133 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136/*
3137 * Called when we want to mark the current iclog as being ready to sync to
3138 * disk.
3139 */
David Chinnera8272ce2007-11-23 16:28:09 +11003140STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3142{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003143 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3146 xlog_state_switch_iclogs(log, iclog, 0);
3147 } else {
3148 ASSERT(iclog->ic_state &
3149 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3150 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153
3154/*****************************************************************************
3155 *
3156 * TICKET functions
3157 *
3158 *****************************************************************************
3159 */
3160
3161/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003162 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003164void
3165xfs_log_ticket_put(
3166 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003168 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003169 if (atomic_dec_and_test(&ticket->t_ref))
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003170 kmem_zone_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003173xlog_ticket_t *
3174xfs_log_ticket_get(
3175 xlog_ticket_t *ticket)
3176{
3177 ASSERT(atomic_read(&ticket->t_ref) > 0);
3178 atomic_inc(&ticket->t_ref);
3179 return ticket;
3180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003183 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003185xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003186xlog_ticket_alloc(
3187 struct log *log,
3188 int unit_bytes,
3189 int cnt,
3190 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003191 uint xflags,
3192 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003194 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003196 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Dave Chinner3383ca52010-05-07 11:04:17 +10003198 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003199 if (!tic)
3200 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 /*
3203 * Permanent reservations have up to 'cnt'-1 active log operations
3204 * in the log. A unit in this case is the amount of space for one
3205 * of these log operations. Normal reservations have a cnt of 1
3206 * and their unit amount is the total amount of space required.
3207 *
3208 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003209 * which occupy space in the on-disk log.
3210 *
3211 * Normal form of a transaction is:
3212 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3213 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3214 *
3215 * We need to account for all the leadup data and trailer data
3216 * around the transaction data.
3217 * And then we need to account for the worst case in terms of using
3218 * more space.
3219 * The worst case will happen if:
3220 * - the placement of the transaction happens to be such that the
3221 * roundoff is at its maximum
3222 * - the transaction data is synced before the commit record is synced
3223 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3224 * Therefore the commit record is in its own Log Record.
3225 * This can happen as the commit record is called with its
3226 * own region to xlog_write().
3227 * This then means that in the worst case, roundoff can happen for
3228 * the commit-rec as well.
3229 * The commit-rec is smaller than padding in this scenario and so it is
3230 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 */
3232
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003233 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003235 unit_bytes += sizeof(xfs_trans_header_t);
3236
3237 /* for start-rec */
3238 unit_bytes += sizeof(xlog_op_header_t);
3239
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003240 /*
3241 * for LR headers - the space for data in an iclog is the size minus
3242 * the space used for the headers. If we use the iclog size, then we
3243 * undercalculate the number of headers required.
3244 *
3245 * Furthermore - the addition of op headers for split-recs might
3246 * increase the space required enough to require more log and op
3247 * headers, so take that into account too.
3248 *
3249 * IMPORTANT: This reservation makes the assumption that if this
3250 * transaction is the first in an iclog and hence has the LR headers
3251 * accounted to it, then the remaining space in the iclog is
3252 * exclusively for this transaction. i.e. if the transaction is larger
3253 * than the iclog, it will be the only thing in that iclog.
3254 * Fundamentally, this means we must pass the entire log vector to
3255 * xlog_write to guarantee this.
3256 */
3257 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3258 num_headers = howmany(unit_bytes, iclog_space);
3259
3260 /* for split-recs - ophdrs added when data split over LRs */
3261 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3262
3263 /* add extra header reservations if we overrun */
3264 while (!num_headers ||
3265 howmany(unit_bytes, iclog_space) > num_headers) {
3266 unit_bytes += sizeof(xlog_op_header_t);
3267 num_headers++;
3268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 unit_bytes += log->l_iclog_hsize * num_headers;
3270
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003271 /* for commit-rec LR header - note: padding will subsume the ophdr */
3272 unit_bytes += log->l_iclog_hsize;
3273
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003274 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003275 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003276 log->l_mp->m_sb.sb_logsunit > 1) {
3277 /* log su roundoff */
3278 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3279 } else {
3280 /* BB roundoff */
3281 unit_bytes += 2*BBSIZE;
3282 }
3283
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003284 atomic_set(&tic->t_ref, 1);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003285 tic->t_task = current;
Dave Chinner10547942010-12-21 12:02:25 +11003286 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 tic->t_unit_res = unit_bytes;
3288 tic->t_curr_res = unit_bytes;
3289 tic->t_cnt = cnt;
3290 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003291 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 tic->t_clientid = client;
3293 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003294 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (xflags & XFS_LOG_PERM_RESERV)
3296 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Christoph Hellwig0adba532007-08-30 17:21:46 +10003298 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003301}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303
3304/******************************************************************************
3305 *
3306 * Log debug routines
3307 *
3308 ******************************************************************************
3309 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003310#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311/*
3312 * Make sure that the destination ptr is within the valid data region of
3313 * one of the iclogs. This uses backup pointers stored in a different
3314 * part of the log in case we trash the log structure.
3315 */
3316void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003317xlog_verify_dest_ptr(
3318 struct log *log,
3319 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 int i;
3322 int good_ptr = 0;
3323
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003324 for (i = 0; i < log->l_iclog_bufs; i++) {
3325 if (ptr >= log->l_iclog_bak[i] &&
3326 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 good_ptr++;
3328 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003329
3330 if (!good_ptr)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003331 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003334/*
3335 * Check to make sure the grant write head didn't just over lap the tail. If
3336 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3337 * the cycles differ by exactly one and check the byte count.
3338 *
3339 * This check is run unlocked, so can give false positives. Rather than assert
3340 * on failures, use a warn-once flag and a panic tag to allow the admin to
3341 * determine if they want to panic the machine when such an error occurs. For
3342 * debug kernels this will have the same effect as using an assert but, unlinke
3343 * an assert, it can be turned off at runtime.
3344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345STATIC void
Dave Chinner3f336c62010-12-21 12:02:52 +11003346xlog_verify_grant_tail(
3347 struct log *log)
3348{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003349 int tail_cycle, tail_blocks;
Dave Chinnera69ed032010-12-21 12:08:20 +11003350 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003351
Dave Chinnera69ed032010-12-21 12:08:20 +11003352 xlog_crack_grant_head(&log->l_grant_write_head, &cycle, &space);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003353 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3354 if (tail_cycle != cycle) {
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003355 if (cycle - 1 != tail_cycle &&
3356 !(log->l_flags & XLOG_TAIL_WARN)) {
3357 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3358 "%s: cycle - 1 != tail_cycle", __func__);
3359 log->l_flags |= XLOG_TAIL_WARN;
3360 }
3361
3362 if (space > BBTOB(tail_blocks) &&
3363 !(log->l_flags & XLOG_TAIL_WARN)) {
3364 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3365 "%s: space > BBTOB(tail_blocks)", __func__);
3366 log->l_flags |= XLOG_TAIL_WARN;
3367 }
Dave Chinner3f336c62010-12-21 12:02:52 +11003368 }
3369}
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371/* check if it will fit */
3372STATIC void
3373xlog_verify_tail_lsn(xlog_t *log,
3374 xlog_in_core_t *iclog,
3375 xfs_lsn_t tail_lsn)
3376{
3377 int blocks;
3378
3379 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3380 blocks =
3381 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3382 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003383 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 } else {
3385 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3386
3387 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003388 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3391 if (blocks < BTOBB(iclog->ic_offset) + 1)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003392 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 }
3394} /* xlog_verify_tail_lsn */
3395
3396/*
3397 * Perform a number of checks on the iclog before writing to disk.
3398 *
3399 * 1. Make sure the iclogs are still circular
3400 * 2. Make sure we have a good magic number
3401 * 3. Make sure we don't have magic numbers in the data
3402 * 4. Check fields of each log operation header for:
3403 * A. Valid client identifier
3404 * B. tid ptr value falls in valid ptr space (user space code)
3405 * C. Length in log record header is correct according to the
3406 * individual operation headers within record.
3407 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3408 * log, check the preceding blocks of the physical log to make sure all
3409 * the cycle numbers agree with the current cycle number.
3410 */
3411STATIC void
3412xlog_verify_iclog(xlog_t *log,
3413 xlog_in_core_t *iclog,
3414 int count,
3415 boolean_t syncing)
3416{
3417 xlog_op_header_t *ophead;
3418 xlog_in_core_t *icptr;
3419 xlog_in_core_2_t *xhdr;
3420 xfs_caddr_t ptr;
3421 xfs_caddr_t base_ptr;
3422 __psint_t field_offset;
3423 __uint8_t clientid;
3424 int len, i, j, k, op_len;
3425 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003428 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 icptr = log->l_iclog;
3430 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003431 if (icptr == NULL)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003432 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 icptr = icptr->ic_next;
3434 }
3435 if (icptr != log->l_iclog)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003436 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003437 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 /* check log magic numbers */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003440 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003441 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003443 ptr = (xfs_caddr_t) &iclog->ic_header;
3444 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 ptr += BBSIZE) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003446 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003447 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3448 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
3450
3451 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003452 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 ptr = iclog->ic_datap;
3454 base_ptr = ptr;
3455 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003456 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 for (i = 0; i < len; i++) {
3458 ophead = (xlog_op_header_t *)ptr;
3459
3460 /* clientid is only 1 byte */
3461 field_offset = (__psint_t)
3462 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3463 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3464 clientid = ophead->oh_clientid;
3465 } else {
3466 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3467 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3468 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3469 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003470 clientid = xlog_get_client_id(
3471 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003473 clientid = xlog_get_client_id(
3474 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
3476 }
3477 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003478 xfs_warn(log->l_mp,
3479 "%s: invalid clientid %d op 0x%p offset 0x%lx",
3480 __func__, clientid, ophead,
3481 (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483 /* check length */
3484 field_offset = (__psint_t)
3485 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3486 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003487 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 } else {
3489 idx = BTOBBT((__psint_t)&ophead->oh_len -
3490 (__psint_t)iclog->ic_datap);
3491 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3492 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3493 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003494 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003496 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498 }
3499 ptr += sizeof(xlog_op_header_t) + op_len;
3500 }
3501} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003505 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 */
3507STATIC int
3508xlog_state_ioerror(
3509 xlog_t *log)
3510{
3511 xlog_in_core_t *iclog, *ic;
3512
3513 iclog = log->l_iclog;
3514 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3515 /*
3516 * Mark all the incore logs IOERROR.
3517 * From now on, no log flushes will result.
3518 */
3519 ic = iclog;
3520 do {
3521 ic->ic_state = XLOG_STATE_IOERROR;
3522 ic = ic->ic_next;
3523 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
3526 /*
3527 * Return non-zero, if state transition has already happened.
3528 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003529 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530}
3531
3532/*
3533 * This is called from xfs_force_shutdown, when we're forcibly
3534 * shutting down the filesystem, typically because of an IO error.
3535 * Our main objectives here are to make sure that:
3536 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3537 * parties to find out, 'atomically'.
3538 * b. those who're sleeping on log reservations, pinned objects and
3539 * other resources get woken up, and be told the bad news.
3540 * c. nothing new gets queued up after (a) and (b) are done.
3541 * d. if !logerror, flush the iclogs to disk, then seal them off
3542 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003543 *
3544 * Note: for delayed logging the !logerror case needs to flush the regions
3545 * held in memory out to the iclogs before flushing them to disk. This needs
3546 * to be done before the log is marked as shutdown, otherwise the flush to the
3547 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 */
3549int
3550xfs_log_force_umount(
3551 struct xfs_mount *mp,
3552 int logerror)
3553{
3554 xlog_ticket_t *tic;
3555 xlog_t *log;
3556 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 log = mp->m_log;
3559
3560 /*
3561 * If this happens during log recovery, don't worry about
3562 * locking; the log isn't open for business yet.
3563 */
3564 if (!log ||
3565 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3566 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003567 if (mp->m_sb_bp)
3568 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571
3572 /*
3573 * Somebody could've already done the hard work for us.
3574 * No need to get locks for this.
3575 */
3576 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3577 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003578 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
3580 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003581
3582 /*
3583 * Flush the in memory commit item list before marking the log as
3584 * being shut down. We need to do it in this order to ensure all the
3585 * completed transactions are flushed to disk with the xfs_log_force()
3586 * call below.
3587 */
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003588 if (!logerror)
Dave Chinnera44f13e2010-08-24 11:40:03 +10003589 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 /*
Dave Chinner3f16b982010-12-21 12:29:01 +11003592 * mark the filesystem and the as in a shutdown state and wake
3593 * everybody up to tell them the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003595 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003597 if (mp->m_sb_bp)
3598 XFS_BUF_DONE(mp->m_sb_bp);
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 /*
3601 * This flag is sort of redundant because of the mount flag, but
3602 * it's good to maintain the separation between the log and the rest
3603 * of XFS.
3604 */
3605 log->l_flags |= XLOG_IO_ERROR;
3606
3607 /*
3608 * If we hit a log error, we want to mark all the iclogs IOERROR
3609 * while we're still holding the loglock.
3610 */
3611 if (logerror)
3612 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003613 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
3615 /*
Dave Chinner10547942010-12-21 12:02:25 +11003616 * We don't want anybody waiting for log reservations after this. That
3617 * means we have to wake up everybody queued up on reserveq as well as
3618 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3619 * we don't enqueue anything once the SHUTDOWN flag is set, and this
Dave Chinner3f16b982010-12-21 12:29:01 +11003620 * action is protected by the grant locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 */
Dave Chinner3f16b982010-12-21 12:29:01 +11003622 spin_lock(&log->l_grant_reserve_lock);
Dave Chinner10547942010-12-21 12:02:25 +11003623 list_for_each_entry(tic, &log->l_reserveq, t_queue)
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003624 wake_up_process(tic->t_task);
Dave Chinner3f16b982010-12-21 12:29:01 +11003625 spin_unlock(&log->l_grant_reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Dave Chinner3f16b982010-12-21 12:29:01 +11003627 spin_lock(&log->l_grant_write_lock);
Dave Chinner10547942010-12-21 12:02:25 +11003628 list_for_each_entry(tic, &log->l_writeq, t_queue)
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003629 wake_up_process(tic->t_task);
Dave Chinner3f16b982010-12-21 12:29:01 +11003630 spin_unlock(&log->l_grant_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003632 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ASSERT(!logerror);
3634 /*
3635 * Force the incore logs to disk before shutting the
3636 * log down completely.
3637 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003638 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3639
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003640 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003642 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 }
3644 /*
3645 * Wake up everybody waiting on xfs_log_force.
3646 * Callback all log item committed functions as if the
3647 * log writes were completed.
3648 */
3649 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3650
3651#ifdef XFSERRORDEBUG
3652 {
3653 xlog_in_core_t *iclog;
3654
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003655 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 iclog = log->l_iclog;
3657 do {
3658 ASSERT(iclog->ic_callback == 0);
3659 iclog = iclog->ic_next;
3660 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003661 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
3663#endif
3664 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003665 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666}
3667
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003668STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669xlog_iclogs_empty(xlog_t *log)
3670{
3671 xlog_in_core_t *iclog;
3672
3673 iclog = log->l_iclog;
3674 do {
3675 /* endianness does not matter here, zero is zero in
3676 * any language.
3677 */
3678 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 iclog = iclog->ic_next;
3681 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003682 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}