]>
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 | * | |
e736b623 | 5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> |
e86c9dd6 NB |
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> |
b0681598 | 29 | #include "dlink.h" |
e86c9dd6 | 30 | |
bbb52f2b TM |
31 | #define MAX_SYSFS_PATH_LEN 120 |
32 | ||
b0681598 MD |
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 | ||
193b6c0b | 45 | int load_sys(char *path, char *buf, int len) |
e86c9dd6 NB |
46 | { |
47 | int fd = open(path, O_RDONLY); | |
48 | int n; | |
49 | if (fd < 0) | |
50 | return -1; | |
193b6c0b | 51 | n = read(fd, buf, len); |
e86c9dd6 | 52 | close(fd); |
193b6c0b | 53 | if (n <0 || n >= len) |
e86c9dd6 NB |
54 | return -1; |
55 | buf[n] = 0; | |
8dfb8619 | 56 | if (n && buf[n-1] == '\n') |
e86c9dd6 NB |
57 | buf[n-1] = 0; |
58 | return 0; | |
59 | } | |
60 | ||
7e0f6979 | 61 | void sysfs_free(struct mdinfo *sra) |
8382f19b | 62 | { |
7e0f6979 NB |
63 | while (sra) { |
64 | struct mdinfo *sra2 = sra->next; | |
65 | while (sra->devs) { | |
66 | struct mdinfo *d = sra->devs; | |
67 | sra->devs = d->next; | |
bb758cca | 68 | free(d->bb.entries); |
7e0f6979 NB |
69 | free(d); |
70 | } | |
bb758cca | 71 | free(sra->bb.entries); |
7e0f6979 NB |
72 | free(sra); |
73 | sra = sra2; | |
8382f19b | 74 | } |
8382f19b NB |
75 | } |
76 | ||
14a86579 MT |
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 | ||
4dd2df09 | 100 | int sysfs_open(char *devnm, char *devname, char *attr) |
549e9569 | 101 | { |
bbb52f2b | 102 | char fname[MAX_SYSFS_PATH_LEN]; |
549e9569 | 103 | int fd; |
549e9569 | 104 | |
bbb52f2b | 105 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/", devnm); |
549e9569 | 106 | if (devname) { |
bbb52f2b TM |
107 | strncat(fname, devname, MAX_SYSFS_PATH_LEN - strlen(fname)); |
108 | strncat(fname, "/", MAX_SYSFS_PATH_LEN - strlen(fname)); | |
549e9569 | 109 | } |
bbb52f2b | 110 | strncat(fname, attr, MAX_SYSFS_PATH_LEN - strlen(fname)); |
549e9569 | 111 | fd = open(fname, O_RDWR); |
ea6d09b0 | 112 | if (fd < 0 && errno == EACCES) |
549e9569 NB |
113 | fd = open(fname, O_RDONLY); |
114 | return fd; | |
115 | } | |
116 | ||
a37563c9 | 117 | void sysfs_init_dev(struct mdinfo *mdi, dev_t devid) |
9465f170 GJ |
118 | { |
119 | snprintf(mdi->sys_name, | |
120 | sizeof(mdi->sys_name), "dev-%s", devid2kname(devid)); | |
121 | } | |
122 | ||
dae13137 | 123 | int sysfs_init(struct mdinfo *mdi, int fd, char *devnm) |
f35f2525 | 124 | { |
67a02d52 JS |
125 | struct stat stb; |
126 | char fname[MAX_SYSFS_PATH_LEN]; | |
dae13137 | 127 | int retval = -ENODEV; |
67a02d52 | 128 | |
678a4a36 | 129 | mdi->sys_name[0] = 0; |
67a02d52 | 130 | if (fd >= 0) |
4dd2df09 | 131 | devnm = fd2devnm(fd); |
67a02d52 | 132 | |
4dd2df09 | 133 | if (devnm == NULL) |
dae13137 | 134 | goto out; |
67a02d52 JS |
135 | |
136 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm); | |
137 | ||
138 | if (stat(fname, &stb)) | |
dae13137 | 139 | goto out; |
67a02d52 | 140 | if (!S_ISDIR(stb.st_mode)) |
dae13137 | 141 | goto out; |
4dd2df09 | 142 | strcpy(mdi->sys_name, devnm); |
dae13137 JS |
143 | |
144 | retval = 0; | |
145 | out: | |
146 | return retval; | |
f35f2525 N |
147 | } |
148 | ||
4dd2df09 | 149 | struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options) |
e86c9dd6 | 150 | { |
33a6535d AW |
151 | char fname[PATH_MAX]; |
152 | char buf[PATH_MAX]; | |
e86c9dd6 NB |
153 | char *base; |
154 | char *dbase; | |
7e0f6979 | 155 | struct mdinfo *sra; |
64e103fe | 156 | struct mdinfo *dev, **devp; |
355726fa | 157 | DIR *dir = NULL; |
e86c9dd6 NB |
158 | struct dirent *de; |
159 | ||
503975b9 | 160 | sra = xcalloc(1, sizeof(*sra)); |
dae13137 | 161 | if (sysfs_init(sra, fd, devnm)) { |
678a4a36 N |
162 | free(sra); |
163 | return NULL; | |
164 | } | |
e86c9dd6 | 165 | |
7e0f6979 | 166 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); |
e86c9dd6 NB |
167 | base = fname + strlen(fname); |
168 | ||
169 | sra->devs = NULL; | |
8382f19b NB |
170 | if (options & GET_VERSION) { |
171 | strcpy(base, "metadata_version"); | |
193b6c0b | 172 | if (load_sys(fname, buf, sizeof(buf))) |
8382f19b | 173 | goto abort; |
b823c8f9 | 174 | if (str_is_none(buf) == true) { |
7e0f6979 NB |
175 | sra->array.major_version = |
176 | sra->array.minor_version = -1; | |
294d6f45 NB |
177 | strcpy(sra->text_version, ""); |
178 | } else if (strncmp(buf, "external:", 9) == 0) { | |
142cb9e1 NB |
179 | sra->array.major_version = -1; |
180 | sra->array.minor_version = -2; | |
181 | strcpy(sra->text_version, buf+9); | |
b8ac1967 | 182 | } else { |
8382f19b | 183 | sscanf(buf, "%d.%d", |
7e0f6979 NB |
184 | &sra->array.major_version, |
185 | &sra->array.minor_version); | |
b8ac1967 NB |
186 | strcpy(sra->text_version, buf); |
187 | } | |
8382f19b | 188 | } |
e86c9dd6 NB |
189 | if (options & GET_LEVEL) { |
190 | strcpy(base, "level"); | |
193b6c0b | 191 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 192 | goto abort; |
7e0f6979 | 193 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
194 | } |
195 | if (options & GET_LAYOUT) { | |
196 | strcpy(base, "layout"); | |
193b6c0b | 197 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 198 | goto abort; |
7e0f6979 | 199 | sra->array.layout = strtoul(buf, NULL, 0); |
e86c9dd6 | 200 | } |
b13b52c8 | 201 | if (options & (GET_DISKS|GET_STATE)) { |
549e9569 | 202 | strcpy(base, "raid_disks"); |
193b6c0b | 203 | if (load_sys(fname, buf, sizeof(buf))) |
549e9569 NB |
204 | goto abort; |
205 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
f1d26766 | 206 | } |
e86c9dd6 NB |
207 | if (options & GET_COMPONENT) { |
208 | strcpy(base, "component_size"); | |
193b6c0b | 209 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
210 | goto abort; |
211 | sra->component_size = strtoull(buf, NULL, 0); | |
353632d9 NB |
212 | /* sysfs reports "K", but we want sectors */ |
213 | sra->component_size *= 2; | |
e86c9dd6 NB |
214 | } |
215 | if (options & GET_CHUNK) { | |
216 | strcpy(base, "chunk_size"); | |
193b6c0b | 217 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 218 | goto abort; |
7e0f6979 | 219 | sra->array.chunk_size = strtoul(buf, NULL, 0); |
e86c9dd6 | 220 | } |
758d3a8e NB |
221 | if (options & GET_CACHE) { |
222 | strcpy(base, "stripe_cache_size"); | |
193b6c0b | 223 | if (load_sys(fname, buf, sizeof(buf))) |
6a67848a N |
224 | /* Probably level doesn't support it */ |
225 | sra->cache_size = 0; | |
226 | else | |
227 | sra->cache_size = strtoul(buf, NULL, 0); | |
758d3a8e | 228 | } |
37dfc3d6 NB |
229 | if (options & GET_MISMATCH) { |
230 | strcpy(base, "mismatch_cnt"); | |
193b6c0b | 231 | if (load_sys(fname, buf, sizeof(buf))) |
37dfc3d6 NB |
232 | goto abort; |
233 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
234 | } | |
1770662b DW |
235 | if (options & GET_SAFEMODE) { |
236 | int scale = 1; | |
237 | int dot = 0; | |
f21e18ca | 238 | unsigned i; |
1770662b DW |
239 | unsigned long msec; |
240 | size_t len; | |
241 | ||
242 | strcpy(base, "safe_mode_delay"); | |
193b6c0b | 243 | if (load_sys(fname, buf, sizeof(buf))) |
1770662b DW |
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 | } | |
c0c1acd6 N |
264 | if (options & GET_BITMAP_LOCATION) { |
265 | strcpy(base, "bitmap/location"); | |
193b6c0b | 266 | if (load_sys(fname, buf, sizeof(buf))) |
c0c1acd6 N |
267 | goto abort; |
268 | if (strncmp(buf, "file", 4) == 0) | |
269 | sra->bitmap_offset = 1; | |
b823c8f9 | 270 | else if (str_is_none(buf) == true) |
c0c1acd6 N |
271 | sra->bitmap_offset = 0; |
272 | else if (buf[0] == '+') | |
fbdef498 | 273 | sra->bitmap_offset = strtol(buf+1, NULL, 10); |
c0c1acd6 N |
274 | else |
275 | goto abort; | |
276 | } | |
e86c9dd6 | 277 | |
5aa644c6 SL |
278 | if (options & GET_ARRAY_STATE) { |
279 | strcpy(base, "array_state"); | |
5e4ca8bb | 280 | if (load_sys(fname, buf, sizeof(buf))) |
5aa644c6 | 281 | goto abort; |
5e4ca8bb | 282 | sra->array_state = map_name(sysfs_array_states, buf); |
5e4ca8bb | 283 | } |
5aa644c6 | 284 | |
5308f117 AP |
285 | if (options & GET_CONSISTENCY_POLICY) { |
286 | strcpy(base, "consistency_policy"); | |
b7580566 | 287 | if (load_sys(fname, buf, sizeof(buf))) |
5308f117 | 288 | sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN; |
b7580566 AP |
289 | else |
290 | sra->consistency_policy = map_name(consistency_policies, | |
291 | buf); | |
5308f117 AP |
292 | } |
293 | ||
e86c9dd6 NB |
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; | |
7e0f6979 | 302 | sra->array.spare_disks = 0; |
64ec81da JS |
303 | sra->array.active_disks = 0; |
304 | sra->array.failed_disks = 0; | |
8b0ebd64 | 305 | sra->array.working_disks = 0; |
e86c9dd6 | 306 | |
64e103fe N |
307 | devp = &sra->devs; |
308 | sra->devs = NULL; | |
e86c9dd6 NB |
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 | ||
bb758cca | 318 | dev = xcalloc(1, sizeof(*dev)); |
e86c9dd6 NB |
319 | |
320 | /* Always get slot, major, minor */ | |
321 | strcpy(dbase, "slot"); | |
193b6c0b | 322 | if (load_sys(fname, buf, sizeof(buf))) { |
4795982e DW |
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 | } | |
64e103fe | 340 | |
4795982e | 341 | } |
4795982e | 342 | strcpy(dev->sys_name, de->d_name); |
06c7f68e NB |
343 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
344 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 | 345 | |
fe05dc43 | 346 | sra->array.nr_disks++; |
e86c9dd6 | 347 | strcpy(dbase, "block/dev"); |
193b6c0b | 348 | if (load_sys(fname, buf, sizeof(buf))) { |
b526e52d DW |
349 | /* assume this is a stale reference to a hot |
350 | * removed device | |
351 | */ | |
ae7d61e3 AP |
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); | |
dab4a513 | 358 | } |
e86c9dd6 | 359 | |
ae7d61e3 AP |
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 | } | |
dab4a513 DW |
368 | } |
369 | ||
370 | /* finally add this disk to the array */ | |
64e103fe N |
371 | *devp = dev; |
372 | devp = & dev->next; | |
373 | dev->next = NULL; | |
dab4a513 | 374 | |
e86c9dd6 NB |
375 | if (options & GET_OFFSET) { |
376 | strcpy(dbase, "offset"); | |
193b6c0b | 377 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 378 | goto abort; |
06c7f68e | 379 | dev->data_offset = strtoull(buf, NULL, 0); |
fe384ca0 | 380 | strcpy(dbase, "new_offset"); |
193b6c0b | 381 | if (load_sys(fname, buf, sizeof(buf)) == 0) |
fe384ca0 N |
382 | dev->new_data_offset = strtoull(buf, NULL, 0); |
383 | else | |
384 | dev->new_data_offset = dev->data_offset; | |
e86c9dd6 NB |
385 | } |
386 | if (options & GET_SIZE) { | |
387 | strcpy(dbase, "size"); | |
193b6c0b | 388 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 389 | goto abort; |
047d2e49 | 390 | dev->component_size = strtoull(buf, NULL, 0) * 2; |
e86c9dd6 NB |
391 | } |
392 | if (options & GET_STATE) { | |
06c7f68e | 393 | dev->disk.state = 0; |
e86c9dd6 | 394 | strcpy(dbase, "state"); |
193b6c0b | 395 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 396 | goto abort; |
b13b52c8 | 397 | if (strstr(buf, "faulty")) |
06c7f68e | 398 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
b13b52c8 | 399 | else { |
8b0ebd64 JS |
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++; | |
64ec81da | 407 | } |
e86c9dd6 NB |
408 | } |
409 | if (options & GET_ERROR) { | |
410 | strcpy(buf, "errors"); | |
193b6c0b | 411 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
412 | goto abort; |
413 | dev->errors = strtoul(buf, NULL, 0); | |
414 | } | |
415 | } | |
b13b52c8 TM |
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 | ||
355726fa | 421 | closedir(dir); |
e86c9dd6 NB |
422 | return sra; |
423 | ||
424 | abort: | |
355726fa NB |
425 | if (dir) |
426 | closedir(dir); | |
8382f19b | 427 | sysfs_free(sra); |
e86c9dd6 NB |
428 | return NULL; |
429 | } | |
430 | ||
1770662b DW |
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 | ||
e86c9dd6 NB |
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 | |
353632d9 | 462 | * |
8686f3ed | 463 | * This returns in units of sectors. |
e86c9dd6 NB |
464 | */ |
465 | struct stat stb; | |
bbb52f2b | 466 | char fname[MAX_SYSFS_PATH_LEN]; |
e86c9dd6 | 467 | int n; |
36138e4e JS |
468 | if (fstat(fd, &stb)) |
469 | return 0; | |
c07566f1 N |
470 | snprintf(fname, MAX_SYSFS_PATH_LEN, |
471 | "/sys/block/%s/md/component_size", stat2devnm(&stb)); | |
e86c9dd6 NB |
472 | fd = open(fname, O_RDONLY); |
473 | if (fd < 0) | |
474 | return 0; | |
475 | n = read(fd, fname, sizeof(fname)); | |
476 | close(fd); | |
99f6e521 | 477 | if (n < 0 || n == sizeof(fname)) |
e86c9dd6 NB |
478 | return 0; |
479 | fname[n] = 0; | |
8686f3ed | 480 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
481 | } |
482 | ||
7e0f6979 | 483 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
484 | char *name, char *val) |
485 | { | |
bbb52f2b | 486 | char fname[MAX_SYSFS_PATH_LEN]; |
f21e18ca | 487 | unsigned int n; |
e86c9dd6 | 488 | int fd; |
7e1432fb | 489 | |
bbb52f2b | 490 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 491 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
492 | fd = open(fname, O_WRONLY); |
493 | if (fd < 0) | |
494 | return -1; | |
495 | n = write(fd, val, strlen(val)); | |
496 | close(fd); | |
2a24d7b6 | 497 | if (n != strlen(val)) { |
1ade5cc1 N |
498 | dprintf("failed to write '%s' to '%s' (%s)\n", |
499 | val, fname, strerror(errno)); | |
e86c9dd6 | 500 | return -1; |
2a24d7b6 | 501 | } |
e86c9dd6 NB |
502 | return 0; |
503 | } | |
504 | ||
7e0f6979 | 505 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
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 | ||
012a8641 JS |
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 | ||
97590376 N |
521 | int sysfs_uevent(struct mdinfo *sra, char *event) |
522 | { | |
bbb52f2b | 523 | char fname[MAX_SYSFS_PATH_LEN]; |
97590376 N |
524 | int n; |
525 | int fd; | |
526 | ||
bbb52f2b | 527 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/uevent", |
97590376 N |
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); | |
e4c72d1d | 534 | if (n != (int)strlen(event)) { |
1ade5cc1 N |
535 | dprintf("failed to write '%s' to '%s' (%s)\n", |
536 | event, fname, strerror(errno)); | |
e4c72d1d LB |
537 | return -1; |
538 | } | |
97590376 | 539 | return 0; |
1011e834 | 540 | } |
97590376 | 541 | |
bc77ed53 DW |
542 | int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name) |
543 | { | |
bbb52f2b | 544 | char fname[MAX_SYSFS_PATH_LEN]; |
bc77ed53 DW |
545 | struct stat st; |
546 | ||
bbb52f2b | 547 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", |
bc77ed53 DW |
548 | sra->sys_name, dev?dev->sys_name:"", name); |
549 | ||
550 | return stat(fname, &st) == 0; | |
551 | } | |
552 | ||
a6288483 N |
553 | int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev, |
554 | char *name) | |
e86c9dd6 | 555 | { |
bbb52f2b | 556 | char fname[MAX_SYSFS_PATH_LEN]; |
e86c9dd6 | 557 | int fd; |
a6288483 | 558 | |
bbb52f2b | 559 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 560 | sra->sys_name, dev?dev->sys_name:"", name); |
a6288483 | 561 | fd = open(fname, O_RDWR); |
e86c9dd6 | 562 | if (fd < 0) |
a6288483 N |
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); | |
e86c9dd6 | 574 | n = read(fd, buf, sizeof(buf)); |
5418499a | 575 | if (n <= 0 || n == sizeof(buf)) |
6560987b | 576 | return -2; |
e86c9dd6 NB |
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 | } | |
2503d23b | 583 | |
a6288483 N |
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 | ||
2eba8496 N |
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)); | |
5418499a | 611 | if (n <= 0 || n == sizeof(buf)) |
2eba8496 N |
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 | ||
7236ee7a N |
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); | |
5418499a | 647 | if (n <= 0 || n == size) |
7236ee7a N |
648 | return -1; |
649 | val[n] = 0; | |
650 | return n; | |
651 | } | |
652 | ||
93ecfa01 N |
653 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, |
654 | char *name, char *val, int size) | |
655 | { | |
93ecfa01 N |
656 | int n; |
657 | int fd; | |
7236ee7a N |
658 | |
659 | fd = sysfs_get_fd(sra, dev, name); | |
93ecfa01 N |
660 | if (fd < 0) |
661 | return -1; | |
7236ee7a | 662 | n = sysfs_fd_get_str(fd, val, size); |
93ecfa01 | 663 | close(fd); |
93ecfa01 N |
664 | return n; |
665 | } | |
666 | ||
8ed3e5e1 DW |
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; | |
0dd3ba30 | 674 | msec = ms % 1000; |
8ed3e5e1 | 675 | |
0dd3ba30 DW |
676 | sprintf(delay, "%ld.%03ld\n", sec, msec); |
677 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
8ed3e5e1 DW |
678 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); |
679 | } | |
680 | ||
f35f2525 | 681 | int sysfs_set_array(struct mdinfo *info, int vers) |
2503d23b NB |
682 | { |
683 | int rv = 0; | |
f35f2525 | 684 | char ver[100]; |
a5062b1c | 685 | int raid_disks = info->array.raid_disks; |
f35f2525 N |
686 | |
687 | ver[0] = 0; | |
688 | if (info->array.major_version == -1 && | |
689 | info->array.minor_version == -2) { | |
90fd7001 | 690 | char buf[SYSFS_MAX_BUF_SIZE]; |
ddb12f6c | 691 | |
f35f2525 N |
692 | strcat(strcpy(ver, "external:"), info->text_version); |
693 | ||
ddb12f6c AK |
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", | |
90fd7001 | 701 | buf, sizeof(buf)) > 0) |
ddb12f6c AK |
702 | if (strlen(buf) >= 9 && buf[9] == '-') |
703 | ver[9] = '-'; | |
704 | ||
f35f2525 N |
705 | if ((vers % 100) < 2 || |
706 | sysfs_set_str(info, NULL, "metadata_version", | |
707 | ver) < 0) { | |
7a862a02 | 708 | pr_err("This kernel does not support external metadata.\n"); |
f35f2525 N |
709 | return 1; |
710 | } | |
711 | } | |
2503d23b NB |
712 | if (info->array.level < 0) |
713 | return 0; /* FIXME */ | |
f35f2525 | 714 | rv |= sysfs_set_str(info, NULL, "level", |
5f21d674 | 715 | map_num_s(pers, info->array.level)); |
a5062b1c AK |
716 | if (info->reshape_active && info->delta_disks != UnSet) |
717 | raid_disks -= info->delta_disks; | |
718 | rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks); | |
f35f2525 N |
719 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); |
720 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
721 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
da9b4a62 DW |
722 | if (info->custom_array_size) { |
723 | int rc; | |
724 | ||
725 | rc = sysfs_set_num(info, NULL, "array_size", | |
726 | info->custom_array_size/2); | |
727 | if (rc && errno == ENOENT) { | |
7a862a02 | 728 | pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n"); |
da9b4a62 DW |
729 | rc = 0; |
730 | } | |
731 | rv |= rc; | |
732 | } | |
733 | ||
f35f2525 N |
734 | if (info->array.level > 0) |
735 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
f897078e N |
736 | |
737 | if (info->reshape_active) { | |
738 | rv |= sysfs_set_num(info, NULL, "reshape_position", | |
739 | info->reshape_progress); | |
740 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk); | |
20a40eca N |
741 | rv |= sysfs_set_num(info, NULL, "layout", info->new_layout); |
742 | rv |= sysfs_set_num(info, NULL, "raid_disks", | |
a5062b1c | 743 | info->array.raid_disks); |
20a40eca N |
744 | /* We don't set 'new_level' here. That can only happen |
745 | * once the reshape completes. | |
f897078e N |
746 | */ |
747 | } | |
2432ce9b AP |
748 | |
749 | if (info->consistency_policy == CONSISTENCY_POLICY_PPL) { | |
5f21d674 MT |
750 | char *policy = map_num_s(consistency_policies, |
751 | info->consistency_policy); | |
752 | ||
753 | if (sysfs_set_str(info, NULL, "consistency_policy", policy)) { | |
2c8890e9 AP |
754 | pr_err("This kernel does not support PPL. Falling back to consistency-policy=resync.\n"); |
755 | info->consistency_policy = CONSISTENCY_POLICY_RESYNC; | |
2432ce9b AP |
756 | } |
757 | } | |
758 | ||
2503d23b NB |
759 | return rv; |
760 | } | |
761 | ||
2904b26f | 762 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume) |
2503d23b | 763 | { |
33a6535d AW |
764 | char dv[PATH_MAX]; |
765 | char nm[PATH_MAX]; | |
2503d23b NB |
766 | char *dname; |
767 | int rv; | |
bb758cca | 768 | int i; |
2503d23b NB |
769 | |
770 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
771 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
772 | if (rv) | |
773 | return rv; | |
774 | ||
775 | memset(nm, 0, sizeof(nm)); | |
bc17158d | 776 | dname = devid2kname(makedev(sd->disk.major, sd->disk.minor)); |
2503d23b NB |
777 | strcpy(sd->sys_name, "dev-"); |
778 | strcpy(sd->sys_name+4, dname); | |
779 | ||
2904b26f DW |
780 | /* test write to see if 'recovery_start' is available */ |
781 | if (resume && sd->recovery_start < MaxSector && | |
782 | sysfs_set_num(sra, sd, "recovery_start", 0)) { | |
783 | sysfs_set_str(sra, sd, "state", "remove"); | |
784 | return -1; | |
785 | } | |
786 | ||
73649188 | 787 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b | 788 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
6f2af6a4 | 789 | if (!is_container(sra->array.level)) { |
2c8890e9 | 790 | if (sra->consistency_policy == CONSISTENCY_POLICY_PPL) { |
2432ce9b AP |
791 | rv |= sysfs_set_num(sra, sd, "ppl_sector", sd->ppl_sector); |
792 | rv |= sysfs_set_num(sra, sd, "ppl_size", sd->ppl_size); | |
793 | } | |
d23534e4 | 794 | if (sd->recovery_start == MaxSector) |
462906cd N |
795 | /* This can correctly fail if array isn't started, |
796 | * yet, so just ignore status for now. | |
797 | */ | |
d23534e4 | 798 | sysfs_set_str(sra, sd, "state", "insync"); |
899aead0 AK |
799 | if (sd->disk.raid_disk >= 0) |
800 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
2904b26f DW |
801 | if (resume) |
802 | sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start); | |
2503d23b | 803 | } |
bb758cca TM |
804 | if (sd->bb.supported) { |
805 | if (sysfs_set_str(sra, sd, "state", "external_bbl")) { | |
806 | /* | |
807 | * backward compatibility - if kernel doesn't support | |
808 | * bad blocks for external metadata, let it continue | |
809 | * as long as there are none known so far | |
810 | */ | |
811 | if (sd->bb.count) { | |
812 | pr_err("The kernel has no support for bad blocks in external metadata\n"); | |
813 | return -1; | |
814 | } | |
815 | } | |
816 | ||
817 | for (i = 0; i < sd->bb.count; i++) { | |
818 | char s[30]; | |
819 | const struct md_bb_entry *entry = &sd->bb.entries[i]; | |
820 | ||
821 | snprintf(s, sizeof(s) - 1, "%llu %d\n", entry->sector, | |
822 | entry->length); | |
823 | rv |= sysfs_set_str(sra, sd, "bad_blocks", s); | |
824 | } | |
825 | } | |
2503d23b NB |
826 | return rv; |
827 | } | |
90c8b707 | 828 | |
f1665f72 DW |
829 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
830 | { | |
831 | /* from an open block device, try to retrieve it scsi_id */ | |
832 | struct stat st; | |
833 | char path[256]; | |
f1665f72 DW |
834 | DIR *dir; |
835 | struct dirent *de; | |
d8924477 | 836 | int host, bus, target, lun; |
f1665f72 DW |
837 | |
838 | if (fstat(fd, &st)) | |
839 | return 1; | |
840 | ||
fa89bdee | 841 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device", |
f1665f72 DW |
842 | major(st.st_rdev), minor(st.st_rdev)); |
843 | ||
844 | dir = opendir(path); | |
845 | if (!dir) | |
846 | return 1; | |
847 | ||
d8924477 DW |
848 | for (de = readdir(dir); de; de = readdir(dir)) { |
849 | int count; | |
850 | ||
851 | if (de->d_type != DT_DIR) | |
852 | continue; | |
853 | ||
854 | count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun); | |
855 | if (count == 4) | |
f1665f72 | 856 | break; |
f1665f72 DW |
857 | } |
858 | closedir(dir); | |
859 | ||
860 | if (!de) | |
861 | return 1; | |
862 | ||
d8924477 | 863 | *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0); |
f1665f72 DW |
864 | return 0; |
865 | } | |
f94d52f4 | 866 | |
4dd2df09 | 867 | int sysfs_unique_holder(char *devnm, long rdev) |
f94d52f4 | 868 | { |
4dd2df09 | 869 | /* Check that devnm is a holder of rdev, |
f94d52f4 NB |
870 | * and is the only holder. |
871 | * we should be locked against races by | |
4dd2df09 | 872 | * an O_EXCL on devnm |
aab15415 N |
873 | * Return values: |
874 | * 0 - not unique, not even a holder | |
875 | * 1 - unique, this is the only holder. | |
876 | * 2/3 - not unique, there is another holder | |
877 | * -1 - error, cannot find the holders | |
f94d52f4 NB |
878 | */ |
879 | DIR *dir; | |
880 | struct dirent *de; | |
881 | char dirname[100]; | |
882 | char l; | |
aab15415 | 883 | int ret = 0; |
f94d52f4 NB |
884 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", |
885 | major(rdev), minor(rdev)); | |
886 | dir = opendir(dirname); | |
f94d52f4 | 887 | if (!dir) |
aab15415 | 888 | return -1; |
f94d52f4 NB |
889 | l = strlen(dirname); |
890 | while ((de = readdir(dir)) != NULL) { | |
4dd2df09 N |
891 | char buf[100]; |
892 | char *sl; | |
f94d52f4 | 893 | int n; |
f94d52f4 NB |
894 | |
895 | if (de->d_ino == 0) | |
896 | continue; | |
897 | if (de->d_name[0] == '.') | |
898 | continue; | |
899 | strcpy(dirname+l, "/"); | |
900 | strcat(dirname+l, de->d_name); | |
4dd2df09 N |
901 | n = readlink(dirname, buf, sizeof(buf)-1); |
902 | if (n <= 0) | |
93f1df33 | 903 | continue; |
f94d52f4 | 904 | buf[n] = 0; |
4dd2df09 N |
905 | sl = strrchr(buf, '/'); |
906 | if (!sl) | |
aab15415 | 907 | continue; |
4dd2df09 | 908 | sl++; |
f94d52f4 | 909 | |
4dd2df09 | 910 | if (strcmp(devnm, sl) == 0) |
aab15415 N |
911 | ret |= 1; |
912 | else | |
913 | ret |= 2; | |
f94d52f4 NB |
914 | } |
915 | closedir(dir); | |
aab15415 | 916 | return ret; |
f94d52f4 | 917 | } |
38a07ed6 | 918 | |
bc77ed53 DW |
919 | int sysfs_freeze_array(struct mdinfo *sra) |
920 | { | |
921 | /* Try to freeze resync/rebuild on this array/container. | |
922 | * Return -1 if the array is busy, | |
bc77ed53 DW |
923 | * return 0 if this kernel doesn't support 'frozen' |
924 | * return 1 if it worked. | |
925 | */ | |
90fd7001 | 926 | char buf[SYSFS_MAX_BUF_SIZE]; |
bc77ed53 DW |
927 | |
928 | if (!sysfs_attribute_available(sra, NULL, "sync_action")) | |
929 | return 1; /* no sync_action == frozen */ | |
90fd7001 | 930 | if (sysfs_get_str(sra, NULL, "sync_action", buf, sizeof(buf)) <= 0) |
bc77ed53 | 931 | return 0; |
fd324b08 N |
932 | if (strcmp(buf, "frozen\n") == 0) |
933 | /* Already frozen */ | |
934 | return 0; | |
dea3786a | 935 | if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0) |
bc77ed53 DW |
936 | return -1; |
937 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
938 | return 0; | |
939 | return 1; | |
940 | } | |
efc67e8e N |
941 | |
942 | int sysfs_wait(int fd, int *msec) | |
943 | { | |
944 | /* Wait up to '*msec' for fd to have an exception condition. | |
945 | * if msec == NULL, wait indefinitely. | |
946 | */ | |
947 | fd_set fds; | |
948 | int n; | |
949 | FD_ZERO(&fds); | |
950 | FD_SET(fd, &fds); | |
951 | if (msec == NULL) | |
952 | n = select(fd+1, NULL, NULL, &fds, NULL); | |
953 | else if (*msec < 0) | |
954 | n = 0; | |
955 | else { | |
956 | struct timeval start, end, tv; | |
957 | gettimeofday(&start, NULL); | |
4bffc964 N |
958 | if (*msec < 1000) { |
959 | tv.tv_sec = 0; | |
efc67e8e | 960 | tv.tv_usec = (*msec)*1000; |
4bffc964 | 961 | } else { |
efc67e8e | 962 | tv.tv_sec = (*msec)/1000; |
4bffc964 N |
963 | tv.tv_usec = 0; |
964 | } | |
efc67e8e N |
965 | n = select(fd+1, NULL, NULL, &fds, &tv); |
966 | gettimeofday(&end, NULL); | |
967 | end.tv_sec -= start.tv_sec; | |
968 | *msec -= (end.tv_sec * 1000 + end.tv_usec/1000 | |
4bffc964 | 969 | - start.tv_usec/1000) + 1; |
efc67e8e N |
970 | } |
971 | return n; | |
972 | } | |
b0681598 MD |
973 | |
974 | int sysfs_rules_apply_check(const struct mdinfo *sra, | |
975 | const struct sysfs_entry *ent) | |
976 | { | |
977 | /* Check whether parameter is regular file, | |
978 | * exists and is under specified directory. | |
979 | */ | |
980 | char fname[MAX_SYSFS_PATH_LEN]; | |
981 | char dname[MAX_SYSFS_PATH_LEN]; | |
982 | char resolved_path[PATH_MAX]; | |
983 | char resolved_dir[PATH_MAX]; | |
fd5b09c9 | 984 | int result; |
b0681598 MD |
985 | |
986 | if (sra == NULL || ent == NULL) | |
987 | return -1; | |
988 | ||
fd5b09c9 KS |
989 | result = snprintf(dname, MAX_SYSFS_PATH_LEN, |
990 | "/sys/block/%s/md/", sra->sys_name); | |
991 | if (result < 0 || result >= MAX_SYSFS_PATH_LEN) | |
992 | return -1; | |
993 | ||
994 | result = snprintf(fname, MAX_SYSFS_PATH_LEN, | |
995 | "%s/%s", dname, ent->name); | |
996 | if (result < 0 || result >= MAX_SYSFS_PATH_LEN) | |
997 | return -1; | |
b0681598 MD |
998 | |
999 | if (realpath(fname, resolved_path) == NULL || | |
1000 | realpath(dname, resolved_dir) == NULL) | |
1001 | return -1; | |
1002 | ||
1003 | if (strncmp(resolved_dir, resolved_path, | |
1004 | strnlen(resolved_dir, PATH_MAX)) != 0) | |
1005 | return -1; | |
1006 | ||
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | static struct dev_sysfs_rule *sysfs_rules; | |
1011 | ||
1012 | void sysfs_rules_apply(char *devnm, struct mdinfo *dev) | |
1013 | { | |
1014 | struct dev_sysfs_rule *rules = sysfs_rules; | |
1015 | ||
1016 | while (rules) { | |
1017 | struct sysfs_entry *ent = rules->entry; | |
1018 | int match = 0; | |
1019 | ||
1020 | if (!rules->uuid_set) { | |
1021 | if (rules->devname) | |
1022 | match = strcmp(devnm, rules->devname) == 0; | |
1023 | } else { | |
1024 | match = memcmp(dev->uuid, rules->uuid, | |
1025 | sizeof(int[4])) == 0; | |
1026 | } | |
1027 | ||
1028 | while (match && ent) { | |
1029 | if (sysfs_rules_apply_check(dev, ent) < 0) | |
1030 | pr_err("SYSFS: failed to write '%s' to '%s'\n", | |
1031 | ent->value, ent->name); | |
1032 | else | |
1033 | sysfs_set_str(dev, NULL, ent->name, ent->value); | |
1034 | ent = ent->next; | |
1035 | } | |
1036 | rules = rules->next; | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | static void sysfs_rule_free(struct dev_sysfs_rule *rule) | |
1041 | { | |
1042 | struct sysfs_entry *entry; | |
1043 | ||
1044 | while (rule) { | |
1045 | struct dev_sysfs_rule *tmp = rule->next; | |
1046 | ||
1047 | entry = rule->entry; | |
1048 | while (entry) { | |
1049 | struct sysfs_entry *tmp = entry->next; | |
1050 | ||
1051 | free(entry->name); | |
1052 | free(entry->value); | |
1053 | free(entry); | |
1054 | entry = tmp; | |
1055 | } | |
1056 | ||
1057 | if (rule->devname) | |
1058 | free(rule->devname); | |
1059 | free(rule); | |
1060 | rule = tmp; | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | void sysfsline(char *line) | |
1065 | { | |
1066 | struct dev_sysfs_rule *sr; | |
1067 | char *w; | |
1068 | ||
1069 | sr = xcalloc(1, sizeof(*sr)); | |
1070 | for (w = dl_next(line); w != line ; w = dl_next(w)) { | |
1071 | if (strncasecmp(w, "name=", 5) == 0) { | |
1072 | char *devname = w + 5; | |
1073 | ||
b9ce7ab0 | 1074 | if (strncmp(devname, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) { |
b0681598 MD |
1075 | if (sr->devname) |
1076 | pr_err("Only give one device per SYSFS line: %s\n", | |
1077 | devname); | |
1078 | else | |
1079 | sr->devname = xstrdup(devname); | |
1080 | } else { | |
1081 | pr_err("%s is an invalid name for an md device - ignored.\n", | |
1082 | devname); | |
1083 | } | |
1084 | } else if (strncasecmp(w, "uuid=", 5) == 0) { | |
1085 | char *uuid = w + 5; | |
1086 | ||
1087 | if (sr->uuid_set) { | |
1088 | pr_err("Only give one uuid per SYSFS line: %s\n", | |
1089 | uuid); | |
1090 | } else { | |
1091 | if (parse_uuid(w + 5, sr->uuid) && | |
1092 | memcmp(sr->uuid, uuid_zero, | |
1093 | sizeof(int[4])) != 0) | |
1094 | sr->uuid_set = 1; | |
1095 | else | |
1096 | pr_err("Invalid uuid: %s\n", uuid); | |
1097 | } | |
1098 | } else { | |
1099 | struct sysfs_entry *prop; | |
1100 | ||
1101 | char *sep = strchr(w, '='); | |
1102 | ||
1103 | if (sep == NULL || *(sep + 1) == 0) { | |
1104 | pr_err("Cannot parse \"%s\" - ignoring.\n", w); | |
1105 | continue; | |
1106 | } | |
1107 | ||
1108 | prop = xmalloc(sizeof(*prop)); | |
1109 | prop->value = xstrdup(sep + 1); | |
1110 | *sep = 0; | |
1111 | prop->name = xstrdup(w); | |
1112 | prop->next = sr->entry; | |
1113 | sr->entry = prop; | |
1114 | } | |
1115 | } | |
1116 | ||
1117 | if (!sr->devname && !sr->uuid_set) { | |
1118 | pr_err("Device name not found in sysfs config entry - ignoring.\n"); | |
1119 | sysfs_rule_free(sr); | |
1120 | return; | |
1121 | } | |
1122 | ||
1123 | sr->next = sysfs_rules; | |
1124 | sysfs_rules = sr; | |
1125 | } |