virscsi.c 11.2 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 23 24 25 26 27 28 29 30
 * 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/>.
 */

#include <config.h>

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

31
#include "virlog.h"
32 33 34 35 36
#include "virscsi.h"
#include "virfile.h"
#include "virutil.h"
#include "virstring.h"
#include "virerror.h"
37
#include "viralloc.h"
38 39 40 41 42

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

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

VIR_LOG_INIT("util.scsi");

C
Chunyan Liu 已提交
46 47 48 49
struct _virUsedByInfo {
    char *drvname; /* which driver */
    char *domname; /* which domain */
};
50 51
typedef struct _virUsedByInfo virUsedByInfo;
typedef virUsedByInfo *virUsedByInfoPtr;
52 53 54 55 56

struct _virSCSIDevice {
    unsigned int adapter;
    unsigned int bus;
    unsigned int target;
57
    unsigned long long unit;
58 59 60 61

    char *name; /* adapter:bus:target:unit */
    char *id;   /* model:vendor */
    char *sg_path; /* e.g. /dev/sg2 */
C
Chunyan Liu 已提交
62
    virUsedByInfoPtr *used_by; /* driver:domain(s) using this dev */
63
    size_t n_used_by; /* how many domains are using this dev */
64 65

    bool readonly;
66
    bool shareable;
67 68 69 70
};

struct _virSCSIDeviceList {
    virObjectLockable parent;
71
    size_t count;
72 73 74 75 76 77 78 79 80 81
    virSCSIDevicePtr *devs;
};

static virClassPtr virSCSIDeviceListClass;

static void virSCSIDeviceListDispose(void *obj);

static int
virSCSIOnceInit(void)
{
82
    if (!VIR_CLASS_NEW(virSCSIDeviceList, virClassForObjectLockable()))
83 84 85 86 87
        return -1;

    return 0;
}

88
VIR_ONCE_GLOBAL_INIT(virSCSI);
89 90 91 92 93

static int
virSCSIDeviceGetAdapterId(const char *adapter,
                          unsigned int *adapter_id)
{
94 95 96 97 98 99 100
    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;
101 102 103
}

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

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

    if (virAsprintf(&path,
121
                    "%s/%d:%u:%u:%llu/scsi_generic",
122
                    prefix, adapter_id, bus, target, unit) < 0)
123 124
        return NULL;

J
Ján Tomko 已提交
125
    if (virDirOpen(&dir, path) < 0)
126 127
        goto cleanup;

E
Eric Blake 已提交
128 129 130 131
    while (virDirRead(dir, &entry, path) > 0) {
        /* Assume a single directory entry */
        ignore_value(VIR_STRDUP(sg, entry->d_name));
        break;
132 133
    }

134
 cleanup:
J
Ján Tomko 已提交
135
    VIR_DIR_CLOSE(dir);
136 137 138
    return sg;
}

139 140 141 142
/* Returns device name (e.g. "sdc") on success, or NULL
 * on failure.
 */
char *
143 144
virSCSIDeviceGetDevName(const char *sysfs_prefix,
                        const char *adapter,
145 146
                        unsigned int bus,
                        unsigned int target,
147
                        unsigned long long unit)
148 149 150
{
    DIR *dir = NULL;
    struct dirent *entry;
151
    VIR_AUTOFREE(char *) path = NULL;
152 153
    char *name = NULL;
    unsigned int adapter_id;
154
    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
155 156 157 158 159

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

    if (virAsprintf(&path,
160
                    "%s/%d:%u:%u:%llu/block",
161
                    prefix, adapter_id, bus, target, unit) < 0)
162 163
        return NULL;

J
Ján Tomko 已提交
164
    if (virDirOpen(&dir, path) < 0)
165 166
        goto cleanup;

E
Eric Blake 已提交
167
    while (virDirRead(dir, &entry, path) > 0) {
168 169
        ignore_value(VIR_STRDUP(name, entry->d_name));
        break;
170 171
    }

172
 cleanup:
J
Ján Tomko 已提交
173
    VIR_DIR_CLOSE(dir);
174 175 176
    return name;
}

177
virSCSIDevicePtr
178 179
virSCSIDeviceNew(const char *sysfs_prefix,
                 const char *adapter,
180 181
                 unsigned int bus,
                 unsigned int target,
182
                 unsigned long long unit,
183 184
                 bool readonly,
                 bool shareable)
185
{
186 187
    VIR_AUTOPTR(virSCSIDevice) dev = NULL;
    virSCSIDevicePtr ret = NULL;
188 189 190 191 192
    VIR_AUTOFREE(char *) sg = NULL;
    VIR_AUTOFREE(char *) vendor_path = NULL;
    VIR_AUTOFREE(char *) model_path = NULL;
    VIR_AUTOFREE(char *) vendor = NULL;
    VIR_AUTOFREE(char *) model = NULL;
193
    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
194

195
    if (VIR_ALLOC(dev) < 0)
196 197 198 199 200 201
        return NULL;

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

204
    if (!(sg = virSCSIDeviceGetSgName(prefix, adapter, bus, target, unit)))
205
        return NULL;
206 207

    if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0)
