• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/sysmacros.h>
35 #include "lapi/syscalls.h"
36 #include "test.h"
37 #include "safe_macros.h"
38 
39 #ifndef LOOP_CTL_GET_FREE
40 # define LOOP_CTL_GET_FREE 0x4C82
41 #endif
42 
43 #define LOOP_CONTROL_FILE "/dev/loop-control"
44 
45 #define DEV_FILE "test_dev.img"
46 #define DEV_SIZE_MB 256u
47 
48 static char dev_path[1024];
49 static int device_acquired;
50 static unsigned long prev_dev_sec_write;
51 
52 static const char *dev_variants[] = {
53 	"/dev/loop%i",
54 	"/dev/loop/%i",
55 	"/dev/block/loop%i"
56 };
57 
set_dev_path(int dev,char * path,size_t path_len)58 static int set_dev_path(int dev, char *path, size_t path_len)
59 {
60 	unsigned int i;
61 	struct stat st;
62 
63 	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
64 		snprintf(path, path_len, dev_variants[i], dev);
65 
66 		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
67 			return 1;
68 	}
69 
70 	return 0;
71 }
72 
tst_find_free_loopdev(char * path,size_t path_len)73 int tst_find_free_loopdev(char *path, size_t path_len)
74 {
75 	int ctl_fd, dev_fd, rc, i;
76 	struct loop_info loopinfo;
77 	char buf[1024];
78 
79 	/* since Linux 3.1 */
80 	ctl_fd = open(LOOP_CONTROL_FILE, O_RDWR);
81 
82 	if (ctl_fd > 0) {
83 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
84 		close(ctl_fd);
85 		if (rc >= 0) {
86 			if (path)
87 				set_dev_path(rc, path, path_len);
88 			tst_resm(TINFO, "Found free device %d '%s'",
89 				rc, path ?: "");
90 			return rc;
91 		}
92 		tst_resm(TINFO, "Couldn't find free loop device");
93 		return -1;
94 	}
95 
96 	switch (errno) {
97 	case ENOENT:
98 	break;
99 	case EACCES:
100 		tst_resm(TINFO | TERRNO,
101 		         "Not allowed to open " LOOP_CONTROL_FILE ". "
102 			 "Are you root?");
103 	break;
104 	default:
105 		tst_resm(TBROK | TERRNO, "Failed to open " LOOP_CONTROL_FILE);
106 	}
107 
108 	/*
109 	 * Older way is to iterate over /dev/loop%i and /dev/loop/%i and try
110 	 * LOOP_GET_STATUS ioctl() which fails for free loop devices.
111 	 */
112 	for (i = 0; i < 256; i++) {
113 
114 		if (!set_dev_path(i, buf, sizeof(buf)))
115 			continue;
116 
117 		dev_fd = open(buf, O_RDONLY);
118 
119 		if (dev_fd < 0)
120 			continue;
121 
122 		if (ioctl(dev_fd, LOOP_GET_STATUS, &loopinfo) == 0) {
123 			tst_resm(TINFO, "Device '%s' in use", buf);
124 		} else {
125 			if (errno != ENXIO)
126 				continue;
127 			tst_resm(TINFO, "Found free device '%s'", buf);
128 			close(dev_fd);
129 			if (path != NULL) {
130 				strncpy(path, buf, path_len);
131 				path[path_len-1] = '\0';
132 			}
133 			return i;
134 		}
135 
136 		close(dev_fd);
137 	}
138 
139 	tst_resm(TINFO, "No free devices found");
140 
141 	return -1;
142 }
143 
tst_attach_device(const char * dev,const char * file)144 int tst_attach_device(const char *dev, const char *file)
145 {
146 	int dev_fd, file_fd;
147 	struct loop_info loopinfo;
148 
149 	dev_fd = open(dev, O_RDWR);
150 	if (dev_fd < 0) {
151 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
152 		return 1;
153 	}
154 
155 	file_fd = open(file, O_RDWR);
156 	if (file_fd < 0) {
157 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", file);
158 		close(dev_fd);
159 		return 1;
160 	}
161 
162 	if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
163 		close(dev_fd);
164 		close(file_fd);
165 		tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
166 			 dev, file);
167 		return 1;
168 	}
169 
170 	/* Old mkfs.btrfs use LOOP_GET_STATUS instead of backing_file to get
171 	 * associated filename, so we need to set up the device by calling
172 	 * LOOP_SET_FD and LOOP_SET_STATUS.
173 	 */
174 	memset(&loopinfo, 0, sizeof(loopinfo));
175 	strcpy(loopinfo.lo_name, file);
176 
177 	if (ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo)) {
178 		close(dev_fd);
179 		close(file_fd);
180 		tst_resm(TWARN | TERRNO,
181 			 "ioctl(%s, LOOP_SET_STATUS, %s) failed", dev, file);
182 		return 1;
183 	}
184 
185 	close(dev_fd);
186 	close(file_fd);
187 	return 0;
188 }
189 
tst_get_device_size(const char * dev_path)190 uint64_t tst_get_device_size(const char *dev_path)
191 {
192 	int fd;
193 	uint64_t size;
194 	struct stat st;
195 
196 	if (!dev_path)
197 		tst_brkm(TBROK, NULL, "No block device path");
198 
199 	if (stat(dev_path, &st)) {
200 		tst_resm(TWARN | TERRNO, "stat() failed");
201 		return -1;
202 	}
203 
204 	if (!S_ISBLK(st.st_mode)) {
205 		tst_resm(TWARN, "%s is not a block device", dev_path);
206 		return -1;
207 	}
208 
209 	fd = open(dev_path, O_RDONLY);
210 	if (fd < 0) {
211 		tst_resm(TWARN | TERRNO,
212 				"open(%s, O_RDONLY) failed", dev_path);
213 		return -1;
214 	}
215 
216 	if (ioctl(fd, BLKGETSIZE64, &size)) {
217 		tst_resm(TWARN | TERRNO,
218 				"ioctl(fd, BLKGETSIZE64, ...) failed");
219 		close(fd);
220 		return -1;
221 	}
222 
223 	if (close(fd)) {
224 		tst_resm(TWARN | TERRNO,
225 				"close(fd) failed");
226 		return -1;
227 	}
228 
229 	return size/1024/1024;
230 }
231 
tst_detach_device_by_fd(const char * dev,int dev_fd)232 int tst_detach_device_by_fd(const char *dev, int dev_fd)
233 {
234 	int ret, i;
235 
236 	/* keep trying to clear LOOPDEV until we get ENXIO, a quick succession
237 	 * of attach/detach might not give udev enough time to complete */
238 	for (i = 0; i < 40; i++) {
239 		ret = ioctl(dev_fd, LOOP_CLR_FD, 0);
240 
241 		if (ret && (errno == ENXIO))
242 			return 0;
243 
244 		if (ret && (errno != EBUSY)) {
245 			tst_resm(TWARN,
246 				 "ioctl(%s, LOOP_CLR_FD, 0) unexpectedly failed with: %s",
247 				 dev, tst_strerrno(errno));
248 			return 1;
249 		}
250 
251 		usleep(50000);
252 	}
253 
254 	tst_resm(TWARN,
255 		"ioctl(%s, LOOP_CLR_FD, 0) no ENXIO for too long", dev);
256 	return 1;
257 }
258 
tst_detach_device(const char * dev)259 int tst_detach_device(const char *dev)
260 {
261 	int dev_fd, ret;
262 
263 	dev_fd = open(dev, O_RDONLY);
264 	if (dev_fd < 0) {
265 		tst_resm(TWARN | TERRNO, "open(%s) failed", dev);
266 		return 1;
267 	}
268 
269 	ret = tst_detach_device_by_fd(dev, dev_fd);
270 	close(dev_fd);
271 	return ret;
272 }
273 
tst_dev_sync(int fd)274 int tst_dev_sync(int fd)
275 {
276 	return syscall(__NR_syncfs, fd);
277 }
278 
tst_acquire_loop_device(unsigned int size,const char * filename)279 const char *tst_acquire_loop_device(unsigned int size, const char *filename)
280 {
281 	unsigned int acq_dev_size = size ? size : DEV_SIZE_MB;
282 
283 	if (tst_prealloc_file(filename, 1024 * 1024, acq_dev_size)) {
284 		tst_resm(TWARN | TERRNO, "Failed to create %s", filename);
285 		return NULL;
286 	}
287 
288 	if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
289 		return NULL;
290 
291 	if (tst_attach_device(dev_path, filename))
292 		return NULL;
293 
294 	return dev_path;
295 }
296 
tst_acquire_device__(unsigned int size)297 const char *tst_acquire_device__(unsigned int size)
298 {
299 	const char *dev;
300 	unsigned int acq_dev_size;
301 	uint64_t ltp_dev_size;
302 
303 	acq_dev_size = size ? size : DEV_SIZE_MB;
304 
305 	dev = getenv("LTP_DEV");
306 
307 	if (dev) {
308 		tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev);
309 
310 		ltp_dev_size = tst_get_device_size(dev);
311 
312 		if (acq_dev_size <= ltp_dev_size)
313 			return dev;
314 
315 		tst_resm(TINFO, "Skipping $LTP_DEV size %"PRIu64"MB, requested size %uMB",
316 				ltp_dev_size, acq_dev_size);
317 	}
318 
319 	dev = tst_acquire_loop_device(acq_dev_size, DEV_FILE);
320 
321 	if (dev)
322 		device_acquired = 1;
323 
324 	return dev;
325 }
326 
tst_acquire_device_(void (cleanup_fn)(void),unsigned int size)327 const char *tst_acquire_device_(void (cleanup_fn)(void), unsigned int size)
328 {
329 	const char *device;
330 
331 	if (device_acquired) {
332 		tst_brkm(TBROK, cleanup_fn, "Device already acquired");
333 		return NULL;
334 	}
335 
336 	if (!tst_tmpdir_created()) {
337 		tst_brkm(TBROK, cleanup_fn,
338 		         "Cannot acquire device without tmpdir() created");
339 		return NULL;
340 	}
341 
342 	device = tst_acquire_device__(size);
343 
344 	if (!device) {
345 		tst_brkm(TBROK, cleanup_fn, "Failed to acquire device");
346 		return NULL;
347 	}
348 
349 	return device;
350 }
351 
tst_release_device(const char * dev)352 int tst_release_device(const char *dev)
353 {
354 	int ret;
355 
356 	if (!device_acquired)
357 		return 0;
358 
359 	/*
360 	 * Loop device was created -> we need to detach it.
361 	 *
362 	 * The file image is deleted in tst_rmdir();
363 	 */
364 	ret = tst_detach_device(dev);
365 
366 	device_acquired = 0;
367 
368 	return ret;
369 }
370 
tst_clear_device(const char * dev)371 int tst_clear_device(const char *dev)
372 {
373 	if (tst_fill_file(dev, 0, 1024, 512)) {
374 		tst_resm(TWARN, "Failed to clear 512k block on %s", dev);
375 		return 1;
376 	}
377 
378 	return 0;
379 }
380 
tst_umount(const char * path)381 int tst_umount(const char *path)
382 {
383 	int err, ret, i;
384 
385 	for (i = 0; i < 50; i++) {
386 		ret = umount(path);
387 		err = errno;
388 
389 		if (!ret)
390 			return 0;
391 
392 		if (err != EBUSY) {
393 			tst_resm(TWARN, "umount('%s') failed with %s",
394 		         path, tst_strerrno(err));
395 			errno = err;
396 			return ret;
397 		}
398 
399 		tst_resm(TINFO, "umount('%s') failed with %s, try %2i...",
400 		         path, tst_strerrno(err), i+1);
401 
402 		if (i == 0) {
403 			tst_resm(TINFO, "Likely gvfsd-trash is probing newly "
404 			         "mounted fs, kill it to speed up tests.");
405 		}
406 
407 		usleep(100000);
408 	}
409 
410 	tst_resm(TWARN, "Failed to umount('%s') after 50 retries", path);
411 	errno = err;
412 	return -1;
413 }
414 
tst_is_mounted(const char * path)415 int tst_is_mounted(const char *path)
416 {
417 	char line[PATH_MAX];
418 	FILE *file;
419 	int ret = 0;
420 
421 	file = SAFE_FOPEN(NULL, "/proc/mounts", "r");
422 
423 	while (fgets(line, sizeof(line), file)) {
424 		if (strstr(line, path) != NULL) {
425 			ret = 1;
426 			break;
427 		}
428 	}
429 
430 	SAFE_FCLOSE(NULL, file);
431 
432 	if (!ret)
433 		tst_resm(TINFO, "No device is mounted at %s", path);
434 
435 	return ret;
436 }
437 
tst_is_mounted_at_tmpdir(const char * path)438 int tst_is_mounted_at_tmpdir(const char *path)
439 {
440 	char cdir[PATH_MAX], mpath[PATH_MAX];
441 	int ret;
442 
443 	if (!getcwd(cdir, PATH_MAX)) {
444 		tst_resm(TWARN | TERRNO, "Failed to find current directory");
445 		return 0;
446 	}
447 
448 	ret = snprintf(mpath, PATH_MAX, "%s/%s", cdir, path);
449 	if (ret < 0 || ret >= PATH_MAX) {
450 		tst_resm(TWARN | TERRNO,
451 			 "snprintf() should have returned %d instead of %d",
452 			 PATH_MAX, ret);
453 		return 0;
454 	}
455 
456 	return tst_is_mounted(mpath);
457 }
458 
find_stat_file(const char * dev,char * path,size_t path_len)459 int find_stat_file(const char *dev, char *path, size_t path_len)
460 {
461 	const char *devname = strrchr(dev, '/') + 1;
462 
463 	snprintf(path, path_len, "/sys/block/%s/stat", devname);
464 
465 	if (!access(path, F_OK))
466 		return 1;
467 
468 	DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
469 	struct dirent *ent;
470 
471 	while ((ent = readdir(dir))) {
472 		snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
473 
474 		if (!access(path, F_OK)) {
475 			SAFE_CLOSEDIR(NULL, dir);
476 			return 1;
477 		}
478 	}
479 
480 	SAFE_CLOSEDIR(NULL, dir);
481 	return 0;
482 }
483 
tst_dev_bytes_written(const char * dev)484 unsigned long tst_dev_bytes_written(const char *dev)
485 {
486 	unsigned long dev_sec_write = 0, dev_bytes_written, io_ticks = 0;
487 	char dev_stat_path[1024];
488 
489 	if (!find_stat_file(dev, dev_stat_path, sizeof(dev_stat_path)))
490 		tst_brkm(TCONF, NULL, "Test device stat file: %s not found",
491 			 dev_stat_path);
492 
493 	SAFE_FILE_SCANF(NULL, dev_stat_path,
494 			"%*s %*s %*s %*s %*s %*s %lu %*s %*s %lu",
495 			&dev_sec_write, &io_ticks);
496 
497 	if (!io_ticks)
498 		tst_brkm(TCONF, NULL, "Test device stat file: %s broken",
499 			 dev_stat_path);
500 
501 	dev_bytes_written = (dev_sec_write - prev_dev_sec_write) * 512;
502 
503 	prev_dev_sec_write = dev_sec_write;
504 
505 	return dev_bytes_written;
506 }
507 
tst_find_backing_dev(const char * path,char * dev)508 void tst_find_backing_dev(const char *path, char *dev)
509 {
510 	struct stat buf;
511 	FILE *file;
512 	char line[PATH_MAX];
513 	char *pre = NULL;
514 	char *next = NULL;
515 	unsigned int dev_major, dev_minor, line_mjr, line_mnr;
516 
517 	if (stat(path, &buf) < 0)
518 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
519 
520 	dev_major = major(buf.st_dev);
521 	dev_minor = minor(buf.st_dev);
522 	file = SAFE_FOPEN(NULL, "/proc/self/mountinfo", "r");
523 	*dev = '\0';
524 
525 	while (fgets(line, sizeof(line), file)) {
526 		if (sscanf(line, "%*d %*d %d:%d", &line_mjr, &line_mnr) != 2)
527 			continue;
528 
529 		if (line_mjr == dev_major && line_mnr == dev_minor) {
530 			pre = strstr(line, " - ");
531 			pre = strtok_r(pre, " ", &next);
532 			pre = strtok_r(NULL, " ", &next);
533 			pre = strtok_r(NULL, " ", &next);
534 			strcpy(dev, pre);
535 			break;
536 		}
537 	}
538 
539 	SAFE_FCLOSE(NULL, file);
540 
541 	if (!*dev)
542 		tst_brkm(TBROK, NULL, "Cannot find block device for %s", path);
543 
544 	if (stat(dev, &buf) < 0)
545 		tst_brkm(TWARN | TERRNO, NULL, "stat(%s) failed", dev);
546 
547 	if (S_ISBLK(buf.st_mode) != 1)
548 		tst_brkm(TCONF, NULL, "dev(%s) isn't a block dev", dev);
549 }
550