1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2014 Cyril Hrubis chrubis@suse.cz
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 for (i = 0; i < 40; i++) {
265 ret = ioctl(dev_fd, LOOP_CLR_FD, 0);
266
267 if (ret && (errno == ENXIO))
268 return 0;
269
270 if (ret && (errno != EBUSY)) {
271 tst_resm(TWARN,
272 "ioctl(%s, LOOP_CLR_FD, 0) unexpectedly failed with: %s",
273 dev, tst_strerrno(errno));
274 return 1;
275 }
276
277 usleep(50000);
278 }
279
280 tst_resm(TWARN,
281 "ioctl(%s, LOOP_CLR_FD, 0) no ENXIO for too long", dev);
282 return 1;
283 }
284
tst_detach_device(const char * dev)285 int tst_detach_device(const char *dev)
286 {
287 int dev_fd, ret;
288
289 dev_fd = open(dev, O_RDONLY);
290 if (dev_fd < 0) {
291 tst_resm(TWARN | TERRNO, "open(%s) failed", dev);
292 return 1;
293 }
294
295 ret = tst_detach_device_by_fd(dev, dev_fd);
296 close(dev_fd);
297 return ret;
298 }
299
tst_dev_sync(int fd)300 int tst_dev_sync(int fd)
301 {
302 return syscall(__NR_syncfs, fd);
303 }
304
tst_acquire_loop_device(unsigned int size,const char * filename)305 const char *tst_acquire_loop_device(unsigned int size, const char *filename)
306 {
307 unsigned int acq_dev_size = size ? size : DEV_SIZE_MB;
308
309 if (tst_prealloc_file(filename, 1024 * 1024, acq_dev_size)) {
310 tst_resm(TWARN | TERRNO, "Failed to create %s", filename);
311 return NULL;
312 }
313
314 if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
315 return NULL;
316
317 if (tst_attach_device(dev_path, filename))
318 return NULL;
319
320 return dev_path;
321 }
322
tst_acquire_device__(unsigned int size)323 const char *tst_acquire_device__(unsigned int size)
324 {
325 const char *dev;
326 unsigned int acq_dev_size;
327 uint64_t ltp_dev_size;
328
329 acq_dev_size = size ? size : DEV_SIZE_MB;
330
331 dev = getenv("LTP_DEV");
332
333 if (dev) {
334 tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev);
335
336 ltp_dev_size = tst_get_device_size(dev);
337
338 if (acq_dev_size <= ltp_dev_size)
339 return dev;
340
341 tst_resm(TINFO, "Skipping $LTP_DEV size %"PRIu64"MB, requested size %uMB",
342 ltp_dev_size, acq_dev_size);
343 }
344
345 dev = tst_acquire_loop_device(acq_dev_size, DEV_FILE);
346
347 if (dev)
348 device_acquired = 1;
349
350 return dev;
351 }
352
tst_acquire_device_(void (cleanup_fn)(void),unsigned int size)353 const char *tst_acquire_device_(void (cleanup_fn)(void), unsigned int size)
354 {
355 const char *device;
356
357 if (device_acquired) {
358 tst_brkm(TBROK, cleanup_fn, "Device already acquired");
359 return NULL;
360 }
361
362 if (!tst_tmpdir_created()) {
363 tst_brkm(TBROK, cleanup_fn,
364 "Cannot acquire device without tmpdir() created");
365 return NULL;
366 }
367
368 device = tst_acquire_device__(size);
369
370 if (!device) {
371 tst_brkm(TBROK, cleanup_fn, "Failed to acquire device");
372 return NULL;
373 }
374
375 return device;
376 }
377
tst_release_device(const char * dev)378 int tst_release_device(const char *dev)
379 {
380 int ret;
381
382 if (!device_acquired)
383 return 0;
384
385 /*
386 * Loop device was created -> we need to detach it.
387 *
388 * The file image is deleted in tst_rmdir();
389 */
390 ret = tst_detach_device(dev);
391
392 device_acquired = 0;
393
394 return ret;
395 }
396
tst_clear_device(const char * dev)397 int tst_clear_device(const char *dev)
398 {
399 if (tst_fill_file(dev, 0, 1024, 512)) {
400 tst_resm(TWARN, "Failed to clear 512k block on %s", dev);
401 return 1;
402 }
403
404 return 0;
405 }
406
tst_umount(const char * path)407 int tst_umount(const char *path)
408 {
409 int err, ret, i;
410
411 for (i = 0; i < 50; i++) {
412 ret = umount(path);
413 err = errno;
414
415 if (!ret)
416 return 0;
417
418 if (err != EBUSY) {
419 tst_resm(TWARN, "umount('%s') failed with %s",
420 path, tst_strerrno(err));
421 errno = err;
422 return ret;
423 }
424
425 tst_resm(TINFO, "umount('%s') failed with %s, try %2i...",
426 path, tst_strerrno(err), i+1);
427
428 if (i == 0) {
429 tst_resm(TINFO, "Likely gvfsd-trash is probing newly "
430 "mounted fs, kill it to speed up tests.");
431 }
432
433 usleep(100000);
434 }
435
436 tst_resm(TWARN, "Failed to umount('%s') after 50 retries", path);
437 errno = err;
438 return -1;
439 }
440
tst_is_mounted(const char * path)441 int tst_is_mounted(const char *path)
442 {
443 char line[PATH_MAX];
444 FILE *file;
445 int ret = 0;
446
447 file = SAFE_FOPEN(NULL, "/proc/mounts", "r");
448
449 while (fgets(line, sizeof(line), file)) {
450 if (strstr(line, path) != NULL) {
451 ret = 1;
452 break;
453 }
454 }
455
456 SAFE_FCLOSE(NULL, file);
457
458 if (!ret)
459 tst_resm(TINFO, "No device is mounted at %s", path);
460
461 return ret;
462 }
463
tst_is_mounted_at_tmpdir(const char * path)464 int tst_is_mounted_at_tmpdir(const char *path)
465 {
466 char cdir[PATH_MAX], mpath[PATH_MAX];
467 int ret;
468
469 if (!getcwd(cdir, PATH_MAX)) {
470 tst_resm(TWARN | TERRNO, "Failed to find current directory");
471 return 0;
472 }
473
474 ret = snprintf(mpath, PATH_MAX, "%s/%s", cdir, path);
475 if (ret < 0 || ret >= PATH_MAX) {
476 tst_resm(TWARN | TERRNO,
477 "snprintf() should have returned %d instead of %d",
478 PATH_MAX, ret);
479 return 0;
480 }
481
482 return tst_is_mounted(mpath);
483 }
484
find_stat_file(const char * dev,char * path,size_t path_len)485 static int find_stat_file(const char *dev, char *path, size_t path_len)
486 {
487 const char *devname = strrchr(dev, '/') + 1;
488
489 snprintf(path, path_len, "/sys/block/%s/stat", devname);
490
491 if (!access(path, F_OK))
492 return 1;
493
494 DIR *dir = SAFE_OPENDIR(NULL, "/sys/block/");
495 struct dirent *ent;
496
497 while ((ent = readdir(dir))) {
498 snprintf(path, path_len, "/sys/block/%s/%s/stat", ent->d_name, devname);
499
500 if (!access(path, F_OK)) {
501 SAFE_CLOSEDIR(NULL, dir);
502 return 1;
503 }
504 }
505
506 SAFE_CLOSEDIR(NULL, dir);
507 return 0;
508 }
509
tst_dev_bytes_written(const char * dev)510 unsigned long tst_dev_bytes_written(const char *dev)
511 {
512 unsigned long dev_sec_write = 0, dev_bytes_written, io_ticks = 0;
513 char dev_stat_path[PATH_MAX];
514
515 if (!find_stat_file(dev, dev_stat_path, sizeof(dev_stat_path)))
516 tst_brkm(TCONF, NULL, "Test device stat file: %s not found",
517 dev_stat_path);
518
519 SAFE_FILE_SCANF(NULL, dev_stat_path,
520 "%*s %*s %*s %*s %*s %*s %lu %*s %*s %lu",
521 &dev_sec_write, &io_ticks);
522
523 if (!io_ticks)
524 tst_brkm(TCONF, NULL, "Test device stat file: %s broken",
525 dev_stat_path);
526
527 dev_bytes_written = (dev_sec_write - prev_dev_sec_write) * 512;
528
529 prev_dev_sec_write = dev_sec_write;
530
531 return dev_bytes_written;
532 }
533
534 __attribute__((nonnull))
tst_find_backing_dev(const char * path,char * dev,size_t dev_size)535 void tst_find_backing_dev(const char *path, char *dev, size_t dev_size)
536 {
537 struct stat buf;
538 struct btrfs_ioctl_fs_info_args args = {0};
539 struct dirent *d;
540 char uevent_path[PATH_MAX+PATH_MAX+10]; //10 is for the static uevent path
541 char dev_name[NAME_MAX];
542 char bdev_path[PATH_MAX];
543 char tmp_path[PATH_MAX];
544 char btrfs_uuid_str[UUID_STR_SZ];
545 DIR *dir;
546 unsigned int dev_major, dev_minor;
547 int fd;
548
549 if (stat(path, &buf) < 0)
550 tst_brkm(TWARN | TERRNO, NULL, "stat() failed");
551
552 strncpy(tmp_path, path, PATH_MAX-1);
553 tmp_path[PATH_MAX-1] = '\0';
554 if (S_ISREG(buf.st_mode))
555 dirname(tmp_path);
556
557 dev_major = major(buf.st_dev);
558 dev_minor = minor(buf.st_dev);
559 *dev = '\0';
560
561 if (dev_major == 0) {
562 tst_resm(TINFO, "Use BTRFS specific strategy");
563
564 fd = SAFE_OPEN(NULL, tmp_path, O_DIRECTORY);
565 if (!ioctl(fd, BTRFS_IOC_FS_INFO, &args)) {
566 sprintf(btrfs_uuid_str,
567 UUID_FMT,
568 args.fsid[0], args.fsid[1],
569 args.fsid[2], args.fsid[3],
570 args.fsid[4], args.fsid[5],
571 args.fsid[6], args.fsid[7],
572 args.fsid[8], args.fsid[9],
573 args.fsid[10], args.fsid[11],
574 args.fsid[12], args.fsid[13],
575 args.fsid[14], args.fsid[15]);
576 sprintf(bdev_path,
577 "/sys/fs/btrfs/%s/devices", btrfs_uuid_str);
578 } else {
579 if (errno == ENOTTY)
580 tst_brkm(TBROK | TERRNO, NULL, "BTRFS ioctl failed. Is %s on a tmpfs?", path);
581
582 tst_brkm(TBROK | TERRNO, NULL, "BTRFS ioctl on %s failed.", tmp_path);
583 }
584 SAFE_CLOSE(NULL, fd);
585
586 dir = SAFE_OPENDIR(NULL, bdev_path);
587 while ((d = SAFE_READDIR(NULL, dir))) {
588 if (d->d_name[0] != '.')
589 break;
590 }
591
592 uevent_path[0] = '\0';
593
594 if (d) {
595 sprintf(uevent_path, "%s/%s/uevent",
596 bdev_path, d->d_name);
597 } else {
598 tst_brkm(TBROK | TERRNO, NULL, "No backing device found while looking in %s.", bdev_path);
599 }
600
601 if (SAFE_READDIR(NULL, dir))
602 tst_resm(TINFO, "Warning: used first of multiple backing device.");
603
604 SAFE_CLOSEDIR(NULL, dir);
605 } else {
606 tst_resm(TINFO, "Use uevent strategy");
607 sprintf(uevent_path,
608 "/sys/dev/block/%d:%d/uevent", dev_major, dev_minor);
609 }
610
611 if (!access(uevent_path, R_OK)) {
612 FILE_LINES_SCANF(NULL, uevent_path, "DEVNAME=%s", dev_name);
613
614 if (!dev_name[0] || set_dev_path(dev_name, dev, dev_size))
615 tst_brkm(TBROK, NULL, "Could not stat backing device %s", dev);
616
617 } else {
618 tst_brkm(TBROK, NULL, "uevent file (%s) access failed", uevent_path);
619 }
620 }
621
tst_stat_mount_dev(const char * const mnt_path,struct stat * const st)622 void tst_stat_mount_dev(const char *const mnt_path, struct stat *const st)
623 {
624 struct mntent *mnt;
625 FILE *mntf = setmntent("/proc/self/mounts", "r");
626
627 if (!mntf) {
628 tst_brkm(TBROK | TERRNO, NULL, "Can't open /proc/self/mounts");
629 return;
630 }
631
632 mnt = getmntent(mntf);
633 if (!mnt) {
634 tst_brkm(TBROK | TERRNO, NULL, "Can't read mounts or no mounts?");
635 return;
636 }
637
638 do {
639 if (strcmp(mnt->mnt_dir, mnt_path)) {
640 mnt = getmntent(mntf);
641 continue;
642 }
643
644 if (stat(mnt->mnt_fsname, st)) {
645 tst_brkm(TBROK | TERRNO, NULL,
646 "Can't stat '%s', mounted at '%s'",
647 mnt->mnt_fsname, mnt_path);
648 }
649
650 return;
651 } while (mnt);
652
653 tst_brkm(TBROK, NULL, "Could not find mount device");
654 }
655
tst_dev_block_size(const char * path)656 int tst_dev_block_size(const char *path)
657 {
658 int fd;
659 int size;
660 char dev_name[PATH_MAX];
661
662 tst_find_backing_dev(path, dev_name, sizeof(dev_name));
663
664 fd = SAFE_OPEN(NULL, dev_name, O_RDONLY);
665 SAFE_IOCTL(NULL, fd, BLKSSZGET, &size);
666 SAFE_CLOSE(NULL, fd);
667
668 return size;
669 }
670