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

« back to all changes in this revision

Viewing changes to ,,mdadm--debian--1.12.0--patch-1--pkg-mdadm-devel@lists.alioth.debian.org--2005.1123495738.2396.13/mdadm.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
 
#include "md_p.h"
32
 
#include <ctype.h>
33
 
 
34
 
 
35
 
 
36
 
int main(int argc, char *argv[])
37
 
{
38
 
        int mode = 0;
39
 
        int opt;
40
 
        int option_index;
41
 
        char *help_text;
42
 
        char *c;
43
 
        int rv;
44
 
 
45
 
        int chunk = 0;
46
 
        int size = -1;
47
 
        int level = UnSet;
48
 
        int layout = UnSet;
49
 
        int raiddisks = 0;
50
 
        int sparedisks = 0;
51
 
        struct mddev_ident_s ident;
52
 
        char *configfile = NULL;
53
 
        char *cp;
54
 
        char *update = NULL;
55
 
        int scan = 0;
56
 
        char devmode = 0;
57
 
        int runstop = 0;
58
 
        int readonly = 0;
59
 
        int SparcAdjust = 0;
60
 
        mddev_dev_t devlist = NULL;
61
 
        mddev_dev_t *devlistend = & devlist;
62
 
        mddev_dev_t dv;
63
 
        int devs_found = 0;
64
 
        int verbose = 0;
65
 
        int brief = 0;
66
 
        int force = 0;
67
 
        int test = 0;
68
 
        int assume_clean = 0;
69
 
        int autof = 0; /* -2 means create device based on name:
70
 
                        *    if it ends mdN, then non-partitioned array N
71
 
                        *    if it ends dN, then partitions array N
72
 
                        * -1 means create non-partitioned, choose N
73
 
                        *  1 or more to create partitioned
74
 
                        * If -1 or 1 and name is a 'standard' name, then
75
 
                        * insist on a match of type and number.
76
 
                        */
77
 
 
78
 
        char *mailaddr = NULL;
79
 
        char *program = NULL;
80
 
        int delay = 0;
81
 
        int daemonise = 0;
82
 
        char *pidfile = NULL;
83
 
        int oneshot = 0;
84
 
 
85
 
        int copies;
86
 
 
87
 
        int mdfd = -1;
88
 
 
89
 
        ident.uuid_set=0;
90
 
        ident.level = UnSet;
91
 
        ident.raid_disks = UnSet;
92
 
        ident.super_minor= UnSet;
93
 
        ident.devices=0;
94
 
        ident.spare_group = NULL;
95
 
        ident.autof = 0;
96
 
 
97
 
        while ((option_index = -1) ,
98
 
               (opt=getopt_long(argc, argv,
99
 
                                short_options, long_options,
100
 
                                &option_index)) != -1) {
101
 
                int newmode = mode;
102
 
                /* firstly, so mode-independant options */
103
 
                switch(opt) {
104
 
                case 'h':
105
 
                        help_text = Help;
106
 
                        if (option_index > 0 && 
107
 
                            strcmp(long_options[option_index].name, "help-options")==0)
108
 
                                help_text = OptionHelp;
109
 
                        else
110
 
                                switch (mode) {
111
 
                                case ASSEMBLE : help_text = Help_assemble; break;
112
 
                                case BUILD    : help_text = Help_build; break;
113
 
                                case CREATE   : help_text = Help_create; break;
114
 
                                case MANAGE   : help_text = Help_manage; break;
115
 
                                case MISC     : help_text = Help_misc; break;
116
 
                                case MONITOR  : help_text = Help_monitor; break;
117
 
                                case GROW     : help_text = Help_grow; break;
118
 
                                }
119
 
                        fputs(help_text,stderr);
120
 
                        exit(0);
121
 
 
122
 
                case 'V':
123
 
                        fputs(Version, stderr);
124
 
                        exit(0);
125
 
 
126
 
                case 'v': verbose++;
127
 
                        continue;
128
 
 
129
 
                case 'b': brief = 1;
130
 
                        continue;
131
 
 
132
 
                case ':':
133
 
                case '?':
134
 
                        fputs(Usage, stderr);
135
 
                        exit(2);
136
 
                }
137
 
                /* second, figure out the mode.
138
 
                 * Some options force the mode.  Others
139
 
                 * set the mode if it isn't already 
140
 
                 */
141
 
 
142
 
                switch(opt) {
143
 
                case '@': /* just incase they say --manage */
144
 
                        newmode = MANAGE; break;
145
 
                case 'a':
146
 
                case 'r':
147
 
                case 'f':
148
 
                        if (!mode) newmode = MANAGE; 
149
 
                        break;
150
 
 
151
 
                case 'A': newmode = ASSEMBLE; break;
152
 
                case 'B': newmode = BUILD; break;
153
 
                case 'C': newmode = CREATE; break;
154
 
                case 'F': newmode = MONITOR;break;
155
 
                case 'G': newmode = GROW; break;
156
 
 
157
 
                case '#':
158
 
                case 'D':
159
 
                case 'E':
160
 
                case 'Q': newmode = MISC; break;
161
 
                case 'R':
162
 
                case 'S':
163
 
                case 'o':
164
 
                case 'w':
165
 
                case 'K': if (!mode) newmode = MISC; break;
166
 
                }
167
 
                if (mode && newmode == mode) {
168
 
                        /* everybody happy ! */
169
 
                } else if (mode && newmode != mode) {
170
 
                        /* not allowed.. */
171
 
                        fprintf(stderr, Name ": ");
172
 
                        if (option_index >= 0)
173
 
                                fprintf(stderr, "--%s", long_options[option_index].name);
174
 
                        else
175
 
                                fprintf(stderr, "-%c", opt);
176
 
                        fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
177
 
                                map_num(modes, newmode),
178
 
                                map_num(modes, mode));
179
 
                        exit(2);
180
 
                } else if (!mode && newmode) {
181
 
                        mode = newmode;
182
 
                } else {
183
 
                        /* special case of -c --help */
184
 
                        if (opt == 'c' && 
185
 
                            ( strncmp(optarg, "--h", 3)==0 ||
186
 
                              strncmp(optarg, "-h", 2)==0)) {
187
 
                                fputs(Help_config, stderr);
188
 
                                exit(0);
189
 
                        }
190
 
 
191
 
                        /* If first option is a device, don't force the mode yet */
192
 
                        if (opt == 1) {
193
 
                                if (devs_found == 0) {
194
 
                                        dv = malloc(sizeof(*dv));
195
 
                                        if (dv == NULL) {
196
 
                                                fprintf(stderr, Name ": malloc failed\n");
197
 
                                                exit(3);
198
 
                                        }
199
 
                                        dv->devname = optarg;
200
 
                                        dv->disposition = devmode;
201
 
                                        dv->next = NULL;
202
 
                                        *devlistend = dv;
203
 
                                        devlistend = &dv->next;
204
 
                        
205
 
                                        devs_found++;
206
 
                                        continue;
207
 
                                }
208
 
                                /* No mode yet, and this is the second device ... */
209
 
                                fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
210
 
                                exit(2);
211
 
                        }
212
 
                        if (option_index >= 0)
213
 
                                fprintf(stderr, Name ": --%s", long_options[option_index].name);
214
 
                        else
215
 
                                fprintf(stderr, Name ": -%c", opt);
216
 
                        fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
217
 
                        exit(2);
218
 
                }
219
 
 
220
 
                /* if we just set the mode, then done */
221
 
                switch(opt) {
222
 
                case '@':
223
 
                case '#':
224
 
                case 'A':
225
 
                case 'B':
226
 
                case 'C':
227
 
                case 'F':
228
 
                case 'G':
229
 
                        continue;
230
 
                }
231
 
                if (opt == 1) {
232
 
                        /* an undecorated option - must be a device name.
233
 
                         */
234
 
                        if (devs_found > 0 && mode == '@' && !devmode) {
235
 
                                fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
236
 
                                exit(2);
237
 
                        }
238
 
                        if (devs_found > 0 && mode == 'G' && !devmode) {
239
 
                                fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
240
 
                                exit(2);
241
 
                        }
242
 
                        dv = malloc(sizeof(*dv));
243
 
                        if (dv == NULL) {
244
 
                                fprintf(stderr, Name ": malloc failed\n");
245
 
                                exit(3);
246
 
                        }
247
 
                        dv->devname = optarg;
248
 
                        dv->disposition = devmode;
249
 
                        dv->next = NULL;
250
 
                        *devlistend = dv;
251
 
                        devlistend = &dv->next;
252
 
                        
253
 
                        devs_found++;
254
 
                        continue;
255
 
                }
256
 
 
257
 
                /* We've got a mode, and opt is now something else which
258
 
                 * could depend on the mode */
259
 
#define O(a,b) ((a<<8)|b)
260
 
                switch (O(mode,opt)) {
261
 
                case O(CREATE,'c'):
262
 
                case O(BUILD,'c'): /* chunk or rounding */
263
 
                        if (chunk) {
264
 
                                fprintf(stderr, Name ": chunk/rounding may only be specified once. "
265
 
                                        "Second value is %s.\n", optarg);
266
 
                                exit(2);
267
 
                        }
268
 
                        chunk = strtol(optarg, &c, 10);
269
 
                        if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
270
 
                                fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
271
 
                                        optarg);
272
 
                                exit(2);
273
 
                        }
