Blame lib/utils_loop.c

Packit 94f725
/*
Packit 94f725
 * loopback block device utilities
Packit 94f725
 *
Packit 94f725
 * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
Packit 94f725
 * Copyright (C) 2009-2020 Milan Broz
Packit 94f725
 *
Packit 94f725
 * This program is free software; you can redistribute it and/or
Packit 94f725
 * modify it under the terms of the GNU General Public License
Packit 94f725
 * as published by the Free Software Foundation; either version 2
Packit 94f725
 * of the License, or (at your option) any later version.
Packit 94f725
 *
Packit 94f725
 * This program is distributed in the hope that it will be useful,
Packit 94f725
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 94f725
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 94f725
 * GNU General Public License for more details.
Packit 94f725
 *
Packit 94f725
 * You should have received a copy of the GNU General Public License
Packit 94f725
 * along with this program; if not, write to the Free Software
Packit 94f725
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 94f725
 */
Packit 94f725
Packit 94f725
#include <stdlib.h>
Packit 94f725
#include <string.h>
Packit 94f725
#include <stdio.h>
Packit 94f725
#include <unistd.h>
Packit 94f725
#include <fcntl.h>
Packit 94f725
#include <errno.h>
Packit 94f725
#include <limits.h>
Packit 94f725
#include <sys/ioctl.h>
Packit 94f725
#include <sys/stat.h>
Packit 94f725
#include <sys/types.h>
Packit 94f725
#ifdef HAVE_SYS_SYSMACROS_H
Packit 94f725
# include <sys/sysmacros.h>     /* for major, minor */
Packit 94f725
#endif
Packit 94f725
#include <linux/loop.h>
Packit 94f725
Packit 94f725
#include "utils_loop.h"
Packit 94f725
Packit 94f725
#define LOOP_DEV_MAJOR 7
Packit 94f725
Packit 94f725
#ifndef LO_FLAGS_AUTOCLEAR
Packit 94f725
#define LO_FLAGS_AUTOCLEAR 4
Packit 94f725
#endif
Packit 94f725
Packit 94f725
#ifndef LOOP_CTL_GET_FREE
Packit 94f725
#define LOOP_CTL_GET_FREE 0x4C82
Packit 94f725
#endif
Packit 94f725
Packit 94f725
#ifndef LOOP_SET_CAPACITY
Packit 94f725
#define LOOP_SET_CAPACITY 0x4C07
Packit 94f725
#endif
Packit 94f725
Packit 94f725
static char *crypt_loop_get_device_old(void)
Packit 94f725
{
Packit 94f725
	char dev[20];
Packit 94f725
	int i, loop_fd;
Packit 94f725
	struct loop_info64 lo64 = {0};
Packit 94f725
Packit 94f725
	for (i = 0; i < 256; i++) {
Packit 94f725
		sprintf(dev, "/dev/loop%d", i);
Packit 94f725
Packit 94f725
		loop_fd = open(dev, O_RDONLY);
Packit 94f725
		if (loop_fd < 0)
Packit 94f725
			return NULL;
Packit 94f725
Packit 94f725
		if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
Packit 94f725
		    errno == ENXIO) {
Packit 94f725
			close(loop_fd);
Packit 94f725
			return strdup(dev);
Packit 94f725
		}
Packit 94f725
		close(loop_fd);
Packit 94f725
	}
Packit 94f725
Packit 94f725
	return NULL;
