1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef _LINUX_JBD2_H
17#define _LINUX_JBD2_H
18
19
20#ifndef __KERNEL__
21#include "jfs_compat.h"
22#define JBD2_DEBUG
23#define jfs_debug jbd_debug
24#else
25
26#include <linux/types.h>
27#include <linux/buffer_head.h>
28#include <linux/journal-head.h>
29#include <linux/stddef.h>
30#include <linux/bit_spinlock.h>
31#include <linux/mutex.h>
32#include <linux/timer.h>
33
34#include <asm/semaphore.h>
35#endif
36
37#define journal_oom_retry 1
38
39
40
41
42
43
44
45
46#undef JBD2_PARANOID_IOFAIL
47
48
49
50
51#define JBD2_DEFAULT_MAX_COMMIT_AGE 5
52
53#ifdef CONFIG_JBD2_DEBUG
54
55
56
57
58
59#define JBD2_EXPENSIVE_CHECKING
60extern u8 jbd2_journal_enable_debug;
61
62#define jbd_debug(n, f, a...) \
63 do { \
64 if ((n) <= jbd2_journal_enable_debug) { \
65 printk (KERN_DEBUG "(%s, %d): %s: ", \
66 __FILE__, __LINE__, __FUNCTION__); \
67 printk (f, ## a); \
68 } \
69 } while (0)
70#else
71#define jbd_debug(f, a...)
72#endif
73
74static inline void *jbd2_alloc(size_t size, gfp_t flags)
75{
76 return (void *)__get_free_pages(flags, get_order(size));
77}
78
79static inline void jbd2_free(void *ptr, size_t size)
80{
81 free_pages((unsigned long)ptr, get_order(size));
82};
83
84#define JBD2_MIN_JOURNAL_BLOCKS 1024
85
86#ifdef __KERNEL__
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105typedef struct handle_s handle_t;
106
107
108
109
110
111
112
113
114
115
116
117
118
119typedef struct journal_s journal_t;
120#endif
121
122
123
124
125
126#define JBD2_MAGIC_NUMBER 0xc03b3998U
127
128
129
130
131
132
133
134
135
136#define JBD2_DESCRIPTOR_BLOCK 1
137#define JBD2_COMMIT_BLOCK 2
138#define JBD2_SUPERBLOCK_V1 3
139#define JBD2_SUPERBLOCK_V2 4
140#define JBD2_REVOKE_BLOCK 5
141
142
143
144
145typedef struct journal_header_s
146{
147 __be32 h_magic;
148 __be32 h_blocktype;
149 __be32 h_sequence;
150} journal_header_t;
151
152
153
154
155
156
157
158
159typedef struct journal_block_tag_s
160{
161 __be32 t_blocknr;
162 __be32 t_flags;
163 __be32 t_blocknr_high;
164} journal_block_tag_t;
165
166#define JBD2_TAG_SIZE32 (offsetof(journal_block_tag_t, t_blocknr_high))
167#define JBD2_TAG_SIZE64 (sizeof(journal_block_tag_t))
168
169
170
171
172
173typedef struct jbd2_journal_revoke_header_s
174{
175 journal_header_t r_header;
176 __be32 r_count;
177} jbd2_journal_revoke_header_t;
178
179
180
181#define JBD2_FLAG_ESCAPE 1
182#define JBD2_FLAG_SAME_UUID 2
183#define JBD2_FLAG_DELETED 4
184#define JBD2_FLAG_LAST_TAG 8
185
186
187
188
189
190typedef struct journal_superblock_s
191{
192
193 journal_header_t s_header;
194
195
196
197 __be32 s_blocksize;
198 __be32 s_maxlen;
199 __be32 s_first;
200
201
202
203 __be32 s_sequence;
204 __be32 s_start;
205
206
207
208 __be32 s_errno;
209
210
211
212 __be32 s_feature_compat;
213 __be32 s_feature_incompat;
214 __be32 s_feature_ro_compat;
215
216 __u8 s_uuid[16];
217
218
219 __be32 s_nr_users;
220
221 __be32 s_dynsuper;
222
223
224 __be32 s_max_transaction;
225 __be32 s_max_trans_data;
226
227
228 __u32 s_padding[44];
229
230
231 __u8 s_users[16*48];
232
233} journal_superblock_t;
234
235#define JBD2_HAS_COMPAT_FEATURE(j,mask) \
236 ((j)->j_format_version >= 2 && \
237 ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask))))
238#define JBD2_HAS_RO_COMPAT_FEATURE(j,mask) \
239 ((j)->j_format_version >= 2 && \
240 ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask))))
241#define JBD2_HAS_INCOMPAT_FEATURE(j,mask) \
242 ((j)->j_format_version >= 2 && \
243 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
244
245#define JBD2_FEATURE_INCOMPAT_REVOKE 0x00000001
246#define JBD2_FEATURE_INCOMPAT_64BIT 0x00000002
247
248
249#define JBD2_KNOWN_COMPAT_FEATURES 0
250#define JBD2_KNOWN_ROCOMPAT_FEATURES 0
251#define JBD2_KNOWN_INCOMPAT_FEATURES (JBD2_FEATURE_INCOMPAT_REVOKE | \
252 JBD2_FEATURE_INCOMPAT_64BIT)
253
254#ifdef __KERNEL__
255
256#include <linux/fs.h>
257#include <linux/sched.h>
258
259#define JBD2_ASSERTIONS
260#ifdef JBD2_ASSERTIONS
261#define J_ASSERT(assert) \
262do { \
263 if (!(assert)) { \
264 printk (KERN_EMERG \
265 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
266 __FUNCTION__, __FILE__, __LINE__, # assert); \
267 BUG(); \
268 } \
269} while (0)
270
271#if defined(CONFIG_BUFFER_DEBUG)
272void buffer_assertion_failure(struct buffer_head *bh);
273#define J_ASSERT_BH(bh, expr) \
274 do { \
275 if (!(expr)) \
276 buffer_assertion_failure(bh); \
277 J_ASSERT(expr); \
278 } while (0)
279#define J_ASSERT_JH(jh, expr) J_ASSERT_BH(jh2bh(jh), expr)
280#else
281#define J_ASSERT_BH(bh, expr) J_ASSERT(expr)
282#define J_ASSERT_JH(jh, expr) J_ASSERT(expr)
283#endif
284
285#else
286#define J_ASSERT(assert) do { } while (0)
287#endif
288
289#if defined(JBD2_PARANOID_IOFAIL)
290#define J_EXPECT(expr, why...) J_ASSERT(expr)
291#define J_EXPECT_BH(bh, expr, why...) J_ASSERT_BH(bh, expr)
292#define J_EXPECT_JH(jh, expr, why...) J_ASSERT_JH(jh, expr)
293#else
294#define __journal_expect(expr, why...) \
295 ({ \
296 int val = (expr); \
297 if (!val) { \
298 printk(KERN_ERR \
299 "EXT3-fs unexpected failure: %s;\n",# expr); \
300 printk(KERN_ERR why "\n"); \
301 } \
302 val; \
303 })
304#define J_EXPECT(expr, why...) __journal_expect(expr, ## why)
305#define J_EXPECT_BH(bh, expr, why...) __journal_expect(expr, ## why)
306#define J_EXPECT_JH(jh, expr, why...) __journal_expect(expr, ## why)
307#endif
308
309enum jbd_state_bits {
310 BH_JBD
311 = BH_PrivateStart,
312 BH_JWrite,
313 BH_Freed,
314 BH_Revoked,
315 BH_RevokeValid,
316 BH_JBDDirty,
317 BH_State,
318 BH_JournalHead,
319 BH_Unshadow,
320};
321
322BUFFER_FNS(JBD, jbd)
323BUFFER_FNS(JWrite, jwrite)
324BUFFER_FNS(JBDDirty, jbddirty)
325TAS_BUFFER_FNS(JBDDirty, jbddirty)
326BUFFER_FNS(Revoked, revoked)
327TAS_BUFFER_FNS(Revoked, revoked)
328BUFFER_FNS(RevokeValid, revokevalid)
329TAS_BUFFER_FNS(RevokeValid, revokevalid)
330BUFFER_FNS(Freed, freed)
331
332static inline struct buffer_head *jh2bh(struct journal_head *jh)
333{
334 return jh->b_bh;
335}
336
337static inline struct journal_head *bh2jh(struct buffer_head *bh)
338{
339 return bh->b_private;
340}
341
342static inline void jbd_lock_bh_state(struct buffer_head *bh)
343{
344 bit_spin_lock(BH_State, &bh->b_state);
345}
346
347static inline int jbd_trylock_bh_state(struct buffer_head *bh)
348{
349 return bit_spin_trylock(BH_State, &bh->b_state);
350}
351
352static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
353{
354 return bit_spin_is_locked(BH_State, &bh->b_state);
355}
356
357static inline void jbd_unlock_bh_state(struct buffer_head *bh)
358{
359 bit_spin_unlock(BH_State, &bh->b_state);
360}
361
362static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
363{
364 bit_spin_lock(BH_JournalHead, &bh->b_state);
365}
366
367static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
368{
369 bit_spin_unlock(BH_JournalHead, &bh->b_state);
370}
371
372struct jbd2_revoke_table_s;
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390struct handle_s
391{
392
393 transaction_t *h_transaction;
394
395
396 int h_buffer_credits;
397
398
399 int h_ref;
400
401
402
403 int h_err;
404
405
406 unsigned int h_sync: 1;
407 unsigned int h_jdata: 1;
408 unsigned int h_aborted: 1;
409};
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448struct transaction_s
449{
450
451 journal_t *t_journal;
452
453
454 tid_t t_tid;
455
456
457
458
459
460
461
462 enum {
463 T_RUNNING,
464 T_LOCKED,
465 T_RUNDOWN,
466 T_FLUSH,
467 T_COMMIT,
468 T_FINISHED
469 } t_state;
470
471
472
473
474 unsigned long t_log_start;
475
476
477 int t_nr_buffers;
478
479
480
481
482
483 struct journal_head *t_reserved_list;
484
485
486
487
488
489 struct journal_head *t_locked_list;
490
491
492
493
494
495 struct journal_head *t_buffers;
496
497
498
499
500
501 struct journal_head *t_sync_datalist;
502
503
504
505
506
507
508 struct journal_head *t_forget;
509
510
511
512
513
514 struct journal_head *t_checkpoint_list;
515
516
517
518
519
520 struct journal_head *t_checkpoint_io_list;
521
522
523
524
525
526 struct journal_head *t_iobuf_list;
527
528
529
530
531
532
533 struct journal_head *t_shadow_list;
534
535
536
537
538
539 struct journal_head *t_log_list;
540
541
542
543
544 spinlock_t t_handle_lock;
545
546
547
548
549
550 int t_updates;
551
552
553
554
555
556 int t_outstanding_credits;
557
558
559
560
561
562 transaction_t *t_cpnext, *t_cpprev;
563
564
565
566
567
568 unsigned long t_expires;
569
570
571
572
573 int t_handle_count;
574
575};
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641struct journal_s
642{
643
644 unsigned long j_flags;
645
646
647
648
649
650 int j_errno;
651
652
653 struct buffer_head *j_sb_buffer;
654 journal_superblock_t *j_superblock;
655
656
657 int j_format_version;
658
659
660
661
662 spinlock_t j_state_lock;
663
664
665
666
667 int j_barrier_count;
668
669
670 struct mutex j_barrier;
671
672
673
674
675
676 transaction_t *j_running_transaction;
677
678
679
680
681
682 transaction_t *j_committing_transaction;
683
684
685
686
687
688 transaction_t *j_checkpoint_transactions;
689
690
691
692
693
694 wait_queue_head_t j_wait_transaction_locked;
695
696
697 wait_queue_head_t j_wait_logspace;
698
699
700 wait_queue_head_t j_wait_done_commit;
701
702
703 wait_queue_head_t j_wait_checkpoint;
704
705
706 wait_queue_head_t j_wait_commit;
707
708
709 wait_queue_head_t j_wait_updates;
710
711
712 struct mutex j_checkpoint_mutex;
713
714
715
716
717
718 unsigned long j_head;
719
720
721
722
723
724 unsigned long j_tail;
725
726
727
728
729
730 unsigned long j_free;
731
732
733
734
735
736 unsigned long j_first;
737 unsigned long j_last;
738
739
740
741
742
743 struct block_device *j_dev;
744 int j_blocksize;
745 unsigned long long j_blk_offset;
746
747
748
749
750
751 struct block_device *j_fs_dev;
752
753
754 unsigned int j_maxlen;
755
756
757
758
759 spinlock_t j_list_lock;
760
761
762
763
764 struct inode *j_inode;
765
766
767
768
769 tid_t j_tail_sequence;
770
771
772
773
774 tid_t j_transaction_sequence;
775
776
777
778
779
780 tid_t j_commit_sequence;
781
782
783
784
785
786 tid_t j_commit_request;
787
788
789
790
791
792
793
794 __u8 j_uuid[16];
795
796
797 struct task_struct *j_task;
798
799
800
801
802
803 int j_max_transaction_buffers;
804
805
806
807
808 unsigned long j_commit_interval;
809
810
811 struct timer_list j_commit_timer;
812
813
814
815
816
817 spinlock_t j_revoke_lock;
818 struct jbd2_revoke_table_s *j_revoke;
819 struct jbd2_revoke_table_s *j_revoke_table[2];
820
821
822
823
824 struct buffer_head **j_wbuf;
825 int j_wbufsize;
826
827 pid_t j_last_sync_writer;
828
829
830
831
832
833 void *j_private;
834};
835
836
837
838
839#define JBD2_UNMOUNT 0x001
840#define JBD2_ABORT 0x002
841#define JBD2_ACK_ERR 0x004
842#define JBD2_FLUSHED 0x008
843#define JBD2_LOADED 0x010
844#define JBD2_BARRIER 0x020
845
846
847
848
849
850
851
852extern void jbd2_journal_unfile_buffer(journal_t *, struct journal_head *);
853extern void __jbd2_journal_unfile_buffer(struct journal_head *);
854extern void __jbd2_journal_refile_buffer(struct journal_head *);
855extern void jbd2_journal_refile_buffer(journal_t *, struct journal_head *);
856extern void __jbd2_journal_file_buffer(struct journal_head *, transaction_t *, int);
857extern void __journal_free_buffer(struct journal_head *bh);
858extern void jbd2_journal_file_buffer(struct journal_head *, transaction_t *, int);
859extern void __journal_clean_data_list(transaction_t *transaction);
860
861
862extern struct journal_head * jbd2_journal_get_descriptor_buffer(journal_t *);
863int jbd2_journal_next_log_block(journal_t *, unsigned long long *);
864
865
866extern void jbd2_journal_commit_transaction(journal_t *);
867
868
869int __jbd2_journal_clean_checkpoint_list(journal_t *journal);
870int __jbd2_journal_remove_checkpoint(struct journal_head *);
871void __jbd2_journal_insert_checkpoint(struct journal_head *, transaction_t *);
872
873
874extern int
875jbd2_journal_write_metadata_buffer(transaction_t *transaction,
876 struct journal_head *jh_in,
877 struct journal_head **jh_out,
878 unsigned long long blocknr);
879
880
881extern void __wait_on_journal (journal_t *);
882
883
884
885
886
887
888
889
890
891
892
893
894static inline handle_t *journal_current_handle(void)
895{
896 return current->journal_info;
897}
898
899
900
901
902
903
904
905extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
906extern int jbd2_journal_restart (handle_t *, int nblocks);
907extern int jbd2_journal_extend (handle_t *, int nblocks);
908extern int jbd2_journal_get_write_access(handle_t *, struct buffer_head *);
909extern int jbd2_journal_get_create_access (handle_t *, struct buffer_head *);
910extern int jbd2_journal_get_undo_access(handle_t *, struct buffer_head *);
911extern int jbd2_journal_dirty_data (handle_t *, struct buffer_head *);
912extern int jbd2_journal_dirty_metadata (handle_t *, struct buffer_head *);
913extern void jbd2_journal_release_buffer (handle_t *, struct buffer_head *);
914extern int jbd2_journal_forget (handle_t *, struct buffer_head *);
915extern void journal_sync_buffer (struct buffer_head *);
916extern void jbd2_journal_invalidatepage(journal_t *,
917 struct page *, unsigned long);
918extern int jbd2_journal_try_to_free_buffers(journal_t *, struct page *, gfp_t);
919extern int jbd2_journal_stop(handle_t *);
920extern int jbd2_journal_flush (journal_t *);
921extern void jbd2_journal_lock_updates (journal_t *);
922extern void jbd2_journal_unlock_updates (journal_t *);
923
924extern journal_t * jbd2_journal_init_dev(struct block_device *bdev,
925 struct block_device *fs_dev,
926 unsigned long long start, int len, int bsize);
927extern journal_t * jbd2_journal_init_inode (struct inode *);
928extern int jbd2_journal_update_format (journal_t *);
929extern int jbd2_journal_check_used_features
930 (journal_t *, unsigned long, unsigned long, unsigned long);
931extern int jbd2_journal_check_available_features
932 (journal_t *, unsigned long, unsigned long, unsigned long);
933extern int jbd2_journal_set_features
934 (journal_t *, unsigned long, unsigned long, unsigned long);
935extern int jbd2_journal_create (journal_t *);
936extern int jbd2_journal_load (journal_t *journal);
937extern void jbd2_journal_destroy (journal_t *);
938extern int jbd2_journal_recover (journal_t *journal);
939extern int jbd2_journal_wipe (journal_t *, int);
940extern int jbd2_journal_skip_recovery (journal_t *);
941extern void jbd2_journal_update_superblock (journal_t *, int);
942extern void __jbd2_journal_abort_hard (journal_t *);
943extern void jbd2_journal_abort (journal_t *, int);
944extern int jbd2_journal_errno (journal_t *);
945extern void jbd2_journal_ack_err (journal_t *);
946extern int jbd2_journal_clear_err (journal_t *);
947extern int jbd2_journal_bmap(journal_t *, unsigned long, unsigned long long *);
948extern int jbd2_journal_force_commit(journal_t *);
949
950
951
952
953struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh);
954struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh);
955void jbd2_journal_remove_journal_head(struct buffer_head *bh);
956void jbd2_journal_put_journal_head(struct journal_head *jh);
957
958
959
960
961extern struct kmem_cache *jbd2_handle_cache;
962
963static inline handle_t *jbd2_alloc_handle(gfp_t gfp_flags)
964{
965 return kmem_cache_alloc(jbd2_handle_cache, gfp_flags);
966}
967
968static inline void jbd2_free_handle(handle_t *handle)
969{
970 kmem_cache_free(jbd2_handle_cache, handle);
971}
972
973
974#define JOURNAL_REVOKE_DEFAULT_HASH 256
975extern int jbd2_journal_init_revoke(journal_t *, int);
976extern void jbd2_journal_destroy_revoke_caches(void);
977extern int jbd2_journal_init_revoke_caches(void);
978
979extern void jbd2_journal_destroy_revoke(journal_t *);
980extern int jbd2_journal_revoke (handle_t *, unsigned long long, struct buffer_head *);
981extern int jbd2_journal_cancel_revoke(handle_t *, struct journal_head *);
982extern void jbd2_journal_write_revoke_records(journal_t *, transaction_t *);
983
984
985extern int jbd2_journal_set_revoke(journal_t *, unsigned long long, tid_t);
986extern int jbd2_journal_test_revoke(journal_t *, unsigned long long, tid_t);
987extern void jbd2_journal_clear_revoke(journal_t *);
988extern void jbd2_journal_switch_revoke_table(journal_t *journal);
989
990
991
992
993
994
995
996
997int __jbd2_log_space_left(journal_t *);
998int jbd2_log_start_commit(journal_t *journal, tid_t tid);
999int __jbd2_log_start_commit(journal_t *journal, tid_t tid);
1000int jbd2_journal_start_commit(journal_t *journal, tid_t *tid);
1001int jbd2_journal_force_commit_nested(journal_t *journal);
1002int jbd2_log_wait_commit(journal_t *journal, tid_t tid);
1003int jbd2_log_do_checkpoint(journal_t *journal);
1004
1005void __jbd2_log_wait_for_space(journal_t *journal);
1006extern void __jbd2_journal_drop_transaction(journal_t *, transaction_t *);
1007extern int jbd2_cleanup_journal_tail(journal_t *);
1008
1009
1010
1011#define jbd_ENOSYS() \
1012do { \
1013 printk (KERN_ERR "JBD unimplemented function %s\n", __FUNCTION__); \
1014 current->state = TASK_UNINTERRUPTIBLE; \
1015 schedule(); \
1016} while (1)
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028static inline int is_journal_aborted(journal_t *journal)
1029{
1030 return journal->j_flags & JBD2_ABORT;
1031}
1032
1033static inline int is_handle_aborted(handle_t *handle)
1034{
1035 if (handle->h_aborted)
1036 return 1;
1037 return is_journal_aborted(handle->h_transaction->t_journal);
1038}
1039
1040static inline void jbd2_journal_abort_handle(handle_t *handle)
1041{
1042 handle->h_aborted = 1;
1043}
1044
1045#endif
1046
1047
1048
1049
1050static inline int tid_gt(tid_t x, tid_t y)
1051{
1052 int difference = (x - y);
1053 return (difference > 0);
1054}
1055
1056static inline int tid_geq(tid_t x, tid_t y)
1057{
1058 int difference = (x - y);
1059 return (difference >= 0);
1060}
1061
1062extern int jbd2_journal_blocks_per_page(struct inode *inode);
1063extern size_t journal_tag_bytes(journal_t *journal);
1064
1065
1066
1067
1068
1069static inline int jbd_space_needed(journal_t *journal)
1070{
1071 int nblocks = journal->j_max_transaction_buffers;
1072 if (journal->j_committing_transaction)
1073 nblocks += journal->j_committing_transaction->
1074 t_outstanding_credits;
1075 return nblocks;
1076}
1077
1078
1079
1080
1081
1082
1083#define BJ_None 0
1084#define BJ_SyncData 1
1085#define BJ_Metadata 2
1086#define BJ_Forget 3
1087#define BJ_IO 4
1088#define BJ_Shadow 5
1089#define BJ_LogCtl 6
1090#define BJ_Reserved 7
1091#define BJ_Locked 8
1092#define BJ_Types 9
1093
1094extern int jbd_blocks_per_page(struct inode *inode);
1095
1096#ifdef __KERNEL__
1097
1098#define buffer_trace_init(bh) do {} while (0)
1099#define print_buffer_fields(bh) do {} while (0)
1100#define print_buffer_trace(bh) do {} while (0)
1101#define BUFFER_TRACE(bh, info) do {} while (0)
1102#define BUFFER_TRACE2(bh, bh2, info) do {} while (0)
1103#define JBUFFER_TRACE(jh, info) do {} while (0)
1104
1105#endif
1106
1107#endif
1108