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