]>
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
34 void make_parts(char *dev
, int cnt
)
36 /* make 'cnt' partition devices for 'dev'
37 * We use the major/minor from dev and add 1..cnt
38 * If dev ends with a digit, we add "p%d" else "%d"
39 * If the name exists, we use it's owner/mode,
45 int nlen
= strlen(dev
) + 20;
46 char *name
= malloc(nlen
);
47 int dig
= isdigit(dev
[strlen(dev
)-1]);
50 if (stat(dev
, &stb
)!= 0)
52 if (!S_ISBLK(stb
.st_mode
))
54 major
= major(stb
.st_rdev
);
55 minor
= minor(stb
.st_rdev
);
56 for (i
=1; i
<= cnt
; i
++) {
58 snprintf(name
, nlen
, "%s%s%d", dev
, dig
?"p":"", i
);
59 if (stat(name
, &stb2
)==0) {
60 if (!S_ISBLK(stb2
.st_mode
))
62 if (stb2
.st_rdev
== makedev(major
, minor
+i
))
68 if (mknod(name
, S_IFBLK
| 0600, makedev(major
, minor
+i
)))
70 if (chown(name
, stb2
.st_uid
, stb2
.st_gid
))
72 if (chmod(name
, stb2
.st_mode
& 07777))
75 add_dev(name
, &stb2
, 0, NULL
);
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.
85 * If it now doesn't exist, we find a new md array and create the device.
86 * Default ownership/mode comes from config file.
88 int open_mddev(char *dev
, int autof
)
95 struct mdstat_ent
*mdlist
;
97 struct createinfo
*ci
= conf_get_create_info(NULL
);
106 if (autof
&& autof
!= 1) {
107 /* autof is set, so we need to check that the name is ok,
108 * and possibly create one if not
112 if (stat(dev
, &stb
)==0 && ! S_ISBLK(stb
.st_mode
)) {
113 fprintf(stderr
, Name
": %s is not a block device.\n",
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
);
121 /* check major number is correct */
123 std
= is_standard(dev
, &num
);
124 if (std
>0) major
= get_mdp_major();
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
);
132 case 3: /* create md, reject std>0 */
134 fprintf(stderr
, Name
": that --auto option not compatable with device named %s\n", dev
);
138 case 4: /* create mdp, reject std<0 */
140 fprintf(stderr
, Name
": that --auto option not compatable with device named %s\n", dev
);
144 case 5: /* default to md if not standard */
146 case 6: /* default to mdp if not standard */
147 if (std
== 0) major
= get_mdp_major();
150 /* major is final. num is -1 if not standard */
151 if (stb
.st_mode
&& major(stb
.st_rdev
) != major
)
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);
158 fprintf(stderr
, Name
": error opening %s: %s\n",
159 dev
, strerror(errno
));
161 } else if (md_get_version(mdfd
) <= 0) {
162 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
167 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)==0) {
172 if (major
!= MD_MAJOR
&& parts
> 0)
173 make_parts(dev
, parts
);
177 /* Ok, need to find a minor that is not in use.
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
184 /* need to pick an unused number */
185 mdlist
= mdstat_read(0, 0);
186 for (num
= 0 ; ; num
++) {
187 struct mdstat_ent
*me
;
189 if (major
!= MD_MAJOR
)
192 for (me
=mdlist
; me
; me
=me
->next
)
193 if (me
->devnum
== devnum
)
196 /* doesn't exist if mdstat.
197 * make sure it is new to /dev too
200 if (major
!= MD_MAJOR
)
201 minor
= num
<< MdpMinorShift
;
204 dn
= map_dev(major
,minor
, 0);
205 if (dn
==NULL
|| is_standard(dn
, NULL
)) {
206 /* this number only used by a 'standard' name,
207 * so it is safe to use
213 } else if (major
== MD_MAJOR
)
216 minor
= num
<< MdpMinorShift
;
217 /* major and minor have been chosen */
219 /* If it was a 'standard' name and it is in-use, then
220 * the device could already be correct
222 if (stb
.st_mode
&& major(stb
.st_rdev
) == major
&&
223 minor(stb
.st_rdev
) == minor
)
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");
234 if (mknod(dev
, S_IFBLK
|0600, makedev(major
, minor
))!= 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 (major
!= MD_MAJOR
)
252 make_parts(dev
,parts
);
255 mdfd
= open(dev
, O_RDWR
, 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",