#include "vinumhdr.h"
#include "request.h"
#include <vm/vm_zone.h>
#include <sys/nlookup.h>
static char *sappend(char *txt, char *s);
static int drivecmp(const void *va, const void *vb);
int
open_drive(struct drive *drive, struct proc *p, int verbose)
{
struct nlookupdata nd;
int error;
if (drive->flags & VF_OPEN)
return EBUSY;
if (rootdev) {
error = nlookup_init(&nd, drive->devicename, UIO_SYSSPACE, NLC_FOLLOW);
if (error)
return error;
error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
drive->vp = nd.nl_open_vp;
nd.nl_open_vp = NULL;
nlookup_done(&nd);
} else {
error = vn_opendisk(drive->devicename, FREAD|FWRITE, &drive->vp);
if (error)
return error;
}
if (error == 0 && drive->vp == NULL)
error = ENODEV;
if (error == 0 && drive->vp->v_type != VCHR) {
vn_close(drive->vp, FREAD|FWRITE, NULL);
drive->vp = NULL;
error = ENODEV;
}
if (error) {
drive->state = drive_down;
if (verbose) {
log(LOG_WARNING,
"vinum open_drive %s: failed with error %d\n",
drive->devicename, error);
}
} else {
drive->dev = drive->vp->v_rdev;
drive->flags |= VF_OPEN;
}
drive->lasterror = error;
return error;
}
int
set_drive_parms(struct drive *drive)
{
drive->blocksize = BLKDEV_IOSIZE;
drive->secsperblock = drive->blocksize
/ drive->partinfo.media_blksize;
bcopy(hostname, drive->label.sysname, VINUMHOSTNAMELEN);
getmicrotime(&drive->label.date_of_birth);
drive->label.drive_size = drive->partinfo.media_size;
#ifdef VINUMDEBUG
if (debug & DEBUG_BIGDRIVE)
drive->label.drive_size *= 100;
#endif
drive->sectors_available = drive->label.drive_size / DEV_BSIZE - DATASTART;
if (drive->label.drive_size < MINVINUMSLICE) {
set_drive_state(drive->driveno, drive_down, setstate_force);
drive->lasterror = ENOSPC;
return ENOSPC;
}
drive->freelist_size = INITIAL_DRIVE_FREELIST;
drive->freelist = (struct drive_freelist *)
Malloc(INITIAL_DRIVE_FREELIST * sizeof(struct drive_freelist));
if (drive->freelist == NULL)
return ENOSPC;
drive->freelist_entries = 1;
drive->freelist[0].offset = DATASTART;
drive->freelist[0].sectors = (drive->label.drive_size >> DEV_BSHIFT) - DATASTART;
if (drive->label.name[0] != '\0')
set_drive_state(drive->driveno, drive_up, setstate_force);
else
drive->state = drive_referenced;
return 0;
}
int
init_drive(struct drive *drive, int verbose)
{
if (drive->devicename[0] != '/') {
drive->lasterror = EINVAL;
log(LOG_ERR, "vinum: Can't open drive without drive name (%s)\n",
drive->devicename);
return EINVAL;
}
drive->lasterror = open_drive(drive, curproc, verbose);
if (drive->lasterror)
return drive->lasterror;
drive->lasterror = VOP_IOCTL(drive->vp, DIOCGPART,
(caddr_t)&drive->partinfo, FREAD|FWRITE,
proc0.p_ucred, NULL);
if (drive->lasterror) {
if (verbose)
log(LOG_WARNING,
"vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
drive->devicename,
drive->lasterror);
close_drive(drive);
return drive->lasterror;
}
if (drive->partinfo.fstype != FS_VINUM &&
!kuuid_is_vinum(&drive->partinfo.fstype_uuid)
) {
drive->lasterror = EFTYPE;
if (verbose)
log(LOG_WARNING,
"vinum open_drive %s: Wrong partition type for vinum\n",
drive->devicename);
close_drive(drive);
return EFTYPE;
}
return set_drive_parms(drive);
}
void
close_drive(struct drive *drive)
{
LOCKDRIVE(drive);
if (drive->flags & VF_OPEN)
close_locked_drive(drive);
if (drive->state > drive_down)
drive->state = drive_down;
unlockdrive(drive);
}
void
close_locked_drive(struct drive *drive)
{
if (drive->vp) {
drive->lasterror = vn_close(drive->vp, FREAD|FWRITE, NULL);
drive->vp = NULL;
}
drive->flags &= ~VF_OPEN;
}
void
remove_drive(int driveno)
{
struct drive *drive = &vinum_conf.drive[driveno];
struct vinum_hdr *vhdr;
int error;
if (drive->state > drive_referenced) {
if (drive->state == drive_up) {
vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);
CHECKALLOC(vhdr, "Can't allocate memory");
error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
if (error)
drive->lasterror = error;
else {
vhdr->magic = VINUM_NOMAGIC;
write_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
}
Free(vhdr);
}
free_drive(drive);
save_config();
}
}
int
driveio(struct drive *drive, char *buf, size_t length, off_t offset, buf_cmd_t cmd)
{
int error;
struct buf *bp;
caddr_t saveaddr;
error = 0;
while (length) {
int len = umin(length, MAXBSIZE);
bp = getpbuf_kva(NULL);
KKASSERT(len <= bp->b_bufsize);
bp->b_cmd = cmd;
bp->b_bio1.bio_offset = offset;
bp->b_bio1.bio_done = biodone_sync;
bp->b_bio1.bio_flags |= BIO_SYNC;
saveaddr = bp->b_data;
bp->b_data = buf;
bp->b_bcount = len;
vn_strategy(drive->vp, &bp->b_bio1);
error = biowait(&bp->b_bio1, (cmd == BUF_CMD_READ ? "drvrd" : "drvwr"));
bp->b_data = saveaddr;
bp->b_flags |= B_INVAL | B_AGE;
bp->b_flags &= ~B_ERROR;
relpbuf(bp, NULL);
if (error)
break;
length -= len;
buf += len;
offset += len;
}
return error;
}
enum drive_label_info
read_drive_label(struct drive *drive, int verbose)
{
int error;
int result;
struct vinum_hdr *vhdr;
error = init_drive(drive, verbose);
if (error)
return DL_CANT_OPEN;
vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);
CHECKALLOC(vhdr, "Can't allocate memory");
drive->state = drive_up;
error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
if (vhdr->magic == VINUM_MAGIC) {
if (drive->label.name[0]
&&(strcmp(drive->label.name, vhdr->label.name))) {
drive->lasterror = EINVAL;
result = DL_WRONG_DRIVE;
drive->state = drive_unallocated;
} else
result = DL_OURS;
drive->label = vhdr->label;
} else if (vhdr->magic == VINUM_NOMAGIC)
result = DL_DELETED_LABEL;
else
result = DL_NOT_OURS;
Free(vhdr);
return result;
}
struct drive *
check_drive(char *devicename)
{
int driveno;
int i;
struct drive *drive;
driveno = find_drive_by_dev(devicename, 1);
drive = &vinum_conf.drive[driveno];
if (read_drive_label(drive, 0) == DL_OURS) {
for (i = 0; i < vinum_conf.drives_allocated; i++) {
if ((i != driveno)
&&(DRIVE[i].state != drive_unallocated)
&&(strcmp(DRIVE[i].label.name,
DRIVE[driveno].label.name) == 0)) {
struct drive *mydrive = &DRIVE[i];
if (mydrive->devicename[0] == '/') {
drive->lasterror = EEXIST;
break;
} else {
int sdno;
for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) {
if ((SD[sdno].driveno == i)
&&(SD[sdno].state != sd_unallocated)) {
SD[sdno].driveno = drive->driveno;
update_sd_state(sdno);
}
}
bzero(mydrive, sizeof(struct drive));
}
}
}
} else {
if (drive->lasterror == 0)
drive->lasterror = ENODEV;
close_drive(drive);
drive->state = drive_down;
}
return drive;
}
static char *
sappend(char *txt, char *s)
{
while ((*s++ = *txt++) != 0);
return s - 1;
}
void
format_config(char *config, int len)
{
int i;
int j;
char *s = config;
char *configend = &config[len];
bzero(config, len);
for (i = 0; i < vinum_conf.volumes_allocated; i++) {
struct volume *vol;
vol = &vinum_conf.volume[i];
if ((vol->state > volume_uninit)
&& (vol->name[0] != '\0')) {
ksnprintf(s,
configend - s,
"volume %s state %s",
vol->name,
volume_state(vol->state));
while (*s)
s++;
if (vol->preferred_plex >= 0)
ksnprintf(s,
configend - s,
" readpol prefer %s",
vinum_conf.plex[vol->preferred_plex].name);
while (*s)
s++;
s = sappend("\n", s);
}
}
for (i = 0; i < vinum_conf.plexes_allocated; i++) {
struct plex *plex;
plex = &vinum_conf.plex[i];
if ((plex->state > plex_referenced)
&& (plex->name[0] != '\0')) {
ksnprintf(s,
configend - s,
"plex name %s state %s org %s ",
plex->name,
plex_state(plex->state),
plex_org(plex->organization));
while (*s)
s++;
if (isstriped(plex)) {
ksnprintf(s,
configend - s,
"%ds ",
(int) plex->stripesize);
while (*s)
s++;
}
if (plex->volno >= 0)
ksnprintf(s,
configend - s,
"vol %s ",
vinum_conf.volume[plex->volno].name);
while (*s)
s++;
for (j = 0; j < plex->subdisks; j++) {
ksnprintf(s,
configend - s,
" sd %s",
vinum_conf.sd[plex->sdnos[j]].name);
}
s = sappend("\n", s);
}
}
for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
struct sd *sd;
char *drivename;
sd = &SD[i];
if ((sd->state != sd_referenced)
&& (sd->state != sd_unallocated)
&& (sd->name[0] != '\0')) {
drivename = vinum_conf.drive[sd->driveno].label.name;
if (drivename[0] == '\0')
drivename = "*invalid*";
ksnprintf(s,
configend - s,
"sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
sd->name,
drivename,
vinum_conf.plex[sd->plexno].name,
(unsigned long long) sd->sectors,
(unsigned long long) sd->driveoffset,
sd_state(sd->state));
while (*s)
s++;
if (sd->plexno >= 0)
ksnprintf(s,
configend - s,
" plexoffset %llds",
(long long) sd->plexoffset);
else
ksnprintf(s, configend - s, " detached");
while (*s)
s++;
if (sd->flags & VF_RETRYERRORS) {
ksnprintf(s, configend - s, " retryerrors");
while (*s)
s++;
}
ksnprintf(s, configend - s, " \n");
while (*s)
s++;
}
}
if (s > &config[len - 2])
panic("vinum: configuration data overflow");
}
void
save_config(void)
{
union daemoninfo di = { .nothing = 0 };
queue_daemon_request(daemonrq_saveconfig, di);
}
void
daemon_save_config(void)
{
int error;
int driveno;
struct drive *drive;
struct vinum_hdr *vhdr;
char *config;
int wlabel_on;
if (vinum_conf.flags & VF_CONFIGURING)
return;
vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN);
CHECKALLOC(vhdr, "Can't allocate config data");
vhdr->magic = VINUM_MAGIC;
vhdr->config_length = MAXCONFIG;
config = Malloc(MAXCONFIG);
CHECKALLOC(config, "Can't allocate config data");
format_config(config, MAXCONFIG);
error = 0;
for (driveno = 0; driveno < vinum_conf.drives_allocated; driveno++) {
drive = &vinum_conf.drive[driveno];
if (drive->state > drive_referenced) {
LOCKDRIVE(drive);
if ((drive->devicename[0] == '\0')
|| (drive->label.name[0] == '\0')) {
unlockdrive(drive);
free_drive(drive);
break;
}
if (((drive->flags & VF_OPEN) == 0)
&&(drive->state > drive_down)) {
unlockdrive(drive);
set_drive_state(driveno, drive_down, setstate_force);
continue;
}
if ((drive->state == drive_down)
&&(drive->flags & VF_OPEN)) {
unlockdrive(drive);
close_drive(drive);
} else if (drive->state > drive_down) {
getmicrotime(&drive->label.last_update);
bcopy((char *) &drive->label,
(char *) &vhdr->label,
sizeof(vhdr->label));
if ((drive->state != drive_unallocated)
&& (drive->state != drive_referenced)) {
wlabel_on = 1;
error = 0;
#if 1
error = VOP_IOCTL(drive->vp, DIOCWLABEL,
(caddr_t)&wlabel_on, FREAD|FWRITE,
proc0.p_ucred, NULL);
#endif
if (error == 0)
error = write_drive(drive, (char *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
if (error == 0)
error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET);
if (error == 0)
error = write_drive(drive, config, MAXCONFIG, VINUM_CONFIG_OFFSET + MAXCONFIG);
wlabel_on = 0;
#if 1
if (error == 0) {
error = VOP_IOCTL(drive->vp, DIOCWLABEL,
(caddr_t)&wlabel_on, FREAD|FWRITE,
proc0.p_ucred, NULL);
}
#endif
unlockdrive(drive);
if (error) {
log(LOG_ERR,
"vinum: Can't write config to %s, error %d\n",
drive->devicename,
error);
set_drive_state(drive->driveno, drive_down, setstate_force);
}
}
} else
unlockdrive(drive);
}
}
Free(vhdr);
Free(config);
}
int
vinum_scandisk(char *devicename[], int drives)
{
struct drive *volatile drive;
volatile int driveno;
volatile int gooddrives;
int firsttime;
int error;
char *config_text;
char *volatile cptr;
char *eptr;
char *config_line;
volatile int status;
int *volatile drivelist;
#define DRIVENAMELEN 64
#define DRIVEPARTS 35
char partname[DRIVENAMELEN];
status = 0;
vinum_conf.flags |= VF_READING_CONFIG;
gooddrives = 0;
firsttime = vinum_conf.drives_used == 0;
drivelist = (int *) Malloc(drives * DRIVEPARTS * sizeof(int));
CHECKALLOC(drivelist, "Can't allocate memory");
error = setjmp(command_fail);
if (error) {
return error;
}
for (driveno = 0; driveno < drives; driveno++) {
char part, has_part = 0;
int slice;
int has_slice = -1;
char *tmp;
if ((tmp = rindex(devicename[driveno], '/')) == NULL)
tmp = devicename[driveno];
else
tmp++;
ksscanf(tmp, "%*[a-z]%*d%*[s]%d%c", &has_slice, &has_part);
for (slice = 0; slice < MAX_SLICES; slice++) {
if (has_slice >= 0 && slice != has_slice)
continue;
for (part = 'a'; part < 'a' + MAXPARTITIONS; part++) {
if (part == 'c')
continue;
if (has_part && part != has_part)
continue;
if (has_slice >= 0 && has_part)
strncpy(partname, devicename[driveno], DRIVENAMELEN);
else if (has_slice >= 0)
ksnprintf(partname, DRIVENAMELEN,
"%s%c", devicename[driveno], part);
else
ksnprintf(partname, DRIVENAMELEN,
"%ss%d%c", devicename[driveno], slice, part);
drive = check_drive(partname);
if ((drive->lasterror != 0)
||(drive->state != drive_up))
free_drive(drive);
else if (drive->flags & VF_CONFIGURED)
log(LOG_WARNING,
"vinum: already read config from %s\n",
drive->label.name);
else {
drivelist[gooddrives] = drive->driveno;
drive->flags &= ~VF_NEWBORN;
gooddrives++;
}
}
}
}
if (gooddrives == 0) {
if (firsttime)
log(LOG_WARNING, "vinum: no drives found\n");
else
log(LOG_INFO, "vinum: no additional drives found\n");
return ENOENT;
}
kqsort(drivelist, gooddrives, sizeof(int), drivecmp);
config_text = (char *) Malloc(MAXCONFIG * 2);
CHECKALLOC(config_text, "Can't allocate memory");
config_line = (char *) Malloc(MAXCONFIGLINE * 2);
CHECKALLOC(config_line, "Can't allocate memory");
for (driveno = 0; driveno < gooddrives; driveno++) {
drive = &DRIVE[drivelist[driveno]];
if (firsttime && (driveno == 0))
log(LOG_INFO, "vinum: reading configuration from %s\n", drive->devicename);
else
log(LOG_INFO, "vinum: updating configuration from %s\n", drive->devicename);
if (drive->state == drive_up)
error = read_drive(drive, config_text, MAXCONFIG * 2, VINUM_CONFIG_OFFSET);
else {
error = EIO;
kprintf("vinum_scandisk: %s is %s\n", drive->devicename, drive_state(drive->state));
}
if (error != 0) {
log(LOG_ERR, "vinum: Can't read device %s, error %d\n", drive->devicename, error);
free_drive(drive);
status = error;
}
else {
vinum_conf.drives_used++;
for (cptr = config_text; *cptr != '\0';) {
volatile int parse_status;
for (eptr = config_line; (*cptr != '\n') && (*cptr != '\0');)
*eptr++ = *cptr++;
*eptr = '\0';
if (setjmp(command_fail) == 0) {
parse_status = parse_config(config_line, &keyword_set, 1);
if (parse_status < 0) {
log(LOG_ERR,
"vinum: Config error on %s, aborting integration\n",
drive->devicename);
free_drive(drive);
status = EINVAL;
}
}
while (*cptr == '\n')
cptr++;
}
}
drive->flags |= VF_CONFIGURED;
}
Free(config_line);
Free(config_text);
Free(drivelist);
vinum_conf.flags &= ~VF_READING_CONFIG;
if (status != 0)
kprintf("vinum: couldn't read configuration");
else
updateconfig(VF_READING_CONFIG);
return status;
}
static int
drivecmp(const void *va, const void *vb)
{
const struct drive *a = &DRIVE[*(const int *) va];
const struct drive *b = &DRIVE[*(const int *) vb];
if ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
&& (a->label.last_update.tv_usec == b->label.last_update.tv_usec))
return 0;
else if ((a->label.last_update.tv_sec > b->label.last_update.tv_sec)
|| ((a->label.last_update.tv_sec == b->label.last_update.tv_sec)
&& (a->label.last_update.tv_usec > b->label.last_update.tv_usec)))
return -1;
else
return 1;
}