]> git.ipfire.org Git - thirdparty/mdadm.git/blame - sysfs.c
New function: sysfs_wait
[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
7236ee7a
N
516int sysfs_fd_get_str(int fd, char *val, int size)
517{
518 int n;
519
520 lseek(fd, 0, 0);
521 n = read(fd, val, size);
522 if (n <= 0)
523 return -1;
524 val[n] = 0;
525 return n;
526}
527
93ecfa01
N
528int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev,
529 char *name, char *val, int size)
530{
93ecfa01
N
531 int n;
532 int fd;
7236ee7a
N
533
534 fd = sysfs_get_fd(sra, dev, name);
93ecfa01
N
535 if (fd < 0)
536 return -1;
7236ee7a 537 n = sysfs_fd_get_str(fd, val, size);
93ecfa01 538 close(fd);
93ecfa01
N
539 return n;
540}
541
8ed3e5e1
DW
542int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms)
543{
544 unsigned long sec;
545 unsigned long msec;
546 char delay[30];
547
548 sec = ms / 1000;
0dd3ba30 549 msec = ms % 1000;
8ed3e5e1 550
0dd3ba30
DW
551 sprintf(delay, "%ld.%03ld\n", sec, msec);
552 /* this '\n' ^ needed for kernels older than 2.6.28 */
8ed3e5e1
DW
553 return sysfs_set_str(sra, NULL, "safe_mode_delay", delay);
554}
555
f35f2525 556int sysfs_set_array(struct mdinfo *info, int vers)
2503d23b
NB
557{
558 int rv = 0;
f35f2525 559 char ver[100];
a5062b1c 560 int raid_disks = info->array.raid_disks;
f35f2525
N
561
562 ver[0] = 0;
563 if (info->array.major_version == -1 &&
564 info->array.minor_version == -2) {
ddb12f6c
AK
565 char buf[1024];
566
f35f2525
N
567 strcat(strcpy(ver, "external:"), info->text_version);
568
ddb12f6c
AK
569 /* meta version might already be set if we are setting
570 * new geometry for a reshape. In that case we don't
571 * want to over-write the 'readonly' flag that is
572 * stored in the metadata version. So read the current
573 * version first, and preserve the flag
574 */
575 if (sysfs_get_str(info, NULL, "metadata_version",
576 buf, 1024) > 0)
577 if (strlen(buf) >= 9 && buf[9] == '-')
578 ver[9] = '-';
579
f35f2525
N
580 if ((vers % 100) < 2 ||
581 sysfs_set_str(info, NULL, "metadata_version",
582 ver) < 0) {
e7b84f9d 583 pr_err("This kernel does not "
f35f2525
N
584 "support external metadata.\n");
585 return 1;
586 }
587 }
2503d23b
NB
588 if (info->array.level < 0)
589 return 0; /* FIXME */
f35f2525 590 rv |= sysfs_set_str(info, NULL, "level",
2503d23b 591 map_num(pers, info->array.level));
a5062b1c
AK
592 if (info->reshape_active && info->delta_disks != UnSet)
593 raid_disks -= info->delta_disks;
594 rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks);
f35f2525
N
595 rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size);
596 rv |= sysfs_set_num(info, NULL, "layout", info->array.layout);
597 rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2);
da9b4a62
DW
598 if (info->custom_array_size) {
599 int rc;
600
601 rc = sysfs_set_num(info, NULL, "array_size",
602 info->custom_array_size/2);
603 if (rc && errno == ENOENT) {
e7b84f9d 604 pr_err("This kernel does not "
da9b4a62
DW
605 "have the md/array_size attribute, "
606 "the array may be larger than expected\n");
607 rc = 0;
608 }
609 rv |= rc;
610 }
611
f35f2525
N
612 if (info->array.level > 0)
613 rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start);
f897078e
N
614
615 if (info->reshape_active) {
616 rv |= sysfs_set_num(info, NULL, "reshape_position",
617 info->reshape_progress);
618 rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk);
20a40eca
N
619 rv |= sysfs_set_num(info, NULL, "layout", info->new_layout);
620 rv |= sysfs_set_num(info, NULL, "raid_disks",
a5062b1c 621 info->array.raid_disks);
20a40eca
N
622 /* We don't set 'new_level' here. That can only happen
623 * once the reshape completes.
f897078e
N
624 */
625 }
2503d23b
NB
626 return rv;
627}
628
2904b26f 629int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume)
2503d23b 630{
33a6535d
AW
631 char dv[PATH_MAX];
632 char nm[PATH_MAX];
2503d23b
NB
633 char *dname;
634 int rv;
635
636 sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor);
637 rv = sysfs_set_str(sra, NULL, "new_dev", dv);
638 if (rv)
639 return rv;
640
641 memset(nm, 0, sizeof(nm));
4dd2df09 642 dname = devid2devnm(makedev(sd->disk.major, sd->disk.minor));
2503d23b
NB
643 strcpy(sd->sys_name, "dev-");
644 strcpy(sd->sys_name+4, dname);
645
2904b26f
DW
646 /* test write to see if 'recovery_start' is available */
647 if (resume && sd->recovery_start < MaxSector &&
648 sysfs_set_num(sra, sd, "recovery_start", 0)) {
649 sysfs_set_str(sra, sd, "state", "remove");
650 return -1;
651 }
652
73649188 653 rv = sysfs_set_num(sra, sd, "offset", sd->data_offset);
2503d23b
NB
654 rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2);
655 if (sra->array.level != LEVEL_CONTAINER) {
d23534e4 656 if (sd->recovery_start == MaxSector)
462906cd
N
657 /* This can correctly fail if array isn't started,
658 * yet, so just ignore status for now.
659 */
d23534e4 660 sysfs_set_str(sra, sd, "state", "insync");
899aead0
AK
661 if (sd->disk.raid_disk >= 0)
662 rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk);
2904b26f
DW
663 if (resume)
664 sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start);
2503d23b 665 }
2503d23b
NB
666 return rv;
667}
90c8b707 668
755c99fa 669#if 0
90c8b707
DW
670int sysfs_disk_to_sg(int fd)
671{
672 /* from an open block device, try find and open its corresponding
673 * scsi_generic interface
674 */
675 struct stat st;
676 char path[256];
677 char sg_path[256];
678 char sg_major_minor[8];
679 char *c;
680 DIR *dir;
681 struct dirent *de;
682 int major, minor, rv;
683
684 if (fstat(fd, &st))
685 return -1;
686
687 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
688 major(st.st_rdev), minor(st.st_rdev));
689
690 dir = opendir(path);
691 if (!dir)
692 return -1;
693
694 de = readdir(dir);
695 while (de) {
696 if (strncmp("scsi_generic:", de->d_name,
697 strlen("scsi_generic:")) == 0)
698 break;
699 de = readdir(dir);
700 }
701 closedir(dir);
702
703 if (!de)
704 return -1;
705
706 snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name);
707 fd = open(sg_path, O_RDONLY);
708 if (fd < 0)
709 return fd;
710
711 rv = read(fd, sg_major_minor, sizeof(sg_major_minor));
712 close(fd);
713 if (rv < 0)
714 return -1;
715 else
716 sg_major_minor[rv - 1] = '\0';
717
718 c = strchr(sg_major_minor, ':');
719 *c = '\0';
720 c++;
721 major = strtol(sg_major_minor, NULL, 10);
722 minor = strtol(c, NULL, 10);
723 snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d",
724 (int) getpid(), major, minor);
725 if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) {
726 fd = open(path, O_RDONLY);
727 unlink(path);
728 return fd;
729 }
730
731 return -1;
732}
755c99fa 733#endif
90c8b707 734
f1665f72
DW
735int sysfs_disk_to_scsi_id(int fd, __u32 *id)
736{
737 /* from an open block device, try to retrieve it scsi_id */
738 struct stat st;
739 char path[256];
f1665f72
DW
740 DIR *dir;
741 struct dirent *de;
d8924477 742 int host, bus, target, lun;
f1665f72
DW
743
744 if (fstat(fd, &st))
745 return 1;
746
fa89bdee 747 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device",
f1665f72
DW
748 major(st.st_rdev), minor(st.st_rdev));
749
750 dir = opendir(path);
751 if (!dir)
752 return 1;
753
d8924477
DW
754 for (de = readdir(dir); de; de = readdir(dir)) {
755 int count;
756
757 if (de->d_type != DT_DIR)
758 continue;
759
760 count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun);
761 if (count == 4)
f1665f72 762 break;
f1665f72
DW
763 }
764 closedir(dir);
765
766 if (!de)
767 return 1;
768
d8924477 769 *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0);
f1665f72
DW
770 return 0;
771}
f94d52f4 772
4dd2df09 773int sysfs_unique_holder(char *devnm, long rdev)
f94d52f4 774{
4dd2df09 775 /* Check that devnm is a holder of rdev,
f94d52f4
NB
776 * and is the only holder.
777 * we should be locked against races by
4dd2df09 778 * an O_EXCL on devnm
aab15415
N
779 * Return values:
780 * 0 - not unique, not even a holder
781 * 1 - unique, this is the only holder.
782 * 2/3 - not unique, there is another holder
783 * -1 - error, cannot find the holders
f94d52f4
NB
784 */
785 DIR *dir;
786 struct dirent *de;
787 char dirname[100];
788 char l;
aab15415 789 int ret = 0;
f94d52f4
NB
790 sprintf(dirname, "/sys/dev/block/%d:%d/holders",
791 major(rdev), minor(rdev));
792 dir = opendir(dirname);
f94d52f4 793 if (!dir)
aab15415 794 return -1;
f94d52f4
NB
795 l = strlen(dirname);
796 while ((de = readdir(dir)) != NULL) {
4dd2df09
N
797 char buf[100];
798 char *sl;
f94d52f4 799 int n;
f94d52f4
NB
800
801 if (de->d_ino == 0)
802 continue;
803 if (de->d_name[0] == '.')
804 continue;
805 strcpy(dirname+l, "/");
806 strcat(dirname+l, de->d_name);
4dd2df09
N
807 n = readlink(dirname, buf, sizeof(buf)-1);
808 if (n <= 0)
93f1df33 809 continue;
f94d52f4 810 buf[n] = 0;
4dd2df09
N
811 sl = strrchr(buf, '/');
812 if (!sl)
aab15415 813 continue;
4dd2df09 814 sl++;
f94d52f4 815
4dd2df09 816 if (strcmp(devnm, sl) == 0)
aab15415
N
817 ret |= 1;
818 else
819 ret |= 2;
f94d52f4
NB
820 }
821 closedir(dir);
aab15415 822 return ret;
f94d52f4 823}
38a07ed6 824
bc77ed53
DW
825int sysfs_freeze_array(struct mdinfo *sra)
826{
827 /* Try to freeze resync/rebuild on this array/container.
828 * Return -1 if the array is busy,
bc77ed53
DW
829 * return 0 if this kernel doesn't support 'frozen'
830 * return 1 if it worked.
831 */
832 char buf[20];
833
834 if (!sysfs_attribute_available(sra, NULL, "sync_action"))
835 return 1; /* no sync_action == frozen */
836 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
837 return 0;
fd324b08
N
838 if (strcmp(buf, "frozen\n") == 0)
839 /* Already frozen */
840 return 0;
dea3786a 841 if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0)
bc77ed53
DW
842 return -1;
843 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
844 return 0;
845 return 1;
846}
efc67e8e
N
847
848int sysfs_wait(int fd, int *msec)
849{
850 /* Wait up to '*msec' for fd to have an exception condition.
851 * if msec == NULL, wait indefinitely.
852 */
853 fd_set fds;
854 int n;
855 FD_ZERO(&fds);
856 FD_SET(fd, &fds);
857 if (msec == NULL)
858 n = select(fd+1, NULL, NULL, &fds, NULL);
859 else if (*msec < 0)
860 n = 0;
861 else {
862 struct timeval start, end, tv;
863 gettimeofday(&start, NULL);
864 if (*msec < 1000)
865 tv.tv_usec = (*msec)*1000;
866 else
867 tv.tv_sec = (*msec)/1000;
868 n = select(fd+1, NULL, NULL, &fds, &tv);
869 gettimeofday(&end, NULL);
870 end.tv_sec -= start.tv_sec;
871 *msec -= (end.tv_sec * 1000 + end.tv_usec/1000
872 - start.tv_usec/100) + 1;
873 }
874 return n;
875}