]>
Commit | Line | Data |
---|---|---|
e5329c37 NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2004 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 | #include "mdadm.h" | |
30 | #include "dlink.h" | |
31 | ||
32 | #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN) | |
33 | #error no endian defined | |
34 | #endif | |
35 | #include "md_u.h" | |
36 | #include "md_p.h" | |
37 | ||
38 | int Grow_Add_device(char *devname, int fd, char *newdev) | |
39 | { | |
40 | /* Add a device to an active array. | |
41 | * Currently, just extend a linear array. | |
42 | * This requires writing a new superblock on the | |
43 | * new device, calling the kernel to add the device, | |
44 | * and if that succeeds, update the superblock on | |
45 | * all other devices. | |
46 | * This means that we need to *find* all other devices. | |
47 | */ | |
4b1ac34b NB |
48 | struct mdinfo info; |
49 | ||
50 | void *super = NULL; | |
e5329c37 NB |
51 | struct stat stb; |
52 | int nfd, fd2; | |
53 | int d, nd; | |
82d9eba6 | 54 | struct supertype *st = NULL; |
e5329c37 NB |
55 | |
56 | ||
4b1ac34b | 57 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
58 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
59 | return 1; | |
60 | } | |
61 | ||
82d9eba6 NB |
62 | st = super_by_version(info.array.major_version, info.array.minor_version); |
63 | if (!st) { | |
f9ce90ba NB |
64 | fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version); |
65 | return 1; | |
66 | } | |
67 | ||
4b1ac34b | 68 | if (info.array.level != -1) { |
e5329c37 NB |
69 | fprintf(stderr, Name ": can only add devices to linear arrays\n"); |
70 | return 1; | |
71 | } | |
72 | ||
73 | nfd = open(newdev, O_RDWR|O_EXCL); | |
74 | if (nfd < 0) { | |
75 | fprintf(stderr, Name ": cannot open %s\n", newdev); | |
76 | return 1; | |
77 | } | |
78 | fstat(nfd, &stb); | |
79 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
80 | fprintf(stderr, Name ": %s is not a block device!\n", newdev); | |
81 | close(nfd); | |
82 | return 1; | |
83 | } | |
84 | /* now check out all the devices and make sure we can read the superblock */ | |
4b1ac34b | 85 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
86 | mdu_disk_info_t disk; |
87 | char *dv; | |
88 | ||
89 | disk.number = d; | |
90 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
91 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
92 | d); | |
93 | return 1; | |
94 | } | |
95 | dv = map_dev(disk.major, disk.minor); | |
96 | if (!dv) { | |
97 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
98 | d); | |
99 | return 1; | |
100 | } | |
101 | fd2 = open(dv, O_RDWR); | |
102 | if (!fd2) { | |
103 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
104 | return 1; | |
105 | } | |
4b1ac34b NB |
106 | if (super) free(super); |
107 | super= NULL; | |
82d9eba6 | 108 | if (st->ss->load_super(st, fd2, &super, NULL)) { |
e5329c37 NB |
109 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
110 | close(fd2); | |
111 | return 1; | |
112 | } | |
113 | close(fd2); | |
114 | } | |
115 | /* Ok, looks good. Lets update the superblock and write it out to | |
116 | * newdev. | |
117 | */ | |
118 | ||
4b1ac34b NB |
119 | info.disk.number = d; |
120 | info.disk.major = major(stb.st_rdev); | |
121 | info.disk.minor = minor(stb.st_rdev); | |
122 | info.disk.raid_disk = d; | |
123 | info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); | |
82d9eba6 | 124 | st->ss->update_super(&info, super, "grow", newdev, 0); |
e5329c37 | 125 | |
96395475 | 126 | if (st->ss->store_super(st, nfd, super)) { |
e5329c37 NB |
127 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", newdev); |
128 | close(nfd); | |
129 | return 1; | |
130 | } | |
e5329c37 | 131 | close(nfd); |
4b1ac34b NB |
132 | |
133 | if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) { | |
e5329c37 NB |
134 | fprintf(stderr, Name ": Cannot add new disk to this array\n"); |
135 | return 1; | |
136 | } | |
137 | /* Well, that seems to have worked. | |
138 | * Now go through and update all superblocks | |
139 | */ | |
140 | ||
4b1ac34b | 141 | if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) { |
e5329c37 NB |
142 | fprintf(stderr, Name ": cannot get array info for %s\n", devname); |
143 | return 1; | |
144 | } | |
145 | ||
146 | nd = d; | |
4b1ac34b | 147 | for (d=0 ; d < info.array.raid_disks ; d++) { |
e5329c37 NB |
148 | mdu_disk_info_t disk; |
149 | char *dv; | |
150 | ||
151 | disk.number = d; | |
152 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
153 | fprintf(stderr, Name ": cannot get device detail for device %d\n", | |
154 | d); | |
155 | return 1; | |
156 | } | |
157 | dv = map_dev(disk.major, disk.minor); | |
158 | if (!dv) { | |
159 | fprintf(stderr, Name ": cannot find device file for device %d\n", | |
160 | d); | |
161 | return 1; | |
162 | } | |
163 | fd2 = open(dv, O_RDWR); | |
164 | if (fd2 < 0) { | |
165 | fprintf(stderr, Name ": cannot open device file %s\n", dv); | |
166 | return 1; | |
167 | } | |
82d9eba6 | 168 | if (st->ss->load_super(st, fd2, &super, NULL)) { |
e5329c37 NB |
169 | fprintf(stderr, Name ": cannot find super block on %s\n", dv); |
170 | close(fd); | |
171 | return 1; | |
172 | } | |
4b1ac34b NB |
173 | info.array.raid_disks = nd+1; |
174 | info.array.nr_disks = nd+1; | |
175 | info.array.active_disks = nd+1; | |
176 | info.array.working_disks = nd+1; | |
177 | info.disk.number = nd; | |
178 | info.disk.major = major(stb.st_rdev); | |
179 | info.disk.minor = minor(stb.st_rdev); | |
180 | info.disk.raid_disk = nd; | |
181 | info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); | |
82d9eba6 | 182 | st->ss->update_super(&info, super, "grow", dv, 0); |
4b1ac34b | 183 | |
96395475 | 184 | if (st->ss->store_super(st, fd2, super)) { |
e5329c37 NB |
185 | fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv); |
186 | close(fd2); | |
187 | return 1; | |
188 | } | |
189 | close(fd2); | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
f5e166fe | 194 | |
dfd4d8ee | 195 | int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind) |
f5e166fe NB |
196 | { |
197 | /* | |
198 | * First check that array doesn't have a bitmap | |
199 | * Then create the bitmap | |
200 | * Then add it | |
201 | * | |
202 | * For internal bitmaps, we need to check the version, | |
203 | * find all the active devices, and write the bitmap block | |
204 | * to all devices | |
205 | */ | |
206 | mdu_bitmap_file_t bmf; | |
207 | mdu_array_info_t array; | |
208 | struct supertype *st; | |
209 | ||
210 | if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) { | |
211 | if (errno == ENOMEM) | |
212 | fprintf(stderr, Name ": Memory allocation failure.\n"); | |
213 | else | |
214 | fprintf(stderr, Name ": bitmaps not supported by this kernel.\n"); | |
215 | return 1; | |
216 | } | |
217 | if (bmf.pathname[0]) { | |
fe80f49b NB |
218 | if (strcmp(file,"none")==0) { |
219 | if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) { | |
220 | fprintf(stderr, Name ": failed to remove bitmap %s\n", | |
221 | bmf.pathname); | |
222 | return 1; | |
223 | } | |
224 | return 0; | |
225 | } | |
f5e166fe NB |
226 | fprintf(stderr, Name ": %s already has a bitmap (%s)\n", |
227 | devname, bmf.pathname); | |
228 | return 1; | |
229 | } | |
230 | if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) { | |
231 | fprintf(stderr, Name ": cannot get array status for %s\n", devname); | |
232 | return 1; | |
233 | } | |
234 | if (array.state & (1<<MD_SB_BITMAP_PRESENT)) { | |
fe80f49b NB |
235 | if (strcmp(file, "none")==0) { |
236 | array.state &= ~(1<<MD_SB_BITMAP_PRESENT); | |
237 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
238 | fprintf(stderr, Name ": failed to remove internal bitmap.\n"); | |
239 | return 1; | |
240 | } | |
241 | return 0; | |
242 | } | |
f5e166fe NB |
243 | fprintf(stderr, Name ": Internal bitmap already present on %s\n", |
244 | devname); | |
245 | return 1; | |
246 | } | |
247 | st = super_by_version(array.major_version, array.minor_version); | |
248 | if (!st) { | |
249 | fprintf(stderr, Name ": Cannot understand version %d.%d\n", | |
250 | array.major_version, array.minor_version); | |
251 | return 1; | |
252 | } | |
fe80f49b NB |
253 | if (strcmp(file, "none") == 0) { |
254 | fprintf(stderr, Name ": no bitmap found on %s\n", devname); | |
255 | return 1; | |
256 | } else if (strcmp(file, "internal") == 0) { | |
f5e166fe | 257 | int d; |
ea329559 | 258 | for (d=0; d< st->max_devs; d++) { |
f5e166fe NB |
259 | mdu_disk_info_t disk; |
260 | char *dv; | |
261 | disk.number = d; | |
262 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
263 | continue; | |
264 | if (disk.major == 0 && | |
265 | disk.minor == 0) | |
266 | continue; | |
267 | if ((disk.state & (1<<MD_DISK_SYNC))==0) | |
268 | continue; | |
269 | dv = map_dev(disk.major, disk.minor); | |
270 | if (dv) { | |
271 | void *super; | |
272 | int fd2 = open(dv, O_RDWR); | |
273 | if (fd2 < 0) | |
274 | continue; | |
275 | if (st->ss->load_super(st, fd2, &super, NULL)==0) { | |
34163fc7 | 276 | st->ss->add_internal_bitmap(st, super, |
dfd4d8ee | 277 | chunk, delay, write_behind, |
f5e166fe NB |
278 | array.size); |
279 | st->ss->write_bitmap(st, fd2, super); | |
280 | } | |
281 | close(fd2); | |
282 | } | |
283 | } | |
284 | array.state |= (1<<MD_SB_BITMAP_PRESENT); | |
285 | if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) { | |
286 | fprintf(stderr, Name ": failed to set internal bitmap.\n"); | |
287 | return 1; | |
288 | } | |
fe80f49b NB |
289 | } else { |
290 | int uuid[4]; | |
291 | int bitmap_fd; | |
292 | int d; | |
293 | int max_devs = st->max_devs; | |
294 | void *super = NULL; | |
295 | if (chunk == UnSet) | |
296 | chunk = DEFAULT_BITMAP_CHUNK; | |
297 | ||
298 | /* try to load a superblock */ | |
299 | for (d=0; d<max_devs; d++) { | |
300 | mdu_disk_info_t disk; | |
301 | char *dv; | |
302 | int fd2; | |
303 | disk.number = d; | |
304 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
305 | continue; | |
306 | if ((disk.major==0 && disk.minor==0) || | |
307 | (disk.state & (1<<MD_DISK_REMOVED))) | |
308 | continue; | |
309 | dv = map_dev(disk.major, disk.minor); | |
310 | if (!dv) continue; | |
311 | fd2 = open(dv, O_RDONLY); | |
312 | if (fd2 >= 0 && | |
313 | st->ss->load_super(st, fd2, &super, NULL) == 0) { | |
314 | close(fd2); | |
315 | st->ss->uuid_from_super(uuid, super); | |
316 | break; | |
317 | } | |
318 | close(fd2); | |
319 | } | |
320 | if (d == max_devs) { | |
321 | fprintf(stderr, Name ": cannot find UUID for array!\n"); | |
322 | return 1; | |
323 | } | |
324 | if (CreateBitmap(file, 0, (char*)uuid, chunk, | |
325 | delay, write_behind, array.size*2ULL)) { | |
326 | return 1; | |
327 | } | |
328 | bitmap_fd = open(file, O_RDWR); | |
329 | if (bitmap_fd < 0) { | |
330 | fprintf(stderr, Name ": weird: %s cannot be openned\n", | |
331 | file); | |
332 | return 1; | |
333 | } | |
334 | if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) { | |
335 | fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n", | |
336 | devname, strerror(errno)); | |
337 | return 1; | |
338 | } | |
339 | } | |
f5e166fe NB |
340 | |
341 | return 0; | |
342 | } | |
343 | ||
344 |