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