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