blob: 30fec0a2a213bef71f06c6afe23ecb78474f8466 [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 Hellwigc303c5b2012-02-20 02:31:26 +0000153STATIC void
154xlog_grant_head_init(
155 struct xlog_grant_head *head)
156{
157 xlog_assign_grant_head(&head->grant, 1, 0);
158 INIT_LIST_HEAD(&head->waiters);
159 spin_lock_init(&head->lock);
160}
161
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000162STATIC bool
163xlog_reserveq_wake(
164 struct log *log,
165 int *free_bytes)
166{
167 struct xlog_ticket *tic;
168 int need_bytes;
169
Christoph Hellwig28496962012-02-20 02:31:25 +0000170 list_for_each_entry(tic, &log->l_reserve_head.waiters, t_queue) {
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000171 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
172 need_bytes = tic->t_unit_res * tic->t_cnt;
173 else
174 need_bytes = tic->t_unit_res;
175
176 if (*free_bytes < need_bytes)
177 return false;
178 *free_bytes -= need_bytes;
179
180 trace_xfs_log_grant_wake_up(log, tic);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000181 wake_up_process(tic->t_task);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000182 }
183
184 return true;
185}
186
187STATIC bool
188xlog_writeq_wake(
189 struct log *log,
190 int *free_bytes)
191{
192 struct xlog_ticket *tic;
193 int need_bytes;
194
Christoph Hellwig28496962012-02-20 02:31:25 +0000195 list_for_each_entry(tic, &log->l_write_head.waiters, t_queue) {
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000196 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
197
198 need_bytes = tic->t_unit_res;
199
200 if (*free_bytes < need_bytes)
201 return false;
202 *free_bytes -= need_bytes;
203
204 trace_xfs_log_regrant_write_wake_up(log, tic);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000205 wake_up_process(tic->t_task);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000206 }
207
208 return true;
209}
210
211STATIC int
212xlog_reserveq_wait(
213 struct log *log,
214 struct xlog_ticket *tic,
215 int need_bytes)
216{
Christoph Hellwig28496962012-02-20 02:31:25 +0000217 list_add_tail(&tic->t_queue, &log->l_reserve_head.waiters);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000218
219 do {
220 if (XLOG_FORCED_SHUTDOWN(log))
221 goto shutdown;
222 xlog_grant_push_ail(log, need_bytes);
223
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000224 __set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig28496962012-02-20 02:31:25 +0000225 spin_unlock(&log->l_reserve_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000226
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000227 XFS_STATS_INC(xs_sleep_logspace);
228
229 trace_xfs_log_grant_sleep(log, tic);
230 schedule();
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000231 trace_xfs_log_grant_wake(log, tic);
232
Christoph Hellwig28496962012-02-20 02:31:25 +0000233 spin_lock(&log->l_reserve_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000234 if (XLOG_FORCED_SHUTDOWN(log))
235 goto shutdown;
Christoph Hellwig28496962012-02-20 02:31:25 +0000236 } while (xlog_space_left(log, &log->l_reserve_head.grant) < need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000237
238 list_del_init(&tic->t_queue);
239 return 0;
240shutdown:
241 list_del_init(&tic->t_queue);
242 return XFS_ERROR(EIO);
243}
244
245STATIC int
246xlog_writeq_wait(
247 struct log *log,
248 struct xlog_ticket *tic,
249 int need_bytes)
250{
Christoph Hellwig28496962012-02-20 02:31:25 +0000251 list_add_tail(&tic->t_queue, &log->l_write_head.waiters);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000252
253 do {
254 if (XLOG_FORCED_SHUTDOWN(log))
255 goto shutdown;
256 xlog_grant_push_ail(log, need_bytes);
257
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000258 __set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig28496962012-02-20 02:31:25 +0000259 spin_unlock(&log->l_write_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000260
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000261 XFS_STATS_INC(xs_sleep_logspace);
262
263 trace_xfs_log_regrant_write_sleep(log, tic);
264 schedule();
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000265 trace_xfs_log_regrant_write_wake(log, tic);
266
Christoph Hellwig28496962012-02-20 02:31:25 +0000267 spin_lock(&log->l_write_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000268 if (XLOG_FORCED_SHUTDOWN(log))
269 goto shutdown;
Christoph Hellwig28496962012-02-20 02:31:25 +0000270 } while (xlog_space_left(log, &log->l_write_head.grant) < need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000271
272 list_del_init(&tic->t_queue);
273 return 0;
274shutdown:
275 list_del_init(&tic->t_queue);
276 return XFS_ERROR(EIO);
277}
278
Christoph Hellwig0adba532007-08-30 17:21:46 +1000279static void
280xlog_tic_reset_res(xlog_ticket_t *tic)
281{
282 tic->t_res_num = 0;
283 tic->t_res_arr_sum = 0;
284 tic->t_res_num_ophdrs = 0;
285}
286
287static void
288xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
289{
290 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
291 /* add to overflow and start again */
292 tic->t_res_o_flow += tic->t_res_arr_sum;
293 tic->t_res_num = 0;
294 tic->t_res_arr_sum = 0;
295 }
296
297 tic->t_res_arr[tic->t_res_num].r_len = len;
298 tic->t_res_arr[tic->t_res_num].r_type = type;
299 tic->t_res_arr_sum += len;
300 tic->t_res_num++;
301}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * NOTES:
305 *
306 * 1. currblock field gets updated at startup and after in-core logs
307 * marked as with WANT_SYNC.
308 */
309
310/*
311 * This routine is called when a user of a log manager ticket is done with
312 * the reservation. If the ticket was ever used, then a commit record for
313 * the associated transaction is written out as a log operation header with
314 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
315 * a given ticket. If the ticket was one with a permanent reservation, then
316 * a few operations are done differently. Permanent reservation tickets by
317 * default don't release the reservation. They just commit the current
318 * transaction with the belief that the reservation is still needed. A flag
319 * must be passed in before permanent reservations are actually released.
320 * When these type of tickets are not released, they need to be set into
321 * the inited state again. By doing this, a start record will be written
322 * out when the next write occurs.
323 */
324xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000325xfs_log_done(
326 struct xfs_mount *mp,
327 struct xlog_ticket *ticket,
328 struct xlog_in_core **iclog,
329 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000331 struct log *log = mp->m_log;
332 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (XLOG_FORCED_SHUTDOWN(log) ||
335 /*
336 * If nothing was ever written, don't write out commit record.
337 * If we get an error, just continue and give back the log ticket.
338 */
339 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100340 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 lsn = (xfs_lsn_t) -1;
342 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
343 flags |= XFS_LOG_REL_PERM_RESERV;
344 }
345 }
346
347
348 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
349 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000350 trace_xfs_log_done_nonperm(log, ticket);
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000353 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * request has been made to release a permanent reservation.
355 */
356 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100357 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000359 trace_xfs_log_done_perm(log, ticket);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000362 /* If this ticket was a permanent reservation and we aren't
363 * trying to release it, reset the inited flags; so next time
364 * we write, a start record will be written out.
365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
373 * Attaches a new iclog I/O completion callback routine during
374 * transaction commit. If the log is in error state, a non-zero
375 * return code is handed back and the caller is responsible for
376 * executing the callback at an appropriate time.
377 */
378int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000379xfs_log_notify(
380 struct xfs_mount *mp,
381 struct xlog_in_core *iclog,
382 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000384 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
David Chinner114d23a2008-04-10 12:18:39 +1000386 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
388 if (!abortflg) {
389 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
390 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
391 cb->cb_next = NULL;
392 *(iclog->ic_callback_tail) = cb;
393 iclog->ic_callback_tail = &(cb->cb_next);
394 }
David Chinner114d23a2008-04-10 12:18:39 +1000395 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000400xfs_log_release_iclog(
401 struct xfs_mount *mp,
402 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000404 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000405 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100406 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 return 0;
410}
411
412/*
413 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
414 * to the reservation.
415 * 2. Potentially, push buffers at tail of log to disk.
416 *
417 * Each reservation is going to reserve extra space for a log record header.
418 * When writes happen to the on-disk log, we don't subtract the length of the
419 * log record header from any reservation. By wasting space in each
420 * reservation, we prevent over allocation problems.
421 */
422int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000423xfs_log_reserve(
424 struct xfs_mount *mp,
425 int unit_bytes,
426 int cnt,
427 struct xlog_ticket **ticket,
428 __uint8_t client,
429 uint flags,
430 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000432 struct log *log = mp->m_log;
433 struct xlog_ticket *internal_ticket;
434 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (XLOG_FORCED_SHUTDOWN(log))
439 return XFS_ERROR(EIO);
440
441 XFS_STATS_INC(xs_try_logspace);
442
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (*ticket != NULL) {
445 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000446 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000447
Dave Chinner524ee362010-05-07 11:05:05 +1000448 /*
449 * this is a new transaction on the ticket, so we need to
450 * change the transaction ID so that the next transaction has a
451 * different TID in the log. Just add one to the existing tid
452 * so that we can see chains of rolling transactions in the log
453 * easily.
454 */
455 internal_ticket->t_tid++;
456
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000457 trace_xfs_log_reserve(log, internal_ticket);
458
Dave Chinner2ced19c2010-12-21 12:09:20 +1100459 xlog_grant_push_ail(log, internal_ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 retval = xlog_regrant_write_log_space(log, internal_ticket);
461 } else {
462 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100463 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Dave Chinner3383ca52010-05-07 11:04:17 +1000464 client, flags,
465 KM_SLEEP|KM_MAYFAIL);
David Chinnereb01c9c2008-04-10 12:18:46 +1000466 if (!internal_ticket)
467 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000468 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000470
471 trace_xfs_log_reserve(log, internal_ticket);
472
Dave Chinner2ced19c2010-12-21 12:09:20 +1100473 xlog_grant_push_ail(log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 (internal_ticket->t_unit_res *
475 internal_ticket->t_cnt));
476 retval = xlog_grant_log_space(log, internal_ticket);
477 }
478
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000479 if (unlikely(retval)) {
480 /*
481 * If we are failing, make sure the ticket doesn't have any
482 * current reservations. We don't want to add this back
483 * when the ticket/ transaction gets cancelled.
484 */
485 internal_ticket->t_curr_res = 0;
486 /* ungrant will give back unit_res * t_cnt. */
487 internal_ticket->t_cnt = 0;
488 }
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return retval;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493
494/*
495 * Mount a log filesystem
496 *
497 * mp - ubiquitous xfs mount point structure
498 * log_target - buftarg of on-disk log device
499 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
500 * num_bblocks - Number of BBSIZE blocks in on-disk log
501 *
502 * Return error or zero.
503 */
504int
David Chinner249a8c12008-02-05 12:13:32 +1100505xfs_log_mount(
506 xfs_mount_t *mp,
507 xfs_buftarg_t *log_target,
508 xfs_daddr_t blk_offset,
509 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
David Chinner249a8c12008-02-05 12:13:32 +1100511 int error;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100514 xfs_notice(mp, "Mounting Filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100516 xfs_notice(mp,
517"Mounting filesystem in no-recovery mode. Filesystem will be inconsistent.");
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000518 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200522 if (IS_ERR(mp->m_log)) {
523 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100524 goto out;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
David Chinner249a8c12008-02-05 12:13:32 +1100528 * Initialize the AIL now we have a log.
529 */
David Chinner249a8c12008-02-05 12:13:32 +1100530 error = xfs_trans_ail_init(mp);
531 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100532 xfs_warn(mp, "AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100533 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100534 }
David Chinnera9c21c12008-10-30 17:39:35 +1100535 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100536
537 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * skip log recovery on a norecovery mount. pretend it all
539 * just worked.
540 */
541 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100542 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000545 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Eric Sandeen65be6052006-01-11 15:34:19 +1100547 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000550 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100552 xfs_warn(mp, "log mount/recovery failed: error %d",
553 error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100554 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 }
557
558 /* Normal transactions can now occur */
559 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
560
Dave Chinner71e330b2010-05-21 14:37:18 +1000561 /*
562 * Now the log has been fully initialised and we know were our
563 * space grant counters are, we can initialise the permanent ticket
564 * needed for delayed logging to work.
565 */
566 xlog_cil_init_post_recovery(mp->m_log);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100569
570out_destroy_ail:
571 xfs_trans_ail_destroy(mp);
572out_free_log:
573 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100574out:
David Chinner249a8c12008-02-05 12:13:32 +1100575 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578/*
579 * Finish the recovery of the file system. This is separate from
580 * the xfs_log_mount() call, because it depends on the code in
581 * xfs_mountfs() to read in the root and real-time bitmap inodes
582 * between calling xfs_log_mount() and here.
583 *
584 * mp - ubiquitous xfs mount point structure
585 */
586int
Christoph Hellwig42490232008-08-13 16:49:32 +1000587xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 int error;
590
591 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000592 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 else {
594 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000595 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
598 return error;
599}
600
601/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * Final log writes as part of unmount.
603 *
604 * Mark the filesystem clean as unmount happens. Note that during relocation
605 * this routine needs to be executed as part of source-bag while the
606 * deallocation must not be done until source-end.
607 */
608
609/*
610 * Unmount record used to have a string "Unmount filesystem--" in the
611 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
612 * We just write the magic number now since that particular field isn't
613 * currently architecture converted and "nUmount" is a bit foo.
614 * As far as I know, there weren't any dependencies on the old behaviour.
615 */
616
617int
618xfs_log_unmount_write(xfs_mount_t *mp)
619{
620 xlog_t *log = mp->m_log;
621 xlog_in_core_t *iclog;
622#ifdef DEBUG
623 xlog_in_core_t *first_iclog;
624#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000625 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 xfs_lsn_t lsn;
627 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * Don't write out unmount record on read-only mounts.
631 * Or, if we are doing a forced umount (typically because of IO errors).
632 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000633 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000636 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000637 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639#ifdef DEBUG
640 first_iclog = iclog = log->l_iclog;
641 do {
642 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
643 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
644 ASSERT(iclog->ic_offset == 0);
645 }
646 iclog = iclog->ic_next;
647 } while (iclog != first_iclog);
648#endif
649 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000650 error = xfs_log_reserve(mp, 600, 1, &tic,
651 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100653 /* the data section must be 32 bit size aligned */
654 struct {
655 __uint16_t magic;
656 __uint16_t pad1;
657 __uint32_t pad2; /* may as well make it 64 bits */
658 } magic = {
659 .magic = XLOG_UNMOUNT_TYPE,
660 };
661 struct xfs_log_iovec reg = {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000662 .i_addr = &magic,
Dave Chinner55b66332010-03-23 11:43:17 +1100663 .i_len = sizeof(magic),
664 .i_type = XLOG_REG_TYPE_UNMOUNT,
665 };
666 struct xfs_log_vec vec = {
667 .lv_niovecs = 1,
668 .lv_iovecp = &reg,
669 };
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100672 tic->t_flags = 0;
673 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 NULL, XLOG_UNMOUNT_TRANS);
675 /*
676 * At this point, we're umounting anyway,
677 * so there's no point in transitioning log state
678 * to IOERROR. Just continue...
679 */
680 }
681
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100682 if (error)
683 xfs_alert(mp, "%s: unmount record failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000686 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100688 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100690 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000691 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000693 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
695 iclog->ic_state == XLOG_STATE_DIRTY)) {
696 if (!XLOG_FORCED_SHUTDOWN(log)) {
Dave Chinnereb40a872010-12-21 12:09:01 +1100697 xlog_wait(&iclog->ic_force_wait,
698 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000700 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000703 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000705 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000706 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000707 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100708 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 } else {
711 /*
712 * We're already in forced_shutdown mode, couldn't
713 * even attempt to write out the unmount transaction.
714 *
715 * Go through the motions of sync'ing and releasing
716 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000717 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * be in progress. Do this as a separate section of
719 * code so we'll know if we ever get stuck here that
720 * we're in this odd situation of trying to unmount
721 * a file system that went into forced_shutdown as
722 * the result of an unmount..
723 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000724 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100726 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100729 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000730 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000732 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
735 || iclog->ic_state == XLOG_STATE_DIRTY
736 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
737
Dave Chinnereb40a872010-12-21 12:09:01 +1100738 xlog_wait(&iclog->ic_force_wait,
739 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000741 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 }
744
David Chinner1bb7d6b2008-04-10 12:24:38 +1000745 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746} /* xfs_log_unmount_write */
747
748/*
749 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100750 *
751 * We need to stop the aild from running before we destroy
752 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100755xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
David Chinner249a8c12008-02-05 12:13:32 +1100757 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000758 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Dave Chinner43f5efc2010-03-23 10:10:00 +1100761void
762xfs_log_item_init(
763 struct xfs_mount *mp,
764 struct xfs_log_item *item,
765 int type,
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000766 const struct xfs_item_ops *ops)
Dave Chinner43f5efc2010-03-23 10:10:00 +1100767{
768 item->li_mountp = mp;
769 item->li_ailp = mp->m_ail;
770 item->li_type = type;
771 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +1000772 item->li_lv = NULL;
773
774 INIT_LIST_HEAD(&item->li_ail);
775 INIT_LIST_HEAD(&item->li_cil);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100776}
777
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000778/*
779 * Wake up processes waiting for log space after we have moved the log tail.
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781void
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000782xfs_log_space_wake(
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000783 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000785 struct log *log = mp->m_log;
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000786 int free_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (XLOG_FORCED_SHUTDOWN(log))
789 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Christoph Hellwig28496962012-02-20 02:31:25 +0000791 if (!list_empty_careful(&log->l_write_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000792 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
793
Christoph Hellwig28496962012-02-20 02:31:25 +0000794 spin_lock(&log->l_write_head.lock);
795 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000796 xlog_writeq_wake(log, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +0000797 spin_unlock(&log->l_write_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Dave Chinner10547942010-12-21 12:02:25 +1100799
Christoph Hellwig28496962012-02-20 02:31:25 +0000800 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000801 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
802
Christoph Hellwig28496962012-02-20 02:31:25 +0000803 spin_lock(&log->l_reserve_head.lock);
804 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000805 xlog_reserveq_wake(log, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +0000806 spin_unlock(&log->l_reserve_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Dave Chinner3f16b982010-12-21 12:29:01 +1100808}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810/*
811 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000812 * that needs to be covered. To begin the transition to the idle state
813 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
814 * If we are then in a state where covering is needed, the caller is informed
815 * that dummy transactions are required to move the log into the idle state.
816 *
817 * Because this is called as part of the sync process, we should also indicate
818 * that dummy transactions should be issued in anything but the covered or
819 * idle states. This ensures that the log tail is accurately reflected in
820 * the log at the end of the sync, hence if a crash occurrs avoids replay
821 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823int
824xfs_log_need_covered(xfs_mount_t *mp)
825{
David Chinner27d8d5f2008-10-30 17:38:39 +1100826 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
David Chinner92821e22007-05-24 15:26:31 +1000829 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000832 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000833 switch (log->l_covered_state) {
834 case XLOG_STATE_COVER_DONE:
835 case XLOG_STATE_COVER_DONE2:
836 case XLOG_STATE_COVER_IDLE:
837 break;
838 case XLOG_STATE_COVER_NEED:
839 case XLOG_STATE_COVER_NEED2:
Dave Chinnerfd074842011-04-08 12:45:07 +1000840 if (!xfs_ail_min_lsn(log->l_ailp) &&
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000841 xlog_iclogs_empty(log)) {
842 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
843 log->l_covered_state = XLOG_STATE_COVER_DONE;
844 else
845 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000847 /* FALLTHRU */
848 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000850 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000852 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100853 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000856/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * We may be holding the log iclog lock upon entering this routine.
858 */
859xfs_lsn_t
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100860xlog_assign_tail_lsn(
861 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100863 xfs_lsn_t tail_lsn;
864 struct log *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000866 /*
867 * To make sure we always have a valid LSN for the log tail we keep
868 * track of the last LSN which was committed in log->l_last_sync_lsn,
869 * and use that when the AIL was empty and xfs_ail_min_lsn returns 0.
870 *
871 * If the AIL has been emptied we also need to wake any process
872 * waiting for this condition.
873 */
Dave Chinnerfd074842011-04-08 12:45:07 +1000874 tail_lsn = xfs_ail_min_lsn(mp->m_ail);
Dave Chinner84f3c682010-12-03 22:11:29 +1100875 if (!tail_lsn)
876 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100877 atomic64_set(&log->l_tail_lsn, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return tail_lsn;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100879}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881/*
882 * Return the space in the log between the tail and the head. The head
883 * is passed in the cycle/bytes formal parms. In the special case where
884 * the reserve head has wrapped passed the tail, this calculation is no
885 * longer valid. In this case, just return 0 which means there is no space
886 * in the log. This works for all places where this function is called
887 * with the reserve head. Of course, if the write head were to ever
888 * wrap the tail, we should blow up. Rather than catch this case here,
889 * we depend on other ASSERTions in other parts of the code. XXXmiken
890 *
891 * This code also handles the case where the reservation head is behind
892 * the tail. The details of this case are described below, but the end
893 * result is that we return the size of the log as the amount of space left.
894 */
David Chinnera8272ce2007-11-23 16:28:09 +1100895STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +1100896xlog_space_left(
897 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100898 atomic64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Dave Chinnera69ed032010-12-21 12:08:20 +1100900 int free_bytes;
901 int tail_bytes;
902 int tail_cycle;
903 int head_cycle;
904 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Dave Chinnera69ed032010-12-21 12:08:20 +1100906 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100907 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
908 tail_bytes = BBTOB(tail_bytes);
Dave Chinnera69ed032010-12-21 12:08:20 +1100909 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
910 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
911 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +1100913 else if (tail_cycle < head_cycle) {
914 ASSERT(tail_cycle == (head_cycle - 1));
915 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 } else {
917 /*
918 * The reservation head is behind the tail.
919 * In this case we just want to return the size of the
920 * log as the amount of space left.
921 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100922 xfs_alert(log->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 "xlog_space_left: head behind tail\n"
924 " tail_cycle = %d, tail_bytes = %d\n"
925 " GH cycle = %d, GH bytes = %d",
Dave Chinnera69ed032010-12-21 12:08:20 +1100926 tail_cycle, tail_bytes, head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 ASSERT(0);
928 free_bytes = log->l_logsize;
929 }
930 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +1100931}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933
934/*
935 * Log function which is called when an io completes.
936 *
937 * The log manager needs its own routine, in order to control what
938 * happens with the buffer after the write completes.
939 */
940void
941xlog_iodone(xfs_buf_t *bp)
942{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200943 xlog_in_core_t *iclog = bp->b_fspriv;
944 xlog_t *l = iclog->ic_log;
945 int aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /*
948 * Race to shutdown the filesystem if we see an error.
949 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000950 if (XFS_TEST_ERROR((xfs_buf_geterror(bp)), l->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
Christoph Hellwig901796a2011-10-10 16:52:49 +0000952 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000953 xfs_buf_stale(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000954 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /*
956 * This flag will be propagated to the trans-committed
957 * callback routines to let them know that the log-commit
958 * didn't succeed.
959 */
960 aborted = XFS_LI_ABORTED;
961 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
962 aborted = XFS_LI_ABORTED;
963 }
David Chinner3db296f2007-05-14 18:24:16 +1000964
965 /* log I/O is always issued ASYNC */
966 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000968 /*
969 * do not reference the buffer (bp) here as we could race
970 * with it being freed after writing the unmount record to the
971 * log.
972 */
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974} /* xlog_iodone */
975
976/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 * Return size of each in-core log record buffer.
978 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200979 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 *
981 * If the filesystem blocksize is too large, we may need to choose a
982 * larger size since the directory code currently logs entire blocks.
983 */
984
985STATIC void
986xlog_get_iclog_buffer_size(xfs_mount_t *mp,
987 xlog_t *log)
988{
989 int size;
990 int xhdrs;
991
Eric Sandeen1cb51252007-08-16 16:24:43 +1000992 if (mp->m_logbufs <= 0)
993 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
994 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100995 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /*
998 * Buffer size passed in from mount system call.
999 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001000 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 size = log->l_iclog_size = mp->m_logbsize;
1002 log->l_iclog_size_log = 0;
1003 while (size != 1) {
1004 log->l_iclog_size_log++;
1005 size >>= 1;
1006 }
1007
Eric Sandeen62118702008-03-06 13:44:28 +11001008 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001009 /* # headers = size / 32k
1010 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 */
1012
1013 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1014 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1015 xhdrs++;
1016 log->l_iclog_hsize = xhdrs << BBSHIFT;
1017 log->l_iclog_heads = xhdrs;
1018 } else {
1019 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1020 log->l_iclog_hsize = BBSIZE;
1021 log->l_iclog_heads = 1;
1022 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001023 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001026 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001027 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1028 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* the default log size is 16k or 32k which is one header sector */
1031 log->l_iclog_hsize = BBSIZE;
1032 log->l_iclog_heads = 1;
1033
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001034done:
1035 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001036 if (mp->m_logbufs == 0)
1037 mp->m_logbufs = log->l_iclog_bufs;
1038 if (mp->m_logbsize == 0)
1039 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040} /* xlog_get_iclog_buffer_size */
1041
1042
1043/*
1044 * This routine initializes some of the log structure for a given mount point.
1045 * Its primary purpose is to fill in enough, so recovery can occur. However,
1046 * some other stuff may be filled in too.
1047 */
1048STATIC xlog_t *
1049xlog_alloc_log(xfs_mount_t *mp,
1050 xfs_buftarg_t *log_target,
1051 xfs_daddr_t blk_offset,
1052 int num_bblks)
1053{
1054 xlog_t *log;
1055 xlog_rec_header_t *head;
1056 xlog_in_core_t **iclogp;
1057 xlog_in_core_t *iclog, *prev_iclog=NULL;
1058 xfs_buf_t *bp;
1059 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001060 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001061 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Dave Chinner644c3562008-11-10 16:50:24 +11001063 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001064 if (!log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001065 xfs_warn(mp, "Log allocation failed: No memory!");
Dave Chinnera6cb7672009-04-06 18:39:27 +02001066 goto out;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 log->l_mp = mp;
1070 log->l_targ = log_target;
1071 log->l_logsize = BBTOB(num_bblks);
1072 log->l_logBBstart = blk_offset;
1073 log->l_logBBsize = num_bblks;
1074 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1075 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1076
1077 log->l_prev_block = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001079 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1080 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Christoph Hellwigc303c5b2012-02-20 02:31:26 +00001082
1083 xlog_grant_head_init(&log->l_reserve_head);
1084 xlog_grant_head_init(&log->l_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Dave Chinnera6cb7672009-04-06 18:39:27 +02001086 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001087 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001088 log2_size = mp->m_sb.sb_logsectlog;
1089 if (log2_size < BBSHIFT) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001090 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1091 log2_size, BBSHIFT);
Alex Elder69ce58f2010-04-20 17:09:59 +10001092 goto out_free_log;
1093 }
1094
1095 log2_size -= BBSHIFT;
1096 if (log2_size > mp->m_sectbb_log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001097 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1098 log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001099 goto out_free_log;
1100 }
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001103 if (log2_size && log->l_logBBstart > 0 &&
1104 !xfs_sb_version_haslogv2(&mp->m_sb)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001105 xfs_warn(mp,
1106 "log sector size (0x%x) invalid for configuration.",
1107 log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001108 goto out_free_log;
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001111 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 xlog_get_iclog_buffer_size(mp, log);
1114
Dave Chinnera6cb7672009-04-06 18:39:27 +02001115 error = ENOMEM;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +00001116 bp = xfs_buf_alloc(mp->m_logdev_targp, 0, log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001117 if (!bp)
1118 goto out_free_log;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001119 bp->b_iodone = xlog_iodone;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001120 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 log->l_xbuf = bp;
1122
Eric Sandeen007c61c2007-10-11 17:43:56 +10001123 spin_lock_init(&log->l_icloglock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001124 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1127 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1128
1129 iclogp = &log->l_iclog;
1130 /*
1131 * The amount of memory to allocate for the iclog structure is
1132 * rather funky due to the way the structure is defined. It is
1133 * done this way so that we can use different sizes for machines
1134 * with different amounts of memory. See the definition of
1135 * xlog_in_core_t in xfs_log_priv.h for details.
1136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 ASSERT(log->l_iclog_size >= 4096);
1138 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001139 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1140 if (!*iclogp)
1141 goto out_free_iclog;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 iclog->ic_prev = prev_iclog;
1145 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001146
Dave Chinner686865f2010-09-24 20:07:47 +10001147 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1148 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001149 if (!bp)
1150 goto out_free_iclog;
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001151
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001152 bp->b_iodone = xlog_iodone;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001153 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001154 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001155#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 head = &iclog->ic_header;
1159 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001160 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1161 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001162 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001163 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001165 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1169 iclog->ic_state = XLOG_STATE_ACTIVE;
1170 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001171 atomic_set(&iclog->ic_refcnt, 0);
1172 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001174 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001176 ASSERT(xfs_buf_islocked(iclog->ic_bp));
Dave Chinnereb40a872010-12-21 12:09:01 +11001177 init_waitqueue_head(&iclog->ic_force_wait);
1178 init_waitqueue_head(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 iclogp = &iclog->ic_next;
1181 }
1182 *iclogp = log->l_iclog; /* complete ring */
1183 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1184
Dave Chinner71e330b2010-05-21 14:37:18 +10001185 error = xlog_cil_init(log);
1186 if (error)
1187 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001189
1190out_free_iclog:
1191 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1192 prev_iclog = iclog->ic_next;
Dave Chinnereb40a872010-12-21 12:09:01 +11001193 if (iclog->ic_bp)
Dave Chinner644c3562008-11-10 16:50:24 +11001194 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001195 kmem_free(iclog);
1196 }
1197 spinlock_destroy(&log->l_icloglock);
Dave Chinner644c3562008-11-10 16:50:24 +11001198 xfs_buf_free(log->l_xbuf);
1199out_free_log:
1200 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001201out:
1202 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203} /* xlog_alloc_log */
1204
1205
1206/*
1207 * Write out the commit record of a transaction associated with the given
1208 * ticket. Return the lsn of the commit record.
1209 */
1210STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001211xlog_commit_record(
1212 struct log *log,
1213 struct xlog_ticket *ticket,
1214 struct xlog_in_core **iclog,
1215 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Dave Chinner55b66332010-03-23 11:43:17 +11001217 struct xfs_mount *mp = log->l_mp;
1218 int error;
1219 struct xfs_log_iovec reg = {
1220 .i_addr = NULL,
1221 .i_len = 0,
1222 .i_type = XLOG_REG_TYPE_COMMIT,
1223 };
1224 struct xfs_log_vec vec = {
1225 .lv_niovecs = 1,
1226 .lv_iovecp = &reg,
1227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001230 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1231 XLOG_COMMIT_TRANS);
1232 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001233 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001234 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237/*
1238 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1239 * log space. This code pushes on the lsn which would supposedly free up
1240 * the 25% which we want to leave free. We may need to adopt a policy which
1241 * pushes on an lsn which is further along in the log once we reach the high
1242 * water mark. In this manner, we would be creating a low water mark.
1243 */
David Chinnera8272ce2007-11-23 16:28:09 +11001244STATIC void
Dave Chinner2ced19c2010-12-21 12:09:20 +11001245xlog_grant_push_ail(
1246 struct log *log,
1247 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001249 xfs_lsn_t threshold_lsn = 0;
Dave Chinner84f3c682010-12-03 22:11:29 +11001250 xfs_lsn_t last_sync_lsn;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001251 int free_blocks;
1252 int free_bytes;
1253 int threshold_block;
1254 int threshold_cycle;
1255 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Dave Chinner2ced19c2010-12-21 12:09:20 +11001257 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Christoph Hellwig28496962012-02-20 02:31:25 +00001259 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001260 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Dave Chinner2ced19c2010-12-21 12:09:20 +11001262 /*
1263 * Set the threshold for the minimum number of free blocks in the
1264 * log to the maximum of what the caller needs, one quarter of the
1265 * log, and 256 blocks.
1266 */
1267 free_threshold = BTOBB(need_bytes);
1268 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1269 free_threshold = MAX(free_threshold, 256);
1270 if (free_blocks >= free_threshold)
1271 return;
1272
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001273 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1274 &threshold_block);
1275 threshold_block += free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001277 threshold_block -= log->l_logBBsize;
1278 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001280 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1281 threshold_block);
1282 /*
1283 * Don't pass in an lsn greater than the lsn of the last
Dave Chinner84f3c682010-12-03 22:11:29 +11001284 * log record known to be on disk. Use a snapshot of the last sync lsn
1285 * so that it doesn't change between the compare and the set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
Dave Chinner84f3c682010-12-03 22:11:29 +11001287 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1288 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1289 threshold_lsn = last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Dave Chinner2ced19c2010-12-21 12:09:20 +11001291 /*
1292 * Get the transaction layer to kick the dirty buffers out to
1293 * disk asynchronously. No point in trying to do this if
1294 * the filesystem is shutting down.
1295 */
1296 if (!XLOG_FORCED_SHUTDOWN(log))
Dave Chinnerfd074842011-04-08 12:45:07 +10001297 xfs_ail_push(log->l_ailp, threshold_lsn);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Christoph Hellwig873ff552010-01-13 22:17:57 +00001300/*
1301 * The bdstrat callback function for log bufs. This gives us a central
1302 * place to trap bufs in case we get hit by a log I/O error and need to
1303 * shutdown. Actually, in practice, even when we didn't get a log error,
1304 * we transition the iclogs to IOERROR state *after* flushing all existing
1305 * iclogs to disk. This is because we don't want anymore new transactions to be
1306 * started or completed afterwards.
1307 */
1308STATIC int
1309xlog_bdstrat(
1310 struct xfs_buf *bp)
1311{
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001312 struct xlog_in_core *iclog = bp->b_fspriv;
Christoph Hellwig873ff552010-01-13 22:17:57 +00001313
Christoph Hellwig873ff552010-01-13 22:17:57 +00001314 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001315 xfs_buf_ioerror(bp, EIO);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001316 xfs_buf_stale(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001317 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001318 /*
1319 * It would seem logical to return EIO here, but we rely on
1320 * the log state machine to propagate I/O errors instead of
1321 * doing it here.
1322 */
1323 return 0;
1324 }
1325
Christoph Hellwig873ff552010-01-13 22:17:57 +00001326 xfs_buf_iorequest(bp);
1327 return 0;
1328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330/*
1331 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1332 * fashion. Previously, we should have moved the current iclog
1333 * ptr in the log to point to the next available iclog. This allows further
1334 * write to continue while this code syncs out an iclog ready to go.
1335 * Before an in-core log can be written out, the data section must be scanned
1336 * to save away the 1st word of each BBSIZE block into the header. We replace
1337 * it with the current cycle count. Each BBSIZE block is tagged with the
1338 * cycle count because there in an implicit assumption that drives will
1339 * guarantee that entire 512 byte blocks get written at once. In other words,
1340 * we can't have part of a 512 byte block written and part not written. By
1341 * tagging each block, we will know which blocks are valid when recovering
1342 * after an unclean shutdown.
1343 *
1344 * This routine is single threaded on the iclog. No other thread can be in
1345 * this routine with the same iclog. Changing contents of iclog can there-
1346 * fore be done without grabbing the state machine lock. Updating the global
1347 * log will require grabbing the lock though.
1348 *
1349 * The entire log manager uses a logical block numbering scheme. Only
1350 * log_sync (and then only bwrite()) know about the fact that the log may
1351 * not start with block zero on a given device. The log block start offset
1352 * is added immediately before calling bwrite().
1353 */
1354
David Chinnera8272ce2007-11-23 16:28:09 +11001355STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356xlog_sync(xlog_t *log,
1357 xlog_in_core_t *iclog)
1358{
1359 xfs_caddr_t dptr; /* pointer to byte sized element */
1360 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001361 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 uint count; /* byte count of bwrite */
1363 uint count_init; /* initial count before roundup */
1364 int roundoff; /* roundoff to BB or stripe */
1365 int split = 0; /* split write into two regions */
1366 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001367 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001370 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* Add for LR header */
1373 count_init = log->l_iclog_hsize + iclog->ic_offset;
1374
1375 /* Round out the log write size */
1376 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1377 /* we have a v2 stripe unit to use */
1378 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1379 } else {
1380 count = BBTOB(BTOBB(count_init));
1381 }
1382 roundoff = count - count_init;
1383 ASSERT(roundoff >= 0);
1384 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1385 roundoff < log->l_mp->m_sb.sb_logsunit)
1386 ||
1387 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1388 roundoff < BBTOB(1)));
1389
1390 /* move grant heads by roundoff in sync */
Christoph Hellwig28496962012-02-20 02:31:25 +00001391 xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
1392 xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* put cycle number in every block */
1395 xlog_pack_data(log, iclog, roundoff);
1396
1397 /* real byte length */
1398 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001399 iclog->ic_header.h_len =
1400 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001402 iclog->ic_header.h_len =
1403 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
Nathan Scottf5faad72006-07-28 17:04:44 +10001406 bp = iclog->ic_bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001407 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1410
1411 /* Do we need to split this write into 2 parts? */
1412 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1413 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1414 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1415 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1416 } else {
1417 iclog->ic_bwritecnt = 1;
1418 }
David Chinner511105b2007-05-24 15:21:57 +10001419 XFS_BUF_SET_COUNT(bp, count);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001420 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001421 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001423 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001424
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001425 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) {
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001426 bp->b_flags |= XBF_FUA;
1427
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001428 /*
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001429 * Flush the data device before flushing the log to make
1430 * sure all meta data written back from the AIL actually made
1431 * it to disk before stamping the new log tail LSN into the
1432 * log buffer. For an external log we need to issue the
1433 * flush explicitly, and unfortunately synchronously here;
1434 * for an internal log we can simply use the block layer
1435 * state machine for preflushes.
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001436 */
1437 if (log->l_mp->m_logdev_targp != log->l_mp->m_ddev_targp)
1438 xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001439 else
1440 bp->b_flags |= XBF_FLUSH;
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1444 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1445
1446 xlog_verify_iclog(log, iclog, count, B_TRUE);
1447
1448 /* account for log which doesn't start at block #0 */
1449 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1450 /*
1451 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1452 * is shutting down.
1453 */
1454 XFS_BUF_WRITE(bp);
1455
Christoph Hellwig901796a2011-10-10 16:52:49 +00001456 error = xlog_bdstrat(bp);
1457 if (error) {
1458 xfs_buf_ioerror_alert(bp, "xlog_sync");
Jesper Juhl014c2542006-01-15 02:37:08 +01001459 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001462 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
Chandra Seetharaman02fe03d2011-07-22 23:40:22 +00001464 xfs_buf_associate_memory(bp,
1465 (char *)&iclog->ic_header + count, split);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001466 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001467 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001469 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001470 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001471 bp->b_flags |= XBF_FUA;
Chandra Seetharaman62926042011-07-22 23:40:15 +00001472 dptr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /*
1474 * Bump the cycle numbers at the start of each block
1475 * since this part of the buffer is at the start of
1476 * a new cycle. Watch out for the header magic number
1477 * case, though.
1478 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001479 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001480 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001481 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001482 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 dptr += BBSIZE;
1484 }
1485
1486 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1487 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1488
Nathan Scottc41564b2006-03-29 08:55:14 +10001489 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1491 XFS_BUF_WRITE(bp);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001492 error = xlog_bdstrat(bp);
1493 if (error) {
1494 xfs_buf_ioerror_alert(bp, "xlog_sync (split)");
Jesper Juhl014c2542006-01-15 02:37:08 +01001495 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499} /* xlog_sync */
1500
1501
1502/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001503 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
David Chinnera8272ce2007-11-23 16:28:09 +11001505STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001506xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 int i;
1510
Dave Chinner71e330b2010-05-21 14:37:18 +10001511 xlog_cil_destroy(log);
1512
Dave Chinner44396472011-04-21 09:34:27 +00001513 /*
1514 * always need to ensure that the extra buffer does not point to memory
1515 * owned by another log buffer before we free it.
1516 */
1517 xfs_buf_set_empty(log->l_xbuf, log->l_iclog_size);
1518 xfs_buf_free(log->l_xbuf);
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 iclog = log->l_iclog;
1521 for (i=0; i<log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001524 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 iclog = next_iclog;
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 spinlock_destroy(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001530 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001531} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533/*
1534 * Update counters atomically now that memcpy is done.
1535 */
1536/* ARGSUSED */
1537static inline void
1538xlog_state_finish_copy(xlog_t *log,
1539 xlog_in_core_t *iclog,
1540 int record_cnt,
1541 int copy_bytes)
1542{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001543 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001545 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 iclog->ic_offset += copy_bytes;
1547
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001548 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549} /* xlog_state_finish_copy */
1550
1551
1552
1553
1554/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001555 * print out info relating to regions written which consume
1556 * the reservation
1557 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001558void
1559xlog_print_tic_res(
1560 struct xfs_mount *mp,
1561 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001562{
1563 uint i;
1564 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1565
1566 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1567 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1568 "bformat",
1569 "bchunk",
1570 "efi_format",
1571 "efd_format",
1572 "iformat",
1573 "icore",
1574 "iext",
1575 "ibroot",
1576 "ilocal",
1577 "iattr_ext",
1578 "iattr_broot",
1579 "iattr_local",
1580 "qformat",
1581 "dquot",
1582 "quotaoff",
1583 "LR header",
1584 "unmount",
1585 "commit",
1586 "trans header"
1587 };
1588 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1589 "SETATTR_NOT_SIZE",
1590 "SETATTR_SIZE",
1591 "INACTIVE",
1592 "CREATE",
1593 "CREATE_TRUNC",
1594 "TRUNCATE_FILE",
1595 "REMOVE",
1596 "LINK",
1597 "RENAME",
1598 "MKDIR",
1599 "RMDIR",
1600 "SYMLINK",
1601 "SET_DMATTRS",
1602 "GROWFS",
1603 "STRAT_WRITE",
1604 "DIOSTRAT",
1605 "WRITE_SYNC",
1606 "WRITEID",
1607 "ADDAFORK",
1608 "ATTRINVAL",
1609 "ATRUNCATE",
1610 "ATTR_SET",
1611 "ATTR_RM",
1612 "ATTR_FLAG",
1613 "CLEAR_AGI_BUCKET",
1614 "QM_SBCHANGE",
1615 "DUMMY1",
1616 "DUMMY2",
1617 "QM_QUOTAOFF",
1618 "QM_DQALLOC",
1619 "QM_SETQLIM",
1620 "QM_DQCLUSTER",
1621 "QM_QINOCREATE",
1622 "QM_QUOTAOFF_END",
1623 "SB_UNIT",
1624 "FSYNC_TS",
1625 "GROWFSRT_ALLOC",
1626 "GROWFSRT_ZERO",
1627 "GROWFSRT_FREE",
1628 "SWAPEXT"
1629 };
1630
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001631 xfs_warn(mp,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001632 "xlog_write: reservation summary:\n"
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001633 " trans type = %s (%u)\n"
1634 " unit res = %d bytes\n"
1635 " current res = %d bytes\n"
1636 " total reg = %u bytes (o/flow = %u bytes)\n"
1637 " ophdrs = %u (ophdr space = %u bytes)\n"
1638 " ophdr + reg = %u bytes\n"
1639 " num regions = %u\n",
1640 ((ticket->t_trans_type <= 0 ||
1641 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1642 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1643 ticket->t_trans_type,
1644 ticket->t_unit_res,
1645 ticket->t_curr_res,
1646 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1647 ticket->t_res_num_ophdrs, ophdr_spc,
1648 ticket->t_res_arr_sum +
1649 ticket->t_res_o_flow + ophdr_spc,
1650 ticket->t_res_num);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001651
1652 for (i = 0; i < ticket->t_res_num; i++) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001653 uint r_type = ticket->t_res_arr[i].r_type;
1654 xfs_warn(mp, "region[%u]: %s - %u bytes\n", i,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001655 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1656 "bad-rtype" : res_type_str[r_type-1]),
1657 ticket->t_res_arr[i].r_len);
1658 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001659
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001660 xfs_alert_tag(mp, XFS_PTAG_LOGRES,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001661 "xlog_write: reservation ran out. Need to up reservation");
Dave Chinner169a7b02010-05-07 11:05:31 +10001662 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001663}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001664
1665/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001666 * Calculate the potential space needed by the log vector. Each region gets
1667 * its own xlog_op_header_t and may need to be double word aligned.
1668 */
1669static int
1670xlog_write_calc_vec_length(
1671 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001672 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001673{
Dave Chinner55b66332010-03-23 11:43:17 +11001674 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001675 int headers = 0;
1676 int len = 0;
1677 int i;
1678
1679 /* acct for start rec of xact */
1680 if (ticket->t_flags & XLOG_TIC_INITED)
1681 headers++;
1682
Dave Chinner55b66332010-03-23 11:43:17 +11001683 for (lv = log_vector; lv; lv = lv->lv_next) {
1684 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001685
Dave Chinner55b66332010-03-23 11:43:17 +11001686 for (i = 0; i < lv->lv_niovecs; i++) {
1687 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1688
1689 len += vecp->i_len;
1690 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1691 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001692 }
1693
1694 ticket->t_res_num_ophdrs += headers;
1695 len += headers * sizeof(struct xlog_op_header);
1696
1697 return len;
1698}
1699
1700/*
1701 * If first write for transaction, insert start record We can't be trying to
1702 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1703 */
1704static int
1705xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001706 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001707 struct xlog_ticket *ticket)
1708{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001709 if (!(ticket->t_flags & XLOG_TIC_INITED))
1710 return 0;
1711
1712 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1713 ophdr->oh_clientid = ticket->t_clientid;
1714 ophdr->oh_len = 0;
1715 ophdr->oh_flags = XLOG_START_TRANS;
1716 ophdr->oh_res2 = 0;
1717
1718 ticket->t_flags &= ~XLOG_TIC_INITED;
1719
1720 return sizeof(struct xlog_op_header);
1721}
1722
1723static xlog_op_header_t *
1724xlog_write_setup_ophdr(
1725 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001726 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001727 struct xlog_ticket *ticket,
1728 uint flags)
1729{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001730 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1731 ophdr->oh_clientid = ticket->t_clientid;
1732 ophdr->oh_res2 = 0;
1733
1734 /* are we copying a commit or unmount record? */
1735 ophdr->oh_flags = flags;
1736
1737 /*
1738 * We've seen logs corrupted with bad transaction client ids. This
1739 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1740 * and shut down the filesystem.
1741 */
1742 switch (ophdr->oh_clientid) {
1743 case XFS_TRANSACTION:
1744 case XFS_VOLUME:
1745 case XFS_LOG:
1746 break;
1747 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001748 xfs_warn(log->l_mp,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001749 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1750 ophdr->oh_clientid, ticket);
1751 return NULL;
1752 }
1753
1754 return ophdr;
1755}
1756
1757/*
1758 * Set up the parameters of the region copy into the log. This has
1759 * to handle region write split across multiple log buffers - this
1760 * state is kept external to this function so that this code can
1761 * can be written in an obvious, self documenting manner.
1762 */
1763static int
1764xlog_write_setup_copy(
1765 struct xlog_ticket *ticket,
1766 struct xlog_op_header *ophdr,
1767 int space_available,
1768 int space_required,
1769 int *copy_off,
1770 int *copy_len,
1771 int *last_was_partial_copy,
1772 int *bytes_consumed)
1773{
1774 int still_to_copy;
1775
1776 still_to_copy = space_required - *bytes_consumed;
1777 *copy_off = *bytes_consumed;
1778
1779 if (still_to_copy <= space_available) {
1780 /* write of region completes here */
1781 *copy_len = still_to_copy;
1782 ophdr->oh_len = cpu_to_be32(*copy_len);
1783 if (*last_was_partial_copy)
1784 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1785 *last_was_partial_copy = 0;
1786 *bytes_consumed = 0;
1787 return 0;
1788 }
1789
1790 /* partial write of region, needs extra log op header reservation */
1791 *copy_len = space_available;
1792 ophdr->oh_len = cpu_to_be32(*copy_len);
1793 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1794 if (*last_was_partial_copy)
1795 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1796 *bytes_consumed += *copy_len;
1797 (*last_was_partial_copy)++;
1798
1799 /* account for new log op header */
1800 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1801 ticket->t_res_num_ophdrs++;
1802
1803 return sizeof(struct xlog_op_header);
1804}
1805
1806static int
1807xlog_write_copy_finish(
1808 struct log *log,
1809 struct xlog_in_core *iclog,
1810 uint flags,
1811 int *record_cnt,
1812 int *data_cnt,
1813 int *partial_copy,
1814 int *partial_copy_len,
1815 int log_offset,
1816 struct xlog_in_core **commit_iclog)
1817{
1818 if (*partial_copy) {
1819 /*
1820 * This iclog has already been marked WANT_SYNC by
1821 * xlog_state_get_iclog_space.
1822 */
1823 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1824 *record_cnt = 0;
1825 *data_cnt = 0;
1826 return xlog_state_release_iclog(log, iclog);
1827 }
1828
1829 *partial_copy = 0;
1830 *partial_copy_len = 0;
1831
1832 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1833 /* no more space in this iclog - push it. */
1834 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1835 *record_cnt = 0;
1836 *data_cnt = 0;
1837
1838 spin_lock(&log->l_icloglock);
1839 xlog_state_want_sync(log, iclog);
1840 spin_unlock(&log->l_icloglock);
1841
1842 if (!commit_iclog)
1843 return xlog_state_release_iclog(log, iclog);
1844 ASSERT(flags & XLOG_COMMIT_TRANS);
1845 *commit_iclog = iclog;
1846 }
1847
1848 return 0;
1849}
1850
1851/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * Write some region out to in-core log
1853 *
1854 * This will be called when writing externally provided regions or when
1855 * writing out a commit record for a given transaction.
1856 *
1857 * General algorithm:
1858 * 1. Find total length of this write. This may include adding to the
1859 * lengths passed in.
1860 * 2. Check whether we violate the tickets reservation.
1861 * 3. While writing to this iclog
1862 * A. Reserve as much space in this iclog as can get
1863 * B. If this is first write, save away start lsn
1864 * C. While writing this region:
1865 * 1. If first write of transaction, write start record
1866 * 2. Write log operation header (header per region)
1867 * 3. Find out if we can fit entire region into this iclog
1868 * 4. Potentially, verify destination memcpy ptr
1869 * 5. Memcpy (partial) region
1870 * 6. If partial copy, release iclog; otherwise, continue
1871 * copying more regions into current iclog
1872 * 4. Mark want sync bit (in simulation mode)
1873 * 5. Release iclog for potential flush to on-disk log.
1874 *
1875 * ERRORS:
1876 * 1. Panic if reservation is overrun. This should never happen since
1877 * reservation amounts are generated internal to the filesystem.
1878 * NOTES:
1879 * 1. Tickets are single threaded data structures.
1880 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1881 * syncing routine. When a single log_write region needs to span
1882 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1883 * on all log operation writes which don't contain the end of the
1884 * region. The XLOG_END_TRANS bit is used for the in-core log
1885 * operation which contains the end of the continued log_write region.
1886 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1887 * we don't really know exactly how much space will be used. As a result,
1888 * we don't update ic_offset until the end when we know exactly how many
1889 * bytes have been written out.
1890 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001891int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001892xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001893 struct log *log,
1894 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001895 struct xlog_ticket *ticket,
1896 xfs_lsn_t *start_lsn,
1897 struct xlog_in_core **commit_iclog,
1898 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001900 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001901 struct xfs_log_iovec *vecp;
1902 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001903 int len;
1904 int index;
1905 int partial_copy = 0;
1906 int partial_copy_len = 0;
1907 int contwr = 0;
1908 int record_cnt = 0;
1909 int data_cnt = 0;
1910 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001912 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Dave Chinner55b66332010-03-23 11:43:17 +11001914 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001915
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001916 /*
1917 * Region headers and bytes are already accounted for.
1918 * We only need to take into account start records and
1919 * split regions in this function.
1920 */
1921 if (ticket->t_flags & XLOG_TIC_INITED)
1922 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1923
1924 /*
1925 * Commit record headers need to be accounted for. These
1926 * come in as separate writes so are easy to detect.
1927 */
1928 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1929 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Dave Chinner71e330b2010-05-21 14:37:18 +10001930
1931 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001932 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Dave Chinner55b66332010-03-23 11:43:17 +11001934 index = 0;
1935 lv = log_vector;
1936 vecp = lv->lv_iovecp;
1937 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001938 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001939 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001941 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1942 &contwr, &log_offset);
1943 if (error)
1944 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001946 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001947 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001949 /* start_lsn is the first lsn written to. That's all we need. */
1950 if (!*start_lsn)
1951 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001953 /*
1954 * This loop writes out as many regions as can fit in the amount
1955 * of space which was allocated by xlog_state_get_iclog_space().
1956 */
Dave Chinner55b66332010-03-23 11:43:17 +11001957 while (lv && index < lv->lv_niovecs) {
1958 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001959 struct xlog_op_header *ophdr;
1960 int start_rec_copy;
1961 int copy_len;
1962 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Dave Chinner55b66332010-03-23 11:43:17 +11001964 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001965 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001967 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1968 if (start_rec_copy) {
1969 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001970 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001971 start_rec_copy);
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001974 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1975 if (!ophdr)
1976 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001978 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001979 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001981 len += xlog_write_setup_copy(ticket, ophdr,
1982 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001983 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001984 &copy_off, &copy_len,
1985 &partial_copy,
1986 &partial_copy_len);
1987 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001988
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001989 /* copy region */
1990 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001991 memcpy(ptr, reg->i_addr + copy_off, copy_len);
1992 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001993
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001994 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1995 record_cnt++;
1996 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001998 error = xlog_write_copy_finish(log, iclog, flags,
1999 &record_cnt, &data_cnt,
2000 &partial_copy,
2001 &partial_copy_len,
2002 log_offset,
2003 commit_iclog);
2004 if (error)
2005 return error;
2006
2007 /*
2008 * if we had a partial copy, we need to get more iclog
2009 * space but we don't want to increment the region
2010 * index because there is still more is this region to
2011 * write.
2012 *
2013 * If we completed writing this region, and we flushed
2014 * the iclog (indicated by resetting of the record
2015 * count), then we also need to get more log space. If
2016 * this was the last record, though, we are done and
2017 * can just return.
2018 */
2019 if (partial_copy)
2020 break;
2021
Dave Chinner55b66332010-03-23 11:43:17 +11002022 if (++index == lv->lv_niovecs) {
2023 lv = lv->lv_next;
2024 index = 0;
2025 if (lv)
2026 vecp = lv->lv_iovecp;
2027 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002028 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11002029 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002030 return 0;
2031 break;
2032 }
2033 }
2034 }
2035
2036 ASSERT(len == 0);
2037
2038 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2039 if (!commit_iclog)
2040 return xlog_state_release_iclog(log, iclog);
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 ASSERT(flags & XLOG_COMMIT_TRANS);
2043 *commit_iclog = iclog;
2044 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047
2048/*****************************************************************************
2049 *
2050 * State Machine functions
2051 *
2052 *****************************************************************************
2053 */
2054
2055/* Clean iclogs starting from the head. This ordering must be
2056 * maintained, so an iclog doesn't become ACTIVE beyond one that
2057 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002058 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 * iclog is trying to sync to disk.
2060 *
2061 * State Change: DIRTY -> ACTIVE
2062 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002063STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064xlog_state_clean_log(xlog_t *log)
2065{
2066 xlog_in_core_t *iclog;
2067 int changed = 0;
2068
2069 iclog = log->l_iclog;
2070 do {
2071 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2072 iclog->ic_state = XLOG_STATE_ACTIVE;
2073 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002074 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * If the number of ops in this iclog indicate it just
2077 * contains the dummy transaction, we can
2078 * change state into IDLE (the second time around).
2079 * Otherwise we should change the state into
2080 * NEED a dummy.
2081 * We don't need to cover the dummy.
2082 */
2083 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002084 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2085 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 changed = 1;
2087 } else {
2088 /*
2089 * We have two dirty iclogs so start over
2090 * This could also be num of ops indicates
2091 * this is not the dummy going out.
2092 */
2093 changed = 2;
2094 }
2095 iclog->ic_header.h_num_logops = 0;
2096 memset(iclog->ic_header.h_cycle_data, 0,
2097 sizeof(iclog->ic_header.h_cycle_data));
2098 iclog->ic_header.h_lsn = 0;
2099 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2100 /* do nothing */;
2101 else
2102 break; /* stop cleaning */
2103 iclog = iclog->ic_next;
2104 } while (iclog != log->l_iclog);
2105
2106 /* log is locked when we are called */
2107 /*
2108 * Change state for the dummy log recording.
2109 * We usually go to NEED. But we go to NEED2 if the changed indicates
2110 * we are done writing the dummy record.
2111 * If we are done with the second dummy recored (DONE2), then
2112 * we go to IDLE.
2113 */
2114 if (changed) {
2115 switch (log->l_covered_state) {
2116 case XLOG_STATE_COVER_IDLE:
2117 case XLOG_STATE_COVER_NEED:
2118 case XLOG_STATE_COVER_NEED2:
2119 log->l_covered_state = XLOG_STATE_COVER_NEED;
2120 break;
2121
2122 case XLOG_STATE_COVER_DONE:
2123 if (changed == 1)
2124 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2125 else
2126 log->l_covered_state = XLOG_STATE_COVER_NEED;
2127 break;
2128
2129 case XLOG_STATE_COVER_DONE2:
2130 if (changed == 1)
2131 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2132 else
2133 log->l_covered_state = XLOG_STATE_COVER_NEED;
2134 break;
2135
2136 default:
2137 ASSERT(0);
2138 }
2139 }
2140} /* xlog_state_clean_log */
2141
2142STATIC xfs_lsn_t
2143xlog_get_lowest_lsn(
2144 xlog_t *log)
2145{
2146 xlog_in_core_t *lsn_log;
2147 xfs_lsn_t lowest_lsn, lsn;
2148
2149 lsn_log = log->l_iclog;
2150 lowest_lsn = 0;
2151 do {
2152 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002153 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if ((lsn && !lowest_lsn) ||
2155 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2156 lowest_lsn = lsn;
2157 }
2158 }
2159 lsn_log = lsn_log->ic_next;
2160 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002161 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
2164
2165STATIC void
2166xlog_state_do_callback(
2167 xlog_t *log,
2168 int aborted,
2169 xlog_in_core_t *ciclog)
2170{
2171 xlog_in_core_t *iclog;
2172 xlog_in_core_t *first_iclog; /* used to know when we've
2173 * processed all iclogs once */
2174 xfs_log_callback_t *cb, *cb_next;
2175 int flushcnt = 0;
2176 xfs_lsn_t lowest_lsn;
2177 int ioerrors; /* counter: iclogs with errors */
2178 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2179 int funcdidcallbacks; /* flag: function did callbacks */
2180 int repeats; /* for issuing console warnings if
2181 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002182 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002184 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 first_iclog = iclog = log->l_iclog;
2186 ioerrors = 0;
2187 funcdidcallbacks = 0;
2188 repeats = 0;
2189
2190 do {
2191 /*
2192 * Scan all iclogs starting with the one pointed to by the
2193 * log. Reset this starting point each time the log is
2194 * unlocked (during callbacks).
2195 *
2196 * Keep looping through iclogs until one full pass is made
2197 * without running any callbacks.
2198 */
2199 first_iclog = log->l_iclog;
2200 iclog = log->l_iclog;
2201 loopdidcallbacks = 0;
2202 repeats++;
2203
2204 do {
2205
2206 /* skip all iclogs in the ACTIVE & DIRTY states */
2207 if (iclog->ic_state &
2208 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2209 iclog = iclog->ic_next;
2210 continue;
2211 }
2212
2213 /*
2214 * Between marking a filesystem SHUTDOWN and stopping
2215 * the log, we do flush all iclogs to disk (if there
2216 * wasn't a log I/O error). So, we do want things to
2217 * go smoothly in case of just a SHUTDOWN w/o a
2218 * LOG_IO_ERROR.
2219 */
2220 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2221 /*
2222 * Can only perform callbacks in order. Since
2223 * this iclog is not in the DONE_SYNC/
2224 * DO_CALLBACK state, we skip the rest and
2225 * just try to clean up. If we set our iclog
2226 * to DO_CALLBACK, we will not process it when
2227 * we retry since a previous iclog is in the
2228 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002229 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 */
2231 if (!(iclog->ic_state &
2232 (XLOG_STATE_DONE_SYNC |
2233 XLOG_STATE_DO_CALLBACK))) {
2234 if (ciclog && (ciclog->ic_state ==
2235 XLOG_STATE_DONE_SYNC)) {
2236 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2237 }
2238 break;
2239 }
2240 /*
2241 * We now have an iclog that is in either the
2242 * DO_CALLBACK or DONE_SYNC states. The other
2243 * states (WANT_SYNC, SYNCING, or CALLBACK were
2244 * caught by the above if and are going to
2245 * clean (i.e. we aren't doing their callbacks)
2246 * see the above if.
2247 */
2248
2249 /*
2250 * We will do one more check here to see if we
2251 * have chased our tail around.
2252 */
2253
2254 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002255 if (lowest_lsn &&
2256 XFS_LSN_CMP(lowest_lsn,
Dave Chinner84f3c682010-12-03 22:11:29 +11002257 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 iclog = iclog->ic_next;
2259 continue; /* Leave this iclog for
2260 * another thread */
2261 }
2262
2263 iclog->ic_state = XLOG_STATE_CALLBACK;
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Dave Chinner84f3c682010-12-03 22:11:29 +11002266 /*
2267 * update the last_sync_lsn before we drop the
2268 * icloglock to ensure we are the only one that
2269 * can update it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 */
Dave Chinner84f3c682010-12-03 22:11:29 +11002271 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2272 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2273 atomic64_set(&log->l_last_sync_lsn,
2274 be64_to_cpu(iclog->ic_header.h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Dave Chinner84f3c682010-12-03 22:11:29 +11002276 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 ioerrors++;
Dave Chinner84f3c682010-12-03 22:11:29 +11002278
2279 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
David Chinner114d23a2008-04-10 12:18:39 +10002281 /*
2282 * Keep processing entries in the callback list until
2283 * we come around and it is empty. We need to
2284 * atomically see that the list is empty and change the
2285 * state to DIRTY so that we don't miss any more
2286 * callbacks being added.
2287 */
2288 spin_lock(&iclog->ic_callback_lock);
2289 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002290 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 iclog->ic_callback_tail = &(iclog->ic_callback);
2292 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002293 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002296 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 cb_next = cb->cb_next;
2298 cb->cb_func(cb->cb_arg, aborted);
2299 }
David Chinner114d23a2008-04-10 12:18:39 +10002300 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 cb = iclog->ic_callback;
2302 }
2303
2304 loopdidcallbacks++;
2305 funcdidcallbacks++;
2306
David Chinner114d23a2008-04-10 12:18:39 +10002307 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002308 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002309 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2311 iclog->ic_state = XLOG_STATE_DIRTY;
2312
2313 /*
2314 * Transition from DIRTY to ACTIVE if applicable.
2315 * NOP if STATE_IOERROR.
2316 */
2317 xlog_state_clean_log(log);
2318
2319 /* wake up threads waiting in xfs_log_force() */
Dave Chinnereb40a872010-12-21 12:09:01 +11002320 wake_up_all(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 iclog = iclog->ic_next;
2323 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002324
2325 if (repeats > 5000) {
2326 flushcnt += repeats;
2327 repeats = 0;
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002328 xfs_warn(log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002329 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002330 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332 } while (!ioerrors && loopdidcallbacks);
2333
2334 /*
2335 * make one last gasp attempt to see if iclogs are being left in
2336 * limbo..
2337 */
2338#ifdef DEBUG
2339 if (funcdidcallbacks) {
2340 first_iclog = iclog = log->l_iclog;
2341 do {
2342 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2343 /*
2344 * Terminate the loop if iclogs are found in states
2345 * which will cause other threads to clean up iclogs.
2346 *
2347 * SYNCING - i/o completion will go through logs
2348 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002349 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 * IOERROR - give up hope all ye who enter here
2351 */
2352 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2353 iclog->ic_state == XLOG_STATE_SYNCING ||
2354 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2355 iclog->ic_state == XLOG_STATE_IOERROR )
2356 break;
2357 iclog = iclog->ic_next;
2358 } while (first_iclog != iclog);
2359 }
2360#endif
2361
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002362 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2363 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002364 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002365
2366 if (wake)
Dave Chinnereb40a872010-12-21 12:09:01 +11002367 wake_up_all(&log->l_flush_wait);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370
2371/*
2372 * Finish transitioning this iclog to the dirty state.
2373 *
2374 * Make sure that we completely execute this routine only when this is
2375 * the last call to the iclog. There is a good chance that iclog flushes,
2376 * when we reach the end of the physical log, get turned into 2 separate
2377 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2378 * routine. By using the reference count bwritecnt, we guarantee that only
2379 * the second completion goes through.
2380 *
2381 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002382 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 */
David Chinnera8272ce2007-11-23 16:28:09 +11002384STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385xlog_state_done_syncing(
2386 xlog_in_core_t *iclog,
2387 int aborted)
2388{
2389 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002391 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2394 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002395 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2397
2398
2399 /*
2400 * If we got an error, either on the first buffer, or in the case of
2401 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2402 * and none should ever be attempted to be written to disk
2403 * again.
2404 */
2405 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2406 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002407 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return;
2409 }
2410 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2411 }
2412
2413 /*
2414 * Someone could be sleeping prior to writing out the next
2415 * iclog buffer, we wake them all, one will get to do the
2416 * I/O, the others get to wait for the result.
2417 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002418 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002419 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2421} /* xlog_state_done_syncing */
2422
2423
2424/*
2425 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002426 * sleep. We wait on the flush queue on the head iclog as that should be
2427 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2428 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 *
2430 * The in-core logs are used in a circular fashion. They are not used
2431 * out-of-order even when an iclog past the head is free.
2432 *
2433 * return:
2434 * * log_offset where xlog_write() can start writing into the in-core
2435 * log's data space.
2436 * * in-core log pointer to which xlog_write() should write.
2437 * * boolean indicating this is a continued write to an in-core log.
2438 * If this is the last write, then the in-core log's offset field
2439 * needs to be incremented, depending on the amount of data which
2440 * is copied.
2441 */
David Chinnera8272ce2007-11-23 16:28:09 +11002442STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443xlog_state_get_iclog_space(xlog_t *log,
2444 int len,
2445 xlog_in_core_t **iclogp,
2446 xlog_ticket_t *ticket,
2447 int *continued_write,
2448 int *logoffsetp)
2449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int log_offset;
2451 xlog_rec_header_t *head;
2452 xlog_in_core_t *iclog;
2453 int error;
2454
2455restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002456 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002458 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return XFS_ERROR(EIO);
2460 }
2461
2462 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002463 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002465
2466 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002467 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 goto restart;
2469 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 head = &iclog->ic_header;
2472
David Chinner155cc6b2008-03-06 13:44:14 +11002473 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 log_offset = iclog->ic_offset;
2475
2476 /* On the 1st write to an iclog, figure out lsn. This works
2477 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2478 * committing to. If the offset is set, that's how many blocks
2479 * must be written.
2480 */
2481 if (log_offset == 0) {
2482 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002483 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002484 log->l_iclog_hsize,
2485 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002486 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2487 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002488 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 ASSERT(log->l_curr_block >= 0);
2490 }
2491
2492 /* If there is enough room to write everything, then do it. Otherwise,
2493 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2494 * bit is on, so this will get flushed out. Don't update ic_offset
2495 * until you know exactly how many bytes get copied. Therefore, wait
2496 * until later to update ic_offset.
2497 *
2498 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2499 * can fit into remaining data section.
2500 */
2501 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2502 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2503
Dave Chinner49641f12008-07-11 17:43:55 +10002504 /*
2505 * If I'm the only one writing to this iclog, sync it to disk.
2506 * We need to do an atomic compare and decrement here to avoid
2507 * racing with concurrent atomic_dec_and_lock() calls in
2508 * xlog_state_release_iclog() when there is more than one
2509 * reference to the iclog.
2510 */
2511 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2512 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002513 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002514 error = xlog_state_release_iclog(log, iclog);
2515 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002516 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002518 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520 goto restart;
2521 }
2522
2523 /* Do we have enough room to write the full amount in the remainder
2524 * of this iclog? Or must we continue a write on the next iclog and
2525 * mark this iclog as completely taken? In the case where we switch
2526 * iclogs (to mark it taken), this particular iclog will release/sync
2527 * to disk in xlog_write().
2528 */
2529 if (len <= iclog->ic_size - iclog->ic_offset) {
2530 *continued_write = 0;
2531 iclog->ic_offset += len;
2532 } else {
2533 *continued_write = 1;
2534 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2535 }
2536 *iclogp = iclog;
2537
2538 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002539 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 *logoffsetp = log_offset;
2542 return 0;
2543} /* xlog_state_get_iclog_space */
2544
2545/*
2546 * Atomically get the log space required for a log ticket.
2547 *
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002548 * Once a ticket gets put onto the reserveq, it will only return after the
2549 * needed reservation is satisfied.
Dave Chinner3f16b982010-12-21 12:29:01 +11002550 *
2551 * This function is structured so that it has a lock free fast path. This is
2552 * necessary because every new transaction reservation will come through this
2553 * path. Hence any lock will be globally hot if we take it unconditionally on
2554 * every pass.
2555 *
Christoph Hellwig28496962012-02-20 02:31:25 +00002556 * As tickets are only ever moved on and off the l_reserve.waiters under the
2557 * l_reserve.lock, we only need to take that lock if we are going to add
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002558 * the ticket to the queue and sleep. We can avoid taking the lock if the ticket
2559 * was never added to the reserveq because the t_queue list head will be empty
2560 * and we hold the only reference to it so it can safely be checked unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 */
2562STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002563xlog_grant_log_space(
2564 struct log *log,
2565 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002567 int free_bytes, need_bytes;
2568 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002570 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002572 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002574 /*
2575 * If there are other waiters on the queue then give them a chance at
2576 * logspace before us. Wake up the first waiters, if we do not wake
2577 * up all the waiters then go to sleep waiting for more free space,
2578 * otherwise try to get some space for this transaction.
2579 */
Dave Chinner3f16b982010-12-21 12:29:01 +11002580 need_bytes = tic->t_unit_res;
2581 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2582 need_bytes *= tic->t_ocnt;
Christoph Hellwig28496962012-02-20 02:31:25 +00002583 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
2584 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
2585 spin_lock(&log->l_reserve_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002586 if (!xlog_reserveq_wake(log, &free_bytes) ||
2587 free_bytes < need_bytes)
2588 error = xlog_reserveq_wait(log, tic, need_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00002589 spin_unlock(&log->l_reserve_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002590 } else if (free_bytes < need_bytes) {
Christoph Hellwig28496962012-02-20 02:31:25 +00002591 spin_lock(&log->l_reserve_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002592 error = xlog_reserveq_wait(log, tic, need_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00002593 spin_unlock(&log->l_reserve_head.lock);
Dave Chinner3f16b982010-12-21 12:29:01 +11002594 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002595 if (error)
2596 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Christoph Hellwig28496962012-02-20 02:31:25 +00002598 xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
2599 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002600 trace_xfs_log_grant_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002601 xlog_verify_grant_tail(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605/*
2606 * Replenish the byte reservation required by moving the grant write head.
2607 *
Dave Chinner3f16b982010-12-21 12:29:01 +11002608 * Similar to xlog_grant_log_space, the function is structured to have a lock
2609 * free fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 */
2611STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002612xlog_regrant_write_log_space(
2613 struct log *log,
2614 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002616 int free_bytes, need_bytes;
2617 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002620 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002625 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002627 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002629 /*
2630 * If there are other waiters on the queue then give them a chance at
2631 * logspace before us. Wake up the first waiters, if we do not wake
2632 * up all the waiters then go to sleep waiting for more free space,
2633 * otherwise try to get some space for this transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002635 need_bytes = tic->t_unit_res;
Christoph Hellwig28496962012-02-20 02:31:25 +00002636 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
2637 if (!list_empty_careful(&log->l_write_head.waiters)) {
2638 spin_lock(&log->l_write_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002639 if (!xlog_writeq_wake(log, &free_bytes) ||
2640 free_bytes < need_bytes)
2641 error = xlog_writeq_wait(log, tic, need_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00002642 spin_unlock(&log->l_write_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002643 } else if (free_bytes < need_bytes) {
Christoph Hellwig28496962012-02-20 02:31:25 +00002644 spin_lock(&log->l_write_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002645 error = xlog_writeq_wait(log, tic, need_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00002646 spin_unlock(&log->l_write_head.lock);
Dave Chinner3f16b982010-12-21 12:29:01 +11002647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002649 if (error)
2650 return error;
2651
Christoph Hellwig28496962012-02-20 02:31:25 +00002652 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002653 trace_xfs_log_regrant_write_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002654 xlog_verify_grant_tail(log);
Jesper Juhl014c2542006-01-15 02:37:08 +01002655 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658/* The first cnt-1 times through here we don't need to
2659 * move the grant write head because the permanent
2660 * reservation has reserved cnt times the unit amount.
2661 * Release part of current permanent unit reservation and
2662 * reset current reservation to be one units worth. Also
2663 * move grant reservation head forward.
2664 */
2665STATIC void
2666xlog_regrant_reserve_log_space(xlog_t *log,
2667 xlog_ticket_t *ticket)
2668{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002669 trace_xfs_log_regrant_reserve_enter(log, ticket);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (ticket->t_cnt > 0)
2672 ticket->t_cnt--;
2673
Christoph Hellwig28496962012-02-20 02:31:25 +00002674 xlog_grant_sub_space(log, &log->l_reserve_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002675 ticket->t_curr_res);
Christoph Hellwig28496962012-02-20 02:31:25 +00002676 xlog_grant_sub_space(log, &log->l_write_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002677 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002679 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002680
2681 trace_xfs_log_regrant_reserve_sub(log, ticket);
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /* just return if we still have some of the pre-reserved space */
Dave Chinnerd0eb2f32010-12-21 12:29:14 +11002684 if (ticket->t_cnt > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Christoph Hellwig28496962012-02-20 02:31:25 +00002687 xlog_grant_add_space(log, &log->l_reserve_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002688 ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002689
2690 trace_xfs_log_regrant_reserve_exit(log, ticket);
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002693 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694} /* xlog_regrant_reserve_log_space */
2695
2696
2697/*
2698 * Give back the space left from a reservation.
2699 *
2700 * All the information we need to make a correct determination of space left
2701 * is present. For non-permanent reservations, things are quite easy. The
2702 * count should have been decremented to zero. We only need to deal with the
2703 * space remaining in the current reservation part of the ticket. If the
2704 * ticket contains a permanent reservation, there may be left over space which
2705 * needs to be released. A count of N means that N-1 refills of the current
2706 * reservation can be done before we need to ask for more space. The first
2707 * one goes to fill up the first current reservation. Once we run out of
2708 * space, the count will stay at zero and the only space remaining will be
2709 * in the current reservation field.
2710 */
2711STATIC void
2712xlog_ungrant_log_space(xlog_t *log,
2713 xlog_ticket_t *ticket)
2714{
Dave Chinner663e4962010-12-21 12:06:05 +11002715 int bytes;
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (ticket->t_cnt > 0)
2718 ticket->t_cnt--;
2719
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002720 trace_xfs_log_ungrant_enter(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002721 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Dave Chinner663e4962010-12-21 12:06:05 +11002723 /*
2724 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 * up more space based on the remaining count.
2726 */
Dave Chinner663e4962010-12-21 12:06:05 +11002727 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 if (ticket->t_cnt > 0) {
2729 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11002730 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
2732
Christoph Hellwig28496962012-02-20 02:31:25 +00002733 xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
2734 xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11002735
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002736 trace_xfs_log_ungrant_exit(log, ticket);
2737
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +00002738 xfs_log_space_wake(log->l_mp);
Christoph Hellwig09a423a2012-02-20 02:31:20 +00002739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 * Flush iclog to disk if this is the last reference to the given iclog and
2743 * the WANT_SYNC bit is set.
2744 *
2745 * When this function is entered, the iclog is not necessarily in the
2746 * WANT_SYNC state. It may be sitting around waiting to get filled.
2747 *
2748 *
2749 */
David Chinnera8272ce2007-11-23 16:28:09 +11002750STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002751xlog_state_release_iclog(
2752 xlog_t *log,
2753 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 int sync = 0; /* do we sync? */
2756
David Chinner155cc6b2008-03-06 13:44:14 +11002757 if (iclog->ic_state & XLOG_STATE_IOERROR)
2758 return XFS_ERROR(EIO);
2759
2760 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2761 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2762 return 0;
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002765 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return XFS_ERROR(EIO);
2767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2769 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2770
David Chinner155cc6b2008-03-06 13:44:14 +11002771 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002772 /* update tail before writing to iclog */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002773 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 sync++;
2775 iclog->ic_state = XLOG_STATE_SYNCING;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002776 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
2777 xlog_verify_tail_lsn(log, iclog, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 /* cycle incremented when incrementing curr_block */
2779 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002780 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 /*
2783 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002784 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2786 * this iclog has consistent data, so we ignore IOERROR
2787 * flags after this point.
2788 */
David Chinnerb5893342008-03-06 13:44:06 +11002789 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792} /* xlog_state_release_iclog */
2793
2794
2795/*
2796 * This routine will mark the current iclog in the ring as WANT_SYNC
2797 * and move the current iclog pointer to the next iclog in the ring.
2798 * When this routine is called from xlog_state_get_iclog_space(), the
2799 * exact size of the iclog has not yet been determined. All we know is
2800 * that every data block. We have run out of space in this log record.
2801 */
2802STATIC void
2803xlog_state_switch_iclogs(xlog_t *log,
2804 xlog_in_core_t *iclog,
2805 int eventual_size)
2806{
2807 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2808 if (!eventual_size)
2809 eventual_size = iclog->ic_offset;
2810 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002811 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 log->l_prev_block = log->l_curr_block;
2813 log->l_prev_cycle = log->l_curr_cycle;
2814
2815 /* roll log?: ic_offset changed later */
2816 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2817
2818 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002819 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 log->l_mp->m_sb.sb_logsunit > 1) {
2821 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2822 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2823 }
2824
2825 if (log->l_curr_block >= log->l_logBBsize) {
2826 log->l_curr_cycle++;
2827 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2828 log->l_curr_cycle++;
2829 log->l_curr_block -= log->l_logBBsize;
2830 ASSERT(log->l_curr_block >= 0);
2831 }
2832 ASSERT(iclog == log->l_iclog);
2833 log->l_iclog = iclog->ic_next;
2834} /* xlog_state_switch_iclogs */
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836/*
2837 * Write out all data in the in-core log as of this exact moment in time.
2838 *
2839 * Data may be written to the in-core log during this call. However,
2840 * we don't guarantee this data will be written out. A change from past
2841 * implementation means this routine will *not* write out zero length LRs.
2842 *
2843 * Basically, we try and perform an intelligent scan of the in-core logs.
2844 * If we determine there is no flushable data, we just return. There is no
2845 * flushable data if:
2846 *
2847 * 1. the current iclog is active and has no data; the previous iclog
2848 * is in the active or dirty state.
2849 * 2. the current iclog is drity, and the previous iclog is in the
2850 * active or dirty state.
2851 *
David Chinner12017fa2008-08-13 16:34:31 +10002852 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 *
2854 * 1. the current iclog is not in the active nor dirty state.
2855 * 2. the current iclog dirty, and the previous iclog is not in the
2856 * active nor dirty state.
2857 * 3. the current iclog is active, and there is another thread writing
2858 * to this particular iclog.
2859 * 4. a) the current iclog is active and has no other writers
2860 * b) when we return from flushing out this iclog, it is still
2861 * not in the active nor dirty state.
2862 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002863int
2864_xfs_log_force(
2865 struct xfs_mount *mp,
2866 uint flags,
2867 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002869 struct log *log = mp->m_log;
2870 struct xlog_in_core *iclog;
2871 xfs_lsn_t lsn;
2872
2873 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002875 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002876
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002877 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 iclog = log->l_iclog;
2880 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002881 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return XFS_ERROR(EIO);
2883 }
2884
2885 /* If the head iclog is not active nor dirty, we just attach
2886 * ourselves to the head and go to sleep.
2887 */
2888 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2889 iclog->ic_state == XLOG_STATE_DIRTY) {
2890 /*
2891 * If the head is dirty or (active and empty), then
2892 * we need to look at the previous iclog. If the previous
2893 * iclog is active or dirty we are done. There is nothing
2894 * to sync out. Otherwise, we attach ourselves to the
2895 * previous iclog and go to sleep.
2896 */
2897 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002898 (atomic_read(&iclog->ic_refcnt) == 0
2899 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 iclog = iclog->ic_prev;
2901 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2902 iclog->ic_state == XLOG_STATE_DIRTY)
2903 goto no_sleep;
2904 else
2905 goto maybe_sleep;
2906 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002907 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /* We are the only one with access to this
2909 * iclog. Flush it out now. There should
2910 * be a roundoff of zero to show that someone
2911 * has already taken care of the roundoff from
2912 * the previous sync.
2913 */
David Chinner155cc6b2008-03-06 13:44:14 +11002914 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002915 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002917 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 if (xlog_state_release_iclog(log, iclog))
2920 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002921
2922 if (log_flushed)
2923 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002924 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002925 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 iclog->ic_state != XLOG_STATE_DIRTY)
2927 goto maybe_sleep;
2928 else
2929 goto no_sleep;
2930 } else {
2931 /* Someone else is writing to this iclog.
2932 * Use its call to flush out the data. However,
2933 * the other thread may not force out this LR,
2934 * so we mark it WANT_SYNC.
2935 */
2936 xlog_state_switch_iclogs(log, iclog, 0);
2937 goto maybe_sleep;
2938 }
2939 }
2940 }
2941
2942 /* By the time we come around again, the iclog could've been filled
2943 * which would give it another lsn. If we have a new lsn, just
2944 * return because the relevant data has been flushed.
2945 */
2946maybe_sleep:
2947 if (flags & XFS_LOG_SYNC) {
2948 /*
2949 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002950 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 * Then we check again after waking up, in case our
2952 * sleep was disturbed by a bad news.
2953 */
2954 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002955 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return XFS_ERROR(EIO);
2957 }
2958 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11002959 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /*
2961 * No need to grab the log lock here since we're
2962 * only deciding whether or not to return EIO
2963 * and the memory read should be atomic.
2964 */
2965 if (iclog->ic_state & XLOG_STATE_IOERROR)
2966 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002967 if (log_flushed)
2968 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 } else {
2970
2971no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002972 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002978 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2979 * about errors or whether the log was flushed or not. This is the normal
2980 * interface to use when trying to unpin items or move the log forward.
2981 */
2982void
2983xfs_log_force(
2984 xfs_mount_t *mp,
2985 uint flags)
2986{
2987 int error;
2988
2989 error = _xfs_log_force(mp, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002990 if (error)
2991 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002992}
2993
2994/*
2995 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 *
2997 * Find in-core log with lsn.
2998 * If it is in the DIRTY state, just return.
2999 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3000 * state and go to sleep or return.
3001 * If it is in any other state, go to sleep or return.
3002 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003003 * Synchronous forces are implemented with a signal variable. All callers
3004 * to force a given lsn to disk will wait on a the sv attached to the
3005 * specific in-core log. When given in-core log finally completes its
3006 * write to disk, that thread will wake up all threads waiting on the
3007 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003009int
3010_xfs_log_force_lsn(
3011 struct xfs_mount *mp,
3012 xfs_lsn_t lsn,
3013 uint flags,
3014 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003016 struct log *log = mp->m_log;
3017 struct xlog_in_core *iclog;
3018 int already_slept = 0;
3019
3020 ASSERT(lsn != 0);
3021
3022 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003024 lsn = xlog_cil_force_lsn(log, lsn);
3025 if (lsn == NULLCOMMITLSN)
3026 return 0;
Dave Chinner71e330b2010-05-21 14:37:18 +10003027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003029 spin_lock(&log->l_icloglock);
3030 iclog = log->l_iclog;
3031 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003032 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003033 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003036 do {
3037 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3038 iclog = iclog->ic_next;
3039 continue;
3040 }
3041
3042 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3043 spin_unlock(&log->l_icloglock);
3044 return 0;
3045 }
3046
3047 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3048 /*
3049 * We sleep here if we haven't already slept (e.g.
3050 * this is the first time we've looked at the correct
3051 * iclog buf) and the buffer before us is going to
3052 * be sync'ed. The reason for this is that if we
3053 * are doing sync transactions here, by waiting for
3054 * the previous I/O to complete, we can allow a few
3055 * more transactions into this iclog before we close
3056 * it down.
3057 *
3058 * Otherwise, we mark the buffer WANT_SYNC, and bump
3059 * up the refcnt so we can release the log (which
3060 * drops the ref count). The state switch keeps new
3061 * transaction commits from using this buffer. When
3062 * the current commits finish writing into the buffer,
3063 * the refcount will drop to zero and the buffer will
3064 * go out then.
3065 */
3066 if (!already_slept &&
3067 (iclog->ic_prev->ic_state &
3068 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3069 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3070
3071 XFS_STATS_INC(xs_log_force_sleep);
3072
Dave Chinnereb40a872010-12-21 12:09:01 +11003073 xlog_wait(&iclog->ic_prev->ic_write_wait,
3074 &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003075 if (log_flushed)
3076 *log_flushed = 1;
3077 already_slept = 1;
3078 goto try_again;
3079 }
David Chinner155cc6b2008-03-06 13:44:14 +11003080 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003082 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (xlog_state_release_iclog(log, iclog))
3084 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003085 if (log_flushed)
3086 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003087 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003090 if ((flags & XFS_LOG_SYNC) && /* sleep */
3091 !(iclog->ic_state &
3092 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3093 /*
3094 * Don't wait on completion if we know that we've
3095 * gotten a log write error.
3096 */
3097 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3098 spin_unlock(&log->l_icloglock);
3099 return XFS_ERROR(EIO);
3100 }
3101 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003102 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003103 /*
3104 * No need to grab the log lock here since we're
3105 * only deciding whether or not to return EIO
3106 * and the memory read should be atomic.
3107 */
3108 if (iclog->ic_state & XLOG_STATE_IOERROR)
3109 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003111 if (log_flushed)
3112 *log_flushed = 1;
3113 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003114 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003116
3117 return 0;
3118 } while (iclog != log->l_iclog);
3119
3120 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003122}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003124/*
3125 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3126 * about errors or whether the log was flushed or not. This is the normal
3127 * interface to use when trying to unpin items or move the log forward.
3128 */
3129void
3130xfs_log_force_lsn(
3131 xfs_mount_t *mp,
3132 xfs_lsn_t lsn,
3133 uint flags)
3134{
3135 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003137 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003138 if (error)
3139 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142/*
3143 * Called when we want to mark the current iclog as being ready to sync to
3144 * disk.
3145 */
David Chinnera8272ce2007-11-23 16:28:09 +11003146STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3148{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003149 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3152 xlog_state_switch_iclogs(log, iclog, 0);
3153 } else {
3154 ASSERT(iclog->ic_state &
3155 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3156 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159
3160/*****************************************************************************
3161 *
3162 * TICKET functions
3163 *
3164 *****************************************************************************
3165 */
3166
3167/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003168 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003170void
3171xfs_log_ticket_put(
3172 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003174 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003175 if (atomic_dec_and_test(&ticket->t_ref))
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003176 kmem_zone_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003179xlog_ticket_t *
3180xfs_log_ticket_get(
3181 xlog_ticket_t *ticket)
3182{
3183 ASSERT(atomic_read(&ticket->t_ref) > 0);
3184 atomic_inc(&ticket->t_ref);
3185 return ticket;
3186}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003189 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003191xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003192xlog_ticket_alloc(
3193 struct log *log,
3194 int unit_bytes,
3195 int cnt,
3196 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003197 uint xflags,
3198 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003200 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003202 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Dave Chinner3383ca52010-05-07 11:04:17 +10003204 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003205 if (!tic)
3206 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 /*
3209 * Permanent reservations have up to 'cnt'-1 active log operations
3210 * in the log. A unit in this case is the amount of space for one
3211 * of these log operations. Normal reservations have a cnt of 1
3212 * and their unit amount is the total amount of space required.
3213 *
3214 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003215 * which occupy space in the on-disk log.
3216 *
3217 * Normal form of a transaction is:
3218 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3219 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3220 *
3221 * We need to account for all the leadup data and trailer data
3222 * around the transaction data.
3223 * And then we need to account for the worst case in terms of using
3224 * more space.
3225 * The worst case will happen if:
3226 * - the placement of the transaction happens to be such that the
3227 * roundoff is at its maximum
3228 * - the transaction data is synced before the commit record is synced
3229 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3230 * Therefore the commit record is in its own Log Record.
3231 * This can happen as the commit record is called with its
3232 * own region to xlog_write().
3233 * This then means that in the worst case, roundoff can happen for
3234 * the commit-rec as well.
3235 * The commit-rec is smaller than padding in this scenario and so it is
3236 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 */
3238
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003239 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003241 unit_bytes += sizeof(xfs_trans_header_t);
3242
3243 /* for start-rec */
3244 unit_bytes += sizeof(xlog_op_header_t);
3245
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003246 /*
3247 * for LR headers - the space for data in an iclog is the size minus
3248 * the space used for the headers. If we use the iclog size, then we
3249 * undercalculate the number of headers required.
3250 *
3251 * Furthermore - the addition of op headers for split-recs might
3252 * increase the space required enough to require more log and op
3253 * headers, so take that into account too.
3254 *
3255 * IMPORTANT: This reservation makes the assumption that if this
3256 * transaction is the first in an iclog and hence has the LR headers
3257 * accounted to it, then the remaining space in the iclog is
3258 * exclusively for this transaction. i.e. if the transaction is larger
3259 * than the iclog, it will be the only thing in that iclog.
3260 * Fundamentally, this means we must pass the entire log vector to
3261 * xlog_write to guarantee this.
3262 */
3263 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3264 num_headers = howmany(unit_bytes, iclog_space);
3265
3266 /* for split-recs - ophdrs added when data split over LRs */
3267 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3268
3269 /* add extra header reservations if we overrun */
3270 while (!num_headers ||
3271 howmany(unit_bytes, iclog_space) > num_headers) {
3272 unit_bytes += sizeof(xlog_op_header_t);
3273 num_headers++;
3274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 unit_bytes += log->l_iclog_hsize * num_headers;
3276
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003277 /* for commit-rec LR header - note: padding will subsume the ophdr */
3278 unit_bytes += log->l_iclog_hsize;
3279
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003280 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003281 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003282 log->l_mp->m_sb.sb_logsunit > 1) {
3283 /* log su roundoff */
3284 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3285 } else {
3286 /* BB roundoff */
3287 unit_bytes += 2*BBSIZE;
3288 }
3289
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003290 atomic_set(&tic->t_ref, 1);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003291 tic->t_task = current;
Dave Chinner10547942010-12-21 12:02:25 +11003292 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 tic->t_unit_res = unit_bytes;
3294 tic->t_curr_res = unit_bytes;
3295 tic->t_cnt = cnt;
3296 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003297 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 tic->t_clientid = client;
3299 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003300 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (xflags & XFS_LOG_PERM_RESERV)
3302 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Christoph Hellwig0adba532007-08-30 17:21:46 +10003304 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309
3310/******************************************************************************
3311 *
3312 * Log debug routines
3313 *
3314 ******************************************************************************
3315 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003316#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317/*
3318 * Make sure that the destination ptr is within the valid data region of
3319 * one of the iclogs. This uses backup pointers stored in a different
3320 * part of the log in case we trash the log structure.
3321 */
3322void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003323xlog_verify_dest_ptr(
3324 struct log *log,
3325 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 int i;
3328 int good_ptr = 0;
3329
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003330 for (i = 0; i < log->l_iclog_bufs; i++) {
3331 if (ptr >= log->l_iclog_bak[i] &&
3332 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 good_ptr++;
3334 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003335
3336 if (!good_ptr)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003337 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003340/*
3341 * Check to make sure the grant write head didn't just over lap the tail. If
3342 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3343 * the cycles differ by exactly one and check the byte count.
3344 *
3345 * This check is run unlocked, so can give false positives. Rather than assert
3346 * on failures, use a warn-once flag and a panic tag to allow the admin to
3347 * determine if they want to panic the machine when such an error occurs. For
3348 * debug kernels this will have the same effect as using an assert but, unlinke
3349 * an assert, it can be turned off at runtime.
3350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351STATIC void
Dave Chinner3f336c62010-12-21 12:02:52 +11003352xlog_verify_grant_tail(
3353 struct log *log)
3354{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003355 int tail_cycle, tail_blocks;
Dave Chinnera69ed032010-12-21 12:08:20 +11003356 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003357
Christoph Hellwig28496962012-02-20 02:31:25 +00003358 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003359 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3360 if (tail_cycle != cycle) {
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003361 if (cycle - 1 != tail_cycle &&
3362 !(log->l_flags & XLOG_TAIL_WARN)) {
3363 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3364 "%s: cycle - 1 != tail_cycle", __func__);
3365 log->l_flags |= XLOG_TAIL_WARN;
3366 }
3367
3368 if (space > BBTOB(tail_blocks) &&
3369 !(log->l_flags & XLOG_TAIL_WARN)) {
3370 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3371 "%s: space > BBTOB(tail_blocks)", __func__);
3372 log->l_flags |= XLOG_TAIL_WARN;
3373 }
Dave Chinner3f336c62010-12-21 12:02:52 +11003374 }
3375}
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377/* check if it will fit */
3378STATIC void
3379xlog_verify_tail_lsn(xlog_t *log,
3380 xlog_in_core_t *iclog,
3381 xfs_lsn_t tail_lsn)
3382{
3383 int blocks;
3384
3385 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3386 blocks =
3387 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3388 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003389 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 } else {
3391 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3392
3393 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003394 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
3396 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3397 if (blocks < BTOBB(iclog->ic_offset) + 1)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003398 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 }
3400} /* xlog_verify_tail_lsn */
3401
3402/*
3403 * Perform a number of checks on the iclog before writing to disk.
3404 *
3405 * 1. Make sure the iclogs are still circular
3406 * 2. Make sure we have a good magic number
3407 * 3. Make sure we don't have magic numbers in the data
3408 * 4. Check fields of each log operation header for:
3409 * A. Valid client identifier
3410 * B. tid ptr value falls in valid ptr space (user space code)
3411 * C. Length in log record header is correct according to the
3412 * individual operation headers within record.
3413 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3414 * log, check the preceding blocks of the physical log to make sure all
3415 * the cycle numbers agree with the current cycle number.
3416 */
3417STATIC void
3418xlog_verify_iclog(xlog_t *log,
3419 xlog_in_core_t *iclog,
3420 int count,
3421 boolean_t syncing)
3422{
3423 xlog_op_header_t *ophead;
3424 xlog_in_core_t *icptr;
3425 xlog_in_core_2_t *xhdr;
3426 xfs_caddr_t ptr;
3427 xfs_caddr_t base_ptr;
3428 __psint_t field_offset;
3429 __uint8_t clientid;
3430 int len, i, j, k, op_len;
3431 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003434 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 icptr = log->l_iclog;
3436 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003437 if (icptr == NULL)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003438 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 icptr = icptr->ic_next;
3440 }
3441 if (icptr != log->l_iclog)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003442 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003443 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 /* check log magic numbers */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003446 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003447 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003449 ptr = (xfs_caddr_t) &iclog->ic_header;
3450 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 ptr += BBSIZE) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003452 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003453 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3454 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
3456
3457 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003458 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 ptr = iclog->ic_datap;
3460 base_ptr = ptr;
3461 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003462 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 for (i = 0; i < len; i++) {
3464 ophead = (xlog_op_header_t *)ptr;
3465
3466 /* clientid is only 1 byte */
3467 field_offset = (__psint_t)
3468 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3469 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3470 clientid = ophead->oh_clientid;
3471 } else {
3472 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3473 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3474 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3475 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003476 clientid = xlog_get_client_id(
3477 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003479 clientid = xlog_get_client_id(
3480 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
3482 }
3483 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003484 xfs_warn(log->l_mp,
3485 "%s: invalid clientid %d op 0x%p offset 0x%lx",
3486 __func__, clientid, ophead,
3487 (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489 /* check length */
3490 field_offset = (__psint_t)
3491 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3492 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003493 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 } else {
3495 idx = BTOBBT((__psint_t)&ophead->oh_len -
3496 (__psint_t)iclog->ic_datap);
3497 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3498 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3499 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003500 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003502 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
3504 }
3505 ptr += sizeof(xlog_op_header_t) + op_len;
3506 }
3507} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003508#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003511 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 */
3513STATIC int
3514xlog_state_ioerror(
3515 xlog_t *log)
3516{
3517 xlog_in_core_t *iclog, *ic;
3518
3519 iclog = log->l_iclog;
3520 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3521 /*
3522 * Mark all the incore logs IOERROR.
3523 * From now on, no log flushes will result.
3524 */
3525 ic = iclog;
3526 do {
3527 ic->ic_state = XLOG_STATE_IOERROR;
3528 ic = ic->ic_next;
3529 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 }
3532 /*
3533 * Return non-zero, if state transition has already happened.
3534 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003535 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536}
3537
3538/*
3539 * This is called from xfs_force_shutdown, when we're forcibly
3540 * shutting down the filesystem, typically because of an IO error.
3541 * Our main objectives here are to make sure that:
3542 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3543 * parties to find out, 'atomically'.
3544 * b. those who're sleeping on log reservations, pinned objects and
3545 * other resources get woken up, and be told the bad news.
3546 * c. nothing new gets queued up after (a) and (b) are done.
3547 * d. if !logerror, flush the iclogs to disk, then seal them off
3548 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003549 *
3550 * Note: for delayed logging the !logerror case needs to flush the regions
3551 * held in memory out to the iclogs before flushing them to disk. This needs
3552 * to be done before the log is marked as shutdown, otherwise the flush to the
3553 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 */
3555int
3556xfs_log_force_umount(
3557 struct xfs_mount *mp,
3558 int logerror)
3559{
3560 xlog_ticket_t *tic;
3561 xlog_t *log;
3562 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564 log = mp->m_log;
3565
3566 /*
3567 * If this happens during log recovery, don't worry about
3568 * locking; the log isn't open for business yet.
3569 */
3570 if (!log ||
3571 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3572 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003573 if (mp->m_sb_bp)
3574 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
3577
3578 /*
3579 * Somebody could've already done the hard work for us.
3580 * No need to get locks for this.
3581 */
3582 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3583 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003584 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003587
3588 /*
3589 * Flush the in memory commit item list before marking the log as
3590 * being shut down. We need to do it in this order to ensure all the
3591 * completed transactions are flushed to disk with the xfs_log_force()
3592 * call below.
3593 */
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003594 if (!logerror)
Dave Chinnera44f13e2010-08-24 11:40:03 +10003595 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 /*
Dave Chinner3f16b982010-12-21 12:29:01 +11003598 * mark the filesystem and the as in a shutdown state and wake
3599 * everybody up to tell them the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003601 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003603 if (mp->m_sb_bp)
3604 XFS_BUF_DONE(mp->m_sb_bp);
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 /*
3607 * This flag is sort of redundant because of the mount flag, but
3608 * it's good to maintain the separation between the log and the rest
3609 * of XFS.
3610 */
3611 log->l_flags |= XLOG_IO_ERROR;
3612
3613 /*
3614 * If we hit a log error, we want to mark all the iclogs IOERROR
3615 * while we're still holding the loglock.
3616 */
3617 if (logerror)
3618 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003619 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
3621 /*
Dave Chinner10547942010-12-21 12:02:25 +11003622 * We don't want anybody waiting for log reservations after this. That
3623 * means we have to wake up everybody queued up on reserveq as well as
3624 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3625 * we don't enqueue anything once the SHUTDOWN flag is set, and this
Dave Chinner3f16b982010-12-21 12:29:01 +11003626 * action is protected by the grant locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 */
Christoph Hellwig28496962012-02-20 02:31:25 +00003628 spin_lock(&log->l_reserve_head.lock);
3629 list_for_each_entry(tic, &log->l_reserve_head.waiters, t_queue)
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003630 wake_up_process(tic->t_task);
Christoph Hellwig28496962012-02-20 02:31:25 +00003631 spin_unlock(&log->l_reserve_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Christoph Hellwig28496962012-02-20 02:31:25 +00003633 spin_lock(&log->l_write_head.lock);
3634 list_for_each_entry(tic, &log->l_write_head.waiters, t_queue)
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003635 wake_up_process(tic->t_task);
Christoph Hellwig28496962012-02-20 02:31:25 +00003636 spin_unlock(&log->l_write_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003638 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 ASSERT(!logerror);
3640 /*
3641 * Force the incore logs to disk before shutting the
3642 * log down completely.
3643 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003644 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3645
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003646 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003648 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650 /*
3651 * Wake up everybody waiting on xfs_log_force.
3652 * Callback all log item committed functions as if the
3653 * log writes were completed.
3654 */
3655 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3656
3657#ifdef XFSERRORDEBUG
3658 {
3659 xlog_in_core_t *iclog;
3660
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003661 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 iclog = log->l_iclog;
3663 do {
3664 ASSERT(iclog->ic_callback == 0);
3665 iclog = iclog->ic_next;
3666 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003667 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669#endif
3670 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003671 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003674STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675xlog_iclogs_empty(xlog_t *log)
3676{
3677 xlog_in_core_t *iclog;
3678
3679 iclog = log->l_iclog;
3680 do {
3681 /* endianness does not matter here, zero is zero in
3682 * any language.
3683 */
3684 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 iclog = iclog->ic_next;
3687 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003688 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}