-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.c
3407 lines (2984 loc) · 101 KB
/
compress.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
/*
* linux/fs/ext2/compress.c
*
* Copyright (C) 1995 Antoine Dumesnil de Maricourt ([email protected])
* (transparent compression code)
*/
/*
* Copyright (C) 2001 Alcatel Business Systems - R&D Illkirch FRANCE
*
* Transparent compression code for 2.4 kernel.
*
* Denis Richard ([email protected])
* Pierre Peiffer ([email protected])
*
* Adapted from patch e2compr-0.4.39-patch-2.2.18 .
*/
#include <asm/segment.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/ext2_fs.h>
#include <linux/ext2_fs_c.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/stat.h>
#include <linux/buffer_head.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/quotaops.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/swap.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/writeback.h>
#include <linux/rmap.h>
#include <linux/swap.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kernel_stat.h>
#include <linux/swap.h>
#include <linux/pagemap.h>
#include <linux/init.h>
#include <linux/highmem.h>
#include <linux/vmstat.h>
#include <linux/file.h>
#include <linux/writeback.h>
#include <linux/blkdev.h>
#include <linux/buffer_head.h>
#include <linux/mm_inline.h>
#include <linux/pagevec.h>
#include <linux/backing-dev.h>
#include <linux/rmap.h>
#include <linux/topology.h>
#include <linux/cpu.h>
#include <linux/cpuset.h>
#include <linux/notifier.h>
#include <linux/rwsem.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <asm/tlbflush.h>
#include <asm/div64.h>
#include <linux/swapops.h>
#include <linux/percpu.h>
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#ifdef CONFIG_HIGHMEM
#define restore_b_data_himem(bh) assert(page_address(bh->b_page)); bh->b_data = page_address(bh->b_page) + bh_offset(bh)
int ext2_kmap_cluster_pages(struct page *page, struct page *pg[],
struct page *epg[])
{
int i = 0;
for (i = 0; i < EXT2_MAX_CLUSTER_PAGES; i++) {
if (!pg[i])
break;
if (epg && epg[i])
kmap(epg[i]);
else
kmap(pg[i]);
}
if (page)
kmap(page);
return 0;
}
int ext2_kunmap_cluster_pages(struct page *page, struct page *pg[],
struct page *epg[])
{
int i = 0;
for (i = 0; i < EXT2_MAX_CLUSTER_PAGES; i++) {
if (!pg[i])
break;
if (epg && epg[i])
kunmap(epg[i]);
else
kunmap(pg[i]);
}
if (page)
kunmap(page);
return 0;
}
#else //no high-mem:
#define restore_b_data_himem(bh) ;
#endif
/*none compression dummy functions*/
size_t ext2_iNONE (int action) { return 0; }
size_t ext2_wNONE (__u8 *ibuf, __u8 *obuf, void *wa, size_t ilen, size_t olen, int xarg) { return 0; }
size_t ext2_rNONE (__u8 *ibuf, __u8 *obuf, void *wa, size_t ilen, size_t olen, int xarg) { return 0; }
/*
* Algorithm and method tables
*/
struct ext2_algorithm ext2_algorithm_table[] = {
/* Note: all algorithms must have the `name' field filled in.
This is used to autoload algorithm modules (ext2-compr-%s), and
in kernel printk. */
/* N.B. Do not renumber these algorithms! (To do so is to change
the binary format.) It's OK for `none' and `undef' to be
renumbered, though. */
/* Fields:
name; available; routines for:
init, compress, decompress. */
{"lzv1", 0, ext2_iNONE, ext2_wNONE, ext2_rNONE},
{"lzrw3a", 0, ext2_iNONE, ext2_wNONE, ext2_rNONE},
{"gzip", 1, ext2_iZLIB, ext2_wZLIB, ext2_rZLIB}, //Andreas: workaround
{"bzip2", 0, ext2_iNONE, ext2_wNONE, ext2_rNONE},
{"lzo", 0, ext2_iNONE, ext2_wNONE, ext2_rNONE},
{"none", 1, ext2_iNONE, ext2_wNONE, ext2_rNONE},
/* This "algorithm" is for unused entries in the method table.
It differs from EXT2_NONE_ALG in that it is considered
unavailable, whereas `none' is always available. */
{"undef", 0, ext2_iNONE, ext2_wNONE, ext2_rNONE},
};
/* Note: EXT2_N_ALGORITHMS can't be increased beyond 16 without
changing the width of the s_algorithms_used field in the in-memory
superblock. The on-disk s_algorithms_used field is 32 bits long.
(This is in a state of flux. Currently (1998-02-05) there is no
distinction: we always use the s_es copy. */
/* The size of this table must be 32 to prevent Oopsen from
invalid data. We index this from 5 bits of i_flags, so
the size is (1 << 5) == 32. */
struct ext2_method ext2_method_table[32] = {
/* Fields: algorithm id, algorithm argument. */
{EXT2_LZV1_ALG, 0},
{EXT2_NONE_ALG, 0}, /* 1: auto */
{EXT2_NONE_ALG, 0}, /* 2: defer */
{EXT2_NONE_ALG, 0}, /* 3: never */
{EXT2_BZIP2_ALG, 0}, /* 4: bzip2 */
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_LZRW3A_ALG, 0}, /* 8: lzrw3a */
{EXT2_UNDEF_ALG, 0},
{EXT2_LZO_ALG, 0}, /* 10: lzo1x_1 */
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_GZIP_ALG, 1}, /* 16 */
{EXT2_GZIP_ALG, 2},
{EXT2_GZIP_ALG, 3},
{EXT2_GZIP_ALG, 4},
{EXT2_GZIP_ALG, 5},
{EXT2_GZIP_ALG, 6},
{EXT2_GZIP_ALG, 7},
{EXT2_GZIP_ALG, 8},
{EXT2_GZIP_ALG, 9},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0},
{EXT2_UNDEF_ALG, 0}
};
static void ext2_mark_algorithm_use(struct inode *inode, unsigned alg)
{
struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
/* Hopefully, lock_super() isn't needed here, as we don't
block in the critical region. True? */
assert(alg < EXT2_N_ALGORITHMS);
if (sbi->s_es->s_feature_incompat
& cpu_to_le32(EXT2_FEATURE_INCOMPAT_COMPRESSION)) {
sbi->s_es->s_algorithm_usage_bitmap |= cpu_to_le32(1 << alg);
} else {
struct ext2_super_block *es = sbi->s_es;
es->s_algorithm_usage_bitmap = cpu_to_le32(1 << alg);
es->s_feature_incompat
|= cpu_to_le32(EXT2_FEATURE_INCOMPAT_COMPRESSION);
if (es->s_rev_level < EXT2_DYNAMIC_REV) {
/* Raise the filesystem revision level to
EXT2_DYNAMIC_REV so that s_feature_incompat
is honoured (except in ancient kernels /
e2fsprogs). We must also initialize two
other dynamic-rev fields. The remaining
fields are assumed to be already correct
(e.g. still zeroed). */
es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
}
}
mark_buffer_dirty(sbi->s_sbh);
}
/* Displays an error message if algorithm ,alg` is not marked in use,
and then marks it in use. */
static void ext2_ensure_algorithm_use(struct inode *inode, unsigned alg)
{
assert(alg < EXT2_N_ALGORITHMS);
if (!(EXT2_SB(inode->i_sb)->s_es->s_algorithm_usage_bitmap
& cpu_to_le32(1 << alg))) {
ext2_msg(inode->i_sb, "algorithm usage bitmap algorithm %s not marked used in inode %lu",
ext2_algorithm_table[alg].name, inode->i_ino);
ext2_mark_algorithm_use(inode, alg);
}
}
/*mw: out of cache bug fix 5-16-07 */
static void create_empty_buffers_e2c(struct page *page,
unsigned long blocksize,
unsigned long b_state,
struct inode *inode)
{
struct buffer_head *bh, *head, *tail;
head = alloc_page_buffers(page, blocksize, 1);
bh = head;
do {
bh->b_state |= b_state;
tail = bh;
bh->b_bdev = NULL; //mw: make it like 2.4
bh->b_blocknr = 0; //mw: make it like 2.4
bh->b_end_io = NULL; //mw: make it like 2.4
bh = bh->b_this_page;
} while (bh);
tail->b_this_page = head;
spin_lock(&inode->i_mapping->private_lock);
if (PageUptodate(page) || PageDirty(page)) {
bh = head;
do {
if (PageDirty(page))
set_buffer_dirty(bh);
if (PageUptodate(page))
set_buffer_uptodate(bh);
bh = bh->b_this_page;
} while (bh != head);
}
attach_page_buffers(page, head);
spin_unlock(&inode->i_mapping->private_lock);
}
int ext2_get_cluster_pages(struct inode *inode, u32 cluster,
struct page *pg[], struct page *page, int compr)
{
int nbpg, npg, i;
u32 page0; /* = position within file (not position within fs). */
u32 idx = 0;
/*mw */
for (i = 0; i < EXT2_MAX_CLUSTER_PAGES; i++)
pg[i] = NULL;
page0 = ext2_cluster_page0(inode, cluster);
nbpg = ext2_cluster_npages(inode, cluster);
if (compr && (((page0 + nbpg) << PAGE_CACHE_SHIFT) > inode->i_size))
nbpg = ((inode->i_size - 1) >> PAGE_CACHE_SHIFT) - page0 + 1;
#ifdef EXT2_COMPR_REPORT
trace_e2c("ext2_get_cluster_pages: page0=%d, nbpg=%d page=%ld\n",
page0, nbpg, ((page != NULL) ? page->index : 0));
#endif
for (npg = 0; npg < nbpg; npg++) {
if ((page == NULL) || ((page0 + npg) != page->index)) {
//pg[npg] = __grab_cache_page(inode->i_mapping, page0+npg); /* &cached_page, &lru_pvec);*/
pg[npg] = grab_cache_page_write_begin(inode->i_mapping, page0+npg, 0);
if (!pg[npg])
goto error;
} else {
pg[npg] = page;
}
if (!page_has_buffers(pg[npg])) {
ClearPageUptodate(pg[npg]);
ClearPageDirty(pg[npg]);
create_empty_buffers_e2c(pg[npg], inode->i_sb->s_blocksize, 0, inode);
if (unlikely(!page_has_buffers(pg[npg])))
trace_e2c("ext2_get_cluster_pages: NOMEM!\n");
assert(!PageUptodate(pg[npg]));
assert(!PageDirty(pg[npg]));
}
}
//set remaining pages to NULL
for (idx = npg; idx < EXT2_MAX_CLUSTER_PAGES; idx++)
pg[idx] = NULL;
return (npg);
error:
while (--npg >= 0) {
if ((page == NULL) || ((page0 + npg) != page->index)) {
unlock_page(pg[npg]);
page_cache_release(pg[npg]);
}
pg[npg] = NULL;
}
trace_e2c("ext2_get_cluster_pages: error no page\n");
return (-ENOMEM);
}
int ext2_get_cluster_extra_pages(struct inode *inode, u32 cluster,
struct page *pg[], struct page *epg[])
{
struct page *page;
int nbpg, npg, i;
for (i = 0; i < EXT2_MAX_CLUSTER_PAGES; i++)
epg[i] = NULL;
nbpg = ext2_cluster_npages(inode, cluster);
for (npg = 0; npg < nbpg; npg++) {
if (pg[npg] == NULL)
break;
if (PageUptodate(pg[npg])) {
//page = page_cache_alloc(inode->i_mapping);
//mw: has gfp-mask of adress-space: gfp_t mapping_gfp_mask(struct address_space * mapping)
// don't trigger. shrink_dcache_memory which might call ext2_cleanup_compressed_inode with the SAME mutex.
page = __page_cache_alloc(GFP_NOFS);
if (!page) {
goto error;
}
ClearPageError(page);
ClearPageReferenced(page);
ClearPageUptodate(page);
ClearPageDirty(page);
lock_page(page);
page->index = pg[npg]->index;
if (!page_has_buffers(page)) {
create_empty_buffers_e2c(page, inode->i_sb->s_blocksize, 0,
inode);
/*mw : only the "extra_pages" for decompression need create_empty_buffers_unlocked, because
* they have no mapping-context and they must not have one. Otherwise they get need a page->index
* which belongs always to an address_space object (e.g.: inode). But I think this is not intented here.
* we just need thei buffers for a short time of decompression */
if (unlikely(!page_has_buffers(page)))
return printk("Error: NOMEM!\n");
}
epg[npg] = page;
#ifdef EXT2_COMPR_REPORT
trace_e2c
("ext2_get_cluster_extra_pages: allocated page idx=%ld\n",
pg[npg]->index);
#endif
} else {
epg[npg] = NULL;
}
}
return (npg);
error:
while (--npg >= 0)
if (epg[npg]) {
ClearPageDirty(epg[npg]);
ClearPageUptodate(epg[npg]);
try_to_free_buffers(epg[npg]);
unlock_page(epg[npg]);
assert(page_count(epg[npg]) == 1);
page_cache_release(epg[npg]);
}
trace_e2c("ext2_get_cluster_extra_pages: error no page\n");
return (-ENOMEM);
}
/* Read every block in the cluster. The blocks are stored in the bh
array, which must be big enough.
Return the number of block contained in the cluster, or -errno if an
error occured. The buffers should be released by the caller
(unless an error occurred).
The inode must be locked, otherwise it is possible that we return
some out of date blocks.
Called by :
ext2_decompress_cluster() [i_sem]
ext2_compress_cluster() [i_sem]
ext2_readpage() [i_sem] */
int ext2_get_cluster_blocks(struct inode *inode, u32 cluster,
struct buffer_head *bh[], struct page *pg[],
struct page *epg[], int compr)
{
struct buffer_head *br[EXT2_MAX_CLUSTER_BLOCKS];
int nreq, nbh = 0, npg, i;
u32 clu_nblocks;
int err;
const int blocks = PAGE_CACHE_SIZE >> inode->i_sb->s_blocksize_bits;
/*mw */
for (i = 0; i < EXT2_MAX_CLUSTER_BLOCKS; i++)
bh[i] = NULL;
assert(atomic_read(&inode->i_mutex.count) <= 0); /* i.e. mutex_lock */
/*
* Request full cluster.
*/
{
u32 endblk;
u32 block; /* = position within file (not position within fs). */
u32 nbpg;
u32 page0; /* = position within file (not position within fs). */
u32 idx;
block = ext2_cluster_block0(inode, cluster);
clu_nblocks = ext2_cluster_nblocks(inode, cluster);
/* impl: Don't shorten endblk for i_size. The
remaining blocks should be NULL anyway, except in
the case when called from ext2_decompress_cluster
from ext2_truncate, in which case i_size is short
and we _want_ to get all of the blocks. */
endblk = block + clu_nblocks;
page0 = ext2_cluster_page0(inode, cluster);
nbpg = ext2_cluster_npages(inode, cluster);
if (compr
&& (((page0 + nbpg) << PAGE_CACHE_SHIFT) > inode->i_size)) {
nbpg = ((inode->i_size - 1) >> PAGE_CACHE_SHIFT) - page0 + 1;
endblk =
block +
(nbpg <<
(PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits));
}
idx = page0 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
#ifdef EXT2_COMPR_REPORT
trace_e2c("ext2_get_cluster_blocks: page0=%d, nbpg=%d\n", page0,
nbpg);
#endif
for (npg = 0; npg < nbpg; npg++) {
struct buffer_head *buffer;
if ((epg != NULL) && (epg[npg] != NULL))
buffer = page_buffers(epg[npg]);
else
buffer = page_buffers(pg[npg]);
for (i = 0; i < blocks && (block + nbh) < endblk;
buffer = buffer->b_this_page, i++) {
if (idx == (block + nbh)) {
bh[nbh] = buffer;
nbh++;
}
idx++;
}
}
#ifdef EXT2_COMPR_REPORT
trace_e2c
("ext2_get_cluster_blocks: get every pages and %d buffers\n",
nbh);
#endif
for (nbh = 0, nreq = 0; block < endblk; nbh++) {
assert(bh[nbh] != NULL);
bh[nbh]->b_blocknr = 0;
clear_bit(BH_Mapped, &bh[nbh]->b_state);
//mw: does not work with 2.6 and holes!!!
//err=ext2_get_block(inode, block++, bh[nbh], (PageDirty(bh[nbh]->b_page) ? 1 : 0));
err = ext2_get_block(inode, block++, bh[nbh], 0);
/* mw: 0: we dont' create non existing blocks here
* let's do it just before the writeback, when we know, which blocks we really need...*/
//err=ext2_get_block(inode, block++, bh[nbh], (buffer_dirty(bh[nbh]) ? 1 : 0));
/* mw: bdev-bug-fix: for files which got compressed and now consume less buffers
* ext2_get_block returns 0, for a empty-block. As these buffer were used before
* the bh[nbh]->b_bdev might be != NULL or just invalid. So we set them explicitly
* to NULL. */
//printk("Get Block cluster %i: (%#x):%i Blk-NR:%lu(%lu)[%lu-%lu] Bdev:%#x(%#x), PGDirty:%i, mapped:%i, PID: %lu\n", cluster, bh[nbh], nbh, block,
//if we are not mapped, then the blocknr will be wrong
//we set a bdev here the we will write to some "random" block
if (!buffer_mapped(bh[nbh])) {
bh[nbh]->b_bdev = NULL; /* don't write wrongly mapped blocks !!! */
/* mw: you encounter null pointer oops you MUST
* map your buffer using ext2_get_block()*/
}
if (bh[nbh]->b_blocknr != 0) {
if (!buffer_uptodate(bh[nbh])
/* TODO: Do we need this
`!buffer_locked' test? */
&& !buffer_locked(bh[nbh])
&& !PageDirty(bh[nbh]->b_page))
br[nreq++] = bh[nbh];
} else if ((err != 0)
&& (err != -EFBIG))
/* impl: for some unknown reason,
ext2_getblk() returns -EFBIG if
!create and there's a hole. ==> not right any more in 2.4 */
goto error;
}
for (i = nbh; i < EXT2_MAX_CLUSTER_BLOCKS; i++) {
bh[i] = NULL;
}
}
#ifdef EXT2_COMPR_REPORT_CPR
trace_e2c("ext2_get_cluster_blocks: nreq=%d for cluster=%d\n", nreq,
cluster);
#endif
//read all blocks, which are not null-blocks
if (nreq > 0)
ll_rw_block(READ, nreq, br);
/*
* Adjust nbh if we have some null blocks at end of cluster.
*/
while ((nbh != 0) && (bh[nbh - 1]->b_blocknr == 0))
nbh--;
/*
* Wait for blocks.
*/
err = -EIO;
CHECK_NOT_ATOMIC
for (i = 0; i < nbh; i++)
if ((!PageDirty(bh[i]->b_page)) && (bh[i]->b_blocknr != 0)) {
wait_on_buffer(bh[i]);
if (!buffer_uptodate(bh[i])) { /* Read error ??? */
trace_e2c
("ext2_get_cluster_blocks: wait_on_buffer error (blocknr=%ld)\n",
bh[i]->b_blocknr);
goto error;
}
}
assert(nbh <= EXT2_MAX_CLU_NBLOCKS);
return nbh;
error:
printk("ERROR: ext2_get_cluster_blocks()\n");
return err;
}
/* Iterations over block in the inode are done with a generic
iteration key mechanism. We need one method to convert a block
number into a new key, one method to iterate (i.e., increment the
key) and one method to free the key. The code could be shared with
truncate.c, as this mechanism is very general.
This code assumes tht nobody else can read or write the file
between ext2_get_key() and ext2_free_key(), so callers need to have
i_sem (which they all do anyway). */
/* TODO: Get all of the bkey routines to return -errno instead of
true/false. */
/* TODO: The bkey routines currently assume tht address blocks are
allocated even if all contained addresses are NULL, but this is not
true. Make sure tht we differentiate between NULL block and error,
and then fix up ext2_set_key_blkaddr() and anything else (including
the pack/unpack routines). */
struct ext2_bkey {
int level;
u32 block;
struct inode *inode;
int off[4];
u32 *ptr[4];
struct buffer_head *ibh[4];
};
/*
* Method to convert a block number into a key.
*
* Returns 1 on success, 0 on failure. You may safely, but need
* not, free the key even if ext2_get_key() fails.
*/
static int ext2_get_key(struct ext2_bkey *key, struct inode *inode,
u32 block)
{
int x, level;
int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
assert(atomic_read(&inode->i_mutex.count) <= 0);
/*
* The first step can be viewed as translating the
* original block number in a special base (powers
* of addr_per_block).
*/
key->block = block;
key->off[0] = key->off[1] = key->off[2] = key->off[3] = 0;
key->ibh[0] = key->ibh[1] = key->ibh[2] = key->ibh[3] = NULL;
key->ptr[0] = key->ptr[1] = key->ptr[2] = key->ptr[3] = NULL;
if (block >= EXT2_NDIR_BLOCKS) {
block -= EXT2_NDIR_BLOCKS;
if (block >= addr_per_block) {
block -= addr_per_block;
if (block >= addr_per_block * addr_per_block) {
block -= addr_per_block * addr_per_block;
key->off[0] = EXT2_TIND_BLOCK;
key->off[1] = (block / (addr_per_block * addr_per_block));
key->off[2] =
(block % (addr_per_block * addr_per_block)) /
addr_per_block;
key->off[3] = (block % addr_per_block);
level = 3;
} else {
key->off[0] = EXT2_DIND_BLOCK;
key->off[1] = block / addr_per_block;
key->off[2] = block % addr_per_block;
level = 2;
}
} else {
key->off[0] = EXT2_IND_BLOCK;
key->off[1] = block;
level = 1;
}
} else {
key->off[0] = block;
level = 0;
}
/*
* In the second step, we load the needed buffers.
*/
key->level = level;
key->inode = inode;
key->ptr[0] = (u32 *) (&(EXT2_I(inode)->i_data));
for (x = 1; x <= level; x++) {
u32 *ptr;
ptr = key->ptr[x - 1];
if (ptr == NULL)
break;
/* Paul Whittaker tweak 19 Feb 2005 */
block = le32_to_cpu(ptr[key->off[x - 1]]);
if (block == 0)
continue; // TLL 05/01/07
if (x - 1 != 0)
block = le32_to_cpu(block);
if ((key->ibh[x] = __bread(inode->i_sb->s_bdev,
block, inode->i_sb->s_blocksize))
== NULL)
goto error;
key->ptr[x] = (u32 *) (key->ibh[x]->b_data);
}
return 1;
error:
for (; x != 0; x--)
if (key->ibh[x] != NULL)
brelse(key->ibh[x]);
return 0;
}
/*
* Find the block for a given key. Return 0 if there
* is no block for this key.
*/
static inline u32 ext2_get_key_blkaddr(struct ext2_bkey *key)
{
assert(key->inode);
assert(atomic_read(&(key->inode)->i_mutex.count) <= 0);
/* Paul Whittaker tweak 19 Feb 2005 */
if (key->ptr[key->level] == NULL)
return 0;
return le32_to_cpu(key->ptr[key->level][key->off[key->level]]);
}
/*
* Change the block for a given key. Return 0 on success,
* -errno on failure.
*/
static inline int ext2_set_key_blkaddr(struct ext2_bkey *key, u32 blkaddr)
{
char bdn[BDEVNAME_SIZE];
assert(key->inode);
assert(atomic_read(&(key->inode)->i_mutex.count) <= 0);
if (key->ptr[key->level] == NULL) {
/* The reason that this "can't happen" is that this
routine is only used to shuffle block numbers or by
free_cluster_blocks. Cluster sizes are such that
clusters can't straddle address blocks. So the
indirect block address can't be zero. AFAIK, ptr
can only be NULL on error or on null indirect block
address. Hmm, come to think of it, I think there
are still some callers that don't check for errors
from ext2_get_key(), so this still can happen until
those are fixed up. */
printk(KERN_ERR
"ext2_set_key_blkaddr: can't happen: NULL parent. "
"dev=%s, ino=%lu, level=%u.\n",
bdevname(key->inode->i_sb->s_bdev, bdn),
key->inode->i_ino, key->level);
return -ENOSYS;
}
/* Paul Whittaker tweak 19 Feb 2005 */
key->ptr[key->level][key->off[key->level]] = le32_to_cpu(blkaddr);
if (key->level > 0)
mark_buffer_dirty(key->ibh[key->level]);
return 0;
}
/*
* Increment the key. Returns 0 if we go beyond the limits,
* 1 otherwise.
*
* Precondition: -key->off[level] <= incr < addr_per_block.
*/
static int ext2_next_key(struct ext2_bkey *key, int incr)
{
int addr_per_block = EXT2_ADDR_PER_BLOCK(key->inode->i_sb);
int x, level = key->level;
u32 tmp;
assert(key->inode);
assert(atomic_read(&(key->inode)->i_mutex.count) <= 0);
/*
* Increment the key. This is done in two step: first
* adjust the off array, then reload buffers that should
* be reloaded (we assume level > 0).
*/
assert(key->off[level] >= -incr);
assert(incr < addr_per_block);
key->block += incr;
key->off[level] += incr;
/*
* First step: should be thought as the propagation
* of a carry.
*/
if (level == 0) {
if (key->off[0] >= EXT2_NDIR_BLOCKS) {
key->off[1] = key->off[0] - EXT2_NDIR_BLOCKS;
key->off[0] = EXT2_IND_BLOCK;
level = 1;
}
x = 0;
} else {
for (x = level; x > 0; x--) {
if (key->off[x] >= addr_per_block) {
key->off[x] -= addr_per_block;
key->off[x - 1]++;
if (x == 1) {
if (++level < 4) {
key->off[level] = key->off[level - 1];
key->off[level - 1] = 0;
} else
return 0;
}
} else
break;
}
}
/*
* Second step: reload the buffers that have changed.
*/
key->level = level;
CHECK_NOT_ATOMIC
while (x++ < level) {
if (key->ibh[x] != NULL) {
if (IS_SYNC(key->inode) && buffer_dirty(key->ibh[x])) {
//mw:
assert(buffer_mapped(key->ibh[x])
&& (key->ibh[x]->b_bdev != NULL));
ll_rw_block(WRITE, 1, &(key->ibh[x]));
wait_on_buffer(key->ibh[x]);
}
brelse(key->ibh[x]);
}
/* Paul Whittaker tweak 19 Feb 2005 */
if ((key->ptr[x - 1] != NULL)
&& ((tmp = le32_to_cpu(key->ptr[x - 1][key->off[x - 1]])) !=
0)) {
if ((key->ibh[x] =
__bread(key->inode->i_sb->s_bdev, tmp,
key->inode->i_sb->s_blocksize))
!= NULL)
key->ptr[x] = (u32 *) (key->ibh[x]->b_data);
else
key->ptr[x] = NULL;
} else {
key->ibh[x] = NULL;
key->ptr[x] = NULL;
}
}
return 1;
}
/* Method to free the key: just release buffers.
Returns 0 on success, -errno on error.
*/
static int ext2_free_key(struct ext2_bkey *key)
{
int x, n;
struct buffer_head *bh[4];
assert(key->inode);
assert(atomic_read(&(key->inode)->i_mutex.count) <= 0);
for (x = 0, n = 0; x <= key->level; x++) {
if (key->ibh[x] != NULL) {
if (IS_SYNC(key->inode) && buffer_dirty(key->ibh[x]))
bh[n++] = key->ibh[x];
else
brelse(key->ibh[x]);
}
}
if (n > 0) {
int ncopy = n;
while (ncopy-- > 0) {
assert(buffer_mapped(bh[ncopy])
&& (bh[ncopy]->b_bdev != NULL));
}
ll_rw_block(WRITE, n, bh);
CHECK_NOT_ATOMIC
while (n-- > 0) {
wait_on_buffer(bh[n]);
/* TODO: Check for error. */
brelse(bh[n]);
}
}
return 0;
}
/* Returns positive if specified cluster is compressed,
zero if not,
-errno if an error occurred.
If you need the result to be accurate, then down i_sem before
calling this, and don't raise i_sem until after you've used the
result. */
int ext2_cluster_is_compressed_fn(struct inode *inode, unsigned cluster)
{
unsigned block = (ext2_cluster_block0(inode, cluster)
+ ext2_cluster_nblocks(inode, cluster)
- 1);
struct ext2_bkey key;
int result;
assert(atomic_read(&inode->i_mutex.count) <= 0);
/* impl: Not all callers of ext2_cluster_is_compressed_fn() have
i_sem down. Of course it is impossible to guarantee
up-to-date information for such callers (someone may
compress or decompress between when we check and when they
use the information), so hopefully it won't matter if the
information we return is slightly inaccurate (e.g. because
someone is de/compressing the cluster while we check). */
if (!ext2_get_key(&key, inode, block))
return -EIO;
result = (ext2_get_key_blkaddr(&key) == EXT2_COMPRESSED_BLKADDR);
ext2_free_key(&key);
return result;
}
/* Support for the GETCOMPRRATIO ioctl() call. We calculate how many
blocks the file would hold if it weren't compressed. This requires
reading the cluster head for every compressed cluster.
Returns either -EAGAIN or the number of blocks that the file would
take up if uncompressed. */
int ext2_count_blocks(struct inode *inode)
{
struct buffer_head *head_bh;
int count;
int cluster;
struct ext2_bkey key;
u32 end_blknr;
if (!(EXT2_I(inode)->i_flags & EXT2_COMPRBLK_FL))
return inode->i_blocks;
mutex_lock(&inode->i_mutex);
end_blknr = ROUNDUP_RSHIFT(inode->i_size,
inode->i_sb->s_blocksize_bits);
/* inode->i_blocks is stored in units of 512-byte blocks. It's
more convenient for us to work in units of s_blocksize. */
{
u32 shift = inode->i_sb->s_blocksize_bits - 9;
count = inode->i_blocks;
if (count & ((1 << shift) - 1))
ext2_msg(inode->i_sb,
"ext2_count_blocks",
"i_blocks not multiple of blocksize");
count >>= shift;
}
cluster = 0;
if (!ext2_get_key(&key, inode, 0)) {
count = -EIO;
goto out;
}
while (key.block < end_blknr) {
u32 head_blkaddr = ext2_get_key_blkaddr(&key);
/* bug fix: init head_bh for each iteration TLL 2/21/07 */
head_bh = NULL;
if (head_blkaddr == EXT2_COMPRESSED_BLKADDR) {
count = -EXT2_ECOMPR;
break;
}
if (!ext2_next_key(&key, ext2_cluster_nblocks(inode, cluster) - 1))
break;
if (ext2_get_key_blkaddr(&key) == EXT2_COMPRESSED_BLKADDR) {
struct ext2_cluster_head *head;
if (head_blkaddr == 0) {
count = -EXT2_ECOMPR;
break;
}
head_bh = __getblk(inode->i_sb->s_bdev,
head_blkaddr, inode->i_sb->s_blocksize);
if (head_bh == NULL) {
/* Hmm, EAGAIN or EIO? */
count = -EAGAIN;
break;
}
if (!buffer_uptodate(head_bh))
ll_rw_block(READ, 1, &head_bh);
CHECK_NOT_ATOMIC
wait_on_buffer(head_bh);
#ifdef CONFIG_HIGHMEM
if (!page_address(head_bh->b_page)) {
BUG();
}