BTRFSでファイルの物理オフセットを見つけるには?

BTRFSでファイルの物理オフセットを見つけるには?

resume_offsetカーネルコマンドラインで使用する必要があるBTRFSにスワップファイルがあります。

正しい物理オフセットを計算する方法は?

filefrag動作しません。

btrfs_map_physical.c動作しません。

ベストアンサー1

以下は、パーティションの先頭から開始オフセットを計算するCプログラムです。一部Btrfs ファイルシステムのファイル。 Btrfsにはこれを行う安定した方法がないようです。

#include <unistd.h>
#include <linux/fs.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/fiemap.h>

int main(int argc, char **argv)
{
    char buffer[sizeof (struct fiemap) + sizeof (struct fiemap_extent)];
    struct fiemap *map = (struct fiemap *)buffer;
    map->fm_start = 0;
    map->fm_length = 4096;
    map->fm_flags = FIEMAP_FLAG_SYNC;
    map->fm_extent_count = 1; /* If you change this, you'll need to enlarge `buffer´. */
    map->fm_reserved = 0;

    int fd;
    if (argc < 2) {
        fprintf(stderr, "Usage %s filename\n", argv[0]);
        return 1;
    }
    fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
        perror("Error opening file");
        return 1;
    }   
    int block = 0;
    int ret = ioctl(fd, FS_IOC_FIEMAP, map);
    if (ret < 0) {
        perror("ioctl");
        close(fd);
        return 1;
    }

    close(fd);
    printf("Number of extents returned: %ld\n", map->fm_mapped_extents);
    printf("File %s starts at byte offset %lu\n", argv[1], map->fm_extents[0].fe_physical);
    return 0;
}

おすすめ記事