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