]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdopen.c
Add device files created with --auto to list of known device files.
[thirdparty/mdadm.git] / mdopen.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
34 void 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
38 * If dev ends with a digit, we add "p%d" else "%d"
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;
45 char *name = malloc(strlen(dev) + 20);
46 int dig = isdigit(dev[strlen(dev)-1]);
47
48 if (stat(dev, &stb)!= 0)
49 return;
50 if (!S_ISBLK(stb.st_mode))
51 return;
52 major = major(stb.st_rdev);
53 minor = minor(stb.st_rdev);
54 for (i=1; i <= cnt ; i++) {
55 struct stat stb2;
56 sprintf(name, "%s%s%d", dev, dig?"p":"", i);
57 if (stat(name, &stb2)==0) {
58 if (!S_ISBLK(stb2.st_mode))
59 continue;
60 if (stb2.st_rdev == makedev(major, minor+i))
61 continue;
62 unlink(name);
63 } else {
64 stb2 = stb;
65 }
66 mknod(name, S_IFBLK | 0600, makedev(major, minor+i));
67 chown(name, stb2.st_uid, stb2.st_gid);
68 chmod(name, stb2.st_mode & 07777);
69 stat(name, &stb2);
70 add_dev(name, &stb2, 0, NULL);
71 }
72 }
73
74 /*
75 * Open a given md device, and check that it really is one.
76 * If 'autof' is given, then we need to create, or recreate, the md device.
77 * If the name already exists, and is not a block device, we fail.
78 * If it exists and is not an md device, is not the right type (partitioned or not),
79 * or is currently in-use, we remove the device, but remember the owner and mode.
80 * If it now doesn't exist, we find a new md array and create the device.
81 * Default ownership is user=0, group=0 perm=0600
82 */
83 int open_mddev(char *dev, int autof)
84 {
85 int mdfd;
86 struct stat stb;
87 int major = MD_MAJOR;
88 int minor = 0;
89 int must_remove = 0;
90 struct mdstat_ent *mdlist;
91 int num;
92
93 if (autof) {
94 /* autof is set, so we need to check that the name is ok,
95 * and possibly create one if not
96 */
97 if (autof == -2 && !is_standard(dev, NULL)) {
98 fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
99 return -1;
100 }
101 stb.st_mode = 0;
102 if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) {
103 fprintf(stderr, Name ": %s is not a block device.\n",
104 dev);
105 return -1;
106 }
107 /* check major number is correct */
108 if (autof>0)
109 major = get_mdp_major();
110 if (stb.st_mode && major(stb.st_rdev) != major)
111 must_remove = 1;
112 if (stb.st_mode && !must_remove) {
113 mdu_array_info_t array;
114 /* looks ok, see if it is available */
115 mdfd = open(dev, O_RDWR, 0);
116 if (mdfd < 0) {
117 fprintf(stderr, Name ": error opening %s: %s\n",
118 dev, strerror(errno));
119 return -1;
120 } else if (md_get_version(mdfd) <= 0) {
121 fprintf(stderr, Name ": %s does not appear to be an md device\n",
122 dev);
123 close(mdfd);
124 return -1;
125 }
126 if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) {
127 /* already active */
128 must_remove = 1;
129 close(mdfd);
130 } else {
131 if (autof > 0)
132 make_parts(dev, autof);
133 return mdfd;
134 }
135 }
136 /* Ok, need to find a minor that is not in use.
137 * If the device name is in a 'standard' format,
138 * intuit the minor from that, else
139 * easiest to read /proc/mdstat, and hunt through for
140 * an unused number
141 */
142 switch(is_standard(dev, &num)) {
143 case -1: /* non partitioned */
144 if (autof > 0) {
145 fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
146 return -1;
147 }
148 minor = num;
149 num = -1-num;
150 break;
151 case 1: /* partitioned */
152 if (autof == -1) {
153 fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
154 return -1;
155 }
156 minor = num << MdpMinorShift;
157 major = get_mdp_major();
158 break;
159 case 0: /* not standard, pick an unused number */
160 mdlist = mdstat_read(0);
161 for (num= (autof>0)?-1:0 ; ; num+= (autof>2)?-1:1) {
162 struct mdstat_ent *me;
163 for (me=mdlist; me; me=me->next)
164 if (me->devnum == num)
165 break;
166 if (!me) {
167 /* doesn't exist if mdstat.
168 * make sure it is new to /dev too
169 */
170 char *dn;
171 if (autof > 0)
172 minor = (-1-num) << MdpMinorShift;
173 else
174 minor = num;
175 dn = map_dev(major,minor);
176 if (dn==NULL || is_standard(dn, NULL)) {
177 /* this number only used by a 'standard' name,
178 * so it is safe to use
179 */
180 break;
181 }
182 }
183 }
184 }
185 /* major and minor have been chosen */
186
187 /* If it was a 'standard' name and it is in-use, then
188 * the device could already be correct
189 */
190 if (stb.st_mode && major(stb.st_rdev) == major &&
191 minor(stb.st_rdev) == minor)
192 ;
193 else {
194 if (major(makedev(major,minor)) != major ||
195 minor(makedev(major,minor)) != minor) {
196 fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n");
197 return -1;
198 }
199 if (must_remove)
200 unlink(dev);
201
202 if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) {
203 fprintf(stderr, Name ": failed to create %s\n", dev);
204 return -1;
205 }
206 if (must_remove) {
207 chown(dev, stb.st_uid, stb.st_gid);
208 chmod(dev, stb.st_mode & 07777);
209 }
210 stat(dev, &stb);
211 add_dev(dev, &stb, 0, NULL);
212 make_parts(dev,autof);
213 }
214 }
215 mdfd = open(dev, O_RDWR, 0);
216 if (mdfd < 0)
217 fprintf(stderr, Name ": error opening %s: %s\n",
218 dev, strerror(errno));
219 else if (md_get_version(mdfd) <= 0) {
220 fprintf(stderr, Name ": %s does not appear to be an md device\n",
221 dev);
222 close(mdfd);
223 mdfd = -1;
224 }
225 return mdfd;
226 }
227