Blame src/slave.c

Packit 7e09eb
/*
Packit 7e09eb
 * Intel(R) Enclosure LED Utilities
Packit 7e09eb
 * Copyright (C) 2009-2019 Intel Corporation.
Packit 7e09eb
 *
Packit 7e09eb
 * This program is free software; you can redistribute it and/or modify it
Packit 7e09eb
 * under the terms and conditions of the GNU General Public License,
Packit 7e09eb
 * version 2, as published by the Free Software Foundation.
Packit 7e09eb
 *
Packit 7e09eb
 * This program is distributed in the hope it will be useful, but WITHOUT
Packit 7e09eb
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 7e09eb
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
Packit 7e09eb
 * more details.
Packit 7e09eb
 *
Packit 7e09eb
 * You should have received a copy of the GNU General Public License along with
Packit 7e09eb
 * this program; if not, write to the Free Software Foundation, Inc.,
Packit 7e09eb
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Packit 7e09eb
 *
Packit 7e09eb
 */
Packit 7e09eb
Packit 7e09eb
#include <limits.h>
Packit 7e09eb
#include <stdint.h>
Packit 7e09eb
#include <stdio.h>
Packit 7e09eb
#include <stdlib.h>
Packit 7e09eb
#include <string.h>
Packit 7e09eb
#include <sys/stat.h>
Packit 7e09eb
Packit 7e09eb
#if _HAVE_DMALLOC_H
Packit 7e09eb
#include <dmalloc.h>
Packit 7e09eb
#endif
Packit 7e09eb
Packit 7e09eb
#include "config.h"
Packit 7e09eb
#include "ibpi.h"
Packit 7e09eb
#include "list.h"
Packit 7e09eb
#include "slave.h"
Packit 7e09eb
#include "status.h"
Packit 7e09eb
#include "sysfs.h"
Packit 7e09eb
#include "utils.h"
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 */
Packit 7e09eb
static unsigned char _get_state(const char *path)
Packit 7e09eb
{
Packit 7e09eb
	char *p, *t, *s;
Packit 7e09eb
	unsigned char result = SLAVE_STATE_UNKNOWN;
Packit 7e09eb
Packit 7e09eb
	s = p = get_text(path, "state");
Packit 7e09eb
	if (p) {
Packit 7e09eb
		while (s) {
Packit 7e09eb
			t = strchr(s, ',');
Packit 7e09eb
			if (t)
Packit 7e09eb
				*(t++) = '\0';
Packit 7e09eb
			if (strcmp(s, "spare") == 0)
Packit 7e09eb
				result |= SLAVE_STATE_SPARE;
Packit 7e09eb
			else if (strcmp(s, "in_sync") == 0)
Packit 7e09eb
				result |= SLAVE_STATE_IN_SYNC;
Packit 7e09eb
			else if (strcmp(s, "faulty") == 0)
Packit 7e09eb
				result |= SLAVE_STATE_FAULTY;
Packit 7e09eb
			else if (strcmp(s, "write_mostly") == 0)
Packit 7e09eb
				result |= SLAVE_STATE_WRITE_MOSTLY;
Packit 7e09eb
			else if (strcmp(s, "blocked") == 0)
Packit 7e09eb
				result |= SLAVE_STATE_BLOCKED;
Packit 7e09eb
			s = t;
Packit 7e09eb
		}
Packit 7e09eb
		free(p);
Packit 7e09eb
	}
Packit 7e09eb
	return result;
Packit 7e09eb
}
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 */
Packit 7e09eb
static unsigned int _get_errors(const char *path)
Packit 7e09eb
{
Packit 7e09eb
	return get_int(path, 0, "errors");
Packit 7e09eb
}
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 */
Packit 7e09eb
static unsigned int _get_slot(const char *path)
Packit 7e09eb
{
Packit 7e09eb
	unsigned int result = -1;
Packit 7e09eb
Packit 7e09eb
	char *p = get_text(path, "slot");
Packit 7e09eb
	if (p) {
Packit 7e09eb
		if (strcmp(p, "none") != 0)
Packit 7e09eb
			result = atoi(p);
Packit 7e09eb
		free(p);
Packit 7e09eb
	}
Packit 7e09eb
	return result;
Packit 7e09eb
}
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 */
Packit 7e09eb
static struct block_device *_get_block(const char *path, struct list *block_list)
Packit 7e09eb
{
Packit 7e09eb
	char temp[PATH_MAX];
Packit 7e09eb
	char link[PATH_MAX];
Packit 7e09eb
	struct block_device *device;
Packit 7e09eb
Packit 7e09eb
	snprintf(temp, sizeof(temp), "%s/block", path);
Packit 7e09eb
Packit 7e09eb
	if (!realpath(temp, link))
Packit 7e09eb
		return NULL;
Packit 7e09eb
Packit 7e09eb
	/* translate partition to master block dev */
Packit 7e09eb
	if (snprintf(temp, PATH_MAX, "%s/partition", link) > 0) {
Packit 7e09eb
		struct stat sb;
Packit 7e09eb
		char *ptr;
Packit 7e09eb
Packit 7e09eb
		if (stat(temp, &sb) == 0 && S_ISREG(sb.st_mode)) {
Packit 7e09eb
			ptr = strrchr(link, '/');
Packit 7e09eb
			if (ptr)
Packit 7e09eb
				*ptr = '\0';
Packit 7e09eb
		}
Packit 7e09eb
	}
Packit 7e09eb
Packit 7e09eb
	list_for_each(block_list, device) {
Packit 7e09eb
		if (strcmp(device->sysfs_path, link) == 0)
Packit 7e09eb
			return device;
Packit 7e09eb
	}
Packit 7e09eb
	return NULL;
Packit 7e09eb
}
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 */
Packit 7e09eb
struct slave_device *slave_device_init(const char *path, struct list *block_list)
Packit 7e09eb
{
Packit 7e09eb
	struct slave_device *device = NULL;
Packit 7e09eb
	struct block_device *block;
Packit 7e09eb
Packit 7e09eb
	block = _get_block(path, block_list);
Packit 7e09eb
	if (block) {
Packit 7e09eb
		device = malloc(sizeof(struct slave_device));
Packit 7e09eb
		if (device) {
Packit 7e09eb
			device->raid = NULL;
Packit 7e09eb
			device->state = _get_state(path);
Packit 7e09eb
			device->slot = _get_slot(path);
Packit 7e09eb
			device->errors = _get_errors(path);
Packit 7e09eb
			device->block = block;
Packit 7e09eb
		}
Packit 7e09eb
	}
Packit 7e09eb
	return device;
Packit 7e09eb
}
Packit 7e09eb
Packit 7e09eb
/**
Packit 7e09eb
 */
Packit 7e09eb
void slave_device_fini(struct slave_device *device)
Packit 7e09eb
{
Packit 7e09eb
	free(device);
Packit 7e09eb
}