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