]> git.neil.brown.name Git - mdadm.git/blob - Incremental.c
Release mdadm-4.0
[mdadm.git] / Incremental.c
1 /*
2  * Incremental.c - support --incremental.  Part of:
3  * mdadm - manage Linux "md" devices aka RAID arrays.
4  *
5  * Copyright (C) 2006-2013 Neil Brown <neilb@suse.de>
6  *
7  *
8  *    This program is free software; you can redistribute it and/or modify
9  *    it under the terms of the GNU General Public License as published by
10  *    the Free Software Foundation; either version 2 of the License, or
11  *    (at your option) any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License
19  *    along with this program; if not, write to the Free Software
20  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  *    Author: Neil Brown
23  *    Email: <neilb@suse.de>
24  *    Paper: Neil Brown
25  *           Novell Inc
26  *           GPO Box Q1283
27  *           QVB Post Office, NSW 1230
28  *           Australia
29  */
30
31 #include        "mdadm.h"
32 #include        <sys/wait.h>
33 #include        <dirent.h>
34 #include        <ctype.h>
35
36 static int count_active(struct supertype *st, struct mdinfo *sra,
37                         int mdfd, char **availp,
38                         struct mdinfo *info);
39 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
40                         int number, __u64 events, int verbose,
41                         char *array_name);
42 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
43                      struct map_ent *target,
44                      struct supertype *st, int verbose);
45
46 static int Incremental_container(struct supertype *st, char *devname,
47                                  struct context *c, char *only);
48
49 int Incremental(struct mddev_dev *devlist, struct context *c,
50                 struct supertype *st)
51 {
52         /* Add this device to an array, creating the array if necessary
53          * and starting the array if sensible or - if runstop>0 - if possible.
54          *
55          * This has several steps:
56          *
57          * 1/ Check if device is permitted by mdadm.conf, reject if not.
58          * 2/ Find metadata, reject if none appropriate (check
59          *       version/name from args)
60          * 3/ Check if there is a match in mdadm.conf
61          * 3a/ if not, check for homehost match.  If no match, assemble as
62          *    a 'foreign' array.
63          * 4/ Determine device number.
64          * - If in mdadm.conf with std name, use that
65          * - UUID in /var/run/mdadm.map  use that
66          * - If name is suggestive, use that. unless in use with different uuid.
67          * - Choose a free, high number.
68          * - Use a partitioned device unless strong suggestion not to.
69          *         e.g. auto=md
70          *   Don't choose partitioned for containers.
71          * 5/ Find out if array already exists
72          * 5a/ if it does not
73          * - choose a name, from mdadm.conf or 'name' field in array.
74          * - create the array
75          * - add the device
76          * 5b/ if it does
77          * - check one drive in array to make sure metadata is a reasonably
78          *       close match.  Reject if not (e.g. different type)
79          * - add the device
80          * 6/ Make sure /var/run/mdadm.map contains this array.
81          * 7/ Is there enough devices to possibly start the array?
82          *     For a container, this means running Incremental_container.
83          * 7a/ if not, finish with success.
84          * 7b/ if yes,
85          * - read all metadata and arrange devices like -A does
86          * - if number of OK devices match expected, or -R and there are enough,
87          *   start the array (auto-readonly).
88          */
89         struct stat stb;
90         struct mdinfo info, dinfo;
91         struct mdinfo *sra = NULL, *d;
92         struct mddev_ident *match;
93         char chosen_name[1024];
94         char *md_devname;
95         int rv = 1;
96         struct map_ent *mp, *map = NULL;
97         int dfd = -1, mdfd = -1;
98         char *avail = NULL;
99         int active_disks;
100         int trustworthy;
101         char *name_to_use;
102         mdu_array_info_t ainf;
103         struct dev_policy *policy = NULL;
104         struct map_ent target_array;
105         int have_target;
106         char *devname = devlist->devname;
107         int journal_device_missing = 0;
108
109         struct createinfo *ci = conf_get_create_info();
110
111         if (stat(devname, &stb) < 0) {
112                 if (c->verbose >= 0)
113                         pr_err("stat failed for %s: %s.\n",
114                                 devname, strerror(errno));
115                 return rv;
116         }
117         if ((stb.st_mode & S_IFMT) != S_IFBLK) {
118                 if (c->verbose >= 0)
119                         pr_err("%s is not a block device.\n",
120                                 devname);
121                 return rv;
122         }
123         dfd = dev_open(devname, O_RDONLY);
124         if (dfd < 0) {
125                 if (c->verbose >= 0)
126                         pr_err("cannot open %s: %s.\n",
127                                 devname, strerror(errno));
128                 return rv;
129         }
130         /* If the device is a container, we do something very different */
131         if (must_be_container(dfd)) {
132                 if (!st)
133                         st = super_by_fd(dfd, NULL);
134                 if (st && st->ss->load_container)
135                         rv = st->ss->load_container(st, dfd, NULL);
136
137                 close(dfd);
138                 if (!rv && st->ss->container_content) {
139                         if (map_lock(&map))
140                                 pr_err("failed to get exclusive lock on mapfile\n");
141                         if (c->export)
142                                 printf("MD_DEVNAME=%s\n", devname);
143                         rv = Incremental_container(st, devname, c, NULL);
144                         map_unlock(&map);
145                         return rv;
146                 }
147
148                 pr_err("%s is not part of an md array.\n",
149                         devname);
150                 return rv;
151         }
152
153         /* 1/ Check if device is permitted by mdadm.conf */
154
155         for (;devlist; devlist = devlist->next)
156                 if (conf_test_dev(devlist->devname))
157                         break;
158         if (!devlist) {
159                 devlist = conf_get_devs();
160                 for (;devlist; devlist = devlist->next) {
161                         struct stat st2;
162                         if (stat(devlist->devname, &st2) == 0 &&
163                             (st2.st_mode & S_IFMT) == S_IFBLK &&
164                             st2.st_rdev == stb.st_rdev)
165                                 break;
166                 }
167         }
168         if (!devlist) {
169                 if (c->verbose >= 0)
170                         pr_err("%s not permitted by mdadm.conf.\n",
171                                devname);
172                 goto out;
173         }
174
175         /* 2/ Find metadata, reject if none appropriate (check
176          *            version/name from args) */
177
178         if (fstat(dfd, &stb) < 0) {
179                 if (c->verbose >= 0)
180                         pr_err("fstat failed for %s: %s.\n",
181                                 devname, strerror(errno));
182                 goto out;
183         }
184         if ((stb.st_mode & S_IFMT) != S_IFBLK) {
185                 if (c->verbose >= 0)
186                         pr_err("%s is not a block device.\n",
187                                 devname);
188                 goto out;
189         }
190
191         dinfo.disk.major = major(stb.st_rdev);
192         dinfo.disk.minor = minor(stb.st_rdev);
193
194         policy = disk_policy(&dinfo);
195         have_target = policy_check_path(&dinfo, &target_array);
196
197         if (st == NULL && (st = guess_super_type(dfd, guess_array)) == NULL) {
198                 if (c->verbose >= 0)
199                         pr_err("no recognisable superblock on %s.\n",
200                                devname);
201                 rv = try_spare(devname, &dfd, policy,
202                                have_target ? &target_array : NULL,
203                                NULL, c->verbose);
204                 goto out;
205         }
206         st->ignore_hw_compat = 0;
207
208         if (st->ss->compare_super == NULL ||
209             st->ss->load_super(st, dfd, c->verbose >= 0 ? devname : NULL)) {
210                 if (c->verbose >= 0)
211                         pr_err("no RAID superblock on %s.\n",
212                                 devname);
213                 rv = try_spare(devname, &dfd, policy,
214                                have_target ? &target_array : NULL,
215                                st, c->verbose);
216                 free(st);
217                 goto out;
218         }
219         close (dfd); dfd = -1;
220
221         st->ss->getinfo_super(st, &info, NULL);
222
223         /* 3/ Check if there is a match in mdadm.conf */
224         match = conf_match(st, &info, devname, c->verbose, &rv);
225         if (!match && rv == 2)
226                 goto out;
227
228         if (match && match->devname
229             && strcasecmp(match->devname, "<ignore>") == 0) {
230                 if (c->verbose >= 0)
231                         pr_err("array containing %s is explicitly ignored by mdadm.conf\n",
232                                 devname);
233                 goto out;
234         }
235
236         /* 3a/ if not, check for homehost match.  If no match, continue
237          * but don't trust the 'name' in the array. Thus a 'random' minor
238          * number will be assigned, and the device name will be based
239          * on that. */
240         if (match)
241                 trustworthy = LOCAL;
242         else if (st->ss->match_home(st, c->homehost) == 1)
243                 trustworthy = LOCAL;
244         else if (st->ss->match_home(st, "any") == 1)
245                 trustworthy = LOCAL_ANY;
246         else
247                 trustworthy = FOREIGN;
248
249         if (!match && !conf_test_metadata(st->ss->name, policy,
250                                           (trustworthy == LOCAL))) {
251                 if (c->verbose >= 1)
252                         pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
253                                devname, st->ss->name);
254                 goto out;
255         }
256         if (trustworthy == LOCAL_ANY)
257                 trustworthy = LOCAL;
258
259         /* There are three possible sources for 'autof':  command line,
260          * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
261          * ARRAY takes precedence, then command line, then
262          * CREATE.
263          */
264         if (match && match->autof)
265                 c->autof = match->autof;
266         if (c->autof == 0)
267                 c->autof = ci->autof;
268
269         name_to_use = info.name;
270         if (name_to_use[0] == 0 &&
271             info.array.level == LEVEL_CONTAINER) {
272                 name_to_use = info.text_version;
273                 trustworthy = METADATA;
274         }
275         if (name_to_use[0] && trustworthy != LOCAL &&
276             ! c->require_homehost &&
277             conf_name_is_free(name_to_use))
278                 trustworthy = LOCAL;
279
280         /* strip "hostname:" prefix from name if we have decided
281          * to treat it as LOCAL
282          */
283         if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
284                 name_to_use = strchr(name_to_use, ':')+1;
285
286         /* 4/ Check if array exists.
287          */
288         if (map_lock(&map))
289                 pr_err("failed to get exclusive lock on mapfile\n");
290         /* Now check we can get O_EXCL.  If not, probably "mdadm -A" has
291          * taken over
292          */
293         dfd = dev_open(devname, O_RDONLY|O_EXCL);
294         if (dfd < 0) {
295                 if (c->verbose >= 0)
296                         pr_err("cannot reopen %s: %s.\n",
297                                 devname, strerror(errno));
298                 goto out_unlock;
299         }
300         /* Cannot hold it open while we add the device to the array,
301          * so we must release the O_EXCL and depend on the map_lock()
302          * So now is the best time to remove any partitions.
303          */
304         remove_partitions(dfd);
305         close(dfd);
306         dfd = -1;
307
308         mp = map_by_uuid(&map, info.uuid);
309         if (mp)
310                 mdfd = open_dev(mp->devnm);
311         else
312                 mdfd = -1;
313
314         if (mdfd < 0) {
315
316                 /* Skip the clustered ones. This should be started by
317                  * clustering resource agents
318                  */
319                 if (info.array.state & (1 << MD_SB_CLUSTERED))
320                         goto out;
321
322                 /* Couldn't find an existing array, maybe make a new one */
323                 mdfd = create_mddev(match ? match->devname : NULL,
324                                     name_to_use, c->autof, trustworthy, chosen_name);
325
326                 if (mdfd < 0)
327                         goto out_unlock;
328
329                 sysfs_init(&info, mdfd, NULL);
330
331                 if (set_array_info(mdfd, st, &info) != 0) {
332                         pr_err("failed to set array info for %s: %s\n",
333                                 chosen_name, strerror(errno));
334                         rv = 2;
335                         goto out_unlock;
336                 }
337
338                 dinfo = info;
339                 dinfo.disk.major = major(stb.st_rdev);
340                 dinfo.disk.minor = minor(stb.st_rdev);
341                 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
342                         pr_err("failed to add %s to new array %s: %s.\n",
343                                 devname, chosen_name, strerror(errno));
344                         ioctl(mdfd, STOP_ARRAY, 0);
345                         rv = 2;
346                         goto out_unlock;
347                 }
348                 sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
349                                               GET_OFFSET | GET_SIZE));
350
351                 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
352                         /* It really should be 'none' - must be old buggy
353                          * kernel, and mdadm -I may not be able to complete.
354                          * So reject it.
355                          */
356                         ioctl(mdfd, STOP_ARRAY, NULL);
357                         pr_err("You have an old buggy kernel which cannot support\n      --incremental reliably.  Aborting.\n");
358                         rv = 2;
359                         goto out_unlock;
360                 }
361                 info.array.working_disks = 1;
362                 /* 6/ Make sure /var/run/mdadm.map contains this array. */
363                 map_update(&map, fd2devnm(mdfd),
364                            info.text_version,
365                            info.uuid, chosen_name);
366         } else {
367         /* 5b/ if it does */
368         /* - check one drive in array to make sure metadata is a reasonably */
369         /*        close match.  Reject if not (e.g. different type) */
370         /* - add the device */
371                 char dn[20];
372                 int dfd2;
373                 int err;
374                 struct supertype *st2;
375                 struct mdinfo info2, *d;
376
377                 sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
378                                             GET_OFFSET | GET_SIZE));
379
380                 if (mp->path)
381                         strcpy(chosen_name, mp->path);
382                 else
383                         strcpy(chosen_name, mp->devnm);
384
385                 /* It is generally not OK to add non-spare drives to a
386                  * running array as they are probably missing because
387                  * they failed.  However if runstop is 1, then the
388                  * array was possibly started early and our best bet is
389                  * to add this anyway.
390                  * Also if action policy is re-add or better we allow
391                  * re-add.
392                  * This doesn't apply to containers as the 'non-spare'
393                  * flag has a different meaning.  The test has to happen
394                  * at the device level there
395                  */
396                 if (!st->ss->external
397                     && (info.disk.state & (1<<MD_DISK_SYNC)) != 0
398                     && ! policy_action_allows(policy, st->ss->name,
399                                               act_re_add)
400                     && c->runstop < 1) {
401                         if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
402                                 pr_err("not adding %s to active array (without --run) %s\n",
403                                        devname, chosen_name);
404                                 rv = 2;
405                                 goto out_unlock;
406                         }
407                 }
408                 if (!sra) {
409                         rv = 2;
410                         goto out_unlock;
411                 }
412                 if (sra->devs) {
413                         sprintf(dn, "%d:%d", sra->devs->disk.major,
414                                 sra->devs->disk.minor);
415                         dfd2 = dev_open(dn, O_RDONLY);
416                         if (dfd2 < 0) {
417                                 pr_err("unable to open %s\n", devname);
418                                 rv = 2;
419                                 goto out_unlock;
420                         }
421                         st2 = dup_super(st);
422                         if (st2->ss->load_super(st2, dfd2, NULL) ||
423                             st->ss->compare_super(st, st2) != 0) {
424                                 pr_err("metadata mismatch between %s and chosen array %s\n",
425                                        devname, chosen_name);
426                                 close(dfd2);
427                                 rv = 2;
428                                 goto out_unlock;
429                         }
430                         close(dfd2);
431                         st2->ss->getinfo_super(st2, &info2, NULL);
432                         st2->ss->free_super(st2);
433                         if (info.array.level != info2.array.level ||
434                             memcmp(info.uuid, info2.uuid, 16) != 0 ||
435                             info.array.raid_disks != info2.array.raid_disks) {
436                                 pr_err("unexpected difference between %s and %s.\n",
437                                        chosen_name, devname);
438                                 rv = 2;
439                                 goto out_unlock;
440                         }
441                 }
442                 info.disk.major = major(stb.st_rdev);
443                 info.disk.minor = minor(stb.st_rdev);
444                 /* add disk needs to know about containers */
445                 if (st->ss->external)
446                         sra->array.level = LEVEL_CONTAINER;
447
448                 if (info.array.state & (1 << MD_SB_CLUSTERED))
449                         info.disk.state |= (1 << MD_DISK_CLUSTER_ADD);
450
451                 err = add_disk(mdfd, st, sra, &info);
452                 if (err < 0 && errno == EBUSY) {
453                         /* could be another device present with the same
454                          * disk.number. Find and reject any such
455                          */
456                         find_reject(mdfd, st, sra, info.disk.number,
457                                     info.events, c->verbose, chosen_name);
458                         err = add_disk(mdfd, st, sra, &info);
459                 }
460                 if (err < 0 && errno == EINVAL &&
461                     info.disk.state & (1<<MD_DISK_SYNC)) {
462                         /* Maybe it needs to be added as a spare */
463                         if (policy_action_allows(policy, st->ss->name,
464                                                  act_force_spare)) {
465                                 info.disk.state &= ~(1<<MD_DISK_SYNC);
466                                 err = add_disk(mdfd, st, sra, &info);
467                         } else
468                                 if (c->verbose >= 0)
469                                         pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
470                                                devname, chosen_name);
471                 }
472                 if (err < 0) {
473                         pr_err("failed to add %s to existing array %s: %s.\n",
474                                 devname, chosen_name, strerror(errno));
475                         rv = 2;
476                         goto out_unlock;
477                 }
478                 info.array.working_disks = 0;
479                 for (d = sra->devs; d; d=d->next)
480                         info.array.working_disks ++;
481
482         }
483         if (strncmp(chosen_name, "/dev/md/", 8) == 0)
484                 md_devname = chosen_name+8;
485         else
486                 md_devname = chosen_name;
487         if (c->export) {
488                 printf("MD_DEVICE=%s\n", fd2devnm(mdfd));
489                 printf("MD_DEVNAME=%s\n", md_devname);
490                 printf("MD_FOREIGN=%s\n", trustworthy == FOREIGN ? "yes" : "no");
491         }
492
493         /* 7/ Is there enough devices to possibly start the array? */
494         /* 7a/ if not, finish with success. */
495         if (info.array.level == LEVEL_CONTAINER) {
496                 char devnm[32];
497                 /* Try to assemble within the container */
498                 sysfs_uevent(sra, "change");
499                 if (!c->export && c->verbose >= 0)
500                         pr_err("container %s now has %d device%s\n",
501                                chosen_name, info.array.working_disks,
502                                info.array.working_disks == 1?"":"s");
503                 wait_for(chosen_name, mdfd);
504                 if (st->ss->external)
505                         strcpy(devnm, fd2devnm(mdfd));
506                 if (st->ss->load_container)
507                         rv = st->ss->load_container(st, mdfd, NULL);
508                 close(mdfd);
509                 sysfs_free(sra);
510                 if (!rv)
511                         rv = Incremental_container(st, chosen_name, c, NULL);
512                 map_unlock(&map);
513                 /* after spare is added, ping monitor for external metadata
514                  * so that it can eg. try to rebuild degraded array */
515                 if (st->ss->external)
516                         ping_monitor(devnm);
517                 return rv;
518         }
519
520         /* We have added something to the array, so need to re-read the
521          * state.  Eventually this state should be kept up-to-date as
522          * things change.
523          */
524         sysfs_free(sra);
525         sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
526                                     GET_OFFSET | GET_SIZE));
527         active_disks = count_active(st, sra, mdfd, &avail, &info);
528
529         journal_device_missing = (info.journal_device_required) && (info.journal_clean == 0);
530
531         if (enough(info.array.level, info.array.raid_disks,
532                    info.array.layout, info.array.state & 1,
533                    avail) == 0) {
534                 if (c->export) {
535                         printf("MD_STARTED=no\n");
536                 } else if (c->verbose >= 0)
537                         pr_err("%s attached to %s, not enough to start (%d).\n",
538                                devname, chosen_name, active_disks);
539                 rv = 0;
540                 goto out_unlock;
541         }
542
543         /* 7b/ if yes, */
544         /* - if number of OK devices match expected, or -R and there */
545         /*             are enough, */
546         /*   + add any bitmap file  */
547         /*   + start the array (auto-readonly). */
548
549         if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
550                 if (c->export) {
551                         printf("MD_STARTED=already\n");
552                 } else if (c->verbose >= 0)
553                         pr_err("%s attached to %s which is already active.\n",
554                                devname, chosen_name);
555                 rv = 0;
556                 goto out_unlock;
557         }
558
559         map_unlock(&map);
560         if (c->runstop > 0 || (!journal_device_missing && active_disks >= info.array.working_disks)) {
561                 struct mdinfo *dsk;
562                 /* Let's try to start it */
563
564                 if (journal_device_missing)
565                         pr_err("Trying to run with missing journal device\n");
566                 if (info.reshape_active && !(info.reshape_active & RESHAPE_NO_BACKUP)) {
567                         pr_err("%s: This array is being reshaped and cannot be started\n",
568                                chosen_name);
569                         cont_err("by --incremental.  Please use --assemble\n");
570                         goto out;
571                 }
572                 if (match && match->bitmap_file) {
573                         int bmfd = open(match->bitmap_file, O_RDWR);
574                         if (bmfd < 0) {
575                                 pr_err("Could not open bitmap file %s.\n",
576                                        match->bitmap_file);
577                                 goto out;
578                         }
579                         if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
580                                 close(bmfd);
581                                 pr_err("Failed to set bitmapfile for %s.\n",
582                                        chosen_name);
583                                 goto out;
584                         }
585                         close(bmfd);
586                 }
587                 /* Need to remove from the array any devices which
588                  * 'count_active' discerned were too old or inappropriate
589                  */
590                 for (d = sra ? sra->devs : NULL ; d ; d = d->next)
591                         if (d->disk.state & (1<<MD_DISK_REMOVED))
592                                 remove_disk(mdfd, st, sra, d);
593
594                 if ((sra == NULL || active_disks >= info.array.working_disks)
595                     && trustworthy != FOREIGN)
596                         rv = ioctl(mdfd, RUN_ARRAY, NULL);
597                 else
598                         rv = sysfs_set_str(sra, NULL,
599                                            "array_state", "read-auto");
600                 /* Array might be O_EXCL which  will interfere with
601                  * fsck and mount.  So re-open without O_EXCL.
602                  */
603                 reopen_mddev(mdfd);
604                 if (rv == 0) {
605                         if (c->export) {
606                                 printf("MD_STARTED=yes\n");
607                         } else if (c->verbose >= 0)
608                                 pr_err("%s attached to %s, which has been started.\n",
609                                        devname, chosen_name);
610                         rv = 0;
611                         wait_for(chosen_name, mdfd);
612                         /* We just started the array, so some devices
613                          * might have been evicted from the array
614                          * because their event counts were too old.
615                          * If the action=re-add policy is in-force for
616                          * those devices we should re-add them now.
617                          */
618                         for (dsk = sra->devs; dsk ; dsk = dsk->next) {
619                                 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
620                                     add_disk(mdfd, st, sra, dsk) == 0)
621                                         pr_err("%s re-added to %s\n",
622                                                dsk->sys_name, chosen_name);
623                         }
624                 } else {
625                         pr_err("%s attached to %s, but failed to start: %s.\n",
626                                devname, chosen_name, strerror(errno));
627                         rv = 1;
628                 }
629         } else {
630                 if (c->export) {
631                         printf("MD_STARTED=unsafe\n");
632                 } else if (journal_device_missing) {
633                         pr_err("Journal device is missing, not safe to start yet.\n");
634                 } else if (c->verbose >= 0)
635                         pr_err("%s attached to %s, not enough to start safely.\n",
636                                devname, chosen_name);
637                 rv = 0;
638         }
639 out:
640         free(avail);
641         if (dfd >= 0)
642                 close(dfd);
643         if (mdfd >= 0)
644                 close(mdfd);
645         if (policy)
646                 dev_policy_free(policy);
647         sysfs_free(sra);
648         return rv;
649 out_unlock:
650         map_unlock(&map);
651         goto out;
652 }
653
654 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
655                         int number, __u64 events, int verbose,
656                         char *array_name)
657 {
658         /* Find a device attached to this array with a disk.number of number
659          * and events less than the passed events, and remove the device.
660          */
661         struct mdinfo *d;
662         mdu_array_info_t ra;
663
664         if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
665                 return; /* not safe to remove from active arrays
666                          * without thinking more */
667
668         for (d = sra->devs; d ; d = d->next) {
669                 char dn[24]; // 2*11 bytes for ints (including sign) + colon + null byte
670                 int dfd;
671                 struct mdinfo info;
672                 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
673                 dfd = dev_open(dn, O_RDONLY);
674                 if (dfd < 0)
675                         continue;
676                 if (st->ss->load_super(st, dfd, NULL)) {
677                         close(dfd);
678                         continue;
679                 }
680                 st->ss->getinfo_super(st, &info, NULL);
681                 st->ss->free_super(st);
682                 close(dfd);
683
684                 if (info.disk.number != number ||
685                     info.events >= events)
686                         continue;
687
688                 if (d->disk.raid_disk > -1)
689                         sysfs_set_str(sra, d, "slot", "none");
690                 if (sysfs_set_str(sra, d, "state", "remove") == 0)
691                         if (verbose >= 0)
692                                 pr_err("removing old device %s from %s\n",
693                                        d->sys_name+4, array_name);
694         }
695 }
696
697 static int count_active(struct supertype *st, struct mdinfo *sra,
698                         int mdfd, char **availp,
699                         struct mdinfo *bestinfo)
700 {
701         /* count how many devices in sra think they are active */
702         struct mdinfo *d;
703         int cnt = 0;
704         int replcnt = 0;
705         __u64 max_events = 0;
706         char *avail = NULL;
707         int *best = NULL;
708         char *devmap = NULL;
709         int numdevs = 0;
710         int devnum;
711         int b, i;
712         int raid_disks = 0;
713
714         if (!sra)
715                 return 0;
716
717         for (d = sra->devs ; d ; d = d->next)
718                 numdevs++;
719         for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) {
720                 char dn[30];
721                 int dfd;
722                 int ok;
723                 struct mdinfo info;
724
725                 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
726                 dfd = dev_open(dn, O_RDONLY);
727                 if (dfd < 0)
728                         continue;
729                 ok =  st->ss->load_super(st, dfd, NULL);
730                 close(dfd);
731                 if (ok != 0)
732                         continue;
733
734                 info.array.raid_disks = raid_disks;
735                 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
736                 if (info.disk.raid_disk == MD_DISK_ROLE_JOURNAL)
737                         bestinfo->journal_clean = 1;
738                 if (!avail) {
739                         raid_disks = info.array.raid_disks;
740                         avail = xcalloc(raid_disks, 1);
741                         *availp = avail;
742
743                         best = xcalloc(raid_disks, sizeof(int));
744                         devmap = xcalloc(raid_disks, numdevs);
745
746                         st->ss->getinfo_super(st, &info, devmap);
747                 }
748
749                 if (info.disk.state & (1<<MD_DISK_SYNC))
750                 {
751                         if (cnt == 0) {
752                                 cnt++;
753                                 max_events = info.events;
754                                 avail[info.disk.raid_disk] = 2;
755                                 best[info.disk.raid_disk] = devnum;
756                                 st->ss->getinfo_super(st, bestinfo, NULL);
757                         } else if (info.events == max_events) {
758                                 avail[info.disk.raid_disk] = 2;
759                                 best[info.disk.raid_disk] = devnum;
760                         } else if (info.events == max_events-1) {
761                                 if (avail[info.disk.raid_disk] == 0) {
762                                         avail[info.disk.raid_disk] = 1;
763                                         best[info.disk.raid_disk] = devnum;
764                                 }
765                         } else if (info.events < max_events - 1)
766                                 ;
767                         else if (info.events == max_events+1) {
768                                 int i;
769                                 max_events = info.events;
770                                 for (i = 0; i < raid_disks; i++)
771                                         if (avail[i])
772                                                 avail[i]--;
773                                 avail[info.disk.raid_disk] = 2;
774                                 best[info.disk.raid_disk] = devnum;
775                                 st->ss->getinfo_super(st, bestinfo, NULL);
776                         } else { /* info.events much bigger */
777                                 memset(avail, 0, raid_disks);
778                                 max_events = info.events;
779                                 avail[info.disk.raid_disk] = 2;
780                                 best[info.disk.raid_disk] = devnum;
781                                 st->ss->getinfo_super(st, bestinfo, NULL);
782                         }
783                 } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT))
784                         replcnt++;
785                 st->ss->free_super(st);
786         }
787
788         if (!avail)
789                 return 0;
790         /* We need to reject any device that thinks the best device is
791          * failed or missing */
792         for (b = 0; b < raid_disks; b++)
793                 if (avail[b] == 2)
794                         break;
795         cnt = 0;
796         for (i = 0 ; i < raid_disks ; i++) {
797                 if (i != b && avail[i])
798                         if (devmap[raid_disks * best[i] + b] == 0) {
799                                 /* This device thinks 'b' is failed -
800                                  * don't use it */
801                                 devnum = best[i];
802                                 for (d=sra->devs ; devnum; d = d->next)
803                                         devnum--;
804                                 d->disk.state |= (1 << MD_DISK_REMOVED);
805                                 avail[i] = 0;
806                         }
807                 if (avail[i])
808                         cnt++;
809         }
810         /* Also need to reject any spare device with an event count that
811          * is too high
812          */
813         for (d = sra->devs; d; d = d->next) {
814                 if (!(d->disk.state & (1<<MD_DISK_SYNC)) &&
815                     d->events > max_events)
816                         d->disk.state |= (1 << MD_DISK_REMOVED);
817         }
818         free(best);
819         free(devmap);
820         return cnt + replcnt;
821 }
822
823 /* test if container has degraded member(s) */
824 static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
825 {
826         mdu_array_info_t array;
827         int afd;
828         int max_degraded = 0;
829
830         for(; map; map = map->next) {
831                 if (!metadata_container_matches(map->metadata, me->devnm))
832                         continue;
833                 afd = open_dev(map->devnm);
834                 if (afd < 0)
835                         continue;
836                 /* most accurate information regarding array degradation */
837                 if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
838                         int degraded = array.raid_disks - array.active_disks -
839                                        array.spare_disks;
840                         if (degraded > max_degraded)
841                                 max_degraded = degraded;
842                 }
843                 close(afd);
844         }
845         return (max_degraded);
846 }
847
848 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
849                            struct map_ent *target, int bare,
850                            struct supertype *st, int verbose)
851 {
852         /* This device doesn't have any md metadata
853          * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
854          * If 'st' is not set, then we only know that some metadata allows this,
855          * others possibly don't.
856          * So look for a container or array to attach the device to.
857          * Prefer 'target' if that is set and the array is found.
858          *
859          * If st is set, then only arrays of that type are considered
860          * Return 0 on success, or some exit code on failure, probably 1.
861          */
862         int rv = 1;
863         struct stat stb;
864         struct map_ent *mp, *map = NULL;
865         struct mdinfo *chosen = NULL;
866         int dfd = *dfdp;
867
868         if (fstat(dfd, &stb) != 0)
869                 return 1;
870
871         /*
872          * Now we need to find a suitable array to add this to.
873          * We only accept arrays that:
874          *  - match 'st'
875          *  - are in the same domains as the device
876          *  - are of an size for which the device will be useful
877          * and we choose the one that is the most degraded
878          */
879
880         if (map_lock(&map)) {
881                 pr_err("failed to get exclusive lock on mapfile\n");
882                 return 1;
883         }
884         for (mp = map ; mp ; mp = mp->next) {
885                 struct supertype *st2;
886                 struct domainlist *dl = NULL;
887                 struct mdinfo *sra;
888                 unsigned long long devsize;
889                 unsigned long long component_size = 0;
890
891                 if (is_subarray(mp->metadata))
892                         continue;
893                 if (st) {
894                         st2 = st->ss->match_metadata_desc(mp->metadata);
895                         if (!st2 ||
896                             (st->minor_version >= 0 &&
897                              st->minor_version != st2->minor_version)) {
898                                 if (verbose > 1)
899                                         pr_err("not adding %s to %s as metadata type doesn't match\n",
900                                                 devname, mp->path);
901                                 free(st2);
902                                 continue;
903                         }
904                         free(st2);
905                 }
906                 sra = sysfs_read(-1, mp->devnm,
907                                  GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
908                                  GET_DEGRADED|GET_COMPONENT|GET_VERSION);
909                 if (!sra) {
910                         /* Probably a container - no degraded info */
911                         sra = sysfs_read(-1, mp->devnm,
912                                          GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
913                                          GET_COMPONENT|GET_VERSION);
914                         if (sra)
915                                 sra->array.failed_disks = -1;
916                 }
917                 if (!sra)
918                         continue;
919                 if (st == NULL) {
920                         int i;
921                         st2 = NULL;
922                         for(i = 0; !st2 && superlist[i]; i++)
923                                 st2 = superlist[i]->match_metadata_desc(
924                                         sra->text_version);
925                         if (!st2) {
926                                 if (verbose > 1)
927                                         pr_err("not adding %s to %s as metadata not recognised.\n",
928                                                 devname, mp->path);
929                                 goto next;
930                         }
931                         /* Need to double check the 'act_spare' permissions applies
932                          * to this metadata.
933                          */
934                         if (!policy_action_allows(pol, st2->ss->name, act_spare))
935                                 goto next;
936                         if (!bare && !policy_action_allows(pol, st2->ss->name,
937                                                            act_spare_same_slot))
938                                 goto next;
939                 } else
940                         st2 = st;
941                 /* update number of failed disks for mostly degraded
942                  * container member */
943                 if (sra->array.failed_disks == -1)
944                         sra->array.failed_disks = container_members_max_degradation(map, mp);
945
946                 get_dev_size(dfd, NULL, &devsize);
947                 if (sra->component_size == 0) {
948                         /* true for containers, here we must read superblock
949                          * to obtain minimum spare size */
950                         struct supertype *st3 = dup_super(st2);
951                         int mdfd = open_dev(mp->devnm);
952                         if (mdfd < 0) {
953                                 free(st3);
954                                 goto next;
955                         }
956                         if (st3->ss->load_container &&
957                             !st3->ss->load_container(st3, mdfd, mp->path)) {
958                                 component_size = st3->ss->min_acceptable_spare_size(st3);
959                                 st3->ss->free_super(st3);
960                         }
961                         free(st3);
962                         close(mdfd);
963                 }
964                 if ((sra->component_size > 0 &&
965                      st2->ss->avail_size(st2, devsize,
966                                          sra->devs
967                                          ? sra->devs->data_offset
968                                          : INVALID_SECTORS)
969                      < sra->component_size)
970                     ||
971                     (sra->component_size == 0 && devsize < component_size)) {
972                         if (verbose > 1)
973                                 pr_err("not adding %s to %s as it is too small\n",
974                                         devname, mp->path);
975                         goto next;
976                 }
977                 /* test against target.
978                  * If 'target' is set and 'bare' is false, we only accept
979                  * arrays/containers that match 'target'.
980                  * If 'target' is set and 'bare' is true, we prefer the
981                  * array which matches 'target'.
982                  * target is considered only if we deal with degraded array
983                  */
984                 if (target && policy_action_allows(pol, st2->ss->name,
985                                                    act_spare_same_slot)) {
986                         if (strcmp(target->metadata, mp->metadata) == 0 &&
987                             memcmp(target->uuid, mp->uuid,
988                                    sizeof(target->uuid)) == 0 &&
989                             sra->array.failed_disks > 0) {
990                                 /* This is our target!! */
991                                 sysfs_free(chosen);
992                                 chosen = sra;
993                                 sra = NULL;
994                                 /* skip to end so we don't check any more */
995                                 while (mp->next)
996                                         mp = mp->next;
997                                 goto next;
998                         }
999                         /* not our target */
1000                         if (!bare)
1001                                 goto next;
1002                 }
1003
1004                 dl = domain_from_array(sra, st2->ss->name);
1005                 if (domain_test(dl, pol, st2->ss->name) != 1) {
1006                         /* domain test fails */
1007                         if (verbose > 1)
1008                                 pr_err("not adding %s to %s as it is not in a compatible domain\n",
1009                                         devname, mp->path);
1010
1011                         goto next;
1012                 }
1013                 /* all tests passed, OK to add to this array */
1014                 if (!chosen) {
1015                         chosen = sra;
1016                         sra = NULL;
1017                 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
1018                         sysfs_free(chosen);
1019                         chosen = sra;
1020                         sra = NULL;
1021                 }
1022         next:
1023                 sysfs_free(sra);
1024                 if (st != st2)
1025                         free(st2);
1026                 if (dl)
1027                         domain_free(dl);
1028         }
1029         if (chosen) {
1030                 /* add current device to chosen array as a spare */
1031                 int mdfd = open_dev(chosen->sys_name);
1032                 if (mdfd >= 0) {
1033                         struct mddev_dev devlist;
1034                         char chosen_devname[24]; // 2*11 for int (including signs) + colon + null
1035                         devlist.next = NULL;
1036                         devlist.used = 0;
1037                         devlist.writemostly = FlagDefault;
1038                         devlist.failfast = FlagDefault;
1039                         devlist.devname = chosen_devname;
1040                         sprintf(chosen_devname, "%d:%d", major(stb.st_rdev),
1041                                 minor(stb.st_rdev));
1042                         devlist.disposition = 'a';
1043                         close(dfd);
1044                         *dfdp = -1;
1045                         rv =  Manage_subdevs(chosen->sys_name, mdfd, &devlist,
1046                                              -1, 0, NULL, 0);
1047                         close(mdfd);
1048                 }
1049                 if (verbose > 0) {
1050                         if (rv == 0)
1051                                 pr_err("added %s as spare for %s\n",
1052                                         devname, chosen->sys_name);
1053                         else
1054                                 pr_err("failed to add %s as spare for %s\n",
1055                                         devname, chosen->sys_name);
1056                 }
1057                 sysfs_free(chosen);
1058         }
1059         map_unlock(&map);
1060         return rv;
1061 }
1062
1063 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1064                                struct supertype *st, int verbose)
1065 {
1066         /* we know that at least one partition virtual-metadata is
1067          * allowed to incorporate spares like this device.  We need to
1068          * find a suitable device to copy partition information from.
1069          *
1070          * Getting a list of all disk (not partition) devices is
1071          * slightly non-trivial.  We could look at /sys/block, but
1072          * that is theoretically due to be removed.  Maybe best to use
1073          * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1074          * we depend on this directory of 'path' info.  But that fails
1075          * to find loop devices and probably others.  Maybe don't
1076          * worry about that, they aren't the real target.
1077          *
1078          * So: check things in /dev/disk/by-path to see if they are in
1079          * a compatible domain, then load the partition table and see
1080          * if it is OK for the new device, and choose the largest
1081          * partition table that fits.
1082          */
1083         DIR *dir;
1084         struct dirent *de;
1085         char *chosen = NULL;
1086         unsigned long long chosen_size = 0;
1087         struct supertype *chosen_st = NULL;
1088         int fd;
1089
1090         dir = opendir("/dev/disk/by-path");
1091         if (!dir)
1092                 return 1;
1093         while ((de = readdir(dir)) != NULL) {
1094                 char *ep;
1095                 struct dev_policy *pol2 = NULL;
1096                 struct domainlist *domlist = NULL;
1097                 int fd = -1;
1098                 struct mdinfo info;
1099                 struct supertype *st2 = NULL;
1100                 char *devname = NULL;
1101                 unsigned long long devsectors;
1102
1103                 if (de->d_ino == 0 ||
1104                     de->d_name[0] == '.' ||
1105                     (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1106                         goto next;
1107
1108                 ep = de->d_name + strlen(de->d_name);
1109                 while (ep > de->d_name &&
1110                        isdigit(ep[-1]))
1111                         ep--;
1112                 if (ep > de->d_name + 5 &&
1113                     strncmp(ep-5, "-part", 5) == 0)
1114                         /* This is a partition - skip it */
1115                         goto next;
1116
1117                 pol2 = path_policy(de->d_name, type_disk);
1118
1119                 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
1120                 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
1121                         /* new device is incompatible with this device. */
1122                         goto next;
1123
1124                 domain_free(domlist);
1125                 domlist = NULL;
1126
1127                 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1128                         devname = NULL;
1129                         goto next;
1130                 }
1131                 fd = open(devname, O_RDONLY);
1132                 if (fd < 0)
1133                         goto next;
1134                 if (get_dev_size(fd, devname, &devsectors) == 0)
1135                         goto next;
1136                 devsectors >>= 9;
1137
1138                 if (st)
1139                         st2 = dup_super(st);
1140                 else
1141                         st2 = guess_super_type(fd, guess_partitions);
1142                 if (st2 == NULL ||
1143                     st2->ss->load_super(st2, fd, NULL) < 0)
1144                         goto next;
1145                 st2->ignore_hw_compat = 0;
1146
1147                 if (!st) {
1148                         /* Check domain policy again, this time referring to metadata */
1149                         domain_merge(&domlist, pol2, st2->ss->name);
1150                         if (domain_test(domlist, pol, st2->ss->name) != 1)
1151                                 /* Incompatible devices for this metadata type */
1152                                 goto next;
1153                         if (!policy_action_allows(pol, st2->ss->name, act_spare))
1154                                 /* Some partition types allow sparing, but not
1155                                  * this one.
1156                                  */
1157                                 goto next;
1158                 }
1159
1160                 st2->ss->getinfo_super(st2, &info, NULL);
1161                 if (info.component_size > devsectors)
1162                         /* This partitioning doesn't fit in the device */
1163                         goto next;
1164
1165                 /* This is an acceptable device to copy partition
1166                  * metadata from.  We could just stop here, but I
1167                  * think I want to keep looking incase a larger
1168                  * metadata which makes better use of the device can
1169                  * be found.
1170                  */
1171                 if (chosen == NULL ||
1172                     chosen_size < info.component_size) {
1173                         chosen_size = info.component_size;
1174                         free(chosen);
1175                         chosen = devname;
1176                         devname = NULL;
1177                         if (chosen_st) {
1178                                 chosen_st->ss->free_super(chosen_st);
1179                                 free(chosen_st);
1180                         }
1181                         chosen_st = st2;
1182                         st2 = NULL;
1183                 }
1184
1185         next:
1186                 free(devname);
1187                 domain_free(domlist);
1188                 dev_policy_free(pol2);
1189                 if (st2)
1190                         st2->ss->free_super(st2);
1191                 free(st2);
1192
1193                 if (fd >= 0)
1194                         close(fd);
1195         }
1196
1197         closedir(dir);
1198
1199         if (!chosen)
1200                 return 1;
1201
1202         /* 'chosen' is the best device we can find.  Let's write its
1203          * metadata to devname dfd is read-only so don't use that
1204          */
1205         fd = open(devname, O_RDWR);
1206         if (fd >= 0) {
1207                 chosen_st->ss->store_super(chosen_st, fd);
1208                 close(fd);
1209         }
1210         free(chosen);
1211         chosen_st->ss->free_super(chosen_st);
1212         free(chosen_st);
1213         return 0;
1214 }
1215
1216 static int is_bare(int dfd)
1217 {
1218         unsigned long long size = 0;
1219         char bufpad[4096 + 4096];
1220         char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1221
1222         if (lseek(dfd, 0, SEEK_SET) != 0 ||
1223             read(dfd, buf, 4096) != 4096)
1224                 return 0;
1225
1226         if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1227                 return 0;
1228         if (memcmp(buf, buf+1, 4095) != 0)
1229                 return 0;
1230
1231         /* OK, first 4K appear blank, try the end. */
1232         get_dev_size(dfd, NULL, &size);
1233         if (lseek(dfd, size-4096, SEEK_SET) < 0 ||
1234             read(dfd, buf, 4096) != 4096)
1235                 return 0;
1236
1237         if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1238                 return 0;
1239         if (memcmp(buf, buf+1, 4095) != 0)
1240                 return 0;
1241
1242         return 1;
1243 }
1244
1245 /* adding a spare to a regular array is quite different from adding one to
1246  * a set-of-partitions virtual array.
1247  * This function determines which is worth trying and tries as appropriate.
1248  * Arrays are given priority over partitions.
1249  */
1250 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1251                      struct map_ent *target,
1252                      struct supertype *st, int verbose)
1253 {
1254         int i;
1255         int rv;
1256         int arrays_ok = 0;
1257         int partitions_ok = 0;
1258         int dfd = *dfdp;
1259         int bare;
1260
1261         /* Can only add a spare if device has at least one domain */
1262         if (pol_find(pol, pol_domain) == NULL)
1263                 return 1;
1264         /* And only if some action allows spares */
1265         if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1266                 return 1;
1267
1268         /* Now check if the device is bare.
1269          * bare devices can always be added as a spare
1270          * non-bare devices can only be added if spare-same-slot is permitted,
1271          * and this device is replacing a previous device - in which case 'target'
1272          * will be set.
1273          */
1274         if (!is_bare(dfd)) {
1275                 /* Must have a target and allow same_slot */
1276                 /* Later - may allow force_spare without target */
1277                 if (!target ||
1278                     !policy_action_allows(pol, st?st->ss->name:NULL,
1279                                           act_spare_same_slot)) {
1280                         if (verbose > 1)
1281                                 pr_err("%s is not bare, so not considering as a spare\n",
1282                                         devname);
1283                         return 1;
1284                 }
1285                 bare = 0;
1286         } else
1287                 bare = 1;
1288
1289         /* It might be OK to add this device to an array - need to see
1290          * what arrays might be candidates.
1291          */
1292         if (st) {
1293                 /* just try try 'array' or 'partition' based on this metadata */
1294                 if (st->ss->add_to_super)
1295                         return array_try_spare(devname, dfdp, pol, target, bare,
1296                                                st, verbose);
1297                 else
1298                         return partition_try_spare(devname, dfdp, pol,
1299                                                    st, verbose);
1300         }
1301         /* No metadata was specified or found so options are open.
1302          * Check for whether any array metadata, or any partition metadata
1303          * might allow adding the spare.  This check is just help to avoid
1304          * a more costly scan of all arrays when we can be sure that will
1305          * fail.
1306          */
1307         for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1308                 if (superlist[i]->add_to_super && !arrays_ok &&
1309                     policy_action_allows(pol, superlist[i]->name, act_spare))
1310                         arrays_ok = 1;
1311                 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1312                     policy_action_allows(pol, superlist[i]->name, act_spare))
1313                         partitions_ok = 1;
1314         }
1315         rv = 1;
1316         if (arrays_ok)
1317                 rv = array_try_spare(devname, dfdp, pol, target, bare,
1318                                      st, verbose);
1319         if (rv != 0 && partitions_ok)
1320                 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1321         return rv;
1322 }
1323
1324 int IncrementalScan(struct context *c, char *devnm)
1325 {
1326         /* look at every device listed in the 'map' file.
1327          * If one is found that is not running then:
1328          *  look in mdadm.conf for bitmap file.
1329          *   if one exists, but array has none, add it.
1330          *  try to start array in auto-readonly mode
1331          */
1332         struct map_ent *mapl = NULL;
1333         struct map_ent *me;
1334         struct mddev_ident *devs, *mddev;
1335         int rv = 0;
1336         char container[32];
1337         char *only = NULL;
1338
1339         map_read(&mapl);
1340         devs = conf_get_ident(NULL);
1341
1342 restart:
1343         for (me = mapl ; me ; me = me->next) {
1344                 mdu_array_info_t array;
1345                 mdu_bitmap_file_t bmf;
1346                 struct mdinfo *sra;
1347                 int mdfd;
1348
1349                 if (devnm && strcmp(devnm, me->devnm) != 0)
1350                         continue;
1351                 if (me->metadata[0] == '/') {
1352                         char *sl;
1353
1354                         if (!devnm)
1355                                 continue;
1356
1357                         /* member array, need to work on container */
1358                         strncpy(container, me->metadata+1, 32);
1359                         container[31] = 0;
1360                         sl = strchr(container, '/');
1361                         if (sl)
1362                                 *sl = 0;
1363                         only = devnm;
1364                         devnm = container;
1365                         goto restart;
1366                 }
1367                 mdfd = open_dev(me->devnm);
1368
1369                 if (mdfd < 0)
1370                         continue;
1371                 if (!isdigit(me->metadata[0])) {
1372                         /* must be a container */
1373                         struct supertype *st = super_by_fd(mdfd, NULL);
1374                         int ret = 0;
1375                         struct map_ent *map = NULL;
1376
1377                         if (st && st->ss->load_container)
1378                                 ret = st->ss->load_container(st, mdfd, NULL);
1379                         close(mdfd);
1380                         if (!ret && st && st->ss->container_content) {
1381                                 if (map_lock(&map))
1382                                         pr_err("failed to get exclusive lock on mapfile\n");
1383                                 ret = Incremental_container(st, me->path, c, only);
1384                                 map_unlock(&map);
1385                         }
1386                         if (ret)
1387                                 rv = 1;
1388                         continue;
1389                 }
1390                 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1391                     errno != ENODEV) {
1392                         close(mdfd);
1393                         continue;
1394                 }
1395                 /* Ok, we can try this one.   Maybe it needs a bitmap */
1396                 for (mddev = devs ; mddev ; mddev = mddev->next)
1397                         if (mddev->devname && me->path
1398                             && devname_matches(mddev->devname, me->path))
1399                                 break;
1400                 if (mddev && mddev->bitmap_file) {
1401                         /*
1402                          * Note: early kernels will wrongly fail this, so it
1403                          * is a hint only
1404                          */
1405                         int added = -1;
1406                         if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1407                                 int bmfd = open(mddev->bitmap_file, O_RDWR);
1408                                 if (bmfd >= 0) {
1409                                         added = ioctl(mdfd, SET_BITMAP_FILE,
1410                                                       bmfd);
1411                                         close(bmfd);
1412                                 }
1413                         }
1414                         if (c->verbose >= 0) {
1415                                 if (added == 0)
1416                                         pr_err("Added bitmap %s to %s\n",
1417                                                mddev->bitmap_file, me->path);
1418                                 else if (errno != EEXIST)
1419                                         pr_err("Failed to add bitmap to %s: %s\n",
1420                                                me->path, strerror(errno));
1421                         }
1422                 }
1423                 /* FIXME check for reshape_active and consider not
1424                  * starting array.
1425                  */
1426                 sra = sysfs_read(mdfd, NULL, 0);
1427                 if (sra) {
1428                         if (sysfs_set_str(sra, NULL,
1429                                           "array_state", "read-auto") == 0) {
1430                                 if (c->verbose >= 0)
1431                                         pr_err("started array %s\n",
1432                                                me->path ?: me->devnm);
1433                         } else {
1434                                 pr_err("failed to start array %s: %s\n",
1435                                        me->path ?: me->devnm,
1436                                        strerror(errno));
1437                                 rv = 1;
1438                         }
1439                         sysfs_free(sra);
1440                 }
1441         }
1442         return rv;
1443 }
1444
1445 static char *container2devname(char *devname)
1446 {
1447         char *mdname = NULL;
1448
1449         if (devname[0] == '/') {
1450                 int fd = open(devname, O_RDONLY);
1451                 if (fd >= 0) {
1452                         mdname = xstrdup(fd2devnm(fd));
1453                         close(fd);
1454                 }
1455         } else {
1456                 int uuid[4];
1457                 struct map_ent *mp, *map = NULL;
1458
1459                 if (!parse_uuid(devname, uuid))
1460                         return mdname;
1461                 mp = map_by_uuid(&map, uuid);
1462                 if (mp)
1463                         mdname = xstrdup(mp->devnm);
1464                 map_free(map);
1465         }
1466
1467         return mdname;
1468 }
1469
1470 static int Incremental_container(struct supertype *st, char *devname,
1471                                  struct context *c, char *only)
1472 {
1473         /* Collect the contents of this container and for each
1474          * array, choose a device name and assemble the array.
1475          */
1476
1477         struct mdinfo *list;
1478         struct mdinfo *ra;
1479         struct map_ent *map = NULL;
1480         struct mdinfo info;
1481         int trustworthy;
1482         struct mddev_ident *match;
1483         int rv = 0;
1484         struct domainlist *domains;
1485         struct map_ent *smp;
1486         int suuid[4];
1487         int sfd;
1488         int ra_blocked = 0;
1489         int ra_all = 0;
1490         int result = 0;
1491
1492         st->ss->getinfo_super(st, &info, NULL);
1493
1494         if ((c->runstop > 0 && info.container_enough >= 0) ||
1495             info.container_enough > 0)
1496                 /* pass */;
1497         else {
1498                 if (c->export) {
1499                         printf("MD_STARTED=no\n");
1500                 } else if (c->verbose)
1501                         pr_err("not enough devices to start the container\n");
1502                 return 0;
1503         }
1504
1505         match = conf_match(st, &info, devname, c->verbose, &rv);
1506         if (match == NULL && rv == 2)
1507                 return rv;
1508
1509         /* Need to compute 'trustworthy' */
1510         if (match)
1511                 trustworthy = LOCAL;
1512         else if (st->ss->match_home(st, c->homehost) == 1)
1513                 trustworthy = LOCAL;
1514         else if (st->ss->match_home(st, "any") == 1)
1515                 trustworthy = LOCAL;
1516         else
1517                 trustworthy = FOREIGN;
1518
1519         list = st->ss->container_content(st, NULL);
1520         /* when nothing to activate - quit */
1521         if (list == NULL) {
1522                 if (c->export) {
1523                         printf("MD_STARTED=nothing\n");
1524                 }
1525                 return 0;
1526         }
1527         for (ra = list ; ra ; ra = ra->next) {
1528                 int mdfd;
1529                 char chosen_name[1024];
1530                 struct map_ent *mp;
1531                 struct mddev_ident *match = NULL;
1532
1533                 ra_all++;
1534                 /* do not activate arrays blocked by metadata handler */
1535                 if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) {
1536                         pr_err("Cannot activate array %s in %s.\n",
1537                                 ra->text_version, devname);
1538                         ra_blocked++;
1539                         continue;
1540                 }
1541                 mp = map_by_uuid(&map, ra->uuid);
1542
1543                 if (mp) {
1544                         mdfd = open_dev(mp->devnm);
1545                         if (mp->path)
1546                                 strcpy(chosen_name, mp->path);
1547                         else
1548                                 strcpy(chosen_name, mp->devnm);
1549                 } else if (!only) {
1550
1551                         /* Check in mdadm.conf for container == devname and
1552                          * member == ra->text_version after second slash.
1553                          */
1554                         char *sub = strchr(ra->text_version+1, '/');
1555                         struct mddev_ident *array_list;
1556                         if (sub) {
1557                                 sub++;
1558                                 array_list = conf_get_ident(NULL);
1559                         } else
1560                                 array_list = NULL;
1561                         for(; array_list ; array_list = array_list->next) {
1562                                 char *dn;
1563                                 if (array_list->member == NULL ||
1564                                     array_list->container == NULL)
1565                                         continue;
1566                                 if (strcmp(array_list->member, sub) != 0)
1567                                         continue;
1568                                 if (array_list->uuid_set &&
1569                                     !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1570                                         continue;
1571                                 dn = container2devname(array_list->container);
1572                                 if (dn == NULL)
1573                                         continue;
1574                                 if (strncmp(dn, ra->text_version+1,
1575                                             strlen(dn)) != 0 ||
1576                                     ra->text_version[strlen(dn)+1] != '/') {
1577                                         free(dn);
1578                                         continue;
1579                                 }
1580                                 free(dn);
1581                                 /* we have a match */
1582                                 match = array_list;
1583                                 if (c->verbose>0)
1584                                         pr_err("match found for member %s\n",
1585                                                 array_list->member);
1586                                 break;
1587                         }
1588
1589                         if (match && match->devname &&
1590                             strcasecmp(match->devname, "<ignore>") == 0) {
1591                                 if (c->verbose > 0)
1592                                         pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1593                                                match->container, match->member);
1594                                 continue;
1595                         }
1596                         if (match)
1597                                 trustworthy = LOCAL;
1598
1599                         mdfd = create_mddev(match ? match->devname : NULL,
1600                                             ra->name,
1601                                             c->autof,
1602                                             trustworthy,
1603                                             chosen_name);
1604                 }
1605                 if (only && (!mp || strcmp(mp->devnm, only) != 0))
1606                         continue;
1607
1608                 if (mdfd < 0) {
1609                         pr_err("failed to open %s: %s.\n",
1610                                 chosen_name, strerror(errno));
1611                         return 2;
1612                 }
1613
1614                 assemble_container_content(st, mdfd, ra, c,
1615                                            chosen_name, &result);
1616                 close(mdfd);
1617         }
1618         if (c->export && result) {
1619                 char sep = '=';
1620                 printf("MD_STARTED");
1621                 if (result & INCR_NO) {
1622                         printf("%cno", sep);
1623                         sep = ',';
1624                 }
1625                 if (result & INCR_UNSAFE) {
1626                         printf("%cunsafe", sep);
1627                         sep = ',';
1628                 }
1629                 if (result & INCR_ALREADY) {
1630                         printf("%calready", sep);
1631                         sep = ',';
1632                 }
1633                 if (result & INCR_YES) {
1634                         printf("%cyes", sep);
1635                         sep = ',';
1636                 }
1637                 printf("\n");
1638         }
1639
1640         /* don't move spares to container with volume being activated
1641            when all volumes are blocked */
1642         if (ra_all == ra_blocked)
1643                 return 0;
1644
1645         /* Now move all suitable spares from spare container */
1646         domains = domain_from_array(list, st->ss->name);
1647         memcpy(suuid, uuid_zero, sizeof(int[4]));
1648         if (domains &&
1649             (smp = map_by_uuid(&map, suuid)) != NULL &&
1650             (sfd = open(smp->path, O_RDONLY)) >= 0) {
1651                 /* spare container found */
1652                 struct supertype *sst =
1653                         super_imsm.match_metadata_desc("imsm");
1654                 struct mdinfo *sinfo;
1655                 unsigned long long min_size = 0;
1656                 if (st->ss->min_acceptable_spare_size)
1657                         min_size = st->ss->min_acceptable_spare_size(st);
1658                 if (!sst->ss->load_container(sst, sfd, NULL)) {
1659                         close(sfd);
1660                         sinfo = container_choose_spares(sst, min_size,
1661                                                         domains, NULL,
1662                                                         st->ss->name, 0);
1663                         sst->ss->free_super(sst);
1664                         if (sinfo){
1665                                 int count = 0;
1666                                 struct mdinfo *disks = sinfo->devs;
1667                                 while (disks) {
1668                                         /* move spare from spare
1669                                          * container to currently
1670                                          * assembled one
1671                                          */
1672                                         if (move_spare(
1673                                                     smp->path,
1674                                                     devname,
1675                                                     makedev(disks->disk.major,
1676                                                             disks->disk.minor)))
1677                                                 count++;
1678                                         disks = disks->next;
1679                                 }
1680                                 if (count)
1681                                         pr_err("Added %d spare%s to %s\n",
1682                                                count, count>1?"s":"", devname);
1683                         }
1684                         sysfs_free(sinfo);
1685                 } else
1686                         close(sfd);
1687         }
1688         domain_free(domains);
1689         return 0;
1690 }
1691
1692 static void run_udisks(char *arg1, char *arg2)
1693 {
1694         int pid = fork();
1695         int status;
1696         if (pid == 0) {
1697                 execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
1698                 execl("/bin/udisks", "udisks", arg1, arg2, NULL);
1699                 exit(1);
1700         }
1701         while (pid > 0 && wait(&status) != pid)
1702                 ;
1703 }
1704
1705 /*
1706  * IncrementalRemove - Attempt to see if the passed in device belongs to any
1707  * raid arrays, and if so first fail (if needed) and then remove the device.
1708  *
1709  * @devname - The device we want to remove
1710  * @id_path - name as found in /dev/disk/by-path for this device
1711  *
1712  * Note: the device name must be a kernel name like "sda", so
1713  * that we can find it in /proc/mdstat
1714  */
1715 int IncrementalRemove(char *devname, char *id_path, int verbose)
1716 {
1717         int mdfd;
1718         int rv = 0;
1719         struct mdstat_ent *ent;
1720         struct mddev_dev devlist;
1721         struct mdinfo mdi;
1722         char buf[32];
1723
1724         if (!id_path)
1725                 dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
1726
1727         if (strchr(devname, '/')) {
1728                 pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname);
1729                 return 1;
1730         }
1731         ent = mdstat_by_component(devname);
1732         if (!ent) {
1733                 if (verbose >= 0)
1734                         pr_err("%s does not appear to be a component of any array\n", devname);
1735                 return 1;
1736         }
1737         sysfs_init(&mdi, -1, ent->devnm);
1738         mdfd = open_dev_excl(ent->devnm);
1739         if (mdfd > 0) {
1740                 close(mdfd);
1741                 if (sysfs_get_str(&mdi, NULL, "array_state",
1742                                   buf, sizeof(buf)) > 0) {
1743                         if (strncmp(buf, "active", 6) == 0 ||
1744                             strncmp(buf, "clean", 5) == 0)
1745                                 sysfs_set_str(&mdi, NULL,
1746                                               "array_state", "read-auto");
1747                 }
1748         }
1749         mdfd = open_dev(ent->devnm);
1750         if (mdfd < 0) {
1751                 if (verbose >= 0)
1752                         pr_err("Cannot open array %s!!\n", ent->devnm);
1753                 free_mdstat(ent);
1754                 return 1;
1755         }
1756
1757         if (id_path) {
1758                 struct map_ent *map = NULL, *me;
1759                 me = map_by_devnm(&map, ent->devnm);
1760                 if (me)
1761                         policy_save_path(id_path, me);
1762                 map_free(map);
1763         }
1764
1765         memset(&devlist, 0, sizeof(devlist));
1766         devlist.devname = devname;
1767         devlist.disposition = 'f';
1768         /* for a container, we must fail each member array */
1769         if (ent->metadata_version &&
1770             strncmp(ent->metadata_version, "external:", 9) == 0) {
1771                 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1772                 struct mdstat_ent *memb;
1773                 for (memb = mdstat ; memb ; memb = memb->next)
1774                         if (is_container_member(memb, ent->devnm)) {
1775                                 int subfd = open_dev(memb->devnm);
1776                                 if (subfd >= 0) {
1777                                         rv |= Manage_subdevs(
1778                                                 memb->devnm, subfd,
1779                                                 &devlist, verbose, 0,
1780                                                 NULL, 0);
1781                                         close(subfd);
1782                                 }
1783                         }
1784                 free_mdstat(mdstat);
1785         } else
1786                 rv |= Manage_subdevs(ent->devnm, mdfd, &devlist,
1787                                     verbose, 0, NULL, 0);
1788         if (rv & 2) {
1789                 /* Failed due to EBUSY, try to stop the array.
1790                  * Give udisks a chance to unmount it first.
1791                  */
1792                 int devid = devnm2devid(ent->devnm);
1793                 run_udisks("--unmount", map_dev(major(devid),minor(devid), 0));
1794                 rv = Manage_stop(ent->devnm, mdfd, verbose, 1);
1795                 if (rv)
1796                         /* At least we can try to trigger a 'remove' */
1797                         sysfs_uevent(&mdi, "remove");
1798                 if (verbose) {
1799                         if (rv)
1800                                 pr_err("Fail to stop %s too.\n", ent->devnm);
1801                 }
1802         } else {
1803                 devlist.disposition = 'r';
1804                 rv = Manage_subdevs(ent->devnm, mdfd, &devlist,
1805                                     verbose, 0, NULL, 0);
1806         }
1807         close(mdfd);
1808         free_mdstat(ent);
1809         return rv;
1810 }