forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 8
/
journal.c
4399 lines (4019 loc) · 120 KB
/
journal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Write ahead logging implementation copyright Chris Mason 2000
*
* The background commits make this code very interrelated, and
* overly complex. I need to rethink things a bit....The major players:
*
* journal_begin -- call with the number of blocks you expect to log.
* If the current transaction is too
* old, it will block until the current transaction is
* finished, and then start a new one.
* Usually, your transaction will get joined in with
* previous ones for speed.
*
* journal_join -- same as journal_begin, but won't block on the current
* transaction regardless of age. Don't ever call
* this. Ever. There are only two places it should be
* called from, and they are both inside this file.
*
* journal_mark_dirty -- adds blocks into this transaction. clears any flags
* that might make them get sent to disk
* and then marks them BH_JDirty. Puts the buffer head
* into the current transaction hash.
*
* journal_end -- if the current transaction is batchable, it does nothing
* otherwise, it could do an async/synchronous commit, or
* a full flush of all log and real blocks in the
* transaction.
*
* flush_old_commits -- if the current transaction is too old, it is ended and
* commit blocks are sent to disk. Forces commit blocks
* to disk for all backgrounded commits that have been
* around too long.
* -- Note, if you call this as an immediate flush from
* from within kupdate, it will ignore the immediate flag
*/
#include <linux/time.h>
#include <linux/semaphore.h>
#include <linux/vmalloc.h>
#include "reiserfs.h"
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/buffer_head.h>
#include <linux/workqueue.h>
#include <linux/writeback.h>
#include <linux/blkdev.h>
#include <linux/backing-dev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
/* gets a struct reiserfs_journal_list * from a list head */
#define JOURNAL_LIST_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
j_list))
#define JOURNAL_WORK_ENTRY(h) (list_entry((h), struct reiserfs_journal_list, \
j_working_list))
/* must be correct to keep the desc and commit structs at 4k */
#define JOURNAL_TRANS_HALF 1018
#define BUFNR 64 /*read ahead */
/* cnode stat bits. Move these into reiserfs_fs.h */
/* this block was freed, and can't be written. */
#define BLOCK_FREED 2
/* this block was freed during this transaction, and can't be written */
#define BLOCK_FREED_HOLDER 3
/* used in flush_journal_list */
#define BLOCK_NEEDS_FLUSH 4
#define BLOCK_DIRTIED 5
/* journal list state bits */
#define LIST_TOUCHED 1
#define LIST_DIRTY 2
#define LIST_COMMIT_PENDING 4 /* someone will commit this list */
/* flags for do_journal_end */
#define FLUSH_ALL 1 /* flush commit and real blocks */
#define COMMIT_NOW 2 /* end and commit this transaction */
#define WAIT 4 /* wait for the log blocks to hit the disk */
static int do_journal_end(struct reiserfs_transaction_handle *, int flags);
static int flush_journal_list(struct super_block *s,
struct reiserfs_journal_list *jl, int flushall);
static int flush_commit_list(struct super_block *s,
struct reiserfs_journal_list *jl, int flushall);
static int can_dirty(struct reiserfs_journal_cnode *cn);
static int journal_join(struct reiserfs_transaction_handle *th,
struct super_block *sb);
static void release_journal_dev(struct super_block *super,
struct reiserfs_journal *journal);
static int dirty_one_transaction(struct super_block *s,
struct reiserfs_journal_list *jl);
static void flush_async_commits(struct work_struct *work);
static void queue_log_writer(struct super_block *s);
/* values for join in do_journal_begin_r */
enum {
JBEGIN_REG = 0, /* regular journal begin */
/* join the running transaction if at all possible */
JBEGIN_JOIN = 1,
/* called from cleanup code, ignores aborted flag */
JBEGIN_ABORT = 2,
};
static int do_journal_begin_r(struct reiserfs_transaction_handle *th,
struct super_block *sb,
unsigned long nblocks, int join);
static void init_journal_hash(struct super_block *sb)
{
struct reiserfs_journal *journal = SB_JOURNAL(sb);
memset(journal->j_hash_table, 0,
JOURNAL_HASH_SIZE * sizeof(struct reiserfs_journal_cnode *));
}
/*
* clears BH_Dirty and sticks the buffer on the clean list. Called because
* I can't allow refile_buffer to make schedule happen after I've freed a
* block. Look at remove_from_transaction and journal_mark_freed for
* more details.
*/
static int reiserfs_clean_and_file_buffer(struct buffer_head *bh)
{
if (bh) {
clear_buffer_dirty(bh);
clear_buffer_journal_test(bh);
}
return 0;
}
static struct reiserfs_bitmap_node *allocate_bitmap_node(struct super_block
*sb)
{
struct reiserfs_bitmap_node *bn;
static int id;
bn = kmalloc(sizeof(struct reiserfs_bitmap_node), GFP_NOFS);
if (!bn) {
return NULL;
}
bn->data = kzalloc(sb->s_blocksize, GFP_NOFS);
if (!bn->data) {
kfree(bn);
return NULL;
}
bn->id = id++;
INIT_LIST_HEAD(&bn->list);
return bn;
}
static struct reiserfs_bitmap_node *get_bitmap_node(struct super_block *sb)
{
struct reiserfs_journal *journal = SB_JOURNAL(sb);
struct reiserfs_bitmap_node *bn = NULL;
struct list_head *entry = journal->j_bitmap_nodes.next;
journal->j_used_bitmap_nodes++;
repeat:
if (entry != &journal->j_bitmap_nodes) {
bn = list_entry(entry, struct reiserfs_bitmap_node, list);
list_del(entry);
memset(bn->data, 0, sb->s_blocksize);
journal->j_free_bitmap_nodes--;
return bn;
}
bn = allocate_bitmap_node(sb);
if (!bn) {
yield();
goto repeat;
}
return bn;
}
static inline void free_bitmap_node(struct super_block *sb,
struct reiserfs_bitmap_node *bn)
{
struct reiserfs_journal *journal = SB_JOURNAL(sb);
journal->j_used_bitmap_nodes--;
if (journal->j_free_bitmap_nodes > REISERFS_MAX_BITMAP_NODES) {
kfree(bn->data);
kfree(bn);
} else {
list_add(&bn->list, &journal->j_bitmap_nodes);
journal->j_free_bitmap_nodes++;
}
}
static void allocate_bitmap_nodes(struct super_block *sb)
{
int i;
struct reiserfs_journal *journal = SB_JOURNAL(sb);
struct reiserfs_bitmap_node *bn = NULL;
for (i = 0; i < REISERFS_MIN_BITMAP_NODES; i++) {
bn = allocate_bitmap_node(sb);
if (bn) {
list_add(&bn->list, &journal->j_bitmap_nodes);
journal->j_free_bitmap_nodes++;
} else {
/* this is ok, we'll try again when more are needed */
break;
}
}
}
static int set_bit_in_list_bitmap(struct super_block *sb,
b_blocknr_t block,
struct reiserfs_list_bitmap *jb)
{
unsigned int bmap_nr = block / (sb->s_blocksize << 3);
unsigned int bit_nr = block % (sb->s_blocksize << 3);
if (!jb->bitmaps[bmap_nr]) {
jb->bitmaps[bmap_nr] = get_bitmap_node(sb);
}
set_bit(bit_nr, (unsigned long *)jb->bitmaps[bmap_nr]->data);
return 0;
}
static void cleanup_bitmap_list(struct super_block *sb,
struct reiserfs_list_bitmap *jb)
{
int i;
if (jb->bitmaps == NULL)
return;
for (i = 0; i < reiserfs_bmap_count(sb); i++) {
if (jb->bitmaps[i]) {
free_bitmap_node(sb, jb->bitmaps[i]);
jb->bitmaps[i] = NULL;
}
}
}
/*
* only call this on FS unmount.
*/
static int free_list_bitmaps(struct super_block *sb,
struct reiserfs_list_bitmap *jb_array)
{
int i;
struct reiserfs_list_bitmap *jb;
for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
jb = jb_array + i;
jb->journal_list = NULL;
cleanup_bitmap_list(sb, jb);
vfree(jb->bitmaps);
jb->bitmaps = NULL;
}
return 0;
}
static int free_bitmap_nodes(struct super_block *sb)
{
struct reiserfs_journal *journal = SB_JOURNAL(sb);
struct list_head *next = journal->j_bitmap_nodes.next;
struct reiserfs_bitmap_node *bn;
while (next != &journal->j_bitmap_nodes) {
bn = list_entry(next, struct reiserfs_bitmap_node, list);
list_del(next);
kfree(bn->data);
kfree(bn);
next = journal->j_bitmap_nodes.next;
journal->j_free_bitmap_nodes--;
}
return 0;
}
/*
* get memory for JOURNAL_NUM_BITMAPS worth of bitmaps.
* jb_array is the array to be filled in.
*/
int reiserfs_allocate_list_bitmaps(struct super_block *sb,
struct reiserfs_list_bitmap *jb_array,
unsigned int bmap_nr)
{
int i;
int failed = 0;
struct reiserfs_list_bitmap *jb;
int mem = bmap_nr * sizeof(struct reiserfs_bitmap_node *);
for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
jb = jb_array + i;
jb->journal_list = NULL;
jb->bitmaps = vzalloc(mem);
if (!jb->bitmaps) {
reiserfs_warning(sb, "clm-2000", "unable to "
"allocate bitmaps for journal lists");
failed = 1;
break;
}
}
if (failed) {
free_list_bitmaps(sb, jb_array);
return -1;
}
return 0;
}
/*
* find an available list bitmap. If you can't find one, flush a commit list
* and try again
*/
static struct reiserfs_list_bitmap *get_list_bitmap(struct super_block *sb,
struct reiserfs_journal_list
*jl)
{
int i, j;
struct reiserfs_journal *journal = SB_JOURNAL(sb);
struct reiserfs_list_bitmap *jb = NULL;
for (j = 0; j < (JOURNAL_NUM_BITMAPS * 3); j++) {
i = journal->j_list_bitmap_index;
journal->j_list_bitmap_index = (i + 1) % JOURNAL_NUM_BITMAPS;
jb = journal->j_list_bitmap + i;
if (journal->j_list_bitmap[i].journal_list) {
flush_commit_list(sb,
journal->j_list_bitmap[i].
journal_list, 1);
if (!journal->j_list_bitmap[i].journal_list) {
break;
}
} else {
break;
}
}
/* double check to make sure if flushed correctly */
if (jb->journal_list)
return NULL;
jb->journal_list = jl;
return jb;
}
/*
* allocates a new chunk of X nodes, and links them all together as a list.
* Uses the cnode->next and cnode->prev pointers
* returns NULL on failure
*/
static struct reiserfs_journal_cnode *allocate_cnodes(int num_cnodes)
{
struct reiserfs_journal_cnode *head;
int i;
if (num_cnodes <= 0) {
return NULL;
}
head = vzalloc(num_cnodes * sizeof(struct reiserfs_journal_cnode));
if (!head) {
return NULL;
}
head[0].prev = NULL;
head[0].next = head + 1;
for (i = 1; i < num_cnodes; i++) {
head[i].prev = head + (i - 1);
head[i].next = head + (i + 1); /* if last one, overwrite it after the if */
}
head[num_cnodes - 1].next = NULL;
return head;
}
/* pulls a cnode off the free list, or returns NULL on failure */
static struct reiserfs_journal_cnode *get_cnode(struct super_block *sb)
{
struct reiserfs_journal_cnode *cn;
struct reiserfs_journal *journal = SB_JOURNAL(sb);
reiserfs_check_lock_depth(sb, "get_cnode");
if (journal->j_cnode_free <= 0) {
return NULL;
}
journal->j_cnode_used++;
journal->j_cnode_free--;
cn = journal->j_cnode_free_list;
if (!cn) {
return cn;
}
if (cn->next) {
cn->next->prev = NULL;
}
journal->j_cnode_free_list = cn->next;
memset(cn, 0, sizeof(struct reiserfs_journal_cnode));
return cn;
}
/*
* returns a cnode to the free list
*/
static void free_cnode(struct super_block *sb,
struct reiserfs_journal_cnode *cn)
{
struct reiserfs_journal *journal = SB_JOURNAL(sb);
reiserfs_check_lock_depth(sb, "free_cnode");
journal->j_cnode_used--;
journal->j_cnode_free++;
/* memset(cn, 0, sizeof(struct reiserfs_journal_cnode)) ; */
cn->next = journal->j_cnode_free_list;
if (journal->j_cnode_free_list) {
journal->j_cnode_free_list->prev = cn;
}
cn->prev = NULL; /* not needed with the memset, but I might kill the memset, and forget to do this */
journal->j_cnode_free_list = cn;
}
static void clear_prepared_bits(struct buffer_head *bh)
{
clear_buffer_journal_prepared(bh);
clear_buffer_journal_restore_dirty(bh);
}
/*
* return a cnode with same dev, block number and size in table,
* or null if not found
*/
static inline struct reiserfs_journal_cnode *get_journal_hash_dev(struct
super_block
*sb,
struct
reiserfs_journal_cnode
**table,
long bl)
{
struct reiserfs_journal_cnode *cn;
cn = journal_hash(table, sb, bl);
while (cn) {
if (cn->blocknr == bl && cn->sb == sb)
return cn;
cn = cn->hnext;
}
return (struct reiserfs_journal_cnode *)0;
}
/*
* this actually means 'can this block be reallocated yet?'. If you set
* search_all, a block can only be allocated if it is not in the current
* transaction, was not freed by the current transaction, and has no chance
* of ever being overwritten by a replay after crashing.
*
* If you don't set search_all, a block can only be allocated if it is not
* in the current transaction. Since deleting a block removes it from the
* current transaction, this case should never happen. If you don't set
* search_all, make sure you never write the block without logging it.
*
* next_zero_bit is a suggestion about the next block to try for find_forward.
* when bl is rejected because it is set in a journal list bitmap, we search
* for the next zero bit in the bitmap that rejected bl. Then, we return
* that through next_zero_bit for find_forward to try.
*
* Just because we return something in next_zero_bit does not mean we won't
* reject it on the next call to reiserfs_in_journal
*/
int reiserfs_in_journal(struct super_block *sb,
unsigned int bmap_nr, int bit_nr, int search_all,
b_blocknr_t * next_zero_bit)
{
struct reiserfs_journal *journal = SB_JOURNAL(sb);
struct reiserfs_journal_cnode *cn;
struct reiserfs_list_bitmap *jb;
int i;
unsigned long bl;
*next_zero_bit = 0; /* always start this at zero. */
PROC_INFO_INC(sb, journal.in_journal);
/*
* If we aren't doing a search_all, this is a metablock, and it
* will be logged before use. if we crash before the transaction
* that freed it commits, this transaction won't have committed
* either, and the block will never be written
*/
if (search_all) {
for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
PROC_INFO_INC(sb, journal.in_journal_bitmap);
jb = journal->j_list_bitmap + i;
if (jb->journal_list && jb->bitmaps[bmap_nr] &&
test_bit(bit_nr,
(unsigned long *)jb->bitmaps[bmap_nr]->
data)) {
*next_zero_bit =
find_next_zero_bit((unsigned long *)
(jb->bitmaps[bmap_nr]->
data),
sb->s_blocksize << 3,
bit_nr + 1);
return 1;
}
}
}
bl = bmap_nr * (sb->s_blocksize << 3) + bit_nr;
/* is it in any old transactions? */
if (search_all
&& (cn =
get_journal_hash_dev(sb, journal->j_list_hash_table, bl))) {
return 1;
}
/* is it in the current transaction. This should never happen */
if ((cn = get_journal_hash_dev(sb, journal->j_hash_table, bl))) {
BUG();
return 1;
}
PROC_INFO_INC(sb, journal.in_journal_reusable);
/* safe for reuse */
return 0;
}
/* insert cn into table */
static inline void insert_journal_hash(struct reiserfs_journal_cnode **table,
struct reiserfs_journal_cnode *cn)
{
struct reiserfs_journal_cnode *cn_orig;
cn_orig = journal_hash(table, cn->sb, cn->blocknr);
cn->hnext = cn_orig;
cn->hprev = NULL;
if (cn_orig) {
cn_orig->hprev = cn;
}
journal_hash(table, cn->sb, cn->blocknr) = cn;
}
/* lock the current transaction */
static inline void lock_journal(struct super_block *sb)
{
PROC_INFO_INC(sb, journal.lock_journal);
reiserfs_mutex_lock_safe(&SB_JOURNAL(sb)->j_mutex, sb);
}
/* unlock the current transaction */
static inline void unlock_journal(struct super_block *sb)
{
mutex_unlock(&SB_JOURNAL(sb)->j_mutex);
}
static inline void get_journal_list(struct reiserfs_journal_list *jl)
{
jl->j_refcount++;
}
static inline void put_journal_list(struct super_block *s,
struct reiserfs_journal_list *jl)
{
if (jl->j_refcount < 1) {
reiserfs_panic(s, "journal-2", "trans id %u, refcount at %d",
jl->j_trans_id, jl->j_refcount);
}
if (--jl->j_refcount == 0)
kfree(jl);
}
/*
* this used to be much more involved, and I'm keeping it just in case
* things get ugly again. it gets called by flush_commit_list, and
* cleans up any data stored about blocks freed during a transaction.
*/
static void cleanup_freed_for_journal_list(struct super_block *sb,
struct reiserfs_journal_list *jl)
{
struct reiserfs_list_bitmap *jb = jl->j_list_bitmap;
if (jb) {
cleanup_bitmap_list(sb, jb);
}
jl->j_list_bitmap->journal_list = NULL;
jl->j_list_bitmap = NULL;
}
static int journal_list_still_alive(struct super_block *s,
unsigned int trans_id)
{
struct reiserfs_journal *journal = SB_JOURNAL(s);
struct list_head *entry = &journal->j_journal_list;
struct reiserfs_journal_list *jl;
if (!list_empty(entry)) {
jl = JOURNAL_LIST_ENTRY(entry->next);
if (jl->j_trans_id <= trans_id) {
return 1;
}
}
return 0;
}
/*
* If page->mapping was null, we failed to truncate this page for
* some reason. Most likely because it was truncated after being
* logged via data=journal.
*
* This does a check to see if the buffer belongs to one of these
* lost pages before doing the final put_bh. If page->mapping was
* null, it tries to free buffers on the page, which should make the
* final put_page drop the page from the lru.
*/
static void release_buffer_page(struct buffer_head *bh)
{
struct page *page = bh->b_page;
if (!page->mapping && trylock_page(page)) {
get_page(page);
put_bh(bh);
if (!page->mapping)
try_to_free_buffers(page);
unlock_page(page);
put_page(page);
} else {
put_bh(bh);
}
}
static void reiserfs_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
{
if (buffer_journaled(bh)) {
reiserfs_warning(NULL, "clm-2084",
"pinned buffer %lu:%pg sent to disk",
bh->b_blocknr, bh->b_bdev);
}
if (uptodate)
set_buffer_uptodate(bh);
else
clear_buffer_uptodate(bh);
unlock_buffer(bh);
release_buffer_page(bh);
}
static void reiserfs_end_ordered_io(struct buffer_head *bh, int uptodate)
{
if (uptodate)
set_buffer_uptodate(bh);
else
clear_buffer_uptodate(bh);
unlock_buffer(bh);
put_bh(bh);
}
static void submit_logged_buffer(struct buffer_head *bh)
{
get_bh(bh);
bh->b_end_io = reiserfs_end_buffer_io_sync;
clear_buffer_journal_new(bh);
clear_buffer_dirty(bh);
if (!test_clear_buffer_journal_test(bh))
BUG();
if (!buffer_uptodate(bh))
BUG();
submit_bh(REQ_OP_WRITE, 0, bh);
}
static void submit_ordered_buffer(struct buffer_head *bh)
{
get_bh(bh);
bh->b_end_io = reiserfs_end_ordered_io;
clear_buffer_dirty(bh);
if (!buffer_uptodate(bh))
BUG();
submit_bh(REQ_OP_WRITE, 0, bh);
}
#define CHUNK_SIZE 32
struct buffer_chunk {
struct buffer_head *bh[CHUNK_SIZE];
int nr;
};
static void write_chunk(struct buffer_chunk *chunk)
{
int i;
for (i = 0; i < chunk->nr; i++) {
submit_logged_buffer(chunk->bh[i]);
}
chunk->nr = 0;
}
static void write_ordered_chunk(struct buffer_chunk *chunk)
{
int i;
for (i = 0; i < chunk->nr; i++) {
submit_ordered_buffer(chunk->bh[i]);
}
chunk->nr = 0;
}
static int add_to_chunk(struct buffer_chunk *chunk, struct buffer_head *bh,
spinlock_t * lock, void (fn) (struct buffer_chunk *))
{
int ret = 0;
BUG_ON(chunk->nr >= CHUNK_SIZE);
chunk->bh[chunk->nr++] = bh;
if (chunk->nr >= CHUNK_SIZE) {
ret = 1;
if (lock) {
spin_unlock(lock);
fn(chunk);
spin_lock(lock);
} else {
fn(chunk);
}
}
return ret;
}
static atomic_t nr_reiserfs_jh = ATOMIC_INIT(0);
static struct reiserfs_jh *alloc_jh(void)
{
struct reiserfs_jh *jh;
while (1) {
jh = kmalloc(sizeof(*jh), GFP_NOFS);
if (jh) {
atomic_inc(&nr_reiserfs_jh);
return jh;
}
yield();
}
}
/*
* we want to free the jh when the buffer has been written
* and waited on
*/
void reiserfs_free_jh(struct buffer_head *bh)
{
struct reiserfs_jh *jh;
jh = bh->b_private;
if (jh) {
bh->b_private = NULL;
jh->bh = NULL;
list_del_init(&jh->list);
kfree(jh);
if (atomic_read(&nr_reiserfs_jh) <= 0)
BUG();
atomic_dec(&nr_reiserfs_jh);
put_bh(bh);
}
}
static inline int __add_jh(struct reiserfs_journal *j, struct buffer_head *bh,
int tail)
{
struct reiserfs_jh *jh;
if (bh->b_private) {
spin_lock(&j->j_dirty_buffers_lock);
if (!bh->b_private) {
spin_unlock(&j->j_dirty_buffers_lock);
goto no_jh;
}
jh = bh->b_private;
list_del_init(&jh->list);
} else {
no_jh:
get_bh(bh);
jh = alloc_jh();
spin_lock(&j->j_dirty_buffers_lock);
/*
* buffer must be locked for __add_jh, should be able to have
* two adds at the same time
*/
BUG_ON(bh->b_private);
jh->bh = bh;
bh->b_private = jh;
}
jh->jl = j->j_current_jl;
if (tail)
list_add_tail(&jh->list, &jh->jl->j_tail_bh_list);
else {
list_add_tail(&jh->list, &jh->jl->j_bh_list);
}
spin_unlock(&j->j_dirty_buffers_lock);
return 0;
}
int reiserfs_add_tail_list(struct inode *inode, struct buffer_head *bh)
{
return __add_jh(SB_JOURNAL(inode->i_sb), bh, 1);
}
int reiserfs_add_ordered_list(struct inode *inode, struct buffer_head *bh)
{
return __add_jh(SB_JOURNAL(inode->i_sb), bh, 0);
}
#define JH_ENTRY(l) list_entry((l), struct reiserfs_jh, list)
static int write_ordered_buffers(spinlock_t * lock,
struct reiserfs_journal *j,
struct reiserfs_journal_list *jl,
struct list_head *list)
{
struct buffer_head *bh;
struct reiserfs_jh *jh;
int ret = j->j_errno;
struct buffer_chunk chunk;
struct list_head tmp;
INIT_LIST_HEAD(&tmp);
chunk.nr = 0;
spin_lock(lock);
while (!list_empty(list)) {
jh = JH_ENTRY(list->next);
bh = jh->bh;
get_bh(bh);
if (!trylock_buffer(bh)) {
if (!buffer_dirty(bh)) {
list_move(&jh->list, &tmp);
goto loop_next;
}
spin_unlock(lock);
if (chunk.nr)
write_ordered_chunk(&chunk);
wait_on_buffer(bh);
cond_resched();
spin_lock(lock);
goto loop_next;
}
/*
* in theory, dirty non-uptodate buffers should never get here,
* but the upper layer io error paths still have a few quirks.
* Handle them here as gracefully as we can
*/
if (!buffer_uptodate(bh) && buffer_dirty(bh)) {
clear_buffer_dirty(bh);
ret = -EIO;
}
if (buffer_dirty(bh)) {
list_move(&jh->list, &tmp);
add_to_chunk(&chunk, bh, lock, write_ordered_chunk);
} else {
reiserfs_free_jh(bh);
unlock_buffer(bh);
}
loop_next:
put_bh(bh);
cond_resched_lock(lock);
}
if (chunk.nr) {
spin_unlock(lock);
write_ordered_chunk(&chunk);
spin_lock(lock);
}
while (!list_empty(&tmp)) {
jh = JH_ENTRY(tmp.prev);
bh = jh->bh;
get_bh(bh);
reiserfs_free_jh(bh);
if (buffer_locked(bh)) {
spin_unlock(lock);
wait_on_buffer(bh);
spin_lock(lock);
}
if (!buffer_uptodate(bh)) {
ret = -EIO;
}
/*
* ugly interaction with invalidatepage here.
* reiserfs_invalidate_page will pin any buffer that has a
* valid journal head from an older transaction. If someone
* else sets our buffer dirty after we write it in the first
* loop, and then someone truncates the page away, nobody
* will ever write the buffer. We're safe if we write the
* page one last time after freeing the journal header.
*/
if (buffer_dirty(bh) && unlikely(bh->b_page->mapping == NULL)) {
spin_unlock(lock);
ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
spin_lock(lock);
}
put_bh(bh);
cond_resched_lock(lock);
}
spin_unlock(lock);
return ret;
}
static int flush_older_commits(struct super_block *s,
struct reiserfs_journal_list *jl)
{
struct reiserfs_journal *journal = SB_JOURNAL(s);
struct reiserfs_journal_list *other_jl;
struct reiserfs_journal_list *first_jl;
struct list_head *entry;
unsigned int trans_id = jl->j_trans_id;
unsigned int other_trans_id;
unsigned int first_trans_id;
find_first:
/*
* first we walk backwards to find the oldest uncommitted transation
*/
first_jl = jl;
entry = jl->j_list.prev;
while (1) {
other_jl = JOURNAL_LIST_ENTRY(entry);
if (entry == &journal->j_journal_list ||
atomic_read(&other_jl->j_older_commits_done))
break;
first_jl = other_jl;
entry = other_jl->j_list.prev;
}
/* if we didn't find any older uncommitted transactions, return now */
if (first_jl == jl) {
return 0;
}
first_trans_id = first_jl->j_trans_id;
entry = &first_jl->j_list;
while (1) {
other_jl = JOURNAL_LIST_ENTRY(entry);
other_trans_id = other_jl->j_trans_id;
if (other_trans_id < trans_id) {
if (atomic_read(&other_jl->j_commit_left) != 0) {
flush_commit_list(s, other_jl, 0);
/* list we were called with is gone, return */
if (!journal_list_still_alive(s, trans_id))
return 1;
/*
* the one we just flushed is gone, this means
* all older lists are also gone, so first_jl
* is no longer valid either. Go back to the
* beginning.
*/
if (!journal_list_still_alive
(s, other_trans_id)) {
goto find_first;
}
}
entry = entry->next;
if (entry == &journal->j_journal_list)
return 0;
} else {
return 0;
}
}
return 0;
}
static int reiserfs_async_progress_wait(struct super_block *s)
{
struct reiserfs_journal *j = SB_JOURNAL(s);
if (atomic_read(&j->j_async_throttle)) {
int depth;
depth = reiserfs_write_unlock_nested(s);
congestion_wait(BLK_RW_ASYNC, HZ / 10);
reiserfs_write_lock_nested(s, depth);
}
return 0;
}
/*
* if this journal list still has commit blocks unflushed, send them to disk.
*
* log areas must be flushed in order (transaction 2 can't commit before
* transaction 1) Before the commit block can by written, every other log
* block must be safely on disk
*/
static int flush_commit_list(struct super_block *s,
struct reiserfs_journal_list *jl, int flushall)
{
int i;
b_blocknr_t bn;
struct buffer_head *tbh = NULL;
unsigned int trans_id = jl->j_trans_id;
struct reiserfs_journal *journal = SB_JOURNAL(s);
int retval = 0;
int write_len;
int depth;
reiserfs_check_lock_depth(s, "flush_commit_list");
if (atomic_read(&jl->j_older_commits_done)) {
return 0;
}
/*
* before we can put our commit blocks on disk, we have to make
* sure everyone older than us is on disk too
*/
BUG_ON(jl->j_len <= 0);
BUG_ON(trans_id == journal->j_trans_id);
get_journal_list(jl);
if (flushall) {
if (flush_older_commits(s, jl) == 1) {