]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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]);
49 if (stat(dev
, &stb
)!= 0)
51 if (!S_ISBLK(stb
.st_mode
))
53 major
= major(stb
.st_rdev
);
54 minor
= minor(stb
.st_rdev
);
55 for (i
=1; i
<= cnt
; i
++) {
57 snprintf(name
, nlen
, "%s%s%d", dev
, dig
?"p":"", i
);
58 if (stat(name
, &stb2
)==0) {
59 if (!S_ISBLK(stb2
.st_mode
))
61 if (stb2
.st_rdev
== makedev(major
, minor
+i
))
67 if (mknod(name
, S_IFBLK
| 0600, makedev(major
, minor
+i
)))
69 if (chown(name
, stb2
.st_uid
, stb2
.st_gid
))
71 if (chmod(name
, stb2
.st_mode
& 07777))
74 add_dev(name
, &stb2
, 0, NULL
);
79 * Open a given md device, and check that it really is one.
80 * If 'autof' is given, then we need to create, or recreate, the md device.
81 * If the name already exists, and is not a block device, we fail.
82 * If it exists and is not an md device, is not the right type (partitioned or not),
83 * or is currently in-use, we remove the device, but remember the owner and mode.
84 * If it now doesn't exist, we find a new md array and create the device.
85 * Default ownership is user=0, group=0 perm=0600
87 int open_mddev(char *dev
, int autof
)
94 struct mdstat_ent
*mdlist
;
98 /* autof is set, so we need to check that the name is ok,
99 * and possibly create one if not
101 if (autof
== -2 && !is_standard(dev
, NULL
)) {
102 fprintf(stderr
, Name
": --auto=yes requires a 'standard' md device name, not %s\n", dev
);
106 if (stat(dev
, &stb
)==0 && ! S_ISBLK(stb
.st_mode
)) {
107 fprintf(stderr
, Name
": %s is not a block device.\n",
111 /* check major number is correct */
113 major
= get_mdp_major();
114 if (stb
.st_mode
&& major(stb
.st_rdev
) != major
)
116 if (stb
.st_mode
&& !must_remove
) {
117 mdu_array_info_t array
;
118 /* looks ok, see if it is available */
119 mdfd
= open(dev
, O_RDWR
, 0);
121 fprintf(stderr
, Name
": error opening %s: %s\n",
122 dev
, strerror(errno
));
124 } else if (md_get_version(mdfd
) <= 0) {
125 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
130 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)==0) {
136 make_parts(dev
, autof
);
140 /* Ok, need to find a minor that is not in use.
141 * If the device name is in a 'standard' format,
142 * intuit the minor from that, else
143 * easiest to read /proc/mdstat, and hunt through for
146 switch(is_standard(dev
, &num
)) {
147 case -1: /* non partitioned */
149 fprintf(stderr
, Name
": that --auto option not compatable with device named %s\n", dev
);
155 case 1: /* partitioned */
157 fprintf(stderr
, Name
": that --auto option not compatable with device named %s\n", dev
);
160 minor
= num
<< MdpMinorShift
;
161 major
= get_mdp_major();
163 case 0: /* not standard, pick an unused number */
164 mdlist
= mdstat_read(0, 0);
165 for (num
= (autof
>0)?-1:0 ; ; num
+= (autof
>2)?-1:1) {
166 struct mdstat_ent
*me
;
167 for (me
=mdlist
; me
; me
=me
->next
)
168 if (me
->devnum
== num
)
171 /* doesn't exist if mdstat.
172 * make sure it is new to /dev too
176 minor
= (-1-num
) << MdpMinorShift
;
179 dn
= map_dev(major
,minor
);
180 if (dn
==NULL
|| is_standard(dn
, NULL
)) {
181 /* this number only used by a 'standard' name,
182 * so it is safe to use
189 /* major and minor have been chosen */
191 /* If it was a 'standard' name and it is in-use, then
192 * the device could already be correct
194 if (stb
.st_mode
&& major(stb
.st_rdev
) == major
&&
195 minor(stb
.st_rdev
) == minor
)
198 if (major(makedev(major
,minor
)) != major
||
199 minor(makedev(major
,minor
)) != minor
) {
200 fprintf(stderr
, Name
": Need newer C library to use more than 4 partitionable md devices, sorry\n");
206 if (mknod(dev
, S_IFBLK
|0600, makedev(major
, minor
))!= 0) {
207 fprintf(stderr
, Name
": failed to create %s\n", dev
);
211 if (chown(dev
, stb
.st_uid
, stb
.st_gid
))
213 if (chmod(dev
, stb
.st_mode
& 07777))
217 add_dev(dev
, &stb
, 0, NULL
);
218 make_parts(dev
,autof
);
221 mdfd
= open(dev
, O_RDWR
, 0);
223 fprintf(stderr
, Name
": error opening %s: %s\n",
224 dev
, strerror(errno
));
225 else if (md_get_version(mdfd
) <= 0) {
226 fprintf(stderr
, Name
": %s does not appear to be an md device\n",