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