~ubuntu-branches/ubuntu/hardy/mdadm/hardy-updates

« back to all changes in this revision

Viewing changes to {arch}/++pristine-trees/unlocked/mdadm/mdadm--debian/mdadm--debian--1.12.0/pkg-mdadm-devel@lists.alioth.debian.org--2005/mdadm--debian--1.12.0--patch-2/Build.c

  • Committer: Package Import Robot
  • Author(s): Scott James Remnant
  • Date: 2006-07-11 17:23:21 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20060711172321-070tz7lox9adujtw
Tags: 2.4.1-6ubuntu1
* Merge from debian unstable, remaining changes:
  - integration with initramfs-tools,
  - autocreate devices when udev is in use,
  - use lstat in mdopen.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
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
 
 
36
 
int Build(char *mddev, int mdfd, int chunk, int level, int layout,
37
 
          int raiddisks,
38
 
          mddev_dev_t devlist, int assume_clean)
39
 
{
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 verbose = 0;
54
 
        int i;
55
 
        int vers;
56
 
        struct stat stb;
57
 
        int subdevs = 0;
58
 
        mddev_dev_t dv;
59
 
 
60
 
        /* scan all devices, make sure they really are block devices */
61
 
        for (dv = devlist; dv; dv=dv->next) {
62
 
                if (stat(dv->devname, &stb)) {
63
 
                        fprintf(stderr, Name ": Cannot find %s: %s\n",
64
 
                                dv->devname, strerror(errno));
65
 
                        return 1;
66
 
                }
67
 
                if ((stb.st_mode & S_IFMT) != S_IFBLK) {
68
 
                        fprintf(stderr, Name ": %s is not a block device.\n",
69
 
                                dv->devname);
70
 
                        return 1;
71
 
                }
72
 
                subdevs++;
73
 
        }
74
 
 
75
 
        if (raiddisks != subdevs) {
76
 
                fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
77
 
                        raiddisks, subdevs);
78
 
                return 1;
79
 
        }
80
 
 
81
 
        if (layout == UnSet)
82
 
                switch(level) {
83
 
                default: /* no layout */
84
 
                        layout = 0;
85
 
                        break;
86
 
                case 10:
87
 
                        layout = 0x102; /* near=2, far=1 */
88
 
                        if (verbose)
89
 
                                fprintf(stderr,
90
 
                                        Name ": layout defaults to n1\n");
91
 
                        break;
92
 
                case 5:
93
 
                case 6:
94
 
                        layout = map_name(r5layout, "default");
95
 
                        if (verbose)
96
 
                                fprintf(stderr,
97
 
                                        Name ": layout defaults to %s\n", map_num(r5layout, layout));
98
 
                        break;
99
 
                case LEVEL_FAULTY:
100
 
                        layout = map_name(faultylayout, "default");
101
 
 
102
 
                        if (verbose)
103
 
                                fprintf(stderr,
104
 
                                        Name ": layout defaults to %s\n", map_num(faultylayout, layout));
105
 
                        break;
106
 
                }
107
 
 
108
 
 
109
 
        vers = md_get_version(mdfd);
110
 
        
111
 
        /* looks Ok, go for it */
112
 
        if (vers >= 9000) {
113
 
                mdu_array_info_t array;
114
 
                array.level = level;
115
 
                array.size = 0;
116
 
                array.nr_disks = raiddisks;
117
 
                array.raid_disks = raiddisks;
118
 
                array.md_minor = 0;
119
 
                if (fstat(mdfd, &stb)==0)
120
 
                        array.md_minor = minor(stb.st_rdev);
121
 
                array.not_persistent = 1;
122
 
                array.state = 0; /* not clean, but no errors */
123
 
                if (assume_clean)
124
 
                        array.state |= 1;
125
 
                array.active_disks = raiddisks;
126
 
                array.working_disks = raiddisks;
127
 
                array.spare_disks = 0;
128
 
                array.failed_disks = 0;
129
 
                if (chunk == 0)  
130
 
                        chunk = 64;
131
 
                array.chunk_size = chunk*1024;
132
 
                array.layout = layout;
133
 
                if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
134
 
                        fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
135
 
                                mddev, strerror(errno));
136
 
                        return 1;
137
 
                }
138
 
        }
139
 
        /* now add the devices */
140
 
        for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
141
 
                if (stat(dv->devname, &stb)) {
142
 
                        fprintf(stderr, Name ": Wierd: %s has disappeared.\n",
143
 
                                dv->devname);
144
 
                        goto abort;
145
 
                }
146
 
                if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
147
 
                        fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
148
 
                                dv->devname);
149
 
                        goto abort;
150
 
                }
151
 
                if (vers>= 9000) {
152
 
                        mdu_disk_info_t disk;
153
 
                        disk.number = i;
154
 
                        disk.raid_disk = i;
155
 
                        disk.state = 6;
156
 
                        disk.major = major(stb.st_rdev);
157
 
                        disk.minor = minor(stb.st_rdev);
158
 
                        if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
159
 
                                fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
160
 
                                        dv->devname, strerror(errno));
161
 
                                goto abort;
162
 
                        }
163
 
                } else {
164
 
                        if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
165
 
                                fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
166
 
                                        dv->devname, strerror(errno));
167
 
                                goto abort;
168
 
                        }
169
 
                }
170
 
        }
171
 
        /* now to start it */
172
 
        if (vers >= 9000) {
173
 
                mdu_param_t param; /* not used by syscall */
174
 
                if (ioctl(mdfd, RUN_ARRAY, &param)) {
175
 
                        fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
176
 
                                strerror(errno));
177
 
                        goto abort;
178
 
                }
179
 
        } else {
180
 
                unsigned long arg;
181
 
                arg=0;
182
 
                while (chunk > 4096) {
183
 
                        arg++;
184
 
                        chunk >>= 1;
185
 
                }
186
 
                if (level == 0)
187
 
                        chunk |= 0x20000;
188
 
                else    chunk |= 0x10000;
189
 
                if (ioctl(mdfd, START_MD, arg)) {
190
 
                        fprintf(stderr, Name ": START_MD failed: %s\n",
191
 
                                strerror(errno));
192
 
                        goto abort;
193
 
                }
194
 
        }
195
 
        fprintf(stderr, Name ": array %s built and started.\n",
196
 
                mddev);
197
 
        return 0;
198
 
 
199
 
 abort:
200
 
        if (vers >= 9000)
201
 
            ioctl(mdfd, STOP_ARRAY, 0);
202
 
        else
203
 
            ioctl(mdfd, STOP_MD, 0);
204
 
        return 1;
205
 
                
206
 
}
207