1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * ehandler.c --- handle bad block errors which come up during the
3*6a54128fSAndroid Build Coastguard Worker * course of an e2fsck session.
4*6a54128fSAndroid Build Coastguard Worker *
5*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
6*6a54128fSAndroid Build Coastguard Worker * under the terms of the GNU Public License.
7*6a54128fSAndroid Build Coastguard Worker */
8*6a54128fSAndroid Build Coastguard Worker
9*6a54128fSAndroid Build Coastguard Worker #include "config.h"
10*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
11*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
12*6a54128fSAndroid Build Coastguard Worker #include <string.h>
13*6a54128fSAndroid Build Coastguard Worker #include <ctype.h>
14*6a54128fSAndroid Build Coastguard Worker #include <termios.h>
15*6a54128fSAndroid Build Coastguard Worker
16*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
17*6a54128fSAndroid Build Coastguard Worker
18*6a54128fSAndroid Build Coastguard Worker #include <sys/time.h>
19*6a54128fSAndroid Build Coastguard Worker #include <sys/resource.h>
20*6a54128fSAndroid Build Coastguard Worker
21*6a54128fSAndroid Build Coastguard Worker static const char *operation;
22*6a54128fSAndroid Build Coastguard Worker
e2fsck_handle_read_error(io_channel channel,unsigned long block,int count,void * data,size_t size EXT2FS_ATTR ((unused)),int actual EXT2FS_ATTR ((unused)),errcode_t error)23*6a54128fSAndroid Build Coastguard Worker static errcode_t e2fsck_handle_read_error(io_channel channel,
24*6a54128fSAndroid Build Coastguard Worker unsigned long block,
25*6a54128fSAndroid Build Coastguard Worker int count,
26*6a54128fSAndroid Build Coastguard Worker void *data,
27*6a54128fSAndroid Build Coastguard Worker size_t size EXT2FS_ATTR((unused)),
28*6a54128fSAndroid Build Coastguard Worker int actual EXT2FS_ATTR((unused)),
29*6a54128fSAndroid Build Coastguard Worker errcode_t error)
30*6a54128fSAndroid Build Coastguard Worker {
31*6a54128fSAndroid Build Coastguard Worker int i;
32*6a54128fSAndroid Build Coastguard Worker char *p;
33*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = (ext2_filsys) channel->app_data;
34*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
35*6a54128fSAndroid Build Coastguard Worker
36*6a54128fSAndroid Build Coastguard Worker ctx = (e2fsck_t) fs->priv_data;
37*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_EXITING)
38*6a54128fSAndroid Build Coastguard Worker return 0;
39*6a54128fSAndroid Build Coastguard Worker /*
40*6a54128fSAndroid Build Coastguard Worker * If more than one block was read, try reading each block
41*6a54128fSAndroid Build Coastguard Worker * separately. We could use the actual bytes read to figure
42*6a54128fSAndroid Build Coastguard Worker * out where to start, but we don't bother.
43*6a54128fSAndroid Build Coastguard Worker */
44*6a54128fSAndroid Build Coastguard Worker if (count > 1) {
45*6a54128fSAndroid Build Coastguard Worker p = (char *) data;
46*6a54128fSAndroid Build Coastguard Worker for (i=0; i < count; i++, p += channel->block_size, block++) {
47*6a54128fSAndroid Build Coastguard Worker error = io_channel_read_blk64(channel, block,
48*6a54128fSAndroid Build Coastguard Worker 1, p);
49*6a54128fSAndroid Build Coastguard Worker if (error)
50*6a54128fSAndroid Build Coastguard Worker return error;
51*6a54128fSAndroid Build Coastguard Worker }
52*6a54128fSAndroid Build Coastguard Worker return 0;
53*6a54128fSAndroid Build Coastguard Worker }
54*6a54128fSAndroid Build Coastguard Worker if (operation)
55*6a54128fSAndroid Build Coastguard Worker printf(_("Error reading block %lu (%s) while %s. "), block,
56*6a54128fSAndroid Build Coastguard Worker error_message(error), operation);
57*6a54128fSAndroid Build Coastguard Worker else
58*6a54128fSAndroid Build Coastguard Worker printf(_("Error reading block %lu (%s). "), block,
59*6a54128fSAndroid Build Coastguard Worker error_message(error));
60*6a54128fSAndroid Build Coastguard Worker preenhalt(ctx);
61*6a54128fSAndroid Build Coastguard Worker
62*6a54128fSAndroid Build Coastguard Worker /* Don't rewrite a block past the end of the FS. */
63*6a54128fSAndroid Build Coastguard Worker if (block >= ext2fs_blocks_count(fs->super))
64*6a54128fSAndroid Build Coastguard Worker return 0;
65*6a54128fSAndroid Build Coastguard Worker
66*6a54128fSAndroid Build Coastguard Worker if (ask(ctx, _("Ignore error"), 1)) {
67*6a54128fSAndroid Build Coastguard Worker if (ask(ctx, _("Force rewrite"), 1))
68*6a54128fSAndroid Build Coastguard Worker io_channel_write_blk64(channel, block, count, data);
69*6a54128fSAndroid Build Coastguard Worker return 0;
70*6a54128fSAndroid Build Coastguard Worker }
71*6a54128fSAndroid Build Coastguard Worker
72*6a54128fSAndroid Build Coastguard Worker return error;
73*6a54128fSAndroid Build Coastguard Worker }
74*6a54128fSAndroid Build Coastguard Worker
e2fsck_handle_write_error(io_channel channel,unsigned long block,int count,const void * data,size_t size EXT2FS_ATTR ((unused)),int actual EXT2FS_ATTR ((unused)),errcode_t error)75*6a54128fSAndroid Build Coastguard Worker static errcode_t e2fsck_handle_write_error(io_channel channel,
76*6a54128fSAndroid Build Coastguard Worker unsigned long block,
77*6a54128fSAndroid Build Coastguard Worker int count,
78*6a54128fSAndroid Build Coastguard Worker const void *data,
79*6a54128fSAndroid Build Coastguard Worker size_t size EXT2FS_ATTR((unused)),
80*6a54128fSAndroid Build Coastguard Worker int actual EXT2FS_ATTR((unused)),
81*6a54128fSAndroid Build Coastguard Worker errcode_t error)
82*6a54128fSAndroid Build Coastguard Worker {
83*6a54128fSAndroid Build Coastguard Worker int i;
84*6a54128fSAndroid Build Coastguard Worker const char *p;
85*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = (ext2_filsys) channel->app_data;
86*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
87*6a54128fSAndroid Build Coastguard Worker
88*6a54128fSAndroid Build Coastguard Worker ctx = (e2fsck_t) fs->priv_data;
89*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_EXITING)
90*6a54128fSAndroid Build Coastguard Worker return 0;
91*6a54128fSAndroid Build Coastguard Worker
92*6a54128fSAndroid Build Coastguard Worker /*
93*6a54128fSAndroid Build Coastguard Worker * If more than one block was written, try writing each block
94*6a54128fSAndroid Build Coastguard Worker * separately. We could use the actual bytes read to figure
95*6a54128fSAndroid Build Coastguard Worker * out where to start, but we don't bother.
96*6a54128fSAndroid Build Coastguard Worker */
97*6a54128fSAndroid Build Coastguard Worker if (count > 1) {
98*6a54128fSAndroid Build Coastguard Worker p = (const char *) data;
99*6a54128fSAndroid Build Coastguard Worker for (i=0; i < count; i++, p += channel->block_size, block++) {
100*6a54128fSAndroid Build Coastguard Worker error = io_channel_write_blk64(channel, block,
101*6a54128fSAndroid Build Coastguard Worker 1, p);
102*6a54128fSAndroid Build Coastguard Worker if (error)
103*6a54128fSAndroid Build Coastguard Worker return error;
104*6a54128fSAndroid Build Coastguard Worker }
105*6a54128fSAndroid Build Coastguard Worker return 0;
106*6a54128fSAndroid Build Coastguard Worker }
107*6a54128fSAndroid Build Coastguard Worker
108*6a54128fSAndroid Build Coastguard Worker if (operation)
109*6a54128fSAndroid Build Coastguard Worker printf(_("Error writing block %lu (%s) while %s. "), block,
110*6a54128fSAndroid Build Coastguard Worker error_message(error), operation);
111*6a54128fSAndroid Build Coastguard Worker else
112*6a54128fSAndroid Build Coastguard Worker printf(_("Error writing block %lu (%s). "), block,
113*6a54128fSAndroid Build Coastguard Worker error_message(error));
114*6a54128fSAndroid Build Coastguard Worker preenhalt(ctx);
115*6a54128fSAndroid Build Coastguard Worker if (ask(ctx, _("Ignore error"), 1))
116*6a54128fSAndroid Build Coastguard Worker return 0;
117*6a54128fSAndroid Build Coastguard Worker
118*6a54128fSAndroid Build Coastguard Worker return error;
119*6a54128fSAndroid Build Coastguard Worker }
120*6a54128fSAndroid Build Coastguard Worker
ehandler_operation(const char * op)121*6a54128fSAndroid Build Coastguard Worker const char *ehandler_operation(const char *op)
122*6a54128fSAndroid Build Coastguard Worker {
123*6a54128fSAndroid Build Coastguard Worker const char *ret = operation;
124*6a54128fSAndroid Build Coastguard Worker
125*6a54128fSAndroid Build Coastguard Worker operation = op;
126*6a54128fSAndroid Build Coastguard Worker return ret;
127*6a54128fSAndroid Build Coastguard Worker }
128*6a54128fSAndroid Build Coastguard Worker
ehandler_init(io_channel channel)129*6a54128fSAndroid Build Coastguard Worker void ehandler_init(io_channel channel)
130*6a54128fSAndroid Build Coastguard Worker {
131*6a54128fSAndroid Build Coastguard Worker channel->read_error = e2fsck_handle_read_error;
132*6a54128fSAndroid Build Coastguard Worker channel->write_error = e2fsck_handle_write_error;
133*6a54128fSAndroid Build Coastguard Worker }
134