]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xfs/xfs_log_cil.c
xfs: remove the deprecated nodelaylog option
[mirror_ubuntu-artful-kernel.git] / fs / xfs / xfs_log_cil.c
CommitLineData
71e330b5
DC
1/*
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"
71e330b5
DC
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_alloc.h"
e84661aa 32#include "xfs_discard.h"
71e330b5
DC
33
34/*
93b8a585 35 * Perform initial CIL structure initialisation.
71e330b5
DC
36 */
37int
38xlog_cil_init(
39 struct log *log)
40{
41 struct xfs_cil *cil;
42 struct xfs_cil_ctx *ctx;
43
71e330b5
DC
44 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);
eb40a875 58 init_waitqueue_head(&cil->xc_commit_wait);
71e330b5
DC
59
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;
a44f13ed 65 cil->xc_current_sequence = ctx->sequence;
71e330b5
DC
66
67 cil->xc_log = log;
68 log->l_cilp = cil;
69 return 0;
70}
71
72void
73xlog_cil_destroy(
74 struct log *log)
75{
71e330b5
DC
76 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{
71e330b5
DC
130 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
71e330b5
DC
136/*
137 * 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 */
162static void
163xlog_cil_format_items(
164 struct log *log,
3b93c7aa 165 struct xfs_log_vec *log_vector)
71e330b5
DC
166{
167 struct xfs_log_vec *lv;
168
71e330b5
DC
169 ASSERT(log_vector);
170 for (lv = log_vector; lv; lv = lv->lv_next) {
171 void *ptr;
172 int index;
173 int len = 0;
174
175 /* build the vector array and calculate it's length */
176 IOP_FORMAT(lv->lv_item, lv->lv_iovecp);
177 for (index = 0; index < lv->lv_niovecs; index++)
178 len += lv->lv_iovecp[index].i_len;
179
180 lv->lv_buf_len = len;
d1583a38 181 lv->lv_buf = kmem_alloc(lv->lv_buf_len, KM_SLEEP|KM_NOFS);
71e330b5
DC
182 ptr = lv->lv_buf;
183
184 for (index = 0; index < lv->lv_niovecs; index++) {
185 struct xfs_log_iovec *vec = &lv->lv_iovecp[index];
186
187 memcpy(ptr, vec->i_addr, vec->i_len);
188 vec->i_addr = ptr;
189 ptr += vec->i_len;
190 }
191 ASSERT(ptr == lv->lv_buf + lv->lv_buf_len);
3b93c7aa
DC
192 }
193}
71e330b5 194
d1583a38
DC
195/*
196 * Prepare the log item for insertion into the CIL. Calculate the difference in
197 * log space and vectors it will consume, and if it is a new item pin it as
198 * well.
199 */
200STATIC void
201xfs_cil_prepare_item(
202 struct log *log,
203 struct xfs_log_vec *lv,
204 int *len,
205 int *diff_iovecs)
206{
207 struct xfs_log_vec *old = lv->lv_item->li_lv;
208
209 if (old) {
210 /* existing lv on log item, space used is a delta */
211 ASSERT(!list_empty(&lv->lv_item->li_cil));
212 ASSERT(old->lv_buf && old->lv_buf_len && old->lv_niovecs);
213
214 *len += lv->lv_buf_len - old->lv_buf_len;
215 *diff_iovecs += lv->lv_niovecs - old->lv_niovecs;
216 kmem_free(old->lv_buf);
217 kmem_free(old);
218 } else {
219 /* new lv, must pin the log item */
220 ASSERT(!lv->lv_item->li_lv);
221 ASSERT(list_empty(&lv->lv_item->li_cil));
222
223 *len += lv->lv_buf_len;
224 *diff_iovecs += lv->lv_niovecs;
225 IOP_PIN(lv->lv_item);
226
227 }
228
229 /* attach new log vector to log item */
230 lv->lv_item->li_lv = lv;
231
232 /*
233 * If this is the first time the item is being committed to the
234 * CIL, store the sequence number on the log item so we can
235 * tell in future commits whether this is the first checkpoint
236 * the item is being committed into.
237 */
238 if (!lv->lv_item->li_seq)
239 lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
240}
241
242/*
243 * Insert the log items into the CIL and calculate the difference in space
244 * consumed by the item. Add the space to the checkpoint ticket and calculate
245 * if the change requires additional log metadata. If it does, take that space
246 * as well. Remove the amount of space we addded to the checkpoint ticket from
247 * the current transaction ticket so that the accounting works out correctly.
248 */
3b93c7aa
DC
249static void
250xlog_cil_insert_items(
251 struct log *log,
252 struct xfs_log_vec *log_vector,
d1583a38 253 struct xlog_ticket *ticket)
3b93c7aa 254{
d1583a38
DC
255 struct xfs_cil *cil = log->l_cilp;
256 struct xfs_cil_ctx *ctx = cil->xc_ctx;
257 struct xfs_log_vec *lv;
258 int len = 0;
259 int diff_iovecs = 0;
260 int iclog_space;
3b93c7aa
DC
261
262 ASSERT(log_vector);
d1583a38
DC
263
264 /*
265 * Do all the accounting aggregation and switching of log vectors
266 * around in a separate loop to the insertion of items into the CIL.
267 * Then we can do a separate loop to update the CIL within a single
268 * lock/unlock pair. This reduces the number of round trips on the CIL
269 * lock from O(nr_logvectors) to O(1) and greatly reduces the overall
270 * hold time for the transaction commit.
271 *
272 * If this is the first time the item is being placed into the CIL in
273 * this context, pin it so it can't be written to disk until the CIL is
274 * flushed to the iclog and the iclog written to disk.
275 *
276 * We can do this safely because the context can't checkpoint until we
277 * are done so it doesn't matter exactly how we update the CIL.
278 */
279 for (lv = log_vector; lv; lv = lv->lv_next)
280 xfs_cil_prepare_item(log, lv, &len, &diff_iovecs);
281
282 /* account for space used by new iovec headers */
283 len += diff_iovecs * sizeof(xlog_op_header_t);
284
285 spin_lock(&cil->xc_cil_lock);
286
287 /* move the items to the tail of the CIL */
3b93c7aa 288 for (lv = log_vector; lv; lv = lv->lv_next)
d1583a38
DC
289 list_move_tail(&lv->lv_item->li_cil, &cil->xc_cil);
290
291 ctx->nvecs += diff_iovecs;
292
293 /*
294 * Now transfer enough transaction reservation to the context ticket
295 * for the checkpoint. The context ticket is special - the unit
296 * reservation has to grow as well as the current reservation as we
297 * steal from tickets so we can correctly determine the space used
298 * during the transaction commit.
299 */
300 if (ctx->ticket->t_curr_res == 0) {
301 /* first commit in checkpoint, steal the header reservation */
302 ASSERT(ticket->t_curr_res >= ctx->ticket->t_unit_res + len);
303 ctx->ticket->t_curr_res = ctx->ticket->t_unit_res;
304 ticket->t_curr_res -= ctx->ticket->t_unit_res;
305 }
306
307 /* do we need space for more log record headers? */
308 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
309 if (len > 0 && (ctx->space_used / iclog_space !=
310 (ctx->space_used + len) / iclog_space)) {
311 int hdrs;
312
313 hdrs = (len + iclog_space - 1) / iclog_space;
314 /* need to take into account split region headers, too */
315 hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
316 ctx->ticket->t_unit_res += hdrs;
317 ctx->ticket->t_curr_res += hdrs;
318 ticket->t_curr_res -= hdrs;
319 ASSERT(ticket->t_curr_res >= len);
320 }
321 ticket->t_curr_res -= len;
322 ctx->space_used += len;
323
324 spin_unlock(&cil->xc_cil_lock);
71e330b5
DC
325}
326
327static void
328xlog_cil_free_logvec(
329 struct xfs_log_vec *log_vector)
330{
331 struct xfs_log_vec *lv;
332
333 for (lv = log_vector; lv; ) {
334 struct xfs_log_vec *next = lv->lv_next;
335 kmem_free(lv->lv_buf);
336 kmem_free(lv);
337 lv = next;
338 }
339}
340
71e330b5
DC
341/*
342 * Mark all items committed and clear busy extents. We free the log vector
343 * chains in a separate pass so that we unpin the log items as quickly as
344 * possible.
345 */
346static void
347xlog_cil_committed(
348 void *args,
349 int abort)
350{
351 struct xfs_cil_ctx *ctx = args;
e84661aa 352 struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
71e330b5 353
0e57f6a3
DC
354 xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
355 ctx->start_lsn, abort);
71e330b5 356
8a072a4d 357 xfs_alloc_busy_sort(&ctx->busy_extents);
e84661aa
CH
358 xfs_alloc_busy_clear(mp, &ctx->busy_extents,
359 (mp->m_flags & XFS_MOUNT_DISCARD) && !abort);
71e330b5
DC
360
361 spin_lock(&ctx->cil->xc_cil_lock);
362 list_del(&ctx->committing);
363 spin_unlock(&ctx->cil->xc_cil_lock);
364
365 xlog_cil_free_logvec(ctx->lv_chain);
e84661aa
CH
366
367 if (!list_empty(&ctx->busy_extents)) {
368 ASSERT(mp->m_flags & XFS_MOUNT_DISCARD);
369
370 xfs_discard_extents(mp, &ctx->busy_extents);
371 xfs_alloc_busy_clear(mp, &ctx->busy_extents, false);
372 }
373
71e330b5
DC
374 kmem_free(ctx);
375}
376
377/*
a44f13ed
DC
378 * Push the Committed Item List to the log. If @push_seq flag is zero, then it
379 * is a background flush and so we can chose to ignore it. Otherwise, if the
380 * current sequence is the same as @push_seq we need to do a flush. If
381 * @push_seq is less than the current sequence, then it has already been
382 * flushed and we don't need to do anything - the caller will wait for it to
383 * complete if necessary.
384 *
385 * @push_seq is a value rather than a flag because that allows us to do an
386 * unlocked check of the sequence number for a match. Hence we can allows log
387 * forces to run racily and not issue pushes for the same sequence twice. If we
388 * get a race between multiple pushes for the same sequence they will block on
389 * the first one and then abort, hence avoiding needless pushes.
71e330b5 390 */
a44f13ed 391STATIC int
71e330b5
DC
392xlog_cil_push(
393 struct log *log,
a44f13ed 394 xfs_lsn_t push_seq)
71e330b5
DC
395{
396 struct xfs_cil *cil = log->l_cilp;
397 struct xfs_log_vec *lv;
398 struct xfs_cil_ctx *ctx;
399 struct xfs_cil_ctx *new_ctx;
400 struct xlog_in_core *commit_iclog;
401 struct xlog_ticket *tic;
402 int num_lv;
403 int num_iovecs;
404 int len;
405 int error = 0;
406 struct xfs_trans_header thdr;
407 struct xfs_log_iovec lhdr;
408 struct xfs_log_vec lvhdr = { NULL };
409 xfs_lsn_t commit_lsn;
410
411 if (!cil)
412 return 0;
413
a44f13ed
DC
414 ASSERT(!push_seq || push_seq <= cil->xc_ctx->sequence);
415
71e330b5
DC
416 new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
417 new_ctx->ticket = xlog_cil_ticket_alloc(log);
418
80168676
DC
419 /*
420 * Lock out transaction commit, but don't block for background pushes
421 * unless we are well over the CIL space limit. See the definition of
422 * XLOG_CIL_HARD_SPACE_LIMIT() for the full explanation of the logic
423 * used here.
424 */
df806158 425 if (!down_write_trylock(&cil->xc_ctx_lock)) {
80168676
DC
426 if (!push_seq &&
427 cil->xc_ctx->space_used < XLOG_CIL_HARD_SPACE_LIMIT(log))
df806158
DC
428 goto out_free_ticket;
429 down_write(&cil->xc_ctx_lock);
430 }
71e330b5
DC
431 ctx = cil->xc_ctx;
432
433 /* check if we've anything to push */
434 if (list_empty(&cil->xc_cil))
435 goto out_skip;
436
df806158 437 /* check for spurious background flush */
a44f13ed
DC
438 if (!push_seq && cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log))
439 goto out_skip;
440
441 /* check for a previously pushed seqeunce */
80168676 442 if (push_seq && push_seq < cil->xc_ctx->sequence)
df806158
DC
443 goto out_skip;
444
71e330b5
DC
445 /*
446 * pull all the log vectors off the items in the CIL, and
447 * remove the items from the CIL. We don't need the CIL lock
448 * here because it's only needed on the transaction commit
449 * side which is currently locked out by the flush lock.
450 */
451 lv = NULL;
452 num_lv = 0;
453 num_iovecs = 0;
454 len = 0;
455 while (!list_empty(&cil->xc_cil)) {
456 struct xfs_log_item *item;
457 int i;
458
459 item = list_first_entry(&cil->xc_cil,
460 struct xfs_log_item, li_cil);
461 list_del_init(&item->li_cil);
462 if (!ctx->lv_chain)
463 ctx->lv_chain = item->li_lv;
464 else
465 lv->lv_next = item->li_lv;
466 lv = item->li_lv;
467 item->li_lv = NULL;
468
469 num_lv++;
470 num_iovecs += lv->lv_niovecs;
471 for (i = 0; i < lv->lv_niovecs; i++)
472 len += lv->lv_iovecp[i].i_len;
473 }
474
475 /*
476 * initialise the new context and attach it to the CIL. Then attach
477 * the current context to the CIL committing lsit so it can be found
478 * during log forces to extract the commit lsn of the sequence that
479 * needs to be forced.
480 */
481 INIT_LIST_HEAD(&new_ctx->committing);
482 INIT_LIST_HEAD(&new_ctx->busy_extents);
483 new_ctx->sequence = ctx->sequence + 1;
484 new_ctx->cil = cil;
485 cil->xc_ctx = new_ctx;
486
a44f13ed
DC
487 /*
488 * mirror the new sequence into the cil structure so that we can do
489 * unlocked checks against the current sequence in log forces without
490 * risking deferencing a freed context pointer.
491 */
492 cil->xc_current_sequence = new_ctx->sequence;
493
71e330b5
DC
494 /*
495 * The switch is now done, so we can drop the context lock and move out
496 * of a shared context. We can't just go straight to the commit record,
497 * though - we need to synchronise with previous and future commits so
498 * that the commit records are correctly ordered in the log to ensure
499 * that we process items during log IO completion in the correct order.
500 *
501 * For example, if we get an EFI in one checkpoint and the EFD in the
502 * next (e.g. due to log forces), we do not want the checkpoint with
503 * the EFD to be committed before the checkpoint with the EFI. Hence
504 * we must strictly order the commit records of the checkpoints so
505 * that: a) the checkpoint callbacks are attached to the iclogs in the
506 * correct order; and b) the checkpoints are replayed in correct order
507 * in log recovery.
508 *
509 * Hence we need to add this context to the committing context list so
510 * that higher sequences will wait for us to write out a commit record
511 * before they do.
512 */
513 spin_lock(&cil->xc_cil_lock);
514 list_add(&ctx->committing, &cil->xc_committing);
515 spin_unlock(&cil->xc_cil_lock);
516 up_write(&cil->xc_ctx_lock);
517
518 /*
519 * Build a checkpoint transaction header and write it to the log to
520 * begin the transaction. We need to account for the space used by the
521 * transaction header here as it is not accounted for in xlog_write().
522 *
523 * The LSN we need to pass to the log items on transaction commit is
524 * the LSN reported by the first log vector write. If we use the commit
525 * record lsn then we can move the tail beyond the grant write head.
526 */
527 tic = ctx->ticket;
528 thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
529 thdr.th_type = XFS_TRANS_CHECKPOINT;
530 thdr.th_tid = tic->t_tid;
531 thdr.th_num_items = num_iovecs;
4e0d5f92 532 lhdr.i_addr = &thdr;
71e330b5
DC
533 lhdr.i_len = sizeof(xfs_trans_header_t);
534 lhdr.i_type = XLOG_REG_TYPE_TRANSHDR;
535 tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t);
536
537 lvhdr.lv_niovecs = 1;
538 lvhdr.lv_iovecp = &lhdr;
539 lvhdr.lv_next = ctx->lv_chain;
540
541 error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0);
542 if (error)
7db37c5e 543 goto out_abort_free_ticket;
71e330b5
DC
544
545 /*
546 * now that we've written the checkpoint into the log, strictly
547 * order the commit records so replay will get them in the right order.
548 */
549restart:
550 spin_lock(&cil->xc_cil_lock);
551 list_for_each_entry(new_ctx, &cil->xc_committing, committing) {
552 /*
553 * Higher sequences will wait for this one so skip them.
554 * Don't wait for own own sequence, either.
555 */
556 if (new_ctx->sequence >= ctx->sequence)
557 continue;
558 if (!new_ctx->commit_lsn) {
559 /*
560 * It is still being pushed! Wait for the push to
561 * complete, then start again from the beginning.
562 */
eb40a875 563 xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
71e330b5
DC
564 goto restart;
565 }
566 }
567 spin_unlock(&cil->xc_cil_lock);
568
7db37c5e 569 /* xfs_log_done always frees the ticket on error. */
71e330b5 570 commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0);
7db37c5e 571 if (commit_lsn == -1)
71e330b5
DC
572 goto out_abort;
573
574 /* attach all the transactions w/ busy extents to iclog */
575 ctx->log_cb.cb_func = xlog_cil_committed;
576 ctx->log_cb.cb_arg = ctx;
577 error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb);
578 if (error)
579 goto out_abort;
580
581 /*
582 * now the checkpoint commit is complete and we've attached the
583 * callbacks to the iclog we can assign the commit LSN to the context
584 * and wake up anyone who is waiting for the commit to complete.
585 */
586 spin_lock(&cil->xc_cil_lock);
587 ctx->commit_lsn = commit_lsn;
eb40a875 588 wake_up_all(&cil->xc_commit_wait);
71e330b5
DC
589 spin_unlock(&cil->xc_cil_lock);
590
591 /* release the hounds! */
592 return xfs_log_release_iclog(log->l_mp, commit_iclog);
593
594out_skip:
595 up_write(&cil->xc_ctx_lock);
df806158 596out_free_ticket:
71e330b5
DC
597 xfs_log_ticket_put(new_ctx->ticket);
598 kmem_free(new_ctx);
599 return 0;
600
7db37c5e
DC
601out_abort_free_ticket:
602 xfs_log_ticket_put(tic);
71e330b5
DC
603out_abort:
604 xlog_cil_committed(ctx, XFS_LI_ABORTED);
605 return XFS_ERROR(EIO);
606}
607
a44f13ed
DC
608/*
609 * Commit a transaction with the given vector to the Committed Item List.
610 *
611 * To do this, we need to format the item, pin it in memory if required and
612 * account for the space used by the transaction. Once we have done that we
613 * need to release the unused reservation for the transaction, attach the
614 * transaction to the checkpoint context so we carry the busy extents through
615 * to checkpoint completion, and then unlock all the items in the transaction.
616 *
617 * For more specific information about the order of operations in
618 * xfs_log_commit_cil() please refer to the comments in
619 * xfs_trans_commit_iclog().
620 *
621 * Called with the context lock already held in read mode to lock out
622 * background commit, returns without it held once background commits are
623 * allowed again.
624 */
c6f990d1 625void
a44f13ed
DC
626xfs_log_commit_cil(
627 struct xfs_mount *mp,
628 struct xfs_trans *tp,
629 struct xfs_log_vec *log_vector,
630 xfs_lsn_t *commit_lsn,
631 int flags)
632{
633 struct log *log = mp->m_log;
634 int log_flags = 0;
635 int push = 0;
636
637 if (flags & XFS_TRANS_RELEASE_LOG_RES)
638 log_flags = XFS_LOG_REL_PERM_RESERV;
639
3b93c7aa
DC
640 /*
641 * do all the hard work of formatting items (including memory
642 * allocation) outside the CIL context lock. This prevents stalling CIL
643 * pushes when we are low on memory and a transaction commit spends a
644 * lot of time in memory reclaim.
645 */
646 xlog_cil_format_items(log, log_vector);
647
a44f13ed
DC
648 /* lock out background commit */
649 down_read(&log->l_cilp->xc_ctx_lock);
d1583a38
DC
650 if (commit_lsn)
651 *commit_lsn = log->l_cilp->xc_ctx->sequence;
652
653 xlog_cil_insert_items(log, log_vector, tp->t_ticket);
a44f13ed
DC
654
655 /* check we didn't blow the reservation */
656 if (tp->t_ticket->t_curr_res < 0)
657 xlog_print_tic_res(log->l_mp, tp->t_ticket);
658
659 /* attach the transaction to the CIL if it has any busy extents */
660 if (!list_empty(&tp->t_busy)) {
661 spin_lock(&log->l_cilp->xc_cil_lock);
662 list_splice_init(&tp->t_busy,
663 &log->l_cilp->xc_ctx->busy_extents);
664 spin_unlock(&log->l_cilp->xc_cil_lock);
665 }
666
667 tp->t_commit_lsn = *commit_lsn;
668 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
669 xfs_trans_unreserve_and_mod_sb(tp);
670
671 /*
672 * Once all the items of the transaction have been copied to the CIL,
673 * the items can be unlocked and freed.
674 *
675 * This needs to be done before we drop the CIL context lock because we
676 * have to update state in the log items and unlock them before they go
677 * to disk. If we don't, then the CIL checkpoint can race with us and
678 * we can run checkpoint completion before we've updated and unlocked
679 * the log items. This affects (at least) processing of stale buffers,
680 * inodes and EFIs.
681 */
682 xfs_trans_free_items(tp, *commit_lsn, 0);
683
684 /* check for background commit before unlock */
685 if (log->l_cilp->xc_ctx->space_used > XLOG_CIL_SPACE_LIMIT(log))
686 push = 1;
687
688 up_read(&log->l_cilp->xc_ctx_lock);
689
690 /*
691 * We need to push CIL every so often so we don't cache more than we
692 * can fit in the log. The limit really is that a checkpoint can't be
693 * more than half the log (the current checkpoint is not allowed to
694 * overwrite the previous checkpoint), but commit latency and memory
695 * usage limit this to a smaller size in most cases.
696 */
697 if (push)
698 xlog_cil_push(log, 0);
a44f13ed
DC
699}
700
71e330b5
DC
701/*
702 * Conditionally push the CIL based on the sequence passed in.
703 *
704 * We only need to push if we haven't already pushed the sequence
705 * number given. Hence the only time we will trigger a push here is
706 * if the push sequence is the same as the current context.
707 *
708 * We return the current commit lsn to allow the callers to determine if a
709 * iclog flush is necessary following this call.
710 *
711 * XXX: Initially, just push the CIL unconditionally and return whatever
712 * commit lsn is there. It'll be empty, so this is broken for now.
713 */
714xfs_lsn_t
a44f13ed 715xlog_cil_force_lsn(
71e330b5 716 struct log *log,
a44f13ed 717 xfs_lsn_t sequence)
71e330b5
DC
718{
719 struct xfs_cil *cil = log->l_cilp;
720 struct xfs_cil_ctx *ctx;
721 xfs_lsn_t commit_lsn = NULLCOMMITLSN;
722
a44f13ed
DC
723 ASSERT(sequence <= cil->xc_current_sequence);
724
725 /*
726 * check to see if we need to force out the current context.
727 * xlog_cil_push() handles racing pushes for the same sequence,
728 * so no need to deal with it here.
729 */
730 if (sequence == cil->xc_current_sequence)
731 xlog_cil_push(log, sequence);
71e330b5
DC
732
733 /*
734 * See if we can find a previous sequence still committing.
71e330b5
DC
735 * We need to wait for all previous sequence commits to complete
736 * before allowing the force of push_seq to go ahead. Hence block
737 * on commits for those as well.
738 */
a44f13ed 739restart:
71e330b5 740 spin_lock(&cil->xc_cil_lock);
71e330b5 741 list_for_each_entry(ctx, &cil->xc_committing, committing) {
a44f13ed 742 if (ctx->sequence > sequence)
71e330b5
DC
743 continue;
744 if (!ctx->commit_lsn) {
745 /*
746 * It is still being pushed! Wait for the push to
747 * complete, then start again from the beginning.
748 */
eb40a875 749 xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
71e330b5
DC
750 goto restart;
751 }
a44f13ed 752 if (ctx->sequence != sequence)
71e330b5
DC
753 continue;
754 /* found it! */
755 commit_lsn = ctx->commit_lsn;
756 }
757 spin_unlock(&cil->xc_cil_lock);
758 return commit_lsn;
759}
ccf7c23f
DC
760
761/*
762 * Check if the current log item was first committed in this sequence.
763 * We can't rely on just the log item being in the CIL, we have to check
764 * the recorded commit sequence number.
765 *
766 * Note: for this to be used in a non-racy manner, it has to be called with
767 * CIL flushing locked out. As a result, it should only be used during the
768 * transaction commit process when deciding what to format into the item.
769 */
770bool
771xfs_log_item_in_current_chkpt(
772 struct xfs_log_item *lip)
773{
774 struct xfs_cil_ctx *ctx;
775
ccf7c23f
DC
776 if (list_empty(&lip->li_cil))
777 return false;
778
779 ctx = lip->li_mountp->m_log->l_cilp->xc_ctx;
780
781 /*
782 * li_seq is written on the first commit of a log item to record the
783 * first checkpoint it is written to. Hence if it is different to the
784 * current sequence, we're in a new checkpoint.
785 */
786 if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0)
787 return false;
788 return true;
789}