2017-11-15 02:53:32 +08:00
|
|
|
#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
|
2013-07-18 17:08:01 +08:00
|
|
|
#define _LARGEFILE64_SOURCE
|
2017-11-15 02:53:32 +08:00
|
|
|
#endif
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifndef O_LARGEFILE
|
|
|
|
#define O_LARGEFILE 0
|
|
|
|
#endif
|
2018-02-27 16:39:33 +08:00
|
|
|
#include <f2fs_fs.h>
|
2013-07-18 17:08:01 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2014-03-03 17:20:58 +08:00
|
|
|
#include <sys/types.h>
|
2022-04-22 06:18:22 +08:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2013-07-18 17:08:01 +08:00
|
|
|
#include <sys/ioctl.h>
|
2022-04-22 06:18:22 +08:00
|
|
|
#endif
|
2013-07-18 17:08:01 +08:00
|
|
|
#include <sys/stat.h>
|
2017-11-15 02:53:32 +08:00
|
|
|
#ifdef HAVE_SYS_SYSMACROS_H
|
2016-04-19 06:19:53 +08:00
|
|
|
#include <sys/sysmacros.h>
|
2017-11-15 02:53:32 +08:00
|
|
|
#endif
|
2014-01-16 11:01:27 +08:00
|
|
|
#include <libgen.h>
|
2017-11-15 02:53:32 +08:00
|
|
|
#ifdef HAVE_LINUX_HDREG_H
|
2014-01-16 11:01:27 +08:00
|
|
|
#include <linux/hdreg.h>
|
2017-11-15 02:53:32 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LINUX_TYPES_H
|
2013-07-18 17:08:01 +08:00
|
|
|
#include <linux/types.h>
|
2017-11-15 02:53:32 +08:00
|
|
|
#endif
|
|
|
|
#ifdef __KERNEL__
|
2014-05-21 11:05:31 +08:00
|
|
|
#include <linux/fs.h>
|
2017-11-15 02:53:32 +08:00
|
|
|
#endif
|
2015-01-22 11:19:05 +08:00
|
|
|
#include <inttypes.h>
|
2017-11-15 02:53:32 +08:00
|
|
|
|
|
|
|
#ifndef FIBMAP
|
|
|
|
#define FIBMAP _IO(0x00, 1) /* bmap access */
|
|
|
|
#endif
|
2013-07-18 17:08:01 +08:00
|
|
|
|
|
|
|
struct file_ext {
|
|
|
|
__u32 f_pos;
|
|
|
|
__u32 start_blk;
|
|
|
|
__u32 end_blk;
|
|
|
|
__u32 blk_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
void print_ext(struct file_ext *ext)
|
|
|
|
{
|
|
|
|
if (ext->end_blk == 0)
|
|
|
|
printf("%8d %8d %8d %8d\n", ext->f_pos, 0, 0, ext->blk_count);
|
|
|
|
else
|
|
|
|
printf("%8d %8d %8d %8d\n", ext->f_pos, ext->start_blk,
|
|
|
|
ext->end_blk, ext->blk_count);
|
|
|
|
}
|
|
|
|
|
2017-11-15 02:53:32 +08:00
|
|
|
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
|
2013-08-08 10:12:21 +08:00
|
|
|
void print_stat(struct stat64 *st)
|
2017-11-15 02:53:32 +08:00
|
|
|
#else
|
|
|
|
void print_stat(struct stat *st)
|
|
|
|
#endif
|
2013-08-08 10:12:21 +08:00
|
|
|
{
|
|
|
|
printf("--------------------------------------------\n");
|
|
|
|
printf("dev [%d:%d]\n", major(st->st_dev), minor(st->st_dev));
|
2015-01-22 11:19:05 +08:00
|
|
|
printf("ino [0x%8"PRIx64" : %"PRIu64"]\n",
|
|
|
|
st->st_ino, st->st_ino);
|
2013-08-08 10:12:21 +08:00
|
|
|
printf("mode [0x%8x : %d]\n", st->st_mode, st->st_mode);
|
2017-11-15 02:53:32 +08:00
|
|
|
printf("nlink [0x%8lx : %ld]\n",
|
|
|
|
(unsigned long)st->st_nlink,
|
|
|
|
(long)st->st_nlink);
|
2013-08-08 10:12:21 +08:00
|
|
|
printf("uid [0x%8x : %d]\n", st->st_uid, st->st_uid);
|
|
|
|
printf("gid [0x%8x : %d]\n", st->st_gid, st->st_gid);
|
2015-01-22 11:19:05 +08:00
|
|
|
printf("size [0x%8"PRIx64" : %"PRIu64"]\n",
|
2017-11-15 02:53:32 +08:00
|
|
|
(u64)st->st_size, (u64)st->st_size);
|
|
|
|
printf("blksize [0x%8lx : %ld]\n",
|
|
|
|
(unsigned long)st->st_blksize,
|
|
|
|
(long)st->st_blksize);
|
2015-01-22 11:19:05 +08:00
|
|
|
printf("blocks [0x%8"PRIx64" : %"PRIu64"]\n",
|
2017-11-15 02:53:32 +08:00
|
|
|
(u64)st->st_blocks, (u64)st->st_blocks);
|
2013-08-08 10:12:21 +08:00
|
|
|
printf("--------------------------------------------\n\n");
|
|
|
|
}
|
|
|
|
|
2017-11-15 02:53:32 +08:00
|
|
|
#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
|
2014-01-16 11:01:27 +08:00
|
|
|
{
|
|
|
|
struct stat bdev_stat;
|
2017-11-15 02:53:32 +08:00
|
|
|
#ifdef HDIO_GETGIO
|
2014-01-16 11:01:27 +08:00
|
|
|
struct hd_geometry geom;
|
2017-11-15 02:53:32 +08:00
|
|
|
#endif
|
2014-01-16 11:01:27 +08:00
|
|
|
char devname[32] = { 0, };
|
|
|
|
char linkname[32] = { 0, };
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
sprintf(devname, "/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)) {
|
2017-11-15 02:53:32 +08:00
|
|
|
#ifdef HDIO_GETGIO
|
2014-01-16 11:01:27 +08:00
|
|
|
if (ioctl(fd, HDIO_GETGEO, &geom) < 0)
|
|
|
|
*start_lba = 0;
|
|
|
|
else
|
|
|
|
*start_lba = geom.start;
|
2017-11-15 02:53:32 +08:00
|
|
|
#else
|
|
|
|
*start_lba = 0;
|
|
|
|
#endif
|
2014-01-16 11:01:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:08:01 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret = 0;
|
|
|
|
char *filename;
|
2017-11-15 02:53:32 +08:00
|
|
|
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
|
2013-07-18 17:08:01 +08:00
|
|
|
struct stat64 st;
|
2017-11-15 02:53:32 +08:00
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
#endif
|
2013-07-18 17:08:01 +08:00
|
|
|
int total_blks;
|
|
|
|
unsigned int i;
|
|
|
|
struct file_ext ext;
|
2014-01-16 11:01:27 +08:00
|
|
|
__u32 start_lba;
|
2013-07-18 17:08:01 +08:00
|
|
|
__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);
|
|
|
|
|
2017-11-15 02:53:32 +08:00
|
|
|
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
|
2013-07-18 17:08:01 +08:00
|
|
|
if (fstat64(fd, &st) < 0) {
|
2017-11-15 02:53:32 +08:00
|
|
|
#else
|
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
#endif
|
2013-07-18 17:08:01 +08:00
|
|
|
ret = errno;
|
|
|
|
perror(filename);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-01-16 11:01:27 +08:00
|
|
|
stat_bdev(&st, &start_lba);
|
|
|
|
|
2013-07-18 17:08:01 +08:00
|
|
|
total_blks = (st.st_size + st.st_blksize - 1) / st.st_blksize;
|
|
|
|
|
2014-01-16 11:01:27 +08:00
|
|
|
printf("\n----------------file info-------------------\n");
|
|
|
|
printf("%s :\n", filename);
|
2013-08-08 10:12:21 +08:00
|
|
|
print_stat(&st);
|
2013-07-18 17:08:01 +08:00
|
|
|
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);
|
|
|
|
ext.f_pos = i * st.st_blksize;
|
|
|
|
ext.start_blk = blknum;
|
|
|
|
ext.end_blk = blknum;
|
2013-12-27 11:57:51 +08:00
|
|
|
ext.blk_count = 1;
|
2013-07-18 17:08:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print_ext(&ext);
|
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|