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