]>
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 NB |
34 | |
35 | void make_dev_symlink(char *dev) | |
36 | { | |
37 | char *new = strdup(dev); | |
38 | ||
39 | if (!new) return; | |
40 | /* /dev/md/0 -> /dev/md0 | |
41 | * /dev/md/d0 -> /dev/md_d0 | |
42 | */ | |
43 | if (isdigit(new[8])) | |
44 | strcpy(new+7, new+8); | |
45 | else | |
46 | new[7] = '_'; | |
3a43c803 ML |
47 | if (symlink(dev+5, new)) |
48 | perror(new); | |
0a6e1c67 NB |
49 | } |
50 | ||
51 | ||
38098016 | 52 | void make_parts(char *dev, int cnt, int symlinks) |
b5e64645 NB |
53 | { |
54 | /* make 'cnt' partition devices for 'dev' | |
55 | * We use the major/minor from dev and add 1..cnt | |
8d80900b | 56 | * If dev ends with a digit, we add "p%d" else "%d" |
b5e64645 NB |
57 | * If the name exists, we use it's owner/mode, |
58 | * else that of dev | |
59 | */ | |
60 | struct stat stb; | |
b440882e | 61 | int major_num, minor_num; |
b5e64645 | 62 | int i; |
8f23b0b3 NB |
63 | int nlen = strlen(dev) + 20; |
64 | char *name = malloc(nlen); | |
b5e64645 NB |
65 | int dig = isdigit(dev[strlen(dev)-1]); |
66 | ||
f1ae21c4 | 67 | if (cnt==0) cnt=4; |
b5e64645 NB |
68 | if (stat(dev, &stb)!= 0) |
69 | return; | |
70 | if (!S_ISBLK(stb.st_mode)) | |
71 | return; | |
b440882e | 72 | major_num = major(stb.st_rdev); |
73 | minor_num = minor(stb.st_rdev); | |
b5e64645 NB |
74 | for (i=1; i <= cnt ; i++) { |
75 | struct stat stb2; | |
8f23b0b3 | 76 | snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); |
b5e64645 NB |
77 | if (stat(name, &stb2)==0) { |
78 | if (!S_ISBLK(stb2.st_mode)) | |
79 | continue; | |
b440882e | 80 | if (stb2.st_rdev == makedev(major_num, minor_num+i)) |
b5e64645 NB |
81 | continue; |
82 | unlink(name); | |
83 | } else { | |
84 | stb2 = stb; | |
85 | } | |
b440882e | 86 | if (mknod(name, S_IFBLK | 0600, makedev(major_num, minor_num+i))) |
1e0d770c NB |
87 | perror("mknod"); |
88 | if (chown(name, stb2.st_uid, stb2.st_gid)) | |
89 | perror("chown"); | |
90 | if (chmod(name, stb2.st_mode & 07777)) | |
91 | perror("chmod"); | |
38098016 | 92 | if (symlinks && strncmp(name, "/dev/md/", 8) == 0) |
0a6e1c67 | 93 | make_dev_symlink(name); |
173fc515 NB |
94 | stat(name, &stb2); |
95 | add_dev(name, &stb2, 0, NULL); | |
b5e64645 NB |
96 | } |
97 | } | |
98 | ||
0a6e1c67 | 99 | |
b5e64645 NB |
100 | /* |
101 | * Open a given md device, and check that it really is one. | |
102 | * If 'autof' is given, then we need to create, or recreate, the md device. | |
103 | * If the name already exists, and is not a block device, we fail. | |
104 | * If it exists and is not an md device, is not the right type (partitioned or not), | |
105 | * or is currently in-use, we remove the device, but remember the owner and mode. | |
8d80900b | 106 | * If it now doesn't exist, we find a new md array and create the device. |
5bbb4842 | 107 | * Default ownership/mode comes from config file. |
b5e64645 NB |
108 | */ |
109 | int open_mddev(char *dev, int autof) | |
110 | { | |
111 | int mdfd; | |
112 | struct stat stb; | |
b440882e | 113 | int major_num = MD_MAJOR; |
114 | int minor_num = 0; | |
b5e64645 | 115 | int must_remove = 0; |
b5e64645 | 116 | int num; |
8aec876d | 117 | struct createinfo *ci = conf_get_create_info(); |
f1ae21c4 | 118 | int parts; |
b5e64645 | 119 | |
5bbb4842 NB |
120 | if (autof == 0) |
121 | autof = ci->autof; | |
122 | ||
f1ae21c4 NB |
123 | parts = autof >> 3; |
124 | autof &= 7; | |
125 | ||
126 | if (autof && autof != 1) { | |
b5e64645 NB |
127 | /* autof is set, so we need to check that the name is ok, |
128 | * and possibly create one if not | |
129 | */ | |
f1ae21c4 | 130 | int std; |
b5e64645 | 131 | stb.st_mode = 0; |
0bbc98b5 | 132 | if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) { |
b5e64645 NB |
133 | fprintf(stderr, Name ": %s is not a block device.\n", |
134 | dev); | |
135 | return -1; | |
136 | } | |
137 | /* check major number is correct */ | |
f1ae21c4 NB |
138 | num = -1; |
139 | std = is_standard(dev, &num); | |
b440882e | 140 | if (std>0) major_num = get_mdp_major(); |
f1ae21c4 NB |
141 | switch(autof) { |
142 | case 2: /* only create is_standard names */ | |
143 | if (!std && !stb.st_mode) { | |
75723446 NB |
144 | fprintf(stderr, Name |
145 | ": %s does not exist and is not a 'standard' name " | |
146 | "so it cannot be created\n", dev); | |
f1ae21c4 NB |
147 | return -1; |
148 | } | |
149 | break; | |
150 | case 3: /* create md, reject std>0 */ | |
151 | if (std > 0) { | |
75723446 NB |
152 | fprintf(stderr, Name ": that --auto option " |
153 | "not compatable with device named %s\n", dev); | |
f1ae21c4 NB |
154 | return -1; |
155 | } | |
156 | break; | |
157 | case 4: /* create mdp, reject std<0 */ | |
158 | if (std < 0) { | |
75723446 NB |
159 | fprintf(stderr, Name ": that --auto option " |
160 | "not compatable with device named %s\n", dev); | |
f1ae21c4 NB |
161 | return -1; |
162 | } | |
dc2ee6b3 | 163 | major_num = get_mdp_major(); |
f1ae21c4 NB |
164 | break; |
165 | case 5: /* default to md if not standard */ | |
166 | break; | |
167 | case 6: /* default to mdp if not standard */ | |
b440882e | 168 | if (std == 0) major_num = get_mdp_major(); |
f1ae21c4 NB |
169 | break; |
170 | } | |
171 | /* major is final. num is -1 if not standard */ | |
b440882e | 172 | if (stb.st_mode && major(stb.st_rdev) != major_num) |
b5e64645 NB |
173 | must_remove = 1; |
174 | if (stb.st_mode && !must_remove) { | |
b5e64645 NB |
175 | /* looks ok, see if it is available */ |
176 | mdfd = open(dev, O_RDWR, 0); | |
177 | if (mdfd < 0) { | |
178 | fprintf(stderr, Name ": error opening %s: %s\n", | |
179 | dev, strerror(errno)); | |
180 | return -1; | |
181 | } else if (md_get_version(mdfd) <= 0) { | |
182 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
183 | dev); | |
184 | close(mdfd); | |
185 | return -1; | |
186 | } | |
b440882e | 187 | if (major_num != MD_MAJOR && parts > 0) |
e60c27d0 LB |
188 | make_parts(dev, parts, ci->symlinks); |
189 | return mdfd; | |
b5e64645 NB |
190 | } |
191 | /* Ok, need to find a minor that is not in use. | |
8d80900b NB |
192 | * If the device name is in a 'standard' format, |
193 | * intuit the minor from that, else | |
194 | * easiest to read /proc/mdstat, and hunt through for | |
aba69144 | 195 | * an unused number |
b5e64645 | 196 | */ |
f1ae21c4 NB |
197 | if (num < 0) { |
198 | /* need to pick an unused number */ | |
63152c1b | 199 | int num = find_free_devnum(major_num != MD_MAJOR); |
f1ae21c4 | 200 | |
63152c1b NB |
201 | if (major_num == MD_MAJOR) |
202 | minor_num = num; | |
203 | else | |
204 | minor_num = (-1-num) << MdpMinorShift; | |
b440882e | 205 | } else if (major_num == MD_MAJOR) |
206 | minor_num = num; | |
f1ae21c4 | 207 | else |
b440882e | 208 | minor_num = num << MdpMinorShift; |
8d80900b | 209 | /* major and minor have been chosen */ |
f1ae21c4 | 210 | |
8d80900b NB |
211 | /* If it was a 'standard' name and it is in-use, then |
212 | * the device could already be correct | |
213 | */ | |
b440882e | 214 | if (stb.st_mode && major(stb.st_rdev) == major_num && |
215 | minor(stb.st_rdev) == minor_num) | |
8d80900b NB |
216 | ; |
217 | else { | |
b440882e | 218 | if (major(makedev(major_num,minor_num)) != major_num || |
219 | minor(makedev(major_num,minor_num)) != minor_num) { | |
0df46c2a NB |
220 | fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n"); |
221 | return -1; | |
222 | } | |
8d80900b NB |
223 | if (must_remove) |
224 | unlink(dev); | |
225 | ||
0a6e1c67 NB |
226 | if (strncmp(dev, "/dev/md/", 8) == 0) { |
227 | if (mkdir("/dev/md",0700)==0) { | |
3a43c803 ML |
228 | if (chown("/dev/md", ci->uid, ci->gid)) |
229 | perror("chown /dev/md"); | |
230 | if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111))) | |
231 | perror("chmod /dev/md"); | |
0a6e1c67 NB |
232 | } |
233 | } | |
b440882e | 234 | if (mknod(dev, S_IFBLK|0600, makedev(major_num, minor_num))!= 0) { |
8d80900b | 235 | fprintf(stderr, Name ": failed to create %s\n", dev); |
b5e64645 NB |
236 | return -1; |
237 | } | |
8d80900b | 238 | if (must_remove) { |
1e0d770c NB |
239 | if (chown(dev, stb.st_uid, stb.st_gid)) |
240 | perror("chown"); | |
241 | if (chmod(dev, stb.st_mode & 07777)) | |
242 | perror("chmod"); | |
5bbb4842 NB |
243 | } else { |
244 | if (chown(dev, ci->uid, ci->gid)) | |
245 | perror("chown"); | |
246 | if (chmod(dev, ci->mode)) | |
247 | perror("chmod"); | |
8d80900b | 248 | } |
173fc515 NB |
249 | stat(dev, &stb); |
250 | add_dev(dev, &stb, 0, NULL); | |
38098016 | 251 | if (ci->symlinks && strncmp(dev, "/dev/md/", 8) == 0) |
0a6e1c67 | 252 | make_dev_symlink(dev); |
b440882e | 253 | if (major_num != MD_MAJOR) |
38098016 | 254 | make_parts(dev,parts, ci->symlinks); |
b5e64645 | 255 | } |
b5e64645 NB |
256 | } |
257 | mdfd = open(dev, O_RDWR, 0); | |
258 | if (mdfd < 0) | |
259 | fprintf(stderr, Name ": error opening %s: %s\n", | |
260 | dev, strerror(errno)); | |
261 | else if (md_get_version(mdfd) <= 0) { | |
262 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
263 | dev); | |
264 | close(mdfd); | |
265 | mdfd = -1; | |
266 | } | |
267 | return mdfd; | |
268 | } | |
269 | ||
8382f19b | 270 | |
9a02c62a NB |
271 | int open_mddev_devnum(char *devname, int devnum, char *name, |
272 | char *chosen_name, int parts) | |
8382f19b NB |
273 | { |
274 | /* Open the md device with number 'devnum', possibly using 'devname', | |
275 | * possibly constructing a name with 'name', but in any case, copying | |
276 | * the name into 'chosen_name' | |
277 | */ | |
b440882e | 278 | int major_num, minor_num; |
8382f19b | 279 | struct stat stb; |
767bd452 | 280 | int i; |
9a02c62a | 281 | struct createinfo *ci = conf_get_create_info(); |
8382f19b NB |
282 | |
283 | if (devname) | |
284 | strcpy(chosen_name, devname); | |
285 | else if (name && strchr(name,'/') == NULL) { | |
286 | char *n = strchr(name, ':'); | |
287 | if (n) n++; else n = name; | |
288 | if (isdigit(*n) && devnum < 0) | |
289 | sprintf(chosen_name, "/dev/md/d%s", n); | |
290 | else | |
291 | sprintf(chosen_name, "/dev/md/%s", n); | |
292 | } else { | |
293 | if (devnum >= 0) | |
294 | sprintf(chosen_name, "/dev/md%d", devnum); | |
295 | else | |
296 | sprintf(chosen_name, "/dev/md/d%d", -1-devnum); | |
297 | } | |
298 | if (devnum >= 0) { | |
b440882e | 299 | major_num = MD_MAJOR; |
300 | minor_num = devnum; | |
8382f19b | 301 | } else { |
b440882e | 302 | major_num = get_mdp_major(); |
303 | minor_num = (-1-devnum) << 6; | |
8382f19b NB |
304 | } |
305 | if (stat(chosen_name, &stb) == 0) { | |
306 | /* It already exists. Check it is right. */ | |
307 | if ( ! S_ISBLK(stb.st_mode) || | |
b440882e | 308 | stb.st_rdev != makedev(major_num, minor_num)) { |
8382f19b NB |
309 | errno = EEXIST; |
310 | return -1; | |
311 | } | |
312 | } else { | |
62552fdf BN |
313 | /* special case: if --incremental is suggesting a name |
314 | * in /dev/md/, we make sure the directory exists. | |
315 | */ | |
316 | if (strncmp(chosen_name, "/dev/md/", 8) == 0) { | |
62552fdf BN |
317 | if (mkdir("/dev/md",0700)==0) { |
318 | if (chown("/dev/md", ci->uid, ci->gid)) | |
319 | perror("chown /dev/md"); | |
320 | if (chmod("/dev/md", ci->mode| | |
321 | ((ci->mode>>2) & 0111))) | |
322 | perror("chmod /dev/md"); | |
323 | } | |
324 | } | |
325 | ||
8382f19b | 326 | if (mknod(chosen_name, S_IFBLK | 0600, |
b440882e | 327 | makedev(major_num, minor_num)) != 0) { |
8382f19b NB |
328 | return -1; |
329 | } | |
330 | /* FIXME chown/chmod ?? */ | |
331 | } | |
767bd452 BN |
332 | |
333 | /* Simple locking to avoid --incr being called for the same | |
334 | * array multiple times in parallel. | |
335 | */ | |
336 | for (i = 0; i < 25 ; i++) { | |
337 | int fd; | |
338 | ||
339 | fd = open(chosen_name, O_RDWR|O_EXCL); | |
9a02c62a NB |
340 | if (fd >= 0 || errno != EBUSY) { |
341 | if (devnum < 0) | |
342 | make_parts(chosen_name, parts, ci->symlinks); | |
767bd452 | 343 | return fd; |
9a02c62a | 344 | } |
767bd452 BN |
345 | usleep(200000); |
346 | } | |
347 | return -1; | |
8382f19b | 348 | } |