virscsi.c 11.0 KB
Newer Older
1 2 3
/*
 * virscsi.c: helper APIs for managing host SCSI devices
 *
4
 * Copyright (C) 2013-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (C) 2013 Fujitsu, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Han Cheng <hanc.fnst@cn.fujitsu.com>
23
 *     Osier Yang <jyang@redhat.com>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 */

#include <config.h>

#include <dirent.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "virscsi.h"
#include "virlog.h"
#include "viralloc.h"
#include "virfile.h"
#include "virutil.h"
#include "virstring.h"
#include "virerror.h"

#define SYSFS_SCSI_DEVICES "/sys/bus/scsi/devices"

/* For virReportOOMError()  and virReportSystemError() */
#define VIR_FROM_THIS VIR_FROM_NONE

struct _virSCSIDevice {
    unsigned int adapter;
    unsigned int bus;
    unsigned int target;
    unsigned int unit;

    char *name; /* adapter:bus:target:unit */
    char *id;   /* model:vendor */
    char *sg_path; /* e.g. /dev/sg2 */
60 61
    char **used_by; /* name of the domains using this dev */
    size_t n_used_by; /* how many domains are using this dev */
62 63

    bool readonly;
64
    bool shareable;
65 66 67 68
};

struct _virSCSIDeviceList {
    virObjectLockable parent;
69
    size_t count;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    virSCSIDevicePtr *devs;
};

static virClassPtr virSCSIDeviceListClass;

static void virSCSIDeviceListDispose(void *obj);

static int
virSCSIOnceInit(void)
{
    if (!(virSCSIDeviceListClass = virClassNew(virClassForObjectLockable(),
                                               "virSCSIDeviceList",
                                               sizeof(virSCSIDeviceList),
                                               virSCSIDeviceListDispose)))
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virSCSI)

static int
virSCSIDeviceGetAdapterId(const char *adapter,
                          unsigned int *adapter_id)
{
95 96 97 98 99 100 101
    if (STRPREFIX(adapter, "scsi_host") &&
        virStrToLong_ui(adapter + strlen("scsi_host"),
                        NULL, 0, adapter_id) == 0)
        return 0;
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Cannot parse adapter '%s'"), adapter);
    return -1;
102 103 104
}

char *
105 106
virSCSIDeviceGetSgName(const char *sysfs_prefix,
                       const char *adapter,
107 108 109 110 111 112 113 114 115
                       unsigned int bus,
                       unsigned int target,
                       unsigned int unit)
{
    DIR *dir = NULL;
    struct dirent *entry;
    char *path = NULL;
    char *sg = NULL;
    unsigned int adapter_id;
116
    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
117 118 119 120 121

    if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
        return NULL;

    if (virAsprintf(&path,
122 123
                    "%s/%d:%d:%d:%d/scsi_generic",
                    prefix, adapter_id, bus, target, unit) < 0)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        return NULL;

    if (!(dir = opendir(path))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), path);
        goto cleanup;
    }

    while ((entry = readdir(dir))) {
        if (entry->d_name[0] == '.')
            continue;

        if (VIR_STRDUP(sg, entry->d_name) < 0)
            goto cleanup;
    }

cleanup:
    closedir(dir);
    VIR_FREE(path);
    return sg;
}

146 147 148 149
/* Returns device name (e.g. "sdc") on success, or NULL
 * on failure.
 */
char *
150 151
virSCSIDeviceGetDevName(const char *sysfs_prefix,
                        const char *adapter,
152 153 154 155 156 157 158 159 160
                        unsigned int bus,
                        unsigned int target,
                        unsigned int unit)
{
    DIR *dir = NULL;
    struct dirent *entry;
    char *path = NULL;
    char *name = NULL;
    unsigned int adapter_id;
161
    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
162 163 164 165 166

    if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
        return NULL;

    if (virAsprintf(&path,
167 168
                    "%s/%d:%d:%d:%d/block",
                    prefix, adapter_id, bus, target, unit) < 0)
169 170 171 172 173 174 175 176 177 178 179 180
        return NULL;

    if (!(dir = opendir(path))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to open %s"), path);
        goto cleanup;
    }

    while ((entry = readdir(dir))) {
        if (entry->d_name[0] == '.')
            continue;

181 182
        ignore_value(VIR_STRDUP(name, entry->d_name));
        break;
183 184 185 186 187 188 189 190
    }

cleanup:
    closedir(dir);
    VIR_FREE(path);
    return name;
}