274
 
                        continue;
275
 
 
276
 
                case O(GROW,'z'):
277
 
                case O(CREATE,'z'): /* size */
278
 
                        if (size >= 0) {
279
 
                                fprintf(stderr, Name ": size may only be specified once. "
280
 
                                        "Second value is %s.\n", optarg);
281
 
                                exit(2);
282
 
                        }
283
 
                        if (strcmp(optarg, "max")==0)
284
 
                                size = 0;
285
 
                        else {
286
 
                                size = strtol(optarg, &c, 10);
287
 
                                if (!optarg[0] || *c || size < 4) {
288
 
                                        fprintf(stderr, Name ": invalid size: %s\n",
289
 
                                                optarg);
290
 
                                        exit(2);
291
 
                                }
292
 
                        }
293
 
                        continue;
294
 
 
295
 
                case O(GROW,'l'): /* hack - needed to understand layout */
296
 
                case O(CREATE,'l'):
297
 
                case O(BUILD,'l'): /* set raid level*/
298
 
                        if (level != UnSet) {
299
 
                                fprintf(stderr, Name ": raid level may only be set once.  "
300
 
                                        "Second value is %s.\n", optarg);
301
 
                                exit(2);
302
 
                        }
303
 
                        level = map_name(pers, optarg);
304
 
                        if (level == UnSet) {
305
 
                                fprintf(stderr, Name ": invalid raid level: %s\n",
306
 
                                        optarg);
307
 
                                exit(2);
308
 
                        }
