blob: c6a29a05c60a5fe9e085d388255fece903c43c5c [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 Hellwiga79bf2d2012-02-20 02:31:27 +0000162STATIC void
163xlog_grant_head_wake_all(
164 struct xlog_grant_head *head)
165{
166 struct xlog_ticket *tic;
167
168 spin_lock(&head->lock);
169 list_for_each_entry(tic, &head->waiters, t_queue)
170 wake_up_process(tic->t_task);
171 spin_unlock(&head->lock);
172}
173
Christoph Hellwige179840d2012-02-20 02:31:29 +0000174static inline int
175xlog_ticket_reservation(
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000176 struct log *log,
Christoph Hellwige179840d2012-02-20 02:31:29 +0000177 struct xlog_grant_head *head,
178 struct xlog_ticket *tic)
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000179{
Christoph Hellwige179840d2012-02-20 02:31:29 +0000180 if (head == &log->l_write_head) {
181 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
182 return tic->t_unit_res;
183 } else {
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000184 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
Christoph Hellwige179840d2012-02-20 02:31:29 +0000185 return tic->t_unit_res * tic->t_cnt;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000186 else
Christoph Hellwige179840d2012-02-20 02:31:29 +0000187 return tic->t_unit_res;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000188 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000189}
190
191STATIC bool
Christoph Hellwige179840d2012-02-20 02:31:29 +0000192xlog_grant_head_wake(
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000193 struct log *log,
Christoph Hellwige179840d2012-02-20 02:31:29 +0000194 struct xlog_grant_head *head,
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000195 int *free_bytes)
196{
197 struct xlog_ticket *tic;
198 int need_bytes;
199
Christoph Hellwige179840d2012-02-20 02:31:29 +0000200 list_for_each_entry(tic, &head->waiters, t_queue) {
201 need_bytes = xlog_ticket_reservation(log, head, tic);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000202 if (*free_bytes < need_bytes)
203 return false;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000204
Christoph Hellwige179840d2012-02-20 02:31:29 +0000205 *free_bytes -= need_bytes;
206 trace_xfs_log_grant_wake_up(log, tic);
Christoph Hellwig14a72352012-02-20 02:31:24 +0000207 wake_up_process(tic->t_task);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000208 }
209
210 return true;
211}
212
213STATIC int
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000214xlog_grant_head_wait(
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000215 struct log *log,
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000216 struct xlog_grant_head *head,
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000217 struct xlog_ticket *tic,
218 int need_bytes)
219{
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000220 list_add_tail(&tic->t_queue, &head->waiters);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000221
222 do {
223 if (XLOG_FORCED_SHUTDOWN(log))
224 goto shutdown;
225 xlog_grant_push_ail(log, need_bytes);
226
Christoph Hellwig14a72352012-02-20 02:31:24 +0000227 __set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000228 spin_unlock(&head->lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000229
Christoph Hellwig14a72352012-02-20 02:31:24 +0000230 XFS_STATS_INC(xs_sleep_logspace);
231
232 trace_xfs_log_grant_sleep(log, tic);
233 schedule();
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000234 trace_xfs_log_grant_wake(log, tic);
235
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000236 spin_lock(&head->lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000237 if (XLOG_FORCED_SHUTDOWN(log))
238 goto shutdown;
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000239 } while (xlog_space_left(log, &head->grant) < need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000240
241 list_del_init(&tic->t_queue);
242 return 0;
243shutdown:
244 list_del_init(&tic->t_queue);
245 return XFS_ERROR(EIO);
246}
247
Christoph Hellwig42ceedb2012-02-20 02:31:30 +0000248/*
249 * Atomically get the log space required for a log ticket.
250 *
251 * Once a ticket gets put onto head->waiters, it will only return after the
252 * needed reservation is satisfied.
253 *
254 * This function is structured so that it has a lock free fast path. This is
255 * necessary because every new transaction reservation will come through this
256 * path. Hence any lock will be globally hot if we take it unconditionally on
257 * every pass.
258 *
259 * As tickets are only ever moved on and off head->waiters under head->lock, we
260 * only need to take that lock if we are going to add the ticket to the queue
261 * and sleep. We can avoid taking the lock if the ticket was never added to
262 * head->waiters because the t_queue list head will be empty and we hold the
263 * only reference to it so it can safely be checked unlocked.
264 */
265STATIC int
266xlog_grant_head_check(
267 struct log *log,
268 struct xlog_grant_head *head,
269 struct xlog_ticket *tic,
270 int *need_bytes)
271{
272 int free_bytes;
273 int error = 0;
274
275 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
276
277 /*
278 * If there are other waiters on the queue then give them a chance at
279 * logspace before us. Wake up the first waiters, if we do not wake
280 * up all the waiters then go to sleep waiting for more free space,
281 * otherwise try to get some space for this transaction.
282 */
283 *need_bytes = xlog_ticket_reservation(log, head, tic);
284 free_bytes = xlog_space_left(log, &head->grant);
285 if (!list_empty_careful(&head->waiters)) {
286 spin_lock(&head->lock);
287 if (!xlog_grant_head_wake(log, head, &free_bytes) ||
288 free_bytes < *need_bytes) {
289 error = xlog_grant_head_wait(log, head, tic,
290 *need_bytes);
291 }
292 spin_unlock(&head->lock);
293 } else if (free_bytes < *need_bytes) {
294 spin_lock(&head->lock);
295 error = xlog_grant_head_wait(log, head, tic, *need_bytes);
296 spin_unlock(&head->lock);
297 }
298
299 return error;
300}
301
Christoph Hellwig0adba532007-08-30 17:21:46 +1000302static void
303xlog_tic_reset_res(xlog_ticket_t *tic)
304{
305 tic->t_res_num = 0;
306 tic->t_res_arr_sum = 0;
307 tic->t_res_num_ophdrs = 0;
308}
309
310static void
311xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
312{
313 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
314 /* add to overflow and start again */
315 tic->t_res_o_flow += tic->t_res_arr_sum;
316 tic->t_res_num = 0;
317 tic->t_res_arr_sum = 0;
318 }
319
320 tic->t_res_arr[tic->t_res_num].r_len = len;
321 tic->t_res_arr[tic->t_res_num].r_type = type;
322 tic->t_res_arr_sum += len;
323 tic->t_res_num++;
324}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*
327 * NOTES:
328 *
329 * 1. currblock field gets updated at startup and after in-core logs
330 * marked as with WANT_SYNC.
331 */
332
333/*
334 * This routine is called when a user of a log manager ticket is done with
335 * the reservation. If the ticket was ever used, then a commit record for
336 * the associated transaction is written out as a log operation header with
337 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
338 * a given ticket. If the ticket was one with a permanent reservation, then
339 * a few operations are done differently. Permanent reservation tickets by
340 * default don't release the reservation. They just commit the current
341 * transaction with the belief that the reservation is still needed. A flag
342 * must be passed in before permanent reservations are actually released.
343 * When these type of tickets are not released, they need to be set into
344 * the inited state again. By doing this, a start record will be written
345 * out when the next write occurs.
346 */
347xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000348xfs_log_done(
349 struct xfs_mount *mp,
350 struct xlog_ticket *ticket,
351 struct xlog_in_core **iclog,
352 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000354 struct log *log = mp->m_log;
355 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (XLOG_FORCED_SHUTDOWN(log) ||
358 /*
359 * If nothing was ever written, don't write out commit record.
360 * If we get an error, just continue and give back the log ticket.
361 */
362 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100363 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 lsn = (xfs_lsn_t) -1;
365 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
366 flags |= XFS_LOG_REL_PERM_RESERV;
367 }
368 }
369
370
371 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
372 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000373 trace_xfs_log_done_nonperm(log, ticket);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000376 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * request has been made to release a permanent reservation.
378 */
379 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100380 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000382 trace_xfs_log_done_perm(log, ticket);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000385 /* If this ticket was a permanent reservation and we aren't
386 * trying to release it, reset the inited flags; so next time
387 * we write, a start record will be written out.
388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000393}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/*
396 * Attaches a new iclog I/O completion callback routine during
397 * transaction commit. If the log is in error state, a non-zero
398 * return code is handed back and the caller is responsible for
399 * executing the callback at an appropriate time.
400 */
401int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000402xfs_log_notify(
403 struct xfs_mount *mp,
404 struct xlog_in_core *iclog,
405 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000407 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
David Chinner114d23a2008-04-10 12:18:39 +1000409 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
411 if (!abortflg) {
412 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
413 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
414 cb->cb_next = NULL;
415 *(iclog->ic_callback_tail) = cb;
416 iclog->ic_callback_tail = &(cb->cb_next);
417 }
David Chinner114d23a2008-04-10 12:18:39 +1000418 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000420}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000423xfs_log_release_iclog(
424 struct xfs_mount *mp,
425 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000427 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000428 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100429 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 return 0;
433}
434
435/*
436 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
437 * to the reservation.
438 * 2. Potentially, push buffers at tail of log to disk.
439 *
440 * Each reservation is going to reserve extra space for a log record header.
441 * When writes happen to the on-disk log, we don't subtract the length of the
442 * log record header from any reservation. By wasting space in each
443 * reservation, we prevent over allocation problems.
444 */
445int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000446xfs_log_reserve(
447 struct xfs_mount *mp,
448 int unit_bytes,
449 int cnt,
450 struct xlog_ticket **ticket,
451 __uint8_t client,
452 uint flags,
453 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000455 struct log *log = mp->m_log;
456 struct xlog_ticket *internal_ticket;
457 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (XLOG_FORCED_SHUTDOWN(log))
462 return XFS_ERROR(EIO);
463
464 XFS_STATS_INC(xs_try_logspace);
465
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (*ticket != NULL) {
468 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000469 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000470
Dave Chinner524ee362010-05-07 11:05:05 +1000471 /*
472 * this is a new transaction on the ticket, so we need to
473 * change the transaction ID so that the next transaction has a
474 * different TID in the log. Just add one to the existing tid
475 * so that we can see chains of rolling transactions in the log
476 * easily.
477 */
478 internal_ticket->t_tid++;
479
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000480 trace_xfs_log_reserve(log, internal_ticket);
481
Dave Chinner2ced19c2010-12-21 12:09:20 +1100482 xlog_grant_push_ail(log, internal_ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 retval = xlog_regrant_write_log_space(log, internal_ticket);
484 } else {
485 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100486 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Dave Chinner3383ca52010-05-07 11:04:17 +1000487 client, flags,
488 KM_SLEEP|KM_MAYFAIL);
David Chinnereb01c9c2008-04-10 12:18:46 +1000489 if (!internal_ticket)
490 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000491 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000493
494 trace_xfs_log_reserve(log, internal_ticket);
495
Dave Chinner2ced19c2010-12-21 12:09:20 +1100496 xlog_grant_push_ail(log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 (internal_ticket->t_unit_res *
498 internal_ticket->t_cnt));
499 retval = xlog_grant_log_space(log, internal_ticket);
500 }
501
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000502 if (unlikely(retval)) {
503 /*
504 * If we are failing, make sure the ticket doesn't have any
505 * current reservations. We don't want to add this back
506 * when the ticket/ transaction gets cancelled.
507 */
508 internal_ticket->t_curr_res = 0;
509 /* ungrant will give back unit_res * t_cnt. */
510 internal_ticket->t_cnt = 0;
511 }
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return retval;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516
517/*
518 * Mount a log filesystem
519 *
520 * mp - ubiquitous xfs mount point structure
521 * log_target - buftarg of on-disk log device
522 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
523 * num_bblocks - Number of BBSIZE blocks in on-disk log
524 *
525 * Return error or zero.
526 */
527int
David Chinner249a8c12008-02-05 12:13:32 +1100528xfs_log_mount(
529 xfs_mount_t *mp,
530 xfs_buftarg_t *log_target,
531 xfs_daddr_t blk_offset,
532 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
David Chinner249a8c12008-02-05 12:13:32 +1100534 int error;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100537 xfs_notice(mp, "Mounting Filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100539 xfs_notice(mp,
540"Mounting filesystem in no-recovery mode. Filesystem will be inconsistent.");
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000541 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200545 if (IS_ERR(mp->m_log)) {
546 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100547 goto out;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /*
David Chinner249a8c12008-02-05 12:13:32 +1100551 * Initialize the AIL now we have a log.
552 */
David Chinner249a8c12008-02-05 12:13:32 +1100553 error = xfs_trans_ail_init(mp);
554 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100555 xfs_warn(mp, "AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100556 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100557 }
David Chinnera9c21c12008-10-30 17:39:35 +1100558 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100559
560 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * skip log recovery on a norecovery mount. pretend it all
562 * just worked.
563 */
564 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100565 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000568 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Eric Sandeen65be6052006-01-11 15:34:19 +1100570 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000573 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100575 xfs_warn(mp, "log mount/recovery failed: error %d",
576 error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100577 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 }
580
581 /* Normal transactions can now occur */
582 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
583
Dave Chinner71e330b2010-05-21 14:37:18 +1000584 /*
585 * Now the log has been fully initialised and we know were our
586 * space grant counters are, we can initialise the permanent ticket
587 * needed for delayed logging to work.
588 */
589 xlog_cil_init_post_recovery(mp->m_log);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100592
593out_destroy_ail:
594 xfs_trans_ail_destroy(mp);
595out_free_log:
596 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100597out:
David Chinner249a8c12008-02-05 12:13:32 +1100598 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601/*
602 * Finish the recovery of the file system. This is separate from
603 * the xfs_log_mount() call, because it depends on the code in
604 * xfs_mountfs() to read in the root and real-time bitmap inodes
605 * between calling xfs_log_mount() and here.
606 *
607 * mp - ubiquitous xfs mount point structure
608 */
609int
Christoph Hellwig42490232008-08-13 16:49:32 +1000610xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 int error;
613
614 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000615 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 else {
617 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000618 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621 return error;
622}
623
624/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 * Final log writes as part of unmount.
626 *
627 * Mark the filesystem clean as unmount happens. Note that during relocation
628 * this routine needs to be executed as part of source-bag while the
629 * deallocation must not be done until source-end.
630 */
631
632/*
633 * Unmount record used to have a string "Unmount filesystem--" in the
634 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
635 * We just write the magic number now since that particular field isn't
636 * currently architecture converted and "nUmount" is a bit foo.
637 * As far as I know, there weren't any dependencies on the old behaviour.
638 */
639
640int
641xfs_log_unmount_write(xfs_mount_t *mp)
642{
643 xlog_t *log = mp->m_log;
644 xlog_in_core_t *iclog;
645#ifdef DEBUG
646 xlog_in_core_t *first_iclog;
647#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000648 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 xfs_lsn_t lsn;
650 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * Don't write out unmount record on read-only mounts.
654 * Or, if we are doing a forced umount (typically because of IO errors).
655 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000656 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000659 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000660 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662#ifdef DEBUG
663 first_iclog = iclog = log->l_iclog;
664 do {
665 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
666 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
667 ASSERT(iclog->ic_offset == 0);
668 }
669 iclog = iclog->ic_next;
670 } while (iclog != first_iclog);
671#endif
672 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000673 error = xfs_log_reserve(mp, 600, 1, &tic,
674 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100676 /* the data section must be 32 bit size aligned */
677 struct {
678 __uint16_t magic;
679 __uint16_t pad1;
680 __uint32_t pad2; /* may as well make it 64 bits */
681 } magic = {
682 .magic = XLOG_UNMOUNT_TYPE,
683 };
684 struct xfs_log_iovec reg = {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000685 .i_addr = &magic,
Dave Chinner55b66332010-03-23 11:43:17 +1100686 .i_len = sizeof(magic),
687 .i_type = XLOG_REG_TYPE_UNMOUNT,
688 };
689 struct xfs_log_vec vec = {
690 .lv_niovecs = 1,
691 .lv_iovecp = &reg,
692 };
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100695 tic->t_flags = 0;
696 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 NULL, XLOG_UNMOUNT_TRANS);
698 /*
699 * At this point, we're umounting anyway,
700 * so there's no point in transitioning log state
701 * to IOERROR. Just continue...
702 */
703 }
704
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100705 if (error)
706 xfs_alert(mp, "%s: unmount record failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000709 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100711 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100713 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000714 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000716 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
718 iclog->ic_state == XLOG_STATE_DIRTY)) {
719 if (!XLOG_FORCED_SHUTDOWN(log)) {
Dave Chinnereb40a872010-12-21 12:09:01 +1100720 xlog_wait(&iclog->ic_force_wait,
721 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000723 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000726 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000728 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000729 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000730 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100731 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 } else {
734 /*
735 * We're already in forced_shutdown mode, couldn't
736 * even attempt to write out the unmount transaction.
737 *
738 * Go through the motions of sync'ing and releasing
739 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000740 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * be in progress. Do this as a separate section of
742 * code so we'll know if we ever get stuck here that
743 * we're in this odd situation of trying to unmount
744 * a file system that went into forced_shutdown as
745 * the result of an unmount..
746 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000747 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100749 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100752 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000753 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000755 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
758 || iclog->ic_state == XLOG_STATE_DIRTY
759 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
760
Dave Chinnereb40a872010-12-21 12:09:01 +1100761 xlog_wait(&iclog->ic_force_wait,
762 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000764 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 }
767
David Chinner1bb7d6b2008-04-10 12:24:38 +1000768 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769} /* xfs_log_unmount_write */
770
771/*
772 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100773 *
774 * We need to stop the aild from running before we destroy
775 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
777void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100778xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
David Chinner249a8c12008-02-05 12:13:32 +1100780 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000781 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Dave Chinner43f5efc2010-03-23 10:10:00 +1100784void
785xfs_log_item_init(
786 struct xfs_mount *mp,
787 struct xfs_log_item *item,
788 int type,
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000789 const struct xfs_item_ops *ops)
Dave Chinner43f5efc2010-03-23 10:10:00 +1100790{
791 item->li_mountp = mp;
792 item->li_ailp = mp->m_ail;
793 item->li_type = type;
794 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +1000795 item->li_lv = NULL;
796
797 INIT_LIST_HEAD(&item->li_ail);
798 INIT_LIST_HEAD(&item->li_cil);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100799}
800
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000801/*
802 * Wake up processes waiting for log space after we have moved the log tail.
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804void
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000805xfs_log_space_wake(
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000806 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000808 struct log *log = mp->m_log;
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000809 int free_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (XLOG_FORCED_SHUTDOWN(log))
812 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Christoph Hellwig28496962012-02-20 02:31:25 +0000814 if (!list_empty_careful(&log->l_write_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000815 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
816
Christoph Hellwig28496962012-02-20 02:31:25 +0000817 spin_lock(&log->l_write_head.lock);
818 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
Christoph Hellwige179840d2012-02-20 02:31:29 +0000819 xlog_grant_head_wake(log, &log->l_write_head, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +0000820 spin_unlock(&log->l_write_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Dave Chinner10547942010-12-21 12:02:25 +1100822
Christoph Hellwig28496962012-02-20 02:31:25 +0000823 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000824 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
825
Christoph Hellwig28496962012-02-20 02:31:25 +0000826 spin_lock(&log->l_reserve_head.lock);
827 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Christoph Hellwige179840d2012-02-20 02:31:29 +0000828 xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +0000829 spin_unlock(&log->l_reserve_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Dave Chinner3f16b982010-12-21 12:29:01 +1100831}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833/*
834 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000835 * that needs to be covered. To begin the transition to the idle state
836 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
837 * If we are then in a state where covering is needed, the caller is informed
838 * that dummy transactions are required to move the log into the idle state.
839 *
840 * Because this is called as part of the sync process, we should also indicate
841 * that dummy transactions should be issued in anything but the covered or
842 * idle states. This ensures that the log tail is accurately reflected in
843 * the log at the end of the sync, hence if a crash occurrs avoids replay
844 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
846int
847xfs_log_need_covered(xfs_mount_t *mp)
848{
David Chinner27d8d5f2008-10-30 17:38:39 +1100849 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
David Chinner92821e22007-05-24 15:26:31 +1000852 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000855 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000856 switch (log->l_covered_state) {
857 case XLOG_STATE_COVER_DONE:
858 case XLOG_STATE_COVER_DONE2:
859 case XLOG_STATE_COVER_IDLE:
860 break;
861 case XLOG_STATE_COVER_NEED:
862 case XLOG_STATE_COVER_NEED2:
Dave Chinnerfd074842011-04-08 12:45:07 +1000863 if (!xfs_ail_min_lsn(log->l_ailp) &&
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000864 xlog_iclogs_empty(log)) {
865 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
866 log->l_covered_state = XLOG_STATE_COVER_DONE;
867 else
868 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000870 /* FALLTHRU */
871 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000875 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100876 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000879/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * We may be holding the log iclog lock upon entering this routine.
881 */
882xfs_lsn_t
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100883xlog_assign_tail_lsn(
884 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100886 xfs_lsn_t tail_lsn;
887 struct log *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000889 /*
890 * To make sure we always have a valid LSN for the log tail we keep
891 * track of the last LSN which was committed in log->l_last_sync_lsn,
892 * and use that when the AIL was empty and xfs_ail_min_lsn returns 0.
893 *
894 * If the AIL has been emptied we also need to wake any process
895 * waiting for this condition.
896 */
Dave Chinnerfd074842011-04-08 12:45:07 +1000897 tail_lsn = xfs_ail_min_lsn(mp->m_ail);
Dave Chinner84f3c682010-12-03 22:11:29 +1100898 if (!tail_lsn)
899 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100900 atomic64_set(&log->l_tail_lsn, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return tail_lsn;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100902}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904/*
905 * Return the space in the log between the tail and the head. The head
906 * is passed in the cycle/bytes formal parms. In the special case where
907 * the reserve head has wrapped passed the tail, this calculation is no
908 * longer valid. In this case, just return 0 which means there is no space
909 * in the log. This works for all places where this function is called
910 * with the reserve head. Of course, if the write head were to ever
911 * wrap the tail, we should blow up. Rather than catch this case here,
912 * we depend on other ASSERTions in other parts of the code. XXXmiken
913 *
914 * This code also handles the case where the reservation head is behind
915 * the tail. The details of this case are described below, but the end
916 * result is that we return the size of the log as the amount of space left.
917 */
David Chinnera8272ce2007-11-23 16:28:09 +1100918STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +1100919xlog_space_left(
920 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100921 atomic64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Dave Chinnera69ed032010-12-21 12:08:20 +1100923 int free_bytes;
924 int tail_bytes;
925 int tail_cycle;
926 int head_cycle;
927 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dave Chinnera69ed032010-12-21 12:08:20 +1100929 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100930 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
931 tail_bytes = BBTOB(tail_bytes);
Dave Chinnera69ed032010-12-21 12:08:20 +1100932 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
933 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
934 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +1100936 else if (tail_cycle < head_cycle) {
937 ASSERT(tail_cycle == (head_cycle - 1));
938 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 } else {
940 /*
941 * The reservation head is behind the tail.
942 * In this case we just want to return the size of the
943 * log as the amount of space left.
944 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100945 xfs_alert(log->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 "xlog_space_left: head behind tail\n"
947 " tail_cycle = %d, tail_bytes = %d\n"
948 " GH cycle = %d, GH bytes = %d",
Dave Chinnera69ed032010-12-21 12:08:20 +1100949 tail_cycle, tail_bytes, head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ASSERT(0);
951 free_bytes = log->l_logsize;
952 }
953 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +1100954}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956
957/*
958 * Log function which is called when an io completes.
959 *
960 * The log manager needs its own routine, in order to control what
961 * happens with the buffer after the write completes.
962 */
963void
964xlog_iodone(xfs_buf_t *bp)
965{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200966 xlog_in_core_t *iclog = bp->b_fspriv;
967 xlog_t *l = iclog->ic_log;
968 int aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /*
971 * Race to shutdown the filesystem if we see an error.
972 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000973 if (XFS_TEST_ERROR((xfs_buf_geterror(bp)), l->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
Christoph Hellwig901796a2011-10-10 16:52:49 +0000975 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000976 xfs_buf_stale(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000977 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * This flag will be propagated to the trans-committed
980 * callback routines to let them know that the log-commit
981 * didn't succeed.
982 */
983 aborted = XFS_LI_ABORTED;
984 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
985 aborted = XFS_LI_ABORTED;
986 }
David Chinner3db296f2007-05-14 18:24:16 +1000987
988 /* log I/O is always issued ASYNC */
989 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000991 /*
992 * do not reference the buffer (bp) here as we could race
993 * with it being freed after writing the unmount record to the
994 * log.
995 */
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997} /* xlog_iodone */
998
999/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * Return size of each in-core log record buffer.
1001 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001002 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
1004 * If the filesystem blocksize is too large, we may need to choose a
1005 * larger size since the directory code currently logs entire blocks.
1006 */
1007
1008STATIC void
1009xlog_get_iclog_buffer_size(xfs_mount_t *mp,
1010 xlog_t *log)
1011{
1012 int size;
1013 int xhdrs;
1014
Eric Sandeen1cb51252007-08-16 16:24:43 +10001015 if (mp->m_logbufs <= 0)
1016 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1017 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001018 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /*
1021 * Buffer size passed in from mount system call.
1022 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001023 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 size = log->l_iclog_size = mp->m_logbsize;
1025 log->l_iclog_size_log = 0;
1026 while (size != 1) {
1027 log->l_iclog_size_log++;
1028 size >>= 1;
1029 }
1030
Eric Sandeen62118702008-03-06 13:44:28 +11001031 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001032 /* # headers = size / 32k
1033 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
1035
1036 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1037 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1038 xhdrs++;
1039 log->l_iclog_hsize = xhdrs << BBSHIFT;
1040 log->l_iclog_heads = xhdrs;
1041 } else {
1042 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1043 log->l_iclog_hsize = BBSIZE;
1044 log->l_iclog_heads = 1;
1045 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001046 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001049 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001050 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1051 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* the default log size is 16k or 32k which is one header sector */
1054 log->l_iclog_hsize = BBSIZE;
1055 log->l_iclog_heads = 1;
1056
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001057done:
1058 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001059 if (mp->m_logbufs == 0)
1060 mp->m_logbufs = log->l_iclog_bufs;
1061 if (mp->m_logbsize == 0)
1062 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063} /* xlog_get_iclog_buffer_size */
1064
1065
1066/*
1067 * This routine initializes some of the log structure for a given mount point.
1068 * Its primary purpose is to fill in enough, so recovery can occur. However,
1069 * some other stuff may be filled in too.
1070 */
1071STATIC xlog_t *
1072xlog_alloc_log(xfs_mount_t *mp,
1073 xfs_buftarg_t *log_target,
1074 xfs_daddr_t blk_offset,
1075 int num_bblks)
1076{
1077 xlog_t *log;
1078 xlog_rec_header_t *head;
1079 xlog_in_core_t **iclogp;
1080 xlog_in_core_t *iclog, *prev_iclog=NULL;
1081 xfs_buf_t *bp;
1082 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001083 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001084 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Dave Chinner644c3562008-11-10 16:50:24 +11001086 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001087 if (!log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001088 xfs_warn(mp, "Log allocation failed: No memory!");
Dave Chinnera6cb7672009-04-06 18:39:27 +02001089 goto out;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 log->l_mp = mp;
1093 log->l_targ = log_target;
1094 log->l_logsize = BBTOB(num_bblks);
1095 log->l_logBBstart = blk_offset;
1096 log->l_logBBsize = num_bblks;
1097 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1098 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1099
1100 log->l_prev_block = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001102 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1103 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Christoph Hellwigc303c5b2012-02-20 02:31:26 +00001105
1106 xlog_grant_head_init(&log->l_reserve_head);
1107 xlog_grant_head_init(&log->l_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Dave Chinnera6cb7672009-04-06 18:39:27 +02001109 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001110 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001111 log2_size = mp->m_sb.sb_logsectlog;
1112 if (log2_size < BBSHIFT) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001113 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1114 log2_size, BBSHIFT);
Alex Elder69ce58f2010-04-20 17:09:59 +10001115 goto out_free_log;
1116 }
1117
1118 log2_size -= BBSHIFT;
1119 if (log2_size > mp->m_sectbb_log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001120 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1121 log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001122 goto out_free_log;
1123 }
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001126 if (log2_size && log->l_logBBstart > 0 &&
1127 !xfs_sb_version_haslogv2(&mp->m_sb)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001128 xfs_warn(mp,
1129 "log sector size (0x%x) invalid for configuration.",
1130 log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001131 goto out_free_log;
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001134 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 xlog_get_iclog_buffer_size(mp, log);
1137
Dave Chinnera6cb7672009-04-06 18:39:27 +02001138 error = ENOMEM;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +00001139 bp = xfs_buf_alloc(mp->m_logdev_targp, 0, log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001140 if (!bp)
1141 goto out_free_log;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001142 bp->b_iodone = xlog_iodone;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001143 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 log->l_xbuf = bp;
1145
Eric Sandeen007c61c2007-10-11 17:43:56 +10001146 spin_lock_init(&log->l_icloglock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001147 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1150 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1151
1152 iclogp = &log->l_iclog;
1153 /*
1154 * The amount of memory to allocate for the iclog structure is
1155 * rather funky due to the way the structure is defined. It is
1156 * done this way so that we can use different sizes for machines
1157 * with different amounts of memory. See the definition of
1158 * xlog_in_core_t in xfs_log_priv.h for details.
1159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ASSERT(log->l_iclog_size >= 4096);
1161 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001162 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1163 if (!*iclogp)
1164 goto out_free_iclog;
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 iclog->ic_prev = prev_iclog;
1168 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001169
Dave Chinner686865f2010-09-24 20:07:47 +10001170 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1171 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001172 if (!bp)
1173 goto out_free_iclog;
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001174
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001175 bp->b_iodone = xlog_iodone;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001176 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001177 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001178#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 head = &iclog->ic_header;
1182 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001183 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1184 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001185 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001186 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001188 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1192 iclog->ic_state = XLOG_STATE_ACTIVE;
1193 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001194 atomic_set(&iclog->ic_refcnt, 0);
1195 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001197 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001199 ASSERT(xfs_buf_islocked(iclog->ic_bp));
Dave Chinnereb40a872010-12-21 12:09:01 +11001200 init_waitqueue_head(&iclog->ic_force_wait);
1201 init_waitqueue_head(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 iclogp = &iclog->ic_next;
1204 }
1205 *iclogp = log->l_iclog; /* complete ring */
1206 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1207
Dave Chinner71e330b2010-05-21 14:37:18 +10001208 error = xlog_cil_init(log);
1209 if (error)
1210 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001212
1213out_free_iclog:
1214 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1215 prev_iclog = iclog->ic_next;
Dave Chinnereb40a872010-12-21 12:09:01 +11001216 if (iclog->ic_bp)
Dave Chinner644c3562008-11-10 16:50:24 +11001217 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001218 kmem_free(iclog);
1219 }
1220 spinlock_destroy(&log->l_icloglock);
Dave Chinner644c3562008-11-10 16:50:24 +11001221 xfs_buf_free(log->l_xbuf);
1222out_free_log:
1223 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001224out:
1225 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226} /* xlog_alloc_log */
1227
1228
1229/*
1230 * Write out the commit record of a transaction associated with the given
1231 * ticket. Return the lsn of the commit record.
1232 */
1233STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001234xlog_commit_record(
1235 struct log *log,
1236 struct xlog_ticket *ticket,
1237 struct xlog_in_core **iclog,
1238 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Dave Chinner55b66332010-03-23 11:43:17 +11001240 struct xfs_mount *mp = log->l_mp;
1241 int error;
1242 struct xfs_log_iovec reg = {
1243 .i_addr = NULL,
1244 .i_len = 0,
1245 .i_type = XLOG_REG_TYPE_COMMIT,
1246 };
1247 struct xfs_log_vec vec = {
1248 .lv_niovecs = 1,
1249 .lv_iovecp = &reg,
1250 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001253 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1254 XLOG_COMMIT_TRANS);
1255 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001256 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001257 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260/*
1261 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1262 * log space. This code pushes on the lsn which would supposedly free up
1263 * the 25% which we want to leave free. We may need to adopt a policy which
1264 * pushes on an lsn which is further along in the log once we reach the high
1265 * water mark. In this manner, we would be creating a low water mark.
1266 */
David Chinnera8272ce2007-11-23 16:28:09 +11001267STATIC void
Dave Chinner2ced19c2010-12-21 12:09:20 +11001268xlog_grant_push_ail(
1269 struct log *log,
1270 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001272 xfs_lsn_t threshold_lsn = 0;
Dave Chinner84f3c682010-12-03 22:11:29 +11001273 xfs_lsn_t last_sync_lsn;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001274 int free_blocks;
1275 int free_bytes;
1276 int threshold_block;
1277 int threshold_cycle;
1278 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Dave Chinner2ced19c2010-12-21 12:09:20 +11001280 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Christoph Hellwig28496962012-02-20 02:31:25 +00001282 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001283 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Dave Chinner2ced19c2010-12-21 12:09:20 +11001285 /*
1286 * Set the threshold for the minimum number of free blocks in the
1287 * log to the maximum of what the caller needs, one quarter of the
1288 * log, and 256 blocks.
1289 */
1290 free_threshold = BTOBB(need_bytes);
1291 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1292 free_threshold = MAX(free_threshold, 256);
1293 if (free_blocks >= free_threshold)
1294 return;
1295
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001296 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1297 &threshold_block);
1298 threshold_block += free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001300 threshold_block -= log->l_logBBsize;
1301 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001303 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1304 threshold_block);
1305 /*
1306 * Don't pass in an lsn greater than the lsn of the last
Dave Chinner84f3c682010-12-03 22:11:29 +11001307 * log record known to be on disk. Use a snapshot of the last sync lsn
1308 * so that it doesn't change between the compare and the set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Dave Chinner84f3c682010-12-03 22:11:29 +11001310 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1311 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1312 threshold_lsn = last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Dave Chinner2ced19c2010-12-21 12:09:20 +11001314 /*
1315 * Get the transaction layer to kick the dirty buffers out to
1316 * disk asynchronously. No point in trying to do this if
1317 * the filesystem is shutting down.
1318 */
1319 if (!XLOG_FORCED_SHUTDOWN(log))
Dave Chinnerfd074842011-04-08 12:45:07 +10001320 xfs_ail_push(log->l_ailp, threshold_lsn);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Christoph Hellwig873ff552010-01-13 22:17:57 +00001323/*
1324 * The bdstrat callback function for log bufs. This gives us a central
1325 * place to trap bufs in case we get hit by a log I/O error and need to
1326 * shutdown. Actually, in practice, even when we didn't get a log error,
1327 * we transition the iclogs to IOERROR state *after* flushing all existing
1328 * iclogs to disk. This is because we don't want anymore new transactions to be
1329 * started or completed afterwards.
1330 */
1331STATIC int
1332xlog_bdstrat(
1333 struct xfs_buf *bp)
1334{
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001335 struct xlog_in_core *iclog = bp->b_fspriv;
Christoph Hellwig873ff552010-01-13 22:17:57 +00001336
Christoph Hellwig873ff552010-01-13 22:17:57 +00001337 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001338 xfs_buf_ioerror(bp, EIO);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001339 xfs_buf_stale(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001340 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001341 /*
1342 * It would seem logical to return EIO here, but we rely on
1343 * the log state machine to propagate I/O errors instead of
1344 * doing it here.
1345 */
1346 return 0;
1347 }
1348
Christoph Hellwig873ff552010-01-13 22:17:57 +00001349 xfs_buf_iorequest(bp);
1350 return 0;
1351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353/*
1354 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1355 * fashion. Previously, we should have moved the current iclog
1356 * ptr in the log to point to the next available iclog. This allows further
1357 * write to continue while this code syncs out an iclog ready to go.
1358 * Before an in-core log can be written out, the data section must be scanned
1359 * to save away the 1st word of each BBSIZE block into the header. We replace
1360 * it with the current cycle count. Each BBSIZE block is tagged with the
1361 * cycle count because there in an implicit assumption that drives will
1362 * guarantee that entire 512 byte blocks get written at once. In other words,
1363 * we can't have part of a 512 byte block written and part not written. By
1364 * tagging each block, we will know which blocks are valid when recovering
1365 * after an unclean shutdown.
1366 *
1367 * This routine is single threaded on the iclog. No other thread can be in
1368 * this routine with the same iclog. Changing contents of iclog can there-
1369 * fore be done without grabbing the state machine lock. Updating the global
1370 * log will require grabbing the lock though.
1371 *
1372 * The entire log manager uses a logical block numbering scheme. Only
1373 * log_sync (and then only bwrite()) know about the fact that the log may
1374 * not start with block zero on a given device. The log block start offset
1375 * is added immediately before calling bwrite().
1376 */
1377
David Chinnera8272ce2007-11-23 16:28:09 +11001378STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379xlog_sync(xlog_t *log,
1380 xlog_in_core_t *iclog)
1381{
1382 xfs_caddr_t dptr; /* pointer to byte sized element */
1383 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001384 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 uint count; /* byte count of bwrite */
1386 uint count_init; /* initial count before roundup */
1387 int roundoff; /* roundoff to BB or stripe */
1388 int split = 0; /* split write into two regions */
1389 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001390 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001393 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* Add for LR header */
1396 count_init = log->l_iclog_hsize + iclog->ic_offset;
1397
1398 /* Round out the log write size */
1399 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1400 /* we have a v2 stripe unit to use */
1401 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1402 } else {
1403 count = BBTOB(BTOBB(count_init));
1404 }
1405 roundoff = count - count_init;
1406 ASSERT(roundoff >= 0);
1407 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1408 roundoff < log->l_mp->m_sb.sb_logsunit)
1409 ||
1410 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1411 roundoff < BBTOB(1)));
1412
1413 /* move grant heads by roundoff in sync */
Christoph Hellwig28496962012-02-20 02:31:25 +00001414 xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
1415 xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* put cycle number in every block */
1418 xlog_pack_data(log, iclog, roundoff);
1419
1420 /* real byte length */
1421 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001422 iclog->ic_header.h_len =
1423 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001425 iclog->ic_header.h_len =
1426 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Nathan Scottf5faad72006-07-28 17:04:44 +10001429 bp = iclog->ic_bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001430 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1433
1434 /* Do we need to split this write into 2 parts? */
1435 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1436 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1437 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1438 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1439 } else {
1440 iclog->ic_bwritecnt = 1;
1441 }
David Chinner511105b2007-05-24 15:21:57 +10001442 XFS_BUF_SET_COUNT(bp, count);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001443 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001444 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001446 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001447
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001448 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) {
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001449 bp->b_flags |= XBF_FUA;
1450
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001451 /*
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001452 * Flush the data device before flushing the log to make
1453 * sure all meta data written back from the AIL actually made
1454 * it to disk before stamping the new log tail LSN into the
1455 * log buffer. For an external log we need to issue the
1456 * flush explicitly, and unfortunately synchronously here;
1457 * for an internal log we can simply use the block layer
1458 * state machine for preflushes.
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001459 */
1460 if (log->l_mp->m_logdev_targp != log->l_mp->m_ddev_targp)
1461 xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001462 else
1463 bp->b_flags |= XBF_FLUSH;
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1467 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1468
1469 xlog_verify_iclog(log, iclog, count, B_TRUE);
1470
1471 /* account for log which doesn't start at block #0 */
1472 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1473 /*
1474 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1475 * is shutting down.
1476 */
1477 XFS_BUF_WRITE(bp);
1478
Christoph Hellwig901796a2011-10-10 16:52:49 +00001479 error = xlog_bdstrat(bp);
1480 if (error) {
1481 xfs_buf_ioerror_alert(bp, "xlog_sync");
Jesper Juhl014c2542006-01-15 02:37:08 +01001482 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001485 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
Chandra Seetharaman02fe03d2011-07-22 23:40:22 +00001487 xfs_buf_associate_memory(bp,
1488 (char *)&iclog->ic_header + count, split);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001489 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001490 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001492 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001493 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001494 bp->b_flags |= XBF_FUA;
Chandra Seetharaman62926042011-07-22 23:40:15 +00001495 dptr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /*
1497 * Bump the cycle numbers at the start of each block
1498 * since this part of the buffer is at the start of
1499 * a new cycle. Watch out for the header magic number
1500 * case, though.
1501 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001502 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001503 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001504 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001505 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 dptr += BBSIZE;
1507 }
1508
1509 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1510 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1511
Nathan Scottc41564b2006-03-29 08:55:14 +10001512 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1514 XFS_BUF_WRITE(bp);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001515 error = xlog_bdstrat(bp);
1516 if (error) {
1517 xfs_buf_ioerror_alert(bp, "xlog_sync (split)");
Jesper Juhl014c2542006-01-15 02:37:08 +01001518 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522} /* xlog_sync */
1523
1524
1525/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001526 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
David Chinnera8272ce2007-11-23 16:28:09 +11001528STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001529xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int i;
1533
Dave Chinner71e330b2010-05-21 14:37:18 +10001534 xlog_cil_destroy(log);
1535
Dave Chinner44396472011-04-21 09:34:27 +00001536 /*
1537 * always need to ensure that the extra buffer does not point to memory
1538 * owned by another log buffer before we free it.
1539 */
1540 xfs_buf_set_empty(log->l_xbuf, log->l_iclog_size);
1541 xfs_buf_free(log->l_xbuf);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 iclog = log->l_iclog;
1544 for (i=0; i<log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001547 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 iclog = next_iclog;
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 spinlock_destroy(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001553 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001554} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556/*
1557 * Update counters atomically now that memcpy is done.
1558 */
1559/* ARGSUSED */
1560static inline void
1561xlog_state_finish_copy(xlog_t *log,
1562 xlog_in_core_t *iclog,
1563 int record_cnt,
1564 int copy_bytes)
1565{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001566 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001568 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 iclog->ic_offset += copy_bytes;
1570
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001571 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572} /* xlog_state_finish_copy */
1573
1574
1575
1576
1577/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001578 * print out info relating to regions written which consume
1579 * the reservation
1580 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001581void
1582xlog_print_tic_res(
1583 struct xfs_mount *mp,
1584 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001585{
1586 uint i;
1587 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1588
1589 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1590 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1591 "bformat",
1592 "bchunk",
1593 "efi_format",
1594 "efd_format",
1595 "iformat",
1596 "icore",
1597 "iext",
1598 "ibroot",
1599 "ilocal",
1600 "iattr_ext",
1601 "iattr_broot",
1602 "iattr_local",
1603 "qformat",
1604 "dquot",
1605 "quotaoff",
1606 "LR header",
1607 "unmount",
1608 "commit",
1609 "trans header"
1610 };
1611 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1612 "SETATTR_NOT_SIZE",
1613 "SETATTR_SIZE",
1614 "INACTIVE",
1615 "CREATE",
1616 "CREATE_TRUNC",
1617 "TRUNCATE_FILE",
1618 "REMOVE",
1619 "LINK",
1620 "RENAME",
1621 "MKDIR",
1622 "RMDIR",
1623 "SYMLINK",
1624 "SET_DMATTRS",
1625 "GROWFS",
1626 "STRAT_WRITE",
1627 "DIOSTRAT",
1628 "WRITE_SYNC",
1629 "WRITEID",
1630 "ADDAFORK",
1631 "ATTRINVAL",
1632 "ATRUNCATE",
1633 "ATTR_SET",
1634 "ATTR_RM",
1635 "ATTR_FLAG",
1636 "CLEAR_AGI_BUCKET",
1637 "QM_SBCHANGE",
1638 "DUMMY1",
1639 "DUMMY2",
1640 "QM_QUOTAOFF",
1641 "QM_DQALLOC",
1642 "QM_SETQLIM",
1643 "QM_DQCLUSTER",
1644 "QM_QINOCREATE",
1645 "QM_QUOTAOFF_END",
1646 "SB_UNIT",
1647 "FSYNC_TS",
1648 "GROWFSRT_ALLOC",
1649 "GROWFSRT_ZERO",
1650 "GROWFSRT_FREE",
1651 "SWAPEXT"
1652 };
1653
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001654 xfs_warn(mp,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001655 "xlog_write: reservation summary:\n"
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001656 " trans type = %s (%u)\n"
1657 " unit res = %d bytes\n"
1658 " current res = %d bytes\n"
1659 " total reg = %u bytes (o/flow = %u bytes)\n"
1660 " ophdrs = %u (ophdr space = %u bytes)\n"
1661 " ophdr + reg = %u bytes\n"
1662 " num regions = %u\n",
1663 ((ticket->t_trans_type <= 0 ||
1664 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1665 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1666 ticket->t_trans_type,
1667 ticket->t_unit_res,
1668 ticket->t_curr_res,
1669 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1670 ticket->t_res_num_ophdrs, ophdr_spc,
1671 ticket->t_res_arr_sum +
1672 ticket->t_res_o_flow + ophdr_spc,
1673 ticket->t_res_num);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001674
1675 for (i = 0; i < ticket->t_res_num; i++) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001676 uint r_type = ticket->t_res_arr[i].r_type;
1677 xfs_warn(mp, "region[%u]: %s - %u bytes\n", i,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001678 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1679 "bad-rtype" : res_type_str[r_type-1]),
1680 ticket->t_res_arr[i].r_len);
1681 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001682
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001683 xfs_alert_tag(mp, XFS_PTAG_LOGRES,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001684 "xlog_write: reservation ran out. Need to up reservation");
Dave Chinner169a7b02010-05-07 11:05:31 +10001685 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001686}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001687
1688/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001689 * Calculate the potential space needed by the log vector. Each region gets
1690 * its own xlog_op_header_t and may need to be double word aligned.
1691 */
1692static int
1693xlog_write_calc_vec_length(
1694 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001695 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001696{
Dave Chinner55b66332010-03-23 11:43:17 +11001697 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001698 int headers = 0;
1699 int len = 0;
1700 int i;
1701
1702 /* acct for start rec of xact */
1703 if (ticket->t_flags & XLOG_TIC_INITED)
1704 headers++;
1705
Dave Chinner55b66332010-03-23 11:43:17 +11001706 for (lv = log_vector; lv; lv = lv->lv_next) {
1707 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001708
Dave Chinner55b66332010-03-23 11:43:17 +11001709 for (i = 0; i < lv->lv_niovecs; i++) {
1710 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1711
1712 len += vecp->i_len;
1713 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1714 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001715 }
1716
1717 ticket->t_res_num_ophdrs += headers;
1718 len += headers * sizeof(struct xlog_op_header);
1719
1720 return len;
1721}
1722
1723/*
1724 * If first write for transaction, insert start record We can't be trying to
1725 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1726 */
1727static int
1728xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001729 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001730 struct xlog_ticket *ticket)
1731{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001732 if (!(ticket->t_flags & XLOG_TIC_INITED))
1733 return 0;
1734
1735 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1736 ophdr->oh_clientid = ticket->t_clientid;
1737 ophdr->oh_len = 0;
1738 ophdr->oh_flags = XLOG_START_TRANS;
1739 ophdr->oh_res2 = 0;
1740
1741 ticket->t_flags &= ~XLOG_TIC_INITED;
1742
1743 return sizeof(struct xlog_op_header);
1744}
1745
1746static xlog_op_header_t *
1747xlog_write_setup_ophdr(
1748 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001749 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001750 struct xlog_ticket *ticket,
1751 uint flags)
1752{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001753 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1754 ophdr->oh_clientid = ticket->t_clientid;
1755 ophdr->oh_res2 = 0;
1756
1757 /* are we copying a commit or unmount record? */
1758 ophdr->oh_flags = flags;
1759
1760 /*
1761 * We've seen logs corrupted with bad transaction client ids. This
1762 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1763 * and shut down the filesystem.
1764 */
1765 switch (ophdr->oh_clientid) {
1766 case XFS_TRANSACTION:
1767 case XFS_VOLUME:
1768 case XFS_LOG:
1769 break;
1770 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001771 xfs_warn(log->l_mp,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001772 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1773 ophdr->oh_clientid, ticket);
1774 return NULL;
1775 }
1776
1777 return ophdr;
1778}
1779
1780/*
1781 * Set up the parameters of the region copy into the log. This has
1782 * to handle region write split across multiple log buffers - this
1783 * state is kept external to this function so that this code can
1784 * can be written in an obvious, self documenting manner.
1785 */
1786static int
1787xlog_write_setup_copy(
1788 struct xlog_ticket *ticket,
1789 struct xlog_op_header *ophdr,
1790 int space_available,
1791 int space_required,
1792 int *copy_off,
1793 int *copy_len,
1794 int *last_was_partial_copy,
1795 int *bytes_consumed)
1796{
1797 int still_to_copy;
1798
1799 still_to_copy = space_required - *bytes_consumed;
1800 *copy_off = *bytes_consumed;
1801
1802 if (still_to_copy <= space_available) {
1803 /* write of region completes here */
1804 *copy_len = still_to_copy;
1805 ophdr->oh_len = cpu_to_be32(*copy_len);
1806 if (*last_was_partial_copy)
1807 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1808 *last_was_partial_copy = 0;
1809 *bytes_consumed = 0;
1810 return 0;
1811 }
1812
1813 /* partial write of region, needs extra log op header reservation */
1814 *copy_len = space_available;
1815 ophdr->oh_len = cpu_to_be32(*copy_len);
1816 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1817 if (*last_was_partial_copy)
1818 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1819 *bytes_consumed += *copy_len;
1820 (*last_was_partial_copy)++;
1821
1822 /* account for new log op header */
1823 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1824 ticket->t_res_num_ophdrs++;
1825
1826 return sizeof(struct xlog_op_header);
1827}
1828
1829static int
1830xlog_write_copy_finish(
1831 struct log *log,
1832 struct xlog_in_core *iclog,
1833 uint flags,
1834 int *record_cnt,
1835 int *data_cnt,
1836 int *partial_copy,
1837 int *partial_copy_len,
1838 int log_offset,
1839 struct xlog_in_core **commit_iclog)
1840{
1841 if (*partial_copy) {
1842 /*
1843 * This iclog has already been marked WANT_SYNC by
1844 * xlog_state_get_iclog_space.
1845 */
1846 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1847 *record_cnt = 0;
1848 *data_cnt = 0;
1849 return xlog_state_release_iclog(log, iclog);
1850 }
1851
1852 *partial_copy = 0;
1853 *partial_copy_len = 0;
1854
1855 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1856 /* no more space in this iclog - push it. */
1857 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1858 *record_cnt = 0;
1859 *data_cnt = 0;
1860
1861 spin_lock(&log->l_icloglock);
1862 xlog_state_want_sync(log, iclog);
1863 spin_unlock(&log->l_icloglock);
1864
1865 if (!commit_iclog)
1866 return xlog_state_release_iclog(log, iclog);
1867 ASSERT(flags & XLOG_COMMIT_TRANS);
1868 *commit_iclog = iclog;
1869 }
1870
1871 return 0;
1872}
1873
1874/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * Write some region out to in-core log
1876 *
1877 * This will be called when writing externally provided regions or when
1878 * writing out a commit record for a given transaction.
1879 *
1880 * General algorithm:
1881 * 1. Find total length of this write. This may include adding to the
1882 * lengths passed in.
1883 * 2. Check whether we violate the tickets reservation.
1884 * 3. While writing to this iclog
1885 * A. Reserve as much space in this iclog as can get
1886 * B. If this is first write, save away start lsn
1887 * C. While writing this region:
1888 * 1. If first write of transaction, write start record
1889 * 2. Write log operation header (header per region)
1890 * 3. Find out if we can fit entire region into this iclog
1891 * 4. Potentially, verify destination memcpy ptr
1892 * 5. Memcpy (partial) region
1893 * 6. If partial copy, release iclog; otherwise, continue
1894 * copying more regions into current iclog
1895 * 4. Mark want sync bit (in simulation mode)
1896 * 5. Release iclog for potential flush to on-disk log.
1897 *
1898 * ERRORS:
1899 * 1. Panic if reservation is overrun. This should never happen since
1900 * reservation amounts are generated internal to the filesystem.
1901 * NOTES:
1902 * 1. Tickets are single threaded data structures.
1903 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1904 * syncing routine. When a single log_write region needs to span
1905 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1906 * on all log operation writes which don't contain the end of the
1907 * region. The XLOG_END_TRANS bit is used for the in-core log
1908 * operation which contains the end of the continued log_write region.
1909 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1910 * we don't really know exactly how much space will be used. As a result,
1911 * we don't update ic_offset until the end when we know exactly how many
1912 * bytes have been written out.
1913 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001914int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001915xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001916 struct log *log,
1917 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001918 struct xlog_ticket *ticket,
1919 xfs_lsn_t *start_lsn,
1920 struct xlog_in_core **commit_iclog,
1921 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001923 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001924 struct xfs_log_iovec *vecp;
1925 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001926 int len;
1927 int index;
1928 int partial_copy = 0;
1929 int partial_copy_len = 0;
1930 int contwr = 0;
1931 int record_cnt = 0;
1932 int data_cnt = 0;
1933 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001935 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Dave Chinner55b66332010-03-23 11:43:17 +11001937 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001938
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001939 /*
1940 * Region headers and bytes are already accounted for.
1941 * We only need to take into account start records and
1942 * split regions in this function.
1943 */
1944 if (ticket->t_flags & XLOG_TIC_INITED)
1945 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1946
1947 /*
1948 * Commit record headers need to be accounted for. These
1949 * come in as separate writes so are easy to detect.
1950 */
1951 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1952 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Dave Chinner71e330b2010-05-21 14:37:18 +10001953
1954 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001955 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Dave Chinner55b66332010-03-23 11:43:17 +11001957 index = 0;
1958 lv = log_vector;
1959 vecp = lv->lv_iovecp;
1960 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001961 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001962 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001964 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1965 &contwr, &log_offset);
1966 if (error)
1967 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001969 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001970 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001972 /* start_lsn is the first lsn written to. That's all we need. */
1973 if (!*start_lsn)
1974 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001976 /*
1977 * This loop writes out as many regions as can fit in the amount
1978 * of space which was allocated by xlog_state_get_iclog_space().
1979 */
Dave Chinner55b66332010-03-23 11:43:17 +11001980 while (lv && index < lv->lv_niovecs) {
1981 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001982 struct xlog_op_header *ophdr;
1983 int start_rec_copy;
1984 int copy_len;
1985 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Dave Chinner55b66332010-03-23 11:43:17 +11001987 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001988 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001990 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1991 if (start_rec_copy) {
1992 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001993 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001994 start_rec_copy);
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001997 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1998 if (!ophdr)
1999 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002001 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002002 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002004 len += xlog_write_setup_copy(ticket, ophdr,
2005 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11002006 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002007 &copy_off, &copy_len,
2008 &partial_copy,
2009 &partial_copy_len);
2010 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002011
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002012 /* copy region */
2013 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002014 memcpy(ptr, reg->i_addr + copy_off, copy_len);
2015 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002016
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002017 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
2018 record_cnt++;
2019 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002021 error = xlog_write_copy_finish(log, iclog, flags,
2022 &record_cnt, &data_cnt,
2023 &partial_copy,
2024 &partial_copy_len,
2025 log_offset,
2026 commit_iclog);
2027 if (error)
2028 return error;
2029
2030 /*
2031 * if we had a partial copy, we need to get more iclog
2032 * space but we don't want to increment the region
2033 * index because there is still more is this region to
2034 * write.
2035 *
2036 * If we completed writing this region, and we flushed
2037 * the iclog (indicated by resetting of the record
2038 * count), then we also need to get more log space. If
2039 * this was the last record, though, we are done and
2040 * can just return.
2041 */
2042 if (partial_copy)
2043 break;
2044
Dave Chinner55b66332010-03-23 11:43:17 +11002045 if (++index == lv->lv_niovecs) {
2046 lv = lv->lv_next;
2047 index = 0;
2048 if (lv)
2049 vecp = lv->lv_iovecp;
2050 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002051 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11002052 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002053 return 0;
2054 break;
2055 }
2056 }
2057 }
2058
2059 ASSERT(len == 0);
2060
2061 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2062 if (!commit_iclog)
2063 return xlog_state_release_iclog(log, iclog);
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 ASSERT(flags & XLOG_COMMIT_TRANS);
2066 *commit_iclog = iclog;
2067 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070
2071/*****************************************************************************
2072 *
2073 * State Machine functions
2074 *
2075 *****************************************************************************
2076 */
2077
2078/* Clean iclogs starting from the head. This ordering must be
2079 * maintained, so an iclog doesn't become ACTIVE beyond one that
2080 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002081 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * iclog is trying to sync to disk.
2083 *
2084 * State Change: DIRTY -> ACTIVE
2085 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002086STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087xlog_state_clean_log(xlog_t *log)
2088{
2089 xlog_in_core_t *iclog;
2090 int changed = 0;
2091
2092 iclog = log->l_iclog;
2093 do {
2094 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2095 iclog->ic_state = XLOG_STATE_ACTIVE;
2096 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002097 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
2099 * If the number of ops in this iclog indicate it just
2100 * contains the dummy transaction, we can
2101 * change state into IDLE (the second time around).
2102 * Otherwise we should change the state into
2103 * NEED a dummy.
2104 * We don't need to cover the dummy.
2105 */
2106 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002107 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2108 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 changed = 1;
2110 } else {
2111 /*
2112 * We have two dirty iclogs so start over
2113 * This could also be num of ops indicates
2114 * this is not the dummy going out.
2115 */
2116 changed = 2;
2117 }
2118 iclog->ic_header.h_num_logops = 0;
2119 memset(iclog->ic_header.h_cycle_data, 0,
2120 sizeof(iclog->ic_header.h_cycle_data));
2121 iclog->ic_header.h_lsn = 0;
2122 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2123 /* do nothing */;
2124 else
2125 break; /* stop cleaning */
2126 iclog = iclog->ic_next;
2127 } while (iclog != log->l_iclog);
2128
2129 /* log is locked when we are called */
2130 /*
2131 * Change state for the dummy log recording.
2132 * We usually go to NEED. But we go to NEED2 if the changed indicates
2133 * we are done writing the dummy record.
2134 * If we are done with the second dummy recored (DONE2), then
2135 * we go to IDLE.
2136 */
2137 if (changed) {
2138 switch (log->l_covered_state) {
2139 case XLOG_STATE_COVER_IDLE:
2140 case XLOG_STATE_COVER_NEED:
2141 case XLOG_STATE_COVER_NEED2:
2142 log->l_covered_state = XLOG_STATE_COVER_NEED;
2143 break;
2144
2145 case XLOG_STATE_COVER_DONE:
2146 if (changed == 1)
2147 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2148 else
2149 log->l_covered_state = XLOG_STATE_COVER_NEED;
2150 break;
2151
2152 case XLOG_STATE_COVER_DONE2:
2153 if (changed == 1)
2154 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2155 else
2156 log->l_covered_state = XLOG_STATE_COVER_NEED;
2157 break;
2158
2159 default:
2160 ASSERT(0);
2161 }
2162 }
2163} /* xlog_state_clean_log */
2164
2165STATIC xfs_lsn_t
2166xlog_get_lowest_lsn(
2167 xlog_t *log)
2168{
2169 xlog_in_core_t *lsn_log;
2170 xfs_lsn_t lowest_lsn, lsn;
2171
2172 lsn_log = log->l_iclog;
2173 lowest_lsn = 0;
2174 do {
2175 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002176 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if ((lsn && !lowest_lsn) ||
2178 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2179 lowest_lsn = lsn;
2180 }
2181 }
2182 lsn_log = lsn_log->ic_next;
2183 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002184 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
2187
2188STATIC void
2189xlog_state_do_callback(
2190 xlog_t *log,
2191 int aborted,
2192 xlog_in_core_t *ciclog)
2193{
2194 xlog_in_core_t *iclog;
2195 xlog_in_core_t *first_iclog; /* used to know when we've
2196 * processed all iclogs once */
2197 xfs_log_callback_t *cb, *cb_next;
2198 int flushcnt = 0;
2199 xfs_lsn_t lowest_lsn;
2200 int ioerrors; /* counter: iclogs with errors */
2201 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2202 int funcdidcallbacks; /* flag: function did callbacks */
2203 int repeats; /* for issuing console warnings if
2204 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002205 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002207 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 first_iclog = iclog = log->l_iclog;
2209 ioerrors = 0;
2210 funcdidcallbacks = 0;
2211 repeats = 0;
2212
2213 do {
2214 /*
2215 * Scan all iclogs starting with the one pointed to by the
2216 * log. Reset this starting point each time the log is
2217 * unlocked (during callbacks).
2218 *
2219 * Keep looping through iclogs until one full pass is made
2220 * without running any callbacks.
2221 */
2222 first_iclog = log->l_iclog;
2223 iclog = log->l_iclog;
2224 loopdidcallbacks = 0;
2225 repeats++;
2226
2227 do {
2228
2229 /* skip all iclogs in the ACTIVE & DIRTY states */
2230 if (iclog->ic_state &
2231 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2232 iclog = iclog->ic_next;
2233 continue;
2234 }
2235
2236 /*
2237 * Between marking a filesystem SHUTDOWN and stopping
2238 * the log, we do flush all iclogs to disk (if there
2239 * wasn't a log I/O error). So, we do want things to
2240 * go smoothly in case of just a SHUTDOWN w/o a
2241 * LOG_IO_ERROR.
2242 */
2243 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2244 /*
2245 * Can only perform callbacks in order. Since
2246 * this iclog is not in the DONE_SYNC/
2247 * DO_CALLBACK state, we skip the rest and
2248 * just try to clean up. If we set our iclog
2249 * to DO_CALLBACK, we will not process it when
2250 * we retry since a previous iclog is in the
2251 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002252 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 */
2254 if (!(iclog->ic_state &
2255 (XLOG_STATE_DONE_SYNC |
2256 XLOG_STATE_DO_CALLBACK))) {
2257 if (ciclog && (ciclog->ic_state ==
2258 XLOG_STATE_DONE_SYNC)) {
2259 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2260 }
2261 break;
2262 }
2263 /*
2264 * We now have an iclog that is in either the
2265 * DO_CALLBACK or DONE_SYNC states. The other
2266 * states (WANT_SYNC, SYNCING, or CALLBACK were
2267 * caught by the above if and are going to
2268 * clean (i.e. we aren't doing their callbacks)
2269 * see the above if.
2270 */
2271
2272 /*
2273 * We will do one more check here to see if we
2274 * have chased our tail around.
2275 */
2276
2277 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002278 if (lowest_lsn &&
2279 XFS_LSN_CMP(lowest_lsn,
Dave Chinner84f3c682010-12-03 22:11:29 +11002280 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 iclog = iclog->ic_next;
2282 continue; /* Leave this iclog for
2283 * another thread */
2284 }
2285
2286 iclog->ic_state = XLOG_STATE_CALLBACK;
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Dave Chinner84f3c682010-12-03 22:11:29 +11002289 /*
2290 * update the last_sync_lsn before we drop the
2291 * icloglock to ensure we are the only one that
2292 * can update it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
Dave Chinner84f3c682010-12-03 22:11:29 +11002294 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2295 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2296 atomic64_set(&log->l_last_sync_lsn,
2297 be64_to_cpu(iclog->ic_header.h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Dave Chinner84f3c682010-12-03 22:11:29 +11002299 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 ioerrors++;
Dave Chinner84f3c682010-12-03 22:11:29 +11002301
2302 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
David Chinner114d23a2008-04-10 12:18:39 +10002304 /*
2305 * Keep processing entries in the callback list until
2306 * we come around and it is empty. We need to
2307 * atomically see that the list is empty and change the
2308 * state to DIRTY so that we don't miss any more
2309 * callbacks being added.
2310 */
2311 spin_lock(&iclog->ic_callback_lock);
2312 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002313 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 iclog->ic_callback_tail = &(iclog->ic_callback);
2315 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002316 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002319 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 cb_next = cb->cb_next;
2321 cb->cb_func(cb->cb_arg, aborted);
2322 }
David Chinner114d23a2008-04-10 12:18:39 +10002323 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 cb = iclog->ic_callback;
2325 }
2326
2327 loopdidcallbacks++;
2328 funcdidcallbacks++;
2329
David Chinner114d23a2008-04-10 12:18:39 +10002330 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002331 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002332 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2334 iclog->ic_state = XLOG_STATE_DIRTY;
2335
2336 /*
2337 * Transition from DIRTY to ACTIVE if applicable.
2338 * NOP if STATE_IOERROR.
2339 */
2340 xlog_state_clean_log(log);
2341
2342 /* wake up threads waiting in xfs_log_force() */
Dave Chinnereb40a872010-12-21 12:09:01 +11002343 wake_up_all(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 iclog = iclog->ic_next;
2346 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002347
2348 if (repeats > 5000) {
2349 flushcnt += repeats;
2350 repeats = 0;
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002351 xfs_warn(log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002352 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002353 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355 } while (!ioerrors && loopdidcallbacks);
2356
2357 /*
2358 * make one last gasp attempt to see if iclogs are being left in
2359 * limbo..
2360 */
2361#ifdef DEBUG
2362 if (funcdidcallbacks) {
2363 first_iclog = iclog = log->l_iclog;
2364 do {
2365 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2366 /*
2367 * Terminate the loop if iclogs are found in states
2368 * which will cause other threads to clean up iclogs.
2369 *
2370 * SYNCING - i/o completion will go through logs
2371 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002372 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 * IOERROR - give up hope all ye who enter here
2374 */
2375 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2376 iclog->ic_state == XLOG_STATE_SYNCING ||
2377 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2378 iclog->ic_state == XLOG_STATE_IOERROR )
2379 break;
2380 iclog = iclog->ic_next;
2381 } while (first_iclog != iclog);
2382 }
2383#endif
2384
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002385 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2386 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002387 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002388
2389 if (wake)
Dave Chinnereb40a872010-12-21 12:09:01 +11002390 wake_up_all(&log->l_flush_wait);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393
2394/*
2395 * Finish transitioning this iclog to the dirty state.
2396 *
2397 * Make sure that we completely execute this routine only when this is
2398 * the last call to the iclog. There is a good chance that iclog flushes,
2399 * when we reach the end of the physical log, get turned into 2 separate
2400 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2401 * routine. By using the reference count bwritecnt, we guarantee that only
2402 * the second completion goes through.
2403 *
2404 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002405 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 */
David Chinnera8272ce2007-11-23 16:28:09 +11002407STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408xlog_state_done_syncing(
2409 xlog_in_core_t *iclog,
2410 int aborted)
2411{
2412 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002414 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2417 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002418 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2420
2421
2422 /*
2423 * If we got an error, either on the first buffer, or in the case of
2424 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2425 * and none should ever be attempted to be written to disk
2426 * again.
2427 */
2428 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2429 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002430 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return;
2432 }
2433 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2434 }
2435
2436 /*
2437 * Someone could be sleeping prior to writing out the next
2438 * iclog buffer, we wake them all, one will get to do the
2439 * I/O, the others get to wait for the result.
2440 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002441 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002442 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2444} /* xlog_state_done_syncing */
2445
2446
2447/*
2448 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002449 * sleep. We wait on the flush queue on the head iclog as that should be
2450 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2451 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 *
2453 * The in-core logs are used in a circular fashion. They are not used
2454 * out-of-order even when an iclog past the head is free.
2455 *
2456 * return:
2457 * * log_offset where xlog_write() can start writing into the in-core
2458 * log's data space.
2459 * * in-core log pointer to which xlog_write() should write.
2460 * * boolean indicating this is a continued write to an in-core log.
2461 * If this is the last write, then the in-core log's offset field
2462 * needs to be incremented, depending on the amount of data which
2463 * is copied.
2464 */
David Chinnera8272ce2007-11-23 16:28:09 +11002465STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466xlog_state_get_iclog_space(xlog_t *log,
2467 int len,
2468 xlog_in_core_t **iclogp,
2469 xlog_ticket_t *ticket,
2470 int *continued_write,
2471 int *logoffsetp)
2472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 int log_offset;
2474 xlog_rec_header_t *head;
2475 xlog_in_core_t *iclog;
2476 int error;
2477
2478restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002479 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002481 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return XFS_ERROR(EIO);
2483 }
2484
2485 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002486 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002488
2489 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002490 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto restart;
2492 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 head = &iclog->ic_header;
2495
David Chinner155cc6b2008-03-06 13:44:14 +11002496 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 log_offset = iclog->ic_offset;
2498
2499 /* On the 1st write to an iclog, figure out lsn. This works
2500 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2501 * committing to. If the offset is set, that's how many blocks
2502 * must be written.
2503 */
2504 if (log_offset == 0) {
2505 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002506 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002507 log->l_iclog_hsize,
2508 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002509 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2510 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002511 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 ASSERT(log->l_curr_block >= 0);
2513 }
2514
2515 /* If there is enough room to write everything, then do it. Otherwise,
2516 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2517 * bit is on, so this will get flushed out. Don't update ic_offset
2518 * until you know exactly how many bytes get copied. Therefore, wait
2519 * until later to update ic_offset.
2520 *
2521 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2522 * can fit into remaining data section.
2523 */
2524 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2525 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2526
Dave Chinner49641f12008-07-11 17:43:55 +10002527 /*
2528 * If I'm the only one writing to this iclog, sync it to disk.
2529 * We need to do an atomic compare and decrement here to avoid
2530 * racing with concurrent atomic_dec_and_lock() calls in
2531 * xlog_state_release_iclog() when there is more than one
2532 * reference to the iclog.
2533 */
2534 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2535 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002536 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002537 error = xlog_state_release_iclog(log, iclog);
2538 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002539 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002541 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543 goto restart;
2544 }
2545
2546 /* Do we have enough room to write the full amount in the remainder
2547 * of this iclog? Or must we continue a write on the next iclog and
2548 * mark this iclog as completely taken? In the case where we switch
2549 * iclogs (to mark it taken), this particular iclog will release/sync
2550 * to disk in xlog_write().
2551 */
2552 if (len <= iclog->ic_size - iclog->ic_offset) {
2553 *continued_write = 0;
2554 iclog->ic_offset += len;
2555 } else {
2556 *continued_write = 1;
2557 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2558 }
2559 *iclogp = iclog;
2560
2561 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002562 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 *logoffsetp = log_offset;
2565 return 0;
2566} /* xlog_state_get_iclog_space */
2567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002569xlog_grant_log_space(
2570 struct log *log,
2571 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
Christoph Hellwig42ceedb2012-02-20 02:31:30 +00002573 int need_bytes;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002574 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002576 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Christoph Hellwig42ceedb2012-02-20 02:31:30 +00002578 error = xlog_grant_head_check(log, &log->l_reserve_head, tic,
2579 &need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002580 if (error)
2581 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Christoph Hellwig28496962012-02-20 02:31:25 +00002583 xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
2584 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002585 trace_xfs_log_grant_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002586 xlog_verify_grant_tail(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002588}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590/*
2591 * Replenish the byte reservation required by moving the grant write head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
2593STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002594xlog_regrant_write_log_space(
2595 struct log *log,
2596 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Christoph Hellwig42ceedb2012-02-20 02:31:30 +00002598 int need_bytes;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002599 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002602 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002607 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Christoph Hellwig42ceedb2012-02-20 02:31:30 +00002609 error = xlog_grant_head_check(log, &log->l_write_head, tic,
2610 &need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002611 if (error)
2612 return error;
2613
Christoph Hellwig28496962012-02-20 02:31:25 +00002614 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002615 trace_xfs_log_regrant_write_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002616 xlog_verify_grant_tail(log);
Jesper Juhl014c2542006-01-15 02:37:08 +01002617 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002618}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620/* The first cnt-1 times through here we don't need to
2621 * move the grant write head because the permanent
2622 * reservation has reserved cnt times the unit amount.
2623 * Release part of current permanent unit reservation and
2624 * reset current reservation to be one units worth. Also
2625 * move grant reservation head forward.
2626 */
2627STATIC void
2628xlog_regrant_reserve_log_space(xlog_t *log,
2629 xlog_ticket_t *ticket)
2630{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002631 trace_xfs_log_regrant_reserve_enter(log, ticket);
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (ticket->t_cnt > 0)
2634 ticket->t_cnt--;
2635
Christoph Hellwig28496962012-02-20 02:31:25 +00002636 xlog_grant_sub_space(log, &log->l_reserve_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002637 ticket->t_curr_res);
Christoph Hellwig28496962012-02-20 02:31:25 +00002638 xlog_grant_sub_space(log, &log->l_write_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002639 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002641 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002642
2643 trace_xfs_log_regrant_reserve_sub(log, ticket);
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /* just return if we still have some of the pre-reserved space */
Dave Chinnerd0eb2f32010-12-21 12:29:14 +11002646 if (ticket->t_cnt > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Christoph Hellwig28496962012-02-20 02:31:25 +00002649 xlog_grant_add_space(log, &log->l_reserve_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002650 ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002651
2652 trace_xfs_log_regrant_reserve_exit(log, ticket);
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002655 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656} /* xlog_regrant_reserve_log_space */
2657
2658
2659/*
2660 * Give back the space left from a reservation.
2661 *
2662 * All the information we need to make a correct determination of space left
2663 * is present. For non-permanent reservations, things are quite easy. The
2664 * count should have been decremented to zero. We only need to deal with the
2665 * space remaining in the current reservation part of the ticket. If the
2666 * ticket contains a permanent reservation, there may be left over space which
2667 * needs to be released. A count of N means that N-1 refills of the current
2668 * reservation can be done before we need to ask for more space. The first
2669 * one goes to fill up the first current reservation. Once we run out of
2670 * space, the count will stay at zero and the only space remaining will be
2671 * in the current reservation field.
2672 */
2673STATIC void
2674xlog_ungrant_log_space(xlog_t *log,
2675 xlog_ticket_t *ticket)
2676{
Dave Chinner663e4962010-12-21 12:06:05 +11002677 int bytes;
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (ticket->t_cnt > 0)
2680 ticket->t_cnt--;
2681
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002682 trace_xfs_log_ungrant_enter(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002683 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Dave Chinner663e4962010-12-21 12:06:05 +11002685 /*
2686 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 * up more space based on the remaining count.
2688 */
Dave Chinner663e4962010-12-21 12:06:05 +11002689 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 if (ticket->t_cnt > 0) {
2691 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11002692 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694
Christoph Hellwig28496962012-02-20 02:31:25 +00002695 xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
2696 xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11002697
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002698 trace_xfs_log_ungrant_exit(log, ticket);
2699
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +00002700 xfs_log_space_wake(log->l_mp);
Christoph Hellwig09a423a2012-02-20 02:31:20 +00002701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 * Flush iclog to disk if this is the last reference to the given iclog and
2705 * the WANT_SYNC bit is set.
2706 *
2707 * When this function is entered, the iclog is not necessarily in the
2708 * WANT_SYNC state. It may be sitting around waiting to get filled.
2709 *
2710 *
2711 */
David Chinnera8272ce2007-11-23 16:28:09 +11002712STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002713xlog_state_release_iclog(
2714 xlog_t *log,
2715 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 int sync = 0; /* do we sync? */
2718
David Chinner155cc6b2008-03-06 13:44:14 +11002719 if (iclog->ic_state & XLOG_STATE_IOERROR)
2720 return XFS_ERROR(EIO);
2721
2722 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2723 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2724 return 0;
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002727 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return XFS_ERROR(EIO);
2729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2731 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2732
David Chinner155cc6b2008-03-06 13:44:14 +11002733 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002734 /* update tail before writing to iclog */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002735 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 sync++;
2737 iclog->ic_state = XLOG_STATE_SYNCING;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002738 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
2739 xlog_verify_tail_lsn(log, iclog, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 /* cycle incremented when incrementing curr_block */
2741 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002742 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 /*
2745 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002746 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2748 * this iclog has consistent data, so we ignore IOERROR
2749 * flags after this point.
2750 */
David Chinnerb5893342008-03-06 13:44:06 +11002751 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754} /* xlog_state_release_iclog */
2755
2756
2757/*
2758 * This routine will mark the current iclog in the ring as WANT_SYNC
2759 * and move the current iclog pointer to the next iclog in the ring.
2760 * When this routine is called from xlog_state_get_iclog_space(), the
2761 * exact size of the iclog has not yet been determined. All we know is
2762 * that every data block. We have run out of space in this log record.
2763 */
2764STATIC void
2765xlog_state_switch_iclogs(xlog_t *log,
2766 xlog_in_core_t *iclog,
2767 int eventual_size)
2768{
2769 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2770 if (!eventual_size)
2771 eventual_size = iclog->ic_offset;
2772 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002773 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 log->l_prev_block = log->l_curr_block;
2775 log->l_prev_cycle = log->l_curr_cycle;
2776
2777 /* roll log?: ic_offset changed later */
2778 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2779
2780 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002781 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 log->l_mp->m_sb.sb_logsunit > 1) {
2783 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2784 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2785 }
2786
2787 if (log->l_curr_block >= log->l_logBBsize) {
2788 log->l_curr_cycle++;
2789 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2790 log->l_curr_cycle++;
2791 log->l_curr_block -= log->l_logBBsize;
2792 ASSERT(log->l_curr_block >= 0);
2793 }
2794 ASSERT(iclog == log->l_iclog);
2795 log->l_iclog = iclog->ic_next;
2796} /* xlog_state_switch_iclogs */
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798/*
2799 * Write out all data in the in-core log as of this exact moment in time.
2800 *
2801 * Data may be written to the in-core log during this call. However,
2802 * we don't guarantee this data will be written out. A change from past
2803 * implementation means this routine will *not* write out zero length LRs.
2804 *
2805 * Basically, we try and perform an intelligent scan of the in-core logs.
2806 * If we determine there is no flushable data, we just return. There is no
2807 * flushable data if:
2808 *
2809 * 1. the current iclog is active and has no data; the previous iclog
2810 * is in the active or dirty state.
2811 * 2. the current iclog is drity, and the previous iclog is in the
2812 * active or dirty state.
2813 *
David Chinner12017fa2008-08-13 16:34:31 +10002814 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 *
2816 * 1. the current iclog is not in the active nor dirty state.
2817 * 2. the current iclog dirty, and the previous iclog is not in the
2818 * active nor dirty state.
2819 * 3. the current iclog is active, and there is another thread writing
2820 * to this particular iclog.
2821 * 4. a) the current iclog is active and has no other writers
2822 * b) when we return from flushing out this iclog, it is still
2823 * not in the active nor dirty state.
2824 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002825int
2826_xfs_log_force(
2827 struct xfs_mount *mp,
2828 uint flags,
2829 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002831 struct log *log = mp->m_log;
2832 struct xlog_in_core *iclog;
2833 xfs_lsn_t lsn;
2834
2835 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002837 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002838
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002839 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 iclog = log->l_iclog;
2842 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002843 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return XFS_ERROR(EIO);
2845 }
2846
2847 /* If the head iclog is not active nor dirty, we just attach
2848 * ourselves to the head and go to sleep.
2849 */
2850 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2851 iclog->ic_state == XLOG_STATE_DIRTY) {
2852 /*
2853 * If the head is dirty or (active and empty), then
2854 * we need to look at the previous iclog. If the previous
2855 * iclog is active or dirty we are done. There is nothing
2856 * to sync out. Otherwise, we attach ourselves to the
2857 * previous iclog and go to sleep.
2858 */
2859 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002860 (atomic_read(&iclog->ic_refcnt) == 0
2861 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 iclog = iclog->ic_prev;
2863 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2864 iclog->ic_state == XLOG_STATE_DIRTY)
2865 goto no_sleep;
2866 else
2867 goto maybe_sleep;
2868 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002869 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /* We are the only one with access to this
2871 * iclog. Flush it out now. There should
2872 * be a roundoff of zero to show that someone
2873 * has already taken care of the roundoff from
2874 * the previous sync.
2875 */
David Chinner155cc6b2008-03-06 13:44:14 +11002876 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002877 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002879 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 if (xlog_state_release_iclog(log, iclog))
2882 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002883
2884 if (log_flushed)
2885 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002886 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002887 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 iclog->ic_state != XLOG_STATE_DIRTY)
2889 goto maybe_sleep;
2890 else
2891 goto no_sleep;
2892 } else {
2893 /* Someone else is writing to this iclog.
2894 * Use its call to flush out the data. However,
2895 * the other thread may not force out this LR,
2896 * so we mark it WANT_SYNC.
2897 */
2898 xlog_state_switch_iclogs(log, iclog, 0);
2899 goto maybe_sleep;
2900 }
2901 }
2902 }
2903
2904 /* By the time we come around again, the iclog could've been filled
2905 * which would give it another lsn. If we have a new lsn, just
2906 * return because the relevant data has been flushed.
2907 */
2908maybe_sleep:
2909 if (flags & XFS_LOG_SYNC) {
2910 /*
2911 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002912 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 * Then we check again after waking up, in case our
2914 * sleep was disturbed by a bad news.
2915 */
2916 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002917 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return XFS_ERROR(EIO);
2919 }
2920 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11002921 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /*
2923 * No need to grab the log lock here since we're
2924 * only deciding whether or not to return EIO
2925 * and the memory read should be atomic.
2926 */
2927 if (iclog->ic_state & XLOG_STATE_IOERROR)
2928 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002929 if (log_flushed)
2930 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 } else {
2932
2933no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002934 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
2936 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002937}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002940 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2941 * about errors or whether the log was flushed or not. This is the normal
2942 * interface to use when trying to unpin items or move the log forward.
2943 */
2944void
2945xfs_log_force(
2946 xfs_mount_t *mp,
2947 uint flags)
2948{
2949 int error;
2950
2951 error = _xfs_log_force(mp, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002952 if (error)
2953 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002954}
2955
2956/*
2957 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 *
2959 * Find in-core log with lsn.
2960 * If it is in the DIRTY state, just return.
2961 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2962 * state and go to sleep or return.
2963 * If it is in any other state, go to sleep or return.
2964 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002965 * Synchronous forces are implemented with a signal variable. All callers
2966 * to force a given lsn to disk will wait on a the sv attached to the
2967 * specific in-core log. When given in-core log finally completes its
2968 * write to disk, that thread will wake up all threads waiting on the
2969 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002971int
2972_xfs_log_force_lsn(
2973 struct xfs_mount *mp,
2974 xfs_lsn_t lsn,
2975 uint flags,
2976 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002978 struct log *log = mp->m_log;
2979 struct xlog_in_core *iclog;
2980 int already_slept = 0;
2981
2982 ASSERT(lsn != 0);
2983
2984 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002986 lsn = xlog_cil_force_lsn(log, lsn);
2987 if (lsn == NULLCOMMITLSN)
2988 return 0;
Dave Chinner71e330b2010-05-21 14:37:18 +10002989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002991 spin_lock(&log->l_icloglock);
2992 iclog = log->l_iclog;
2993 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002994 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002995 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
2997
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002998 do {
2999 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3000 iclog = iclog->ic_next;
3001 continue;
3002 }
3003
3004 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3005 spin_unlock(&log->l_icloglock);
3006 return 0;
3007 }
3008
3009 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3010 /*
3011 * We sleep here if we haven't already slept (e.g.
3012 * this is the first time we've looked at the correct
3013 * iclog buf) and the buffer before us is going to
3014 * be sync'ed. The reason for this is that if we
3015 * are doing sync transactions here, by waiting for
3016 * the previous I/O to complete, we can allow a few
3017 * more transactions into this iclog before we close
3018 * it down.
3019 *
3020 * Otherwise, we mark the buffer WANT_SYNC, and bump
3021 * up the refcnt so we can release the log (which
3022 * drops the ref count). The state switch keeps new
3023 * transaction commits from using this buffer. When
3024 * the current commits finish writing into the buffer,
3025 * the refcount will drop to zero and the buffer will
3026 * go out then.
3027 */
3028 if (!already_slept &&
3029 (iclog->ic_prev->ic_state &
3030 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3031 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3032
3033 XFS_STATS_INC(xs_log_force_sleep);
3034
Dave Chinnereb40a872010-12-21 12:09:01 +11003035 xlog_wait(&iclog->ic_prev->ic_write_wait,
3036 &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003037 if (log_flushed)
3038 *log_flushed = 1;
3039 already_slept = 1;
3040 goto try_again;
3041 }
David Chinner155cc6b2008-03-06 13:44:14 +11003042 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003044 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (xlog_state_release_iclog(log, iclog))
3046 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003047 if (log_flushed)
3048 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003049 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003052 if ((flags & XFS_LOG_SYNC) && /* sleep */
3053 !(iclog->ic_state &
3054 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3055 /*
3056 * Don't wait on completion if we know that we've
3057 * gotten a log write error.
3058 */
3059 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3060 spin_unlock(&log->l_icloglock);
3061 return XFS_ERROR(EIO);
3062 }
3063 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003064 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003065 /*
3066 * No need to grab the log lock here since we're
3067 * only deciding whether or not to return EIO
3068 * and the memory read should be atomic.
3069 */
3070 if (iclog->ic_state & XLOG_STATE_IOERROR)
3071 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003073 if (log_flushed)
3074 *log_flushed = 1;
3075 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003076 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003078
3079 return 0;
3080 } while (iclog != log->l_iclog);
3081
3082 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003086/*
3087 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3088 * about errors or whether the log was flushed or not. This is the normal
3089 * interface to use when trying to unpin items or move the log forward.
3090 */
3091void
3092xfs_log_force_lsn(
3093 xfs_mount_t *mp,
3094 xfs_lsn_t lsn,
3095 uint flags)
3096{
3097 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003099 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003100 if (error)
3101 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104/*
3105 * Called when we want to mark the current iclog as being ready to sync to
3106 * disk.
3107 */
David Chinnera8272ce2007-11-23 16:28:09 +11003108STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3110{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003111 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3114 xlog_state_switch_iclogs(log, iclog, 0);
3115 } else {
3116 ASSERT(iclog->ic_state &
3117 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3118 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121
3122/*****************************************************************************
3123 *
3124 * TICKET functions
3125 *
3126 *****************************************************************************
3127 */
3128
3129/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003130 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003132void
3133xfs_log_ticket_put(
3134 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003136 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003137 if (atomic_dec_and_test(&ticket->t_ref))
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003138 kmem_zone_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003141xlog_ticket_t *
3142xfs_log_ticket_get(
3143 xlog_ticket_t *ticket)
3144{
3145 ASSERT(atomic_read(&ticket->t_ref) > 0);
3146 atomic_inc(&ticket->t_ref);
3147 return ticket;
3148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003151 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003153xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003154xlog_ticket_alloc(
3155 struct log *log,
3156 int unit_bytes,
3157 int cnt,
3158 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003159 uint xflags,
3160 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003162 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003164 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Dave Chinner3383ca52010-05-07 11:04:17 +10003166 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003167 if (!tic)
3168 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 /*
3171 * Permanent reservations have up to 'cnt'-1 active log operations
3172 * in the log. A unit in this case is the amount of space for one
3173 * of these log operations. Normal reservations have a cnt of 1
3174 * and their unit amount is the total amount of space required.
3175 *
3176 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003177 * which occupy space in the on-disk log.
3178 *
3179 * Normal form of a transaction is:
3180 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3181 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3182 *
3183 * We need to account for all the leadup data and trailer data
3184 * around the transaction data.
3185 * And then we need to account for the worst case in terms of using
3186 * more space.
3187 * The worst case will happen if:
3188 * - the placement of the transaction happens to be such that the
3189 * roundoff is at its maximum
3190 * - the transaction data is synced before the commit record is synced
3191 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3192 * Therefore the commit record is in its own Log Record.
3193 * This can happen as the commit record is called with its
3194 * own region to xlog_write().
3195 * This then means that in the worst case, roundoff can happen for
3196 * the commit-rec as well.
3197 * The commit-rec is smaller than padding in this scenario and so it is
3198 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 */
3200
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003201 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003203 unit_bytes += sizeof(xfs_trans_header_t);
3204
3205 /* for start-rec */
3206 unit_bytes += sizeof(xlog_op_header_t);
3207
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003208 /*
3209 * for LR headers - the space for data in an iclog is the size minus
3210 * the space used for the headers. If we use the iclog size, then we
3211 * undercalculate the number of headers required.
3212 *
3213 * Furthermore - the addition of op headers for split-recs might
3214 * increase the space required enough to require more log and op
3215 * headers, so take that into account too.
3216 *
3217 * IMPORTANT: This reservation makes the assumption that if this
3218 * transaction is the first in an iclog and hence has the LR headers
3219 * accounted to it, then the remaining space in the iclog is
3220 * exclusively for this transaction. i.e. if the transaction is larger
3221 * than the iclog, it will be the only thing in that iclog.
3222 * Fundamentally, this means we must pass the entire log vector to
3223 * xlog_write to guarantee this.
3224 */
3225 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3226 num_headers = howmany(unit_bytes, iclog_space);
3227
3228 /* for split-recs - ophdrs added when data split over LRs */
3229 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3230
3231 /* add extra header reservations if we overrun */
3232 while (!num_headers ||
3233 howmany(unit_bytes, iclog_space) > num_headers) {
3234 unit_bytes += sizeof(xlog_op_header_t);
3235 num_headers++;
3236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 unit_bytes += log->l_iclog_hsize * num_headers;
3238
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003239 /* for commit-rec LR header - note: padding will subsume the ophdr */
3240 unit_bytes += log->l_iclog_hsize;
3241
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003242 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003243 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003244 log->l_mp->m_sb.sb_logsunit > 1) {
3245 /* log su roundoff */
3246 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3247 } else {
3248 /* BB roundoff */
3249 unit_bytes += 2*BBSIZE;
3250 }
3251
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003252 atomic_set(&tic->t_ref, 1);
Christoph Hellwig14a72352012-02-20 02:31:24 +00003253 tic->t_task = current;
Dave Chinner10547942010-12-21 12:02:25 +11003254 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 tic->t_unit_res = unit_bytes;
3256 tic->t_curr_res = unit_bytes;
3257 tic->t_cnt = cnt;
3258 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003259 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 tic->t_clientid = client;
3261 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003262 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (xflags & XFS_LOG_PERM_RESERV)
3264 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Christoph Hellwig0adba532007-08-30 17:21:46 +10003266 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271
3272/******************************************************************************
3273 *
3274 * Log debug routines
3275 *
3276 ******************************************************************************
3277 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003278#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279/*
3280 * Make sure that the destination ptr is within the valid data region of
3281 * one of the iclogs. This uses backup pointers stored in a different
3282 * part of the log in case we trash the log structure.
3283 */
3284void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003285xlog_verify_dest_ptr(
3286 struct log *log,
3287 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
3289 int i;
3290 int good_ptr = 0;
3291
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003292 for (i = 0; i < log->l_iclog_bufs; i++) {
3293 if (ptr >= log->l_iclog_bak[i] &&
3294 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 good_ptr++;
3296 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003297
3298 if (!good_ptr)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003299 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003302/*
3303 * Check to make sure the grant write head didn't just over lap the tail. If
3304 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3305 * the cycles differ by exactly one and check the byte count.
3306 *
3307 * This check is run unlocked, so can give false positives. Rather than assert
3308 * on failures, use a warn-once flag and a panic tag to allow the admin to
3309 * determine if they want to panic the machine when such an error occurs. For
3310 * debug kernels this will have the same effect as using an assert but, unlinke
3311 * an assert, it can be turned off at runtime.
3312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313STATIC void
Dave Chinner3f336c62010-12-21 12:02:52 +11003314xlog_verify_grant_tail(
3315 struct log *log)
3316{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003317 int tail_cycle, tail_blocks;
Dave Chinnera69ed032010-12-21 12:08:20 +11003318 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003319
Christoph Hellwig28496962012-02-20 02:31:25 +00003320 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003321 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3322 if (tail_cycle != cycle) {
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003323 if (cycle - 1 != tail_cycle &&
3324 !(log->l_flags & XLOG_TAIL_WARN)) {
3325 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3326 "%s: cycle - 1 != tail_cycle", __func__);
3327 log->l_flags |= XLOG_TAIL_WARN;
3328 }
3329
3330 if (space > BBTOB(tail_blocks) &&
3331 !(log->l_flags & XLOG_TAIL_WARN)) {
3332 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3333 "%s: space > BBTOB(tail_blocks)", __func__);
3334 log->l_flags |= XLOG_TAIL_WARN;
3335 }
Dave Chinner3f336c62010-12-21 12:02:52 +11003336 }
3337}
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339/* check if it will fit */
3340STATIC void
3341xlog_verify_tail_lsn(xlog_t *log,
3342 xlog_in_core_t *iclog,
3343 xfs_lsn_t tail_lsn)
3344{
3345 int blocks;
3346
3347 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3348 blocks =
3349 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3350 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003351 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 } else {
3353 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3354
3355 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003356 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3359 if (blocks < BTOBB(iclog->ic_offset) + 1)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003360 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
3362} /* xlog_verify_tail_lsn */
3363
3364/*
3365 * Perform a number of checks on the iclog before writing to disk.
3366 *
3367 * 1. Make sure the iclogs are still circular
3368 * 2. Make sure we have a good magic number
3369 * 3. Make sure we don't have magic numbers in the data
3370 * 4. Check fields of each log operation header for:
3371 * A. Valid client identifier
3372 * B. tid ptr value falls in valid ptr space (user space code)
3373 * C. Length in log record header is correct according to the
3374 * individual operation headers within record.
3375 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3376 * log, check the preceding blocks of the physical log to make sure all
3377 * the cycle numbers agree with the current cycle number.
3378 */
3379STATIC void
3380xlog_verify_iclog(xlog_t *log,
3381 xlog_in_core_t *iclog,
3382 int count,
3383 boolean_t syncing)
3384{
3385 xlog_op_header_t *ophead;
3386 xlog_in_core_t *icptr;
3387 xlog_in_core_2_t *xhdr;
3388 xfs_caddr_t ptr;
3389 xfs_caddr_t base_ptr;
3390 __psint_t field_offset;
3391 __uint8_t clientid;
3392 int len, i, j, k, op_len;
3393 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003396 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 icptr = log->l_iclog;
3398 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003399 if (icptr == NULL)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003400 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 icptr = icptr->ic_next;
3402 }
3403 if (icptr != log->l_iclog)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003404 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003405 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407 /* check log magic numbers */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003408 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003409 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003411 ptr = (xfs_caddr_t) &iclog->ic_header;
3412 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 ptr += BBSIZE) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003414 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003415 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3416 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
3418
3419 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003420 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 ptr = iclog->ic_datap;
3422 base_ptr = ptr;
3423 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003424 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 for (i = 0; i < len; i++) {
3426 ophead = (xlog_op_header_t *)ptr;
3427
3428 /* clientid is only 1 byte */
3429 field_offset = (__psint_t)
3430 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3431 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3432 clientid = ophead->oh_clientid;
3433 } else {
3434 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3435 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3436 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3437 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003438 clientid = xlog_get_client_id(
3439 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003441 clientid = xlog_get_client_id(
3442 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444 }
3445 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003446 xfs_warn(log->l_mp,
3447 "%s: invalid clientid %d op 0x%p offset 0x%lx",
3448 __func__, clientid, ophead,
3449 (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 /* check length */
3452 field_offset = (__psint_t)
3453 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3454 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003455 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 } else {
3457 idx = BTOBBT((__psint_t)&ophead->oh_len -
3458 (__psint_t)iclog->ic_datap);
3459 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3460 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3461 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003462 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003464 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
3466 }
3467 ptr += sizeof(xlog_op_header_t) + op_len;
3468 }
3469} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003473 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 */
3475STATIC int
3476xlog_state_ioerror(
3477 xlog_t *log)
3478{
3479 xlog_in_core_t *iclog, *ic;
3480
3481 iclog = log->l_iclog;
3482 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3483 /*
3484 * Mark all the incore logs IOERROR.
3485 * From now on, no log flushes will result.
3486 */
3487 ic = iclog;
3488 do {
3489 ic->ic_state = XLOG_STATE_IOERROR;
3490 ic = ic->ic_next;
3491 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494 /*
3495 * Return non-zero, if state transition has already happened.
3496 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003497 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
3500/*
3501 * This is called from xfs_force_shutdown, when we're forcibly
3502 * shutting down the filesystem, typically because of an IO error.
3503 * Our main objectives here are to make sure that:
3504 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3505 * parties to find out, 'atomically'.
3506 * b. those who're sleeping on log reservations, pinned objects and
3507 * other resources get woken up, and be told the bad news.
3508 * c. nothing new gets queued up after (a) and (b) are done.
3509 * d. if !logerror, flush the iclogs to disk, then seal them off
3510 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003511 *
3512 * Note: for delayed logging the !logerror case needs to flush the regions
3513 * held in memory out to the iclogs before flushing them to disk. This needs
3514 * to be done before the log is marked as shutdown, otherwise the flush to the
3515 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 */
3517int
3518xfs_log_force_umount(
3519 struct xfs_mount *mp,
3520 int logerror)
3521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 xlog_t *log;
3523 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 log = mp->m_log;
3526
3527 /*
3528 * If this happens during log recovery, don't worry about
3529 * locking; the log isn't open for business yet.
3530 */
3531 if (!log ||
3532 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3533 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003534 if (mp->m_sb_bp)
3535 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
3538
3539 /*
3540 * Somebody could've already done the hard work for us.
3541 * No need to get locks for this.
3542 */
3543 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3544 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003545 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
3547 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003548
3549 /*
3550 * Flush the in memory commit item list before marking the log as
3551 * being shut down. We need to do it in this order to ensure all the
3552 * completed transactions are flushed to disk with the xfs_log_force()
3553 * call below.
3554 */
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003555 if (!logerror)
Dave Chinnera44f13e2010-08-24 11:40:03 +10003556 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003557
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 /*
Dave Chinner3f16b982010-12-21 12:29:01 +11003559 * mark the filesystem and the as in a shutdown state and wake
3560 * everybody up to tell them the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003562 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003564 if (mp->m_sb_bp)
3565 XFS_BUF_DONE(mp->m_sb_bp);
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 /*
3568 * This flag is sort of redundant because of the mount flag, but
3569 * it's good to maintain the separation between the log and the rest
3570 * of XFS.
3571 */
3572 log->l_flags |= XLOG_IO_ERROR;
3573
3574 /*
3575 * If we hit a log error, we want to mark all the iclogs IOERROR
3576 * while we're still holding the loglock.
3577 */
3578 if (logerror)
3579 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003580 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
3582 /*
Dave Chinner10547942010-12-21 12:02:25 +11003583 * We don't want anybody waiting for log reservations after this. That
3584 * means we have to wake up everybody queued up on reserveq as well as
3585 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3586 * we don't enqueue anything once the SHUTDOWN flag is set, and this
Dave Chinner3f16b982010-12-21 12:29:01 +11003587 * action is protected by the grant locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 */
Christoph Hellwiga79bf2d2012-02-20 02:31:27 +00003589 xlog_grant_head_wake_all(&log->l_reserve_head);
3590 xlog_grant_head_wake_all(&log->l_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003592 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 ASSERT(!logerror);
3594 /*
3595 * Force the incore logs to disk before shutting the
3596 * log down completely.
3597 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003598 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3599
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003600 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003602 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
3604 /*
3605 * Wake up everybody waiting on xfs_log_force.
3606 * Callback all log item committed functions as if the
3607 * log writes were completed.
3608 */
3609 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3610
3611#ifdef XFSERRORDEBUG
3612 {
3613 xlog_in_core_t *iclog;
3614
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003615 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 iclog = log->l_iclog;
3617 do {
3618 ASSERT(iclog->ic_callback == 0);
3619 iclog = iclog->ic_next;
3620 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003621 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
3623#endif
3624 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003625 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003628STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629xlog_iclogs_empty(xlog_t *log)
3630{
3631 xlog_in_core_t *iclog;
3632
3633 iclog = log->l_iclog;
3634 do {
3635 /* endianness does not matter here, zero is zero in
3636 * any language.
3637 */
3638 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 iclog = iclog->ic_next;
3641 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003642 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}