1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-22 17:35:59 +03:00
lvm2/lib/locking/file_locking.c

165 lines
4.4 KiB
C
Raw Normal View History

/*
2008-01-30 17:00:02 +03:00
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2014 Red Hat, Inc. All rights reserved.
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2.
*
2004-03-30 23:35:44 +04:00
* 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.
2004-03-30 23:35:44 +04:00
*
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2002-11-18 17:01:16 +03:00
#include "lib.h"
#include "locking.h"
#include "locking_types.h"
#include "activate.h"
#include "config.h"
#include "defaults.h"
#include "lvm-string.h"
#include "lvm-flock.h"
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
static char _lock_dir[PATH_MAX];
static void _fin_file_locking(void)
{
release_flocks(1);
2003-05-06 16:03:13 +04:00
}
static void _reset_file_locking(void)
{
release_flocks(0);
}
static int _file_lock_resource(struct cmd_context *cmd, const char *resource,
uint32_t flags, const struct logical_volume *lv)
{
char lockfile[PATH_MAX];
unsigned origin_only = (flags & LCK_ORIGIN_ONLY) ? 1 : 0;
unsigned revert = (flags & LCK_REVERT) ? 1 : 0;
switch (flags & LCK_SCOPE_MASK) {
case LCK_ACTIVATION:
if (dm_snprintf(lockfile, sizeof(lockfile),
"%s/A_%s", _lock_dir, resource) < 0) {
log_error("Too long locking filename %s/A_%s.", _lock_dir, resource);
return 0;
}
if (!lock_file(lockfile, flags))
return_0;
break;
case LCK_VG:
if (!strcmp(resource, VG_SYNC_NAMES))
fs_unlock();
2008-05-09 23:26:58 +04:00
/* LCK_CACHE does not require a real lock */
if (flags & LCK_CACHE)
break;
2008-05-09 23:26:58 +04:00
2011-01-05 18:10:30 +03:00
if (is_orphan_vg(resource) || is_global_vg(resource)) {
if (dm_snprintf(lockfile, sizeof(lockfile),
"%s/P_%s", _lock_dir, resource + 1) < 0) {
log_error("Too long locking filename %s/P_%s.",
_lock_dir, resource + 1);
return 0;
}
} else
if (dm_snprintf(lockfile, sizeof(lockfile),
"%s/V_%s", _lock_dir, resource) < 0) {
log_error("Too long locking filename %s/V_%s.",
_lock_dir, resource);
return 0;
}
if (!lock_file(lockfile, flags))
return_0;
break;
case LCK_LV:
switch (flags & LCK_TYPE_MASK) {
2002-04-04 15:18:45 +04:00
case LCK_UNLOCK:
log_very_verbose("Unlocking LV %s%s%s", resource, origin_only ? " without snapshots" : "", revert ? " (reverting)" : "");
if (!lv_resume_if_active(cmd, resource, origin_only, 0, revert, lv_committed(lv)))
return 0;
break;
2004-05-05 16:03:07 +04:00
case LCK_NULL:
log_very_verbose("Locking LV %s (NL)", resource);
if (!lv_deactivate(cmd, resource, lv_committed(lv)))
2004-05-05 16:03:07 +04:00
return 0;
break;
case LCK_READ:
log_very_verbose("Locking LV %s (R)", resource);
if (!lv_activate_with_filter(cmd, resource, 0, (lv->status & LV_NOSCAN) ? 1 : 0,
(lv->status & LV_TEMPORARY) ? 1 : 0, lv_committed(lv)))
return 0;
break;
case LCK_PREAD:
log_very_verbose("Locking LV %s (PR) - ignored", resource);
break;
case LCK_WRITE:
log_very_verbose("Locking LV %s (W)%s", resource, origin_only ? " without snapshots" : "");
if (!lv_suspend_if_active(cmd, resource, origin_only, 0, lv_committed(lv), lv))
return 0;
break;
case LCK_EXCL:
log_very_verbose("Locking LV %s (EX)", resource);
if (!lv_activate_with_filter(cmd, resource, 1, (lv->status & LV_NOSCAN) ? 1 : 0,
(lv->status & LV_TEMPORARY) ? 1 : 0, lv_committed(lv)))
return 0;
break;
default:
break;
}
break;
default:
log_error("Unrecognised lock scope: %d",
flags & LCK_SCOPE_MASK);
return 0;
}
return 1;
}
int init_file_locking(struct locking_type *locking, struct cmd_context *cmd,
int suppress_messages)
{
int r;
const char *locking_dir;
init_flock(cmd);
locking->lock_resource = _file_lock_resource;
2003-05-06 16:03:13 +04:00
locking->reset_locking = _reset_file_locking;
locking->fin_locking = _fin_file_locking;
2004-03-26 23:17:11 +03:00
locking->flags = 0;
/* Get lockfile directory from config file */
locking_dir = find_config_tree_str(cmd, global_locking_dir_CFG, NULL);
if (!dm_strncpy(_lock_dir, locking_dir, sizeof(_lock_dir))) {
log_error("Path for locking_dir %s is invalid.", locking_dir);
return 0;
}
(void) dm_prepare_selinux_context(_lock_dir, S_IFDIR);
r = dm_create_dir(_lock_dir);
(void) dm_prepare_selinux_context(NULL, 0);
if (!r)
return 0;
2002-07-11 00:43:32 +04:00
/* Trap a read-only file system */
if ((access(_lock_dir, R_OK | W_OK | X_OK) == -1) && (errno == EROFS))
2002-07-11 00:43:32 +04:00
return 0;
return 1;
}