]>
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 | #include "dlink.h" | |
30 | ||
31 | #define MAX_SYSFS_PATH_LEN 120 | |
32 | ||
33 | struct dev_sysfs_rule { | |
34 | struct dev_sysfs_rule *next; | |
35 | char *devname; | |
36 | int uuid[4]; | |
37 | int uuid_set; | |
38 | struct sysfs_entry { | |
39 | struct sysfs_entry *next; | |
40 | char *name; | |
41 | char *value; | |
42 | } *entry; | |
43 | }; | |
44 | ||
45 | int load_sys(char *path, char *buf, int len) | |
46 | { | |
47 | int fd = open(path, O_RDONLY); | |
48 | int n; | |
49 | if (fd < 0) | |
50 | return -1; | |
51 | n = read(fd, buf, len); | |
52 | close(fd); | |
53 | if (n <0 || n >= len) | |
54 | return -1; | |
55 | buf[n] = 0; | |
56 | if (n && buf[n-1] == '\n') | |
57 | buf[n-1] = 0; | |
58 | return 0; | |
59 | } | |
60 | ||
61 | void sysfs_free(struct mdinfo *sra) | |
62 | { | |
63 | while (sra) { | |
64 | struct mdinfo *sra2 = sra->next; | |
65 | while (sra->devs) { | |
66 | struct mdinfo *d = sra->devs; | |
67 | sra->devs = d->next; | |
68 | free(d->bb.entries); | |
69 | free(d); | |
70 | } | |
71 | free(sra->bb.entries); | |
72 | free(sra); | |
73 | sra = sra2; | |
74 | } | |
75 | } | |
76 | ||
77 | /** | |
78 | * sysfs_get_container_devnm() - extract container device name. | |
79 | * @mdi: md_info describes member array, with GET_VERSION option. | |
80 | * @buf: buf to fill, must be MD_NAME_MAX. | |
81 | * | |
82 | * External array version is in format {/,-}<container_devnm>/<array_index> | |
83 | * Extract container_devnm from it and safe it in @buf. | |
84 | */ | |
85 | void sysfs_get_container_devnm(struct mdinfo *mdi, char *buf) | |
86 | { | |
87 | char *p; | |
88 | ||
89 | assert(is_subarray(mdi->text_version)); | |
90 | ||
91 | /* Skip first special sign */ | |
92 | snprintf(buf, MD_NAME_MAX, "%s", mdi->text_version + 1); | |
93 | ||
94 | /* Remove array index */ | |
95 | p = strchr(buf, '/'); | |
96 | if (p) | |
97 | *p = 0; | |
98 | } | |
99 | ||
100 | int sysfs_open(char *devnm, char *devname, char *attr) | |
101 | { | |
102 | char fname[MAX_SYSFS_PATH_LEN]; | |
103 | int fd; | |
104 | ||
105 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/", devnm); | |
106 | if (devname) { | |
107 | strncat(fname, devname, MAX_SYSFS_PATH_LEN - strlen(fname)); | |
108 | strncat(fname, "/", MAX_SYSFS_PATH_LEN - strlen(fname)); | |
109 | } | |
110 | strncat(fname, attr, MAX_SYSFS_PATH_LEN - strlen(fname)); | |
111 | fd = open(fname, O_RDWR); | |
112 | if (fd < 0 && errno == EACCES) | |
113 | fd = open(fname, O_RDONLY); | |
114 | return fd; | |
115 | } | |
116 | ||
117 | void sysfs_init_dev(struct mdinfo *mdi, dev_t devid) | |
118 | { | |
119 | snprintf(mdi->sys_name, | |
120 | sizeof(mdi->sys_name), "dev-%s", devid2kname(devid)); | |
121 | } | |
122 | ||
123 | int sysfs_init(struct mdinfo *mdi, int fd, char *devnm) | |
124 | { | |
125 | struct stat stb; | |
126 | char fname[MAX_SYSFS_PATH_LEN]; | |
127 | int retval = -ENODEV; | |
128 | ||
129 | mdi->sys_name[0] = 0; | |
130 | if (fd >= 0) | |
131 | devnm = fd2devnm(fd); | |
132 | ||
133 | if (devnm == NULL) | |
134 | goto out; | |
135 | ||
136 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm); | |
137 | ||
138 | if (stat(fname, &stb)) | |
139 | goto out; | |
140 | if (!S_ISDIR(stb.st_mode)) | |
141 | goto out; | |
142 | strcpy(mdi->sys_name, devnm); | |
143 | ||
144 | retval = 0; | |
145 | out: | |
146 | return retval; | |
147 | } | |
148 | ||
149 | struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options) | |
150 | { | |
151 | char fname[PATH_MAX]; | |
152 | char buf[PATH_MAX]; | |
153 | char *base; | |
154 | char *dbase; | |
155 | struct mdinfo *sra; | |
156 | struct mdinfo *dev, **devp; | |
157 | DIR *dir = NULL; | |
158 | struct dirent *de; | |
159 | ||
160 | sra = xcalloc(1, sizeof(*sra)); | |
161 | if (sysfs_init(sra, fd, devnm)) { | |
162 | free(sra); | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); | |
167 | base = fname + strlen(fname); | |
168 | ||
169 | sra->devs = NULL; | |
170 | if (options & GET_VERSION) { | |
171 | strcpy(base, "metadata_version"); | |
172 | if (load_sys(fname, buf, sizeof(buf))) | |
173 | goto abort; | |
174 | if (str_is_none(buf) == true) { | |
175 | sra->array.major_version = | |
176 | sra->array.minor_version = -1; | |
177 | strcpy(sra->text_version, ""); | |
178 | } else if (strncmp(buf, "external:", 9) == 0) { | |
179 | sra->array.major_version = -1; | |
180 | sra->array.minor_version = -2; | |
181 | strcpy(sra->text_version, buf+9); | |
182 | } else { | |
183 | sscanf(buf, "%d.%d", | |
184 | &sra->array.major_version, | |
185 | &sra->array.minor_version); | |
186 | strcpy(sra->text_version, buf); | |
187 | } | |
188 | } | |
189 | if (options & GET_LEVEL) { | |
190 | strcpy(base, "level"); | |
191 | if (load_sys(fname, buf, sizeof(buf))) | |
192 | goto abort; | |
193 | sra->array.level = map_name(pers, buf); | |
194 | } | |
195 | if (options & GET_LAYOUT) { | |
196 | strcpy(base, "layout"); | |
197 | if (load_sys(fname, buf, sizeof(buf))) | |
198 | goto abort; | |
199 | sra->array.layout = strtoul(buf, NULL, 0); | |
200 | } | |
201 | if (options & (GET_DISKS|GET_STATE)) { | |
202 | strcpy(base, "raid_disks"); | |
203 | if (load_sys(fname, buf, sizeof(buf))) | |
204 | goto abort; | |
205 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
206 | } | |
207 | if (options & GET_COMPONENT) { | |
208 | strcpy(base, "component_size"); | |
209 | if (load_sys(fname, buf, sizeof(buf))) | |
210 | goto abort; | |
211 | sra->component_size = strtoull(buf, NULL, 0); | |
212 | /* sysfs reports "K", but we want sectors */ | |
213 | sra->component_size *= 2; | |
214 | } | |
215 | if (options & GET_CHUNK) { | |
216 | strcpy(base, "chunk_size"); | |
217 | if (load_sys(fname, buf, sizeof(buf))) | |
218 | goto abort; | |
219 | sra->array.chunk_size = strtoul(buf, NULL, 0); | |
220 | } | |
221 | if (options & GET_CACHE) { | |
222 | strcpy(base, "stripe_cache_size"); | |
223 | if (load_sys(fname, buf, sizeof(buf))) | |
224 | /* Probably level doesn't support it */ | |
225 | sra->cache_size = 0; | |
226 | else | |
227 | sra->cache_size = strtoul(buf, NULL, 0); | |
228 | } | |
229 | if (options & GET_MISMATCH) { | |
230 | strcpy(base, "mismatch_cnt"); | |
231 | if (load_sys(fname, buf, sizeof(buf))) | |
232 | goto abort; | |
233 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
234 | } | |
235 | if (options & GET_SAFEMODE) { | |
236 | int scale = 1; | |
237 | int dot = 0; | |
238 | unsigned i; | |
239 | unsigned long msec; | |
240 | size_t len; | |
241 | ||
242 | strcpy(base, "safe_mode_delay"); | |
243 | if (load_sys(fname, buf, sizeof(buf))) | |
244 | goto abort; | |
245 | ||
246 | /* remove a period, and count digits after it */ | |
247 | len = strlen(buf); | |
248 | for (i = 0; i < len; i++) { | |
249 | if (dot) { | |
250 | if (isdigit(buf[i])) { | |
251 | buf[i-1] = buf[i]; | |
252 | scale *= 10; | |
253 | } | |
254 | buf[i] = 0; | |
255 | } else if (buf[i] == '.') { | |
256 | dot=1; | |
257 | buf[i] = 0; | |
258 | } | |
259 | } | |
260 | msec = strtoul(buf, NULL, 10); | |
261 | msec = (msec * 1000) / scale; | |
262 | sra->safe_mode_delay = msec; | |
263 | } | |
264 | if (options & GET_BITMAP_LOCATION) { | |
265 | strcpy(base, "bitmap/location"); | |
266 | if (load_sys(fname, buf, sizeof(buf))) | |
267 | goto abort; | |
268 | if (strncmp(buf, "file", 4) == 0) | |
269 | sra->bitmap_offset = 1; | |
270 | else if (str_is_none(buf) == true) | |
271 | sra->bitmap_offset = 0; | |
272 | else if (buf[0] == '+') | |
273 | sra->bitmap_offset = strtol(buf+1, NULL, 10); | |
274 | else | |
275 | goto abort; | |
276 | } | |
277 | ||
278 | if (options & GET_ARRAY_STATE) { | |
279 | strcpy(base, "array_state"); | |
280 | if (load_sys(fname, buf, sizeof(buf))) | |
281 | goto abort; | |
282 | sra->array_state = map_name(sysfs_array_states, buf); | |
283 | } | |
284 | ||
285 | if (options & GET_CONSISTENCY_POLICY) { | |
286 | strcpy(base, "consistency_policy"); | |
287 | if (load_sys(fname, buf, sizeof(buf))) | |
288 | sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN; | |
289 | else | |
290 | sra->consistency_policy = map_name(consistency_policies, | |
291 | buf); | |
292 | } | |
293 | ||
294 | if (! (options & GET_DEVS)) | |
295 | return sra; | |
296 | ||
297 | /* Get all the devices as well */ | |
298 | *base = 0; | |
299 | dir = opendir(fname); | |
300 | if (!dir) | |
301 | goto abort; | |
302 | sra->array.spare_disks = 0; | |
303 | sra->array.active_disks = 0; | |
304 | sra->array.failed_disks = 0; | |
305 | sra->array.working_disks = 0; | |
306 | ||
307 | devp = &sra->devs; | |
308 | sra->devs = NULL; | |
309 | while ((de = readdir(dir)) != NULL) { | |
310 | char *ep; | |
311 | if (de->d_ino == 0 || | |
312 | strncmp(de->d_name, "dev-", 4) != 0) | |
313 | continue; | |
314 | strcpy(base, de->d_name); | |
315 | dbase = base + strlen(base); | |
316 | *dbase++ = '/'; | |
317 | ||
318 | dev = xcalloc(1, sizeof(*dev)); | |
319 | ||
320 | /* Always get slot, major, minor */ | |
321 | strcpy(dbase, "slot"); | |
322 | if (load_sys(fname, buf, sizeof(buf))) { | |
323 | /* hmm... unable to read 'slot' maybe the device | |
324 | * is going away? | |
325 | */ | |
326 | strcpy(dbase, "block"); | |
327 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
328 | errno != ENAMETOOLONG) { | |
329 | /* ...yup device is gone */ | |
330 | free(dev); | |
331 | continue; | |
332 | } else { | |
333 | /* slot is unreadable but 'block' link | |
334 | * still intact... something bad is happening | |
335 | * so abort | |
336 | */ | |
337 | free(dev); | |
338 | goto abort; | |
339 | } | |
340 | ||
341 | } | |
342 | strcpy(dev->sys_name, de->d_name); | |
343 | dev->disk.raid_disk = strtoul(buf, &ep, 10); | |
344 | if (*ep) dev->disk.raid_disk = -1; | |
345 | ||
346 | sra->array.nr_disks++; | |
347 | strcpy(dbase, "block/dev"); | |
348 | if (load_sys(fname, buf, sizeof(buf))) { | |
349 | /* assume this is a stale reference to a hot | |
350 | * removed device | |
351 | */ | |
352 | if (!(options & GET_DEVS_ALL)) { | |
353 | free(dev); | |
354 | continue; | |
355 | } | |
356 | } else { | |
357 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); | |
358 | } | |
359 | ||
360 | if (!(options & GET_DEVS_ALL)) { | |
361 | /* special case check for block devices that can go 'offline' */ | |
362 | strcpy(dbase, "block/device/state"); | |
363 | if (load_sys(fname, buf, sizeof(buf)) == 0 && | |
364 | strncmp(buf, "offline", 7) == 0) { | |
365 | free(dev); | |
366 | continue; | |
367 | } | |
368 | } | |
369 | ||
370 | /* finally add this disk to the array */ | |
371 | *devp = dev; | |
372 | devp = & dev->next; | |
373 | dev->next = NULL; | |
374 | ||
375 | if (options & GET_OFFSET) { | |
376 | strcpy(dbase, "offset"); | |
377 | if (load_sys(fname, buf, sizeof(buf))) | |
378 | goto abort; | |
379 | dev->data_offset = strtoull(buf, NULL, 0); | |
380 | strcpy(dbase, "new_offset"); | |
381 | if (load_sys(fname, buf, sizeof(buf)) == 0) | |
382 | dev->new_data_offset = strtoull(buf, NULL, 0); | |
383 | else | |
384 | dev->new_data_offset = dev->data_offset; | |
385 | } | |
386 | if (options & GET_SIZE) { | |
387 | strcpy(dbase, "size"); | |
388 | if (load_sys(fname, buf, sizeof(buf))) | |
389 | goto abort; | |
390 | dev->component_size = strtoull(buf, NULL, 0) * 2; | |
391 | } | |
392 | if (options & GET_STATE) { | |
393 | dev->disk.state = 0; | |
394 | strcpy(dbase, "state"); | |
395 | if (load_sys(fname, buf, sizeof(buf))) | |
396 | goto abort; | |
397 | if (strstr(buf, "faulty")) | |
398 | dev->disk.state |= (1<<MD_DISK_FAULTY); | |
399 | else { | |
400 | sra->array.working_disks++; | |
401 | if (strstr(buf, "in_sync")) { | |
402 | dev->disk.state |= (1<<MD_DISK_SYNC); | |
403 | sra->array.active_disks++; | |
404 | } | |
405 | if (dev->disk.state == 0) | |
406 | sra->array.spare_disks++; | |
407 | } | |
408 | } | |
409 | if (options & GET_ERROR) { | |
410 | strcpy(buf, "errors"); | |
411 | if (load_sys(fname, buf, sizeof(buf))) | |
412 | goto abort; | |
413 | dev->errors = strtoul(buf, NULL, 0); | |
414 | } | |
415 | } | |
416 | ||
417 | if ((options & GET_STATE) && sra->array.raid_disks) | |
418 | sra->array.failed_disks = sra->array.raid_disks - | |
419 | sra->array.active_disks - sra->array.spare_disks; | |
420 | ||
421 | closedir(dir); | |
422 | return sra; | |
423 | ||
424 | abort: | |
425 | if (dir) | |
426 | closedir(dir); | |
427 | sysfs_free(sra); | |
428 | return NULL; | |
429 | } | |
430 | ||
431 | int sysfs_attr_match(const char *attr, const char *str) | |
432 | { | |
433 | /* See if attr, read from a sysfs file, matches | |
434 | * str. They must either be the same, or attr can | |
435 | * have a trailing newline or comma | |
436 | */ | |
437 | while (*attr && *str && *attr == *str) { | |
438 | attr++; | |
439 | str++; | |
440 | } | |
441 | ||
442 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
443 | return 0; | |
444 | return 1; | |
445 | } | |
446 | ||
447 | int sysfs_match_word(const char *word, char **list) | |
448 | { | |
449 | int n; | |
450 | for (n=0; list[n]; n++) | |
451 | if (sysfs_attr_match(word, list[n])) | |
452 | break; | |
453 | return n; | |
454 | } | |
455 | ||
456 | unsigned long long get_component_size(int fd) | |
457 | { | |
458 | /* Find out the component size of the array. | |
459 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
460 | * size field is only 32bits. | |
461 | * So look in /sys/block/mdXXX/md/component_size | |
462 | * | |
463 | * This returns in units of sectors. | |
464 | */ | |
465 | struct stat stb; | |
466 | char fname[MAX_SYSFS_PATH_LEN]; | |
467 | int n; | |
468 | if (fstat(fd, &stb)) | |
469 | return 0; | |
470 | snprintf(fname, MAX_SYSFS_PATH_LEN, | |
471 | "/sys/block/%s/md/component_size", stat2devnm(&stb)); | |
472 | fd = open(fname, O_RDONLY); | |
473 | if (fd < 0) | |
474 | return 0; | |
475 | n = read(fd, fname, sizeof(fname)); | |
476 | close(fd); | |
477 | if (n < 0 || n == sizeof(fname)) | |
478 | return 0; | |
479 | fname[n] = 0; | |
480 | return strtoull(fname, NULL, 10) * 2; | |
481 | } | |
482 | ||
483 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, | |
484 | char *name, char *val) | |
485 | { | |
486 | char fname[MAX_SYSFS_PATH_LEN]; | |
487 | unsigned int n; | |
488 | int fd; | |
489 | ||
490 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", | |
491 | sra->sys_name, dev?dev->sys_name:"", name); | |
492 | fd = open(fname, O_WRONLY); | |
493 | if (fd < 0) | |
494 | return -1; | |
495 | n = write(fd, val, strlen(val)); | |
496 | close(fd); | |
497 | if (n != strlen(val)) { | |
498 | dprintf("failed to write '%s' to '%s' (%s)\n", | |
499 | val, fname, strerror(errno)); | |
500 | return -1; | |
501 | } | |
502 | return 0; | |
503 | } | |
504 | ||
505 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, | |
506 | char *name, unsigned long long val) | |
507 | { | |
508 | char valstr[50]; | |
509 | sprintf(valstr, "%llu", val); | |
510 | return sysfs_set_str(sra, dev, name, valstr); | |
511 | } | |
512 | ||
513 | int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev, | |
514 | char *name, long long val) | |
515 | { | |
516 | char valstr[50]; | |
517 | sprintf(valstr, "%lli", val); | |
518 | return sysfs_set_str(sra, dev, name, valstr); | |
519 | } | |
520 | ||
521 | int sysfs_uevent(struct mdinfo *sra, char *event) | |
522 | { | |
523 | char fname[MAX_SYSFS_PATH_LEN]; | |
524 | int n; | |
525 | int fd; | |
526 | ||
527 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/uevent", | |
528 | sra->sys_name); | |
529 | fd = open(fname, O_WRONLY); | |
530 | if (fd < 0) | |
531 | return -1; | |
532 | n = write(fd, event, strlen(event)); | |
533 | close(fd); | |
534 | if (n != (int)strlen(event)) { | |
535 | dprintf("failed to write '%s' to '%s' (%s)\n", | |
536 | event, fname, strerror(errno)); | |
537 | return -1; | |
538 | } | |
539 | return 0; | |
540 | } | |
541 | ||
542 | int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name) | |
543 | { | |
544 | char fname[MAX_SYSFS_PATH_LEN]; | |
545 | struct stat st; | |
546 | ||
547 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", | |
548 | sra->sys_name, dev?dev->sys_name:"", name); | |
549 | ||
550 | return stat(fname, &st) == 0; | |
551 | } | |
552 | ||
553 | int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev, | |
554 | char *name) | |
555 | { | |
556 | char fname[MAX_SYSFS_PATH_LEN]; | |
557 | int fd; | |
558 | ||
559 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", | |
560 | sra->sys_name, dev?dev->sys_name:"", name); | |
561 | fd = open(fname, O_RDWR); | |
562 | if (fd < 0) | |
563 | fd = open(fname, O_RDONLY); | |
564 | return fd; | |
565 | } | |
566 | ||
567 | int sysfs_fd_get_ll(int fd, unsigned long long *val) | |
568 | { | |
569 | char buf[50]; | |
570 | int n; | |
571 | char *ep; | |
572 | ||
573 | lseek(fd, 0, 0); | |
574 | n = read(fd, buf, sizeof(buf)); | |
575 | if (n <= 0 || n == sizeof(buf)) | |
576 | return -2; | |
577 | buf[n] = 0; | |
578 | *val = strtoull(buf, &ep, 0); | |
579 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
580 | return -1; | |
581 | return 0; | |
582 | } | |
583 | ||
584 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, | |
585 | char *name, unsigned long long *val) | |
586 | { | |
587 | int n; | |
588 | int fd; | |
589 | ||
590 | fd = sysfs_get_fd(sra, dev, name); | |
591 | if (fd < 0) | |
592 | return -1; | |
593 | n = sysfs_fd_get_ll(fd, val); | |
594 | close(fd); | |
595 | return n; | |
596 | } | |
597 | ||
598 | int sysfs_fd_get_two(int fd, unsigned long long *v1, unsigned long long *v2) | |
599 | { | |
600 | /* two numbers in this sysfs file, either | |
601 | * NNN (NNN) | |
602 | * or | |
603 | * NNN / NNN | |
604 | */ | |
605 | char buf[80]; | |
606 | int n; | |
607 | char *ep, *ep2; | |
608 | ||
609 | lseek(fd, 0, 0); | |
610 | n = read(fd, buf, sizeof(buf)); | |
611 | if (n <= 0 || n == sizeof(buf)) | |
612 | return -2; | |
613 | buf[n] = 0; | |
614 | *v1 = strtoull(buf, &ep, 0); | |
615 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
616 | return -1; | |
617 | while (*ep == ' ' || *ep == '/' || *ep == '(') | |
618 | ep++; | |
619 | *v2 = strtoull(ep, &ep2, 0); | |
620 | if (ep2 == ep || (*ep2 != 0 && *ep2 != '\n' && *ep2 != ' ' && *ep2 != ')')) { | |
621 | *v2 = *v1; | |
622 | return 1; | |
623 | } | |
624 | return 2; | |
625 | } | |
626 | ||
627 | int sysfs_get_two(struct mdinfo *sra, struct mdinfo *dev, | |
628 | char *name, unsigned long long *v1, unsigned long long *v2) | |
629 | { | |
630 | int n; | |
631 | int fd; | |
632 | ||
633 | fd = sysfs_get_fd(sra, dev, name); | |
634 | if (fd < 0) | |
635 | return -1; | |
636 | n = sysfs_fd_get_two(fd, v1, v2); | |
637 | close(fd); | |
638 | return n; | |
639 | } | |
640 | ||
641 | int sysfs_fd_get_str(int fd, char *val, int size) | |
642 | { | |
643 | int n; | |
644 | ||
645 | lseek(fd, 0, 0); | |
646 | n = read(fd, val, size); | |
647 | if (n <= 0 || n == size) | |
648 | return -1; | |
649 | val[n] = 0; | |
650 | return n; | |
651 | } | |
652 | ||
653 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, | |
654 | char *name, char *val, int size) | |
655 | { | |
656 | int n; | |
657 | int fd; | |
658 | ||
659 | fd = sysfs_get_fd(sra, dev, name); | |
660 | if (fd < 0) | |
661 | return -1; | |
662 | n = sysfs_fd_get_str(fd, val, size); | |
663 | close(fd); | |
664 | return n; | |
665 | } | |
666 | ||
667 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) | |
668 | { | |
669 | unsigned long sec; | |
670 | unsigned long msec; | |
671 | char delay[30]; | |
672 | ||
673 | sec = ms / 1000; | |
674 | msec = ms % 1000; | |
675 | ||
676 | sprintf(delay, "%ld.%03ld\n", sec, msec); | |
677 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
678 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); | |
679 | } | |
680 | ||
681 | int sysfs_set_array(struct mdinfo *info) | |
682 | { | |
683 | int rv = 0; | |
684 | char ver[100]; | |
685 | int raid_disks = info->array.raid_disks; | |
686 | ||
687 | ver[0] = 0; | |
688 | if (info->array.major_version == -1 && | |
689 | info->array.minor_version == -2) { | |
690 | char buf[SYSFS_MAX_BUF_SIZE]; | |
691 | ||
692 | strcat(strcpy(ver, "external:"), info->text_version); | |
693 | ||
694 | /* meta version might already be set if we are setting | |
695 | * new geometry for a reshape. In that case we don't | |
696 | * want to over-write the 'readonly' flag that is | |
697 | * stored in the metadata version. So read the current | |
698 | * version first, and preserve the flag | |
699 | */ | |
700 | if (sysfs_get_str(info, NULL, "metadata_version", | |
701 | buf, sizeof(buf)) > 0) | |
702 | if (strlen(buf) >= 9 && buf[9] == '-') | |
703 | ver[9] = '-'; | |
704 | ||
705 | if (sysfs_set_str(info, NULL, "metadata_version", ver) < 0) { | |
706 | pr_err("This kernel does not support external metadata.\n"); | |
707 | return 1; | |
708 | } | |
709 | } | |
710 | if (info->array.level < 0) | |
711 | return 0; /* FIXME */ | |
712 | rv |= sysfs_set_str(info, NULL, "level", | |
713 | map_num_s(pers, info->array.level)); | |
714 | if (info->reshape_active && info->delta_disks != UnSet) | |
715 | raid_disks -= info->delta_disks; | |
716 | rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks); | |
717 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); | |
718 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
719 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
720 | if (info->custom_array_size) { | |
721 | int rc; | |
722 | ||
723 | rc = sysfs_set_num(info, NULL, "array_size", | |
724 | info->custom_array_size/2); | |
725 | if (rc && errno == ENOENT) { | |
726 | pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n"); | |
727 | rc = 0; | |
728 | } | |
729 | rv |= rc; | |
730 | } | |
731 | ||
732 | if (info->array.level > 0) | |
733 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
734 | ||
735 | if (info->reshape_active) { | |
736 | rv |= sysfs_set_num(info, NULL, "reshape_position", | |
737 | info->reshape_progress); | |
738 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk); | |
739 | rv |= sysfs_set_num(info, NULL, "layout", info->new_layout); | |
740 | rv |= sysfs_set_num(info, NULL, "raid_disks", | |
741 | info->array.raid_disks); | |
742 | /* We don't set 'new_level' here. That can only happen | |
743 | * once the reshape completes. | |
744 | */ | |
745 | } | |
746 | ||
747 | if (info->consistency_policy == CONSISTENCY_POLICY_PPL) { | |
748 | char *policy = map_num_s(consistency_policies, | |
749 | info->consistency_policy); | |
750 | ||
751 | if (sysfs_set_str(info, NULL, "consistency_policy", policy)) { | |
752 | pr_err("This kernel does not support PPL. Falling back to consistency-policy=resync.\n"); | |
753 | info->consistency_policy = CONSISTENCY_POLICY_RESYNC; | |
754 | } | |
755 | } | |
756 | ||
757 | return rv; | |
758 | } | |
759 | ||
760 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume) | |
761 | { | |
762 | char dv[PATH_MAX]; | |
763 | char nm[PATH_MAX]; | |
764 | char *dname; | |
765 | int rv; | |
766 | int i; | |
767 | ||
768 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
769 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
770 | if (rv) | |
771 | return rv; | |
772 | ||
773 | memset(nm, 0, sizeof(nm)); | |
774 | dname = devid2kname(makedev(sd->disk.major, sd->disk.minor)); | |
775 | strcpy(sd->sys_name, "dev-"); | |
776 | strcpy(sd->sys_name+4, dname); | |
777 | ||
778 | /* test write to see if 'recovery_start' is available */ | |
779 | if (resume && sd->recovery_start < MaxSector && | |
780 | sysfs_set_num(sra, sd, "recovery_start", 0)) { | |
781 | sysfs_set_str(sra, sd, "state", "remove"); | |
782 | return -1; | |
783 | } | |
784 | ||
785 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); | |
786 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); | |
787 | if (!is_container(sra->array.level)) { | |
788 | if (sra->consistency_policy == CONSISTENCY_POLICY_PPL) { | |
789 | rv |= sysfs_set_num(sra, sd, "ppl_sector", sd->ppl_sector); | |
790 | rv |= sysfs_set_num(sra, sd, "ppl_size", sd->ppl_size); | |
791 | } | |
792 | if (sd->recovery_start == MaxSector) | |
793 | /* This can correctly fail if array isn't started, | |
794 | * yet, so just ignore status for now. | |
795 | */ | |
796 | sysfs_set_str(sra, sd, "state", "insync"); | |
797 | if (sd->disk.raid_disk >= 0) | |
798 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
799 | if (resume) | |
800 | sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start); | |
801 | } | |
802 | if (sd->bb.supported) { | |
803 | if (sysfs_set_str(sra, sd, "state", "external_bbl")) { | |
804 | /* | |
805 | * backward compatibility - if kernel doesn't support | |
806 | * bad blocks for external metadata, let it continue | |
807 | * as long as there are none known so far | |
808 | */ | |
809 | if (sd->bb.count) { | |
810 | pr_err("The kernel has no support for bad blocks in external metadata\n"); | |
811 | return -1; | |
812 | } | |
813 | } | |
814 | ||
815 | for (i = 0; i < sd->bb.count; i++) { | |
816 | char s[30]; | |
817 | const struct md_bb_entry *entry = &sd->bb.entries[i]; | |
818 | ||
819 | snprintf(s, sizeof(s) - 1, "%llu %d\n", entry->sector, | |
820 | entry->length); | |
821 | rv |= sysfs_set_str(sra, sd, "bad_blocks", s); | |
822 | } | |
823 | } | |
824 | return rv; | |
825 | } | |
826 | ||
827 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) | |
828 | { | |
829 | /* from an open block device, try to retrieve it scsi_id */ | |
830 | struct stat st; | |
831 | char path[256]; | |
832 | DIR *dir; | |
833 | struct dirent *de; | |
834 | int host, bus, target, lun; | |
835 | ||
836 | if (fstat(fd, &st)) | |
837 | return 1; | |
838 | ||
839 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device", | |
840 | major(st.st_rdev), minor(st.st_rdev)); | |
841 | ||
842 | dir = opendir(path); | |
843 | if (!dir) | |
844 | return 1; | |
845 | ||
846 | for (de = readdir(dir); de; de = readdir(dir)) { | |
847 | int count; | |
848 | ||
849 | if (de->d_type != DT_DIR) | |
850 | continue; | |
851 | ||
852 | count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun); | |
853 | if (count == 4) | |
854 | break; | |
855 | } | |
856 | closedir(dir); | |
857 | ||
858 | if (!de) | |
859 | return 1; | |
860 | ||
861 | *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0); | |
862 | return 0; | |
863 | } | |
864 | ||
865 | int sysfs_unique_holder(char *devnm, long rdev) | |
866 | { | |
867 | /* Check that devnm is a holder of rdev, | |
868 | * and is the only holder. | |
869 | * we should be locked against races by | |
870 | * an O_EXCL on devnm | |
871 | * Return values: | |
872 | * 0 - not unique, not even a holder | |
873 | * 1 - unique, this is the only holder. | |
874 | * 2/3 - not unique, there is another holder | |
875 | * -1 - error, cannot find the holders | |
876 | */ | |
877 | DIR *dir; | |
878 | struct dirent *de; | |
879 | char dirname[100]; | |
880 | char l; | |
881 | int ret = 0; | |
882 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", | |
883 | major(rdev), minor(rdev)); | |
884 | dir = opendir(dirname); | |
885 | if (!dir) | |
886 | return -1; | |
887 | l = strlen(dirname); | |
888 | while ((de = readdir(dir)) != NULL) { | |
889 | char buf[100]; | |
890 | char *sl; | |
891 | int n; | |
892 | ||
893 | if (de->d_ino == 0) | |
894 | continue; | |
895 | if (de->d_name[0] == '.') | |
896 | continue; | |
897 | strcpy(dirname+l, "/"); | |
898 | strcat(dirname+l, de->d_name); | |
899 | n = readlink(dirname, buf, sizeof(buf)-1); | |
900 | if (n <= 0) | |
901 | continue; | |
902 | buf[n] = 0; | |
903 | sl = strrchr(buf, '/'); | |
904 | if (!sl) | |
905 | continue; | |
906 | sl++; | |
907 | ||
908 | if (strcmp(devnm, sl) == 0) | |
909 | ret |= 1; | |
910 | else | |
911 | ret |= 2; | |
912 | } | |
913 | closedir(dir); | |
914 | return ret; | |
915 | } | |
916 | ||
917 | int sysfs_freeze_array(struct mdinfo *sra) | |
918 | { | |
919 | /* Try to freeze resync/rebuild on this array/container. | |
920 | * Return -1 if the array is busy, | |
921 | * return 0 if this kernel doesn't support 'frozen' | |
922 | * return 1 if it worked. | |
923 | */ | |
924 | char buf[SYSFS_MAX_BUF_SIZE]; | |
925 | ||
926 | if (!sysfs_attribute_available(sra, NULL, "sync_action")) | |
927 | return 1; /* no sync_action == frozen */ | |
928 | if (sysfs_get_str(sra, NULL, "sync_action", buf, sizeof(buf)) <= 0) | |
929 | return 0; | |
930 | if (strcmp(buf, "frozen\n") == 0) | |
931 | /* Already frozen */ | |
932 | return 0; | |
933 | if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0) | |
934 | return -1; | |
935 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
936 | return 0; | |
937 | return 1; | |
938 | } | |
939 | ||
940 | int sysfs_wait(int fd, int *msec) | |
941 | { | |
942 | /* Wait up to '*msec' for fd to have an exception condition. | |
943 | * if msec == NULL, wait indefinitely. | |
944 | */ | |
945 | fd_set fds; | |
946 | int n; | |
947 | FD_ZERO(&fds); | |
948 | FD_SET(fd, &fds); | |
949 | if (msec == NULL) | |
950 | n = select(fd+1, NULL, NULL, &fds, NULL); | |
951 | else if (*msec < 0) | |
952 | n = 0; | |
953 | else { | |
954 | struct timeval start, end, tv; | |
955 | gettimeofday(&start, NULL); | |
956 | if (*msec < 1000) { | |
957 | tv.tv_sec = 0; | |
958 | tv.tv_usec = (*msec)*1000; | |
959 | } else { | |
960 | tv.tv_sec = (*msec)/1000; | |
961 | tv.tv_usec = 0; | |
962 | } | |
963 | n = select(fd+1, NULL, NULL, &fds, &tv); | |
964 | gettimeofday(&end, NULL); | |
965 | end.tv_sec -= start.tv_sec; | |
966 | *msec -= (end.tv_sec * 1000 + end.tv_usec/1000 | |
967 | - start.tv_usec/1000) + 1; | |
968 | } | |
969 | return n; | |
970 | } | |
971 | ||
972 | int sysfs_rules_apply_check(const struct mdinfo *sra, | |
973 | const struct sysfs_entry *ent) | |
974 | { | |
975 | /* Check whether parameter is regular file, | |
976 | * exists and is under specified directory. | |
977 | */ | |
978 | char fname[MAX_SYSFS_PATH_LEN]; | |
979 | char dname[MAX_SYSFS_PATH_LEN]; | |
980 | char resolved_path[PATH_MAX]; | |
981 | char resolved_dir[PATH_MAX]; | |
982 | int result; | |
983 | ||
984 | if (sra == NULL || ent == NULL) | |
985 | return -1; | |
986 | ||
987 | result = snprintf(dname, MAX_SYSFS_PATH_LEN, | |
988 | "/sys/block/%s/md/", sra->sys_name); | |
989 | if (result < 0 || result >= MAX_SYSFS_PATH_LEN) | |
990 | return -1; | |
991 | ||
992 | result = snprintf(fname, MAX_SYSFS_PATH_LEN, | |
993 | "%s/%s", dname, ent->name); | |
994 | if (result < 0 || result >= MAX_SYSFS_PATH_LEN) | |
995 | return -1; | |
996 | ||
997 | if (realpath(fname, resolved_path) == NULL || | |
998 | realpath(dname, resolved_dir) == NULL) | |
999 | return -1; | |
1000 | ||
1001 | if (strncmp(resolved_dir, resolved_path, | |
1002 | strnlen(resolved_dir, PATH_MAX)) != 0) | |
1003 | return -1; | |
1004 | ||
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | static struct dev_sysfs_rule *sysfs_rules; | |
1009 | ||
1010 | void sysfs_rules_apply(char *devnm, struct mdinfo *dev) | |
1011 | { | |
1012 | struct dev_sysfs_rule *rules = sysfs_rules; | |
1013 | ||
1014 | while (rules) { | |
1015 | struct sysfs_entry *ent = rules->entry; | |
1016 | int match = 0; | |
1017 | ||
1018 | if (!rules->uuid_set) { | |
1019 | if (rules->devname) | |
1020 | match = strcmp(devnm, rules->devname) == 0; | |
1021 | } else { | |
1022 | match = memcmp(dev->uuid, rules->uuid, | |
1023 | sizeof(int[4])) == 0; | |
1024 | } | |
1025 | ||
1026 | while (match && ent) { | |
1027 | if (sysfs_rules_apply_check(dev, ent) < 0) | |
1028 | pr_err("SYSFS: failed to write '%s' to '%s'\n", | |
1029 | ent->value, ent->name); | |
1030 | else | |
1031 | sysfs_set_str(dev, NULL, ent->name, ent->value); | |
1032 | ent = ent->next; | |
1033 | } | |
1034 | rules = rules->next; | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | static void sysfs_rule_free(struct dev_sysfs_rule *rule) | |
1039 | { | |
1040 | struct sysfs_entry *entry; | |
1041 | ||
1042 | while (rule) { | |
1043 | struct dev_sysfs_rule *tmp = rule->next; | |
1044 | ||
1045 | entry = rule->entry; | |
1046 | while (entry) { | |
1047 | struct sysfs_entry *tmp = entry->next; | |
1048 | ||
1049 | free(entry->name); | |
1050 | free(entry->value); | |
1051 | free(entry); | |
1052 | entry = tmp; | |
1053 | } | |
1054 | ||
1055 | if (rule->devname) | |
1056 | free(rule->devname); | |
1057 | free(rule); | |
1058 | rule = tmp; | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | void sysfsline(char *line) | |
1063 | { | |
1064 | struct dev_sysfs_rule *sr; | |
1065 | char *w; | |
1066 | ||
1067 | sr = xcalloc(1, sizeof(*sr)); | |
1068 | for (w = dl_next(line); w != line ; w = dl_next(w)) { | |
1069 | if (strncasecmp(w, "name=", 5) == 0) { | |
1070 | char *devname = w + 5; | |
1071 | ||
1072 | if (strncmp(devname, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) { | |
1073 | if (sr->devname) | |
1074 | pr_err("Only give one device per SYSFS line: %s\n", | |
1075 | devname); | |
1076 | else | |
1077 | sr->devname = xstrdup(devname); | |
1078 | } else { | |
1079 | pr_err("%s is an invalid name for an md device - ignored.\n", | |
1080 | devname); | |
1081 | } | |
1082 | } else if (strncasecmp(w, "uuid=", 5) == 0) { | |
1083 | char *uuid = w + 5; | |
1084 | ||
1085 | if (sr->uuid_set) { | |
1086 | pr_err("Only give one uuid per SYSFS line: %s\n", | |
1087 | uuid); | |
1088 | } else { | |
1089 | if (parse_uuid(w + 5, sr->uuid) && | |
1090 | memcmp(sr->uuid, uuid_zero, | |
1091 | sizeof(int[4])) != 0) | |
1092 | sr->uuid_set = 1; | |
1093 | else | |
1094 | pr_err("Invalid uuid: %s\n", uuid); | |
1095 | } | |
1096 | } else { | |
1097 | struct sysfs_entry *prop; | |
1098 | ||
1099 | char *sep = strchr(w, '='); | |
1100 | ||
1101 | if (sep == NULL || *(sep + 1) == 0) { | |
1102 | pr_err("Cannot parse \"%s\" - ignoring.\n", w); | |
1103 | continue; | |
1104 | } | |
1105 | ||
1106 | prop = xmalloc(sizeof(*prop)); | |
1107 | prop->value = xstrdup(sep + 1); | |
1108 | *sep = 0; | |
1109 | prop->name = xstrdup(w); | |
1110 | prop->next = sr->entry; | |
1111 | sr->entry = prop; | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | if (!sr->devname && !sr->uuid_set) { | |
1116 | pr_err("Device name not found in sysfs config entry - ignoring.\n"); | |
1117 | sysfs_rule_free(sr); | |
1118 | return; | |
1119 | } | |
1120 | ||
1121 | sr->next = sysfs_rules; | |
1122 | sysfs_rules = sr; | |
1123 | } | |
1124 | ||
1125 | /** | |
1126 | * sysfs_is_libata_allow_tpm_enabled() - check if libata allow_tmp is enabled. | |
1127 | * @verbose: verbose flag. | |
1128 | * | |
1129 | * Check if libata allow_tmp flag is set, this is required for SATA Opal Security commands to work. | |
1130 | * | |
1131 | * Return: true if allow_tpm enable, false otherwise. | |
1132 | */ | |
1133 | bool sysfs_is_libata_allow_tpm_enabled(const int verbose) | |
1134 | { | |
1135 | const char *path = "/sys/module/libata/parameters/allow_tpm"; | |
1136 | const char *expected_value = "1"; | |
1137 | int fd = open(path, O_RDONLY); | |
1138 | char buf[3]; | |
1139 | ||
1140 | if (!is_fd_valid(fd)) { | |
1141 | pr_vrb("Failed open file descriptor to %s. Cannot check libata allow_tpm param.\n", | |
1142 | path); | |
1143 | return false; | |
1144 | } | |
1145 | ||
1146 | sysfs_fd_get_str(fd, buf, sizeof(buf)); | |
1147 | close(fd); | |
1148 | ||
1149 | if (strncmp(buf, expected_value, 1) == 0) | |
1150 | return true; | |
1151 | return false; | |
1152 | } |