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