1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-19 14:04:17 +03:00
lvm2/lib/label/label.c

2053 lines
56 KiB
C
Raw Normal View History

/*
2004-03-30 19:35:44 +00:00
* Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
2018-04-20 10:43:50 -05:00
* Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
*
2004-03-30 19:35:44 +00:00
* This file is part of LVM2.
*
* 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 19:35:44 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 19:35:44 +00:00
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "lib/misc/lib.h"
#include "base/memory/zalloc.h"
#include "lib/label/label.h"
#include "lib/misc/crc.h"
#include "lib/mm/xlate.h"
#include "lib/cache/lvmcache.h"
#include "lib/device/bcache.h"
#include "lib/commands/toolcontext.h"
#include "lib/activate/activate.h"
#include "lib/label/hints.h"
#include "lib/metadata/metadata.h"
2019-10-30 15:11:57 -05:00
#include "lib/format_text/layout.h"
device usage based on devices file The LVM devices file lists devices that lvm can use. The default file is /etc/lvm/devices/system.devices, and the lvmdevices(8) command is used to add or remove device entries. If the file does not exist, or if lvm.conf includes use_devicesfile=0, then lvm will not use a devices file. When the devices file is in use, the regex filter is not used, and the filter settings in lvm.conf or on the command line are ignored. LVM records devices in the devices file using hardware-specific IDs, such as the WWID, and attempts to use subsystem-specific IDs for virtual device types. These device IDs are also written in the VG metadata. When no hardware or virtual ID is available, lvm falls back using the unstable device name as the device ID. When devnames are used, lvm performs extra scanning to find devices if their devname changes, e.g. after reboot. When proper device IDs are used, an lvm command will not look at devices outside the devices file, but when devnames are used as a fallback, lvm will scan devices outside the devices file to locate PVs on renamed devices. A config setting search_for_devnames can be used to control the scanning for renamed devname entries. Related to the devices file, the new command option --devices <devnames> allows a list of devices to be specified for the command to use, overriding the devices file. The listed devices act as a sort of devices file in terms of limiting which devices lvm will see and use. Devices that are not listed will appear to be missing to the lvm command. Multiple devices files can be kept in /etc/lvm/devices, which allows lvm to be used with different sets of devices, e.g. system devices do not need to be exposed to a specific application, and the application can use lvm on its own set of devices that are not exposed to the system. The option --devicesfile <filename> is used to select the devices file to use with the command. Without the option set, the default system devices file is used. Setting --devicesfile "" causes lvm to not use a devices file. An existing, empty devices file means lvm will see no devices. The new command vgimportdevices adds PVs from a VG to the devices file and updates the VG metadata to include the device IDs. vgimportdevices -a will import all VGs into the system devices file. LVM commands run by dmeventd not use a devices file by default, and will look at all devices on the system. A devices file can be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If this file exists, lvm commands run by dmeventd will use it. Internal implementaion: - device_ids_read - read the devices file . add struct dev_use (du) to cmd->use_devices for each devices file entry - dev_cache_scan - get /dev entries . add struct device (dev) to dev_cache for each device on the system - device_ids_match - match devices file entries to /dev entries . match each du on cmd->use_devices to a dev in dev_cache, using device ID . on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID - label_scan - read lvm headers and metadata from devices . filters are applied, those that do not need data from the device . filter-deviceid skips devs without MATCHED_USE_ID, i.e. skips /dev entries that are not listed in the devices file . read lvm label from dev . filters are applied, those that use data from the device . read lvm metadata from dev . add info/vginfo structs for PVs/VGs (info is "lvmcache") - device_ids_find_renamed_devs - handle devices with unstable devname ID where devname changed . this step only needed when devs do not have proper device IDs, and their dev names change, e.g. after reboot sdb becomes sdc. . detect incorrect match because PVID in the devices file entry does not match the PVID found when the device was read above . undo incorrect match between du and dev above . search system devices for new location of PVID . update devices file with new devnames for PVIDs on renamed devices . label_scan the renamed devs - continue with command processing
2020-06-23 13:25:41 -05:00
#include "lib/device/device_id.h"
#include "lib/device/online.h"
2002-11-18 14:04:08 +00:00
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/resource.h>
2002-11-18 14:04:08 +00:00
/* FIXME Allow for larger labels? Restricted to single sector currently */
static uint64_t _current_bcache_size_bytes;
/*
* Internal labeller struct.
*/
struct labeller_i {
struct dm_list list;
struct labeller *l;
char name[0];
};
static struct dm_list _labellers;
static struct labeller_i *_alloc_li(const char *name, struct labeller *l)
{
struct labeller_i *li;
size_t len;
len = strlen(name);
if (!(li = malloc(sizeof(*li) + len + 1))) {
log_error("Couldn't allocate memory for labeller list object.");
return NULL;
}
li->l = l;
memcpy(li->name, name, len + 1);
return li;
2001-12-12 09:05:44 +00:00
}
int label_init(void)
{
dm_list_init(&_labellers);
return 1;
}
void label_exit(void)
{
struct labeller_i *li, *tli;
dm_list_iterate_items_safe(li, tli, &_labellers) {
dm_list_del(&li->list);
2002-11-18 14:04:08 +00:00
li->l->ops->destroy(li->l);
free(li);
}
2004-05-04 21:25:57 +00:00
dm_list_init(&_labellers);
}
int label_register_handler(struct labeller *handler)
{
struct labeller_i *li;
if (!(li = _alloc_li(handler->fmt->name, handler)))
2008-01-30 13:19:47 +00:00
return_0;
dm_list_add(&_labellers, &li->list);
return 1;
}
struct labeller *label_get_handler(const char *name)
{
struct labeller_i *li;
dm_list_iterate_items(li, &_labellers)
if (!strcmp(li->name, name))
return li->l;
return NULL;
}
2002-11-18 14:04:08 +00:00
/* FIXME Also wipe associated metadata area headers? */
2002-01-11 10:43:32 +00:00
int label_remove(struct device *dev)
{
2018-02-19 15:40:44 -06:00
char readbuf[LABEL_SIZE] __attribute__((aligned(8)));
2002-11-18 14:04:08 +00:00
int r = 1;
uint64_t sector;
int wipe;
struct labeller_i *li;
struct label_header *lh;
struct lvmcache_info *info;
2002-11-18 14:04:08 +00:00
log_very_verbose("Scanning for labels to wipe from %s", dev_name(dev));
if (!label_scan_open_excl(dev)) {
2018-02-19 15:40:44 -06:00
log_error("Failed to open device %s", dev_name(dev));
return 0;
2002-11-18 14:04:08 +00:00
}
/* Scan first few sectors for anything looking like a label */
for (sector = 0; sector < LABEL_SCAN_SECTORS;
sector += LABEL_SIZE >> SECTOR_SHIFT) {
2018-02-19 15:40:44 -06:00
memset(readbuf, 0, sizeof(readbuf));
if (!dev_read_bytes(dev, sector << SECTOR_SHIFT, LABEL_SIZE, readbuf)) {
log_error("Failed to read label from %s sector %llu",
dev_name(dev), (unsigned long long)sector);
2018-02-19 15:40:44 -06:00
continue;
}
lh = (struct label_header *)readbuf;
2002-11-18 14:04:08 +00:00
wipe = 0;
if (!memcmp(lh->id, LABEL_ID, sizeof(lh->id))) {
2002-11-18 14:04:08 +00:00
if (xlate64(lh->sector_xl) == sector)
wipe = 1;
} else {
dm_list_iterate_items(li, &_labellers) {
2018-02-19 15:40:44 -06:00
if (li->l->ops->can_handle(li->l, (char *)lh, sector)) {
2002-11-18 14:04:08 +00:00
wipe = 1;
break;
}
}
}
if (wipe) {
log_very_verbose("%s: Wiping label at sector %llu",
dev_name(dev), (unsigned long long)sector);
2018-02-19 15:40:44 -06:00
if (!dev_write_zeros(dev, sector << SECTOR_SHIFT, LABEL_SIZE)) {
log_error("Failed to remove label from %s at sector %llu",
dev_name(dev), (unsigned long long)sector);
2018-02-19 15:40:44 -06:00
r = 0;
} else {
/* Also remove the PV record from cache. */
info = lvmcache_info_from_pvid(dev->pvid, dev, 0);
if (info)
lvmcache_del(info);
2002-11-18 14:04:08 +00:00
}
}
}
return r;
}
2002-11-18 14:04:08 +00:00
/* Caller may need to use label_get_handler to create label struct! */
int label_write(struct device *dev, struct label *label)
{
char buf[LABEL_SIZE] __attribute__((aligned(8)));
2002-11-18 14:04:08 +00:00
struct label_header *lh = (struct label_header *) buf;
uint64_t offset;
2002-11-18 14:04:08 +00:00
int r = 1;
if (!label->labeller->ops->write) {
log_error("Label handler does not support label writes");
return 0;
}
2002-11-18 14:04:08 +00:00
if ((LABEL_SIZE + (label->sector << SECTOR_SHIFT)) > LABEL_SCAN_SIZE) {
log_error("Label sector %" PRIu64 " beyond range (%ld)",
2002-11-18 14:04:08 +00:00
label->sector, LABEL_SCAN_SECTORS);
return 0;
}
memset(buf, 0, LABEL_SIZE);
memcpy(lh->id, LABEL_ID, sizeof(lh->id));
2002-11-18 14:04:08 +00:00
lh->sector_xl = xlate64(label->sector);
lh->offset_xl = xlate32(sizeof(*lh));
2008-01-30 13:19:47 +00:00
if (!(label->labeller->ops->write)(label, buf))
return_0;
2002-11-18 14:04:08 +00:00
lh->crc_xl = xlate32(calc_crc(INITIAL_CRC, (uint8_t *)&lh->offset_xl, LABEL_SIZE -
((uint8_t *) &lh->offset_xl - (uint8_t *) lh)));
2002-11-18 14:04:08 +00:00
log_very_verbose("%s: Writing label to sector %" PRIu64 " with stored offset %"
PRIu32 ".", dev_name(dev), label->sector,
xlate32(lh->offset_xl));
2018-02-19 15:40:44 -06:00
if (!label_scan_open(dev)) {
log_error("Failed to open device %s", dev_name(dev));
return 0;
}
offset = label->sector << SECTOR_SHIFT;
dev_set_last_byte(dev, offset + LABEL_SIZE);
if (!dev_write_bytes(dev, offset, LABEL_SIZE, buf)) {
log_debug_devs("Failed to write label to %s", dev_name(dev));
return 0;
2002-11-18 14:04:08 +00:00
}
dev_unset_last_byte(dev);
2002-11-18 14:04:08 +00:00
return r;
}
2002-11-18 14:04:08 +00:00
void label_destroy(struct label *label)
{
2002-11-18 14:04:08 +00:00
label->labeller->ops->destroy_label(label->labeller, label);
free(label);
2002-11-18 14:04:08 +00:00
}
struct label *label_create(struct labeller *labeller)
{
struct label *label;
if (!(label = zalloc(sizeof(*label)))) {
2002-11-18 14:04:08 +00:00
log_error("label allocaction failed");
return NULL;
}
label->labeller = labeller;
labeller->ops->initialise_label(labeller, label);
return label;
}
/* global variable for accessing the bcache populated by label scan */
/* FIXME/TODO convert to cmd_context */
static struct bcache *scan_bcache;
2018-02-15 10:27:43 -06:00
#define BCACHE_BLOCK_SIZE_IN_SECTORS 256 /* 256*512 = 128K */
static bool _in_bcache(struct device *dev)
{
if (!dev)
return NULL;
return (dev->flags & DEV_IN_BCACHE) ? true : false;
}
static struct labeller *_find_lvm_header(struct device *dev,
char *headers_buf,
size_t headers_buf_size,
uint64_t *label_sector,
uint64_t block_sector,
uint64_t start_sector)
{
struct labeller_i *li;
struct labeller *labeller_ret = NULL;
struct label_header *lh;
uint64_t sector;
int found = 0;
for (sector = start_sector; sector < start_sector + LABEL_SCAN_SECTORS;
sector += LABEL_SIZE >> SECTOR_SHIFT) {
if ((sector * 512) >= headers_buf_size)
break;
lh = (struct label_header *) (headers_buf + (sector << SECTOR_SHIFT));
if (!memcmp(lh->id, LABEL_ID, sizeof(lh->id))) {
if (found) {
log_error("Ignoring additional label on %s at sector %llu",
dev_name(dev), (unsigned long long)(block_sector + sector));
}
if (xlate64(lh->sector_xl) != sector) {
log_warn("%s: Label for sector %llu found at sector %llu - ignoring.",
dev_name(dev),
(unsigned long long)xlate64(lh->sector_xl),
(unsigned long long)(block_sector + sector));
continue;
}
if (calc_crc(INITIAL_CRC, (uint8_t *)&lh->offset_xl,
LABEL_SIZE - ((uint8_t *) &lh->offset_xl - (uint8_t *) lh)) != xlate32(lh->crc_xl)) {
log_very_verbose("Label checksum incorrect on %s - ignoring", dev_name(dev));
continue;
}
if (found)
continue;
}
dm_list_iterate_items(li, &_labellers) {
if (li->l->ops->can_handle(li->l, (char *) lh, block_sector + sector)) {
log_debug("Found label at sector %llu on %s",
(unsigned long long)(block_sector + sector), dev_name(dev));
if (found) {
log_error("Ignoring additional label on %s at sector %llu",
dev_name(dev),
(unsigned long long)(block_sector + sector));
continue;
}
labeller_ret = li->l;
found = 1;
if (label_sector)
*label_sector = block_sector + sector;
break;
}
}
}
return labeller_ret;
}
/*
* Process/parse the headers from the data read from a device.
* Populates lvmcache with device / mda locations / vgname
* so that vg_read(vgname) will know which devices/locations
* to read metadata from.
*
* If during processing, headers/metadata are found to be needed
* beyond the range of the scanned block, then additional reads
* are performed in the processing functions to get that data.
*/
static int _process_block(struct cmd_context *cmd, struct dev_filter *f,
struct device *dev, char *headers_buf, size_t headers_buf_size,
uint64_t block_sector, uint64_t start_sector,
int *is_lvm_device)
{
char *label_buf;
struct labeller *labeller;
uint64_t label_sector = 0;
int is_duplicate = 0;
int ret = 0;
dev->flags &= ~DEV_SCAN_FOUND_LABEL;
/*
* The device may have signatures that exclude it from being processed,
* even if it might look like a PV. Now that the device has been read
* and data is available in bcache for it, recheck filters, including
* those that use data. The device needs to be excluded before it
* begins to be processed as a PV.
*/
if (f) {
if (!f->passes_filter(cmd, f, dev, NULL)) {
/*
* If this device was previously scanned (not common)
* and if it passed filters at that point, lvmcache
* info may have been saved for it. Now the same
* device is being scanned again, and it may fail
* filters this time. If the caller did not clear
* lvmcache info for this dev before rescanning, do
* that now. It's unlikely this is actually needed.
*/
if (dev->pvid[0]) {
log_print_unless_silent("Clear pvid and info for filtered dev %s.", dev_name(dev));
lvmcache_del_dev(dev);
memset(dev->pvid, 0, sizeof(dev->pvid));
}
*is_lvm_device = 0;
goto_out;
}
}
/*
* Finds the data sector containing the label.
*/
if (!(labeller = _find_lvm_header(dev, headers_buf, headers_buf_size, &label_sector, block_sector, start_sector))) {
/*
* Non-PVs exit here
*
* FIXME: check for PVs with errors that also exit here!
* i.e. this code cannot distinguish between a non-lvm
* device an an lvm device with errors.
*/
log_very_verbose("%s: No lvm label detected", dev_name(dev));
/* See comment above */
if (dev->pvid[0]) {
log_print_unless_silent("Clear pvid and info for no lvm header %s", dev_name(dev));
lvmcache_del_dev(dev);
memset(dev->pvid, 0, sizeof(dev->pvid));
}
dev->flags |= DEV_SCAN_FOUND_NOLABEL;
*is_lvm_device = 0;
goto out;
}
dev->flags |= DEV_SCAN_FOUND_LABEL;
*is_lvm_device = 1;
label_buf = headers_buf + (label_sector * 512);
/*
* This is the point where the scanning code dives into the rest of
* lvm. ops->read() is _text_read() which reads the pv_header, mda
* locations, and metadata text. All of the info it finds about the PV
* and VG is stashed in lvmcache which saves it in the form of
* info/vginfo structs. That lvmcache info is used later when the
* command wants to read the VG to do something to it.
*/
ret = labeller->ops->read(cmd, labeller, dev, label_buf, label_sector, &is_duplicate);
if (!ret) {
if (is_duplicate) {
/*
* _text_read() called lvmcache_add() which found an
* existing info struct for this PVID but for a
* different dev. lvmcache_add() did not add an info
* struct for this dev, but added this dev to the list
* of duplicate devs.
*/
log_debug("label scan found duplicate PVID %s on %s", dev->pvid, dev_name(dev));
} else {
/*
* Leave the info in lvmcache because the device is
* present and can still be used even if it has
* metadata that we can't process (we can get metadata
* from another PV/mda.) _text_read only saves mdas
* with good metadata in lvmcache (this includes old
* metadata), and if a PV has no mdas with good
* metadata, then the info for the PV will be in
* lvmcache with empty info->mdas, and it will behave
* like a PV with no mdas (a common configuration.)
*/
log_warn("WARNING: scan failed to get metadata summary from %s PVID %s", dev_name(dev), dev->pvid);
}
}
out:
return ret;
}
static int _scan_dev_open(struct device *dev)
{
struct dm_list *name_list;
struct dm_str_list *name_sl;
const char *name;
const char *modestr;
struct stat sbuf;
int flags = 0;
int fd, di;
if (!dev)
return 0;
if (dev->flags & DEV_IN_BCACHE) {
/* Shouldn't happen */
log_error("Device open %s has DEV_IN_BCACHE already set", dev_name(dev));
dev->flags &= ~DEV_IN_BCACHE;
}
if (dev->bcache_di != -1) {
/* Shouldn't happen */
log_error("Device open %s already open with di %d fd %d",
dev_name(dev), dev->bcache_di, dev->bcache_fd);
return 0;
}
next_name:
/*
* All the names for this device (major:minor) are kept on
* dev->aliases, the first one is the primary/preferred name.
*
* The default name preferences in dev-cache mean that the first
* name in dev->aliases is not a symlink for scsi devices, but is
* the /dev/mapper/ symlink for mpath devices.
*
* If preferred names are set to symlinks, should this
* first attempt to open using a non-symlink?
*
* dm_list_first() returns NULL if the list is empty.
*/
if (!(name_list = dm_list_first(&dev->aliases))) {
log_error("Device open %u:%u has no path names.",
MAJOR(dev->dev), MINOR(dev->dev));
return 0;
}
name_sl = dm_list_item(name_list, struct dm_str_list);
name = name_sl->str;
flags |= O_DIRECT;
flags |= O_NOATIME;
/*
* FIXME: udev is a train wreck when we open RDWR and close, so we
* need to only use RDWR when we actually need to write, and use
* RDONLY otherwise. Fix, disable or scrap udev nonsense so we can
* just open with RDWR by default.
*/
if (dev->flags & DEV_BCACHE_EXCL) {
flags |= O_EXCL;
flags |= O_RDWR;
modestr = "rwex";
} else if (dev->flags & DEV_BCACHE_WRITE) {
flags |= O_RDWR;
modestr = "rw";
} else {
flags |= O_RDONLY;
modestr = "ro";
}
fd = open(name, flags, 0777);
if (fd < 0) {
if ((errno == EBUSY) && (flags & O_EXCL)) {
log_error("Can't open %s exclusively. Mounted filesystem?",
dev_name(dev));
return 0;
} else {
/*
* drop name from dev->aliases and use verify_aliases to
* drop any other invalid aliases before retrying open with
* any remaining valid paths.
*/
log_debug("Drop alias for %u:%u failed open %s (%d).",
MAJOR(dev->dev), MINOR(dev->dev), name, errno);
dev_cache_failed_path(dev, name);
dev_cache_verify_aliases(dev);
goto next_name;
}
}
/* Verify that major:minor from the path still match dev. */
if ((fstat(fd, &sbuf) < 0) || (sbuf.st_rdev != dev->dev)) {
log_warn("Invalid path %s for device %u:%u, trying different path.",
name, MAJOR(dev->dev), MINOR(dev->dev));
(void)close(fd);
dev_cache_failed_path(dev, name);
dev_cache_verify_aliases(dev);
goto next_name;
}
dev->flags |= DEV_IN_BCACHE;
dev->bcache_fd = fd;
di = bcache_set_fd(fd);
if (di == -1) {
log_error("Failed to set bcache fd.");
if (close(fd))
log_sys_debug("close", name);
dev->bcache_fd = -1;
return 0;
}
log_debug("open %s %s di %d fd %d", dev_name(dev), modestr, di, fd);
dev->bcache_di = di;
return 1;
}
static int _scan_dev_close(struct device *dev)
{
if (!(dev->flags & DEV_IN_BCACHE))
log_error("scan_dev_close %s no DEV_IN_BCACHE set", dev_name(dev));
dev->flags &= ~DEV_IN_BCACHE;
dev->flags &= ~DEV_BCACHE_EXCL;
dev->flags &= ~DEV_BCACHE_WRITE;
if (dev->bcache_di == -1) {
log_error("scan_dev_close %s already closed", dev_name(dev));
return 0;
}