]>
| Commit | Line | Data |
|---|---|---|
| 1 | /* | |
| 2 | * sysfs - extract md related information from sysfs. Part of: | |
| 3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
| 4 | * | |
| 5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> | |
| 6 | * | |
| 7 | * | |
| 8 | * This program is free software; you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU General Public License as published by | |
| 10 | * the Free Software Foundation; either version 2 of the License, or | |
| 11 | * (at your option) any later version. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | * GNU General Public License for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU General Public License | |
| 19 | * along with this program; if not, write to the Free Software | |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 | * | |
| 22 | * Author: Neil Brown | |
| 23 | * Email: <neilb@suse.de> | |
| 24 | */ | |
| 25 | ||
| 26 | #include "mdadm.h" | |
| 27 | #include "dlink.h" | |
| 28 | #include "xmalloc.h" | |
| 29 | ||
| 30 | #include <dirent.h> | |
| 31 | #include <ctype.h> | |
| 32 | ||
| 33 | #define MAX_SYSFS_PATH_LEN 120 | |
| 34 | ||
| 35 | struct dev_sysfs_rule { | |
| 36 | struct dev_sysfs_rule *next; | |
| 37 | char *devname; | |
| 38 | int uuid[4]; | |
| 39 | int uuid_set; | |
| 40 | struct sysfs_entry { | |
| 41 | struct sysfs_entry *next; | |
| 42 | char *name; | |
| 43 | char *value; | |
| 44 | } *entry; | |
| 45 | }; | |
| 46 | ||
| 47 | int load_sys(char *path, char *buf, int len) | |
| 48 | { | |
| 49 | int fd = open(path, O_RDONLY); | |
| 50 | int n; | |
| 51 | if (fd < 0) | |
| 52 | return -1; | |
| 53 | n = read(fd, buf, len); | |
| 54 | close(fd); | |
| 55 | if (n <0 || n >= len) | |
| 56 | return -1; | |
| 57 | buf[n] = 0; | |
| 58 | if (n && buf[n-1] == '\n') | |
| 59 | buf[n-1] = 0; | |
| 60 | return 0; | |
| 61 | } | |
| 62 | ||
| 63 | void sysfs_free(struct mdinfo *sra) | |
| 64 | { | |
| 65 | while (sra) { | |
| 66 | struct mdinfo *sra2 = sra->next; | |
| 67 | while (sra->devs) { | |
| 68 | struct mdinfo *d = sra->devs; | |
| 69 | sra->devs = d->next; | |
| 70 | free(d->bb.entries); | |
| 71 | free(d); | |
| 72 | } | |
| 73 | free(sra->bb.entries); | |
| 74 | free(sra); | |
| 75 | sra = sra2; | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | mapping_t sysfs_memb_states[] = { | |
| 80 | {"external_bbl", MEMB_STATE_EXTERNAL_BBL}, | |
| 81 | {"blocked", MEMB_STATE_BLOCKED}, | |
| 82 | {"spare", MEMB_STATE_SPARE}, | |
| 83 | {"write_mostly", MEMB_STATE_WRITE_MOSTLY}, | |
| 84 | {"in_sync", MEMB_STATE_IN_SYNC}, | |
| 85 | {"faulty", MEMB_STATE_FAULTY}, | |
| 86 | {"remove", MEMB_STATE_REMOVE}, | |
| 87 | {NULL, MEMB_STATE_UNKNOWN} | |
| 88 | }; | |
| 89 | ||
| 90 | char *map_memb_state(memb_state_t state) | |
| 91 | { | |
| 92 | return map_num_s(sysfs_memb_states, state); | |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * write_attr() - write value to fd, don't check errno. | |
| 97 | * @attr: value to write. | |
| 98 | * @fd: file descriptor write to. | |
| 99 | * | |
| 100 | * Size to write is calculated by strlen(). | |
| 101 | */ | |
| 102 | mdadm_status_t write_attr(const char *value, const int fd) | |
| 103 | { | |
| 104 | return sysfs_write_descriptor(fd, value, strlen(value), NULL); | |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * sysfs_write_descriptor()- wrapper for write(), projected to be used with sysfs. | |
| 109 | * @fd: file descriptor. | |
| 110 | * @value: value to set. | |
| 111 | * @len: length of the value. | |
| 112 | * @errno_p: On write() failure, buffer to copy errno value, might be NULL. | |
| 113 | * | |
| 114 | * Errors are differentiated, because (at least theoretically) kernel may not process whole string | |
| 115 | * and it may or may not be a problem (it depends on implementation in kernel). Decision belongs to | |
| 116 | * caller then. | |
| 117 | * Generally, it should be safe to check if @errno_p changed to determine if error occurred. | |
| 118 | */ | |
| 119 | mdadm_status_t sysfs_write_descriptor(const int fd, const char *value, const ssize_t len, | |
| 120 | int *errno_p) | |
| 121 | { | |
| 122 | ssize_t ret; | |
| 123 | ||
| 124 | ret = write(fd, value, len); | |
| 125 | if (ret == -1) { | |
| 126 | if (errno_p) | |
| 127 | *errno_p = errno; | |
| 128 | return MDADM_STATUS_ERROR; | |
| 129 | } | |
| 130 | ||
| 131 | if (ret != len) | |
| 132 | return MDADM_STATUS_UNDEF; | |
| 133 | ||
| 134 | return MDADM_STATUS_SUCCESS; | |
| 135 | } | |
| 136 | ||
| 137 | /** | |
| 138 | * sysfs_set_memb_state_fd() - write to md/<memb>/state file. | |
| 139 | * @fd: open file descriptor to the file. | |
| 140 | * @state: enum value describing value to write | |
| 141 | * @err: errno value pointer in case of error. | |
| 142 | * | |
| 143 | * This is helper to avoid inlining values, they are extracted from map now. | |
| 144 | */ | |
| 145 | mdadm_status_t sysfs_set_memb_state_fd(int fd, memb_state_t state, int *err) | |
| 146 | { | |
| 147 | const char *val = map_memb_state(state); | |
| 148 | ||
| 149 | return sysfs_write_descriptor(fd, val, strlen(val), err); | |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * sysfs_set_memb_state() - write to member disk state file. | |
| 154 | * @array_devnm: kernel name of the array. | |
| 155 | * @memb_devnm: kernel name of member device. | |
| 156 | * @state: value to write. | |
| 157 | * | |
| 158 | * Function expects that the device exists, error is unconditionally printed. | |
| 159 | */ | |
| 160 | mdadm_status_t sysfs_set_memb_state(char *array_devnm, char *memb_devnm, memb_state_t state) | |
| 161 | { | |
| 162 | int state_fd = sysfs_open_memb_attr(array_devnm, memb_devnm, "state", O_RDWR); | |
| 163 | ||
| 164 | if (!is_fd_valid(state_fd)) { | |
| 165 | pr_err("Cannot open file descriptor to %s in array %s, aborting.\n", | |
| 166 | memb_devnm, array_devnm); | |
| 167 | return MDADM_STATUS_ERROR; | |
| 168 | } | |
| 169 | ||
| 170 | return sysfs_set_memb_state_fd(state_fd, state, NULL); | |
| 171 | ||
| 172 | close_fd(&state_fd); | |
| 173 | } | |
| 174 | ||
| 175 | /** | |
| 176 | * sysfs_get_container_devnm() - extract container device name. | |
| 177 | * @mdi: md_info describes member array, with GET_VERSION option. | |
| 178 | * @buf: buf to fill, must be MD_NAME_MAX. | |
| 179 | * | |
| 180 | * External array version is in format {/,-}<container_devnm>/<array_index> | |
| 181 | * Extract container_devnm from it and safe it in @buf. | |
| 182 | */ | |
| 183 | void sysfs_get_container_devnm(struct mdinfo *mdi, char *buf) | |
| 184 | { | |
| 185 | char *p; | |
| 186 | ||
| 187 | assert(is_subarray(mdi->text_version)); | |
| 188 | ||
| 189 | /* Skip first special sign */ | |
| 190 | snprintf(buf, MD_NAME_MAX, "%s", mdi->text_version + 1); | |
| 191 | ||
| 192 | /* Remove array index */ | |
| 193 | p = strchr(buf, '/'); | |
| 194 | if (p) | |
| 195 | *p = 0; | |
| 196 | } | |
| 197 | ||
| 198 | /** | |
| 199 | * sysfs_open_memb_attr() - helper to get sysfs attr descriptor for member device. | |
| 200 | * @array_devnm: array kernel device name. | |
| 201 | * @memb_devnm: member device kernel device name. | |
| 202 | * @attr: requested sysfs attribute. | |
| 203 | * @oflag: open() flags. | |
| 204 | * | |
| 205 | * To refer member device directory, we need to append "dev-" before the member device name. | |
| 206 | */ | |
| 207 | int sysfs_open_memb_attr(char *array_devnm, char *memb_devnm, char *attr, int oflag) | |
| 208 | { | |
| 209 | char path[PATH_MAX]; | |
| 210 | ||
| 211 | snprintf(path, PATH_MAX, "/sys/block/%s/md/dev-%s/%s", array_devnm, memb_devnm, attr); | |
| 212 | ||
| 213 | return open(path, oflag); | |
| 214 | } | |
| 215 | ||
| 216 | int sysfs_open(char *devnm, char *devname, char *attr) | |
| 217 | { | |
| 218 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 219 | int fd; | |
| 220 | ||
| 221 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/", devnm); | |
| 222 | if (devname) { | |
| 223 | strncat(fname, devname, MAX_SYSFS_PATH_LEN - strlen(fname)); | |
| 224 | strncat(fname, "/", MAX_SYSFS_PATH_LEN - strlen(fname)); | |
| 225 | } | |
| 226 | strncat(fname, attr, MAX_SYSFS_PATH_LEN - strlen(fname)); | |
| 227 | fd = open(fname, O_RDWR); | |
| 228 | if (fd < 0 && errno == EACCES) | |
| 229 | fd = open(fname, O_RDONLY); | |
| 230 | return fd; | |
| 231 | } | |
| 232 | ||
| 233 | void sysfs_init_dev(struct mdinfo *mdi, dev_t devid) | |
| 234 | { | |
| 235 | snprintf(mdi->sys_name, | |
| 236 | sizeof(mdi->sys_name), "dev-%s", devid2kname(devid)); | |
| 237 | } | |
| 238 | ||
| 239 | int sysfs_init(struct mdinfo *mdi, int fd, char *devnm) | |
| 240 | { | |
| 241 | struct stat stb; | |
| 242 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 243 | int retval = -ENODEV; | |
| 244 | ||
| 245 | mdi->sys_name[0] = 0; | |
| 246 | if (fd >= 0) | |
| 247 | devnm = fd2devnm(fd); | |
| 248 | ||
| 249 | if (devnm == NULL) | |
| 250 | goto out; | |
| 251 | ||
| 252 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm); | |
| 253 | ||
| 254 | if (stat(fname, &stb)) | |
| 255 | goto out; | |
| 256 | if (!S_ISDIR(stb.st_mode)) | |
| 257 | goto out; | |
| 258 | strncpy(mdi->sys_name, devnm, sizeof(mdi->sys_name) - 1); | |
| 259 | ||
| 260 | retval = 0; | |
| 261 | out: | |
| 262 | return retval; | |
| 263 | } | |
| 264 | ||
| 265 | /* If fd >= 0, get the array it is open on, else use devnm. */ | |
| 266 | struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options) | |
| 267 | { | |
| 268 | char fname[PATH_MAX]; | |
| 269 | char buf[PATH_MAX]; | |
| 270 | char *base; | |
| 271 | char *dbase; | |
| 272 | struct mdinfo *sra; | |
| 273 | struct mdinfo *dev, **devp; | |
| 274 | DIR *dir = NULL; | |
| 275 | struct dirent *de; | |
| 276 | ||
| 277 | sra = xcalloc(1, sizeof(*sra)); | |
| 278 | if (sysfs_init(sra, fd, devnm)) { | |
| 279 | free(sra); | |
| 280 | return NULL; | |
| 281 | } | |
| 282 | ||
| 283 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); | |
| 284 | base = fname + strlen(fname); | |
| 285 | ||
| 286 | sra->devs = NULL; | |
| 287 | if (options & GET_VERSION) { | |
| 288 | strcpy(base, "metadata_version"); | |
| 289 | if (load_sys(fname, buf, sizeof(buf))) | |
| 290 | goto abort; | |
| 291 | if (str_is_none(buf) == true) { | |
| 292 | sra->array.major_version = | |
| 293 | sra->array.minor_version = -1; | |
| 294 | strcpy(sra->text_version, ""); | |
| 295 | } else if (strncmp(buf, "external:", 9) == 0) { | |
| 296 | sra->array.major_version = -1; | |
| 297 | sra->array.minor_version = -2; | |
| 298 | strcpy(sra->text_version, buf+9); | |
| 299 | sra->text_version[sizeof(sra->text_version) - 1] = '\0'; | |
| 300 | } else { | |
| 301 | sscanf(buf, "%d.%d", | |
| 302 | &sra->array.major_version, | |
| 303 | &sra->array.minor_version); | |
| 304 | strcpy(sra->text_version, buf); | |
| 305 | } | |
| 306 | } | |
| 307 | if (options & GET_LEVEL) { | |
| 308 | strcpy(base, "level"); | |
| 309 | if (load_sys(fname, buf, sizeof(buf))) | |
| 310 | goto abort; | |
| 311 | sra->array.level = map_name(pers, buf); | |
| 312 | } | |
| 313 | if (options & GET_LAYOUT) { | |
| 314 | strcpy(base, "layout"); | |
| 315 | if (load_sys(fname, buf, sizeof(buf))) | |
| 316 | goto abort; | |
| 317 | sra->array.layout = strtoul(buf, NULL, 0); | |
| 318 | } | |
| 319 | if (options & (GET_DISKS|GET_STATE)) { | |
| 320 | strcpy(base, "raid_disks"); | |
| 321 | if (load_sys(fname, buf, sizeof(buf))) | |
| 322 | goto abort; | |
| 323 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
| 324 | } | |
| 325 | if (options & GET_COMPONENT) { | |
| 326 | strcpy(base, "component_size"); | |
| 327 | if (load_sys(fname, buf, sizeof(buf))) | |
| 328 | goto abort; | |
| 329 | sra->component_size = strtoull(buf, NULL, 0); | |
| 330 | /* sysfs reports "K", but we want sectors */ | |
| 331 | sra->component_size *= 2; | |
| 332 | } | |
| 333 | if (options & GET_CHUNK) { | |
| 334 | strcpy(base, "chunk_size"); | |
| 335 | if (load_sys(fname, buf, sizeof(buf))) | |
| 336 | goto abort; | |
| 337 | sra->array.chunk_size = strtoul(buf, NULL, 0); | |
| 338 | } | |
| 339 | if (options & GET_CACHE) { | |
| 340 | strcpy(base, "stripe_cache_size"); | |
| 341 | if (load_sys(fname, buf, sizeof(buf))) | |
| 342 | /* Probably level doesn't support it */ | |
| 343 | sra->cache_size = 0; | |
| 344 | else | |
| 345 | sra->cache_size = strtoul(buf, NULL, 0); | |
| 346 | } | |
| 347 | if (options & GET_MISMATCH) { | |
| 348 | strcpy(base, "mismatch_cnt"); | |
| 349 | if (load_sys(fname, buf, sizeof(buf))) | |
| 350 | goto abort; | |
| 351 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
| 352 | } | |
| 353 | if (options & GET_SAFEMODE) { | |
| 354 | int scale = 1; | |
| 355 | int dot = 0; | |
| 356 | unsigned i; | |
| 357 | unsigned long msec; | |
| 358 | size_t len; | |
| 359 | ||
| 360 | strcpy(base, "safe_mode_delay"); | |
| 361 | if (load_sys(fname, buf, sizeof(buf))) | |
| 362 | goto abort; | |
| 363 | ||
| 364 | /* remove a period, and count digits after it */ | |
| 365 | len = strlen(buf); | |
| 366 | for (i = 0; i < len; i++) { | |
| 367 | if (dot) { | |
| 368 | if (isdigit(buf[i])) { | |
| 369 | buf[i-1] = buf[i]; | |
| 370 | scale *= 10; | |
| 371 | } | |
| 372 | buf[i] = 0; | |
| 373 | } else if (buf[i] == '.') { | |
| 374 | dot=1; | |
| 375 | buf[i] = 0; | |
| 376 | } | |
| 377 | } | |
| 378 | msec = strtoul(buf, NULL, 10); | |
| 379 | msec = (msec * 1000) / scale; | |
| 380 | sra->safe_mode_delay = msec; | |
| 381 | } | |
| 382 | if (options & GET_BITMAP_LOCATION) { | |
| 383 | strcpy(base, "bitmap/location"); | |
| 384 | if (load_sys(fname, buf, sizeof(buf))) | |
| 385 | goto abort; | |
| 386 | if (strncmp(buf, "file", 4) == 0) | |
| 387 | sra->bitmap_offset = 1; | |
| 388 | else if (str_is_none(buf) == true) | |
| 389 | sra->bitmap_offset = 0; | |
| 390 | else if (buf[0] == '+') | |
| 391 | sra->bitmap_offset = strtol(buf+1, NULL, 10); | |
| 392 | else | |
| 393 | goto abort; | |
| 394 | } | |
| 395 | ||
| 396 | if (options & GET_ARRAY_STATE) { | |
| 397 | strcpy(base, "array_state"); | |
| 398 | if (load_sys(fname, buf, sizeof(buf))) | |
| 399 | goto abort; | |
| 400 | sra->array_state = map_name(sysfs_array_states, buf); | |
| 401 | } | |
| 402 | ||
| 403 | if (options & GET_CONSISTENCY_POLICY) { | |
| 404 | strcpy(base, "consistency_policy"); | |
| 405 | if (load_sys(fname, buf, sizeof(buf))) | |
| 406 | sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN; | |
| 407 | else | |
| 408 | sra->consistency_policy = map_name(consistency_policies, | |
| 409 | buf); | |
| 410 | } | |
| 411 | ||
| 412 | if (! (options & GET_DEVS)) | |
| 413 | return sra; | |
| 414 | ||
| 415 | /* Get all the devices as well */ | |
| 416 | *base = 0; | |
| 417 | dir = opendir(fname); | |
| 418 | if (!dir) | |
| 419 | goto abort; | |
| 420 | sra->array.spare_disks = 0; | |
| 421 | sra->array.active_disks = 0; | |
| 422 | sra->array.failed_disks = 0; | |
| 423 | sra->array.working_disks = 0; | |
| 424 | ||
| 425 | devp = &sra->devs; | |
| 426 | sra->devs = NULL; | |
| 427 | while ((de = readdir(dir)) != NULL) { | |
| 428 | char *ep; | |
| 429 | if (de->d_ino == 0 || | |
| 430 | strncmp(de->d_name, "dev-", 4) != 0) | |
| 431 | continue; | |
| 432 | strcpy(base, de->d_name); | |
| 433 | dbase = base + strlen(base); | |
| 434 | *dbase++ = '/'; | |
| 435 | ||
| 436 | dev = xcalloc(1, sizeof(*dev)); | |
| 437 | ||
| 438 | /* Always get slot, major, minor */ | |
| 439 | strcpy(dbase, "slot"); | |
| 440 | if (load_sys(fname, buf, sizeof(buf))) { | |
| 441 | /* hmm... unable to read 'slot' maybe the device | |
| 442 | * is going away? | |
| 443 | */ | |
| 444 | strcpy(dbase, "block"); | |
| 445 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
| 446 | errno != ENAMETOOLONG) { | |
| 447 | /* ...yup device is gone */ | |
| 448 | free(dev); | |
| 449 | continue; | |
| 450 | } else { | |
| 451 | /* slot is unreadable but 'block' link | |
| 452 | * still intact... something bad is happening | |
| 453 | * so abort | |
| 454 | */ | |
| 455 | free(dev); | |
| 456 | goto abort; | |
| 457 | } | |
| 458 | ||
| 459 | } | |
| 460 | strcpy(dev->sys_name, de->d_name); | |
| 461 | dev->sys_name[sizeof(dev->sys_name) - 1] = '\0'; | |
| 462 | dev->disk.raid_disk = strtoul(buf, &ep, 10); | |
| 463 | if (*ep) dev->disk.raid_disk = -1; | |
| 464 | ||
| 465 | sra->array.nr_disks++; | |
| 466 | strcpy(dbase, "block/dev"); | |
| 467 | if (load_sys(fname, buf, sizeof(buf))) { | |
| 468 | /* assume this is a stale reference to a hot | |
| 469 | * removed device | |
| 470 | */ | |
| 471 | if (!(options & GET_DEVS_ALL)) { | |
| 472 | free(dev); | |
| 473 | continue; | |
| 474 | } | |
| 475 | } else { | |
| 476 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); | |
| 477 | } | |
| 478 | ||
| 479 | if (!(options & GET_DEVS_ALL)) { | |
| 480 | /* special case check for block devices that can go 'offline' */ | |
| 481 | strcpy(dbase, "block/device/state"); | |
| 482 | if (load_sys(fname, buf, sizeof(buf)) == 0 && | |
| 483 | strncmp(buf, "offline", 7) == 0) { | |
| 484 | free(dev); | |
| 485 | continue; | |
| 486 | } | |
| 487 | } | |
| 488 | ||
| 489 | /* finally add this disk to the array */ | |
| 490 | *devp = dev; | |
| 491 | devp = & dev->next; | |
| 492 | dev->next = NULL; | |
| 493 | ||
| 494 | if (options & GET_OFFSET) { | |
| 495 | strcpy(dbase, "offset"); | |
| 496 | if (load_sys(fname, buf, sizeof(buf))) | |
| 497 | goto abort; | |
| 498 | dev->data_offset = strtoull(buf, NULL, 0); | |
| 499 | strcpy(dbase, "new_offset"); | |
| 500 | if (load_sys(fname, buf, sizeof(buf)) == 0) | |
| 501 | dev->new_data_offset = strtoull(buf, NULL, 0); | |
| 502 | else | |
| 503 | dev->new_data_offset = dev->data_offset; | |
| 504 | } | |
| 505 | if (options & GET_SIZE) { | |
| 506 | strcpy(dbase, "size"); | |
| 507 | if (load_sys(fname, buf, sizeof(buf))) | |
| 508 | goto abort; | |
| 509 | dev->component_size = strtoull(buf, NULL, 0) * 2; | |
| 510 | } | |
| 511 | if (options & GET_STATE) { | |
| 512 | dev->disk.state = 0; | |
| 513 | strcpy(dbase, "state"); | |
| 514 | if (load_sys(fname, buf, sizeof(buf))) | |
| 515 | goto abort; | |
| 516 | if (strstr(buf, "faulty")) | |
| 517 | dev->disk.state |= (1<<MD_DISK_FAULTY); | |
| 518 | else { | |
| 519 | sra->array.working_disks++; | |
| 520 | if (strstr(buf, "in_sync")) { | |
| 521 | dev->disk.state |= (1<<MD_DISK_SYNC); | |
| 522 | sra->array.active_disks++; | |
| 523 | } | |
| 524 | if (dev->disk.state == 0) | |
| 525 | sra->array.spare_disks++; | |
| 526 | } | |
| 527 | } | |
| 528 | if (options & GET_ERROR) { | |
| 529 | strcpy(buf, "errors"); | |
| 530 | if (load_sys(fname, buf, sizeof(buf))) | |
| 531 | goto abort; | |
| 532 | dev->errors = strtoul(buf, NULL, 0); | |
| 533 | } | |
| 534 | } | |
| 535 | ||
| 536 | if ((options & GET_STATE) && sra->array.raid_disks) | |
| 537 | sra->array.failed_disks = sra->array.raid_disks - | |
| 538 | sra->array.active_disks - sra->array.spare_disks; | |
| 539 | ||
| 540 | closedir(dir); | |
| 541 | return sra; | |
| 542 | ||
| 543 | abort: | |
| 544 | if (dir) | |
| 545 | closedir(dir); | |
| 546 | sysfs_free(sra); | |
| 547 | return NULL; | |
| 548 | } | |
| 549 | ||
| 550 | int sysfs_attr_match(const char *attr, const char *str) | |
| 551 | { | |
| 552 | /* See if attr, read from a sysfs file, matches | |
| 553 | * str. They must either be the same, or attr can | |
| 554 | * have a trailing newline or comma | |
| 555 | */ | |
| 556 | while (*attr && *str && *attr == *str) { | |
| 557 | attr++; | |
| 558 | str++; | |
| 559 | } | |
| 560 | ||
| 561 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
| 562 | return 0; | |
| 563 | return 1; | |
| 564 | } | |
| 565 | ||
| 566 | int sysfs_match_word(const char *word, char **list) | |
| 567 | { | |
| 568 | int n; | |
| 569 | for (n=0; list[n]; n++) | |
| 570 | if (sysfs_attr_match(word, list[n])) | |
| 571 | break; | |
| 572 | return n; | |
| 573 | } | |
| 574 | ||
| 575 | unsigned long long get_component_size(int fd) | |
| 576 | { | |
| 577 | /* Find out the component size of the array. | |
| 578 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
| 579 | * size field is only 32bits. | |
| 580 | * So look in /sys/block/mdXXX/md/component_size | |
| 581 | * | |
| 582 | * This returns in units of sectors. | |
| 583 | */ | |
| 584 | struct stat stb; | |
| 585 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 586 | int n; | |
| 587 | if (fstat(fd, &stb)) | |
| 588 | return 0; | |
| 589 | snprintf(fname, MAX_SYSFS_PATH_LEN, | |
| 590 | "/sys/block/%s/md/component_size", stat2devnm(&stb)); | |
| 591 | fd = open(fname, O_RDONLY); | |
| 592 | if (fd < 0) | |
| 593 | return 0; | |
| 594 | n = read(fd, fname, sizeof(fname)); | |
| 595 | close(fd); | |
| 596 | if (n < 0 || n == sizeof(fname)) | |
| 597 | return 0; | |
| 598 | fname[n] = 0; | |
| 599 | return strtoull(fname, NULL, 10) * 2; | |
| 600 | } | |
| 601 | ||
| 602 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, | |
| 603 | char *name, char *val) | |
| 604 | { | |
| 605 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 606 | int fd; | |
| 607 | ||
| 608 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", | |
| 609 | sra->sys_name, dev?dev->sys_name:"", name); | |
| 610 | fd = open(fname, O_WRONLY); | |
| 611 | if (fd < 0) | |
| 612 | return -1; | |
| 613 | ||
| 614 | if (write_attr(val, fd)) { | |
| 615 | pr_err("failed to write '%s' to '%s' (%s)\n", val, fname, strerror(errno)); | |
| 616 | close(fd); | |
| 617 | return -1; | |
| 618 | } | |
| 619 | ||
| 620 | close(fd); | |
| 621 | return 0; | |
| 622 | } | |
| 623 | ||
| 624 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, | |
| 625 | char *name, unsigned long long val) | |
| 626 | { | |
| 627 | char valstr[50]; | |
| 628 | sprintf(valstr, "%llu", val); | |
| 629 | return sysfs_set_str(sra, dev, name, valstr); | |
| 630 | } | |
| 631 | ||
| 632 | int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev, | |
| 633 | char *name, long long val) | |
| 634 | { | |
| 635 | char valstr[50]; | |
| 636 | sprintf(valstr, "%lli", val); | |
| 637 | return sysfs_set_str(sra, dev, name, valstr); | |
| 638 | } | |
| 639 | ||
| 640 | int sysfs_uevent(struct mdinfo *sra, char *event) | |
| 641 | { | |
| 642 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 643 | int fd; | |
| 644 | ||
| 645 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/uevent", | |
| 646 | sra->sys_name); | |
| 647 | fd = open(fname, O_WRONLY); | |
| 648 | if (fd < 0) | |
| 649 | return -1; | |
| 650 | ||
| 651 | if (write_attr(event, fd)) { | |
| 652 | pr_err("failed to write '%s' to '%s' (%s)\n", event, fname, strerror(errno)); | |
| 653 | close(fd); | |
| 654 | return -1; | |
| 655 | } | |
| 656 | ||
| 657 | close(fd); | |
| 658 | return 0; | |
| 659 | } | |
| 660 | ||
| 661 | int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name) | |
| 662 | { | |
| 663 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 664 | struct stat st; | |
| 665 | ||
| 666 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", | |
| 667 | sra->sys_name, dev?dev->sys_name:"", name); | |
| 668 | ||
| 669 | return stat(fname, &st) == 0; | |
| 670 | } | |
| 671 | ||
| 672 | int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev, | |
| 673 | char *name) | |
| 674 | { | |
| 675 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 676 | int fd; | |
| 677 | ||
| 678 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", | |
| 679 | sra->sys_name, dev?dev->sys_name:"", name); | |
| 680 | fd = open(fname, O_RDWR); | |
| 681 | if (fd < 0) | |
| 682 | fd = open(fname, O_RDONLY); | |
| 683 | return fd; | |
| 684 | } | |
| 685 | ||
| 686 | int sysfs_fd_get_ll(int fd, unsigned long long *val) | |
| 687 | { | |
| 688 | char buf[50]; | |
| 689 | int n; | |
| 690 | char *ep; | |
| 691 | ||
| 692 | lseek(fd, 0, 0); | |
| 693 | n = read(fd, buf, sizeof(buf)); | |
| 694 | if (n <= 0 || n == sizeof(buf)) | |
| 695 | return -2; | |
| 696 | buf[n] = 0; | |
| 697 | *val = strtoull(buf, &ep, 0); | |
| 698 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
| 699 | return -1; | |
| 700 | return 0; | |
| 701 | } | |
| 702 | ||
| 703 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, | |
| 704 | char *name, unsigned long long *val) | |
| 705 | { | |
| 706 | int n; | |
| 707 | int fd; | |
| 708 | ||
| 709 | fd = sysfs_get_fd(sra, dev, name); | |
| 710 | if (fd < 0) | |
| 711 | return -1; | |
| 712 | n = sysfs_fd_get_ll(fd, val); | |
| 713 | close(fd); | |
| 714 | return n; | |
| 715 | } | |
| 716 | ||
| 717 | int sysfs_fd_get_two(int fd, unsigned long long *v1, unsigned long long *v2) | |
| 718 | { | |
| 719 | /* two numbers in this sysfs file, either | |
| 720 | * NNN (NNN) | |
| 721 | * or | |
| 722 | * NNN / NNN | |
| 723 | */ | |
| 724 | char buf[80]; | |
| 725 | int n; | |
| 726 | char *ep, *ep2; | |
| 727 | ||
| 728 | lseek(fd, 0, 0); | |
| 729 | n = read(fd, buf, sizeof(buf)); | |
| 730 | if (n <= 0 || n == sizeof(buf)) | |
| 731 | return -2; | |
| 732 | buf[n] = 0; | |
| 733 | *v1 = strtoull(buf, &ep, 0); | |
| 734 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
| 735 | return -1; | |
| 736 | while (*ep == ' ' || *ep == '/' || *ep == '(') | |
| 737 | ep++; | |
| 738 | *v2 = strtoull(ep, &ep2, 0); | |
| 739 | if (ep2 == ep || (*ep2 != 0 && *ep2 != '\n' && *ep2 != ' ' && *ep2 != ')')) { | |
| 740 | *v2 = *v1; | |
| 741 | return 1; | |
| 742 | } | |
| 743 | return 2; | |
| 744 | } | |
| 745 | ||
| 746 | int sysfs_get_two(struct mdinfo *sra, struct mdinfo *dev, | |
| 747 | char *name, unsigned long long *v1, unsigned long long *v2) | |
| 748 | { | |
| 749 | int n; | |
| 750 | int fd; | |
| 751 | ||
| 752 | fd = sysfs_get_fd(sra, dev, name); | |
| 753 | if (fd < 0) | |
| 754 | return -1; | |
| 755 | n = sysfs_fd_get_two(fd, v1, v2); | |
| 756 | close(fd); | |
| 757 | return n; | |
| 758 | } | |
| 759 | ||
| 760 | int sysfs_fd_get_str(int fd, char *val, int size) | |
| 761 | { | |
| 762 | int n; | |
| 763 | ||
| 764 | lseek(fd, 0, 0); | |
| 765 | n = read(fd, val, size); | |
| 766 | if (n <= 0 || n == size) | |
| 767 | return -1; | |
| 768 | val[n] = 0; | |
| 769 | return n; | |
| 770 | } | |
| 771 | ||
| 772 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, | |
| 773 | char *name, char *val, int size) | |
| 774 | { | |
| 775 | int n; | |
| 776 | int fd; | |
| 777 | ||
| 778 | fd = sysfs_get_fd(sra, dev, name); | |
| 779 | if (fd < 0) | |
| 780 | return -1; | |
| 781 | n = sysfs_fd_get_str(fd, val, size); | |
| 782 | close(fd); | |
| 783 | return n; | |
| 784 | } | |
| 785 | ||
| 786 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) | |
| 787 | { | |
| 788 | unsigned long sec; | |
| 789 | unsigned long msec; | |
| 790 | char delay[30]; | |
| 791 | ||
| 792 | sec = ms / 1000; | |
| 793 | msec = ms % 1000; | |
| 794 | ||
| 795 | sprintf(delay, "%ld.%03ld\n", sec, msec); | |
| 796 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
| 797 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); | |
| 798 | } | |
| 799 | ||
| 800 | int sysfs_set_array(struct mdinfo *info) | |
| 801 | { | |
| 802 | int rv = 0; | |
| 803 | char ver[100]; | |
| 804 | int raid_disks = info->array.raid_disks; | |
| 805 | ||
| 806 | ver[0] = 0; | |
| 807 | if (info->array.major_version == -1 && | |
| 808 | info->array.minor_version == -2) { | |
| 809 | char buf[SYSFS_MAX_BUF_SIZE]; | |
| 810 | ||
| 811 | strcat(strcpy(ver, "external:"), info->text_version); | |
| 812 | ||
| 813 | /* meta version might already be set if we are setting | |
| 814 | * new geometry for a reshape. In that case we don't | |
| 815 | * want to over-write the 'readonly' flag that is | |
| 816 | * stored in the metadata version. So read the current | |
| 817 | * version first, and preserve the flag | |
| 818 | */ | |
| 819 | if (sysfs_get_str(info, NULL, "metadata_version", | |
| 820 | buf, sizeof(buf)) > 0) | |
| 821 | if (strlen(buf) >= 9 && buf[9] == '-') | |
| 822 | ver[9] = '-'; | |
| 823 | ||
| 824 | if (sysfs_set_str(info, NULL, "metadata_version", ver) < 0) { | |
| 825 | pr_err("This kernel does not support external metadata.\n"); | |
| 826 | return 1; | |
| 827 | } | |
| 828 | } | |
| 829 | if (info->array.level < 0) | |
| 830 | return 0; /* FIXME */ | |
| 831 | rv |= sysfs_set_str(info, NULL, "level", | |
| 832 | map_num_s(pers, info->array.level)); | |
| 833 | if (info->reshape_active && info->delta_disks != UnSet) | |
| 834 | raid_disks -= info->delta_disks; | |
| 835 | rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks); | |
| 836 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); | |
| 837 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
| 838 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
| 839 | if (info->custom_array_size) { | |
| 840 | int rc; | |
| 841 | ||
| 842 | rc = sysfs_set_num(info, NULL, "array_size", | |
| 843 | info->custom_array_size/2); | |
| 844 | if (rc && errno == ENOENT) { | |
| 845 | pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n"); | |
| 846 | rc = 0; | |
| 847 | } | |
| 848 | rv |= rc; | |
| 849 | } | |
| 850 | ||
| 851 | if (info->array.level > 0) | |
| 852 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
| 853 | ||
| 854 | if (info->reshape_active) { | |
| 855 | rv |= sysfs_set_num(info, NULL, "reshape_position", | |
| 856 | info->reshape_progress); | |
| 857 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk); | |
| 858 | rv |= sysfs_set_num(info, NULL, "layout", info->new_layout); | |
| 859 | rv |= sysfs_set_num(info, NULL, "raid_disks", | |
| 860 | info->array.raid_disks); | |
| 861 | /* We don't set 'new_level' here. That can only happen | |
| 862 | * once the reshape completes. | |
| 863 | */ | |
| 864 | } | |
| 865 | ||
| 866 | if (info->consistency_policy == CONSISTENCY_POLICY_PPL) { | |
| 867 | char *policy = map_num_s(consistency_policies, | |
| 868 | info->consistency_policy); | |
| 869 | ||
| 870 | if (sysfs_set_str(info, NULL, "consistency_policy", policy)) { | |
| 871 | pr_err("This kernel does not support PPL. Falling back to consistency-policy=resync.\n"); | |
| 872 | info->consistency_policy = CONSISTENCY_POLICY_RESYNC; | |
| 873 | } | |
| 874 | } | |
| 875 | ||
| 876 | return rv; | |
| 877 | } | |
| 878 | ||
| 879 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume) | |
| 880 | { | |
| 881 | char dv[PATH_MAX]; | |
| 882 | char nm[PATH_MAX]; | |
| 883 | char *dname; | |
| 884 | int rv; | |
| 885 | int i; | |
| 886 | ||
| 887 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
| 888 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
| 889 | if (rv) | |
| 890 | return rv; | |
| 891 | ||
| 892 | memset(nm, 0, sizeof(nm)); | |
| 893 | dname = devid2kname(makedev(sd->disk.major, sd->disk.minor)); | |
| 894 | ||
| 895 | snprintf(sd->sys_name, sizeof(sd->sys_name), "dev-%s", dname); | |
| 896 | ||
| 897 | /* test write to see if 'recovery_start' is available */ | |
| 898 | if (resume && sd->recovery_start < MaxSector && | |
| 899 | sysfs_set_num(sra, sd, "recovery_start", 0)) { | |
| 900 | sysfs_set_str(sra, sd, "state", "remove"); | |
| 901 | return -1; | |
| 902 | } | |
| 903 | ||
| 904 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); | |
| 905 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); | |
| 906 | if (!is_container(sra->array.level)) { | |
| 907 | if (sra->consistency_policy == CONSISTENCY_POLICY_PPL) { | |
| 908 | rv |= sysfs_set_num(sra, sd, "ppl_sector", sd->ppl_sector); | |
| 909 | rv |= sysfs_set_num(sra, sd, "ppl_size", sd->ppl_size); | |
| 910 | } | |
| 911 | if (sd->recovery_start == MaxSector) | |
| 912 | /* This can correctly fail if array isn't started, | |
| 913 | * yet, so just ignore status for now. | |
| 914 | */ | |
| 915 | sysfs_set_str(sra, sd, "state", "insync"); | |
| 916 | if (sd->disk.raid_disk >= 0) | |
| 917 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
| 918 | if (resume) | |
| 919 | sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start); | |
| 920 | } | |
| 921 | if (sd->bb.supported) { | |
| 922 | if (sysfs_set_str(sra, sd, "state", "external_bbl")) { | |
| 923 | /* | |
| 924 | * backward compatibility - if kernel doesn't support | |
| 925 | * bad blocks for external metadata, let it continue | |
| 926 | * as long as there are none known so far | |
| 927 | */ | |
| 928 | if (sd->bb.count) { | |
| 929 | pr_err("The kernel has no support for bad blocks in external metadata\n"); | |
| 930 | return -1; | |
| 931 | } | |
| 932 | } | |
| 933 | ||
| 934 | for (i = 0; i < sd->bb.count; i++) { | |
| 935 | char s[30]; | |
| 936 | const struct md_bb_entry *entry = &sd->bb.entries[i]; | |
| 937 | ||
| 938 | snprintf(s, sizeof(s) - 1, "%llu %d\n", entry->sector, | |
| 939 | entry->length); | |
| 940 | rv |= sysfs_set_str(sra, sd, "bad_blocks", s); | |
| 941 | } | |
| 942 | } | |
| 943 | return rv; | |
| 944 | } | |
| 945 | ||
| 946 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) | |
| 947 | { | |
| 948 | /* from an open block device, try to retrieve it scsi_id */ | |
| 949 | struct stat st; | |
| 950 | char path[256]; | |
| 951 | DIR *dir; | |
| 952 | struct dirent *de; | |
| 953 | int host, bus, target, lun; | |
| 954 | ||
| 955 | if (fstat(fd, &st)) | |
| 956 | return 1; | |
| 957 | ||
| 958 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device", | |
| 959 | major(st.st_rdev), minor(st.st_rdev)); | |
| 960 | ||
| 961 | dir = opendir(path); | |
| 962 | if (!dir) | |
| 963 | return 1; | |
| 964 | ||
| 965 | for (de = readdir(dir); de; de = readdir(dir)) { | |
| 966 | int count; | |
| 967 | ||
| 968 | if (de->d_type != DT_DIR) | |
| 969 | continue; | |
| 970 | ||
| 971 | count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun); | |
| 972 | if (count == 4) | |
| 973 | break; | |
| 974 | } | |
| 975 | closedir(dir); | |
| 976 | ||
| 977 | if (!de) | |
| 978 | return 1; | |
| 979 | ||
| 980 | *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0); | |
| 981 | return 0; | |
| 982 | } | |
| 983 | ||
| 984 | int sysfs_unique_holder(char *devnm, long rdev) | |
| 985 | { | |
| 986 | /* Check that devnm is a holder of rdev, | |
| 987 | * and is the only holder. | |
| 988 | * we should be locked against races by | |
| 989 | * an O_EXCL on devnm | |
| 990 | * Return values: | |
| 991 | * 0 - not unique, not even a holder | |
| 992 | * 1 - unique, this is the only holder. | |
| 993 | * 2/3 - not unique, there is another holder | |
| 994 | * -1 - error, cannot find the holders | |
| 995 | */ | |
| 996 | DIR *dir; | |
| 997 | struct dirent *de; | |
| 998 | char dirname[100]; | |
| 999 | char l; | |
| 1000 | int ret = 0; | |
| 1001 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", | |
| 1002 | major(rdev), minor(rdev)); | |
| 1003 | dir = opendir(dirname); | |
| 1004 | if (!dir) | |
| 1005 | return -1; | |
| 1006 | l = strlen(dirname); | |
| 1007 | while ((de = readdir(dir)) != NULL) { | |
| 1008 | char buf[100]; | |
| 1009 | char *sl; | |
| 1010 | int n; | |
| 1011 | ||
| 1012 | if (de->d_ino == 0) | |
| 1013 | continue; | |
| 1014 | if (de->d_name[0] == '.') | |
| 1015 | continue; | |
| 1016 | strcpy(dirname+l, "/"); | |
| 1017 | strcat(dirname+l, de->d_name); | |
| 1018 | n = readlink(dirname, buf, sizeof(buf)-1); | |
| 1019 | if (n <= 0) | |
| 1020 | continue; | |
| 1021 | buf[n] = 0; | |
| 1022 | sl = strrchr(buf, '/'); | |
| 1023 | if (!sl) | |
| 1024 | continue; | |
| 1025 | sl++; | |
| 1026 | ||
| 1027 | if (strcmp(devnm, sl) == 0) | |
| 1028 | ret |= 1; | |
| 1029 | else | |
| 1030 | ret |= 2; | |
| 1031 | } | |
| 1032 | closedir(dir); | |
| 1033 | return ret; | |
| 1034 | } | |
| 1035 | ||
| 1036 | int sysfs_freeze_array(struct mdinfo *sra) | |
| 1037 | { | |
| 1038 | /* Try to freeze resync/rebuild on this array/container. | |
| 1039 | * Return -1 if the array is busy, | |
| 1040 | * return 0 if this kernel doesn't support 'frozen' | |
| 1041 | * return 1 if it worked. | |
| 1042 | */ | |
| 1043 | char buf[SYSFS_MAX_BUF_SIZE]; | |
| 1044 | ||
| 1045 | if (!sysfs_attribute_available(sra, NULL, "sync_action")) | |
| 1046 | return 1; /* no sync_action == frozen */ | |
| 1047 | if (sysfs_get_str(sra, NULL, "sync_action", buf, sizeof(buf)) <= 0) | |
| 1048 | return 0; | |
| 1049 | if (strcmp(buf, "frozen\n") == 0) | |
| 1050 | /* Already frozen */ | |
| 1051 | return 0; | |
| 1052 | if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0) | |
| 1053 | return -1; | |
| 1054 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
| 1055 | return 0; | |
| 1056 | return 1; | |
| 1057 | } | |
| 1058 | ||
| 1059 | int sysfs_wait(int fd, int *msec) | |
| 1060 | { | |
| 1061 | /* Wait up to '*msec' for fd to have an exception condition. | |
| 1062 | * if msec == NULL, wait indefinitely. | |
| 1063 | */ | |
| 1064 | fd_set fds; | |
| 1065 | int n; | |
| 1066 | FD_ZERO(&fds); | |
| 1067 | FD_SET(fd, &fds); | |
| 1068 | if (msec == NULL) | |
| 1069 | n = select(fd+1, NULL, NULL, &fds, NULL); | |
| 1070 | else if (*msec < 0) | |
| 1071 | n = 0; | |
| 1072 | else { | |
| 1073 | struct timeval start, end, tv; | |
| 1074 | gettimeofday(&start, NULL); | |
| 1075 | if (*msec < 1000) { | |
| 1076 | tv.tv_sec = 0; | |
| 1077 | tv.tv_usec = (*msec)*1000; | |
| 1078 | } else { | |
| 1079 | tv.tv_sec = (*msec)/1000; | |
| 1080 | tv.tv_usec = 0; | |
| 1081 | } | |
| 1082 | n = select(fd+1, NULL, NULL, &fds, &tv); | |
| 1083 | gettimeofday(&end, NULL); | |
| 1084 | end.tv_sec -= start.tv_sec; | |
| 1085 | *msec -= (end.tv_sec * 1000 + end.tv_usec/1000 | |
| 1086 | - start.tv_usec/1000) + 1; | |
| 1087 | } | |
| 1088 | return n; | |
| 1089 | } | |
| 1090 | ||
| 1091 | int sysfs_rules_apply_check(const struct mdinfo *sra, | |
| 1092 | const struct sysfs_entry *ent) | |
| 1093 | { | |
| 1094 | /* Check whether parameter is regular file, | |
| 1095 | * exists and is under specified directory. | |
| 1096 | */ | |
| 1097 | char fname[MAX_SYSFS_PATH_LEN]; | |
| 1098 | char dname[MAX_SYSFS_PATH_LEN]; | |
| 1099 | char resolved_path[PATH_MAX]; | |
| 1100 | char resolved_dir[PATH_MAX]; | |
| 1101 | int result; | |
| 1102 | ||
| 1103 | if (sra == NULL || ent == NULL) | |
| 1104 | return -1; | |
| 1105 | ||
| 1106 | result = snprintf(dname, MAX_SYSFS_PATH_LEN, | |
| 1107 | "/sys/block/%s/md/", sra->sys_name); | |
| 1108 | if (result < 0 || result >= MAX_SYSFS_PATH_LEN) | |
| 1109 | return -1; | |
| 1110 | ||
| 1111 | result = snprintf(fname, MAX_SYSFS_PATH_LEN, | |
| 1112 | "%s/%s", dname, ent->name); | |
| 1113 | if (result < 0 || result >= MAX_SYSFS_PATH_LEN) | |
| 1114 | return -1; | |
| 1115 | ||
| 1116 | if (realpath(fname, resolved_path) == NULL || | |
| 1117 | realpath(dname, resolved_dir) == NULL) | |
| 1118 | return -1; | |
| 1119 | ||
| 1120 | if (strncmp(resolved_dir, resolved_path, | |
| 1121 | strnlen(resolved_dir, PATH_MAX)) != 0) | |
| 1122 | return -1; | |
| 1123 | ||
| 1124 | return 0; | |
| 1125 | } | |
| 1126 | ||
| 1127 | static struct dev_sysfs_rule *sysfs_rules; | |
| 1128 | ||
| 1129 | void sysfs_rules_apply(char *devnm, struct mdinfo *dev) | |
| 1130 | { | |
| 1131 | struct dev_sysfs_rule *rules = sysfs_rules; | |
| 1132 | ||
| 1133 | while (rules) { | |
| 1134 | struct sysfs_entry *ent = rules->entry; | |
| 1135 | int match = 0; | |
| 1136 | ||
| 1137 | if (!rules->uuid_set) { | |
| 1138 | if (rules->devname) | |
| 1139 | match = strcmp(devnm, rules->devname) == 0; | |
| 1140 | } else { | |
| 1141 | match = memcmp(dev->uuid, rules->uuid, | |
| 1142 | sizeof(int[4])) == 0; | |
| 1143 | } | |
| 1144 | ||
| 1145 | while (match && ent) { | |
| 1146 | if (sysfs_rules_apply_check(dev, ent) < 0) | |
| 1147 | pr_err("SYSFS: failed to write '%s' to '%s'\n", | |
| 1148 | ent->value, ent->name); | |
| 1149 | else | |
| 1150 | sysfs_set_str(dev, NULL, ent->name, ent->value); | |
| 1151 | ent = ent->next; | |
| 1152 | } | |
| 1153 | rules = rules->next; | |
| 1154 | } | |
| 1155 | } | |
| 1156 | ||
| 1157 | static void sysfs_rule_free(struct dev_sysfs_rule *rule) | |
| 1158 | { | |
| 1159 | struct sysfs_entry *entry; | |
| 1160 | ||
| 1161 | while (rule) { | |
| 1162 | struct dev_sysfs_rule *tmp = rule->next; | |
| 1163 | ||
| 1164 | entry = rule->entry; | |
| 1165 | while (entry) { | |
| 1166 | struct sysfs_entry *tmp = entry->next; | |
| 1167 | ||
| 1168 | free(entry->name); | |
| 1169 | free(entry->value); | |
| 1170 | free(entry); | |
| 1171 | entry = tmp; | |
| 1172 | } | |
| 1173 | ||
| 1174 | if (rule->devname) | |
| 1175 | free(rule->devname); | |
| 1176 | free(rule); | |
| 1177 | rule = tmp; | |
| 1178 | } | |
| 1179 | } | |
| 1180 | ||
| 1181 | void sysfsline(char *line) | |
| 1182 | { | |
| 1183 | struct dev_sysfs_rule *sr; | |
| 1184 | char *w; | |
| 1185 | ||
| 1186 | sr = xcalloc(1, sizeof(*sr)); | |
| 1187 | for (w = dl_next(line); w != line ; w = dl_next(w)) { | |
| 1188 | if (strncasecmp(w, "name=", 5) == 0) { | |
| 1189 | char *devname = w + 5; | |
| 1190 | ||
| 1191 | if (strncmp(devname, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) { | |
| 1192 | if (sr->devname) | |
| 1193 | pr_err("Only give one device per SYSFS line: %s\n", | |
| 1194 | devname); | |
| 1195 | else | |
| 1196 | sr->devname = xstrdup(devname); | |
| 1197 | } else { | |
| 1198 | pr_err("%s is an invalid name for an md device - ignored.\n", | |
| 1199 | devname); | |
| 1200 | } | |
| 1201 | } else if (strncasecmp(w, "uuid=", 5) == 0) { | |
| 1202 | char *uuid = w + 5; | |
| 1203 | ||
| 1204 | if (sr->uuid_set) { | |
| 1205 | pr_err("Only give one uuid per SYSFS line: %s\n", | |
| 1206 | uuid); | |
| 1207 | } else { | |
| 1208 | if (parse_uuid(w + 5, sr->uuid) && | |
| 1209 | memcmp(sr->uuid, uuid_zero, | |
| 1210 | sizeof(int[4])) != 0) | |
| 1211 | sr->uuid_set = 1; | |
| 1212 | else | |
| 1213 | pr_err("Invalid uuid: %s\n", uuid); | |
| 1214 | } | |
| 1215 | } else { | |
| 1216 | struct sysfs_entry *prop; | |
| 1217 | ||
| 1218 | char *sep = strchr(w, '='); | |
| 1219 | ||
| 1220 | if (sep == NULL || *(sep + 1) == 0) { | |
| 1221 | pr_err("Cannot parse \"%s\" - ignoring.\n", w); | |
| 1222 | continue; | |
| 1223 | } | |
| 1224 | ||
| 1225 | prop = xmalloc(sizeof(*prop)); | |
| 1226 | prop->value = xstrdup(sep + 1); | |
| 1227 | *sep = 0; | |
| 1228 | prop->name = xstrdup(w); | |
| 1229 | prop->next = sr->entry; | |
| 1230 | sr->entry = prop; | |
| 1231 | } | |
| 1232 | } | |
| 1233 | ||
| 1234 | if (!sr->devname && !sr->uuid_set) { | |
| 1235 | pr_err("Device name not found in sysfs config entry - ignoring.\n"); | |
| 1236 | sysfs_rule_free(sr); | |
| 1237 | return; | |
| 1238 | } | |
| 1239 | ||
| 1240 | sr->next = sysfs_rules; | |
| 1241 | sysfs_rules = sr; | |
| 1242 | } | |
| 1243 | ||
| 1244 | /** | |
| 1245 | * sysfs_is_libata_allow_tpm_enabled() - check if libata allow_tmp is enabled. | |
| 1246 | * @verbose: verbose flag. | |
| 1247 | * | |
| 1248 | * Check if libata allow_tmp flag is set, this is required for SATA Opal Security commands to work. | |
| 1249 | * | |
| 1250 | * Return: true if allow_tpm enable, false otherwise. | |
| 1251 | */ | |
| 1252 | bool sysfs_is_libata_allow_tpm_enabled(const int verbose) | |
| 1253 | { | |
| 1254 | const char *path = "/sys/module/libata/parameters/allow_tpm"; | |
| 1255 | const char *expected_value = "1"; | |
| 1256 | int fd = open(path, O_RDONLY); | |
| 1257 | char buf[3]; | |
| 1258 | ||
| 1259 | if (!is_fd_valid(fd)) { | |
| 1260 | pr_vrb("Failed open file descriptor to %s. Cannot check libata allow_tpm param.\n", | |
| 1261 | path); | |
| 1262 | return false; | |
| 1263 | } | |
| 1264 | ||
| 1265 | sysfs_fd_get_str(fd, buf, sizeof(buf)); | |
| 1266 | close(fd); | |
| 1267 | ||
| 1268 | if (strncmp(buf, expected_value, 1) == 0) | |
| 1269 | return true; | |
| 1270 | return false; | |
| 1271 | } |