309
 
                        if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
310
 
                                fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
311
 
                                        optarg);
312
 
                                exit(2);
313
 
                        }
314
 
                        if (sparedisks > 0 && level < 1 && level >= -1) {
315
 
                                fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
316
 
                                        optarg);
317
 
                                exit(2);
318
 
                        }
319
 
                        ident.level = level;
320
 
                        continue;
321
 
 
322
 
                case O(CREATE,'p'): /* raid5 layout */
323
 
                case O(BUILD,'p'): /* faulty layout */
324
 
                case O(GROW, 'p'): /* faulty reconfig */
325
 
                        if (layout != UnSet) {
326
 
                                fprintf(stderr,Name ": layout may only be sent once.  "
327
 
                                        "Second value was %s\n", optarg);
328
 
                                exit(2);
329
 
                        }
330
 
                        switch(level) {
331
 
                        default:
332
 
                                fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
333
 
                                        map_num(pers, level));
334
 
                                exit(2);
335
 
                        case UnSet:
336
 
                                fprintf(stderr, Name ": raid level must be given before layout.\n");
337
 
                                exit(2);
338
 
 
339
 
                        case 5:
340
 
                        case 6:
341
 
                                layout = map_name(r5layout, optarg);
342
 
                                if (layout==UnSet) {
343
 
                                        fprintf(stderr, Name ": layout %s not understood for raid5.\n",
344
 
                                                optarg);
345
 
                                        exit(2);
346
 
                                }
347
 
                                break;
348
 
 
349
 
                        case 10:
350
 
                                /* 'f' or 'n' followed by a number <= raid_disks */
351
 
                                if ((optarg[0] !=  'n' && optarg[0] != 'f') ||
352
 
                                    (copies = strtoul(optarg+1, &cp, 10)) < 1 ||
353
 
                                    copies > 200 ||
354
 
                                    *cp) {
355
 
                                        fprintf(stderr, Name ": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg);
356
 
                                        exit(2);
357
 
                                }
358
 
                                if (optarg[0] == 'n')
359
 
                                        layout = 256 + copies;
360
 
                                else
361
 
                                        layout = 1 + (copies<<8);
362
 
                                break;
363
 
                        case -5: /* Faulty
364
 
                                  * modeNNN
365
 
                                  */
366
 
                                    
367
 
                        {
368
 
                                int ln = strcspn(optarg, "0123456789");
369
 
                                char *m = strdup(optarg);
370
 
                                int mode;
371
 
                                m[ln] = 0;
372
 
                                mode = map_name(faultylayout, m);
373
 
                                if (mode == UnSet) {
374
 
                                        fprintf(stderr, Name ": layout %s not understood for faulty.\n",
375
 
                                                optarg);
376
 
                                        exit(2);
377
 
                                }
378
 
                                layout = mode | (atoi(optarg+ln)<< ModeShift);
379
 
                        }
380
 
                        }
