]> git.ipfire.org Git - thirdparty/mdadm.git/blame - sysfs.c
Discard devnum in favour of devnm
[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 }
06c7f68e 283 sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
e86c9dd6 284
dab4a513 285 /* special case check for block devices that can go 'offline' */
b526e52d
DW
286 strcpy(dbase, "block/device/state");
287 if (load_sys(fname, buf) == 0 &&
288 strncmp(buf, "offline", 7) == 0) {
289 free(dev);
290 continue;
dab4a513
DW
291 }
292
293 /* finally add this disk to the array */
294 dev->next = sra->devs;
295 sra->devs = dev;
296
e86c9dd6
NB
297 if (options & GET_OFFSET) {
298 strcpy(dbase, "offset");
299 if (load_sys(fname, buf))
300 goto abort;
06c7f68e 301 dev->data_offset = strtoull(buf, NULL, 0);
fe384ca0
N
302 strcpy(dbase, "new_offset");
303 if (load_sys(fname, buf) == 0)
304 dev->new_data_offset = strtoull(buf, NULL, 0);
305 else
306 dev->new_data_offset = dev->data_offset;
e86c9dd6
NB
307 }
308 if (options & GET_SIZE) {
309 strcpy(dbase, "size");
310 if (load_sys(fname, buf))
311 goto abort;
047d2e49 312 dev->component_size = strtoull(buf, NULL, 0) * 2;
e86c9dd6
NB
313 }
314 if (options & GET_STATE) {
06c7f68e 315 dev->disk.state = 0;
e86c9dd6
NB
316 strcpy(dbase, "state");
317 if (load_sys(fname, buf))
318 goto abort;
319 if (strstr(buf, "in_sync"))
06c7f68e 320 dev->disk.state |= (1<<MD_DISK_SYNC);
e86c9dd6 321 if (strstr(buf, "faulty"))
06c7f68e
NB
322 dev->disk.state |= (1<<MD_DISK_FAULTY);
323 if (dev->disk.state == 0)
7e0f6979 324 sra->array.spare_disks++;
e86c9dd6
NB
325 }
326 if (options & GET_ERROR) {
327 strcpy(buf, "errors");
328 if (load_sys(fname, buf))
329 goto abort;
330 dev->errors = strtoul(buf, NULL, 0);
331 }
332 }
355726fa 333 closedir(dir);
e86c9dd6
NB
334 return sra;
335
336 abort:
355726fa
NB
337 if (dir)
338 closedir(dir);
8382f19b 339 sysfs_free(sra);
e86c9dd6
NB
340 return NULL;
341}
342
1770662b
DW
343int sysfs_attr_match(const char *attr, const char *str)
344{
345 /* See if attr, read from a sysfs file, matches
346 * str. They must either be the same, or attr can
347 * have a trailing newline or comma
348 */
349 while (*attr && *str && *attr == *str) {
350 attr++;
351 str++;
352 }
353
354 if (*str || (*attr && *attr != ',' && *attr != '\n'))
355 return 0;
356 return 1;
357}
358
359int sysfs_match_word(const char *word, char **list)
360{
361 int n;
362 for (n=0; list[n]; n++)
363 if (sysfs_attr_match(word, list[n]))
364 break;
365 return n;
366}
367
e86c9dd6
NB
368unsigned long long get_component_size(int fd)
369{
370 /* Find out the component size of the array.
371 * We cannot trust GET_ARRAY_INFO ioctl as it's
372 * size field is only 32bits.
373 * So look in /sys/block/mdXXX/md/component_size
353632d9 374 *
8686f3ed 375 * This returns in units of sectors.
e86c9dd6
NB
376 */
377 struct stat stb;
378 char fname[50];
379 int n;
380 if (fstat(fd, &stb)) return 0;
f21e18ca 381 if (major(stb.st_rdev) != (unsigned)get_mdp_major())
e86c9dd6 382 sprintf(fname, "/sys/block/md%d/md/component_size",
ea24acd0 383 (int)minor(stb.st_rdev));
e86c9dd6
NB
384 else
385 sprintf(fname, "/sys/block/md_d%d/md/component_size",
ea24acd0 386 (int)minor(stb.st_rdev)>>MdpMinorShift);
e86c9dd6
NB
387 fd = open(fname, O_RDONLY);
388 if (fd < 0)
389 return 0;
390 n = read(fd, fname, sizeof(fname));
391 close(fd);
99f6e521 392 if (n < 0 || n == sizeof(fname))
e86c9dd6
NB
393 return 0;
394 fname[n] = 0;
8686f3ed 395 return strtoull(fname, NULL, 10) * 2;
e86c9dd6
NB
396}
397
7e0f6979 398int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev,
e86c9dd6
NB
399 char *name, char *val)
400{
401 char fname[50];
f21e18ca 402 unsigned int n;
e86c9dd6 403 int fd;
7e1432fb 404
e86c9dd6 405 sprintf(fname, "/sys/block/%s/md/%s/%s",
7e0f6979 406 sra->sys_name, dev?dev->sys_name:"", name);
e86c9dd6
NB
407 fd = open(fname, O_WRONLY);
408 if (fd < 0)
409 return -1;
410 n = write(fd, val, strlen(val));
411 close(fd);
2a24d7b6
DW
412 if (n != strlen(val)) {
413 dprintf(Name ": failed to write '%s' to '%s' (%s)\n",
414 val, fname, strerror(errno));
e86c9dd6 415 return -1;
2a24d7b6 416 }
e86c9dd6
NB
417 return 0;
418}
419
7e0f6979 420int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev,
e86c9dd6
NB
421 char *name, unsigned long long val)
422{
423 char valstr[50];
424 sprintf(valstr, "%llu", val);
425 return sysfs_set_str(sra, dev, name, valstr);
426}
427
012a8641
JS
428int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev,
429 char *name, long long val)
430{
431 char valstr[50];
432 sprintf(valstr, "%lli", val);
433 return sysfs_set_str(sra, dev, name, valstr);
434}
435
97590376
N
436int sysfs_uevent(struct mdinfo *sra, char *event)
437{
438 char fname[50];
439 int n;
440 int fd;
441
442 sprintf(fname, "/sys/block/%s/uevent",
443 sra->sys_name);
444 fd = open(fname, O_WRONLY);
445 if (fd < 0)
446 return -1;
447 n = write(fd, event, strlen(event));
448 close(fd);
e4c72d1d
LB
449 if (n != (int)strlen(event)) {
450 dprintf(Name ": failed to write '%s' to '%s' (%s)\n",
451 event, fname, strerror(errno));
452 return -1;
453 }
97590376
N
454 return 0;
455}
456
bc77ed53
DW
457int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name)
458{
459 char fname[50];
460 struct stat st;
461
462 sprintf(fname, "/sys/block/%s/md/%s/%s",
463 sra->sys_name, dev?dev->sys_name:"", name);
464
465 return stat(fname, &st) == 0;
466}
467
a6288483
N
468int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev,
469 char *name)
e86c9dd6
NB
470{
471 char fname[50];
e86c9dd6 472 int fd;
a6288483 473
e86c9dd6 474 sprintf(fname, "/sys/block/%s/md/%s/%s",
7e0f6979 475 sra->sys_name, dev?dev->sys_name:"", name);
a6288483 476 fd = open(fname, O_RDWR);
e86c9dd6 477 if (fd < 0)
a6288483
N
478 fd = open(fname, O_RDONLY);
479 return fd;
480}
481
482int sysfs_fd_get_ll(int fd, unsigned long long *val)
483{
484 char buf[50];
485 int n;
486 char *ep;
487
488 lseek(fd, 0, 0);
e86c9dd6 489 n = read(fd, buf, sizeof(buf));
e86c9dd6 490 if (n <= 0)
6560987b 491 return -2;
e86c9dd6
NB
492 buf[n] = 0;
493 *val = strtoull(buf, &ep, 0);
494 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
495 return -1;
496 return 0;
497}
2503d23b 498
a6288483
N
499int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev,
500 char *name, unsigned long long *val)
501{
502 int n;
503 int fd;
504
505 fd = sysfs_get_fd(sra, dev, name);
506 if (fd < 0)
507 return -1;
508 n = sysfs_fd_get_ll(fd, val);
509 close(fd);
510 return n;
511}
512
7236ee7a
N
513int sysfs_fd_get_str(int fd, char *val, int size)
514{
515 int n;
516
517 lseek(fd, 0, 0);
518 n = read(fd, val, size);
519 if (n <= 0)
520 return -1;
521 val[n] = 0;
522 return n;
523}
524
93ecfa01
N
525int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev,
526 char *name, char *val, int size)
527{
93ecfa01
N
528 int n;
529 int fd;
7236ee7a
N
530
531 fd = sysfs_get_fd(sra, dev, name);
93ecfa01
N
532 if (fd < 0)
533 return -1;
7236ee7a 534 n = sysfs_fd_get_str(fd, val, size);
93ecfa01 535 close(fd);
93ecfa01
N
536 return n;
537}
538
8ed3e5e1
DW
539int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms)
540{
541 unsigned long sec;
542 unsigned long msec;
543 char delay[30];
544
545 sec = ms / 1000;
0dd3ba30 546 msec = ms % 1000;
8ed3e5e1 547
0dd3ba30
DW
548 sprintf(delay, "%ld.%03ld\n", sec, msec);
549 /* this '\n' ^ needed for kernels older than 2.6.28 */
8ed3e5e1
DW
550 return sysfs_set_str(sra, NULL, "safe_mode_delay", delay);
551}
552
f35f2525 553int sysfs_set_array(struct mdinfo *info, int vers)
2503d23b
NB
554{
555 int rv = 0;
f35f2525 556 char ver[100];
a5062b1c 557 int raid_disks = info->array.raid_disks;
f35f2525
N
558
559 ver[0] = 0;
560 if (info->array.major_version == -1 &&
561 info->array.minor_version == -2) {
ddb12f6c
AK
562 char buf[1024];
563
f35f2525
N
564 strcat(strcpy(ver, "external:"), info->text_version);
565
ddb12f6c
AK
566 /* meta version might already be set if we are setting
567 * new geometry for a reshape. In that case we don't
568 * want to over-write the 'readonly' flag that is
569 * stored in the metadata version. So read the current
570 * version first, and preserve the flag
571 */
572 if (sysfs_get_str(info, NULL, "metadata_version",
573 buf, 1024) > 0)
574 if (strlen(buf) >= 9 && buf[9] == '-')
575 ver[9] = '-';
576
f35f2525
N
577 if ((vers % 100) < 2 ||
578 sysfs_set_str(info, NULL, "metadata_version",
579 ver) < 0) {
e7b84f9d 580 pr_err("This kernel does not "
f35f2525
N
581 "support external metadata.\n");
582 return 1;
583 }
584 }
2503d23b
NB
585 if (info->array.level < 0)
586 return 0; /* FIXME */
f35f2525 587 rv |= sysfs_set_str(info, NULL, "level",
2503d23b 588 map_num(pers, info->array.level));
a5062b1c
AK
589 if (info->reshape_active && info->delta_disks != UnSet)
590 raid_disks -= info->delta_disks;
591 rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks);
f35f2525
N
592 rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size);
593 rv |= sysfs_set_num(info, NULL, "layout", info->array.layout);
594 rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2);
da9b4a62
DW
595 if (info->custom_array_size) {
596 int rc;
597
598 rc = sysfs_set_num(info, NULL, "array_size",
599 info->custom_array_size/2);
600 if (rc && errno == ENOENT) {
e7b84f9d 601 pr_err("This kernel does not "
da9b4a62
DW
602 "have the md/array_size attribute, "
603 "the array may be larger than expected\n");
604 rc = 0;
605 }
606 rv |= rc;
607 }
608
f35f2525
N
609 if (info->array.level > 0)
610 rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start);
f897078e
N
611
612 if (info->reshape_active) {
613 rv |= sysfs_set_num(info, NULL, "reshape_position",
614 info->reshape_progress);
615 rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk);
20a40eca
N
616 rv |= sysfs_set_num(info, NULL, "layout", info->new_layout);
617 rv |= sysfs_set_num(info, NULL, "raid_disks",
a5062b1c 618 info->array.raid_disks);
20a40eca
N
619 /* We don't set 'new_level' here. That can only happen
620 * once the reshape completes.
f897078e
N
621 */
622 }
2503d23b
NB
623 return rv;
624}
625
2904b26f 626int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume)
2503d23b 627{
33a6535d
AW
628 char dv[PATH_MAX];
629 char nm[PATH_MAX];
2503d23b
NB
630 char *dname;
631 int rv;
632
633 sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor);
634 rv = sysfs_set_str(sra, NULL, "new_dev", dv);
635 if (rv)
636 return rv;
637
638 memset(nm, 0, sizeof(nm));
4dd2df09 639 dname = devid2devnm(makedev(sd->disk.major, sd->disk.minor));
2503d23b
NB
640 strcpy(sd->sys_name, "dev-");
641 strcpy(sd->sys_name+4, dname);
642
2904b26f
DW
643 /* test write to see if 'recovery_start' is available */
644 if (resume && sd->recovery_start < MaxSector &&
645 sysfs_set_num(sra, sd, "recovery_start", 0)) {
646 sysfs_set_str(sra, sd, "state", "remove");
647 return -1;
648 }
649
73649188 650 rv = sysfs_set_num(sra, sd, "offset", sd->data_offset);
2503d23b
NB
651 rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2);
652 if (sra->array.level != LEVEL_CONTAINER) {
d23534e4 653 if (sd->recovery_start == MaxSector)
462906cd
N
654 /* This can correctly fail if array isn't started,
655 * yet, so just ignore status for now.
656 */
d23534e4 657 sysfs_set_str(sra, sd, "state", "insync");
899aead0
AK
658 if (sd->disk.raid_disk >= 0)
659 rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk);
2904b26f
DW
660 if (resume)
661 sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start);
2503d23b 662 }
2503d23b
NB
663 return rv;
664}
90c8b707 665
755c99fa 666#if 0
90c8b707
DW
667int sysfs_disk_to_sg(int fd)
668{
669 /* from an open block device, try find and open its corresponding
670 * scsi_generic interface
671 */
672 struct stat st;
673 char path[256];
674 char sg_path[256];
675 char sg_major_minor[8];
676 char *c;
677 DIR *dir;
678 struct dirent *de;
679 int major, minor, rv;
680
681 if (fstat(fd, &st))
682 return -1;
683
684 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
685 major(st.st_rdev), minor(st.st_rdev));
686
687 dir = opendir(path);
688 if (!dir)
689 return -1;
690
691 de = readdir(dir);
692 while (de) {
693 if (strncmp("scsi_generic:", de->d_name,
694 strlen("scsi_generic:")) == 0)
695 break;
696 de = readdir(dir);
697 }
698 closedir(dir);
699
700 if (!de)
701 return -1;
702
703 snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name);
704 fd = open(sg_path, O_RDONLY);
705 if (fd < 0)
706 return fd;
707
708 rv = read(fd, sg_major_minor, sizeof(sg_major_minor));
709 close(fd);
710 if (rv < 0)
711 return -1;
712 else
713 sg_major_minor[rv - 1] = '\0';
714
715 c = strchr(sg_major_minor, ':');
716 *c = '\0';
717 c++;
718 major = strtol(sg_major_minor, NULL, 10);
719 minor = strtol(c, NULL, 10);
720 snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d",
721 (int) getpid(), major, minor);
722 if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) {
723 fd = open(path, O_RDONLY);
724 unlink(path);
725 return fd;
726 }
727
728 return -1;
729}
755c99fa 730#endif
90c8b707 731
f1665f72
DW
732int sysfs_disk_to_scsi_id(int fd, __u32 *id)
733{
734 /* from an open block device, try to retrieve it scsi_id */
735 struct stat st;
736 char path[256];
f1665f72
DW
737 DIR *dir;
738 struct dirent *de;
d8924477 739 int host, bus, target, lun;
f1665f72
DW
740
741 if (fstat(fd, &st))
742 return 1;
743
fa89bdee 744 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device",
f1665f72
DW
745 major(st.st_rdev), minor(st.st_rdev));
746
747 dir = opendir(path);
748 if (!dir)
749 return 1;
750
d8924477
DW
751 for (de = readdir(dir); de; de = readdir(dir)) {
752 int count;
753
754 if (de->d_type != DT_DIR)
755 continue;
756
757 count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun);
758 if (count == 4)
f1665f72 759 break;
f1665f72
DW
760 }
761 closedir(dir);
762
763 if (!de)
764 return 1;
765
d8924477 766 *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0);
f1665f72
DW
767 return 0;
768}
f94d52f4
NB
769
770
4dd2df09 771int sysfs_unique_holder(char *devnm, long rdev)
f94d52f4 772{
4dd2df09 773 /* Check that devnm is a holder of rdev,
f94d52f4
NB
774 * and is the only holder.
775 * we should be locked against races by
4dd2df09 776 * an O_EXCL on devnm
aab15415
N
777 * Return values:
778 * 0 - not unique, not even a holder
779 * 1 - unique, this is the only holder.
780 * 2/3 - not unique, there is another holder
781 * -1 - error, cannot find the holders
f94d52f4
NB
782 */
783 DIR *dir;
784 struct dirent *de;
785 char dirname[100];
786 char l;
aab15415 787 int ret = 0;
f94d52f4
NB
788 sprintf(dirname, "/sys/dev/block/%d:%d/holders",
789 major(rdev), minor(rdev));
790 dir = opendir(dirname);
f94d52f4 791 if (!dir)
aab15415 792 return -1;
f94d52f4
NB
793 l = strlen(dirname);
794 while ((de = readdir(dir)) != NULL) {
4dd2df09
N
795 char buf[100];
796 char *sl;
f94d52f4 797 int n;
f94d52f4
NB
798
799 if (de->d_ino == 0)
800 continue;
801 if (de->d_name[0] == '.')
802 continue;
803 strcpy(dirname+l, "/");
804 strcat(dirname+l, de->d_name);
4dd2df09
N
805 n = readlink(dirname, buf, sizeof(buf)-1);
806 if (n <= 0)
93f1df33 807 continue;
f94d52f4 808 buf[n] = 0;
4dd2df09
N
809 sl = strrchr(buf, '/');
810 if (!sl)
aab15415 811 continue;
4dd2df09 812 sl++;
f94d52f4 813
4dd2df09 814 if (strcmp(devnm, sl) == 0)
aab15415
N
815 ret |= 1;
816 else
817 ret |= 2;
f94d52f4
NB
818 }
819 closedir(dir);
aab15415 820 return ret;
f94d52f4 821}
38a07ed6 822
bc77ed53
DW
823int sysfs_freeze_array(struct mdinfo *sra)
824{
825 /* Try to freeze resync/rebuild on this array/container.
826 * Return -1 if the array is busy,
bc77ed53
DW
827 * return 0 if this kernel doesn't support 'frozen'
828 * return 1 if it worked.
829 */
830 char buf[20];
831
832 if (!sysfs_attribute_available(sra, NULL, "sync_action"))
833 return 1; /* no sync_action == frozen */
834 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
835 return 0;
fd324b08
N
836 if (strcmp(buf, "frozen\n") == 0)
837 /* Already frozen */
838 return 0;
839 if (strcmp(buf, "idle\n") != 0)
bc77ed53
DW
840 return -1;
841 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
842 return 0;
843 return 1;
844}