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