Skip to content

Commit cb680c1

Browse files
jeffmahoneyLinus Torvalds
authored andcommitted
reiserfs: ignore on disk s_bmap_nr value
Implement support for file systems larger than 8 TiB. The reiserfs superblock contains a 16 bit value for counting the number of bitmap blocks. The rest of the disk format supports file systems up to 2^32 blocks, but the bitmap block limitation artificially limits this to 8 TiB with a 4KiB block size. Rather than trust the superblock's 16-bit bitmap block count, we calculate it dynamically based on the number of blocks in the file system. When an incorrect value is observed in the superblock, it is zeroed out, ensuring that older kernels will not be able to mount the file system. Userspace support has already been implemented and shipped in reiserfsprogs 3.6.20. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4d20851 commit cb680c1

5 files changed

Lines changed: 56 additions & 23 deletions

File tree

‎fs/reiserfs/bitmap.c‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static inline void get_bit_address(struct super_block *s,
6161
int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
6262
{
6363
unsigned int bmap, offset;
64+
unsigned int bmap_count = reiserfs_bmap_count(s);
6465

6566
if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
6667
reiserfs_warning(s,
@@ -76,25 +77,26 @@ int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
7677
if (unlikely(test_bit(REISERFS_OLD_FORMAT,
7778
&(REISERFS_SB(s)->s_properties)))) {
7879
b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
79-
if (block >= bmap1 && block <= bmap1 + SB_BMAP_NR(s)) {
80+
if (block >= bmap1 &&
81+
block <= bmap1 + bmap_count) {
8082
reiserfs_warning(s, "vs: 4019: is_reusable: "
8183
"bitmap block %lu(%u) can't be freed or reused",
82-
block, SB_BMAP_NR(s));
84+
block, bmap_count);
8385
return 0;
8486
}
8587
} else {
8688
if (offset == 0) {
8789
reiserfs_warning(s, "vs: 4020: is_reusable: "
8890
"bitmap block %lu(%u) can't be freed or reused",
89-
block, SB_BMAP_NR(s));
91+
block, bmap_count);
9092
return 0;
9193
}
9294
}
9395

94-
if (bmap >= SB_BMAP_NR(s)) {
96+
if (bmap >= bmap_count) {
9597
reiserfs_warning(s,
9698
"vs-4030: is_reusable: there is no so many bitmap blocks: "
97-
"block=%lu, bitmap_nr=%d", block, bmap);
99+
"block=%lu, bitmap_nr=%u", block, bmap);
98100
return 0;
99101
}
100102

@@ -143,8 +145,8 @@ static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
143145

144146
BUG_ON(!th->t_trans_id);
145147

146-
RFALSE(bmap_n >= SB_BMAP_NR(s), "Bitmap %d is out of range (0..%d)",
147-
bmap_n, SB_BMAP_NR(s) - 1);
148+
RFALSE(bmap_n >= reiserfs_bmap_count(s), "Bitmap %u is out of "
149+
"range (0..%u)", bmap_n, reiserfs_bmap_count(s) - 1);
148150
PROC_INFO_INC(s, scan_bitmap.bmap);
149151
/* this is unclear and lacks comments, explain how journal bitmaps
150152
work here for the reader. Convey a sense of the design here. What
@@ -249,12 +251,12 @@ static int bmap_hash_id(struct super_block *s, u32 id)
249251
} else {
250252
hash_in = (char *)(&id);
251253
hash = keyed_hash(hash_in, 4);
252-
bm = hash % SB_BMAP_NR(s);
254+
bm = hash % reiserfs_bmap_count(s);
253255
if (!bm)
254256
bm = 1;
255257
}
256258
/* this can only be true when SB_BMAP_NR = 1 */
257-
if (bm >= SB_BMAP_NR(s))
259+
if (bm >= reiserfs_bmap_count(s))
258260
bm = 0;
259261
return bm;
260262
}
@@ -328,10 +330,10 @@ static int scan_bitmap(struct reiserfs_transaction_handle *th,
328330

329331
get_bit_address(s, *start, &bm, &off);
330332
get_bit_address(s, finish, &end_bm, &end_off);
331-
if (bm > SB_BMAP_NR(s))
333+
if (bm > reiserfs_bmap_count(s))
332334
return 0;
333-
if (end_bm > SB_BMAP_NR(s))
334-
end_bm = SB_BMAP_NR(s);
335+
if (end_bm > reiserfs_bmap_count(s))
336+
end_bm = reiserfs_bmap_count(s);
335337

336338
/* When the bitmap is more than 10% free, anyone can allocate.
337339
* When it's less than 10% free, only files that already use the
@@ -397,10 +399,12 @@ static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
397399

398400
get_bit_address(s, block, &nr, &offset);
399401

400-
if (nr >= sb_bmap_nr(rs)) {
402+
if (nr >= reiserfs_bmap_count(s)) {
401403
reiserfs_warning(s, "vs-4075: reiserfs_free_block: "
402-
"block %lu is out of range on %s",
403-
block, reiserfs_bdevname(s));
404+
"block %lu is out of range on %s "
405+
"(nr=%u,max=%u)", block,
406+
reiserfs_bdevname(s), nr,
407+
reiserfs_bmap_count(s));
404408
return;
405409
}
406410

@@ -1269,12 +1273,13 @@ struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
12691273
int reiserfs_init_bitmap_cache(struct super_block *sb)
12701274
{
12711275
struct reiserfs_bitmap_info *bitmap;
1276+
unsigned int bmap_nr = reiserfs_bmap_count(sb);
12721277

1273-
bitmap = vmalloc(sizeof (*bitmap) * SB_BMAP_NR(sb));
1278+
bitmap = vmalloc(sizeof(*bitmap) * bmap_nr);
12741279
if (bitmap == NULL)
12751280
return -ENOMEM;
12761281

1277-
memset(bitmap, 0xff, sizeof(*bitmap) * SB_BMAP_NR(sb));
1282+
memset(bitmap, 0xff, sizeof(*bitmap) * bmap_nr);
12781283

12791284
SB_AP_BITMAP(sb) = bitmap;
12801285

‎fs/reiserfs/journal.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static void cleanup_bitmap_list(struct super_block *p_s_sb,
240240
if (jb->bitmaps == NULL)
241241
return;
242242

243-
for (i = 0; i < SB_BMAP_NR(p_s_sb); i++) {
243+
for (i = 0; i < reiserfs_bmap_count(p_s_sb); i++) {
244244
if (jb->bitmaps[i]) {
245245
free_bitmap_node(p_s_sb, jb->bitmaps[i]);
246246
jb->bitmaps[i] = NULL;
@@ -2734,15 +2734,15 @@ int journal_init(struct super_block *p_s_sb, const char *j_dev_name,
27342734
journal->j_persistent_trans = 0;
27352735
if (reiserfs_allocate_list_bitmaps(p_s_sb,
27362736
journal->j_list_bitmap,
2737-
SB_BMAP_NR(p_s_sb)))
2737+
reiserfs_bmap_count(p_s_sb)))
27382738
goto free_and_return;
27392739
allocate_bitmap_nodes(p_s_sb);
27402740

27412741
/* reserved for journal area support */
27422742
SB_JOURNAL_1st_RESERVED_BLOCK(p_s_sb) = (old_format ?
27432743
REISERFS_OLD_DISK_OFFSET_IN_BYTES
27442744
/ p_s_sb->s_blocksize +
2745-
SB_BMAP_NR(p_s_sb) +
2745+
reiserfs_bmap_count(p_s_sb) +
27462746
1 :
27472747
REISERFS_DISK_OFFSET_IN_BYTES /
27482748
p_s_sb->s_blocksize + 2);

‎fs/reiserfs/resize.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ int reiserfs_resize(struct super_block *s, unsigned long block_count_new)
6161
}
6262

6363
/* count used bits in last bitmap block */
64-
block_r = SB_BLOCK_COUNT(s) - (SB_BMAP_NR(s) - 1) * s->s_blocksize * 8;
64+
block_r = SB_BLOCK_COUNT(s) -
65+
(reiserfs_bmap_count(s) - 1) * s->s_blocksize * 8;
6566

6667
/* count bitmap blocks in new fs */
6768
bmap_nr_new = block_count_new / (s->s_blocksize * 8);
@@ -73,7 +74,7 @@ int reiserfs_resize(struct super_block *s, unsigned long block_count_new)
7374

7475
/* save old values */
7576
block_count = SB_BLOCK_COUNT(s);
76-
bmap_nr = SB_BMAP_NR(s);
77+
bmap_nr = reiserfs_bmap_count(s);
7778

7879
/* resizing of reiserfs bitmaps (journal and real), if needed */
7980
if (bmap_nr_new > bmap_nr) {
@@ -200,7 +201,7 @@ int reiserfs_resize(struct super_block *s, unsigned long block_count_new)
200201
free_blocks + (block_count_new - block_count -
201202
(bmap_nr_new - bmap_nr)));
202203
PUT_SB_BLOCK_COUNT(s, block_count_new);
203-
PUT_SB_BMAP_NR(s, bmap_nr_new);
204+
PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new);
204205
s->s_dirt = 1;
205206

