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>
42 #include <linux/types.h>
44 #include <linux/lustre/lustre_user.h>
47 # define FS_IOC_FIEMAP (_IOWR('f', 11, struct fiemap))
52 static inline void print_extent_flags(unsigned int flags)
57 printf("flags (0x%x):", flags);
58 if (flags & FIEMAP_EXTENT_LAST)
60 if (flags & FIEMAP_EXTENT_UNKNOWN)
62 if (flags & FIEMAP_EXTENT_DELALLOC)
64 if (flags & FIEMAP_EXTENT_ENCODED)
66 if (flags & FIEMAP_EXTENT_DATA_ENCRYPTED)
67 printf(" DATA_ENCRYPTED");
68 if (flags & FIEMAP_EXTENT_NOT_ALIGNED)
69 printf(" NOT_ALIGNED");
70 if (flags & FIEMAP_EXTENT_DATA_INLINE)
71 printf(" DATA_INLINE");
72 if (flags & FIEMAP_EXTENT_DATA_TAIL)
74 if (flags & FIEMAP_EXTENT_UNWRITTEN)
76 if (flags & FIEMAP_EXTENT_MERGED)
78 if (flags & FIEMAP_EXTENT_SHARED)
80 if (flags & FIEMAP_EXTENT_NET)
86 /* This test executes fiemap ioctl and check
87 * a) there are no file ranges marked with FIEMAP_EXTENT_UNWRITTEN
88 * b) data ranges sizes sum is equal to given in second param
90 static int check_fiemap(int fd, long long expected_sum,
91 unsigned int *mapped_extents)
93 /* This buffer is enougth for 1MB length file */
94 union { struct fiemap f; char c[4096]; } fiemap_buf;
95 struct fiemap *fiemap = &fiemap_buf.f;
96 struct fiemap_extent *fm_extents = &fiemap->fm_extents[0];
97 unsigned int count = (sizeof(fiemap_buf) - sizeof(*fiemap)) /
100 long long ext_len_sum = 0;
102 memset(&fiemap_buf, 0, sizeof(fiemap_buf));
104 fiemap->fm_start = 0;
105 fiemap->fm_flags = (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_DEVICE_ORDER);
106 fiemap->fm_extent_count = count;
107 fiemap->fm_length = FIEMAP_MAX_OFFSET;
109 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
110 fprintf(stderr, "error while ioctl %i\n", errno);
114 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
115 printf("extent %d in offset %lu, length %lu\n",
116 i, (unsigned long)fm_extents[i].fe_logical,
117 (unsigned long)fm_extents[i].fe_length);
119 print_extent_flags(fm_extents[i].fe_flags);
121 if (fm_extents[i].fe_flags & FIEMAP_EXTENT_UNWRITTEN) {
122 fprintf(stderr, "Unwritten extent\n");
125 ext_len_sum += fm_extents[i].fe_length;
128 printf("No unwritten extents, extents number %u, sum of lengths %lli, expected sum %lli\n",
129 fiemap->fm_mapped_extents,
130 ext_len_sum, expected_sum);
132 *mapped_extents = fiemap->fm_mapped_extents;
133 return ext_len_sum != expected_sum ||
134 (expected_sum && !*mapped_extents);
138 * When userspace uses fiemap with fm_extent_count=0, it means that kernelspace
139 * should return only the number of extents. So we should always check
140 * fm_extent_count before accessing to fm_extents array. Otherwise this could
141 * lead to buffer overflow and slab memory corruption.
149 static void *corruption_th(void *args)
152 struct th_args *ta = args;
153 struct fiemap fiemap = {
155 .fm_flags = (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_DEVICE_ORDER),
156 .fm_extent_count = 0,
157 .fm_length = FIEMAP_MAX_OFFSET };
159 for (i = 0; i < ta->iter_nbr; i++) {
160 if (ioctl(ta->fd, FS_IOC_FIEMAP, &fiemap) < 0) {
161 fprintf(stderr, "error while ioctl: %s\n",
163 return (void *) (long long) -errno;
165 if (ta->expected_mapped != fiemap.fm_mapped_extents) {
167 "mapped extents mismatch: expected=%d, returned=%d\n",
168 ta->expected_mapped, fiemap.fm_mapped_extents);
169 return (void *) -EINVAL;
176 int main(int argc, char **argv)
179 struct option long_opts[] = {
180 { .name = "test", .has_arg = no_argument, .val = 't' },
181 { .name = "corruption_test", .has_arg = no_argument, .val = 'c' },
186 unsigned int mapped_extents = 0;
187 bool corruption_test = false;
190 while ((c = getopt_long(argc, argv, "tc", long_opts, NULL)) != -1) {
195 corruption_test = true;
198 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
199 argv[0], argv[optind - 1]);
204 if (optind != argc - 2) {
205 fprintf(stderr, "Usage: %s <filename> <filesize>\n", argv[0]);
209 fd = open(argv[optind], O_RDONLY);
211 fprintf(stderr, "cannot open %s for reading, error %i",
212 argv[optind], errno);
216 rc = check_fiemap(fd, atoll(argv[optind + 1]), &mapped_extents);
220 if (corruption_test) {
224 struct th_args args = {
226 .expected_mapped = mapped_extents,
230 for (i = 0; i < 200; i++) {
231 rc = pthread_create(&th[i], NULL, corruption_th, &args);
235 for (i = 0; i < 200; i++) {
236 rc = pthread_join(th[i], &rval);
245 fprintf(stderr, "closing %s, error %i", argv[optind], errno);