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