Packit 94f725
}
Packit 94f725
Packit 94f725
static char *crypt_loop_get_device(void)
Packit 94f725
{
Packit 94f725
	char dev[64];
Packit 94f725
	int i, loop_fd;
Packit 94f725
	struct stat st;
Packit 94f725
Packit 94f725
	loop_fd = open("/dev/loop-control", O_RDONLY);
Packit 94f725
	if (loop_fd < 0)
Packit 94f725
		return crypt_loop_get_device_old();
Packit 94f725
Packit 94f725
	i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
Packit 94f725
	if (i < 0) {
Packit 94f725
		close(loop_fd);
Packit 94f725
		return NULL;
Packit 94f725
	}
Packit 94f725
	close(loop_fd);
Packit 94f725
Packit 94f725
	if (sprintf(dev, "/dev/loop%d", i) < 0)
Packit 94f725
		return NULL;
Packit 94f725
Packit 94f725
	if (stat(dev, &st) || !S_ISBLK(st.st_mode))
Packit 94f725
		return NULL;
Packit 94f725
Packit 94f725
	return strdup(dev);
Packit 94f725
}
Packit 94f725
Packit 94f725
int crypt_loop_attach(char **loop, const char *file, int offset,
Packit 94f725
		      int autoclear, int *readonly)
Packit 94f725
{
Packit 94f725
	struct loop_info64 lo64 = {0};
Packit 94f725
	char *lo_file_name;
Packit 94f725
	int loop_fd = -1, file_fd = -1, r = 1;
Packit 94f725
Packit 94f725
	*loop = NULL;
Packit 94f725
Packit 94f725
	file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
Packit 94f725
	if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
Packit 94f725
		*readonly = 1;
Packit 94f725
		file_fd = open(file, O_RDONLY | O_EXCL);
Packit 94f725
	}
Packit 94f725
	if (file_fd < 0)
Packit 94f725
		goto out;
Packit 94f725
Packit 94f725
	while (loop_fd < 0)  {
Packit 94f725
		*loop = crypt_loop_get_device();
Packit 94f725
		if (!*loop)
Packit 94f725
			goto out;
Packit 94f725
Packit 94f725
		loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
Packit 94f725
		if (loop_fd < 0)
Packit 94f725
			goto out;
Packit 94f725
Packit 94f725
		if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
Packit 94f725
			if (errno != EBUSY)
Packit 94f725
				goto out;
Packit 94f725
			free(*loop);
Packit 94f725
			*loop = NULL;
Packit 94f725
Packit 94f725
			close(loop_fd);
Packit 94f725
			loop_fd = -1;
Packit 94f725
		}
Packit 94f725
	}
Packit 94f725
Packit 94f725
	lo_file_name = (char*)lo64.lo_file_name;
Packit 94f725
	lo_file_name[LO_NAME_SIZE-1] = '\0';
Packit 94f725
	strncpy(lo_file_name, file, LO_NAME_SIZE-1);
Packit 94f725
	lo64.lo_offset = offset;
Packit 94f725
	if (autoclear)
Packit 94f725
		lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
Packit 94f725
Packit 94f725
	if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
Packit 94f725
		(void)ioctl(loop_fd, LOOP_CLR_FD, 0);
Packit 94f725
		goto out;
Packit 94f725
	}
Packit 94f725
Packit 94f725
	/* Verify that autoclear is really set */
Packit 94f725
	if (autoclear) {
Packit 94f725
		memset(&lo64, 0, sizeof(lo64));
Packit 94f725
		if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
Packit 94f725
		   !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
Packit 94f725
		(void)ioctl(loop_fd, LOOP_CLR_FD, 0);
Packit 94f725
			goto out;
Packit 94f725
		}
Packit 94f725
	}
Packit 94f725
Packit 94f725
	r = 0;
Packit 94f725
out:
Packit 94f725
	if (r && loop_fd >= 0)
Packit 94f725
		close(loop_fd);
Packit 94f725
	if (file_fd >= 0)
Packit 94f725
		close(file_fd);
Packit 94f725
	if (r && *loop) {
Packit 94f725
		free(*loop);
Packit 94f725
		*loop = NULL;
Packit 94f725
	}
Packit 94f725
	return r ? -1 : loop_fd;