381
 
                        continue;
382
 
 
383
 
                case O(CREATE,3):
384
 
                case O(BUILD,3): /* assume clean */
385
 
                        assume_clean = 1;
386
 
                        continue;
387
 
 
388
 
                case O(GROW,'n'):
389
 
                case O(CREATE,'n'):
390
 
                case O(BUILD,'n'): /* number of raid disks */
391
 
                        if (raiddisks) {
392
 
                                fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
393
 
                                        raiddisks, optarg);
394
 
                                exit(2);
395
 
                        }
396
 
                        raiddisks = strtol(optarg, &c, 10);
397
 
                        if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
398
 
                                fprintf(stderr, Name ": invalid number of raid devices: %s\n",
399
 
                                        optarg);
400
 
                                exit(2);
401
 
                        }
402
 
                        if (raiddisks == 1 &&  !force && level != -5) {
403
 
                                fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
404
 
                                        "     a mistake.  If you really mean it you will need to specify --force before\n"
405
 
                                        "     setting the number of drives.\n");
406
 
                                exit(2);
407
 
                        }
408
 
                        ident.raid_disks = raiddisks;
409
 
                        continue;
410
 
 
411
 
                case O(CREATE,'x'): /* number of spare (eXtra) discs */
412
 
                        if (sparedisks) {
413
 
                                fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
414
 
                                        sparedisks, optarg);
415
 
                                exit(2);
416
 
                        }
417
 
                        if (level != UnSet && level <= 0 && level >= -1) {
418
 
                                fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
419
 
                                        level);
420
 
                                exit(2);
421
 
                        }
422
 
                        sparedisks = strtol(optarg, &c, 10);
423
 
                        if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
424
 
                                fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
425
 
                                        optarg);
426
 
                                exit(2);
427
 
                        }
428
 
                        continue;
429
 
 
430
 
                case O(CREATE,'a'):
431
 
                case O(BUILD,'a'):
432
 
                case O(ASSEMBLE,'a'): /* auto-creation of device node */
433
 
                        if (optarg == NULL)
434
 
                                autof = -2;
435
 
                        else if (strcasecmp(optarg,"no")==0)
436
 
                                autof = 0;
437
 
                        else if (strcasecmp(optarg,"yes")==0)
438
 
                                autof = -2;
439
 
                        else if (strcasecmp(optarg,"md")==0)
440
 
                                autof = -1;
441
 
                        else {
442
 
                                /* There might be digits, and maybe a hypen, at the end */
443
 
                                char *e = optarg + strlen(optarg);
444
 
                                int num = 4;
445
 
                                int len;
446
 
                                while (e > optarg && isdigit(e[-1]))
447
 
                                        e--;
448
 
                                if (*e) {
449
 
                                        num = atoi(e);
450
 
                                        if (num <= 0) num = 1;
451
 
                                }
452
 
                                if (e > optarg && e[-1] == '-')
453
 
                                        e--;
454
 
                                len = e - optarg;
455
 
                                if ((len == 3 && strncasecmp(optarg,"mdp",3)==0) ||
456
 
                                    (len == 1 && strncasecmp(optarg,"p",1)==0) ||
457
 
                                    (len >= 4 && strncasecmp(optarg,"part",4)==0))
458
 
                                        autof = num;
459
 
                                else {
460
 
                                        fprintf(stderr, Name ": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
461
 
                                                "        optionally followed by a number.\n",
462
 
                                                optarg);
463
 
                                        exit(2);
464
 
                                }
465
 
                        }
466
 
                        continue;
467
 
 
468
 
                case O(BUILD,'f'): /* force honouring '-n 1' */
469
 
                case O(GROW,'f'): /* ditto */
470
 
                case O(CREATE,'f'): /* force honouring of device list */
471
 
                case O(ASSEMBLE,'f'): /* force assembly */
472
 
                case O(MISC,'f'): /* force zero */
473
 
                        force=1;
474
 
                        continue;
475
 
 
476
 
                        /* now for the Assemble options */
477
 
                case O(ASSEMBLE,'u'): /* uuid of array */
478
 
                        if (ident.uuid_set) {
479
 
                                fprintf(stderr, Name ": uuid cannot be set twice.  "
480
 
                                        "Second value %s.\n", optarg);
481
 
                                exit(2);
482
 
                        }
483
 
                        if (parse_uuid(optarg, ident.uuid))
484
 
                                ident.uuid_set = 1;
485
 
                        else {
486
 
                                fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
487
 
                                exit(2);
488
 
                        }
489
 
                        continue;
490
 
 
491
 
                case O(ASSEMBLE,'m'): /* super-minor for array */
