xref: /third_party/ltp/testcases/kernel/fs/mongo/map5.c (revision f08c3bdf)
1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3f08c3bdfSopenharmony_ci */
4f08c3bdfSopenharmony_ci
5f08c3bdfSopenharmony_ci#include <stdio.h>
6f08c3bdfSopenharmony_ci#include <sys/types.h>
7f08c3bdfSopenharmony_ci#include <sys/stat.h>
8f08c3bdfSopenharmony_ci#include <fcntl.h>
9f08c3bdfSopenharmony_ci#include <linux/fs.h>
10f08c3bdfSopenharmony_ci#include <errno.h>
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ciint main(int argc, char **argv)
13f08c3bdfSopenharmony_ci{
14f08c3bdfSopenharmony_ci	int fd;
15f08c3bdfSopenharmony_ci	int block;
16f08c3bdfSopenharmony_ci	int first_block;
17f08c3bdfSopenharmony_ci	int last_block;
18f08c3bdfSopenharmony_ci	int totals_block;
19f08c3bdfSopenharmony_ci	int fragments;
20f08c3bdfSopenharmony_ci	int i;
21f08c3bdfSopenharmony_ci	int n;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci	for (n = 1; n < argc; n++) {
24f08c3bdfSopenharmony_ci		if (argc < 2) {
25f08c3bdfSopenharmony_ci			printf
26f08c3bdfSopenharmony_ci			    ("Used to see file maps \nUsage: %s filename1 [[..[filename2]...filename(N-1)] filenameN]\n",
27f08c3bdfSopenharmony_ci			     argv[0]);
28f08c3bdfSopenharmony_ci			return 0;
29f08c3bdfSopenharmony_ci		}
30f08c3bdfSopenharmony_ci		fd = open(argv[n], O_RDONLY);
31f08c3bdfSopenharmony_ci		if (fd == -1) {
32f08c3bdfSopenharmony_ci			perror("open failed");
33f08c3bdfSopenharmony_ci			continue;
34f08c3bdfSopenharmony_ci		}
35f08c3bdfSopenharmony_ci		// printf ("file %s occupies blocks: \n", argv[1]);
36f08c3bdfSopenharmony_ci		// printf ("START\tEND\tCOUNT\n");
37f08c3bdfSopenharmony_ci		i = 0;
38f08c3bdfSopenharmony_ci		block = 0;
39f08c3bdfSopenharmony_ci		first_block = 0;
40f08c3bdfSopenharmony_ci		last_block = 0;
41f08c3bdfSopenharmony_ci		fragments = 0;
42f08c3bdfSopenharmony_ci		totals_block = 0;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci		while (ioctl(fd, FIBMAP, &block) == 0) {
45f08c3bdfSopenharmony_ci			if (first_block == 0) {
46f08c3bdfSopenharmony_ci				last_block = block - 1;
47f08c3bdfSopenharmony_ci				first_block = block;
48f08c3bdfSopenharmony_ci			}
49f08c3bdfSopenharmony_ci			if (block != last_block + 1) {
50f08c3bdfSopenharmony_ci				// printf ("%d\t%d\t%d\n",first_block,last_block,last_block-first_block+1);
51f08c3bdfSopenharmony_ci				totals_block += last_block - first_block + 1;
52f08c3bdfSopenharmony_ci				fragments++;
53f08c3bdfSopenharmony_ci				first_block = block;
54f08c3bdfSopenharmony_ci				last_block = block;
55f08c3bdfSopenharmony_ci			} else {
56f08c3bdfSopenharmony_ci				last_block++;
57f08c3bdfSopenharmony_ci			}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci			if (!block) {
60f08c3bdfSopenharmony_ci				//printf ("Fragments: %d\tBlocks: %d\n",fragments,totals_block);
61f08c3bdfSopenharmony_ci				//printf ("%d:%d\t",fragments,totals_block);
62f08c3bdfSopenharmony_ci				//if (fragments == 1) printf(".",totals_block);
63f08c3bdfSopenharmony_ci				//else printf("%d_",fragments,totals_block);
64f08c3bdfSopenharmony_ci				printf("%d\n", fragments);
65f08c3bdfSopenharmony_ci				break;
66f08c3bdfSopenharmony_ci			}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci			i++;
69f08c3bdfSopenharmony_ci			block = i;
70f08c3bdfSopenharmony_ci		}
71f08c3bdfSopenharmony_ci		if (errno) {
72f08c3bdfSopenharmony_ci			perror("FIBMAP failed");
73f08c3bdfSopenharmony_ci		}
74f08c3bdfSopenharmony_ci		close(fd);
75f08c3bdfSopenharmony_ci		// printf ("\n");
76f08c3bdfSopenharmony_ci	}
77f08c3bdfSopenharmony_ci	return 0;
78f08c3bdfSopenharmony_ci}
79