3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License version 2 for more details (a copy is included
13 * in the LICENSE file that accompanied this code).
15 * You should have received a copy of the GNU General Public License
16 * version 2 along with this program; If not, see http://www.gnu.org/licenses
18 * Please visit http://www.xyratex.com/contact if you need additional
19 * information or have any questions.
25 * Copyright 2013 Xyratex Technology Limited
27 * Author: Artem Blagodarenko <Artem_Blagodarenko@xyratex.com>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
43 # include <linux/types.h>
44 # include <linux/fiemap.h>
48 # ifndef FS_IOC_FIEMAP
49 # define FS_IOC_FIEMAP (_IOWR('f', 11, struct fiemap))
55 /* This test executes fiemap ioctl and check
56 * a) there are no file ranges marked with FIEMAP_EXTENT_UNWRITTEN
57 * b) data ranges sizes sum is equal to given in second param */
58 int check_fiemap(int fd, long long orig_size)
60 /* This buffer is enougth for 1MB length file */
61 union { struct fiemap f; char c[4096]; } fiemap_buf;
62 struct fiemap *fiemap = &fiemap_buf.f;
63 struct fiemap_extent *fm_extents = &fiemap->fm_extents[0];
64 unsigned int count = (sizeof(fiemap_buf) - sizeof(*fiemap)) /
67 long long file_size = 0;
69 memset(&fiemap_buf, 0, sizeof(fiemap_buf));
72 fiemap->fm_flags = FIEMAP_FLAG_SYNC;
73 fiemap->fm_extent_count = count;
74 fiemap->fm_length = FIEMAP_MAX_OFFSET;
76 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
77 fprintf(stderr, "error while ioctl %i\n", errno);
81 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
83 "offset %lu, length %lu\n"
85 (unsigned long)fm_extents[i].fe_logical,
86 (unsigned long)fm_extents[i].fe_length,
87 fm_extents[i].fe_flags);
89 if (fm_extents[i].fe_flags & FIEMAP_EXTENT_UNWRITTEN) {
90 fprintf(stderr, "Unwritten extent\n");
93 file_size += fm_extents[i].fe_length;
97 printf("No unwritten extents, extents number %u, "
98 "file size %lli, original size %lli\n",
99 fiemap->fm_mapped_extents,
100 file_size, orig_size);
101 return file_size != orig_size;
104 int main(int argc, char **argv)
107 struct option long_opts[] = {
108 {"test", no_argument, 0, 't'},
115 while ((c = getopt_long(argc, argv, "t", long_opts, NULL)) != -1) {
120 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
121 argv[0], argv[optind - 1]);
126 if (optind != argc - 2) {
127 fprintf(stderr, "Usage: %s <filename> <filesize>\n", argv[0]);
131 fd = open(argv[optind], O_RDONLY);
133 fprintf(stderr, "cannot open %s for reading, error %i",
134 argv[optind], errno);
138 fprintf(stderr, "fd: %i\n", fd);
140 rc = check_fiemap(fd, atoll(argv[optind + 1]));
143 fprintf(stderr, "closing %s, error %i", argv[optind], errno);