492
 
                        if (ident.super_minor != UnSet) {
493
 
                                fprintf(stderr, Name ": super-minor cannot be set twice.  "
494
 
                                        "Second value: %s.\n", optarg);
495
 
                                exit(2);
496
 
                        }
497
 
                        if (strcmp(optarg, "dev")==0)
498
 
                                ident.super_minor = -2;
499
 
                        else {
500
 
                                ident.super_minor = strtoul(optarg, &cp, 10);
501
 
                                if (!optarg[0] || *cp) {
502
 
                                        fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
503
 
                                        exit(2);
504
 
                                }
505
 
                        }
506
 
                        continue;
507
 
 
508
 
                case O(ASSEMBLE,'U'): /* update the superblock */
509
 
                        if (update) {
510
 
                                fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
511
 
                                        update, optarg);
512
 
                                exit(2);
513
 
                        }
514
 
                        update = optarg;
515
 
                        if (strcmp(update, "sparc2.2")==0) 
516
 
                                continue;
517
 
                        if (strcmp(update, "super-minor") == 0)
518
 
                                continue;
519
 
                        if (strcmp(update, "summaries")==0)
520
 
                                continue;
521
 
                        if (strcmp(update, "resync")==0)
522
 
                                continue;
523
 
                        fprintf(stderr, Name ": '--update %s' invalid.  Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update);
524
 
                        exit(2);
525
 
 
526
 
                case O(ASSEMBLE,'c'): /* config file */
527
 
                case O(MISC, 'c'):
528
 
                case O(MONITOR,'c'):
529
 
                        if (configfile) {
530
 
                                fprintf(stderr, Name ": configfile cannot be set twice.  "
531
 
                                        "Second value is %s.\n", optarg);
532
 
                                exit(2);
533
 
                        }
534
 
                        configfile = optarg;
535
 
                        /* FIXME possibly check that config file exists.  Even parse it */
536
 
                        continue;
537
 
                case O(ASSEMBLE,'s'): /* scan */
538
 
                case O(MISC,'s'):
539
 
                case O(MONITOR,'s'):
540
 
                        scan = 1;
541
 
                        continue;
542
 
 
543
 
                case O(MONITOR,'m'): /* mail address */
544
 
                        if (mailaddr)
545
 
                                fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
546
 
                                        optarg);
547
 
                        else
548
 
                                mailaddr = optarg;
549
 
                        continue;
550
 
 
551
 
                case O(MONITOR,'p'): /* alert program */
552
 
                        if (program)
553
 
                                fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
554
 
                                        optarg);
555
 
                        else
556
 
                                program = optarg;
557
 
                        continue;
558
 
 
559
 
                case O(MONITOR,'d'): /* delay in seconds */
560
 
                        if (delay)
561
 
                                fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
562
 
                                        optarg);
563
 
                        else {
564
 
                                delay = strtol(optarg, &c, 10);
565
 
                                if (!optarg[0] || *c || delay<1) {
566
 
                                        fprintf(stderr, Name ": invalid delay: %s\n",
567
 
                                                optarg);
568
 
                                        exit(2);
569
 
                                }
570
 
                        }
571
 
                        continue;
572
 
                case O(MONITOR,'f'): /* daemonise */
573
 
                        daemonise = 1;
574
 
                        continue;
575
 
                case O(MONITOR,'i'): /* pid */
576
 
                        if (pidfile)
577
 
                                fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
578
 
                                        optarg);
579
 
                        else
580
 
                                pidfile = optarg;
581
 
                        continue;
582
 
                case O(MONITOR,'1'): /* oneshot */
583
 
                        oneshot = 1;
584
 
                        continue;
585
 
                case O(MONITOR,'t'): /* test */
586
 
                        test = 1;
587
 
                        continue;
588
 
 
589
 
                        /* now the general management options.  Some are applicable
590
 
                         * to other modes. None have arguments.
591
 
                         */
592
 
                case O(GROW,'a'):
593
 
                case O(MANAGE,'a'): /* add a drive */
594
 
                        devmode = 'a';
595
 
                        continue;
596
 
                case O(MANAGE,'r'): /* remove a drive */
597
 
                        devmode = 'r';
598
 
                        continue;
599
 
                case O(MANAGE,'f'): /* set faulty */
600
 
                        devmode = 'f';
601
 
                        continue;
602
 
                case O(MANAGE,'R'):
603
 
                case O(ASSEMBLE,'R'):
604
 
                case O(BUILD,'R'):
605
 
                case O(CREATE,'R'): /* Run the array */
606
 
                        if (runstop < 0) {
607
 
                                fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
608
 
                                exit(2);
609
 
                        }
