1 /*
2  * Copyright (C) 2014 Cyril Hrubis chrubis@suse.cz
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/ioctl.h>
27 #include <sys/mount.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <linux/loop.h>
32 #include <stdint.h>
33 #include <inttypes.h>
34 #include "lapi/syscalls.h"
35 #include "test.h"
36 #include "safe_macros.h"
37 
38 #ifndef LOOP_CTL_GET_FREE
39 # define LOOP_CTL_GET_FREE 0x4C82
40 #endif
41 
42 #define LOOP_CONTROL_FILE "/dev/loop-control"
43 
44 #define DEV_FILE "test_dev.img"
45 #define DEV_SIZE_MB 256u
46 
47 static char dev_path[1024];
48 static int device_acquired;
49 static unsigned long prev_dev_sec_write;
50 
51 static const char *dev_variants[] = {
52 	"/dev/loop%i",
53 	"/dev/loop/%i",
54 	"/dev/block/loop%i"
55 };
56 
set_dev_path(int dev,char * path,size_t path_len)57 static int set_dev_path(int dev, char *path, size_t path_len)
58 {
59 	unsigned int i;
60 	struct stat st;
61 
62 	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
63 		snprintf(path, path_len, dev_variants[i], dev);
64 
65 		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
66 			return 1;
67 	}
68 
69 	return 0;
70 }
71 
tst_find_free_loopdev(char * path,size_t path_len)72 int tst_find_free_loopdev(char *path, size_t path_len)
73 {
74 	int ctl_fd, dev_fd, rc, i;
75 	struct loop_info loopinfo;
76 	char buf[1024];
77 
78 	/* since Linux 3.1 */
79 	ctl_fd = open(LOOP_CONTROL_FILE, O_RDWR);
80 
81 	if (ctl_fd > 0) {
82 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
83 		close(ctl_fd);
84 		if (rc >= 0) {
85 			if (path)
86 				set_dev_path(rc, path, path_len);
87 			tst_resm(TINFO, "Found free device %d '%s'",
88 				rc, path ?: "");
89 			return rc;
90 		}
91 		tst_resm(TINFO, "Couldn't find free loop device");
92 		return -1;
93 	}
94 
95 	switch (errno) {
96 	case ENOENT:
97 	break;
98 	case EACCES:
99 		tst_resm(TINFO | TERRNO,
100 		         "Not allowed to open " LOOP_CONTROL_FILE ". "
101 			 "Are you root?");
102 	break;
103 	default:
104 		tst_resm(TBROK | TERRNO, "Failed to open " LOOP_CONTROL_FILE);
105 	}
106 
107 	/*
108 	 * Older way is to iterate over /dev/loop%i and /dev/loop/%i and try
109 	 * LOOP_GET_STATUS ioctl() which fails for free loop devices.
110 	 */
111 	for (i = 0; i < 256; i++) {
112 
113 		if (!set_dev_path(i, buf, sizeof(buf)))
114 			continue;
115 
116 		dev_fd = open(buf, O_RDONLY);
117 
118 		if (dev_fd < 0)
119 			continue;
120 
121 		if (ioctl(dev_fd, LOOP_GET_STATUS, &loopinfo) == 0) {
122 			tst_resm(TINFO, "Device '%s' in use", buf);
123 		} else {
124 			if (errno != ENXIO)
125 				continue;
126 			tst_resm(TINFO, "Found free device '%s'", buf);
127 			close(dev_fd);
128 			if (path != NULL) {
129 				strncpy(path, buf, path_len);
130 				path[path_len-1] = '\0';
131 			}
132 			return i;
133 		}
134 
135 		close(dev_fd);
136 	}
137 
138 	tst_resm(TINFO, "No free devices found");
139 
140 	return -1;
141 }
142 
tst_attach_device(const char * dev,const char * file)143 int tst_attach_device(const char *dev, const char *file)
144 {
145 	int dev_fd, file_fd;
146 	struct loop_info loopinfo;
147 
148 	/* b/148978487 */
149 	int attach_tries = 20;
150 	while (attach_tries--) {
151 		dev_fd = open(dev, O_RDWR);
152 		if (dev_fd >= 0)
153 			break;
154 		usleep(50000);
155 	}
156 	if (dev_fd < 0) {
157 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
158 		return 1;
159 	}
160 
161 	file_fd = open(file, O_RDWR);
162 	if (file_fd < 0) {
163 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", file);
164 		close(dev_fd);
165 		return 1;
166 	}
167 
168 	if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
169 		close(dev_fd);
170 		close(file_fd);
171 		tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
172 			 dev, file);
173 		return 1;
174 	}
175 
176 	/* Old mkfs.btrfs use LOOP_GET_STATUS instead of backing_file to get
177 	 * associated filename, so we need to set up the device by calling
178 	 * LOOP_SET_FD and LOOP_SET_STATUS.
179 	 */
180 	memset(&loopinfo, 0, sizeof(loopinfo));
181 	strcpy(loopinfo.lo_name, file);
182 
183 	if (ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo)) {
184 		close(dev_fd);
185 		close(file_fd);
186 		tst_resm(TWARN | TERRNO,
187 			 "ioctl(%s, LOOP_SET_STATUS, %s) failed", dev, file);
188 		return 1;
189 	}
190 
191 	close(dev_fd);
192 	close(file_fd);
193 	return 0;
194 }
195 
tst_detach_device(const char * dev)196 int tst_detach_device(const char *dev)
197 {
198 	int dev_fd, ret, i;
199 
200 	dev_fd = open(dev, O_RDONLY);
201 	if (dev_fd < 0) {
202 		tst_resm(TWARN | TERRNO, "open(%s) failed", dev);
203 		return 1;
204 	}
205 
206 	/* keep trying to clear LOOPDEV until we get ENXIO, a quick succession
207 	 * of attach/detach might not give udev enough time to complete */
208 	for (i = 0; i < 40; i++) {
209 		ret = ioctl(dev_fd, LOOP_CLR_FD, 0);
210 
211 		if (ret && (errno == ENXIO)) {
212 			close(dev_fd);
213 			return 0;
214 		}
215 
216 		if (ret && (errno != EBUSY)) {
217 			tst_resm(TWARN,
218 				 "ioctl(%s, LOOP_CLR_FD, 0) unexpectedly failed with: %s",
219 				 dev, tst_strerrno(errno));
220 			close(dev_fd);
221 			return 1;
222 		}
223 
224 		usleep(50000);
225 	}
226 
227 	close(dev_fd);
228 	tst_resm(TWARN,
229 		"ioctl(%s, LOOP_CLR_FD, 0) no ENXIO for too long", dev);
230 	return 1;
231 }
232 
tst_dev_sync(int fd)233 int tst_dev_sync(int fd)
234 {
235 	return syscall(__NR_syncfs, fd);
236 }
237 
tst_acquire_device__(unsigned int size)238 const char *tst_acquire_device__(unsigned int size)
239 {
240 	int fd;
241 	char *dev;
242 	struct stat st;
243 	unsigned int acq_dev_size;
244 	uint64_t ltp_dev_size;
245 
246 	acq_dev_size = MAX(size, DEV_SIZE_MB);
247 
248 	dev = getenv("LTP_DEV");
249 
250 	if (dev) {
251 		tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev);
252 
253 		if (stat(dev, &st)) {
254 			tst_resm(TWARN | TERRNO, "stat() failed");
255 			return NULL;
256 		}
257 
258 		if (!S_ISBLK(st.st_mode)) {
259 			tst_resm(TWARN, "%s is not a block device", dev);
260 			return NULL;
261 		}
262 
263 		fd = open(dev, O_RDONLY);
264 		if (fd < 0) {
265 			tst_resm(TWARN | TERRNO,
266 				 "open(%s, O_RDONLY) failed", dev);
267 			return NULL;
268 		}
269 
270 		if (ioctl(fd, BLKGETSIZE64, &ltp_dev_size)) {
271 			tst_resm(TWARN | TERRNO,
272 				 "ioctl(fd, BLKGETSIZE64, ...) failed");
273 			close(fd);
274 			return NULL;
275 		}
276 
277 		if (close(fd)) {
278 			tst_resm(TWARN | TERRNO,
279 				 "close(fd) failed");
280 			return NULL;
281 		}
282 
283 		ltp_dev_size = ltp_dev_size/1024/1024;
284 
285 		if (acq_dev_size <= ltp_dev_size)
286 			return dev;
287 
288 		tst_resm(TINFO, "Skipping $LTP_DEV size %"PRIu64"MB, requested size %uMB",
289 				ltp_dev_size, acq_dev_size);
290 	}
291 
292 	if (tst_fill_file(DEV_FILE, 0, 1024 * 1024, acq_dev_size)) {
293 		tst_resm(TWARN | TERRNO, "Failed to create " DEV_FILE);
294 		return NULL;
295 	}
296 
297 	if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
298 		return NULL;
299 
300 	if (tst_attach_device(dev_path, DEV_FILE))
301 		return NULL;
302 
303 	device_acquired = 1;
304 
305 	return dev_path;
306 }
307 
tst_acquire_device_(void (cleanup_fn)(void),unsigned int size)308 const char *tst_acquire_device_(void (cleanup_fn)(void), unsigned int size)
309 {
310 	const char *device;
311 
312 	if (device_acquired) {
313 		tst_brkm(TBROK, cleanup_fn, "Device already acquired");
314 		return NULL;
315 	}
316 
317 	if (!tst_tmpdir_created()) {
318 		tst_brkm(TBROK, cleanup_fn,
319 		         "Cannot acquire device without tmpdir() created");
320 		return NULL;
321 	}
322 
323 	device = tst_acquire_device__(size);
324 
325 	if (!device) {
326 		tst_brkm(TBROK, cleanup_fn, "Failed to acquire device");
327 		return NULL;
328 	}
329 
330 	return device;
331 }
332 
tst_release_device(const char * dev)333 int tst_release_device(const char *dev)
334 {
335 	int ret;
336 
337 	if (!device_acquired)
338 		return 0;
339 
340 	/*
341 	 * Loop device was created -> we need to detach it.
342 	 *
343 	 * The file image is deleted in tst_rmdir();
344 	 */
345 	ret = tst_detach_device(dev);
346 
347 	device_acquired = 0;
348 
349 	return ret;
350 }
351 
tst_clear_device(const char * dev)352 int tst_clear_device(const char *dev)
353 {
354 	if (tst_fill_file(dev, 0, 1024, 512)) {
355 		tst_resm(TWARN, "Failed to clear 512k block on %s", dev);
356 		return 1;
357 	}
358 
359 	return 0;
360 }
361 
tst_umount(const char * path)362 int tst_umount(const char *path)
363 {
364 	int err, ret, i;
365 
366 	for (i = 0; i < 50; i++) {
367 		ret = umount(path);
368 		err = errno;
369 
370 		if (!ret)
371 			return 0;
372 
373 		tst_resm(TINFO, "umount('%s') failed with %s, try %2i...",
374 		         path, tst_strerrno(err), i+1);
375 
376 		if (i == 0 && err == EBUSY) {
377 			tst_resm(TINFO, "Likely gvfsd-trash is probing newly "
378 			         "mounted fs, kill it to speed up tests.");
379 		}
380 
381 		usleep(100000);
382 	}
383 
384 	tst_resm(TWARN, "Failed to umount('%s') after 50 retries", path);
385 	errno = err;
386 	return -1;
387 }
388 
find_stat_file(const char * dev,char * path,size_t path_len)389 int find_stat_file(const char *dev, char *path, size_t path_len)
390 {
391 	const char *devname = strrchr(dev, '/') + 1;
392 
393 	snprintf(path, path_len, "/sys/block/%s/stat", devname);
394 
395 	if (!access(path, F_OK))
396 		return 1;
397 
398 	DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
399 	struct dirent *ent;
400 
401 	while ((ent = readdir(dir))) {
402 		snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
403 
404 		if (!access(path, F_OK)) {
405 			SAFE_CLOSEDIR(NULL, dir);
406 			return 1;
407 		}
408 	}
409 
410 	SAFE_CLOSEDIR(NULL, dir);
411 	return 0;
412 }
413 
tst_dev_bytes_written(const char * dev)414 unsigned long tst_dev_bytes_written(const char *dev)
415 {
416 	unsigned long dev_sec_write = 0, dev_bytes_written, io_ticks = 0;
417 	char dev_stat_path[1024];
418 
419 	if (!find_stat_file(dev, dev_stat_path, sizeof(dev_stat_path)))
420 		tst_brkm(TCONF, NULL, "Test device stat file: %s not found",
421 			 dev_stat_path);
422 
423 	SAFE_FILE_SCANF(NULL, dev_stat_path,
424 			"%*s %*s %*s %*s %*s %*s %lu %*s %*s %lu",
425 			&dev_sec_write, &io_ticks);
426 
427 	if (!io_ticks)
428 		tst_brkm(TCONF, NULL, "Test device stat file: %s broken",
429 			 dev_stat_path);
430 
431 	dev_bytes_written = (dev_sec_write - prev_dev_sec_write) * 512;
432 
433 	prev_dev_sec_write = dev_sec_write;
434 
435 	return dev_bytes_written;
436 }
437