]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
mdopen: only use 'dev' as chosen name if it is a full path.
[thirdparty/mdadm.git] / mdopen.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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
30 void make_parts(char *dev, int cnt)
31 {
32 /* make 'cnt' partition devices for 'dev'
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.
36 * If dev ends with a digit, we add "p%d" else "%d"
37 * If the name exists, we use it's owner/mode,
38 * else that of dev
39 */
40 struct stat stb;
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 */
44 int i;
45 int nlen = strlen(dev) + 20;
46 char *name = malloc(nlen);
47 int dig = isdigit(dev[strlen(dev)-1]);
48 char orig[1024];
49 char sym[1024];
50 int err;
51
52 if (cnt==0) cnt=4;
53 if (lstat(dev, &stb)!= 0)
54 return;
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;
66 for (i=1; i <= cnt ; i++) {
67 struct stat stb2;
68 snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
69 if (stat(name, &stb2)==0) {
70 if (!S_ISBLK(stb2.st_mode))
71 continue;
72 if (stb2.st_rdev == makedev(major_num, minor_num+i))
73 continue;
74 unlink(name);
75 } else {
76 stb2 = stb;
77 }
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");
86 err = 0;
87 } else {
88 snprintf(sym, sizeof(sym), "%s%s%d", orig, odig?"p":"", i);
89 err = symlink(sym, name);
90 }
91
92 if (err == 0 && stat(name, &stb2) == 0)
93 add_dev(name, &stb2, 0, NULL);
94 }
95 }
96
97
98 /*
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.
130 */
131
132 int create_mddev(char *dev, char *name, int autof, int trustworthy,
133 char *chosen)
134 {
135 int mdfd;
136 struct stat stb;
137 int num = -1;
138 int use_mdp = -1;
139 struct createinfo *ci = conf_get_create_info();
140 int parts;
141 char *cname;
142 char devname[20];
143 char cbuf[400];
144 if (chosen == NULL)
145 chosen = cbuf;
146
147
148 if (autof == 0)
149 autof = ci->autof;
150
151 parts = autof >> 3;
152 autof &= 7;
153
154 strcpy(chosen, "/dev/md/");
155 cname = chosen + strlen(chosen);
156
157
158 if (dev) {
159 if (strncmp(dev, "/dev/md/", 8) == 0) {
160 strcpy(cname, dev+8);
161 } else if (strncmp(dev, "/dev/", 5) == 0) {
162 char *e = dev + strlen(dev);
163 while (e > dev && isdigit(e[-1]))
164 e--;
165 if (e[0])
166 num = strtoul(e, NULL, 10);
167 strcpy(cname, dev+5);
168 cname[e-(dev+5)] = 0;
169 /* name *must* be mdXX or md_dXX in this context */
170 if (num < 0 ||
171 (strcmp(cname, "md") != 0 && strcmp(cname, "md_d") != 0)) {
172 fprintf(stderr, Name ": %s is an invalid name "
173 "for an md device. Try /dev/md/%s\n",
174 dev, dev+5);
175 return -1;
176 }
177 if (strcmp(cname, "md") == 0)
178 use_mdp = 0;
179 else
180 use_mdp = 1;
181 /* recreate name: /dev/md/0 or /dev/md/d0 */
182 sprintf(cname, "%s%d", use_mdp?"d":"", num);
183 } else
184 strcpy(cname, dev);
185
186 /* 'cname' must not contain a slash, and may not be
187 * empty.
188 */
189 if (strchr(cname, '/') != NULL) {
190 fprintf(stderr, Name ": %s is an invalid name "
191 "for an md device.\n", dev);
192 return -1;
193 }
194 if (cname[0] == 0) {
195 fprintf(stderr, Name ": %s is an invalid name "
196 "for an md device (empty!).", dev);
197 return -1;
198 }
199 if (num < 0) {
200 /* If cname is 'N' or 'dN', we get dev number
201 * from there.
202 */
203 char *sp = cname;
204 char *ep;
205 if (cname[0] == 'd')
206 sp++;
207 num = strtoul(sp, &ep, 10);
208 if (ep == sp || *ep || num < 0)
209 num = -1;
210 else if (cname[0] == 'd')
211 use_mdp = 1;
212 else
213 use_mdp = 0;
214 }
215 }
216
217 /* Now determine device number */
218 /* named 'METADATA' cannot use 'mdp'. */
219 if (name && name[0] == 0)
220 name = NULL;
221 if (name && trustworthy == METADATA && use_mdp == 1) {
222 fprintf(stderr, Name ": %s is not allowed for a %s container. "
223 "Consider /dev/md%d.\n", dev, name, num);
224 return -1;
225 }
226 if (name && trustworthy == METADATA)
227 use_mdp = 0;
228 if (use_mdp == -1) {
229 if (autof == 4 || autof == 6)
230 use_mdp = 1;
231 else
232 use_mdp = 0;
233 }
234 if (num < 0 && trustworthy == LOCAL && name) {
235 /* if name is numeric, possibly prefixed by
236 * 'md' or '/dev/md', use that for num
237 * if it is not already in use */
238 char *ep;
239 char *n2 = name;
240 if (strncmp(n2, "/dev/", 5) == 0)
241 n2 += 5;
242 if (strncmp(n2, "md", 2) == 0)
243 n2 += 2;
244 if (*n2 == '/')
245 n2++;
246 num = strtoul(n2, &ep, 10);
247 if (ep == n2 || *ep)
248 num = -1;
249 else if (mddev_busy(use_mdp ? (-1-num) : num))
250 num = -1;
251 }
252
253 if (num < 0) {
254 /* need to choose a free number. */
255 num = find_free_devnum(use_mdp);
256 if (num == NoMdDev) {
257 fprintf(stderr, Name ": No avail md devices - aborting\n");
258 return -1;
259 }
260 } else {
261 num = use_mdp ? (-1-num) : num;
262 if (mddev_busy(num)) {
263 fprintf(stderr, Name ": %s is already in use.\n",
264 dev);
265 return -1;
266 }
267 }
268
269 if (num < 0)
270 sprintf(devname, "/dev/md_d%d", -1-num);
271 else
272 sprintf(devname, "/dev/md%d", num);
273
274 if (cname[0] == 0 && name) {
275 /* Need to find a name if we can
276 * We don't completely trust 'name'. Truncate to
277 * reasonable length and remove '/'
278 */
279 char *cp;
280 struct map_ent *map = NULL;
281 int conflict = 1;
282 int unum = 0;
283 int cnlen;
284 strncpy(cname, name, 200);
285 cname[200] = 0;
286 while ((cp = strchr(cname, '/')) != NULL)
287 *cp = '-';
288 if (trustworthy == LOCAL ||
289 (trustworthy == FOREIGN && strchr(cname, ':') != NULL)) {
290 /* Only need suffix if there is a conflict */
291 if (map_by_name(&map, cname) == NULL)
292 conflict = 0;
293 }
294 cnlen = strlen(cname);
295 while (conflict) {
296 if (trustworthy == METADATA && !isdigit(cname[cnlen-1]))
297 sprintf(cname+cnlen, "%d", unum);
298 else
299 /* add _%d to FOREIGN array that don't
300 * a 'host:' prefix
301 */
302 sprintf(cname+cnlen, "_%d", unum);
303 unum++;
304 if (map_by_name(&map, cname) == NULL)
305 conflict = 0;
306 }
307 }
308
309 if (dev && dev[0] == '/')
310 strcpy(chosen, dev);
311 else if (cname[0] == 0)
312 strcpy(chosen, devname);
313
314 /* We have a device number and name.
315 * If we cannot detect udev, we need to make
316 * devices and links ourselves.
317 */
318 if (stat("/dev/.udev", &stb) != 0 ||
319 check_env("MDADM_NO_UDEV")) {
320 /* Make sure 'devname' exists and 'chosen' is a symlink to it */
321 if (lstat(devname, &stb) == 0) {
322 /* Must be the correct device, else error */
323 if ((stb.st_mode&S_IFMT) != S_IFBLK ||
324 stb.st_rdev != makedev(dev2major(num),dev2minor(num))) {
325 fprintf(stderr, Name ": %s exists but looks wrong, please fix\n",
326 devname);
327 return -1;
328 }
329 } else {
330 if (mknod(devname, S_IFBLK|0600,
331 makedev(dev2major(num),dev2minor(num))) != 0) {
332 fprintf(stderr, Name ": failed to create %s\n",
333 devname);
334 return -1;
335 }
336 if (chown(devname, ci->uid, ci->gid))
337 perror("chown");
338 if (chmod(devname, ci->mode))
339 perror("chmod");
340 stat(devname, &stb);
341 add_dev(devname, &stb, 0, NULL);
342 }
343 if (use_mdp == 1)
344 make_parts(devname, parts);
345 if (strcmp(chosen, devname) != 0) {
346
347 if (mkdir("/dev/md",0700)==0) {
348 if (chown("/dev/md", ci->uid, ci->gid))
349 perror("chown /dev/md");
350 if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111)))
351 perror("chmod /dev/md");
352 }
353
354 if (dev && strcmp(chosen, dev) == 0)
355 /* We know we are allowed to use this name */
356 unlink(chosen);
357
358 if (lstat(chosen, &stb) == 0) {
359 char buf[300];
360 if ((stb.st_mode & S_IFMT) != S_IFLNK ||
361 readlink(chosen, buf, 300) <0 ||
362 strcmp(buf, devname) != 0) {
363 fprintf(stderr, Name ": %s exists - ignoring\n",
364 chosen);
365 strcpy(chosen, devname);
366 }
367 } else if (symlink(devname, chosen) != 0)
368 fprintf(stderr, Name ": failed to create %s: %s\n",
369 chosen, strerror(errno));
370 if (use_mdp && strcmp(chosen, devname) != 0)
371 make_parts(chosen, parts);
372 }
373 }
374 mdfd = open_dev_excl(num);
375 if (mdfd < 0)
376 fprintf(stderr, Name ": unexpected failure opening %s\n",
377 devname);
378 return mdfd;
379 }
380
381
382 /* Open this and check that it is an md device.
383 * On success, return filedescriptor.
384 * On failure, return -1 if it doesn't exist,
385 * or -2 if it exists but is not an md device.
386 */
387 int open_mddev(char *dev, int report_errors)
388 {
389 int mdfd = open(dev, O_RDWR);
390 if (mdfd < 0) {
391 if (report_errors)
392 fprintf(stderr, Name ": error opening %s: %s\n",
393 dev, strerror(errno));
394 return -1;
395 }
396 if (md_get_version(mdfd) <= 0) {
397 close(mdfd);
398 if (report_errors)
399 fprintf(stderr, Name ": %s does not appear to be "
400 "an md device\n", dev);
401 return -2;
402 }
403 return mdfd;
404 }