]> git.ipfire.org Git - thirdparty/mdadm.git/blame - mdopen.c
mdadm-1.8.0
[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
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 == MKDEV(major, minor+i))
61 continue;
62 unlink(name);
63 } else {
64 stb2 = stb;
65 }
66 mknod(name, S_IFBLK | 0600, MKDEV(major, minor+i));
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.
78 * If it now doesn't exist, we find a few md array and create the device.
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;
86 int minor;
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 */
95 stb.st_mode = 0;
96 if (lstat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) {
97 fprintf(stderr, Name ": %s is not a block device.\n",
98 dev);
99 return -1;
100 }
101 /* check major number is correct */
102 if (autof>0)
103 major = get_mdp_major();
104 if (stb.st_mode && MAJOR(stb.st_rdev) != major)
105 must_remove = 1;
106 if (stb.st_mode && !must_remove) {
107 mdu_array_info_t array;
108 /* looks ok, see if it is available */
109 mdfd = open(dev, O_RDWR, 0);
110 if (mdfd < 0) {
111 fprintf(stderr, Name ": error opening %s: %s\n",
112 dev, strerror(errno));
113 return -1;
114 } else if (md_get_version(mdfd) <= 0) {
115 fprintf(stderr, Name ": %s does not appear to be an md device\n",
116 dev);
117 close(mdfd);
118 return -1;
119 }
120 if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) {
121 /* already active */
122 must_remove = 1;
123 close(mdfd);
124 } else {
125 if (autof > 0)
126 make_parts(dev, autof);
127 return mdfd;
128 }
129 }
130 /* Ok, need to find a minor that is not in use.
131 * Easiest to read /proc/mdstat, and hunt through for
132 * an unused number
133 */
134 mdlist = mdstat_read(0);
135 for (num= (autof>0)?-1:0 ; ; num+= (autof>2)?-1:1) {
136 struct mdstat_ent *me;
137 for (me=mdlist; me; me=me->next)
138 if (me->devnum == num)
139 break;
140 if (!me) {
141 /* doesn't exist if mdstat.
142 * make sure it is new to /dev too
143 */
144 char *dn;
145 if (autof > 0)
146 minor = (-1-num) << MdpMinorShift;
147 else
148 minor = num;
149 dn = map_dev(major,minor);
150 if (dn==NULL || is_standard(dn)) {
151 /* this number only used by a 'standard' name,
152 * so it is safe to use
153 */
154 break;
155 }
156 }
157 }
158 /* 'num' is the number to use, >=0 for md, <0 for mdp */
159 if (must_remove) {
160 /* never remove a device name that ends /mdNN or /dNN,
161 * that would be confusing
162 */
163 if (is_standard(dev)) {
164 fprintf(stderr, Name ": --auto refusing to remove %s as it looks like a standard name.\n",
165 dev);
166 return -1;
167 }
168 unlink(dev);
169 }
170
171 if (mknod(dev, S_IFBLK|0600, MKDEV(major, minor))!= 0) {
172 fprintf(stderr, Name ": failed to create %s\n", dev);
173 return -1;
174 }
175 if (must_remove) {
176 chown(dev, stb.st_uid, stb.st_gid);
177 chmod(dev, stb.st_mode & 07777);
178 }
179 make_parts(dev,autof);
180 }
181 mdfd = open(dev, O_RDWR, 0);
182 if (mdfd < 0)
183 fprintf(stderr, Name ": error opening %s: %s\n",
184 dev, strerror(errno));
185 else if (md_get_version(mdfd) <= 0) {
186 fprintf(stderr, Name ": %s does not appear to be an md device\n",
187 dev);
188 close(mdfd);
189 mdfd = -1;
190 }
191 return mdfd;
192}
193