fibmap.c revision 1aae03594ba111174ed4902a29b969a1fb145adf
1dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#define _LARGEFILE64_SOURCE 2dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <unistd.h> 3dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <string.h> 4dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <stdlib.h> 5dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <stdio.h> 6dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <fcntl.h> 7dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <errno.h> 8dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <sys/ioctl.h> 9dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <sys/stat.h> 10dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <linux/types.h> 11dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee#include <linux/fs.h> 12dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 13dfe2a3384fbacb960463088176654bbf13c106e4Changman Leestruct file_ext { 14dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee __u32 f_pos; 15dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee __u32 start_blk; 16dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee __u32 end_blk; 17dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee __u32 blk_count; 18dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee}; 19dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 20dfe2a3384fbacb960463088176654bbf13c106e4Changman Leevoid print_ext(struct file_ext *ext) 21dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee{ 22dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee if (ext->end_blk == 0) 23dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee printf("%8d %8d %8d %8d\n", ext->f_pos, 0, 0, ext->blk_count); 24dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee else 25dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee printf("%8d %8d %8d %8d\n", ext->f_pos, ext->start_blk, 26dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext->end_blk, ext->blk_count); 27dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee} 28dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 29e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Leevoid print_stat(struct stat64 *st) 30e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee{ 31e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("--------------------------------------------\n"); 32e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("dev [%d:%d]\n", major(st->st_dev), minor(st->st_dev)); 33e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("ino [0x%8lx : %ld]\n", st->st_ino, st->st_ino); 34e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("mode [0x%8x : %d]\n", st->st_mode, st->st_mode); 35e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("nlink [0x%8lx : %ld]\n", st->st_nlink, st->st_nlink); 36e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("uid [0x%8x : %d]\n", st->st_uid, st->st_uid); 37e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("gid [0x%8x : %d]\n", st->st_gid, st->st_gid); 38e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("size [0x%8lx : %ld]\n", st->st_size, st->st_size); 39e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("blksize [0x%8lx : %ld]\n", st->st_blksize, st->st_blksize); 40e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("blocks [0x%8lx : %ld]\n", st->st_blocks, st->st_blocks); 41e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee printf("--------------------------------------------\n\n"); 42e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee} 43e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee 44dfe2a3384fbacb960463088176654bbf13c106e4Changman Leeint main(int argc, char *argv[]) 45dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee{ 46dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee int fd; 47dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee int ret = 0; 48dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee char *filename; 49dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee struct stat64 st; 50dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee int total_blks; 51dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee unsigned int i; 52dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee struct file_ext ext; 53dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee __u32 blknum; 54dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 55dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee if (argc != 2) { 56dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee fprintf(stderr, "No filename\n"); 57dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee exit(-1); 58dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } 59dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee filename = argv[1]; 60dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 61dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee fd = open(filename, O_RDONLY|O_LARGEFILE); 62dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee if (fd < 0) { 63dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ret = errno; 64dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee perror(filename); 65dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee exit(-1); 66dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } 67dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 68dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee fsync(fd); 69dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 70dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee if (fstat64(fd, &st) < 0) { 71dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ret = errno; 72dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee perror(filename); 73dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee goto out; 74dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } 75dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 76dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee total_blks = (st.st_size + st.st_blksize - 1) / st.st_blksize; 77dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 78dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee printf("\n%s :\n", filename); 79e23d58533c631a1babd29dcf732c99c9cc4f8e89Changman Lee print_stat(&st); 80dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee printf("file_pos start_blk end_blk blks\n"); 81dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 82dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee blknum = 0; 83dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee if (ioctl(fd, FIBMAP, &blknum) < 0) { 84dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ret = errno; 85dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee perror("ioctl(FIBMAP)"); 86dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee goto out; 87dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } 88dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.f_pos = 0; 89dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.start_blk = blknum; 90dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.end_blk = blknum; 91dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.blk_count = 1; 92dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 93dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee for (i = 1; i < total_blks; i++) { 94dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee blknum = i; 95dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 96dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee if (ioctl(fd, FIBMAP, &blknum) < 0) { 97dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ret = errno; 98dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee perror("ioctl(FIBMAP)"); 99dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee goto out; 100dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } 101dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 102dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee if ((blknum == 0 && blknum == ext.end_blk) || (ext.end_blk + 1) == blknum) { 103dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.end_blk = blknum; 104dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.blk_count++; 105dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } else { 106dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee print_ext(&ext); 107dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.f_pos = i * st.st_blksize; 108dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.start_blk = blknum; 109dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee ext.end_blk = blknum; 1101aae03594ba111174ed4902a29b969a1fb145adfJaegeuk Kim ext.blk_count = 1; 111dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } 112dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee } 113dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee 114dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee print_ext(&ext); 115dfe2a3384fbacb960463088176654bbf13c106e4Changman Leeout: 116dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee close(fd); 117dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee return ret; 118dfe2a3384fbacb960463088176654bbf13c106e4Changman Lee} 119