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