-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathfiemap-fault.c
74 lines (60 loc) · 1.51 KB
/
fiemap-fault.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2024 Meta Platforms, Inc. All Rights Reserved.
*/
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/fiemap.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int prep_mmap_buffer(int fd, void **addr)
{
struct stat st;
int ret;
ret = fstat(fd, &st);
if (ret)
err(1, "failed to stat %d", fd);
*addr = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (*addr == MAP_FAILED)
err(1, "failed to mmap %d", fd);
return st.st_size;
}
int main(int argc, char *argv[])
{
struct fiemap *fiemap;
size_t sz, last = 0;
void *buf = NULL;
int ret, fd;
if (argc != 2)
errx(1, "no in and out file name arguments given");
fd = open(argv[1], O_RDWR, 0666);
if (fd == -1)
err(1, "failed to open %s", argv[1]);
sz = prep_mmap_buffer(fd, &buf);
fiemap = (struct fiemap *)buf;
fiemap->fm_flags = 0;
fiemap->fm_extent_count = (sz - sizeof(struct fiemap)) /
sizeof(struct fiemap_extent);
while (last < sz) {
int i;
fiemap->fm_start = last;
fiemap->fm_length = sz - last;
ret = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
if (ret < 0)
err(1, "fiemap failed %d", errno);
for (i = 0; i < fiemap->fm_mapped_extents; i++)
last = fiemap->fm_extents[i].fe_logical +
fiemap->fm_extents[i].fe_length;
}
munmap(buf, sz);
close(fd);
return 0;
}