]>
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; | |
241 | dev->next = sra->devs; | |
242 | sra->devs = dev; | |
06c7f68e | 243 | strcpy(dev->sys_name, de->d_name); |
e86c9dd6 NB |
244 | |
245 | /* Always get slot, major, minor */ | |
246 | strcpy(dbase, "slot"); | |
247 | if (load_sys(fname, buf)) | |
248 | goto abort; | |
06c7f68e NB |
249 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
250 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
251 | |
252 | strcpy(dbase, "block/dev"); | |
253 | if (load_sys(fname, buf)) | |
254 | goto abort; | |
06c7f68e | 255 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 NB |
256 | |
257 | if (options & GET_OFFSET) { | |
258 | strcpy(dbase, "offset"); | |
259 | if (load_sys(fname, buf)) | |
260 | goto abort; | |
06c7f68e | 261 | dev->data_offset = strtoull(buf, NULL, 0); |
e86c9dd6 NB |
262 | } |
263 | if (options & GET_SIZE) { | |
264 | strcpy(dbase, "size"); | |
265 | if (load_sys(fname, buf)) | |
266 | goto abort; | |
047d2e49 | 267 | dev->component_size = strtoull(buf, NULL, 0) * 2; |
e86c9dd6 NB |
268 | } |
269 | if (options & GET_STATE) { | |
06c7f68e | 270 | dev->disk.state = 0; |
e86c9dd6 NB |
271 | strcpy(dbase, "state"); |
272 | if (load_sys(fname, buf)) | |
273 | goto abort; | |
274 | if (strstr(buf, "in_sync")) | |
06c7f68e | 275 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 276 | if (strstr(buf, "faulty")) |
06c7f68e NB |
277 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
278 | if (dev->disk.state == 0) | |
7e0f6979 | 279 | sra->array.spare_disks++; |
e86c9dd6 NB |
280 | } |
281 | if (options & GET_ERROR) { | |
282 | strcpy(buf, "errors"); | |
283 | if (load_sys(fname, buf)) | |
284 | goto abort; | |
285 | dev->errors = strtoul(buf, NULL, 0); | |
286 | } | |
287 | } | |
355726fa | 288 | closedir(dir); |
e86c9dd6 NB |
289 | return sra; |
290 | ||
291 | abort: | |
355726fa NB |
292 | if (dir) |
293 | closedir(dir); | |
8382f19b | 294 | sysfs_free(sra); |
e86c9dd6 NB |
295 | return NULL; |
296 | } | |
297 | ||
1770662b DW |
298 | int sysfs_attr_match(const char *attr, const char *str) |
299 | { | |
300 | /* See if attr, read from a sysfs file, matches | |
301 | * str. They must either be the same, or attr can | |
302 | * have a trailing newline or comma | |
303 | */ | |
304 | while (*attr && *str && *attr == *str) { | |
305 | attr++; | |
306 | str++; | |
307 | } | |
308 | ||
309 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
310 | return 0; | |
311 | return 1; | |
312 | } | |
313 | ||
314 | int sysfs_match_word(const char *word, char **list) | |
315 | { | |
316 | int n; | |
317 | for (n=0; list[n]; n++) | |
318 | if (sysfs_attr_match(word, list[n])) | |
319 | break; | |
320 | return n; | |
321 | } | |
322 | ||
e86c9dd6 NB |
323 | unsigned long long get_component_size(int fd) |
324 | { | |
325 | /* Find out the component size of the array. | |
326 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
327 | * size field is only 32bits. | |
328 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 329 | * |
8686f3ed | 330 | * This returns in units of sectors. |
e86c9dd6 NB |
331 | */ |
332 | struct stat stb; | |
333 | char fname[50]; | |
334 | int n; | |
335 | if (fstat(fd, &stb)) return 0; | |
336 | if (major(stb.st_rdev) == 9) | |
337 | sprintf(fname, "/sys/block/md%d/md/component_size", | |
ea24acd0 | 338 | (int)minor(stb.st_rdev)); |
e86c9dd6 NB |
339 | else |
340 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
ea24acd0 | 341 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
342 | fd = open(fname, O_RDONLY); |
343 | if (fd < 0) | |
344 | return 0; | |
345 | n = read(fd, fname, sizeof(fname)); | |
346 | close(fd); | |
347 | if (n == sizeof(fname)) | |
348 | return 0; | |
349 | fname[n] = 0; | |
8686f3ed | 350 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
351 | } |
352 | ||
7e0f6979 | 353 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
354 | char *name, char *val) |
355 | { | |
356 | char fname[50]; | |
357 | int n; | |
358 | int fd; | |
7e1432fb | 359 | |
e86c9dd6 | 360 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 361 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
362 | fd = open(fname, O_WRONLY); |
363 | if (fd < 0) | |
364 | return -1; | |
365 | n = write(fd, val, strlen(val)); | |
366 | close(fd); | |
367 | if (n != strlen(val)) | |
368 | return -1; | |
369 | return 0; | |
370 | } | |
371 | ||
7e0f6979 | 372 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
373 | char *name, unsigned long long val) |
374 | { | |
375 | char valstr[50]; | |
376 | sprintf(valstr, "%llu", val); | |
377 | return sysfs_set_str(sra, dev, name, valstr); | |
378 | } | |
379 | ||
7e0f6979 | 380 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
381 | char *name, unsigned long long *val) |
382 | { | |
383 | char fname[50]; | |
384 | char buf[50]; | |
385 | int n; | |
386 | int fd; | |
387 | char *ep; | |
388 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
7e0f6979 | 389 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
390 | fd = open(fname, O_RDONLY); |
391 | if (fd < 0) | |
392 | return -1; | |
393 | n = read(fd, buf, sizeof(buf)); | |
394 | close(fd); | |
395 | if (n <= 0) | |
396 | return -1; | |
397 | buf[n] = 0; | |
398 | *val = strtoull(buf, &ep, 0); | |
399 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
400 | return -1; | |
401 | return 0; | |
402 | } | |
2503d23b | 403 | |
8ed3e5e1 DW |
404 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) |
405 | { | |
406 | unsigned long sec; | |
407 | unsigned long msec; | |
408 | char delay[30]; | |
409 | ||
410 | sec = ms / 1000; | |
411 | msec = ms - (sec * 1000); | |
412 | ||
413 | sprintf(delay, "%ld.%ld", sec, msec); | |
414 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); | |
415 | } | |
416 | ||
2503d23b NB |
417 | int sysfs_set_array(struct mdinfo *sra, |
418 | struct mdinfo *info) | |
419 | { | |
420 | int rv = 0; | |
421 | sra->array = info->array; | |
2f6079dc | 422 | |
2503d23b NB |
423 | if (info->array.level < 0) |
424 | return 0; /* FIXME */ | |
425 | rv |= sysfs_set_str(sra, NULL, "level", | |
426 | map_num(pers, info->array.level)); | |
427 | rv |= sysfs_set_num(sra, NULL, "raid_disks", info->array.raid_disks); | |
428 | rv |= sysfs_set_num(sra, NULL, "chunk_size", info->array.chunk_size); | |
429 | rv |= sysfs_set_num(sra, NULL, "layout", info->array.layout); | |
047d2e49 | 430 | rv |= sysfs_set_num(sra, NULL, "component_size", info->component_size/2); |
0fd5c350 | 431 | rv |= sysfs_set_num(sra, NULL, "resync_start", info->resync_start); |
2503d23b NB |
432 | sra->array = info->array; |
433 | return rv; | |
434 | } | |
435 | ||
2318b9f0 | 436 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd) |
2503d23b NB |
437 | { |
438 | char dv[100]; | |
439 | char nm[100]; | |
440 | struct mdinfo *sd2; | |
441 | char *dname; | |
442 | int rv; | |
443 | ||
444 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
445 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
446 | if (rv) | |
447 | return rv; | |
448 | ||
449 | memset(nm, 0, sizeof(nm)); | |
450 | sprintf(dv, "/sys/dev/block/%d:%d", sd->disk.major, sd->disk.minor); | |
73649188 N |
451 | rv = readlink(dv, nm, sizeof(nm)); |
452 | if (rv <= 0) | |
2503d23b | 453 | return -1; |
73649188 | 454 | nm[rv] = '\0'; |
2503d23b NB |
455 | dname = strrchr(nm, '/'); |
456 | if (dname) dname++; | |
457 | strcpy(sd->sys_name, "dev-"); | |
458 | strcpy(sd->sys_name+4, dname); | |
459 | ||
73649188 | 460 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b NB |
461 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
462 | if (sra->array.level != LEVEL_CONTAINER) { | |
463 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
464 | // rv |= sysfs_set_str(sra, sd, "state", "in_sync"); | |
465 | } | |
3cb07116 NB |
466 | if (! rv) { |
467 | sd2 = malloc(sizeof(*sd2)); | |
468 | *sd2 = *sd; | |
469 | sd2->next = sra->devs; | |
470 | sra->devs = sd2; | |
471 | } | |
2503d23b NB |
472 | return rv; |
473 | } | |
90c8b707 | 474 | |
755c99fa | 475 | #if 0 |
90c8b707 DW |
476 | int sysfs_disk_to_sg(int fd) |
477 | { | |
478 | /* from an open block device, try find and open its corresponding | |
479 | * scsi_generic interface | |
480 | */ | |
481 | struct stat st; | |
482 | char path[256]; | |
483 | char sg_path[256]; | |
484 | char sg_major_minor[8]; | |
485 | char *c; | |
486 | DIR *dir; | |
487 | struct dirent *de; | |
488 | int major, minor, rv; | |
489 | ||
490 | if (fstat(fd, &st)) | |
491 | return -1; | |
492 | ||
493 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
494 | major(st.st_rdev), minor(st.st_rdev)); | |
495 | ||
496 | dir = opendir(path); | |
497 | if (!dir) | |
498 | return -1; | |
499 | ||
500 | de = readdir(dir); | |
501 | while (de) { | |
502 | if (strncmp("scsi_generic:", de->d_name, | |
503 | strlen("scsi_generic:")) == 0) | |
504 | break; | |
505 | de = readdir(dir); | |
506 | } | |
507 | closedir(dir); | |
508 | ||
509 | if (!de) | |
510 | return -1; | |
511 | ||
512 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
513 | fd = open(sg_path, O_RDONLY); | |
514 | if (fd < 0) | |
515 | return fd; | |
516 | ||
517 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
518 | close(fd); | |
519 | if (rv < 0) | |
520 | return -1; | |
521 | else | |
522 | sg_major_minor[rv - 1] = '\0'; | |
523 | ||
524 | c = strchr(sg_major_minor, ':'); | |
525 | *c = '\0'; | |
526 | c++; | |
527 | major = strtol(sg_major_minor, NULL, 10); | |
528 | minor = strtol(c, NULL, 10); | |
529 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
530 | (int) getpid(), major, minor); | |
531 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
532 | fd = open(path, O_RDONLY); | |
533 | unlink(path); | |
534 | return fd; | |
535 | } | |
536 | ||
537 | return -1; | |
538 | } | |
755c99fa | 539 | #endif |
90c8b707 | 540 | |
f1665f72 DW |
541 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
542 | { | |
543 | /* from an open block device, try to retrieve it scsi_id */ | |
544 | struct stat st; | |
545 | char path[256]; | |
546 | char *c1, *c2; | |
547 | DIR *dir; | |
548 | struct dirent *de; | |
549 | ||
550 | if (fstat(fd, &st)) | |
551 | return 1; | |
552 | ||
553 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
554 | major(st.st_rdev), minor(st.st_rdev)); | |
555 | ||
556 | dir = opendir(path); | |
557 | if (!dir) | |
558 | return 1; | |
559 | ||
560 | de = readdir(dir); | |
561 | while (de) { | |
562 | if (strncmp("scsi_disk:", de->d_name, | |
563 | strlen("scsi_disk:")) == 0) | |
564 | break; | |
565 | de = readdir(dir); | |
566 | } | |
567 | closedir(dir); | |
568 | ||
569 | if (!de) | |
570 | return 1; | |
571 | ||
572 | c1 = strchr(de->d_name, ':'); | |
573 | c1++; | |
574 | c2 = strchr(c1, ':'); | |
575 | *c2 = '\0'; | |
576 | *id = strtol(c1, NULL, 10) << 24; /* host */ | |
577 | c1 = c2 + 1; | |
578 | c2 = strchr(c1, ':'); | |
579 | *c2 = '\0'; | |
580 | *id |= strtol(c1, NULL, 10) << 16; /* channel */ | |
581 | c1 = c2 + 1; | |
582 | c2 = strchr(c1, ':'); | |
583 | *c2 = '\0'; | |
584 | *id |= strtol(c1, NULL, 10) << 8; /* lun */ | |
585 | c1 = c2 + 1; | |
586 | *id |= strtol(c1, NULL, 10); /* id */ | |
587 | ||
588 | return 0; | |
589 | } | |
f94d52f4 NB |
590 | |
591 | ||
592 | int sysfs_unique_holder(int devnum, long rdev) | |
593 | { | |
594 | /* Check that devnum is a holder of rdev, | |
595 | * and is the only holder. | |
596 | * we should be locked against races by | |
597 | * an O_EXCL on devnum | |
598 | */ | |
599 | DIR *dir; | |
600 | struct dirent *de; | |
601 | char dirname[100]; | |
602 | char l; | |
603 | int found = 0; | |
604 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", | |
605 | major(rdev), minor(rdev)); | |
606 | dir = opendir(dirname); | |
607 | errno = ENOENT; | |
608 | if (!dir) | |
609 | return 0; | |
610 | l = strlen(dirname); | |
611 | while ((de = readdir(dir)) != NULL) { | |
612 | char buf[10]; | |
613 | int n; | |
614 | int mj, mn; | |
615 | char c; | |
616 | int fd; | |
617 | ||
618 | if (de->d_ino == 0) | |
619 | continue; | |
620 | if (de->d_name[0] == '.') | |
621 | continue; | |
622 | strcpy(dirname+l, "/"); | |
623 | strcat(dirname+l, de->d_name); | |
624 | strcat(dirname+l, "/dev"); | |
625 | fd = open(dirname, O_RDONLY); | |
626 | if (fd < 0) { | |
627 | errno = ENOENT; | |
628 | break; | |
629 | } | |
630 | n = read(fd, buf, sizeof(buf)-1); | |
631 | close(fd); | |
632 | buf[n] = 0; | |
633 | if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 || | |
634 | c != '\n') { | |
635 | errno = ENOENT; | |
636 | break; | |
637 | } | |
638 | if (mj != MD_MAJOR) | |
639 | mn = -1-(mn>>6); | |
640 | ||
641 | if (devnum != mn) { | |
642 | errno = EEXIST; | |
643 | break; | |
644 | } | |
645 | found = 1; | |
646 | } | |
647 | closedir(dir); | |
648 | if (de) | |
649 | return 0; | |
650 | else | |
651 | return found; | |
652 | } |