]> git.ipfire.org Git - thirdparty/mdadm.git/blame - sysfs.c
sysfs: Use the presence of /sys/block/<dev>/md as indicator of valid device
[thirdparty/mdadm.git] / sysfs.c
CommitLineData
e86c9dd6
NB
1/*
2 * sysfs - extract md related information from sysfs. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
e736b623 5 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
e86c9dd6
NB
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 <dirent.h>
1770662b 28#include <ctype.h>
e86c9dd6 29
bbb52f2b
TM
30#define MAX_SYSFS_PATH_LEN 120
31
193b6c0b 32int load_sys(char *path, char *buf, int len)
e86c9dd6
NB
33{
34 int fd = open(path, O_RDONLY);
35 int n;
36 if (fd < 0)
37 return -1;
193b6c0b 38 n = read(fd, buf, len);
e86c9dd6 39 close(fd);
193b6c0b 40 if (n <0 || n >= len)
e86c9dd6
NB
41 return -1;
42 buf[n] = 0;
8dfb8619 43 if (n && buf[n-1] == '\n')
e86c9dd6
NB
44 buf[n-1] = 0;
45 return 0;
46}
47
7e0f6979 48void sysfs_free(struct mdinfo *sra)
8382f19b 49{
7e0f6979
NB
50 while (sra) {
51 struct mdinfo *sra2 = sra->next;
52 while (sra->devs) {
53 struct mdinfo *d = sra->devs;
54 sra->devs = d->next;
bb758cca 55 free(d->bb.entries);
7e0f6979
NB
56 free(d);
57 }
bb758cca 58 free(sra->bb.entries);
7e0f6979
NB
59 free(sra);
60 sra = sra2;
8382f19b 61 }
8382f19b
NB
62}
63
4dd2df09 64int sysfs_open(char *devnm, char *devname, char *attr)
549e9569 65{
bbb52f2b 66 char fname[MAX_SYSFS_PATH_LEN];
549e9569 67 int fd;
549e9569 68
bbb52f2b 69 snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/", devnm);
549e9569 70 if (devname) {
bbb52f2b
TM
71 strncat(fname, devname, MAX_SYSFS_PATH_LEN - strlen(fname));
72 strncat(fname, "/", MAX_SYSFS_PATH_LEN - strlen(fname));
549e9569 73 }
bbb52f2b 74 strncat(fname, attr, MAX_SYSFS_PATH_LEN - strlen(fname));
549e9569 75 fd = open(fname, O_RDWR);
ea6d09b0 76 if (fd < 0 && errno == EACCES)
549e9569
NB
77 fd = open(fname, O_RDONLY);
78 return fd;
79}
80
9465f170
GJ
81void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid)
82{
83 snprintf(mdi->sys_name,
84 sizeof(mdi->sys_name), "dev-%s", devid2kname(devid));
85}
86
4dd2df09 87void sysfs_init(struct mdinfo *mdi, int fd, char *devnm)
f35f2525 88{
67a02d52
JS
89 struct stat stb;
90 char fname[MAX_SYSFS_PATH_LEN];
91
678a4a36 92 mdi->sys_name[0] = 0;
67a02d52 93 if (fd >= 0)
4dd2df09 94 devnm = fd2devnm(fd);
67a02d52 95
4dd2df09 96 if (devnm == NULL)
d7ab966b 97 return;
67a02d52
JS
98
99 snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm);
100
101 if (stat(fname, &stb))
102 return;
103 if (!S_ISDIR(stb.st_mode))
104 return;
4dd2df09 105 strcpy(mdi->sys_name, devnm);
f35f2525
N
106}
107
4dd2df09 108struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
e86c9dd6 109{
33a6535d
AW
110 char fname[PATH_MAX];
111 char buf[PATH_MAX];
e86c9dd6
NB
112 char *base;
113 char *dbase;
7e0f6979 114 struct mdinfo *sra;
64e103fe 115 struct mdinfo *dev, **devp;
355726fa 116 DIR *dir = NULL;
e86c9dd6
NB
117 struct dirent *de;
118
503975b9 119 sra = xcalloc(1, sizeof(*sra));
4dd2df09 120 sysfs_init(sra, fd, devnm);
678a4a36
N
121 if (sra->sys_name[0] == 0) {
122 free(sra);
123 return NULL;
124 }
e86c9dd6 125
7e0f6979 126 sprintf(fname, "/sys/block/%s/md/", sra->sys_name);
e86c9dd6
NB
127 base = fname + strlen(fname);
128
129 sra->devs = NULL;
8382f19b
NB
130 if (options & GET_VERSION) {
131 strcpy(base, "metadata_version");
193b6c0b 132 if (load_sys(fname, buf, sizeof(buf)))
8382f19b 133 goto abort;
294d6f45 134 if (strncmp(buf, "none", 4) == 0) {
7e0f6979
NB
135 sra->array.major_version =
136 sra->array.minor_version = -1;
294d6f45
NB
137 strcpy(sra->text_version, "");
138 } else if (strncmp(buf, "external:", 9) == 0) {
142cb9e1
NB
139 sra->array.major_version = -1;
140 sra->array.minor_version = -2;
141 strcpy(sra->text_version, buf+9);
b8ac1967 142 } else {
8382f19b 143 sscanf(buf, "%d.%d",
7e0f6979
NB
144 &sra->array.major_version,
145 &sra->array.minor_version);
b8ac1967
NB
146 strcpy(sra->text_version, buf);
147 }
8382f19b 148 }
e86c9dd6
NB
149 if (options & GET_LEVEL) {
150 strcpy(base, "level");
193b6c0b 151 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6 152 goto abort;
7e0f6979 153 sra->array.level = map_name(pers, buf);
e86c9dd6
NB
154 }
155 if (options & GET_LAYOUT) {
156 strcpy(base, "layout");
193b6c0b 157 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6 158 goto abort;
7e0f6979 159 sra->array.layout = strtoul(buf, NULL, 0);
e86c9dd6 160 }
549e9569
NB
161 if (options & GET_DISKS) {
162 strcpy(base, "raid_disks");
193b6c0b 163 if (load_sys(fname, buf, sizeof(buf)))
549e9569
NB
164 goto abort;
165 sra->array.raid_disks = strtoul(buf, NULL, 0);
f1d26766
DW
166 }
167 if (options & GET_DEGRADED) {
168 strcpy(base, "degraded");
193b6c0b 169 if (load_sys(fname, buf, sizeof(buf)))
f1d26766
DW
170 goto abort;
171 sra->array.failed_disks = strtoul(buf, NULL, 0);
549e9569 172 }
e86c9dd6
NB
173 if (options & GET_COMPONENT) {
174 strcpy(base, "component_size");
193b6c0b 175 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6
NB
176 goto abort;
177 sra->component_size = strtoull(buf, NULL, 0);
353632d9
NB
178 /* sysfs reports "K", but we want sectors */
179 sra->component_size *= 2;
e86c9dd6
NB
180 }
181 if (options & GET_CHUNK) {
182 strcpy(base, "chunk_size");
193b6c0b 183 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6 184 goto abort;
7e0f6979 185 sra->array.chunk_size = strtoul(buf, NULL, 0);
e86c9dd6 186 }
758d3a8e
NB
187 if (options & GET_CACHE) {
188 strcpy(base, "stripe_cache_size");
193b6c0b 189 if (load_sys(fname, buf, sizeof(buf)))
6a67848a
N
190 /* Probably level doesn't support it */
191 sra->cache_size = 0;
192 else
193 sra->cache_size = strtoul(buf, NULL, 0);
758d3a8e 194 }
37dfc3d6
NB
195 if (options & GET_MISMATCH) {
196 strcpy(base, "mismatch_cnt");
193b6c0b 197 if (load_sys(fname, buf, sizeof(buf)))
37dfc3d6
NB
198 goto abort;
199 sra->mismatch_cnt = strtoul(buf, NULL, 0);
200 }
1770662b
DW
201 if (options & GET_SAFEMODE) {
202 int scale = 1;
203 int dot = 0;
f21e18ca 204 unsigned i;
1770662b
DW
205 unsigned long msec;
206 size_t len;
207
208 strcpy(base, "safe_mode_delay");
193b6c0b 209 if (load_sys(fname, buf, sizeof(buf)))
1770662b
DW
210 goto abort;
211
212 /* remove a period, and count digits after it */
213 len = strlen(buf);
214 for (i = 0; i < len; i++) {
215 if (dot) {
216 if (isdigit(buf[i])) {
217 buf[i-1] = buf[i];
218 scale *= 10;
219 }
220 buf[i] = 0;
221 } else if (buf[i] == '.') {
222 dot=1;
223 buf[i] = 0;
224 }
225 }
226 msec = strtoul(buf, NULL, 10);
227 msec = (msec * 1000) / scale;
228 sra->safe_mode_delay = msec;
229 }
c0c1acd6
N
230 if (options & GET_BITMAP_LOCATION) {
231 strcpy(base, "bitmap/location");
193b6c0b 232 if (load_sys(fname, buf, sizeof(buf)))
c0c1acd6
N
233 goto abort;
234 if (strncmp(buf, "file", 4) == 0)
235 sra->bitmap_offset = 1;
236 else if (strncmp(buf, "none", 4) == 0)
237 sra->bitmap_offset = 0;
238 else if (buf[0] == '+')
fbdef498 239 sra->bitmap_offset = strtol(buf+1, NULL, 10);
c0c1acd6
N
240 else
241 goto abort;
242 }
e86c9dd6 243
5aa644c6
SL
244 if (options & GET_ARRAY_STATE) {
245 strcpy(base, "array_state");
193b6c0b
JS
246 if (load_sys(fname, sra->sysfs_array_state,
247 sizeof(sra->sysfs_array_state)))
5aa644c6
SL
248 goto abort;
249 } else
250 sra->sysfs_array_state[0] = 0;
251
5308f117
AP
252 if (options & GET_CONSISTENCY_POLICY) {
253 strcpy(base, "consistency_policy");
254 if (load_sys(fname, buf, sizeof(buf))) {
255 sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN;
256 } else {
257 sra->consistency_policy = map_name(consistency_policies, buf);
258 if (sra->consistency_policy == UnSet)
259 sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN;
260 }
261 }
262
e86c9dd6
NB
263 if (! (options & GET_DEVS))
264 return sra;
265
266 /* Get all the devices as well */
267 *base = 0;
268 dir = opendir(fname);
269 if (!dir)
270 goto abort;
7e0f6979 271 sra->array.spare_disks = 0;
e86c9dd6 272
64e103fe
N
273 devp = &sra->devs;
274 sra->devs = NULL;
e86c9dd6
NB
275 while ((de = readdir(dir)) != NULL) {
276 char *ep;
277 if (de->d_ino == 0 ||
278 strncmp(de->d_name, "dev-", 4) != 0)
279 continue;
280 strcpy(base, de->d_name);
281 dbase = base + strlen(base);
282 *dbase++ = '/';
283
bb758cca 284 dev = xcalloc(1, sizeof(*dev));
e86c9dd6
NB
285
286 /* Always get slot, major, minor */
287 strcpy(dbase, "slot");
193b6c0b 288 if (load_sys(fname, buf, sizeof(buf))) {
4795982e
DW
289 /* hmm... unable to read 'slot' maybe the device
290 * is going away?
291 */
292 strcpy(dbase, "block");
293 if (readlink(fname, buf, sizeof(buf)) < 0 &&
294 errno != ENAMETOOLONG) {
295 /* ...yup device is gone */
296 free(dev);
297 continue;
298 } else {
299 /* slot is unreadable but 'block' link
300 * still intact... something bad is happening
301 * so abort
302 */
303 free(dev);
304 goto abort;
305 }
64e103fe 306
4795982e 307 }
4795982e 308 strcpy(dev->sys_name, de->d_name);
06c7f68e
NB
309 dev->disk.raid_disk = strtoul(buf, &ep, 10);
310 if (*ep) dev->disk.raid_disk = -1;
e86c9dd6
NB
311
312 strcpy(dbase, "block/dev");
193b6c0b 313 if (load_sys(fname, buf, sizeof(buf))) {
b526e52d
DW
314 /* assume this is a stale reference to a hot
315 * removed device
316 */
dab4a513 317 free(dev);
b526e52d 318 continue;
dab4a513 319 }
e6fc80a8 320 sra->array.nr_disks++;
06c7f68e 321 sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
e86c9dd6 322
dab4a513 323 /* special case check for block devices that can go 'offline' */
b526e52d 324 strcpy(dbase, "block/device/state");
193b6c0b 325 if (load_sys(fname, buf, sizeof(buf)) == 0 &&
b526e52d
DW
326 strncmp(buf, "offline", 7) == 0) {
327 free(dev);
328 continue;
dab4a513
DW
329 }
330
331 /* finally add this disk to the array */
64e103fe
N
332 *devp = dev;
333 devp = & dev->next;
334 dev->next = NULL;
dab4a513 335
e86c9dd6
NB
336 if (options & GET_OFFSET) {
337 strcpy(dbase, "offset");
193b6c0b 338 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6 339 goto abort;
06c7f68e 340 dev->data_offset = strtoull(buf, NULL, 0);
fe384ca0 341 strcpy(dbase, "new_offset");
193b6c0b 342 if (load_sys(fname, buf, sizeof(buf)) == 0)
fe384ca0
N
343 dev->new_data_offset = strtoull(buf, NULL, 0);
344 else
345 dev->new_data_offset = dev->data_offset;
e86c9dd6
NB
346 }
347 if (options & GET_SIZE) {
348 strcpy(dbase, "size");
193b6c0b 349 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6 350 goto abort;
047d2e49 351 dev->component_size = strtoull(buf, NULL, 0) * 2;
e86c9dd6
NB
352 }
353 if (options & GET_STATE) {
06c7f68e 354 dev->disk.state = 0;
e86c9dd6 355 strcpy(dbase, "state");
193b6c0b 356 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6
NB
357 goto abort;
358 if (strstr(buf, "in_sync"))
06c7f68e 359 dev->disk.state |= (1<<MD_DISK_SYNC);
e86c9dd6 360 if (strstr(buf, "faulty"))
06c7f68e
NB
361 dev->disk.state |= (1<<MD_DISK_FAULTY);
362 if (dev->disk.state == 0)
7e0f6979 363 sra->array.spare_disks++;
e86c9dd6
NB
364 }
365 if (options & GET_ERROR) {
366 strcpy(buf, "errors");
193b6c0b 367 if (load_sys(fname, buf, sizeof(buf)))
e86c9dd6
NB
368 goto abort;
369 dev->errors = strtoul(buf, NULL, 0);
370 }
371 }
355726fa 372 closedir(dir);
e86c9dd6
NB
373 return sra;
374
375 abort:
355726fa
NB
376 if (dir)
377 closedir(dir);
8382f19b 378 sysfs_free(sra);
e86c9dd6
NB
379 return NULL;
380}
381
1770662b
DW
382int sysfs_attr_match(const char *attr, const char *str)
383{
384 /* See if attr, read from a sysfs file, matches
385 * str. They must either be the same, or attr can
386 * have a trailing newline or comma
387 */
388 while (*attr && *str && *attr == *str) {
389 attr++;
390 str++;
391 }
392
393 if (*str || (*attr && *attr != ',' && *attr != '\n'))
394 return 0;
395 return 1;
396}
397
398int sysfs_match_word(const char *word, char **list)
399{
400 int n;
401 for (n=0; list[n]; n++)
402 if (sysfs_attr_match(word, list[n]))
403 break;
404 return n;
405}
406
e86c9dd6
NB
407unsigned long long get_component_size(int fd)
408{
409 /* Find out the component size of the array.
410 * We cannot trust GET_ARRAY_INFO ioctl as it's
411 * size field is only 32bits.
412 * So look in /sys/block/mdXXX/md/component_size
353632d9 413 *
8686f3ed 414 * This returns in units of sectors.
e86c9dd6
NB
415 */
416 struct stat stb;
bbb52f2b 417 char fname[MAX_SYSFS_PATH_LEN];
e86c9dd6 418 int n;
36138e4e
JS
419 if (fstat(fd, &stb))
420 return 0;
c07566f1
N
421 snprintf(fname, MAX_SYSFS_PATH_LEN,
422 "/sys/block/%s/md/component_size", stat2devnm(&stb));
e86c9dd6
NB
423 fd = open(fname, O_RDONLY);
424 if (fd < 0)
425 return 0;
426 n = read(fd, fname, sizeof(fname));
427 close(fd);
99f6e521 428 if (n < 0 || n == sizeof(fname))
e86c9dd6
NB
429 return 0;
430 fname[n] = 0;
8686f3ed 431 return strtoull(fname, NULL, 10) * 2;
e86c9dd6
NB
432}
433
7e0f6979 434int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev,
e86c9dd6
NB
435 char *name, char *val)
436{
bbb52f2b 437 char fname[MAX_SYSFS_PATH_LEN];
f21e18ca 438 unsigned int n;
e86c9dd6 439 int fd;
7e1432fb 440
bbb52f2b 441 snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s",
7e0f6979 442 sra->sys_name, dev?dev->sys_name:"", name);
e86c9dd6
NB
443 fd = open(fname, O_WRONLY);
444 if (fd < 0)
445 return -1;
446 n = write(fd, val, strlen(val));
447 close(fd);
2a24d7b6 448 if (n != strlen(val)) {
1ade5cc1
N
449 dprintf("failed to write '%s' to '%s' (%s)\n",
450 val, fname, strerror(errno));
e86c9dd6 451 return -1;
2a24d7b6 452 }
e86c9dd6
NB
453 return 0;
454}
455
7e0f6979 456int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev,
e86c9dd6
NB
457 char *name, unsigned long long val)
458{
459 char valstr[50];
460 sprintf(valstr, "%llu", val);
461 return sysfs_set_str(sra, dev, name, valstr);
462}
463
012a8641
JS
464int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev,
465 char *name, long long val)
466{
467 char valstr[50];
468 sprintf(valstr, "%lli", val);
469 return sysfs_set_str(sra, dev, name, valstr);
470}
471
97590376
N
472int sysfs_uevent(struct mdinfo *sra, char *event)
473{
bbb52f2b 474 char fname[MAX_SYSFS_PATH_LEN];
97590376
N
475 int n;
476 int fd;
477
bbb52f2b 478 snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/uevent",
97590376
N
479 sra->sys_name);
480 fd = open(fname, O_WRONLY);
481 if (fd < 0)
482 return -1;
483 n = write(fd, event, strlen(event));
484 close(fd);
e4c72d1d 485 if (n != (int)strlen(event)) {
1ade5cc1
N
486 dprintf("failed to write '%s' to '%s' (%s)\n",
487 event, fname, strerror(errno));
e4c72d1d
LB
488 return -1;
489 }
97590376 490 return 0;
1011e834 491}
97590376 492
bc77ed53
DW
493int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name)
494{
bbb52f2b 495 char fname[MAX_SYSFS_PATH_LEN];
bc77ed53
DW
496 struct stat st;
497
bbb52f2b 498 snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s",
bc77ed53
DW
499 sra->sys_name, dev?dev->sys_name:"", name);
500
501 return stat(fname, &st) == 0;
502}
503
a6288483
N
504int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev,
505 char *name)
e86c9dd6 506{
bbb52f2b 507 char fname[MAX_SYSFS_PATH_LEN];
e86c9dd6 508 int fd;
a6288483 509
bbb52f2b 510 snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s",
7e0f6979 511 sra->sys_name, dev?dev->sys_name:"", name);
a6288483 512 fd = open(fname, O_RDWR);
e86c9dd6 513 if (fd < 0)
a6288483
N
514 fd = open(fname, O_RDONLY);
515 return fd;
516}
517
518int sysfs_fd_get_ll(int fd, unsigned long long *val)
519{
520 char buf[50];
521 int n;
522 char *ep;
523
524 lseek(fd, 0, 0);
e86c9dd6 525 n = read(fd, buf, sizeof(buf));
5418499a 526 if (n <= 0 || n == sizeof(buf))
6560987b 527 return -2;
e86c9dd6
NB
528 buf[n] = 0;
529 *val = strtoull(buf, &ep, 0);
530 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
531 return -1;
532 return 0;
533}
2503d23b 534
a6288483
N
535int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev,
536 char *name, unsigned long long *val)
537{
538 int n;
539 int fd;
540
541 fd = sysfs_get_fd(sra, dev, name);
542 if (fd < 0)
543 return -1;
544 n = sysfs_fd_get_ll(fd, val);
545 close(fd);
546 return n;
547}
548
2eba8496
N
549int sysfs_fd_get_two(int fd, unsigned long long *v1, unsigned long long *v2)
550{
551 /* two numbers in this sysfs file, either
552 * NNN (NNN)
553 * or
554 * NNN / NNN
555 */
556 char buf[80];
557 int n;
558 char *ep, *ep2;
559
560 lseek(fd, 0, 0);
561 n = read(fd, buf, sizeof(buf));
5418499a 562 if (n <= 0 || n == sizeof(buf))
2eba8496
N
563 return -2;
564 buf[n] = 0;
565 *v1 = strtoull(buf, &ep, 0);
566 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
567 return -1;
568 while (*ep == ' ' || *ep == '/' || *ep == '(')
569 ep++;
570 *v2 = strtoull(ep, &ep2, 0);
571 if (ep2 == ep || (*ep2 != 0 && *ep2 != '\n' && *ep2 != ' ' && *ep2 != ')')) {
572 *v2 = *v1;
573 return 1;
574 }
575 return 2;
576}
577
578int sysfs_get_two(struct mdinfo *sra, struct mdinfo *dev,
579 char *name, unsigned long long *v1, unsigned long long *v2)
580{
581 int n;
582 int fd;
583
584 fd = sysfs_get_fd(sra, dev, name);
585 if (fd < 0)
586 return -1;
587 n = sysfs_fd_get_two(fd, v1, v2);
588 close(fd);
589 return n;
590}
591
7236ee7a
N
592int sysfs_fd_get_str(int fd, char *val, int size)
593{
594 int n;
595
596 lseek(fd, 0, 0);
597 n = read(fd, val, size);
5418499a 598 if (n <= 0 || n == size)
7236ee7a
N
599 return -1;
600 val[n] = 0;
601 return n;
602}
603
93ecfa01
N
604int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev,
605 char *name, char *val, int size)
606{
93ecfa01
N
607 int n;
608 int fd;
7236ee7a
N
609
610 fd = sysfs_get_fd(sra, dev, name);
93ecfa01
N
611 if (fd < 0)
612 return -1;
7236ee7a 613 n = sysfs_fd_get_str(fd, val, size);
93ecfa01 614 close(fd);
93ecfa01
N
615 return n;
616}
617
8ed3e5e1
DW
618int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms)
619{
620 unsigned long sec;
621 unsigned long msec;
622 char delay[30];
623
624 sec = ms / 1000;
0dd3ba30 625 msec = ms % 1000;
8ed3e5e1 626
0dd3ba30
DW
627 sprintf(delay, "%ld.%03ld\n", sec, msec);
628 /* this '\n' ^ needed for kernels older than 2.6.28 */
8ed3e5e1
DW
629 return sysfs_set_str(sra, NULL, "safe_mode_delay", delay);
630}
631
f35f2525 632int sysfs_set_array(struct mdinfo *info, int vers)
2503d23b
NB
633{
634 int rv = 0;
f35f2525 635 char ver[100];
a5062b1c 636 int raid_disks = info->array.raid_disks;
f35f2525
N
637
638 ver[0] = 0;
639 if (info->array.major_version == -1 &&
640 info->array.minor_version == -2) {
ddb12f6c
AK
641 char buf[1024];
642
f35f2525
N
643 strcat(strcpy(ver, "external:"), info->text_version);
644
ddb12f6c
AK
645 /* meta version might already be set if we are setting
646 * new geometry for a reshape. In that case we don't
647 * want to over-write the 'readonly' flag that is
648 * stored in the metadata version. So read the current
649 * version first, and preserve the flag
650 */
651 if (sysfs_get_str(info, NULL, "metadata_version",
652 buf, 1024) > 0)
653 if (strlen(buf) >= 9 && buf[9] == '-')
654 ver[9] = '-';
655
f35f2525
N
656 if ((vers % 100) < 2 ||
657 sysfs_set_str(info, NULL, "metadata_version",
658 ver) < 0) {
7a862a02 659 pr_err("This kernel does not support external metadata.\n");
f35f2525
N
660 return 1;
661 }
662 }
2503d23b
NB
663 if (info->array.level < 0)
664 return 0; /* FIXME */
f35f2525 665 rv |= sysfs_set_str(info, NULL, "level",
2503d23b 666 map_num(pers, info->array.level));
a5062b1c
AK
667 if (info->reshape_active && info->delta_disks != UnSet)
668 raid_disks -= info->delta_disks;
669 rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks);
f35f2525
N
670 rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size);
671 rv |= sysfs_set_num(info, NULL, "layout", info->array.layout);
672 rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2);
da9b4a62
DW
673 if (info->custom_array_size) {
674 int rc;
675
676 rc = sysfs_set_num(info, NULL, "array_size",
677 info->custom_array_size/2);
678 if (rc && errno == ENOENT) {
7a862a02 679 pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n");
da9b4a62
DW
680 rc = 0;
681 }
682 rv |= rc;
683 }
684
f35f2525
N
685 if (info->array.level > 0)
686 rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start);
f897078e
N
687
688 if (info->reshape_active) {
689 rv |= sysfs_set_num(info, NULL, "reshape_position",
690 info->reshape_progress);
691 rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk);
20a40eca
N
692 rv |= sysfs_set_num(info, NULL, "layout", info->new_layout);
693 rv |= sysfs_set_num(info, NULL, "raid_disks",
a5062b1c 694 info->array.raid_disks);
20a40eca
N
695 /* We don't set 'new_level' here. That can only happen
696 * once the reshape completes.
f897078e
N
697 */
698 }
2432ce9b
AP
699
700 if (info->consistency_policy == CONSISTENCY_POLICY_PPL) {
701 if (sysfs_set_str(info, NULL, "consistency_policy",
702 map_num(consistency_policies,
703 info->consistency_policy))) {
704 pr_err("This kernel does not support PPL\n");
705 return 1;
706 }
707 }
708
2503d23b
NB
709 return rv;
710}
711
2904b26f 712int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume)
2503d23b 713{
33a6535d
AW
714 char dv[PATH_MAX];
715 char nm[PATH_MAX];
2503d23b
NB
716 char *dname;
717 int rv;
bb758cca 718 int i;
2503d23b
NB
719
720 sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor);
721 rv = sysfs_set_str(sra, NULL, "new_dev", dv);
722 if (rv)
723 return rv;
724
725 memset(nm, 0, sizeof(nm));
bc17158d 726 dname = devid2kname(makedev(sd->disk.major, sd->disk.minor));
2503d23b
NB
727 strcpy(sd->sys_name, "dev-");
728 strcpy(sd->sys_name+4, dname);
729
2904b26f
DW
730 /* test write to see if 'recovery_start' is available */
731 if (resume && sd->recovery_start < MaxSector &&
732 sysfs_set_num(sra, sd, "recovery_start", 0)) {
733 sysfs_set_str(sra, sd, "state", "remove");
734 return -1;
735 }
736
73649188 737 rv = sysfs_set_num(sra, sd, "offset", sd->data_offset);
2503d23b
NB
738 rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2);
739 if (sra->array.level != LEVEL_CONTAINER) {
2432ce9b
AP
740 if (sd->consistency_policy == CONSISTENCY_POLICY_PPL) {
741 rv |= sysfs_set_num(sra, sd, "ppl_sector", sd->ppl_sector);
742 rv |= sysfs_set_num(sra, sd, "ppl_size", sd->ppl_size);
743 }
d23534e4 744 if (sd->recovery_start == MaxSector)
462906cd
N
745 /* This can correctly fail if array isn't started,
746 * yet, so just ignore status for now.
747 */
d23534e4 748 sysfs_set_str(sra, sd, "state", "insync");
899aead0
AK
749 if (sd->disk.raid_disk >= 0)
750 rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk);
2904b26f
DW
751 if (resume)
752 sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start);
2503d23b 753 }
bb758cca
TM
754 if (sd->bb.supported) {
755 if (sysfs_set_str(sra, sd, "state", "external_bbl")) {
756 /*
757 * backward compatibility - if kernel doesn't support
758 * bad blocks for external metadata, let it continue
759 * as long as there are none known so far
760 */
761 if (sd->bb.count) {
762 pr_err("The kernel has no support for bad blocks in external metadata\n");
763 return -1;
764 }
765 }
766
767 for (i = 0; i < sd->bb.count; i++) {
768 char s[30];
769 const struct md_bb_entry *entry = &sd->bb.entries[i];
770
771 snprintf(s, sizeof(s) - 1, "%llu %d\n", entry->sector,
772 entry->length);
773 rv |= sysfs_set_str(sra, sd, "bad_blocks", s);
774 }
775 }
2503d23b
NB
776 return rv;
777}
90c8b707 778
755c99fa 779#if 0
90c8b707
DW
780int sysfs_disk_to_sg(int fd)
781{
782 /* from an open block device, try find and open its corresponding
783 * scsi_generic interface
784 */
785 struct stat st;
786 char path[256];
787 char sg_path[256];
5418499a 788 char sg_major_minor[10];
90c8b707
DW
789 char *c;
790 DIR *dir;
791 struct dirent *de;
792 int major, minor, rv;
793
794 if (fstat(fd, &st))
795 return -1;
796
797 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
798 major(st.st_rdev), minor(st.st_rdev));
799
800 dir = opendir(path);
801 if (!dir)
802 return -1;
803
804 de = readdir(dir);
805 while (de) {
806 if (strncmp("scsi_generic:", de->d_name,
807 strlen("scsi_generic:")) == 0)
808 break;
809 de = readdir(dir);
810 }
811 closedir(dir);
812
813 if (!de)
814 return -1;
815
816 snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name);
817 fd = open(sg_path, O_RDONLY);
818 if (fd < 0)
819 return fd;
820
821 rv = read(fd, sg_major_minor, sizeof(sg_major_minor));
822 close(fd);
5418499a 823 if (rv < 0 || rv == sizeof(sg_major_minor))
90c8b707
DW
824 return -1;
825 else
826 sg_major_minor[rv - 1] = '\0';
827
828 c = strchr(sg_major_minor, ':');
829 *c = '\0';
830 c++;
831 major = strtol(sg_major_minor, NULL, 10);
832 minor = strtol(c, NULL, 10);
833 snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d",
834 (int) getpid(), major, minor);
835 if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) {
836 fd = open(path, O_RDONLY);
837 unlink(path);
838 return fd;
839 }
840
841 return -1;
842}
755c99fa 843#endif
90c8b707 844
f1665f72
DW
845int sysfs_disk_to_scsi_id(int fd, __u32 *id)
846{
847 /* from an open block device, try to retrieve it scsi_id */
848 struct stat st;
849 char path[256];
f1665f72
DW
850 DIR *dir;
851 struct dirent *de;
d8924477 852 int host, bus, target, lun;
f1665f72
DW
853
854 if (fstat(fd, &st))
855 return 1;
856
fa89bdee 857 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device",
f1665f72
DW
858 major(st.st_rdev), minor(st.st_rdev));
859
860 dir = opendir(path);
861 if (!dir)
862 return 1;
863
d8924477
DW
864 for (de = readdir(dir); de; de = readdir(dir)) {
865 int count;
866
867 if (de->d_type != DT_DIR)
868 continue;
869
870 count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun);
871 if (count == 4)
f1665f72 872 break;
f1665f72
DW
873 }
874 closedir(dir);
875
876 if (!de)
877 return 1;
878
d8924477 879 *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0);
f1665f72
DW
880 return 0;
881}
f94d52f4 882
4dd2df09 883int sysfs_unique_holder(char *devnm, long rdev)
f94d52f4 884{
4dd2df09 885 /* Check that devnm is a holder of rdev,
f94d52f4
NB
886 * and is the only holder.
887 * we should be locked against races by
4dd2df09 888 * an O_EXCL on devnm
aab15415
N
889 * Return values:
890 * 0 - not unique, not even a holder
891 * 1 - unique, this is the only holder.
892 * 2/3 - not unique, there is another holder
893 * -1 - error, cannot find the holders
f94d52f4
NB
894 */
895 DIR *dir;
896 struct dirent *de;
897 char dirname[100];
898 char l;
aab15415 899 int ret = 0;
f94d52f4
NB
900 sprintf(dirname, "/sys/dev/block/%d:%d/holders",
901 major(rdev), minor(rdev));
902 dir = opendir(dirname);
f94d52f4 903 if (!dir)
aab15415 904 return -1;
f94d52f4
NB
905 l = strlen(dirname);
906 while ((de = readdir(dir)) != NULL) {
4dd2df09
N
907 char buf[100];
908 char *sl;
f94d52f4 909 int n;
f94d52f4
NB
910
911 if (de->d_ino == 0)
912 continue;
913 if (de->d_name[0] == '.')
914 continue;
915 strcpy(dirname+l, "/");
916 strcat(dirname+l, de->d_name);
4dd2df09
N
917 n = readlink(dirname, buf, sizeof(buf)-1);
918 if (n <= 0)
93f1df33 919 continue;
f94d52f4 920 buf[n] = 0;
4dd2df09
N
921 sl = strrchr(buf, '/');
922 if (!sl)
aab15415 923 continue;
4dd2df09 924 sl++;
f94d52f4 925
4dd2df09 926 if (strcmp(devnm, sl) == 0)
aab15415
N
927 ret |= 1;
928 else
929 ret |= 2;
f94d52f4
NB
930 }
931 closedir(dir);
aab15415 932 return ret;
f94d52f4 933}
38a07ed6 934
bc77ed53
DW
935int sysfs_freeze_array(struct mdinfo *sra)
936{
937 /* Try to freeze resync/rebuild on this array/container.
938 * Return -1 if the array is busy,
bc77ed53
DW
939 * return 0 if this kernel doesn't support 'frozen'
940 * return 1 if it worked.
941 */
942 char buf[20];
943
944 if (!sysfs_attribute_available(sra, NULL, "sync_action"))
945 return 1; /* no sync_action == frozen */
946 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
947 return 0;
fd324b08
N
948 if (strcmp(buf, "frozen\n") == 0)
949 /* Already frozen */
950 return 0;
dea3786a 951 if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0)
bc77ed53
DW
952 return -1;
953 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
954 return 0;
955 return 1;
956}
efc67e8e
N
957
958int sysfs_wait(int fd, int *msec)
959{
960 /* Wait up to '*msec' for fd to have an exception condition.
961 * if msec == NULL, wait indefinitely.
962 */
963 fd_set fds;
964 int n;
965 FD_ZERO(&fds);
966 FD_SET(fd, &fds);
967 if (msec == NULL)
968 n = select(fd+1, NULL, NULL, &fds, NULL);
969 else if (*msec < 0)
970 n = 0;
971 else {
972 struct timeval start, end, tv;
973 gettimeofday(&start, NULL);
4bffc964
N
974 if (*msec < 1000) {
975 tv.tv_sec = 0;
efc67e8e 976 tv.tv_usec = (*msec)*1000;
4bffc964 977 } else {
efc67e8e 978 tv.tv_sec = (*msec)/1000;
4bffc964
N
979 tv.tv_usec = 0;
980 }
efc67e8e
N
981 n = select(fd+1, NULL, NULL, &fds, &tv);
982 gettimeofday(&end, NULL);
983 end.tv_sec -= start.tv_sec;
984 *msec -= (end.tv_sec * 1000 + end.tv_usec/1000
4bffc964 985 - start.tv_usec/1000) + 1;
efc67e8e
N
986 }
987 return n;
988}