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