191
virSCSIDevicePtr
192 193
virSCSIDeviceNew(const char *sysfs_prefix,
                 const char *adapter,
194 195 196
                 unsigned int bus,
                 unsigned int target,
                 unsigned int unit,
197 198
                 bool readonly,
                 bool shareable)
199 200 201 202 203 204 205
{
    virSCSIDevicePtr dev, ret = NULL;
    char *sg = NULL;
    char *vendor_path = NULL;
    char *model_path = NULL;
    char *vendor = NULL;
    char *model = NULL;
206
    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
207

208
    if (VIR_ALLOC(dev) < 0)
209 210 211 212 213 214
        return NULL;

    dev->bus = bus;
    dev->target = target;
    dev->unit = unit;
    dev->readonly = readonly;
O
Osier Yang 已提交
215
    dev->shareable = shareable;
216

217
    if (!(sg = virSCSIDeviceGetSgName(prefix, adapter, bus, target, unit)))
218 219 220 221 222 223
        goto cleanup;

    if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0)
        goto cleanup;

    if (virAsprintf(&dev->name, "%d:%d:%d:%d", dev->adapter,
224
                    dev->bus, dev->target, dev->unit) < 0 ||
225 226
        virAsprintf(&dev->sg_path, "%s/%s",
                    sysfs_prefix ? sysfs_prefix : "/dev", sg) < 0)
227 228
        goto cleanup;

229
    if (!virFileExists(dev->sg_path)) {
230 231 232 233 234 235 236
        virReportSystemError(errno,
                             _("SCSI device '%s': could not access %s"),
                             dev->name, dev->sg_path);
        goto cleanup;
    }

    if (virAsprintf(&vendor_path,
237
                    "%s/%s/vendor", prefix, dev->name) < 0 ||
238
        virAsprintf(&model_path,
239
                    "%s/%s/model", prefix, dev->name) < 0)
240 241 242 243 244 245 246 247 248 249 250
        goto cleanup;

    if (virFileReadAll(vendor_path, 1024, &vendor) < 0)
        goto cleanup;

    if (virFileReadAll(model_path, 1024, &model) < 0)
        goto cleanup;

    virTrimSpaces(vendor, NULL);
    virTrimSpaces(model, NULL);

251
    if (virAsprintf(&dev->id, "%s:%s", vendor, model) < 0)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        goto cleanup;

    ret = dev;
cleanup:
    VIR_FREE(sg);
    VIR_FREE(vendor);
    VIR_FREE(model);
    VIR_FREE(vendor_path);
    VIR_FREE(model_path);
    if (!ret)
        virSCSIDeviceFree(dev);
    return ret;
}

void
virSCSIDeviceFree(virSCSIDevicePtr dev)
{
269 270
    size_t i;

271 272 273 274 275 276
    if (!dev)
        return;

    VIR_FREE(dev->id);
    VIR_FREE(dev->name);
    VIR_FREE(dev->sg_path);
277 278 279
    for (i = 0; i < dev->n_used_by; i++)
        VIR_FREE(dev->used_by[i]);
    VIR_FREE(dev->used_by);
280 281 282
    VIR_FREE(dev);
}

283
int
284 285 286
virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev,
                       const char *name)
{
287 288 289 290 291 292
    char *copy = NULL;

    if (VIR_STRDUP(copy, name) < 0)
        return -1;

    return VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy);
293 294
}

295 296
bool
virSCSIDeviceIsAvailable(virSCSIDevicePtr dev)
297
{
298
    return dev->n_used_by == 0;
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
}

const char *
virSCSIDeviceGetName(virSCSIDevicePtr dev)
{
    return dev->name;
}

unsigned int
virSCSIDeviceGetAdapter(virSCSIDevicePtr dev)
{
    return dev->adapter;
}

unsigned int
virSCSIDeviceGetBus(virSCSIDevicePtr dev)
{
    return dev->bus;
}

