blob: 875be9fca59370263fd099debdbdb50d57c3ef86 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * read_bb --- read the bad blocks inode
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <fcntl.h>
18#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include "ext2fs.h"
28
29struct read_bb_record {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000030 ext2_badblocks_list bb_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +000031 errcode_t err;
32};
33
34/*
35 * Helper function for ext2fs_read_bb_inode()
36 */
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000037#ifdef __TURBOC__
Theodore Ts'o48e6e812003-07-06 00:36:48 -040038 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000039#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000040static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -050041 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
42 blk_t ref_block EXT2FS_ATTR((unused)),
43 int ref_offset EXT2FS_ATTR((unused)),
44 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +000045{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000046 struct read_bb_record *rb = (struct read_bb_record *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +000047
48 if (blockcnt < 0)
49 return 0;
50
Theodore Ts'o4faba5b1998-06-16 05:23:41 +000051 if ((*block_nr < fs->super->s_first_data_block) ||
52 (*block_nr >= fs->super->s_blocks_count))
53 return 0; /* Ignore illegal blocks */
54
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000055 rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +000056 if (rb->err)
57 return BLOCK_ABORT;
58 return 0;
59}
60
61/*
62 * Reads the current bad blocks from the bad blocks inode.
63 */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000064errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +000065{
66 errcode_t retval;
67 struct read_bb_record rb;
68 struct ext2_inode inode;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000069 blk_t numblocks;
Theodore Ts'o3839e651997-04-26 13:21:57 +000070
Theodore Ts'of3db3561997-04-26 13:34:30 +000071 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
72
Theodore Ts'o3839e651997-04-26 13:21:57 +000073 if (!*bb_list) {
74 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
75 if (retval)
76 return retval;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040077 numblocks = inode.i_blocks;
78 if (!((fs->super->s_feature_ro_compat &
79 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
80 (inode.i_flags & EXT4_HUGE_FILE_FL)))
81 numblocks = numblocks / (fs->blocksize / 512);
82 numblocks += 20;
83 if (numblocks < 50)
84 numblocks = 50;
85 if (numblocks > 50000)
Theodore Ts'o4faba5b1998-06-16 05:23:41 +000086 numblocks = 500;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000087 retval = ext2fs_badblocks_list_create(bb_list, numblocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 if (retval)
89 return retval;
90 }
91
92 rb.bb_list = *bb_list;
93 rb.err = 0;
Theodore Ts'o357d1862008-02-02 21:26:54 -050094 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, BLOCK_FLAG_READ_ONLY,
95 0, mark_bad_block, &rb);
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 if (retval)
97 return retval;
98
99 return rb.err;
100}
101
102