4 * Copyright (C) 2012 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
18 #include <sys/types.h>
28 void do_zap_block(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
29 void *infop
EXT2FS_ATTR((unused
)))
31 unsigned long pattern
= 0;
42 if (check_fs_open(argv
[0]))
44 if (check_fs_read_write(argv
[0]))
48 while ((c
= getopt (argc
, argv
, "b:f:l:o:p:")) != EOF
) {
54 bit
= parse_ulong(optarg
, argv
[0],
58 if (bit
>= (int) current_fs
->blocksize
* 8) {
59 com_err(argv
[0], 0, "The bit to flip "
60 "must be within a %d block\n",
61 current_fs
->blocksize
);
66 pattern
= parse_ulong(optarg
, argv
[0],
71 com_err(argv
[0], 0, "The fill pattern must "
72 "be an 8-bit value\n");
77 offset
= parse_ulong(optarg
, argv
[0],
81 if (offset
>= (int) current_fs
->blocksize
) {
82 com_err(argv
[0], 0, "The offset must be "
83 "within a %d block\n",
84 current_fs
->blocksize
);
91 length
= parse_ulong(optarg
, argv
[0],
101 if (bit
> 0 && offset
> 0) {
102 com_err(argv
[0], 0, "The -o and -b options can not be mixed.");
109 length
= current_fs
->blocksize
- offset
;
110 if ((offset
+ length
) > (int) current_fs
->blocksize
) {
111 com_err(argv
[0], 0, "The specified length is too bug\n");
115 if (argc
!= optind
+1) {
117 com_err(0, 0, "Usage:\tzap_block [-f file] [-o offset] "
118 "[-l length] [-p pattern] block_num");
119 com_err(0, 0, "\tzap_block [-f file] [-b bit] "
124 block
= parse_ulonglong(argv
[optind
], argv
[0], "block", &err
);
129 inode
= string_to_inode(file
);
132 errcode
= ext2fs_bmap2(current_fs
, inode
, 0, 0, 0,
135 com_err(argv
[0], errcode
,
136 "while mapping logical block %llu\n",
137 (unsigned long long) block
);
142 buf
= malloc(current_fs
->blocksize
);
144 com_err(argv
[0], 0, "Couldn't allocate block buffer");
148 errcode
= io_channel_read_blk64(current_fs
->io
, block
, 1, buf
);
150 com_err(argv
[0], errcode
,
151 "while reading block %llu\n",
152 (unsigned long long) block
);
157 buf
[bit
>> 3] ^= 1 << (bit
& 7);
159 memset(buf
+offset
, pattern
, length
);
161 errcode
= io_channel_write_blk64(current_fs
->io
, block
, 1, buf
);
163 com_err(argv
[0], errcode
,
164 "while write block %llu\n",
165 (unsigned long long) block
);
174 void do_block_dump(int argc
, char *argv
[], int sci_idx
EXT2FS_ATTR((unused
)),
175 void *infop
EXT2FS_ATTR((unused
)))
185 if (check_fs_open(argv
[0]))
189 while ((c
= getopt (argc
, argv
, "f:x")) != EOF
) {
202 if (argc
!= optind
+ 1) {
204 com_err(0, 0, "Usage: block_dump [-x] [-f inode] block_num");
208 block
= parse_ulonglong(argv
[optind
], argv
[0], "block", &err
);
213 inode
= string_to_inode(file
);
216 errcode
= ext2fs_bmap2(current_fs
, inode
, 0, 0, 0,
219 com_err(argv
[0], errcode
,
220 "while mapping logical block %llu\n",
221 (unsigned long long) block
);
226 buf
= malloc(current_fs
->blocksize
);
228 com_err(argv
[0], 0, "Couldn't allocate block buffer");
232 errcode
= io_channel_read_blk64(current_fs
->io
, block
, 1, buf
);
234 com_err(argv
[0], errcode
,
235 "while reading block %llu\n",
236 (unsigned long long) block
);
241 block_xattr_dump(stdout
, buf
, current_fs
->blocksize
);
243 do_byte_hexdump(stdout
, buf
, current_fs
->blocksize
);