]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
35 void make_dev_symlink(char *dev
)
37 char *new = strdup(dev
);
40 /* /dev/md/0 -> /dev/md0
41 * /dev/md/d0 -> /dev/md_d0
51 void make_parts(char *dev
, int cnt
, int symlinks
)
53 /* make 'cnt' partition devices for 'dev'
54 * We use the major/minor from dev and add 1..cnt
55 * If dev ends with a digit, we add "p%d" else "%d"
56 * If the name exists, we use it's owner/mode,
62 int nlen
= strlen(dev
) + 20;
63 char *name
= malloc(nlen
);
64 int dig
= isdigit(dev
[strlen(dev
)-1]);
67 if (stat(dev
, &stb
)!= 0)
69 if (!S_ISBLK(stb
.st_mode
))
71 major
= major(stb
.st_rdev
);
72 minor
= minor(stb
.st_rdev
);
73 for (i
=1; i
<= cnt
; i
++) {
75 snprintf(name
, nlen
, "%s%s%d", dev
, dig
?"p":"", i
);
76 if (stat(name
, &stb2
)==0) {
77 if (!S_ISBLK(stb2
.st_mode
))
79 if (stb2
.st_rdev
== makedev(major
, minor
+i
))
85 if (mknod(name
, S_IFBLK
| 0600, makedev(major
, minor
+i
)))
87 if (chown(name
, stb2
.st_uid
, stb2
.st_gid
))
89 if (chmod(name
, stb2
.st_mode
& 07777))
91 if (symlinks
&& strncmp(name
, "/dev/md/", 8) == 0)
92 make_dev_symlink(name
);
94 add_dev(name
, &stb2
, 0, NULL
);
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.
105 * If it now doesn't exist, we find a new md array and create the device.
106 * Default ownership/mode comes from config file.
108 int open_mddev(char *dev
, int autof
)
112 int major
= MD_MAJOR
;
115 struct mdstat_ent
*mdlist
;
117 struct createinfo
*ci
= conf_get_create_info();
126 if (autof
&& autof
!= 1) {
127 /* autof is set, so we need to check that the name is ok,
128 * and possibly create one if not
132 if (stat(dev
, &stb
)==0 && ! S_ISBLK(stb
.st_mode
)) {
133 fprintf(stderr
, Name
": %s is not a block device.\n",
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
);
141 /* check major number is correct */
143 std
= is_standard(dev
, &num
);
144 if (std
>0) major
= get_mdp_major();
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
);
152 case 3: /* create md, reject std>0 */
154 fprintf(stderr
, Name
": that --auto option not compatable with device named %s\n", dev
);
158 case 4: /* create mdp, reject std<0 */
160 fprintf(stderr
, Name
": that --auto option not compatable with device named %s\n", dev
);
164 case 5: /* default to md if not standard */
166 case 6: /* default to mdp if not standard */
167 if (std
== 0) major
= get_mdp_major();
170 /* major is final. num is -1 if not standard */
171 if (stb
.st_mode
&& major(stb
.st_rdev
) != major
)
173 if (stb
.st_mode
&& !must_remove
) {
174 mdu_array_info_t array
;
175 /* looks ok, see if it is available */
176 mdfd
= open(dev
, O_RDWR
, 0);
178 fprintf(stderr
, Name
": error opening %s: %s\n",
179 dev
, strerror(errno
));
181 } else if (md_get_version(mdfd
) <= 0) {
182 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
187 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)==0) {
190 fprintf(stderr
, Name
": %s is already active.\n",
194 if (major
!= MD_MAJOR
&& parts
> 0)
195 make_parts(dev
, parts
, ci
->symlinks
);
199 /* Ok, need to find a minor that is not in use.
200 * If the device name is in a 'standard' format,
201 * intuit the minor from that, else
202 * easiest to read /proc/mdstat, and hunt through for
206 /* need to pick an unused number */
207 mdlist
= mdstat_read(0, 0);
208 /* Choose a large number. Start from 127 and search down,
209 * but if nothing is found, start really big
211 for (num
= 127 ; num
!= 128 ; num
= num
? num
-1 : (1<<22)-1) {
212 struct mdstat_ent
*me
;
214 if (major
!= MD_MAJOR
)
217 for (me
=mdlist
; me
; me
=me
->next
)
218 if (me
->devnum
== devnum
)
221 /* doesn't exist in mdstat.
222 * make sure it is new to /dev too
225 if (major
!= MD_MAJOR
)
226 minor
= num
<< MdpMinorShift
;
229 dn
= map_dev(major
,minor
, 0);
230 if (dn
==NULL
|| is_standard(dn
, NULL
)) {
231 /* this number only used by a 'standard' name,
232 * so it is safe to use
238 } else if (major
== MD_MAJOR
)
241 minor
= num
<< MdpMinorShift
;
242 /* major and minor have been chosen */
244 /* If it was a 'standard' name and it is in-use, then
245 * the device could already be correct
247 if (stb
.st_mode
&& major(stb
.st_rdev
) == major
&&
248 minor(stb
.st_rdev
) == minor
)
251 if (major(makedev(major
,minor
)) != major
||
252 minor(makedev(major
,minor
)) != minor
) {
253 fprintf(stderr
, Name
": Need newer C library to use more than 4 partitionable md devices, sorry\n");
259 if (strncmp(dev
, "/dev/md/", 8) == 0) {
260 if (mkdir("/dev/md",0700)==0) {
261 chown("/dev/md", ci
->uid
, ci
->gid
);
262 chmod("/dev/md", ci
->mode
| ((ci
->mode
>>2) & 0111));
265 if (mknod(dev
, S_IFBLK
|0600, makedev(major
, minor
))!= 0) {
266 fprintf(stderr
, Name
": failed to create %s\n", dev
);
270 if (chown(dev
, stb
.st_uid
, stb
.st_gid
))
272 if (chmod(dev
, stb
.st_mode
& 07777))
275 if (chown(dev
, ci
->uid
, ci
->gid
))
277 if (chmod(dev
, ci
->mode
))
281 add_dev(dev
, &stb
, 0, NULL
);
282 if (ci
->symlinks
&& strncmp(dev
, "/dev/md/", 8) == 0)
283 make_dev_symlink(dev
);
284 if (major
!= MD_MAJOR
)
285 make_parts(dev
,parts
, ci
->symlinks
);
288 mdfd
= open(dev
, O_RDWR
, 0);
290 fprintf(stderr
, Name
": error opening %s: %s\n",
291 dev
, strerror(errno
));
292 else if (md_get_version(mdfd
) <= 0) {
293 fprintf(stderr
, Name
": %s does not appear to be an md device\n",