]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/xfs/xfs_log_cil.c
xfs: reimplement fdatasync support
[mirror_ubuntu-artful-kernel.git] / fs / xfs / xfs_log_cil.c
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"
29 #include "xfs_mount.h"
30 #include "xfs_error.h"
31 #include "xfs_alloc.h"
32 #include "xfs_discard.h"
33
34 /*
35 * Perform initial CIL structure initialisation.
36 */
37 int
38 xlog_cil_init(
39 struct log *log)
40 {
41 struct xfs_cil *cil;
42 struct xfs_cil_ctx *ctx;
43
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);
58 init_waitqueue_head(&cil->xc_commit_wait);
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;
65 cil->xc_current_sequence = ctx->sequence;
66
67 cil->xc_log = log;
68 log->l_cilp = cil;
69 return 0;
70 }
71
72 void
73 xlog_cil_destroy(
74 struct log *log)
75 {
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 */
98 static struct xlog_ticket *
99 xlog_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 */
126 void
127 xlog_cil_init_post_recovery(
128 struct log *log)
129 {
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
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 */
162 static struct xfs_log_vec *
163 xlog_cil_prepare_log_vecs(
164 struct xfs_trans *tp)
165 {
166 struct xfs_log_item_desc *lidp;
167 struct xfs_log_vec *lv = NULL;
168 struct xfs_log_vec *ret_lv = NULL;
169
170
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;
179 void *ptr;
180 int index;
181 int len = 0;
182 uint niovecs;
183
184 /* 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 */
189 niovecs = IOP_SIZE(lidp->lid_item);
190 if (!niovecs)
191 continue;
192
193 new_lv = kmem_zalloc(sizeof(*new_lv) +
194 niovecs * sizeof(struct xfs_log_iovec),
195 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];
199 new_lv->lv_niovecs = niovecs;
200 new_lv->lv_item = lidp->lid_item;
201
202 /* build the vector array and calculate it's length */
203 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;
206
207 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;
211
212 for (index = 0; index < new_lv->lv_niovecs; index++) {
213 struct xfs_log_iovec *vec = &new_lv->lv_iovecp[index];
214
215 memcpy(ptr, vec->i_addr, vec->i_len);
216 vec->i_addr = ptr;
217 ptr += vec->i_len;
218 }
219 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;
226 }
227
228 return ret_lv;
229 }
230
231 /*
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 */
236 STATIC void
237 xfs_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
282 * as well. Remove the amount of space we added to the checkpoint ticket from
283 * the current transaction ticket so that the accounting works out correctly.
284 */
285 static void
286 xlog_cil_insert_items(
287 struct log *log,
288 struct xfs_log_vec *log_vector,
289 struct xlog_ticket *ticket)
290 {
291 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;
297
298 ASSERT(log_vector);
299
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 */
315 for (lv = log_vector; lv; lv = lv->lv_next)
316 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);
361 }
362
363 static void
364 xlog_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 /*
378 * 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 */
382 static void
383 xlog_cil_committed(
384 void *args,
385 int abort)
386 {
387 struct xfs_cil_ctx *ctx = args;
388 struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
389
390 xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
391 ctx->start_lsn, abort);
392
393 xfs_alloc_busy_sort(&ctx->busy_extents);
394 xfs_alloc_busy_clear(mp, &ctx->busy_extents,
395 (mp->m_flags & XFS_MOUNT_DISCARD) && !abort);
396
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);
402
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
410 kmem_free(ctx);
411 }
412
413 /*
414 * 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.
426 */
427 STATIC int
428 xlog_cil_push(
429 struct log *log,
430 xfs_lsn_t push_seq)
431 {
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
450 ASSERT(!push_seq || push_seq <= cil->xc_ctx->sequence);
451
452 new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
453 new_ctx->ticket = xlog_cil_ticket_alloc(log);
454
455 /*
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 */
461 if (!down_write_trylock(&cil->xc_ctx_lock)) {
462 if (!push_seq &&
463 cil->xc_ctx->space_used < XLOG_CIL_HARD_SPACE_LIMIT(log))
464 goto out_free_ticket;
465 down_write(&cil->xc_ctx_lock);
466 }
467 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
473 /* check for spurious background flush */
474 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 */
478 if (push_seq && push_seq < cil->xc_ctx->sequence)
479 goto out_skip;
480
481 /*
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 /*
524 * 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 /*
531 * 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;
568 lhdr.i_addr = &thdr;
569 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)
579 goto out_abort_free_ticket;
580
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 */
585 restart:
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 */
599 xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
600 goto restart;
601 }
602 }
603 spin_unlock(&cil->xc_cil_lock);
604
605 /* xfs_log_done always frees the ticket on error. */
606 commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0);
607 if (commit_lsn == -1)
608 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;
624 wake_up_all(&cil->xc_commit_wait);
625 spin_unlock(&cil->xc_cil_lock);
626
627 /* release the hounds! */
628 return xfs_log_release_iclog(log->l_mp, commit_iclog);
629
630 out_skip:
631 up_write(&cil->xc_ctx_lock);
632 out_free_ticket:
633 xfs_log_ticket_put(new_ctx->ticket);
634 kmem_free(new_ctx);
635 return 0;
636
637 out_abort_free_ticket:
638 xfs_log_ticket_put(tic);
639 out_abort:
640 xlog_cil_committed(ctx, XFS_LI_ABORTED);
641 return XFS_ERROR(EIO);
642 }
643
644 /*
645 * 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 */
661 int
662 xfs_log_commit_cil(
663 struct xfs_mount *mp,
664 struct xfs_trans *tp,
665 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;
671 struct xfs_log_vec *log_vector;
672
673 if (flags & XFS_TRANS_RELEASE_LOG_RES)
674 log_flags = XFS_LOG_REL_PERM_RESERV;
675
676 /*
677 * Do all the hard work of formatting items (including memory
678 * 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 */
682 log_vector = xlog_cil_prepare_log_vecs(tp);
683 if (!log_vector)
684 return ENOMEM;
685
686 /* lock out background commit */
687 down_read(&log->l_cilp->xc_ctx_lock);
688 if (commit_lsn)
689 *commit_lsn = log->l_cilp->xc_ctx->sequence;
690
691 xlog_cil_insert_items(log, log_vector, tp->t_ticket);
692
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);
737 return 0;
738 }
739
740 /*
741 * 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 */
753 xfs_lsn_t
754 xlog_cil_force_lsn(
755 struct log *log,
756 xfs_lsn_t sequence)
757 {
758 struct xfs_cil *cil = log->l_cilp;
759 struct xfs_cil_ctx *ctx;
760 xfs_lsn_t commit_lsn = NULLCOMMITLSN;
761
762 ASSERT(sequence <= cil->xc_current_sequence);
763
764 /*
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);
771
772 /*
773 * See if we can find a previous sequence still committing.
774 * 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 */
778 restart:
779 spin_lock(&cil->xc_cil_lock);
780 list_for_each_entry(ctx, &cil->xc_committing, committing) {
781 if (ctx->sequence > sequence)
782 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 */
788 xlog_wait(&cil->xc_commit_wait, &cil->xc_cil_lock);
789 goto restart;
790 }
791 if (ctx->sequence != sequence)
792 continue;
793 /* found it! */
794 commit_lsn = ctx->commit_lsn;
795 }
796 spin_unlock(&cil->xc_cil_lock);
797 return commit_lsn;
798 }
799
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 */
809 bool
810 xfs_log_item_in_current_chkpt(
811 struct xfs_log_item *lip)
812 {
813 struct xfs_cil_ctx *ctx;
814
815 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 }