206207
journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));

‎fs/reiserfs/super.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,21 @@ static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
17251725
set_sb_umount_state(rs, REISERFS_ERROR_FS);
17261726
set_sb_fs_state(rs, 0);
17271727

1728+
/* Clear out s_bmap_nr if it would wrap. We can handle this
1729+
* case, but older revisions can't. This will cause the
1730+
* file system to fail mount on those older implementations,
1731+
* avoiding corruption. -jeffm */
1732+
if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
1733+
sb_bmap_nr(rs) != 0) {
1734+
reiserfs_warning(s, "super-2030: This file system "
1735+
"claims to use %u bitmap blocks in "
1736+
"its super block, but requires %u. "
1737+
"Clearing to zero.", sb_bmap_nr(rs),
1738+
reiserfs_bmap_count(s));
1739+
1740+
set_sb_bmap_nr(rs, 0);
1741+
}
1742+
17281743
if (old_format_only(s)) {
17291744
/* filesystem of format 3.5 either with standard or non-standard
17301745
journal */

‎include/linux/reiserfs_fs.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ static inline struct reiserfs_sb_info *REISERFS_SB(const struct super_block *sb)
283283
return sb->s_fs_info;
284284
}
285285

286+
/* Don't trust REISERFS_SB(sb)->s_bmap_nr, it's a u16
287+
* which overflows on large file systems. */
288+
static inline u32 reiserfs_bmap_count(struct super_block *sb)
289+
{
290+
return (SB_BLOCK_COUNT(sb) - 1) / (sb->s_blocksize * 8) + 1;
291+
}
292+
293+
static inline int bmap_would_wrap(unsigned bmap_nr)
294+
{
295+
return bmap_nr > ((1LL << 16) - 1);
296+
}
297+
286298
/** this says about version of key of all items (but stat data) the
287299
object consists of */
288300
#define get_inode_item_key_version( inode ) \

0 commit comments

Comments
 (0)