]>
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]; | |
2503d23b NB |
459 | char *dname; |
460 | int rv; | |
461 | ||
462 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
463 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
464 | if (rv) | |
465 | return rv; | |
466 | ||
467 | memset(nm, 0, sizeof(nm)); | |
468 | sprintf(dv, "/sys/dev/block/%d:%d", sd->disk.major, sd->disk.minor); | |
73649188 N |
469 | rv = readlink(dv, nm, sizeof(nm)); |
470 | if (rv <= 0) | |
2503d23b | 471 | return -1; |
73649188 | 472 | nm[rv] = '\0'; |
2503d23b NB |
473 | dname = strrchr(nm, '/'); |
474 | if (dname) dname++; | |
475 | strcpy(sd->sys_name, "dev-"); | |
476 | strcpy(sd->sys_name+4, dname); | |
477 | ||
73649188 | 478 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b NB |
479 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
480 | if (sra->array.level != LEVEL_CONTAINER) { | |
481 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
482 | // rv |= sysfs_set_str(sra, sd, "state", "in_sync"); | |
483 | } | |
2503d23b NB |
484 | return rv; |
485 | } | |
90c8b707 | 486 | |
755c99fa | 487 | #if 0 |
90c8b707 DW |
488 | int sysfs_disk_to_sg(int fd) |
489 | { | |
490 | /* from an open block device, try find and open its corresponding | |
491 | * scsi_generic interface | |
492 | */ | |
493 | struct stat st; | |
494 | char path[256]; | |
495 | char sg_path[256]; | |
496 | char sg_major_minor[8]; | |
497 | char *c; | |
498 | DIR *dir; | |
499 | struct dirent *de; | |
500 | int major, minor, rv; | |
501 | ||
502 | if (fstat(fd, &st)) | |
503 | return -1; | |
504 | ||
505 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
506 | major(st.st_rdev), minor(st.st_rdev)); | |
507 | ||
508 | dir = opendir(path); | |
509 | if (!dir) | |
510 | return -1; | |
511 | ||
512 | de = readdir(dir); | |
513 | while (de) { | |
514 | if (strncmp("scsi_generic:", de->d_name, | |
515 | strlen("scsi_generic:")) == 0) | |
516 | break; | |
517 | de = readdir(dir); | |
518 | } | |
519 | closedir(dir); | |
520 | ||
521 | if (!de) | |
522 | return -1; | |
523 | ||
524 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
525 | fd = open(sg_path, O_RDONLY); | |
526 | if (fd < 0) | |
527 | return fd; | |
528 | ||
529 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
530 | close(fd); | |
531 | if (rv < 0) | |
532 | return -1; | |
533 | else | |
534 | sg_major_minor[rv - 1] = '\0'; | |
535 | ||
536 | c = strchr(sg_major_minor, ':'); | |
537 | *c = '\0'; | |
538 | c++; | |
539 | major = strtol(sg_major_minor, NULL, 10); | |
540 | minor = strtol(c, NULL, 10); | |
541 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
542 | (int) getpid(), major, minor); | |
543 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
544 | fd = open(path, O_RDONLY); | |
545 | unlink(path); | |
546 | return fd; | |
547 | } | |
548 | ||
549 | return -1; | |
550 | } | |
755c99fa | 551 | #endif |
90c8b707 | 552 | |
f1665f72 DW |
553 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
554 | { | |
555 | /* from an open block device, try to retrieve it scsi_id */ | |
556 | struct stat st; | |
557 | char path[256]; | |
558 | char *c1, *c2; | |
559 | DIR *dir; | |
560 | struct dirent *de; | |
561 | ||
562 | if (fstat(fd, &st)) | |
563 | return 1; | |
564 | ||
565 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
566 | major(st.st_rdev), minor(st.st_rdev)); | |
567 | ||
568 | dir = opendir(path); | |
569 | if (!dir) | |
570 | return 1; | |
571 | ||
572 | de = readdir(dir); | |
573 | while (de) { | |
574 | if (strncmp("scsi_disk:", de->d_name, | |
575 | strlen("scsi_disk:")) == 0) | |
576 | break; | |
577 | de = readdir(dir); | |
578 | } | |
579 | closedir(dir); | |
580 | ||
581 | if (!de) | |
582 | return 1; | |
583 | ||
584 | c1 = strchr(de->d_name, ':'); | |
585 | c1++; | |
586 | c2 = strchr(c1, ':'); | |
587 | *c2 = '\0'; | |
588 | *id = strtol(c1, NULL, 10) << 24; /* host */ | |
589 | c1 = c2 + 1; | |
590 | c2 = strchr(c1, ':'); | |
591 | *c2 = '\0'; | |
592 | *id |= strtol(c1, NULL, 10) << 16; /* channel */ | |
593 | c1 = c2 + 1; | |
594 | c2 = strchr(c1, ':'); | |
595 | *c2 = '\0'; | |
596 | *id |= strtol(c1, NULL, 10) << 8; /* lun */ | |
597 | c1 = c2 + 1; | |
598 | *id |= strtol(c1, NULL, 10); /* id */ | |
599 | ||
600 | return 0; | |
601 | } | |
f94d52f4 NB |
602 | |
603 | ||
604 | int sysfs_unique_holder(int devnum, long rdev) | |
605 | { | |
606 | /* Check that devnum is a holder of rdev, | |
607 | * and is the only holder. | |
608 | * we should be locked against races by | |
609 | * an O_EXCL on devnum | |
610 | */ | |
611 | DIR *dir; | |
612 | struct dirent *de; | |
613 | char dirname[100]; | |
614 | char l; | |
615 | int found = 0; | |
616 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", | |
617 | major(rdev), minor(rdev)); | |
618 | dir = opendir(dirname); | |
619 | errno = ENOENT; | |
620 | if (!dir) | |
621 | return 0; | |
622 | l = strlen(dirname); | |
623 | while ((de = readdir(dir)) != NULL) { | |
624 | char buf[10]; | |
625 | int n; | |
626 | int mj, mn; | |
627 | char c; | |
628 | int fd; | |
629 | ||
630 | if (de->d_ino == 0) | |
631 | continue; | |
632 | if (de->d_name[0] == '.') | |
633 | continue; | |
634 | strcpy(dirname+l, "/"); | |
635 | strcat(dirname+l, de->d_name); | |
636 | strcat(dirname+l, "/dev"); | |
637 | fd = open(dirname, O_RDONLY); | |
638 | if (fd < 0) { | |
639 | errno = ENOENT; | |
640 | break; | |
641 | } | |
642 | n = read(fd, buf, sizeof(buf)-1); | |
643 | close(fd); | |
644 | buf[n] = 0; | |
645 | if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 || | |
646 | c != '\n') { | |
647 | errno = ENOENT; | |
648 | break; | |
649 | } | |
650 | if (mj != MD_MAJOR) | |
651 | mn = -1-(mn>>6); | |
652 | ||
653 | if (devnum != mn) { | |
654 | errno = EEXIST; | |
655 | break; | |
656 | } | |
657 | found = 1; | |
658 | } | |
659 | closedir(dir); | |
660 | if (de) | |
661 | return 0; | |
662 | else | |
663 | return found; | |
664 | } |