blob: d4fadbe8ac90d732d6eca14b735eeb22a3e90f12 [file] [log] [blame]
Dave Chinner71e330b2010-05-21 14:37:18 +10001/*
2 * Copyright (c) 2010 Red Hat, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write the Free Software Foundation,
15 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_trans_priv.h"
26#include "xfs_log_priv.h"
27#include "xfs_sb.h"
28#include "xfs_ag.h"
Dave Chinner71e330b2010-05-21 14:37:18 +100029#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_alloc.h"
Christoph Hellwige84661a2011-05-20 13:45:32 +000032#include "xfs_discard.h"
Dave Chinner71e330b2010-05-21 14:37:18 +100033
34/*
Christoph Hellwig93b8a582011-12-06 21:58:07 +000035 * Perform initial CIL structure initialisation.
Dave Chinner71e330b2010-05-21 14:37:18 +100036 */
37int
38xlog_cil_init(
39 struct log *log)
40{
41 struct xfs_cil *cil;
42 struct xfs_cil_ctx *ctx;
43
Dave Chinner71e330b2010-05-21 14:37:18 +100044 cil = kmem_zalloc(sizeof(*cil), KM_SLEEP|KM_MAYFAIL);
45 if (!cil)
46 return ENOMEM;
47
48 ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP|KM_MAYFAIL);
49 if (!ctx) {
50 kmem_free(cil);
51 return ENOMEM;
52 }
53
54 INIT_LIST_HEAD(&cil->xc_cil);
55 INIT_LIST_HEAD(&cil->xc_committing);
56 spin_lock_init(&cil->xc_cil_lock);
57 init_rwsem(&cil->xc_ctx_lock);
Dave Chinnereb40a872010-12-21 12:09:01 +110058 init_waitqueue_head(&cil->xc_commit_wait);
Dave Chinner71e330b2010-05-21 14:37:18 +100059
60 INIT_LIST_HEAD(&ctx->committing);
61 INIT_LIST_HEAD(&ctx->busy_extents);
62 ctx->sequence = 1;
63 ctx->cil = cil;
64 cil->xc_ctx = ctx;
Dave Chinnera44f13e2010-08-24 11:40:03 +100065 cil->xc_current_sequence = ctx->sequence;
Dave Chinner71e330b2010-05-21 14:37:18 +100066
67 cil->xc_log = log;
68 log->l_cilp = cil;
69 return 0;
70}
71
72void
73xlog_cil_destroy(
74 struct log *log)
75{
Dave Chinner71e330b2010-05-21 14:37:18 +100076 if (log->l_cilp->xc_ctx) {
77 if (log->l_cilp->xc_ctx->ticket)
78 xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket);
79 kmem_free(log->l_cilp->xc_ctx);
80 }
81
82 ASSERT(list_empty(&log->l_cilp->xc_cil));
83 kmem_free(log->l_cilp);
84}
85
86/*
87 * Allocate a new ticket. Failing to get a new ticket makes it really hard to
88 * recover, so we don't allow failure here. Also, we allocate in a context that
89 * we don't want to be issuing transactions from, so we need to tell the
90 * allocation code this as well.
91 *
92 * We don't reserve any space for the ticket - we are going to steal whatever
93 * space we require from transactions as they commit. To ensure we reserve all
94 * the space required, we need to set the current reservation of the ticket to
95 * zero so that we know to steal the initial transaction overhead from the
96 * first transaction commit.
97 */
98static struct xlog_ticket *
99xlog_cil_ticket_alloc(
100 struct log *log)
101{
102 struct xlog_ticket *tic;
103
104 tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0,
105 KM_SLEEP|KM_NOFS);
106 tic->t_trans_type = XFS_TRANS_CHECKPOINT;
107
108 /*
109 * set the current reservation to zero so we know to steal the basic
110 * transaction overhead reservation from the first transaction commit.
111 */
112 tic->t_curr_res = 0;
113 return tic;
114}
115
116/*
117 * After the first stage of log recovery is done, we know where the head and
118 * tail of the log are. We need this log initialisation done before we can
119 * initialise the first CIL checkpoint context.
120 *
121 * Here we allocate a log ticket to track space usage during a CIL push. This
122 * ticket is passed to xlog_write() directly so that we don't slowly leak log
123 * space by failing to account for space used by log headers and additional
124 * region headers for split regions.
125 */
126void
127xlog_cil_init_post_recovery(
128 struct log *log)
129{
Dave Chinner71e330b2010-05-21 14:37:18 +1000130 log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
131 log->l_cilp->xc_ctx->sequence = 1;
132 log->l_cilp->xc_ctx->commit_lsn = xlog_assign_lsn(log->l_curr_cycle,
133 log->l_curr_block);
134}
135
136/*
Dave Chinner71e330b2010-05-21 14:37:18 +1000137 * Format log item into a flat buffers
138 *
139 * For delayed logging, we need to hold a formatted buffer containing all the
140 * changes on the log item. This enables us to relog the item in memory and
141 * write it out asynchronously without needing to relock the object that was
142 * modified at the time it gets written into the iclog.
143 *
144 * This function builds a vector for the changes in each log item in the
145 * transaction. It then works out the length of the buffer needed for each log
146 * item, allocates them and formats the vector for the item into the buffer.
147 * The buffer is then attached to the log item are then inserted into the
148 * Committed Item List for tracking until the next checkpoint is written out.
149 *
150 * We don't set up region headers during this process; we simply copy the
151 * regions into the flat buffer. We can do this because we still have to do a
152 * formatting step to write the regions into the iclog buffer. Writing the
153 * ophdrs during the iclog write means that we can support splitting large
154 * regions across iclog boundares without needing a change in the format of the
155 * item/region encapsulation.
156 *
157 * Hence what we need to do now is change the rewrite the vector array to point
158 * to the copied region inside the buffer we just allocated. This allows us to
159 * format the regions into the iclog as though they are being formatted
160 * directly out of the objects themselves.
161 */
Christoph Hellwig0244b962011-12-06 21:58:08 +0000162static struct xfs_log_vec *
163xlog_cil_prepare_log_vecs(
164 struct xfs_trans *tp)
Dave Chinner71e330b2010-05-21 14:37:18 +1000165{
Christoph Hellwig0244b962011-12-06 21:58:08 +0000166 struct xfs_log_item_desc *lidp;
167 struct xfs_log_vec *lv = NULL;
168 struct xfs_log_vec *ret_lv = NULL;
Dave Chinner71e330b2010-05-21 14:37:18 +1000169
Christoph Hellwig0244b962011-12-06 21:58:08 +0000170
171 /* Bail out if we didn't find a log item. */
172 if (list_empty(&tp->t_items)) {
173 ASSERT(0);
174 return NULL;
175 }
176
177 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
178 struct xfs_log_vec *new_lv;
Dave Chinner71e330b2010-05-21 14:37:18 +1000179 void *ptr;
180 int index;
181 int len = 0;
Christoph Hellwigb3934212011-12-06 21:58:09 +0000182 uint niovecs;
Dave Chinner71e330b2010-05-21 14:37:18 +1000183
Christoph Hellwig0244b962011-12-06 21:58:08 +0000184 /* Skip items which aren't dirty in this transaction. */
185 if (!(lidp->lid_flags & XFS_LID_DIRTY))
186 continue;
187
188 /* Skip items that do not have any vectors for writing */
Christoph Hellwigb3934212011-12-06 21:58:09 +0000189 niovecs = IOP_SIZE(lidp->lid_item);
190 if (!niovecs)
Christoph Hellwig0244b962011-12-06 21:58:08 +0000191 continue;
192
193 new_lv = kmem_zalloc(sizeof(*new_lv) +
Christoph Hellwigb3934212011-12-06 21:58:09 +0000194 niovecs * sizeof(struct xfs_log_iovec),
Christoph Hellwig0244b962011-12-06 21:58:08 +0000195 KM_SLEEP);
196
197 /* The allocated iovec region lies beyond the log vector. */
198 new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
Christoph Hellwigb3934212011-12-06 21:58:09 +0000199 new_lv->lv_niovecs = niovecs;
Christoph Hellwig0244b962011-12-06 21:58:08 +0000200 new_lv->lv_item = lidp->lid_item;
201
Dave Chinner71e330b2010-05-21 14:37:18 +1000202 /* build the vector array and calculate it's length */
Christoph Hellwig0244b962011-12-06 21:58:08 +0000203 IOP_FORMAT(new_lv->lv_item, new_lv->lv_iovecp);
204 for (index = 0; index < new_lv->lv_niovecs; index++)
205 len += new_lv->lv_iovecp[index].i_len;
Dave Chinner71e330b2010-05-21 14:37:18 +1000206
Christoph Hellwig0244b962011-12-06 21:58:08 +0000207 new_lv->lv_buf_len = len;
208 new_lv->lv_buf = kmem_alloc(new_lv->lv_buf_len,
209 KM_SLEEP|KM_NOFS);
210 ptr = new_lv->lv_buf;
Dave Chinner71e330b2010-05-21 14:37:18 +1000211
Christoph Hellwig0244b962011-12-06 21:58:08 +0000212 for (index = 0; index < new_lv->lv_niovecs; index++) {
213 struct xfs_log_iovec *vec = &new_lv->lv_iovecp[index];
Dave Chinner71e330b2010-05-21 14:37:18 +1000214
215 memcpy(ptr, vec->i_addr, vec->i_len);
216 vec->i_addr = ptr;
217 ptr += vec->i_len;
218 }
Christoph Hellwig0244b962011-12-06 21:58:08 +0000219 ASSERT(ptr == new_lv->lv_buf + new_lv->lv_buf_len);
220
221 if (!ret_lv)
222 ret_lv = new_lv;
223 else
224 lv->lv_next = new_lv;
225 lv = new_lv;
Dave Chinner71e330b2010-05-21 14:37:18 +1000226 }
Christoph Hellwig0244b962011-12-06 21:58:08 +0000227
228 return ret_lv;
Dave Chinner71e330b2010-05-21 14:37:18 +1000229}
230
Dave Chinnerd1583a32010-09-24 18:14:13 +1000231/*
232 * Prepare the log item for insertion into the CIL. Calculate the difference in
233 * log space and vectors it will consume, and if it is a new item pin it as
234 * well.
235 */
236STATIC void
237xfs_cil_prepare_item(
238 struct log *log,
239 struct xfs_log_vec *lv,
240 int *len,
241 int *diff_iovecs)
242{
243 struct xfs_log_vec *old = lv->lv_item->li_lv;
244
245 if (old) {
246 /* existing lv on log item, space used is a delta */
247 ASSERT(!list_empty(&lv->lv_item->li_cil));
248 ASSERT(old->lv_buf && old->lv_buf_len && old->lv_niovecs);
249
250 *len += lv->lv_buf_len - old->lv_buf_len;
251 *diff_iovecs += lv->lv_niovecs - old->lv_niovecs;
252 kmem_free(old->lv_buf);
253 kmem_free(old);
254 } else {
255 /* new lv, must pin the log item */
256 ASSERT(!lv->lv_item->li_lv);
257 ASSERT(list_empty(&lv->lv_item->li_cil));
258
259 *len += lv->lv_buf_len;
260 *diff_iovecs += lv->lv_niovecs;
261 IOP_PIN(lv->lv_item);
262
263 }
264
265 /* attach new log vector to log item */
266 lv->lv_item->li_lv = lv;
267
268 /*
269 * If this is the first time the item is being committed to the
270 * CIL, store the sequence number on the log item so we can
271 * tell in future commits whether this is the first checkpoint
272 * the item is being committed into.
273 */
274 if (!lv->lv_item->li_seq)
275 lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
276}
277
278/*
279 * Insert the log items into the CIL and calculate the difference in space
280 * consumed by the item. Add the space to the checkpoint ticket and calculate
281 * if the change requires additional log metadata. If it does, take that space
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800282 * as well. Remove the amount of space we added to the checkpoint ticket from
Dave Chinnerd1583a32010-09-24 18:14:13 +1000283 * the current transaction ticket so that the accounting works out correctly.
284 */
Dave Chinner71e330b2010-05-21 14:37:18 +1000285static void
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000286xlog_cil_insert_items(
287 struct log *log,
288 struct xfs_log_vec *log_vector,
Dave Chinnerd1583a32010-09-24 18:14:13 +1000289 struct xlog_ticket *ticket)
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000290{
Dave Chinnerd1583a32010-09-24 18:14:13 +1000291 struct xfs_cil *cil = log->l_cilp;
292 struct xfs_cil_ctx *ctx = cil->xc_ctx;
293 struct xfs_log_vec *lv;
294 int len = 0;
295 int diff_iovecs = 0;
296 int iclog_space;
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000297
298 ASSERT(log_vector);
Dave Chinnerd1583a32010-09-24 18:14:13 +1000299
300 /*
301 * Do all the accounting aggregation and switching of log vectors
302 * around in a separate loop to the insertion of items into the CIL.
303 * Then we can do a separate loop to update the CIL within a single
304 * lock/unlock pair. This reduces the number of round trips on the CIL
305 * lock from O(nr_logvectors) to O(1) and greatly reduces the overall
306 * hold time for the transaction commit.
307 *
308 * If this is the first time the item is being placed into the CIL in
309 * this context, pin it so it can't be written to disk until the CIL is
310 * flushed to the iclog and the iclog written to disk.
311 *
312 * We can do this safely because the context can't checkpoint until we
313 * are done so it doesn't matter exactly how we update the CIL.
314 */
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000315 for (lv = log_vector; lv; lv = lv->lv_next)
Dave Chinnerd1583a32010-09-24 18:14:13 +1000316 xfs_cil_prepare_item(log, lv, &len, &diff_iovecs);
317
318 /* account for space used by new iovec headers */
319 len += diff_iovecs * sizeof(xlog_op_header_t);
320
321 spin_lock(&cil->xc_cil_lock);
322
323 /* move the items to the tail of the CIL */
324 for (lv = log_vector; lv; lv = lv->lv_next)
325 list_move_tail(&lv->lv_item->li_cil, &cil->xc_cil);
326
327 ctx->nvecs += diff_iovecs;
328
329 /*
330 * Now transfer enough transaction reservation to the context ticket
331 * for the checkpoint. The context ticket is special - the unit
332 * reservation has to grow as well as the current reservation as we
333 * steal from tickets so we can correctly determine the space used
334 * during the transaction commit.
335 */
336 if (ctx->ticket->t_curr_res == 0) {
337 /* first commit in checkpoint, steal the header reservation */
338 ASSERT(ticket->t_curr_res >= ctx->ticket->t_unit_res + len);
339 ctx->ticket->t_curr_res = ctx->ticket->t_unit_res;
340 ticket->t_curr_res -= ctx->ticket->t_unit_res;
341 }
342
343 /* do we need space for more log record headers? */
344 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
345 if (len > 0 && (ctx->space_used / iclog_space !=
346 (ctx->space_used + len) / iclog_space)) {
347 int hdrs;
348
349 hdrs = (len + iclog_space - 1) / iclog_space;
350 /* need to take into account split region headers, too */
351 hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
352 ctx->ticket->t_unit_res += hdrs;
353 ctx->ticket->t_curr_res += hdrs;
354 ticket->t_curr_res -= hdrs;
355 ASSERT(ticket->t_curr_res >= len);
356 }
357 ticket->t_curr_res -= len;
358 ctx->space_used += len;
359
360 spin_unlock(&cil->xc_cil_lock);
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000361}
362
363static void
Dave Chinner71e330b2010-05-21 14:37:18 +1000364xlog_cil_free_logvec(
365 struct xfs_log_vec *log_vector)
366{
367 struct xfs_log_vec *lv;
368
369 for (lv = log_vector; lv; ) {
370 struct xfs_log_vec *next = lv->lv_next;
371 kmem_free(lv->lv_buf);
372 kmem_free(lv);
373 lv = next;
374 }
375}
376
377/*
Dave Chinner71e330b2010-05-21 14:37:18 +1000378 * Mark all items committed and clear busy extents. We free the log vector
379 * chains in a separate pass so that we unpin the log items as quickly as
380 * possible.
381 */
382static void
383xlog_cil_committed(
384 void *args,
385 int abort)
386{
387 struct xfs_cil_ctx *ctx = args;
Christoph Hellwige84661a2011-05-20 13:45:32 +0000388 struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
Dave Chinner71e330b2010-05-21 14:37:18 +1000389
Dave Chinner0e57f6a2010-12-20 12:02:19 +1100390 xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
391 ctx->start_lsn, abort);
Dave Chinner71e330b2010-05-21 14:37:18 +1000392
Christoph Hellwig8a072a42011-04-24 19:06:17 +0000393 xfs_alloc_busy_sort(&ctx->busy_extents);
Christoph Hellwige84661a2011-05-20 13:45:32 +0000394 xfs_alloc_busy_clear(mp, &ctx->busy_extents,
395 (mp->m_flags & XFS_MOUNT_DISCARD) && !abort);
Dave Chinner71e330b2010-05-21 14:37:18 +1000396
397 spin_lock(&ctx->cil->xc_cil_lock);
398 list_del(&ctx->committing);
399 spin_unlock(&ctx->cil->xc_cil_lock);
400
401 xlog_cil_free_logvec(ctx->lv_chain);
Christoph Hellwige84661a2011-05-20 13:45:32 +0000402
403 if (!list_empty(&ctx->busy_extents)) {
404 ASSERT(mp->m_flags & XFS_MOUNT_DISCARD);
405
406 xfs_discard_extents(mp, &ctx->busy_extents);
407 xfs_alloc_busy_clear(mp, &ctx->busy_extents, false);
408 }
409
Dave Chinner71e330b2010-05-21 14:37:18 +1000410 kmem_free(ctx);
411}
412
413/*
Dave Chinnera44f13e2010-08-24 11:40:03 +1000414 * Push the Committed Item List to the log. If @push_seq flag is zero, then it
415 * is a background flush and so we can chose to ignore it. Otherwise, if the
416 * current sequence is the same as @push_seq we need to do a flush. If
417 * @push_seq is less than the current sequence, then it has already been
418 * flushed and we don't need to do anything - the caller will wait for it to
419 * complete if necessary.
420 *
421 * @push_seq is a value rather than a flag because that allows us to do an
422 * unlocked check of the sequence number for a match. Hence we can allows log
423 * forces to run racily and not issue pushes for the same sequence twice. If we
424 * get a race between multiple pushes for the same sequence they will block on
425 * the first one and then abort, hence avoiding needless pushes.
Dave Chinner71e330b2010-05-21 14:37:18 +1000426 */
Dave Chinnera44f13e2010-08-24 11:40:03 +1000427STATIC int
Dave Chinner71e330b2010-05-21 14:37:18 +1000428xlog_cil_push(
429 struct log *log,
Dave Chinnera44f13e2010-08-24 11:40:03 +1000430 xfs_lsn_t push_seq)
Dave Chinner71e330b2010-05-21 14:37:18 +1000431{
432 struct xfs_cil *cil = log->l_cilp;
433 struct xfs_log_vec *lv;
434 struct xfs_cil_ctx *ctx;
435 struct xfs_cil_ctx *new_ctx;
436 struct xlog_in_core *commit_iclog;
437 struct xlog_ticket *tic;
438 int num_lv;
439 int num_iovecs;
440 int len;
441 int error = 0;
442 struct xfs_trans_header thdr;
443 struct xfs_log_iovec lhdr;
444 struct xfs_log_vec lvhdr = { NULL };
445 xfs_lsn_t commit_lsn;
446
447 if (!cil)
448 return 0;
449
Dave Chinnera44f13e2010-08-24 11:40:03 +1000450 ASSERT(!push_seq || push_seq <= cil->xc_ctx->sequence);
451
Dave Chinner71e330b2010-05-21 14:37:18 +1000452 new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
453 new_ctx->ticket = xlog_cil_ticket_alloc(log);
454
Dave Chinner80168672010-09-24 18:13:44 +1000455 /*
456 * Lock out transaction commit, but don't block for background pushes
457 * unless we are well over the CIL space limit. See the definition of
458 * XLOG_CIL_HARD_SPACE_LIMIT() for the full explanation of the logic
459 * used here.
460 */
Dave Chinnerdf806152010-05-17 15:52:13 +1000461 if (!down_write_trylock(&cil->xc_ctx_lock)) {
Dave Chinner80168672010-09-24 18:13:44 +1000462 if (!push_seq &&
463 cil->xc_ctx->space_used < XLOG_CIL_HARD_SPACE_LIMIT(log))
Dave Chinnerdf806152010-05-17 15:52:13 +1000464 goto out_free_ticket;
465 down_write(&cil->xc_ctx_lock);
466 }
Dave Chinner71e330b2010-05-21 14:37:18 +1000467 ctx = cil->xc_ctx;
468
469 /* check if we've anything to push */
470 if (list_empty(&cil->xc_cil))
471 goto out_skip;
472
Dave Chinnerdf806152010-05-17 15:52:13 +1000473 /* check for spurious background flush */
Dave Chinnera44f13e2010-08-24 11:40:03 +1000474 if (!push_seq && cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log))
475 goto out_skip;
476
477 /* check for a previously pushed seqeunce */
Dave Chinner80168672010-09-24 18:13:44 +1000478 if (push_seq && push_seq < cil->xc_ctx->sequence)
Dave Chinnerdf806152010-05-17 15:52:13 +1000479 goto out_skip;
480
Dave Chinner71e330b2010-05-21 14:37:18 +1000481 /*
482 * pull all the log vectors off the items in the CIL, and
483 * remove the items from the CIL. We don't need the CIL lock
484 * here because it's only needed on the transaction commit
485 * side which is currently locked out by the flush lock.
486 */
487 lv = NULL;
488 num_lv = 0;
489 num_iovecs = 0;
490 len = 0;
491 while (!list_empty(&cil->xc_cil)) {
492 struct xfs_log_item *item;
493 int i;
494
495 item = list_first_entry(&cil->xc_cil,
496 struct xfs_log_item, li_cil);
497 list_del_init(&item->li_cil);
498 if (!ctx->lv_chain)
499 ctx->lv_chain = item->li_lv;
500 else
501 lv->lv_next = item->li_lv;
502 lv = item->li_lv;
503 item->li_lv = NULL;
504
505 num_lv++;
506 num_iovecs += lv->lv_niovecs;
507 for (i = 0; i < lv->lv_niovecs; i++)
508 len += lv->lv_iovecp[i].i_len;
509 }
510
511 /*
512 * initialise the new context and attach it to the CIL. Then attach
513 * the current context to the CIL committing lsit so it can be found
514 * during log forces to extract the commit lsn of the sequence that
515 * needs to be forced.
516 */
517 INIT_LIST_HEAD(&new_ctx->committing);
518 INIT_LIST_HEAD(&new_ctx->busy_extents);
519 new_ctx->sequence = ctx->sequence + 1;
520 new_ctx->cil = cil;
521 cil->xc_ctx = new_ctx;
522
523 /*
Dave Chinnera44f13e2010-08-24 11:40:03 +1000524 * mirror the new sequence into the cil structure so that we can do
525 * unlocked checks against the current sequence in log forces without
526 * risking deferencing a freed context pointer.
527 */
528 cil->xc_current_sequence = new_ctx->sequence;
529
530 /*
Dave Chinner71e330b2010-05-21 14:37:18 +1000531 * The switch is now done, so we can drop the context lock and move out
532 * of a shared context. We can't just go straight to the commit record,
533 * though - we need to synchronise with previous and future commits so
534 * that the commit records are correctly ordered in the log to ensure
535 * that we process items during log IO completion in the correct order.
536 *
537 * For example, if we get an EFI in one checkpoint and the EFD in the
538 * next (e.g. due to log forces), we do not want the checkpoint with
539 * the EFD to be committed before the checkpoint with the EFI. Hence
540 * we must strictly order the commit records of the checkpoints so
541 * that: a) the checkpoint callbacks are attached to the iclogs in the
542 * correct order; and b) the checkpoints are replayed in correct order
543 * in log recovery.
544 *
545 * Hence we need to add this context to the committing context list so
546 * that higher sequences will wait for us to write out a commit record
547 * before they do.
548 */
549 spin_lock(&cil->xc_cil_lock);
550 list_add(&ctx->committing, &cil->xc_committing);
551 spin_unlock(&cil->xc_cil_lock);
552 up_write(&cil->xc_ctx_lock);
553
554 /*
555 * Build a checkpoint transaction header and write it to the log to
556 * begin the transaction. We need to account for the space used by the
557 * transaction header here as it is not accounted for in xlog_write().
558 *
559 * The LSN we need to pass to the log items on transaction commit is
560 * the LSN reported by the first log vector write. If we use the commit
561 * record lsn then we can move the tail beyond the grant write head.
562 */
563 tic = ctx->ticket;
564 thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
565 thdr.th_type = XFS_TRANS_CHECKPOINT;
566 thdr.th_tid = tic->t_tid;
567 thdr.th_num_items = num_iovecs;
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000568 lhdr.i_addr = &thdr;
Dave Chinner71e330b2010-05-21 14:37:18 +1000569 lhdr.i_len = sizeof(xfs_trans_header_t);
570 lhdr.i_type = XLOG_REG_TYPE_TRANSHDR;
571 tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t);
572
573 lvhdr.lv_niovecs = 1;
574 lvhdr.lv_iovecp = &lhdr;
575 lvhdr.lv_next = ctx->lv_chain;
576
577 error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0);
578 if (error)
Dave Chinner7db37c52011-01-27 12:02:00 +1100579 goto out_abort_free_ticket;
Dave Chinner71e330b2010-05-21 14:37:18 +1000580
581 /*
582 * now that we've written the checkpoint into the log, strictly
583 * order the commit records so replay will get them in the right order.
584 */
585restart:
586 spin_lock(&cil->xc_cil_lock);
587 list_for_each_entry(new_ctx, &cil->xc_committing, committing) {
588 /*
589 * Higher sequences will wait for this one so skip them.
590 * Don't wait for own own sequence, either.
591 */
592 if (new_ctx->sequence >= ctx->sequence)
593 continue;
594 if (!new_ctx->commit_lsn) {
595 /*
596 * It is still being pushed! Wait for the push to
597 * complete, then start again from the beginning.
598 */
Dave Chinnereb40a872010-12-21 12:09:01 +1100599 xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
Dave Chinner71e330b2010-05-21 14:37:18 +1000600 goto restart;
601 }
602 }
603 spin_unlock(&cil->xc_cil_lock);
604
Dave Chinner7db37c52011-01-27 12:02:00 +1100605 /* xfs_log_done always frees the ticket on error. */
Dave Chinner71e330b2010-05-21 14:37:18 +1000606 commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0);
Dave Chinner7db37c52011-01-27 12:02:00 +1100607 if (commit_lsn == -1)
Dave Chinner71e330b2010-05-21 14:37:18 +1000608 goto out_abort;
609
610 /* attach all the transactions w/ busy extents to iclog */
611 ctx->log_cb.cb_func = xlog_cil_committed;
612 ctx->log_cb.cb_arg = ctx;
613 error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb);
614 if (error)
615 goto out_abort;
616
617 /*
618 * now the checkpoint commit is complete and we've attached the
619 * callbacks to the iclog we can assign the commit LSN to the context
620 * and wake up anyone who is waiting for the commit to complete.
621 */
622 spin_lock(&cil->xc_cil_lock);
623 ctx->commit_lsn = commit_lsn;
Dave Chinnereb40a872010-12-21 12:09:01 +1100624 wake_up_all(&cil->xc_commit_wait);
Dave Chinner71e330b2010-05-21 14:37:18 +1000625 spin_unlock(&cil->xc_cil_lock);
626
627 /* release the hounds! */
628 return xfs_log_release_iclog(log->l_mp, commit_iclog);
629
630out_skip:
631 up_write(&cil->xc_ctx_lock);
Dave Chinnerdf806152010-05-17 15:52:13 +1000632out_free_ticket:
Dave Chinner71e330b2010-05-21 14:37:18 +1000633 xfs_log_ticket_put(new_ctx->ticket);
634 kmem_free(new_ctx);
635 return 0;
636
Dave Chinner7db37c52011-01-27 12:02:00 +1100637out_abort_free_ticket:
638 xfs_log_ticket_put(tic);
Dave Chinner71e330b2010-05-21 14:37:18 +1000639out_abort:
640 xlog_cil_committed(ctx, XFS_LI_ABORTED);
641 return XFS_ERROR(EIO);
642}
643
644/*
Dave Chinnera44f13e2010-08-24 11:40:03 +1000645 * Commit a transaction with the given vector to the Committed Item List.
646 *
647 * To do this, we need to format the item, pin it in memory if required and
648 * account for the space used by the transaction. Once we have done that we
649 * need to release the unused reservation for the transaction, attach the
650 * transaction to the checkpoint context so we carry the busy extents through
651 * to checkpoint completion, and then unlock all the items in the transaction.
652 *
653 * For more specific information about the order of operations in
654 * xfs_log_commit_cil() please refer to the comments in
655 * xfs_trans_commit_iclog().
656 *
657 * Called with the context lock already held in read mode to lock out
658 * background commit, returns without it held once background commits are
659 * allowed again.
660 */
Christoph Hellwig0244b962011-12-06 21:58:08 +0000661int
Dave Chinnera44f13e2010-08-24 11:40:03 +1000662xfs_log_commit_cil(
663 struct xfs_mount *mp,
664 struct xfs_trans *tp,
Dave Chinnera44f13e2010-08-24 11:40:03 +1000665 xfs_lsn_t *commit_lsn,
666 int flags)
667{
668 struct log *log = mp->m_log;
669 int log_flags = 0;
670 int push = 0;
Christoph Hellwig0244b962011-12-06 21:58:08 +0000671 struct xfs_log_vec *log_vector;
Dave Chinnera44f13e2010-08-24 11:40:03 +1000672
673 if (flags & XFS_TRANS_RELEASE_LOG_RES)
674 log_flags = XFS_LOG_REL_PERM_RESERV;
675
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000676 /*
Christoph Hellwig0244b962011-12-06 21:58:08 +0000677 * Do all the hard work of formatting items (including memory
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000678 * allocation) outside the CIL context lock. This prevents stalling CIL
679 * pushes when we are low on memory and a transaction commit spends a
680 * lot of time in memory reclaim.
681 */
Christoph Hellwig0244b962011-12-06 21:58:08 +0000682 log_vector = xlog_cil_prepare_log_vecs(tp);
683 if (!log_vector)
684 return ENOMEM;
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000685
Dave Chinnera44f13e2010-08-24 11:40:03 +1000686 /* lock out background commit */
687 down_read(&log->l_cilp->xc_ctx_lock);
Dave Chinnerd1583a32010-09-24 18:14:13 +1000688 if (commit_lsn)
689 *commit_lsn = log->l_cilp->xc_ctx->sequence;
690
691 xlog_cil_insert_items(log, log_vector, tp->t_ticket);
Dave Chinnera44f13e2010-08-24 11:40:03 +1000692
693 /* check we didn't blow the reservation */
694 if (tp->t_ticket->t_curr_res < 0)
695 xlog_print_tic_res(log->l_mp, tp->t_ticket);
696
697 /* attach the transaction to the CIL if it has any busy extents */
698 if (!list_empty(&tp->t_busy)) {
699 spin_lock(&log->l_cilp->xc_cil_lock);
700 list_splice_init(&tp->t_busy,
701 &log->l_cilp->xc_ctx->busy_extents);
702 spin_unlock(&log->l_cilp->xc_cil_lock);
703 }
704
705 tp->t_commit_lsn = *commit_lsn;
706 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
707 xfs_trans_unreserve_and_mod_sb(tp);
708
709 /*
710 * Once all the items of the transaction have been copied to the CIL,
711 * the items can be unlocked and freed.
712 *
713 * This needs to be done before we drop the CIL context lock because we
714 * have to update state in the log items and unlock them before they go
715 * to disk. If we don't, then the CIL checkpoint can race with us and
716 * we can run checkpoint completion before we've updated and unlocked
717 * the log items. This affects (at least) processing of stale buffers,
718 * inodes and EFIs.
719 */
720 xfs_trans_free_items(tp, *commit_lsn, 0);
721
722 /* check for background commit before unlock */
723 if (log->l_cilp->xc_ctx->space_used > XLOG_CIL_SPACE_LIMIT(log))
724 push = 1;
725
726 up_read(&log->l_cilp->xc_ctx_lock);
727
728 /*
729 * We need to push CIL every so often so we don't cache more than we
730 * can fit in the log. The limit really is that a checkpoint can't be
731 * more than half the log (the current checkpoint is not allowed to
732 * overwrite the previous checkpoint), but commit latency and memory
733 * usage limit this to a smaller size in most cases.
734 */
735 if (push)
736 xlog_cil_push(log, 0);
Christoph Hellwig0244b962011-12-06 21:58:08 +0000737 return 0;
Dave Chinnera44f13e2010-08-24 11:40:03 +1000738}
739
740/*
Dave Chinner71e330b2010-05-21 14:37:18 +1000741 * Conditionally push the CIL based on the sequence passed in.
742 *
743 * We only need to push if we haven't already pushed the sequence
744 * number given. Hence the only time we will trigger a push here is
745 * if the push sequence is the same as the current context.
746 *
747 * We return the current commit lsn to allow the callers to determine if a
748 * iclog flush is necessary following this call.
749 *
750 * XXX: Initially, just push the CIL unconditionally and return whatever
751 * commit lsn is there. It'll be empty, so this is broken for now.
752 */
753xfs_lsn_t
Dave Chinnera44f13e2010-08-24 11:40:03 +1000754xlog_cil_force_lsn(
Dave Chinner71e330b2010-05-21 14:37:18 +1000755 struct log *log,
Dave Chinnera44f13e2010-08-24 11:40:03 +1000756 xfs_lsn_t sequence)
Dave Chinner71e330b2010-05-21 14:37:18 +1000757{
758 struct xfs_cil *cil = log->l_cilp;
759 struct xfs_cil_ctx *ctx;
760 xfs_lsn_t commit_lsn = NULLCOMMITLSN;
761
Dave Chinnera44f13e2010-08-24 11:40:03 +1000762 ASSERT(sequence <= cil->xc_current_sequence);
Dave Chinner71e330b2010-05-21 14:37:18 +1000763
Dave Chinnera44f13e2010-08-24 11:40:03 +1000764 /*
765 * check to see if we need to force out the current context.
766 * xlog_cil_push() handles racing pushes for the same sequence,
767 * so no need to deal with it here.
768 */
769 if (sequence == cil->xc_current_sequence)
770 xlog_cil_push(log, sequence);
Dave Chinner71e330b2010-05-21 14:37:18 +1000771
772 /*
773 * See if we can find a previous sequence still committing.
Dave Chinner71e330b2010-05-21 14:37:18 +1000774 * We need to wait for all previous sequence commits to complete
775 * before allowing the force of push_seq to go ahead. Hence block
776 * on commits for those as well.
777 */
Dave Chinnera44f13e2010-08-24 11:40:03 +1000778restart:
Dave Chinner71e330b2010-05-21 14:37:18 +1000779 spin_lock(&cil->xc_cil_lock);
Dave Chinner71e330b2010-05-21 14:37:18 +1000780 list_for_each_entry(ctx, &cil->xc_committing, committing) {
Dave Chinnera44f13e2010-08-24 11:40:03 +1000781 if (ctx->sequence > sequence)
Dave Chinner71e330b2010-05-21 14:37:18 +1000782 continue;
783 if (!ctx->commit_lsn) {
784 /*
785 * It is still being pushed! Wait for the push to
786 * complete, then start again from the beginning.
787 */
Dave Chinnereb40a872010-12-21 12:09:01 +1100788 xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
Dave Chinner71e330b2010-05-21 14:37:18 +1000789 goto restart;
790 }
Dave Chinnera44f13e2010-08-24 11:40:03 +1000791 if (ctx->sequence != sequence)
Dave Chinner71e330b2010-05-21 14:37:18 +1000792 continue;
793 /* found it! */
794 commit_lsn = ctx->commit_lsn;
795 }
796 spin_unlock(&cil->xc_cil_lock);
797 return commit_lsn;
798}
Dave Chinnerccf7c232010-05-20 23:19:42 +1000799
800/*
801 * Check if the current log item was first committed in this sequence.
802 * We can't rely on just the log item being in the CIL, we have to check
803 * the recorded commit sequence number.
804 *
805 * Note: for this to be used in a non-racy manner, it has to be called with
806 * CIL flushing locked out. As a result, it should only be used during the
807 * transaction commit process when deciding what to format into the item.
808 */
809bool
810xfs_log_item_in_current_chkpt(
811 struct xfs_log_item *lip)
812{
813 struct xfs_cil_ctx *ctx;
814
Dave Chinnerccf7c232010-05-20 23:19:42 +1000815 if (list_empty(&lip->li_cil))
816 return false;
817
818 ctx = lip->li_mountp->m_log->l_cilp->xc_ctx;
819
820 /*
821 * li_seq is written on the first commit of a log item to record the
822 * first checkpoint it is written to. Hence if it is different to the
823 * current sequence, we're in a new checkpoint.
824 */
825 if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0)
826 return false;
827 return true;
828}