208
        return NULL;
209

210
    if (virAsprintf(&dev->name, "%d:%u:%u:%llu", dev->adapter,
211
                    dev->bus, dev->target, dev->unit) < 0 ||
212 213
        virAsprintf(&dev->sg_path, "%s/%s",
                    sysfs_prefix ? sysfs_prefix : "/dev", sg) < 0)
214
        return NULL;
215

216
    if (!virFileExists(dev->sg_path)) {
217 218 219
        virReportSystemError(errno,
                             _("SCSI device '%s': could not access %s"),
                             dev->name, dev->sg_path);
220
        return NULL;
221 222 223
    }

    if (virAsprintf(&vendor_path,
224
                    "%s/%s/vendor", prefix, dev->name) < 0 ||
225
        virAsprintf(&model_path,
226
                    "%s/%s/model", prefix, dev->name) < 0)
227
        return NULL;
228 229

    if (virFileReadAll(vendor_path, 1024, &vendor) < 0)
230
        return NULL;
231 232

    if (virFileReadAll(model_path, 1024, &model) < 0)
233
        return NULL;
234 235 236 237

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

238
    if (virAsprintf(&dev->id, "%s:%s", vendor, model) < 0)
239
        return NULL;
240

241
    VIR_STEAL_PTR(ret, dev);
242 243 244
    return ret;
}

245 246 247 248 249 250 251
static void
virSCSIDeviceUsedByInfoFree(virUsedByInfoPtr used_by)
{
    VIR_FREE(used_by->drvname);
    VIR_FREE(used_by->domname);
    VIR_FREE(used_by);
}
252
VIR_DEFINE_AUTOPTR_FUNC(virUsedByInfo, virSCSIDeviceUsedByInfoFree);
253

254 255 256
void
virSCSIDeviceFree(virSCSIDevicePtr dev)
{
257 258
    size_t i;

259 260 261 262 263 264
    if (!dev)
        return;

    VIR_FREE(dev->id);
    VIR_FREE(dev->name);
    VIR_FREE(dev->sg_path);
265 266
    for (i = 0; i < dev->n_used_by; i++)
        virSCSIDeviceUsedByInfoFree(dev->used_by[i]);
267
    VIR_FREE(dev->used_by);
268 269 270
    VIR_FREE(dev);
}

271
int
272
virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev,
C
Chunyan Liu 已提交
273 274
                       const char *drvname,
                       const char *domname)
275
{
276 277
    VIR_AUTOPTR(virUsedByInfo) copy = NULL;

C
Chunyan Liu 已提交
278 279
    if (VIR_ALLOC(copy) < 0)
        return -1;
280 281
    if (VIR_STRDUP(copy->drvname, drvname) < 0 ||
        VIR_STRDUP(copy->domname, domname) < 0)
282
        return -1;
283 284

    if (VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy) < 0)
285
        return -1;
286

287
    return 0;
288 289
}

290 291
bool
virSCSIDeviceIsAvailable(virSCSIDevicePtr dev)
292
{
293
    return dev->n_used_by == 0;
294 295 296 297 298 299 300 301
}

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

302 303 304 305 306 307
const char *
virSCSIDeviceGetPath(virSCSIDevicePtr dev)
{
    return dev->sg_path;
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
unsigned int
virSCSIDeviceGetAdapter(virSCSIDevicePtr dev)
{
    return dev->adapter;
}

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

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

326
unsigned long long
327 328 329 330 331 332 333 334 335 336 337
virSCSIDeviceGetUnit(virSCSIDevicePtr dev)
{
    return dev->unit;
}

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

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

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 369
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;
370
    size_t i;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

    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;
    }

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

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

    return list->devs[idx];
}

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

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

    for (i = 0; i < list->count; i++) {
415 416 417 418 419 420 421
        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;
422 423 424 425 426 427 428 429
        }
    }

    return ret;
}

void
virSCSIDeviceListDel(virSCSIDeviceListPtr list,
430
                     virSCSIDevicePtr dev,
C
Chunyan Liu 已提交
431 432
                     const char *drvname,
                     const char *domname)
433
{
434 435 436
    size_t i;

    for (i = 0; i < dev->n_used_by; i++) {
C
Chunyan Liu 已提交
437 438
        if (STREQ_NULLABLE(dev->used_by[i]->drvname, drvname) &&
            STREQ_NULLABLE(dev->used_by[i]->domname, domname)) {
439
            if (dev->n_used_by > 1) {
440
                virSCSIDeviceUsedByInfoFree(dev->used_by[i]);
441 442
                VIR_DELETE_ELEMENT(dev->used_by, i, dev->n_used_by);
            } else {
443
                VIR_AUTOPTR(virSCSIDevice) tmp = NULL;
444 445 446 447 448
                tmp = virSCSIDeviceListSteal(list, dev);
            }
            break;
        }
    }
449 450 451 452 453 454
}

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

    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;
}