Packit 94f725
}
Packit 94f725
Packit 94f725
int crypt_loop_detach(const char *loop)
Packit 94f725
{
Packit 94f725
	int loop_fd = -1, r = 1;
Packit 94f725
Packit 94f725
	loop_fd = open(loop, O_RDONLY);
Packit 94f725
	if (loop_fd < 0)
Packit 94f725
                return 1;
Packit 94f725
Packit 94f725
	if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
Packit 94f725
		r = 0;
Packit 94f725
Packit 94f725
	close(loop_fd);
Packit 94f725
	return r;
Packit 94f725
}
Packit 94f725
Packit 94f725
int crypt_loop_resize(const char *loop)
Packit 94f725
{
Packit 94f725
	int loop_fd = -1, r = 1;
Packit 94f725
Packit 94f725
	loop_fd = open(loop, O_RDONLY);
Packit 94f725
	if (loop_fd < 0)
Packit 94f725
                return 1;
Packit 94f725
Packit 94f725
	if (!ioctl(loop_fd, LOOP_SET_CAPACITY, 0))
Packit 94f725
		r = 0;
Packit 94f725
Packit 94f725
	close(loop_fd);
Packit 94f725
	return r;
Packit 94f725
}
Packit 94f725
Packit 94f725
static char *_ioctl_backing_file(const char *loop)
Packit 94f725
{
Packit 94f725
	struct loop_info64 lo64 = {0};
Packit 94f725
	int loop_fd;
Packit 94f725
Packit 94f725
	loop_fd = open(loop, O_RDONLY);
Packit 94f725
	if (loop_fd < 0)
Packit 94f725
		return NULL;
Packit 94f725
Packit 94f725
	if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
Packit 94f725
		close(loop_fd);
Packit 94f725
		return NULL;
Packit 94f725
	}
Packit 94f725
Packit 94f725
	lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
Packit 94f725
	lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
Packit 94f725
Packit 94f725
	close(loop_fd);
Packit 94f725
Packit 94f725
	return strdup((char*)lo64.lo_file_name);
Packit 94f725
}
Packit 94f725
Packit 94f725
static char *_sysfs_backing_file(const char *loop)
Packit 94f725
{
Packit 94f725
	struct stat st;
Packit 94f725
	char buf[PATH_MAX];
Packit 94f725
	size_t len;
Packit 94f725
	int fd;
Packit 94f725
Packit 94f725
	if (stat(loop, &st) || !S_ISBLK(st.st_mode))
Packit 94f725
		return NULL;
Packit 94f725
Packit 94f725
	snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
Packit 94f725
		 major(st.st_rdev), minor(st.st_rdev));
Packit 94f725
Packit 94f725
	fd = open(buf, O_RDONLY);
Packit 94f725
	if (fd < 0)
Packit 94f725
		return NULL;
Packit 94f725
Packit 94f725
	len = read(fd, buf, PATH_MAX);
Packit 94f725
	close(fd);
Packit 94f725
	if (len < 2)
Packit 94f725
		return NULL;
Packit 94f725
Packit 94f725
	buf[len - 1] = '\0';
Packit 94f725
	return strdup(buf);
Packit 94f725
}
Packit 94f725
Packit 94f725
char *crypt_loop_backing_file(const char *loop)
Packit 94f725
{
Packit 94f725
	char *bf;
Packit 94f725
Packit 94f725
	if (!crypt_loop_device(loop))
Packit 94f725
		return NULL;
Packit 94f725
Packit 94f725
	bf = _sysfs_backing_file(loop);
Packit 94f725
	return bf ?: _ioctl_backing_file(loop);
Packit 94f725
}
Packit 94f725
Packit 94f725
int crypt_loop_device(const char *loop)
Packit 94f725
{
Packit 94f725
	struct stat st;
Packit 94f725
Packit 94f725
	if (!loop)
Packit 94f725
		return 0;
Packit 94f725
Packit 94f725
	if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
Packit 94f725
	    major(st.st_rdev) != LOOP_DEV_MAJOR)
Packit 94f725
		return 0;
Packit 94f725
Packit 94f725
	return 1;
Packit 94f725
}