blob: 7e206fc1fa362ed4bb761a8f5ad11ef39c2dd5c0 [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"
32
33/*
34 * Perform initial CIL structure initialisation. If the CIL is not
35 * enabled in this filesystem, ensure the log->l_cilp is null so
36 * we can check this conditional to determine if we are doing delayed
37 * logging or not.
38 */
39int
40xlog_cil_init(
41 struct log *log)
42{
43 struct xfs_cil *cil;
44 struct xfs_cil_ctx *ctx;
45
46 log->l_cilp = NULL;
47 if (!(log->l_mp->m_flags & XFS_MOUNT_DELAYLOG))
48 return 0;
49
50 cil = kmem_zalloc(sizeof(*cil), KM_SLEEP|KM_MAYFAIL);
51 if (!cil)
52 return ENOMEM;
53
54 ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP|KM_MAYFAIL);
55 if (!ctx) {
56 kmem_free(cil);
57 return ENOMEM;
58 }
59
60 INIT_LIST_HEAD(&cil->xc_cil);
61 INIT_LIST_HEAD(&cil->xc_committing);
62 spin_lock_init(&cil->xc_cil_lock);
63 init_rwsem(&cil->xc_ctx_lock);
64 sv_init(&cil->xc_commit_wait, SV_DEFAULT, "cilwait");
65
66 INIT_LIST_HEAD(&ctx->committing);
67 INIT_LIST_HEAD(&ctx->busy_extents);
68 ctx->sequence = 1;
69 ctx->cil = cil;
70 cil->xc_ctx = ctx;
Dave Chinnera44f13e2010-08-24 11:40:03 +100071 cil->xc_current_sequence = ctx->sequence;
Dave Chinner71e330b2010-05-21 14:37:18 +100072
73 cil->xc_log = log;
74 log->l_cilp = cil;
75 return 0;
76}
77
78void
79xlog_cil_destroy(
80 struct log *log)
81{
82 if (!log->l_cilp)
83 return;
84
85 if (log->l_cilp->xc_ctx) {
86 if (log->l_cilp->xc_ctx->ticket)
87 xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket);
88 kmem_free(log->l_cilp->xc_ctx);
89 }
90
91 ASSERT(list_empty(&log->l_cilp->xc_cil));
92 kmem_free(log->l_cilp);
93}
94
95/*
96 * Allocate a new ticket. Failing to get a new ticket makes it really hard to
97 * recover, so we don't allow failure here. Also, we allocate in a context that
98 * we don't want to be issuing transactions from, so we need to tell the
99 * allocation code this as well.
100 *
101 * We don't reserve any space for the ticket - we are going to steal whatever
102 * space we require from transactions as they commit. To ensure we reserve all
103 * the space required, we need to set the current reservation of the ticket to
104 * zero so that we know to steal the initial transaction overhead from the
105 * first transaction commit.
106 */
107static struct xlog_ticket *
108xlog_cil_ticket_alloc(
109 struct log *log)
110{
111 struct xlog_ticket *tic;
112
113 tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0,
114 KM_SLEEP|KM_NOFS);
115 tic->t_trans_type = XFS_TRANS_CHECKPOINT;
116
117 /*
118 * set the current reservation to zero so we know to steal the basic
119 * transaction overhead reservation from the first transaction commit.
120 */
121 tic->t_curr_res = 0;
122 return tic;
123}
124
125/*
126 * After the first stage of log recovery is done, we know where the head and
127 * tail of the log are. We need this log initialisation done before we can
128 * initialise the first CIL checkpoint context.
129 *
130 * Here we allocate a log ticket to track space usage during a CIL push. This
131 * ticket is passed to xlog_write() directly so that we don't slowly leak log
132 * space by failing to account for space used by log headers and additional
133 * region headers for split regions.
134 */
135void
136xlog_cil_init_post_recovery(
137 struct log *log)
138{
139 if (!log->l_cilp)
140 return;
141
142 log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
143 log->l_cilp->xc_ctx->sequence = 1;
144 log->l_cilp->xc_ctx->commit_lsn = xlog_assign_lsn(log->l_curr_cycle,
145 log->l_curr_block);
146}
147
148/*
149 * Insert the log item into the CIL and calculate the difference in space
150 * consumed by the item. Add the space to the checkpoint ticket and calculate
151 * if the change requires additional log metadata. If it does, take that space
152 * as well. Remove the amount of space we addded to the checkpoint ticket from
153 * the current transaction ticket so that the accounting works out correctly.
154 *
155 * If this is the first time the item is being placed into the CIL in this
156 * context, pin it so it can't be written to disk until the CIL is flushed to
157 * the iclog and the iclog written to disk.
158 */
159static void
160xlog_cil_insert(
161 struct log *log,
162 struct xlog_ticket *ticket,
163 struct xfs_log_item *item,
164 struct xfs_log_vec *lv)
165{
166 struct xfs_cil *cil = log->l_cilp;
167 struct xfs_log_vec *old = lv->lv_item->li_lv;
168 struct xfs_cil_ctx *ctx = cil->xc_ctx;
169 int len;
170 int diff_iovecs;
171 int iclog_space;
172
173 if (old) {
174 /* existing lv on log item, space used is a delta */
175 ASSERT(!list_empty(&item->li_cil));
176 ASSERT(old->lv_buf && old->lv_buf_len && old->lv_niovecs);
177
178 len = lv->lv_buf_len - old->lv_buf_len;
179 diff_iovecs = lv->lv_niovecs - old->lv_niovecs;
180 kmem_free(old->lv_buf);
181 kmem_free(old);
182 } else {
183 /* new lv, must pin the log item */
184 ASSERT(!lv->lv_item->li_lv);
185 ASSERT(list_empty(&item->li_cil));
186
187 len = lv->lv_buf_len;
188 diff_iovecs = lv->lv_niovecs;
189 IOP_PIN(lv->lv_item);
190
191 }
192 len += diff_iovecs * sizeof(xlog_op_header_t);
193
194 /* attach new log vector to log item */
195 lv->lv_item->li_lv = lv;
196
197 spin_lock(&cil->xc_cil_lock);
198 list_move_tail(&item->li_cil, &cil->xc_cil);
199 ctx->nvecs += diff_iovecs;
200
201 /*
Dave Chinnerccf7c232010-05-20 23:19:42 +1000202 * If this is the first time the item is being committed to the CIL,
203 * store the sequence number on the log item so we can tell
204 * in future commits whether this is the first checkpoint the item is
205 * being committed into.
206 */
207 if (!item->li_seq)
208 item->li_seq = ctx->sequence;
209
210 /*
Dave Chinner71e330b2010-05-21 14:37:18 +1000211 * Now transfer enough transaction reservation to the context ticket
212 * for the checkpoint. The context ticket is special - the unit
213 * reservation has to grow as well as the current reservation as we
214 * steal from tickets so we can correctly determine the space used
215 * during the transaction commit.
216 */
217 if (ctx->ticket->t_curr_res == 0) {
218 /* first commit in checkpoint, steal the header reservation */
219 ASSERT(ticket->t_curr_res >= ctx->ticket->t_unit_res + len);
220 ctx->ticket->t_curr_res = ctx->ticket->t_unit_res;
221 ticket->t_curr_res -= ctx->ticket->t_unit_res;
222 }
223
224 /* do we need space for more log record headers? */
225 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
226 if (len > 0 && (ctx->space_used / iclog_space !=
227 (ctx->space_used + len) / iclog_space)) {
228 int hdrs;
229
230 hdrs = (len + iclog_space - 1) / iclog_space;
231 /* need to take into account split region headers, too */
232 hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
233 ctx->ticket->t_unit_res += hdrs;
234 ctx->ticket->t_curr_res += hdrs;
235 ticket->t_curr_res -= hdrs;
236 ASSERT(ticket->t_curr_res >= len);
237 }
238 ticket->t_curr_res -= len;
239 ctx->space_used += len;
240
241 spin_unlock(&cil->xc_cil_lock);
242}
243
244/*
245 * Format log item into a flat buffers
246 *
247 * For delayed logging, we need to hold a formatted buffer containing all the
248 * changes on the log item. This enables us to relog the item in memory and
249 * write it out asynchronously without needing to relock the object that was
250 * modified at the time it gets written into the iclog.
251 *
252 * This function builds a vector for the changes in each log item in the
253 * transaction. It then works out the length of the buffer needed for each log
254 * item, allocates them and formats the vector for the item into the buffer.
255 * The buffer is then attached to the log item are then inserted into the
256 * Committed Item List for tracking until the next checkpoint is written out.
257 *
258 * We don't set up region headers during this process; we simply copy the
259 * regions into the flat buffer. We can do this because we still have to do a
260 * formatting step to write the regions into the iclog buffer. Writing the
261 * ophdrs during the iclog write means that we can support splitting large
262 * regions across iclog boundares without needing a change in the format of the
263 * item/region encapsulation.
264 *
265 * Hence what we need to do now is change the rewrite the vector array to point
266 * to the copied region inside the buffer we just allocated. This allows us to
267 * format the regions into the iclog as though they are being formatted
268 * directly out of the objects themselves.
269 */
270static void
271xlog_cil_format_items(
272 struct log *log,
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000273 struct xfs_log_vec *log_vector)
Dave Chinner71e330b2010-05-21 14:37:18 +1000274{
275 struct xfs_log_vec *lv;
276
Dave Chinner71e330b2010-05-21 14:37:18 +1000277 ASSERT(log_vector);
278 for (lv = log_vector; lv; lv = lv->lv_next) {
279 void *ptr;
280 int index;
281 int len = 0;
282
283 /* build the vector array and calculate it's length */
284 IOP_FORMAT(lv->lv_item, lv->lv_iovecp);
285 for (index = 0; index < lv->lv_niovecs; index++)
286 len += lv->lv_iovecp[index].i_len;
287
288 lv->lv_buf_len = len;
289 lv->lv_buf = kmem_zalloc(lv->lv_buf_len, KM_SLEEP|KM_NOFS);
290 ptr = lv->lv_buf;
291
292 for (index = 0; index < lv->lv_niovecs; index++) {
293 struct xfs_log_iovec *vec = &lv->lv_iovecp[index];
294
295 memcpy(ptr, vec->i_addr, vec->i_len);
296 vec->i_addr = ptr;
297 ptr += vec->i_len;
298 }
299 ASSERT(ptr == lv->lv_buf + lv->lv_buf_len);
Dave Chinner71e330b2010-05-21 14:37:18 +1000300 }
301}
302
303static void
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000304xlog_cil_insert_items(
305 struct log *log,
306 struct xfs_log_vec *log_vector,
307 struct xlog_ticket *ticket,
308 xfs_lsn_t *start_lsn)
309{
310 struct xfs_log_vec *lv;
311
312 if (start_lsn)
313 *start_lsn = log->l_cilp->xc_ctx->sequence;
314
315 ASSERT(log_vector);
316 for (lv = log_vector; lv; lv = lv->lv_next)
317 xlog_cil_insert(log, ticket, lv->lv_item, lv);
318}
319
320static void
Dave Chinner71e330b2010-05-21 14:37:18 +1000321xlog_cil_free_logvec(
322 struct xfs_log_vec *log_vector)
323{
324 struct xfs_log_vec *lv;
325
326 for (lv = log_vector; lv; ) {
327 struct xfs_log_vec *next = lv->lv_next;
328 kmem_free(lv->lv_buf);
329 kmem_free(lv);
330 lv = next;
331 }
332}
333
334/*
Dave Chinner71e330b2010-05-21 14:37:18 +1000335 * Mark all items committed and clear busy extents. We free the log vector
336 * chains in a separate pass so that we unpin the log items as quickly as
337 * possible.
338 */
339static void
340xlog_cil_committed(
341 void *args,
342 int abort)
343{
344 struct xfs_cil_ctx *ctx = args;
345 struct xfs_log_vec *lv;
346 int abortflag = abort ? XFS_LI_ABORTED : 0;
347 struct xfs_busy_extent *busyp, *n;
348
349 /* unpin all the log items */
350 for (lv = ctx->lv_chain; lv; lv = lv->lv_next ) {
351 xfs_trans_item_committed(lv->lv_item, ctx->start_lsn,
352 abortflag);
353 }
354
355 list_for_each_entry_safe(busyp, n, &ctx->busy_extents, list)
356 xfs_alloc_busy_clear(ctx->cil->xc_log->l_mp, busyp);
357
358 spin_lock(&ctx->cil->xc_cil_lock);
359 list_del(&ctx->committing);
360 spin_unlock(&ctx->cil->xc_cil_lock);
361
362 xlog_cil_free_logvec(ctx->lv_chain);
363 kmem_free(ctx);
364}
365
366/*
Dave Chinnera44f13e2010-08-24 11:40:03 +1000367 * Push the Committed Item List to the log. If @push_seq flag is zero, then it
368 * is a background flush and so we can chose to ignore it. Otherwise, if the
369 * current sequence is the same as @push_seq we need to do a flush. If
370 * @push_seq is less than the current sequence, then it has already been
371 * flushed and we don't need to do anything - the caller will wait for it to
372 * complete if necessary.
373 *
374 * @push_seq is a value rather than a flag because that allows us to do an
375 * unlocked check of the sequence number for a match. Hence we can allows log
376 * forces to run racily and not issue pushes for the same sequence twice. If we
377 * get a race between multiple pushes for the same sequence they will block on
378 * the first one and then abort, hence avoiding needless pushes.
Dave Chinner71e330b2010-05-21 14:37:18 +1000379 */
Dave Chinnera44f13e2010-08-24 11:40:03 +1000380STATIC int
Dave Chinner71e330b2010-05-21 14:37:18 +1000381xlog_cil_push(
382 struct log *log,
Dave Chinnera44f13e2010-08-24 11:40:03 +1000383 xfs_lsn_t push_seq)
Dave Chinner71e330b2010-05-21 14:37:18 +1000384{
385 struct xfs_cil *cil = log->l_cilp;
386 struct xfs_log_vec *lv;
387 struct xfs_cil_ctx *ctx;
388 struct xfs_cil_ctx *new_ctx;
389 struct xlog_in_core *commit_iclog;
390 struct xlog_ticket *tic;
391 int num_lv;
392 int num_iovecs;
393 int len;
394 int error = 0;
395 struct xfs_trans_header thdr;
396 struct xfs_log_iovec lhdr;
397 struct xfs_log_vec lvhdr = { NULL };
398 xfs_lsn_t commit_lsn;
399
400 if (!cil)
401 return 0;
402
Dave Chinnera44f13e2010-08-24 11:40:03 +1000403 ASSERT(!push_seq || push_seq <= cil->xc_ctx->sequence);
404
Dave Chinner71e330b2010-05-21 14:37:18 +1000405 new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
406 new_ctx->ticket = xlog_cil_ticket_alloc(log);
407
Dave Chinner80168672010-09-24 18:13:44 +1000408 /*
409 * Lock out transaction commit, but don't block for background pushes
410 * unless we are well over the CIL space limit. See the definition of
411 * XLOG_CIL_HARD_SPACE_LIMIT() for the full explanation of the logic
412 * used here.
413 */
Dave Chinnerdf806152010-05-17 15:52:13 +1000414 if (!down_write_trylock(&cil->xc_ctx_lock)) {
Dave Chinner80168672010-09-24 18:13:44 +1000415 if (!push_seq &&
416 cil->xc_ctx->space_used < XLOG_CIL_HARD_SPACE_LIMIT(log))
Dave Chinnerdf806152010-05-17 15:52:13 +1000417 goto out_free_ticket;
418 down_write(&cil->xc_ctx_lock);
419 }
Dave Chinner71e330b2010-05-21 14:37:18 +1000420 ctx = cil->xc_ctx;
421
422 /* check if we've anything to push */
423 if (list_empty(&cil->xc_cil))
424 goto out_skip;
425
Dave Chinnerdf806152010-05-17 15:52:13 +1000426 /* check for spurious background flush */
Dave Chinnera44f13e2010-08-24 11:40:03 +1000427 if (!push_seq && cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log))
428 goto out_skip;
429
430 /* check for a previously pushed seqeunce */
Dave Chinner80168672010-09-24 18:13:44 +1000431 if (push_seq && push_seq < cil->xc_ctx->sequence)
Dave Chinnerdf806152010-05-17 15:52:13 +1000432 goto out_skip;
433
Dave Chinner71e330b2010-05-21 14:37:18 +1000434 /*
435 * pull all the log vectors off the items in the CIL, and
436 * remove the items from the CIL. We don't need the CIL lock
437 * here because it's only needed on the transaction commit
438 * side which is currently locked out by the flush lock.
439 */
440 lv = NULL;
441 num_lv = 0;
442 num_iovecs = 0;
443 len = 0;
444 while (!list_empty(&cil->xc_cil)) {
445 struct xfs_log_item *item;
446 int i;
447
448 item = list_first_entry(&cil->xc_cil,
449 struct xfs_log_item, li_cil);
450 list_del_init(&item->li_cil);
451 if (!ctx->lv_chain)
452 ctx->lv_chain = item->li_lv;
453 else
454 lv->lv_next = item->li_lv;
455 lv = item->li_lv;
456 item->li_lv = NULL;
457
458 num_lv++;
459 num_iovecs += lv->lv_niovecs;
460 for (i = 0; i < lv->lv_niovecs; i++)
461 len += lv->lv_iovecp[i].i_len;
462 }
463
464 /*
465 * initialise the new context and attach it to the CIL. Then attach
466 * the current context to the CIL committing lsit so it can be found
467 * during log forces to extract the commit lsn of the sequence that
468 * needs to be forced.
469 */
470 INIT_LIST_HEAD(&new_ctx->committing);
471 INIT_LIST_HEAD(&new_ctx->busy_extents);
472 new_ctx->sequence = ctx->sequence + 1;
473 new_ctx->cil = cil;
474 cil->xc_ctx = new_ctx;
475
476 /*
Dave Chinnera44f13e2010-08-24 11:40:03 +1000477 * mirror the new sequence into the cil structure so that we can do
478 * unlocked checks against the current sequence in log forces without
479 * risking deferencing a freed context pointer.
480 */
481 cil->xc_current_sequence = new_ctx->sequence;
482
483 /*
Dave Chinner71e330b2010-05-21 14:37:18 +1000484 * The switch is now done, so we can drop the context lock and move out
485 * of a shared context. We can't just go straight to the commit record,
486 * though - we need to synchronise with previous and future commits so
487 * that the commit records are correctly ordered in the log to ensure
488 * that we process items during log IO completion in the correct order.
489 *
490 * For example, if we get an EFI in one checkpoint and the EFD in the
491 * next (e.g. due to log forces), we do not want the checkpoint with
492 * the EFD to be committed before the checkpoint with the EFI. Hence
493 * we must strictly order the commit records of the checkpoints so
494 * that: a) the checkpoint callbacks are attached to the iclogs in the
495 * correct order; and b) the checkpoints are replayed in correct order
496 * in log recovery.
497 *
498 * Hence we need to add this context to the committing context list so
499 * that higher sequences will wait for us to write out a commit record
500 * before they do.
501 */
502 spin_lock(&cil->xc_cil_lock);
503 list_add(&ctx->committing, &cil->xc_committing);
504 spin_unlock(&cil->xc_cil_lock);
505 up_write(&cil->xc_ctx_lock);
506
507 /*
508 * Build a checkpoint transaction header and write it to the log to
509 * begin the transaction. We need to account for the space used by the
510 * transaction header here as it is not accounted for in xlog_write().
511 *
512 * The LSN we need to pass to the log items on transaction commit is
513 * the LSN reported by the first log vector write. If we use the commit
514 * record lsn then we can move the tail beyond the grant write head.
515 */
516 tic = ctx->ticket;
517 thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
518 thdr.th_type = XFS_TRANS_CHECKPOINT;
519 thdr.th_tid = tic->t_tid;
520 thdr.th_num_items = num_iovecs;
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000521 lhdr.i_addr = &thdr;
Dave Chinner71e330b2010-05-21 14:37:18 +1000522 lhdr.i_len = sizeof(xfs_trans_header_t);
523 lhdr.i_type = XLOG_REG_TYPE_TRANSHDR;
524 tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t);
525
526 lvhdr.lv_niovecs = 1;
527 lvhdr.lv_iovecp = &lhdr;
528 lvhdr.lv_next = ctx->lv_chain;
529
530 error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0);
531 if (error)
532 goto out_abort;
533
534 /*
535 * now that we've written the checkpoint into the log, strictly
536 * order the commit records so replay will get them in the right order.
537 */
538restart:
539 spin_lock(&cil->xc_cil_lock);
540 list_for_each_entry(new_ctx, &cil->xc_committing, committing) {
541 /*
542 * Higher sequences will wait for this one so skip them.
543 * Don't wait for own own sequence, either.
544 */
545 if (new_ctx->sequence >= ctx->sequence)
546 continue;
547 if (!new_ctx->commit_lsn) {
548 /*
549 * It is still being pushed! Wait for the push to
550 * complete, then start again from the beginning.
551 */
552 sv_wait(&cil->xc_commit_wait, 0, &cil->xc_cil_lock, 0);
553 goto restart;
554 }
555 }
556 spin_unlock(&cil->xc_cil_lock);
557
558 commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0);
559 if (error || commit_lsn == -1)
560 goto out_abort;
561
562 /* attach all the transactions w/ busy extents to iclog */
563 ctx->log_cb.cb_func = xlog_cil_committed;
564 ctx->log_cb.cb_arg = ctx;
565 error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb);
566 if (error)
567 goto out_abort;
568
569 /*
570 * now the checkpoint commit is complete and we've attached the
571 * callbacks to the iclog we can assign the commit LSN to the context
572 * and wake up anyone who is waiting for the commit to complete.
573 */
574 spin_lock(&cil->xc_cil_lock);
575 ctx->commit_lsn = commit_lsn;
576 sv_broadcast(&cil->xc_commit_wait);
577 spin_unlock(&cil->xc_cil_lock);
578
579 /* release the hounds! */
580 return xfs_log_release_iclog(log->l_mp, commit_iclog);
581
582out_skip:
583 up_write(&cil->xc_ctx_lock);
Dave Chinnerdf806152010-05-17 15:52:13 +1000584out_free_ticket:
Dave Chinner71e330b2010-05-21 14:37:18 +1000585 xfs_log_ticket_put(new_ctx->ticket);
586 kmem_free(new_ctx);
587 return 0;
588
589out_abort:
590 xlog_cil_committed(ctx, XFS_LI_ABORTED);
591 return XFS_ERROR(EIO);
592}
593
594/*
Dave Chinnera44f13e2010-08-24 11:40:03 +1000595 * Commit a transaction with the given vector to the Committed Item List.
596 *
597 * To do this, we need to format the item, pin it in memory if required and
598 * account for the space used by the transaction. Once we have done that we
599 * need to release the unused reservation for the transaction, attach the
600 * transaction to the checkpoint context so we carry the busy extents through
601 * to checkpoint completion, and then unlock all the items in the transaction.
602 *
603 * For more specific information about the order of operations in
604 * xfs_log_commit_cil() please refer to the comments in
605 * xfs_trans_commit_iclog().
606 *
607 * Called with the context lock already held in read mode to lock out
608 * background commit, returns without it held once background commits are
609 * allowed again.
610 */
611int
612xfs_log_commit_cil(
613 struct xfs_mount *mp,
614 struct xfs_trans *tp,
615 struct xfs_log_vec *log_vector,
616 xfs_lsn_t *commit_lsn,
617 int flags)
618{
619 struct log *log = mp->m_log;
620 int log_flags = 0;
621 int push = 0;
622
623 if (flags & XFS_TRANS_RELEASE_LOG_RES)
624 log_flags = XFS_LOG_REL_PERM_RESERV;
625
626 if (XLOG_FORCED_SHUTDOWN(log)) {
627 xlog_cil_free_logvec(log_vector);
628 return XFS_ERROR(EIO);
629 }
630
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000631 /*
632 * do all the hard work of formatting items (including memory
633 * allocation) outside the CIL context lock. This prevents stalling CIL
634 * pushes when we are low on memory and a transaction commit spends a
635 * lot of time in memory reclaim.
636 */
637 xlog_cil_format_items(log, log_vector);
638
Dave Chinnera44f13e2010-08-24 11:40:03 +1000639 /* lock out background commit */
640 down_read(&log->l_cilp->xc_ctx_lock);
Dave Chinner3b93c7a2010-08-24 11:45:53 +1000641 xlog_cil_insert_items(log, log_vector, tp->t_ticket, commit_lsn);
Dave Chinnera44f13e2010-08-24 11:40:03 +1000642
643 /* check we didn't blow the reservation */
644 if (tp->t_ticket->t_curr_res < 0)
645 xlog_print_tic_res(log->l_mp, tp->t_ticket);
646
647 /* attach the transaction to the CIL if it has any busy extents */
648 if (!list_empty(&tp->t_busy)) {
649 spin_lock(&log->l_cilp->xc_cil_lock);
650 list_splice_init(&tp->t_busy,
651 &log->l_cilp->xc_ctx->busy_extents);
652 spin_unlock(&log->l_cilp->xc_cil_lock);
653 }
654
655 tp->t_commit_lsn = *commit_lsn;
656 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
657 xfs_trans_unreserve_and_mod_sb(tp);
658
659 /*
660 * Once all the items of the transaction have been copied to the CIL,
661 * the items can be unlocked and freed.
662 *
663 * This needs to be done before we drop the CIL context lock because we
664 * have to update state in the log items and unlock them before they go
665 * to disk. If we don't, then the CIL checkpoint can race with us and
666 * we can run checkpoint completion before we've updated and unlocked
667 * the log items. This affects (at least) processing of stale buffers,
668 * inodes and EFIs.
669 */
670 xfs_trans_free_items(tp, *commit_lsn, 0);
671
672 /* check for background commit before unlock */
673 if (log->l_cilp->xc_ctx->space_used > XLOG_CIL_SPACE_LIMIT(log))
674 push = 1;
675
676 up_read(&log->l_cilp->xc_ctx_lock);
677
678 /*
679 * We need to push CIL every so often so we don't cache more than we
680 * can fit in the log. The limit really is that a checkpoint can't be
681 * more than half the log (the current checkpoint is not allowed to
682 * overwrite the previous checkpoint), but commit latency and memory
683 * usage limit this to a smaller size in most cases.
684 */
685 if (push)
686 xlog_cil_push(log, 0);
687 return 0;
688}
689
690/*
Dave Chinner71e330b2010-05-21 14:37:18 +1000691 * Conditionally push the CIL based on the sequence passed in.
692 *
693 * We only need to push if we haven't already pushed the sequence
694 * number given. Hence the only time we will trigger a push here is
695 * if the push sequence is the same as the current context.
696 *
697 * We return the current commit lsn to allow the callers to determine if a
698 * iclog flush is necessary following this call.
699 *
700 * XXX: Initially, just push the CIL unconditionally and return whatever
701 * commit lsn is there. It'll be empty, so this is broken for now.
702 */
703xfs_lsn_t
Dave Chinnera44f13e2010-08-24 11:40:03 +1000704xlog_cil_force_lsn(
Dave Chinner71e330b2010-05-21 14:37:18 +1000705 struct log *log,
Dave Chinnera44f13e2010-08-24 11:40:03 +1000706 xfs_lsn_t sequence)
Dave Chinner71e330b2010-05-21 14:37:18 +1000707{
708 struct xfs_cil *cil = log->l_cilp;
709 struct xfs_cil_ctx *ctx;
710 xfs_lsn_t commit_lsn = NULLCOMMITLSN;
711
Dave Chinnera44f13e2010-08-24 11:40:03 +1000712 ASSERT(sequence <= cil->xc_current_sequence);
Dave Chinner71e330b2010-05-21 14:37:18 +1000713
Dave Chinnera44f13e2010-08-24 11:40:03 +1000714 /*
715 * check to see if we need to force out the current context.
716 * xlog_cil_push() handles racing pushes for the same sequence,
717 * so no need to deal with it here.
718 */
719 if (sequence == cil->xc_current_sequence)
720 xlog_cil_push(log, sequence);
Dave Chinner71e330b2010-05-21 14:37:18 +1000721
722 /*
723 * See if we can find a previous sequence still committing.
Dave Chinner71e330b2010-05-21 14:37:18 +1000724 * We need to wait for all previous sequence commits to complete
725 * before allowing the force of push_seq to go ahead. Hence block
726 * on commits for those as well.
727 */
Dave Chinnera44f13e2010-08-24 11:40:03 +1000728restart:
Dave Chinner71e330b2010-05-21 14:37:18 +1000729 spin_lock(&cil->xc_cil_lock);
Dave Chinner71e330b2010-05-21 14:37:18 +1000730 list_for_each_entry(ctx, &cil->xc_committing, committing) {
Dave Chinnera44f13e2010-08-24 11:40:03 +1000731 if (ctx->sequence > sequence)
Dave Chinner71e330b2010-05-21 14:37:18 +1000732 continue;
733 if (!ctx->commit_lsn) {
734 /*
735 * It is still being pushed! Wait for the push to
736 * complete, then start again from the beginning.
737 */
738 sv_wait(&cil->xc_commit_wait, 0, &cil->xc_cil_lock, 0);
739 goto restart;
740 }
Dave Chinnera44f13e2010-08-24 11:40:03 +1000741 if (ctx->sequence != sequence)
Dave Chinner71e330b2010-05-21 14:37:18 +1000742 continue;
743 /* found it! */
744 commit_lsn = ctx->commit_lsn;
745 }
746 spin_unlock(&cil->xc_cil_lock);
747 return commit_lsn;
748}
Dave Chinnerccf7c232010-05-20 23:19:42 +1000749
750/*
751 * Check if the current log item was first committed in this sequence.
752 * We can't rely on just the log item being in the CIL, we have to check
753 * the recorded commit sequence number.
754 *
755 * Note: for this to be used in a non-racy manner, it has to be called with
756 * CIL flushing locked out. As a result, it should only be used during the
757 * transaction commit process when deciding what to format into the item.
758 */
759bool
760xfs_log_item_in_current_chkpt(
761 struct xfs_log_item *lip)
762{
763 struct xfs_cil_ctx *ctx;
764
765 if (!(lip->li_mountp->m_flags & XFS_MOUNT_DELAYLOG))
766 return false;
767 if (list_empty(&lip->li_cil))
768 return false;
769
770 ctx = lip->li_mountp->m_log->l_cilp->xc_ctx;
771
772 /*
773 * li_seq is written on the first commit of a log item to record the
774 * first checkpoint it is written to. Hence if it is different to the
775 * current sequence, we're in a new checkpoint.
776 */
777 if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0)
778 return false;
779 return true;
780}