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