Skip to content

Commit

Permalink
add filemap
Browse files Browse the repository at this point in the history
we can use filemap to dump file real extents info. Ex,

0:      0000000000000000 0000000193480000 0000000000001000 0000
1:      0000000000002000 0000000193482000 0000000000001000 0000
2:      0000000000004000 0000000193484000 0000000000001000 0000
3:      0000000000006000 0000000193486000 0000000000001000 0000
4:      0000000000008000 0000000193488000 0000000000001000 0000
5:      000000000000a000 000000019348a000 0000000000001000 0000
6:      000000000000c000 000000019348c000 0000000000001000 0000
7:      000000000000e000 000000019348e000 0000000000001000 0000
8:      0000000000010000 0000000193490000 0000000000001000 0000
9:      0000000000012000 0000000193492000 0000000000001000 0000
10:     0000000000014000 0000000193494000 0000000000001000 0000
11:     0000000000016000 0000000193496000 0000000000001000 0000
12:     0000000000018000 0000000193498000 0000000000001000 0000

Signed-off-by: zhenwei pi <p_ace@126.com>
  • Loading branch information
pizhenwei committed Jul 13, 2018
1 parent 176ef33 commit 74f9cb4
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ GETTIMEOFDAY=gettimeofday
GETALLCHANGELOG=get-all-changelog.py
OBJDUMPFUNCTION=objdump-function.sh
SPARSEFILE=sparse-file
FILEMAP=filemap

.PHONY : $(DTOH) $(HTOD) $(CTIME) $(GETTIMEOFDAY) $(SPARSEFILE)
.PHONY : $(DTOH) $(HTOD) $(CTIME) $(GETTIMEOFDAY) $(SPARSEFILE) $(FILEMAP)

all : $(DTOH) $(HTOD) $(CTIME) $(GETTIMEOFDAY) $(SPARSEFILE)
all : $(DTOH) $(HTOD) $(CTIME) $(GETTIMEOFDAY) $(SPARSEFILE) $(FILEMAP)

$(DTOH) :
gcc $(DTOH).c -O2 -o $(DTOH)
Expand All @@ -25,6 +26,9 @@ $(GETTIMEOFDAY) :
$(SPARSEFILE) :
gcc $(SPARSEFILE).c -O2 -o $(SPARSEFILE)

$(FILEMAP) :
gcc $(FILEMAP).c -O2 -o $(FILEMAP)

install :
@sudo cp $(DTOH) /usr/bin
@sudo cp $(HTOD) /usr/bin
Expand All @@ -33,6 +37,7 @@ install :
@sudo cp $(GETALLCHANGELOG) /usr/bin
@sudo cp $(OBJDUMPFUNCTION) /usr/bin
@sudo cp $(SPARSEFILE) /usr/bin
@sudo cp $(FILEMAP) /usr/bin

clean :
@rm -rf $(DTOH)
Expand All @@ -42,3 +47,4 @@ clean :
@rm -rf $(GETALLCHANGELOG)
@rm -rf $(OBJDUMPFUNCTION)
@rm -rf $(SPARSEFILE)
@rm -rf $(FILEMAP)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ RUN
>
>sparse-file : generate sparse file
> Ex, sparse-file -s 4096 -t 409600000
>
>filemap : dump file extents, logical address, physical address mapping info.
> Ex, filemap test.file
114 changes: 114 additions & 0 deletions filemap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fiemap.h>
#include <linux/fs.h>

#ifndef FS_IOC_FIEMAP
#define FS_IOC_FIEMAP _IOWR('f', 11, struct fiemap)
#endif

void usage(char **argv)
{
fprintf(stderr, "%s [filename] ...\n", argv[0]);
}

struct fiemap *read_fiemap(int fd)
{
struct fiemap *fiemap;
int extents_size;

if ((fiemap = (struct fiemap*)malloc(sizeof(struct fiemap))) == NULL) {
fprintf(stderr, "malloc\n");
return NULL;
}

memset(fiemap, 0, sizeof(struct fiemap));

fiemap->fm_start = 0;
fiemap->fm_length = ~0;
fiemap->fm_flags = 0;
fiemap->fm_extent_count = 0;
fiemap->fm_mapped_extents = 0;

/* count how many extents there are */
if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
fprintf(stderr, "fiemap ioctl() failed\n");
return NULL;
}

/* read in the extents */
extents_size = sizeof(struct fiemap_extent) *
(fiemap->fm_mapped_extents);

/* resize fiemaps for all extents */
if ((fiemap = (struct fiemap*)realloc(fiemap,sizeof(struct fiemap) +
extents_size)) == NULL) {
fprintf(stderr, "realloc fiemap\n");
return NULL;
}

memset(fiemap->fm_extents, 0, extents_size);
fiemap->fm_extent_count = fiemap->fm_mapped_extents;
fiemap->fm_mapped_extents = 0;

if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
fprintf(stderr, "ioctl() FS_IOC_FIEMAP failed\n");
return NULL;
}

return fiemap;
}

void show_fiemap(struct fiemap *fiemap, char *filename)
{
int i = 0;

printf("File %s has %d extents :\n", filename, fiemap->fm_mapped_extents);
printf("#\tLogical Physical Length Flag\n");
for (i = 0; i < fiemap->fm_mapped_extents; i++) {
printf("%d:\t%-16.16llx %-16.16llx %-16.16llx %-4.4x\n", i,
fiemap->fm_extents[i].fe_logical,
fiemap->fm_extents[i].fe_physical,
fiemap->fm_extents[i].fe_length,
fiemap->fm_extents[i].fe_flags);
}

printf("\n");
}

int main(int argc, char **argv)
{
int i = 0;

if (argc < 2) {
usage(argv);
return 0;
}

for (i = 1; i < argc; i++) {
int fd = 0;

if ((fd = open(argv[i], O_RDONLY)) < 0) {
perror("open file %s failed\n");
perror(argv[i]);
break;
} else {
struct fiemap *fiemap = NULL;

if ((fiemap = read_fiemap(fd)) != NULL) {
show_fiemap(fiemap, argv[i]);
free(fiemap);
}
close(fd);
}
}

return 0;
}

0 comments on commit 74f9cb4

Please sign in to comment.