blob: b5a0d7b49be160b03597406fc985cf59620e0953 [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%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <fcntl.h>
19#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000020#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#endif
23#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000026
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000027#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include "ext2fs.h"
29
30struct read_bb_record {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000031 ext2_badblocks_list bb_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +000032 errcode_t err;
33};
34
35/*
36 * Helper function for ext2fs_read_bb_inode()
37 */
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000038#ifdef __TURBOC__
Theodore Ts'o48e6e812003-07-06 00:36:48 -040039 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000040#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000041static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'oefc6f622008-08-27 23:07:54 -040042 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -050043 blk_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'oefc6f622008-08-27 23:07:54 -040044 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -050045 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +000046{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000047 struct read_bb_record *rb = (struct read_bb_record *) priv_data;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040048
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 if (blockcnt < 0)
50 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040051
Theodore Ts'o4faba5b1998-06-16 05:23:41 +000052 if ((*block_nr < fs->super->s_first_data_block) ||
Valerie Aurora Henson6d8b37f2010-06-13 11:00:00 -040053 (*block_nr >= ext2fs_blocks_count(fs->super)))
Theodore Ts'o4faba5b1998-06-16 05:23:41 +000054 return 0; /* Ignore illegal blocks */
55
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000056 rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 if (rb->err)
58 return BLOCK_ABORT;
59 return 0;
60}
61
62/*
63 * Reads the current bad blocks from the bad blocks inode.
64 */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000065errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +000066{
67 errcode_t retval;
68 struct read_bb_record rb;
69 struct ext2_inode inode;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000070 blk_t numblocks;
Theodore Ts'o3839e651997-04-26 13:21:57 +000071
Theodore Ts'of3db3561997-04-26 13:34:30 +000072 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
73
Theodore Ts'o3839e651997-04-26 13:21:57 +000074 if (!*bb_list) {
75 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
76 if (retval)
77 return retval;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040078 numblocks = inode.i_blocks;
79 if (!((fs->super->s_feature_ro_compat &
80 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
81 (inode.i_flags & EXT4_HUGE_FILE_FL)))
82 numblocks = numblocks / (fs->blocksize / 512);
83 numblocks += 20;
84 if (numblocks < 50)
85 numblocks = 50;
86 if (numblocks > 50000)
Theodore Ts'o4faba5b1998-06-16 05:23:41 +000087 numblocks = 500;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000088 retval = ext2fs_badblocks_list_create(bb_list, numblocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +000089 if (retval)
90 return retval;
91 }
92
93 rb.bb_list = *bb_list;
94 rb.err = 0;
Theodore Ts'o357d1862008-02-02 21:26:54 -050095 retval = ext2fs_block_iterate2(fs, EXT2_BAD_INO, BLOCK_FLAG_READ_ONLY,
96 0, mark_bad_block, &rb);
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 if (retval)
98 return retval;
99
100 return rb.err;
101}
102
103