1
2
3
4
5
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_mount.h"
13#include "xfs_errortag.h"
14#include "xfs_error.h"
15#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
17#include "xfs_log.h"
18#include "xfs_log_priv.h"
19#include "xfs_trace.h"
20#include "xfs_sysfs.h"
21#include "xfs_sb.h"
22#include "xfs_health.h"
23
24kmem_zone_t *xfs_log_ticket_zone;
25
26
27STATIC int
28xlog_commit_record(
29 struct xlog *log,
30 struct xlog_ticket *ticket,
31 struct xlog_in_core **iclog,
32 xfs_lsn_t *commitlsnp);
33
34STATIC struct xlog *
35xlog_alloc_log(
36 struct xfs_mount *mp,
37 struct xfs_buftarg *log_target,
38 xfs_daddr_t blk_offset,
39 int num_bblks);
40STATIC int
41xlog_space_left(
42 struct xlog *log,
43 atomic64_t *head);
44STATIC void
45xlog_dealloc_log(
46 struct xlog *log);
47
48
49STATIC void xlog_state_done_syncing(
50 struct xlog_in_core *iclog,
51 bool aborted);
52STATIC int
53xlog_state_get_iclog_space(
54 struct xlog *log,
55 int len,
56 struct xlog_in_core **iclog,
57 struct xlog_ticket *ticket,
58 int *continued_write,
59 int *logoffsetp);
60STATIC int
61xlog_state_release_iclog(
62 struct xlog *log,
63 struct xlog_in_core *iclog);
64STATIC void
65xlog_state_switch_iclogs(
66 struct xlog *log,
67 struct xlog_in_core *iclog,
68 int eventual_size);
69STATIC void
70xlog_state_want_sync(
71 struct xlog *log,
72 struct xlog_in_core *iclog);
73
74STATIC void
75xlog_grant_push_ail(
76 struct xlog *log,
77 int need_bytes);
78STATIC void
79xlog_regrant_reserve_log_space(
80 struct xlog *log,
81 struct xlog_ticket *ticket);
82STATIC void
83xlog_ungrant_log_space(
84 struct xlog *log,
85 struct xlog_ticket *ticket);
86
87#if defined(DEBUG)
88STATIC void
89xlog_verify_dest_ptr(
90 struct xlog *log,
91 void *ptr);
92STATIC void
93xlog_verify_grant_tail(
94 struct xlog *log);
95STATIC void
96xlog_verify_iclog(
97 struct xlog *log,
98 struct xlog_in_core *iclog,
99 int count);
100STATIC void
101xlog_verify_tail_lsn(
102 struct xlog *log,
103 struct xlog_in_core *iclog,
104 xfs_lsn_t tail_lsn);
105#else
106#define xlog_verify_dest_ptr(a,b)
107#define xlog_verify_grant_tail(a)
108#define xlog_verify_iclog(a,b,c)
109#define xlog_verify_tail_lsn(a,b,c)
110#endif
111
112STATIC int
113xlog_iclogs_empty(
114 struct xlog *log);
115
116static void
117xlog_grant_sub_space(
118 struct xlog *log,
119 atomic64_t *head,
120 int bytes)
121{
122 int64_t head_val = atomic64_read(head);
123 int64_t new, old;
124
125 do {
126 int cycle, space;
127
128 xlog_crack_grant_head_val(head_val, &cycle, &space);
129
130 space -= bytes;
131 if (space < 0) {
132 space += log->l_logsize;
133 cycle--;
134 }
135
136 old = head_val;
137 new = xlog_assign_grant_head_val(cycle, space);
138 head_val = atomic64_cmpxchg(head, old, new);
139 } while (head_val != old);
140}
141
142static void
143xlog_grant_add_space(
144 struct xlog *log,
145 atomic64_t *head,
146 int bytes)
147{
148 int64_t head_val = atomic64_read(head);
149 int64_t new, old;
150
151 do {
152 int tmp;
153 int cycle, space;
154
155 xlog_crack_grant_head_val(head_val, &cycle, &space);
156
157 tmp = log->l_logsize - space;
158 if (tmp > bytes)
159 space += bytes;
160 else {
161 space = bytes - tmp;
162 cycle++;
163 }
164
165 old = head_val;
166 new = xlog_assign_grant_head_val(cycle, space);
167 head_val = atomic64_cmpxchg(head, old, new);
168 } while (head_val != old);
169}
170
171STATIC void
172xlog_grant_head_init(
173 struct xlog_grant_head *head)
174{
175 xlog_assign_grant_head(&head->grant, 1, 0);
176 INIT_LIST_HEAD(&head->waiters);
177 spin_lock_init(&head->lock);
178}
179
180STATIC void
181xlog_grant_head_wake_all(
182 struct xlog_grant_head *head)
183{
184 struct xlog_ticket *tic;
185
186 spin_lock(&head->lock);
187 list_for_each_entry(tic, &head->waiters, t_queue)
188 wake_up_process(tic->t_task);
189 spin_unlock(&head->lock);
190}
191
192static inline int
193xlog_ticket_reservation(
194 struct xlog *log,
195 struct xlog_grant_head *head,
196 struct xlog_ticket *tic)
197{
198 if (head == &log->l_write_head) {
199 ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
200 return tic->t_unit_res;
201 } else {
202 if (tic->t_flags & XLOG_TIC_PERM_RESERV)
203 return tic->t_unit_res * tic->t_cnt;
204 else
205 return tic->t_unit_res;
206 }
207}
208
209STATIC bool
210xlog_grant_head_wake(
211 struct xlog *log,
212 struct xlog_grant_head *head,
213 int *free_bytes)
214{
215 struct xlog_ticket *tic;
216 int need_bytes;
217
218 list_for_each_entry(tic, &head->waiters, t_queue) {
219 need_bytes = xlog_ticket_reservation(log, head, tic);
220 if (*free_bytes < need_bytes)
221 return false;
222
223 *free_bytes -= need_bytes;
224 trace_xfs_log_grant_wake_up(log, tic);
225 wake_up_process(tic->t_task);
226 }
227
228 return true;
229}
230
231STATIC int
232xlog_grant_head_wait(
233 struct xlog *log,
234 struct xlog_grant_head *head,
235 struct xlog_ticket *tic,
236 int need_bytes) __releases(&head->lock)
237 __acquires(&head->lock)
238{
239 list_add_tail(&tic->t_queue, &head->waiters);
240
241 do {
242 if (XLOG_FORCED_SHUTDOWN(log))
243 goto shutdown;
244 xlog_grant_push_ail(log, need_bytes);
245
246 __set_current_state(TASK_UNINTERRUPTIBLE);
247 spin_unlock(&head->lock);
248
249 XFS_STATS_INC(log->l_mp, xs_sleep_logspace);
250
251 trace_xfs_log_grant_sleep(log, tic);
252 schedule();
253 trace_xfs_log_grant_wake(log, tic);
254
255 spin_lock(&head->lock);
256 if (XLOG_FORCED_SHUTDOWN(log))
257 goto shutdown;
258 } while (xlog_space_left(log, &head->grant) < need_bytes);
259
260 list_del_init(&tic->t_queue);
261 return 0;
262shutdown:
263 list_del_init(&tic->t_queue);
264 return -EIO;
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284STATIC int
285xlog_grant_head_check(
286 struct xlog *log,
287 struct xlog_grant_head *head,
288 struct xlog_ticket *tic,
289 int *need_bytes)
290{
291 int free_bytes;
292 int error = 0;
293
294 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
295
296
297
298
299
300
301
302 *need_bytes = xlog_ticket_reservation(log, head, tic);
303 free_bytes = xlog_space_left(log, &head->grant);
304 if (!list_empty_careful(&head->waiters)) {
305 spin_lock(&head->lock);
306 if (!xlog_grant_head_wake(log, head, &free_bytes) ||
307 free_bytes < *need_bytes) {
308 error = xlog_grant_head_wait(log, head, tic,
309 *need_bytes);
310 }
311 spin_unlock(&head->lock);
312 } else if (free_bytes < *need_bytes) {
313 spin_lock(&head->lock);
314 error = xlog_grant_head_wait(log, head, tic, *need_bytes);
315 spin_unlock(&head->lock);
316 }
317
318 return error;
319}
320
321static void
322xlog_tic_reset_res(xlog_ticket_t *tic)
323{
324 tic->t_res_num = 0;
325 tic->t_res_arr_sum = 0;
326 tic->t_res_num_ophdrs = 0;
327}
328
329static void
330xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
331{
332 if (tic->t_res_num == XLOG_TIC_LEN_MAX) {
333
334 tic->t_res_o_flow += tic->t_res_arr_sum;
335 tic->t_res_num = 0;
336 tic->t_res_arr_sum = 0;
337 }
338
339 tic->t_res_arr[tic->t_res_num].r_len = len;
340 tic->t_res_arr[tic->t_res_num].r_type = type;
341 tic->t_res_arr_sum += len;
342 tic->t_res_num++;
343}
344
345
346
347
348int
349xfs_log_regrant(
350 struct xfs_mount *mp,
351 struct xlog_ticket *tic)
352{
353 struct xlog *log = mp->m_log;
354 int need_bytes;
355 int error = 0;
356
357 if (XLOG_FORCED_SHUTDOWN(log))
358 return -EIO;
359
360 XFS_STATS_INC(mp, xs_try_logspace);
361
362
363
364
365
366
367
368 tic->t_tid++;
369
370 xlog_grant_push_ail(log, tic->t_unit_res);
371
372 tic->t_curr_res = tic->t_unit_res;
373 xlog_tic_reset_res(tic);
374
375 if (tic->t_cnt > 0)
376 return 0;
377
378 trace_xfs_log_regrant(log, tic);
379
380 error = xlog_grant_head_check(log, &log->l_write_head, tic,
381 &need_bytes);
382 if (error)
383 goto out_error;
384
385 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
386 trace_xfs_log_regrant_exit(log, tic);
387 xlog_verify_grant_tail(log);
388 return 0;
389
390out_error:
391
392
393
394
395
396 tic->t_curr_res = 0;
397 tic->t_cnt = 0;
398 return error;
399}
400
401
402
403
404
405
406
407
408
409int
410xfs_log_reserve(
411 struct xfs_mount *mp,
412 int unit_bytes,
413 int cnt,
414 struct xlog_ticket **ticp,
415 uint8_t client,
416 bool permanent)
417{
418 struct xlog *log = mp->m_log;
419 struct xlog_ticket *tic;
420 int need_bytes;
421 int error = 0;
422
423 ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
424
425 if (XLOG_FORCED_SHUTDOWN(log))
426 return -EIO;
427
428 XFS_STATS_INC(mp, xs_try_logspace);
429
430 ASSERT(*ticp == NULL);
431 tic = xlog_ticket_alloc(log, unit_bytes, cnt, client, permanent,
432 KM_SLEEP);
433 *ticp = tic;
434
435 xlog_grant_push_ail(log, tic->t_cnt ? tic->t_unit_res * tic->t_cnt
436 : tic->t_unit_res);
437
438 trace_xfs_log_reserve(log, tic);
439
440 error = xlog_grant_head_check(log, &log->l_reserve_head, tic,
441 &need_bytes);
442 if (error)
443 goto out_error;
444
445 xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
446 xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
447 trace_xfs_log_reserve_exit(log, tic);
448 xlog_verify_grant_tail(log);
449 return 0;
450
451out_error:
452
453
454
455
456
457 tic->t_curr_res = 0;
458 tic->t_cnt = 0;
459 return error;
460}
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484xfs_lsn_t
485xfs_log_done(
486 struct xfs_mount *mp,
487 struct xlog_ticket *ticket,
488 struct xlog_in_core **iclog,
489 bool regrant)
490{
491 struct xlog *log = mp->m_log;
492 xfs_lsn_t lsn = 0;
493
494 if (XLOG_FORCED_SHUTDOWN(log) ||
495
496
497
498
499 (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
500 (xlog_commit_record(log, ticket, iclog, &lsn)))) {
501 lsn = (xfs_lsn_t) -1;
502 regrant = false;
503 }
504
505
506 if (!regrant) {
507 trace_xfs_log_done_nonperm(log, ticket);
508
509
510
511
512
513 xlog_ungrant_log_space(log, ticket);
514 } else {
515 trace_xfs_log_done_perm(log, ticket);
516
517 xlog_regrant_reserve_log_space(log, ticket);
518
519
520
521
522 ticket->t_flags |= XLOG_TIC_INITED;
523 }
524
525 xfs_log_ticket_put(ticket);
526 return lsn;
527}
528
529int
530xfs_log_release_iclog(
531 struct xfs_mount *mp,
532 struct xlog_in_core *iclog)
533{
534 if (xlog_state_release_iclog(mp->m_log, iclog)) {
535 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
536 return -EIO;
537 }
538
539 return 0;
540}
541
542
543
544
545
546
547
548
549
550
551
552int
553xfs_log_mount(
554 xfs_mount_t *mp,
555 xfs_buftarg_t *log_target,
556 xfs_daddr_t blk_offset,
557 int num_bblks)
558{
559 bool fatal = xfs_sb_version_hascrc(&mp->m_sb);
560 int error = 0;
561 int min_logfsbs;
562
563 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
564 xfs_notice(mp, "Mounting V%d Filesystem",
565 XFS_SB_VERSION_NUM(&mp->m_sb));
566 } else {
567 xfs_notice(mp,
568"Mounting V%d filesystem in no-recovery mode. Filesystem will be inconsistent.",
569 XFS_SB_VERSION_NUM(&mp->m_sb));
570 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
571 }
572
573 mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
574 if (IS_ERR(mp->m_log)) {
575 error = PTR_ERR(mp->m_log);
576 goto out;
577 }
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593 min_logfsbs = xfs_log_calc_minimum_size(mp);
594
595 if (mp->m_sb.sb_logblocks < min_logfsbs) {
596 xfs_warn(mp,
597 "Log size %d blocks too small, minimum size is %d blocks",
598 mp->m_sb.sb_logblocks, min_logfsbs);
599 error = -EINVAL;
600 } else if (mp->m_sb.sb_logblocks > XFS_MAX_LOG_BLOCKS) {
601 xfs_warn(mp,
602 "Log size %d blocks too large, maximum size is %lld blocks",
603 mp->m_sb.sb_logblocks, XFS_MAX_LOG_BLOCKS);
604 error = -EINVAL;
605 } else if (XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks) > XFS_MAX_LOG_BYTES) {
606 xfs_warn(mp,
607 "log size %lld bytes too large, maximum size is %lld bytes",
608 XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks),
609 XFS_MAX_LOG_BYTES);
610 error = -EINVAL;
611 } else if (mp->m_sb.sb_logsunit > 1 &&
612 mp->m_sb.sb_logsunit % mp->m_sb.sb_blocksize) {
613 xfs_warn(mp,
614 "log stripe unit %u bytes must be a multiple of block size",
615 mp->m_sb.sb_logsunit);
616 error = -EINVAL;
617 fatal = true;
618 }
619 if (error) {
620
621
622
623
624 if (fatal) {
625 xfs_crit(mp, "AAIEEE! Log failed size checks. Abort!");
626 ASSERT(0);
627 goto out_free_log;
628 }
629 xfs_crit(mp, "Log size out of supported range.");
630 xfs_crit(mp,
631"Continuing onwards, but if log hangs are experienced then please report this message in the bug report.");
632 }
633
634
635
636
637 error = xfs_trans_ail_init(mp);
638 if (error) {
639 xfs_warn(mp, "AIL initialisation failed: error %d", error);
640 goto out_free_log;
641 }
642 mp->m_log->l_ailp = mp->m_ail;
643
644
645
646
647
648 if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
649 int readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
650
651 if (readonly)
652 mp->m_flags &= ~XFS_MOUNT_RDONLY;
653
654 error = xlog_recover(mp->m_log);
655
656 if (readonly)
657 mp->m_flags |= XFS_MOUNT_RDONLY;
658 if (error) {
659 xfs_warn(mp, "log mount/recovery failed: error %d",
660 error);
661 xlog_recover_cancel(mp->m_log);
662 goto out_destroy_ail;
663 }
664 }
665
666 error = xfs_sysfs_init(&mp->m_log->l_kobj, &xfs_log_ktype, &mp->m_kobj,
667 "log");
668 if (error)
669 goto out_destroy_ail;
670
671
672 mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
673
674
675
676
677
678
679 xlog_cil_init_post_recovery(mp->m_log);
680
681 return 0;
682
683out_destroy_ail:
684 xfs_trans_ail_destroy(mp);
685out_free_log:
686 xlog_dealloc_log(mp->m_log);
687out:
688 return error;
689}
690
691
692
693
694
695
696
697
698
699
700
701int
702xfs_log_mount_finish(
703 struct xfs_mount *mp)
704{
705 int error = 0;
706 bool readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
707 bool recovered = mp->m_log->l_flags & XLOG_RECOVERY_NEEDED;
708
709 if (mp->m_flags & XFS_MOUNT_NORECOVERY) {
710 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
711 return 0;
712 } else if (readonly) {
713
714 mp->m_flags &= ~XFS_MOUNT_RDONLY;
715 }
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737 mp->m_super->s_flags |= SB_ACTIVE;
738 error = xlog_recover_finish(mp->m_log);
739 if (!error)
740 xfs_log_work_queue(mp);
741 mp->m_super->s_flags &= ~SB_ACTIVE;
742 evict_inodes(mp->m_super);
743
744
745
746
747
748
749
750
751
752
753 if (!error && recovered) {
754 xfs_log_force(mp, XFS_LOG_SYNC);
755 xfs_ail_push_all_sync(mp->m_ail);
756 }
757 xfs_wait_buftarg(mp->m_ddev_targp);
758
759 if (readonly)
760 mp->m_flags |= XFS_MOUNT_RDONLY;
761
762 return error;
763}
764
765
766
767
768
769void
770xfs_log_mount_cancel(
771 struct xfs_mount *mp)
772{
773 xlog_recover_cancel(mp->m_log);
774 xfs_log_unmount(mp);
775}
776
777
778
779
780
781
782
783
784
785
786static void
787xfs_log_write_unmount_record(
788 struct xfs_mount *mp)
789{
790
791 struct xfs_unmount_log_format magic = {
792 .magic = XLOG_UNMOUNT_TYPE,
793 };
794 struct xfs_log_iovec reg = {
795 .i_addr = &magic,
796 .i_len = sizeof(magic),
797 .i_type = XLOG_REG_TYPE_UNMOUNT,
798 };
799 struct xfs_log_vec vec = {
800 .lv_niovecs = 1,
801 .lv_iovecp = ®,
802 };
803 struct xlog *log = mp->m_log;
804 struct xlog_in_core *iclog;
805 struct xlog_ticket *tic = NULL;
806 xfs_lsn_t lsn;
807 uint flags = XLOG_UNMOUNT_TRANS;
808 int error;
809
810 error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0);
811 if (error)
812 goto out_err;
813
814
815
816
817
818
819
820 if (XFS_TEST_ERROR(xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS), mp,
821 XFS_ERRTAG_FORCE_SUMMARY_RECALC)) {
822 xfs_alert(mp, "%s: will fix summary counters at next mount",
823 __func__);
824 flags &= ~XLOG_UNMOUNT_TRANS;
825 }
826
827
828 tic->t_flags = 0;
829 tic->t_curr_res -= sizeof(magic);
830 error = xlog_write(log, &vec, tic, &lsn, NULL, flags);
831
832
833
834
835out_err:
836 if (error)
837 xfs_alert(mp, "%s: unmount record failed", __func__);
838
839 spin_lock(&log->l_icloglock);
840 iclog = log->l_iclog;
841 atomic_inc(&iclog->ic_refcnt);
842 xlog_state_want_sync(log, iclog);
843 spin_unlock(&log->l_icloglock);
844 error = xlog_state_release_iclog(log, iclog);
845
846 spin_lock(&log->l_icloglock);
847 switch (iclog->ic_state) {
848 default:
849 if (!XLOG_FORCED_SHUTDOWN(log)) {
850 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
851 break;
852 }
853
854 case XLOG_STATE_ACTIVE:
855 case XLOG_STATE_DIRTY:
856 spin_unlock(&log->l_icloglock);
857 break;
858 }
859
860 if (tic) {
861 trace_xfs_log_umount_write(log, tic);
862 xlog_ungrant_log_space(log, tic);
863 xfs_log_ticket_put(tic);
864 }
865}
866
867
868
869
870
871
872
873
874
875static int
876xfs_log_unmount_write(xfs_mount_t *mp)
877{
878 struct xlog *log = mp->m_log;
879 xlog_in_core_t *iclog;
880#ifdef DEBUG
881 xlog_in_core_t *first_iclog;
882#endif
883 int error;
884
885
886
887
888
889 if (mp->m_flags & XFS_MOUNT_NORECOVERY ||
890 xfs_readonly_buftarg(log->l_targ)) {
891 ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
892 return 0;
893 }
894
895 error = xfs_log_force(mp, XFS_LOG_SYNC);
896 ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
897
898#ifdef DEBUG
899 first_iclog = iclog = log->l_iclog;
900 do {
901 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
902 ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE);
903 ASSERT(iclog->ic_offset == 0);
904 }
905 iclog = iclog->ic_next;
906 } while (iclog != first_iclog);
907#endif
908 if (! (XLOG_FORCED_SHUTDOWN(log))) {
909 xfs_log_write_unmount_record(mp);
910 } else {
911
912
913
914
915
916
917
918
919
920
921
922
923
924 spin_lock(&log->l_icloglock);
925 iclog = log->l_iclog;
926 atomic_inc(&iclog->ic_refcnt);
927
928 xlog_state_want_sync(log, iclog);
929 spin_unlock(&log->l_icloglock);
930 error = xlog_state_release_iclog(log, iclog);
931
932 spin_lock(&log->l_icloglock);
933
934 if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE
935 || iclog->ic_state == XLOG_STATE_DIRTY
936 || iclog->ic_state == XLOG_STATE_IOERROR) ) {
937
938 xlog_wait(&iclog->ic_force_wait,
939 &log->l_icloglock);
940 } else {
941 spin_unlock(&log->l_icloglock);
942 }
943 }
944
945 return error;
946}
947
948
949
950
951
952
953
954
955
956
957void
958xfs_log_quiesce(
959 struct xfs_mount *mp)
960{
961 cancel_delayed_work_sync(&mp->m_log->l_work);
962 xfs_log_force(mp, XFS_LOG_SYNC);
963
964
965
966
967
968
969
970
971 xfs_ail_push_all_sync(mp->m_ail);
972 xfs_wait_buftarg(mp->m_ddev_targp);
973 xfs_buf_lock(mp->m_sb_bp);
974 xfs_buf_unlock(mp->m_sb_bp);
975
976 xfs_log_unmount_write(mp);
977}
978
979
980
981
982
983
984
985
986void
987xfs_log_unmount(
988 struct xfs_mount *mp)
989{
990 xfs_log_quiesce(mp);
991
992 xfs_trans_ail_destroy(mp);
993
994 xfs_sysfs_del(&mp->m_log->l_kobj);
995
996 xlog_dealloc_log(mp->m_log);
997}
998
999void
1000xfs_log_item_init(
1001 struct xfs_mount *mp,
1002 struct xfs_log_item *item,
1003 int type,
1004 const struct xfs_item_ops *ops)
1005{
1006 item->li_mountp = mp;
1007 item->li_ailp = mp->m_ail;
1008 item->li_type = type;
1009 item->li_ops = ops;
1010 item->li_lv = NULL;
1011
1012 INIT_LIST_HEAD(&item->li_ail);
1013 INIT_LIST_HEAD(&item->li_cil);
1014 INIT_LIST_HEAD(&item->li_bio_list);
1015 INIT_LIST_HEAD(&item->li_trans);
1016}
1017
1018
1019
1020
1021void
1022xfs_log_space_wake(
1023 struct xfs_mount *mp)
1024{
1025 struct xlog *log = mp->m_log;
1026 int free_bytes;
1027
1028 if (XLOG_FORCED_SHUTDOWN(log))
1029 return;
1030
1031 if (!list_empty_careful(&log->l_write_head.waiters)) {
1032 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
1033
1034 spin_lock(&log->l_write_head.lock);
1035 free_bytes = xlog_space_left(log, &log->l_write_head.grant);
1036 xlog_grant_head_wake(log, &log->l_write_head, &free_bytes);
1037 spin_unlock(&log->l_write_head.lock);
1038 }
1039
1040 if (!list_empty_careful(&log->l_reserve_head.waiters)) {
1041 ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY));
1042
1043 spin_lock(&log->l_reserve_head.lock);
1044 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
1045 xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes);
1046 spin_unlock(&log->l_reserve_head.lock);
1047 }
1048}
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067static int
1068xfs_log_need_covered(xfs_mount_t *mp)
1069{
1070 struct xlog *log = mp->m_log;
1071 int needed = 0;
1072
1073 if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
1074 return 0;
1075
1076 if (!xlog_cil_empty(log))
1077 return 0;
1078
1079 spin_lock(&log->l_icloglock);
1080 switch (log->l_covered_state) {
1081 case XLOG_STATE_COVER_DONE:
1082 case XLOG_STATE_COVER_DONE2:
1083 case XLOG_STATE_COVER_IDLE:
1084 break;
1085 case XLOG_STATE_COVER_NEED:
1086 case XLOG_STATE_COVER_NEED2:
1087 if (xfs_ail_min_lsn(log->l_ailp))
1088 break;
1089 if (!xlog_iclogs_empty(log))
1090 break;
1091
1092 needed = 1;
1093 if (log->l_covered_state == XLOG_STATE_COVER_NEED)
1094 log->l_covered_state = XLOG_STATE_COVER_DONE;
1095 else
1096 log->l_covered_state = XLOG_STATE_COVER_DONE2;
1097 break;
1098 default:
1099 needed = 1;
1100 break;
1101 }
1102 spin_unlock(&log->l_icloglock);
1103 return needed;
1104}
1105
1106
1107
1108
1109xfs_lsn_t
1110xlog_assign_tail_lsn_locked(
1111 struct xfs_mount *mp)
1112{
1113 struct xlog *log = mp->m_log;
1114 struct xfs_log_item *lip;
1115 xfs_lsn_t tail_lsn;
1116
1117 assert_spin_locked(&mp->m_ail->ail_lock);
1118
1119
1120
1121
1122
1123
1124 lip = xfs_ail_min(mp->m_ail);
1125 if (lip)
1126 tail_lsn = lip->li_lsn;
1127 else
1128 tail_lsn = atomic64_read(&log->l_last_sync_lsn);
1129 trace_xfs_log_assign_tail_lsn(log, tail_lsn);
1130 atomic64_set(&log->l_tail_lsn, tail_lsn);
1131 return tail_lsn;
1132}
1133
1134xfs_lsn_t
1135xlog_assign_tail_lsn(
1136 struct xfs_mount *mp)
1137{
1138 xfs_lsn_t tail_lsn;
1139
1140 spin_lock(&mp->m_ail->ail_lock);
1141 tail_lsn = xlog_assign_tail_lsn_locked(mp);
1142 spin_unlock(&mp->m_ail->ail_lock);
1143
1144 return tail_lsn;
1145}
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161STATIC int
1162xlog_space_left(
1163 struct xlog *log,
1164 atomic64_t *head)
1165{
1166 int free_bytes;
1167 int tail_bytes;
1168 int tail_cycle;
1169 int head_cycle;
1170 int head_bytes;
1171
1172 xlog_crack_grant_head(head, &head_cycle, &head_bytes);
1173 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
1174 tail_bytes = BBTOB(tail_bytes);
1175 if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
1176 free_bytes = log->l_logsize - (head_bytes - tail_bytes);
1177 else if (tail_cycle + 1 < head_cycle)
1178 return 0;
1179 else if (tail_cycle < head_cycle) {
1180 ASSERT(tail_cycle == (head_cycle - 1));
1181 free_bytes = tail_bytes - head_bytes;
1182 } else {
1183
1184
1185
1186
1187
1188 xfs_alert(log->l_mp, "xlog_space_left: head behind tail");
1189 xfs_alert(log->l_mp,
1190 " tail_cycle = %d, tail_bytes = %d",
1191 tail_cycle, tail_bytes);
1192 xfs_alert(log->l_mp,
1193 " GH cycle = %d, GH bytes = %d",
1194 head_cycle, head_bytes);
1195 ASSERT(0);
1196 free_bytes = log->l_logsize;
1197 }
1198 return free_bytes;
1199}
1200
1201
1202static void
1203xlog_ioend_work(
1204 struct work_struct *work)
1205{
1206 struct xlog_in_core *iclog =
1207 container_of(work, struct xlog_in_core, ic_end_io_work);
1208 struct xlog *log = iclog->ic_log;
1209 bool aborted = false;
1210 int error;
1211
1212 error = blk_status_to_errno(iclog->ic_bio.bi_status);
1213#ifdef DEBUG
1214
1215 if (iclog->ic_fail_crc)
1216 error = -EIO;
1217#endif
1218
1219
1220
1221
1222 if (XFS_TEST_ERROR(error, log->l_mp, XFS_ERRTAG_IODONE_IOERR)) {
1223 xfs_alert(log->l_mp, "log I/O error %d", error);
1224 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
1225
1226
1227
1228
1229
1230 aborted = true;
1231 } else if (iclog->ic_state & XLOG_STATE_IOERROR) {
1232 aborted = true;
1233 }
1234
1235 xlog_state_done_syncing(iclog, aborted);
1236 bio_uninit(&iclog->ic_bio);
1237
1238
1239
1240
1241
1242
1243
1244 up(&iclog->ic_sema);
1245}
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255STATIC void
1256xlog_get_iclog_buffer_size(
1257 struct xfs_mount *mp,
1258 struct xlog *log)
1259{
1260 if (mp->m_logbufs <= 0)
1261 mp->m_logbufs = XLOG_MAX_ICLOGS;
1262 if (mp->m_logbsize <= 0)
1263 mp->m_logbsize = XLOG_BIG_RECORD_BSIZE;
1264
1265 log->l_iclog_bufs = mp->m_logbufs;
1266 log->l_iclog_size = mp->m_logbsize;
1267
1268
1269
1270
1271 log->l_iclog_heads =
1272 DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE);
1273 log->l_iclog_hsize = log->l_iclog_heads << BBSHIFT;
1274}
1275
1276void
1277xfs_log_work_queue(
1278 struct xfs_mount *mp)
1279{
1280 queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work,
1281 msecs_to_jiffies(xfs_syncd_centisecs * 10));
1282}
1283
1284
1285
1286
1287
1288
1289static void
1290xfs_log_worker(
1291 struct work_struct *work)
1292{
1293 struct xlog *log = container_of(to_delayed_work(work),
1294 struct xlog, l_work);
1295 struct xfs_mount *mp = log->l_mp;
1296
1297
1298 if (xfs_log_need_covered(mp)) {
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310 xfs_sync_sb(mp, true);
1311 } else
1312 xfs_log_force(mp, 0);
1313
1314
1315 xfs_ail_push_all(mp->m_ail);
1316
1317
1318 xfs_log_work_queue(mp);
1319}
1320
1321
1322
1323
1324
1325
1326STATIC struct xlog *
1327xlog_alloc_log(
1328 struct xfs_mount *mp,
1329 struct xfs_buftarg *log_target,
1330 xfs_daddr_t blk_offset,
1331 int num_bblks)
1332{
1333 struct xlog *log;
1334 xlog_rec_header_t *head;
1335 xlog_in_core_t **iclogp;
1336 xlog_in_core_t *iclog, *prev_iclog=NULL;
1337 int i;
1338 int error = -ENOMEM;
1339 uint log2_size = 0;
1340
1341 log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL);
1342 if (!log) {
1343 xfs_warn(mp, "Log allocation failed: No memory!");
1344 goto out;
1345 }
1346
1347 log->l_mp = mp;
1348 log->l_targ = log_target;
1349 log->l_logsize = BBTOB(num_bblks);
1350 log->l_logBBstart = blk_offset;
1351 log->l_logBBsize = num_bblks;
1352 log->l_covered_state = XLOG_STATE_COVER_IDLE;
1353 log->l_flags |= XLOG_ACTIVE_RECOVERY;
1354 INIT_DELAYED_WORK(&log->l_work, xfs_log_worker);
1355
1356 log->l_prev_block = -1;
1357
1358 xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
1359 xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
1360 log->l_curr_cycle = 1;
1361
1362 xlog_grant_head_init(&log->l_reserve_head);
1363 xlog_grant_head_init(&log->l_write_head);
1364
1365 error = -EFSCORRUPTED;
1366 if (xfs_sb_version_hassector(&mp->m_sb)) {
1367 log2_size = mp->m_sb.sb_logsectlog;
1368 if (log2_size < BBSHIFT) {
1369 xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
1370 log2_size, BBSHIFT);
1371 goto out_free_log;
1372 }
1373
1374 log2_size -= BBSHIFT;
1375 if (log2_size > mp->m_sectbb_log) {
1376 xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
1377 log2_size, mp->m_sectbb_log);
1378 goto out_free_log;
1379 }
1380
1381
1382 if (log2_size && log->l_logBBstart > 0 &&
1383 !xfs_sb_version_haslogv2(&mp->m_sb)) {
1384 xfs_warn(mp,
1385 "log sector size (0x%x) invalid for configuration.",
1386 log2_size);
1387 goto out_free_log;
1388 }
1389 }
1390 log->l_sectBBsize = 1 << log2_size;
1391
1392 xlog_get_iclog_buffer_size(mp, log);
1393
1394 spin_lock_init(&log->l_icloglock);
1395 init_waitqueue_head(&log->l_flush_wait);
1396
1397 iclogp = &log->l_iclog;
1398
1399
1400
1401
1402
1403
1404
1405 ASSERT(log->l_iclog_size >= 4096);
1406 for (i = 0; i < log->l_iclog_bufs; i++) {
1407 size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) *
1408 sizeof(struct bio_vec);
1409
1410 iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL);
1411 if (!iclog)
1412 goto out_free_iclog;
1413
1414 *iclogp = iclog;
1415 iclog->ic_prev = prev_iclog;
1416 prev_iclog = iclog;
1417
1418 iclog->ic_data = kmem_alloc_large(log->l_iclog_size,
1419 KM_MAYFAIL);
1420 if (!iclog->ic_data)
1421 goto out_free_iclog;
1422#ifdef DEBUG
1423 log->l_iclog_bak[i] = &iclog->ic_header;
1424#endif
1425 head = &iclog->ic_header;
1426 memset(head, 0, sizeof(xlog_rec_header_t));
1427 head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1428 head->h_version = cpu_to_be32(
1429 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
1430 head->h_size = cpu_to_be32(log->l_iclog_size);
1431
1432 head->h_fmt = cpu_to_be32(XLOG_FMT);
1433 memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
1434
1435 iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize;
1436 iclog->ic_state = XLOG_STATE_ACTIVE;
1437 iclog->ic_log = log;
1438 atomic_set(&iclog->ic_refcnt, 0);
1439 spin_lock_init(&iclog->ic_callback_lock);
1440 INIT_LIST_HEAD(&iclog->ic_callbacks);
1441 iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
1442
1443 init_waitqueue_head(&iclog->ic_force_wait);
1444 init_waitqueue_head(&iclog->ic_write_wait);
1445 INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work);
1446 sema_init(&iclog->ic_sema, 1);
1447
1448 iclogp = &iclog->ic_next;
1449 }
1450 *iclogp = log->l_iclog;
1451 log->l_iclog->ic_prev = prev_iclog;
1452
1453 log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s",
1454 WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_HIGHPRI, 0,
1455 mp->m_fsname);
1456 if (!log->l_ioend_workqueue)
1457 goto out_free_iclog;
1458
1459 error = xlog_cil_init(log);
1460 if (error)
1461 goto out_destroy_workqueue;
1462 return log;
1463
1464out_destroy_workqueue:
1465 destroy_workqueue(log->l_ioend_workqueue);
1466out_free_iclog:
1467 for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
1468 prev_iclog = iclog->ic_next;
1469 kmem_free(iclog->ic_data);
1470 kmem_free(iclog);
1471 }
1472out_free_log:
1473 kmem_free(log);
1474out:
1475 return ERR_PTR(error);
1476}
1477
1478
1479
1480
1481
1482
1483STATIC int
1484xlog_commit_record(
1485 struct xlog *log,
1486 struct xlog_ticket *ticket,
1487 struct xlog_in_core **iclog,
1488 xfs_lsn_t *commitlsnp)
1489{
1490 struct xfs_mount *mp = log->l_mp;
1491 int error;
1492 struct xfs_log_iovec reg = {
1493 .i_addr = NULL,
1494 .i_len = 0,
1495 .i_type = XLOG_REG_TYPE_COMMIT,
1496 };
1497 struct xfs_log_vec vec = {
1498 .lv_niovecs = 1,
1499 .lv_iovecp = ®,
1500 };
1501
1502 ASSERT_ALWAYS(iclog);
1503 error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
1504 XLOG_COMMIT_TRANS);
1505 if (error)
1506 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1507 return error;
1508}
1509
1510
1511
1512
1513
1514
1515
1516
1517STATIC void
1518xlog_grant_push_ail(
1519 struct xlog *log,
1520 int need_bytes)
1521{
1522 xfs_lsn_t threshold_lsn = 0;
1523 xfs_lsn_t last_sync_lsn;
1524 int free_blocks;
1525 int free_bytes;
1526 int threshold_block;
1527 int threshold_cycle;
1528 int free_threshold;
1529
1530 ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
1531
1532 free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
1533 free_blocks = BTOBBT(free_bytes);
1534
1535
1536
1537
1538
1539
1540 free_threshold = BTOBB(need_bytes);
1541 free_threshold = max(free_threshold, (log->l_logBBsize >> 2));
1542 free_threshold = max(free_threshold, 256);
1543 if (free_blocks >= free_threshold)
1544 return;
1545
1546 xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
1547 &threshold_block);
1548 threshold_block += free_threshold;
1549 if (threshold_block >= log->l_logBBsize) {
1550 threshold_block -= log->l_logBBsize;
1551 threshold_cycle += 1;
1552 }
1553 threshold_lsn = xlog_assign_lsn(threshold_cycle,
1554 threshold_block);
1555
1556
1557
1558
1559
1560 last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
1561 if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
1562 threshold_lsn = last_sync_lsn;
1563
1564
1565
1566
1567
1568
1569 if (!XLOG_FORCED_SHUTDOWN(log))
1570 xfs_ail_push(log->l_ailp, threshold_lsn);
1571}
1572
1573
1574
1575
1576STATIC void
1577xlog_pack_data(
1578 struct xlog *log,
1579 struct xlog_in_core *iclog,
1580 int roundoff)
1581{
1582 int i, j, k;
1583 int size = iclog->ic_offset + roundoff;
1584 __be32 cycle_lsn;
1585 char *dp;
1586
1587 cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
1588
1589 dp = iclog->ic_datap;
1590 for (i = 0; i < BTOBB(size); i++) {
1591 if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE))
1592 break;
1593 iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
1594 *(__be32 *)dp = cycle_lsn;
1595 dp += BBSIZE;
1596 }
1597
1598 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1599 xlog_in_core_2_t *xhdr = iclog->ic_data;
1600
1601 for ( ; i < BTOBB(size); i++) {
1602 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1603 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1604 xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
1605 *(__be32 *)dp = cycle_lsn;
1606 dp += BBSIZE;
1607 }
1608
1609 for (i = 1; i < log->l_iclog_heads; i++)
1610 xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
1611 }
1612}
1613
1614
1615
1616
1617
1618
1619
1620__le32
1621xlog_cksum(
1622 struct xlog *log,
1623 struct xlog_rec_header *rhead,
1624 char *dp,
1625 int size)
1626{
1627 uint32_t crc;
1628
1629
1630 crc = xfs_start_cksum_update((char *)rhead,
1631 sizeof(struct xlog_rec_header),
1632 offsetof(struct xlog_rec_header, h_crc));
1633
1634
1635 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1636 union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead;
1637 int i;
1638 int xheads;
1639
1640 xheads = size / XLOG_HEADER_CYCLE_SIZE;
1641 if (size % XLOG_HEADER_CYCLE_SIZE)
1642 xheads++;
1643
1644 for (i = 1; i < xheads; i++) {
1645 crc = crc32c(crc, &xhdr[i].hic_xheader,
1646 sizeof(struct xlog_rec_ext_header));
1647 }
1648 }
1649
1650
1651 crc = crc32c(crc, dp, size);
1652
1653 return xfs_end_cksum(crc);
1654}
1655
1656static void
1657xlog_bio_end_io(
1658 struct bio *bio)
1659{
1660 struct xlog_in_core *iclog = bio->bi_private;
1661
1662 queue_work(iclog->ic_log->l_ioend_workqueue,
1663 &iclog->ic_end_io_work);
1664}
1665
1666static void
1667xlog_map_iclog_data(
1668 struct bio *bio,
1669 void *data,
1670 size_t count)
1671{
1672 do {
1673 struct page *page = kmem_to_page(data);
1674 unsigned int off = offset_in_page(data);
1675 size_t len = min_t(size_t, count, PAGE_SIZE - off);
1676
1677 WARN_ON_ONCE(bio_add_page(bio, page, len, off) != len);
1678
1679 data += len;
1680 count -= len;
1681 } while (count);
1682}
1683
1684STATIC void
1685xlog_write_iclog(
1686 struct xlog *log,
1687 struct xlog_in_core *iclog,
1688 uint64_t bno,
1689 unsigned int count,
1690 bool need_flush)
1691{
1692 ASSERT(bno < log->l_logBBsize);
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702 down(&iclog->ic_sema);
1703 if (unlikely(iclog->ic_state & XLOG_STATE_IOERROR)) {
1704
1705
1706
1707
1708
1709
1710
1711 xlog_state_done_syncing(iclog, XFS_LI_ABORTED);
1712 up(&iclog->ic_sema);
1713 return;
1714 }
1715
1716 iclog->ic_io_size = count;
1717
1718 bio_init(&iclog->ic_bio, iclog->ic_bvec, howmany(count, PAGE_SIZE));
1719 bio_set_dev(&iclog->ic_bio, log->l_targ->bt_bdev);
1720 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno;
1721 iclog->ic_bio.bi_end_io = xlog_bio_end_io;
1722 iclog->ic_bio.bi_private = iclog;
1723 iclog->ic_bio.bi_opf = REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_FUA;
1724 if (need_flush)
1725 iclog->ic_bio.bi_opf |= REQ_PREFLUSH;
1726
1727 xlog_map_iclog_data(&iclog->ic_bio, iclog->ic_data, iclog->ic_io_size);
1728 if (is_vmalloc_addr(iclog->ic_data))
1729 flush_kernel_vmap_range(iclog->ic_data, iclog->ic_io_size);
1730
1731
1732
1733
1734
1735 if (bno + BTOBB(count) > log->l_logBBsize) {
1736 struct bio *split;
1737
1738 split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno,
1739 GFP_NOIO, &fs_bio_set);
1740 bio_chain(split, &iclog->ic_bio);
1741 submit_bio(split);
1742
1743
1744 iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart;
1745 }
1746
1747 submit_bio(&iclog->ic_bio);
1748}
1749
1750
1751
1752
1753
1754
1755static void
1756xlog_split_iclog(
1757 struct xlog *log,
1758 void *data,
1759 uint64_t bno,
1760 unsigned int count)
1761{
1762 unsigned int split_offset = BBTOB(log->l_logBBsize - bno);
1763 unsigned int i;
1764
1765 for (i = split_offset; i < count; i += BBSIZE) {
1766 uint32_t cycle = get_unaligned_be32(data + i);
1767
1768 if (++cycle == XLOG_HEADER_MAGIC_NUM)
1769 cycle++;
1770 put_unaligned_be32(cycle, data + i);
1771 }
1772}
1773
1774static int
1775xlog_calc_iclog_size(
1776 struct xlog *log,
1777 struct xlog_in_core *iclog,
1778 uint32_t *roundoff)
1779{
1780 uint32_t count_init, count;
1781 bool use_lsunit;
1782
1783 use_lsunit = xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
1784 log->l_mp->m_sb.sb_logsunit > 1;
1785
1786
1787 count_init = log->l_iclog_hsize + iclog->ic_offset;
1788
1789
1790 if (use_lsunit) {
1791
1792 count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
1793 } else {
1794 count = BBTOB(BTOBB(count_init));
1795 }
1796
1797 ASSERT(count >= count_init);
1798 *roundoff = count - count_init;
1799
1800 if (use_lsunit)
1801 ASSERT(*roundoff < log->l_mp->m_sb.sb_logsunit);
1802 else
1803 ASSERT(*roundoff < BBTOB(1));
1804 return count;
1805}
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830STATIC void
1831xlog_sync(
1832 struct xlog *log,
1833 struct xlog_in_core *iclog)
1834{
1835 unsigned int count;
1836 unsigned int roundoff;
1837 uint64_t bno;
1838 unsigned int size;
1839 bool need_flush = true, split = false;
1840
1841 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
1842
1843 count = xlog_calc_iclog_size(log, iclog, &roundoff);
1844
1845
1846 xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
1847 xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
1848
1849
1850 xlog_pack_data(log, iclog, roundoff);
1851
1852
1853 size = iclog->ic_offset;
1854 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb))
1855 size += roundoff;
1856 iclog->ic_header.h_len = cpu_to_be32(size);
1857
1858 XFS_STATS_INC(log->l_mp, xs_log_writes);
1859 XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count));
1860
1861 bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn));
1862
1863
1864 if (bno + BTOBB(count) > log->l_logBBsize) {
1865 xlog_split_iclog(log, &iclog->ic_header, bno, count);
1866 split = true;
1867 }
1868
1869
1870 iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header,
1871 iclog->ic_datap, size);
1872
1873
1874
1875
1876
1877
1878
1879#ifdef DEBUG
1880 if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) {
1881 iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA);
1882 iclog->ic_fail_crc = true;
1883 xfs_warn(log->l_mp,
1884 "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.",
1885 be64_to_cpu(iclog->ic_header.h_lsn));
1886 }
1887#endif
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897 if (log->l_targ != log->l_mp->m_ddev_targp || split) {
1898 xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
1899 need_flush = false;
1900 }
1901
1902 xlog_verify_iclog(log, iclog, count);
1903 xlog_write_iclog(log, iclog, bno, count, need_flush);
1904}
1905
1906
1907
1908
1909STATIC void
1910xlog_dealloc_log(
1911 struct xlog *log)
1912{
1913 xlog_in_core_t *iclog, *next_iclog;
1914 int i;
1915
1916 xlog_cil_destroy(log);
1917
1918
1919
1920
1921
1922 iclog = log->l_iclog;
1923 for (i = 0; i < log->l_iclog_bufs; i++) {
1924 down(&iclog->ic_sema);
1925 up(&iclog->ic_sema);
1926 iclog = iclog->ic_next;
1927 }
1928
1929 iclog = log->l_iclog;
1930 for (i = 0; i < log->l_iclog_bufs; i++) {
1931 next_iclog = iclog->ic_next;
1932 kmem_free(iclog->ic_data);
1933 kmem_free(iclog);
1934 iclog = next_iclog;
1935 }
1936
1937 log->l_mp->m_log = NULL;
1938 destroy_workqueue(log->l_ioend_workqueue);
1939 kmem_free(log);
1940}
1941
1942
1943
1944
1945
1946static inline void
1947xlog_state_finish_copy(
1948 struct xlog *log,
1949 struct xlog_in_core *iclog,
1950 int record_cnt,
1951 int copy_bytes)
1952{
1953 spin_lock(&log->l_icloglock);
1954
1955 be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
1956 iclog->ic_offset += copy_bytes;
1957
1958 spin_unlock(&log->l_icloglock);
1959}
1960
1961
1962
1963
1964
1965
1966
1967
1968void
1969xlog_print_tic_res(
1970 struct xfs_mount *mp,
1971 struct xlog_ticket *ticket)
1972{
1973 uint i;
1974 uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
1975
1976
1977#define REG_TYPE_STR(type, str) [XLOG_REG_TYPE_##type] = str
1978 static char *res_type_str[] = {
1979 REG_TYPE_STR(BFORMAT, "bformat"),
1980 REG_TYPE_STR(BCHUNK, "bchunk"),
1981 REG_TYPE_STR(EFI_FORMAT, "efi_format"),
1982 REG_TYPE_STR(EFD_FORMAT, "efd_format"),
1983 REG_TYPE_STR(IFORMAT, "iformat"),
1984 REG_TYPE_STR(ICORE, "icore"),
1985 REG_TYPE_STR(IEXT, "iext"),
1986 REG_TYPE_STR(IBROOT, "ibroot"),
1987 REG_TYPE_STR(ILOCAL, "ilocal"),
1988 REG_TYPE_STR(IATTR_EXT, "iattr_ext"),
1989 REG_TYPE_STR(IATTR_BROOT, "iattr_broot"),
1990 REG_TYPE_STR(IATTR_LOCAL, "iattr_local"),
1991 REG_TYPE_STR(QFORMAT, "qformat"),
1992 REG_TYPE_STR(DQUOT, "dquot"),
1993 REG_TYPE_STR(QUOTAOFF, "quotaoff"),
1994 REG_TYPE_STR(LRHEADER, "LR header"),
1995 REG_TYPE_STR(UNMOUNT, "unmount"),
1996 REG_TYPE_STR(COMMIT, "commit"),
1997 REG_TYPE_STR(TRANSHDR, "trans header"),
1998 REG_TYPE_STR(ICREATE, "inode create"),
1999 REG_TYPE_STR(RUI_FORMAT, "rui_format"),
2000 REG_TYPE_STR(RUD_FORMAT, "rud_format"),
2001 REG_TYPE_STR(CUI_FORMAT, "cui_format"),
2002 REG_TYPE_STR(CUD_FORMAT, "cud_format"),
2003 REG_TYPE_STR(BUI_FORMAT, "bui_format"),
2004 REG_TYPE_STR(BUD_FORMAT, "bud_format"),
2005 };
2006 BUILD_BUG_ON(ARRAY_SIZE(res_type_str) != XLOG_REG_TYPE_MAX + 1);
2007#undef REG_TYPE_STR
2008
2009 xfs_warn(mp, "ticket reservation summary:");
2010 xfs_warn(mp, " unit res = %d bytes",
2011 ticket->t_unit_res);
2012 xfs_warn(mp, " current res = %d bytes",
2013 ticket->t_curr_res);
2014 xfs_warn(mp, " total reg = %u bytes (o/flow = %u bytes)",
2015 ticket->t_res_arr_sum, ticket->t_res_o_flow);
2016 xfs_warn(mp, " ophdrs = %u (ophdr space = %u bytes)",
2017 ticket->t_res_num_ophdrs, ophdr_spc);
2018 xfs_warn(mp, " ophdr + reg = %u bytes",
2019 ticket->t_res_arr_sum + ticket->t_res_o_flow + ophdr_spc);
2020 xfs_warn(mp, " num regions = %u",
2021 ticket->t_res_num);
2022
2023 for (i = 0; i < ticket->t_res_num; i++) {
2024 uint r_type = ticket->t_res_arr[i].r_type;
2025 xfs_warn(mp, "region[%u]: %s - %u bytes", i,
2026 ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
2027 "bad-rtype" : res_type_str[r_type]),
2028 ticket->t_res_arr[i].r_len);
2029 }
2030}
2031
2032
2033
2034
2035void
2036xlog_print_trans(
2037 struct xfs_trans *tp)
2038{
2039 struct xfs_mount *mp = tp->t_mountp;
2040 struct xfs_log_item *lip;
2041
2042
2043 xfs_warn(mp, "transaction summary:");
2044 xfs_warn(mp, " log res = %d", tp->t_log_res);
2045 xfs_warn(mp, " log count = %d", tp->t_log_count);
2046 xfs_warn(mp, " flags = 0x%x", tp->t_flags);
2047
2048 xlog_print_tic_res(mp, tp->t_ticket);
2049
2050
2051 list_for_each_entry(lip, &tp->t_items, li_trans) {
2052 struct xfs_log_vec *lv = lip->li_lv;
2053 struct xfs_log_iovec *vec;
2054 int i;
2055
2056 xfs_warn(mp, "log item: ");
2057 xfs_warn(mp, " type = 0x%x", lip->li_type);
2058 xfs_warn(mp, " flags = 0x%lx", lip->li_flags);
2059 if (!lv)
2060 continue;
2061 xfs_warn(mp, " niovecs = %d", lv->lv_niovecs);
2062 xfs_warn(mp, " size = %d", lv->lv_size);
2063 xfs_warn(mp, " bytes = %d", lv->lv_bytes);
2064 xfs_warn(mp, " buf len = %d", lv->lv_buf_len);
2065
2066
2067 vec = lv->lv_iovecp;
2068 for (i = 0; i < lv->lv_niovecs; i++) {
2069 int dumplen = min(vec->i_len, 32);
2070
2071 xfs_warn(mp, " iovec[%d]", i);
2072 xfs_warn(mp, " type = 0x%x", vec->i_type);
2073 xfs_warn(mp, " len = %d", vec->i_len);
2074 xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i);
2075 xfs_hex_dump(vec->i_addr, dumplen);
2076
2077 vec++;
2078 }
2079 }
2080}
2081
2082
2083
2084
2085
2086static int
2087xlog_write_calc_vec_length(
2088 struct xlog_ticket *ticket,
2089 struct xfs_log_vec *log_vector)
2090{
2091 struct xfs_log_vec *lv;
2092 int headers = 0;
2093 int len = 0;
2094 int i;
2095
2096
2097 if (ticket->t_flags & XLOG_TIC_INITED)
2098 headers++;
2099
2100 for (lv = log_vector; lv; lv = lv->lv_next) {
2101
2102 if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED)
2103 continue;
2104
2105 headers += lv->lv_niovecs;
2106
2107 for (i = 0; i < lv->lv_niovecs; i++) {
2108 struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
2109
2110 len += vecp->i_len;
2111 xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
2112 }
2113 }
2114
2115 ticket->t_res_num_ophdrs += headers;
2116 len += headers * sizeof(struct xlog_op_header);
2117
2118 return len;
2119}
2120
2121
2122
2123
2124
2125static int
2126xlog_write_start_rec(
2127 struct xlog_op_header *ophdr,
2128 struct xlog_ticket *ticket)
2129{
2130 if (!(ticket->t_flags & XLOG_TIC_INITED))
2131 return 0;
2132
2133 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2134 ophdr->oh_clientid = ticket->t_clientid;
2135 ophdr->oh_len = 0;
2136 ophdr->oh_flags = XLOG_START_TRANS;
2137 ophdr->oh_res2 = 0;
2138
2139 ticket->t_flags &= ~XLOG_TIC_INITED;
2140
2141 return sizeof(struct xlog_op_header);
2142}
2143
2144static xlog_op_header_t *
2145xlog_write_setup_ophdr(
2146 struct xlog *log,
2147 struct xlog_op_header *ophdr,
2148 struct xlog_ticket *ticket,
2149 uint flags)
2150{
2151 ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
2152 ophdr->oh_clientid = ticket->t_clientid;
2153 ophdr->oh_res2 = 0;
2154
2155
2156 ophdr->oh_flags = flags;
2157
2158
2159
2160
2161
2162
2163 switch (ophdr->oh_clientid) {
2164 case XFS_TRANSACTION:
2165 case XFS_VOLUME:
2166 case XFS_LOG:
2167 break;
2168 default:
2169 xfs_warn(log->l_mp,
2170 "Bad XFS transaction clientid 0x%x in ticket "PTR_FMT,
2171 ophdr->oh_clientid, ticket);
2172 return NULL;
2173 }
2174
2175 return ophdr;
2176}
2177
2178
2179
2180
2181
2182
2183
2184static int
2185xlog_write_setup_copy(
2186 struct xlog_ticket *ticket,
2187 struct xlog_op_header *ophdr,
2188 int space_available,
2189 int space_required,
2190 int *copy_off,
2191 int *copy_len,
2192 int *last_was_partial_copy,
2193 int *bytes_consumed)
2194{
2195 int still_to_copy;
2196
2197 still_to_copy = space_required - *bytes_consumed;
2198 *copy_off = *bytes_consumed;
2199
2200 if (still_to_copy <= space_available) {
2201
2202 *copy_len = still_to_copy;
2203 ophdr->oh_len = cpu_to_be32(*copy_len);
2204 if (*last_was_partial_copy)
2205 ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
2206 *last_was_partial_copy = 0;
2207 *bytes_consumed = 0;
2208 return 0;
2209 }
2210
2211
2212 *copy_len = space_available;
2213 ophdr->oh_len = cpu_to_be32(*copy_len);
2214 ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
2215 if (*last_was_partial_copy)
2216 ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
2217 *bytes_consumed += *copy_len;
2218 (*last_was_partial_copy)++;
2219
2220
2221 ticket->t_curr_res -= sizeof(struct xlog_op_header);
2222 ticket->t_res_num_ophdrs++;
2223
2224 return sizeof(struct xlog_op_header);
2225}
2226
2227static int
2228xlog_write_copy_finish(
2229 struct xlog *log,
2230 struct xlog_in_core *iclog,
2231 uint flags,
2232 int *record_cnt,
2233 int *data_cnt,
2234 int *partial_copy,
2235 int *partial_copy_len,
2236 int log_offset,
2237 struct xlog_in_core **commit_iclog)
2238{
2239 if (*partial_copy) {
2240
2241
2242
2243
2244 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
2245 *record_cnt = 0;
2246 *data_cnt = 0;
2247 return xlog_state_release_iclog(log, iclog);
2248 }
2249
2250 *partial_copy = 0;
2251 *partial_copy_len = 0;
2252
2253 if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
2254
2255 xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
2256 *record_cnt = 0;
2257 *data_cnt = 0;
2258
2259 spin_lock(&log->l_icloglock);
2260 xlog_state_want_sync(log, iclog);
2261 spin_unlock(&log->l_icloglock);
2262
2263 if (!commit_iclog)
2264 return xlog_state_release_iclog(log, iclog);
2265 ASSERT(flags & XLOG_COMMIT_TRANS);
2266 *commit_iclog = iclog;
2267 }
2268
2269 return 0;
2270}
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312int
2313xlog_write(
2314 struct xlog *log,
2315 struct xfs_log_vec *log_vector,
2316 struct xlog_ticket *ticket,
2317 xfs_lsn_t *start_lsn,
2318 struct xlog_in_core **commit_iclog,
2319 uint flags)
2320{
2321 struct xlog_in_core *iclog = NULL;
2322 struct xfs_log_iovec *vecp;
2323 struct xfs_log_vec *lv;
2324 int len;
2325 int index;
2326 int partial_copy = 0;
2327 int partial_copy_len = 0;
2328 int contwr = 0;
2329 int record_cnt = 0;
2330 int data_cnt = 0;
2331 int error;
2332
2333 *start_lsn = 0;
2334
2335 len = xlog_write_calc_vec_length(ticket, log_vector);
2336
2337
2338
2339
2340
2341
2342 if (ticket->t_flags & XLOG_TIC_INITED)
2343 ticket->t_curr_res -= sizeof(xlog_op_header_t);
2344
2345
2346
2347
2348
2349 if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
2350 ticket->t_curr_res -= sizeof(xlog_op_header_t);
2351
2352 if (ticket->t_curr_res < 0) {
2353 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
2354 "ctx ticket reservation ran out. Need to up reservation");
2355 xlog_print_tic_res(log->l_mp, ticket);
2356 xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
2357 }
2358
2359 index = 0;
2360 lv = log_vector;
2361 vecp = lv->lv_iovecp;
2362 while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
2363 void *ptr;
2364 int log_offset;
2365
2366 error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
2367 &contwr, &log_offset);
2368 if (error)
2369 return error;
2370
2371 ASSERT(log_offset <= iclog->ic_size - 1);
2372 ptr = iclog->ic_datap + log_offset;
2373
2374
2375 if (!*start_lsn)
2376 *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2377
2378
2379
2380
2381
2382 while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
2383 struct xfs_log_iovec *reg;
2384 struct xlog_op_header *ophdr;
2385 int start_rec_copy;
2386 int copy_len;
2387 int copy_off;
2388 bool ordered = false;
2389
2390
2391 if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED) {
2392 ASSERT(lv->lv_niovecs == 0);
2393 ordered = true;
2394 goto next_lv;
2395 }
2396
2397 reg = &vecp[index];
2398 ASSERT(reg->i_len % sizeof(int32_t) == 0);
2399 ASSERT((unsigned long)ptr % sizeof(int32_t) == 0);
2400
2401 start_rec_copy = xlog_write_start_rec(ptr, ticket);
2402 if (start_rec_copy) {
2403 record_cnt++;
2404 xlog_write_adv_cnt(&ptr, &len, &log_offset,
2405 start_rec_copy);
2406 }
2407
2408 ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
2409 if (!ophdr)
2410 return -EIO;
2411
2412 xlog_write_adv_cnt(&ptr, &len, &log_offset,
2413 sizeof(struct xlog_op_header));
2414
2415 len += xlog_write_setup_copy(ticket, ophdr,
2416 iclog->ic_size-log_offset,
2417 reg->i_len,
2418 ©_off, ©_len,
2419 &partial_copy,
2420 &partial_copy_len);
2421 xlog_verify_dest_ptr(log, ptr);
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431 ASSERT(copy_len >= 0);
2432 if (copy_len > 0) {
2433 memcpy(ptr, reg->i_addr + copy_off, copy_len);
2434 xlog_write_adv_cnt(&ptr, &len, &log_offset,
2435 copy_len);
2436 }
2437 copy_len += start_rec_copy + sizeof(xlog_op_header_t);
2438 record_cnt++;
2439 data_cnt += contwr ? copy_len : 0;
2440
2441 error = xlog_write_copy_finish(log, iclog, flags,
2442 &record_cnt, &data_cnt,
2443 &partial_copy,
2444 &partial_copy_len,
2445 log_offset,
2446 commit_iclog);
2447 if (error)
2448 return error;
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462 if (partial_copy)
2463 break;
2464
2465 if (++index == lv->lv_niovecs) {
2466next_lv:
2467 lv = lv->lv_next;
2468 index = 0;
2469 if (lv)
2470 vecp = lv->lv_iovecp;
2471 }
2472 if (record_cnt == 0 && !ordered) {
2473 if (!lv)
2474 return 0;
2475 break;
2476 }
2477 }
2478 }
2479
2480 ASSERT(len == 0);
2481
2482 xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
2483 if (!commit_iclog)
2484 return xlog_state_release_iclog(log, iclog);
2485
2486 ASSERT(flags & XLOG_COMMIT_TRANS);
2487 *commit_iclog = iclog;
2488 return 0;
2489}
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507STATIC void
2508xlog_state_clean_log(
2509 struct xlog *log)
2510{
2511 xlog_in_core_t *iclog;
2512 int changed = 0;
2513
2514 iclog = log->l_iclog;
2515 do {
2516 if (iclog->ic_state == XLOG_STATE_DIRTY) {
2517 iclog->ic_state = XLOG_STATE_ACTIVE;
2518 iclog->ic_offset = 0;
2519 ASSERT(list_empty_careful(&iclog->ic_callbacks));
2520
2521
2522
2523
2524
2525
2526
2527
2528 if (!changed &&
2529 (be32_to_cpu(iclog->ic_header.h_num_logops) ==
2530 XLOG_COVER_OPS)) {
2531 changed = 1;
2532 } else {
2533
2534
2535
2536
2537
2538 changed = 2;
2539 }
2540 iclog->ic_header.h_num_logops = 0;
2541 memset(iclog->ic_header.h_cycle_data, 0,
2542 sizeof(iclog->ic_header.h_cycle_data));
2543 iclog->ic_header.h_lsn = 0;
2544 } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
2545 ;
2546 else
2547 break;
2548 iclog = iclog->ic_next;
2549 } while (iclog != log->l_iclog);
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559 if (changed) {
2560 switch (log->l_covered_state) {
2561 case XLOG_STATE_COVER_IDLE:
2562 case XLOG_STATE_COVER_NEED:
2563 case XLOG_STATE_COVER_NEED2:
2564 log->l_covered_state = XLOG_STATE_COVER_NEED;
2565 break;
2566
2567 case XLOG_STATE_COVER_DONE:
2568 if (changed == 1)
2569 log->l_covered_state = XLOG_STATE_COVER_NEED2;
2570 else
2571 log->l_covered_state = XLOG_STATE_COVER_NEED;
2572 break;
2573
2574 case XLOG_STATE_COVER_DONE2:
2575 if (changed == 1)
2576 log->l_covered_state = XLOG_STATE_COVER_IDLE;
2577 else
2578 log->l_covered_state = XLOG_STATE_COVER_NEED;
2579 break;
2580
2581 default:
2582 ASSERT(0);
2583 }
2584 }
2585}
2586
2587STATIC xfs_lsn_t
2588xlog_get_lowest_lsn(
2589 struct xlog *log)
2590{
2591 struct xlog_in_core *iclog = log->l_iclog;
2592 xfs_lsn_t lowest_lsn = 0, lsn;
2593
2594 do {
2595 if (iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))
2596 continue;
2597
2598 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
2599 if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0)
2600 lowest_lsn = lsn;
2601 } while ((iclog = iclog->ic_next) != log->l_iclog);
2602
2603 return lowest_lsn;
2604}
2605
2606STATIC void
2607xlog_state_do_callback(
2608 struct xlog *log,
2609 bool aborted,
2610 struct xlog_in_core *ciclog)
2611{
2612 xlog_in_core_t *iclog;
2613 xlog_in_core_t *first_iclog;
2614
2615 int flushcnt = 0;
2616 xfs_lsn_t lowest_lsn;
2617 int ioerrors;
2618 int loopdidcallbacks;
2619 int funcdidcallbacks;
2620 int repeats;
2621
2622 int wake = 0;
2623
2624 spin_lock(&log->l_icloglock);
2625 first_iclog = iclog = log->l_iclog;
2626 ioerrors = 0;
2627 funcdidcallbacks = 0;
2628 repeats = 0;
2629
2630 do {
2631
2632
2633
2634
2635
2636
2637
2638
2639 first_iclog = log->l_iclog;
2640 iclog = log->l_iclog;
2641 loopdidcallbacks = 0;
2642 repeats++;
2643
2644 do {
2645
2646
2647 if (iclog->ic_state &
2648 (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
2649 iclog = iclog->ic_next;
2650 continue;
2651 }
2652
2653
2654
2655
2656
2657
2658
2659
2660 if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671 if (!(iclog->ic_state &
2672 (XLOG_STATE_DONE_SYNC |
2673 XLOG_STATE_DO_CALLBACK))) {
2674 if (ciclog && (ciclog->ic_state ==
2675 XLOG_STATE_DONE_SYNC)) {
2676 ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
2677 }
2678 break;
2679 }
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694 lowest_lsn = xlog_get_lowest_lsn(log);
2695 if (lowest_lsn &&
2696 XFS_LSN_CMP(lowest_lsn,
2697 be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
2698 iclog = iclog->ic_next;
2699 continue;
2700
2701 }
2702
2703 iclog->ic_state = XLOG_STATE_CALLBACK;
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723 ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
2724 be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
2725 if (!list_empty_careful(&iclog->ic_callbacks))
2726 atomic64_set(&log->l_last_sync_lsn,
2727 be64_to_cpu(iclog->ic_header.h_lsn));
2728
2729 } else
2730 ioerrors++;
2731
2732 spin_unlock(&log->l_icloglock);
2733
2734
2735
2736
2737
2738
2739
2740
2741 spin_lock(&iclog->ic_callback_lock);
2742 while (!list_empty(&iclog->ic_callbacks)) {
2743 LIST_HEAD(tmp);
2744
2745 list_splice_init(&iclog->ic_callbacks, &tmp);
2746
2747 spin_unlock(&iclog->ic_callback_lock);
2748 xlog_cil_process_committed(&tmp, aborted);
2749 spin_lock(&iclog->ic_callback_lock);
2750 }
2751
2752 loopdidcallbacks++;
2753 funcdidcallbacks++;
2754
2755 spin_lock(&log->l_icloglock);
2756 spin_unlock(&iclog->ic_callback_lock);
2757 if (!(iclog->ic_state & XLOG_STATE_IOERROR))
2758 iclog->ic_state = XLOG_STATE_DIRTY;
2759
2760
2761
2762
2763
2764 xlog_state_clean_log(log);
2765
2766
2767 wake_up_all(&iclog->ic_force_wait);
2768
2769 iclog = iclog->ic_next;
2770 } while (first_iclog != iclog);
2771
2772 if (repeats > 5000) {
2773 flushcnt += repeats;
2774 repeats = 0;
2775 xfs_warn(log->l_mp,
2776 "%s: possible infinite loop (%d iterations)",
2777 __func__, flushcnt);
2778 }
2779 } while (!ioerrors && loopdidcallbacks);
2780
2781#ifdef DEBUG
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794 if (funcdidcallbacks) {
2795 first_iclog = iclog = log->l_iclog;
2796 do {
2797 ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807 if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
2808 iclog->ic_state & XLOG_STATE_SYNCING ||
2809 iclog->ic_state == XLOG_STATE_DONE_SYNC ||
2810 iclog->ic_state == XLOG_STATE_IOERROR )
2811 break;
2812 iclog = iclog->ic_next;
2813 } while (first_iclog != iclog);
2814 }
2815#endif
2816
2817 if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
2818 wake = 1;
2819 spin_unlock(&log->l_icloglock);
2820
2821 if (wake)
2822 wake_up_all(&log->l_flush_wait);
2823}
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839STATIC void
2840xlog_state_done_syncing(
2841 struct xlog_in_core *iclog,
2842 bool aborted)
2843{
2844 struct xlog *log = iclog->ic_log;
2845
2846 spin_lock(&log->l_icloglock);
2847
2848 ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
2849 iclog->ic_state == XLOG_STATE_IOERROR);
2850 ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
2851
2852
2853
2854
2855
2856
2857
2858 if (iclog->ic_state != XLOG_STATE_IOERROR)
2859 iclog->ic_state = XLOG_STATE_DONE_SYNC;
2860
2861
2862
2863
2864
2865
2866 wake_up_all(&iclog->ic_write_wait);
2867 spin_unlock(&log->l_icloglock);
2868 xlog_state_do_callback(log, aborted, iclog);
2869}
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890STATIC int
2891xlog_state_get_iclog_space(
2892 struct xlog *log,
2893 int len,
2894 struct xlog_in_core **iclogp,
2895 struct xlog_ticket *ticket,
2896 int *continued_write,
2897 int *logoffsetp)
2898{
2899 int log_offset;
2900 xlog_rec_header_t *head;
2901 xlog_in_core_t *iclog;
2902 int error;
2903
2904restart:
2905 spin_lock(&log->l_icloglock);
2906 if (XLOG_FORCED_SHUTDOWN(log)) {
2907 spin_unlock(&log->l_icloglock);
2908 return -EIO;
2909 }
2910
2911 iclog = log->l_iclog;
2912 if (iclog->ic_state != XLOG_STATE_ACTIVE) {
2913 XFS_STATS_INC(log->l_mp, xs_log_noiclogs);
2914
2915
2916 xlog_wait(&log->l_flush_wait, &log->l_icloglock);
2917 goto restart;
2918 }
2919
2920 head = &iclog->ic_header;
2921
2922 atomic_inc(&iclog->ic_refcnt);
2923 log_offset = iclog->ic_offset;
2924
2925
2926
2927
2928
2929
2930 if (log_offset == 0) {
2931 ticket->t_curr_res -= log->l_iclog_hsize;
2932 xlog_tic_add_region(ticket,
2933 log->l_iclog_hsize,
2934 XLOG_REG_TYPE_LRHEADER);
2935 head->h_cycle = cpu_to_be32(log->l_curr_cycle);
2936 head->h_lsn = cpu_to_be64(
2937 xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
2938 ASSERT(log->l_curr_block >= 0);
2939 }
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
2951 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2952
2953
2954
2955
2956
2957
2958
2959
2960 if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2961
2962 spin_unlock(&log->l_icloglock);
2963 error = xlog_state_release_iclog(log, iclog);
2964 if (error)
2965 return error;
2966 } else {
2967 spin_unlock(&log->l_icloglock);
2968 }
2969 goto restart;
2970 }
2971
2972
2973
2974
2975
2976
2977
2978 if (len <= iclog->ic_size - iclog->ic_offset) {
2979 *continued_write = 0;
2980 iclog->ic_offset += len;
2981 } else {
2982 *continued_write = 1;
2983 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
2984 }
2985 *iclogp = iclog;
2986
2987 ASSERT(iclog->ic_offset <= iclog->ic_size);
2988 spin_unlock(&log->l_icloglock);
2989
2990 *logoffsetp = log_offset;
2991 return 0;
2992}
2993
2994
2995
2996
2997
2998
2999
3000
3001STATIC void
3002xlog_regrant_reserve_log_space(
3003 struct xlog *log,
3004 struct xlog_ticket *ticket)
3005{
3006 trace_xfs_log_regrant_reserve_enter(log, ticket);
3007
3008 if (ticket->t_cnt > 0)
3009 ticket->t_cnt--;
3010
3011 xlog_grant_sub_space(log, &log->l_reserve_head.grant,
3012 ticket->t_curr_res);
3013 xlog_grant_sub_space(log, &log->l_write_head.grant,
3014 ticket->t_curr_res);
3015 ticket->t_curr_res = ticket->t_unit_res;
3016 xlog_tic_reset_res(ticket);
3017
3018 trace_xfs_log_regrant_reserve_sub(log, ticket);
3019
3020
3021 if (ticket->t_cnt > 0)
3022 return;
3023
3024 xlog_grant_add_space(log, &log->l_reserve_head.grant,
3025 ticket->t_unit_res);
3026
3027 trace_xfs_log_regrant_reserve_exit(log, ticket);
3028
3029 ticket->t_curr_res = ticket->t_unit_res;
3030 xlog_tic_reset_res(ticket);
3031}
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048STATIC void
3049xlog_ungrant_log_space(
3050 struct xlog *log,
3051 struct xlog_ticket *ticket)
3052{
3053 int bytes;
3054
3055 if (ticket->t_cnt > 0)
3056 ticket->t_cnt--;
3057
3058 trace_xfs_log_ungrant_enter(log, ticket);
3059 trace_xfs_log_ungrant_sub(log, ticket);
3060
3061
3062
3063
3064
3065 bytes = ticket->t_curr_res;
3066 if (ticket->t_cnt > 0) {
3067 ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
3068 bytes += ticket->t_unit_res*ticket->t_cnt;
3069 }
3070
3071 xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
3072 xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
3073
3074 trace_xfs_log_ungrant_exit(log, ticket);
3075
3076 xfs_log_space_wake(log->l_mp);
3077}
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088STATIC int
3089xlog_state_release_iclog(
3090 struct xlog *log,
3091 struct xlog_in_core *iclog)
3092{
3093 int sync = 0;
3094
3095 if (iclog->ic_state & XLOG_STATE_IOERROR)
3096 return -EIO;
3097
3098 ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
3099 if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
3100 return 0;
3101
3102 if (iclog->ic_state & XLOG_STATE_IOERROR) {
3103 spin_unlock(&log->l_icloglock);
3104 return -EIO;
3105 }
3106 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
3107 iclog->ic_state == XLOG_STATE_WANT_SYNC);
3108
3109 if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
3110
3111 xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
3112 sync++;
3113 iclog->ic_state = XLOG_STATE_SYNCING;
3114 iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
3115 xlog_verify_tail_lsn(log, iclog, tail_lsn);
3116
3117 }
3118 spin_unlock(&log->l_icloglock);
3119
3120
3121
3122
3123
3124
3125
3126
3127 if (sync)
3128 xlog_sync(log, iclog);
3129 return 0;
3130}
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140STATIC void
3141xlog_state_switch_iclogs(
3142 struct xlog *log,
3143 struct xlog_in_core *iclog,
3144 int eventual_size)
3145{
3146 ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
3147 if (!eventual_size)
3148 eventual_size = iclog->ic_offset;
3149 iclog->ic_state = XLOG_STATE_WANT_SYNC;
3150 iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
3151 log->l_prev_block = log->l_curr_block;
3152 log->l_prev_cycle = log->l_curr_cycle;
3153
3154
3155 log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
3156
3157
3158 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
3159 log->l_mp->m_sb.sb_logsunit > 1) {
3160 uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
3161 log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
3162 }
3163
3164 if (log->l_curr_block >= log->l_logBBsize) {
3165
3166
3167
3168
3169
3170
3171
3172 log->l_curr_block -= log->l_logBBsize;
3173 ASSERT(log->l_curr_block >= 0);
3174 smp_wmb();
3175 log->l_curr_cycle++;
3176 if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
3177 log->l_curr_cycle++;
3178 }
3179 ASSERT(iclog == log->l_iclog);
3180 log->l_iclog = iclog->ic_next;
3181}
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210int
3211xfs_log_force(
3212 struct xfs_mount *mp,
3213 uint flags)
3214{
3215 struct xlog *log = mp->m_log;
3216 struct xlog_in_core *iclog;
3217 xfs_lsn_t lsn;
3218
3219 XFS_STATS_INC(mp, xs_log_force);
3220 trace_xfs_log_force(mp, 0, _RET_IP_);
3221
3222 xlog_cil_force(log);
3223
3224 spin_lock(&log->l_icloglock);
3225 iclog = log->l_iclog;
3226 if (iclog->ic_state & XLOG_STATE_IOERROR)
3227 goto out_error;
3228
3229 if (iclog->ic_state == XLOG_STATE_DIRTY ||
3230 (iclog->ic_state == XLOG_STATE_ACTIVE &&
3231 atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) {
3232
3233
3234
3235
3236
3237
3238
3239
3240 iclog = iclog->ic_prev;
3241 if (iclog->ic_state == XLOG_STATE_ACTIVE ||
3242 iclog->ic_state == XLOG_STATE_DIRTY)
3243 goto out_unlock;
3244 } else if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3245 if (atomic_read(&iclog->ic_refcnt) == 0) {
3246
3247
3248
3249
3250
3251
3252
3253 atomic_inc(&iclog->ic_refcnt);
3254 lsn = be64_to_cpu(iclog->ic_header.h_lsn);
3255 xlog_state_switch_iclogs(log, iclog, 0);
3256 spin_unlock(&log->l_icloglock);
3257
3258 if (xlog_state_release_iclog(log, iclog))
3259 return -EIO;
3260
3261 spin_lock(&log->l_icloglock);
3262 if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn ||
3263 iclog->ic_state == XLOG_STATE_DIRTY)
3264 goto out_unlock;
3265 } else {
3266
3267
3268
3269
3270
3271
3272
3273 xlog_state_switch_iclogs(log, iclog, 0);
3274 }
3275 } else {
3276
3277
3278
3279
3280 ;
3281 }
3282
3283 if (!(flags & XFS_LOG_SYNC))
3284 goto out_unlock;
3285
3286 if (iclog->ic_state & XLOG_STATE_IOERROR)
3287 goto out_error;
3288 XFS_STATS_INC(mp, xs_log_force_sleep);
3289 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
3290 if (iclog->ic_state & XLOG_STATE_IOERROR)
3291 return -EIO;
3292 return 0;
3293
3294out_unlock:
3295 spin_unlock(&log->l_icloglock);
3296 return 0;
3297out_error:
3298 spin_unlock(&log->l_icloglock);
3299 return -EIO;
3300}
3301
3302static int
3303__xfs_log_force_lsn(
3304 struct xfs_mount *mp,
3305 xfs_lsn_t lsn,
3306 uint flags,
3307 int *log_flushed,
3308 bool already_slept)
3309{
3310 struct xlog *log = mp->m_log;
3311 struct xlog_in_core *iclog;
3312
3313 spin_lock(&log->l_icloglock);
3314 iclog = log->l_iclog;
3315 if (iclog->ic_state & XLOG_STATE_IOERROR)
3316 goto out_error;
3317
3318 while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
3319 iclog = iclog->ic_next;
3320 if (iclog == log->l_iclog)
3321 goto out_unlock;
3322 }
3323
3324 if (iclog->ic_state == XLOG_STATE_DIRTY)
3325 goto out_unlock;
3326
3327 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343 if (!already_slept &&
3344 (iclog->ic_prev->ic_state &
3345 (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
3346 ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
3347
3348 XFS_STATS_INC(mp, xs_log_force_sleep);
3349
3350 xlog_wait(&iclog->ic_prev->ic_write_wait,
3351 &log->l_icloglock);
3352 return -EAGAIN;
3353 }
3354 atomic_inc(&iclog->ic_refcnt);
3355 xlog_state_switch_iclogs(log, iclog, 0);
3356 spin_unlock(&log->l_icloglock);
3357 if (xlog_state_release_iclog(log, iclog))
3358 return -EIO;
3359 if (log_flushed)
3360 *log_flushed = 1;
3361 spin_lock(&log->l_icloglock);
3362 }
3363
3364 if (!(flags & XFS_LOG_SYNC) ||
3365 (iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY)))
3366 goto out_unlock;
3367
3368 if (iclog->ic_state & XLOG_STATE_IOERROR)
3369 goto out_error;
3370
3371 XFS_STATS_INC(mp, xs_log_force_sleep);
3372 xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
3373 if (iclog->ic_state & XLOG_STATE_IOERROR)
3374 return -EIO;
3375 return 0;
3376
3377out_unlock:
3378 spin_unlock(&log->l_icloglock);
3379 return 0;
3380out_error:
3381 spin_unlock(&log->l_icloglock);
3382 return -EIO;
3383}
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399int
3400xfs_log_force_lsn(
3401 struct xfs_mount *mp,
3402 xfs_lsn_t lsn,
3403 uint flags,
3404 int *log_flushed)
3405{
3406 int ret;
3407 ASSERT(lsn != 0);
3408
3409 XFS_STATS_INC(mp, xs_log_force);
3410 trace_xfs_log_force(mp, lsn, _RET_IP_);
3411
3412 lsn = xlog_cil_force_lsn(mp->m_log, lsn);
3413 if (lsn == NULLCOMMITLSN)
3414 return 0;
3415
3416 ret = __xfs_log_force_lsn(mp, lsn, flags, log_flushed, false);
3417 if (ret == -EAGAIN)
3418 ret = __xfs_log_force_lsn(mp, lsn, flags, log_flushed, true);
3419 return ret;
3420}
3421
3422
3423
3424
3425
3426STATIC void
3427xlog_state_want_sync(
3428 struct xlog *log,
3429 struct xlog_in_core *iclog)
3430{
3431 assert_spin_locked(&log->l_icloglock);
3432
3433 if (iclog->ic_state == XLOG_STATE_ACTIVE) {
3434 xlog_state_switch_iclogs(log, iclog, 0);
3435 } else {
3436 ASSERT(iclog->ic_state &
3437 (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
3438 }
3439}
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452void
3453xfs_log_ticket_put(
3454 xlog_ticket_t *ticket)
3455{
3456 ASSERT(atomic_read(&ticket->t_ref) > 0);
3457 if (atomic_dec_and_test(&ticket->t_ref))
3458 kmem_zone_free(xfs_log_ticket_zone, ticket);
3459}
3460
3461xlog_ticket_t *
3462xfs_log_ticket_get(
3463 xlog_ticket_t *ticket)
3464{
3465 ASSERT(atomic_read(&ticket->t_ref) > 0);
3466 atomic_inc(&ticket->t_ref);
3467 return ticket;
3468}
3469
3470
3471
3472
3473
3474int
3475xfs_log_calc_unit_res(
3476 struct xfs_mount *mp,
3477 int unit_bytes)
3478{
3479 struct xlog *log = mp->m_log;
3480 int iclog_space;
3481 uint num_headers;
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515 unit_bytes += sizeof(xlog_op_header_t);
3516 unit_bytes += sizeof(xfs_trans_header_t);
3517
3518
3519 unit_bytes += sizeof(xlog_op_header_t);
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
3539 num_headers = howmany(unit_bytes, iclog_space);
3540
3541
3542 unit_bytes += sizeof(xlog_op_header_t) * num_headers;
3543
3544
3545 while (!num_headers ||
3546 howmany(unit_bytes, iclog_space) > num_headers) {
3547 unit_bytes += sizeof(xlog_op_header_t);
3548 num_headers++;
3549 }
3550 unit_bytes += log->l_iclog_hsize * num_headers;
3551
3552
3553 unit_bytes += log->l_iclog_hsize;
3554
3555
3556 if (xfs_sb_version_haslogv2(&mp->m_sb) && mp->m_sb.sb_logsunit > 1) {
3557
3558 unit_bytes += 2 * mp->m_sb.sb_logsunit;
3559 } else {
3560
3561 unit_bytes += 2 * BBSIZE;
3562 }
3563
3564 return unit_bytes;
3565}
3566
3567
3568
3569
3570struct xlog_ticket *
3571xlog_ticket_alloc(
3572 struct xlog *log,
3573 int unit_bytes,
3574 int cnt,
3575 char client,
3576 bool permanent,
3577 xfs_km_flags_t alloc_flags)
3578{
3579 struct xlog_ticket *tic;
3580 int unit_res;
3581
3582 tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
3583 if (!tic)
3584 return NULL;
3585
3586 unit_res = xfs_log_calc_unit_res(log->l_mp, unit_bytes);
3587
3588 atomic_set(&tic->t_ref, 1);
3589 tic->t_task = current;
3590 INIT_LIST_HEAD(&tic->t_queue);
3591 tic->t_unit_res = unit_res;
3592 tic->t_curr_res = unit_res;
3593 tic->t_cnt = cnt;
3594 tic->t_ocnt = cnt;
3595 tic->t_tid = prandom_u32();
3596 tic->t_clientid = client;
3597 tic->t_flags = XLOG_TIC_INITED;
3598 if (permanent)
3599 tic->t_flags |= XLOG_TIC_PERM_RESERV;
3600
3601 xlog_tic_reset_res(tic);
3602
3603 return tic;
3604}
3605
3606
3607
3608
3609
3610
3611
3612
3613#if defined(DEBUG)
3614
3615
3616
3617
3618
3619STATIC void
3620xlog_verify_dest_ptr(
3621 struct xlog *log,
3622 void *ptr)
3623{
3624 int i;
3625 int good_ptr = 0;
3626
3627 for (i = 0; i < log->l_iclog_bufs; i++) {
3628 if (ptr >= log->l_iclog_bak[i] &&
3629 ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
3630 good_ptr++;
3631 }
3632
3633 if (!good_ptr)
3634 xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
3635}
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648STATIC void
3649xlog_verify_grant_tail(
3650 struct xlog *log)
3651{
3652 int tail_cycle, tail_blocks;
3653 int cycle, space;
3654
3655 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
3656 xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
3657 if (tail_cycle != cycle) {
3658 if (cycle - 1 != tail_cycle &&
3659 !(log->l_flags & XLOG_TAIL_WARN)) {
3660 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3661 "%s: cycle - 1 != tail_cycle", __func__);
3662 log->l_flags |= XLOG_TAIL_WARN;
3663 }
3664
3665 if (space > BBTOB(tail_blocks) &&
3666 !(log->l_flags & XLOG_TAIL_WARN)) {
3667 xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
3668 "%s: space > BBTOB(tail_blocks)", __func__);
3669 log->l_flags |= XLOG_TAIL_WARN;
3670 }
3671 }
3672}
3673
3674
3675STATIC void
3676xlog_verify_tail_lsn(
3677 struct xlog *log,
3678 struct xlog_in_core *iclog,
3679 xfs_lsn_t tail_lsn)
3680{
3681 int blocks;
3682
3683 if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
3684 blocks =
3685 log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
3686 if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
3687 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
3688 } else {
3689 ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
3690
3691 if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
3692 xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
3693
3694 blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
3695 if (blocks < BTOBB(iclog->ic_offset) + 1)
3696 xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
3697 }
3698}
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715STATIC void
3716xlog_verify_iclog(
3717 struct xlog *log,
3718 struct xlog_in_core *iclog,
3719 int count)
3720{
3721 xlog_op_header_t *ophead;
3722 xlog_in_core_t *icptr;
3723 xlog_in_core_2_t *xhdr;
3724 void *base_ptr, *ptr, *p;
3725 ptrdiff_t field_offset;
3726 uint8_t clientid;
3727 int len, i, j, k, op_len;
3728 int idx;
3729
3730
3731 spin_lock(&log->l_icloglock);
3732 icptr = log->l_iclog;
3733 for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next)
3734 ASSERT(icptr);
3735
3736 if (icptr != log->l_iclog)
3737 xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
3738 spin_unlock(&log->l_icloglock);
3739
3740
3741 if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
3742 xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
3743
3744 base_ptr = ptr = &iclog->ic_header;
3745 p = &iclog->ic_header;
3746 for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) {
3747 if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
3748 xfs_emerg(log->l_mp, "%s: unexpected magic num",
3749 __func__);
3750 }
3751
3752
3753 len = be32_to_cpu(iclog->ic_header.h_num_logops);
3754 base_ptr = ptr = iclog->ic_datap;
3755 ophead = ptr;
3756 xhdr = iclog->ic_data;
3757 for (i = 0; i < len; i++) {
3758 ophead = ptr;
3759
3760
3761 p = &ophead->oh_clientid;
3762 field_offset = p - base_ptr;
3763 if (field_offset & 0x1ff) {
3764 clientid = ophead->oh_clientid;
3765 } else {
3766 idx = BTOBBT((char *)&ophead->oh_clientid - iclog->ic_datap);
3767 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3768 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3769 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3770 clientid = xlog_get_client_id(
3771 xhdr[j].hic_xheader.xh_cycle_data[k]);
3772 } else {
3773 clientid = xlog_get_client_id(
3774 iclog->ic_header.h_cycle_data[idx]);
3775 }
3776 }
3777 if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
3778 xfs_warn(log->l_mp,
3779 "%s: invalid clientid %d op "PTR_FMT" offset 0x%lx",
3780 __func__, clientid, ophead,
3781 (unsigned long)field_offset);
3782
3783
3784 p = &ophead->oh_len;
3785 field_offset = p - base_ptr;
3786 if (field_offset & 0x1ff) {
3787 op_len = be32_to_cpu(ophead->oh_len);
3788 } else {
3789 idx = BTOBBT((uintptr_t)&ophead->oh_len -
3790 (uintptr_t)iclog->ic_datap);
3791 if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
3792 j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3793 k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3794 op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
3795 } else {
3796 op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
3797 }
3798 }
3799 ptr += sizeof(xlog_op_header_t) + op_len;
3800 }
3801}
3802#endif
3803
3804
3805
3806
3807STATIC int
3808xlog_state_ioerror(
3809 struct xlog *log)
3810{
3811 xlog_in_core_t *iclog, *ic;
3812
3813 iclog = log->l_iclog;
3814 if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
3815
3816
3817
3818
3819 ic = iclog;
3820 do {
3821 ic->ic_state = XLOG_STATE_IOERROR;
3822 ic = ic->ic_next;
3823 } while (ic != iclog);
3824 return 0;
3825 }
3826
3827
3828
3829 return 1;
3830}
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848int
3849xfs_log_force_umount(
3850 struct xfs_mount *mp,
3851 int logerror)
3852{
3853 struct xlog *log;
3854 int retval;
3855
3856 log = mp->m_log;
3857
3858
3859
3860
3861
3862 if (!log ||
3863 log->l_flags & XLOG_ACTIVE_RECOVERY) {
3864 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3865 if (mp->m_sb_bp)
3866 mp->m_sb_bp->b_flags |= XBF_DONE;
3867 return 0;
3868 }
3869
3870
3871
3872
3873
3874 if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
3875 ASSERT(XLOG_FORCED_SHUTDOWN(log));
3876 return 1;
3877 }
3878
3879
3880
3881
3882
3883
3884
3885
3886 if (!logerror)
3887 xfs_log_force(mp, XFS_LOG_SYNC);
3888
3889
3890
3891
3892
3893 spin_lock(&log->l_icloglock);
3894 mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
3895 if (mp->m_sb_bp)
3896 mp->m_sb_bp->b_flags |= XBF_DONE;
3897
3898
3899
3900
3901
3902 log->l_flags |= XLOG_IO_ERROR;
3903 retval = xlog_state_ioerror(log);
3904 spin_unlock(&log->l_icloglock);
3905
3906
3907
3908
3909
3910
3911
3912
3913 xlog_grant_head_wake_all(&log->l_reserve_head);
3914 xlog_grant_head_wake_all(&log->l_write_head);
3915
3916
3917
3918
3919
3920
3921
3922 wake_up_all(&log->l_cilp->xc_commit_wait);
3923 xlog_state_do_callback(log, true, NULL);
3924
3925#ifdef XFSERRORDEBUG
3926 {
3927 xlog_in_core_t *iclog;
3928
3929 spin_lock(&log->l_icloglock);
3930 iclog = log->l_iclog;
3931 do {
3932 ASSERT(iclog->ic_callback == 0);
3933 iclog = iclog->ic_next;
3934 } while (iclog != log->l_iclog);
3935 spin_unlock(&log->l_icloglock);
3936 }
3937#endif
3938
3939 return retval;
3940}
3941
3942STATIC int
3943xlog_iclogs_empty(
3944 struct xlog *log)
3945{
3946 xlog_in_core_t *iclog;
3947
3948 iclog = log->l_iclog;
3949 do {
3950
3951
3952
3953 if (iclog->ic_header.h_num_logops)
3954 return 0;
3955 iclog = iclog->ic_next;
3956 } while (iclog != log->l_iclog);
3957 return 1;
3958}
3959
3960
3961
3962
3963
3964bool
3965xfs_log_check_lsn(
3966 struct xfs_mount *mp,
3967 xfs_lsn_t lsn)
3968{
3969 struct xlog *log = mp->m_log;
3970 bool valid;
3971
3972
3973
3974
3975
3976
3977 if (mp->m_flags & XFS_MOUNT_NORECOVERY)
3978 return true;
3979
3980
3981
3982
3983
3984 if (lsn == NULLCOMMITLSN)
3985 return true;
3986
3987 valid = xlog_valid_lsn(mp->m_log, lsn);
3988
3989
3990 if (!valid) {
3991 spin_lock(&log->l_icloglock);
3992 xfs_warn(mp,
3993"Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). "
3994"Please unmount and run xfs_repair (>= v4.3) to resolve.",
3995 CYCLE_LSN(lsn), BLOCK_LSN(lsn),
3996 log->l_curr_cycle, log->l_curr_block);
3997 spin_unlock(&log->l_icloglock);
3998 }
3999
4000 return valid;
4001}
4002
4003bool
4004xfs_log_in_recovery(
4005 struct xfs_mount *mp)
4006{
4007 struct xlog *log = mp->m_log;
4008
4009 return log->l_flags & XLOG_ACTIVE_RECOVERY;
4010}
4011