-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|