blob: 372642d3987205ab79ec3c65792a8f69686002d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
28#include "xfs_error.h"
29#include "xfs_log_priv.h"
30#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Chinnereb01c9c2008-04-10 12:18:46 +100041kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Local miscellaneous function prototypes */
Dave Chinner55b66332010-03-23 11:43:17 +110044STATIC int xlog_commit_record(struct log *log, struct xlog_ticket *ticket,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 xlog_in_core_t **, xfs_lsn_t *);
46STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
47 xfs_buftarg_t *log_target,
48 xfs_daddr_t blk_offset,
49 int num_bblks);
Dave Chinnerc8a09ff2010-12-04 00:02:40 +110050STATIC int xlog_space_left(struct log *log, atomic64_t *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100052STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* local state machine functions */
55STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
56STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
57STATIC int xlog_state_get_iclog_space(xlog_t *log,
58 int len,
59 xlog_in_core_t **iclog,
60 xlog_ticket_t *ticket,
61 int *continued_write,
62 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xlog_state_release_iclog(xlog_t *log,
64 xlog_in_core_t *iclog);
65STATIC void xlog_state_switch_iclogs(xlog_t *log,
66 xlog_in_core_t *iclog,
67 int eventual_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
69
70/* local functions to manipulate grant head */
71STATIC int xlog_grant_log_space(xlog_t *log,
72 xlog_ticket_t *xtic);
Dave Chinner2ced19c2010-12-21 12:09:20 +110073STATIC void xlog_grant_push_ail(struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int need_bytes);
75STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
76 xlog_ticket_t *ticket);
77STATIC int xlog_regrant_write_log_space(xlog_t *log,
78 xlog_ticket_t *ticket);
79STATIC void xlog_ungrant_log_space(xlog_t *log,
80 xlog_ticket_t *ticket);
81
Nathan Scottcfcbbbd2005-11-02 15:12:04 +110082#if defined(DEBUG)
Christoph Hellwige6b1f272010-03-23 11:47:38 +110083STATIC void xlog_verify_dest_ptr(xlog_t *log, char *ptr);
Dave Chinner3f336c62010-12-21 12:02:52 +110084STATIC void xlog_verify_grant_tail(struct log *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
86 int count, boolean_t syncing);
87STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
88 xfs_lsn_t tail_lsn);
89#else
90#define xlog_verify_dest_ptr(a,b)
Dave Chinner3f336c62010-12-21 12:02:52 +110091#define xlog_verify_grant_tail(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define xlog_verify_iclog(a,b,c,d)
93#define xlog_verify_tail_lsn(a,b,c)
94#endif
95
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100096STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Christoph Hellwigdd954c62006-01-11 15:34:50 +110098static void
Dave Chinner663e4962010-12-21 12:06:05 +110099xlog_grant_sub_space(
100 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100101 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100102 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100103{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100104 int64_t head_val = atomic64_read(head);
105 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100106
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100107 do {
108 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100109
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100110 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100111
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100112 space -= bytes;
113 if (space < 0) {
114 space += log->l_logsize;
115 cycle--;
116 }
117
118 old = head_val;
119 new = xlog_assign_grant_head_val(cycle, space);
120 head_val = atomic64_cmpxchg(head, old, new);
121 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100122}
123
124static void
Dave Chinner663e4962010-12-21 12:06:05 +1100125xlog_grant_add_space(
126 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100127 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100128 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100129{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100130 int64_t head_val = atomic64_read(head);
131 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100132
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100133 do {
134 int tmp;
135 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100136
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100137 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100138
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100139 tmp = log->l_logsize - space;
140 if (tmp > bytes)
141 space += bytes;
142 else {
143 space = bytes - tmp;
144 cycle++;
145 }
146
147 old = head_val;
148 new = xlog_assign_grant_head_val(cycle, space);
149 head_val = atomic64_cmpxchg(head, old, new);
150 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100151}
Dave Chinnera69ed032010-12-21 12:08:20 +1100152
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000153STATIC bool
154xlog_reserveq_wake(
155 struct log *log,
156 int *free_bytes)
157{
158 struct xlog_ticket *tic;
159 int need_bytes;
160
161 list_for_each_entry(tic, &log->l_reserveq, t_queue) {
162 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
163 need_bytes = tic->t_unit_res * tic->t_cnt;
164 else
165 need_bytes = tic->t_unit_res;
166
167 if (*free_bytes < need_bytes)
168 return false;
169 *free_bytes -= need_bytes;
170
171 trace_xfs_log_grant_wake_up(log, tic);
172 wake_up(&tic->t_wait);
173 }
174
175 return true;
176}
177
178STATIC bool
179xlog_writeq_wake(
180 struct log *log,
181 int *free_bytes)
182{
183 struct xlog_ticket *tic;
184 int need_bytes;
185
186 list_for_each_entry(tic, &log->l_writeq, t_queue) {
187 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
188
189 need_bytes = tic->t_unit_res;
190
191 if (*free_bytes < need_bytes)
192 return false;
193 *free_bytes -= need_bytes;
194
195 trace_xfs_log_regrant_write_wake_up(log, tic);
196 wake_up(&tic->t_wait);
197 }
198
199 return true;
200}
201
202STATIC int
203xlog_reserveq_wait(
204 struct log *log,
205 struct xlog_ticket *tic,
206 int need_bytes)
207{
208 list_add_tail(&tic->t_queue, &log->l_reserveq);
209
210 do {
211 if (XLOG_FORCED_SHUTDOWN(log))
212 goto shutdown;
213 xlog_grant_push_ail(log, need_bytes);
214
215 XFS_STATS_INC(xs_sleep_logspace);
216 trace_xfs_log_grant_sleep(log, tic);
217
218 xlog_wait(&tic->t_wait, &log->l_grant_reserve_lock);
219 trace_xfs_log_grant_wake(log, tic);
220
221 spin_lock(&log->l_grant_reserve_lock);
222 if (XLOG_FORCED_SHUTDOWN(log))
223 goto shutdown;
224 } while (xlog_space_left(log, &log->l_grant_reserve_head) < need_bytes);
225
226 list_del_init(&tic->t_queue);
227 return 0;
228shutdown:
229 list_del_init(&tic->t_queue);
230 return XFS_ERROR(EIO);
231}
232
233STATIC int
234xlog_writeq_wait(
235 struct log *log,
236 struct xlog_ticket *tic,
237 int need_bytes)
238{
239 list_add_tail(&tic->t_queue, &log->l_writeq);
240
241 do {
242 if (XLOG_FORCED_SHUTDOWN(log))
243 goto shutdown;
244 xlog_grant_push_ail(log, need_bytes);
245
246 XFS_STATS_INC(xs_sleep_logspace);
247 trace_xfs_log_regrant_write_sleep(log, tic);
248
249 xlog_wait(&tic->t_wait, &log->l_grant_write_lock);
250 trace_xfs_log_regrant_write_wake(log, tic);
251
252 spin_lock(&log->l_grant_write_lock);
253 if (XLOG_FORCED_SHUTDOWN(log))
254 goto shutdown;
255 } while (xlog_space_left(log, &log->l_grant_write_head) < need_bytes);
256
257 list_del_init(&tic->t_queue);
258 return 0;
259shutdown:
260 list_del_init(&tic->t_queue);
261 return XFS_ERROR(EIO);
262}
263
Christoph Hellwig0adba532007-08-30 17:21:46 +1000264static void
265xlog_tic_reset_res(xlog_ticket_t *tic)
266{
267 tic->t_res_num = 0;
268 tic->t_res_arr_sum = 0;
269 tic->t_res_num_ophdrs = 0;
270}
271
272static void
273xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
274{
275 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
276 /* add to overflow and start again */
277 tic->t_res_o_flow += tic->t_res_arr_sum;
278 tic->t_res_num = 0;
279 tic->t_res_arr_sum = 0;
280 }
281
282 tic->t_res_arr[tic->t_res_num].r_len = len;
283 tic->t_res_arr[tic->t_res_num].r_type = type;
284 tic->t_res_arr_sum += len;
285 tic->t_res_num++;
286}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
289 * NOTES:
290 *
291 * 1. currblock field gets updated at startup and after in-core logs
292 * marked as with WANT_SYNC.
293 */
294
295/*
296 * This routine is called when a user of a log manager ticket is done with
297 * the reservation. If the ticket was ever used, then a commit record for
298 * the associated transaction is written out as a log operation header with
299 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
300 * a given ticket. If the ticket was one with a permanent reservation, then
301 * a few operations are done differently. Permanent reservation tickets by
302 * default don't release the reservation. They just commit the current
303 * transaction with the belief that the reservation is still needed. A flag
304 * must be passed in before permanent reservations are actually released.
305 * When these type of tickets are not released, they need to be set into
306 * the inited state again. By doing this, a start record will be written
307 * out when the next write occurs.
308 */
309xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000310xfs_log_done(
311 struct xfs_mount *mp,
312 struct xlog_ticket *ticket,
313 struct xlog_in_core **iclog,
314 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000316 struct log *log = mp->m_log;
317 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (XLOG_FORCED_SHUTDOWN(log) ||
320 /*
321 * If nothing was ever written, don't write out commit record.
322 * If we get an error, just continue and give back the log ticket.
323 */
324 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100325 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 lsn = (xfs_lsn_t) -1;
327 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
328 flags |= XFS_LOG_REL_PERM_RESERV;
329 }
330 }
331
332
333 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
334 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000335 trace_xfs_log_done_nonperm(log, ticket);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000338 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * request has been made to release a permanent reservation.
340 */
341 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100342 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000344 trace_xfs_log_done_perm(log, ticket);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000347 /* If this ticket was a permanent reservation and we aren't
348 * trying to release it, reset the inited flags; so next time
349 * we write, a start record will be written out.
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
358 * Attaches a new iclog I/O completion callback routine during
359 * transaction commit. If the log is in error state, a non-zero
360 * return code is handed back and the caller is responsible for
361 * executing the callback at an appropriate time.
362 */
363int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000364xfs_log_notify(
365 struct xfs_mount *mp,
366 struct xlog_in_core *iclog,
367 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000369 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Chinner114d23a2008-04-10 12:18:39 +1000371 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
373 if (!abortflg) {
374 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
375 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
376 cb->cb_next = NULL;
377 *(iclog->ic_callback_tail) = cb;
378 iclog->ic_callback_tail = &(cb->cb_next);
379 }
David Chinner114d23a2008-04-10 12:18:39 +1000380 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000385xfs_log_release_iclog(
386 struct xfs_mount *mp,
387 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000389 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000390 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100391 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 return 0;
395}
396
397/*
398 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
399 * to the reservation.
400 * 2. Potentially, push buffers at tail of log to disk.
401 *
402 * Each reservation is going to reserve extra space for a log record header.
403 * When writes happen to the on-disk log, we don't subtract the length of the
404 * log record header from any reservation. By wasting space in each
405 * reservation, we prevent over allocation problems.
406 */
407int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000408xfs_log_reserve(
409 struct xfs_mount *mp,
410 int unit_bytes,
411 int cnt,
412 struct xlog_ticket **ticket,
413 __uint8_t client,
414 uint flags,
415 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000417 struct log *log = mp->m_log;
418 struct xlog_ticket *internal_ticket;
419 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (XLOG_FORCED_SHUTDOWN(log))
424 return XFS_ERROR(EIO);
425
426 XFS_STATS_INC(xs_try_logspace);
427
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (*ticket != NULL) {
430 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000431 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000432
Dave Chinner524ee362010-05-07 11:05:05 +1000433 /*
434 * this is a new transaction on the ticket, so we need to
435 * change the transaction ID so that the next transaction has a
436 * different TID in the log. Just add one to the existing tid
437 * so that we can see chains of rolling transactions in the log
438 * easily.
439 */
440 internal_ticket->t_tid++;
441
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000442 trace_xfs_log_reserve(log, internal_ticket);
443
Dave Chinner2ced19c2010-12-21 12:09:20 +1100444 xlog_grant_push_ail(log, internal_ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 retval = xlog_regrant_write_log_space(log, internal_ticket);
446 } else {
447 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100448 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Dave Chinner3383ca52010-05-07 11:04:17 +1000449 client, flags,
450 KM_SLEEP|KM_MAYFAIL);
David Chinnereb01c9c2008-04-10 12:18:46 +1000451 if (!internal_ticket)
452 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000453 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000455
456 trace_xfs_log_reserve(log, internal_ticket);
457
Dave Chinner2ced19c2010-12-21 12:09:20 +1100458 xlog_grant_push_ail(log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 (internal_ticket->t_unit_res *
460 internal_ticket->t_cnt));
461 retval = xlog_grant_log_space(log, internal_ticket);
462 }
463
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000464 if (unlikely(retval)) {
465 /*
466 * If we are failing, make sure the ticket doesn't have any
467 * current reservations. We don't want to add this back
468 * when the ticket/ transaction gets cancelled.
469 */
470 internal_ticket->t_curr_res = 0;
471 /* ungrant will give back unit_res * t_cnt. */
472 internal_ticket->t_cnt = 0;
473 }
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return retval;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478
479/*
480 * Mount a log filesystem
481 *
482 * mp - ubiquitous xfs mount point structure
483 * log_target - buftarg of on-disk log device
484 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
485 * num_bblocks - Number of BBSIZE blocks in on-disk log
486 *
487 * Return error or zero.
488 */
489int
David Chinner249a8c12008-02-05 12:13:32 +1100490xfs_log_mount(
491 xfs_mount_t *mp,
492 xfs_buftarg_t *log_target,
493 xfs_daddr_t blk_offset,
494 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
David Chinner249a8c12008-02-05 12:13:32 +1100496 int error;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100499 xfs_notice(mp, "Mounting Filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100501 xfs_notice(mp,
502"Mounting filesystem in no-recovery mode. Filesystem will be inconsistent.");
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000503 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200507 if (IS_ERR(mp->m_log)) {
508 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100509 goto out;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /*
David Chinner249a8c12008-02-05 12:13:32 +1100513 * Initialize the AIL now we have a log.
514 */
David Chinner249a8c12008-02-05 12:13:32 +1100515 error = xfs_trans_ail_init(mp);
516 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100517 xfs_warn(mp, "AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100518 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100519 }
David Chinnera9c21c12008-10-30 17:39:35 +1100520 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100521
522 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * skip log recovery on a norecovery mount. pretend it all
524 * just worked.
525 */
526 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100527 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000530 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Eric Sandeen65be6052006-01-11 15:34:19 +1100532 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000535 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100537 xfs_warn(mp, "log mount/recovery failed: error %d",
538 error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100539 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 }
542
543 /* Normal transactions can now occur */
544 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
545
Dave Chinner71e330b2010-05-21 14:37:18 +1000546 /*
547 * Now the log has been fully initialised and we know were our
548 * space grant counters are, we can initialise the permanent ticket
549 * needed for delayed logging to work.
550 */
551 xlog_cil_init_post_recovery(mp->m_log);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100554
555out_destroy_ail:
556 xfs_trans_ail_destroy(mp);
557out_free_log:
558 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100559out:
David Chinner249a8c12008-02-05 12:13:32 +1100560 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563/*
564 * Finish the recovery of the file system. This is separate from
565 * the xfs_log_mount() call, because it depends on the code in
566 * xfs_mountfs() to read in the root and real-time bitmap inodes
567 * between calling xfs_log_mount() and here.
568 *
569 * mp - ubiquitous xfs mount point structure
570 */
571int
Christoph Hellwig42490232008-08-13 16:49:32 +1000572xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int error;
575
576 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000577 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 else {
579 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000580 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 return error;
584}
585
586/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * Final log writes as part of unmount.
588 *
589 * Mark the filesystem clean as unmount happens. Note that during relocation
590 * this routine needs to be executed as part of source-bag while the
591 * deallocation must not be done until source-end.
592 */
593
594/*
595 * Unmount record used to have a string "Unmount filesystem--" in the
596 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
597 * We just write the magic number now since that particular field isn't
598 * currently architecture converted and "nUmount" is a bit foo.
599 * As far as I know, there weren't any dependencies on the old behaviour.
600 */
601
602int
603xfs_log_unmount_write(xfs_mount_t *mp)
604{
605 xlog_t *log = mp->m_log;
606 xlog_in_core_t *iclog;
607#ifdef DEBUG
608 xlog_in_core_t *first_iclog;
609#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000610 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfs_lsn_t lsn;
612 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Don't write out unmount record on read-only mounts.
616 * Or, if we are doing a forced umount (typically because of IO errors).
617 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000618 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
620
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000621 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000622 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624#ifdef DEBUG
625 first_iclog = iclog = log->l_iclog;
626 do {
627 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
628 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
629 ASSERT(iclog->ic_offset == 0);
630 }
631 iclog = iclog->ic_next;
632 } while (iclog != first_iclog);
633#endif
634 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000635 error = xfs_log_reserve(mp, 600, 1, &tic,
636 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100638 /* the data section must be 32 bit size aligned */
639 struct {
640 __uint16_t magic;
641 __uint16_t pad1;
642 __uint32_t pad2; /* may as well make it 64 bits */
643 } magic = {
644 .magic = XLOG_UNMOUNT_TYPE,
645 };
646 struct xfs_log_iovec reg = {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000647 .i_addr = &magic,
Dave Chinner55b66332010-03-23 11:43:17 +1100648 .i_len = sizeof(magic),
649 .i_type = XLOG_REG_TYPE_UNMOUNT,
650 };
651 struct xfs_log_vec vec = {
652 .lv_niovecs = 1,
653 .lv_iovecp = &reg,
654 };
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100657 tic->t_flags = 0;
658 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 NULL, XLOG_UNMOUNT_TRANS);
660 /*
661 * At this point, we're umounting anyway,
662 * so there's no point in transitioning log state
663 * to IOERROR. Just continue...
664 */
665 }
666
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100667 if (error)
668 xfs_alert(mp, "%s: unmount record failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000671 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100673 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100675 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000676 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000678 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
680 iclog->ic_state == XLOG_STATE_DIRTY)) {
681 if (!XLOG_FORCED_SHUTDOWN(log)) {
Dave Chinnereb40a872010-12-21 12:09:01 +1100682 xlog_wait(&iclog->ic_force_wait,
683 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000685 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000688 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000690 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000691 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000692 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100693 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 } else {
696 /*
697 * We're already in forced_shutdown mode, couldn't
698 * even attempt to write out the unmount transaction.
699 *
700 * Go through the motions of sync'ing and releasing
701 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000702 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * be in progress. Do this as a separate section of
704 * code so we'll know if we ever get stuck here that
705 * we're in this odd situation of trying to unmount
706 * a file system that went into forced_shutdown as
707 * the result of an unmount..
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
713 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100714 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000715 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000717 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
720 || iclog->ic_state == XLOG_STATE_DIRTY
721 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
722
Dave Chinnereb40a872010-12-21 12:09:01 +1100723 xlog_wait(&iclog->ic_force_wait,
724 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000726 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728 }
729
David Chinner1bb7d6b2008-04-10 12:24:38 +1000730 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731} /* xfs_log_unmount_write */
732
733/*
734 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100735 *
736 * We need to stop the aild from running before we destroy
737 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
739void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100740xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
David Chinner249a8c12008-02-05 12:13:32 +1100742 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000743 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Dave Chinner43f5efc2010-03-23 10:10:00 +1100746void
747xfs_log_item_init(
748 struct xfs_mount *mp,
749 struct xfs_log_item *item,
750 int type,
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000751 const struct xfs_item_ops *ops)
Dave Chinner43f5efc2010-03-23 10:10:00 +1100752{
753 item->li_mountp = mp;
754 item->li_ailp = mp->m_ail;
755 item->li_type = type;
756 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +1000757 item->li_lv = NULL;
758
759 INIT_LIST_HEAD(&item->li_ail);
760 INIT_LIST_HEAD(&item->li_cil);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100761}
762
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000763/*
764 * Wake up processes waiting for log space after we have moved the log tail.
765 *
766 * If opportunistic is set wake up one waiter even if we do not have enough
767 * free space by our strict accounting.
768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769void
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000770xfs_log_space_wake(
771 struct xfs_mount *mp,
772 bool opportunistic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000774 struct xlog_ticket *tic;
775 struct log *log = mp->m_log;
776 int need_bytes, free_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (XLOG_FORCED_SHUTDOWN(log))
779 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Chinner3f16b982010-12-21 12:29:01 +1100781 if (!list_empty_careful(&log->l_writeq)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000782 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
783
Dave Chinner3f16b982010-12-21 12:29:01 +1100784 spin_lock(&log->l_grant_write_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +1100785 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Dave Chinner10547942010-12-21 12:02:25 +1100786 list_for_each_entry(tic, &log->l_writeq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
788
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000789 if (free_bytes < tic->t_unit_res && !opportunistic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000791 opportunistic = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 free_bytes -= tic->t_unit_res;
Dave Chinner3f16b982010-12-21 12:29:01 +1100793 trace_xfs_log_regrant_write_wake_up(log, tic);
Dave Chinnereb40a872010-12-21 12:09:01 +1100794 wake_up(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100795 }
Dave Chinner3f16b982010-12-21 12:29:01 +1100796 spin_unlock(&log->l_grant_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Dave Chinner10547942010-12-21 12:02:25 +1100798
Dave Chinner3f16b982010-12-21 12:29:01 +1100799 if (!list_empty_careful(&log->l_reserveq)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000800 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
801
Dave Chinner3f16b982010-12-21 12:29:01 +1100802 spin_lock(&log->l_grant_reserve_lock);
Dave Chinnera69ed032010-12-21 12:08:20 +1100803 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Dave Chinner10547942010-12-21 12:02:25 +1100804 list_for_each_entry(tic, &log->l_reserveq, t_queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
806 need_bytes = tic->t_unit_res*tic->t_cnt;
807 else
808 need_bytes = tic->t_unit_res;
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000809 if (free_bytes < need_bytes && !opportunistic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000811 opportunistic = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 free_bytes -= need_bytes;
Dave Chinner3f16b982010-12-21 12:29:01 +1100813 trace_xfs_log_grant_wake_up(log, tic);
Dave Chinnereb40a872010-12-21 12:09:01 +1100814 wake_up(&tic->t_wait);
Dave Chinner10547942010-12-21 12:02:25 +1100815 }
Dave Chinner3f16b982010-12-21 12:29:01 +1100816 spin_unlock(&log->l_grant_reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Dave Chinner3f16b982010-12-21 12:29:01 +1100818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820/*
821 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000822 * that needs to be covered. To begin the transition to the idle state
823 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
824 * If we are then in a state where covering is needed, the caller is informed
825 * that dummy transactions are required to move the log into the idle state.
826 *
827 * Because this is called as part of the sync process, we should also indicate
828 * that dummy transactions should be issued in anything but the covered or
829 * idle states. This ensures that the log tail is accurately reflected in
830 * the log at the end of the sync, hence if a crash occurrs avoids replay
831 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
833int
834xfs_log_need_covered(xfs_mount_t *mp)
835{
David Chinner27d8d5f2008-10-30 17:38:39 +1100836 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David Chinner92821e22007-05-24 15:26:31 +1000839 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000842 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000843 switch (log->l_covered_state) {
844 case XLOG_STATE_COVER_DONE:
845 case XLOG_STATE_COVER_DONE2:
846 case XLOG_STATE_COVER_IDLE:
847 break;
848 case XLOG_STATE_COVER_NEED:
849 case XLOG_STATE_COVER_NEED2:
Dave Chinnerfd074842011-04-08 12:45:07 +1000850 if (!xfs_ail_min_lsn(log->l_ailp) &&
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000851 xlog_iclogs_empty(log)) {
852 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
853 log->l_covered_state = XLOG_STATE_COVER_DONE;
854 else
855 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000857 /* FALLTHRU */
858 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000860 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000862 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100863 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000866/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 * We may be holding the log iclog lock upon entering this routine.
868 */
869xfs_lsn_t
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100870xlog_assign_tail_lsn(
871 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100873 xfs_lsn_t tail_lsn;
874 struct log *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000876 /*
877 * To make sure we always have a valid LSN for the log tail we keep
878 * track of the last LSN which was committed in log->l_last_sync_lsn,
879 * and use that when the AIL was empty and xfs_ail_min_lsn returns 0.
880 *
881 * If the AIL has been emptied we also need to wake any process
882 * waiting for this condition.
883 */
Dave Chinnerfd074842011-04-08 12:45:07 +1000884 tail_lsn = xfs_ail_min_lsn(mp->m_ail);
Dave Chinner84f3c682010-12-03 22:11:29 +1100885 if (!tail_lsn)
886 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100887 atomic64_set(&log->l_tail_lsn, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return tail_lsn;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891/*
892 * Return the space in the log between the tail and the head. The head
893 * is passed in the cycle/bytes formal parms. In the special case where
894 * the reserve head has wrapped passed the tail, this calculation is no
895 * longer valid. In this case, just return 0 which means there is no space
896 * in the log. This works for all places where this function is called
897 * with the reserve head. Of course, if the write head were to ever
898 * wrap the tail, we should blow up. Rather than catch this case here,
899 * we depend on other ASSERTions in other parts of the code. XXXmiken
900 *
901 * This code also handles the case where the reservation head is behind
902 * the tail. The details of this case are described below, but the end
903 * result is that we return the size of the log as the amount of space left.
904 */
David Chinnera8272ce2007-11-23 16:28:09 +1100905STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +1100906xlog_space_left(
907 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100908 atomic64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Dave Chinnera69ed032010-12-21 12:08:20 +1100910 int free_bytes;
911 int tail_bytes;
912 int tail_cycle;
913 int head_cycle;
914 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Dave Chinnera69ed032010-12-21 12:08:20 +1100916 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100917 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
918 tail_bytes = BBTOB(tail_bytes);
Dave Chinnera69ed032010-12-21 12:08:20 +1100919 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
920 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
921 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +1100923 else if (tail_cycle < head_cycle) {
924 ASSERT(tail_cycle == (head_cycle - 1));
925 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 } else {
927 /*
928 * The reservation head is behind the tail.
929 * In this case we just want to return the size of the
930 * log as the amount of space left.
931 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100932 xfs_alert(log->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 "xlog_space_left: head behind tail\n"
934 " tail_cycle = %d, tail_bytes = %d\n"
935 " GH cycle = %d, GH bytes = %d",
Dave Chinnera69ed032010-12-21 12:08:20 +1100936 tail_cycle, tail_bytes, head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 ASSERT(0);
938 free_bytes = log->l_logsize;
939 }
940 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +1100941}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943
944/*
945 * Log function which is called when an io completes.
946 *
947 * The log manager needs its own routine, in order to control what
948 * happens with the buffer after the write completes.
949 */
950void
951xlog_iodone(xfs_buf_t *bp)
952{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200953 xlog_in_core_t *iclog = bp->b_fspriv;
954 xlog_t *l = iclog->ic_log;
955 int aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /*
958 * Race to shutdown the filesystem if we see an error.
959 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000960 if (XFS_TEST_ERROR((xfs_buf_geterror(bp)), l->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
Christoph Hellwig901796a2011-10-10 16:52:49 +0000962 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000963 xfs_buf_stale(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000964 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * This flag will be propagated to the trans-committed
967 * callback routines to let them know that the log-commit
968 * didn't succeed.
969 */
970 aborted = XFS_LI_ABORTED;
971 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
972 aborted = XFS_LI_ABORTED;
973 }
David Chinner3db296f2007-05-14 18:24:16 +1000974
975 /* log I/O is always issued ASYNC */
976 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000978 /*
979 * do not reference the buffer (bp) here as we could race
980 * with it being freed after writing the unmount record to the
981 * log.
982 */
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984} /* xlog_iodone */
985
986/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * Return size of each in-core log record buffer.
988 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200989 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
991 * If the filesystem blocksize is too large, we may need to choose a
992 * larger size since the directory code currently logs entire blocks.
993 */
994
995STATIC void
996xlog_get_iclog_buffer_size(xfs_mount_t *mp,
997 xlog_t *log)
998{
999 int size;
1000 int xhdrs;
1001
Eric Sandeen1cb51252007-08-16 16:24:43 +10001002 if (mp->m_logbufs <= 0)
1003 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
1004 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001005 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
1008 * Buffer size passed in from mount system call.
1009 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001010 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 size = log->l_iclog_size = mp->m_logbsize;
1012 log->l_iclog_size_log = 0;
1013 while (size != 1) {
1014 log->l_iclog_size_log++;
1015 size >>= 1;
1016 }
1017
Eric Sandeen62118702008-03-06 13:44:28 +11001018 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001019 /* # headers = size / 32k
1020 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
1022
1023 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
1024 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
1025 xhdrs++;
1026 log->l_iclog_hsize = xhdrs << BBSHIFT;
1027 log->l_iclog_heads = xhdrs;
1028 } else {
1029 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
1030 log->l_iclog_hsize = BBSIZE;
1031 log->l_iclog_heads = 1;
1032 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001033 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001036 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +10001037 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
1038 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* the default log size is 16k or 32k which is one header sector */
1041 log->l_iclog_hsize = BBSIZE;
1042 log->l_iclog_heads = 1;
1043
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001044done:
1045 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001046 if (mp->m_logbufs == 0)
1047 mp->m_logbufs = log->l_iclog_bufs;
1048 if (mp->m_logbsize == 0)
1049 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050} /* xlog_get_iclog_buffer_size */
1051
1052
1053/*
1054 * This routine initializes some of the log structure for a given mount point.
1055 * Its primary purpose is to fill in enough, so recovery can occur. However,
1056 * some other stuff may be filled in too.
1057 */
1058STATIC xlog_t *
1059xlog_alloc_log(xfs_mount_t *mp,
1060 xfs_buftarg_t *log_target,
1061 xfs_daddr_t blk_offset,
1062 int num_bblks)
1063{
1064 xlog_t *log;
1065 xlog_rec_header_t *head;
1066 xlog_in_core_t **iclogp;
1067 xlog_in_core_t *iclog, *prev_iclog=NULL;
1068 xfs_buf_t *bp;
1069 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001070 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001071 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Dave Chinner644c3562008-11-10 16:50:24 +11001073 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001074 if (!log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001075 xfs_warn(mp, "Log allocation failed: No memory!");
Dave Chinnera6cb7672009-04-06 18:39:27 +02001076 goto out;
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 log->l_mp = mp;
1080 log->l_targ = log_target;
1081 log->l_logsize = BBTOB(num_bblks);
1082 log->l_logBBstart = blk_offset;
1083 log->l_logBBsize = num_bblks;
1084 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1085 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1086
1087 log->l_prev_block = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001089 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1090 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Dave Chinnera69ed032010-12-21 12:08:20 +11001092 xlog_assign_grant_head(&log->l_grant_reserve_head, 1, 0);
1093 xlog_assign_grant_head(&log->l_grant_write_head, 1, 0);
Dave Chinner10547942010-12-21 12:02:25 +11001094 INIT_LIST_HEAD(&log->l_reserveq);
1095 INIT_LIST_HEAD(&log->l_writeq);
Dave Chinner3f16b982010-12-21 12:29:01 +11001096 spin_lock_init(&log->l_grant_reserve_lock);
1097 spin_lock_init(&log->l_grant_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Dave Chinnera6cb7672009-04-06 18:39:27 +02001099 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001100 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001101 log2_size = mp->m_sb.sb_logsectlog;
1102 if (log2_size < BBSHIFT) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001103 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1104 log2_size, BBSHIFT);
Alex Elder69ce58f2010-04-20 17:09:59 +10001105 goto out_free_log;
1106 }
1107
1108 log2_size -= BBSHIFT;
1109 if (log2_size > mp->m_sectbb_log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001110 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1111 log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001112 goto out_free_log;
1113 }
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001116 if (log2_size && log->l_logBBstart > 0 &&
1117 !xfs_sb_version_haslogv2(&mp->m_sb)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001118 xfs_warn(mp,
1119 "log sector size (0x%x) invalid for configuration.",
1120 log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001121 goto out_free_log;
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001124 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 xlog_get_iclog_buffer_size(mp, log);
1127
Dave Chinnera6cb7672009-04-06 18:39:27 +02001128 error = ENOMEM;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +00001129 bp = xfs_buf_alloc(mp->m_logdev_targp, 0, log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001130 if (!bp)
1131 goto out_free_log;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001132 bp->b_iodone = xlog_iodone;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001133 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 log->l_xbuf = bp;
1135
Eric Sandeen007c61c2007-10-11 17:43:56 +10001136 spin_lock_init(&log->l_icloglock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001137 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1140 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1141
1142 iclogp = &log->l_iclog;
1143 /*
1144 * The amount of memory to allocate for the iclog structure is
1145 * rather funky due to the way the structure is defined. It is
1146 * done this way so that we can use different sizes for machines
1147 * with different amounts of memory. See the definition of
1148 * xlog_in_core_t in xfs_log_priv.h for details.
1149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 ASSERT(log->l_iclog_size >= 4096);
1151 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001152 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1153 if (!*iclogp)
1154 goto out_free_iclog;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 iclog->ic_prev = prev_iclog;
1158 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001159
Dave Chinner686865f2010-09-24 20:07:47 +10001160 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1161 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001162 if (!bp)
1163 goto out_free_iclog;
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001164
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001165 bp->b_iodone = xlog_iodone;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001166 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001167 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001168#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 head = &iclog->ic_header;
1172 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001173 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1174 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001175 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001176 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001178 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1182 iclog->ic_state = XLOG_STATE_ACTIVE;
1183 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001184 atomic_set(&iclog->ic_refcnt, 0);
1185 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001187 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001189 ASSERT(xfs_buf_islocked(iclog->ic_bp));
Dave Chinnereb40a872010-12-21 12:09:01 +11001190 init_waitqueue_head(&iclog->ic_force_wait);
1191 init_waitqueue_head(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 iclogp = &iclog->ic_next;
1194 }
1195 *iclogp = log->l_iclog; /* complete ring */
1196 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1197
Dave Chinner71e330b2010-05-21 14:37:18 +10001198 error = xlog_cil_init(log);
1199 if (error)
1200 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001202
1203out_free_iclog:
1204 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1205 prev_iclog = iclog->ic_next;
Dave Chinnereb40a872010-12-21 12:09:01 +11001206 if (iclog->ic_bp)
Dave Chinner644c3562008-11-10 16:50:24 +11001207 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001208 kmem_free(iclog);
1209 }
1210 spinlock_destroy(&log->l_icloglock);
Dave Chinner644c3562008-11-10 16:50:24 +11001211 xfs_buf_free(log->l_xbuf);
1212out_free_log:
1213 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001214out:
1215 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216} /* xlog_alloc_log */
1217
1218
1219/*
1220 * Write out the commit record of a transaction associated with the given
1221 * ticket. Return the lsn of the commit record.
1222 */
1223STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001224xlog_commit_record(
1225 struct log *log,
1226 struct xlog_ticket *ticket,
1227 struct xlog_in_core **iclog,
1228 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Dave Chinner55b66332010-03-23 11:43:17 +11001230 struct xfs_mount *mp = log->l_mp;
1231 int error;
1232 struct xfs_log_iovec reg = {
1233 .i_addr = NULL,
1234 .i_len = 0,
1235 .i_type = XLOG_REG_TYPE_COMMIT,
1236 };
1237 struct xfs_log_vec vec = {
1238 .lv_niovecs = 1,
1239 .lv_iovecp = &reg,
1240 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001243 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1244 XLOG_COMMIT_TRANS);
1245 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001246 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001247 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250/*
1251 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1252 * log space. This code pushes on the lsn which would supposedly free up
1253 * the 25% which we want to leave free. We may need to adopt a policy which
1254 * pushes on an lsn which is further along in the log once we reach the high
1255 * water mark. In this manner, we would be creating a low water mark.
1256 */
David Chinnera8272ce2007-11-23 16:28:09 +11001257STATIC void
Dave Chinner2ced19c2010-12-21 12:09:20 +11001258xlog_grant_push_ail(
1259 struct log *log,
1260 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001262 xfs_lsn_t threshold_lsn = 0;
Dave Chinner84f3c682010-12-03 22:11:29 +11001263 xfs_lsn_t last_sync_lsn;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001264 int free_blocks;
1265 int free_bytes;
1266 int threshold_block;
1267 int threshold_cycle;
1268 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Dave Chinner2ced19c2010-12-21 12:09:20 +11001270 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Dave Chinner2ced19c2010-12-21 12:09:20 +11001272 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
1273 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Dave Chinner2ced19c2010-12-21 12:09:20 +11001275 /*
1276 * Set the threshold for the minimum number of free blocks in the
1277 * log to the maximum of what the caller needs, one quarter of the
1278 * log, and 256 blocks.
1279 */
1280 free_threshold = BTOBB(need_bytes);
1281 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1282 free_threshold = MAX(free_threshold, 256);
1283 if (free_blocks >= free_threshold)
1284 return;
1285
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001286 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1287 &threshold_block);
1288 threshold_block += free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001290 threshold_block -= log->l_logBBsize;
1291 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001293 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1294 threshold_block);
1295 /*
1296 * Don't pass in an lsn greater than the lsn of the last
Dave Chinner84f3c682010-12-03 22:11:29 +11001297 * log record known to be on disk. Use a snapshot of the last sync lsn
1298 * so that it doesn't change between the compare and the set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
Dave Chinner84f3c682010-12-03 22:11:29 +11001300 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1301 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1302 threshold_lsn = last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Dave Chinner2ced19c2010-12-21 12:09:20 +11001304 /*
1305 * Get the transaction layer to kick the dirty buffers out to
1306 * disk asynchronously. No point in trying to do this if
1307 * the filesystem is shutting down.
1308 */
1309 if (!XLOG_FORCED_SHUTDOWN(log))
Dave Chinnerfd074842011-04-08 12:45:07 +10001310 xfs_ail_push(log->l_ailp, threshold_lsn);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001311}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Christoph Hellwig873ff552010-01-13 22:17:57 +00001313/*
1314 * The bdstrat callback function for log bufs. This gives us a central
1315 * place to trap bufs in case we get hit by a log I/O error and need to
1316 * shutdown. Actually, in practice, even when we didn't get a log error,
1317 * we transition the iclogs to IOERROR state *after* flushing all existing
1318 * iclogs to disk. This is because we don't want anymore new transactions to be
1319 * started or completed afterwards.
1320 */
1321STATIC int
1322xlog_bdstrat(
1323 struct xfs_buf *bp)
1324{
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001325 struct xlog_in_core *iclog = bp->b_fspriv;
Christoph Hellwig873ff552010-01-13 22:17:57 +00001326
Christoph Hellwig873ff552010-01-13 22:17:57 +00001327 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001328 xfs_buf_ioerror(bp, EIO);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001329 xfs_buf_stale(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001330 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001331 /*
1332 * It would seem logical to return EIO here, but we rely on
1333 * the log state machine to propagate I/O errors instead of
1334 * doing it here.
1335 */
1336 return 0;
1337 }
1338
Christoph Hellwig873ff552010-01-13 22:17:57 +00001339 xfs_buf_iorequest(bp);
1340 return 0;
1341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343/*
1344 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1345 * fashion. Previously, we should have moved the current iclog
1346 * ptr in the log to point to the next available iclog. This allows further
1347 * write to continue while this code syncs out an iclog ready to go.
1348 * Before an in-core log can be written out, the data section must be scanned
1349 * to save away the 1st word of each BBSIZE block into the header. We replace
1350 * it with the current cycle count. Each BBSIZE block is tagged with the
1351 * cycle count because there in an implicit assumption that drives will
1352 * guarantee that entire 512 byte blocks get written at once. In other words,
1353 * we can't have part of a 512 byte block written and part not written. By
1354 * tagging each block, we will know which blocks are valid when recovering
1355 * after an unclean shutdown.
1356 *
1357 * This routine is single threaded on the iclog. No other thread can be in
1358 * this routine with the same iclog. Changing contents of iclog can there-
1359 * fore be done without grabbing the state machine lock. Updating the global
1360 * log will require grabbing the lock though.
1361 *
1362 * The entire log manager uses a logical block numbering scheme. Only
1363 * log_sync (and then only bwrite()) know about the fact that the log may
1364 * not start with block zero on a given device. The log block start offset
1365 * is added immediately before calling bwrite().
1366 */
1367
David Chinnera8272ce2007-11-23 16:28:09 +11001368STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369xlog_sync(xlog_t *log,
1370 xlog_in_core_t *iclog)
1371{
1372 xfs_caddr_t dptr; /* pointer to byte sized element */
1373 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001374 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 uint count; /* byte count of bwrite */
1376 uint count_init; /* initial count before roundup */
1377 int roundoff; /* roundoff to BB or stripe */
1378 int split = 0; /* split write into two regions */
1379 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001380 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001383 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* Add for LR header */
1386 count_init = log->l_iclog_hsize + iclog->ic_offset;
1387
1388 /* Round out the log write size */
1389 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1390 /* we have a v2 stripe unit to use */
1391 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1392 } else {
1393 count = BBTOB(BTOBB(count_init));
1394 }
1395 roundoff = count - count_init;
1396 ASSERT(roundoff >= 0);
1397 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1398 roundoff < log->l_mp->m_sb.sb_logsunit)
1399 ||
1400 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1401 roundoff < BBTOB(1)));
1402
1403 /* move grant heads by roundoff in sync */
Dave Chinnera69ed032010-12-21 12:08:20 +11001404 xlog_grant_add_space(log, &log->l_grant_reserve_head, roundoff);
1405 xlog_grant_add_space(log, &log->l_grant_write_head, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /* put cycle number in every block */
1408 xlog_pack_data(log, iclog, roundoff);
1409
1410 /* real byte length */
1411 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001412 iclog->ic_header.h_len =
1413 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001415 iclog->ic_header.h_len =
1416 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
Nathan Scottf5faad72006-07-28 17:04:44 +10001419 bp = iclog->ic_bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001420 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1423
1424 /* Do we need to split this write into 2 parts? */
1425 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1426 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1427 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1428 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1429 } else {
1430 iclog->ic_bwritecnt = 1;
1431 }
David Chinner511105b2007-05-24 15:21:57 +10001432 XFS_BUF_SET_COUNT(bp, count);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001433 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001434 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001436 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001437
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001438 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) {
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001439 bp->b_flags |= XBF_FUA;
1440
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001441 /*
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001442 * Flush the data device before flushing the log to make
1443 * sure all meta data written back from the AIL actually made
1444 * it to disk before stamping the new log tail LSN into the
1445 * log buffer. For an external log we need to issue the
1446 * flush explicitly, and unfortunately synchronously here;
1447 * for an internal log we can simply use the block layer
1448 * state machine for preflushes.
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001449 */
1450 if (log->l_mp->m_logdev_targp != log->l_mp->m_ddev_targp)
1451 xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001452 else
1453 bp->b_flags |= XBF_FLUSH;
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1457 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1458
1459 xlog_verify_iclog(log, iclog, count, B_TRUE);
1460
1461 /* account for log which doesn't start at block #0 */
1462 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1463 /*
1464 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1465 * is shutting down.
1466 */
1467 XFS_BUF_WRITE(bp);
1468
Christoph Hellwig901796a2011-10-10 16:52:49 +00001469 error = xlog_bdstrat(bp);
1470 if (error) {
1471 xfs_buf_ioerror_alert(bp, "xlog_sync");
Jesper Juhl014c2542006-01-15 02:37:08 +01001472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001475 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
Chandra Seetharaman02fe03d2011-07-22 23:40:22 +00001477 xfs_buf_associate_memory(bp,
1478 (char *)&iclog->ic_header + count, split);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001479 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001480 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001482 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001483 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001484 bp->b_flags |= XBF_FUA;
Chandra Seetharaman62926042011-07-22 23:40:15 +00001485 dptr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * Bump the cycle numbers at the start of each block
1488 * since this part of the buffer is at the start of
1489 * a new cycle. Watch out for the header magic number
1490 * case, though.
1491 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001492 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001493 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001494 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001495 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 dptr += BBSIZE;
1497 }
1498
1499 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1500 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1501
Nathan Scottc41564b2006-03-29 08:55:14 +10001502 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1504 XFS_BUF_WRITE(bp);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001505 error = xlog_bdstrat(bp);
1506 if (error) {
1507 xfs_buf_ioerror_alert(bp, "xlog_sync (split)");
Jesper Juhl014c2542006-01-15 02:37:08 +01001508 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512} /* xlog_sync */
1513
1514
1515/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001516 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
David Chinnera8272ce2007-11-23 16:28:09 +11001518STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001519xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int i;
1523
Dave Chinner71e330b2010-05-21 14:37:18 +10001524 xlog_cil_destroy(log);
1525
Dave Chinner44396472011-04-21 09:34:27 +00001526 /*
1527 * always need to ensure that the extra buffer does not point to memory
1528 * owned by another log buffer before we free it.
1529 */
1530 xfs_buf_set_empty(log->l_xbuf, log->l_iclog_size);
1531 xfs_buf_free(log->l_xbuf);
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 iclog = log->l_iclog;
1534 for (i=0; i<log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001537 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 iclog = next_iclog;
1539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 spinlock_destroy(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001543 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001544} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546/*
1547 * Update counters atomically now that memcpy is done.
1548 */
1549/* ARGSUSED */
1550static inline void
1551xlog_state_finish_copy(xlog_t *log,
1552 xlog_in_core_t *iclog,
1553 int record_cnt,
1554 int copy_bytes)
1555{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001556 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001558 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 iclog->ic_offset += copy_bytes;
1560
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001561 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562} /* xlog_state_finish_copy */
1563
1564
1565
1566
1567/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001568 * print out info relating to regions written which consume
1569 * the reservation
1570 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001571void
1572xlog_print_tic_res(
1573 struct xfs_mount *mp,
1574 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001575{
1576 uint i;
1577 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1578
1579 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1580 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1581 "bformat",
1582 "bchunk",
1583 "efi_format",
1584 "efd_format",
1585 "iformat",
1586 "icore",
1587 "iext",
1588 "ibroot",
1589 "ilocal",
1590 "iattr_ext",
1591 "iattr_broot",
1592 "iattr_local",
1593 "qformat",
1594 "dquot",
1595 "quotaoff",
1596 "LR header",
1597 "unmount",
1598 "commit",
1599 "trans header"
1600 };
1601 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1602 "SETATTR_NOT_SIZE",
1603 "SETATTR_SIZE",
1604 "INACTIVE",
1605 "CREATE",
1606 "CREATE_TRUNC",
1607 "TRUNCATE_FILE",
1608 "REMOVE",
1609 "LINK",
1610 "RENAME",
1611 "MKDIR",
1612 "RMDIR",
1613 "SYMLINK",
1614 "SET_DMATTRS",
1615 "GROWFS",
1616 "STRAT_WRITE",
1617 "DIOSTRAT",
1618 "WRITE_SYNC",
1619 "WRITEID",
1620 "ADDAFORK",
1621 "ATTRINVAL",
1622 "ATRUNCATE",
1623 "ATTR_SET",
1624 "ATTR_RM",
1625 "ATTR_FLAG",
1626 "CLEAR_AGI_BUCKET",
1627 "QM_SBCHANGE",
1628 "DUMMY1",
1629 "DUMMY2",
1630 "QM_QUOTAOFF",
1631 "QM_DQALLOC",
1632 "QM_SETQLIM",
1633 "QM_DQCLUSTER",
1634 "QM_QINOCREATE",
1635 "QM_QUOTAOFF_END",
1636 "SB_UNIT",
1637 "FSYNC_TS",
1638 "GROWFSRT_ALLOC",
1639 "GROWFSRT_ZERO",
1640 "GROWFSRT_FREE",
1641 "SWAPEXT"
1642 };
1643
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001644 xfs_warn(mp,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001645 "xlog_write: reservation summary:\n"
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001646 " trans type = %s (%u)\n"
1647 " unit res = %d bytes\n"
1648 " current res = %d bytes\n"
1649 " total reg = %u bytes (o/flow = %u bytes)\n"
1650 " ophdrs = %u (ophdr space = %u bytes)\n"
1651 " ophdr + reg = %u bytes\n"
1652 " num regions = %u\n",
1653 ((ticket->t_trans_type <= 0 ||
1654 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1655 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1656 ticket->t_trans_type,
1657 ticket->t_unit_res,
1658 ticket->t_curr_res,
1659 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1660 ticket->t_res_num_ophdrs, ophdr_spc,
1661 ticket->t_res_arr_sum +
1662 ticket->t_res_o_flow + ophdr_spc,
1663 ticket->t_res_num);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001664
1665 for (i = 0; i < ticket->t_res_num; i++) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001666 uint r_type = ticket->t_res_arr[i].r_type;
1667 xfs_warn(mp, "region[%u]: %s - %u bytes\n", i,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001668 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1669 "bad-rtype" : res_type_str[r_type-1]),
1670 ticket->t_res_arr[i].r_len);
1671 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001672
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001673 xfs_alert_tag(mp, XFS_PTAG_LOGRES,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001674 "xlog_write: reservation ran out. Need to up reservation");
Dave Chinner169a7b02010-05-07 11:05:31 +10001675 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001676}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001677
1678/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001679 * Calculate the potential space needed by the log vector. Each region gets
1680 * its own xlog_op_header_t and may need to be double word aligned.
1681 */
1682static int
1683xlog_write_calc_vec_length(
1684 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001685 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001686{
Dave Chinner55b66332010-03-23 11:43:17 +11001687 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001688 int headers = 0;
1689 int len = 0;
1690 int i;
1691
1692 /* acct for start rec of xact */
1693 if (ticket->t_flags & XLOG_TIC_INITED)
1694 headers++;
1695
Dave Chinner55b66332010-03-23 11:43:17 +11001696 for (lv = log_vector; lv; lv = lv->lv_next) {
1697 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001698
Dave Chinner55b66332010-03-23 11:43:17 +11001699 for (i = 0; i < lv->lv_niovecs; i++) {
1700 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1701
1702 len += vecp->i_len;
1703 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1704 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001705 }
1706
1707 ticket->t_res_num_ophdrs += headers;
1708 len += headers * sizeof(struct xlog_op_header);
1709
1710 return len;
1711}
1712
1713/*
1714 * If first write for transaction, insert start record We can't be trying to
1715 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1716 */
1717static int
1718xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001719 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001720 struct xlog_ticket *ticket)
1721{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001722 if (!(ticket->t_flags & XLOG_TIC_INITED))
1723 return 0;
1724
1725 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1726 ophdr->oh_clientid = ticket->t_clientid;
1727 ophdr->oh_len = 0;
1728 ophdr->oh_flags = XLOG_START_TRANS;
1729 ophdr->oh_res2 = 0;
1730
1731 ticket->t_flags &= ~XLOG_TIC_INITED;
1732
1733 return sizeof(struct xlog_op_header);
1734}
1735
1736static xlog_op_header_t *
1737xlog_write_setup_ophdr(
1738 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001739 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001740 struct xlog_ticket *ticket,
1741 uint flags)
1742{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001743 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1744 ophdr->oh_clientid = ticket->t_clientid;
1745 ophdr->oh_res2 = 0;
1746
1747 /* are we copying a commit or unmount record? */
1748 ophdr->oh_flags = flags;
1749
1750 /*
1751 * We've seen logs corrupted with bad transaction client ids. This
1752 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1753 * and shut down the filesystem.
1754 */
1755 switch (ophdr->oh_clientid) {
1756 case XFS_TRANSACTION:
1757 case XFS_VOLUME:
1758 case XFS_LOG:
1759 break;
1760 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001761 xfs_warn(log->l_mp,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001762 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1763 ophdr->oh_clientid, ticket);
1764 return NULL;
1765 }
1766
1767 return ophdr;
1768}
1769
1770/*
1771 * Set up the parameters of the region copy into the log. This has
1772 * to handle region write split across multiple log buffers - this
1773 * state is kept external to this function so that this code can
1774 * can be written in an obvious, self documenting manner.
1775 */
1776static int
1777xlog_write_setup_copy(
1778 struct xlog_ticket *ticket,
1779 struct xlog_op_header *ophdr,
1780 int space_available,
1781 int space_required,
1782 int *copy_off,
1783 int *copy_len,
1784 int *last_was_partial_copy,
1785 int *bytes_consumed)
1786{
1787 int still_to_copy;
1788
1789 still_to_copy = space_required - *bytes_consumed;
1790 *copy_off = *bytes_consumed;
1791
1792 if (still_to_copy <= space_available) {
1793 /* write of region completes here */
1794 *copy_len = still_to_copy;
1795 ophdr->oh_len = cpu_to_be32(*copy_len);
1796 if (*last_was_partial_copy)
1797 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1798 *last_was_partial_copy = 0;
1799 *bytes_consumed = 0;
1800 return 0;
1801 }
1802
1803 /* partial write of region, needs extra log op header reservation */
1804 *copy_len = space_available;
1805 ophdr->oh_len = cpu_to_be32(*copy_len);
1806 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1807 if (*last_was_partial_copy)
1808 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1809 *bytes_consumed += *copy_len;
1810 (*last_was_partial_copy)++;
1811
1812 /* account for new log op header */
1813 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1814 ticket->t_res_num_ophdrs++;
1815
1816 return sizeof(struct xlog_op_header);
1817}
1818
1819static int
1820xlog_write_copy_finish(
1821 struct log *log,
1822 struct xlog_in_core *iclog,
1823 uint flags,
1824 int *record_cnt,
1825 int *data_cnt,
1826 int *partial_copy,
1827 int *partial_copy_len,
1828 int log_offset,
1829 struct xlog_in_core **commit_iclog)
1830{
1831 if (*partial_copy) {
1832 /*
1833 * This iclog has already been marked WANT_SYNC by
1834 * xlog_state_get_iclog_space.
1835 */
1836 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1837 *record_cnt = 0;
1838 *data_cnt = 0;
1839 return xlog_state_release_iclog(log, iclog);
1840 }
1841
1842 *partial_copy = 0;
1843 *partial_copy_len = 0;
1844
1845 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1846 /* no more space in this iclog - push it. */
1847 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1848 *record_cnt = 0;
1849 *data_cnt = 0;
1850
1851 spin_lock(&log->l_icloglock);
1852 xlog_state_want_sync(log, iclog);
1853 spin_unlock(&log->l_icloglock);
1854
1855 if (!commit_iclog)
1856 return xlog_state_release_iclog(log, iclog);
1857 ASSERT(flags & XLOG_COMMIT_TRANS);
1858 *commit_iclog = iclog;
1859 }
1860
1861 return 0;
1862}
1863
1864/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 * Write some region out to in-core log
1866 *
1867 * This will be called when writing externally provided regions or when
1868 * writing out a commit record for a given transaction.
1869 *
1870 * General algorithm:
1871 * 1. Find total length of this write. This may include adding to the
1872 * lengths passed in.
1873 * 2. Check whether we violate the tickets reservation.
1874 * 3. While writing to this iclog
1875 * A. Reserve as much space in this iclog as can get
1876 * B. If this is first write, save away start lsn
1877 * C. While writing this region:
1878 * 1. If first write of transaction, write start record
1879 * 2. Write log operation header (header per region)
1880 * 3. Find out if we can fit entire region into this iclog
1881 * 4. Potentially, verify destination memcpy ptr
1882 * 5. Memcpy (partial) region
1883 * 6. If partial copy, release iclog; otherwise, continue
1884 * copying more regions into current iclog
1885 * 4. Mark want sync bit (in simulation mode)
1886 * 5. Release iclog for potential flush to on-disk log.
1887 *
1888 * ERRORS:
1889 * 1. Panic if reservation is overrun. This should never happen since
1890 * reservation amounts are generated internal to the filesystem.
1891 * NOTES:
1892 * 1. Tickets are single threaded data structures.
1893 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1894 * syncing routine. When a single log_write region needs to span
1895 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1896 * on all log operation writes which don't contain the end of the
1897 * region. The XLOG_END_TRANS bit is used for the in-core log
1898 * operation which contains the end of the continued log_write region.
1899 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1900 * we don't really know exactly how much space will be used. As a result,
1901 * we don't update ic_offset until the end when we know exactly how many
1902 * bytes have been written out.
1903 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001904int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001905xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001906 struct log *log,
1907 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001908 struct xlog_ticket *ticket,
1909 xfs_lsn_t *start_lsn,
1910 struct xlog_in_core **commit_iclog,
1911 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001913 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001914 struct xfs_log_iovec *vecp;
1915 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001916 int len;
1917 int index;
1918 int partial_copy = 0;
1919 int partial_copy_len = 0;
1920 int contwr = 0;
1921 int record_cnt = 0;
1922 int data_cnt = 0;
1923 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001925 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Dave Chinner55b66332010-03-23 11:43:17 +11001927 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001928
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001929 /*
1930 * Region headers and bytes are already accounted for.
1931 * We only need to take into account start records and
1932 * split regions in this function.
1933 */
1934 if (ticket->t_flags & XLOG_TIC_INITED)
1935 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1936
1937 /*
1938 * Commit record headers need to be accounted for. These
1939 * come in as separate writes so are easy to detect.
1940 */
1941 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1942 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Dave Chinner71e330b2010-05-21 14:37:18 +10001943
1944 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001945 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Dave Chinner55b66332010-03-23 11:43:17 +11001947 index = 0;
1948 lv = log_vector;
1949 vecp = lv->lv_iovecp;
1950 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001951 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001952 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001954 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1955 &contwr, &log_offset);
1956 if (error)
1957 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001959 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001960 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001962 /* start_lsn is the first lsn written to. That's all we need. */
1963 if (!*start_lsn)
1964 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001966 /*
1967 * This loop writes out as many regions as can fit in the amount
1968 * of space which was allocated by xlog_state_get_iclog_space().
1969 */
Dave Chinner55b66332010-03-23 11:43:17 +11001970 while (lv && index < lv->lv_niovecs) {
1971 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001972 struct xlog_op_header *ophdr;
1973 int start_rec_copy;
1974 int copy_len;
1975 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Dave Chinner55b66332010-03-23 11:43:17 +11001977 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001978 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001980 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1981 if (start_rec_copy) {
1982 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001983 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001984 start_rec_copy);
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001987 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1988 if (!ophdr)
1989 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001991 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001992 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001994 len += xlog_write_setup_copy(ticket, ophdr,
1995 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001996 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001997 &copy_off, &copy_len,
1998 &partial_copy,
1999 &partial_copy_len);
2000 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002001
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002002 /* copy region */
2003 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11002004 memcpy(ptr, reg->i_addr + copy_off, copy_len);
2005 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11002006
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002007 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
2008 record_cnt++;
2009 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002011 error = xlog_write_copy_finish(log, iclog, flags,
2012 &record_cnt, &data_cnt,
2013 &partial_copy,
2014 &partial_copy_len,
2015 log_offset,
2016 commit_iclog);
2017 if (error)
2018 return error;
2019
2020 /*
2021 * if we had a partial copy, we need to get more iclog
2022 * space but we don't want to increment the region
2023 * index because there is still more is this region to
2024 * write.
2025 *
2026 * If we completed writing this region, and we flushed
2027 * the iclog (indicated by resetting of the record
2028 * count), then we also need to get more log space. If
2029 * this was the last record, though, we are done and
2030 * can just return.
2031 */
2032 if (partial_copy)
2033 break;
2034
Dave Chinner55b66332010-03-23 11:43:17 +11002035 if (++index == lv->lv_niovecs) {
2036 lv = lv->lv_next;
2037 index = 0;
2038 if (lv)
2039 vecp = lv->lv_iovecp;
2040 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002041 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11002042 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002043 return 0;
2044 break;
2045 }
2046 }
2047 }
2048
2049 ASSERT(len == 0);
2050
2051 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2052 if (!commit_iclog)
2053 return xlog_state_release_iclog(log, iclog);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 ASSERT(flags & XLOG_COMMIT_TRANS);
2056 *commit_iclog = iclog;
2057 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060
2061/*****************************************************************************
2062 *
2063 * State Machine functions
2064 *
2065 *****************************************************************************
2066 */
2067
2068/* Clean iclogs starting from the head. This ordering must be
2069 * maintained, so an iclog doesn't become ACTIVE beyond one that
2070 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002071 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 * iclog is trying to sync to disk.
2073 *
2074 * State Change: DIRTY -> ACTIVE
2075 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002076STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077xlog_state_clean_log(xlog_t *log)
2078{
2079 xlog_in_core_t *iclog;
2080 int changed = 0;
2081
2082 iclog = log->l_iclog;
2083 do {
2084 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2085 iclog->ic_state = XLOG_STATE_ACTIVE;
2086 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002087 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
2089 * If the number of ops in this iclog indicate it just
2090 * contains the dummy transaction, we can
2091 * change state into IDLE (the second time around).
2092 * Otherwise we should change the state into
2093 * NEED a dummy.
2094 * We don't need to cover the dummy.
2095 */
2096 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002097 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2098 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 changed = 1;
2100 } else {
2101 /*
2102 * We have two dirty iclogs so start over
2103 * This could also be num of ops indicates
2104 * this is not the dummy going out.
2105 */
2106 changed = 2;
2107 }
2108 iclog->ic_header.h_num_logops = 0;
2109 memset(iclog->ic_header.h_cycle_data, 0,
2110 sizeof(iclog->ic_header.h_cycle_data));
2111 iclog->ic_header.h_lsn = 0;
2112 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2113 /* do nothing */;
2114 else
2115 break; /* stop cleaning */
2116 iclog = iclog->ic_next;
2117 } while (iclog != log->l_iclog);
2118
2119 /* log is locked when we are called */
2120 /*
2121 * Change state for the dummy log recording.
2122 * We usually go to NEED. But we go to NEED2 if the changed indicates
2123 * we are done writing the dummy record.
2124 * If we are done with the second dummy recored (DONE2), then
2125 * we go to IDLE.
2126 */
2127 if (changed) {
2128 switch (log->l_covered_state) {
2129 case XLOG_STATE_COVER_IDLE:
2130 case XLOG_STATE_COVER_NEED:
2131 case XLOG_STATE_COVER_NEED2:
2132 log->l_covered_state = XLOG_STATE_COVER_NEED;
2133 break;
2134
2135 case XLOG_STATE_COVER_DONE:
2136 if (changed == 1)
2137 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2138 else
2139 log->l_covered_state = XLOG_STATE_COVER_NEED;
2140 break;
2141
2142 case XLOG_STATE_COVER_DONE2:
2143 if (changed == 1)
2144 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2145 else
2146 log->l_covered_state = XLOG_STATE_COVER_NEED;
2147 break;
2148
2149 default:
2150 ASSERT(0);
2151 }
2152 }
2153} /* xlog_state_clean_log */
2154
2155STATIC xfs_lsn_t
2156xlog_get_lowest_lsn(
2157 xlog_t *log)
2158{
2159 xlog_in_core_t *lsn_log;
2160 xfs_lsn_t lowest_lsn, lsn;
2161
2162 lsn_log = log->l_iclog;
2163 lowest_lsn = 0;
2164 do {
2165 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002166 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if ((lsn && !lowest_lsn) ||
2168 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2169 lowest_lsn = lsn;
2170 }
2171 }
2172 lsn_log = lsn_log->ic_next;
2173 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002174 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177
2178STATIC void
2179xlog_state_do_callback(
2180 xlog_t *log,
2181 int aborted,
2182 xlog_in_core_t *ciclog)
2183{
2184 xlog_in_core_t *iclog;
2185 xlog_in_core_t *first_iclog; /* used to know when we've
2186 * processed all iclogs once */
2187 xfs_log_callback_t *cb, *cb_next;
2188 int flushcnt = 0;
2189 xfs_lsn_t lowest_lsn;
2190 int ioerrors; /* counter: iclogs with errors */
2191 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2192 int funcdidcallbacks; /* flag: function did callbacks */
2193 int repeats; /* for issuing console warnings if
2194 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002195 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002197 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 first_iclog = iclog = log->l_iclog;
2199 ioerrors = 0;
2200 funcdidcallbacks = 0;
2201 repeats = 0;
2202
2203 do {
2204 /*
2205 * Scan all iclogs starting with the one pointed to by the
2206 * log. Reset this starting point each time the log is
2207 * unlocked (during callbacks).
2208 *
2209 * Keep looping through iclogs until one full pass is made
2210 * without running any callbacks.
2211 */
2212 first_iclog = log->l_iclog;
2213 iclog = log->l_iclog;
2214 loopdidcallbacks = 0;
2215 repeats++;
2216
2217 do {
2218
2219 /* skip all iclogs in the ACTIVE & DIRTY states */
2220 if (iclog->ic_state &
2221 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2222 iclog = iclog->ic_next;
2223 continue;
2224 }
2225
2226 /*
2227 * Between marking a filesystem SHUTDOWN and stopping
2228 * the log, we do flush all iclogs to disk (if there
2229 * wasn't a log I/O error). So, we do want things to
2230 * go smoothly in case of just a SHUTDOWN w/o a
2231 * LOG_IO_ERROR.
2232 */
2233 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2234 /*
2235 * Can only perform callbacks in order. Since
2236 * this iclog is not in the DONE_SYNC/
2237 * DO_CALLBACK state, we skip the rest and
2238 * just try to clean up. If we set our iclog
2239 * to DO_CALLBACK, we will not process it when
2240 * we retry since a previous iclog is in the
2241 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002242 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
2244 if (!(iclog->ic_state &
2245 (XLOG_STATE_DONE_SYNC |
2246 XLOG_STATE_DO_CALLBACK))) {
2247 if (ciclog && (ciclog->ic_state ==
2248 XLOG_STATE_DONE_SYNC)) {
2249 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2250 }
2251 break;
2252 }
2253 /*
2254 * We now have an iclog that is in either the
2255 * DO_CALLBACK or DONE_SYNC states. The other
2256 * states (WANT_SYNC, SYNCING, or CALLBACK were
2257 * caught by the above if and are going to
2258 * clean (i.e. we aren't doing their callbacks)
2259 * see the above if.
2260 */
2261
2262 /*
2263 * We will do one more check here to see if we
2264 * have chased our tail around.
2265 */
2266
2267 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002268 if (lowest_lsn &&
2269 XFS_LSN_CMP(lowest_lsn,
Dave Chinner84f3c682010-12-03 22:11:29 +11002270 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 iclog = iclog->ic_next;
2272 continue; /* Leave this iclog for
2273 * another thread */
2274 }
2275
2276 iclog->ic_state = XLOG_STATE_CALLBACK;
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Dave Chinner84f3c682010-12-03 22:11:29 +11002279 /*
2280 * update the last_sync_lsn before we drop the
2281 * icloglock to ensure we are the only one that
2282 * can update it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 */
Dave Chinner84f3c682010-12-03 22:11:29 +11002284 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2285 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2286 atomic64_set(&log->l_last_sync_lsn,
2287 be64_to_cpu(iclog->ic_header.h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Dave Chinner84f3c682010-12-03 22:11:29 +11002289 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 ioerrors++;
Dave Chinner84f3c682010-12-03 22:11:29 +11002291
2292 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
David Chinner114d23a2008-04-10 12:18:39 +10002294 /*
2295 * Keep processing entries in the callback list until
2296 * we come around and it is empty. We need to
2297 * atomically see that the list is empty and change the
2298 * state to DIRTY so that we don't miss any more
2299 * callbacks being added.
2300 */
2301 spin_lock(&iclog->ic_callback_lock);
2302 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002303 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 iclog->ic_callback_tail = &(iclog->ic_callback);
2305 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002306 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002309 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 cb_next = cb->cb_next;
2311 cb->cb_func(cb->cb_arg, aborted);
2312 }
David Chinner114d23a2008-04-10 12:18:39 +10002313 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 cb = iclog->ic_callback;
2315 }
2316
2317 loopdidcallbacks++;
2318 funcdidcallbacks++;
2319
David Chinner114d23a2008-04-10 12:18:39 +10002320 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002321 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002322 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2324 iclog->ic_state = XLOG_STATE_DIRTY;
2325
2326 /*
2327 * Transition from DIRTY to ACTIVE if applicable.
2328 * NOP if STATE_IOERROR.
2329 */
2330 xlog_state_clean_log(log);
2331
2332 /* wake up threads waiting in xfs_log_force() */
Dave Chinnereb40a872010-12-21 12:09:01 +11002333 wake_up_all(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 iclog = iclog->ic_next;
2336 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002337
2338 if (repeats > 5000) {
2339 flushcnt += repeats;
2340 repeats = 0;
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002341 xfs_warn(log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002342 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002343 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345 } while (!ioerrors && loopdidcallbacks);
2346
2347 /*
2348 * make one last gasp attempt to see if iclogs are being left in
2349 * limbo..
2350 */
2351#ifdef DEBUG
2352 if (funcdidcallbacks) {
2353 first_iclog = iclog = log->l_iclog;
2354 do {
2355 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2356 /*
2357 * Terminate the loop if iclogs are found in states
2358 * which will cause other threads to clean up iclogs.
2359 *
2360 * SYNCING - i/o completion will go through logs
2361 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002362 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 * IOERROR - give up hope all ye who enter here
2364 */
2365 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2366 iclog->ic_state == XLOG_STATE_SYNCING ||
2367 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2368 iclog->ic_state == XLOG_STATE_IOERROR )
2369 break;
2370 iclog = iclog->ic_next;
2371 } while (first_iclog != iclog);
2372 }
2373#endif
2374
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002375 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2376 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002377 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002378
2379 if (wake)
Dave Chinnereb40a872010-12-21 12:09:01 +11002380 wake_up_all(&log->l_flush_wait);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383
2384/*
2385 * Finish transitioning this iclog to the dirty state.
2386 *
2387 * Make sure that we completely execute this routine only when this is
2388 * the last call to the iclog. There is a good chance that iclog flushes,
2389 * when we reach the end of the physical log, get turned into 2 separate
2390 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2391 * routine. By using the reference count bwritecnt, we guarantee that only
2392 * the second completion goes through.
2393 *
2394 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002395 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
David Chinnera8272ce2007-11-23 16:28:09 +11002397STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398xlog_state_done_syncing(
2399 xlog_in_core_t *iclog,
2400 int aborted)
2401{
2402 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002404 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2407 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002408 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2410
2411
2412 /*
2413 * If we got an error, either on the first buffer, or in the case of
2414 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2415 * and none should ever be attempted to be written to disk
2416 * again.
2417 */
2418 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2419 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002420 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return;
2422 }
2423 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2424 }
2425
2426 /*
2427 * Someone could be sleeping prior to writing out the next
2428 * iclog buffer, we wake them all, one will get to do the
2429 * I/O, the others get to wait for the result.
2430 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002431 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002432 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2434} /* xlog_state_done_syncing */
2435
2436
2437/*
2438 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002439 * sleep. We wait on the flush queue on the head iclog as that should be
2440 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2441 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 *
2443 * The in-core logs are used in a circular fashion. They are not used
2444 * out-of-order even when an iclog past the head is free.
2445 *
2446 * return:
2447 * * log_offset where xlog_write() can start writing into the in-core
2448 * log's data space.
2449 * * in-core log pointer to which xlog_write() should write.
2450 * * boolean indicating this is a continued write to an in-core log.
2451 * If this is the last write, then the in-core log's offset field
2452 * needs to be incremented, depending on the amount of data which
2453 * is copied.
2454 */
David Chinnera8272ce2007-11-23 16:28:09 +11002455STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456xlog_state_get_iclog_space(xlog_t *log,
2457 int len,
2458 xlog_in_core_t **iclogp,
2459 xlog_ticket_t *ticket,
2460 int *continued_write,
2461 int *logoffsetp)
2462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 int log_offset;
2464 xlog_rec_header_t *head;
2465 xlog_in_core_t *iclog;
2466 int error;
2467
2468restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002469 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002471 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return XFS_ERROR(EIO);
2473 }
2474
2475 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002476 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002478
2479 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002480 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 goto restart;
2482 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 head = &iclog->ic_header;
2485
David Chinner155cc6b2008-03-06 13:44:14 +11002486 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 log_offset = iclog->ic_offset;
2488
2489 /* On the 1st write to an iclog, figure out lsn. This works
2490 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2491 * committing to. If the offset is set, that's how many blocks
2492 * must be written.
2493 */
2494 if (log_offset == 0) {
2495 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002496 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002497 log->l_iclog_hsize,
2498 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002499 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2500 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002501 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 ASSERT(log->l_curr_block >= 0);
2503 }
2504
2505 /* If there is enough room to write everything, then do it. Otherwise,
2506 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2507 * bit is on, so this will get flushed out. Don't update ic_offset
2508 * until you know exactly how many bytes get copied. Therefore, wait
2509 * until later to update ic_offset.
2510 *
2511 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2512 * can fit into remaining data section.
2513 */
2514 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2515 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2516
Dave Chinner49641f12008-07-11 17:43:55 +10002517 /*
2518 * If I'm the only one writing to this iclog, sync it to disk.
2519 * We need to do an atomic compare and decrement here to avoid
2520 * racing with concurrent atomic_dec_and_lock() calls in
2521 * xlog_state_release_iclog() when there is more than one
2522 * reference to the iclog.
2523 */
2524 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2525 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002526 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002527 error = xlog_state_release_iclog(log, iclog);
2528 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002529 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002531 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533 goto restart;
2534 }
2535
2536 /* Do we have enough room to write the full amount in the remainder
2537 * of this iclog? Or must we continue a write on the next iclog and
2538 * mark this iclog as completely taken? In the case where we switch
2539 * iclogs (to mark it taken), this particular iclog will release/sync
2540 * to disk in xlog_write().
2541 */
2542 if (len <= iclog->ic_size - iclog->ic_offset) {
2543 *continued_write = 0;
2544 iclog->ic_offset += len;
2545 } else {
2546 *continued_write = 1;
2547 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2548 }
2549 *iclogp = iclog;
2550
2551 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002552 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 *logoffsetp = log_offset;
2555 return 0;
2556} /* xlog_state_get_iclog_space */
2557
2558/*
2559 * Atomically get the log space required for a log ticket.
2560 *
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002561 * Once a ticket gets put onto the reserveq, it will only return after the
2562 * needed reservation is satisfied.
Dave Chinner3f16b982010-12-21 12:29:01 +11002563 *
2564 * This function is structured so that it has a lock free fast path. This is
2565 * necessary because every new transaction reservation will come through this
2566 * path. Hence any lock will be globally hot if we take it unconditionally on
2567 * every pass.
2568 *
2569 * As tickets are only ever moved on and off the reserveq under the
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002570 * l_grant_reserve_lock, we only need to take that lock if we are going to add
2571 * the ticket to the queue and sleep. We can avoid taking the lock if the ticket
2572 * was never added to the reserveq because the t_queue list head will be empty
2573 * and we hold the only reference to it so it can safely be checked unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 */
2575STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002576xlog_grant_log_space(
2577 struct log *log,
2578 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002580 int free_bytes, need_bytes;
2581 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002583 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002585 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002587 /*
2588 * If there are other waiters on the queue then give them a chance at
2589 * logspace before us. Wake up the first waiters, if we do not wake
2590 * up all the waiters then go to sleep waiting for more free space,
2591 * otherwise try to get some space for this transaction.
2592 */
Dave Chinner3f16b982010-12-21 12:29:01 +11002593 need_bytes = tic->t_unit_res;
2594 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2595 need_bytes *= tic->t_ocnt;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002596 free_bytes = xlog_space_left(log, &log->l_grant_reserve_head);
Dave Chinner3f16b982010-12-21 12:29:01 +11002597 if (!list_empty_careful(&log->l_reserveq)) {
2598 spin_lock(&log->l_grant_reserve_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002599 if (!xlog_reserveq_wake(log, &free_bytes) ||
2600 free_bytes < need_bytes)
2601 error = xlog_reserveq_wait(log, tic, need_bytes);
2602 spin_unlock(&log->l_grant_reserve_lock);
2603 } else if (free_bytes < need_bytes) {
Dave Chinner3f16b982010-12-21 12:29:01 +11002604 spin_lock(&log->l_grant_reserve_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002605 error = xlog_reserveq_wait(log, tic, need_bytes);
Dave Chinner3f16b982010-12-21 12:29:01 +11002606 spin_unlock(&log->l_grant_reserve_lock);
2607 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002608 if (error)
2609 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Dave Chinnera69ed032010-12-21 12:08:20 +11002611 xlog_grant_add_space(log, &log->l_grant_reserve_head, need_bytes);
2612 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002613 trace_xfs_log_grant_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002614 xlog_verify_grant_tail(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618/*
2619 * Replenish the byte reservation required by moving the grant write head.
2620 *
Dave Chinner3f16b982010-12-21 12:29:01 +11002621 * Similar to xlog_grant_log_space, the function is structured to have a lock
2622 * free fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 */
2624STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002625xlog_regrant_write_log_space(
2626 struct log *log,
2627 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002629 int free_bytes, need_bytes;
2630 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002633 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002638 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002640 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002642 /*
2643 * If there are other waiters on the queue then give them a chance at
2644 * logspace before us. Wake up the first waiters, if we do not wake
2645 * up all the waiters then go to sleep waiting for more free space,
2646 * otherwise try to get some space for this transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002648 need_bytes = tic->t_unit_res;
Dave Chinnera69ed032010-12-21 12:08:20 +11002649 free_bytes = xlog_space_left(log, &log->l_grant_write_head);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002650 if (!list_empty_careful(&log->l_writeq)) {
Dave Chinner3f16b982010-12-21 12:29:01 +11002651 spin_lock(&log->l_grant_write_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002652 if (!xlog_writeq_wake(log, &free_bytes) ||
2653 free_bytes < need_bytes)
2654 error = xlog_writeq_wait(log, tic, need_bytes);
2655 spin_unlock(&log->l_grant_write_lock);
2656 } else if (free_bytes < need_bytes) {
Dave Chinner3f16b982010-12-21 12:29:01 +11002657 spin_lock(&log->l_grant_write_lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002658 error = xlog_writeq_wait(log, tic, need_bytes);
Dave Chinner3f16b982010-12-21 12:29:01 +11002659 spin_unlock(&log->l_grant_write_lock);
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002662 if (error)
2663 return error;
2664
Dave Chinnera69ed032010-12-21 12:08:20 +11002665 xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002666 trace_xfs_log_regrant_write_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002667 xlog_verify_grant_tail(log);
Jesper Juhl014c2542006-01-15 02:37:08 +01002668 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671/* The first cnt-1 times through here we don't need to
2672 * move the grant write head because the permanent
2673 * reservation has reserved cnt times the unit amount.
2674 * Release part of current permanent unit reservation and
2675 * reset current reservation to be one units worth. Also
2676 * move grant reservation head forward.
2677 */
2678STATIC void
2679xlog_regrant_reserve_log_space(xlog_t *log,
2680 xlog_ticket_t *ticket)
2681{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002682 trace_xfs_log_regrant_reserve_enter(log, ticket);
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (ticket->t_cnt > 0)
2685 ticket->t_cnt--;
2686
Dave Chinnera69ed032010-12-21 12:08:20 +11002687 xlog_grant_sub_space(log, &log->l_grant_reserve_head,
2688 ticket->t_curr_res);
2689 xlog_grant_sub_space(log, &log->l_grant_write_head,
2690 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002692 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002693
2694 trace_xfs_log_regrant_reserve_sub(log, ticket);
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 /* just return if we still have some of the pre-reserved space */
Dave Chinnerd0eb2f32010-12-21 12:29:14 +11002697 if (ticket->t_cnt > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Dave Chinnera69ed032010-12-21 12:08:20 +11002700 xlog_grant_add_space(log, &log->l_grant_reserve_head,
2701 ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002702
2703 trace_xfs_log_regrant_reserve_exit(log, ticket);
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002706 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707} /* xlog_regrant_reserve_log_space */
2708
2709
2710/*
2711 * Give back the space left from a reservation.
2712 *
2713 * All the information we need to make a correct determination of space left
2714 * is present. For non-permanent reservations, things are quite easy. The
2715 * count should have been decremented to zero. We only need to deal with the
2716 * space remaining in the current reservation part of the ticket. If the
2717 * ticket contains a permanent reservation, there may be left over space which
2718 * needs to be released. A count of N means that N-1 refills of the current
2719 * reservation can be done before we need to ask for more space. The first
2720 * one goes to fill up the first current reservation. Once we run out of
2721 * space, the count will stay at zero and the only space remaining will be
2722 * in the current reservation field.
2723 */
2724STATIC void
2725xlog_ungrant_log_space(xlog_t *log,
2726 xlog_ticket_t *ticket)
2727{
Dave Chinner663e4962010-12-21 12:06:05 +11002728 int bytes;
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (ticket->t_cnt > 0)
2731 ticket->t_cnt--;
2732
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002733 trace_xfs_log_ungrant_enter(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002734 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Dave Chinner663e4962010-12-21 12:06:05 +11002736 /*
2737 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * up more space based on the remaining count.
2739 */
Dave Chinner663e4962010-12-21 12:06:05 +11002740 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (ticket->t_cnt > 0) {
2742 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11002743 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745
Dave Chinnera69ed032010-12-21 12:08:20 +11002746 xlog_grant_sub_space(log, &log->l_grant_reserve_head, bytes);
2747 xlog_grant_sub_space(log, &log->l_grant_write_head, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11002748
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002749 trace_xfs_log_ungrant_exit(log, ticket);
2750
Christoph Hellwig09a423a2012-02-20 02:31:20 +00002751 xfs_log_space_wake(log->l_mp, true);
2752}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 * Flush iclog to disk if this is the last reference to the given iclog and
2756 * the WANT_SYNC bit is set.
2757 *
2758 * When this function is entered, the iclog is not necessarily in the
2759 * WANT_SYNC state. It may be sitting around waiting to get filled.
2760 *
2761 *
2762 */
David Chinnera8272ce2007-11-23 16:28:09 +11002763STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002764xlog_state_release_iclog(
2765 xlog_t *log,
2766 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 int sync = 0; /* do we sync? */
2769
David Chinner155cc6b2008-03-06 13:44:14 +11002770 if (iclog->ic_state & XLOG_STATE_IOERROR)
2771 return XFS_ERROR(EIO);
2772
2773 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2774 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2775 return 0;
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002778 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return XFS_ERROR(EIO);
2780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2782 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2783
David Chinner155cc6b2008-03-06 13:44:14 +11002784 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002785 /* update tail before writing to iclog */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002786 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 sync++;
2788 iclog->ic_state = XLOG_STATE_SYNCING;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002789 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
2790 xlog_verify_tail_lsn(log, iclog, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /* cycle incremented when incrementing curr_block */
2792 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002793 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 /*
2796 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002797 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2799 * this iclog has consistent data, so we ignore IOERROR
2800 * flags after this point.
2801 */
David Chinnerb5893342008-03-06 13:44:06 +11002802 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805} /* xlog_state_release_iclog */
2806
2807
2808/*
2809 * This routine will mark the current iclog in the ring as WANT_SYNC
2810 * and move the current iclog pointer to the next iclog in the ring.
2811 * When this routine is called from xlog_state_get_iclog_space(), the
2812 * exact size of the iclog has not yet been determined. All we know is
2813 * that every data block. We have run out of space in this log record.
2814 */
2815STATIC void
2816xlog_state_switch_iclogs(xlog_t *log,
2817 xlog_in_core_t *iclog,
2818 int eventual_size)
2819{
2820 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2821 if (!eventual_size)
2822 eventual_size = iclog->ic_offset;
2823 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002824 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 log->l_prev_block = log->l_curr_block;
2826 log->l_prev_cycle = log->l_curr_cycle;
2827
2828 /* roll log?: ic_offset changed later */
2829 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2830
2831 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002832 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 log->l_mp->m_sb.sb_logsunit > 1) {
2834 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2835 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2836 }
2837
2838 if (log->l_curr_block >= log->l_logBBsize) {
2839 log->l_curr_cycle++;
2840 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2841 log->l_curr_cycle++;
2842 log->l_curr_block -= log->l_logBBsize;
2843 ASSERT(log->l_curr_block >= 0);
2844 }
2845 ASSERT(iclog == log->l_iclog);
2846 log->l_iclog = iclog->ic_next;
2847} /* xlog_state_switch_iclogs */
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/*
2850 * Write out all data in the in-core log as of this exact moment in time.
2851 *
2852 * Data may be written to the in-core log during this call. However,
2853 * we don't guarantee this data will be written out. A change from past
2854 * implementation means this routine will *not* write out zero length LRs.
2855 *
2856 * Basically, we try and perform an intelligent scan of the in-core logs.
2857 * If we determine there is no flushable data, we just return. There is no
2858 * flushable data if:
2859 *
2860 * 1. the current iclog is active and has no data; the previous iclog
2861 * is in the active or dirty state.
2862 * 2. the current iclog is drity, and the previous iclog is in the
2863 * active or dirty state.
2864 *
David Chinner12017fa2008-08-13 16:34:31 +10002865 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 *
2867 * 1. the current iclog is not in the active nor dirty state.
2868 * 2. the current iclog dirty, and the previous iclog is not in the
2869 * active nor dirty state.
2870 * 3. the current iclog is active, and there is another thread writing
2871 * to this particular iclog.
2872 * 4. a) the current iclog is active and has no other writers
2873 * b) when we return from flushing out this iclog, it is still
2874 * not in the active nor dirty state.
2875 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002876int
2877_xfs_log_force(
2878 struct xfs_mount *mp,
2879 uint flags,
2880 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002882 struct log *log = mp->m_log;
2883 struct xlog_in_core *iclog;
2884 xfs_lsn_t lsn;
2885
2886 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002888 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002889
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002890 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 iclog = log->l_iclog;
2893 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002894 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return XFS_ERROR(EIO);
2896 }
2897
2898 /* If the head iclog is not active nor dirty, we just attach
2899 * ourselves to the head and go to sleep.
2900 */
2901 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2902 iclog->ic_state == XLOG_STATE_DIRTY) {
2903 /*
2904 * If the head is dirty or (active and empty), then
2905 * we need to look at the previous iclog. If the previous
2906 * iclog is active or dirty we are done. There is nothing
2907 * to sync out. Otherwise, we attach ourselves to the
2908 * previous iclog and go to sleep.
2909 */
2910 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002911 (atomic_read(&iclog->ic_refcnt) == 0
2912 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 iclog = iclog->ic_prev;
2914 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2915 iclog->ic_state == XLOG_STATE_DIRTY)
2916 goto no_sleep;
2917 else
2918 goto maybe_sleep;
2919 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002920 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 /* We are the only one with access to this
2922 * iclog. Flush it out now. There should
2923 * be a roundoff of zero to show that someone
2924 * has already taken care of the roundoff from
2925 * the previous sync.
2926 */
David Chinner155cc6b2008-03-06 13:44:14 +11002927 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002928 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002930 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 if (xlog_state_release_iclog(log, iclog))
2933 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002934
2935 if (log_flushed)
2936 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002937 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002938 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 iclog->ic_state != XLOG_STATE_DIRTY)
2940 goto maybe_sleep;
2941 else
2942 goto no_sleep;
2943 } else {
2944 /* Someone else is writing to this iclog.
2945 * Use its call to flush out the data. However,
2946 * the other thread may not force out this LR,
2947 * so we mark it WANT_SYNC.
2948 */
2949 xlog_state_switch_iclogs(log, iclog, 0);
2950 goto maybe_sleep;
2951 }
2952 }
2953 }
2954
2955 /* By the time we come around again, the iclog could've been filled
2956 * which would give it another lsn. If we have a new lsn, just
2957 * return because the relevant data has been flushed.
2958 */
2959maybe_sleep:
2960 if (flags & XFS_LOG_SYNC) {
2961 /*
2962 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002963 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 * Then we check again after waking up, in case our
2965 * sleep was disturbed by a bad news.
2966 */
2967 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002968 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return XFS_ERROR(EIO);
2970 }
2971 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11002972 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 /*
2974 * No need to grab the log lock here since we're
2975 * only deciding whether or not to return EIO
2976 * and the memory read should be atomic.
2977 */
2978 if (iclog->ic_state & XLOG_STATE_IOERROR)
2979 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002980 if (log_flushed)
2981 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 } else {
2983
2984no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002985 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002988}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002991 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2992 * about errors or whether the log was flushed or not. This is the normal
2993 * interface to use when trying to unpin items or move the log forward.
2994 */
2995void
2996xfs_log_force(
2997 xfs_mount_t *mp,
2998 uint flags)
2999{
3000 int error;
3001
3002 error = _xfs_log_force(mp, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003003 if (error)
3004 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003005}
3006
3007/*
3008 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 *
3010 * Find in-core log with lsn.
3011 * If it is in the DIRTY state, just return.
3012 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
3013 * state and go to sleep or return.
3014 * If it is in any other state, go to sleep or return.
3015 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003016 * Synchronous forces are implemented with a signal variable. All callers
3017 * to force a given lsn to disk will wait on a the sv attached to the
3018 * specific in-core log. When given in-core log finally completes its
3019 * write to disk, that thread will wake up all threads waiting on the
3020 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003022int
3023_xfs_log_force_lsn(
3024 struct xfs_mount *mp,
3025 xfs_lsn_t lsn,
3026 uint flags,
3027 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003029 struct log *log = mp->m_log;
3030 struct xlog_in_core *iclog;
3031 int already_slept = 0;
3032
3033 ASSERT(lsn != 0);
3034
3035 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003037 lsn = xlog_cil_force_lsn(log, lsn);
3038 if (lsn == NULLCOMMITLSN)
3039 return 0;
Dave Chinner71e330b2010-05-21 14:37:18 +10003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003042 spin_lock(&log->l_icloglock);
3043 iclog = log->l_iclog;
3044 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003045 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003046 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003049 do {
3050 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3051 iclog = iclog->ic_next;
3052 continue;
3053 }
3054
3055 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3056 spin_unlock(&log->l_icloglock);
3057 return 0;
3058 }
3059
3060 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3061 /*
3062 * We sleep here if we haven't already slept (e.g.
3063 * this is the first time we've looked at the correct
3064 * iclog buf) and the buffer before us is going to
3065 * be sync'ed. The reason for this is that if we
3066 * are doing sync transactions here, by waiting for
3067 * the previous I/O to complete, we can allow a few
3068 * more transactions into this iclog before we close
3069 * it down.
3070 *
3071 * Otherwise, we mark the buffer WANT_SYNC, and bump
3072 * up the refcnt so we can release the log (which
3073 * drops the ref count). The state switch keeps new
3074 * transaction commits from using this buffer. When
3075 * the current commits finish writing into the buffer,
3076 * the refcount will drop to zero and the buffer will
3077 * go out then.
3078 */
3079 if (!already_slept &&
3080 (iclog->ic_prev->ic_state &
3081 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3082 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3083
3084 XFS_STATS_INC(xs_log_force_sleep);
3085
Dave Chinnereb40a872010-12-21 12:09:01 +11003086 xlog_wait(&iclog->ic_prev->ic_write_wait,
3087 &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003088 if (log_flushed)
3089 *log_flushed = 1;
3090 already_slept = 1;
3091 goto try_again;
3092 }
David Chinner155cc6b2008-03-06 13:44:14 +11003093 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003095 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (xlog_state_release_iclog(log, iclog))
3097 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003098 if (log_flushed)
3099 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003100 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003103 if ((flags & XFS_LOG_SYNC) && /* sleep */
3104 !(iclog->ic_state &
3105 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3106 /*
3107 * Don't wait on completion if we know that we've
3108 * gotten a log write error.
3109 */
3110 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3111 spin_unlock(&log->l_icloglock);
3112 return XFS_ERROR(EIO);
3113 }
3114 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003115 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003116 /*
3117 * No need to grab the log lock here since we're
3118 * only deciding whether or not to return EIO
3119 * and the memory read should be atomic.
3120 */
3121 if (iclog->ic_state & XLOG_STATE_IOERROR)
3122 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003124 if (log_flushed)
3125 *log_flushed = 1;
3126 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003127 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003129
3130 return 0;
3131 } while (iclog != log->l_iclog);
3132
3133 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003137/*
3138 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3139 * about errors or whether the log was flushed or not. This is the normal
3140 * interface to use when trying to unpin items or move the log forward.
3141 */
3142void
3143xfs_log_force_lsn(
3144 xfs_mount_t *mp,
3145 xfs_lsn_t lsn,
3146 uint flags)
3147{
3148 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003150 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003151 if (error)
3152 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155/*
3156 * Called when we want to mark the current iclog as being ready to sync to
3157 * disk.
3158 */
David Chinnera8272ce2007-11-23 16:28:09 +11003159STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3161{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003162 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3165 xlog_state_switch_iclogs(log, iclog, 0);
3166 } else {
3167 ASSERT(iclog->ic_state &
3168 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3169 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172
3173/*****************************************************************************
3174 *
3175 * TICKET functions
3176 *
3177 *****************************************************************************
3178 */
3179
3180/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003181 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003183void
3184xfs_log_ticket_put(
3185 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003187 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003188 if (atomic_dec_and_test(&ticket->t_ref))
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003189 kmem_zone_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003192xlog_ticket_t *
3193xfs_log_ticket_get(
3194 xlog_ticket_t *ticket)
3195{
3196 ASSERT(atomic_read(&ticket->t_ref) > 0);
3197 atomic_inc(&ticket->t_ref);
3198 return ticket;
3199}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003202 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003204xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003205xlog_ticket_alloc(
3206 struct log *log,
3207 int unit_bytes,
3208 int cnt,
3209 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003210 uint xflags,
3211 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003213 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003215 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Dave Chinner3383ca52010-05-07 11:04:17 +10003217 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003218 if (!tic)
3219 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 /*
3222 * Permanent reservations have up to 'cnt'-1 active log operations
3223 * in the log. A unit in this case is the amount of space for one
3224 * of these log operations. Normal reservations have a cnt of 1
3225 * and their unit amount is the total amount of space required.
3226 *
3227 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003228 * which occupy space in the on-disk log.
3229 *
3230 * Normal form of a transaction is:
3231 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3232 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3233 *
3234 * We need to account for all the leadup data and trailer data
3235 * around the transaction data.
3236 * And then we need to account for the worst case in terms of using
3237 * more space.
3238 * The worst case will happen if:
3239 * - the placement of the transaction happens to be such that the
3240 * roundoff is at its maximum
3241 * - the transaction data is synced before the commit record is synced
3242 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3243 * Therefore the commit record is in its own Log Record.
3244 * This can happen as the commit record is called with its
3245 * own region to xlog_write().
3246 * This then means that in the worst case, roundoff can happen for
3247 * the commit-rec as well.
3248 * The commit-rec is smaller than padding in this scenario and so it is
3249 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 */
3251
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003252 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003254 unit_bytes += sizeof(xfs_trans_header_t);
3255
3256 /* for start-rec */
3257 unit_bytes += sizeof(xlog_op_header_t);
3258
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003259 /*
3260 * for LR headers - the space for data in an iclog is the size minus
3261 * the space used for the headers. If we use the iclog size, then we
3262 * undercalculate the number of headers required.
3263 *
3264 * Furthermore - the addition of op headers for split-recs might
3265 * increase the space required enough to require more log and op
3266 * headers, so take that into account too.
3267 *
3268 * IMPORTANT: This reservation makes the assumption that if this
3269 * transaction is the first in an iclog and hence has the LR headers
3270 * accounted to it, then the remaining space in the iclog is
3271 * exclusively for this transaction. i.e. if the transaction is larger
3272 * than the iclog, it will be the only thing in that iclog.
3273 * Fundamentally, this means we must pass the entire log vector to
3274 * xlog_write to guarantee this.
3275 */
3276 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3277 num_headers = howmany(unit_bytes, iclog_space);
3278
3279 /* for split-recs - ophdrs added when data split over LRs */
3280 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3281
3282 /* add extra header reservations if we overrun */
3283 while (!num_headers ||
3284 howmany(unit_bytes, iclog_space) > num_headers) {
3285 unit_bytes += sizeof(xlog_op_header_t);
3286 num_headers++;
3287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 unit_bytes += log->l_iclog_hsize * num_headers;
3289
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003290 /* for commit-rec LR header - note: padding will subsume the ophdr */
3291 unit_bytes += log->l_iclog_hsize;
3292
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003293 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003294 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003295 log->l_mp->m_sb.sb_logsunit > 1) {
3296 /* log su roundoff */
3297 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3298 } else {
3299 /* BB roundoff */
3300 unit_bytes += 2*BBSIZE;
3301 }
3302
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003303 atomic_set(&tic->t_ref, 1);
Dave Chinner10547942010-12-21 12:02:25 +11003304 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 tic->t_unit_res = unit_bytes;
3306 tic->t_curr_res = unit_bytes;
3307 tic->t_cnt = cnt;
3308 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003309 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 tic->t_clientid = client;
3311 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003312 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (xflags & XFS_LOG_PERM_RESERV)
3314 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Dave Chinnereb40a872010-12-21 12:09:01 +11003315 init_waitqueue_head(&tic->t_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Christoph Hellwig0adba532007-08-30 17:21:46 +10003317 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003320}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322
3323/******************************************************************************
3324 *
3325 * Log debug routines
3326 *
3327 ******************************************************************************
3328 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003329#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330/*
3331 * Make sure that the destination ptr is within the valid data region of
3332 * one of the iclogs. This uses backup pointers stored in a different
3333 * part of the log in case we trash the log structure.
3334 */
3335void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003336xlog_verify_dest_ptr(
3337 struct log *log,
3338 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 int i;
3341 int good_ptr = 0;
3342
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003343 for (i = 0; i < log->l_iclog_bufs; i++) {
3344 if (ptr >= log->l_iclog_bak[i] &&
3345 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 good_ptr++;
3347 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003348
3349 if (!good_ptr)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003350 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003353/*
3354 * Check to make sure the grant write head didn't just over lap the tail. If
3355 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3356 * the cycles differ by exactly one and check the byte count.
3357 *
3358 * This check is run unlocked, so can give false positives. Rather than assert
3359 * on failures, use a warn-once flag and a panic tag to allow the admin to
3360 * determine if they want to panic the machine when such an error occurs. For
3361 * debug kernels this will have the same effect as using an assert but, unlinke
3362 * an assert, it can be turned off at runtime.
3363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364STATIC void
Dave Chinner3f336c62010-12-21 12:02:52 +11003365xlog_verify_grant_tail(
3366 struct log *log)
3367{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003368 int tail_cycle, tail_blocks;
Dave Chinnera69ed032010-12-21 12:08:20 +11003369 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003370
Dave Chinnera69ed032010-12-21 12:08:20 +11003371 xlog_crack_grant_head(&log->l_grant_write_head, &cycle, &space);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003372 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3373 if (tail_cycle != cycle) {
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003374 if (cycle - 1 != tail_cycle &&
3375 !(log->l_flags & XLOG_TAIL_WARN)) {
3376 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3377 "%s: cycle - 1 != tail_cycle", __func__);
3378 log->l_flags |= XLOG_TAIL_WARN;
3379 }
3380
3381 if (space > BBTOB(tail_blocks) &&
3382 !(log->l_flags & XLOG_TAIL_WARN)) {
3383 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3384 "%s: space > BBTOB(tail_blocks)", __func__);
3385 log->l_flags |= XLOG_TAIL_WARN;
3386 }
Dave Chinner3f336c62010-12-21 12:02:52 +11003387 }
3388}
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390/* check if it will fit */
3391STATIC void
3392xlog_verify_tail_lsn(xlog_t *log,
3393 xlog_in_core_t *iclog,
3394 xfs_lsn_t tail_lsn)
3395{
3396 int blocks;
3397
3398 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3399 blocks =
3400 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3401 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003402 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 } else {
3404 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3405
3406 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003407 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3410 if (blocks < BTOBB(iclog->ic_offset) + 1)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003411 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
3413} /* xlog_verify_tail_lsn */
3414
3415/*
3416 * Perform a number of checks on the iclog before writing to disk.
3417 *
3418 * 1. Make sure the iclogs are still circular
3419 * 2. Make sure we have a good magic number
3420 * 3. Make sure we don't have magic numbers in the data
3421 * 4. Check fields of each log operation header for:
3422 * A. Valid client identifier
3423 * B. tid ptr value falls in valid ptr space (user space code)
3424 * C. Length in log record header is correct according to the
3425 * individual operation headers within record.
3426 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3427 * log, check the preceding blocks of the physical log to make sure all
3428 * the cycle numbers agree with the current cycle number.
3429 */
3430STATIC void
3431xlog_verify_iclog(xlog_t *log,
3432 xlog_in_core_t *iclog,
3433 int count,
3434 boolean_t syncing)
3435{
3436 xlog_op_header_t *ophead;
3437 xlog_in_core_t *icptr;
3438 xlog_in_core_2_t *xhdr;
3439 xfs_caddr_t ptr;
3440 xfs_caddr_t base_ptr;
3441 __psint_t field_offset;
3442 __uint8_t clientid;
3443 int len, i, j, k, op_len;
3444 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003447 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 icptr = log->l_iclog;
3449 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003450 if (icptr == NULL)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003451 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 icptr = icptr->ic_next;
3453 }
3454 if (icptr != log->l_iclog)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003455 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003456 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 /* check log magic numbers */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003459 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003460 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003462 ptr = (xfs_caddr_t) &iclog->ic_header;
3463 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 ptr += BBSIZE) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003465 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003466 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3467 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
3469
3470 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003471 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 ptr = iclog->ic_datap;
3473 base_ptr = ptr;
3474 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003475 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 for (i = 0; i < len; i++) {
3477 ophead = (xlog_op_header_t *)ptr;
3478
3479 /* clientid is only 1 byte */
3480 field_offset = (__psint_t)
3481 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3482 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3483 clientid = ophead->oh_clientid;
3484 } else {
3485 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3486 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3487 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3488 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003489 clientid = xlog_get_client_id(
3490 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003492 clientid = xlog_get_client_id(
3493 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495 }
3496 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003497 xfs_warn(log->l_mp,
3498 "%s: invalid clientid %d op 0x%p offset 0x%lx",
3499 __func__, clientid, ophead,
3500 (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 /* check length */
3503 field_offset = (__psint_t)
3504 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3505 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003506 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 } else {
3508 idx = BTOBBT((__psint_t)&ophead->oh_len -
3509 (__psint_t)iclog->ic_datap);
3510 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3511 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3512 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003513 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003515 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
3517 }
3518 ptr += sizeof(xlog_op_header_t) + op_len;
3519 }
3520} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003521#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
3523/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003524 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 */
3526STATIC int
3527xlog_state_ioerror(
3528 xlog_t *log)
3529{
3530 xlog_in_core_t *iclog, *ic;
3531
3532 iclog = log->l_iclog;
3533 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3534 /*
3535 * Mark all the incore logs IOERROR.
3536 * From now on, no log flushes will result.
3537 */
3538 ic = iclog;
3539 do {
3540 ic->ic_state = XLOG_STATE_IOERROR;
3541 ic = ic->ic_next;
3542 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
3545 /*
3546 * Return non-zero, if state transition has already happened.
3547 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003548 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
3551/*
3552 * This is called from xfs_force_shutdown, when we're forcibly
3553 * shutting down the filesystem, typically because of an IO error.
3554 * Our main objectives here are to make sure that:
3555 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3556 * parties to find out, 'atomically'.
3557 * b. those who're sleeping on log reservations, pinned objects and
3558 * other resources get woken up, and be told the bad news.
3559 * c. nothing new gets queued up after (a) and (b) are done.
3560 * d. if !logerror, flush the iclogs to disk, then seal them off
3561 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003562 *
3563 * Note: for delayed logging the !logerror case needs to flush the regions
3564 * held in memory out to the iclogs before flushing them to disk. This needs
3565 * to be done before the log is marked as shutdown, otherwise the flush to the
3566 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 */
3568int
3569xfs_log_force_umount(
3570 struct xfs_mount *mp,
3571 int logerror)
3572{
3573 xlog_ticket_t *tic;
3574 xlog_t *log;
3575 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 log = mp->m_log;
3578
3579 /*
3580 * If this happens during log recovery, don't worry about
3581 * locking; the log isn't open for business yet.
3582 */
3583 if (!log ||
3584 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3585 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003586 if (mp->m_sb_bp)
3587 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 }
3590
3591 /*
3592 * Somebody could've already done the hard work for us.
3593 * No need to get locks for this.
3594 */
3595 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3596 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003597 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003600
3601 /*
3602 * Flush the in memory commit item list before marking the log as
3603 * being shut down. We need to do it in this order to ensure all the
3604 * completed transactions are flushed to disk with the xfs_log_force()
3605 * call below.
3606 */
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003607 if (!logerror)
Dave Chinnera44f13e2010-08-24 11:40:03 +10003608 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /*
Dave Chinner3f16b982010-12-21 12:29:01 +11003611 * mark the filesystem and the as in a shutdown state and wake
3612 * everybody up to tell them the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003614 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003616 if (mp->m_sb_bp)
3617 XFS_BUF_DONE(mp->m_sb_bp);
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /*
3620 * This flag is sort of redundant because of the mount flag, but
3621 * it's good to maintain the separation between the log and the rest
3622 * of XFS.
3623 */
3624 log->l_flags |= XLOG_IO_ERROR;
3625
3626 /*
3627 * If we hit a log error, we want to mark all the iclogs IOERROR
3628 * while we're still holding the loglock.
3629 */
3630 if (logerror)
3631 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003632 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 /*
Dave Chinner10547942010-12-21 12:02:25 +11003635 * We don't want anybody waiting for log reservations after this. That
3636 * means we have to wake up everybody queued up on reserveq as well as
3637 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3638 * we don't enqueue anything once the SHUTDOWN flag is set, and this
Dave Chinner3f16b982010-12-21 12:29:01 +11003639 * action is protected by the grant locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 */
Dave Chinner3f16b982010-12-21 12:29:01 +11003641 spin_lock(&log->l_grant_reserve_lock);
Dave Chinner10547942010-12-21 12:02:25 +11003642 list_for_each_entry(tic, &log->l_reserveq, t_queue)
Dave Chinnereb40a872010-12-21 12:09:01 +11003643 wake_up(&tic->t_wait);
Dave Chinner3f16b982010-12-21 12:29:01 +11003644 spin_unlock(&log->l_grant_reserve_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Dave Chinner3f16b982010-12-21 12:29:01 +11003646 spin_lock(&log->l_grant_write_lock);
Dave Chinner10547942010-12-21 12:02:25 +11003647 list_for_each_entry(tic, &log->l_writeq, t_queue)
Dave Chinnereb40a872010-12-21 12:09:01 +11003648 wake_up(&tic->t_wait);
Dave Chinner3f16b982010-12-21 12:29:01 +11003649 spin_unlock(&log->l_grant_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003651 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 ASSERT(!logerror);
3653 /*
3654 * Force the incore logs to disk before shutting the
3655 * log down completely.
3656 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003657 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3658
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003659 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003661 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
3663 /*
3664 * Wake up everybody waiting on xfs_log_force.
3665 * Callback all log item committed functions as if the
3666 * log writes were completed.
3667 */
3668 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3669
3670#ifdef XFSERRORDEBUG
3671 {
3672 xlog_in_core_t *iclog;
3673
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003674 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 iclog = log->l_iclog;
3676 do {
3677 ASSERT(iclog->ic_callback == 0);
3678 iclog = iclog->ic_next;
3679 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003680 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
3682#endif
3683 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003684 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685}
3686
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003687STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688xlog_iclogs_empty(xlog_t *log)
3689{
3690 xlog_in_core_t *iclog;
3691
3692 iclog = log->l_iclog;
3693 do {
3694 /* endianness does not matter here, zero is zero in
3695 * any language.
3696 */
3697 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 iclog = iclog->ic_next;
3700 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003701 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}