1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_log_format.h"
21#include "xfs_shared.h"
22#include "xfs_trans_resv.h"
23#include "xfs_sb.h"
24#include "xfs_ag.h"
25#include "xfs_mount.h"
26#include "xfs_error.h"
27#include "xfs_alloc.h"
28#include "xfs_extent_busy.h"
29#include "xfs_discard.h"
30#include "xfs_trans.h"
31#include "xfs_trans_priv.h"
32#include "xfs_log.h"
33#include "xfs_log_priv.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47static struct xlog_ticket *
48xlog_cil_ticket_alloc(
49 struct xlog *log)
50{
51 struct xlog_ticket *tic;
52
53 tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0,
54 KM_SLEEP|KM_NOFS);
55 tic->t_trans_type = XFS_TRANS_CHECKPOINT;
56
57
58
59
60
61 tic->t_curr_res = 0;
62 return tic;
63}
64
65
66
67
68
69
70
71
72
73
74
75void
76xlog_cil_init_post_recovery(
77 struct xlog *log)
78{
79 log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
80 log->l_cilp->xc_ctx->sequence = 1;
81}
82
83
84
85
86
87
88STATIC void
89xfs_cil_prepare_item(
90 struct xlog *log,
91 struct xfs_log_vec *lv,
92 struct xfs_log_vec *old_lv,
93 int *diff_len,
94 int *diff_iovecs)
95{
96
97 if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED) {
98 *diff_len += lv->lv_bytes;
99 *diff_iovecs += lv->lv_niovecs;
100 }
101
102
103
104
105
106
107 if (!old_lv)
108 lv->lv_item->li_ops->iop_pin(lv->lv_item);
109 else if (old_lv != lv) {
110 ASSERT(lv->lv_buf_len != XFS_LOG_VEC_ORDERED);
111
112 *diff_len -= old_lv->lv_bytes;
113 *diff_iovecs -= old_lv->lv_niovecs;
114 kmem_free(old_lv);
115 }
116
117
118 lv->lv_item->li_lv = lv;
119
120
121
122
123
124
125
126 if (!lv->lv_item->li_seq)
127 lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156static void
157xlog_cil_insert_format_items(
158 struct xlog *log,
159 struct xfs_trans *tp,
160 int *diff_len,
161 int *diff_iovecs)
162{
163 struct xfs_log_item_desc *lidp;
164
165
166
167 if (list_empty(&tp->t_items)) {
168 ASSERT(0);
169 return;
170 }
171
172 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
173 struct xfs_log_item *lip = lidp->lid_item;
174 struct xfs_log_vec *lv;
175 struct xfs_log_vec *old_lv;
176 int niovecs = 0;
177 int nbytes = 0;
178 int buf_size;
179 bool ordered = false;
180
181
182 if (!(lidp->lid_flags & XFS_LID_DIRTY))
183 continue;
184
185
186 lip->li_ops->iop_size(lip, &niovecs, &nbytes);
187
188
189 if (!niovecs)
190 continue;
191
192
193
194
195
196
197 if (niovecs == XFS_LOG_VEC_ORDERED) {
198 ordered = true;
199 niovecs = 0;
200 nbytes = 0;
201 }
202
203
204
205
206
207
208
209
210 nbytes += niovecs * sizeof(uint64_t);
211 nbytes = round_up(nbytes, sizeof(uint64_t));
212
213
214 old_lv = lip->li_lv;
215
216
217
218
219
220
221 buf_size = nbytes +
222 round_up((sizeof(struct xfs_log_vec) +
223 niovecs * sizeof(struct xfs_log_iovec)),
224 sizeof(uint64_t));
225
226
227 if (lip->li_lv && buf_size <= lip->li_lv->lv_size) {
228
229 lv = lip->li_lv;
230 lv->lv_next = NULL;
231
232 if (ordered)
233 goto insert;
234
235
236
237
238
239 *diff_iovecs -= lv->lv_niovecs;
240 *diff_len -= lv->lv_bytes;
241 } else {
242
243 lv = kmem_zalloc(buf_size, KM_SLEEP|KM_NOFS);
244 lv->lv_item = lip;
245 lv->lv_size = buf_size;
246 if (ordered) {
247
248 ASSERT(lip->li_lv == NULL);
249 lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
250 goto insert;
251 }
252 lv->lv_iovecp = (struct xfs_log_iovec *)&lv[1];
253 }
254
255
256 lv->lv_niovecs = niovecs;
257
258
259 lv->lv_buf_len = 0;
260 lv->lv_bytes = 0;
261 lv->lv_buf = (char *)lv + buf_size - nbytes;
262 ASSERT(IS_ALIGNED((unsigned long)lv->lv_buf, sizeof(uint64_t)));
263
264 lip->li_ops->iop_format(lip, lv);
265insert:
266 ASSERT(lv->lv_buf_len <= nbytes);
267 xfs_cil_prepare_item(log, lv, old_lv, diff_len, diff_iovecs);
268 }
269}
270
271
272
273
274
275
276
277
278static void
279xlog_cil_insert_items(
280 struct xlog *log,
281 struct xfs_trans *tp)
282{
283 struct xfs_cil *cil = log->l_cilp;
284 struct xfs_cil_ctx *ctx = cil->xc_ctx;
285 struct xfs_log_item_desc *lidp;
286 int len = 0;
287 int diff_iovecs = 0;
288 int iclog_space;
289
290 ASSERT(tp);
291
292
293
294
295
296 xlog_cil_insert_format_items(log, tp, &len, &diff_iovecs);
297
298
299
300
301
302
303 spin_lock(&cil->xc_cil_lock);
304 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
305 struct xfs_log_item *lip = lidp->lid_item;
306
307
308 if (!(lidp->lid_flags & XFS_LID_DIRTY))
309 continue;
310
311 list_move_tail(&lip->li_cil, &cil->xc_cil);
312 }
313
314
315 len += diff_iovecs * sizeof(xlog_op_header_t);
316 ctx->nvecs += diff_iovecs;
317
318
319 if (!list_empty(&tp->t_busy))
320 list_splice_init(&tp->t_busy, &ctx->busy_extents);
321
322
323
324
325
326
327
328
329 if (ctx->ticket->t_curr_res == 0) {
330 ctx->ticket->t_curr_res = ctx->ticket->t_unit_res;
331 tp->t_ticket->t_curr_res -= ctx->ticket->t_unit_res;
332 }
333
334
335 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
336 if (len > 0 && (ctx->space_used / iclog_space !=
337 (ctx->space_used + len) / iclog_space)) {
338 int hdrs;
339
340 hdrs = (len + iclog_space - 1) / iclog_space;
341
342 hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
343 ctx->ticket->t_unit_res += hdrs;
344 ctx->ticket->t_curr_res += hdrs;
345 tp->t_ticket->t_curr_res -= hdrs;
346 ASSERT(tp->t_ticket->t_curr_res >= len);
347 }
348 tp->t_ticket->t_curr_res -= len;
349 ctx->space_used += len;
350
351 spin_unlock(&cil->xc_cil_lock);
352}
353
354static void
355xlog_cil_free_logvec(
356 struct xfs_log_vec *log_vector)
357{
358 struct xfs_log_vec *lv;
359
360 for (lv = log_vector; lv; ) {
361 struct xfs_log_vec *next = lv->lv_next;
362 kmem_free(lv);
363 lv = next;
364 }
365}
366
367
368
369
370
371
372static void
373xlog_cil_committed(
374 void *args,
375 int abort)
376{
377 struct xfs_cil_ctx *ctx = args;
378 struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
379
380 xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
381 ctx->start_lsn, abort);
382
383 xfs_extent_busy_sort(&ctx->busy_extents);
384 xfs_extent_busy_clear(mp, &ctx->busy_extents,
385 (mp->m_flags & XFS_MOUNT_DISCARD) && !abort);
386
387
388
389
390
391
392
393 spin_lock(&ctx->cil->xc_push_lock);
394 if (abort)
395 wake_up_all(&ctx->cil->xc_commit_wait);
396 list_del(&ctx->committing);
397 spin_unlock(&ctx->cil->xc_push_lock);
398
399 xlog_cil_free_logvec(ctx->lv_chain);
400
401 if (!list_empty(&ctx->busy_extents)) {
402 ASSERT(mp->m_flags & XFS_MOUNT_DISCARD);
403
404 xfs_discard_extents(mp, &ctx->busy_extents);
405 xfs_extent_busy_clear(mp, &ctx->busy_extents, false);
406 }
407
408 kmem_free(ctx);
409}
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425STATIC int
426xlog_cil_push(
427 struct xlog *log)
428{
429 struct xfs_cil *cil = log->l_cilp;
430 struct xfs_log_vec *lv;
431 struct xfs_cil_ctx *ctx;
432 struct xfs_cil_ctx *new_ctx;
433 struct xlog_in_core *commit_iclog;
434 struct xlog_ticket *tic;
435 int num_iovecs;
436 int error = 0;
437 struct xfs_trans_header thdr;
438 struct xfs_log_iovec lhdr;
439 struct xfs_log_vec lvhdr = { NULL };
440 xfs_lsn_t commit_lsn;
441 xfs_lsn_t push_seq;
442
443 if (!cil)
444 return 0;
445
446 new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS);
447 new_ctx->ticket = xlog_cil_ticket_alloc(log);
448
449 down_write(&cil->xc_ctx_lock);
450 ctx = cil->xc_ctx;
451
452 spin_lock(&cil->xc_push_lock);
453 push_seq = cil->xc_push_seq;
454 ASSERT(push_seq <= ctx->sequence);
455
456
457
458
459
460
461 if (list_empty(&cil->xc_cil)) {
462 cil->xc_push_seq = 0;
463 spin_unlock(&cil->xc_push_lock);
464 goto out_skip;
465 }
466 spin_unlock(&cil->xc_push_lock);
467
468
469
470 if (push_seq < cil->xc_ctx->sequence)
471 goto out_skip;
472
473
474
475
476
477
478
479 lv = NULL;
480 num_iovecs = 0;
481 while (!list_empty(&cil->xc_cil)) {
482 struct xfs_log_item *item;
483
484 item = list_first_entry(&cil->xc_cil,
485 struct xfs_log_item, li_cil);
486 list_del_init(&item->li_cil);
487 if (!ctx->lv_chain)
488 ctx->lv_chain = item->li_lv;
489 else
490 lv->lv_next = item->li_lv;
491 lv = item->li_lv;
492 item->li_lv = NULL;
493 num_iovecs += lv->lv_niovecs;
494 }
495
496
497
498
499
500
501
502 INIT_LIST_HEAD(&new_ctx->committing);
503 INIT_LIST_HEAD(&new_ctx->busy_extents);
504 new_ctx->sequence = ctx->sequence + 1;
505 new_ctx->cil = cil;
506 cil->xc_ctx = new_ctx;
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533 spin_lock(&cil->xc_push_lock);
534 cil->xc_current_sequence = new_ctx->sequence;
535 list_add(&ctx->committing, &cil->xc_committing);
536 spin_unlock(&cil->xc_push_lock);
537 up_write(&cil->xc_ctx_lock);
538
539
540
541
542
543
544
545
546
547
548 tic = ctx->ticket;
549 thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
550 thdr.th_type = XFS_TRANS_CHECKPOINT;
551 thdr.th_tid = tic->t_tid;
552 thdr.th_num_items = num_iovecs;
553 lhdr.i_addr = &thdr;
554 lhdr.i_len = sizeof(xfs_trans_header_t);
555 lhdr.i_type = XLOG_REG_TYPE_TRANSHDR;
556 tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t);
557
558 lvhdr.lv_niovecs = 1;
559 lvhdr.lv_iovecp = &lhdr;
560 lvhdr.lv_next = ctx->lv_chain;
561
562 error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0);
563 if (error)
564 goto out_abort_free_ticket;
565
566
567
568
569
570restart:
571 spin_lock(&cil->xc_push_lock);
572 list_for_each_entry(new_ctx, &cil->xc_committing, committing) {
573
574
575
576
577
578 if (XLOG_FORCED_SHUTDOWN(log)) {
579 spin_unlock(&cil->xc_push_lock);
580 goto out_abort_free_ticket;
581 }
582
583
584
585
586
587 if (new_ctx->sequence >= ctx->sequence)
588 continue;
589 if (!new_ctx->commit_lsn) {
590
591
592
593
594 xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
595 goto restart;
596 }
597 }
598 spin_unlock(&cil->xc_push_lock);
599
600
601 commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0);
602 if (commit_lsn == -1)
603 goto out_abort;
604
605
606 ctx->log_cb.cb_func = xlog_cil_committed;
607 ctx->log_cb.cb_arg = ctx;
608 error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb);
609 if (error)
610 goto out_abort;
611
612
613
614
615
616
617 spin_lock(&cil->xc_push_lock);
618 ctx->commit_lsn = commit_lsn;
619 wake_up_all(&cil->xc_commit_wait);
620 spin_unlock(&cil->xc_push_lock);
621
622
623 return xfs_log_release_iclog(log->l_mp, commit_iclog);
624
625out_skip:
626 up_write(&cil->xc_ctx_lock);
627 xfs_log_ticket_put(new_ctx->ticket);
628 kmem_free(new_ctx);
629 return 0;
630
631out_abort_free_ticket:
632 xfs_log_ticket_put(tic);
633out_abort:
634 xlog_cil_committed(ctx, XFS_LI_ABORTED);
635 return -EIO;
636}
637
638static void
639xlog_cil_push_work(
640 struct work_struct *work)
641{
642 struct xfs_cil *cil = container_of(work, struct xfs_cil,
643 xc_push_work);
644 xlog_cil_push(cil->xc_log);
645}
646
647
648
649
650
651
652
653
654static void
655xlog_cil_push_background(
656 struct xlog *log)
657{
658 struct xfs_cil *cil = log->l_cilp;
659
660
661
662
663
664 ASSERT(!list_empty(&cil->xc_cil));
665
666
667
668
669
670 if (cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log))
671 return;
672
673 spin_lock(&cil->xc_push_lock);
674 if (cil->xc_push_seq < cil->xc_current_sequence) {
675 cil->xc_push_seq = cil->xc_current_sequence;
676 queue_work(log->l_mp->m_cil_workqueue, &cil->xc_push_work);
677 }
678 spin_unlock(&cil->xc_push_lock);
679
680}
681
682
683
684
685
686
687
688static void
689xlog_cil_push_now(
690 struct xlog *log,
691 xfs_lsn_t push_seq)
692{
693 struct xfs_cil *cil = log->l_cilp;
694
695 if (!cil)
696 return;
697
698 ASSERT(push_seq && push_seq <= cil->xc_current_sequence);
699
700
701 flush_work(&cil->xc_push_work);
702
703
704
705
706
707 spin_lock(&cil->xc_push_lock);
708 if (list_empty(&cil->xc_cil) || push_seq <= cil->xc_push_seq) {
709 spin_unlock(&cil->xc_push_lock);
710 return;
711 }
712
713 cil->xc_push_seq = push_seq;
714 queue_work(log->l_mp->m_cil_workqueue, &cil->xc_push_work);
715 spin_unlock(&cil->xc_push_lock);
716}
717
718bool
719xlog_cil_empty(
720 struct xlog *log)
721{
722 struct xfs_cil *cil = log->l_cilp;
723 bool empty = false;
724
725 spin_lock(&cil->xc_push_lock);
726 if (list_empty(&cil->xc_cil))
727 empty = true;
728 spin_unlock(&cil->xc_push_lock);
729 return empty;
730}
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745void
746xfs_log_commit_cil(
747 struct xfs_mount *mp,
748 struct xfs_trans *tp,
749 xfs_lsn_t *commit_lsn,
750 int flags)
751{
752 struct xlog *log = mp->m_log;
753 struct xfs_cil *cil = log->l_cilp;
754 int log_flags = 0;
755
756 if (flags & XFS_TRANS_RELEASE_LOG_RES)
757 log_flags = XFS_LOG_REL_PERM_RESERV;
758
759
760 down_read(&cil->xc_ctx_lock);
761
762 xlog_cil_insert_items(log, tp);
763
764
765 if (tp->t_ticket->t_curr_res < 0)
766 xlog_print_tic_res(mp, tp->t_ticket);
767
768 tp->t_commit_lsn = cil->xc_ctx->sequence;
769 if (commit_lsn)
770 *commit_lsn = tp->t_commit_lsn;
771
772 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
773 xfs_trans_unreserve_and_mod_sb(tp);
774
775
776
777
778
779
780
781
782
783
784
785
786 xfs_trans_free_items(tp, tp->t_commit_lsn, 0);
787
788 xlog_cil_push_background(log);
789
790 up_read(&cil->xc_ctx_lock);
791}
792
793
794
795
796
797
798
799
800
801
802
803xfs_lsn_t
804xlog_cil_force_lsn(
805 struct xlog *log,
806 xfs_lsn_t sequence)
807{
808 struct xfs_cil *cil = log->l_cilp;
809 struct xfs_cil_ctx *ctx;
810 xfs_lsn_t commit_lsn = NULLCOMMITLSN;
811
812 ASSERT(sequence <= cil->xc_current_sequence);
813
814
815
816
817
818
819restart:
820 xlog_cil_push_now(log, sequence);
821
822
823
824
825
826
827
828 spin_lock(&cil->xc_push_lock);
829 list_for_each_entry(ctx, &cil->xc_committing, committing) {
830
831
832
833
834
835 if (XLOG_FORCED_SHUTDOWN(log))
836 goto out_shutdown;
837 if (ctx->sequence > sequence)
838 continue;
839 if (!ctx->commit_lsn) {
840
841
842
843
844 xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
845 goto restart;
846 }
847 if (ctx->sequence != sequence)
848 continue;
849
850 commit_lsn = ctx->commit_lsn;
851 }
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866 if (sequence == cil->xc_current_sequence &&
867 !list_empty(&cil->xc_cil)) {
868 spin_unlock(&cil->xc_push_lock);
869 goto restart;
870 }
871
872 spin_unlock(&cil->xc_push_lock);
873 return commit_lsn;
874
875
876
877
878
879
880
881
882out_shutdown:
883 spin_unlock(&cil->xc_push_lock);
884 return 0;
885}
886
887
888
889
890
891
892
893
894
895
896bool
897xfs_log_item_in_current_chkpt(
898 struct xfs_log_item *lip)
899{
900 struct xfs_cil_ctx *ctx;
901
902 if (list_empty(&lip->li_cil))
903 return false;
904
905 ctx = lip->li_mountp->m_log->l_cilp->xc_ctx;
906
907
908
909
910
911
912 if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0)
913 return false;
914 return true;
915}
916
917
918
919
920int
921xlog_cil_init(
922 struct xlog *log)
923{
924 struct xfs_cil *cil;
925 struct xfs_cil_ctx *ctx;
926
927 cil = kmem_zalloc(sizeof(*cil), KM_SLEEP|KM_MAYFAIL);
928 if (!cil)
929 return -ENOMEM;
930
931 ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP|KM_MAYFAIL);
932 if (!ctx) {
933 kmem_free(cil);
934 return -ENOMEM;
935 }
936
937 INIT_WORK(&cil->xc_push_work, xlog_cil_push_work);
938 INIT_LIST_HEAD(&cil->xc_cil);
939 INIT_LIST_HEAD(&cil->xc_committing);
940 spin_lock_init(&cil->xc_cil_lock);
941 spin_lock_init(&cil->xc_push_lock);
942 init_rwsem(&cil->xc_ctx_lock);
943 init_waitqueue_head(&cil->xc_commit_wait);
944
945 INIT_LIST_HEAD(&ctx->committing);
946 INIT_LIST_HEAD(&ctx->busy_extents);
947 ctx->sequence = 1;
948 ctx->cil = cil;
949 cil->xc_ctx = ctx;
950 cil->xc_current_sequence = ctx->sequence;
951
952 cil->xc_log = log;
953 log->l_cilp = cil;
954 return 0;
955}
956
957void
958xlog_cil_destroy(
959 struct xlog *log)
960{
961 if (log->l_cilp->xc_ctx) {
962 if (log->l_cilp->xc_ctx->ticket)
963 xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket);
964 kmem_free(log->l_cilp->xc_ctx);
965 }
966
967 ASSERT(list_empty(&log->l_cilp->xc_cil));
968 kmem_free(log->l_cilp);
969}
970
971