]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdopen.c
When updating uuid, update the bitmap as well - external bitmaps
[thirdparty/mdadm.git] / mdopen.c
CommitLineData
b5e64645
NB
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
34void 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
8d80900b 38 * If dev ends with a digit, we add "p%d" else "%d"
b5e64645
NB
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;
8f23b0b3
NB
45 int nlen = strlen(dev) + 20;
46 char *name = malloc(nlen);
b5e64645
NB
47 int dig = isdigit(dev[strlen(dev)-1]);
48
49 if (stat(dev, &stb)!= 0)
50 return;
51 if (!S_ISBLK(stb.st_mode))
52 return;
0df46c2a
NB
53 major = major(stb.st_rdev);
54 minor = minor(stb.st_rdev);
b5e64645
NB
55 for (i=1; i <= cnt ; i++) {
56 struct stat stb2;
8f23b0b3 57 snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
b5e64645
NB
58 if (stat(name, &stb2)==0) {
59 if (!S_ISBLK(stb2.st_mode))
60 continue;
0df46c2a 61 if (stb2.st_rdev == makedev(major, minor+i))
b5e64645
NB
62 continue;
63 unlink(name);
64 } else {
65 stb2 = stb;
66 }
1e0d770c
NB
67 if (mknod(name, S_IFBLK | 0600, makedev(major, minor+i)))
68 perror("mknod");
69 if (chown(name, stb2.st_uid, stb2.st_gid))
70 perror("chown");
71 if (chmod(name, stb2.st_mode & 07777))
72 perror("chmod");
173fc515
NB
73 stat(name, &stb2);
74 add_dev(name, &stb2, 0, NULL);
b5e64645
NB
75 }
76}
77
78/*
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.
8d80900b 84 * If it now doesn't exist, we find a new md array and create the device.
5bbb4842 85 * Default ownership/mode comes from config file.
b5e64645
NB
86 */
87int open_mddev(char *dev, int autof)
88{
89 int mdfd;
90 struct stat stb;
91 int major = MD_MAJOR;
a0ef61bf 92 int minor = 0;
b5e64645
NB
93 int must_remove = 0;
94 struct mdstat_ent *mdlist;
95 int num;
5bbb4842 96 struct createinfo *ci = conf_get_create_info(NULL);
b5e64645 97
5bbb4842
NB
98 if (autof == 0)
99 autof = ci->autof;
100
101 if (autof && autof != -3) {
b5e64645
NB
102 /* autof is set, so we need to check that the name is ok,
103 * and possibly create one if not
104 */
48f7b27a
NB
105 if (autof == -2 && !is_standard(dev, NULL)) {
106 fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
107 return -1;
108 }
b5e64645 109 stb.st_mode = 0;
0bbc98b5 110 if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) {
b5e64645
NB
111 fprintf(stderr, Name ": %s is not a block device.\n",
112 dev);
113 return -1;
114 }
115 /* check major number is correct */
116 if (autof>0)
117 major = get_mdp_major();
0df46c2a 118 if (stb.st_mode && major(stb.st_rdev) != major)
b5e64645
NB
119 must_remove = 1;
120 if (stb.st_mode && !must_remove) {
121 mdu_array_info_t array;
122 /* looks ok, see if it is available */
123 mdfd = open(dev, O_RDWR, 0);
124 if (mdfd < 0) {
125 fprintf(stderr, Name ": error opening %s: %s\n",
126 dev, strerror(errno));
127 return -1;
128 } else if (md_get_version(mdfd) <= 0) {
129 fprintf(stderr, Name ": %s does not appear to be an md device\n",
130 dev);
131 close(mdfd);
132 return -1;
133 }
134 if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) {
135 /* already active */
136 must_remove = 1;
137 close(mdfd);
138 } else {
139 if (autof > 0)
140 make_parts(dev, autof);
141 return mdfd;
142 }
143 }
144 /* Ok, need to find a minor that is not in use.
8d80900b
NB
145 * If the device name is in a 'standard' format,
146 * intuit the minor from that, else
147 * easiest to read /proc/mdstat, and hunt through for
b5e64645
NB
148 * an unused number
149 */
8d80900b
NB
150 switch(is_standard(dev, &num)) {
151 case -1: /* non partitioned */
152 if (autof > 0) {
153 fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
154 return -1;
155 }
156 minor = num;
157 num = -1-num;
158 break;
159 case 1: /* partitioned */
160 if (autof == -1) {
161 fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
162 return -1;
163 }
164 minor = num << MdpMinorShift;
165 major = get_mdp_major();
166 break;
167 case 0: /* not standard, pick an unused number */
22a88995 168 mdlist = mdstat_read(0, 0);
8d80900b
NB
169 for (num= (autof>0)?-1:0 ; ; num+= (autof>2)?-1:1) {
170 struct mdstat_ent *me;
171 for (me=mdlist; me; me=me->next)
172 if (me->devnum == num)
173 break;
174 if (!me) {
175 /* doesn't exist if mdstat.
176 * make sure it is new to /dev too
b5e64645 177 */
8d80900b
NB
178 char *dn;
179 if (autof > 0)
180 minor = (-1-num) << MdpMinorShift;
181 else
182 minor = num;
16c6fa80 183 dn = map_dev(major,minor, 0);
8d80900b
NB
184 if (dn==NULL || is_standard(dn, NULL)) {
185 /* this number only used by a 'standard' name,
186 * so it is safe to use
187 */
188 break;
189 }
b5e64645
NB
190 }
191 }
192 }
8d80900b
NB
193 /* major and minor have been chosen */
194
195 /* If it was a 'standard' name and it is in-use, then
196 * the device could already be correct
197 */
0df46c2a
NB
198 if (stb.st_mode && major(stb.st_rdev) == major &&
199 minor(stb.st_rdev) == minor)
8d80900b
NB
200 ;
201 else {
0df46c2a
NB
202 if (major(makedev(major,minor)) != major ||
203 minor(makedev(major,minor)) != minor) {
204 fprintf(stderr, Name ": Need newer C library to use more than 4 partitionable md devices, sorry\n");
205 return -1;
206 }
8d80900b
NB
207 if (must_remove)
208 unlink(dev);
209
0df46c2a 210 if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) {
8d80900b 211 fprintf(stderr, Name ": failed to create %s\n", dev);
b5e64645
NB
212 return -1;
213 }
8d80900b 214 if (must_remove) {
1e0d770c
NB
215 if (chown(dev, stb.st_uid, stb.st_gid))
216 perror("chown");
217 if (chmod(dev, stb.st_mode & 07777))
218 perror("chmod");
5bbb4842
NB
219 } else {
220 if (chown(dev, ci->uid, ci->gid))
221 perror("chown");
222 if (chmod(dev, ci->mode))
223 perror("chmod");
8d80900b 224 }
173fc515
NB
225 stat(dev, &stb);
226 add_dev(dev, &stb, 0, NULL);
8d80900b 227 make_parts(dev,autof);
b5e64645 228 }
b5e64645
NB
229 }
230 mdfd = open(dev, O_RDWR, 0);
231 if (mdfd < 0)
232 fprintf(stderr, Name ": error opening %s: %s\n",
233 dev, strerror(errno));
234 else if (md_get_version(mdfd) <= 0) {
235 fprintf(stderr, Name ": %s does not appear to be an md device\n",
236 dev);
237 close(mdfd);
238 mdfd = -1;
239 }
240 return mdfd;
241}
242