xref: /aosp_15_r20/external/ltp/lib/tst_device.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2014 Cyril Hrubis [email protected]
4  */
5 
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/ioctl.h>
9 #include <sys/mount.h>
10 #include <mntent.h>
11 #include <errno.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <linux/loop.h>
15 #include <stdint.h>
16 #include <inttypes.h>
17 #include <sys/sysmacros.h>
18 #include <linux/btrfs.h>
19 #include <linux/limits.h>
20 #include "lapi/syscalls.h"
21 #include "test.h"
22 #include "safe_macros.h"
23 #include "tst_device.h"
24 
25 #ifndef LOOP_CTL_GET_FREE
26 # define LOOP_CTL_GET_FREE 0x4C82
27 #endif
28 
29 #define LOOP_CONTROL_FILE "/dev/loop-control"
30 
31 #define DEV_FILE "test_dev.img"
32 #define DEV_SIZE_MB 300u
33 #define UUID_STR_SZ 37
34 #define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
35 
36 static char dev_path[PATH_MAX];
37 static int device_acquired;
38 static unsigned long prev_dev_sec_write;
39 
40 static const char * const dev_loop_variants[] = {
41 	"/dev/loop%i",
42 	"/dev/loop/%i",
43 	"/dev/block/loop%i"
44 };
45 
46 static const char * const dev_variants[] = {
47 	"/dev/%s",
48 	"/dev/block/%s"
49 };
50 
set_dev_loop_path(int dev,char * path,size_t path_len)51 static int set_dev_loop_path(int dev, char *path, size_t path_len)
52 {
53 	unsigned int i;
54 	struct stat st;
55 
56 	for (i = 0; i < ARRAY_SIZE(dev_loop_variants); i++) {
57 		snprintf(path, path_len, dev_loop_variants[i], dev);
58 
59 		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
60 			return 0;
61 	}
62 
63 	return 1;
64 }
65 
set_dev_path(char * dev,char * path,size_t path_len)66 static int set_dev_path(char *dev, char *path, size_t path_len)
67 {
68 	unsigned int i;
69 	struct stat st;
70 
71 	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
72 		snprintf(path, path_len, dev_variants[i], dev);
73 
74 		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
75 			return 0;
76 	}
77 
78 	return 1;
79 }
80 
tst_find_free_loopdev(char * path,size_t path_len)81 int tst_find_free_loopdev(char *path, size_t path_len)
82 {
83 	int ctl_fd, dev_fd, rc, i, path_set;
84 	struct loop_info loopinfo;
85 	char buf[PATH_MAX];
86 
87 	/* since Linux 3.1 */
88 	ctl_fd = open(LOOP_CONTROL_FILE, O_RDWR);
89 
90 	if (ctl_fd > 0) {
91 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
92 		close(ctl_fd);
93 		if (rc >= 0) {
94 			if (path) {
95 				// b/148978487 retry to allow time for device creation
96 				for (i = 0; i < 50; i++) {
97 					path_set = set_dev_loop_path(rc, path, path_len);
98 					// set_dev_path returns 1 on success
99 					if (!path_set)
100 						break;
101 					usleep(50000);
102 				}
103 				if (path_set)
104 					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
105 			}
106 			tst_resm(TINFO, "Found free device %d '%s'",
107 				rc, path ?: "");
108 			return rc;
109 		}
110 		tst_resm(TINFO, "Couldn't find free loop device");
111 		return -1;
112 	}
113 
114 	switch (errno) {
115 	case ENOENT:
116 	break;
117 	case EACCES:
118 		tst_resm(TINFO | TERRNO,
119 			 "Not allowed to open " LOOP_CONTROL_FILE ". "
120 			 "Are you root?");
121 	break;
122 	default:
123 		tst_resm(TBROK | TERRNO, "Failed to open " LOOP_CONTROL_FILE);
124 	}
125 
126 	/*
127 	 * Older way is to iterate over /dev/loop%i and /dev/loop/%i and try
128 	 * LOOP_GET_STATUS ioctl() which fails for free loop devices.
129 	 */
130 	for (i = 0; i < 256; i++) {
131 
132 		if (set_dev_loop_path(i, buf, sizeof(buf)))
133 			continue;
134 
135 		dev_fd = open(buf, O_RDONLY);
136 
137 		if (dev_fd < 0)
138 			continue;
139 
140 		if (ioctl(dev_fd, LOOP_GET_STATUS, &loopinfo) == 0) {
141 			tst_resm(TINFO, "Device '%s' in use", buf);
142 		} else {
143 			if (errno != ENXIO)
144 				continue;
145 			tst_resm(TINFO, "Found free device '%s'", buf);
146 			close(dev_fd);
147 			if (path != NULL) {
148 				strncpy(path, buf, path_len);
149 				path[path_len-1] = '\0';
150 			}
151 			return i;
152 		}
153 
154 		close(dev_fd);
155 	}
156 
157 	tst_resm(TINFO, "No free devices found");
158 
159 	return -1;
160 }
161 
tst_attach_device(const char * dev,const char * file)162 int tst_attach_device(const char *dev, const char *file)
163 {
164 	int dev_fd, file_fd;
165 	struct loop_info loopinfo;
166 
167 	// b/148978487 retry to allow time for device creation
168 	int attach_tries = 20;
169 	while (attach_tries--) {
170 		dev_fd = open(dev, O_RDWR);
171 		if (dev_fd >= 0)
172 			break;
173 		usleep(50000);
174 	}
175 	if (dev_fd < 0) {
176 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
177 		return 1;
178 	}
179 
180 	file_fd = open(file, O_RDWR);
181 	if (file_fd < 0) {
182 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", file);
183 		close(dev_fd);
184 		return 1;
185 	}
186 
187 	if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
188 		close(dev_fd);
189 		close(file_fd);
190 		tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
191 			 dev, file);
192 		return 1;
193 	}
194 
195 	/* Old mkfs.btrfs use LOOP_GET_STATUS instead of backing_file to get
196 	 * associated filename, so we need to set up the device by calling
197 	 * LOOP_SET_FD and LOOP_SET_STATUS.
198 	 */
199 	memset(&loopinfo, 0, sizeof(loopinfo));
200 	strcpy(loopinfo.lo_name, file);
201 
202 	if (ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo)) {
203 		close(dev_fd);
204 		close(file_fd);
205 		tst_resm(TWARN | TERRNO,
206 			 "ioctl(%s, LOOP_SET_STATUS, %s) failed", dev, file);
207 		return 1;
208 	}
209 
210 	close(dev_fd);
211 	close(file_fd);
212 	return 0;
213 }
214 
tst_get_device_size(const char * dev_path)215 uint64_t tst_get_device_size(const char *dev_path)
216 {
217 	int fd;
218 	uint64_t size;
219 	struct stat st;
220 
221 	if (!dev_path)
222 		tst_brkm(TBROK, NULL, "No block device path");
223 
224 	if (stat(dev_path, &st)) {
225 		tst_resm(TWARN | TERRNO, "stat() failed");
226 		return -1;
227 	}
228 
229 	if (!S_ISBLK(st.st_mode)) {
230 		tst_resm(TWARN, "%s is not a block device", dev_path);
231 		return -1;
232 	}
233 
234 	fd = open(dev_path, O_RDONLY);
235 	if (fd < 0) {
236 		tst_resm(TWARN | TERRNO,
237 				"open(%s, O_RDONLY) failed", dev_path);
238 		return -1;
239 	}
240 
241 	if (ioctl(fd, BLKGETSIZE64, &size)) {
242 		tst_resm(TWARN | TERRNO,
243 				"ioctl(fd, BLKGETSIZE64, ...) failed");
244 		close(fd);
245 		return -1;
246 	}
247 
248 	if (close(fd)) {
249 		tst_resm(TWARN | TERRNO,
250 				"close(fd) failed");
251 		return -1;
252 	}
253 
254 	return size/1024/1024;
255 }
256 
tst_detach_device_by_fd(const char * dev,int dev_fd)257 int tst_detach_device_by_fd(const char *dev, int dev_fd)
258 {
259 	int ret, i;
260 
261 	/* keep trying to clear LOOPDEV until we get ENXIO, a quick succession
262 	 * of attach/detach might not give udev enough time to complete
263 	 *
264 	 * Since 18048c1af783 ("loop: Fix a race between loop detach and loop open")
265 	 * device is detached only after last close.
266 	 */
267 	for (i = 0; i < 40; i++) {
268 		ret = ioctl(dev_fd, LOOP_CLR_FD, 0);
269 
270 		if (ret && (errno == ENXIO)) {
271 			SAFE_CLOSE(NULL, dev_fd);
272 			return 0;
273 		}
274 
275 		if (ret && (errno != EBUSY)) {
276 			tst_resm(TWARN,
277 				 "ioctl(%s, LOOP_CLR_FD, 0) unexpectedly failed with: %s",
278 				 dev, tst_strerrno(errno));
279 			SAFE_CLOSE(NULL, dev_fd);
280 			return 1;
281 		}
282 
283 		usleep(50000);
284 	}
285 
286 	tst_resm(TWARN,
287 		"ioctl(%s, LOOP_CLR_FD, 0) no ENXIO for too long", dev);
288 	SAFE_CLOSE(NULL, dev_fd);
289 	return 1;
290 }
291 
tst_detach_device(const char * dev)292 int tst_detach_device(const char *dev)
293 {
294 	int dev_fd, ret;
295 
296 	dev_fd = open(dev, O_RDONLY);
297 	if (dev_fd < 0) {
298 		tst_resm(TWARN | TERRNO, "open(%s) failed", dev);
299 		return 1;
300 	}
301 
302 	ret = tst_detach_device_by_fd(dev, dev_fd);
303 	return ret;
304 }
305 
tst_dev_sync(int fd)306 int tst_dev_sync(int fd)
307 {
308 	return syscall(__NR_syncfs, fd);
309 }
310 
tst_acquire_loop_device(unsigned int size,const char * filename)311 const char *tst_acquire_loop_device(unsigned int size, const char *filename)
312 {
313 	unsigned int acq_dev_size = size ? size : DEV_SIZE_MB;
314 
315 	if (tst_prealloc_file(filename, 1024 * 1024, acq_dev_size)) {
316 		tst_resm(TWARN | TERRNO, "Failed to create %s", filename);
317 		return NULL;
318 	}
319 
320 	if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
321 		return NULL;
322 
323 	if (tst_attach_device(dev_path, filename))
324 		return NULL;
325 
326 	return dev_path;
327 }
328 
tst_acquire_device__(unsigned int size)329 const char *tst_acquire_device__(unsigned int size)
330 {
331 	const char *dev;
332 	unsigned int acq_dev_size;
333 	uint64_t ltp_dev_size;
334 
335 	acq_dev_size = size ? size : DEV_SIZE_MB;
336 
337 	dev = getenv("LTP_DEV");
338 
339 	if (dev) {
340 		tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev);
341 
342 		ltp_dev_size = tst_get_device_size(dev);
343 
344 		if (acq_dev_size <= ltp_dev_size)
345 			return dev;
346 
347 		tst_resm(TINFO, "Skipping $LTP_DEV size %"PRIu64"MB, requested size %uMB",
348 				ltp_dev_size, acq_dev_size);
349 	}
350 
351 	dev = tst_acquire_loop_device(acq_dev_size, DEV_FILE);
352 
353 	if (dev)
354 		device_acquired = 1;
355 
356 	return dev;
357 }
358 
tst_acquire_device_(void (cleanup_fn)(void),unsigned int size)359 const char *tst_acquire_device_(void (cleanup_fn)(void), unsigned int size)
360 {
361 	const char *device;
362 
363 	if (device_acquired) {
364 		tst_brkm(TBROK, cleanup_fn, "Device already acquired");
365 		return NULL;
366 	}
367 
368 	if (!tst_tmpdir_created()) {
369 		tst_brkm(TBROK, cleanup_fn,
370 			 "Cannot acquire device without tmpdir() created");
371 		return NULL;
372 	}
373 
374 	device = tst_acquire_device__(size);
375 
376 	if (!device) {
377 		tst_brkm(TBROK, cleanup_fn, "Failed to acquire device");
378 		return NULL;
379 	}
380 
381 	return device;
382 }
383 
tst_release_device(const char * dev)384 int tst_release_device(const char *dev)
385 {
386 	int ret;
387 
388 	if (!device_acquired)
389 		return 0;
390 
391 	/*
392 	 * Loop device was created -> we need to detach it.
393 	 *
394 	 * The file image is deleted in tst_rmdir();
395 	 */
396 	ret = tst_detach_device(dev);
397 
398 	device_acquired = 0;
399 
400 	return ret;
401 }
402 
tst_clear_device(const char * dev)403 int tst_clear_device(const char *dev)
404 {
405 	if (tst_fill_file(dev, 0, 1024, 512)) {
406 		tst_resm(TWARN, "Failed to clear 512k block on %s", dev);
407 		return 1;
408 	}
409 
410 	return 0;
411 }
412 
tst_umount(const char * path)413 int tst_umount(const char *path)
414 {
415 	int err, ret, i;
416 
417 	for (i = 0; i < 50; i++) {
418 		ret = umount(path);
419 		err = errno;
420 
421 		if (!ret)
422 			return 0;
423 
424 		if (err != EBUSY) {
425 			tst_resm(TWARN, "umount('%s') failed with %s",
426 				 path, tst_strerrno(err));
427 			errno = err;
428 			return ret;
429 		}
430 
431 		tst_resm(TINFO, "umount('%s') failed with %s, try %2i...",
432 			 path, tst_strerrno(err), i+1);
433 
434 		if (i == 0) {
435 			tst_resm(TINFO, "Likely gvfsd-trash is probing newly "
436 				 "mounted fs, kill it to speed up tests.");
437 		}
438 
439 		usleep(100000);
440 	}
441 
442 	tst_resm(TWARN, "Failed to umount('%s') after 50 retries", path);
443 	errno = err;
444 	return -1;
445 }
446 
tst_is_mounted(const char * path)447 int tst_is_mounted(const char *path)
448 {
449 	char line[PATH_MAX];
450 	FILE *file;
451 	int ret = 0;
452 
453 	file = SAFE_FOPEN(NULL, "/proc/mounts", "r");
454 
455 	while (fgets(line, sizeof(line), file)) {
456 		if (strstr(line, path) != NULL) {
457 			ret = 1;
458 			break;
459 		}
460 	}
461 
462 	SAFE_FCLOSE(NULL, file);
463 
464 	if (!ret)
465 		tst_resm(TINFO, "No device is mounted at %s", path);
466 
467 	return ret;
468 }
469 
tst_is_mounted_at_tmpdir(const char * path)470 int tst_is_mounted_at_tmpdir(const char *path)
471 {
472 	char cdir[PATH_MAX], mpath[PATH_MAX];
473 	int ret;
474 
475 	if (!getcwd(cdir, PATH_MAX)) {
476 		tst_resm(TWARN | TERRNO, "Failed to find current directory");
477 		return 0;
478 	}
479 
480 	ret = snprintf(mpath, PATH_MAX, "%s/%s", cdir, path);
481 	if (ret < 0 || ret >= PATH_MAX) {
482 		tst_resm(TWARN | TERRNO,
483 			 "snprintf() should have returned %d instead of %d",
484 			 PATH_MAX, ret);
485 		return 0;
486 	}
487 
488 	return tst_is_mounted(mpath);
489 }
490 
find_stat_file(const char * dev,char * path,size_t path_len)491 static int find_stat_file(const char *dev, char *path, size_t path_len)
492 {
493 	const char *devname = strrchr(dev, '/') + 1;
494 
495 	snprintf(path, path_len, "/sys/block/%s/stat", devname);
496 
497 	if (!access(path, F_OK))
498 		return 1;
499 
500 	DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
501 	struct dirent *ent;
502 
503 	while ((ent = readdir(dir))) {
504 		snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
505 
506 		if (!access(path, F_OK)) {
507 			SAFE_CLOSEDIR(NULL, dir);
508 			return 1;
509 		}
510 	}
511 
512 	SAFE_CLOSEDIR(NULL, dir);
513 	return 0;
514 }
515 
tst_dev_bytes_written(const char * dev)516 unsigned long tst_dev_bytes_written(const char *dev)
517 {
518 	unsigned long dev_sec_write = 0, dev_bytes_written, io_ticks = 0;
519 	char dev_stat_path[PATH_MAX];
520 
521 	if (!find_stat_file(dev, dev_stat_path, sizeof(dev_stat_path)))
522 		tst_brkm(TCONF, NULL, "Test device stat file: %s not found",
523 			 dev_stat_path);
524 
525 	SAFE_FILE_SCANF(NULL, dev_stat_path,
526 			"%*s %*s %*s %*s %*s %*s %lu %*s %*s %lu",
527 			&dev_sec_write, &io_ticks);
528 
529 	if (!io_ticks)
530 		tst_brkm(TCONF, NULL, "Test device stat file: %s broken",
531 			 dev_stat_path);
532 
533 	dev_bytes_written = (dev_sec_write - prev_dev_sec_write) * 512;
534 
535 	prev_dev_sec_write = dev_sec_write;
536 
537 	return dev_bytes_written;
538 }
539 
540 __attribute__((nonnull))
tst_find_backing_dev(const char * path,char * dev,size_t dev_size)541 void tst_find_backing_dev(const char *path, char *dev, size_t dev_size)
542 {
543 	struct stat buf;
544 	struct btrfs_ioctl_fs_info_args args = {0};
545 	struct dirent *d;
546 	char uevent_path[PATH_MAX+PATH_MAX+10]; //10 is for the static uevent path
547 	char dev_name[NAME_MAX];
548 	char bdev_path[PATH_MAX];
549 	char tmp_path[PATH_MAX];
550 	char btrfs_uuid_str[UUID_STR_SZ];
551 	DIR *dir;
552 	unsigned int dev_major, dev_minor;
553 	int fd;
554 
555 	if (stat(path, &buf) < 0)
556 		tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
557 
558 	strncpy(tmp_path, path, PATH_MAX-1);
559 	tmp_path[PATH_MAX-1] = '\0';
560 	if (S_ISREG(buf.st_mode))
561 		dirname(tmp_path);
562 
563 	dev_major = major(buf.st_dev);
564 	dev_minor = minor(buf.st_dev);
565 	*dev = '\0';
566 
567 	if (dev_major == 0) {
568 		tst_resm(TINFO, "Use BTRFS specific strategy");
569 
570 		fd = SAFE_OPEN(NULL, tmp_path, O_DIRECTORY);
571 		if (!ioctl(fd, BTRFS_IOC_FS_INFO, &args)) {
572 			sprintf(btrfs_uuid_str,
573 				UUID_FMT,
574 				args.fsid[0], args.fsid[1],
575 				args.fsid[2], args.fsid[3],
576 				args.fsid[4], args.fsid[5],
577 				args.fsid[6], args.fsid[7],
578 				args.fsid[8], args.fsid[9],
579 				args.fsid[10], args.fsid[11],
580 				args.fsid[12], args.fsid[13],
581 				args.fsid[14], args.fsid[15]);
582 			sprintf(bdev_path,
583 				"/sys/fs/btrfs/%s/devices", btrfs_uuid_str);
584 		} else {
585 			if (errno == ENOTTY)
586 				tst_brkm(TBROK | TERRNO, NULL, "BTRFS ioctl failed. Is %s on a tmpfs?", path);
587 
588 			tst_brkm(TBROK | TERRNO, NULL, "BTRFS ioctl on %s failed.", tmp_path);
589 		}
590 		SAFE_CLOSE(NULL, fd);
591 
592 		dir = SAFE_OPENDIR(NULL, bdev_path);
593 		while ((d = SAFE_READDIR(NULL, dir))) {
594 			if (d->d_name[0] != '.')
595 				break;
596 		}
597 
598 		uevent_path[0] = '\0';
599 
600 		if (d) {
601 			sprintf(uevent_path, "%s/%s/uevent",
602 				bdev_path, d->d_name);
603 		} else {
604 			tst_brkm(TBROK | TERRNO, NULL, "No backing device found while looking in %s.", bdev_path);
605 		}
606 
607 		if (SAFE_READDIR(NULL, dir))
608 			tst_resm(TINFO, "Warning: used first of multiple backing device.");
609 
610 		SAFE_CLOSEDIR(NULL, dir);
611 	} else {
612 		tst_resm(TINFO, "Use uevent strategy");
613 		sprintf(uevent_path,
614 			"/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
615 	}
616 
617 	if (!access(uevent_path, R_OK)) {
618 		FILE_LINES_SCANF(NULL, uevent_path, "DEVNAME=%s", dev_name);
619 
620 		if (!dev_name[0] || set_dev_path(dev_name, dev, dev_size))
621 			tst_brkm(TBROK, NULL, "Could not stat backing device %s", dev);
622 
623 	} else {
624 		tst_brkm(TBROK, NULL, "uevent file (%s) access failed", uevent_path);
625 	}
626 }
627 
tst_stat_mount_dev(const char * const mnt_path,struct stat * const st)628 void tst_stat_mount_dev(const char *const mnt_path, struct stat *const st)
629 {
630 	struct mntent *mnt;
631 	FILE *mntf = setmntent("/proc/self/mounts", "r");
632 
633 	if (!mntf) {
634 		tst_brkm(TBROK | TERRNO, NULL, "Can't open /proc/self/mounts");
635 		return;
636 	}
637 
638 	mnt = getmntent(mntf);
639 	if (!mnt) {
640 		tst_brkm(TBROK | TERRNO, NULL, "Can't read mounts or no mounts?");
641 		return;
642 	}
643 
644 	do {
645 		if (strcmp(mnt->mnt_dir, mnt_path)) {
646 			mnt = getmntent(mntf);
647 			continue;
648 		}
649 
650 		if (stat(mnt->mnt_fsname, st)) {
651 			tst_brkm(TBROK | TERRNO, NULL,
652 				 "Can't stat '%s', mounted at '%s'",
653 				 mnt->mnt_fsname, mnt_path);
654 		}
655 
656 		return;
657 	} while (mnt);
658 
659 	tst_brkm(TBROK, NULL, "Could not find mount device");
660 }
661 
tst_dev_block_size(const char * path)662 int tst_dev_block_size(const char *path)
663 {
664 	int fd;
665 	int size;
666 	char dev_name[PATH_MAX];
667 
668 	tst_find_backing_dev(path, dev_name, sizeof(dev_name));
669 
670 	fd = SAFE_OPEN(NULL, dev_name, O_RDONLY);
671 	SAFE_IOCTL(NULL, fd, BLKSSZGET, &size);
672 	SAFE_CLOSE(NULL, fd);
673 
674 	return size;
675 }
676