]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdopen.c
More consistent honoring of --configfile
[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
34void 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 */
88int 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;
8aec876d 97 struct createinfo *ci = conf_get_create_info();
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 */
b5e64645 169 close(mdfd);
d55e3aef 170 fprintf(stderr, Name ": %s is already active.\n",
2e2642f2
NB
171 dev);
172 return -1;
b5e64645 173 } else {
f1ae21c4
NB
174 if (major != MD_MAJOR && parts > 0)
175 make_parts(dev, parts);
b5e64645
NB
176 return mdfd;
177 }
178 }
179 /* Ok, need to find a minor that is not in use.
8d80900b
NB
180 * If the device name is in a 'standard' format,
181 * intuit the minor from that, else
182 * easiest to read /proc/mdstat, and hunt through for
b5e64645
NB
183 * an unused number
184 */
f1ae21c4
NB
185 if (num < 0) {
186 /* need to pick an unused number */
22a88995 187 mdlist = mdstat_read(0, 0);
e7bb5d23
NB
188 /* Choose a large number. Start from 127 and search down,
189 * but if nothing is found, start really big
190 */
191 for (num = 127 ; num != 128 ; num = num ? num-1 : (1<<22)-1) {
8d80900b 192 struct mdstat_ent *me;
f1ae21c4
NB
193 int devnum = num;
194 if (major != MD_MAJOR)
195 devnum = -1-num;
196
8d80900b 197 for (me=mdlist; me; me=me->next)
f1ae21c4 198 if (me->devnum == devnum)
8d80900b
NB
199 break;
200 if (!me) {
e7bb5d23 201 /* doesn't exist in mdstat.
8d80900b 202 * make sure it is new to /dev too
b5e64645 203 */
8d80900b 204 char *dn;
f1ae21c4
NB
205 if (major != MD_MAJOR)
206 minor = num << MdpMinorShift;
8d80900b
NB
207 else
208 minor = num;
16c6fa80 209 dn = map_dev(major,minor, 0);
8d80900b
NB
210 if (dn==NULL || is_standard(dn, NULL)) {
211 /* this number only used by a 'standard' name,
212 * so it is safe to use
213 */
214 break;
215 }
b5e64645
NB
216 }
217 }
f1ae21c4
NB
218 } else if (major == MD_MAJOR)
219 minor = num;
220 else
221 minor = num << MdpMinorShift;
8d80900b 222 /* major and minor have been chosen */
f1ae21c4 223
8d80900b
NB
224 /* If it was a 'standard' name and it is in-use, then
225 * the device could already be correct
226 */
0df46c2a
NB
227 if (stb.st_mode && major(stb.st_rdev) == major &&
228 minor(stb.st_rdev) == minor)
8d80900b
NB
229 ;
230 else {
0df46c2a
NB
231 if (major(makedev(major,minor)) != major ||
232 minor(makedev(major,minor)) != minor) {
233 fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n");
234 return -1;
235 }
8d80900b
NB
236 if (must_remove)
237 unlink(dev);
238
0df46c2a 239 if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) {
8d80900b 240 fprintf(stderr, Name ": failed to create %s\n", dev);
b5e64645
NB
241 return -1;
242 }
8d80900b 243 if (must_remove) {
1e0d770c
NB
244 if (chown(dev, stb.st_uid, stb.st_gid))
245 perror("chown");
246 if (chmod(dev, stb.st_mode & 07777))
247 perror("chmod");
5bbb4842
NB
248 } else {
249 if (chown(dev, ci->uid, ci->gid))
250 perror("chown");
251 if (chmod(dev, ci->mode))
252 perror("chmod");
8d80900b 253 }
173fc515
NB
254 stat(dev, &stb);
255 add_dev(dev, &stb, 0, NULL);
f1ae21c4
NB
256 if (major != MD_MAJOR)
257 make_parts(dev,parts);
b5e64645 258 }
b5e64645
NB
259 }
260 mdfd = open(dev, O_RDWR, 0);
261 if (mdfd < 0)
262 fprintf(stderr, Name ": error opening %s: %s\n",
263 dev, strerror(errno));
264 else if (md_get_version(mdfd) <= 0) {
265 fprintf(stderr, Name ": %s does not appear to be an md device\n",
266 dev);
267 close(mdfd);
268 mdfd = -1;
269 }
270 return mdfd;
271}
272