]>
Commit | Line | Data |
---|---|---|
e86c9dd6 NB |
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> | |
1770662b | 28 | #include <ctype.h> |
e86c9dd6 NB |
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); | |
2f6079dc | 38 | if (n <0 || n >= 1024) |
e86c9dd6 NB |
39 | return -1; |
40 | buf[n] = 0; | |
8dfb8619 | 41 | if (n && buf[n-1] == '\n') |
e86c9dd6 NB |
42 | buf[n-1] = 0; |
43 | return 0; | |
44 | } | |
45 | ||
7e0f6979 | 46 | void sysfs_free(struct mdinfo *sra) |
8382f19b | 47 | { |
7e0f6979 NB |
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; | |
8382f19b | 57 | } |
8382f19b NB |
58 | } |
59 | ||
549e9569 NB |
60 | int sysfs_open(int devnum, char *devname, char *attr) |
61 | { | |
62 | char fname[50]; | |
549e9569 | 63 | int fd; |
549e9569 | 64 | |
77472ff8 | 65 | sprintf(fname, "/sys/block/%s/md/", devnum2devname(devnum)); |
549e9569 NB |
66 | if (devname) { |
67 | strcat(fname, devname); | |
68 | strcat(fname, "/"); | |
69 | } | |
70 | strcat(fname, attr); | |
71 | fd = open(fname, O_RDWR); | |
ea6d09b0 | 72 | if (fd < 0 && errno == EACCES) |
549e9569 NB |
73 | fd = open(fname, O_RDONLY); |
74 | return fd; | |
75 | } | |
76 | ||
7e0f6979 | 77 | struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options) |
e86c9dd6 NB |
78 | { |
79 | /* Longest possible name in sysfs, mounted at /sys, is | |
80 | * /sys/block/md_dXXX/md/dev-XXXXX/block/dev | |
81 | * /sys/block/md_dXXX/md/metadata_version | |
82 | * which is about 41 characters. 50 should do for now | |
83 | */ | |
84 | char fname[50]; | |
85 | char buf[1024]; | |
86 | char *base; | |
87 | char *dbase; | |
7e0f6979 | 88 | struct mdinfo *sra; |
06c7f68e | 89 | struct mdinfo *dev; |
355726fa | 90 | DIR *dir = NULL; |
e86c9dd6 NB |
91 | struct dirent *de; |
92 | ||
93 | sra = malloc(sizeof(*sra)); | |
94 | if (sra == NULL) | |
95 | return sra; | |
7e0f6979 | 96 | sra->next = NULL; |
e86c9dd6 NB |
97 | |
98 | if (fd >= 0) { | |
99 | struct stat stb; | |
2faf1f5f NB |
100 | mdu_version_t vers; |
101 | if (fstat(fd, &stb)) return NULL; | |
102 | if (ioctl(fd, RAID_VERSION, &vers) != 0) | |
103 | return NULL; | |
e86c9dd6 | 104 | if (major(stb.st_rdev)==9) |
ea24acd0 | 105 | sprintf(sra->sys_name, "md%d", (int)minor(stb.st_rdev)); |
e86c9dd6 | 106 | else |
7e0f6979 | 107 | sprintf(sra->sys_name, "md_d%d", |
ea24acd0 | 108 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
109 | } else { |
110 | if (devnum >= 0) | |
7e0f6979 | 111 | sprintf(sra->sys_name, "md%d", devnum); |
e86c9dd6 | 112 | else |
7e0f6979 | 113 | sprintf(sra->sys_name, "md_d%d", |
e86c9dd6 NB |
114 | -1-devnum); |
115 | } | |
7e0f6979 | 116 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); |
e86c9dd6 NB |
117 | base = fname + strlen(fname); |
118 | ||
119 | sra->devs = NULL; | |
8382f19b NB |
120 | if (options & GET_VERSION) { |
121 | strcpy(base, "metadata_version"); | |
122 | if (load_sys(fname, buf)) | |
123 | goto abort; | |
294d6f45 | 124 | if (strncmp(buf, "none", 4) == 0) { |
7e0f6979 NB |
125 | sra->array.major_version = |
126 | sra->array.minor_version = -1; | |
294d6f45 NB |
127 | strcpy(sra->text_version, ""); |
128 | } else if (strncmp(buf, "external:", 9) == 0) { | |
142cb9e1 NB |
129 | sra->array.major_version = -1; |
130 | sra->array.minor_version = -2; | |
131 | strcpy(sra->text_version, buf+9); | |
b8ac1967 | 132 | } else { |
8382f19b | 133 | sscanf(buf, "%d.%d", |
7e0f6979 NB |
134 | &sra->array.major_version, |
135 | &sra->array.minor_version); | |
b8ac1967 NB |
136 | strcpy(sra->text_version, buf); |
137 | } | |
8382f19b | 138 | } |
e86c9dd6 NB |
139 | if (options & GET_LEVEL) { |
140 | strcpy(base, "level"); | |
141 | if (load_sys(fname, buf)) | |
142 | goto abort; | |
7e0f6979 | 143 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
144 | } |
145 | if (options & GET_LAYOUT) { | |
146 | strcpy(base, "layout"); | |
147 | if (load_sys(fname, buf)) | |
148 | goto abort; | |
7e0f6979 | 149 | sra->array.layout = strtoul(buf, NULL, 0); |
e86c9dd6 | 150 | } |
549e9569 NB |
151 | if (options & GET_DISKS) { |
152 | strcpy(base, "raid_disks"); | |
153 | if (load_sys(fname, buf)) | |
154 | goto abort; | |
155 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
f1d26766 DW |
156 | } |
157 | if (options & GET_DEGRADED) { | |
158 | strcpy(base, "degraded"); | |
159 | if (load_sys(fname, buf)) | |
160 | goto abort; | |
161 | sra->array.failed_disks = strtoul(buf, NULL, 0); | |
549e9569 | 162 | } |
e86c9dd6 NB |
163 | if (options & GET_COMPONENT) { |
164 | strcpy(base, "component_size"); | |
165 | if (load_sys(fname, buf)) | |
166 | goto abort; | |
167 | sra->component_size = strtoull(buf, NULL, 0); | |
353632d9 NB |
168 | /* sysfs reports "K", but we want sectors */ |
169 | sra->component_size *= 2; | |
e86c9dd6 NB |
170 | } |
171 | if (options & GET_CHUNK) { | |
172 | strcpy(base, "chunk_size"); | |
173 | if (load_sys(fname, buf)) | |
174 | goto abort; | |
7e0f6979 | 175 | sra->array.chunk_size = strtoul(buf, NULL, 0); |
e86c9dd6 | 176 | } |
758d3a8e NB |
177 | if (options & GET_CACHE) { |
178 | strcpy(base, "stripe_cache_size"); | |
179 | if (load_sys(fname, buf)) | |
180 | goto abort; | |
181 | sra->cache_size = strtoul(buf, NULL, 0); | |
182 | } | |
37dfc3d6 NB |
183 | if (options & GET_MISMATCH) { |
184 | strcpy(base, "mismatch_cnt"); | |
185 | if (load_sys(fname, buf)) | |
186 | goto abort; | |
187 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
188 | } | |
1770662b DW |
189 | if (options & GET_SAFEMODE) { |
190 | int scale = 1; | |
191 | int dot = 0; | |
192 | int i; | |
193 | unsigned long msec; | |
194 | size_t len; | |
195 | ||
196 | strcpy(base, "safe_mode_delay"); | |
197 | if (load_sys(fname, buf)) | |
198 | goto abort; | |
199 | ||
200 | /* remove a period, and count digits after it */ | |
201 | len = strlen(buf); | |
202 | for (i = 0; i < len; i++) { | |
203 | if (dot) { | |
204 | if (isdigit(buf[i])) { | |
205 | buf[i-1] = buf[i]; | |
206 | scale *= 10; | |
207 | } | |
208 | buf[i] = 0; | |
209 | } else if (buf[i] == '.') { | |
210 | dot=1; | |
211 | buf[i] = 0; | |
212 | } | |
213 | } | |
214 | msec = strtoul(buf, NULL, 10); | |
215 | msec = (msec * 1000) / scale; | |
216 | sra->safe_mode_delay = msec; | |
217 | } | |
e86c9dd6 NB |
218 | |
219 | if (! (options & GET_DEVS)) | |
220 | return sra; | |
221 | ||
222 | /* Get all the devices as well */ | |
223 | *base = 0; | |
224 | dir = opendir(fname); | |
225 | if (!dir) | |
226 | goto abort; | |
7e0f6979 | 227 | sra->array.spare_disks = 0; |
e86c9dd6 NB |
228 | |
229 | while ((de = readdir(dir)) != NULL) { | |
230 | char *ep; | |
231 | if (de->d_ino == 0 || | |
232 | strncmp(de->d_name, "dev-", 4) != 0) | |
233 | continue; | |
234 | strcpy(base, de->d_name); | |
235 | dbase = base + strlen(base); | |
236 | *dbase++ = '/'; | |
237 | ||
238 | dev = malloc(sizeof(*dev)); | |
239 | if (!dev) | |
240 | goto abort; | |
e86c9dd6 NB |
241 | |
242 | /* Always get slot, major, minor */ | |
243 | strcpy(dbase, "slot"); | |
4795982e DW |
244 | if (load_sys(fname, buf)) { |
245 | /* hmm... unable to read 'slot' maybe the device | |
246 | * is going away? | |
247 | */ | |
248 | strcpy(dbase, "block"); | |
249 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
250 | errno != ENAMETOOLONG) { | |
251 | /* ...yup device is gone */ | |
252 | free(dev); | |
253 | continue; | |
254 | } else { | |
255 | /* slot is unreadable but 'block' link | |
256 | * still intact... something bad is happening | |
257 | * so abort | |
258 | */ | |
259 | free(dev); | |
260 | goto abort; | |
261 | } | |
262 | ||
263 | } | |
264 | dev->next = sra->devs; | |
265 | sra->devs = dev; | |
266 | ||
267 | strcpy(dev->sys_name, de->d_name); | |
06c7f68e NB |
268 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
269 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
270 | |
271 | strcpy(dbase, "block/dev"); | |
272 | if (load_sys(fname, buf)) | |
273 | goto abort; | |
06c7f68e | 274 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 NB |
275 | |
276 | if (options & GET_OFFSET) { | |
277 | strcpy(dbase, "offset"); | |
278 | if (load_sys(fname, buf)) | |
279 | goto abort; | |
06c7f68e | 280 | dev->data_offset = strtoull(buf, NULL, 0); |
e86c9dd6 NB |
281 | } |
282 | if (options & GET_SIZE) { | |
283 | strcpy(dbase, "size"); | |
284 | if (load_sys(fname, buf)) | |
285 | goto abort; | |
047d2e49 | 286 | dev->component_size = strtoull(buf, NULL, 0) * 2; |
e86c9dd6 NB |
287 | } |
288 | if (options & GET_STATE) { | |
06c7f68e | 289 | dev->disk.state = 0; |
e86c9dd6 NB |
290 | strcpy(dbase, "state"); |
291 | if (load_sys(fname, buf)) | |
292 | goto abort; | |
293 | if (strstr(buf, "in_sync")) | |
06c7f68e | 294 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 295 | if (strstr(buf, "faulty")) |
06c7f68e NB |
296 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
297 | if (dev->disk.state == 0) | |
7e0f6979 | 298 | sra->array.spare_disks++; |
e86c9dd6 NB |
299 | } |
300 | if (options & GET_ERROR) { | |
301 | strcpy(buf, "errors"); | |
302 | if (load_sys(fname, buf)) | |
303 | goto abort; | |
304 | dev->errors = strtoul(buf, NULL, 0); | |
305 | } | |
306 | } | |
355726fa | 307 | closedir(dir); |
e86c9dd6 NB |
308 | return sra; |
309 | ||
310 | abort: | |
355726fa NB |
311 | if (dir) |
312 | closedir(dir); | |
8382f19b | 313 | sysfs_free(sra); |
e86c9dd6 NB |
314 | return NULL; |
315 | } | |
316 | ||
1770662b DW |
317 | int sysfs_attr_match(const char *attr, const char *str) |
318 | { | |
319 | /* See if attr, read from a sysfs file, matches | |
320 | * str. They must either be the same, or attr can | |
321 | * have a trailing newline or comma | |
322 | */ | |
323 | while (*attr && *str && *attr == *str) { | |
324 | attr++; | |
325 | str++; | |
326 | } | |
327 | ||
328 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
329 | return 0; | |
330 | return 1; | |
331 | } | |
332 | ||
333 | int sysfs_match_word(const char *word, char **list) | |
334 | { | |
335 | int n; | |
336 | for (n=0; list[n]; n++) | |
337 | if (sysfs_attr_match(word, list[n])) | |
338 | break; | |
339 | return n; | |
340 | } | |
341 | ||
e86c9dd6 NB |
342 | unsigned long long get_component_size(int fd) |
343 | { | |
344 | /* Find out the component size of the array. | |
345 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
346 | * size field is only 32bits. | |
347 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 348 | * |
8686f3ed | 349 | * This returns in units of sectors. |
e86c9dd6 NB |
350 | */ |
351 | struct stat stb; | |
352 | char fname[50]; | |
353 | int n; | |
354 | if (fstat(fd, &stb)) return 0; | |
355 | if (major(stb.st_rdev) == 9) | |
356 | sprintf(fname, "/sys/block/md%d/md/component_size", | |
ea24acd0 | 357 | (int)minor(stb.st_rdev)); |
e86c9dd6 NB |
358 | else |
359 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
ea24acd0 | 360 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
361 | fd = open(fname, O_RDONLY); |
362 | if (fd < 0) | |
363 | return 0; | |
364 | n = read(fd, fname, sizeof(fname)); | |
365 | close(fd); | |
366 | if (n == sizeof(fname)) | |
367 | return 0; | |
368 | fname[n] = 0; | |
8686f3ed | 369 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
370 | } |
371 | ||
7e0f6979 | 372 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
373 | char *name, char *val) |
374 | { | |
375 | char fname[50]; | |
376 | int n; | |
377 | int fd; | |
7e1432fb | 378 | |
e86c9dd6 | 379 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 380 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
381 | fd = open(fname, O_WRONLY); |
382 | if (fd < 0) | |
383 | return -1; | |
384 | n = write(fd, val, strlen(val)); | |
385 | close(fd); | |
386 | if (n != strlen(val)) | |
387 | return -1; | |
388 | return 0; | |
389 | } | |
390 | ||
7e0f6979 | 391 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
392 | char *name, unsigned long long val) |
393 | { | |
394 | char valstr[50]; | |
395 | sprintf(valstr, "%llu", val); | |
396 | return sysfs_set_str(sra, dev, name, valstr); | |
397 | } | |
398 | ||
7e0f6979 | 399 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
400 | char *name, unsigned long long *val) |
401 | { | |
402 | char fname[50]; | |
403 | char buf[50]; | |
404 | int n; | |
405 | int fd; | |
406 | char *ep; | |
407 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
7e0f6979 | 408 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
409 | fd = open(fname, O_RDONLY); |
410 | if (fd < 0) | |
411 | return -1; | |
412 | n = read(fd, buf, sizeof(buf)); | |
413 | close(fd); | |
414 | if (n <= 0) | |
415 | return -1; | |
416 | buf[n] = 0; | |
417 | *val = strtoull(buf, &ep, 0); | |
418 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
419 | return -1; | |
420 | return 0; | |
421 | } | |
2503d23b | 422 | |
8ed3e5e1 DW |
423 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) |
424 | { | |
425 | unsigned long sec; | |
426 | unsigned long msec; | |
427 | char delay[30]; | |
428 | ||
429 | sec = ms / 1000; | |
430 | msec = ms - (sec * 1000); | |
431 | ||
432 | sprintf(delay, "%ld.%ld", sec, msec); | |
433 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); | |
434 | } | |
435 | ||
2503d23b NB |
436 | int sysfs_set_array(struct mdinfo *sra, |
437 | struct mdinfo *info) | |
438 | { | |
439 | int rv = 0; | |
440 | sra->array = info->array; | |
2f6079dc | 441 | |
2503d23b NB |
442 | if (info->array.level < 0) |
443 | return 0; /* FIXME */ | |
444 | rv |= sysfs_set_str(sra, NULL, "level", | |
445 | map_num(pers, info->array.level)); | |
446 | rv |= sysfs_set_num(sra, NULL, "raid_disks", info->array.raid_disks); | |
447 | rv |= sysfs_set_num(sra, NULL, "chunk_size", info->array.chunk_size); | |
448 | rv |= sysfs_set_num(sra, NULL, "layout", info->array.layout); | |
047d2e49 | 449 | rv |= sysfs_set_num(sra, NULL, "component_size", info->component_size/2); |
0fd5c350 | 450 | rv |= sysfs_set_num(sra, NULL, "resync_start", info->resync_start); |
2503d23b NB |
451 | sra->array = info->array; |
452 | return rv; | |
453 | } | |
454 | ||
2318b9f0 | 455 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd) |
2503d23b NB |
456 | { |
457 | char dv[100]; | |
458 | char nm[100]; | |
459 | struct mdinfo *sd2; | |
460 | char *dname; | |
461 | int rv; | |
462 | ||
463 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
464 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
465 | if (rv) | |
466 | return rv; | |
467 | ||
468 | memset(nm, 0, sizeof(nm)); | |
469 | sprintf(dv, "/sys/dev/block/%d:%d", sd->disk.major, sd->disk.minor); | |
73649188 N |
470 | rv = readlink(dv, nm, sizeof(nm)); |
471 | if (rv <= 0) | |
2503d23b | 472 | return -1; |
73649188 | 473 | nm[rv] = '\0'; |
2503d23b NB |
474 | dname = strrchr(nm, '/'); |
475 | if (dname) dname++; | |
476 | strcpy(sd->sys_name, "dev-"); | |
477 | strcpy(sd->sys_name+4, dname); | |
478 | ||
73649188 | 479 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b NB |
480 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
481 | if (sra->array.level != LEVEL_CONTAINER) { | |
482 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
483 | // rv |= sysfs_set_str(sra, sd, "state", "in_sync"); | |
484 | } | |
3cb07116 NB |
485 | if (! rv) { |
486 | sd2 = malloc(sizeof(*sd2)); | |
487 | *sd2 = *sd; | |
488 | sd2->next = sra->devs; | |
489 | sra->devs = sd2; | |
490 | } | |
2503d23b NB |
491 | return rv; |
492 | } | |
90c8b707 | 493 | |
755c99fa | 494 | #if 0 |
90c8b707 DW |
495 | int sysfs_disk_to_sg(int fd) |
496 | { | |
497 | /* from an open block device, try find and open its corresponding | |
498 | * scsi_generic interface | |
499 | */ | |
500 | struct stat st; | |
501 | char path[256]; | |
502 | char sg_path[256]; | |
503 | char sg_major_minor[8]; | |
504 | char *c; | |
505 | DIR *dir; | |
506 | struct dirent *de; | |
507 | int major, minor, rv; | |
508 | ||
509 | if (fstat(fd, &st)) | |
510 | return -1; | |
511 | ||
512 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
513 | major(st.st_rdev), minor(st.st_rdev)); | |
514 | ||
515 | dir = opendir(path); | |
516 | if (!dir) | |
517 | return -1; | |
518 | ||
519 | de = readdir(dir); | |
520 | while (de) { | |
521 | if (strncmp("scsi_generic:", de->d_name, | |
522 | strlen("scsi_generic:")) == 0) | |
523 | break; | |
524 | de = readdir(dir); | |
525 | } | |
526 | closedir(dir); | |
527 | ||
528 | if (!de) | |
529 | return -1; | |
530 | ||
531 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
532 | fd = open(sg_path, O_RDONLY); | |
533 | if (fd < 0) | |
534 | return fd; | |
535 | ||
536 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
537 | close(fd); | |
538 | if (rv < 0) | |
539 | return -1; | |
540 | else | |
541 | sg_major_minor[rv - 1] = '\0'; | |
542 | ||
543 | c = strchr(sg_major_minor, ':'); | |
544 | *c = '\0'; | |
545 | c++; | |
546 | major = strtol(sg_major_minor, NULL, 10); | |
547 | minor = strtol(c, NULL, 10); | |
548 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
549 | (int) getpid(), major, minor); | |
550 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
551 | fd = open(path, O_RDONLY); | |
552 | unlink(path); | |
553 | return fd; | |
554 | } | |
555 | ||
556 | return -1; | |
557 | } | |
755c99fa | 558 | #endif |
90c8b707 | 559 | |
f1665f72 DW |
560 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
561 | { | |
562 | /* from an open block device, try to retrieve it scsi_id */ | |
563 | struct stat st; | |
564 | char path[256]; | |
565 | char *c1, *c2; | |
566 | DIR *dir; | |
567 | struct dirent *de; | |
568 | ||
569 | if (fstat(fd, &st)) | |
570 | return 1; | |
571 | ||
572 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
573 | major(st.st_rdev), minor(st.st_rdev)); | |
574 | ||
575 | dir = opendir(path); | |
576 | if (!dir) | |
577 | return 1; | |
578 | ||
579 | de = readdir(dir); | |
580 | while (de) { | |
581 | if (strncmp("scsi_disk:", de->d_name, | |
582 | strlen("scsi_disk:")) == 0) | |
583 | break; | |
584 | de = readdir(dir); | |
585 | } | |
586 | closedir(dir); | |
587 | ||
588 | if (!de) | |
589 | return 1; | |
590 | ||
591 | c1 = strchr(de->d_name, ':'); | |
592 | c1++; | |
593 | c2 = strchr(c1, ':'); | |
594 | *c2 = '\0'; | |
595 | *id = strtol(c1, NULL, 10) << 24; /* host */ | |
596 | c1 = c2 + 1; | |
597 | c2 = strchr(c1, ':'); | |
598 | *c2 = '\0'; | |
599 | *id |= strtol(c1, NULL, 10) << 16; /* channel */ | |
600 | c1 = c2 + 1; | |
601 | c2 = strchr(c1, ':'); | |
602 | *c2 = '\0'; | |
603 | *id |= strtol(c1, NULL, 10) << 8; /* lun */ | |
604 | c1 = c2 + 1; | |
605 | *id |= strtol(c1, NULL, 10); /* id */ | |
606 | ||
607 | return 0; | |
608 | } | |
f94d52f4 NB |
609 | |
610 | ||
611 | int sysfs_unique_holder(int devnum, long rdev) | |
612 | { | |
613 | /* Check that devnum is a holder of rdev, | |
614 | * and is the only holder. | |
615 | * we should be locked against races by | |
616 | * an O_EXCL on devnum | |
617 | */ | |
618 | DIR *dir; | |
619 | struct dirent *de; | |
620 | char dirname[100]; | |
621 | char l; | |
622 | int found = 0; | |
623 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", | |
624 | major(rdev), minor(rdev)); | |
625 | dir = opendir(dirname); | |
626 | errno = ENOENT; | |
627 | if (!dir) | |
628 | return 0; | |
629 | l = strlen(dirname); | |
630 | while ((de = readdir(dir)) != NULL) { | |
631 | char buf[10]; | |
632 | int n; | |
633 | int mj, mn; | |
634 | char c; | |
635 | int fd; | |
636 | ||
637 | if (de->d_ino == 0) | |
638 | continue; | |
639 | if (de->d_name[0] == '.') | |
640 | continue; | |
641 | strcpy(dirname+l, "/"); | |
642 | strcat(dirname+l, de->d_name); | |
643 | strcat(dirname+l, "/dev"); | |
644 | fd = open(dirname, O_RDONLY); | |
645 | if (fd < 0) { | |
646 | errno = ENOENT; | |
647 | break; | |
648 | } | |
649 | n = read(fd, buf, sizeof(buf)-1); | |
650 | close(fd); | |
651 | buf[n] = 0; | |
652 | if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 || | |
653 | c != '\n') { | |
654 | errno = ENOENT; | |
655 | break; | |
656 | } | |
657 | if (mj != MD_MAJOR) | |
658 | mn = -1-(mn>>6); | |
659 | ||
660 | if (devnum != mn) { | |
661 | errno = EEXIST; | |
662 | break; | |
663 | } | |
664 | found = 1; | |
665 | } | |
666 | closedir(dir); | |
667 | if (de) | |
668 | return 0; | |
669 | else | |
670 | return found; | |
671 | } |