610
 
                        runstop = 1;
611
 
                        continue;
612
 
                case O(MANAGE,'S'):
613
 
                        if (runstop > 0) {
614
 
                                fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
615
 
                                exit(2);
616
 
                        }
617
 
                        runstop = -1;
618
 
                        continue;
619
 
 
620
 
                case O(MANAGE,'o'):
621
 
                        if (readonly < 0) {
622
 
                                fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
623
 
                                exit(2);
624
 
                        }
625
 
                        readonly = 1;
626
 
                        continue;
627
 
                case O(MANAGE,'w'):
628
 
                        if (readonly > 0) {
629
 
                                fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
630
 
                                exit(2);
631
 
                        }
632
 
                        readonly = -1;
633
 
                        continue;
634
 
 
635
 
                case O(MISC,'Q'):
636
 
                case O(MISC,'D'):
637
 
                case O(MISC,'E'):
638
 
                case O(MISC,'K'):
639
 
                case O(MISC,'R'):
640
 
                case O(MISC,'S'):
641
 
                case O(MISC,'o'):
642
 
                case O(MISC,'w'):
643
 
                        if (devmode && devmode != opt &&
644
 
                            (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
645
 
                                fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
646
 
                                        devmode =='E'?opt:devmode);
647
 
                                exit(2);
648
 
                        }
649
 
                        devmode = opt;
650
 
                        continue;
651
 
                case O(MISC,'t'):
652
 
                        test = 1;
653
 
                        continue;
654
 
 
655
 
                case O(MISC, 22):
656
 
                        if (devmode != 'E') {
657
 
                                fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
658
 
                                exit(2);
659
 
                        }
660
 
                        SparcAdjust = 1;
661
 
                        continue;
662
 
                }
663
 
                /* We have now processed all the valid options. Anything else is
664
 
                 * an error
665
 
                 */
666
 
                fprintf(stderr, Name ": option %c not valid in %s mode\n",
667
 
                        opt, map_num(modes, mode));
668
 
                exit(2);
669
 
 
670
 
        }
671
 
 
672
 
        if (!mode && devs_found) {
673
 
                mode = MISC;
674
 
                devmode = 'Q';
675
 
                if (devlist->disposition == 0)
676
 
                        devlist->disposition = devmode;
677
 
        }
678
 
        if (!mode) {
679
 
                fputs(Usage, stderr);
680
 
                exit(2);
681
 
        }
682
 
        /* Ok, got the option parsing out of the way
683
 
         * hopefully it's mostly right but there might be some stuff
684
 
         * missing
685
 
         *
686
 
         * That is mosty checked in the per-mode stuff but...
687
 
         *
688
 
         * For @,B,C  and A without -s, the first device listed must be an md device
689
 
         * we check that here and open it.
690
 
         */
691
 
 
692
 
        if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
693
 
            (mode == ASSEMBLE && ! scan)) {
694
 
                if (devs_found < 1) {
695
 
                        fprintf(stderr, Name ": an md device must be given in this mode\n");
696
 
                        exit(2);
697
 
                }
698
 
                if ((int)ident.super_minor == -2 && autof) {
699
 
                        fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");      
700
 
                        exit(2);
701
 
                }
702
 
                mdfd = open_mddev(devlist->devname, autof);
703
 
                if (mdfd < 0)
704
 
                        exit(1);
705
 
                if ((int)ident.super_minor == -2) {
706
 
                        struct stat stb;
707
 
                        fstat(mdfd, &stb);
708
 
                        ident.super_minor = minor(stb.st_rdev);
709
 
                }
710
 
        }
711
 
 
712
 
        rv = 0;