unsigned int
virSCSIDeviceGetTarget(virSCSIDevicePtr dev)
{
    return dev->target;
}

unsigned int
virSCSIDeviceGetUnit(virSCSIDevicePtr dev)
{
    return dev->unit;
}

bool
virSCSIDeviceGetReadonly(virSCSIDevicePtr dev)
{
    return dev->readonly;
}

337 338 339 340 341 342
bool
virSCSIDeviceGetShareable(virSCSIDevicePtr dev)
{
    return dev->shareable;
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
int
virSCSIDeviceFileIterate(virSCSIDevicePtr dev,
                         virSCSIDeviceFileActor actor,
                         void *opaque)
{
    return (actor)(dev, dev->sg_path, opaque);
}

virSCSIDeviceListPtr
virSCSIDeviceListNew(void)
{
    virSCSIDeviceListPtr list;

    if (virSCSIInitialize() < 0)
        return NULL;

    if (!(list = virObjectLockableNew(virSCSIDeviceListClass)))
        return NULL;

    return list;
}

static void
virSCSIDeviceListDispose(void *obj)
{
    virSCSIDeviceListPtr list = obj;
369
    size_t i;
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

    for (i = 0; i < list->count; i++)
        virSCSIDeviceFree(list->devs[i]);

    VIR_FREE(list->devs);
}

int
virSCSIDeviceListAdd(virSCSIDeviceListPtr list,
                     virSCSIDevicePtr dev)
{
    if (virSCSIDeviceListFind(list, dev)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Device %s already exists"),
                       dev->name);
        return -1;
    }

388
    return VIR_APPEND_ELEMENT(list->devs, list->count, dev);
389 390 391 392 393 394 395 396 397 398 399
}

virSCSIDevicePtr
virSCSIDeviceListGet(virSCSIDeviceListPtr list, int idx)
{
    if (idx >= list->count || idx < 0)
        return NULL;

    return list->devs[idx];
}

400
size_t
401 402 403 404 405 406 407 408 409 410
virSCSIDeviceListCount(virSCSIDeviceListPtr list)
{
    return list->count;
}

virSCSIDevicePtr
virSCSIDeviceListSteal(virSCSIDeviceListPtr list,
                       virSCSIDevicePtr dev)
{
    virSCSIDevicePtr ret = NULL;
411
    size_t i;
412 413

    for (i = 0; i < list->count; i++) {
414 415 416 417 418 419 420
        if (list->devs[i]->adapter == dev->adapter &&
            list->devs[i]->bus == dev->bus &&
            list->devs[i]->target == dev->target &&
            list->devs[i]->unit == dev->unit) {
            ret = list->devs[i];
            VIR_DELETE_ELEMENT(list->devs, i, list->count);
            break;
421 422 423 424 425 426 427 428
        }
    }

    return ret;
}

void
virSCSIDeviceListDel(virSCSIDeviceListPtr list,
429 430
                     virSCSIDevicePtr dev,
                     const char *name)
431
{
432 433 434 435 436 437
    virSCSIDevicePtr tmp = NULL;
    size_t i;

    for (i = 0; i < dev->n_used_by; i++) {
        if (STREQ_NULLABLE(dev->used_by[i], name)) {
            if (dev->n_used_by > 1) {
438
                VIR_FREE(dev->used_by[i]);
439 440 441 442 443 444 445 446
                VIR_DELETE_ELEMENT(dev->used_by, i, dev->n_used_by);
            } else {
                tmp = virSCSIDeviceListSteal(list, dev);
                virSCSIDeviceFree(tmp);
            }
            break;
        }
    }
447 448 449 450 451 452
}

virSCSIDevicePtr
virSCSIDeviceListFind(virSCSIDeviceListPtr list,
                      virSCSIDevicePtr dev)
{
453
    size_t i;
454 455 456 457 458 459 460 461 462 463 464

    for (i = 0; i < list->count; i++) {
        if (list->devs[i]->adapter == dev->adapter &&
            list->devs[i]->bus == dev->bus &&
            list->devs[i]->target == dev->target &&
            list->devs[i]->unit == dev->unit)
            return list->devs[i];
    }

    return NULL;
}