]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Grow.c
Separate sueprblock handling into separate file
[thirdparty/mdadm.git] / Grow.c
CommitLineData
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
38int 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;
54
55
4b1ac34b 56 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
57 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
58 return 1;
59 }
60
4b1ac34b 61 if (info.array.level != -1) {
e5329c37
NB
62 fprintf(stderr, Name ": can only add devices to linear arrays\n");
63 return 1;
64 }
65
66 nfd = open(newdev, O_RDWR|O_EXCL);
67 if (nfd < 0) {
68 fprintf(stderr, Name ": cannot open %s\n", newdev);
69 return 1;
70 }
71 fstat(nfd, &stb);
72 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
73 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
74 close(nfd);
75 return 1;
76 }
77 /* now check out all the devices and make sure we can read the superblock */
4b1ac34b 78 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
79 mdu_disk_info_t disk;
80 char *dv;
81
82 disk.number = d;
83 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
84 fprintf(stderr, Name ": cannot get device detail for device %d\n",
85 d);
86 return 1;
87 }
88 dv = map_dev(disk.major, disk.minor);
89 if (!dv) {
90 fprintf(stderr, Name ": cannot find device file for device %d\n",
91 d);
92 return 1;
93 }
94 fd2 = open(dv, O_RDWR);
95 if (!fd2) {
96 fprintf(stderr, Name ": cannot open device file %s\n", dv);
97 return 1;
98 }
4b1ac34b
NB
99 if (super) free(super);
100 super= NULL;
101 if (load_super0(fd2, &super, NULL)) {
e5329c37
NB
102 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
103 close(fd2);
104 return 1;
105 }
106 close(fd2);
107 }
108 /* Ok, looks good. Lets update the superblock and write it out to
109 * newdev.
110 */
111
4b1ac34b
NB
112 info.disk.number = d;
113 info.disk.major = major(stb.st_rdev);
114 info.disk.minor = minor(stb.st_rdev);
115 info.disk.raid_disk = d;
116 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
117 update_super0(&info, super, "grow", newdev, 0);
e5329c37 118
4b1ac34b 119 if (store_super0(nfd, super)) {
e5329c37
NB
120 fprintf(stderr, Name ": Cannot store new superblock on %s\n", newdev);
121 close(nfd);
122 return 1;
123 }
e5329c37 124 close(nfd);
4b1ac34b
NB
125
126 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
e5329c37
NB
127 fprintf(stderr, Name ": Cannot add new disk to this array\n");
128 return 1;
129 }
130 /* Well, that seems to have worked.
131 * Now go through and update all superblocks
132 */
133
4b1ac34b 134 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
e5329c37
NB
135 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
136 return 1;
137 }
138
139 nd = d;
4b1ac34b 140 for (d=0 ; d < info.array.raid_disks ; d++) {
e5329c37
NB
141 mdu_disk_info_t disk;
142 char *dv;
143
144 disk.number = d;
145 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
146 fprintf(stderr, Name ": cannot get device detail for device %d\n",
147 d);
148 return 1;
149 }
150 dv = map_dev(disk.major, disk.minor);
151 if (!dv) {
152 fprintf(stderr, Name ": cannot find device file for device %d\n",
153 d);
154 return 1;
155 }
156 fd2 = open(dv, O_RDWR);
157 if (fd2 < 0) {
158 fprintf(stderr, Name ": cannot open device file %s\n", dv);
159 return 1;
160 }
4b1ac34b 161 if (load_super0(fd2, &super, NULL)) {
e5329c37
NB
162 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
163 close(fd);
164 return 1;
165 }
4b1ac34b
NB
166 info.array.raid_disks = nd+1;
167 info.array.nr_disks = nd+1;
168 info.array.active_disks = nd+1;
169 info.array.working_disks = nd+1;
170 info.disk.number = nd;
171 info.disk.major = major(stb.st_rdev);
172 info.disk.minor = minor(stb.st_rdev);
173 info.disk.raid_disk = nd;
174 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
175 update_super0(&info, super, "grow", dv, 0);
176
177 if (store_super0(fd2, super)) {
e5329c37
NB
178 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
179 close(fd2);
180 return 1;
181 }
182 close(fd2);
183 }
184
185 return 0;
186}