713
 
        switch(mode) {
714
 
        case MANAGE:
715
 
                /* readonly, add/remove, readwrite, runstop */
716
 
                if (readonly>0)
717
 
                        rv = Manage_ro(devlist->devname, mdfd, readonly);
718
 
                if (!rv && devs_found>1)
719
 
                        rv = Manage_subdevs(devlist->devname, mdfd,
720
 
                                            devlist->next);
721
 
                if (!rv && readonly < 0)
722
 
                        rv = Manage_ro(devlist->devname, mdfd, readonly);
723
 
                if (!rv && runstop)
724
 
                        rv = Manage_runstop(devlist->devname, mdfd, runstop, 0);
725
 
                break;
726
 
        case ASSEMBLE:
727
 
                if (devs_found == 1 && ident.uuid_set == 0 &&
728
 
                    ident.super_minor == UnSet && !scan ) {
729
 
                        /* Only a device has been given, so get details from config file */
730
 
                        mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
731
 
                        if (array_ident == NULL) {
732
 
                                fprintf(stderr, Name ": %s not identified in config file.\n",
733
 
                                        devlist->devname);
734
 
                                rv |= 1;
735
 
                        } else {
736
 
                                mdfd = open_mddev(devlist->devname, 
737
 
                                                  array_ident->autof ? array_ident->autof : autof);
738
 
                                if (mdfd < 0)
739
 
                                        rv |= 1;
740
 
                                else {
741
 
                                        rv |= Assemble(devlist->devname, mdfd, array_ident, configfile,
742
 
                                                       NULL,
743
 
                                                       readonly, runstop, update, verbose, force);
744
 
                                        close(mdfd);
745
 
                                }
746
 
                        }
747
 
                } else if (!scan)
748
 
                        rv = Assemble(devlist->devname, mdfd, &ident, configfile,
749
 
                                      devlist->next,
750
 
                                      readonly, runstop, update, verbose, force);
751
 
                else if (devs_found>0) {
752
 
                        if (update && devs_found > 1) {
753
 
                                fprintf(stderr, Name ": can only update a single array at a time\n");
754
 
                                exit(1);
755
 
                        }
756
 
                        for (dv = devlist ; dv ; dv=dv->next) {
757
 
                                mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
758
 
                                if (array_ident == NULL) {
759
 
                                        fprintf(stderr, Name ": %s not identified in config file.\n",
760
 
                                                dv->devname);
761
 
                                        rv |= 1;
762
 
                                        continue;
763
 
                                }
764
 
                                mdfd = open_mddev(dv->devname, 
765
 
                                                  array_ident->autof ?array_ident->autof : autof);
766
 
                                if (mdfd < 0) {
767
 
                                        rv |= 1;
768
 
                                        continue;
769
 
                                }
770
 
                                rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
771
 
                                               NULL,
772
 
                                               readonly, runstop, update, verbose, force);
773
 
                                close(mdfd);
774
 
                        }
775
 
                } else {
776
 
                        mddev_ident_t array_list =  conf_get_ident(configfile, NULL);
777
 
                        if (!array_list) {
778
 
                                fprintf(stderr, Name ": No arrays found in config file\n");
779
 
                                rv = 1;
780
 
                        } else
781
 
                                for (; array_list; array_list = array_list->next) {
782
 
                                        mdu_array_info_t array;
783
 
                                        mdfd = open_mddev(array_list->devname, 
784
 
                                                          array_list->autof ? array_list->autof : autof);
785
 
                                        if (mdfd < 0) {
786
 
                                                rv |= 1;
787
 
                                                continue;
788
 
                                        }
789
 
                                        if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
790
 
                                                /* already assembled, skip */
791
 
                                                ;
792
 
                                        else
793
 
                                                rv |= Assemble(array_list->devname, mdfd,
794
 
                                                               array_list, configfile,
795
 
                                                               NULL,
796
 
                                                               readonly, runstop, NULL, verbose, force);
797
 
                                        close(mdfd);
798
 
                                }
799
 
                }
800
 
                break;
801
 
        case BUILD:
802
 
                rv = Build(devlist->devname, mdfd, chunk, level, layout, raiddisks, devlist->next, assume_clean);
803
 
                break;
804
 
        case CREATE:
805
 
                rv = Create(devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
806
 
                            raiddisks, sparedisks,
807
 
                            devs_found-1, devlist->next, runstop, verbose, force);
808
 
                break;
809
 
        case MISC:
810
 
 
811
 
                if (devmode == 'E') {
812
 
                        if (devlist == NULL && !scan) {
813
 
                                fprintf(stderr, Name ": No devices to examine\n");
814
 
                                exit(2);
815
 
                        }
816
 
                        if (devlist == NULL)
817
 
                                devlist = conf_get_devs(configfile);
818
 
                        if (devlist == NULL) {
819
 
                                fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
820
 
                                exit(1);
821
 
                        }
822
 
                        rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, scan, SparcAdjust);
