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