]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
6f02172d | 4 | * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de> |
64c4757e NB |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
e736b623 | 22 | * Email: <neilb@suse.de> |
64c4757e NB |
23 | */ |
24 | ||
9a9dab36 | 25 | #include "mdadm.h" |
64c4757e | 26 | #include "md_p.h" |
edd8d13c | 27 | #include <sys/socket.h> |
64c4757e | 28 | #include <sys/utsname.h> |
9fe32043 | 29 | #include <sys/wait.h> |
edd8d13c | 30 | #include <sys/un.h> |
a7dec3fd | 31 | #include <sys/resource.h> |
a9c15847 | 32 | #include <sys/vfs.h> |
b2514242 | 33 | #include <sys/mman.h> |
a9c15847 | 34 | #include <linux/magic.h> |
986b8688 | 35 | #include <poll.h> |
98c6faba | 36 | #include <ctype.h> |
a322f70c | 37 | #include <dirent.h> |
a931db9e | 38 | #include <signal.h> |
7716570e | 39 | #include <dlfcn.h> |
7716570e | 40 | |
0a816ef9 NB |
41 | |
42 | /* | |
43 | * following taken from linux/blkpg.h because they aren't | |
44 | * anywhere else and it isn't safe to #include linux/ * stuff. | |
45 | */ | |
46 | ||
47 | #define BLKPG _IO(0x12,105) | |
48 | ||
49 | /* The argument structure */ | |
50 | struct blkpg_ioctl_arg { | |
5d500228 N |
51 | int op; |
52 | int flags; | |
53 | int datalen; | |
54 | void *data; | |
0a816ef9 NB |
55 | }; |
56 | ||
57 | /* The subfunctions (for the op field) */ | |
58 | #define BLKPG_ADD_PARTITION 1 | |
59 | #define BLKPG_DEL_PARTITION 2 | |
60 | ||
61 | /* Sizes of name fields. Unused at present. */ | |
62 | #define BLKPG_DEVNAMELTH 64 | |
63 | #define BLKPG_VOLNAMELTH 64 | |
64 | ||
65 | /* The data structure for ADD_PARTITION and DEL_PARTITION */ | |
66 | struct blkpg_partition { | |
67 | long long start; /* starting offset in bytes */ | |
68 | long long length; /* length in bytes */ | |
69 | int pno; /* partition number */ | |
70 | char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2, | |
71 | to be used in kernel messages */ | |
72 | char volname[BLKPG_VOLNAMELTH]; /* volume label */ | |
73 | }; | |
64c4757e | 74 | |
0f22b998 | 75 | #include "part.h" |
056b331e N |
76 | |
77 | /* Force a compilation error if condition is true */ | |
78 | #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) | |
79 | ||
80 | /* Force a compilation error if condition is true, but also produce a | |
81 | result (of value 0 and type size_t), so the expression can be used | |
82 | e.g. in a structure initializer (or where-ever else comma expressions | |
83 | aren't permitted). */ | |
84 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) | |
85 | ||
d15a1f72 | 86 | static int is_dlm_hooks_ready = 0; |
7071320a N |
87 | |
88 | int dlm_funs_ready(void) | |
89 | { | |
90 | return is_dlm_hooks_ready ? 1 : 0; | |
91 | } | |
92 | ||
7071320a | 93 | static struct dlm_hooks *dlm_hooks = NULL; |
d15a1f72 GJ |
94 | struct dlm_lock_resource *dlm_lock_res = NULL; |
95 | static int ast_called = 0; | |
96 | ||
97 | struct dlm_lock_resource { | |
98 | dlm_lshandle_t *ls; | |
99 | struct dlm_lksb lksb; | |
100 | }; | |
101 | ||
d15a1f72 GJ |
102 | /* Using poll(2) to wait for and dispatch ASTs */ |
103 | static int poll_for_ast(dlm_lshandle_t ls) | |
104 | { | |
105 | struct pollfd pfd; | |
106 | ||
107 | pfd.fd = dlm_hooks->ls_get_fd(ls); | |
108 | pfd.events = POLLIN; | |
109 | ||
110 | while (!ast_called) | |
111 | { | |
112 | if (poll(&pfd, 1, 0) < 0) | |
113 | { | |
114 | perror("poll"); | |
115 | return -1; | |
116 | } | |
117 | dlm_hooks->dispatch(dlm_hooks->ls_get_fd(ls)); | |
118 | } | |
119 | ast_called = 0; | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | static void dlm_ast(void *arg) | |
125 | { | |
126 | ast_called = 1; | |
127 | } | |
128 | ||
81a8a694 | 129 | static char *cluster_name = NULL; |
d15a1f72 | 130 | /* Create the lockspace, take bitmapXXX locks on all the bitmaps. */ |
81a8a694 | 131 | int cluster_get_dlmlock(int *lockid) |
d15a1f72 GJ |
132 | { |
133 | int ret = -1; | |
134 | char str[64]; | |
135 | int flags = LKF_NOQUEUE; | |
136 | ||
81a8a694 GJ |
137 | ret = get_cluster_name(&cluster_name); |
138 | if (ret) { | |
139 | pr_err("The md can't get cluster name\n"); | |
140 | return -1; | |
141 | } | |
142 | ||
d15a1f72 | 143 | dlm_lock_res = xmalloc(sizeof(struct dlm_lock_resource)); |
81a8a694 | 144 | dlm_lock_res->ls = dlm_hooks->create_lockspace(cluster_name, O_RDWR); |
d15a1f72 | 145 | if (!dlm_lock_res->ls) { |
81a8a694 | 146 | pr_err("%s failed to create lockspace\n", cluster_name); |
32539f74 | 147 | return -ENOMEM; |
d15a1f72 GJ |
148 | } |
149 | ||
81a8a694 | 150 | snprintf(str, 64, "bitmap%s", cluster_name); |
b7a462e5 JS |
151 | ret = dlm_hooks->ls_lock(dlm_lock_res->ls, LKM_PWMODE, |
152 | &dlm_lock_res->lksb, flags, str, strlen(str), | |
153 | 0, dlm_ast, dlm_lock_res, NULL, NULL); | |
d15a1f72 GJ |
154 | if (ret) { |
155 | pr_err("error %d when get PW mode on lock %s\n", errno, str); | |
32539f74 GJ |
156 | dlm_hooks->release_lockspace(cluster_name, dlm_lock_res->ls, 1); |
157 | return ret; | |
d15a1f72 GJ |
158 | } |
159 | ||
160 | /* Wait for it to complete */ | |
161 | poll_for_ast(dlm_lock_res->ls); | |
162 | *lockid = dlm_lock_res->lksb.sb_lkid; | |
163 | ||
32539f74 | 164 | return dlm_lock_res->lksb.sb_status; |
d15a1f72 GJ |
165 | } |
166 | ||
81a8a694 | 167 | int cluster_release_dlmlock(int lockid) |
d15a1f72 GJ |
168 | { |
169 | int ret = -1; | |
170 | ||
81a8a694 GJ |
171 | if (!cluster_name) |
172 | return -1; | |
173 | ||
d15a1f72 GJ |
174 | ret = dlm_hooks->ls_unlock(dlm_lock_res->ls, lockid, 0, |
175 | &dlm_lock_res->lksb, dlm_lock_res); | |
176 | if (ret) { | |
177 | pr_err("error %d happened when unlock\n", errno); | |
178 | /* XXX make sure the lock is unlocked eventually */ | |
179 | goto out; | |
180 | } | |
181 | ||
182 | /* Wait for it to complete */ | |
183 | poll_for_ast(dlm_lock_res->ls); | |
184 | ||
185 | errno = dlm_lock_res->lksb.sb_status; | |
186 | if (errno != EUNLOCK) { | |
b7a462e5 JS |
187 | pr_err("error %d happened in ast when unlock lockspace\n", |
188 | errno); | |
d15a1f72 GJ |
189 | /* XXX make sure the lockspace is unlocked eventually */ |
190 | goto out; | |
191 | } | |
192 | ||
81a8a694 | 193 | ret = dlm_hooks->release_lockspace(cluster_name, dlm_lock_res->ls, 1); |
d15a1f72 GJ |
194 | if (ret) { |
195 | pr_err("error %d happened when release lockspace\n", errno); | |
196 | /* XXX make sure the lockspace is released eventually */ | |
197 | goto out; | |
198 | } | |
199 | free(dlm_lock_res); | |
200 | ||
201 | out: | |
202 | return ret; | |
203 | } | |
204 | ||
9db2ab4e JS |
205 | int md_array_valid(int fd) |
206 | { | |
207 | struct mdinfo *sra; | |
208 | int ret; | |
209 | ||
210 | sra = sysfs_read(fd, NULL, GET_ARRAY_STATE); | |
211 | if (sra) { | |
212 | if (sra->array_state != ARRAY_UNKNOWN_STATE) | |
213 | ret = 0; | |
214 | else | |
215 | ret = -ENODEV; | |
216 | ||
217 | free(sra); | |
218 | } else { | |
219 | /* | |
220 | * GET_ARRAY_INFO doesn't provide access to the proper state | |
221 | * information, so fallback to a basic check for raid_disks != 0 | |
222 | */ | |
223 | ret = ioctl(fd, RAID_VERSION); | |
224 | } | |
225 | ||
226 | return !ret; | |
227 | } | |
228 | ||
3ab8f4bf JS |
229 | int md_array_active(int fd) |
230 | { | |
231 | struct mdinfo *sra; | |
232 | struct mdu_array_info_s array; | |
a822017f | 233 | int ret = 0; |
3ab8f4bf JS |
234 | |
235 | sra = sysfs_read(fd, NULL, GET_ARRAY_STATE); | |
236 | if (sra) { | |
a822017f | 237 | if (!md_array_is_active(sra)) |
3ab8f4bf JS |
238 | ret = -ENODEV; |
239 | ||
240 | free(sra); | |
241 | } else { | |
242 | /* | |
243 | * GET_ARRAY_INFO doesn't provide access to the proper state | |
244 | * information, so fallback to a basic check for raid_disks != 0 | |
245 | */ | |
246 | ret = ioctl(fd, GET_ARRAY_INFO, &array); | |
247 | } | |
248 | ||
249 | return !ret; | |
250 | } | |
251 | ||
a822017f MT |
252 | int md_array_is_active(struct mdinfo *info) |
253 | { | |
254 | return (info->array_state != ARRAY_CLEAR && | |
255 | info->array_state != ARRAY_INACTIVE && | |
256 | info->array_state != ARRAY_UNKNOWN_STATE); | |
257 | } | |
258 | ||
9cd39f01 JS |
259 | /* |
260 | * Get array info from the kernel. Longer term we want to deprecate the | |
261 | * ioctl and get it from sysfs. | |
262 | */ | |
263 | int md_get_array_info(int fd, struct mdu_array_info_s *array) | |
264 | { | |
265 | return ioctl(fd, GET_ARRAY_INFO, array); | |
266 | } | |
267 | ||
018a4882 JS |
268 | /* |
269 | * Set array info | |
270 | */ | |
271 | int md_set_array_info(int fd, struct mdu_array_info_s *array) | |
272 | { | |
273 | return ioctl(fd, SET_ARRAY_INFO, array); | |
274 | } | |
275 | ||
d97572f5 JS |
276 | /* |
277 | * Get disk info from the kernel. | |
278 | */ | |
279 | int md_get_disk_info(int fd, struct mdu_disk_info_s *disk) | |
280 | { | |
281 | return ioctl(fd, GET_DISK_INFO, disk); | |
282 | } | |
283 | ||
64c4757e NB |
284 | /* |
285 | * Parse a 128 bit uuid in 4 integers | |
286 | * format is 32 hexx nibbles with options :.<space> separator | |
287 | * If not exactly 32 hex digits are found, return 0 | |
288 | * else return 1 | |
289 | */ | |
290 | int parse_uuid(char *str, int uuid[4]) | |
291 | { | |
aba69144 NB |
292 | int hit = 0; /* number of Hex digIT */ |
293 | int i; | |
294 | char c; | |
ca3b6696 N |
295 | for (i = 0; i < 4; i++) |
296 | uuid[i] = 0; | |
aba69144 | 297 | |
ca3b6696 | 298 | while ((c = *str++) != 0) { |
aba69144 | 299 | int n; |
ca3b6696 | 300 | if (c >= '0' && c <= '9') |
aba69144 | 301 | n = c-'0'; |
ca3b6696 | 302 | else if (c >= 'a' && c <= 'f') |
aba69144 | 303 | n = 10 + c - 'a'; |
ca3b6696 | 304 | else if (c >= 'A' && c <= 'F') |
aba69144 NB |
305 | n = 10 + c - 'A'; |
306 | else if (strchr(":. -", c)) | |
307 | continue; | |
308 | else return 0; | |
309 | ||
310 | if (hit<32) { | |
311 | uuid[hit/8] <<= 4; | |
312 | uuid[hit/8] += n; | |
313 | } | |
314 | hit++; | |
82b27616 | 315 | } |
aba69144 NB |
316 | if (hit == 32) |
317 | return 1; | |
318 | return 0; | |
64c4757e NB |
319 | } |
320 | ||
64c4757e NB |
321 | int get_linux_version() |
322 | { | |
323 | struct utsname name; | |
98c6faba | 324 | char *cp; |
f161d047 | 325 | int a = 0, b = 0,c = 0; |
64c4757e NB |
326 | if (uname(&name) <0) |
327 | return -1; | |
328 | ||
98c6faba NB |
329 | cp = name.release; |
330 | a = strtoul(cp, &cp, 10); | |
f161d047 N |
331 | if (*cp == '.') |
332 | b = strtoul(cp+1, &cp, 10); | |
333 | if (*cp == '.') | |
334 | c = strtoul(cp+1, &cp, 10); | |
98c6faba | 335 | |
682c7051 | 336 | return (a*1000000)+(b*1000)+c; |
64c4757e NB |
337 | } |
338 | ||
bc77ed53 DW |
339 | int mdadm_version(char *version) |
340 | { | |
341 | int a, b, c; | |
342 | char *cp; | |
343 | ||
344 | if (!version) | |
345 | version = Version; | |
346 | ||
347 | cp = strchr(version, '-'); | |
348 | if (!cp || *(cp+1) != ' ' || *(cp+2) != 'v') | |
349 | return -1; | |
350 | cp += 3; | |
351 | a = strtoul(cp, &cp, 10); | |
352 | if (*cp != '.') | |
353 | return -1; | |
354 | b = strtoul(cp+1, &cp, 10); | |
355 | if (*cp == '.') | |
356 | c = strtoul(cp+1, &cp, 10); | |
357 | else | |
358 | c = 0; | |
359 | if (*cp != ' ' && *cp != '-') | |
360 | return -1; | |
361 | return (a*1000000)+(b*1000)+c; | |
362 | } | |
363 | ||
51d4261c | 364 | unsigned long long parse_size(char *size) |
84e11361 N |
365 | { |
366 | /* parse 'size' which should be a number optionally | |
367 | * followed by 'K', 'M', or 'G'. | |
368 | * Without a suffix, K is assumed. | |
369 | * Number returned is in sectors (half-K) | |
822e393a | 370 | * INVALID_SECTORS returned on error. |
84e11361 N |
371 | */ |
372 | char *c; | |
373 | long long s = strtoll(size, &c, 10); | |
374 | if (s > 0) { | |
375 | switch (*c) { | |
376 | case 'K': | |
377 | c++; | |
378 | default: | |
379 | s *= 2; | |
380 | break; | |
381 | case 'M': | |
382 | c++; | |
383 | s *= 1024 * 2; | |
384 | break; | |
385 | case 'G': | |
386 | c++; | |
387 | s *= 1024 * 1024 * 2; | |
388 | break; | |
72ca9bcf N |
389 | case 's': /* sectors */ |
390 | c++; | |
391 | break; | |
84e11361 | 392 | } |
51d4261c | 393 | } else |
822e393a | 394 | s = INVALID_SECTORS; |
84e11361 | 395 | if (*c) |
822e393a | 396 | s = INVALID_SECTORS; |
84e11361 N |
397 | return s; |
398 | } | |
399 | ||
4a06e2c2 N |
400 | int parse_layout_10(char *layout) |
401 | { | |
402 | int copies, rv; | |
403 | char *cp; | |
404 | /* Parse the layout string for raid10 */ | |
405 | /* 'f', 'o' or 'n' followed by a number <= raid_disks */ | |
406 | if ((layout[0] != 'n' && layout[0] != 'f' && layout[0] != 'o') || | |
407 | (copies = strtoul(layout+1, &cp, 10)) < 1 || | |
408 | copies > 200 || | |
409 | *cp) | |
410 | return -1; | |
411 | if (layout[0] == 'n') | |
412 | rv = 256 + copies; | |
413 | else if (layout[0] == 'o') | |
414 | rv = 0x10000 + (copies<<8) + 1; | |
415 | else | |
416 | rv = 1 + (copies<<8); | |
417 | return rv; | |
418 | } | |
419 | ||
420 | int parse_layout_faulty(char *layout) | |
421 | { | |
422 | /* Parse the layout string for 'faulty' */ | |
423 | int ln = strcspn(layout, "0123456789"); | |
503975b9 | 424 | char *m = xstrdup(layout); |
4a06e2c2 N |
425 | int mode; |
426 | m[ln] = 0; | |
427 | mode = map_name(faultylayout, m); | |
428 | if (mode == UnSet) | |
429 | return -1; | |
430 | ||
431 | return mode | (atoi(layout+ln)<< ModeShift); | |
432 | } | |
79868890 N |
433 | |
434 | long parse_num(char *num) | |
435 | { | |
436 | /* Either return a valid number, or -1 */ | |
437 | char *c; | |
438 | long rv = strtol(num, &c, 10); | |
439 | if (rv < 0 || *c || !num[0]) | |
440 | return -1; | |
441 | else | |
442 | return rv; | |
443 | } | |
4a06e2c2 | 444 | |
4de90913 GJ |
445 | int parse_cluster_confirm_arg(char *input, char **devname, int *slot) |
446 | { | |
447 | char *dev; | |
448 | *slot = strtoul(input, &dev, 10); | |
449 | if (dev == input || dev[0] != ':') | |
450 | return -1; | |
451 | *devname = dev+1; | |
452 | return 0; | |
453 | } | |
454 | ||
0430ed48 NB |
455 | void remove_partitions(int fd) |
456 | { | |
457 | /* remove partitions from this block devices. | |
458 | * This is used for components added to an array | |
459 | */ | |
460 | #ifdef BLKPG_DEL_PARTITION | |
461 | struct blkpg_ioctl_arg a; | |
462 | struct blkpg_partition p; | |
463 | ||
464 | a.op = BLKPG_DEL_PARTITION; | |
465 | a.data = (void*)&p; | |
466 | a.datalen = sizeof(p); | |
467 | a.flags = 0; | |
468 | memset(a.data, 0, a.datalen); | |
ca3b6696 | 469 | for (p.pno = 0; p.pno < 16; p.pno++) |
0430ed48 NB |
470 | ioctl(fd, BLKPG, &a); |
471 | #endif | |
472 | } | |
473 | ||
691c6ee1 N |
474 | int test_partition(int fd) |
475 | { | |
476 | /* Check if fd is a whole-disk or a partition. | |
477 | * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION | |
478 | * will return ENXIO on an invalid partition number. | |
479 | */ | |
480 | struct blkpg_ioctl_arg a; | |
481 | struct blkpg_partition p; | |
482 | a.op = BLKPG_DEL_PARTITION; | |
483 | a.data = (void*)&p; | |
484 | a.datalen = sizeof(p); | |
485 | a.flags = 0; | |
486 | memset(a.data, 0, a.datalen); | |
487 | p.pno = 1<<30; | |
488 | if (ioctl(fd, BLKPG, &a) == 0) | |
489 | /* Very unlikely, but not a partition */ | |
490 | return 0; | |
357ac106 | 491 | if (errno == ENXIO || errno == ENOTTY) |
691c6ee1 N |
492 | /* not a partition */ |
493 | return 0; | |
494 | ||
495 | return 1; | |
496 | } | |
497 | ||
bfd76b93 CA |
498 | int test_partition_from_id(dev_t id) |
499 | { | |
500 | char buf[20]; | |
501 | int fd, rv; | |
502 | ||
503 | sprintf(buf, "%d:%d", major(id), minor(id)); | |
504 | fd = dev_open(buf, O_RDONLY); | |
505 | if (fd < 0) | |
506 | return -1; | |
507 | rv = test_partition(fd); | |
508 | close(fd); | |
509 | return rv; | |
510 | } | |
691c6ee1 | 511 | |
de5a472e | 512 | int enough(int level, int raid_disks, int layout, int clean, char *avail) |
64c4757e | 513 | { |
265e0f17 | 514 | int copies, first; |
de5a472e N |
515 | int i; |
516 | int avail_disks = 0; | |
517 | ||
518 | for (i = 0; i < raid_disks; i++) | |
519 | avail_disks += !!avail[i]; | |
520 | ||
64c4757e | 521 | switch (level) { |
265e0f17 NB |
522 | case 10: |
523 | /* This is the tricky one - we need to check | |
524 | * which actual disks are present. | |
525 | */ | |
702b557b | 526 | copies = (layout&255)* ((layout>>8) & 255); |
ca3b6696 | 527 | first = 0; |
265e0f17 NB |
528 | do { |
529 | /* there must be one of the 'copies' form 'first' */ | |
530 | int n = copies; | |
ca3b6696 | 531 | int cnt = 0; |
b3ec716d | 532 | int this = first; |
265e0f17 | 533 | while (n--) { |
b3ec716d | 534 | if (avail[this]) |
265e0f17 | 535 | cnt++; |
b3ec716d | 536 | this = (this+1) % raid_disks; |
265e0f17 NB |
537 | } |
538 | if (cnt == 0) | |
539 | return 0; | |
b3ec716d | 540 | first = (first+(layout&255)) % raid_disks; |
265e0f17 NB |
541 | } while (first != 0); |
542 | return 1; | |
e5329c37 | 543 | |
df0d4ea0 | 544 | case LEVEL_MULTIPATH: |
e0d19036 | 545 | return avail_disks>= 1; |
df0d4ea0 | 546 | case LEVEL_LINEAR: |
64c4757e NB |
547 | case 0: |
548 | return avail_disks == raid_disks; | |
549 | case 1: | |
550 | return avail_disks >= 1; | |
551 | case 4: | |
330d6900 N |
552 | if (avail_disks == raid_disks - 1 && |
553 | !avail[raid_disks - 1]) | |
554 | /* If just the parity device is missing, then we | |
555 | * have enough, even if not clean | |
556 | */ | |
557 | return 1; | |
558 | /* FALL THROUGH */ | |
64c4757e | 559 | case 5: |
583315d9 NB |
560 | if (clean) |
561 | return avail_disks >= raid_disks-1; | |
562 | else | |
563 | return avail_disks >= raid_disks; | |
98c6faba | 564 | case 6: |
583315d9 NB |
565 | if (clean) |
566 | return avail_disks >= raid_disks-2; | |
567 | else | |
568 | return avail_disks >= raid_disks; | |
64c4757e NB |
569 | default: |
570 | return 0; | |
571 | } | |
572 | } | |
573 | ||
22e263f6 AC |
574 | const int uuid_zero[4] = { 0, 0, 0, 0 }; |
575 | ||
f277ce36 | 576 | int same_uuid(int a[4], int b[4], int swapuuid) |
64c4757e | 577 | { |
f277ce36 NB |
578 | if (swapuuid) { |
579 | /* parse uuids are hostendian. | |
580 | * uuid's from some superblocks are big-ending | |
aba69144 | 581 | * if there is a difference, we need to swap.. |
f277ce36 NB |
582 | */ |
583 | unsigned char *ac = (unsigned char *)a; | |
584 | unsigned char *bc = (unsigned char *)b; | |
585 | int i; | |
ca3b6696 | 586 | for (i = 0; i < 16; i += 4) { |
f277ce36 NB |
587 | if (ac[i+0] != bc[i+3] || |
588 | ac[i+1] != bc[i+2] || | |
589 | ac[i+2] != bc[i+1] || | |
590 | ac[i+3] != bc[i+0]) | |
591 | return 0; | |
592 | } | |
593 | return 1; | |
594 | } else { | |
595 | if (a[0]==b[0] && | |
596 | a[1]==b[1] && | |
597 | a[2]==b[2] && | |
598 | a[3]==b[3]) | |
599 | return 1; | |
600 | return 0; | |
601 | } | |
64c4757e | 602 | } |
ca3b6696 | 603 | |
350f29f9 NB |
604 | void copy_uuid(void *a, int b[4], int swapuuid) |
605 | { | |
606 | if (swapuuid) { | |
607 | /* parse uuids are hostendian. | |
608 | * uuid's from some superblocks are big-ending | |
609 | * if there is a difference, we need to swap.. | |
610 | */ | |
611 | unsigned char *ac = (unsigned char *)a; | |
612 | unsigned char *bc = (unsigned char *)b; | |
613 | int i; | |
ca3b6696 | 614 | for (i = 0; i < 16; i += 4) { |
350f29f9 NB |
615 | ac[i+0] = bc[i+3]; |
616 | ac[i+1] = bc[i+2]; | |
617 | ac[i+2] = bc[i+1]; | |
618 | ac[i+3] = bc[i+0]; | |
619 | } | |
620 | } else | |
621 | memcpy(a, b, 16); | |
622 | } | |
64c4757e | 623 | |
aae5a112 | 624 | char *__fname_from_uuid(int id[4], int swap, char *buf, char sep) |
d7288ddc | 625 | { |
9968e376 | 626 | int i, j; |
d7288ddc N |
627 | char uuid[16]; |
628 | char *c = buf; | |
629 | strcpy(c, "UUID-"); | |
630 | c += strlen(c); | |
aae5a112 | 631 | copy_uuid(uuid, id, swap); |
9968e376 | 632 | for (i = 0; i < 4; i++) { |
9968e376 | 633 | if (i) |
ff54de6e | 634 | *c++ = sep; |
9968e376 DW |
635 | for (j = 3; j >= 0; j--) { |
636 | sprintf(c,"%02x", (unsigned char) uuid[j+4*i]); | |
637 | c+= 2; | |
638 | } | |
d7288ddc N |
639 | } |
640 | return buf; | |
aae5a112 DW |
641 | |
642 | } | |
643 | ||
b7a462e5 JS |
644 | char *fname_from_uuid(struct supertype *st, struct mdinfo *info, |
645 | char *buf, char sep) | |
aae5a112 | 646 | { |
86983cce N |
647 | // dirty hack to work around an issue with super1 superblocks... |
648 | // super1 superblocks need swapuuid set in order for assembly to | |
649 | // work, but can't have it set if we want this printout to match | |
650 | // all the other uuid printouts in super1.c, so we force swapuuid | |
651 | // to 1 to make our printout match the rest of super1 | |
b7a462e5 JS |
652 | return __fname_from_uuid(info->uuid, (st->ss == &super1) ? 1 : |
653 | st->ss->swapuuid, buf, sep); | |
d7288ddc N |
654 | } |
655 | ||
682c7051 NB |
656 | int check_ext2(int fd, char *name) |
657 | { | |
658 | /* | |
659 | * Check for an ext2fs file system. | |
660 | * Superblock is always 1K at 1K offset | |
661 | * | |
662 | * s_magic is le16 at 56 == 0xEF53 | |
663 | * report mtime - le32 at 44 | |
664 | * blocks - le32 at 4 | |
665 | * logblksize - le32 at 24 | |
666 | */ | |
667 | unsigned char sb[1024]; | |
668 | time_t mtime; | |
2538ba2a N |
669 | unsigned long long size; |
670 | int bsize; | |
682c7051 NB |
671 | if (lseek(fd, 1024,0)!= 1024) |
672 | return 0; | |
673 | if (read(fd, sb, 1024)!= 1024) | |
674 | return 0; | |
675 | if (sb[56] != 0x53 || sb[57] != 0xef) | |
676 | return 0; | |
677 | ||
678 | mtime = sb[44]|(sb[45]|(sb[46]|sb[47]<<8)<<8)<<8; | |
679 | bsize = sb[24]|(sb[25]|(sb[26]|sb[27]<<8)<<8)<<8; | |
680 | size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8; | |
2538ba2a | 681 | size <<= bsize; |
e7b84f9d | 682 | pr_err("%s appears to contain an ext2fs file system\n", |
682c7051 | 683 | name); |
2538ba2a | 684 | cont_err("size=%lluK mtime=%s", size, ctime(&mtime)); |
682c7051 NB |
685 | return 1; |
686 | } | |
687 | ||
688 | int check_reiser(int fd, char *name) | |
689 | { | |
690 | /* | |
691 | * superblock is at 64K | |
692 | * size is 1024; | |
693 | * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52 | |
694 | * | |
695 | */ | |
696 | unsigned char sb[1024]; | |
2538ba2a | 697 | unsigned long long size; |
682c7051 NB |
698 | if (lseek(fd, 64*1024, 0) != 64*1024) |
699 | return 0; | |
700 | if (read(fd, sb, 1024) != 1024) | |
701 | return 0; | |
ca3b6696 N |
702 | if (strncmp((char*)sb+52, "ReIsErFs",8) != 0 && |
703 | strncmp((char*)sb+52, "ReIsEr2Fs",9) != 0) | |
682c7051 | 704 | return 0; |
e7b84f9d | 705 | pr_err("%s appears to contain a reiserfs file system\n",name); |
682c7051 | 706 | size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8; |
2538ba2a | 707 | cont_err("size = %lluK\n", size*4); |
aba69144 | 708 | |
682c7051 NB |
709 | return 1; |
710 | } | |
711 | ||
712 | int check_raid(int fd, char *name) | |
713 | { | |
4b1ac34b | 714 | struct mdinfo info; |
682c7051 | 715 | time_t crtime; |
d078d77c | 716 | char *level; |
82d9eba6 | 717 | struct supertype *st = guess_super(fd); |
f9ce90ba | 718 | |
cb8f6859 N |
719 | if (!st) |
720 | return 0; | |
fa219dd2 MD |
721 | if (st->ss->add_to_super != NULL) { |
722 | st->ss->load_super(st, fd, name); | |
723 | /* Looks like a raid array .. */ | |
724 | pr_err("%s appears to be part of a raid array:\n", name); | |
725 | st->ss->getinfo_super(st, &info, NULL); | |
726 | st->ss->free_super(st); | |
727 | crtime = info.array.ctime; | |
728 | level = map_num(pers, info.array.level); | |
729 | if (!level) | |
730 | level = "-unknown-"; | |
731 | cont_err("level=%s devices=%d ctime=%s", | |
732 | level, info.array.raid_disks, ctime(&crtime)); | |
733 | } else { | |
734 | /* Looks like GPT or MBR */ | |
735 | pr_err("partition table exists on %s\n", name); | |
736 | } | |
82d9eba6 | 737 | return 1; |
682c7051 NB |
738 | } |
739 | ||
0a6bff09 ZL |
740 | int fstat_is_blkdev(int fd, char *devname, dev_t *rdev) |
741 | { | |
742 | struct stat stb; | |
743 | ||
744 | if (fstat(fd, &stb) != 0) { | |
745 | pr_err("fstat failed for %s: %s\n", devname, strerror(errno)); | |
746 | return 0; | |
747 | } | |
748 | if ((S_IFMT & stb.st_mode) != S_IFBLK) { | |
749 | pr_err("%s is not a block device.\n", devname); | |
9e04ac1c ZL |
750 | return 0; |
751 | } | |
752 | if (rdev) | |
753 | *rdev = stb.st_rdev; | |
754 | return 1; | |
755 | } | |
756 | ||
757 | int stat_is_blkdev(char *devname, dev_t *rdev) | |
758 | { | |
759 | struct stat stb; | |
760 | ||
761 | if (stat(devname, &stb) != 0) { | |
762 | pr_err("stat failed for %s: %s\n", devname, strerror(errno)); | |
763 | return 0; | |
764 | } | |
765 | if ((S_IFMT & stb.st_mode) != S_IFBLK) { | |
766 | pr_err("%s is not a block device.\n", devname); | |
0a6bff09 ZL |
767 | return 0; |
768 | } | |
769 | if (rdev) | |
770 | *rdev = stb.st_rdev; | |
771 | return 1; | |
772 | } | |
773 | ||
682c7051 NB |
774 | int ask(char *mesg) |
775 | { | |
776 | char *add = ""; | |
777 | int i; | |
ca3b6696 | 778 | for (i = 0; i < 5; i++) { |
682c7051 NB |
779 | char buf[100]; |
780 | fprintf(stderr, "%s%s", mesg, add); | |
781 | fflush(stderr); | |
782 | if (fgets(buf, 100, stdin)==NULL) | |
783 | return 0; | |
784 | if (buf[0]=='y' || buf[0]=='Y') | |
785 | return 1; | |
786 | if (buf[0]=='n' || buf[0]=='N') | |
787 | return 0; | |
788 | add = "(y/n) "; | |
789 | } | |
e7b84f9d | 790 | pr_err("assuming 'no'\n"); |
682c7051 NB |
791 | return 0; |
792 | } | |
793 | ||
8d80900b | 794 | int is_standard(char *dev, int *nump) |
e5329c37 NB |
795 | { |
796 | /* tests if dev is a "standard" md dev name. | |
797 | * i.e if the last component is "/dNN" or "/mdNN", | |
aba69144 | 798 | * where NN is a string of digits |
598f0d58 NB |
799 | * Returns 1 if a partitionable standard, |
800 | * -1 if non-partitonable, | |
801 | * 0 if not a standard name. | |
e5329c37 | 802 | */ |
8d80900b | 803 | char *d = strrchr(dev, '/'); |
ca3b6696 | 804 | int type = 0; |
8d80900b NB |
805 | int num; |
806 | if (!d) | |
e5329c37 | 807 | return 0; |
ca3b6696 N |
808 | if (strncmp(d, "/d",2) == 0) |
809 | d += 2, type = 1; /* /dev/md/dN{pM} */ | |
810 | else if (strncmp(d, "/md_d", 5) == 0) | |
811 | d += 5, type = 1; /* /dev/md_dN{pM} */ | |
812 | else if (strncmp(d, "/md", 3) == 0) | |
813 | d += 3, type = -1; /* /dev/mdN */ | |
814 | else if (d-dev > 3 && strncmp(d-2, "md/", 3) == 0) | |
815 | d += 1, type = -1; /* /dev/md/N */ | |
e5329c37 NB |
816 | else |
817 | return 0; | |
8d80900b | 818 | if (!*d) |
e5329c37 | 819 | return 0; |
8d80900b NB |
820 | num = atoi(d); |
821 | while (isdigit(*d)) | |
822 | d++; | |
823 | if (*d) | |
e5329c37 | 824 | return 0; |
8d80900b NB |
825 | if (nump) *nump = num; |
826 | ||
827 | return type; | |
e5329c37 NB |
828 | } |
829 | ||
4b1ac34b | 830 | unsigned long calc_csum(void *super, int bytes) |
82b27616 | 831 | { |
56eb10c0 | 832 | unsigned long long newcsum = 0; |
82b27616 | 833 | int i; |
4b1ac34b NB |
834 | unsigned int csum; |
835 | unsigned int *superc = (unsigned int*) super; | |
82b27616 | 836 | |
ca3b6696 N |
837 | for(i = 0; i < bytes/4; i++) |
838 | newcsum += superc[i]; | |
82b27616 | 839 | csum = (newcsum& 0xffffffff) + (newcsum>>32); |
570c0542 | 840 | #ifdef __alpha__ |
aba69144 | 841 | /* The in-kernel checksum calculation is always 16bit on |
570c0542 | 842 | * the alpha, though it is 32 bit on i386... |
ca3b6696 | 843 | * I wonder what it is elsewhere... (it uses an API in |
570c0542 NB |
844 | * a way that it shouldn't). |
845 | */ | |
846 | csum = (csum & 0xffff) + (csum >> 16); | |
847 | csum = (csum & 0xffff) + (csum >> 16); | |
848 | #endif | |
82b27616 NB |
849 | return csum; |
850 | } | |
cd29a5c8 | 851 | |
56eb10c0 | 852 | char *human_size(long long bytes) |
cd29a5c8 | 853 | { |
ff9239ee | 854 | static char buf[47]; |
d5d3721e NB |
855 | |
856 | /* We convert bytes to either centi-M{ega,ibi}bytes or | |
857 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
858 | * and then print 1/100th of those as a decimal. | |
859 | * We allow upto 2048Megabytes before converting to | |
860 | * gigabytes, as that shows more precision and isn't | |
861 | * too large a number. | |
01619b48 | 862 | * Terabytes are not yet handled. |
d5d3721e | 863 | */ |
cd29a5c8 | 864 | |
56eb10c0 | 865 | if (bytes < 5000*1024) |
ca3b6696 | 866 | buf[0] = 0; |
d5d3721e | 867 | else if (bytes < 2*1024LL*1024LL*1024LL) { |
93d3bd3b | 868 | long cMiB = (bytes * 200LL / (1LL<<20) + 1) / 2; |
d5d3721e | 869 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; |
8f23b0b3 | 870 | snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", |
efa29530 | 871 | cMiB/100, cMiB % 100, cMB/100, cMB % 100); |
d5d3721e | 872 | } else { |
93d3bd3b | 873 | long cGiB = (bytes * 200LL / (1LL<<30) +1) / 2; |
d5d3721e | 874 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; |
8f23b0b3 | 875 | snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", |
efa29530 | 876 | cGiB/100, cGiB % 100, cGB/100, cGB % 100); |
d5d3721e | 877 | } |
cd29a5c8 NB |
878 | return buf; |
879 | } | |
e0d19036 | 880 | |
f0ec6710 | 881 | char *human_size_brief(long long bytes, int prefix) |
e0d19036 NB |
882 | { |
883 | static char buf[30]; | |
e0d19036 | 884 | |
570abc6f MN |
885 | /* We convert bytes to either centi-M{ega,ibi}bytes or |
886 | * centi-G{igi,ibi}bytes, with appropriate rounding, | |
887 | * and then print 1/100th of those as a decimal. | |
888 | * We allow upto 2048Megabytes before converting to | |
889 | * gigabytes, as that shows more precision and isn't | |
890 | * too large a number. | |
891 | * Terabytes are not yet handled. | |
f0ec6710 MN |
892 | * |
893 | * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc. | |
894 | * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc. | |
570abc6f MN |
895 | */ |
896 | ||
e0d19036 | 897 | if (bytes < 5000*1024) |
570abc6f | 898 | buf[0] = 0; |
f0ec6710 MN |
899 | else if (prefix == IEC) { |
900 | if (bytes < 2*1024LL*1024LL*1024LL) { | |
93d3bd3b | 901 | long cMiB = (bytes * 200LL / (1LL<<20) +1) /2; |
f0ec6710 | 902 | snprintf(buf, sizeof(buf), "%ld.%02ldMiB", |
efa29530 | 903 | cMiB/100, cMiB % 100); |
f0ec6710 | 904 | } else { |
93d3bd3b | 905 | long cGiB = (bytes * 200LL / (1LL<<30) +1) /2; |
f0ec6710 | 906 | snprintf(buf, sizeof(buf), "%ld.%02ldGiB", |
efa29530 | 907 | cGiB/100, cGiB % 100); |
f0ec6710 MN |
908 | } |
909 | } | |
910 | else if (prefix == JEDEC) { | |
911 | if (bytes < 2*1024LL*1024LL*1024LL) { | |
912 | long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; | |
913 | snprintf(buf, sizeof(buf), "%ld.%02ldMB", | |
efa29530 | 914 | cMB/100, cMB % 100); |
f0ec6710 MN |
915 | } else { |
916 | long cGB = (bytes / (1000000000LL/200LL ) +1) /2; | |
917 | snprintf(buf, sizeof(buf), "%ld.%02ldGB", | |
efa29530 | 918 | cGB/100, cGB % 100); |
f0ec6710 | 919 | } |
570abc6f | 920 | } |
f0ec6710 MN |
921 | else |
922 | buf[0] = 0; | |
570abc6f | 923 | |
e0d19036 NB |
924 | return buf; |
925 | } | |
e4965ef8 N |
926 | |
927 | void print_r10_layout(int layout) | |
928 | { | |
929 | int near = layout & 255; | |
930 | int far = (layout >> 8) & 255; | |
931 | int offset = (layout&0x10000); | |
932 | char *sep = ""; | |
933 | ||
934 | if (near != 1) { | |
935 | printf("%s near=%d", sep, near); | |
936 | sep = ","; | |
937 | } | |
938 | if (far != 1) | |
939 | printf("%s %s=%d", sep, offset?"offset":"far", far); | |
940 | if (near*far == 1) | |
941 | printf("NO REDUNDANCY"); | |
942 | } | |
e0d19036 | 943 | |
5f8097be NB |
944 | unsigned long long calc_array_size(int level, int raid_disks, int layout, |
945 | int chunksize, unsigned long long devsize) | |
577e8448 | 946 | { |
e03640bd CA |
947 | if (level == 1) |
948 | return devsize; | |
577e8448 AK |
949 | devsize &= ~(unsigned long long)((chunksize>>9)-1); |
950 | return get_data_disks(level, layout, raid_disks) * devsize; | |
951 | } | |
952 | ||
953 | int get_data_disks(int level, int layout, int raid_disks) | |
5f8097be NB |
954 | { |
955 | int data_disks = 0; | |
956 | switch (level) { | |
ca3b6696 N |
957 | case 0: data_disks = raid_disks; |
958 | break; | |
959 | case 1: data_disks = 1; | |
960 | break; | |
5f8097be | 961 | case 4: |
ca3b6696 N |
962 | case 5: data_disks = raid_disks - 1; |
963 | break; | |
964 | case 6: data_disks = raid_disks - 2; | |
965 | break; | |
5f8097be NB |
966 | case 10: data_disks = raid_disks / (layout & 255) / ((layout>>8)&255); |
967 | break; | |
968 | } | |
577e8448 AK |
969 | |
970 | return data_disks; | |
5f8097be NB |
971 | } |
972 | ||
13db17bd | 973 | dev_t devnm2devid(char *devnm) |
4dd2df09 N |
974 | { |
975 | /* First look in /sys/block/$DEVNM/dev for %d:%d | |
976 | * If that fails, try parsing out a number | |
977 | */ | |
978 | char path[100]; | |
979 | char *ep; | |
980 | int fd; | |
981 | int mjr,mnr; | |
982 | ||
983 | sprintf(path, "/sys/block/%s/dev", devnm); | |
984 | fd = open(path, O_RDONLY); | |
985 | if (fd >= 0) { | |
986 | char buf[20]; | |
987 | int n = read(fd, buf, sizeof(buf)); | |
988 | close(fd); | |
989 | if (n > 0) | |
990 | buf[n] = 0; | |
991 | if (n > 0 && sscanf(buf, "%d:%d\n", &mjr, &mnr) == 2) | |
992 | return makedev(mjr, mnr); | |
993 | } | |
994 | if (strncmp(devnm, "md_d", 4) == 0 && | |
995 | isdigit(devnm[4]) && | |
996 | (mnr = strtoul(devnm+4, &ep, 10)) >= 0 && | |
997 | ep > devnm && *ep == 0) | |
998 | return makedev(get_mdp_major(), mnr << MdpMinorShift); | |
999 | ||
1000 | if (strncmp(devnm, "md", 2) == 0 && | |
1001 | isdigit(devnm[2]) && | |
1002 | (mnr = strtoul(devnm+2, &ep, 10)) >= 0 && | |
1003 | ep > devnm && *ep == 0) | |
1004 | return makedev(MD_MAJOR, mnr); | |
1005 | ||
1006 | return 0; | |
1007 | } | |
1008 | ||
1009 | char *get_md_name(char *devnm) | |
e0d19036 NB |
1010 | { |
1011 | /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */ | |
98c6faba | 1012 | /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */ |
4dd2df09 | 1013 | |
e0d19036 NB |
1014 | static char devname[50]; |
1015 | struct stat stb; | |
4dd2df09 | 1016 | dev_t rdev = devnm2devid(devnm); |
dd0781e5 | 1017 | char *dn; |
98c6faba | 1018 | |
4dd2df09 N |
1019 | if (rdev == 0) |
1020 | return 0; | |
1021 | if (strncmp(devnm, "md_", 3) == 0) { | |
1022 | snprintf(devname, sizeof(devname), "/dev/md/%s", | |
1023 | devnm + 3); | |
fc54fe7a JS |
1024 | if (stat(devname, &stb) == 0 && |
1025 | (S_IFMT&stb.st_mode) == S_IFBLK && (stb.st_rdev == rdev)) | |
98c6faba NB |
1026 | return devname; |
1027 | } | |
4dd2df09 | 1028 | snprintf(devname, sizeof(devname), "/dev/%s", devnm); |
fc54fe7a JS |
1029 | if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK && |
1030 | (stb.st_rdev == rdev)) | |
4dd2df09 N |
1031 | return devname; |
1032 | ||
1033 | snprintf(devname, sizeof(devname), "/dev/md/%s", devnm+2); | |
fc54fe7a JS |
1034 | if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK && |
1035 | (stb.st_rdev == rdev)) | |
4dd2df09 N |
1036 | return devname; |
1037 | ||
16c6fa80 | 1038 | dn = map_dev(major(rdev), minor(rdev), 0); |
dd0781e5 NB |
1039 | if (dn) |
1040 | return dn; | |
4dd2df09 | 1041 | snprintf(devname, sizeof(devname), "/dev/.tmp.%s", devnm); |
e0d19036 | 1042 | if (mknod(devname, S_IFBLK | 0600, rdev) == -1) |
dd0781e5 NB |
1043 | if (errno != EEXIST) |
1044 | return NULL; | |
e0d19036 | 1045 | |
fc54fe7a JS |
1046 | if (stat(devname, &stb) == 0 && (S_IFMT&stb.st_mode) == S_IFBLK && |
1047 | (stb.st_rdev == rdev)) | |
e0d19036 NB |
1048 | return devname; |
1049 | unlink(devname); | |
1050 | return NULL; | |
1051 | } | |
1052 | ||
1053 | void put_md_name(char *name) | |
1054 | { | |
ca3b6696 | 1055 | if (strncmp(name, "/dev/.tmp.md", 12) == 0) |
e0d19036 NB |
1056 | unlink(name); |
1057 | } | |
f9ce90ba | 1058 | |
cc742d38 N |
1059 | int get_maj_min(char *dev, int *major, int *minor) |
1060 | { | |
1061 | char *e; | |
1062 | *major = strtoul(dev, &e, 0); | |
1063 | return (e > dev && *e == ':' && e[1] && | |
1064 | (*minor = strtoul(e+1, &e, 0)) >= 0 && | |
1065 | *e == 0); | |
1066 | } | |
1067 | ||
8b0dabea NB |
1068 | int dev_open(char *dev, int flags) |
1069 | { | |
1070 | /* like 'open', but if 'dev' matches %d:%d, create a temp | |
1071 | * block device and open that | |
1072 | */ | |
8b0dabea NB |
1073 | int fd = -1; |
1074 | char devname[32]; | |
e81cdd9f | 1075 | int major; |
8b0dabea | 1076 | int minor; |
e81cdd9f | 1077 | |
9f0ad56b JS |
1078 | if (!dev) |
1079 | return -1; | |
6df6a774 | 1080 | flags |= O_DIRECT; |
e81cdd9f | 1081 | |
cc742d38 | 1082 | if (get_maj_min(dev, &major, &minor)) { |
9dc7d357 N |
1083 | snprintf(devname, sizeof(devname), "/dev/.tmp.md.%d:%d:%d", |
1084 | (int)getpid(), major, minor); | |
1085 | if (mknod(devname, S_IFBLK|0600, makedev(major, minor)) == 0) { | |
1086 | fd = open(devname, flags); | |
1087 | unlink(devname); | |
6df6a774 N |
1088 | } |
1089 | if (fd < 0) { | |
9dc7d357 | 1090 | /* Try /tmp as /dev appear to be read-only */ |
b7a462e5 JS |
1091 | snprintf(devname, sizeof(devname), |
1092 | "/tmp/.tmp.md.%d:%d:%d", | |
6df6a774 | 1093 | (int)getpid(), major, minor); |
b7a462e5 JS |
1094 | if (mknod(devname, S_IFBLK|0600, |
1095 | makedev(major, minor)) == 0) { | |
6df6a774 N |
1096 | fd = open(devname, flags); |
1097 | unlink(devname); | |
1098 | } | |
8b0dabea NB |
1099 | } |
1100 | } else | |
6df6a774 | 1101 | fd = open(dev, flags); |
8b0dabea NB |
1102 | return fd; |
1103 | } | |
f9ce90ba | 1104 | |
4dd2df09 | 1105 | int open_dev_flags(char *devnm, int flags) |
e8a70c89 | 1106 | { |
13db17bd | 1107 | dev_t devid; |
e8a70c89 N |
1108 | char buf[20]; |
1109 | ||
4dd2df09 N |
1110 | devid = devnm2devid(devnm); |
1111 | sprintf(buf, "%d:%d", major(devid), minor(devid)); | |
d998b738 N |
1112 | return dev_open(buf, flags); |
1113 | } | |
1114 | ||
4dd2df09 | 1115 | int open_dev(char *devnm) |
d998b738 | 1116 | { |
4dd2df09 | 1117 | return open_dev_flags(devnm, O_RDONLY); |
e8a70c89 N |
1118 | } |
1119 | ||
4dd2df09 | 1120 | int open_dev_excl(char *devnm) |
a931db9e NB |
1121 | { |
1122 | char buf[20]; | |
1123 | int i; | |
7187750e | 1124 | int flags = O_RDWR; |
13db17bd | 1125 | dev_t devid = devnm2devid(devnm); |
399e0b97 | 1126 | long delay = 1000; |
a931db9e | 1127 | |
4dd2df09 | 1128 | sprintf(buf, "%d:%d", major(devid), minor(devid)); |
efa29530 | 1129 | for (i = 0; i < 25; i++) { |
7187750e | 1130 | int fd = dev_open(buf, flags|O_EXCL); |
a931db9e NB |
1131 | if (fd >= 0) |
1132 | return fd; | |
7187750e N |
1133 | if (errno == EACCES && flags == O_RDWR) { |
1134 | flags = O_RDONLY; | |
1135 | continue; | |
1136 | } | |
a931db9e NB |
1137 | if (errno != EBUSY) |
1138 | return fd; | |
399e0b97 N |
1139 | usleep(delay); |
1140 | if (delay < 200000) | |
1141 | delay *= 2; | |
a931db9e NB |
1142 | } |
1143 | return -1; | |
1144 | } | |
1145 | ||
9008ed1c N |
1146 | int same_dev(char *one, char *two) |
1147 | { | |
1148 | struct stat st1, st2; | |
1149 | if (stat(one, &st1) != 0) | |
1150 | return 0; | |
1151 | if (stat(two, &st2) != 0) | |
1152 | return 0; | |
1153 | if ((st1.st_mode & S_IFMT) != S_IFBLK) | |
1154 | return 0; | |
1155 | if ((st2.st_mode & S_IFMT) != S_IFBLK) | |
1156 | return 0; | |
1157 | return st1.st_rdev == st2.st_rdev; | |
1158 | } | |
1159 | ||
a7c6e3fb | 1160 | void wait_for(char *dev, int fd) |
a714580e N |
1161 | { |
1162 | int i; | |
a7c6e3fb | 1163 | struct stat stb_want; |
399e0b97 | 1164 | long delay = 1000; |
a7c6e3fb N |
1165 | |
1166 | if (fstat(fd, &stb_want) != 0 || | |
1167 | (stb_want.st_mode & S_IFMT) != S_IFBLK) | |
1168 | return; | |
a714580e | 1169 | |
efa29530 | 1170 | for (i = 0; i < 25; i++) { |
a714580e | 1171 | struct stat stb; |
a7c6e3fb N |
1172 | if (stat(dev, &stb) == 0 && |
1173 | (stb.st_mode & S_IFMT) == S_IFBLK && | |
1174 | (stb.st_rdev == stb_want.st_rdev)) | |
a714580e | 1175 | return; |
399e0b97 N |
1176 | usleep(delay); |
1177 | if (delay < 200000) | |
1178 | delay *= 2; | |
a714580e | 1179 | } |
436305c6 | 1180 | if (i == 25) |
9b8fea91 | 1181 | pr_err("timeout waiting for %s\n", dev); |
a714580e N |
1182 | } |
1183 | ||
0f22b998 N |
1184 | struct superswitch *superlist[] = |
1185 | { | |
1186 | &super0, &super1, | |
1187 | &super_ddf, &super_imsm, | |
0592faeb | 1188 | &mbr, &gpt, |
32141c17 JS |
1189 | NULL |
1190 | }; | |
f7dd881f | 1191 | |
4725bc31 | 1192 | struct supertype *super_by_fd(int fd, char **subarrayp) |
f9ce90ba | 1193 | { |
1686dc25 NB |
1194 | mdu_array_info_t array; |
1195 | int vers; | |
1196 | int minor; | |
1197 | struct supertype *st = NULL; | |
7e0f6979 | 1198 | struct mdinfo *sra; |
142cb9e1 | 1199 | char *verstr; |
1686dc25 NB |
1200 | char version[20]; |
1201 | int i; | |
f7e7067b | 1202 | char *subarray = NULL; |
4dd2df09 | 1203 | char container[32] = ""; |
1686dc25 | 1204 | |
4dd2df09 | 1205 | sra = sysfs_read(fd, NULL, GET_VERSION); |
1686dc25 NB |
1206 | |
1207 | if (sra) { | |
7e0f6979 NB |
1208 | vers = sra->array.major_version; |
1209 | minor = sra->array.minor_version; | |
142cb9e1 | 1210 | verstr = sra->text_version; |
1686dc25 | 1211 | } else { |
9cd39f01 | 1212 | if (md_get_array_info(fd, &array)) |
1686dc25 NB |
1213 | array.major_version = array.minor_version = 0; |
1214 | vers = array.major_version; | |
1215 | minor = array.minor_version; | |
142cb9e1 | 1216 | verstr = ""; |
6fbba4c9 | 1217 | } |
82d9eba6 | 1218 | |
1686dc25 NB |
1219 | if (vers != -1) { |
1220 | sprintf(version, "%d.%d", vers, minor); | |
1221 | verstr = version; | |
6fbba4c9 | 1222 | } |
3c558363 | 1223 | if (minor == -2 && is_subarray(verstr)) { |
f7e7067b | 1224 | char *dev = verstr+1; |
5f7e44b2 | 1225 | |
f7e7067b | 1226 | subarray = strchr(dev, '/'); |
92d49ecf | 1227 | if (subarray) { |
f7e7067b | 1228 | *subarray++ = '\0'; |
503975b9 | 1229 | subarray = xstrdup(subarray); |
92d49ecf | 1230 | } |
4dd2df09 | 1231 | strcpy(container, dev); |
15d230f7 | 1232 | sysfs_free(sra); |
5f7e44b2 | 1233 | sra = sysfs_read(-1, container, GET_VERSION); |
603f24a0 N |
1234 | if (sra && sra->text_version[0]) |
1235 | verstr = sra->text_version; | |
1236 | else | |
1237 | verstr = "-no-metadata-"; | |
f7e7067b NB |
1238 | } |
1239 | ||
efa29530 | 1240 | for (i = 0; st == NULL && superlist[i]; i++) |
f7e7067b | 1241 | st = superlist[i]->match_metadata_desc(verstr); |
1686dc25 | 1242 | |
15d230f7 | 1243 | sysfs_free(sra); |
f7e7067b | 1244 | if (st) { |
3b0896f8 | 1245 | st->sb = NULL; |
1f49fb3a N |
1246 | if (subarrayp) |
1247 | *subarrayp = subarray; | |
4dd2df09 N |
1248 | strcpy(st->container_devnm, container); |
1249 | strcpy(st->devnm, fd2devnm(fd)); | |
4725bc31 N |
1250 | } else |
1251 | free(subarray); | |
5f7e44b2 | 1252 | |
82d9eba6 | 1253 | return st; |
f9ce90ba | 1254 | } |
ea24acd0 | 1255 | |
0f0749ad | 1256 | int dev_size_from_id(dev_t id, unsigned long long *size) |
52d5d101 AC |
1257 | { |
1258 | char buf[20]; | |
1259 | int fd; | |
1260 | ||
1261 | sprintf(buf, "%d:%d", major(id), minor(id)); | |
1262 | fd = dev_open(buf, O_RDONLY); | |
1263 | if (fd < 0) | |
1264 | return 0; | |
1265 | if (get_dev_size(fd, NULL, size)) { | |
1266 | close(fd); | |
1267 | return 1; | |
1268 | } | |
1269 | close(fd); | |
1270 | return 0; | |
1271 | } | |
f9ce90ba | 1272 | |
4b57ecf6 AO |
1273 | int dev_sector_size_from_id(dev_t id, unsigned int *size) |
1274 | { | |
1275 | char buf[20]; | |
1276 | int fd; | |
1277 | ||
1278 | sprintf(buf, "%d:%d", major(id), minor(id)); | |
1279 | fd = dev_open(buf, O_RDONLY); | |
1280 | if (fd < 0) | |
1281 | return 0; | |
1282 | if (get_dev_sector_size(fd, NULL, size)) { | |
1283 | close(fd); | |
1284 | return 1; | |
1285 | } | |
1286 | close(fd); | |
1287 | return 0; | |
1288 | } | |
1289 | ||
159c3a1a | 1290 | struct supertype *dup_super(struct supertype *orig) |
3da92f27 | 1291 | { |
159c3a1a | 1292 | struct supertype *st; |
1686dc25 | 1293 | |
d2ca6449 NB |
1294 | if (!orig) |
1295 | return orig; | |
503975b9 | 1296 | st = xcalloc(1, sizeof(*st)); |
159c3a1a NB |
1297 | st->ss = orig->ss; |
1298 | st->max_devs = orig->max_devs; | |
1299 | st->minor_version = orig->minor_version; | |
cb8f6859 | 1300 | st->ignore_hw_compat = orig->ignore_hw_compat; |
23bf42cc | 1301 | st->data_offset = orig->data_offset; |
159c3a1a NB |
1302 | st->sb = NULL; |
1303 | st->info = NULL; | |
1304 | return st; | |
3da92f27 NB |
1305 | } |
1306 | ||
54887ad8 | 1307 | struct supertype *guess_super_type(int fd, enum guess_types guess_type) |
f9ce90ba NB |
1308 | { |
1309 | /* try each load_super to find the best match, | |
1310 | * and return the best superswitch | |
1311 | */ | |
82d9eba6 NB |
1312 | struct superswitch *ss; |
1313 | struct supertype *st; | |
1d13b599 | 1314 | unsigned int besttime = 0; |
570c0542 | 1315 | int bestsuper = -1; |
f9ce90ba NB |
1316 | int i; |
1317 | ||
503975b9 | 1318 | st = xcalloc(1, sizeof(*st)); |
4dd2df09 | 1319 | st->container_devnm[0] = 0; |
d1d599ea | 1320 | |
efa29530 | 1321 | for (i = 0; superlist[i]; i++) { |
f9ce90ba NB |
1322 | int rv; |
1323 | ss = superlist[i]; | |
54887ad8 N |
1324 | if (guess_type == guess_array && ss->add_to_super == NULL) |
1325 | continue; | |
1326 | if (guess_type == guess_partitions && ss->add_to_super != NULL) | |
1327 | continue; | |
ef609477 | 1328 | memset(st, 0, sizeof(*st)); |
df3346e6 | 1329 | st->ignore_hw_compat = 1; |
3da92f27 | 1330 | rv = ss->load_super(st, fd, NULL); |
570c0542 NB |
1331 | if (rv == 0) { |
1332 | struct mdinfo info; | |
a5d85af7 | 1333 | st->ss->getinfo_super(st, &info, NULL); |
570c0542 NB |
1334 | if (bestsuper == -1 || |
1335 | besttime < info.array.ctime) { | |
1336 | bestsuper = i; | |
1337 | besttime = info.array.ctime; | |
570c0542 | 1338 | } |
3da92f27 | 1339 | ss->free_super(st); |
570c0542 NB |
1340 | } |
1341 | } | |
1342 | if (bestsuper != -1) { | |
1343 | int rv; | |
ef609477 | 1344 | memset(st, 0, sizeof(*st)); |
df3346e6 | 1345 | st->ignore_hw_compat = 1; |
3da92f27 | 1346 | rv = superlist[bestsuper]->load_super(st, fd, NULL); |
f9ce90ba | 1347 | if (rv == 0) { |
5e747af2 | 1348 | superlist[bestsuper]->free_super(st); |
82d9eba6 | 1349 | return st; |
f9ce90ba NB |
1350 | } |
1351 | } | |
570c0542 | 1352 | free(st); |
f9ce90ba NB |
1353 | return NULL; |
1354 | } | |
fe6729fa | 1355 | |
beae1dfe NB |
1356 | /* Return size of device in bytes */ |
1357 | int get_dev_size(int fd, char *dname, unsigned long long *sizep) | |
1358 | { | |
1359 | unsigned long long ldsize; | |
c2c9bb6f NB |
1360 | struct stat st; |
1361 | ||
1362 | if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) | |
1363 | ldsize = (unsigned long long)st.st_size; | |
1364 | else | |
beae1dfe NB |
1365 | #ifdef BLKGETSIZE64 |
1366 | if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0) | |
1367 | #endif | |
1368 | { | |
1369 | unsigned long dsize; | |
1370 | if (ioctl(fd, BLKGETSIZE, &dsize) == 0) { | |
1371 | ldsize = dsize; | |
1372 | ldsize <<= 9; | |
1373 | } else { | |
1374 | if (dname) | |
32971509 | 1375 | pr_err("Cannot get size of %s: %s\n", |
beae1dfe NB |
1376 | dname, strerror(errno)); |
1377 | return 0; | |
1378 | } | |
1379 | } | |
1380 | *sizep = ldsize; | |
1381 | return 1; | |
1382 | } | |
8fac0577 | 1383 | |
32971509 PB |
1384 | /* Return sector size of device in bytes */ |
1385 | int get_dev_sector_size(int fd, char *dname, unsigned int *sectsizep) | |
1386 | { | |
1387 | unsigned int sectsize; | |
1388 | ||
1389 | if (ioctl(fd, BLKSSZGET, §size) != 0) { | |
1390 | if (dname) | |
1391 | pr_err("Cannot get sector size of %s: %s\n", | |
1392 | dname, strerror(errno)); | |
1393 | return 0; | |
1394 | } | |
1395 | ||
1396 | *sectsizep = sectsize; | |
1397 | return 1; | |
1398 | } | |
1399 | ||
3a371610 N |
1400 | /* Return true if this can only be a container, not a member device. |
1401 | * i.e. is and md device and size is zero | |
1402 | */ | |
1403 | int must_be_container(int fd) | |
1404 | { | |
f5c924f4 | 1405 | struct mdinfo *mdi; |
3a371610 | 1406 | unsigned long long size; |
f5c924f4 JS |
1407 | |
1408 | mdi = sysfs_read(fd, NULL, GET_VERSION); | |
1409 | if (!mdi) | |
3a371610 | 1410 | return 0; |
f5c924f4 JS |
1411 | sysfs_free(mdi); |
1412 | ||
3a371610 N |
1413 | if (get_dev_size(fd, NULL, &size) == 0) |
1414 | return 1; | |
1415 | if (size == 0) | |
1416 | return 1; | |
1417 | return 0; | |
1418 | } | |
034b203a TM |
1419 | |
1420 | /* Sets endofpart parameter to the last block used by the last GPT partition on the device. | |
1421 | * Returns: 1 if successful | |
1422 | * -1 for unknown partition type | |
1423 | * 0 for other errors | |
1424 | */ | |
1425 | static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart) | |
1426 | { | |
056b331e | 1427 | struct GPT gpt; |
034b203a | 1428 | unsigned char empty_gpt_entry[16]= {0}; |
db7fdfe4 JS |
1429 | struct GPT_part_entry *part; |
1430 | char buf[512]; | |
034b203a TM |
1431 | unsigned long long curr_part_end; |
1432 | unsigned all_partitions, entry_size; | |
f21e18ca | 1433 | unsigned part_nr; |
41b06495 | 1434 | unsigned int sector_size = 0; |
034b203a TM |
1435 | |
1436 | *endofpart = 0; | |
1437 | ||
056b331e | 1438 | BUILD_BUG_ON(sizeof(gpt) != 512); |
73e658d8 | 1439 | /* skip protective MBR */ |
41b06495 MD |
1440 | if (!get_dev_sector_size(fd, NULL, §or_size)) |
1441 | return 0; | |
1442 | lseek(fd, sector_size, SEEK_SET); | |
73e658d8 | 1443 | /* read GPT header */ |
056b331e | 1444 | if (read(fd, &gpt, 512) != 512) |
034b203a TM |
1445 | return 0; |
1446 | ||
1447 | /* get the number of partition entries and the entry size */ | |
056b331e N |
1448 | all_partitions = __le32_to_cpu(gpt.part_cnt); |
1449 | entry_size = __le32_to_cpu(gpt.part_size); | |
034b203a TM |
1450 | |
1451 | /* Check GPT signature*/ | |
056b331e | 1452 | if (gpt.magic != GPT_SIGNATURE_MAGIC) |
034b203a TM |
1453 | return -1; |
1454 | ||
1455 | /* sanity checks */ | |
1456 | if (all_partitions > 1024 || | |
db7fdfe4 | 1457 | entry_size > sizeof(buf)) |
034b203a TM |
1458 | return -1; |
1459 | ||
db7fdfe4 JS |
1460 | part = (struct GPT_part_entry *)buf; |
1461 | ||
41b06495 MD |
1462 | /* set offset to third block (GPT entries) */ |
1463 | lseek(fd, sector_size*2, SEEK_SET); | |
ca3b6696 | 1464 | for (part_nr = 0; part_nr < all_partitions; part_nr++) { |
73e658d8 | 1465 | /* read partition entry */ |
db7fdfe4 | 1466 | if (read(fd, buf, entry_size) != (ssize_t)entry_size) |
73e658d8 LB |
1467 | return 0; |
1468 | ||
034b203a | 1469 | /* is this valid partition? */ |
db7fdfe4 | 1470 | if (memcmp(part->type_guid, empty_gpt_entry, 16) != 0) { |
034b203a | 1471 | /* check the last lba for the current partition */ |
db7fdfe4 | 1472 | curr_part_end = __le64_to_cpu(part->ending_lba); |
034b203a TM |
1473 | if (curr_part_end > *endofpart) |
1474 | *endofpart = curr_part_end; | |
1475 | } | |
1476 | ||
034b203a TM |
1477 | } |
1478 | return 1; | |
1479 | } | |
1480 | ||
1481 | /* Sets endofpart parameter to the last block used by the last partition on the device. | |
1482 | * Returns: 1 if successful | |
1483 | * -1 for unknown partition type | |
1484 | * 0 for other errors | |
1485 | */ | |
1486 | static int get_last_partition_end(int fd, unsigned long long *endofpart) | |
1487 | { | |
056b331e | 1488 | struct MBR boot_sect; |
034b203a | 1489 | unsigned long long curr_part_end; |
f21e18ca | 1490 | unsigned part_nr; |
31208db9 | 1491 | unsigned int sector_size; |
034b203a TM |
1492 | int retval = 0; |
1493 | ||
1494 | *endofpart = 0; | |
1495 | ||
056b331e | 1496 | BUILD_BUG_ON(sizeof(boot_sect) != 512); |
034b203a TM |
1497 | /* read MBR */ |
1498 | lseek(fd, 0, 0); | |
056b331e | 1499 | if (read(fd, &boot_sect, 512) != 512) |
034b203a TM |
1500 | goto abort; |
1501 | ||
1502 | /* check MBP signature */ | |
056b331e | 1503 | if (boot_sect.magic == MBR_SIGNATURE_MAGIC) { |
034b203a TM |
1504 | retval = 1; |
1505 | /* found the correct signature */ | |
034b203a | 1506 | |
ca3b6696 | 1507 | for (part_nr = 0; part_nr < MBR_PARTITIONS; part_nr++) { |
8e2bca51 JC |
1508 | /* |
1509 | * Have to make every access through boot_sect rather | |
1510 | * than using a pointer to the partition table (or an | |
1511 | * entry), since the entries are not properly aligned. | |
1512 | */ | |
1513 | ||
034b203a | 1514 | /* check for GPT type */ |
8e2bca51 JC |
1515 | if (boot_sect.parts[part_nr].part_type == |
1516 | MBR_GPT_PARTITION_TYPE) { | |
034b203a TM |
1517 | retval = get_gpt_last_partition_end(fd, endofpart); |
1518 | break; | |
1519 | } | |
1520 | /* check the last used lba for the current partition */ | |
8e2bca51 JC |
1521 | curr_part_end = |
1522 | __le32_to_cpu(boot_sect.parts[part_nr].first_sect_lba) + | |
1523 | __le32_to_cpu(boot_sect.parts[part_nr].blocks_num); | |
034b203a TM |
1524 | if (curr_part_end > *endofpart) |
1525 | *endofpart = curr_part_end; | |
034b203a TM |
1526 | } |
1527 | } else { | |
1528 | /* Unknown partition table */ | |
1529 | retval = -1; | |
1530 | } | |
31208db9 MD |
1531 | /* calculate number of 512-byte blocks */ |
1532 | if (get_dev_sector_size(fd, NULL, §or_size)) | |
1533 | *endofpart *= (sector_size / 512); | |
034b203a TM |
1534 | abort: |
1535 | return retval; | |
1536 | } | |
1537 | ||
53ed6ac3 KW |
1538 | int check_partitions(int fd, char *dname, unsigned long long freesize, |
1539 | unsigned long long size) | |
034b203a TM |
1540 | { |
1541 | /* | |
1542 | * Check where the last partition ends | |
1543 | */ | |
1544 | unsigned long long endofpart; | |
034b203a | 1545 | |
31208db9 | 1546 | if (get_last_partition_end(fd, &endofpart) > 0) { |
034b203a TM |
1547 | /* There appears to be a partition table here */ |
1548 | if (freesize == 0) { | |
1549 | /* partitions will not be visible in new device */ | |
e7b84f9d N |
1550 | pr_err("partition table exists on %s but will be lost or\n" |
1551 | " meaningless after creating array\n", | |
1552 | dname); | |
034b203a TM |
1553 | return 1; |
1554 | } else if (endofpart > freesize) { | |
1555 | /* last partition overlaps metadata */ | |
e7b84f9d N |
1556 | pr_err("metadata will over-write last partition on %s.\n", |
1557 | dname); | |
034b203a | 1558 | return 1; |
53ed6ac3 KW |
1559 | } else if (size && endofpart > size) { |
1560 | /* partitions will be truncated in new device */ | |
e7b84f9d N |
1561 | pr_err("array size is too small to cover all partitions on %s.\n", |
1562 | dname); | |
53ed6ac3 | 1563 | return 1; |
034b203a TM |
1564 | } |
1565 | } | |
1566 | return 0; | |
1567 | } | |
1568 | ||
a322f70c DW |
1569 | int open_container(int fd) |
1570 | { | |
1571 | /* 'fd' is a block device. Find out if it is in use | |
1572 | * by a container, and return an open fd on that container. | |
1573 | */ | |
1574 | char path[256]; | |
1575 | char *e; | |
1576 | DIR *dir; | |
1577 | struct dirent *de; | |
1578 | int dfd, n; | |
1579 | char buf[200]; | |
1580 | int major, minor; | |
1581 | struct stat st; | |
1582 | ||
1583 | if (fstat(fd, &st) != 0) | |
1584 | return -1; | |
1585 | sprintf(path, "/sys/dev/block/%d:%d/holders", | |
1586 | (int)major(st.st_rdev), (int)minor(st.st_rdev)); | |
1587 | e = path + strlen(path); | |
1588 | ||
1589 | dir = opendir(path); | |
1590 | if (!dir) | |
1591 | return -1; | |
1592 | while ((de = readdir(dir))) { | |
1593 | if (de->d_ino == 0) | |
1594 | continue; | |
1595 | if (de->d_name[0] == '.') | |
1596 | continue; | |
a994592d N |
1597 | /* Need to make sure it is a container and not a volume */ |
1598 | sprintf(e, "/%s/md/metadata_version", de->d_name); | |
1599 | dfd = open(path, O_RDONLY); | |
1600 | if (dfd < 0) | |
1601 | continue; | |
1602 | n = read(dfd, buf, sizeof(buf)); | |
1603 | close(dfd); | |
1604 | if (n <= 0 || (unsigned)n >= sizeof(buf)) | |
1605 | continue; | |
1606 | buf[n] = 0; | |
1607 | if (strncmp(buf, "external", 8) != 0 || | |
1608 | n < 10 || | |
1609 | buf[9] == '/') | |
1610 | continue; | |
a322f70c DW |
1611 | sprintf(e, "/%s/dev", de->d_name); |
1612 | dfd = open(path, O_RDONLY); | |
1613 | if (dfd < 0) | |
1614 | continue; | |
1615 | n = read(dfd, buf, sizeof(buf)); | |
1616 | close(dfd); | |
f21e18ca | 1617 | if (n <= 0 || (unsigned)n >= sizeof(buf)) |
a322f70c DW |
1618 | continue; |
1619 | buf[n] = 0; | |
1620 | if (sscanf(buf, "%d:%d", &major, &minor) != 2) | |
1621 | continue; | |
1622 | sprintf(buf, "%d:%d", major, minor); | |
1623 | dfd = dev_open(buf, O_RDONLY); | |
1624 | if (dfd >= 0) { | |
1625 | closedir(dir); | |
1626 | return dfd; | |
1627 | } | |
1628 | } | |
355726fa | 1629 | closedir(dir); |
a322f70c DW |
1630 | return -1; |
1631 | } | |
1632 | ||
33414a01 DW |
1633 | struct superswitch *version_to_superswitch(char *vers) |
1634 | { | |
1635 | int i; | |
1636 | ||
1637 | for (i = 0; superlist[i]; i++) { | |
1638 | struct superswitch *ss = superlist[i]; | |
1639 | ||
1640 | if (strcmp(vers, ss->name) == 0) | |
1641 | return ss; | |
1642 | } | |
1643 | ||
1644 | return NULL; | |
1645 | } | |
1646 | ||
4dd2df09 N |
1647 | int metadata_container_matches(char *metadata, char *devnm) |
1648 | { | |
1649 | /* Check if 'devnm' is the container named in 'metadata' | |
1650 | * which is | |
1651 | * /containername/componentname or | |
1652 | * -containername/componentname | |
1653 | */ | |
1654 | int l; | |
1655 | if (*metadata != '/' && *metadata != '-') | |
1656 | return 0; | |
1657 | l = strlen(devnm); | |
1658 | if (strncmp(metadata+1, devnm, l) != 0) | |
1659 | return 0; | |
1660 | if (metadata[l+1] != '/') | |
1661 | return 0; | |
1662 | return 1; | |
1663 | } | |
1664 | ||
1665 | int metadata_subdev_matches(char *metadata, char *devnm) | |
1666 | { | |
1667 | /* Check if 'devnm' is the subdev named in 'metadata' | |
1668 | * which is | |
1669 | * /containername/subdev or | |
1670 | * -containername/subdev | |
1671 | */ | |
1672 | char *sl; | |
1673 | if (*metadata != '/' && *metadata != '-') | |
1674 | return 0; | |
1675 | sl = strchr(metadata+1, '/'); | |
1676 | if (!sl) | |
1677 | return 0; | |
1678 | if (strcmp(sl+1, devnm) == 0) | |
1679 | return 1; | |
1680 | return 0; | |
1681 | } | |
1682 | ||
33414a01 DW |
1683 | int is_container_member(struct mdstat_ent *mdstat, char *container) |
1684 | { | |
1685 | if (mdstat->metadata_version == NULL || | |
1686 | strncmp(mdstat->metadata_version, "external:", 9) != 0 || | |
4dd2df09 | 1687 | !metadata_container_matches(mdstat->metadata_version+9, container)) |
33414a01 DW |
1688 | return 0; |
1689 | ||
1690 | return 1; | |
1691 | } | |
1692 | ||
1693 | int is_subarray_active(char *subarray, char *container) | |
1694 | { | |
1695 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
1696 | struct mdstat_ent *ent; | |
1697 | ||
e5408a32 DW |
1698 | for (ent = mdstat; ent; ent = ent->next) |
1699 | if (is_container_member(ent, container)) | |
e5e5d7ce | 1700 | if (strcmp(to_subarray(ent, container), subarray) == 0) |
33414a01 | 1701 | break; |
33414a01 DW |
1702 | |
1703 | free_mdstat(mdstat); | |
1704 | ||
1705 | return ent != NULL; | |
1706 | } | |
1707 | ||
1708 | /* open_subarray - opens a subarray in a container | |
1709 | * @dev: container device name | |
feab51f8 | 1710 | * @st: empty supertype |
33414a01 DW |
1711 | * @quiet: block reporting errors flag |
1712 | * | |
1713 | * On success returns an fd to a container and fills in *st | |
1714 | */ | |
feab51f8 | 1715 | int open_subarray(char *dev, char *subarray, struct supertype *st, int quiet) |
33414a01 DW |
1716 | { |
1717 | struct mdinfo *mdi; | |
a951a4f7 | 1718 | struct mdinfo *info; |
33414a01 | 1719 | int fd, err = 1; |
4dd2df09 | 1720 | char *_devnm; |
33414a01 DW |
1721 | |
1722 | fd = open(dev, O_RDWR|O_EXCL); | |
1723 | if (fd < 0) { | |
1724 | if (!quiet) | |
e7b84f9d | 1725 | pr_err("Couldn't open %s, aborting\n", |
33414a01 | 1726 | dev); |
b990032d | 1727 | return -1; |
33414a01 DW |
1728 | } |
1729 | ||
4dd2df09 N |
1730 | _devnm = fd2devnm(fd); |
1731 | if (_devnm == NULL) { | |
33414a01 | 1732 | if (!quiet) |
e7b84f9d N |
1733 | pr_err("Failed to determine device number for %s\n", |
1734 | dev); | |
33414a01 DW |
1735 | goto close_fd; |
1736 | } | |
4dd2df09 | 1737 | strcpy(st->devnm, _devnm); |
33414a01 | 1738 | |
4dd2df09 | 1739 | mdi = sysfs_read(fd, st->devnm, GET_VERSION|GET_LEVEL); |
33414a01 DW |
1740 | if (!mdi) { |
1741 | if (!quiet) | |
e7b84f9d | 1742 | pr_err("Failed to read sysfs for %s\n", |
33414a01 DW |
1743 | dev); |
1744 | goto close_fd; | |
1745 | } | |
1746 | ||
1747 | if (mdi->array.level != UnSet) { | |
1748 | if (!quiet) | |
e7b84f9d | 1749 | pr_err("%s is not a container\n", dev); |
33414a01 DW |
1750 | goto free_sysfs; |
1751 | } | |
1752 | ||
1753 | st->ss = version_to_superswitch(mdi->text_version); | |
1754 | if (!st->ss) { | |
1755 | if (!quiet) | |
e7b84f9d N |
1756 | pr_err("Operation not supported for %s metadata\n", |
1757 | mdi->text_version); | |
33414a01 DW |
1758 | goto free_sysfs; |
1759 | } | |
1760 | ||
4dd2df09 | 1761 | if (st->devnm[0] == 0) { |
33414a01 | 1762 | if (!quiet) |
e7b84f9d | 1763 | pr_err("Failed to allocate device name\n"); |
33414a01 DW |
1764 | goto free_sysfs; |
1765 | } | |
1766 | ||
db20d413 | 1767 | if (!st->ss->load_container) { |
33414a01 | 1768 | if (!quiet) |
e7b84f9d | 1769 | pr_err("%s is not a container\n", dev); |
4dd2df09 | 1770 | goto free_sysfs; |
33414a01 DW |
1771 | } |
1772 | ||
db20d413 | 1773 | if (st->ss->load_container(st, fd, NULL)) { |
33414a01 | 1774 | if (!quiet) |
e7b84f9d | 1775 | pr_err("Failed to load metadata for %s\n", |
db20d413 | 1776 | dev); |
4dd2df09 | 1777 | goto free_sysfs; |
33414a01 DW |
1778 | } |
1779 | ||
a951a4f7 N |
1780 | info = st->ss->container_content(st, subarray); |
1781 | if (!info) { | |
1782 | if (!quiet) | |
e7b84f9d | 1783 | pr_err("Failed to find subarray-%s in %s\n", |
a951a4f7 N |
1784 | subarray, dev); |
1785 | goto free_super; | |
1786 | } | |
1787 | free(info); | |
1788 | ||
33414a01 DW |
1789 | err = 0; |
1790 | ||
1791 | free_super: | |
1792 | if (err) | |
1793 | st->ss->free_super(st); | |
33414a01 DW |
1794 | free_sysfs: |
1795 | sysfs_free(mdi); | |
1796 | close_fd: | |
1797 | if (err) | |
1798 | close(fd); | |
1799 | ||
1800 | if (err) | |
1801 | return -1; | |
1802 | else | |
1803 | return fd; | |
1804 | } | |
1805 | ||
7801ac20 N |
1806 | int add_disk(int mdfd, struct supertype *st, |
1807 | struct mdinfo *sra, struct mdinfo *info) | |
1808 | { | |
1809 | /* Add a device to an array, in one of 2 ways. */ | |
1810 | int rv; | |
32141c17 | 1811 | |
7801ac20 | 1812 | if (st->ss->external) { |
d23534e4 DW |
1813 | if (info->disk.state & (1<<MD_DISK_SYNC)) |
1814 | info->recovery_start = MaxSector; | |
1815 | else | |
1816 | info->recovery_start = 0; | |
2904b26f | 1817 | rv = sysfs_add_disk(sra, info, 0); |
7801ac20 N |
1818 | if (! rv) { |
1819 | struct mdinfo *sd2; | |
f35f2525 N |
1820 | for (sd2 = sra->devs; sd2; sd2=sd2->next) |
1821 | if (sd2 == info) | |
1822 | break; | |
1823 | if (sd2 == NULL) { | |
503975b9 | 1824 | sd2 = xmalloc(sizeof(*sd2)); |
f35f2525 N |
1825 | *sd2 = *info; |
1826 | sd2->next = sra->devs; | |
1827 | sra->devs = sd2; | |
1828 | } | |
7801ac20 N |
1829 | } |
1830 | } else | |
7801ac20 N |
1831 | rv = ioctl(mdfd, ADD_NEW_DISK, &info->disk); |
1832 | return rv; | |
1833 | } | |
1834 | ||
de6ae750 N |
1835 | int remove_disk(int mdfd, struct supertype *st, |
1836 | struct mdinfo *sra, struct mdinfo *info) | |
1837 | { | |
1838 | int rv; | |
32141c17 | 1839 | |
de6ae750 | 1840 | /* Remove the disk given by 'info' from the array */ |
de6ae750 N |
1841 | if (st->ss->external) |
1842 | rv = sysfs_set_str(sra, info, "slot", "none"); | |
1843 | else | |
de6ae750 N |
1844 | rv = ioctl(mdfd, HOT_REMOVE_DISK, makedev(info->disk.major, |
1845 | info->disk.minor)); | |
1846 | return rv; | |
1847 | } | |
1848 | ||
1ab9ed2a | 1849 | int hot_remove_disk(int mdfd, unsigned long dev, int force) |
2dd271fe | 1850 | { |
1ab9ed2a | 1851 | int cnt = force ? 500 : 5; |
2dd271fe N |
1852 | int ret; |
1853 | ||
1854 | /* HOT_REMOVE_DISK can fail with EBUSY if there are | |
1855 | * outstanding IO requests to the device. | |
1856 | * In this case, it can be helpful to wait a little while, | |
1ab9ed2a | 1857 | * up to 5 seconds if 'force' is set, or 50 msec if not. |
2dd271fe N |
1858 | */ |
1859 | while ((ret = ioctl(mdfd, HOT_REMOVE_DISK, dev)) == -1 && | |
1860 | errno == EBUSY && | |
1861 | cnt-- > 0) | |
1862 | usleep(10000); | |
1863 | ||
1864 | return ret; | |
1865 | } | |
1866 | ||
1ab9ed2a | 1867 | int sys_hot_remove_disk(int statefd, int force) |
fdd01569 | 1868 | { |
1ab9ed2a | 1869 | int cnt = force ? 500 : 5; |
fdd01569 N |
1870 | int ret; |
1871 | ||
1872 | while ((ret = write(statefd, "remove", 6)) == -1 && | |
1873 | errno == EBUSY && | |
1874 | cnt-- > 0) | |
1875 | usleep(10000); | |
1876 | return ret == 6 ? 0 : -1; | |
1877 | } | |
1878 | ||
f35f2525 N |
1879 | int set_array_info(int mdfd, struct supertype *st, struct mdinfo *info) |
1880 | { | |
1881 | /* Initialise kernel's knowledge of array. | |
1882 | * This varies between externally managed arrays | |
1883 | * and older kernels | |
1884 | */ | |
700483a2 | 1885 | mdu_array_info_t inf; |
f35f2525 N |
1886 | int rv; |
1887 | ||
f35f2525 | 1888 | if (st->ss->external) |
700483a2 | 1889 | return sysfs_set_array(info, 9003); |
700483a2 JS |
1890 | |
1891 | memset(&inf, 0, sizeof(inf)); | |
1892 | inf.major_version = info->array.major_version; | |
1893 | inf.minor_version = info->array.minor_version; | |
1894 | rv = md_set_array_info(mdfd, &inf); | |
1895 | ||
f35f2525 N |
1896 | return rv; |
1897 | } | |
1898 | ||
1e5c6983 DW |
1899 | unsigned long long min_recovery_start(struct mdinfo *array) |
1900 | { | |
1901 | /* find the minimum recovery_start in an array for metadata | |
1902 | * formats that only record per-array recovery progress instead | |
1903 | * of per-device | |
1904 | */ | |
1905 | unsigned long long recovery_start = MaxSector; | |
1906 | struct mdinfo *d; | |
1907 | ||
1908 | for (d = array->devs; d; d = d->next) | |
1909 | recovery_start = min(recovery_start, d->recovery_start); | |
1910 | ||
1911 | return recovery_start; | |
1912 | } | |
1913 | ||
4dd2df09 | 1914 | int mdmon_pid(char *devnm) |
a931db9e NB |
1915 | { |
1916 | char path[100]; | |
1917 | char pid[10]; | |
1918 | int fd; | |
1919 | int n; | |
10013317 | 1920 | |
4dd2df09 | 1921 | sprintf(path, "%s/%s.pid", MDMON_DIR, devnm); |
10013317 | 1922 | |
24f6f99b | 1923 | fd = open(path, O_RDONLY | O_NOATIME, 0); |
a931db9e NB |
1924 | |
1925 | if (fd < 0) | |
cf556303 | 1926 | return -1; |
a931db9e NB |
1927 | n = read(fd, pid, 9); |
1928 | close(fd); | |
1929 | if (n <= 0) | |
cf556303 | 1930 | return -1; |
24f6f99b | 1931 | return atoi(pid); |
a931db9e NB |
1932 | } |
1933 | ||
4dd2df09 | 1934 | int mdmon_running(char *devnm) |
a931db9e | 1935 | { |
4dd2df09 | 1936 | int pid = mdmon_pid(devnm); |
24f6f99b | 1937 | if (pid <= 0) |
a931db9e | 1938 | return 0; |
24f6f99b | 1939 | if (kill(pid, 0) == 0) |
a931db9e NB |
1940 | return 1; |
1941 | return 0; | |
1942 | } | |
1943 | ||
4dd2df09 | 1944 | int start_mdmon(char *devnm) |
8850ee3e | 1945 | { |
1913c325 | 1946 | int i, skipped; |
44d2e365 | 1947 | int len; |
ca3b6696 | 1948 | pid_t pid; |
9fe32043 | 1949 | int status; |
44d2e365 N |
1950 | char pathbuf[1024]; |
1951 | char *paths[4] = { | |
1952 | pathbuf, | |
85945e19 | 1953 | BINDIR "/mdmon", |
2f1bcf43 | 1954 | "./mdmon", |
44d2e365 N |
1955 | NULL |
1956 | }; | |
8850ee3e | 1957 | |
40ebbb9c | 1958 | if (check_env("MDADM_NO_MDMON")) |
8850ee3e N |
1959 | return 0; |
1960 | ||
9cf014ec | 1961 | len = readlink("/proc/self/exe", pathbuf, sizeof(pathbuf)-1); |
44d2e365 N |
1962 | if (len > 0) { |
1963 | char *sl; | |
1964 | pathbuf[len] = 0; | |
1965 | sl = strrchr(pathbuf, '/'); | |
1966 | if (sl) | |
1967 | sl++; | |
1968 | else | |
1969 | sl = pathbuf; | |
1970 | strcpy(sl, "mdmon"); | |
1971 | } else | |
1972 | pathbuf[0] = '\0'; | |
1973 | ||
0f7bdf89 | 1974 | /* First try to run systemctl */ |
9540cc24 N |
1975 | if (!check_env("MDADM_NO_SYSTEMCTL")) |
1976 | switch(fork()) { | |
1977 | case 0: | |
1978 | /* FIXME yuk. CLOSE_EXEC?? */ | |
1979 | skipped = 0; | |
1980 | for (i = 3; skipped < 20; i++) | |
1981 | if (close(i) < 0) | |
1982 | skipped++; | |
1983 | else | |
1984 | skipped = 0; | |
1985 | ||
1986 | /* Don't want to see error messages from | |
1987 | * systemctl. If the service doesn't exist, | |
1988 | * we start mdmon ourselves. | |
1989 | */ | |
1990 | close(2); | |
1991 | open("/dev/null", O_WRONLY); | |
1992 | snprintf(pathbuf, sizeof(pathbuf), "mdmon@%s.service", | |
1993 | devnm); | |
1994 | status = execl("/usr/bin/systemctl", "systemctl", | |
1995 | "start", | |
1996 | pathbuf, NULL); | |
1997 | status = execl("/bin/systemctl", "systemctl", "start", | |
1998 | pathbuf, NULL); | |
1999 | exit(1); | |
7a862a02 | 2000 | case -1: pr_err("cannot run mdmon. Array remains readonly\n"); |
9540cc24 N |
2001 | return -1; |
2002 | default: /* parent - good */ | |
2003 | pid = wait(&status); | |
2004 | if (pid >= 0 && status == 0) | |
2005 | return 0; | |
2006 | } | |
0f7bdf89 JS |
2007 | |
2008 | /* That failed, try running mdmon directly */ | |
8850ee3e N |
2009 | switch(fork()) { |
2010 | case 0: | |
2011 | /* FIXME yuk. CLOSE_EXEC?? */ | |
1913c325 | 2012 | skipped = 0; |
ca3b6696 | 2013 | for (i = 3; skipped < 20; i++) |
1913c325 N |
2014 | if (close(i) < 0) |
2015 | skipped++; | |
2016 | else | |
2017 | skipped = 0; | |
2018 | ||
ca3b6696 | 2019 | for (i = 0; paths[i]; i++) |
a0963a86 | 2020 | if (paths[i][0]) { |
2f1bcf43 | 2021 | execl(paths[i], paths[i], |
4dd2df09 | 2022 | devnm, NULL); |
a0963a86 | 2023 | } |
8850ee3e | 2024 | exit(1); |
7a862a02 | 2025 | case -1: pr_err("cannot run mdmon. Array remains readonly\n"); |
8850ee3e | 2026 | return -1; |
9fe32043 N |
2027 | default: /* parent - good */ |
2028 | pid = wait(&status); | |
15c10423 | 2029 | if (pid < 0 || status != 0) { |
7a862a02 | 2030 | pr_err("failed to launch mdmon. Array remains readonly\n"); |
9fe32043 | 2031 | return -1; |
15c10423 | 2032 | } |
8850ee3e N |
2033 | } |
2034 | return 0; | |
2035 | } | |
2036 | ||
148acb7b DW |
2037 | __u32 random32(void) |
2038 | { | |
2039 | __u32 rv; | |
2040 | int rfd = open("/dev/urandom", O_RDONLY); | |
2041 | if (rfd < 0 || read(rfd, &rv, 4) != 4) | |
2042 | rv = random(); | |
2043 | if (rfd >= 0) | |
2044 | close(rfd); | |
2045 | return rv; | |
2046 | } | |
2047 | ||
c5f71c24 JS |
2048 | void random_uuid(__u8 *buf) |
2049 | { | |
2050 | int fd, i, len; | |
2051 | __u32 r[4]; | |
2052 | ||
2053 | fd = open("/dev/urandom", O_RDONLY); | |
2054 | if (fd < 0) | |
2055 | goto use_random; | |
2056 | len = read(fd, buf, 16); | |
2057 | close(fd); | |
2058 | if (len != 16) | |
2059 | goto use_random; | |
2060 | ||
2061 | return; | |
2062 | ||
2063 | use_random: | |
2064 | for (i = 0; i < 4; i++) | |
2065 | r[i] = random(); | |
2066 | memcpy(buf, r, 16); | |
2067 | } | |
2068 | ||
edd8d13c NB |
2069 | int flush_metadata_updates(struct supertype *st) |
2070 | { | |
2071 | int sfd; | |
2072 | if (!st->updates) { | |
2073 | st->update_tail = NULL; | |
2074 | return -1; | |
2075 | } | |
2076 | ||
4dd2df09 | 2077 | sfd = connect_monitor(st->container_devnm); |
edd8d13c NB |
2078 | if (sfd < 0) |
2079 | return -1; | |
2080 | ||
2081 | while (st->updates) { | |
2082 | struct metadata_update *mu = st->updates; | |
2083 | st->updates = mu->next; | |
2084 | ||
2085 | send_message(sfd, mu, 0); | |
2086 | wait_reply(sfd, 0); | |
2087 | free(mu->buf); | |
2088 | free(mu); | |
2089 | } | |
2090 | ack(sfd, 0); | |
2091 | wait_reply(sfd, 0); | |
2092 | close(sfd); | |
2093 | st->update_tail = NULL; | |
2094 | return 0; | |
2095 | } | |
2096 | ||
2097 | void append_metadata_update(struct supertype *st, void *buf, int len) | |
2098 | { | |
2099 | ||
503975b9 | 2100 | struct metadata_update *mu = xmalloc(sizeof(*mu)); |
edd8d13c NB |
2101 | |
2102 | mu->buf = buf; | |
2103 | mu->len = len; | |
2104 | mu->space = NULL; | |
cb23f1f4 | 2105 | mu->space_list = NULL; |
edd8d13c NB |
2106 | mu->next = NULL; |
2107 | *st->update_tail = mu; | |
2108 | st->update_tail = &mu->next; | |
2109 | } | |
a931db9e | 2110 | |
fe6729fa NB |
2111 | #ifdef __TINYC__ |
2112 | /* tinyc doesn't optimize this check in ioctl.h out ... */ | |
2113 | unsigned int __invalid_size_argument_for_IOC = 0; | |
2114 | #endif | |
2115 | ||
6d11ec6f AK |
2116 | int experimental(void) |
2117 | { | |
2118 | if (check_env("MDADM_EXPERIMENTAL")) | |
2119 | return 1; | |
2120 | else { | |
7a862a02 | 2121 | pr_err("To use this feature MDADM_EXPERIMENTAL environment variable has to be defined.\n"); |
6d11ec6f AK |
2122 | return 0; |
2123 | } | |
2124 | } | |
2125 | ||
326727d9 AC |
2126 | /* Pick all spares matching given criteria from a container |
2127 | * if min_size == 0 do not check size | |
2128 | * if domlist == NULL do not check domains | |
2129 | * if spare_group given add it to domains of each spare | |
2130 | * metadata allows to test domains using metadata of destination array */ | |
2131 | struct mdinfo *container_choose_spares(struct supertype *st, | |
fbfdcb06 | 2132 | struct spare_criteria *criteria, |
326727d9 AC |
2133 | struct domainlist *domlist, |
2134 | char *spare_group, | |
2135 | const char *metadata, int get_one) | |
2136 | { | |
2137 | struct mdinfo *d, **dp, *disks = NULL; | |
2138 | ||
2139 | /* get list of all disks in container */ | |
2140 | if (st->ss->getinfo_super_disks) | |
2141 | disks = st->ss->getinfo_super_disks(st); | |
2142 | ||
2143 | if (!disks) | |
2144 | return disks; | |
2145 | /* find spare devices on the list */ | |
2146 | dp = &disks->devs; | |
2147 | disks->array.spare_disks = 0; | |
2148 | while (*dp) { | |
2149 | int found = 0; | |
2150 | d = *dp; | |
2151 | if (d->disk.state == 0) { | |
2152 | /* check if size is acceptable */ | |
2153 | unsigned long long dev_size; | |
4b57ecf6 AO |
2154 | unsigned int dev_sector_size; |
2155 | int size_valid = 0; | |
2156 | int sector_size_valid = 0; | |
2157 | ||
326727d9 AC |
2158 | dev_t dev = makedev(d->disk.major,d->disk.minor); |
2159 | ||
fbfdcb06 | 2160 | if (!criteria->min_size || |
326727d9 | 2161 | (dev_size_from_id(dev, &dev_size) && |
fbfdcb06 | 2162 | dev_size >= criteria->min_size)) |
4b57ecf6 AO |
2163 | size_valid = 1; |
2164 | ||
2165 | if (!criteria->sector_size || | |
2166 | (dev_sector_size_from_id(dev, &dev_sector_size) && | |
2167 | criteria->sector_size == dev_sector_size)) | |
2168 | sector_size_valid = 1; | |
2169 | ||
2170 | found = size_valid && sector_size_valid; | |
2171 | ||
326727d9 AC |
2172 | /* check if domain matches */ |
2173 | if (found && domlist) { | |
4dd2df09 | 2174 | struct dev_policy *pol = devid_policy(dev); |
326727d9 AC |
2175 | if (spare_group) |
2176 | pol_add(&pol, pol_domain, | |
2177 | spare_group, NULL); | |
e5508b36 | 2178 | if (domain_test(domlist, pol, metadata) != 1) |
326727d9 AC |
2179 | found = 0; |
2180 | dev_policy_free(pol); | |
2181 | } | |
2182 | } | |
2183 | if (found) { | |
2184 | dp = &d->next; | |
2185 | disks->array.spare_disks++; | |
2186 | if (get_one) { | |
2187 | sysfs_free(*dp); | |
2188 | d->next = NULL; | |
2189 | } | |
2190 | } else { | |
2191 | *dp = d->next; | |
2192 | d->next = NULL; | |
2193 | sysfs_free(d); | |
2194 | } | |
2195 | } | |
2196 | return disks; | |
2197 | } | |
9eafa1de MN |
2198 | |
2199 | /* Checks if paths point to the same device | |
2200 | * Returns 0 if they do. | |
2201 | * Returns 1 if they don't. | |
2202 | * Returns -1 if something went wrong, | |
2203 | * e.g. paths are empty or the files | |
2204 | * they point to don't exist */ | |
2205 | int compare_paths (char* path1, char* path2) | |
2206 | { | |
2207 | struct stat st1,st2; | |
2208 | ||
2209 | if (path1 == NULL || path2 == NULL) | |
2210 | return -1; | |
2211 | if (stat(path1,&st1) != 0) | |
2212 | return -1; | |
2213 | if (stat(path2,&st2) != 0) | |
2214 | return -1; | |
2215 | if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) | |
2216 | return 0; | |
2217 | return 1; | |
2218 | } | |
a7dec3fd N |
2219 | |
2220 | /* Make sure we can open as many devices as needed */ | |
2221 | void enable_fds(int devices) | |
2222 | { | |
2223 | unsigned int fds = 20 + devices; | |
2224 | struct rlimit lim; | |
d7be7d87 | 2225 | if (getrlimit(RLIMIT_NOFILE, &lim) != 0 || lim.rlim_cur >= fds) |
a7dec3fd N |
2226 | return; |
2227 | if (lim.rlim_max < fds) | |
2228 | lim.rlim_max = fds; | |
2229 | lim.rlim_cur = fds; | |
2230 | setrlimit(RLIMIT_NOFILE, &lim); | |
2231 | } | |
a9c15847 N |
2232 | |
2233 | int in_initrd(void) | |
2234 | { | |
2235 | /* This is based on similar function in systemd. */ | |
2236 | struct statfs s; | |
76d0f188 JS |
2237 | /* statfs.f_type is signed long on s390x and MIPS, causing all |
2238 | sorts of sign extension problems with RAMFS_MAGIC being | |
2239 | defined as 0x858458f6 */ | |
a9c15847 | 2240 | return statfs("/", &s) >= 0 && |
7ac5d47e | 2241 | ((unsigned long)s.f_type == TMPFS_MAGIC || |
76d0f188 JS |
2242 | ((unsigned long)s.f_type & 0xFFFFFFFFUL) == |
2243 | ((unsigned long)RAMFS_MAGIC & 0xFFFFFFFFUL)); | |
a9c15847 | 2244 | } |
8832342d N |
2245 | |
2246 | void reopen_mddev(int mdfd) | |
2247 | { | |
2248 | /* Re-open without any O_EXCL, but keep | |
2249 | * the same fd | |
2250 | */ | |
2251 | char *devnm; | |
2252 | int fd; | |
2253 | devnm = fd2devnm(mdfd); | |
2254 | close(mdfd); | |
2255 | fd = open_dev(devnm); | |
2256 | if (fd >= 0 && fd != mdfd) | |
2257 | dup2(fd, mdfd); | |
2258 | } | |
7716570e | 2259 | |
e80357f8 GJ |
2260 | static struct cmap_hooks *cmap_hooks = NULL; |
2261 | static int is_cmap_hooks_ready = 0; | |
7716570e | 2262 | |
e80357f8 GJ |
2263 | void set_cmap_hooks(void) |
2264 | { | |
2265 | cmap_hooks = xmalloc(sizeof(struct cmap_hooks)); | |
2266 | cmap_hooks->cmap_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL); | |
2267 | if (!cmap_hooks->cmap_handle) | |
2268 | return; | |
7716570e | 2269 | |
b7a462e5 JS |
2270 | cmap_hooks->initialize = |
2271 | dlsym(cmap_hooks->cmap_handle, "cmap_initialize"); | |
2272 | cmap_hooks->get_string = | |
2273 | dlsym(cmap_hooks->cmap_handle, "cmap_get_string"); | |
e80357f8 | 2274 | cmap_hooks->finalize = dlsym(cmap_hooks->cmap_handle, "cmap_finalize"); |
7716570e | 2275 | |
e80357f8 GJ |
2276 | if (!cmap_hooks->initialize || !cmap_hooks->get_string || |
2277 | !cmap_hooks->finalize) | |
2278 | dlclose(cmap_hooks->cmap_handle); | |
2279 | else | |
2280 | is_cmap_hooks_ready = 1; | |
2281 | } | |
7716570e | 2282 | |
e80357f8 GJ |
2283 | int get_cluster_name(char **cluster_name) |
2284 | { | |
2285 | int rv = -1; | |
2286 | cmap_handle_t handle; | |
7716570e | 2287 | |
e80357f8 GJ |
2288 | if (!is_cmap_hooks_ready) |
2289 | return rv; | |
7716570e | 2290 | |
e80357f8 | 2291 | rv = cmap_hooks->initialize(&handle); |
7716570e GJ |
2292 | if (rv != CS_OK) |
2293 | goto out; | |
2294 | ||
e80357f8 | 2295 | rv = cmap_hooks->get_string(handle, "totem.cluster_name", cluster_name); |
7716570e GJ |
2296 | if (rv != CS_OK) { |
2297 | free(*cluster_name); | |
2298 | rv = -1; | |
2299 | goto name_err; | |
2300 | } | |
2301 | ||
2302 | rv = 0; | |
2303 | name_err: | |
e80357f8 | 2304 | cmap_hooks->finalize(handle); |
7716570e | 2305 | out: |
7716570e GJ |
2306 | return rv; |
2307 | } | |
d15a1f72 GJ |
2308 | |
2309 | void set_dlm_hooks(void) | |
2310 | { | |
2311 | dlm_hooks = xmalloc(sizeof(struct dlm_hooks)); | |
2312 | dlm_hooks->dlm_handle = dlopen("libdlm_lt.so.3", RTLD_NOW | RTLD_LOCAL); | |
2313 | if (!dlm_hooks->dlm_handle) | |
2314 | return; | |
2315 | ||
b7a462e5 JS |
2316 | dlm_hooks->create_lockspace = |
2317 | dlsym(dlm_hooks->dlm_handle, "dlm_create_lockspace"); | |
2318 | dlm_hooks->release_lockspace = | |
2319 | dlsym(dlm_hooks->dlm_handle, "dlm_release_lockspace"); | |
d15a1f72 GJ |
2320 | dlm_hooks->ls_lock = dlsym(dlm_hooks->dlm_handle, "dlm_ls_lock"); |
2321 | dlm_hooks->ls_unlock = dlsym(dlm_hooks->dlm_handle, "dlm_ls_unlock"); | |
2322 | dlm_hooks->ls_get_fd = dlsym(dlm_hooks->dlm_handle, "dlm_ls_get_fd"); | |
2323 | dlm_hooks->dispatch = dlsym(dlm_hooks->dlm_handle, "dlm_dispatch"); | |
2324 | ||
2325 | if (!dlm_hooks->create_lockspace || !dlm_hooks->ls_lock || | |
2326 | !dlm_hooks->ls_unlock || !dlm_hooks->release_lockspace || | |
2327 | !dlm_hooks->ls_get_fd || !dlm_hooks->dispatch) | |
2328 | dlclose(dlm_hooks->dlm_handle); | |
2329 | else | |
2330 | is_dlm_hooks_ready = 1; | |
2331 | } | |
e80357f8 GJ |
2332 | |
2333 | void set_hooks(void) | |
2334 | { | |
2335 | set_dlm_hooks(); | |
2336 | set_cmap_hooks(); | |
2337 | } | |
b2514242 PB |
2338 | |
2339 | int zero_disk_range(int fd, unsigned long long sector, size_t count) | |
2340 | { | |
2341 | int ret = 0; | |
2342 | int fd_zero; | |
2343 | void *addr = NULL; | |
2344 | size_t written = 0; | |
2345 | size_t len = count * 512; | |
2346 | ssize_t n; | |
2347 | ||
2348 | fd_zero = open("/dev/zero", O_RDONLY); | |
2349 | if (fd_zero < 0) { | |
2350 | pr_err("Cannot open /dev/zero\n"); | |
2351 | return -1; | |
2352 | } | |
2353 | ||
2354 | if (lseek64(fd, sector * 512, SEEK_SET) < 0) { | |
2355 | ret = -errno; | |
2356 | pr_err("Failed to seek offset for zeroing\n"); | |
2357 | goto out; | |
2358 | } | |
2359 | ||
2360 | addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd_zero, 0); | |
2361 | ||
2362 | if (addr == MAP_FAILED) { | |
2363 | ret = -errno; | |
2364 | pr_err("Mapping /dev/zero failed\n"); | |
2365 | goto out; | |
2366 | } | |
2367 | ||
2368 | do { | |
2369 | n = write(fd, addr + written, len - written); | |
2370 | if (n < 0) { | |
2371 | if (errno == EINTR) | |
2372 | continue; | |
2373 | ret = -errno; | |
2374 | pr_err("Zeroing disk range failed\n"); | |
2375 | break; | |
2376 | } | |
2377 | written += n; | |
2378 | } while (written != len); | |
2379 | ||
2380 | munmap(addr, len); | |
2381 | ||
2382 | out: | |
2383 | close(fd_zero); | |
2384 | return ret; | |
2385 | } |