blob: d8c6463aecf1f315fed5310470dd1b2755aa8f16 [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#if EXT2_FLAT_INCLUDES
27#include "ext2_fs.h"
28#else
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000030#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000031
32#include "ext2fs.h"
33
34struct read_bb_record {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000035 ext2_badblocks_list bb_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +000036 errcode_t err;
37};
38
39/*
40 * Helper function for ext2fs_read_bb_inode()
41 */
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000042#ifdef __TURBOC__
43#pragma argsused
44#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000045static int mark_bad_block(ext2_filsys fs, blk_t *block_nr,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000046 int blockcnt, void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +000047{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000048 struct read_bb_record *rb = (struct read_bb_record *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
50 if (blockcnt < 0)
51 return 0;
52
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000053 rb->err = ext2fs_badblocks_list_add(rb->bb_list, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +000054 if (rb->err)
55 return BLOCK_ABORT;
56 return 0;
57}
58
59/*
60 * Reads the current bad blocks from the bad blocks inode.
61 */
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000062errcode_t ext2fs_read_bb_inode(ext2_filsys fs, ext2_badblocks_list *bb_list)
Theodore Ts'o3839e651997-04-26 13:21:57 +000063{
64 errcode_t retval;
65 struct read_bb_record rb;
66 struct ext2_inode inode;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000067 blk_t numblocks;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
Theodore Ts'of3db3561997-04-26 13:34:30 +000069 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
70
Theodore Ts'o3839e651997-04-26 13:21:57 +000071 if (!*bb_list) {
72 retval = ext2fs_read_inode(fs, EXT2_BAD_INO, &inode);
73 if (retval)
74 return retval;
75 numblocks = (inode.i_blocks / (fs->blocksize / 512)) + 20;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000076 retval = ext2fs_badblocks_list_create(bb_list, numblocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +000077 if (retval)
78 return retval;
79 }
80
81 rb.bb_list = *bb_list;
82 rb.err = 0;
83 retval = ext2fs_block_iterate(fs, EXT2_BAD_INO, 0, 0,
84 mark_bad_block, &rb);
85 if (retval)
86 return retval;
87
88 return rb.err;
89}
90
91