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