Esempio n. 1
0
/*
 * Find the mount point for a mounted device.
 * On success, returns 0 with mountpoint in *mp.
 * On failure, returns -errno (not mounted yields -EINVAL)
 * Is noisy on failures, expects to be given a mounted device.
 */
int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
{
	int ret;
	int fd = -1;

	ret = is_block_device(dev);
	if (ret <= 0) {
		if (!ret) {
			fprintf(stderr, "%s is not a block device\n", dev);
			ret = -EINVAL;
		} else {
			fprintf(stderr, "Could not check %s: %s\n",
				dev, strerror(-ret));
		}
		goto out;
	}

	fd = open(dev, O_RDONLY);
	if (fd < 0) {
		ret = -errno;
		fprintf(stderr, "Could not open %s: %s\n", dev, strerror(errno));
		goto out;
	}

	ret = check_mounted_where(fd, dev, mp, mp_size, NULL);
	if (!ret) {
		ret = -EINVAL;
	} else { /* mounted, all good */
		ret = 0;
	}
out:
	if (fd != -1)
		close(fd);
	return ret;
}
Esempio n. 2
0
static int cmd_scrub_cancel(int argc, char **argv)
{
	char *path;
	int ret;
	int fdmnt;
	int err;
	char mp[BTRFS_PATH_NAME_MAX + 1];
	struct btrfs_fs_devices *fs_devices_mnt = NULL;

	if (check_argc_exact(argc, 2))
		usage(cmd_scrub_cancel_usage);

	path = argv[1];

	fdmnt = open_file_or_dir(path);
	if (fdmnt < 0) {
		fprintf(stderr, "ERROR: scrub cancel failed\n");
		return 12;
	}

again:
	ret = ioctl(fdmnt, BTRFS_IOC_SCRUB_CANCEL, NULL);
	err = errno;
	close(fdmnt);

	if (ret && err == EINVAL) {
		/* path is no mounted btrfs. try if it's a device */
		ret = check_mounted_where(fdmnt, path, mp, sizeof(mp),
					  &fs_devices_mnt);
		close(fdmnt);
		if (ret) {
			fdmnt = open_file_or_dir(mp);
			if (fdmnt >= 0) {
				path = mp;
				goto again;
			}
		}
	}

	if (ret) {
		fprintf(stderr, "ERROR: scrub cancel failed on %s: %s\n", path,
			err == ENOTCONN ? "not running" : strerror(errno));
		return 1;
	}

	printf("scrub cancelled\n");

	return 0;
}