mdadm-1.6.0
[thirdparty/mdadm.git] / Build.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
64c4757e
NB
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
9a9dab36 30#include "mdadm.h"
64c4757e 31
82b27616
NB
32#define REGISTER_DEV _IO (MD_MAJOR, 1)
33#define START_MD _IO (MD_MAJOR, 2)
34#define STOP_MD _IO (MD_MAJOR, 3)
35
64c4757e
NB
36int Build(char *mddev, int mdfd, int chunk, int level,
37 int raiddisks,
dd0781e5 38 mddev_dev_t devlist, int assume_clean)
64c4757e 39{
82b27616
NB
40 /* Build a linear or raid0 arrays without superblocks
41 * We cannot really do any checks, we just do it.
42 * For md_version < 0.90.0, we call REGISTER_DEV
43 * with the device numbers, and then
44 * START_MD giving the "geometry"
45 * geometry is 0xpp00cc
46 * where pp is personality: 1==linear, 2=raid0
47 * cc = chunk size factor: 0==4k, 1==8k etc.
48 *
49 * For md_version >= 0.90.0 we call
50 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
51 *
52 */
53 int i;
54 int vers;
55 struct stat stb;
cd29a5c8
NB
56 int subdevs = 0;
57 mddev_dev_t dv;
82b27616
NB
58
59 /* scan all devices, make sure they really are block devices */
cd29a5c8
NB
60 for (dv = devlist; dv; dv=dv->next) {
61 if (stat(dv->devname, &stb)) {
82b27616 62 fprintf(stderr, Name ": Cannot find %s: %s\n",
cd29a5c8 63 dv->devname, strerror(errno));
82b27616
NB
64 return 1;
65 }
66 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
67 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 68 dv->devname);
82b27616
NB
69 return 1;
70 }
cd29a5c8
NB
71 subdevs++;
72 }
73
74 if (raiddisks != subdevs) {
75 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
76 raiddisks, subdevs);
77 return 1;
82b27616
NB
78 }
79
80 vers = md_get_version(mdfd);
81
82 /* looks Ok, go for it */
cd29a5c8 83 if (vers >= 9000) {
82b27616
NB
84 mdu_array_info_t array;
85 array.level = level;
86 array.size = 0;
87 array.nr_disks = raiddisks;
88 array.raid_disks = raiddisks;
89 array.md_minor = 0;
90 if (fstat(mdfd, &stb)==0)
91 array.md_minor = MINOR(stb.st_rdev);
cd29a5c8 92 array.not_persistent = 1;
82b27616 93 array.state = 0; /* not clean, but no errors */
dd0781e5
NB
94 if (assume_clean)
95 array.state |= 1;
82b27616
NB
96 array.active_disks = raiddisks;
97 array.working_disks = raiddisks;
98 array.spare_disks = 0;
99 array.failed_disks = 0;
cd29a5c8
NB
100 if (chunk == 0)
101 chunk = 64;
82b27616
NB
102 array.chunk_size = chunk*1024;
103 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
104 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
105 mddev, strerror(errno));
106 return 1;
107 }
108 }
109 /* now add the devices */
cd29a5c8
NB
110 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
111 if (stat(dv->devname, &stb)) {
82b27616 112 fprintf(stderr, Name ": Wierd: %s has disappeared.\n",
cd29a5c8 113 dv->devname);
82b27616
NB
114 goto abort;
115 }
cd29a5c8 116 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
82b27616 117 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
cd29a5c8 118 dv->devname);
82b27616
NB
119 goto abort;
120 }
cd29a5c8 121 if (vers>= 9000) {
82b27616
NB
122 mdu_disk_info_t disk;
123 disk.number = i;
124 disk.raid_disk = i;
125 disk.state = 6;
126 disk.major = MAJOR(stb.st_rdev);
127 disk.minor = MINOR(stb.st_rdev);
128 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
129 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
cd29a5c8 130 dv->devname, strerror(errno));
82b27616
NB
131 goto abort;
132 }
133 } else {
134 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
e0d19036 135 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
cd29a5c8 136 dv->devname, strerror(errno));
82b27616
NB
137 goto abort;
138 }
139 }
140 }
141 /* now to start it */
cd29a5c8 142 if (vers >= 9000) {
82b27616 143 mdu_param_t param; /* not used by syscall */
cd29a5c8 144 if (ioctl(mdfd, RUN_ARRAY, &param)) {
82b27616
NB
145 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
146 strerror(errno));
147 goto abort;
148 }
149 } else {
cd29a5c8 150 unsigned long arg;
82b27616
NB
151 arg=0;
152 while (chunk > 4096) {
153 arg++;
154 chunk >>= 1;
155 }
156 if (level == 0)
157 chunk |= 0x20000;
158 else chunk |= 0x10000;
159 if (ioctl(mdfd, START_MD, arg)) {
160 fprintf(stderr, Name ": START_MD failed: %s\n",
161 strerror(errno));
162 goto abort;
163 }
164 }
165 fprintf(stderr, Name ": array %s built and started.\n",
166 mddev);
167 return 0;
168
169 abort:
cd29a5c8 170 if (vers >= 9000)
82b27616
NB
171 ioctl(mdfd, STOP_ARRAY, 0);
172 else
173 ioctl(mdfd, STOP_MD, 0);
174 return 1;
175
64c4757e 176}
82b27616 177