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