]>
Commit | Line | Data |
---|---|---|
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 | 35 | void 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 | |
133 | int 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; | |
11fb4c05 N |
183 | /* recreate name: /dev/md/0 or /dev/md/d0 */ |
184 | sprintf(cname, "%s%d", use_mdp?"d":"", num); | |
69207ff6 N |
185 | } else |
186 | strcpy(cname, dev); | |
187 | ||
11fb4c05 N |
188 | /* 'cname' must not contain a slash, and may not be |
189 | * empty. | |
69207ff6 | 190 | */ |
11fb4c05 | 191 | if (strchr(cname, '/') != NULL) { |
69207ff6 N |
192 | fprintf(stderr, Name ": %s is an invalid name " |
193 | "for an md device.\n", dev); | |
194 | return -1; | |
f1ae21c4 | 195 | } |
11fb4c05 | 196 | if (cname[0] == 0) { |
69207ff6 N |
197 | fprintf(stderr, Name ": %s is an invalid name " |
198 | "for an md device (empty!).", dev); | |
199 | return -1; | |
b5e64645 | 200 | } |
11fb4c05 N |
201 | if (num < 0) { |
202 | /* If cname is 'N' or 'dN', we get dev number | |
203 | * from there. | |
204 | */ | |
205 | char *sp = cname; | |
206 | char *ep; | |
207 | if (cname[0] == 'd') | |
208 | sp++; | |
209 | num = strtoul(sp, &ep, 10); | |
210 | if (ep == sp || *ep || num < 0) | |
211 | num = -1; | |
212 | else if (cname[0] == 'd') | |
213 | use_mdp = 1; | |
214 | else | |
215 | use_mdp = 0; | |
216 | } | |
69207ff6 | 217 | } |
f1ae21c4 | 218 | |
69207ff6 N |
219 | /* Now determine device number */ |
220 | /* named 'METADATA' cannot use 'mdp'. */ | |
221 | if (name && name[0] == 0) | |
222 | name = NULL; | |
223 | if (name && trustworthy == METADATA && use_mdp == 1) { | |
224 | fprintf(stderr, Name ": %s is not allowed for a %s container. " | |
225 | "Consider /dev/md%d.\n", dev, name, num); | |
226 | return -1; | |
227 | } | |
228 | if (name && trustworthy == METADATA) | |
229 | use_mdp = 0; | |
230 | if (use_mdp == -1) { | |
231 | if (autof == 4 || autof == 6) | |
232 | use_mdp = 1; | |
f1ae21c4 | 233 | else |
69207ff6 N |
234 | use_mdp = 0; |
235 | } | |
236 | if (num < 0 && trustworthy == LOCAL && name) { | |
237 | /* if name is numeric, us that for num */ | |
238 | char *ep; | |
239 | num = strtoul(name, &ep, 10); | |
240 | if (ep == name || *ep) | |
241 | num = -1; | |
242 | } | |
243 | ||
244 | if (num < 0) { | |
245 | /* need to choose a free number. */ | |
246 | num = find_free_devnum(use_mdp); | |
247 | if (num == NoMdDev) { | |
248 | fprintf(stderr, Name ": No avail md devices - aborting\n"); | |
249 | return -1; | |
250 | } | |
251 | } else { | |
252 | num = use_mdp ? (-1-num) : num; | |
253 | if (mddev_busy(num)) { | |
254 | fprintf(stderr, Name ": %s is already in use.\n", | |
255 | dev); | |
256 | return -1; | |
257 | } | |
258 | } | |
259 | ||
260 | if (num < 0) | |
261 | sprintf(devname, "/dev/md_d%d", -1-num); | |
262 | else | |
263 | sprintf(devname, "/dev/md%d", num); | |
f1ae21c4 | 264 | |
69207ff6 N |
265 | if (cname[0] == 0 && name) { |
266 | /* Need to find a name if we can | |
267 | * We don't completely trust 'name'. Truncate to | |
268 | * reasonable length and remove '/' | |
8d80900b | 269 | */ |
69207ff6 N |
270 | char *cp; |
271 | strncpy(cname, name, 200); | |
272 | cname[200] = 0; | |
273 | while ((cp = strchr(cname, '/')) != NULL) | |
274 | *cp = '-'; | |
275 | if (trustworthy == METADATA) | |
276 | /* always add device number to metadata */ | |
277 | sprintf(cname+strlen(cname), "%d", num); | |
278 | else if (trustworthy == FOREIGN && | |
279 | strchr(cname, ':') == NULL) | |
280 | /* add _%d to FOREIGN array that don't have | |
281 | * a 'host:' prefix | |
282 | */ | |
283 | sprintf(cname+strlen(cname), "_%d", num<0?(-1-num):num); | |
284 | } | |
285 | if (cname[0] == 0) | |
286 | strcpy(chosen, devname); | |
8d80900b | 287 | |
69207ff6 | 288 | /* We have a device number and name. |
11fb4c05 N |
289 | * If we cannot detect udev, we need to make |
290 | * devices and links ourselves. | |
69207ff6 N |
291 | */ |
292 | if (stat("/dev/.udev", &stb) != 0 || | |
293 | check_env("MDADM_NO_UDEV")) { | |
294 | /* Make sure 'devname' exists and 'chosen' is a symlink to it */ | |
295 | if (lstat(devname, &stb) == 0) { | |
296 | /* Must be the correct device, else error */ | |
297 | if ((stb.st_mode&S_IFMT) != S_IFBLK || | |
298 | stb.st_rdev != makedev(dev2major(num),dev2minor(num))) { | |
299 | fprintf(stderr, Name ": %s exists but looks wrong, please fix\n", | |
300 | devname); | |
301 | return -1; | |
0a6e1c67 | 302 | } |
69207ff6 N |
303 | } else { |
304 | if (mknod(devname, S_IFBLK|0600, | |
305 | makedev(dev2major(num),dev2minor(num))) != 0) { | |
306 | fprintf(stderr, Name ": failed to create %s\n", | |
307 | devname); | |
b5e64645 NB |
308 | return -1; |
309 | } | |
69207ff6 N |
310 | if (chown(devname, ci->uid, ci->gid)) |
311 | perror("chown"); | |
312 | if (chmod(devname, ci->mode)) | |
313 | perror("chmod"); | |
314 | stat(devname, &stb); | |
315 | add_dev(devname, &stb, 0, NULL); | |
316 | } | |
c4fe2d4f N |
317 | if (use_mdp == 1) |
318 | make_parts(devname, parts); | |
69207ff6 N |
319 | if (strcmp(chosen, devname) != 0) { |
320 | ||
321 | if (mkdir("/dev/md",0700)==0) { | |
322 | if (chown("/dev/md", ci->uid, ci->gid)) | |
323 | perror("chown /dev/md"); | |
324 | if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111))) | |
325 | perror("chmod /dev/md"); | |
8d80900b | 326 | } |
69207ff6 N |
327 | |
328 | if (dev && strcmp(chosen, dev) == 0) | |
329 | /* We know we are allowed to use this name */ | |
330 | unlink(chosen); | |
331 | ||
332 | if (lstat(chosen, &stb) == 0) { | |
333 | char buf[300]; | |
334 | if ((stb.st_mode & S_IFMT) != S_IFLNK || | |
335 | readlink(chosen, buf, 300) <0 || | |
336 | strcmp(buf, devname) != 0) { | |
337 | fprintf(stderr, Name ": %s exists - ignoring\n", | |
338 | chosen); | |
339 | strcpy(chosen, devname); | |
340 | } | |
341 | } else | |
342 | symlink(devname, chosen); | |
c4fe2d4f N |
343 | if (use_mdp && strcmp(chosen, devname) != 0) |
344 | make_parts(chosen, parts); | |
b5e64645 | 345 | } |
b5e64645 | 346 | } |
69207ff6 N |
347 | mdfd = open_dev_excl(num); |
348 | if (mdfd < 0) | |
349 | fprintf(stderr, Name ": unexpected failure opening %s\n", | |
350 | devname); | |
6be1d39d N |
351 | return mdfd; |
352 | } | |
353 | ||
69207ff6 | 354 | |
6be1d39d N |
355 | /* Open this and check that it is an md device. |
356 | * On success, return filedescriptor. | |
357 | * On failure, return -1 if it doesn't exist, | |
358 | * or -2 if it exists but is not an md device. | |
359 | */ | |
360 | int open_mddev(char *dev, int report_errors) | |
361 | { | |
362 | int mdfd = open(dev, O_RDWR); | |
363 | if (mdfd < 0) { | |
364 | if (report_errors) | |
365 | fprintf(stderr, Name ": error opening %s: %s\n", | |
366 | dev, strerror(errno)); | |
367 | return -1; | |
368 | } | |
369 | if (md_get_version(mdfd) <= 0) { | |
b5e64645 | 370 | close(mdfd); |
6be1d39d N |
371 | if (report_errors) |
372 | fprintf(stderr, Name ": %s does not appear to be " | |
373 | "an md device\n", dev); | |
374 | return -2; | |
b5e64645 NB |
375 | } |
376 | return mdfd; | |
377 | } |