]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdopen.c
Drop the superblock arg from all metadata methods.
[thirdparty/mdadm.git] / mdopen.c
CommitLineData
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
35void 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 52void 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 */
109int 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
NB
115 int must_remove = 0;
116 struct mdstat_ent *mdlist;
117 int num;
8aec876d 118 struct createinfo *ci = conf_get_create_info();
f1ae21c4 119 int parts;
b5e64645 120
5bbb4842
NB
121 if (autof == 0)
122 autof = ci->autof;
123
f1ae21c4
NB
124 parts = autof >> 3;
125 autof &= 7;
126
127 if (autof && autof != 1) {
b5e64645
NB
128 /* autof is set, so we need to check that the name is ok,
129 * and possibly create one if not
130 */
f1ae21c4 131 int std;
b5e64645 132 stb.st_mode = 0;
0bbc98b5 133 if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) {
b5e64645
NB
134 fprintf(stderr, Name ": %s is not a block device.\n",
135 dev);
136 return -1;
137 }
138 /* check major number is correct */
f1ae21c4
NB
139 num = -1;
140 std = is_standard(dev, &num);
b440882e 141 if (std>0) major_num = get_mdp_major();
f1ae21c4
NB
142 switch(autof) {
143 case 2: /* only create is_standard names */
144 if (!std && !stb.st_mode) {
75723446
NB
145 fprintf(stderr, Name
146 ": %s does not exist and is not a 'standard' name "
147 "so it cannot be created\n", dev);
f1ae21c4
NB
148 return -1;
149 }
150 break;
151 case 3: /* create md, reject std>0 */
152 if (std > 0) {
75723446
NB
153 fprintf(stderr, Name ": that --auto option "
154 "not compatable with device named %s\n", dev);
f1ae21c4
NB
155 return -1;
156 }
157 break;
158 case 4: /* create mdp, reject std<0 */
159 if (std < 0) {
75723446
NB
160 fprintf(stderr, Name ": that --auto option "
161 "not compatable with device named %s\n", dev);
f1ae21c4
NB
162 return -1;
163 }
dc2ee6b3 164 major_num = get_mdp_major();
f1ae21c4
NB
165 break;
166 case 5: /* default to md if not standard */
167 break;
168 case 6: /* default to mdp if not standard */
b440882e 169 if (std == 0) major_num = get_mdp_major();
f1ae21c4
NB
170 break;
171 }
172 /* major is final. num is -1 if not standard */
b440882e 173 if (stb.st_mode && major(stb.st_rdev) != major_num)
b5e64645
NB
174 must_remove = 1;
175 if (stb.st_mode && !must_remove) {
b5e64645
NB
176 /* looks ok, see if it is available */
177 mdfd = open(dev, O_RDWR, 0);
178 if (mdfd < 0) {
179 fprintf(stderr, Name ": error opening %s: %s\n",
180 dev, strerror(errno));
181 return -1;
182 } else if (md_get_version(mdfd) <= 0) {
183 fprintf(stderr, Name ": %s does not appear to be an md device\n",
184 dev);
185 close(mdfd);
186 return -1;
187 }
b440882e 188 if (major_num != MD_MAJOR && parts > 0)
e60c27d0
LB
189 make_parts(dev, parts, ci->symlinks);
190 return mdfd;
b5e64645
NB
191 }
192 /* Ok, need to find a minor that is not in use.
8d80900b
NB
193 * If the device name is in a 'standard' format,
194 * intuit the minor from that, else
195 * easiest to read /proc/mdstat, and hunt through for
aba69144 196 * an unused number
b5e64645 197 */
f1ae21c4
NB
198 if (num < 0) {
199 /* need to pick an unused number */
22a88995 200 mdlist = mdstat_read(0, 0);
e7bb5d23
NB
201 /* Choose a large number. Start from 127 and search down,
202 * but if nothing is found, start really big
203 */
204 for (num = 127 ; num != 128 ; num = num ? num-1 : (1<<22)-1) {
8d80900b 205 struct mdstat_ent *me;
f1ae21c4 206 int devnum = num;
b440882e 207 if (major_num != MD_MAJOR)
f1ae21c4
NB
208 devnum = -1-num;
209
8d80900b 210 for (me=mdlist; me; me=me->next)
f1ae21c4 211 if (me->devnum == devnum)
8d80900b
NB
212 break;
213 if (!me) {
e7bb5d23 214 /* doesn't exist in mdstat.
8d80900b 215 * make sure it is new to /dev too
b5e64645 216 */
8d80900b 217 char *dn;
b440882e 218 if (major_num != MD_MAJOR)
219 minor_num = num << MdpMinorShift;
8d80900b 220 else
b440882e 221 minor_num = num;
222 dn = map_dev(major_num,minor_num, 0);
8d80900b
NB
223 if (dn==NULL || is_standard(dn, NULL)) {
224 /* this number only used by a 'standard' name,
225 * so it is safe to use
226 */
227 break;
228 }
b5e64645
NB
229 }
230 }
b440882e 231 } else if (major_num == MD_MAJOR)
232 minor_num = num;
f1ae21c4 233 else
b440882e 234 minor_num = num << MdpMinorShift;
8d80900b 235 /* major and minor have been chosen */
f1ae21c4 236
8d80900b
NB
237 /* If it was a 'standard' name and it is in-use, then
238 * the device could already be correct
239 */
b440882e 240 if (stb.st_mode && major(stb.st_rdev) == major_num &&
241 minor(stb.st_rdev) == minor_num)
8d80900b
NB
242 ;
243 else {
b440882e 244 if (major(makedev(major_num,minor_num)) != major_num ||
245 minor(makedev(major_num,minor_num)) != minor_num) {
0df46c2a
NB
246 fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n");
247 return -1;
248 }
8d80900b
NB
249 if (must_remove)
250 unlink(dev);
251
0a6e1c67
NB
252 if (strncmp(dev, "/dev/md/", 8) == 0) {
253 if (mkdir("/dev/md",0700)==0) {
3a43c803
ML
254 if (chown("/dev/md", ci->uid, ci->gid))
255 perror("chown /dev/md");
256 if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111)))
257 perror("chmod /dev/md");
0a6e1c67
NB
258 }
259 }
b440882e 260 if (mknod(dev, S_IFBLK|0600, makedev(major_num, minor_num))!= 0) {
8d80900b 261 fprintf(stderr, Name ": failed to create %s\n", dev);
b5e64645
NB
262 return -1;
263 }
8d80900b 264 if (must_remove) {
1e0d770c
NB
265 if (chown(dev, stb.st_uid, stb.st_gid))
266 perror("chown");
267 if (chmod(dev, stb.st_mode & 07777))
268 perror("chmod");
5bbb4842
NB
269 } else {
270 if (chown(dev, ci->uid, ci->gid))
271 perror("chown");
272 if (chmod(dev, ci->mode))
273 perror("chmod");
8d80900b 274 }
173fc515
NB
275 stat(dev, &stb);
276 add_dev(dev, &stb, 0, NULL);
38098016 277 if (ci->symlinks && strncmp(dev, "/dev/md/", 8) == 0)
0a6e1c67 278 make_dev_symlink(dev);
b440882e 279 if (major_num != MD_MAJOR)
38098016 280 make_parts(dev,parts, ci->symlinks);
b5e64645 281 }
b5e64645
NB
282 }
283 mdfd = open(dev, O_RDWR, 0);
284 if (mdfd < 0)
285 fprintf(stderr, Name ": error opening %s: %s\n",
286 dev, strerror(errno));
287 else if (md_get_version(mdfd) <= 0) {
288 fprintf(stderr, Name ": %s does not appear to be an md device\n",
289 dev);
290 close(mdfd);
291 mdfd = -1;
292 }
293 return mdfd;
294}
295
8382f19b
NB
296
297int open_mddev_devnum(char *devname, int devnum, char *name, char *chosen_name)
298{
299 /* Open the md device with number 'devnum', possibly using 'devname',
300 * possibly constructing a name with 'name', but in any case, copying
301 * the name into 'chosen_name'
302 */
b440882e 303 int major_num, minor_num;
8382f19b
NB
304 struct stat stb;
305
306 if (devname)
307 strcpy(chosen_name, devname);
308 else if (name && strchr(name,'/') == NULL) {
309 char *n = strchr(name, ':');
310 if (n) n++; else n = name;
311 if (isdigit(*n) && devnum < 0)
312 sprintf(chosen_name, "/dev/md/d%s", n);
313 else
314 sprintf(chosen_name, "/dev/md/%s", n);
315 } else {
316 if (devnum >= 0)
317 sprintf(chosen_name, "/dev/md%d", devnum);
318 else
319 sprintf(chosen_name, "/dev/md/d%d", -1-devnum);
320 }
321 if (devnum >= 0) {
b440882e 322 major_num = MD_MAJOR;
323 minor_num = devnum;
8382f19b 324 } else {
b440882e 325 major_num = get_mdp_major();
326 minor_num = (-1-devnum) << 6;
8382f19b
NB
327 }
328 if (stat(chosen_name, &stb) == 0) {
329 /* It already exists. Check it is right. */
330 if ( ! S_ISBLK(stb.st_mode) ||
b440882e 331 stb.st_rdev != makedev(major_num, minor_num)) {
8382f19b
NB
332 errno = EEXIST;
333 return -1;
334 }
335 } else {
336 if (mknod(chosen_name, S_IFBLK | 0600,
b440882e 337 makedev(major_num, minor_num)) != 0) {
8382f19b
NB
338 return -1;
339 }
340 /* FIXME chown/chmod ?? */
341 }
342 return open(chosen_name, O_RDWR);
343}