1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-09 18:03:17 +03:00
lvm2/libdm/libdm-common.c

1364 lines
28 KiB
C
Raw Normal View History

/*
2004-03-30 23:08:57 +04:00
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
*
2004-03-30 23:08:57 +04:00
* This file is part of the device-mapper userspace tools.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "dmlib.h"
#include "libdm-targets.h"
#include "libdm-common.h"
#include "kdev_t.h"
2008-11-01 05:19:19 +03:00
#include "dm-ioctl.h"
#include <stdarg.h>
#include <sys/param.h>
2007-12-03 20:56:36 +03:00
#include <sys/ioctl.h>
2007-11-30 19:42:26 +03:00
#include <fcntl.h>
#include <dirent.h>
#ifdef UDEV_SYNC_SUPPORT
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/sem.h>
# define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
# include <libudev.h>
#endif
2007-11-30 19:42:26 +03:00
#ifdef linux
# include <linux/fs.h>
#endif
2004-04-06 22:54:00 +04:00
#ifdef HAVE_SELINUX
# include <selinux/selinux.h>
#endif
#ifdef HAVE_SELINUX_LABEL_H
# include <selinux/label.h>
#endif
2004-04-06 22:54:00 +04:00
#define DEV_DIR "/dev/"
#ifdef UDEV_SYNC_SUPPORT
#ifdef _SEM_SEMUN_UNDEFINED
union semun
{
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
unsigned short int *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
#endif
#endif
static char _dm_dir[PATH_MAX] = DEV_DIR DM_DIR;
2003-01-22 00:25:11 +03:00
static int _verbose = 0;
#ifdef HAVE_SELINUX_LABEL_H
static struct selabel_handle *_selabel_handle = NULL;
#endif
#ifdef UDEV_SYNC_SUPPORT
static int _semaphore_supported = -1;
static int _udev_running = -1;
static int _sync_with_udev = 1;
static int _udev_checking = 1;
#endif
/*
* Library users can provide their own logging
* function.
*/
static void _default_log_line(int level,
const char *file __attribute__((unused)),
int line __attribute__((unused)), int dm_errno,
const char *f, va_list ap)
{
int use_stderr = level & _LOG_STDERR;
level &= ~_LOG_STDERR;
2003-01-22 00:25:11 +03:00
if (level > _LOG_WARN && !_verbose)
return;
2003-01-22 00:25:11 +03:00
if (level < _LOG_WARN)
vfprintf(stderr, f, ap);
2003-01-22 00:25:11 +03:00
else
vfprintf(use_stderr ? stderr : stdout, f, ap);
2003-01-22 00:25:11 +03:00
if (level < _LOG_WARN)
fprintf(stderr, "\n");
else
fprintf(use_stderr ? stderr : stdout, "\n");
}
static void _default_log_with_errno(int level,
const char *file __attribute__((unused)),
int line __attribute__((unused)), int dm_errno,
const char *f, ...)
{
va_list ap;
va_start(ap, f);
_default_log_line(level, file, line, dm_errno, f, ap);
va_end(ap);
}
static void _default_log(int level, const char *file,
int line, const char *f, ...)
{
va_list ap;
va_start(ap, f);
_default_log_line(level, file, line, 0, f, ap);
va_end(ap);
}
dm_log_fn dm_log = _default_log;
dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno;
void dm_log_init(dm_log_fn fn)
{
if (fn)
dm_log = fn;
else
dm_log = _default_log;
dm_log_with_errno = _default_log_with_errno;
}
int dm_log_is_non_default(void)
{
return (dm_log == _default_log) ? 0 : 1;
}
void dm_log_with_errno_init(dm_log_with_errno_fn fn)
{
if (fn)
dm_log_with_errno = fn;
else
dm_log_with_errno = _default_log_with_errno;
dm_log = _default_log;
}
2003-01-22 00:25:11 +03:00
void dm_log_init_verbose(int level)
{
_verbose = level;
}
static void _build_dev_path(char *buffer, size_t len, const char *dev_name)
{
/* If there's a /, assume caller knows what they're doing */
if (strchr(dev_name, '/'))
snprintf(buffer, len, "%s", dev_name);
else
snprintf(buffer, len, "%s/%s", _dm_dir, dev_name);
}
int dm_get_library_version(char *version, size_t size)
{
strncpy(version, DM_LIB_VERSION, size);
return 1;
}
struct dm_task *dm_task_create(int type)
{
struct dm_task *dmt = dm_zalloc(sizeof(*dmt));
if (!dmt) {
log_error("dm_task_create: malloc(%" PRIsize_t ") failed",
sizeof(*dmt));
return NULL;
}
2006-05-10 20:23:41 +04:00
if (!dm_check_version()) {
dm_free(dmt);
return NULL;
2006-05-10 20:23:41 +04:00
}
dmt->type = type;
dmt->minor = -1;
2003-04-02 23:03:00 +04:00
dmt->major = -1;
dmt->allow_default_major_fallback = 1;
2008-06-07 00:44:35 +04:00
dmt->uid = DM_DEVICE_UID;
dmt->gid = DM_DEVICE_GID;
dmt->mode = DM_DEVICE_MODE;
2005-09-20 20:39:12 +04:00
dmt->no_open_count = 0;
dmt->read_ahead = DM_READ_AHEAD_AUTO;
dmt->read_ahead_flags = 0;
dmt->event_nr = 0;
2009-08-03 22:01:45 +04:00
dmt->cookie_set = 0;
dmt->query_inactive_table = 0;
dmt->new_uuid = 0;
2003-01-22 00:25:11 +03:00
return dmt;
}
/*
* Find the name associated with a given device number by scanning _dm_dir.
*/
2009-09-25 22:13:17 +04:00
static char *_find_dm_name_of_device(dev_t st_rdev)
{
const char *name;
char path[PATH_MAX];
struct dirent *dirent;
DIR *d;
struct stat buf;
char *new_name = NULL;
if (!(d = opendir(_dm_dir))) {
log_sys_error("opendir", _dm_dir);
return NULL;
}
while ((dirent = readdir(d))) {
name = dirent->d_name;
if (!strcmp(name, ".") || !strcmp(name, ".."))
continue;
if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
name) == -1) {
log_error("Couldn't create path for %s", name);
continue;
}
if (stat(path, &buf))
continue;
if (buf.st_rdev == st_rdev) {
2009-09-25 22:19:09 +04:00
if (!(new_name = dm_strdup(name)))
log_error("dm_task_set_name: strdup(%s) failed",
name);
break;
}
}
if (closedir(d))
log_sys_error("closedir", _dm_dir);
return new_name;
}
int dm_task_set_name(struct dm_task *dmt, const char *name)
{
char *pos;
char *new_name = NULL;
char path[PATH_MAX];
struct stat st1, st2;
dm_free(dmt->dev_name);
dmt->dev_name = NULL;
/*
* Path supplied for existing device?
*/
if ((pos = strrchr(name, '/'))) {
if (dmt->type == DM_DEVICE_CREATE) {
log_error("Name \"%s\" invalid. It contains \"/\".", name);
return 0;
}
if (stat(name, &st1)) {
log_error("Device %s not found", name);
return 0;
}
/*
* If supplied path points to same device as last component
* under /dev/mapper, use that name directly. Otherwise call
2009-09-25 22:13:17 +04:00
* _find_dm_name_of_device() to scan _dm_dir for a match.
*/
2009-09-25 23:06:05 +04:00
if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
pos + 1) == -1) {
log_error("Couldn't create path for %s", pos + 1);
return 0;
}
if (!stat(path, &st2) && (st1.st_rdev == st2.st_rdev))
name = pos + 1;
2009-09-25 22:13:17 +04:00
else if ((new_name = _find_dm_name_of_device(st1.st_rdev)))
name = new_name;
else {
log_error("Device %s not found", name);
return 0;
}
}
if (strlen(name) >= DM_NAME_LEN) {
log_error("Name \"%s\" too long", name);
dm_free(new_name);
return 0;
}
if (new_name)
dmt->dev_name = new_name;
else if (!(dmt->dev_name = dm_strdup(name))) {
2002-01-18 22:37:26 +03:00
log_error("dm_task_set_name: strdup(%s) failed", name);
return 0;
}
return 1;
}
int dm_task_set_uuid(struct dm_task *dmt, const char *uuid)
{
dm_free(dmt->uuid);
2005-10-17 02:57:20 +04:00
if (!(dmt->uuid = dm_strdup(uuid))) {
log_error("dm_task_set_uuid: strdup(%s) failed", uuid);
return 0;
}
return 1;
}
2003-04-02 23:03:00 +04:00
int dm_task_set_major(struct dm_task *dmt, int major)
{
dmt->major = major;
dmt->allow_default_major_fallback = 0;
2003-04-02 23:03:00 +04:00
return 1;
}
int dm_task_set_minor(struct dm_task *dmt, int minor)
{
dmt->minor = minor;
return 1;
}
int dm_task_set_major_minor(struct dm_task *dmt, int major, int minor,
int allow_default_major_fallback)
{
dmt->major = major;
dmt->minor = minor;
dmt->allow_default_major_fallback = allow_default_major_fallback;
return 1;
}
int dm_task_set_uid(struct dm_task *dmt, uid_t uid)
{
dmt->uid = uid;
return 1;
}
int dm_task_set_gid(struct dm_task *dmt, gid_t gid)
{
dmt->gid = gid;
return 1;
}
int dm_task_set_mode(struct dm_task *dmt, mode_t mode)
{
dmt->mode = mode;
return 1;
}
int dm_task_add_target(struct dm_task *dmt, uint64_t start, uint64_t size,
const char *ttype, const char *params)
{
struct target *t = create_target(start, size, ttype, params);
if (!t)
return 0;
if (!dmt->head)
dmt->head = dmt->tail = t;
else {
dmt->tail->next = t;
dmt->tail = t;
}
return 1;
}
static int _selabel_lookup(const char *path, mode_t mode,
security_context_t *scontext)
{
#ifdef HAVE_SELINUX
#ifdef HAVE_SELINUX_LABEL_H
if (!_selabel_handle &&
!(_selabel_handle = selabel_open(SELABEL_CTX_FILE, NULL, 0))) {
log_error("selabel_open failed: %s", strerror(errno));
return 0;
}
if (selabel_lookup(_selabel_handle, scontext, path, mode)) {
log_error("selabel_lookup failed: %s", strerror(errno));
return 0;
}
#else
if (matchpathcon(path, mode, scontext)) {
log_error("matchpathcon failed: %s", strerror(errno));
return 0;
}
#endif
#endif
return 1;
}
int dm_prepare_selinux_context(const char *path, mode_t mode)
2004-04-06 22:54:00 +04:00
{
#ifdef HAVE_SELINUX
security_context_t scontext = NULL;
2004-04-06 22:54:00 +04:00
if (is_selinux_enabled() <= 0)
2004-07-03 22:14:12 +04:00
return 1;
2004-04-06 22:54:00 +04:00
if (path) {
if (!_selabel_lookup(path, mode, &scontext))
return_0;
log_debug("Preparing SELinux context for %s to %s.", path, scontext);
}
else
log_debug("Resetting SELinux context to default value.");
if (setfscreatecon(scontext) < 0) {
log_sys_error("setfscreatecon", path);
freecon(scontext);
2004-04-06 22:54:00 +04:00
return 0;
}
freecon(scontext);
#endif
return 1;
}
int dm_set_selinux_context(const char *path, mode_t mode)
{
#ifdef HAVE_SELINUX
security_context_t scontext;
if (is_selinux_enabled() <= 0)
return 1;
if (!_selabel_lookup(path, mode, &scontext))
return_0;
2005-06-13 17:11:48 +04:00
log_debug("Setting SELinux context for %s to %s.", path, scontext);
if ((lsetfilecon(path, scontext) < 0) && (errno != ENOTSUP)) {
log_sys_error("lsetfilecon", path);
2005-06-13 17:11:48 +04:00
freecon(scontext);
2004-04-06 22:54:00 +04:00
return 0;
}
2005-06-13 17:11:48 +04:00
freecon(scontext);
#endif
2004-04-06 22:54:00 +04:00
return 1;
}
void selinux_release(void)
{
#ifdef HAVE_SELINUX_LABEL_H
if (_selabel_handle)
selabel_close(_selabel_handle);
_selabel_handle = NULL;
#endif
}
static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
uid_t uid, gid_t gid, mode_t mode, int check_udev)
{
char path[PATH_MAX];
struct stat info;
2003-01-22 00:25:11 +03:00
dev_t dev = MKDEV(major, minor);
mode_t old_mask;
_build_dev_path(path, sizeof(path), dev_name);
if (stat(path, &info) >= 0) {
if (!S_ISBLK(info.st_mode)) {
log_error("A non-block device file at '%s' "
"is already present", path);
return 0;
}
/* If right inode already exists we don't touch uid etc. */
if (info.st_rdev == dev)
return 1;
if (unlink(path) < 0) {
log_error("Unable to unlink device node for '%s'",
dev_name);
return 0;
}
} else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
check_udev)
2009-08-03 22:33:08 +04:00
log_warn("%s not set up by udev: Falling back to direct "
"node creation.", path);
(void) dm_prepare_selinux_context(path, S_IFBLK);
old_mask = umask(0);
if (mknod(path, S_IFBLK | mode, dev) < 0) {
log_error("Unable to make device node for '%s'", dev_name);
umask(old_mask);
(void) dm_prepare_selinux_context(NULL, 0);
return 0;
}
umask(old_mask);
(void) dm_prepare_selinux_context(NULL, 0);
if (chown(path, uid, gid) < 0) {
log_sys_error("chown", path);
return 0;
}
log_debug("Created %s", path);
return 1;
}
static int _rm_dev_node(const char *dev_name, int check_udev)
{
char path[PATH_MAX];
struct stat info;
_build_dev_path(path, sizeof(path), dev_name);
if (stat(path, &info) < 0)
return 1;
else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
check_udev)
log_warn("Node %s was not removed by udev. "
"Falling back to direct node removal.", path);
if (unlink(path) < 0) {
log_error("Unable to unlink device node for '%s'", dev_name);
return 0;
}
log_debug("Removed %s", path);
return 1;
}
static int _rename_dev_node(const char *old_name, const char *new_name,
int check_udev)
{
char oldpath[PATH_MAX];
char newpath[PATH_MAX];
struct stat info;
_build_dev_path(oldpath, sizeof(oldpath), old_name);
_build_dev_path(newpath, sizeof(newpath), new_name);
if (stat(newpath, &info) == 0) {
if (!S_ISBLK(info.st_mode)) {
log_error("A non-block device file at '%s' "
"is already present", newpath);
return 0;
}
else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
check_udev) {
if (stat(oldpath, &info) < 0 &&
errno == ENOENT)
/* assume udev already deleted this */
return 1;
else {
log_warn("The node %s should have been renamed to %s "
"by udev but old node is still present. "
"Falling back to direct old node removal.",
oldpath, newpath);
return _rm_dev_node(old_name, 0);
}
}
if (unlink(newpath) < 0) {
2003-01-22 00:25:11 +03:00
if (errno == EPERM) {
/* devfs, entry has already been renamed */
return 1;
}
log_error("Unable to unlink device node for '%s'",
new_name);
return 0;
}
}
else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
check_udev)
log_warn("The node %s should have been renamed to %s "
"by udev but new node is not present. "
"Falling back to direct node rename.",
oldpath, newpath);
if (rename(oldpath, newpath) < 0) {
log_error("Unable to rename device node from '%s' to '%s'",
old_name, new_name);
return 0;
}
log_debug("Renamed %s to %s", oldpath, newpath);
return 1;
}
2007-11-30 19:42:26 +03:00
#ifdef linux
static int _open_dev_node(const char *dev_name)
{
int fd = -1;
char path[PATH_MAX];
_build_dev_path(path, sizeof(path), dev_name);
if ((fd = open(path, O_RDONLY, 0)) < 0)
log_sys_error("open", path);
return fd;
}
2007-11-30 17:59:57 +03:00
int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
{
2007-11-30 19:42:26 +03:00
int r = 1;
int fd;
long read_ahead_long;
2007-11-30 19:42:26 +03:00
if (!*dev_name) {
log_error("Empty device name passed to BLKRAGET");
return 0;
}
2007-11-30 19:42:26 +03:00
if ((fd = _open_dev_node(dev_name)) < 0)
2007-11-30 19:44:42 +03:00
return_0;
2007-11-30 19:42:26 +03:00
if (ioctl(fd, BLKRAGET, &read_ahead_long)) {
2007-11-30 19:42:26 +03:00
log_sys_error("BLKRAGET", dev_name);
*read_ahead = 0;
2007-11-30 19:42:26 +03:00
r = 0;
} else {
*read_ahead = (uint32_t) read_ahead_long;
2007-11-30 19:42:26 +03:00
log_debug("%s: read ahead is %" PRIu32, dev_name, *read_ahead);
}
2007-11-30 19:42:26 +03:00
2007-12-04 01:48:36 +03:00
if (close(fd))
2007-11-30 19:42:26 +03:00
stack;
return r;
2007-11-30 17:59:57 +03:00
}
static int _set_read_ahead(const char *dev_name, uint32_t read_ahead)
{
2007-11-30 19:42:26 +03:00
int r = 1;
int fd;
long read_ahead_long = (long) read_ahead;
2007-11-30 19:42:26 +03:00
if (!*dev_name) {
log_error("Empty device name passed to BLKRAGET");
return 0;
}
2007-11-30 19:42:26 +03:00
if ((fd = _open_dev_node(dev_name)) < 0)
return_0;
log_debug("%s: Setting read ahead to %" PRIu32, dev_name, read_ahead);
if (ioctl(fd, BLKRASET, read_ahead_long)) {
2007-11-30 19:42:26 +03:00
log_sys_error("BLKRASET", dev_name);
r = 0;
}
2007-12-04 01:48:36 +03:00
if (close(fd))
2007-11-30 19:42:26 +03:00
stack;
return r;
2007-11-30 17:59:57 +03:00
}
static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
uint32_t read_ahead_flags)
{
uint32_t current_read_ahead;
if (read_ahead == DM_READ_AHEAD_AUTO)
return 1;
if (read_ahead == DM_READ_AHEAD_NONE)
read_ahead = 0;
if (read_ahead_flags & DM_READ_AHEAD_MINIMUM_FLAG) {
if (!get_dev_node_read_ahead(dev_name, &current_read_ahead))
return_0;
if (current_read_ahead > read_ahead) {
2007-12-05 21:57:34 +03:00
log_debug("%s: retaining kernel read ahead of %" PRIu32
" (requested %" PRIu32 ")",
2007-11-30 17:59:57 +03:00
dev_name, current_read_ahead, read_ahead);
return 1;
}
}
return _set_read_ahead(dev_name, read_ahead);
}
2007-11-30 19:42:26 +03:00
#else
int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
{
*read_ahead = 0;
return 1;
}
static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
uint32_t read_ahead_flags)
{