]>
Commit | Line | Data |
---|---|---|
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 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 | /* Longest possible name in sysfs, mounted at /sys, is | |
104 | * /sys/block/md_dXXX/md/dev-XXXXX/block/dev | |
105 | * /sys/block/md_dXXX/md/metadata_version | |
106 | * which is about 41 characters. 50 should do for now | |
107 | */ | |
108 | char fname[50]; | |
109 | char buf[1024]; | |
110 | char *base; | |
111 | char *dbase; | |
112 | struct mdinfo *sra; | |
113 | struct mdinfo *dev; | |
114 | DIR *dir = NULL; | |
115 | struct dirent *de; | |
116 | ||
117 | sra = malloc(sizeof(*sra)); | |
118 | if (sra == NULL) | |
119 | return sra; | |
120 | memset(sra, 0, sizeof(*sra)); | |
121 | sysfs_init(sra, fd, devnum); | |
122 | if (sra->sys_name[0] == 0) { | |
123 | free(sra); | |
124 | return NULL; | |
125 | } | |
126 | ||
127 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); | |
128 | base = fname + strlen(fname); | |
129 | ||
130 | sra->devs = NULL; | |
131 | if (options & GET_VERSION) { | |
132 | strcpy(base, "metadata_version"); | |
133 | if (load_sys(fname, buf)) | |
134 | goto abort; | |
135 | if (strncmp(buf, "none", 4) == 0) { | |
136 | sra->array.major_version = | |
137 | sra->array.minor_version = -1; | |
138 | strcpy(sra->text_version, ""); | |
139 | } else if (strncmp(buf, "external:", 9) == 0) { | |
140 | sra->array.major_version = -1; | |
141 | sra->array.minor_version = -2; | |
142 | strcpy(sra->text_version, buf+9); | |
143 | } else { | |
144 | sscanf(buf, "%d.%d", | |
145 | &sra->array.major_version, | |
146 | &sra->array.minor_version); | |
147 | strcpy(sra->text_version, buf); | |
148 | } | |
149 | } | |
150 | if (options & GET_LEVEL) { | |
151 | strcpy(base, "level"); | |
152 | if (load_sys(fname, buf)) | |
153 | goto abort; | |
154 | sra->array.level = map_name(pers, buf); | |
155 | } | |
156 | if (options & GET_LAYOUT) { | |
157 | strcpy(base, "layout"); | |
158 | if (load_sys(fname, buf)) | |
159 | goto abort; | |
160 | sra->array.layout = strtoul(buf, NULL, 0); | |
161 | } | |
162 | if (options & GET_DISKS) { | |
163 | strcpy(base, "raid_disks"); | |
164 | if (load_sys(fname, buf)) | |
165 | goto abort; | |
166 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
167 | } | |
168 | if (options & GET_DEGRADED) { | |
169 | strcpy(base, "degraded"); | |
170 | if (load_sys(fname, buf)) | |
171 | goto abort; | |
172 | sra->array.failed_disks = strtoul(buf, NULL, 0); | |
173 | } | |
174 | if (options & GET_COMPONENT) { | |
175 | strcpy(base, "component_size"); | |
176 | if (load_sys(fname, buf)) | |
177 | goto abort; | |
178 | sra->component_size = strtoull(buf, NULL, 0); | |
179 | /* sysfs reports "K", but we want sectors */ | |
180 | sra->component_size *= 2; | |
181 | } | |
182 | if (options & GET_CHUNK) { | |
183 | strcpy(base, "chunk_size"); | |
184 | if (load_sys(fname, buf)) | |
185 | goto abort; | |
186 | sra->array.chunk_size = strtoul(buf, NULL, 0); | |
187 | } | |
188 | if (options & GET_CACHE) { | |
189 | strcpy(base, "stripe_cache_size"); | |
190 | if (load_sys(fname, buf)) | |
191 | goto abort; | |
192 | sra->cache_size = strtoul(buf, NULL, 0); | |
193 | } | |
194 | if (options & GET_MISMATCH) { | |
195 | strcpy(base, "mismatch_cnt"); | |
196 | if (load_sys(fname, buf)) | |
197 | goto abort; | |
198 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
199 | } | |
200 | if (options & GET_SAFEMODE) { | |
201 | int scale = 1; | |
202 | int dot = 0; | |
203 | int i; | |
204 | unsigned long msec; | |
205 | size_t len; | |
206 | ||
207 | strcpy(base, "safe_mode_delay"); | |
208 | if (load_sys(fname, buf)) | |
209 | goto abort; | |
210 | ||
211 | /* remove a period, and count digits after it */ | |
212 | len = strlen(buf); | |
213 | for (i = 0; i < len; i++) { | |
214 | if (dot) { | |
215 | if (isdigit(buf[i])) { | |
216 | buf[i-1] = buf[i]; | |
217 | scale *= 10; | |
218 | } | |
219 | buf[i] = 0; | |
220 | } else if (buf[i] == '.') { | |
221 | dot=1; | |
222 | buf[i] = 0; | |
223 | } | |
224 | } | |
225 | msec = strtoul(buf, NULL, 10); | |
226 | msec = (msec * 1000) / scale; | |
227 | sra->safe_mode_delay = msec; | |
228 | } | |
229 | ||
230 | if (! (options & GET_DEVS)) | |
231 | return sra; | |
232 | ||
233 | /* Get all the devices as well */ | |
234 | *base = 0; | |
235 | dir = opendir(fname); | |
236 | if (!dir) | |
237 | goto abort; | |
238 | sra->array.spare_disks = 0; | |
239 | ||
240 | while ((de = readdir(dir)) != NULL) { | |
241 | char *ep; | |
242 | if (de->d_ino == 0 || | |
243 | strncmp(de->d_name, "dev-", 4) != 0) | |
244 | continue; | |
245 | strcpy(base, de->d_name); | |
246 | dbase = base + strlen(base); | |
247 | *dbase++ = '/'; | |
248 | ||
249 | dev = malloc(sizeof(*dev)); | |
250 | if (!dev) | |
251 | goto abort; | |
252 | ||
253 | /* Always get slot, major, minor */ | |
254 | strcpy(dbase, "slot"); | |
255 | if (load_sys(fname, buf)) { | |
256 | /* hmm... unable to read 'slot' maybe the device | |
257 | * is going away? | |
258 | */ | |
259 | strcpy(dbase, "block"); | |
260 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
261 | errno != ENAMETOOLONG) { | |
262 | /* ...yup device is gone */ | |
263 | free(dev); | |
264 | continue; | |
265 | } else { | |
266 | /* slot is unreadable but 'block' link | |
267 | * still intact... something bad is happening | |
268 | * so abort | |
269 | */ | |
270 | free(dev); | |
271 | goto abort; | |
272 | } | |
273 | ||
274 | } | |
275 | strcpy(dev->sys_name, de->d_name); | |
276 | dev->disk.raid_disk = strtoul(buf, &ep, 10); | |
277 | if (*ep) dev->disk.raid_disk = -1; | |
278 | ||
279 | strcpy(dbase, "block/dev"); | |
280 | if (load_sys(fname, buf)) { | |
281 | free(dev); | |
282 | if (options & SKIP_GONE_DEVS) | |
283 | continue; | |
284 | else | |
285 | goto abort; | |
286 | } | |
287 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); | |
288 | ||
289 | /* special case check for block devices that can go 'offline' */ | |
290 | if (options & SKIP_GONE_DEVS) { | |
291 | strcpy(dbase, "block/device/state"); | |
292 | if (load_sys(fname, buf) == 0 && | |
293 | strncmp(buf, "offline", 7) == 0) { | |
294 | free(dev); | |
295 | continue; | |
296 | } | |
297 | } | |
298 | ||
299 | /* finally add this disk to the array */ | |
300 | dev->next = sra->devs; | |
301 | sra->devs = dev; | |
302 | ||
303 | if (options & GET_OFFSET) { | |
304 | strcpy(dbase, "offset"); | |
305 | if (load_sys(fname, buf)) | |
306 | goto abort; | |
307 | dev->data_offset = strtoull(buf, NULL, 0); | |
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) != 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 == 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 | 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_uevent(struct mdinfo *sra, char *event) | |
430 | { | |
431 | char fname[50]; | |
432 | int n; | |
433 | int fd; | |
434 | ||
435 | sprintf(fname, "/sys/block/%s/uevent", | |
436 | sra->sys_name); | |
437 | fd = open(fname, O_WRONLY); | |
438 | if (fd < 0) | |
439 | return -1; | |
440 | n = write(fd, event, strlen(event)); | |
441 | close(fd); | |
442 | return 0; | |
443 | } | |
444 | ||
445 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, | |
446 | char *name, unsigned long long *val) | |
447 | { | |
448 | char fname[50]; | |
449 | char buf[50]; | |
450 | int n; | |
451 | int fd; | |
452 | char *ep; | |
453 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
454 | sra->sys_name, dev?dev->sys_name:"", name); | |
455 | fd = open(fname, O_RDONLY); | |
456 | if (fd < 0) | |
457 | return -1; | |
458 | n = read(fd, buf, sizeof(buf)); | |
459 | close(fd); | |
460 | if (n <= 0) | |
461 | return -1; | |
462 | buf[n] = 0; | |
463 | *val = strtoull(buf, &ep, 0); | |
464 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
465 | return -1; | |
466 | return 0; | |
467 | } | |
468 | ||
469 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, | |
470 | char *name, char *val, int size) | |
471 | { | |
472 | char fname[50]; | |
473 | int n; | |
474 | int fd; | |
475 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
476 | sra->sys_name, dev?dev->sys_name:"", name); | |
477 | fd = open(fname, O_RDONLY); | |
478 | if (fd < 0) | |
479 | return -1; | |
480 | n = read(fd, val, size); | |
481 | close(fd); | |
482 | if (n <= 0) | |
483 | return -1; | |
484 | val[n] = 0; | |
485 | return n; | |
486 | } | |
487 | ||
488 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) | |
489 | { | |
490 | unsigned long sec; | |
491 | unsigned long msec; | |
492 | char delay[30]; | |
493 | ||
494 | sec = ms / 1000; | |
495 | msec = ms % 1000; | |
496 | ||
497 | sprintf(delay, "%ld.%03ld\n", sec, msec); | |
498 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
499 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); | |
500 | } | |
501 | ||
502 | int sysfs_set_array(struct mdinfo *info, int vers) | |
503 | { | |
504 | int rv = 0; | |
505 | char ver[100]; | |
506 | ||
507 | ver[0] = 0; | |
508 | if (info->array.major_version == -1 && | |
509 | info->array.minor_version == -2) { | |
510 | strcat(strcpy(ver, "external:"), info->text_version); | |
511 | ||
512 | if ((vers % 100) < 2 || | |
513 | sysfs_set_str(info, NULL, "metadata_version", | |
514 | ver) < 0) { | |
515 | fprintf(stderr, Name ": This kernel does not " | |
516 | "support external metadata.\n"); | |
517 | return 1; | |
518 | } | |
519 | } | |
520 | if (info->array.level < 0) | |
521 | return 0; /* FIXME */ | |
522 | rv |= sysfs_set_str(info, NULL, "level", | |
523 | map_num(pers, info->array.level)); | |
524 | rv |= sysfs_set_num(info, NULL, "raid_disks", info->array.raid_disks); | |
525 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); | |
526 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
527 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
528 | if (info->custom_array_size) { | |
529 | int rc; | |
530 | ||
531 | rc = sysfs_set_num(info, NULL, "array_size", | |
532 | info->custom_array_size/2); | |
533 | if (rc && errno == ENOENT) { | |
534 | fprintf(stderr, Name ": This kernel does not " | |
535 | "have the md/array_size attribute, " | |
536 | "the array may be larger than expected\n"); | |
537 | rc = 0; | |
538 | } | |
539 | rv |= rc; | |
540 | } | |
541 | ||
542 | if (info->array.level > 0) | |
543 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
544 | return rv; | |
545 | } | |
546 | ||
547 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int in_sync) | |
548 | { | |
549 | char dv[100]; | |
550 | char nm[100]; | |
551 | char *dname; | |
552 | int rv; | |
553 | ||
554 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
555 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
556 | if (rv) | |
557 | return rv; | |
558 | ||
559 | memset(nm, 0, sizeof(nm)); | |
560 | sprintf(dv, "/sys/dev/block/%d:%d", sd->disk.major, sd->disk.minor); | |
561 | rv = readlink(dv, nm, sizeof(nm)); | |
562 | if (rv <= 0) | |
563 | return -1; | |
564 | nm[rv] = '\0'; | |
565 | dname = strrchr(nm, '/'); | |
566 | if (dname) dname++; | |
567 | strcpy(sd->sys_name, "dev-"); | |
568 | strcpy(sd->sys_name+4, dname); | |
569 | ||
570 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); | |
571 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); | |
572 | if (sra->array.level != LEVEL_CONTAINER) { | |
573 | if (in_sync) | |
574 | /* This can correctly fail if array isn't started, | |
575 | * yet, so just ignore status for now. | |
576 | */ | |
577 | sysfs_set_str(sra, sd, "state", "in_sync"); | |
578 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
579 | } | |
580 | return rv; | |
581 | } | |
582 | ||
583 | #if 0 | |
584 | int sysfs_disk_to_sg(int fd) | |
585 | { | |
586 | /* from an open block device, try find and open its corresponding | |
587 | * scsi_generic interface | |
588 | */ | |
589 | struct stat st; | |
590 | char path[256]; | |
591 | char sg_path[256]; | |
592 | char sg_major_minor[8]; | |
593 | char *c; | |
594 | DIR *dir; | |
595 | struct dirent *de; | |
596 | int major, minor, rv; | |
597 | ||
598 | if (fstat(fd, &st)) | |
599 | return -1; | |
600 | ||
601 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
602 | major(st.st_rdev), minor(st.st_rdev)); | |
603 | ||
604 | dir = opendir(path); | |
605 | if (!dir) | |
606 | return -1; | |
607 | ||
608 | de = readdir(dir); | |
609 | while (de) { | |
610 | if (strncmp("scsi_generic:", de->d_name, | |
611 | strlen("scsi_generic:")) == 0) | |
612 | break; | |
613 | de = readdir(dir); | |
614 | } | |
615 | closedir(dir); | |
616 | ||
617 | if (!de) | |
618 | return -1; | |
619 | ||
620 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
621 | fd = open(sg_path, O_RDONLY); | |
622 | if (fd < 0) | |
623 | return fd; | |
624 | ||
625 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
626 | close(fd); | |
627 | if (rv < 0) | |
628 | return -1; | |
629 | else | |
630 | sg_major_minor[rv - 1] = '\0'; | |
631 | ||
632 | c = strchr(sg_major_minor, ':'); | |
633 | *c = '\0'; | |
634 | c++; | |
635 | major = strtol(sg_major_minor, NULL, 10); | |
636 | minor = strtol(c, NULL, 10); | |
637 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
638 | (int) getpid(), major, minor); | |
639 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
640 | fd = open(path, O_RDONLY); | |
641 | unlink(path); | |
642 | return fd; | |
643 | } | |
644 | ||
645 | return -1; | |
646 | } | |
647 | #endif | |
648 | ||
649 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) | |
650 | { | |
651 | /* from an open block device, try to retrieve it scsi_id */ | |
652 | struct stat st; | |
653 | char path[256]; | |
654 | char *c1, *c2; | |
655 | DIR *dir; | |
656 | struct dirent *de; | |
657 | ||
658 | if (fstat(fd, &st)) | |
659 | return 1; | |
660 | ||
661 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
662 | major(st.st_rdev), minor(st.st_rdev)); | |
663 | ||
664 | dir = opendir(path); | |
665 | if (!dir) | |
666 | return 1; | |
667 | ||
668 | de = readdir(dir); | |
669 | while (de) { | |
670 | if (strncmp("scsi_disk:", de->d_name, | |
671 | strlen("scsi_disk:")) == 0) | |
672 | break; | |
673 | de = readdir(dir); | |
674 | } | |
675 | closedir(dir); | |
676 | ||
677 | if (!de) | |
678 | return 1; | |
679 | ||
680 | c1 = strchr(de->d_name, ':'); | |
681 | c1++; | |
682 | c2 = strchr(c1, ':'); | |
683 | *c2 = '\0'; | |
684 | *id = strtol(c1, NULL, 10) << 24; /* host */ | |
685 | c1 = c2 + 1; | |
686 | c2 = strchr(c1, ':'); | |
687 | *c2 = '\0'; | |
688 | *id |= strtol(c1, NULL, 10) << 16; /* channel */ | |
689 | c1 = c2 + 1; | |
690 | c2 = strchr(c1, ':'); | |
691 | *c2 = '\0'; | |
692 | *id |= strtol(c1, NULL, 10) << 8; /* lun */ | |
693 | c1 = c2 + 1; | |
694 | *id |= strtol(c1, NULL, 10); /* id */ | |
695 | ||
696 | return 0; | |
697 | } | |
698 | ||
699 | ||
700 | int sysfs_unique_holder(int devnum, long rdev) | |
701 | { | |
702 | /* Check that devnum is a holder of rdev, | |
703 | * and is the only holder. | |
704 | * we should be locked against races by | |
705 | * an O_EXCL on devnum | |
706 | */ | |
707 | DIR *dir; | |
708 | struct dirent *de; | |
709 | char dirname[100]; | |
710 | char l; | |
711 | int found = 0; | |
712 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", | |
713 | major(rdev), minor(rdev)); | |
714 | dir = opendir(dirname); | |
715 | errno = ENOENT; | |
716 | if (!dir) | |
717 | return 0; | |
718 | l = strlen(dirname); | |
719 | while ((de = readdir(dir)) != NULL) { | |
720 | char buf[10]; | |
721 | int n; | |
722 | int mj, mn; | |
723 | char c; | |
724 | int fd; | |
725 | ||
726 | if (de->d_ino == 0) | |
727 | continue; | |
728 | if (de->d_name[0] == '.') | |
729 | continue; | |
730 | strcpy(dirname+l, "/"); | |
731 | strcat(dirname+l, de->d_name); | |
732 | strcat(dirname+l, "/dev"); | |
733 | fd = open(dirname, O_RDONLY); | |
734 | if (fd < 0) { | |
735 | errno = ENOENT; | |
736 | break; | |
737 | } | |
738 | n = read(fd, buf, sizeof(buf)-1); | |
739 | close(fd); | |
740 | buf[n] = 0; | |
741 | if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 || | |
742 | c != '\n') { | |
743 | errno = ENOENT; | |
744 | break; | |
745 | } | |
746 | if (mj != MD_MAJOR) | |
747 | mn = -1-(mn>>6); | |
748 | ||
749 | if (devnum != mn) { | |
750 | errno = EEXIST; | |
751 | break; | |
752 | } | |
753 | found = 1; | |
754 | } | |
755 | closedir(dir); | |
756 | if (de) | |
757 | return 0; | |
758 | else | |
759 | return found; | |
760 | } |