]>
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
47 if (symlink(dev
+5, new))
52 void make_parts(char *dev
, int cnt
, int symlinks
)
54 /* make 'cnt' partition devices for 'dev'
55 * We use the major/minor from dev and add 1..cnt
56 * If dev ends with a digit, we add "p%d" else "%d"
57 * If the name exists, we use it's owner/mode,
61 int major_num
, minor_num
;
63 int nlen
= strlen(dev
) + 20;
64 char *name
= malloc(nlen
);
65 int dig
= isdigit(dev
[strlen(dev
)-1]);
68 if (stat(dev
, &stb
)!= 0)
70 if (!S_ISBLK(stb
.st_mode
))
72 major_num
= major(stb
.st_rdev
);
73 minor_num
= minor(stb
.st_rdev
);
74 for (i
=1; i
<= cnt
; i
++) {
76 snprintf(name
, nlen
, "%s%s%d", dev
, dig
?"p":"", i
);
77 if (stat(name
, &stb2
)==0) {
78 if (!S_ISBLK(stb2
.st_mode
))
80 if (stb2
.st_rdev
== makedev(major_num
, minor_num
+i
))
86 if (mknod(name
, S_IFBLK
| 0600, makedev(major_num
, minor_num
+i
)))
88 if (chown(name
, stb2
.st_uid
, stb2
.st_gid
))
90 if (chmod(name
, stb2
.st_mode
& 07777))
92 if (symlinks
&& strncmp(name
, "/dev/md/", 8) == 0)
93 make_dev_symlink(name
);
95 add_dev(name
, &stb2
, 0, NULL
);
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.
106 * If it now doesn't exist, we find a new md array and create the device.
107 * Default ownership/mode comes from config file.
109 int open_mddev(char *dev
, int autof
)
113 int major_num
= MD_MAJOR
;
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 /* check major number is correct */
139 std
= is_standard(dev
, &num
);
140 if (std
>0) major_num
= get_mdp_major();
142 case 2: /* only create is_standard names */
143 if (!std
&& !stb
.st_mode
) {
145 ": %s does not exist and is not a 'standard' name "
146 "so it cannot be created\n", dev
);
150 case 3: /* create md, reject std>0 */
152 fprintf(stderr
, Name
": that --auto option "
153 "not compatable with device named %s\n", dev
);
157 case 4: /* create mdp, reject std<0 */
159 fprintf(stderr
, Name
": that --auto option "
160 "not compatable with device named %s\n", dev
);
163 major_num
= get_mdp_major();
165 case 5: /* default to md if not standard */
167 case 6: /* default to mdp if not standard */
168 if (std
== 0) major_num
= get_mdp_major();
171 /* major is final. num is -1 if not standard */
172 if (stb
.st_mode
&& major(stb
.st_rdev
) != major_num
)
174 if (stb
.st_mode
&& !must_remove
) {
175 /* looks ok, see if it is available */
176 mdfd
= open(dev
, O_RDWR
);
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 (major_num
!= MD_MAJOR
&& parts
> 0)
188 make_parts(dev
, parts
, ci
->symlinks
);
191 /* Ok, need to find a minor that is not in use.
192 * If the device name is in a 'standard' format,
193 * intuit the minor from that, else
194 * easiest to read /proc/mdstat, and hunt through for
198 /* need to pick an unused number */
199 int num
= find_free_devnum(major_num
!= MD_MAJOR
);
201 if (major_num
== MD_MAJOR
)
204 minor_num
= (-1-num
) << MdpMinorShift
;
205 } else if (major_num
== MD_MAJOR
)
208 minor_num
= num
<< MdpMinorShift
;
209 /* major and minor have been chosen */
211 /* If it was a 'standard' name and it is in-use, then
212 * the device could already be correct
214 if (stb
.st_mode
&& major(stb
.st_rdev
) == major_num
&&
215 minor(stb
.st_rdev
) == minor_num
)
218 if (major(makedev(major_num
,minor_num
)) != major_num
||
219 minor(makedev(major_num
,minor_num
)) != minor_num
) {
220 fprintf(stderr
, Name
": Need newer C library to use more than 4 partitionable md devices, sorry\n");
226 if (strncmp(dev
, "/dev/md/", 8) == 0) {
227 if (mkdir("/dev/md",0700)==0) {
228 if (chown("/dev/md", ci
->uid
, ci
->gid
))
229 perror("chown /dev/md");
230 if (chmod("/dev/md", ci
->mode
| ((ci
->mode
>>2) & 0111)))
231 perror("chmod /dev/md");
234 if (mknod(dev
, S_IFBLK
|0600, makedev(major_num
, minor_num
))!= 0) {
235 fprintf(stderr
, Name
": failed to create %s\n", dev
);
239 if (chown(dev
, stb
.st_uid
, stb
.st_gid
))
241 if (chmod(dev
, stb
.st_mode
& 07777))
244 if (chown(dev
, ci
->uid
, ci
->gid
))
246 if (chmod(dev
, ci
->mode
))
250 add_dev(dev
, &stb
, 0, NULL
);
251 if (ci
->symlinks
&& strncmp(dev
, "/dev/md/", 8) == 0)
252 make_dev_symlink(dev
);
253 if (major_num
!= MD_MAJOR
)
254 make_parts(dev
,parts
, ci
->symlinks
);
257 mdfd
= open(dev
, O_RDWR
);
259 fprintf(stderr
, Name
": error opening %s: %s\n",
260 dev
, strerror(errno
));
261 else if (md_get_version(mdfd
) <= 0) {
262 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
271 int open_mddev_devnum(char *devname
, int devnum
, char *name
,
272 char *chosen_name
, int parts
)
274 /* Open the md device with number 'devnum', possibly using 'devname',
275 * possibly constructing a name with 'name', but in any case, copying
276 * the name into 'chosen_name'
278 int major_num
, minor_num
;
281 struct createinfo
*ci
= conf_get_create_info();
284 strcpy(chosen_name
, devname
);
285 else if (name
&& *name
&& strchr(name
,'/') == NULL
) {
286 char *n
= strchr(name
, ':');
287 if (n
) n
++; else n
= name
;
288 if (isdigit(*n
) && devnum
< 0)
289 sprintf(chosen_name
, "/dev/md/d%s", n
);
291 sprintf(chosen_name
, "/dev/md/%s", n
);
294 sprintf(chosen_name
, "/dev/md%d", devnum
);
296 sprintf(chosen_name
, "/dev/md/d%d", -1-devnum
);
299 major_num
= MD_MAJOR
;
302 major_num
= get_mdp_major();
303 minor_num
= (-1-devnum
) << 6;
305 if (stat(chosen_name
, &stb
) == 0) {
306 /* It already exists. Check it is right. */
307 if ( ! S_ISBLK(stb
.st_mode
) ||
308 stb
.st_rdev
!= makedev(major_num
, minor_num
)) {
313 /* special case: if --incremental is suggesting a name
314 * in /dev/md/, we make sure the directory exists.
316 if (strncmp(chosen_name
, "/dev/md/", 8) == 0) {
317 if (mkdir("/dev/md",0700)==0) {
318 if (chown("/dev/md", ci
->uid
, ci
->gid
))
319 perror("chown /dev/md");
320 if (chmod("/dev/md", ci
->mode
|
321 ((ci
->mode
>>2) & 0111)))
322 perror("chmod /dev/md");
326 if (mknod(chosen_name
, S_IFBLK
| 0600,
327 makedev(major_num
, minor_num
)) != 0) {
330 /* FIXME chown/chmod ?? */
333 /* Simple locking to avoid --incr being called for the same
334 * array multiple times in parallel.
336 for (i
= 0; i
< 25 ; i
++) {
339 fd
= open(chosen_name
, O_RDWR
|O_EXCL
);
340 if (fd
>= 0 || errno
!= EBUSY
) {
342 make_parts(chosen_name
, parts
, ci
->symlinks
);