]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
Add failfast support.
[thirdparty/mdadm.git] / mdopen.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
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
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "md_p.h"
27 #include <ctype.h>
28
29 void make_parts(char *dev, int cnt)
30 {
31 /* make 'cnt' partition devices for 'dev'
32 * If dev is a device name we use the
33 * major/minor from dev and add 1..cnt
34 * If it is a symlink, we make similar symlinks.
35 * If dev ends with a digit, we add "p%d" else "%d"
36 * If the name exists, we use it's owner/mode,
37 * else that of dev
38 */
39 struct stat stb;
40 int major_num;
41 int minor_num;
42 int odig;
43 int i;
44 int nlen = strlen(dev) + 20;
45 char *name;
46 int dig = isdigit(dev[strlen(dev)-1]);
47 char orig[1024];
48 char sym[1024];
49 int err;
50
51 if (cnt == 0)
52 cnt = 4;
53 if (lstat(dev, &stb)!= 0)
54 return;
55
56 if (S_ISBLK(stb.st_mode)) {
57 major_num = major(stb.st_rdev);
58 minor_num = minor(stb.st_rdev);
59 odig = -1;
60 } else if (S_ISLNK(stb.st_mode)) {
61 int len = readlink(dev, orig, sizeof(orig));
62 if (len < 0 || len > 1000)
63 return;
64 orig[len] = 0;
65 odig = isdigit(orig[len-1]);
66 major_num = -1;
67 minor_num = -1;
68 } else
69 return;
70 name = xmalloc(nlen);
71 for (i = 1; i <= cnt ; i++) {
72 struct stat stb2;
73 snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
74 if (stat(name, &stb2) == 0) {
75 if (!S_ISBLK(stb2.st_mode) || !S_ISBLK(stb.st_mode))
76 continue;
77 if (stb2.st_rdev == makedev(major_num, minor_num+i))
78 continue;
79 unlink(name);
80 } else {
81 stb2 = stb;
82 }
83 if (S_ISBLK(stb.st_mode)) {
84 if (mknod(name, S_IFBLK | 0600,
85 makedev(major_num, minor_num+i)))
86 perror("mknod");
87 if (chown(name, stb2.st_uid, stb2.st_gid))
88 perror("chown");
89 if (chmod(name, stb2.st_mode & 07777))
90 perror("chmod");
91 err = 0;
92 } else {
93 snprintf(sym, sizeof(sym), "%s%s%d", orig, odig?"p":"", i);
94 err = symlink(sym, name);
95 }
96
97 if (err == 0 && stat(name, &stb2) == 0)
98 add_dev(name, &stb2, 0, NULL);
99 }
100 free(name);
101 }
102
103 /*
104 * We need a new md device to assemble/build/create an array.
105 * 'dev' is a name given us by the user (command line or mdadm.conf)
106 * It might start with /dev or /dev/md any might end with a digit
107 * string.
108 * If it starts with just /dev, it must be /dev/mdX or /dev/md_dX
109 * If it ends with a digit string, then it must be as above, or
110 * 'trustworthy' must be 'METADATA' and the 'dev' must be
111 * /dev/md/'name'NN or 'name'NN
112 * If it doesn't end with a digit string, it must be /dev/md/'name'
113 * or 'name' or must be NULL.
114 * If the digit string is present, it gives the minor number to use
115 * If not, we choose a high, unused minor number.
116 * If the 'dev' is a standard name, it devices whether 'md' or 'mdp'.
117 * else if the name is 'd[0-9]+' then we use mdp
118 * else if trustworthy is 'METADATA' we use md
119 * else the choice depends on 'autof'.
120 * If name is NULL it is assumed to match whatever dev provides.
121 * If both name and dev are NULL, we choose a name 'mdXX' or 'mdpXX'
122 *
123 * If 'name' is given, and 'trustworthy' is 'foreign' and name is not
124 * supported by 'dev', we add a "_%d" suffix based on the minor number
125 * use that.
126 *
127 * If udev is configured, we create a temporary device, open it, and
128 * unlink it.
129 * If not, we create the /dev/mdXX device, and if name is usable,
130 * /dev/md/name
131 * In any case we return /dev/md/name or (if that isn't available)
132 * /dev/mdXX in 'chosen'.
133 *
134 * When we create devices, we use uid/gid/umask from config file.
135 */
136
137 int create_mddev(char *dev, char *name, int autof, int trustworthy,
138 char *chosen)
139 {
140 int mdfd;
141 struct stat stb;
142 int num = -1;
143 int use_mdp = -1;
144 struct createinfo *ci = conf_get_create_info();
145 int parts;
146 char *cname;
147 char devname[37];
148 char devnm[32];
149 char cbuf[400];
150 if (chosen == NULL)
151 chosen = cbuf;
152
153 if (autof == 0)
154 autof = ci->autof;
155
156 parts = autof >> 3;
157 autof &= 7;
158
159 strcpy(chosen, "/dev/md/");
160 cname = chosen + strlen(chosen);
161
162 if (dev) {
163 if (strncmp(dev, "/dev/md/", 8) == 0) {
164 strcpy(cname, dev+8);
165 } else if (strncmp(dev, "/dev/", 5) == 0) {
166 char *e = dev + strlen(dev);
167 while (e > dev && isdigit(e[-1]))
168 e--;
169 if (e[0])
170 num = strtoul(e, NULL, 10);
171 strcpy(cname, dev+5);
172 cname[e-(dev+5)] = 0;
173 /* name *must* be mdXX or md_dXX in this context */
174 if (num < 0 ||
175 (strcmp(cname, "md") != 0 && strcmp(cname, "md_d") != 0)) {
176 pr_err("%s is an invalid name for an md device. Try /dev/md/%s\n",
177 dev, dev+5);
178 return -1;
179 }
180 if (strcmp(cname, "md") == 0)
181 use_mdp = 0;
182 else
183 use_mdp = 1;
184 /* recreate name: /dev/md/0 or /dev/md/d0 */
185 sprintf(cname, "%s%d", use_mdp?"d":"", num);
186 } else
187 strcpy(cname, dev);
188
189 /* 'cname' must not contain a slash, and may not be
190 * empty.
191 */
192 if (strchr(cname, '/') != NULL) {
193 pr_err("%s is an invalid name for an md device.\n", dev);
194 return -1;
195 }
196 if (cname[0] == 0) {
197 pr_err("%s is an invalid name for an md device (empty!).", dev);
198 return -1;
199 }
200 if (num < 0) {
201 /* If cname is 'N' or 'dN', we get dev number
202 * from there.
203 */
204 char *sp = cname;
205 char *ep;
206 if (cname[0] == 'd')
207 sp++;
208 if (isdigit(sp[0]))
209 num = strtoul(sp, &ep, 10);
210 else
211 ep = sp;
212 if (ep == sp || *ep || num < 0)
213 num = -1;
214 else if (cname[0] == 'd')
215 use_mdp = 1;
216 else
217 use_mdp = 0;
218 }
219 }
220
221 /* Now determine device number */
222 /* named 'METADATA' cannot use 'mdp'. */
223 if (name && name[0] == 0)
224 name = NULL;
225 if (name && trustworthy == METADATA && use_mdp == 1) {
226 pr_err("%s is not allowed for a %s container. Consider /dev/md%d.\n", dev, name, num);
227 return -1;
228 }
229 if (name && trustworthy == METADATA)
230 use_mdp = 0;
231 if (use_mdp == -1) {
232 if (autof == 4 || autof == 6)
233 use_mdp = 1;
234 else
235 use_mdp = 0;
236 }
237 if (num < 0 && trustworthy == LOCAL && name) {
238 /* if name is numeric, possibly prefixed by
239 * 'md' or '/dev/md', use that for num
240 * if it is not already in use */
241 char *ep;
242 char *n2 = name;
243 if (strncmp(n2, "/dev/", 5) == 0)
244 n2 += 5;
245 if (strncmp(n2, "md", 2) == 0)
246 n2 += 2;
247 if (*n2 == '/')
248 n2++;
249 num = strtoul(n2, &ep, 10);
250 if (ep == n2 || *ep)
251 num = -1;
252 else {
253 sprintf(devnm, "md%s%d", use_mdp ? "_d":"", num);
254 if (mddev_busy(devnm))
255 num = -1;
256 }
257 }
258
259 if (cname[0] == 0 && name) {
260 /* Need to find a name if we can
261 * We don't completely trust 'name'. Truncate to
262 * reasonable length and remove '/'
263 */
264 char *cp;
265 struct map_ent *map = NULL;
266 int conflict = 1;
267 int unum = 0;
268 int cnlen;
269 strncpy(cname, name, 200);
270 cname[200] = 0;
271 for (cp = cname; *cp ; cp++)
272 switch (*cp) {
273 case '/':
274 *cp = '-';
275 break;
276 case ' ':
277 case '\t':
278 *cp = '_';
279 break;
280 }
281
282 if (trustworthy == LOCAL ||
283 (trustworthy == FOREIGN && strchr(cname, ':') != NULL)) {
284 /* Only need suffix if there is a conflict */
285 if (map_by_name(&map, cname) == NULL)
286 conflict = 0;
287 }
288 cnlen = strlen(cname);
289 while (conflict) {
290 if (trustworthy == METADATA && !isdigit(cname[cnlen-1]))
291 sprintf(cname+cnlen, "%d", unum);
292 else
293 /* add _%d to FOREIGN array that don't
294 * a 'host:' prefix
295 */
296 sprintf(cname+cnlen, "_%d", unum);
297 unum++;
298 if (map_by_name(&map, cname) == NULL)
299 conflict = 0;
300 }
301 }
302
303 devnm[0] = 0;
304 if (num < 0 && cname && ci->names) {
305 int fd;
306 int n = -1;
307 sprintf(devnm, "md_%s", cname);
308 fd = open("/sys/module/md_mod/parameters/new_array", O_WRONLY);
309 if (fd >= 0) {
310 n = write(fd, devnm, strlen(devnm));
311 close(fd);
312 }
313 if (n < 0)
314 devnm[0] = 0;
315 }
316 if (devnm[0])
317 ;
318 else if (num < 0) {
319 /* need to choose a free number. */
320 char *_devnm = find_free_devnm(use_mdp);
321 if (_devnm == NULL) {
322 pr_err("No avail md devices - aborting\n");
323 return -1;
324 }
325 strcpy(devnm, _devnm);
326 } else {
327 sprintf(devnm, "%s%d", use_mdp?"md_d":"md", num);
328 if (mddev_busy(devnm)) {
329 pr_err("%s is already in use.\n",
330 dev);
331 return -1;
332 }
333 }
334
335 sprintf(devname, "/dev/%s", devnm);
336
337 if (dev && dev[0] == '/')
338 strcpy(chosen, dev);
339 else if (cname[0] == 0)
340 strcpy(chosen, devname);
341
342 /* We have a device number and name.
343 * If we cannot detect udev, we need to make
344 * devices and links ourselves.
345 */
346 if (!use_udev()) {
347 /* Make sure 'devname' exists and 'chosen' is a symlink to it */
348 if (lstat(devname, &stb) == 0) {
349 /* Must be the correct device, else error */
350 if ((stb.st_mode&S_IFMT) != S_IFBLK ||
351 stb.st_rdev != devnm2devid(devnm)) {
352 pr_err("%s exists but looks wrong, please fix\n",
353 devname);
354 return -1;
355 }
356 } else {
357 if (mknod(devname, S_IFBLK|0600,
358 devnm2devid(devnm)) != 0) {
359 pr_err("failed to create %s\n",
360 devname);
361 return -1;
362 }
363 if (chown(devname, ci->uid, ci->gid))
364 perror("chown");
365 if (chmod(devname, ci->mode))
366 perror("chmod");
367 stat(devname, &stb);
368 add_dev(devname, &stb, 0, NULL);
369 }
370 if (use_mdp == 1)
371 make_parts(devname, parts);
372
373 if (strcmp(chosen, devname) != 0) {
374 if (mkdir("/dev/md",0700) == 0) {
375 if (chown("/dev/md", ci->uid, ci->gid))
376 perror("chown /dev/md");
377 if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111)))
378 perror("chmod /dev/md");
379 }
380
381 if (dev && strcmp(chosen, dev) == 0)
382 /* We know we are allowed to use this name */
383 unlink(chosen);
384
385 if (lstat(chosen, &stb) == 0) {
386 char buf[300];
387 ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1);
388 if (link_len >= 0)
389 buf[link_len] = '\0';
390
391 if ((stb.st_mode & S_IFMT) != S_IFLNK ||
392 link_len < 0 ||
393 strcmp(buf, devname) != 0) {
394 pr_err("%s exists - ignoring\n",
395 chosen);
396 strcpy(chosen, devname);
397 }
398 } else if (symlink(devname, chosen) != 0)
399 pr_err("failed to create %s: %s\n",
400 chosen, strerror(errno));
401 if (use_mdp && strcmp(chosen, devname) != 0)
402 make_parts(chosen, parts);
403 }
404 }
405 mdfd = open_dev_excl(devnm);
406 if (mdfd < 0)
407 pr_err("unexpected failure opening %s\n",
408 devname);
409 return mdfd;
410 }
411
412 /* Open this and check that it is an md device.
413 * On success, return filedescriptor.
414 * On failure, return -1 if it doesn't exist,
415 * or -2 if it exists but is not an md device.
416 */
417 int open_mddev(char *dev, int report_errors)
418 {
419 int mdfd = open(dev, O_RDWR);
420 if (mdfd < 0 && errno == EACCES)
421 mdfd = open(dev, O_RDONLY);
422 if (mdfd < 0) {
423 if (report_errors)
424 pr_err("error opening %s: %s\n",
425 dev, strerror(errno));
426 return -1;
427 }
428 if (md_get_version(mdfd) <= 0) {
429 close(mdfd);
430 if (report_errors)
431 pr_err("%s does not appear to be an md device\n", dev);
432 return -2;
433 }
434 return mdfd;
435 }
436
437 char *find_free_devnm(int use_partitions)
438 {
439 static char devnm[32];
440 int devnum;
441 for (devnum = 127; devnum != 128;
442 devnum = devnum ? devnum-1 : (1<<9)-1) {
443
444 if (use_partitions)
445 sprintf(devnm, "md_d%d", devnum);
446 else
447 sprintf(devnm, "md%d", devnum);
448 if (mddev_busy(devnm))
449 continue;
450 if (!conf_name_is_free(devnm))
451 continue;
452 if (!use_udev()) {
453 /* make sure it is new to /dev too, at least as a
454 * non-standard */
455 dev_t devid = devnm2devid(devnm);
456 if (devid) {
457 char *dn = map_dev(major(devid),
458 minor(devid), 0);
459 if (dn && ! is_standard(dn, NULL))
460 continue;
461 }
462 }
463 break;
464 }
465 if (devnum == 128)
466 return NULL;
467 return devnm;
468 }