823
 
                } else {
824
 
                        if (devlist == NULL) {
825
 
                                if (devmode=='D' && scan) {
826
 
                                        /* apply --detail to all devices in /proc/mdstat */
827
 
                                        struct mdstat_ent *ms = mdstat_read(0);
828
 
                                        struct mdstat_ent *e;
829
 
                                        for (e=ms ; e ; e=e->next) {
830
 
                                                char *name = get_md_name(e->devnum);
831
 
 
832
 
                                                if (!name) {
833
 
                                                        fprintf(stderr, Name ": cannot find device file for %s\n",
834
 
                                                                e->dev);
835
 
                                                        continue;
836
 
                                                }
837
 
                                                rv |= Detail(name, verbose>1?0:verbose+1, test);
838
 
                                                put_md_name(name);
839
 
                                        }
840
 
                                } else  if (devmode == 'S' && scan) {
841
 
                                        /* apply --stop to all devices in /proc/mdstat */
842
 
                                        /* Due to possible stacking of devices, repeat until
843
 
                                         * nothing more can be stopped
844
 
                                         */
845
 
                                        int progress=1, err;
846
 
                                        int last = 0;
847
 
                                        do {
848
 
                                                struct mdstat_ent *ms = mdstat_read(0);
849
 
                                                struct mdstat_ent *e;
850
 
 
851
 
                                                if (!progress) last = 1;
852
 
                                                progress = 0; err = 0;
853
 
                                                for (e=ms ; e ; e=e->next) {
854
 
                                                        char *name = get_md_name(e->devnum);
855
 
 
856
 
                                                        if (!name) {
857
 
                                                                fprintf(stderr, Name ": cannot find device file for %s\n",
858
 
                                                                        e->dev);
859
 
                                                                continue;
860
 
                                                        }
861
 
                                                        mdfd = open_mddev(name, 0);
862
 
                                                        if (mdfd >= 0) {
863
 
                                                                if (Manage_runstop(name, mdfd, -1, !last))
864
 
                                                                        err = 1;
865
 
                                                                else
866
 
                                                                        progress = 1;
867
 
                                                                close(mdfd);
868
 
                                                        }
869
 
 
870
 
                                                        put_md_name(name);
871
 
                                                }
872
 
                                        } while (!last && err);
873
 
                                } else {
874
 
                                        fprintf(stderr, Name ": No devices given.\n");
875
 
                                        exit(2);
876
 
                                }
877
 
                        }
878
 
                        for (dv=devlist ; dv; dv=dv->next) {
879
 
                                switch(dv->disposition) {
880
 
                                case 'D':
881
 
                                        rv |= Detail(dv->devname, brief?1+verbose:0, test); continue;
882
 
                                case 'K': /* Zero superblock */
883
 
                                        rv |= Kill(dv->devname, force); continue;
884
 
                                case 'Q':
885
 
                                        rv |= Query(dv->devname); continue;
886
 
                                }
887
 
                                mdfd = open_mddev(dv->devname, 0);
888
 
                                if (mdfd>=0) {
889
 
                                        switch(dv->disposition) {
890
 
                                        case 'R':
891
 
                                                rv |= Manage_runstop(dv->devname, mdfd, 1, 0); break;
892
 
                                        case 'S':
893
 
                                                rv |= Manage_runstop(dv->devname, mdfd, -1, 0); break;
894
 
                                        case 'o':
895
 
                                                rv |= Manage_ro(dv->devname, mdfd, 1); break;
896
 
                                        case 'w':
897
 
                                                rv |= Manage_ro(dv->devname, mdfd, -1); break;
898
 
                                        }
899
 
                                        close(mdfd);
900
 
                                }
901
 
                        }
902
 
                }
903
 
                break;
904
 
        case MONITOR:
905
 
                if (!devlist && !scan) {
906
 
                        fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
907
 
                        rv = 1;
908
 
                        break;
909
 
                }
910
 
                if (pidfile && !daemonise) {
911
 
                        fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
912
 
                        rv = 1;
913
 
                        break;
914
 
                }
915
 
                rv= Monitor(devlist, mailaddr, program,
916
 
                            delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile);
917
 
                break;
918
 
 
919
 
        case GROW:
920
 
                if (devs_found > 1) {
921
 
                        
922
 
                        /* must be '-a'. */
923
 
                        if (size >= 0 || raiddisks) {
924
 
                                fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
925
 
                                rv = 1;
926
 
                                break;
927
 
                        }
928
 
                        for (dv=devlist->next; dv ; dv=dv->next) {
929
 
                                rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
930
 
                                if (rv)
931
 
                                        break;
932
 
                        }
933
 
                } else if ((size >= 0) + (raiddisks != 0) +  (layout != UnSet) > 1) {
934
 
                        fprintf(stderr, Name ": can change at most one of size, raiddisks, and layout\n");
935
 
                        rv = 1;
936
 
                        break;
937
 
                } else if (layout != UnSet)
938
 
                        rv = Manage_reconfig(devlist->devname, mdfd, layout);
939
 
                else if (size >= 0 || raiddisks)
940
 
                        rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
941
 
                else 
942
 
                        fprintf(stderr, Name ": no changes to --grow\n");
943
 
                break;
944
 
        }
945
 
        exit(rv);
946
 
}