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