]> git.ipfire.org Git - thirdparty/mdadm.git/blob - sysfs.c
Disk removal support for Raid10->Raid0 takeover
[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 rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk);
618 if (resume)
619 sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start);
620 }
621 return rv;
622 }
623
624 #if 0
625 int sysfs_disk_to_sg(int fd)
626 {
627 /* from an open block device, try find and open its corresponding
628 * scsi_generic interface
629 */
630 struct stat st;
631 char path[256];
632 char sg_path[256];
633 char sg_major_minor[8];
634 char *c;
635 DIR *dir;
636 struct dirent *de;
637 int major, minor, rv;
638
639 if (fstat(fd, &st))
640 return -1;
641
642 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
643 major(st.st_rdev), minor(st.st_rdev));
644
645 dir = opendir(path);
646 if (!dir)
647 return -1;
648
649 de = readdir(dir);
650 while (de) {
651 if (strncmp("scsi_generic:", de->d_name,
652 strlen("scsi_generic:")) == 0)
653 break;
654 de = readdir(dir);
655 }
656 closedir(dir);
657
658 if (!de)
659 return -1;
660
661 snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name);
662 fd = open(sg_path, O_RDONLY);
663 if (fd < 0)
664 return fd;
665
666 rv = read(fd, sg_major_minor, sizeof(sg_major_minor));
667 close(fd);
668 if (rv < 0)
669 return -1;
670 else
671 sg_major_minor[rv - 1] = '\0';
672
673 c = strchr(sg_major_minor, ':');
674 *c = '\0';
675 c++;
676 major = strtol(sg_major_minor, NULL, 10);
677 minor = strtol(c, NULL, 10);
678 snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d",
679 (int) getpid(), major, minor);
680 if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) {
681 fd = open(path, O_RDONLY);
682 unlink(path);
683 return fd;
684 }
685
686 return -1;
687 }
688 #endif
689
690 int sysfs_disk_to_scsi_id(int fd, __u32 *id)
691 {
692 /* from an open block device, try to retrieve it scsi_id */
693 struct stat st;
694 char path[256];
695 char *c1, *c2;
696 DIR *dir;
697 struct dirent *de;
698
699 if (fstat(fd, &st))
700 return 1;
701
702 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
703 major(st.st_rdev), minor(st.st_rdev));
704
705 dir = opendir(path);
706 if (!dir)
707 return 1;
708
709 de = readdir(dir);
710 while (de) {
711 if (strncmp("scsi_disk:", de->d_name,
712 strlen("scsi_disk:")) == 0)
713 break;
714 de = readdir(dir);
715 }
716 closedir(dir);
717
718 if (!de)
719 return 1;
720
721 c1 = strchr(de->d_name, ':');
722 c1++;
723 c2 = strchr(c1, ':');
724 *c2 = '\0';
725 *id = strtol(c1, NULL, 10) << 24; /* host */
726 c1 = c2 + 1;
727 c2 = strchr(c1, ':');
728 *c2 = '\0';
729 *id |= strtol(c1, NULL, 10) << 16; /* channel */
730 c1 = c2 + 1;
731 c2 = strchr(c1, ':');
732 *c2 = '\0';
733 *id |= strtol(c1, NULL, 10) << 8; /* lun */
734 c1 = c2 + 1;
735 *id |= strtol(c1, NULL, 10); /* id */
736
737 return 0;
738 }
739
740
741 int sysfs_unique_holder(int devnum, long rdev)
742 {
743 /* Check that devnum is a holder of rdev,
744 * and is the only holder.
745 * we should be locked against races by
746 * an O_EXCL on devnum
747 */
748 DIR *dir;
749 struct dirent *de;
750 char dirname[100];
751 char l;
752 int found = 0;
753 sprintf(dirname, "/sys/dev/block/%d:%d/holders",
754 major(rdev), minor(rdev));
755 dir = opendir(dirname);
756 errno = ENOENT;
757 if (!dir)
758 return 0;
759 l = strlen(dirname);
760 while ((de = readdir(dir)) != NULL) {
761 char buf[10];
762 int n;
763 int mj, mn;
764 char c;
765 int fd;
766
767 if (de->d_ino == 0)
768 continue;
769 if (de->d_name[0] == '.')
770 continue;
771 strcpy(dirname+l, "/");
772 strcat(dirname+l, de->d_name);
773 strcat(dirname+l, "/dev");
774 fd = open(dirname, O_RDONLY);
775 if (fd < 0) {
776 errno = ENOENT;
777 break;
778 }
779 n = read(fd, buf, sizeof(buf)-1);
780 close(fd);
781 buf[n] = 0;
782 if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 ||
783 c != '\n') {
784 errno = ENOENT;
785 break;
786 }
787 if (mj != MD_MAJOR)
788 mn = -1-(mn>>6);
789
790 if (devnum != mn) {
791 errno = EEXIST;
792 break;
793 }
794 found = 1;
795 }
796 closedir(dir);
797 if (de)
798 return 0;
799 else
800 return found;
801 }
802
803 int sysfs_freeze_array(struct mdinfo *sra)
804 {
805 /* Try to freeze resync/rebuild on this array/container.
806 * Return -1 if the array is busy,
807 * return -2 container cannot be frozen,
808 * return 0 if this kernel doesn't support 'frozen'
809 * return 1 if it worked.
810 */
811 char buf[20];
812
813 if (!sysfs_attribute_available(sra, NULL, "sync_action"))
814 return 1; /* no sync_action == frozen */
815 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
816 return 0;
817 if (strcmp(buf, "idle\n") != 0 &&
818 strcmp(buf, "frozen\n") != 0)
819 return -1;
820 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
821 return 0;
822 return 1;
823 }
824
825 #ifndef MDASSEMBLE
826
827 static char *clean_states[] = {
828 "clear", "inactive", "readonly", "read-auto", "clean", NULL };
829
830 int WaitClean(char *dev, int sock, int verbose)
831 {
832 int fd;
833 struct mdinfo *mdi;
834 int rv = 1;
835 int devnum;
836
837 fd = open(dev, O_RDONLY);
838 if (fd < 0) {
839 if (verbose)
840 fprintf(stderr, Name ": Couldn't open %s: %s\n", dev, strerror(errno));
841 return 1;
842 }
843
844 devnum = fd2devnum(fd);
845 mdi = sysfs_read(fd, devnum, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
846 if (!mdi) {
847 if (verbose)
848 fprintf(stderr, Name ": Failed to read sysfs attributes for "
849 "%s\n", dev);
850 close(fd);
851 return 0;
852 }
853
854 switch(mdi->array.level) {
855 case LEVEL_LINEAR:
856 case LEVEL_MULTIPATH:
857 case 0:
858 /* safemode delay is irrelevant for these levels */
859 rv = 0;
860
861 }
862
863 /* for internal metadata the kernel handles the final clean
864 * transition, containers can never be dirty
865 */
866 if (!is_subarray(mdi->text_version))
867 rv = 0;
868
869 /* safemode disabled ? */
870 if (mdi->safe_mode_delay == 0)
871 rv = 0;
872
873 if (rv) {
874 int state_fd = sysfs_open(fd2devnum(fd), NULL, "array_state");
875 char buf[20];
876 fd_set fds;
877 struct timeval tm;
878
879 /* minimize the safe_mode_delay and prepare to wait up to 5s
880 * for writes to quiesce
881 */
882 sysfs_set_safemode(mdi, 1);
883 tm.tv_sec = 5;
884 tm.tv_usec = 0;
885
886 FD_ZERO(&fds);
887
888 /* wait for array_state to be clean */
889 while (1) {
890 rv = read(state_fd, buf, sizeof(buf));
891 if (rv < 0)
892 break;
893 if (sysfs_match_word(buf, clean_states) <= 4)
894 break;
895 FD_SET(state_fd, &fds);
896 rv = select(state_fd + 1, NULL, NULL, &fds, &tm);
897 if (rv < 0 && errno != EINTR)
898 break;
899 lseek(state_fd, 0, SEEK_SET);
900 }
901 if (rv < 0)
902 rv = 1;
903 else if (fping_monitor(sock) == 0 ||
904 ping_monitor(mdi->text_version) == 0) {
905 /* we need to ping to close the window between array
906 * state transitioning to clean and the metadata being
907 * marked clean
908 */
909 rv = 0;
910 } else
911 rv = 1;
912 if (rv && verbose)
913 fprintf(stderr, Name ": Error waiting for %s to be clean\n",
914 dev);
915
916 /* restore the original safe_mode_delay */
917 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
918 close(state_fd);
919 }
920
921 sysfs_free(mdi);
922 close(fd);
923
924 return rv;
925 }
926 #endif /* MDASSEMBLE */