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