/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
*
* 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.
*
* 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
*/
static int _is_partitionable(struct device *dev)
{
int parts = max_partitions(MAJOR(dev->dev));
/* All MD devices are partitionable via blkext (as of 2.6.28) */
if (MAJOR(dev->dev) == md_major())
return 1;
if ((parts <= 1) || (MINOR(dev->dev) % parts))
return 0;
return 1;
}
static int _has_partition_table(struct device *dev)
{
int ret = 0;
unsigned p;
uint16_t buf[SECTOR_SIZE/sizeof(uint16_t)];
uint16_t *part_magic;
struct partition *part;
if (!dev_open(dev)) {
stack;
return -1;
}
if (!dev_read(dev, UINT64_C(0), sizeof(buf), &buf))
goto_out;
/* FIXME Check for other types of partition table too */
/* Check for msdos partition table */
part_magic = buf + PART_MAGIC_OFFSET/sizeof(buf[0]);
if ((*part_magic == xlate16(PART_MAGIC))) {
part = (struct partition *) (buf + PART_OFFSET/sizeof(buf[0]));
for (p = 0; p < 4; p++, part++) {
/* Table is invalid if boot indicator not 0 or 0x80 */
if ((part->boot_ind & 0x7f)) {
ret = 0;
break;
}
/* Must have at least one non-empty partition */
if (part->nr_sects)
ret = 1;
}
}
out:
if (!dev_close(dev))
stack;
return ret;
}
int is_partitioned_dev(struct device *dev)
{
if (!_is_partitionable(dev))
return 0;
if (s_buffer[255] == 0xAA55) {
if (is_whole_disk(dm, d))
return -1;
} else
return 0;
extended_partition = 0;
/* Loop through primary partitions */
for (p = 0; p < 4; p++) {
if (part[p].sys_ind == DOS_EXTENDED_PARTITION ||
part[p].sys_ind == LINUX_EXTENDED_PARTITION
|| part[p].sys_ind == WIN98_EXTENDED_PARTITION) {
extended_partition = 1;
offset = extended_offset + part[p].start_sect;
if (extended_offset == 0)
extended_offset = part[p].start_sect;
if (first_partition == 1)
part_found++;
} else if (first_partition == 1) {
if (p == part_sought) {
if (part[p].sys_ind == 0) {
/* missing primary? */
return 0;
}
} else
part_found++;
} else if (!part[p].sys_ind)
part_found++;
if (part_sought == part_found)
return part[p].sys_ind;
}
first_partition = 0;
}
while (extended_partition == 1);
return 0;
}
#endif
#ifdef linux
int get_primary_dev(const char *sysfs_dir,
struct device *dev, dev_t *result)
{
char path[PATH_MAX+1];
char temp_path[PATH_MAX+1];
char buffer[64];
struct stat info;
FILE *fp;
uint32_t pri_maj, pri_min;
int ret = 0;
/* check if dev is a partition */
if (dm_snprintf(path, PATH_MAX, "%s/dev/block/%d:%d/partition",
sysfs_dir, (int)MAJOR(dev->dev), (int)MINOR(dev->dev)) < 0) {
log_error("dm_snprintf partition failed");
return ret;
}
if (stat(path, &info) == -1) {
if (errno != ENOENT)
log_sys_error("stat", path);
return ret;
}
/*
* check if the desired sysfs attribute exists
* - if not: either the kernel doesn't have topology support
* or the device could be a partition
*/
if (stat(path, &info) == -1) {
if (errno != ENOENT) {
log_sys_error("stat", path);
return 0;
}
if (!get_primary_dev(sysfs_dir, dev, &primary))
return 0;
/* get attribute from partition's primary device */
if (dm_snprintf(path, PATH_MAX, sysfs_fmt_str, sysfs_dir,
(int)MAJOR(primary), (int)MINOR(primary),
attribute) < 0) {
log_error("primary dm_snprintf %s failed", attribute);
return 0;
}
if (stat(path, &info) == -1) {
if (errno != ENOENT)
log_sys_error("stat", path);
return 0;
}
}