third_party_f2fs-tools/tools/fibmap.c

224 lines
4.8 KiB
C
Raw Normal View History

#if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
#define _XOPEN_SOURCE 600
#define _DARWIN_C_SOURCE
#define _FILE_OFFSET_BITS 64
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#endif
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#include <f2fs_fs.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <sys/stat.h>
#ifdef HAVE_SYS_SYSMACROS_H
#include <sys/sysmacros.h>
#endif
#include <libgen.h>
#ifdef HAVE_LINUX_HDREG_H
#include <linux/hdreg.h>
#endif
#ifdef HAVE_LINUX_TYPES_H
#include <linux/types.h>
#endif
#ifdef __KERNEL__
#include <linux/fs.h>
#endif
#include <inttypes.h>
#ifndef FIBMAP
#define FIBMAP _IO(0x00, 1) /* bmap access */
#endif
struct file_ext {
tools: fix file too large causing overflow fibmap: Fix file too large causing file_pos overflow fibmap show file_pos with incorrectly value when passing a large file. Before: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 -1506856960 3878170 4133725 255556 -460099584 1510048 1510052 5 Patched: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 2788110336 3878170 4133725 255556 3834867712 1510048 1510052 5 Signed-off-by: Dylan Chang <dylan.chang@oneplus.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
2022-06-16 03:58:06 +00:00
__u64 f_pos;
__u32 start_blk;
__u32 end_blk;
__u32 blk_count;
};
void print_ext(struct file_ext *ext)
{
if (ext->end_blk == 0)
tools: fix file too large causing overflow fibmap: Fix file too large causing file_pos overflow fibmap show file_pos with incorrectly value when passing a large file. Before: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 -1506856960 3878170 4133725 255556 -460099584 1510048 1510052 5 Patched: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 2788110336 3878170 4133725 255556 3834867712 1510048 1510052 5 Signed-off-by: Dylan Chang <dylan.chang@oneplus.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
2022-06-16 03:58:06 +00:00
printf("%8llu %8d %8d %8d\n", ext->f_pos, 0, 0, ext->blk_count);
else
tools: fix file too large causing overflow fibmap: Fix file too large causing file_pos overflow fibmap show file_pos with incorrectly value when passing a large file. Before: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 -1506856960 3878170 4133725 255556 -460099584 1510048 1510052 5 Patched: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 2788110336 3878170 4133725 255556 3834867712 1510048 1510052 5 Signed-off-by: Dylan Chang <dylan.chang@oneplus.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
2022-06-16 03:58:06 +00:00
printf("%8llu %8d %8d %8d\n", ext->f_pos, ext->start_blk,
ext->end_blk, ext->blk_count);
}
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
void print_stat(struct stat64 *st)
#else
void print_stat(struct stat *st)
#endif
{
printf("--------------------------------------------\n");
printf("dev [%d:%d]\n", major(st->st_dev), minor(st->st_dev));
printf("ino [0x%8"PRIx64" : %"PRIu64"]\n",
st->st_ino, st->st_ino);
printf("mode [0x%8x : %d]\n", st->st_mode, st->st_mode);
printf("nlink [0x%8lx : %ld]\n",
(unsigned long)st->st_nlink,
(long)st->st_nlink);
printf("uid [0x%8x : %d]\n", st->st_uid, st->st_uid);
printf("gid [0x%8x : %d]\n", st->st_gid, st->st_gid);
printf("size [0x%8"PRIx64" : %"PRIu64"]\n",
(u64)st->st_size, (u64)st->st_size);
printf("blksize [0x%8lx : %ld]\n",
(unsigned long)st->st_blksize,
(long)st->st_blksize);
printf("blocks [0x%8"PRIx64" : %"PRIu64"]\n",
(u64)st->st_blocks, (u64)st->st_blocks);
printf("--------------------------------------------\n\n");
}
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
static void stat_bdev(struct stat64 *st, unsigned int *start_lba)
#else
static void stat_bdev(struct stat *st, unsigned int *start_lba)
#endif
{
struct stat bdev_stat;
#ifdef HDIO_GETGIO
struct hd_geometry geom;
#endif
char devname[32] = { 0, };
char linkname[32] = { 0, };
int fd;
f2fs-tools: fix cannot get bdev information Some device like android phone doesn't show the dev number path in /dev/block/, for example: $ ls /dev/block/ bootdevice dm-6 loop24 loop43 ram13 sda5 sdd4 sde24 sde43 sde62 sdf1 by-name dm-7 loop25 loop44 ram14 sda6 sdd5 sde25 sde44 sde63 sdf10 dm-0 dm-8 loop26 loop45 ram15 sda7 sdd6 sde26 sde45 sde64 sdf11 dm-1 dm-9 loop27 loop46 ram2 sda8 sdd7 sde27 sde46 sde65 sdf12 dm-10 loop0 loop28 loop47 ram3 sda9 sde sde28 sde47 sde66 sdf13 dm-11 loop1 loop29 loop48 ram4 sdb sde1 sde29 sde48 sde67 sdf14 dm-12 loop10 loop3 loop49 ram5 sdb1 sde10 sde3 sde49 sde68 sdf15 dm-13 loop11 loop30 loop5 ram6 sdb2 sde11 sde30 sde5 sde69 sdf16 dm-14 loop12 loop31 loop50 ram7 sdb3 sde12 sde31 sde50 sde7 sdf17 dm-15 loop13 loop32 loop51 ram8 sdb4 sde13 sde32 sde51 sde70 sdf18 dm-16 loop14 loop33 loop6 ram9 sdb5 sde14 sde33 sde52 sde71 sdf19 dm-17 loop15 loop34 loop7 sda sdc sde15 sde34 sde53 sde72 sdf2 dm-18 loop16 loop35 loop8 sda1 sdc1 sde16 sde35 sde54 sde73 sdf3 dm-19 loop17 loop36 loop9 sda10 sdc2 sde17 sde36 sde55 sde74 sdf4 dm-2 loop18 loop37 mapper sda11 sdc3 sde18 sde37 sde56 sde75 sdf5 dm-20 loop19 loop38 platform sda12 sdc4 sde19 sde38 sde57 sde76 sdf6 dm-21 loop2 loop39 ram0 sda13 sdc5 sde2 sde39 sde58 sde77 sdf7 dm-3 loop20 loop4 ram1 sda14 sdd sde20 sde4 sde59 sde78 sdf8 dm-4 loop21 loop40 ram10 sda2 sdd1 sde21 sde40 sde6 sde8 sdf9 dm-5 loop22 loop41 ram11 sda3 sdd2 sde22 sde41 sde60 sde9 vold dm-53 loop23 loop42 ram12 sda4 sdd3 sde23 sde42 sde61 sdf zram0 this will cause stat_bdev cannot show bdev information. But most of devices (include android) show dev number path in /sys/dev/block, for example: $ ls /sys/dev/block 1:0 254:1 254:6 259:21 259:38 259:54 7:104 7:24 7:368 8:11 8:4 8:70 8:87 1:1 254:10 254:7 259:22 259:39 259:55 7:112 7:240 7:376 8:12 8:48 8:71 8:88 1:10 254:11 254:8 259:23 259:4 259:56 7:120 7:248 7:384 8:13 8:49 8:72 8:89 1:11 254:12 254:9 259:24 259:40 259:57 7:128 7:256 7:392 8:14 8:5 8:73 8:9 1:12 254:13 259:0 259:25 259:41 259:58 7:136 7:264 7:40 8:16 8:50 8:74 8:90 1:13 254:14 259:1 259:26 259:42 259:59 7:144 7:272 7:400 8:17 8:51 8:75 8:91 1:14 254:15 259:10 259:27 259:43 259:6 7:152 7:280 7:408 8:18 8:52 8:76 8:92 1:15 254:16 259:11 259:28 259:44 259:60 7:16 7:288 7:48 8:19 8:53 8:77 8:93 1:2 254:17 259:12 259:29 259:45 259:61 7:160 7:296 7:56 8:2 8:54 8:78 8:94 1:3 254:18 259:13 259:3 259:46 259:62 7:168 7:304 7:64 8:20 8:55 8:79 8:95 1:4 254:19 259:14 259:30 259:47 259:63 7:176 7:312 7:72 8:21 8:6 8:8 1:5 254:2 259:15 259:31 259:48 259:64 7:184 7:32 7:8 8:3 8:64 8:80 1:6 254:20 259:16 259:32 259:49 259:65 7:192 7:320 7:80 8:32 8:65 8:81 1:7 254:21 259:17 259:33 259:5 259:66 7:200 7:328 7:88 8:33 8:66 8:82 1:8 254:3 259:18 259:34 259:50 259:7 7:208 7:336 7:96 8:34 8:67 8:83 1:9 254:4 259:19 259:35 259:51 259:8 7:216 7:344 8:0 8:35 8:68 8:84 253:0 254:5 259:2 259:36 259:52 259:9 7:224 7:352 8:1 8:36 8:69 8:85 254:0 254:53 259:20 259:37 259:53 7:0 7:232 7:360 8:10 8:37 8:7 8:86 so change the access path from /dev/block to /sys/dev/block (same method of libudev) Before: $./f2fs.fibmap testfile_4k ----------------file info------------------- testfile_4k : -------------------------------------------- dev [254:53] ino [0x 3dda : 15834] mode [0x 81b6 : 33206] nlink [0x 1 : 1] uid [0x 0 : 0] gid [0x 0 : 0] size [0x 40000 : 262144] blksize [0x 1000 : 4096] blocks [0x 200 : 512] -------------------------------------------- file_pos start_blk end_blk blks 0 2256926 2256987 62 253952 2150337 2150338 2 Patched: $./f2fs.fibmap testfile_4k ----------------bdev info------------------- devname = 254:53 start_lba = 0 ----------------file info------------------- testfile_4k : -------------------------------------------- dev [254:53] ino [0x 3dda : 15834] mode [0x 81b6 : 33206] nlink [0x 1 : 1] uid [0x 0 : 0] gid [0x 0 : 0] size [0x 40000 : 262144] blksize [0x 1000 : 4096] blocks [0x 200 : 512] -------------------------------------------- file_pos start_blk end_blk blks 0 2256926 2256987 62 253952 2150337 2150338 2 Signed-off-by: Ben Chang <ben.chang@sonicsky.net> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
2022-06-20 03:53:01 +00:00
sprintf(devname, "/sys/dev/block/%d:%d", major(st->st_dev), minor(st->st_dev));
fd = open(devname, O_RDONLY);
if (fd < 0)
return;
if (fstat(fd, &bdev_stat) < 0)
goto out;
if (S_ISBLK(bdev_stat.st_mode)) {
#ifdef HDIO_GETGIO
if (ioctl(fd, HDIO_GETGEO, &geom) < 0)
*start_lba = 0;
else
*start_lba = geom.start;
#else
*start_lba = 0;
#endif
}
if (readlink(devname, linkname, sizeof(linkname)) < 0)
goto out;
printf("----------------bdev info-------------------\n");
printf("devname = %s\n", basename(linkname));
printf("start_lba = %u\n", *start_lba);
out:
close(fd);
}
int main(int argc, char *argv[])
{
int fd;
int ret = 0;
char *filename;
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
struct stat64 st;
#else
struct stat st;
#endif
int total_blks;
unsigned int i;
struct file_ext ext;
__u32 start_lba;
__u32 blknum;
if (argc != 2) {
fprintf(stderr, "No filename\n");
exit(-1);
}
filename = argv[1];
fd = open(filename, O_RDONLY|O_LARGEFILE);
if (fd < 0) {
ret = errno;
perror(filename);
exit(-1);
}
fsync(fd);
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
if (fstat64(fd, &st) < 0) {
#else
if (fstat(fd, &st) < 0) {
#endif
ret = errno;
perror(filename);
goto out;
}
stat_bdev(&st, &start_lba);
total_blks = (st.st_size + st.st_blksize - 1) / st.st_blksize;
printf("\n----------------file info-------------------\n");
printf("%s :\n", filename);
print_stat(&st);
printf("file_pos start_blk end_blk blks\n");
blknum = 0;
if (ioctl(fd, FIBMAP, &blknum) < 0) {
ret = errno;
perror("ioctl(FIBMAP)");
goto out;
}
ext.f_pos = 0;
ext.start_blk = blknum;
ext.end_blk = blknum;
ext.blk_count = 1;
for (i = 1; i < total_blks; i++) {
blknum = i;
if (ioctl(fd, FIBMAP, &blknum) < 0) {
ret = errno;
perror("ioctl(FIBMAP)");
goto out;
}
if ((blknum == 0 && blknum == ext.end_blk) || (ext.end_blk + 1) == blknum) {
ext.end_blk = blknum;
ext.blk_count++;
} else {
print_ext(&ext);
tools: fix file too large causing overflow fibmap: Fix file too large causing file_pos overflow fibmap show file_pos with incorrectly value when passing a large file. Before: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 -1506856960 3878170 4133725 255556 -460099584 1510048 1510052 5 Patched: ----------------file info------------------- /data/media/0/data1 : -------------------------------------------- dev [254:18] ino [0x 4db1 : 19889] mode [0x 81b0 : 33200] nlink [0x 1 : 1] uid [0x 280e : 10254] gid [0x 3ff : 1023] size [0x1b3dca314 : 7312548628] blksize [0x 1000 : 4096] blocks [0x da2530 : 14296368] -------------------------------------------- file_pos start_blk end_blk blks 0 3197602 3198463 862 3530752 3197509 3197509 1 3534848 3197557 3197578 22 3624960 3198464 3396701 198238 815607808 3396703 3632480 235778 1781354496 3632482 3652095 19614 1861693440 3396702 3396702 1 1861697536 3632481 3632481 1 1861701632 1514948 1514948 1 1861705728 1518774 1518774 1 1861709824 2543104 2543125 22 ... 1862111232 2457813 2457813 1 1862115328 3652096 3878168 226073 2788110336 3878170 4133725 255556 3834867712 1510048 1510052 5 Signed-off-by: Dylan Chang <dylan.chang@oneplus.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
2022-06-16 03:58:06 +00:00
ext.f_pos = (__u64)i * st.st_blksize;
ext.start_blk = blknum;
ext.end_blk = blknum;
ext.blk_count = 1;
}
}
print_ext(&ext);
out:
close(fd);
return ret;
}