blob: 685997548fb84eb87df6383dfdf0fe470ac61849 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
28#include "xfs_error.h"
29#include "xfs_log_priv.h"
30#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_log_recover.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_trans_priv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Chinnereb01c9c2008-04-10 12:18:46 +100041kmem_zone_t *xfs_log_ticket_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Local miscellaneous function prototypes */
Dave Chinner55b66332010-03-23 11:43:17 +110044STATIC int xlog_commit_record(struct log *log, struct xlog_ticket *ticket,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 xlog_in_core_t **, xfs_lsn_t *);
46STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp,
47 xfs_buftarg_t *log_target,
48 xfs_daddr_t blk_offset,
49 int num_bblks);
Dave Chinnerc8a09ff2010-12-04 00:02:40 +110050STATIC int xlog_space_left(struct log *log, atomic64_t *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
Nathan Scottc41564b2006-03-29 08:55:14 +100052STATIC void xlog_dealloc_log(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* local state machine functions */
55STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
56STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog);
57STATIC int xlog_state_get_iclog_space(xlog_t *log,
58 int len,
59 xlog_in_core_t **iclog,
60 xlog_ticket_t *ticket,
61 int *continued_write,
62 int *logoffsetp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC int xlog_state_release_iclog(xlog_t *log,
64 xlog_in_core_t *iclog);
65STATIC void xlog_state_switch_iclogs(xlog_t *log,
66 xlog_in_core_t *iclog,
67 int eventual_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog);
69
70/* local functions to manipulate grant head */
71STATIC int xlog_grant_log_space(xlog_t *log,
72 xlog_ticket_t *xtic);
Dave Chinner2ced19c2010-12-21 12:09:20 +110073STATIC void xlog_grant_push_ail(struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int need_bytes);
75STATIC void xlog_regrant_reserve_log_space(xlog_t *log,
76 xlog_ticket_t *ticket);
77STATIC int xlog_regrant_write_log_space(xlog_t *log,
78 xlog_ticket_t *ticket);
79STATIC void xlog_ungrant_log_space(xlog_t *log,
80 xlog_ticket_t *ticket);
81
Nathan Scottcfcbbbd2005-11-02 15:12:04 +110082#if defined(DEBUG)
Christoph Hellwige6b1f272010-03-23 11:47:38 +110083STATIC void xlog_verify_dest_ptr(xlog_t *log, char *ptr);
Dave Chinner3f336c62010-12-21 12:02:52 +110084STATIC void xlog_verify_grant_tail(struct log *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
86 int count, boolean_t syncing);
87STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
88 xfs_lsn_t tail_lsn);
89#else
90#define xlog_verify_dest_ptr(a,b)
Dave Chinner3f336c62010-12-21 12:02:52 +110091#define xlog_verify_grant_tail(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define xlog_verify_iclog(a,b,c,d)
93#define xlog_verify_tail_lsn(a,b,c)
94#endif
95
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100096STATIC int xlog_iclogs_empty(xlog_t *log);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Christoph Hellwigdd954c62006-01-11 15:34:50 +110098static void
Dave Chinner663e4962010-12-21 12:06:05 +110099xlog_grant_sub_space(
100 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100101 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100102 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100103{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100104 int64_t head_val = atomic64_read(head);
105 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100106
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100107 do {
108 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100109
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100110 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100111
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100112 space -= bytes;
113 if (space < 0) {
114 space += log->l_logsize;
115 cycle--;
116 }
117
118 old = head_val;
119 new = xlog_assign_grant_head_val(cycle, space);
120 head_val = atomic64_cmpxchg(head, old, new);
121 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100122}
123
124static void
Dave Chinner663e4962010-12-21 12:06:05 +1100125xlog_grant_add_space(
126 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100127 atomic64_t *head,
Dave Chinner663e4962010-12-21 12:06:05 +1100128 int bytes)
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100129{
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100130 int64_t head_val = atomic64_read(head);
131 int64_t new, old;
Dave Chinnera69ed032010-12-21 12:08:20 +1100132
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100133 do {
134 int tmp;
135 int cycle, space;
Dave Chinnera69ed032010-12-21 12:08:20 +1100136
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100137 xlog_crack_grant_head_val(head_val, &cycle, &space);
Dave Chinnera69ed032010-12-21 12:08:20 +1100138
Dave Chinnerd0eb2f32010-12-21 12:29:14 +1100139 tmp = log->l_logsize - space;
140 if (tmp > bytes)
141 space += bytes;
142 else {
143 space = bytes - tmp;
144 cycle++;
145 }
146
147 old = head_val;
148 new = xlog_assign_grant_head_val(cycle, space);
149 head_val = atomic64_cmpxchg(head, old, new);
150 } while (head_val != old);
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100151}
Dave Chinnera69ed032010-12-21 12:08:20 +1100152
Christoph Hellwigc303c5b2012-02-20 02:31:26 +0000153STATIC void
154xlog_grant_head_init(
155 struct xlog_grant_head *head)
156{
157 xlog_assign_grant_head(&head->grant, 1, 0);
158 INIT_LIST_HEAD(&head->waiters);
159 spin_lock_init(&head->lock);
160}
161
Christoph Hellwiga79bf2d2012-02-20 02:31:27 +0000162STATIC void
163xlog_grant_head_wake_all(
164 struct xlog_grant_head *head)
165{
166 struct xlog_ticket *tic;
167
168 spin_lock(&head->lock);
169 list_for_each_entry(tic, &head->waiters, t_queue)
170 wake_up_process(tic->t_task);
171 spin_unlock(&head->lock);
172}
173
Christoph Hellwige179840d2012-02-20 02:31:29 +0000174static inline int
175xlog_ticket_reservation(
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000176 struct log *log,
Christoph Hellwige179840d2012-02-20 02:31:29 +0000177 struct xlog_grant_head *head,
178 struct xlog_ticket *tic)
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000179{
Christoph Hellwige179840d2012-02-20 02:31:29 +0000180 if (head == &log->l_write_head) {
181 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
182 return tic->t_unit_res;
183 } else {
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000184 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
Christoph Hellwige179840d2012-02-20 02:31:29 +0000185 return tic->t_unit_res * tic->t_cnt;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000186 else
Christoph Hellwige179840d2012-02-20 02:31:29 +0000187 return tic->t_unit_res;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000188 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000189}
190
191STATIC bool
Christoph Hellwige179840d2012-02-20 02:31:29 +0000192xlog_grant_head_wake(
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000193 struct log *log,
Christoph Hellwige179840d2012-02-20 02:31:29 +0000194 struct xlog_grant_head *head,
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000195 int *free_bytes)
196{
197 struct xlog_ticket *tic;
198 int need_bytes;
199
Christoph Hellwige179840d2012-02-20 02:31:29 +0000200 list_for_each_entry(tic, &head->waiters, t_queue) {
201 need_bytes = xlog_ticket_reservation(log, head, tic);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000202 if (*free_bytes < need_bytes)
203 return false;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000204
Christoph Hellwige179840d2012-02-20 02:31:29 +0000205 *free_bytes -= need_bytes;
206 trace_xfs_log_grant_wake_up(log, tic);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000207 wake_up_process(tic->t_task);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000208 }
209
210 return true;
211}
212
213STATIC int
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000214xlog_grant_head_wait(
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000215 struct log *log,
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000216 struct xlog_grant_head *head,
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000217 struct xlog_ticket *tic,
218 int need_bytes)
219{
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000220 list_add_tail(&tic->t_queue, &head->waiters);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000221
222 do {
223 if (XLOG_FORCED_SHUTDOWN(log))
224 goto shutdown;
225 xlog_grant_push_ail(log, need_bytes);
226
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000227 __set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000228 spin_unlock(&head->lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000229
Christoph Hellwig14a7235f2012-02-20 02:31:24 +0000230 XFS_STATS_INC(xs_sleep_logspace);
231
232 trace_xfs_log_grant_sleep(log, tic);
233 schedule();
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000234 trace_xfs_log_grant_wake(log, tic);
235
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000236 spin_lock(&head->lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000237 if (XLOG_FORCED_SHUTDOWN(log))
238 goto shutdown;
Christoph Hellwig23ee3df2012-02-20 02:31:28 +0000239 } while (xlog_space_left(log, &head->grant) < need_bytes);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000240
241 list_del_init(&tic->t_queue);
242 return 0;
243shutdown:
244 list_del_init(&tic->t_queue);
245 return XFS_ERROR(EIO);
246}
247
Christoph Hellwig0adba532007-08-30 17:21:46 +1000248static void
249xlog_tic_reset_res(xlog_ticket_t *tic)
250{
251 tic->t_res_num = 0;
252 tic->t_res_arr_sum = 0;
253 tic->t_res_num_ophdrs = 0;
254}
255
256static void
257xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
258{
259 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
260 /* add to overflow and start again */
261 tic->t_res_o_flow += tic->t_res_arr_sum;
262 tic->t_res_num = 0;
263 tic->t_res_arr_sum = 0;
264 }
265
266 tic->t_res_arr[tic->t_res_num].r_len = len;
267 tic->t_res_arr[tic->t_res_num].r_type = type;
268 tic->t_res_arr_sum += len;
269 tic->t_res_num++;
270}
Christoph Hellwigdd954c62006-01-11 15:34:50 +1100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
273 * NOTES:
274 *
275 * 1. currblock field gets updated at startup and after in-core logs
276 * marked as with WANT_SYNC.
277 */
278
279/*
280 * This routine is called when a user of a log manager ticket is done with
281 * the reservation. If the ticket was ever used, then a commit record for
282 * the associated transaction is written out as a log operation header with
283 * no data. The flag XLOG_TIC_INITED is set when the first write occurs with
284 * a given ticket. If the ticket was one with a permanent reservation, then
285 * a few operations are done differently. Permanent reservation tickets by
286 * default don't release the reservation. They just commit the current
287 * transaction with the belief that the reservation is still needed. A flag
288 * must be passed in before permanent reservations are actually released.
289 * When these type of tickets are not released, they need to be set into
290 * the inited state again. By doing this, a start record will be written
291 * out when the next write occurs.
292 */
293xfs_lsn_t
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000294xfs_log_done(
295 struct xfs_mount *mp,
296 struct xlog_ticket *ticket,
297 struct xlog_in_core **iclog,
298 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000300 struct log *log = mp->m_log;
301 xfs_lsn_t lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (XLOG_FORCED_SHUTDOWN(log) ||
304 /*
305 * If nothing was ever written, don't write out commit record.
306 * If we get an error, just continue and give back the log ticket.
307 */
308 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
Dave Chinner55b66332010-03-23 11:43:17 +1100309 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 lsn = (xfs_lsn_t) -1;
311 if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
312 flags |= XFS_LOG_REL_PERM_RESERV;
313 }
314 }
315
316
317 if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
318 (flags & XFS_LOG_REL_PERM_RESERV)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000319 trace_xfs_log_done_nonperm(log, ticket);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000322 * Release ticket if not permanent reservation or a specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * request has been made to release a permanent reservation.
324 */
325 xlog_ungrant_log_space(log, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100326 xfs_log_ticket_put(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000328 trace_xfs_log_done_perm(log, ticket);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 xlog_regrant_reserve_log_space(log, ticket);
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000331 /* If this ticket was a permanent reservation and we aren't
332 * trying to release it, reset the inited flags; so next time
333 * we write, a start record will be written out.
334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 ticket->t_flags |= XLOG_TIC_INITED;
Lachlan McIlroyc6a7b0f2008-08-13 16:52:50 +1000336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return lsn;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/*
342 * Attaches a new iclog I/O completion callback routine during
343 * transaction commit. If the log is in error state, a non-zero
344 * return code is handed back and the caller is responsible for
345 * executing the callback at an appropriate time.
346 */
347int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000348xfs_log_notify(
349 struct xfs_mount *mp,
350 struct xlog_in_core *iclog,
351 xfs_log_callback_t *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000353 int abortflg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
David Chinner114d23a2008-04-10 12:18:39 +1000355 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
357 if (!abortflg) {
358 ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
359 (iclog->ic_state == XLOG_STATE_WANT_SYNC));
360 cb->cb_next = NULL;
361 *(iclog->ic_callback_tail) = cb;
362 iclog->ic_callback_tail = &(cb->cb_next);
363 }
David Chinner114d23a2008-04-10 12:18:39 +1000364 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return abortflg;
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000369xfs_log_release_iclog(
370 struct xfs_mount *mp,
371 struct xlog_in_core *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000373 if (xlog_state_release_iclog(mp->m_log, iclog)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000374 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +0100375 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 return 0;
379}
380
381/*
382 * 1. Reserve an amount of on-disk log space and return a ticket corresponding
383 * to the reservation.
384 * 2. Potentially, push buffers at tail of log to disk.
385 *
386 * Each reservation is going to reserve extra space for a log record header.
387 * When writes happen to the on-disk log, we don't subtract the length of the
388 * log record header from any reservation. By wasting space in each
389 * reservation, we prevent over allocation problems.
390 */
391int
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000392xfs_log_reserve(
393 struct xfs_mount *mp,
394 int unit_bytes,
395 int cnt,
396 struct xlog_ticket **ticket,
397 __uint8_t client,
398 uint flags,
399 uint t_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000401 struct log *log = mp->m_log;
402 struct xlog_ticket *internal_ticket;
403 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (XLOG_FORCED_SHUTDOWN(log))
408 return XFS_ERROR(EIO);
409
410 XFS_STATS_INC(xs_try_logspace);
411
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (*ticket != NULL) {
414 ASSERT(flags & XFS_LOG_PERM_RESERV);
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000415 internal_ticket = *ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000416
Dave Chinner524ee362010-05-07 11:05:05 +1000417 /*
418 * this is a new transaction on the ticket, so we need to
419 * change the transaction ID so that the next transaction has a
420 * different TID in the log. Just add one to the existing tid
421 * so that we can see chains of rolling transactions in the log
422 * easily.
423 */
424 internal_ticket->t_tid++;
425
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000426 trace_xfs_log_reserve(log, internal_ticket);
427
Dave Chinner2ced19c2010-12-21 12:09:20 +1100428 xlog_grant_push_ail(log, internal_ticket->t_unit_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 retval = xlog_regrant_write_log_space(log, internal_ticket);
430 } else {
431 /* may sleep if need to allocate more tickets */
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100432 internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
Dave Chinner3383ca52010-05-07 11:04:17 +1000433 client, flags,
434 KM_SLEEP|KM_MAYFAIL);
David Chinnereb01c9c2008-04-10 12:18:46 +1000435 if (!internal_ticket)
436 return XFS_ERROR(ENOMEM);
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000437 internal_ticket->t_trans_type = t_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *ticket = internal_ticket;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000439
440 trace_xfs_log_reserve(log, internal_ticket);
441
Dave Chinner2ced19c2010-12-21 12:09:20 +1100442 xlog_grant_push_ail(log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 (internal_ticket->t_unit_res *
444 internal_ticket->t_cnt));
445 retval = xlog_grant_log_space(log, internal_ticket);
446 }
447
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000448 if (unlikely(retval)) {
449 /*
450 * If we are failing, make sure the ticket doesn't have any
451 * current reservations. We don't want to add this back
452 * when the ticket/ transaction gets cancelled.
453 */
454 internal_ticket->t_curr_res = 0;
455 /* ungrant will give back unit_res * t_cnt. */
456 internal_ticket->t_cnt = 0;
457 }
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return retval;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +0000460}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462
463/*
464 * Mount a log filesystem
465 *
466 * mp - ubiquitous xfs mount point structure
467 * log_target - buftarg of on-disk log device
468 * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
469 * num_bblocks - Number of BBSIZE blocks in on-disk log
470 *
471 * Return error or zero.
472 */
473int
David Chinner249a8c12008-02-05 12:13:32 +1100474xfs_log_mount(
475 xfs_mount_t *mp,
476 xfs_buftarg_t *log_target,
477 xfs_daddr_t blk_offset,
478 int num_bblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
David Chinner249a8c12008-02-05 12:13:32 +1100480 int error;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100483 xfs_notice(mp, "Mounting Filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100485 xfs_notice(mp,
486"Mounting filesystem in no-recovery mode. Filesystem will be inconsistent.");
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000487 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
Dave Chinnera6cb7672009-04-06 18:39:27 +0200491 if (IS_ERR(mp->m_log)) {
492 error = -PTR_ERR(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100493 goto out;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
David Chinner249a8c12008-02-05 12:13:32 +1100497 * Initialize the AIL now we have a log.
498 */
David Chinner249a8c12008-02-05 12:13:32 +1100499 error = xfs_trans_ail_init(mp);
500 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100501 xfs_warn(mp, "AIL initialisation failed: error %d", error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100502 goto out_free_log;
David Chinner249a8c12008-02-05 12:13:32 +1100503 }
David Chinnera9c21c12008-10-30 17:39:35 +1100504 mp->m_log->l_ailp = mp->m_ail;
David Chinner249a8c12008-02-05 12:13:32 +1100505
506 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * skip log recovery on a norecovery mount. pretend it all
508 * just worked.
509 */
510 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
David Chinner249a8c12008-02-05 12:13:32 +1100511 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000514 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Eric Sandeen65be6052006-01-11 15:34:19 +1100516 error = xlog_recover(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (readonly)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000519 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100521 xfs_warn(mp, "log mount/recovery failed: error %d",
522 error);
Christoph Hellwig26430752009-02-12 19:55:48 +0100523 goto out_destroy_ail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 }
526
527 /* Normal transactions can now occur */
528 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
529
Dave Chinner71e330b2010-05-21 14:37:18 +1000530 /*
531 * Now the log has been fully initialised and we know were our
532 * space grant counters are, we can initialise the permanent ticket
533 * needed for delayed logging to work.
534 */
535 xlog_cil_init_post_recovery(mp->m_log);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
Christoph Hellwig26430752009-02-12 19:55:48 +0100538
539out_destroy_ail:
540 xfs_trans_ail_destroy(mp);
541out_free_log:
542 xlog_dealloc_log(mp->m_log);
Dave Chinner644c3562008-11-10 16:50:24 +1100543out:
David Chinner249a8c12008-02-05 12:13:32 +1100544 return error;
Christoph Hellwig26430752009-02-12 19:55:48 +0100545}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547/*
548 * Finish the recovery of the file system. This is separate from
549 * the xfs_log_mount() call, because it depends on the code in
550 * xfs_mountfs() to read in the root and real-time bitmap inodes
551 * between calling xfs_log_mount() and here.
552 *
553 * mp - ubiquitous xfs mount point structure
554 */
555int
Christoph Hellwig42490232008-08-13 16:49:32 +1000556xfs_log_mount_finish(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 int error;
559
560 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
Christoph Hellwig42490232008-08-13 16:49:32 +1000561 error = xlog_recover_finish(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 else {
563 error = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000564 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 return error;
568}
569
570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * Final log writes as part of unmount.
572 *
573 * Mark the filesystem clean as unmount happens. Note that during relocation
574 * this routine needs to be executed as part of source-bag while the
575 * deallocation must not be done until source-end.
576 */
577
578/*
579 * Unmount record used to have a string "Unmount filesystem--" in the
580 * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
581 * We just write the magic number now since that particular field isn't
582 * currently architecture converted and "nUmount" is a bit foo.
583 * As far as I know, there weren't any dependencies on the old behaviour.
584 */
585
586int
587xfs_log_unmount_write(xfs_mount_t *mp)
588{
589 xlog_t *log = mp->m_log;
590 xlog_in_core_t *iclog;
591#ifdef DEBUG
592 xlog_in_core_t *first_iclog;
593#endif
Christoph Hellwig35a8a722010-02-15 23:34:54 +0000594 xlog_ticket_t *tic = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 xfs_lsn_t lsn;
596 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Don't write out unmount record on read-only mounts.
600 * Or, if we are doing a forced umount (typically because of IO errors).
601 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000602 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000605 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
David Chinnerb911ca02008-04-10 12:24:30 +1000606 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608#ifdef DEBUG
609 first_iclog = iclog = log->l_iclog;
610 do {
611 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
612 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
613 ASSERT(iclog->ic_offset == 0);
614 }
615 iclog = iclog->ic_next;
616 } while (iclog != first_iclog);
617#endif
618 if (! (XLOG_FORCED_SHUTDOWN(log))) {
Tim Shimmin955e47a2006-09-28 11:04:16 +1000619 error = xfs_log_reserve(mp, 600, 1, &tic,
620 XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!error) {
Dave Chinner55b66332010-03-23 11:43:17 +1100622 /* the data section must be 32 bit size aligned */
623 struct {
624 __uint16_t magic;
625 __uint16_t pad1;
626 __uint32_t pad2; /* may as well make it 64 bits */
627 } magic = {
628 .magic = XLOG_UNMOUNT_TYPE,
629 };
630 struct xfs_log_iovec reg = {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000631 .i_addr = &magic,
Dave Chinner55b66332010-03-23 11:43:17 +1100632 .i_len = sizeof(magic),
633 .i_type = XLOG_REG_TYPE_UNMOUNT,
634 };
635 struct xfs_log_vec vec = {
636 .lv_niovecs = 1,
637 .lv_iovecp = &reg,
638 };
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* remove inited flag */
Dave Chinner55b66332010-03-23 11:43:17 +1100641 tic->t_flags = 0;
642 error = xlog_write(log, &vec, tic, &lsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 NULL, XLOG_UNMOUNT_TRANS);
644 /*
645 * At this point, we're umounting anyway,
646 * so there's no point in transitioning log state
647 * to IOERROR. Just continue...
648 */
649 }
650
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100651 if (error)
652 xfs_alert(mp, "%s: unmount record failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000655 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100657 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100659 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000660 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000662 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
664 iclog->ic_state == XLOG_STATE_DIRTY)) {
665 if (!XLOG_FORCED_SHUTDOWN(log)) {
Dave Chinnereb40a872010-12-21 12:09:01 +1100666 xlog_wait(&iclog->ic_force_wait,
667 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000669 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000672 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Tim Shimmin955e47a2006-09-28 11:04:16 +1000674 if (tic) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000675 trace_xfs_log_umount_write(log, tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000676 xlog_ungrant_log_space(log, tic);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100677 xfs_log_ticket_put(tic);
Tim Shimmin955e47a2006-09-28 11:04:16 +1000678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 } else {
680 /*
681 * We're already in forced_shutdown mode, couldn't
682 * even attempt to write out the unmount transaction.
683 *
684 * Go through the motions of sync'ing and releasing
685 * the iclog, even though no I/O will actually happen,
Nathan Scottc41564b2006-03-29 08:55:14 +1000686 * we need to wait for other log I/Os that may already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * be in progress. Do this as a separate section of
688 * code so we'll know if we ever get stuck here that
689 * we're in this odd situation of trying to unmount
690 * a file system that went into forced_shutdown as
691 * the result of an unmount..
692 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000693 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 iclog = log->l_iclog;
David Chinner155cc6b2008-03-06 13:44:14 +1100695 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 xlog_state_want_sync(log, iclog);
Christoph Hellwig39e2def2008-12-03 12:20:28 +0100698 spin_unlock(&log->l_icloglock);
David Chinner1bb7d6b2008-04-10 12:24:38 +1000699 error = xlog_state_release_iclog(log, iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000701 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
704 || iclog->ic_state == XLOG_STATE_DIRTY
705 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
706
Dave Chinnereb40a872010-12-21 12:09:01 +1100707 xlog_wait(&iclog->ic_force_wait,
708 &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000710 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 }
713
David Chinner1bb7d6b2008-04-10 12:24:38 +1000714 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715} /* xfs_log_unmount_write */
716
717/*
718 * Deallocate log structures for unmount/relocation.
David Chinner249a8c12008-02-05 12:13:32 +1100719 *
720 * We need to stop the aild from running before we destroy
721 * and deallocate the log as the aild references the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
723void
Christoph Hellwig21b699c2009-03-16 08:19:29 +0100724xfs_log_unmount(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
David Chinner249a8c12008-02-05 12:13:32 +1100726 xfs_trans_ail_destroy(mp);
Nathan Scottc41564b2006-03-29 08:55:14 +1000727 xlog_dealloc_log(mp->m_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Dave Chinner43f5efc2010-03-23 10:10:00 +1100730void
731xfs_log_item_init(
732 struct xfs_mount *mp,
733 struct xfs_log_item *item,
734 int type,
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000735 const struct xfs_item_ops *ops)
Dave Chinner43f5efc2010-03-23 10:10:00 +1100736{
737 item->li_mountp = mp;
738 item->li_ailp = mp->m_ail;
739 item->li_type = type;
740 item->li_ops = ops;
Dave Chinner71e330b2010-05-21 14:37:18 +1000741 item->li_lv = NULL;
742
743 INIT_LIST_HEAD(&item->li_ail);
744 INIT_LIST_HEAD(&item->li_cil);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100745}
746
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000747/*
748 * Wake up processes waiting for log space after we have moved the log tail.
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750void
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000751xfs_log_space_wake(
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000752 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000754 struct log *log = mp->m_log;
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +0000755 int free_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (XLOG_FORCED_SHUTDOWN(log))
758 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Christoph Hellwig28496962012-02-20 02:31:25 +0000760 if (!list_empty_careful(&log->l_write_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000761 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
762
Christoph Hellwig28496962012-02-20 02:31:25 +0000763 spin_lock(&log->l_write_head.lock);
764 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
Christoph Hellwige179840d2012-02-20 02:31:29 +0000765 xlog_grant_head_wake(log, &log->l_write_head, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +0000766 spin_unlock(&log->l_write_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Dave Chinner10547942010-12-21 12:02:25 +1100768
Christoph Hellwig28496962012-02-20 02:31:25 +0000769 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000770 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
771
Christoph Hellwig28496962012-02-20 02:31:25 +0000772 spin_lock(&log->l_reserve_head.lock);
773 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Christoph Hellwige179840d2012-02-20 02:31:29 +0000774 xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +0000775 spin_unlock(&log->l_reserve_head.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Dave Chinner3f16b982010-12-21 12:29:01 +1100777}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779/*
780 * Determine if we have a transaction that has gone to disk
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000781 * that needs to be covered. To begin the transition to the idle state
782 * firstly the log needs to be idle (no AIL and nothing in the iclogs).
783 * If we are then in a state where covering is needed, the caller is informed
784 * that dummy transactions are required to move the log into the idle state.
785 *
786 * Because this is called as part of the sync process, we should also indicate
787 * that dummy transactions should be issued in anything but the covered or
788 * idle states. This ensures that the log tail is accurately reflected in
789 * the log at the end of the sync, hence if a crash occurrs avoids replay
790 * of transactions where the metadata is already on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
792int
793xfs_log_need_covered(xfs_mount_t *mp)
794{
David Chinner27d8d5f2008-10-30 17:38:39 +1100795 int needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 xlog_t *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
David Chinner92821e22007-05-24 15:26:31 +1000798 if (!xfs_fs_writable(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000801 spin_lock(&log->l_icloglock);
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000802 switch (log->l_covered_state) {
803 case XLOG_STATE_COVER_DONE:
804 case XLOG_STATE_COVER_DONE2:
805 case XLOG_STATE_COVER_IDLE:
806 break;
807 case XLOG_STATE_COVER_NEED:
808 case XLOG_STATE_COVER_NEED2:
Dave Chinnerfd074842011-04-08 12:45:07 +1000809 if (!xfs_ail_min_lsn(log->l_ailp) &&
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000810 xlog_iclogs_empty(log)) {
811 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
812 log->l_covered_state = XLOG_STATE_COVER_DONE;
813 else
814 log->l_covered_state = XLOG_STATE_COVER_DONE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000816 /* FALLTHRU */
817 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 needed = 1;
Dave Chinnerb6f8dd42010-04-13 15:06:44 +1000819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +1000821 spin_unlock(&log->l_icloglock);
Jesper Juhl014c2542006-01-15 02:37:08 +0100822 return needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000825/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * We may be holding the log iclog lock upon entering this routine.
827 */
828xfs_lsn_t
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100829xlog_assign_tail_lsn(
830 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100832 xfs_lsn_t tail_lsn;
833 struct log *log = mp->m_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Christoph Hellwig09a423a2012-02-20 02:31:20 +0000835 /*
836 * To make sure we always have a valid LSN for the log tail we keep
837 * track of the last LSN which was committed in log->l_last_sync_lsn,
838 * and use that when the AIL was empty and xfs_ail_min_lsn returns 0.
839 *
840 * If the AIL has been emptied we also need to wake any process
841 * waiting for this condition.
842 */
Dave Chinnerfd074842011-04-08 12:45:07 +1000843 tail_lsn = xfs_ail_min_lsn(mp->m_ail);
Dave Chinner84f3c682010-12-03 22:11:29 +1100844 if (!tail_lsn)
845 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100846 atomic64_set(&log->l_tail_lsn, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return tail_lsn;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100848}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/*
851 * Return the space in the log between the tail and the head. The head
852 * is passed in the cycle/bytes formal parms. In the special case where
853 * the reserve head has wrapped passed the tail, this calculation is no
854 * longer valid. In this case, just return 0 which means there is no space
855 * in the log. This works for all places where this function is called
856 * with the reserve head. Of course, if the write head were to ever
857 * wrap the tail, we should blow up. Rather than catch this case here,
858 * we depend on other ASSERTions in other parts of the code. XXXmiken
859 *
860 * This code also handles the case where the reservation head is behind
861 * the tail. The details of this case are described below, but the end
862 * result is that we return the size of the log as the amount of space left.
863 */
David Chinnera8272ce2007-11-23 16:28:09 +1100864STATIC int
Dave Chinnera69ed032010-12-21 12:08:20 +1100865xlog_space_left(
866 struct log *log,
Dave Chinnerc8a09ff2010-12-04 00:02:40 +1100867 atomic64_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Dave Chinnera69ed032010-12-21 12:08:20 +1100869 int free_bytes;
870 int tail_bytes;
871 int tail_cycle;
872 int head_cycle;
873 int head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Dave Chinnera69ed032010-12-21 12:08:20 +1100875 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100876 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
877 tail_bytes = BBTOB(tail_bytes);
Dave Chinnera69ed032010-12-21 12:08:20 +1100878 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
879 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
880 else if (tail_cycle + 1 < head_cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
Dave Chinnera69ed032010-12-21 12:08:20 +1100882 else if (tail_cycle < head_cycle) {
883 ASSERT(tail_cycle == (head_cycle - 1));
884 free_bytes = tail_bytes - head_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 } else {
886 /*
887 * The reservation head is behind the tail.
888 * In this case we just want to return the size of the
889 * log as the amount of space left.
890 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100891 xfs_alert(log->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 "xlog_space_left: head behind tail\n"
893 " tail_cycle = %d, tail_bytes = %d\n"
894 " GH cycle = %d, GH bytes = %d",
Dave Chinnera69ed032010-12-21 12:08:20 +1100895 tail_cycle, tail_bytes, head_cycle, head_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 ASSERT(0);
897 free_bytes = log->l_logsize;
898 }
899 return free_bytes;
Dave Chinnera69ed032010-12-21 12:08:20 +1100900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902
903/*
904 * Log function which is called when an io completes.
905 *
906 * The log manager needs its own routine, in order to control what
907 * happens with the buffer after the write completes.
908 */
909void
910xlog_iodone(xfs_buf_t *bp)
911{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200912 xlog_in_core_t *iclog = bp->b_fspriv;
913 xlog_t *l = iclog->ic_log;
914 int aborted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /*
917 * Race to shutdown the filesystem if we see an error.
918 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000919 if (XFS_TEST_ERROR((xfs_buf_geterror(bp)), l->l_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) {
Christoph Hellwig901796a2011-10-10 16:52:49 +0000921 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000922 xfs_buf_stale(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +1000923 xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /*
925 * This flag will be propagated to the trans-committed
926 * callback routines to let them know that the log-commit
927 * didn't succeed.
928 */
929 aborted = XFS_LI_ABORTED;
930 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
931 aborted = XFS_LI_ABORTED;
932 }
David Chinner3db296f2007-05-14 18:24:16 +1000933
934 /* log I/O is always issued ASYNC */
935 ASSERT(XFS_BUF_ISASYNC(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 xlog_state_done_syncing(iclog, aborted);
David Chinner3db296f2007-05-14 18:24:16 +1000937 /*
938 * do not reference the buffer (bp) here as we could race
939 * with it being freed after writing the unmount record to the
940 * log.
941 */
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943} /* xlog_iodone */
944
945/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * Return size of each in-core log record buffer.
947 *
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200948 * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 *
950 * If the filesystem blocksize is too large, we may need to choose a
951 * larger size since the directory code currently logs entire blocks.
952 */
953
954STATIC void
955xlog_get_iclog_buffer_size(xfs_mount_t *mp,
956 xlog_t *log)
957{
958 int size;
959 int xhdrs;
960
Eric Sandeen1cb51252007-08-16 16:24:43 +1000961 if (mp->m_logbufs <= 0)
962 log->l_iclog_bufs = XLOG_MAX_ICLOGS;
963 else
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100964 log->l_iclog_bufs = mp->m_logbufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /*
967 * Buffer size passed in from mount system call.
968 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100969 if (mp->m_logbsize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 size = log->l_iclog_size = mp->m_logbsize;
971 log->l_iclog_size_log = 0;
972 while (size != 1) {
973 log->l_iclog_size_log++;
974 size >>= 1;
975 }
976
Eric Sandeen62118702008-03-06 13:44:28 +1100977 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200978 /* # headers = size / 32k
979 * one header holds cycles from 32k of data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
981
982 xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
983 if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE)
984 xhdrs++;
985 log->l_iclog_hsize = xhdrs << BBSHIFT;
986 log->l_iclog_heads = xhdrs;
987 } else {
988 ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE);
989 log->l_iclog_hsize = BBSIZE;
990 log->l_iclog_heads = 1;
991 }
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100992 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200995 /* All machines use 32kB buffers by default. */
Eric Sandeen1cb51252007-08-16 16:24:43 +1000996 log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
997 log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* the default log size is 16k or 32k which is one header sector */
1000 log->l_iclog_hsize = BBSIZE;
1001 log->l_iclog_heads = 1;
1002
Christoph Hellwig7153f8b2009-02-09 08:36:46 +01001003done:
1004 /* are we being asked to make the sizes selected above visible? */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001005 if (mp->m_logbufs == 0)
1006 mp->m_logbufs = log->l_iclog_bufs;
1007 if (mp->m_logbsize == 0)
1008 mp->m_logbsize = log->l_iclog_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009} /* xlog_get_iclog_buffer_size */
1010
1011
1012/*
1013 * This routine initializes some of the log structure for a given mount point.
1014 * Its primary purpose is to fill in enough, so recovery can occur. However,
1015 * some other stuff may be filled in too.
1016 */
1017STATIC xlog_t *
1018xlog_alloc_log(xfs_mount_t *mp,
1019 xfs_buftarg_t *log_target,
1020 xfs_daddr_t blk_offset,
1021 int num_bblks)
1022{
1023 xlog_t *log;
1024 xlog_rec_header_t *head;
1025 xlog_in_core_t **iclogp;
1026 xlog_in_core_t *iclog, *prev_iclog=NULL;
1027 xfs_buf_t *bp;
1028 int i;
Dave Chinnera6cb7672009-04-06 18:39:27 +02001029 int error = ENOMEM;
Alex Elder69ce58f2010-04-20 17:09:59 +10001030 uint log2_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Dave Chinner644c3562008-11-10 16:50:24 +11001032 log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001033 if (!log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001034 xfs_warn(mp, "Log allocation failed: No memory!");
Dave Chinnera6cb7672009-04-06 18:39:27 +02001035 goto out;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 log->l_mp = mp;
1039 log->l_targ = log_target;
1040 log->l_logsize = BBTOB(num_bblks);
1041 log->l_logBBstart = blk_offset;
1042 log->l_logBBsize = num_bblks;
1043 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1044 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1045
1046 log->l_prev_block = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001048 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1049 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
Christoph Hellwigc303c5b2012-02-20 02:31:26 +00001051
1052 xlog_grant_head_init(&log->l_reserve_head);
1053 xlog_grant_head_init(&log->l_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Dave Chinnera6cb7672009-04-06 18:39:27 +02001055 error = EFSCORRUPTED;
Eric Sandeen62118702008-03-06 13:44:28 +11001056 if (xfs_sb_version_hassector(&mp->m_sb)) {
Alex Elder69ce58f2010-04-20 17:09:59 +10001057 log2_size = mp->m_sb.sb_logsectlog;
1058 if (log2_size < BBSHIFT) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001059 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1060 log2_size, BBSHIFT);
Alex Elder69ce58f2010-04-20 17:09:59 +10001061 goto out_free_log;
1062 }
1063
1064 log2_size -= BBSHIFT;
1065 if (log2_size > mp->m_sectbb_log) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001066 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1067 log2_size, mp->m_sectbb_log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001068 goto out_free_log;
1069 }
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* for larger sector sizes, must have v2 or external log */
Alex Elder69ce58f2010-04-20 17:09:59 +10001072 if (log2_size && log->l_logBBstart > 0 &&
1073 !xfs_sb_version_haslogv2(&mp->m_sb)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001074 xfs_warn(mp,
1075 "log sector size (0x%x) invalid for configuration.",
1076 log2_size);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001077 goto out_free_log;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Alex Elder69ce58f2010-04-20 17:09:59 +10001080 log->l_sectBBsize = 1 << log2_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 xlog_get_iclog_buffer_size(mp, log);
1083
Dave Chinnera6cb7672009-04-06 18:39:27 +02001084 error = ENOMEM;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +00001085 bp = xfs_buf_alloc(mp->m_logdev_targp, 0, log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001086 if (!bp)
1087 goto out_free_log;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001088 bp->b_iodone = xlog_iodone;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001089 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 log->l_xbuf = bp;
1091
Eric Sandeen007c61c2007-10-11 17:43:56 +10001092 spin_lock_init(&log->l_icloglock);
Dave Chinnereb40a872010-12-21 12:09:01 +11001093 init_waitqueue_head(&log->l_flush_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
1096 ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
1097
1098 iclogp = &log->l_iclog;
1099 /*
1100 * The amount of memory to allocate for the iclog structure is
1101 * rather funky due to the way the structure is defined. It is
1102 * done this way so that we can use different sizes for machines
1103 * with different amounts of memory. See the definition of
1104 * xlog_in_core_t in xfs_log_priv.h for details.
1105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 ASSERT(log->l_iclog_size >= 4096);
1107 for (i=0; i < log->l_iclog_bufs; i++) {
Dave Chinner644c3562008-11-10 16:50:24 +11001108 *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
1109 if (!*iclogp)
1110 goto out_free_iclog;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 iclog = *iclogp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 iclog->ic_prev = prev_iclog;
1114 prev_iclog = iclog;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001115
Dave Chinner686865f2010-09-24 20:07:47 +10001116 bp = xfs_buf_get_uncached(mp->m_logdev_targp,
1117 log->l_iclog_size, 0);
Dave Chinner644c3562008-11-10 16:50:24 +11001118 if (!bp)
1119 goto out_free_iclog;
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +02001120
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001121 bp->b_iodone = xlog_iodone;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +10001122 iclog->ic_bp = bp;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001123 iclog->ic_data = bp->b_addr;
David Chinner4679b2d2008-04-10 12:18:54 +10001124#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
David Chinner4679b2d2008-04-10 12:18:54 +10001126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 head = &iclog->ic_header;
1128 memset(head, 0, sizeof(xlog_rec_header_t));
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001129 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1130 head->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001131 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001132 head->h_size = cpu_to_be32(log->l_iclog_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* new fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001134 head->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
1138 iclog->ic_state = XLOG_STATE_ACTIVE;
1139 iclog->ic_log = log;
David Chinner114d23a2008-04-10 12:18:39 +10001140 atomic_set(&iclog->ic_refcnt, 0);
1141 spin_lock_init(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 iclog->ic_callback_tail = &(iclog->ic_callback);
Christoph Hellwigb28708d2008-11-28 14:23:38 +11001143 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001145 ASSERT(xfs_buf_islocked(iclog->ic_bp));
Dave Chinnereb40a872010-12-21 12:09:01 +11001146 init_waitqueue_head(&iclog->ic_force_wait);
1147 init_waitqueue_head(&iclog->ic_write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 iclogp = &iclog->ic_next;
1150 }
1151 *iclogp = log->l_iclog; /* complete ring */
1152 log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
1153
Dave Chinner71e330b2010-05-21 14:37:18 +10001154 error = xlog_cil_init(log);
1155 if (error)
1156 goto out_free_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return log;
Dave Chinner644c3562008-11-10 16:50:24 +11001158
1159out_free_iclog:
1160 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1161 prev_iclog = iclog->ic_next;
Dave Chinnereb40a872010-12-21 12:09:01 +11001162 if (iclog->ic_bp)
Dave Chinner644c3562008-11-10 16:50:24 +11001163 xfs_buf_free(iclog->ic_bp);
Dave Chinner644c3562008-11-10 16:50:24 +11001164 kmem_free(iclog);
1165 }
1166 spinlock_destroy(&log->l_icloglock);
Dave Chinner644c3562008-11-10 16:50:24 +11001167 xfs_buf_free(log->l_xbuf);
1168out_free_log:
1169 kmem_free(log);
Dave Chinnera6cb7672009-04-06 18:39:27 +02001170out:
1171 return ERR_PTR(-error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172} /* xlog_alloc_log */
1173
1174
1175/*
1176 * Write out the commit record of a transaction associated with the given
1177 * ticket. Return the lsn of the commit record.
1178 */
1179STATIC int
Dave Chinner55b66332010-03-23 11:43:17 +11001180xlog_commit_record(
1181 struct log *log,
1182 struct xlog_ticket *ticket,
1183 struct xlog_in_core **iclog,
1184 xfs_lsn_t *commitlsnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Dave Chinner55b66332010-03-23 11:43:17 +11001186 struct xfs_mount *mp = log->l_mp;
1187 int error;
1188 struct xfs_log_iovec reg = {
1189 .i_addr = NULL,
1190 .i_len = 0,
1191 .i_type = XLOG_REG_TYPE_COMMIT,
1192 };
1193 struct xfs_log_vec vec = {
1194 .lv_niovecs = 1,
1195 .lv_iovecp = &reg,
1196 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 ASSERT_ALWAYS(iclog);
Dave Chinner55b66332010-03-23 11:43:17 +11001199 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1200 XLOG_COMMIT_TRANS);
1201 if (error)
Nathan Scott7d04a332006-06-09 14:58:38 +10001202 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Jesper Juhl014c2542006-01-15 02:37:08 +01001203 return error;
Dave Chinner55b66332010-03-23 11:43:17 +11001204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206/*
1207 * Push on the buffer cache code if we ever use more than 75% of the on-disk
1208 * log space. This code pushes on the lsn which would supposedly free up
1209 * the 25% which we want to leave free. We may need to adopt a policy which
1210 * pushes on an lsn which is further along in the log once we reach the high
1211 * water mark. In this manner, we would be creating a low water mark.
1212 */
David Chinnera8272ce2007-11-23 16:28:09 +11001213STATIC void
Dave Chinner2ced19c2010-12-21 12:09:20 +11001214xlog_grant_push_ail(
1215 struct log *log,
1216 int need_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Dave Chinner2ced19c2010-12-21 12:09:20 +11001218 xfs_lsn_t threshold_lsn = 0;
Dave Chinner84f3c682010-12-03 22:11:29 +11001219 xfs_lsn_t last_sync_lsn;
Dave Chinner2ced19c2010-12-21 12:09:20 +11001220 int free_blocks;
1221 int free_bytes;
1222 int threshold_block;
1223 int threshold_cycle;
1224 int free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dave Chinner2ced19c2010-12-21 12:09:20 +11001226 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Christoph Hellwig28496962012-02-20 02:31:25 +00001228 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001229 free_blocks = BTOBBT(free_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Dave Chinner2ced19c2010-12-21 12:09:20 +11001231 /*
1232 * Set the threshold for the minimum number of free blocks in the
1233 * log to the maximum of what the caller needs, one quarter of the
1234 * log, and 256 blocks.
1235 */
1236 free_threshold = BTOBB(need_bytes);
1237 free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2));
1238 free_threshold = MAX(free_threshold, 256);
1239 if (free_blocks >= free_threshold)
1240 return;
1241
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001242 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1243 &threshold_block);
1244 threshold_block += free_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (threshold_block >= log->l_logBBsize) {
Dave Chinner2ced19c2010-12-21 12:09:20 +11001246 threshold_block -= log->l_logBBsize;
1247 threshold_cycle += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Dave Chinner2ced19c2010-12-21 12:09:20 +11001249 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1250 threshold_block);
1251 /*
1252 * Don't pass in an lsn greater than the lsn of the last
Dave Chinner84f3c682010-12-03 22:11:29 +11001253 * log record known to be on disk. Use a snapshot of the last sync lsn
1254 * so that it doesn't change between the compare and the set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
Dave Chinner84f3c682010-12-03 22:11:29 +11001256 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1257 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1258 threshold_lsn = last_sync_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Dave Chinner2ced19c2010-12-21 12:09:20 +11001260 /*
1261 * Get the transaction layer to kick the dirty buffers out to
1262 * disk asynchronously. No point in trying to do this if
1263 * the filesystem is shutting down.
1264 */
1265 if (!XLOG_FORCED_SHUTDOWN(log))
Dave Chinnerfd074842011-04-08 12:45:07 +10001266 xfs_ail_push(log->l_ailp, threshold_lsn);
Dave Chinner2ced19c2010-12-21 12:09:20 +11001267}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Christoph Hellwig873ff552010-01-13 22:17:57 +00001269/*
1270 * The bdstrat callback function for log bufs. This gives us a central
1271 * place to trap bufs in case we get hit by a log I/O error and need to
1272 * shutdown. Actually, in practice, even when we didn't get a log error,
1273 * we transition the iclogs to IOERROR state *after* flushing all existing
1274 * iclogs to disk. This is because we don't want anymore new transactions to be
1275 * started or completed afterwards.
1276 */
1277STATIC int
1278xlog_bdstrat(
1279 struct xfs_buf *bp)
1280{
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001281 struct xlog_in_core *iclog = bp->b_fspriv;
Christoph Hellwig873ff552010-01-13 22:17:57 +00001282
Christoph Hellwig873ff552010-01-13 22:17:57 +00001283 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001284 xfs_buf_ioerror(bp, EIO);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001285 xfs_buf_stale(bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001286 xfs_buf_ioend(bp, 0);
Christoph Hellwig873ff552010-01-13 22:17:57 +00001287 /*
1288 * It would seem logical to return EIO here, but we rely on
1289 * the log state machine to propagate I/O errors instead of
1290 * doing it here.
1291 */
1292 return 0;
1293 }
1294
Christoph Hellwig873ff552010-01-13 22:17:57 +00001295 xfs_buf_iorequest(bp);
1296 return 0;
1297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299/*
1300 * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
1301 * fashion. Previously, we should have moved the current iclog
1302 * ptr in the log to point to the next available iclog. This allows further
1303 * write to continue while this code syncs out an iclog ready to go.
1304 * Before an in-core log can be written out, the data section must be scanned
1305 * to save away the 1st word of each BBSIZE block into the header. We replace
1306 * it with the current cycle count. Each BBSIZE block is tagged with the
1307 * cycle count because there in an implicit assumption that drives will
1308 * guarantee that entire 512 byte blocks get written at once. In other words,
1309 * we can't have part of a 512 byte block written and part not written. By
1310 * tagging each block, we will know which blocks are valid when recovering
1311 * after an unclean shutdown.
1312 *
1313 * This routine is single threaded on the iclog. No other thread can be in
1314 * this routine with the same iclog. Changing contents of iclog can there-
1315 * fore be done without grabbing the state machine lock. Updating the global
1316 * log will require grabbing the lock though.
1317 *
1318 * The entire log manager uses a logical block numbering scheme. Only
1319 * log_sync (and then only bwrite()) know about the fact that the log may
1320 * not start with block zero on a given device. The log block start offset
1321 * is added immediately before calling bwrite().
1322 */
1323
David Chinnera8272ce2007-11-23 16:28:09 +11001324STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325xlog_sync(xlog_t *log,
1326 xlog_in_core_t *iclog)
1327{
1328 xfs_caddr_t dptr; /* pointer to byte sized element */
1329 xfs_buf_t *bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001330 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 uint count; /* byte count of bwrite */
1332 uint count_init; /* initial count before roundup */
1333 int roundoff; /* roundoff to BB or stripe */
1334 int split = 0; /* split write into two regions */
1335 int error;
Eric Sandeen62118702008-03-06 13:44:28 +11001336 int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 XFS_STATS_INC(xs_log_writes);
David Chinner155cc6b2008-03-06 13:44:14 +11001339 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /* Add for LR header */
1342 count_init = log->l_iclog_hsize + iclog->ic_offset;
1343
1344 /* Round out the log write size */
1345 if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
1346 /* we have a v2 stripe unit to use */
1347 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1348 } else {
1349 count = BBTOB(BTOBB(count_init));
1350 }
1351 roundoff = count - count_init;
1352 ASSERT(roundoff >= 0);
1353 ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
1354 roundoff < log->l_mp->m_sb.sb_logsunit)
1355 ||
1356 (log->l_mp->m_sb.sb_logsunit <= 1 &&
1357 roundoff < BBTOB(1)));
1358
1359 /* move grant heads by roundoff in sync */
Christoph Hellwig28496962012-02-20 02:31:25 +00001360 xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
1361 xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /* put cycle number in every block */
1364 xlog_pack_data(log, iclog, roundoff);
1365
1366 /* real byte length */
1367 if (v2) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001368 iclog->ic_header.h_len =
1369 cpu_to_be32(iclog->ic_offset + roundoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001371 iclog->ic_header.h_len =
1372 cpu_to_be32(iclog->ic_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
Nathan Scottf5faad72006-07-28 17:04:44 +10001375 bp = iclog->ic_bp;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001376 XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 XFS_STATS_ADD(xs_log_blocks, BTOBB(count));
1379
1380 /* Do we need to split this write into 2 parts? */
1381 if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
1382 split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
1383 count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
1384 iclog->ic_bwritecnt = 2; /* split into 2 writes */
1385 } else {
1386 iclog->ic_bwritecnt = 1;
1387 }
David Chinner511105b2007-05-24 15:21:57 +10001388 XFS_BUF_SET_COUNT(bp, count);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001389 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001390 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001392 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwig651701d2010-06-28 10:34:34 -04001393
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001394 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) {
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001395 bp->b_flags |= XBF_FUA;
1396
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001397 /*
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001398 * Flush the data device before flushing the log to make
1399 * sure all meta data written back from the AIL actually made
1400 * it to disk before stamping the new log tail LSN into the
1401 * log buffer. For an external log we need to issue the
1402 * flush explicitly, and unfortunately synchronously here;
1403 * for an internal log we can simply use the block layer
1404 * state machine for preflushes.
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001405 */
1406 if (log->l_mp->m_logdev_targp != log->l_mp->m_ddev_targp)
1407 xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001408 else
1409 bp->b_flags |= XBF_FLUSH;
Christoph Hellwiga27a2632011-06-16 12:02:23 +00001410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1413 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1414
1415 xlog_verify_iclog(log, iclog, count, B_TRUE);
1416
1417 /* account for log which doesn't start at block #0 */
1418 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1419 /*
1420 * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
1421 * is shutting down.
1422 */
1423 XFS_BUF_WRITE(bp);
1424
Christoph Hellwig901796a2011-10-10 16:52:49 +00001425 error = xlog_bdstrat(bp);
1426 if (error) {
1427 xfs_buf_ioerror_alert(bp, "xlog_sync");
Jesper Juhl014c2542006-01-15 02:37:08 +01001428 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 if (split) {
Nathan Scottf5faad72006-07-28 17:04:44 +10001431 bp = iclog->ic_log->l_xbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
Chandra Seetharaman02fe03d2011-07-22 23:40:22 +00001433 xfs_buf_associate_memory(bp,
1434 (char *)&iclog->ic_header + count, split);
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001435 bp->b_fspriv = iclog;
Nathan Scottf5faad72006-07-28 17:04:44 +10001436 XFS_BUF_ZEROFLAGS(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 XFS_BUF_ASYNC(bp);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001438 bp->b_flags |= XBF_SYNCIO;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001439 if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwige163cbd2011-07-08 14:36:36 +02001440 bp->b_flags |= XBF_FUA;
Chandra Seetharaman62926042011-07-22 23:40:15 +00001441 dptr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /*
1443 * Bump the cycle numbers at the start of each block
1444 * since this part of the buffer is at the start of
1445 * a new cycle. Watch out for the header magic number
1446 * case, though.
1447 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001448 for (i = 0; i < split; i += BBSIZE) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001449 be32_add_cpu((__be32 *)dptr, 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001450 if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001451 be32_add_cpu((__be32 *)dptr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 dptr += BBSIZE;
1453 }
1454
1455 ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
1456 ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
1457
Nathan Scottc41564b2006-03-29 08:55:14 +10001458 /* account for internal log which doesn't start at block #0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
1460 XFS_BUF_WRITE(bp);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001461 error = xlog_bdstrat(bp);
1462 if (error) {
1463 xfs_buf_ioerror_alert(bp, "xlog_sync (split)");
Jesper Juhl014c2542006-01-15 02:37:08 +01001464 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468} /* xlog_sync */
1469
1470
1471/*
Nathan Scottc41564b2006-03-29 08:55:14 +10001472 * Deallocate a log structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
David Chinnera8272ce2007-11-23 16:28:09 +11001474STATIC void
Nathan Scottc41564b2006-03-29 08:55:14 +10001475xlog_dealloc_log(xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 xlog_in_core_t *iclog, *next_iclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 int i;
1479
Dave Chinner71e330b2010-05-21 14:37:18 +10001480 xlog_cil_destroy(log);
1481
Dave Chinner44396472011-04-21 09:34:27 +00001482 /*
1483 * always need to ensure that the extra buffer does not point to memory
1484 * owned by another log buffer before we free it.
1485 */
1486 xfs_buf_set_empty(log->l_xbuf, log->l_iclog_size);
1487 xfs_buf_free(log->l_xbuf);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 iclog = log->l_iclog;
1490 for (i=0; i<log->l_iclog_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfs_buf_free(iclog->ic_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 next_iclog = iclog->ic_next;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001493 kmem_free(iclog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 iclog = next_iclog;
1495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 spinlock_destroy(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 log->l_mp->m_log = NULL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001499 kmem_free(log);
Nathan Scottc41564b2006-03-29 08:55:14 +10001500} /* xlog_dealloc_log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502/*
1503 * Update counters atomically now that memcpy is done.
1504 */
1505/* ARGSUSED */
1506static inline void
1507xlog_state_finish_copy(xlog_t *log,
1508 xlog_in_core_t *iclog,
1509 int record_cnt,
1510 int copy_bytes)
1511{
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001512 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001514 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 iclog->ic_offset += copy_bytes;
1516
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10001517 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518} /* xlog_state_finish_copy */
1519
1520
1521
1522
1523/*
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001524 * print out info relating to regions written which consume
1525 * the reservation
1526 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001527void
1528xlog_print_tic_res(
1529 struct xfs_mount *mp,
1530 struct xlog_ticket *ticket)
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001531{
1532 uint i;
1533 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1534
1535 /* match with XLOG_REG_TYPE_* in xfs_log.h */
1536 static char *res_type_str[XLOG_REG_TYPE_MAX] = {
1537 "bformat",
1538 "bchunk",
1539 "efi_format",
1540 "efd_format",
1541 "iformat",
1542 "icore",
1543 "iext",
1544 "ibroot",
1545 "ilocal",
1546 "iattr_ext",
1547 "iattr_broot",
1548 "iattr_local",
1549 "qformat",
1550 "dquot",
1551 "quotaoff",
1552 "LR header",
1553 "unmount",
1554 "commit",
1555 "trans header"
1556 };
1557 static char *trans_type_str[XFS_TRANS_TYPE_MAX] = {
1558 "SETATTR_NOT_SIZE",
1559 "SETATTR_SIZE",
1560 "INACTIVE",
1561 "CREATE",
1562 "CREATE_TRUNC",
1563 "TRUNCATE_FILE",
1564 "REMOVE",
1565 "LINK",
1566 "RENAME",
1567 "MKDIR",
1568 "RMDIR",
1569 "SYMLINK",
1570 "SET_DMATTRS",
1571 "GROWFS",
1572 "STRAT_WRITE",
1573 "DIOSTRAT",
1574 "WRITE_SYNC",
1575 "WRITEID",
1576 "ADDAFORK",
1577 "ATTRINVAL",
1578 "ATRUNCATE",
1579 "ATTR_SET",
1580 "ATTR_RM",
1581 "ATTR_FLAG",
1582 "CLEAR_AGI_BUCKET",
1583 "QM_SBCHANGE",
1584 "DUMMY1",
1585 "DUMMY2",
1586 "QM_QUOTAOFF",
1587 "QM_DQALLOC",
1588 "QM_SETQLIM",
1589 "QM_DQCLUSTER",
1590 "QM_QINOCREATE",
1591 "QM_QUOTAOFF_END",
1592 "SB_UNIT",
1593 "FSYNC_TS",
1594 "GROWFSRT_ALLOC",
1595 "GROWFSRT_ZERO",
1596 "GROWFSRT_FREE",
1597 "SWAPEXT"
1598 };
1599
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001600 xfs_warn(mp,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001601 "xlog_write: reservation summary:\n"
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001602 " trans type = %s (%u)\n"
1603 " unit res = %d bytes\n"
1604 " current res = %d bytes\n"
1605 " total reg = %u bytes (o/flow = %u bytes)\n"
1606 " ophdrs = %u (ophdr space = %u bytes)\n"
1607 " ophdr + reg = %u bytes\n"
1608 " num regions = %u\n",
1609 ((ticket->t_trans_type <= 0 ||
1610 ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ?
1611 "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]),
1612 ticket->t_trans_type,
1613 ticket->t_unit_res,
1614 ticket->t_curr_res,
1615 ticket->t_res_arr_sum, ticket->t_res_o_flow,
1616 ticket->t_res_num_ophdrs, ophdr_spc,
1617 ticket->t_res_arr_sum +
1618 ticket->t_res_o_flow + ophdr_spc,
1619 ticket->t_res_num);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001620
1621 for (i = 0; i < ticket->t_res_num; i++) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001622 uint r_type = ticket->t_res_arr[i].r_type;
1623 xfs_warn(mp, "region[%u]: %s - %u bytes\n", i,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001624 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
1625 "bad-rtype" : res_type_str[r_type-1]),
1626 ticket->t_res_arr[i].r_len);
1627 }
Dave Chinner169a7b02010-05-07 11:05:31 +10001628
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001629 xfs_alert_tag(mp, XFS_PTAG_LOGRES,
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001630 "xlog_write: reservation ran out. Need to up reservation");
Dave Chinner169a7b02010-05-07 11:05:31 +10001631 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001632}
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001633
1634/*
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001635 * Calculate the potential space needed by the log vector. Each region gets
1636 * its own xlog_op_header_t and may need to be double word aligned.
1637 */
1638static int
1639xlog_write_calc_vec_length(
1640 struct xlog_ticket *ticket,
Dave Chinner55b66332010-03-23 11:43:17 +11001641 struct xfs_log_vec *log_vector)
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001642{
Dave Chinner55b66332010-03-23 11:43:17 +11001643 struct xfs_log_vec *lv;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001644 int headers = 0;
1645 int len = 0;
1646 int i;
1647
1648 /* acct for start rec of xact */
1649 if (ticket->t_flags & XLOG_TIC_INITED)
1650 headers++;
1651
Dave Chinner55b66332010-03-23 11:43:17 +11001652 for (lv = log_vector; lv; lv = lv->lv_next) {
1653 headers += lv->lv_niovecs;
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001654
Dave Chinner55b66332010-03-23 11:43:17 +11001655 for (i = 0; i < lv->lv_niovecs; i++) {
1656 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
1657
1658 len += vecp->i_len;
1659 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
1660 }
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001661 }
1662
1663 ticket->t_res_num_ophdrs += headers;
1664 len += headers * sizeof(struct xlog_op_header);
1665
1666 return len;
1667}
1668
1669/*
1670 * If first write for transaction, insert start record We can't be trying to
1671 * commit if we are inited. We can't have any "partial_copy" if we are inited.
1672 */
1673static int
1674xlog_write_start_rec(
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001675 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001676 struct xlog_ticket *ticket)
1677{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001678 if (!(ticket->t_flags & XLOG_TIC_INITED))
1679 return 0;
1680
1681 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1682 ophdr->oh_clientid = ticket->t_clientid;
1683 ophdr->oh_len = 0;
1684 ophdr->oh_flags = XLOG_START_TRANS;
1685 ophdr->oh_res2 = 0;
1686
1687 ticket->t_flags &= ~XLOG_TIC_INITED;
1688
1689 return sizeof(struct xlog_op_header);
1690}
1691
1692static xlog_op_header_t *
1693xlog_write_setup_ophdr(
1694 struct log *log,
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001695 struct xlog_op_header *ophdr,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001696 struct xlog_ticket *ticket,
1697 uint flags)
1698{
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001699 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
1700 ophdr->oh_clientid = ticket->t_clientid;
1701 ophdr->oh_res2 = 0;
1702
1703 /* are we copying a commit or unmount record? */
1704 ophdr->oh_flags = flags;
1705
1706 /*
1707 * We've seen logs corrupted with bad transaction client ids. This
1708 * makes sure that XFS doesn't generate them on. Turn this into an EIO
1709 * and shut down the filesystem.
1710 */
1711 switch (ophdr->oh_clientid) {
1712 case XFS_TRANSACTION:
1713 case XFS_VOLUME:
1714 case XFS_LOG:
1715 break;
1716 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001717 xfs_warn(log->l_mp,
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001718 "Bad XFS transaction clientid 0x%x in ticket 0x%p",
1719 ophdr->oh_clientid, ticket);
1720 return NULL;
1721 }
1722
1723 return ophdr;
1724}
1725
1726/*
1727 * Set up the parameters of the region copy into the log. This has
1728 * to handle region write split across multiple log buffers - this
1729 * state is kept external to this function so that this code can
1730 * can be written in an obvious, self documenting manner.
1731 */
1732static int
1733xlog_write_setup_copy(
1734 struct xlog_ticket *ticket,
1735 struct xlog_op_header *ophdr,
1736 int space_available,
1737 int space_required,
1738 int *copy_off,
1739 int *copy_len,
1740 int *last_was_partial_copy,
1741 int *bytes_consumed)
1742{
1743 int still_to_copy;
1744
1745 still_to_copy = space_required - *bytes_consumed;
1746 *copy_off = *bytes_consumed;
1747
1748 if (still_to_copy <= space_available) {
1749 /* write of region completes here */
1750 *copy_len = still_to_copy;
1751 ophdr->oh_len = cpu_to_be32(*copy_len);
1752 if (*last_was_partial_copy)
1753 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
1754 *last_was_partial_copy = 0;
1755 *bytes_consumed = 0;
1756 return 0;
1757 }
1758
1759 /* partial write of region, needs extra log op header reservation */
1760 *copy_len = space_available;
1761 ophdr->oh_len = cpu_to_be32(*copy_len);
1762 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
1763 if (*last_was_partial_copy)
1764 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
1765 *bytes_consumed += *copy_len;
1766 (*last_was_partial_copy)++;
1767
1768 /* account for new log op header */
1769 ticket->t_curr_res -= sizeof(struct xlog_op_header);
1770 ticket->t_res_num_ophdrs++;
1771
1772 return sizeof(struct xlog_op_header);
1773}
1774
1775static int
1776xlog_write_copy_finish(
1777 struct log *log,
1778 struct xlog_in_core *iclog,
1779 uint flags,
1780 int *record_cnt,
1781 int *data_cnt,
1782 int *partial_copy,
1783 int *partial_copy_len,
1784 int log_offset,
1785 struct xlog_in_core **commit_iclog)
1786{
1787 if (*partial_copy) {
1788 /*
1789 * This iclog has already been marked WANT_SYNC by
1790 * xlog_state_get_iclog_space.
1791 */
1792 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1793 *record_cnt = 0;
1794 *data_cnt = 0;
1795 return xlog_state_release_iclog(log, iclog);
1796 }
1797
1798 *partial_copy = 0;
1799 *partial_copy_len = 0;
1800
1801 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
1802 /* no more space in this iclog - push it. */
1803 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
1804 *record_cnt = 0;
1805 *data_cnt = 0;
1806
1807 spin_lock(&log->l_icloglock);
1808 xlog_state_want_sync(log, iclog);
1809 spin_unlock(&log->l_icloglock);
1810
1811 if (!commit_iclog)
1812 return xlog_state_release_iclog(log, iclog);
1813 ASSERT(flags & XLOG_COMMIT_TRANS);
1814 *commit_iclog = iclog;
1815 }
1816
1817 return 0;
1818}
1819
1820/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * Write some region out to in-core log
1822 *
1823 * This will be called when writing externally provided regions or when
1824 * writing out a commit record for a given transaction.
1825 *
1826 * General algorithm:
1827 * 1. Find total length of this write. This may include adding to the
1828 * lengths passed in.
1829 * 2. Check whether we violate the tickets reservation.
1830 * 3. While writing to this iclog
1831 * A. Reserve as much space in this iclog as can get
1832 * B. If this is first write, save away start lsn
1833 * C. While writing this region:
1834 * 1. If first write of transaction, write start record
1835 * 2. Write log operation header (header per region)
1836 * 3. Find out if we can fit entire region into this iclog
1837 * 4. Potentially, verify destination memcpy ptr
1838 * 5. Memcpy (partial) region
1839 * 6. If partial copy, release iclog; otherwise, continue
1840 * copying more regions into current iclog
1841 * 4. Mark want sync bit (in simulation mode)
1842 * 5. Release iclog for potential flush to on-disk log.
1843 *
1844 * ERRORS:
1845 * 1. Panic if reservation is overrun. This should never happen since
1846 * reservation amounts are generated internal to the filesystem.
1847 * NOTES:
1848 * 1. Tickets are single threaded data structures.
1849 * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
1850 * syncing routine. When a single log_write region needs to span
1851 * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
1852 * on all log operation writes which don't contain the end of the
1853 * region. The XLOG_END_TRANS bit is used for the in-core log
1854 * operation which contains the end of the continued log_write region.
1855 * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
1856 * we don't really know exactly how much space will be used. As a result,
1857 * we don't update ic_offset until the end when we know exactly how many
1858 * bytes have been written out.
1859 */
Dave Chinner71e330b2010-05-21 14:37:18 +10001860int
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001861xlog_write(
Dave Chinner55b66332010-03-23 11:43:17 +11001862 struct log *log,
1863 struct xfs_log_vec *log_vector,
Christoph Hellwig35a8a722010-02-15 23:34:54 +00001864 struct xlog_ticket *ticket,
1865 xfs_lsn_t *start_lsn,
1866 struct xlog_in_core **commit_iclog,
1867 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001869 struct xlog_in_core *iclog = NULL;
Dave Chinner55b66332010-03-23 11:43:17 +11001870 struct xfs_log_iovec *vecp;
1871 struct xfs_log_vec *lv;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001872 int len;
1873 int index;
1874 int partial_copy = 0;
1875 int partial_copy_len = 0;
1876 int contwr = 0;
1877 int record_cnt = 0;
1878 int data_cnt = 0;
1879 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001881 *start_lsn = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Dave Chinner55b66332010-03-23 11:43:17 +11001883 len = xlog_write_calc_vec_length(ticket, log_vector);
Dave Chinner71e330b2010-05-21 14:37:18 +10001884
Christoph Hellwig93b8a582011-12-06 21:58:07 +00001885 /*
1886 * Region headers and bytes are already accounted for.
1887 * We only need to take into account start records and
1888 * split regions in this function.
1889 */
1890 if (ticket->t_flags & XLOG_TIC_INITED)
1891 ticket->t_curr_res -= sizeof(xlog_op_header_t);
1892
1893 /*
1894 * Commit record headers need to be accounted for. These
1895 * come in as separate writes so are easy to detect.
1896 */
1897 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
1898 ticket->t_curr_res -= sizeof(xlog_op_header_t);
Dave Chinner71e330b2010-05-21 14:37:18 +10001899
1900 if (ticket->t_curr_res < 0)
Dave Chinner55b66332010-03-23 11:43:17 +11001901 xlog_print_tic_res(log->l_mp, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Dave Chinner55b66332010-03-23 11:43:17 +11001903 index = 0;
1904 lv = log_vector;
1905 vecp = lv->lv_iovecp;
1906 while (lv && index < lv->lv_niovecs) {
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001907 void *ptr;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001908 int log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001910 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
1911 &contwr, &log_offset);
1912 if (error)
1913 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001915 ASSERT(log_offset <= iclog->ic_size - 1);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001916 ptr = iclog->ic_datap + log_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001918 /* start_lsn is the first lsn written to. That's all we need. */
1919 if (!*start_lsn)
1920 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001922 /*
1923 * This loop writes out as many regions as can fit in the amount
1924 * of space which was allocated by xlog_state_get_iclog_space().
1925 */
Dave Chinner55b66332010-03-23 11:43:17 +11001926 while (lv && index < lv->lv_niovecs) {
1927 struct xfs_log_iovec *reg = &vecp[index];
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001928 struct xlog_op_header *ophdr;
1929 int start_rec_copy;
1930 int copy_len;
1931 int copy_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Dave Chinner55b66332010-03-23 11:43:17 +11001933 ASSERT(reg->i_len % sizeof(__int32_t) == 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001934 ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001936 start_rec_copy = xlog_write_start_rec(ptr, ticket);
1937 if (start_rec_copy) {
1938 record_cnt++;
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001939 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001940 start_rec_copy);
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001943 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
1944 if (!ophdr)
1945 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001947 xlog_write_adv_cnt(&ptr, &len, &log_offset,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001948 sizeof(struct xlog_op_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001950 len += xlog_write_setup_copy(ticket, ophdr,
1951 iclog->ic_size-log_offset,
Dave Chinner55b66332010-03-23 11:43:17 +11001952 reg->i_len,
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001953 &copy_off, &copy_len,
1954 &partial_copy,
1955 &partial_copy_len);
1956 xlog_verify_dest_ptr(log, ptr);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001957
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001958 /* copy region */
1959 ASSERT(copy_len >= 0);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11001960 memcpy(ptr, reg->i_addr + copy_off, copy_len);
1961 xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
Dave Chinnerb5203cd2010-03-23 11:29:44 +11001962
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001963 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
1964 record_cnt++;
1965 data_cnt += contwr ? copy_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001967 error = xlog_write_copy_finish(log, iclog, flags,
1968 &record_cnt, &data_cnt,
1969 &partial_copy,
1970 &partial_copy_len,
1971 log_offset,
1972 commit_iclog);
1973 if (error)
1974 return error;
1975
1976 /*
1977 * if we had a partial copy, we need to get more iclog
1978 * space but we don't want to increment the region
1979 * index because there is still more is this region to
1980 * write.
1981 *
1982 * If we completed writing this region, and we flushed
1983 * the iclog (indicated by resetting of the record
1984 * count), then we also need to get more log space. If
1985 * this was the last record, though, we are done and
1986 * can just return.
1987 */
1988 if (partial_copy)
1989 break;
1990
Dave Chinner55b66332010-03-23 11:43:17 +11001991 if (++index == lv->lv_niovecs) {
1992 lv = lv->lv_next;
1993 index = 0;
1994 if (lv)
1995 vecp = lv->lv_iovecp;
1996 }
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001997 if (record_cnt == 0) {
Dave Chinner55b66332010-03-23 11:43:17 +11001998 if (!lv)
Christoph Hellwig99428ad2010-03-23 11:35:45 +11001999 return 0;
2000 break;
2001 }
2002 }
2003 }
2004
2005 ASSERT(len == 0);
2006
2007 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2008 if (!commit_iclog)
2009 return xlog_state_release_iclog(log, iclog);
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 ASSERT(flags & XLOG_COMMIT_TRANS);
2012 *commit_iclog = iclog;
2013 return 0;
Christoph Hellwig99428ad2010-03-23 11:35:45 +11002014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016
2017/*****************************************************************************
2018 *
2019 * State Machine functions
2020 *
2021 *****************************************************************************
2022 */
2023
2024/* Clean iclogs starting from the head. This ordering must be
2025 * maintained, so an iclog doesn't become ACTIVE beyond one that
2026 * is SYNCING. This is also required to maintain the notion that we use
David Chinner12017fa2008-08-13 16:34:31 +10002027 * a ordered wait queue to hold off would be writers to the log when every
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 * iclog is trying to sync to disk.
2029 *
2030 * State Change: DIRTY -> ACTIVE
2031 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002032STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033xlog_state_clean_log(xlog_t *log)
2034{
2035 xlog_in_core_t *iclog;
2036 int changed = 0;
2037
2038 iclog = log->l_iclog;
2039 do {
2040 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2041 iclog->ic_state = XLOG_STATE_ACTIVE;
2042 iclog->ic_offset = 0;
David Chinner114d23a2008-04-10 12:18:39 +10002043 ASSERT(iclog->ic_callback == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /*
2045 * If the number of ops in this iclog indicate it just
2046 * contains the dummy transaction, we can
2047 * change state into IDLE (the second time around).
2048 * Otherwise we should change the state into
2049 * NEED a dummy.
2050 * We don't need to cover the dummy.
2051 */
2052 if (!changed &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002053 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2054 XLOG_COVER_OPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 changed = 1;
2056 } else {
2057 /*
2058 * We have two dirty iclogs so start over
2059 * This could also be num of ops indicates
2060 * this is not the dummy going out.
2061 */
2062 changed = 2;
2063 }
2064 iclog->ic_header.h_num_logops = 0;
2065 memset(iclog->ic_header.h_cycle_data, 0,
2066 sizeof(iclog->ic_header.h_cycle_data));
2067 iclog->ic_header.h_lsn = 0;
2068 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2069 /* do nothing */;
2070 else
2071 break; /* stop cleaning */
2072 iclog = iclog->ic_next;
2073 } while (iclog != log->l_iclog);
2074
2075 /* log is locked when we are called */
2076 /*
2077 * Change state for the dummy log recording.
2078 * We usually go to NEED. But we go to NEED2 if the changed indicates
2079 * we are done writing the dummy record.
2080 * If we are done with the second dummy recored (DONE2), then
2081 * we go to IDLE.
2082 */
2083 if (changed) {
2084 switch (log->l_covered_state) {
2085 case XLOG_STATE_COVER_IDLE:
2086 case XLOG_STATE_COVER_NEED:
2087 case XLOG_STATE_COVER_NEED2:
2088 log->l_covered_state = XLOG_STATE_COVER_NEED;
2089 break;
2090
2091 case XLOG_STATE_COVER_DONE:
2092 if (changed == 1)
2093 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2094 else
2095 log->l_covered_state = XLOG_STATE_COVER_NEED;
2096 break;
2097
2098 case XLOG_STATE_COVER_DONE2:
2099 if (changed == 1)
2100 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2101 else
2102 log->l_covered_state = XLOG_STATE_COVER_NEED;
2103 break;
2104
2105 default:
2106 ASSERT(0);
2107 }
2108 }
2109} /* xlog_state_clean_log */
2110
2111STATIC xfs_lsn_t
2112xlog_get_lowest_lsn(
2113 xlog_t *log)
2114{
2115 xlog_in_core_t *lsn_log;
2116 xfs_lsn_t lowest_lsn, lsn;
2117
2118 lsn_log = log->l_iclog;
2119 lowest_lsn = 0;
2120 do {
2121 if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002122 lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if ((lsn && !lowest_lsn) ||
2124 (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
2125 lowest_lsn = lsn;
2126 }
2127 }
2128 lsn_log = lsn_log->ic_next;
2129 } while (lsn_log != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002130 return lowest_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133
2134STATIC void
2135xlog_state_do_callback(
2136 xlog_t *log,
2137 int aborted,
2138 xlog_in_core_t *ciclog)
2139{
2140 xlog_in_core_t *iclog;
2141 xlog_in_core_t *first_iclog; /* used to know when we've
2142 * processed all iclogs once */
2143 xfs_log_callback_t *cb, *cb_next;
2144 int flushcnt = 0;
2145 xfs_lsn_t lowest_lsn;
2146 int ioerrors; /* counter: iclogs with errors */
2147 int loopdidcallbacks; /* flag: inner loop did callbacks*/
2148 int funcdidcallbacks; /* flag: function did callbacks */
2149 int repeats; /* for issuing console warnings if
2150 * looping too many times */
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002151 int wake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002153 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 first_iclog = iclog = log->l_iclog;
2155 ioerrors = 0;
2156 funcdidcallbacks = 0;
2157 repeats = 0;
2158
2159 do {
2160 /*
2161 * Scan all iclogs starting with the one pointed to by the
2162 * log. Reset this starting point each time the log is
2163 * unlocked (during callbacks).
2164 *
2165 * Keep looping through iclogs until one full pass is made
2166 * without running any callbacks.
2167 */
2168 first_iclog = log->l_iclog;
2169 iclog = log->l_iclog;
2170 loopdidcallbacks = 0;
2171 repeats++;
2172
2173 do {
2174
2175 /* skip all iclogs in the ACTIVE & DIRTY states */
2176 if (iclog->ic_state &
2177 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2178 iclog = iclog->ic_next;
2179 continue;
2180 }
2181
2182 /*
2183 * Between marking a filesystem SHUTDOWN and stopping
2184 * the log, we do flush all iclogs to disk (if there
2185 * wasn't a log I/O error). So, we do want things to
2186 * go smoothly in case of just a SHUTDOWN w/o a
2187 * LOG_IO_ERROR.
2188 */
2189 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2190 /*
2191 * Can only perform callbacks in order. Since
2192 * this iclog is not in the DONE_SYNC/
2193 * DO_CALLBACK state, we skip the rest and
2194 * just try to clean up. If we set our iclog
2195 * to DO_CALLBACK, we will not process it when
2196 * we retry since a previous iclog is in the
2197 * CALLBACK and the state cannot change since
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002198 * we are holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 */
2200 if (!(iclog->ic_state &
2201 (XLOG_STATE_DONE_SYNC |
2202 XLOG_STATE_DO_CALLBACK))) {
2203 if (ciclog && (ciclog->ic_state ==
2204 XLOG_STATE_DONE_SYNC)) {
2205 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2206 }
2207 break;
2208 }
2209 /*
2210 * We now have an iclog that is in either the
2211 * DO_CALLBACK or DONE_SYNC states. The other
2212 * states (WANT_SYNC, SYNCING, or CALLBACK were
2213 * caught by the above if and are going to
2214 * clean (i.e. we aren't doing their callbacks)
2215 * see the above if.
2216 */
2217
2218 /*
2219 * We will do one more check here to see if we
2220 * have chased our tail around.
2221 */
2222
2223 lowest_lsn = xlog_get_lowest_lsn(log);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002224 if (lowest_lsn &&
2225 XFS_LSN_CMP(lowest_lsn,
Dave Chinner84f3c682010-12-03 22:11:29 +11002226 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 iclog = iclog->ic_next;
2228 continue; /* Leave this iclog for
2229 * another thread */
2230 }
2231
2232 iclog->ic_state = XLOG_STATE_CALLBACK;
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Dave Chinner84f3c682010-12-03 22:11:29 +11002235 /*
2236 * update the last_sync_lsn before we drop the
2237 * icloglock to ensure we are the only one that
2238 * can update it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 */
Dave Chinner84f3c682010-12-03 22:11:29 +11002240 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2241 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2242 atomic64_set(&log->l_last_sync_lsn,
2243 be64_to_cpu(iclog->ic_header.h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Dave Chinner84f3c682010-12-03 22:11:29 +11002245 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 ioerrors++;
Dave Chinner84f3c682010-12-03 22:11:29 +11002247
2248 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
David Chinner114d23a2008-04-10 12:18:39 +10002250 /*
2251 * Keep processing entries in the callback list until
2252 * we come around and it is empty. We need to
2253 * atomically see that the list is empty and change the
2254 * state to DIRTY so that we don't miss any more
2255 * callbacks being added.
2256 */
2257 spin_lock(&iclog->ic_callback_lock);
2258 cb = iclog->ic_callback;
Christoph Hellwig4b809162007-08-16 15:37:36 +10002259 while (cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 iclog->ic_callback_tail = &(iclog->ic_callback);
2261 iclog->ic_callback = NULL;
David Chinner114d23a2008-04-10 12:18:39 +10002262 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 /* perform callbacks in the order given */
Christoph Hellwig4b809162007-08-16 15:37:36 +10002265 for (; cb; cb = cb_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 cb_next = cb->cb_next;
2267 cb->cb_func(cb->cb_arg, aborted);
2268 }
David Chinner114d23a2008-04-10 12:18:39 +10002269 spin_lock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 cb = iclog->ic_callback;
2271 }
2272
2273 loopdidcallbacks++;
2274 funcdidcallbacks++;
2275
David Chinner114d23a2008-04-10 12:18:39 +10002276 spin_lock(&log->l_icloglock);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002277 ASSERT(iclog->ic_callback == NULL);
David Chinner114d23a2008-04-10 12:18:39 +10002278 spin_unlock(&iclog->ic_callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2280 iclog->ic_state = XLOG_STATE_DIRTY;
2281
2282 /*
2283 * Transition from DIRTY to ACTIVE if applicable.
2284 * NOP if STATE_IOERROR.
2285 */
2286 xlog_state_clean_log(log);
2287
2288 /* wake up threads waiting in xfs_log_force() */
Dave Chinnereb40a872010-12-21 12:09:01 +11002289 wake_up_all(&iclog->ic_force_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 iclog = iclog->ic_next;
2292 } while (first_iclog != iclog);
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002293
2294 if (repeats > 5000) {
2295 flushcnt += repeats;
2296 repeats = 0;
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002297 xfs_warn(log->l_mp,
Nathan Scotta3c6685e2006-09-28 11:02:14 +10002298 "%s: possible infinite loop (%d iterations)",
Harvey Harrison34a622b2008-04-10 12:19:21 +10002299 __func__, flushcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 } while (!ioerrors && loopdidcallbacks);
2302
2303 /*
2304 * make one last gasp attempt to see if iclogs are being left in
2305 * limbo..
2306 */
2307#ifdef DEBUG
2308 if (funcdidcallbacks) {
2309 first_iclog = iclog = log->l_iclog;
2310 do {
2311 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2312 /*
2313 * Terminate the loop if iclogs are found in states
2314 * which will cause other threads to clean up iclogs.
2315 *
2316 * SYNCING - i/o completion will go through logs
2317 * DONE_SYNC - interrupt thread should be waiting for
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002318 * l_icloglock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 * IOERROR - give up hope all ye who enter here
2320 */
2321 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2322 iclog->ic_state == XLOG_STATE_SYNCING ||
2323 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2324 iclog->ic_state == XLOG_STATE_IOERROR )
2325 break;
2326 iclog = iclog->ic_next;
2327 } while (first_iclog != iclog);
2328 }
2329#endif
2330
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002331 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2332 wake = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002333 spin_unlock(&log->l_icloglock);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002334
2335 if (wake)
Dave Chinnereb40a872010-12-21 12:09:01 +11002336 wake_up_all(&log->l_flush_wait);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339
2340/*
2341 * Finish transitioning this iclog to the dirty state.
2342 *
2343 * Make sure that we completely execute this routine only when this is
2344 * the last call to the iclog. There is a good chance that iclog flushes,
2345 * when we reach the end of the physical log, get turned into 2 separate
2346 * calls to bwrite. Hence, one iclog flush could generate two calls to this
2347 * routine. By using the reference count bwritecnt, we guarantee that only
2348 * the second completion goes through.
2349 *
2350 * Callbacks could take time, so they are done outside the scope of the
David Chinner12017fa2008-08-13 16:34:31 +10002351 * global state machine log lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 */
David Chinnera8272ce2007-11-23 16:28:09 +11002353STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354xlog_state_done_syncing(
2355 xlog_in_core_t *iclog,
2356 int aborted)
2357{
2358 xlog_t *log = iclog->ic_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002360 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2363 iclog->ic_state == XLOG_STATE_IOERROR);
David Chinner155cc6b2008-03-06 13:44:14 +11002364 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
2366
2367
2368 /*
2369 * If we got an error, either on the first buffer, or in the case of
2370 * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
2371 * and none should ever be attempted to be written to disk
2372 * again.
2373 */
2374 if (iclog->ic_state != XLOG_STATE_IOERROR) {
2375 if (--iclog->ic_bwritecnt == 1) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002376 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return;
2378 }
2379 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2380 }
2381
2382 /*
2383 * Someone could be sleeping prior to writing out the next
2384 * iclog buffer, we wake them all, one will get to do the
2385 * I/O, the others get to wait for the result.
2386 */
Dave Chinnereb40a872010-12-21 12:09:01 +11002387 wake_up_all(&iclog->ic_write_wait);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002388 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
2390} /* xlog_state_done_syncing */
2391
2392
2393/*
2394 * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
David Chinner12017fa2008-08-13 16:34:31 +10002395 * sleep. We wait on the flush queue on the head iclog as that should be
2396 * the first iclog to complete flushing. Hence if all iclogs are syncing,
2397 * we will wait here and all new writes will sleep until a sync completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 *
2399 * The in-core logs are used in a circular fashion. They are not used
2400 * out-of-order even when an iclog past the head is free.
2401 *
2402 * return:
2403 * * log_offset where xlog_write() can start writing into the in-core
2404 * log's data space.
2405 * * in-core log pointer to which xlog_write() should write.
2406 * * boolean indicating this is a continued write to an in-core log.
2407 * If this is the last write, then the in-core log's offset field
2408 * needs to be incremented, depending on the amount of data which
2409 * is copied.
2410 */
David Chinnera8272ce2007-11-23 16:28:09 +11002411STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412xlog_state_get_iclog_space(xlog_t *log,
2413 int len,
2414 xlog_in_core_t **iclogp,
2415 xlog_ticket_t *ticket,
2416 int *continued_write,
2417 int *logoffsetp)
2418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 int log_offset;
2420 xlog_rec_header_t *head;
2421 xlog_in_core_t *iclog;
2422 int error;
2423
2424restart:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002425 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (XLOG_FORCED_SHUTDOWN(log)) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002427 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return XFS_ERROR(EIO);
2429 }
2430
2431 iclog = log->l_iclog;
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002432 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 XFS_STATS_INC(xs_log_noiclogs);
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002434
2435 /* Wait for log writes to have flushed */
Dave Chinnereb40a872010-12-21 12:09:01 +11002436 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 goto restart;
2438 }
Matthew Wilcoxd748c622008-05-19 16:34:27 +10002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 head = &iclog->ic_header;
2441
David Chinner155cc6b2008-03-06 13:44:14 +11002442 atomic_inc(&iclog->ic_refcnt); /* prevents sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 log_offset = iclog->ic_offset;
2444
2445 /* On the 1st write to an iclog, figure out lsn. This works
2446 * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
2447 * committing to. If the offset is set, that's how many blocks
2448 * must be written.
2449 */
2450 if (log_offset == 0) {
2451 ticket->t_curr_res -= log->l_iclog_hsize;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002452 xlog_tic_add_region(ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +10002453 log->l_iclog_hsize,
2454 XLOG_REG_TYPE_LRHEADER);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002455 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2456 head->h_lsn = cpu_to_be64(
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10002457 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 ASSERT(log->l_curr_block >= 0);
2459 }
2460
2461 /* If there is enough room to write everything, then do it. Otherwise,
2462 * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
2463 * bit is on, so this will get flushed out. Don't update ic_offset
2464 * until you know exactly how many bytes get copied. Therefore, wait
2465 * until later to update ic_offset.
2466 *
2467 * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
2468 * can fit into remaining data section.
2469 */
2470 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2471 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2472
Dave Chinner49641f12008-07-11 17:43:55 +10002473 /*
2474 * If I'm the only one writing to this iclog, sync it to disk.
2475 * We need to do an atomic compare and decrement here to avoid
2476 * racing with concurrent atomic_dec_and_lock() calls in
2477 * xlog_state_release_iclog() when there is more than one
2478 * reference to the iclog.
2479 */
2480 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2481 /* we are the only one */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002482 spin_unlock(&log->l_icloglock);
Dave Chinner49641f12008-07-11 17:43:55 +10002483 error = xlog_state_release_iclog(log, iclog);
2484 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01002485 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 } else {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002487 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489 goto restart;
2490 }
2491
2492 /* Do we have enough room to write the full amount in the remainder
2493 * of this iclog? Or must we continue a write on the next iclog and
2494 * mark this iclog as completely taken? In the case where we switch
2495 * iclogs (to mark it taken), this particular iclog will release/sync
2496 * to disk in xlog_write().
2497 */
2498 if (len <= iclog->ic_size - iclog->ic_offset) {
2499 *continued_write = 0;
2500 iclog->ic_offset += len;
2501 } else {
2502 *continued_write = 1;
2503 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2504 }
2505 *iclogp = iclog;
2506
2507 ASSERT(iclog->ic_offset <= iclog->ic_size);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002508 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 *logoffsetp = log_offset;
2511 return 0;
2512} /* xlog_state_get_iclog_space */
2513
2514/*
2515 * Atomically get the log space required for a log ticket.
2516 *
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002517 * Once a ticket gets put onto the reserveq, it will only return after the
2518 * needed reservation is satisfied.
Dave Chinner3f16b982010-12-21 12:29:01 +11002519 *
2520 * This function is structured so that it has a lock free fast path. This is
2521 * necessary because every new transaction reservation will come through this
2522 * path. Hence any lock will be globally hot if we take it unconditionally on
2523 * every pass.
2524 *
Christoph Hellwig28496962012-02-20 02:31:25 +00002525 * As tickets are only ever moved on and off the l_reserve.waiters under the
2526 * l_reserve.lock, we only need to take that lock if we are going to add
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002527 * the ticket to the queue and sleep. We can avoid taking the lock if the ticket
2528 * was never added to the reserveq because the t_queue list head will be empty
2529 * and we hold the only reference to it so it can safely be checked unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 */
2531STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002532xlog_grant_log_space(
2533 struct log *log,
2534 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002536 int free_bytes, need_bytes;
2537 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002539 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002541 trace_xfs_log_grant_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002543 /*
2544 * If there are other waiters on the queue then give them a chance at
2545 * logspace before us. Wake up the first waiters, if we do not wake
2546 * up all the waiters then go to sleep waiting for more free space,
2547 * otherwise try to get some space for this transaction.
2548 */
Dave Chinner3f16b982010-12-21 12:29:01 +11002549 need_bytes = tic->t_unit_res;
2550 if (tic->t_flags & XFS_LOG_PERM_RESERV)
2551 need_bytes *= tic->t_ocnt;
Christoph Hellwig28496962012-02-20 02:31:25 +00002552 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
2553 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
2554 spin_lock(&log->l_reserve_head.lock);
Christoph Hellwige179840d2012-02-20 02:31:29 +00002555 if (!xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes) ||
Christoph Hellwig23ee3df2012-02-20 02:31:28 +00002556 free_bytes < need_bytes) {
2557 error = xlog_grant_head_wait(log, &log->l_reserve_head,
2558 tic, need_bytes);
2559 }
Christoph Hellwig28496962012-02-20 02:31:25 +00002560 spin_unlock(&log->l_reserve_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002561 } else if (free_bytes < need_bytes) {
Christoph Hellwig28496962012-02-20 02:31:25 +00002562 spin_lock(&log->l_reserve_head.lock);
Christoph Hellwig23ee3df2012-02-20 02:31:28 +00002563 error = xlog_grant_head_wait(log, &log->l_reserve_head, tic,
2564 need_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00002565 spin_unlock(&log->l_reserve_head.lock);
Dave Chinner3f16b982010-12-21 12:29:01 +11002566 }
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002567 if (error)
2568 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Christoph Hellwig28496962012-02-20 02:31:25 +00002570 xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
2571 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002572 trace_xfs_log_grant_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002573 xlog_verify_grant_tail(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577/*
2578 * Replenish the byte reservation required by moving the grant write head.
2579 *
Dave Chinner3f16b982010-12-21 12:29:01 +11002580 * Similar to xlog_grant_log_space, the function is structured to have a lock
2581 * free fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 */
2583STATIC int
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002584xlog_regrant_write_log_space(
2585 struct log *log,
2586 struct xlog_ticket *tic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002588 int free_bytes, need_bytes;
2589 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 tic->t_curr_res = tic->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002592 xlog_tic_reset_res(tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 if (tic->t_cnt > 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01002595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002597 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002599 trace_xfs_log_regrant_write_enter(log, tic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002601 /*
2602 * If there are other waiters on the queue then give them a chance at
2603 * logspace before us. Wake up the first waiters, if we do not wake
2604 * up all the waiters then go to sleep waiting for more free space,
2605 * otherwise try to get some space for this transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 */
Dave Chinner9d7fef72009-04-06 18:42:59 +02002607 need_bytes = tic->t_unit_res;
Christoph Hellwig28496962012-02-20 02:31:25 +00002608 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
2609 if (!list_empty_careful(&log->l_write_head.waiters)) {
2610 spin_lock(&log->l_write_head.lock);
Christoph Hellwige179840d2012-02-20 02:31:29 +00002611 if (!xlog_grant_head_wake(log, &log->l_write_head, &free_bytes) ||
Christoph Hellwig23ee3df2012-02-20 02:31:28 +00002612 free_bytes < need_bytes) {
2613 error = xlog_grant_head_wait(log, &log->l_write_head,
2614 tic, need_bytes);
2615 }
Christoph Hellwig28496962012-02-20 02:31:25 +00002616 spin_unlock(&log->l_write_head.lock);
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002617 } else if (free_bytes < need_bytes) {
Christoph Hellwig28496962012-02-20 02:31:25 +00002618 spin_lock(&log->l_write_head.lock);
Christoph Hellwig23ee3df2012-02-20 02:31:28 +00002619 error = xlog_grant_head_wait(log, &log->l_write_head, tic,
2620 need_bytes);
Christoph Hellwig28496962012-02-20 02:31:25 +00002621 spin_unlock(&log->l_write_head.lock);
Dave Chinner3f16b982010-12-21 12:29:01 +11002622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002624 if (error)
2625 return error;
2626
Christoph Hellwig28496962012-02-20 02:31:25 +00002627 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002628 trace_xfs_log_regrant_write_exit(log, tic);
Dave Chinner3f336c62010-12-21 12:02:52 +11002629 xlog_verify_grant_tail(log);
Jesper Juhl014c2542006-01-15 02:37:08 +01002630 return 0;
Christoph Hellwig9f9c19e2011-11-28 08:17:36 +00002631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633/* The first cnt-1 times through here we don't need to
2634 * move the grant write head because the permanent
2635 * reservation has reserved cnt times the unit amount.
2636 * Release part of current permanent unit reservation and
2637 * reset current reservation to be one units worth. Also
2638 * move grant reservation head forward.
2639 */
2640STATIC void
2641xlog_regrant_reserve_log_space(xlog_t *log,
2642 xlog_ticket_t *ticket)
2643{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002644 trace_xfs_log_regrant_reserve_enter(log, ticket);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (ticket->t_cnt > 0)
2647 ticket->t_cnt--;
2648
Christoph Hellwig28496962012-02-20 02:31:25 +00002649 xlog_grant_sub_space(log, &log->l_reserve_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002650 ticket->t_curr_res);
Christoph Hellwig28496962012-02-20 02:31:25 +00002651 xlog_grant_sub_space(log, &log->l_write_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002652 ticket->t_curr_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002654 xlog_tic_reset_res(ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002655
2656 trace_xfs_log_regrant_reserve_sub(log, ticket);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /* just return if we still have some of the pre-reserved space */
Dave Chinnerd0eb2f32010-12-21 12:29:14 +11002659 if (ticket->t_cnt > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Christoph Hellwig28496962012-02-20 02:31:25 +00002662 xlog_grant_add_space(log, &log->l_reserve_head.grant,
Dave Chinnera69ed032010-12-21 12:08:20 +11002663 ticket->t_unit_res);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002664
2665 trace_xfs_log_regrant_reserve_exit(log, ticket);
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 ticket->t_curr_res = ticket->t_unit_res;
Christoph Hellwig0adba532007-08-30 17:21:46 +10002668 xlog_tic_reset_res(ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669} /* xlog_regrant_reserve_log_space */
2670
2671
2672/*
2673 * Give back the space left from a reservation.
2674 *
2675 * All the information we need to make a correct determination of space left
2676 * is present. For non-permanent reservations, things are quite easy. The
2677 * count should have been decremented to zero. We only need to deal with the
2678 * space remaining in the current reservation part of the ticket. If the
2679 * ticket contains a permanent reservation, there may be left over space which
2680 * needs to be released. A count of N means that N-1 refills of the current
2681 * reservation can be done before we need to ask for more space. The first
2682 * one goes to fill up the first current reservation. Once we run out of
2683 * space, the count will stay at zero and the only space remaining will be
2684 * in the current reservation field.
2685 */
2686STATIC void
2687xlog_ungrant_log_space(xlog_t *log,
2688 xlog_ticket_t *ticket)
2689{
Dave Chinner663e4962010-12-21 12:06:05 +11002690 int bytes;
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (ticket->t_cnt > 0)
2693 ticket->t_cnt--;
2694
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002695 trace_xfs_log_ungrant_enter(log, ticket);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002696 trace_xfs_log_ungrant_sub(log, ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Dave Chinner663e4962010-12-21 12:06:05 +11002698 /*
2699 * If this is a permanent reservation ticket, we may be able to free
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 * up more space based on the remaining count.
2701 */
Dave Chinner663e4962010-12-21 12:06:05 +11002702 bytes = ticket->t_curr_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (ticket->t_cnt > 0) {
2704 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
Dave Chinner663e4962010-12-21 12:06:05 +11002705 bytes += ticket->t_unit_res*ticket->t_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707
Christoph Hellwig28496962012-02-20 02:31:25 +00002708 xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
2709 xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
Dave Chinner663e4962010-12-21 12:06:05 +11002710
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002711 trace_xfs_log_ungrant_exit(log, ticket);
2712
Christoph Hellwigcfb7cdc2012-02-20 02:31:23 +00002713 xfs_log_space_wake(log->l_mp);
Christoph Hellwig09a423a2012-02-20 02:31:20 +00002714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 * Flush iclog to disk if this is the last reference to the given iclog and
2718 * the WANT_SYNC bit is set.
2719 *
2720 * When this function is entered, the iclog is not necessarily in the
2721 * WANT_SYNC state. It may be sitting around waiting to get filled.
2722 *
2723 *
2724 */
David Chinnera8272ce2007-11-23 16:28:09 +11002725STATIC int
David Chinnerb5893342008-03-06 13:44:06 +11002726xlog_state_release_iclog(
2727 xlog_t *log,
2728 xlog_in_core_t *iclog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 int sync = 0; /* do we sync? */
2731
David Chinner155cc6b2008-03-06 13:44:14 +11002732 if (iclog->ic_state & XLOG_STATE_IOERROR)
2733 return XFS_ERROR(EIO);
2734
2735 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
2736 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
2737 return 0;
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002740 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return XFS_ERROR(EIO);
2742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
2744 iclog->ic_state == XLOG_STATE_WANT_SYNC);
2745
David Chinner155cc6b2008-03-06 13:44:14 +11002746 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
David Chinnerb5893342008-03-06 13:44:06 +11002747 /* update tail before writing to iclog */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002748 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 sync++;
2750 iclog->ic_state = XLOG_STATE_SYNCING;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11002751 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
2752 xlog_verify_tail_lsn(log, iclog, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 /* cycle incremented when incrementing curr_block */
2754 }
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002755 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /*
2758 * We let the log lock go, so it's possible that we hit a log I/O
Nathan Scottc41564b2006-03-29 08:55:14 +10002759 * error or some other SHUTDOWN condition that marks the iclog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 * as XLOG_STATE_IOERROR before the bwrite. However, we know that
2761 * this iclog has consistent data, so we ignore IOERROR
2762 * flags after this point.
2763 */
David Chinnerb5893342008-03-06 13:44:06 +11002764 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return xlog_sync(log, iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01002766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767} /* xlog_state_release_iclog */
2768
2769
2770/*
2771 * This routine will mark the current iclog in the ring as WANT_SYNC
2772 * and move the current iclog pointer to the next iclog in the ring.
2773 * When this routine is called from xlog_state_get_iclog_space(), the
2774 * exact size of the iclog has not yet been determined. All we know is
2775 * that every data block. We have run out of space in this log record.
2776 */
2777STATIC void
2778xlog_state_switch_iclogs(xlog_t *log,
2779 xlog_in_core_t *iclog,
2780 int eventual_size)
2781{
2782 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
2783 if (!eventual_size)
2784 eventual_size = iclog->ic_offset;
2785 iclog->ic_state = XLOG_STATE_WANT_SYNC;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002786 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 log->l_prev_block = log->l_curr_block;
2788 log->l_prev_cycle = log->l_curr_cycle;
2789
2790 /* roll log?: ic_offset changed later */
2791 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
2792
2793 /* Round up to next log-sunit */
Eric Sandeen62118702008-03-06 13:44:28 +11002794 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 log->l_mp->m_sb.sb_logsunit > 1) {
2796 __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
2797 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
2798 }
2799
2800 if (log->l_curr_block >= log->l_logBBsize) {
2801 log->l_curr_cycle++;
2802 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
2803 log->l_curr_cycle++;
2804 log->l_curr_block -= log->l_logBBsize;
2805 ASSERT(log->l_curr_block >= 0);
2806 }
2807 ASSERT(iclog == log->l_iclog);
2808 log->l_iclog = iclog->ic_next;
2809} /* xlog_state_switch_iclogs */
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811/*
2812 * Write out all data in the in-core log as of this exact moment in time.
2813 *
2814 * Data may be written to the in-core log during this call. However,
2815 * we don't guarantee this data will be written out. A change from past
2816 * implementation means this routine will *not* write out zero length LRs.
2817 *
2818 * Basically, we try and perform an intelligent scan of the in-core logs.
2819 * If we determine there is no flushable data, we just return. There is no
2820 * flushable data if:
2821 *
2822 * 1. the current iclog is active and has no data; the previous iclog
2823 * is in the active or dirty state.
2824 * 2. the current iclog is drity, and the previous iclog is in the
2825 * active or dirty state.
2826 *
David Chinner12017fa2008-08-13 16:34:31 +10002827 * We may sleep if:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 *
2829 * 1. the current iclog is not in the active nor dirty state.
2830 * 2. the current iclog dirty, and the previous iclog is not in the
2831 * active nor dirty state.
2832 * 3. the current iclog is active, and there is another thread writing
2833 * to this particular iclog.
2834 * 4. a) the current iclog is active and has no other writers
2835 * b) when we return from flushing out this iclog, it is still
2836 * not in the active nor dirty state.
2837 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002838int
2839_xfs_log_force(
2840 struct xfs_mount *mp,
2841 uint flags,
2842 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002844 struct log *log = mp->m_log;
2845 struct xlog_in_core *iclog;
2846 xfs_lsn_t lsn;
2847
2848 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002850 xlog_cil_force(log);
Dave Chinner71e330b2010-05-21 14:37:18 +10002851
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002852 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 iclog = log->l_iclog;
2855 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002856 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return XFS_ERROR(EIO);
2858 }
2859
2860 /* If the head iclog is not active nor dirty, we just attach
2861 * ourselves to the head and go to sleep.
2862 */
2863 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2864 iclog->ic_state == XLOG_STATE_DIRTY) {
2865 /*
2866 * If the head is dirty or (active and empty), then
2867 * we need to look at the previous iclog. If the previous
2868 * iclog is active or dirty we are done. There is nothing
2869 * to sync out. Otherwise, we attach ourselves to the
2870 * previous iclog and go to sleep.
2871 */
2872 if (iclog->ic_state == XLOG_STATE_DIRTY ||
David Chinner155cc6b2008-03-06 13:44:14 +11002873 (atomic_read(&iclog->ic_refcnt) == 0
2874 && iclog->ic_offset == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 iclog = iclog->ic_prev;
2876 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
2877 iclog->ic_state == XLOG_STATE_DIRTY)
2878 goto no_sleep;
2879 else
2880 goto maybe_sleep;
2881 } else {
David Chinner155cc6b2008-03-06 13:44:14 +11002882 if (atomic_read(&iclog->ic_refcnt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 /* We are the only one with access to this
2884 * iclog. Flush it out now. There should
2885 * be a roundoff of zero to show that someone
2886 * has already taken care of the roundoff from
2887 * the previous sync.
2888 */
David Chinner155cc6b2008-03-06 13:44:14 +11002889 atomic_inc(&iclog->ic_refcnt);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002890 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002892 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 if (xlog_state_release_iclog(log, iclog))
2895 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002896
2897 if (log_flushed)
2898 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002899 spin_lock(&log->l_icloglock);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002900 if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 iclog->ic_state != XLOG_STATE_DIRTY)
2902 goto maybe_sleep;
2903 else
2904 goto no_sleep;
2905 } else {
2906 /* Someone else is writing to this iclog.
2907 * Use its call to flush out the data. However,
2908 * the other thread may not force out this LR,
2909 * so we mark it WANT_SYNC.
2910 */
2911 xlog_state_switch_iclogs(log, iclog, 0);
2912 goto maybe_sleep;
2913 }
2914 }
2915 }
2916
2917 /* By the time we come around again, the iclog could've been filled
2918 * which would give it another lsn. If we have a new lsn, just
2919 * return because the relevant data has been flushed.
2920 */
2921maybe_sleep:
2922 if (flags & XFS_LOG_SYNC) {
2923 /*
2924 * We must check if we're shutting down here, before
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002925 * we wait, while we're holding the l_icloglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 * Then we check again after waking up, in case our
2927 * sleep was disturbed by a bad news.
2928 */
2929 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002930 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return XFS_ERROR(EIO);
2932 }
2933 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11002934 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 /*
2936 * No need to grab the log lock here since we're
2937 * only deciding whether or not to return EIO
2938 * and the memory read should be atomic.
2939 */
2940 if (iclog->ic_state & XLOG_STATE_IOERROR)
2941 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002942 if (log_flushed)
2943 *log_flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 } else {
2945
2946no_sleep:
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10002947 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 }
2949 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952/*
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002953 * Wrapper for _xfs_log_force(), to be used when caller doesn't care
2954 * about errors or whether the log was flushed or not. This is the normal
2955 * interface to use when trying to unpin items or move the log forward.
2956 */
2957void
2958xfs_log_force(
2959 xfs_mount_t *mp,
2960 uint flags)
2961{
2962 int error;
2963
2964 error = _xfs_log_force(mp, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002965 if (error)
2966 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002967}
2968
2969/*
2970 * Force the in-core log to disk for a specific LSN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 *
2972 * Find in-core log with lsn.
2973 * If it is in the DIRTY state, just return.
2974 * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
2975 * state and go to sleep or return.
2976 * If it is in any other state, go to sleep or return.
2977 *
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002978 * Synchronous forces are implemented with a signal variable. All callers
2979 * to force a given lsn to disk will wait on a the sv attached to the
2980 * specific in-core log. When given in-core log finally completes its
2981 * write to disk, that thread will wake up all threads waiting on the
2982 * sv.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002984int
2985_xfs_log_force_lsn(
2986 struct xfs_mount *mp,
2987 xfs_lsn_t lsn,
2988 uint flags,
2989 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002991 struct log *log = mp->m_log;
2992 struct xlog_in_core *iclog;
2993 int already_slept = 0;
2994
2995 ASSERT(lsn != 0);
2996
2997 XFS_STATS_INC(xs_log_force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Christoph Hellwig93b8a582011-12-06 21:58:07 +00002999 lsn = xlog_cil_force_lsn(log, lsn);
3000 if (lsn == NULLCOMMITLSN)
3001 return 0;
Dave Chinner71e330b2010-05-21 14:37:18 +10003002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003try_again:
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003004 spin_lock(&log->l_icloglock);
3005 iclog = log->l_iclog;
3006 if (iclog->ic_state & XLOG_STATE_IOERROR) {
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003007 spin_unlock(&log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003008 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
3010
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003011 do {
3012 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3013 iclog = iclog->ic_next;
3014 continue;
3015 }
3016
3017 if (iclog->ic_state == XLOG_STATE_DIRTY) {
3018 spin_unlock(&log->l_icloglock);
3019 return 0;
3020 }
3021
3022 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3023 /*
3024 * We sleep here if we haven't already slept (e.g.
3025 * this is the first time we've looked at the correct
3026 * iclog buf) and the buffer before us is going to
3027 * be sync'ed. The reason for this is that if we
3028 * are doing sync transactions here, by waiting for
3029 * the previous I/O to complete, we can allow a few
3030 * more transactions into this iclog before we close
3031 * it down.
3032 *
3033 * Otherwise, we mark the buffer WANT_SYNC, and bump
3034 * up the refcnt so we can release the log (which
3035 * drops the ref count). The state switch keeps new
3036 * transaction commits from using this buffer. When
3037 * the current commits finish writing into the buffer,
3038 * the refcount will drop to zero and the buffer will
3039 * go out then.
3040 */
3041 if (!already_slept &&
3042 (iclog->ic_prev->ic_state &
3043 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3044 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3045
3046 XFS_STATS_INC(xs_log_force_sleep);
3047
Dave Chinnereb40a872010-12-21 12:09:01 +11003048 xlog_wait(&iclog->ic_prev->ic_write_wait,
3049 &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003050 if (log_flushed)
3051 *log_flushed = 1;
3052 already_slept = 1;
3053 goto try_again;
3054 }
David Chinner155cc6b2008-03-06 13:44:14 +11003055 atomic_inc(&iclog->ic_refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 xlog_state_switch_iclogs(log, iclog, 0);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003057 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (xlog_state_release_iclog(log, iclog))
3059 return XFS_ERROR(EIO);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003060 if (log_flushed)
3061 *log_flushed = 1;
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003062 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003065 if ((flags & XFS_LOG_SYNC) && /* sleep */
3066 !(iclog->ic_state &
3067 (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
3068 /*
3069 * Don't wait on completion if we know that we've
3070 * gotten a log write error.
3071 */
3072 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3073 spin_unlock(&log->l_icloglock);
3074 return XFS_ERROR(EIO);
3075 }
3076 XFS_STATS_INC(xs_log_force_sleep);
Dave Chinnereb40a872010-12-21 12:09:01 +11003077 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003078 /*
3079 * No need to grab the log lock here since we're
3080 * only deciding whether or not to return EIO
3081 * and the memory read should be atomic.
3082 */
3083 if (iclog->ic_state & XLOG_STATE_IOERROR)
3084 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003086 if (log_flushed)
3087 *log_flushed = 1;
3088 } else { /* just return */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003089 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003091
3092 return 0;
3093 } while (iclog != log->l_iclog);
3094
3095 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return 0;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003099/*
3100 * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
3101 * about errors or whether the log was flushed or not. This is the normal
3102 * interface to use when trying to unpin items or move the log forward.
3103 */
3104void
3105xfs_log_force_lsn(
3106 xfs_mount_t *mp,
3107 xfs_lsn_t lsn,
3108 uint flags)
3109{
3110 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003112 error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003113 if (error)
3114 xfs_warn(mp, "%s: error %d returned.", __func__, error);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117/*
3118 * Called when we want to mark the current iclog as being ready to sync to
3119 * disk.
3120 */
David Chinnera8272ce2007-11-23 16:28:09 +11003121STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
3123{
Christoph Hellwiga8914f32009-08-10 11:32:44 -03003124 assert_spin_locked(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3127 xlog_state_switch_iclogs(log, iclog, 0);
3128 } else {
3129 ASSERT(iclog->ic_state &
3130 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3131 }
Christoph Hellwig39e2def2008-12-03 12:20:28 +01003132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134
3135/*****************************************************************************
3136 *
3137 * TICKET functions
3138 *
3139 *****************************************************************************
3140 */
3141
3142/*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003143 * Free a used ticket when its refcount falls to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003145void
3146xfs_log_ticket_put(
3147 xlog_ticket_t *ticket)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003149 ASSERT(atomic_read(&ticket->t_ref) > 0);
Dave Chinnereb40a872010-12-21 12:09:01 +11003150 if (atomic_dec_and_test(&ticket->t_ref))
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003151 kmem_zone_free(xfs_log_ticket_zone, ticket);
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003154xlog_ticket_t *
3155xfs_log_ticket_get(
3156 xlog_ticket_t *ticket)
3157{
3158 ASSERT(atomic_read(&ticket->t_ref) > 0);
3159 atomic_inc(&ticket->t_ref);
3160 return ticket;
3161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163/*
David Chinnereb01c9c2008-04-10 12:18:46 +10003164 * Allocate and initialise a new log ticket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 */
Dave Chinner71e330b2010-05-21 14:37:18 +10003166xlog_ticket_t *
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003167xlog_ticket_alloc(
3168 struct log *log,
3169 int unit_bytes,
3170 int cnt,
3171 char client,
Dave Chinner3383ca52010-05-07 11:04:17 +10003172 uint xflags,
3173 int alloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003175 struct xlog_ticket *tic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 uint num_headers;
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003177 int iclog_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Dave Chinner3383ca52010-05-07 11:04:17 +10003179 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
David Chinnereb01c9c2008-04-10 12:18:46 +10003180 if (!tic)
3181 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 /*
3184 * Permanent reservations have up to 'cnt'-1 active log operations
3185 * in the log. A unit in this case is the amount of space for one
3186 * of these log operations. Normal reservations have a cnt of 1
3187 * and their unit amount is the total amount of space required.
3188 *
3189 * The following lines of code account for non-transaction data
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003190 * which occupy space in the on-disk log.
3191 *
3192 * Normal form of a transaction is:
3193 * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
3194 * and then there are LR hdrs, split-recs and roundoff at end of syncs.
3195 *
3196 * We need to account for all the leadup data and trailer data
3197 * around the transaction data.
3198 * And then we need to account for the worst case in terms of using
3199 * more space.
3200 * The worst case will happen if:
3201 * - the placement of the transaction happens to be such that the
3202 * roundoff is at its maximum
3203 * - the transaction data is synced before the commit record is synced
3204 * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
3205 * Therefore the commit record is in its own Log Record.
3206 * This can happen as the commit record is called with its
3207 * own region to xlog_write().
3208 * This then means that in the worst case, roundoff can happen for
3209 * the commit-rec as well.
3210 * The commit-rec is smaller than padding in this scenario and so it is
3211 * not added separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 */
3213
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003214 /* for trans header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 unit_bytes += sizeof(xlog_op_header_t);
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003216 unit_bytes += sizeof(xfs_trans_header_t);
3217
3218 /* for start-rec */
3219 unit_bytes += sizeof(xlog_op_header_t);
3220
Dave Chinner9b9fc2b72010-03-23 11:21:11 +11003221 /*
3222 * for LR headers - the space for data in an iclog is the size minus
3223 * the space used for the headers. If we use the iclog size, then we
3224 * undercalculate the number of headers required.
3225 *
3226 * Furthermore - the addition of op headers for split-recs might
3227 * increase the space required enough to require more log and op
3228 * headers, so take that into account too.
3229 *
3230 * IMPORTANT: This reservation makes the assumption that if this
3231 * transaction is the first in an iclog and hence has the LR headers
3232 * accounted to it, then the remaining space in the iclog is
3233 * exclusively for this transaction. i.e. if the transaction is larger
3234 * than the iclog, it will be the only thing in that iclog.
3235 * Fundamentally, this means we must pass the entire log vector to
3236 * xlog_write to guarantee this.
3237 */
3238 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3239 num_headers = howmany(unit_bytes, iclog_space);
3240
3241 /* for split-recs - ophdrs added when data split over LRs */
3242 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3243
3244 /* add extra header reservations if we overrun */
3245 while (!num_headers ||
3246 howmany(unit_bytes, iclog_space) > num_headers) {
3247 unit_bytes += sizeof(xlog_op_header_t);
3248 num_headers++;
3249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 unit_bytes += log->l_iclog_hsize * num_headers;
3251
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003252 /* for commit-rec LR header - note: padding will subsume the ophdr */
3253 unit_bytes += log->l_iclog_hsize;
3254
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003255 /* for roundoff padding for transaction data and one for commit record */
Eric Sandeen62118702008-03-06 13:44:28 +11003256 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
Tim Shimmin32fb9b52005-09-02 16:41:43 +10003257 log->l_mp->m_sb.sb_logsunit > 1) {
3258 /* log su roundoff */
3259 unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
3260 } else {
3261 /* BB roundoff */
3262 unit_bytes += 2*BBSIZE;
3263 }
3264
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003265 atomic_set(&tic->t_ref, 1);
Christoph Hellwig14a7235f2012-02-20 02:31:24 +00003266 tic->t_task = current;
Dave Chinner10547942010-12-21 12:02:25 +11003267 INIT_LIST_HEAD(&tic->t_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 tic->t_unit_res = unit_bytes;
3269 tic->t_curr_res = unit_bytes;
3270 tic->t_cnt = cnt;
3271 tic->t_ocnt = cnt;
Dave Chinnerf9837102010-04-14 15:47:55 +10003272 tic->t_tid = random32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 tic->t_clientid = client;
3274 tic->t_flags = XLOG_TIC_INITED;
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003275 tic->t_trans_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (xflags & XFS_LOG_PERM_RESERV)
3277 tic->t_flags |= XLOG_TIC_PERM_RESERV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Christoph Hellwig0adba532007-08-30 17:21:46 +10003279 xlog_tic_reset_res(tic);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10003280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return tic;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003282}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284
3285/******************************************************************************
3286 *
3287 * Log debug routines
3288 *
3289 ******************************************************************************
3290 */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003291#if defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292/*
3293 * Make sure that the destination ptr is within the valid data region of
3294 * one of the iclogs. This uses backup pointers stored in a different
3295 * part of the log in case we trash the log structure.
3296 */
3297void
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003298xlog_verify_dest_ptr(
3299 struct log *log,
3300 char *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
3302 int i;
3303 int good_ptr = 0;
3304
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003305 for (i = 0; i < log->l_iclog_bufs; i++) {
3306 if (ptr >= log->l_iclog_bak[i] &&
3307 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 good_ptr++;
3309 }
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003310
3311 if (!good_ptr)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003312 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Christoph Hellwige6b1f272010-03-23 11:47:38 +11003313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003315/*
3316 * Check to make sure the grant write head didn't just over lap the tail. If
3317 * the cycles are the same, we can't be overlapping. Otherwise, make sure that
3318 * the cycles differ by exactly one and check the byte count.
3319 *
3320 * This check is run unlocked, so can give false positives. Rather than assert
3321 * on failures, use a warn-once flag and a panic tag to allow the admin to
3322 * determine if they want to panic the machine when such an error occurs. For
3323 * debug kernels this will have the same effect as using an assert but, unlinke
3324 * an assert, it can be turned off at runtime.
3325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326STATIC void
Dave Chinner3f336c62010-12-21 12:02:52 +11003327xlog_verify_grant_tail(
3328 struct log *log)
3329{
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003330 int tail_cycle, tail_blocks;
Dave Chinnera69ed032010-12-21 12:08:20 +11003331 int cycle, space;
Dave Chinner3f336c62010-12-21 12:02:52 +11003332
Christoph Hellwig28496962012-02-20 02:31:25 +00003333 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11003334 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3335 if (tail_cycle != cycle) {
Dave Chinnerda8a1a42011-04-08 12:45:07 +10003336 if (cycle - 1 != tail_cycle &&
3337 !(log->l_flags & XLOG_TAIL_WARN)) {
3338 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3339 "%s: cycle - 1 != tail_cycle", __func__);
3340 log->l_flags |= XLOG_TAIL_WARN;
3341 }
3342
3343 if (space > BBTOB(tail_blocks) &&
3344 !(log->l_flags & XLOG_TAIL_WARN)) {
3345 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3346 "%s: space > BBTOB(tail_blocks)", __func__);
3347 log->l_flags |= XLOG_TAIL_WARN;
3348 }
Dave Chinner3f336c62010-12-21 12:02:52 +11003349 }
3350}
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352/* check if it will fit */
3353STATIC void
3354xlog_verify_tail_lsn(xlog_t *log,
3355 xlog_in_core_t *iclog,
3356 xfs_lsn_t tail_lsn)
3357{
3358 int blocks;
3359
3360 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3361 blocks =
3362 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3363 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003364 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 } else {
3366 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3367
3368 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003369 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
3371 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3372 if (blocks < BTOBB(iclog->ic_offset) + 1)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003373 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375} /* xlog_verify_tail_lsn */
3376
3377/*
3378 * Perform a number of checks on the iclog before writing to disk.
3379 *
3380 * 1. Make sure the iclogs are still circular
3381 * 2. Make sure we have a good magic number
3382 * 3. Make sure we don't have magic numbers in the data
3383 * 4. Check fields of each log operation header for:
3384 * A. Valid client identifier
3385 * B. tid ptr value falls in valid ptr space (user space code)
3386 * C. Length in log record header is correct according to the
3387 * individual operation headers within record.
3388 * 5. When a bwrite will occur within 5 blocks of the front of the physical
3389 * log, check the preceding blocks of the physical log to make sure all
3390 * the cycle numbers agree with the current cycle number.
3391 */
3392STATIC void
3393xlog_verify_iclog(xlog_t *log,
3394 xlog_in_core_t *iclog,
3395 int count,
3396 boolean_t syncing)
3397{
3398 xlog_op_header_t *ophead;
3399 xlog_in_core_t *icptr;
3400 xlog_in_core_2_t *xhdr;
3401 xfs_caddr_t ptr;
3402 xfs_caddr_t base_ptr;
3403 __psint_t field_offset;
3404 __uint8_t clientid;
3405 int len, i, j, k, op_len;
3406 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 /* check validity of iclog pointers */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003409 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 icptr = log->l_iclog;
3411 for (i=0; i < log->l_iclog_bufs; i++) {
Christoph Hellwig4b809162007-08-16 15:37:36 +10003412 if (icptr == NULL)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003413 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 icptr = icptr->ic_next;
3415 }
3416 if (icptr != log->l_iclog)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003417 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003418 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 /* check log magic numbers */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003421 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003422 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003424 ptr = (xfs_caddr_t) &iclog->ic_header;
3425 for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 ptr += BBSIZE) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003427 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003428 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3429 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 }
3431
3432 /* check fields */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003433 len = be32_to_cpu(iclog->ic_header.h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 ptr = iclog->ic_datap;
3435 base_ptr = ptr;
3436 ophead = (xlog_op_header_t *)ptr;
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003437 xhdr = iclog->ic_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 for (i = 0; i < len; i++) {
3439 ophead = (xlog_op_header_t *)ptr;
3440
3441 /* clientid is only 1 byte */
3442 field_offset = (__psint_t)
3443 ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr);
3444 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
3445 clientid = ophead->oh_clientid;
3446 } else {
3447 idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap);
3448 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3449 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3450 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003451 clientid = xlog_get_client_id(
3452 xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 } else {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10003454 clientid = xlog_get_client_id(
3455 iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457 }
3458 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003459 xfs_warn(log->l_mp,
3460 "%s: invalid clientid %d op 0x%p offset 0x%lx",
3461 __func__, clientid, ophead,
3462 (unsigned long)field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 /* check length */
3465 field_offset = (__psint_t)
3466 ((xfs_caddr_t)&(ophead->oh_len) - base_ptr);
3467 if (syncing == B_FALSE || (field_offset & 0x1ff)) {
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003468 op_len = be32_to_cpu(ophead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 } else {
3470 idx = BTOBBT((__psint_t)&ophead->oh_len -
3471 (__psint_t)iclog->ic_datap);
3472 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3473 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3474 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003475 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 } else {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003477 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
3479 }
3480 ptr += sizeof(xlog_op_header_t) + op_len;
3481 }
3482} /* xlog_verify_iclog */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11003483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485/*
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003486 * Mark all iclogs IOERROR. l_icloglock is held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 */
3488STATIC int
3489xlog_state_ioerror(
3490 xlog_t *log)
3491{
3492 xlog_in_core_t *iclog, *ic;
3493
3494 iclog = log->l_iclog;
3495 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3496 /*
3497 * Mark all the incore logs IOERROR.
3498 * From now on, no log flushes will result.
3499 */
3500 ic = iclog;
3501 do {
3502 ic->ic_state = XLOG_STATE_IOERROR;
3503 ic = ic->ic_next;
3504 } while (ic != iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507 /*
3508 * Return non-zero, if state transition has already happened.
3509 */
Jesper Juhl014c2542006-01-15 02:37:08 +01003510 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
3513/*
3514 * This is called from xfs_force_shutdown, when we're forcibly
3515 * shutting down the filesystem, typically because of an IO error.
3516 * Our main objectives here are to make sure that:
3517 * a. the filesystem gets marked 'SHUTDOWN' for all interested
3518 * parties to find out, 'atomically'.
3519 * b. those who're sleeping on log reservations, pinned objects and
3520 * other resources get woken up, and be told the bad news.
3521 * c. nothing new gets queued up after (a) and (b) are done.
3522 * d. if !logerror, flush the iclogs to disk, then seal them off
3523 * for business.
Dave Chinner9da1ab12010-05-17 15:51:59 +10003524 *
3525 * Note: for delayed logging the !logerror case needs to flush the regions
3526 * held in memory out to the iclogs before flushing them to disk. This needs
3527 * to be done before the log is marked as shutdown, otherwise the flush to the
3528 * iclogs will fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 */
3530int
3531xfs_log_force_umount(
3532 struct xfs_mount *mp,
3533 int logerror)
3534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 xlog_t *log;
3536 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 log = mp->m_log;
3539
3540 /*
3541 * If this happens during log recovery, don't worry about
3542 * locking; the log isn't open for business yet.
3543 */
3544 if (!log ||
3545 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3546 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003547 if (mp->m_sb_bp)
3548 XFS_BUF_DONE(mp->m_sb_bp);
Jesper Juhl014c2542006-01-15 02:37:08 +01003549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 }
3551
3552 /*
3553 * Somebody could've already done the hard work for us.
3554 * No need to get locks for this.
3555 */
3556 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3557 ASSERT(XLOG_FORCED_SHUTDOWN(log));
Jesper Juhl014c2542006-01-15 02:37:08 +01003558 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 retval = 0;
Dave Chinner9da1ab12010-05-17 15:51:59 +10003561
3562 /*
3563 * Flush the in memory commit item list before marking the log as
3564 * being shut down. We need to do it in this order to ensure all the
3565 * completed transactions are flushed to disk with the xfs_log_force()
3566 * call below.
3567 */
Christoph Hellwig93b8a582011-12-06 21:58:07 +00003568 if (!logerror)
Dave Chinnera44f13e2010-08-24 11:40:03 +10003569 xlog_cil_force(log);
Dave Chinner9da1ab12010-05-17 15:51:59 +10003570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 /*
Dave Chinner3f16b982010-12-21 12:29:01 +11003572 * mark the filesystem and the as in a shutdown state and wake
3573 * everybody up to tell them the bad news.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 */
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003575 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
Christoph Hellwigbac8dca2008-11-28 14:23:31 +11003577 if (mp->m_sb_bp)
3578 XFS_BUF_DONE(mp->m_sb_bp);
3579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 /*
3581 * This flag is sort of redundant because of the mount flag, but
3582 * it's good to maintain the separation between the log and the rest
3583 * of XFS.
3584 */
3585 log->l_flags |= XLOG_IO_ERROR;
3586
3587 /*
3588 * If we hit a log error, we want to mark all the iclogs IOERROR
3589 * while we're still holding the loglock.
3590 */
3591 if (logerror)
3592 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003593 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 /*
Dave Chinner10547942010-12-21 12:02:25 +11003596 * We don't want anybody waiting for log reservations after this. That
3597 * means we have to wake up everybody queued up on reserveq as well as
3598 * writeq. In addition, we make sure in xlog_{re}grant_log_space that
3599 * we don't enqueue anything once the SHUTDOWN flag is set, and this
Dave Chinner3f16b982010-12-21 12:29:01 +11003600 * action is protected by the grant locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 */
Christoph Hellwiga79bf2d2012-02-20 02:31:27 +00003602 xlog_grant_head_wake_all(&log->l_reserve_head);
3603 xlog_grant_head_wake_all(&log->l_write_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003605 if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 ASSERT(!logerror);
3607 /*
3608 * Force the incore logs to disk before shutting the
3609 * log down completely.
3610 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003611 _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
3612
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003613 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 retval = xlog_state_ioerror(log);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003615 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617 /*
3618 * Wake up everybody waiting on xfs_log_force.
3619 * Callback all log item committed functions as if the
3620 * log writes were completed.
3621 */
3622 xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
3623
3624#ifdef XFSERRORDEBUG
3625 {
3626 xlog_in_core_t *iclog;
3627
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003628 spin_lock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 iclog = log->l_iclog;
3630 do {
3631 ASSERT(iclog->ic_callback == 0);
3632 iclog = iclog->ic_next;
3633 } while (iclog != log->l_iclog);
Eric Sandeenb22cd72c2007-10-11 17:37:10 +10003634 spin_unlock(&log->l_icloglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636#endif
3637 /* return non-zero if log IOERROR transition had already happened */
Jesper Juhl014c2542006-01-15 02:37:08 +01003638 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003641STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642xlog_iclogs_empty(xlog_t *log)
3643{
3644 xlog_in_core_t *iclog;
3645
3646 iclog = log->l_iclog;
3647 do {
3648 /* endianness does not matter here, zero is zero in
3649 * any language.
3650 */
3651 if (iclog->ic_header.h_num_logops)
Jesper Juhl014c2542006-01-15 02:37:08 +01003652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 iclog = iclog->ic_next;
3654 } while (iclog != log->l_iclog);
Jesper Juhl014c2542006-01-15 02:37:08 +01003655 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}