4 * Copyright (C) 2011 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
23 #include <sys/types.h>
30 extern ss_request_table tst_bitmaps_cmds;
32 static char subsystem_name[] = "tst_bitmaps";
33 static char version[] = "1.0";
38 static int source_file(const char *cmd_file, int sci_idx)
46 if (strcmp(cmd_file, "-") == 0)
49 f = fopen(cmd_file, "r");
60 if (fgets(buf, sizeof(buf), f) == NULL)
69 cp = strchr(buf, '\n');
72 cp = strchr(buf, '\r');
76 printf("%s: %s\n", subsystem_name, buf);
77 retval = ss_execute_line(sci_idx, buf);
79 ss_perror(sci_idx, retval, buf);
88 * This function resets the libc getopt() function, which keeps
89 * internal state. Bad design! Stupid libc API designers! No
92 * BSD-derived getopt() functions require that optind be reset to 1 in
93 * order to reset getopt() state. This used to be generally accepted
94 * way of resetting getopt(). However, glibc's getopt()
95 * has additional getopt() state beyond optind, and requires that
96 * optind be set zero to reset its state. So the unfortunate state of
97 * affairs is that BSD-derived versions of getopt() misbehave if
98 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
99 * will core dump if optind is set 1 in order to reset getopt().
101 * More modern versions of BSD require that optreset be set to 1 in
102 * order to reset getopt(). Sigh. Standards, anyone?
104 * We hide the hair here.
106 void reset_getopt(void)
108 #if defined(__GLIBC__) || defined(__linux__)
114 optreset = 1; /* Makes BSD getopt happy */
119 * This function will convert a string to an unsigned long, printing
120 * an error message if it fails, and returning success or failure in err.
122 unsigned long parse_ulong(const char *str, const char *cmd,
123 const char *descr, int *err)
128 ret = strtoul(str, &tmp, 0);
134 com_err(cmd, 0, "Bad %s - %s", descr, str);
143 int check_fs_open(char *name)
146 com_err(name, 0, "Filesystem not open");
152 static void setup_filesystem(const char *name,
153 unsigned int blocks, unsigned int inodes,
156 struct ext2_super_block param;
159 memset(¶m, 0, sizeof(param));
160 ext2fs_blocks_count_set(¶m, blocks);
161 param.s_inodes_count = inodes;
163 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m,
164 test_io_manager, &test_fs);
167 com_err(name, retval, "while initializing filesystem");
170 test_fs->default_bitmap_type = type;
171 ext2fs_free_block_bitmap(test_fs->block_map);
172 test_fs->block_map = 0;
173 ext2fs_free_inode_bitmap(test_fs->inode_map);
174 test_fs->inode_map = 0;
175 retval = ext2fs_allocate_block_bitmap(test_fs, "block bitmap",
176 &test_fs->block_map);
178 com_err(name, retval, "while allocating block bitmap");
181 retval = ext2fs_allocate_inode_bitmap(test_fs, "inode bitmap",
182 &test_fs->inode_map);
184 com_err(name, retval, "while allocating inode bitmap");
190 ext2fs_close(test_fs);
194 void setup_cmd(int argc, char **argv)
198 unsigned int blocks = 128;
199 unsigned int inodes = 0;
200 unsigned int type = EXT2FS_BMAP64_BITARRAY;
203 ext2fs_close(test_fs);
208 while ((c = getopt(argc, argv, "b:i:t:")) != EOF) {
211 blocks = parse_ulong(optarg, argv[0],
212 "number of blocks", &err);
217 inodes = parse_ulong(optarg, argv[0],
218 "number of blocks", &err);
223 type = parse_ulong(optarg, argv[0],
224 "bitmap backend type", &err);
229 fprintf(stderr, "%s: usage: setup [-b blocks] "
230 "[-i inodes] [-t type]\n", argv[0]);
234 setup_filesystem(argv[0], blocks, inodes, type);
237 void close_cmd(int argc, char **argv)
239 if (check_fs_open(argv[0]))
242 ext2fs_close(test_fs);
247 void dump_bitmap(ext2fs_generic_bitmap bmap, unsigned int start, unsigned num)
251 int i, len = (num - start + 7) / 8;
255 com_err("dump_bitmap", 0, "couldn't allocate buffer");
259 retval = ext2fs_get_generic_bmap_range(bmap, (__u64) start, num, buf);
261 com_err("dump_bitmap", retval,
262 "while calling ext2fs_generic_bmap_range");
266 for (i=0; i < len; i++)
267 printf("%02x", buf[i]);
272 void dump_inode_bitmap_cmd(int argc, char **argv)
274 if (check_fs_open(argv[0]))
277 printf("inode bitmap: ");
278 dump_bitmap(test_fs->inode_map, 1, test_fs->super->s_inodes_count);
281 void dump_block_bitmap_cmd(int argc, char **argv)
283 if (check_fs_open(argv[0]))
286 printf("block bitmap: ");
287 dump_bitmap(test_fs->block_map, test_fs->super->s_first_data_block,
288 test_fs->super->s_blocks_count);
291 void do_setb(int argc, char *argv[])
293 unsigned int block, num;
295 int test_result, op_result;
297 if (check_fs_open(argv[0]))
300 if (argc != 2 && argc != 3) {
301 com_err(argv[0], 0, "Usage: setb <block> [num]");
305 block = parse_ulong(argv[1], argv[0], "block", &err);
310 num = parse_ulong(argv[2], argv[0], "num", &err);
314 ext2fs_mark_block_bitmap_range2(test_fs->block_map,
316 printf("Marking blocks %u to %u\n", block, block + num - 1);
320 test_result = ext2fs_test_block_bitmap2(test_fs->block_map, block);
321 op_result = ext2fs_mark_block_bitmap2(test_fs->block_map, block);
322 printf("Setting block %u, was %s before\n", block, op_result ?
324 if (!test_result != !op_result)
325 com_err(argv[0], 0, "*ERROR* test_result different! (%d, %d)",
326 test_result, op_result);
329 void do_clearb(int argc, char *argv[])
331 unsigned int block, num;
333 int test_result, op_result;
335 if (check_fs_open(argv[0]))
338 if (argc != 2 && argc != 3) {
339 com_err(argv[0], 0, "Usage: clearb <block> [num]");
343 block = parse_ulong(argv[1], argv[0], "block", &err);
348 num = parse_ulong(argv[2], argv[0], "num", &err);
352 ext2fs_unmark_block_bitmap_range2(test_fs->block_map,
354 printf("Clearing blocks %u to %u\n", block, block + num - 1);
358 test_result = ext2fs_test_block_bitmap2(test_fs->block_map, block);
359 op_result = ext2fs_unmark_block_bitmap2(test_fs->block_map, block);
360 printf("Clearing block %u, was %s before\n", block, op_result ?
362 if (!test_result != !op_result)
363 com_err(argv[0], 0, "*ERROR* test_result different! (%d, %d)",
364 test_result, op_result);
367 void do_testb(int argc, char *argv[])
369 unsigned int block, num;
371 int test_result, op_result;
373 if (check_fs_open(argv[0]))
376 if (argc != 2 && argc != 3) {
377 com_err(argv[0], 0, "Usage: testb <block> [num]");
381 block = parse_ulong(argv[1], argv[0], "block", &err);
386 num = parse_ulong(argv[2], argv[0], "num", &err);
391 ext2fs_test_block_bitmap_range2(test_fs->block_map,
393 printf("Blocks %u to %u are %sall clear.\n",
394 block, block + num - 1, test_result ? "" : "NOT ");
398 test_result = ext2fs_test_block_bitmap2(test_fs->block_map, block);
399 printf("Block %u is %s\n", block, test_result ? "set" : "clear");
402 void do_zerob(int argc, char *argv[])
404 if (check_fs_open(argv[0]))
407 printf("Clearing block bitmap.\n");
408 ext2fs_clear_block_bitmap(test_fs->block_map);
411 void do_seti(int argc, char *argv[])
415 int test_result, op_result;
417 if (check_fs_open(argv[0]))
421 com_err(argv[0], 0, "Usage: seti <inode>");
425 inode = parse_ulong(argv[1], argv[0], "inode", &err);
429 test_result = ext2fs_test_inode_bitmap2(test_fs->inode_map, inode);
430 op_result = ext2fs_mark_inode_bitmap2(test_fs->inode_map, inode);
431 printf("Setting inode %u, was %s before\n", inode, op_result ?
433 if (!test_result != !op_result) {
434 com_err(argv[0], 0, "*ERROR* test_result different! (%d, %d)",
435 test_result, op_result);
440 void do_cleari(int argc, char *argv[])
444 int test_result, op_result;
446 if (check_fs_open(argv[0]))
450 com_err(argv[0], 0, "Usage: clearb <inode>");
454 inode = parse_ulong(argv[1], argv[0], "inode", &err);
458 test_result = ext2fs_test_inode_bitmap2(test_fs->inode_map, inode);
459 op_result = ext2fs_unmark_inode_bitmap2(test_fs->inode_map, inode);
460 printf("Clearing inode %u, was %s before\n", inode, op_result ?
462 if (!test_result != !op_result) {
463 com_err(argv[0], 0, "*ERROR* test_result different! (%d, %d)",
464 test_result, op_result);
469 void do_testi(int argc, char *argv[])
473 int test_result, op_result;
475 if (check_fs_open(argv[0]))
479 com_err(argv[0], 0, "Usage: testb <inode>");
483 inode = parse_ulong(argv[1], argv[0], "inode", &err);
487 test_result = ext2fs_test_inode_bitmap2(test_fs->inode_map, inode);
488 printf("Inode %u is %s\n", inode, test_result ? "set" : "clear");
491 void do_zeroi(int argc, char *argv[])
493 if (check_fs_open(argv[0]))
496 printf("Clearing inode bitmap.\n");
497 ext2fs_clear_inode_bitmap(test_fs->inode_map);
500 int main(int argc, char **argv)
502 unsigned int blocks = 128;
503 unsigned int inodes = 0;
504 unsigned int type = EXT2FS_BMAP64_BITARRAY;
506 char *request = (char *)NULL;
510 add_error_table(&et_ss_error_table);
511 add_error_table(&et_ext2_error_table);
512 while ((c = getopt (argc, argv, "b:i:t:R:f:")) != EOF) {
515 blocks = parse_ulong(optarg, argv[0],
516 "number of blocks", &err);
521 inodes = parse_ulong(optarg, argv[0],
522 "number of blocks", &err);
527 type = parse_ulong(optarg, argv[0],
528 "bitmap backend type", &err);
539 com_err(argv[0], 0, "Usage: %s [-R request] "
540 "[-f cmd_file]", subsystem_name);
545 sci_idx = ss_create_invocation(subsystem_name, version,
546 (char *)NULL, &tst_bitmaps_cmds, &code);
548 ss_perror(sci_idx, code, "creating invocation");
552 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &code);
554 ss_perror(sci_idx, code, "adding standard requests");
558 printf("%s %s. Type '?' for a list of commands.\n\n",
559 subsystem_name, version);
561 setup_filesystem(argv[0], blocks, inodes, type);
564 code = ss_execute_line(sci_idx, request);
566 ss_perror(sci_idx, code, request);
569 } else if (cmd_file) {
570 exit_status = source_file(cmd_file, sci_idx);