]>
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] = '_'; | |
47 | symlink(dev+5, new); | |
48 | } | |
49 | ||
50 | ||
38098016 | 51 | void make_parts(char *dev, int cnt, int symlinks) |
b5e64645 NB |
52 | { |
53 | /* make 'cnt' partition devices for 'dev' | |
54 | * We use the major/minor from dev and add 1..cnt | |
8d80900b | 55 | * If dev ends with a digit, we add "p%d" else "%d" |
b5e64645 NB |
56 | * If the name exists, we use it's owner/mode, |
57 | * else that of dev | |
58 | */ | |
59 | struct stat stb; | |
60 | int major, minor; | |
61 | int i; | |
8f23b0b3 NB |
62 | int nlen = strlen(dev) + 20; |
63 | char *name = malloc(nlen); | |
b5e64645 NB |
64 | int dig = isdigit(dev[strlen(dev)-1]); |
65 | ||
f1ae21c4 | 66 | if (cnt==0) cnt=4; |
b5e64645 NB |
67 | if (stat(dev, &stb)!= 0) |
68 | return; | |
69 | if (!S_ISBLK(stb.st_mode)) | |
70 | return; | |
0df46c2a NB |
71 | major = major(stb.st_rdev); |
72 | minor = minor(stb.st_rdev); | |
b5e64645 NB |
73 | for (i=1; i <= cnt ; i++) { |
74 | struct stat stb2; | |
8f23b0b3 | 75 | snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); |
b5e64645 NB |
76 | if (stat(name, &stb2)==0) { |
77 | if (!S_ISBLK(stb2.st_mode)) | |
78 | continue; | |
0df46c2a | 79 | if (stb2.st_rdev == makedev(major, minor+i)) |
b5e64645 NB |
80 | continue; |
81 | unlink(name); | |
82 | } else { | |
83 | stb2 = stb; | |
84 | } | |
1e0d770c NB |
85 | if (mknod(name, S_IFBLK | 0600, makedev(major, minor+i))) |
86 | perror("mknod"); | |
87 | if (chown(name, stb2.st_uid, stb2.st_gid)) | |
88 | perror("chown"); | |
89 | if (chmod(name, stb2.st_mode & 07777)) | |
90 | perror("chmod"); | |
38098016 | 91 | if (symlinks && strncmp(name, "/dev/md/", 8) == 0) |
0a6e1c67 | 92 | make_dev_symlink(name); |
173fc515 NB |
93 | stat(name, &stb2); |
94 | add_dev(name, &stb2, 0, NULL); | |
b5e64645 NB |
95 | } |
96 | } | |
97 | ||
0a6e1c67 | 98 | |
b5e64645 NB |
99 | /* |
100 | * Open a given md device, and check that it really is one. | |
101 | * If 'autof' is given, then we need to create, or recreate, the md device. | |
102 | * If the name already exists, and is not a block device, we fail. | |
103 | * If it exists and is not an md device, is not the right type (partitioned or not), | |
104 | * or is currently in-use, we remove the device, but remember the owner and mode. | |
8d80900b | 105 | * If it now doesn't exist, we find a new md array and create the device. |
5bbb4842 | 106 | * Default ownership/mode comes from config file. |
b5e64645 NB |
107 | */ |
108 | int open_mddev(char *dev, int autof) | |
109 | { | |
110 | int mdfd; | |
111 | struct stat stb; | |
112 | int major = MD_MAJOR; | |
a0ef61bf | 113 | int minor = 0; |
b5e64645 NB |
114 | int must_remove = 0; |
115 | struct mdstat_ent *mdlist; | |
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 | } | |
f1ae21c4 NB |
137 | if (autof == 2 && stb.st_mode == 0 && !is_standard(dev, NULL)) { |
138 | fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev); | |
139 | return -1; | |
140 | } | |
b5e64645 | 141 | /* check major number is correct */ |
f1ae21c4 NB |
142 | num = -1; |
143 | std = is_standard(dev, &num); | |
144 | if (std>0) major = get_mdp_major(); | |
145 | switch(autof) { | |
146 | case 2: /* only create is_standard names */ | |
147 | if (!std && !stb.st_mode) { | |
148 | fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev); | |
149 | return -1; | |
150 | } | |
151 | break; | |
152 | case 3: /* create md, reject std>0 */ | |
153 | if (std > 0) { | |
154 | fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev); | |
155 | return -1; | |
156 | } | |
157 | break; | |
158 | case 4: /* create mdp, reject std<0 */ | |
159 | if (std < 0) { | |
160 | fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev); | |
161 | return -1; | |
162 | } | |
163 | break; | |
164 | case 5: /* default to md if not standard */ | |
165 | break; | |
166 | case 6: /* default to mdp if not standard */ | |
167 | if (std == 0) major = get_mdp_major(); | |
168 | break; | |
169 | } | |
170 | /* major is final. num is -1 if not standard */ | |
0df46c2a | 171 | if (stb.st_mode && major(stb.st_rdev) != major) |
b5e64645 NB |
172 | must_remove = 1; |
173 | if (stb.st_mode && !must_remove) { | |
b5e64645 NB |
174 | /* looks ok, see if it is available */ |
175 | mdfd = open(dev, O_RDWR, 0); | |
176 | if (mdfd < 0) { | |
177 | fprintf(stderr, Name ": error opening %s: %s\n", | |
178 | dev, strerror(errno)); | |
179 | return -1; | |
180 | } else if (md_get_version(mdfd) <= 0) { | |
181 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
182 | dev); | |
183 | close(mdfd); | |
184 | return -1; | |
185 | } | |
e60c27d0 LB |
186 | if (major != MD_MAJOR && parts > 0) |
187 | make_parts(dev, parts, ci->symlinks); | |
188 | return mdfd; | |
b5e64645 NB |
189 | } |
190 | /* Ok, need to find a minor that is not in use. | |
8d80900b NB |
191 | * If the device name is in a 'standard' format, |
192 | * intuit the minor from that, else | |
193 | * easiest to read /proc/mdstat, and hunt through for | |
b5e64645 NB |
194 | * an unused number |
195 | */ | |
f1ae21c4 NB |
196 | if (num < 0) { |
197 | /* need to pick an unused number */ | |
22a88995 | 198 | mdlist = mdstat_read(0, 0); |
e7bb5d23 NB |
199 | /* Choose a large number. Start from 127 and search down, |
200 | * but if nothing is found, start really big | |
201 | */ | |
202 | for (num = 127 ; num != 128 ; num = num ? num-1 : (1<<22)-1) { | |
8d80900b | 203 | struct mdstat_ent *me; |
f1ae21c4 NB |
204 | int devnum = num; |
205 | if (major != MD_MAJOR) | |
206 | devnum = -1-num; | |
207 | ||
8d80900b | 208 | for (me=mdlist; me; me=me->next) |
f1ae21c4 | 209 | if (me->devnum == devnum) |
8d80900b NB |
210 | break; |
211 | if (!me) { | |
e7bb5d23 | 212 | /* doesn't exist in mdstat. |
8d80900b | 213 | * make sure it is new to /dev too |
b5e64645 | 214 | */ |
8d80900b | 215 | char *dn; |
f1ae21c4 NB |
216 | if (major != MD_MAJOR) |
217 | minor = num << MdpMinorShift; | |
8d80900b NB |
218 | else |
219 | minor = num; | |
16c6fa80 | 220 | dn = map_dev(major,minor, 0); |
8d80900b NB |
221 | if (dn==NULL || is_standard(dn, NULL)) { |
222 | /* this number only used by a 'standard' name, | |
223 | * so it is safe to use | |
224 | */ | |
225 | break; | |
226 | } | |
b5e64645 NB |
227 | } |
228 | } | |
f1ae21c4 NB |
229 | } else if (major == MD_MAJOR) |
230 | minor = num; | |
231 | else | |
232 | minor = num << MdpMinorShift; | |
8d80900b | 233 | /* major and minor have been chosen */ |
f1ae21c4 | 234 | |
8d80900b NB |
235 | /* If it was a 'standard' name and it is in-use, then |
236 | * the device could already be correct | |
237 | */ | |
0df46c2a NB |
238 | if (stb.st_mode && major(stb.st_rdev) == major && |
239 | minor(stb.st_rdev) == minor) | |
8d80900b NB |
240 | ; |
241 | else { | |
0df46c2a NB |
242 | if (major(makedev(major,minor)) != major || |
243 | minor(makedev(major,minor)) != minor) { | |
244 | fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n"); | |
245 | return -1; | |
246 | } | |
8d80900b NB |
247 | if (must_remove) |
248 | unlink(dev); | |
249 | ||
0a6e1c67 NB |
250 | if (strncmp(dev, "/dev/md/", 8) == 0) { |
251 | if (mkdir("/dev/md",0700)==0) { | |
252 | chown("/dev/md", ci->uid, ci->gid); | |
253 | chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111)); | |
254 | } | |
255 | } | |
0df46c2a | 256 | if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) { |
8d80900b | 257 | fprintf(stderr, Name ": failed to create %s\n", dev); |
b5e64645 NB |
258 | return -1; |
259 | } | |
8d80900b | 260 | if (must_remove) { |
1e0d770c NB |
261 | if (chown(dev, stb.st_uid, stb.st_gid)) |
262 | perror("chown"); | |
263 | if (chmod(dev, stb.st_mode & 07777)) | |
264 | perror("chmod"); | |
5bbb4842 NB |
265 | } else { |
266 | if (chown(dev, ci->uid, ci->gid)) | |
267 | perror("chown"); | |
268 | if (chmod(dev, ci->mode)) | |
269 | perror("chmod"); | |
8d80900b | 270 | } |
173fc515 NB |
271 | stat(dev, &stb); |
272 | add_dev(dev, &stb, 0, NULL); | |
38098016 | 273 | if (ci->symlinks && strncmp(dev, "/dev/md/", 8) == 0) |
0a6e1c67 | 274 | make_dev_symlink(dev); |
f1ae21c4 | 275 | if (major != MD_MAJOR) |
38098016 | 276 | make_parts(dev,parts, ci->symlinks); |
b5e64645 | 277 | } |
b5e64645 NB |
278 | } |
279 | mdfd = open(dev, O_RDWR, 0); | |
280 | if (mdfd < 0) | |
281 | fprintf(stderr, Name ": error opening %s: %s\n", | |
282 | dev, strerror(errno)); | |
283 | else if (md_get_version(mdfd) <= 0) { | |
284 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
285 | dev); | |
286 | close(mdfd); | |
287 | mdfd = -1; | |
288 | } | |
289 | return mdfd; | |
290 | } | |
291 |