From 0dbd99fb3e117cd5f87eda492b6b4fab1b5bea23 Mon Sep 17 00:00:00 2001 From: Goffredo Baroncelli Date: Wed, 15 Jun 2011 21:55:25 +0200 Subject: Scan the devices listed in /proc/partitions During the commands: - btrfs filesystem show - btrfs device scan the devices "scanned" are extracted from /proc/partitions. This should avoid to scan devices not suitable for a btrfs filesystem like cdrom and floppy or to scan not existant devices. The old behavior (scan all the block devices under /dev) may be forced passing the "--all-devices" switch. --- utils.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 8c80cfba..86c643c1 100644 --- a/utils.c +++ b/utils.c @@ -1114,3 +1114,61 @@ int check_label(char *input) return 0; } + +int btrfs_scan_block_devices(int run_ioctl) +{ + + struct stat st; + int ret; + int fd; + struct btrfs_fs_devices *tmp_devices; + u64 num_devices; + FILE *proc_partitions; + int i; + char buf[1024]; + char fullpath[110]; + + proc_partitions = fopen("/proc/partitions","r"); + if (!proc_partitions) { + fprintf(stderr, "Unable to open '/proc/partitions' for scanning\n"); + return -ENOENT; + } + /* skip the header */ + for(i=0; i < 2 ; i++) + if(!fgets(buf, 1023, proc_partitions)){ + fprintf(stderr, "Unable to read '/proc/partitions' for scanning\n"); + fclose(proc_partitions); + return -ENOENT; + } + + strcpy(fullpath,"/dev/"); + while(fgets(buf, 1023, proc_partitions)) { + + i = sscanf(buf," %*d %*d %*d %99s", fullpath+5); + ret = lstat(fullpath, &st); + if (ret < 0) { + fprintf(stderr, "failed to stat %s\n", fullpath); + continue; + } + if (!S_ISBLK(st.st_mode)) { + continue; + } + + fd = open(fullpath, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "failed to read %s\n", fullpath); + continue; + } + ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices, + &num_devices, + BTRFS_SUPER_INFO_OFFSET); + if (ret == 0 && run_ioctl > 0) { + btrfs_register_one_device(fullpath); + } + close(fd); + } + + fclose(proc_partitions); + return 0; +} + -- cgit v1.2.3