Check all member devices in enough_fd
[thirdparty/mdadm.git] / Manage.c
1 /*
2  * mdadm - manage Linux "md" devices aka RAID arrays.
3  *
4  * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
23  */
24
25 #include "mdadm.h"
26 #include "md_u.h"
27 #include "md_p.h"
28 #include <ctype.h>
29
30 #define REGISTER_DEV            _IO (MD_MAJOR, 1)
31 #define START_MD                _IO (MD_MAJOR, 2)
32 #define STOP_MD                 _IO (MD_MAJOR, 3)
33
34 int Manage_ro(char *devname, int fd, int readonly)
35 {
36         /* switch to readonly or rw
37          *
38          * requires >= 0.90.0
39          * first check that array is runing
40          * use RESTART_ARRAY_RW or STOP_ARRAY_RO
41          *
42          */
43         mdu_array_info_t array;
44 #ifndef MDASSEMBLE
45         struct mdinfo *mdi;
46 #endif
47
48         if (md_get_version(fd) < 9000) {
49                 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
50                 return 1;
51         }
52 #ifndef MDASSEMBLE
53         /* If this is an externally-manage array, we need to modify the
54          * metadata_version so that mdmon doesn't undo our change.
55          */
56         mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
57         if (mdi &&
58             mdi->array.major_version == -1 &&
59             is_subarray(mdi->text_version)) {
60                 char vers[64];
61                 strcpy(vers, "external:");
62                 strcat(vers, mdi->text_version);
63                 if (readonly > 0) {
64                         int rv;
65                         /* We set readonly ourselves. */
66                         vers[9] = '-';
67                         sysfs_set_str(mdi, NULL, "metadata_version", vers);
68
69                         close(fd);
70                         rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
71
72                         if (rv < 0) {
73                                 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
74                                         devname, strerror(errno));
75
76                                 vers[9] = mdi->text_version[0];
77                                 sysfs_set_str(mdi, NULL, "metadata_version", vers);
78                                 return 1;
79                         }
80                 } else {
81                         char *cp;
82                         /* We cannot set read/write - must signal mdmon */
83                         vers[9] = '/';
84                         sysfs_set_str(mdi, NULL, "metadata_version", vers);
85
86                         cp = strchr(vers+10, '/');
87                         if (*cp)
88                                 *cp = 0;
89                         ping_monitor(vers+10);
90                         if (mdi->array.level <= 0)
91                                 sysfs_set_str(mdi, NULL, "array_state", "active");
92                 }
93                 return 0;
94         }
95 #endif
96         if (ioctl(fd, GET_ARRAY_INFO, &array)) {
97                 fprintf(stderr, Name ": %s does not appear to be active.\n",
98                         devname);
99                 return 1;
100         }
101
102         if (readonly>0) {
103                 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
104                         fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
105                                 devname, strerror(errno));
106                         return 1;
107                 }
108         } else if (readonly < 0) {
109                 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
110                         fprintf(stderr, Name ": failed to set writable for %s: %s\n",
111                                 devname, strerror(errno));
112                         return 1;
113                 }
114         }
115         return 0;
116 }
117
118 #ifndef MDASSEMBLE
119
120 static void remove_devices(int devnum, char *path)
121 {
122         /* 
123          * Remove names at 'path' - possibly with
124          * partition suffixes - which link to the 'standard'
125          * name for devnum.  These were probably created
126          * by mdadm when the array was assembled.
127          */
128         char base[40];
129         char *path2;
130         char link[1024];
131         int n;
132         int part;
133         char *be;
134         char *pe;
135
136         if (!path)
137                 return;
138
139         if (devnum >= 0)
140                 sprintf(base, "/dev/md%d", devnum);
141         else
142                 sprintf(base, "/dev/md_d%d", -1-devnum);
143         be = base + strlen(base);
144
145         path2 = malloc(strlen(path)+20);
146         strcpy(path2, path);
147         pe = path2 + strlen(path2);
148         
149         for (part = 0; part < 16; part++) {
150                 if (part) {
151                         sprintf(be, "p%d", part);
152
153                         if (isdigit(pe[-1]))
154                                 sprintf(pe, "p%d", part);
155                         else
156                                 sprintf(pe, "%d", part);
157                 }
158                 n = readlink(path2, link, sizeof(link));
159                 if (n && (int)strlen(base) == n &&
160                     strncmp(link, base, n) == 0)
161                         unlink(path2);
162         }
163         free(path2);
164 }
165         
166
167 int Manage_runstop(char *devname, int fd, int runstop, int quiet)
168 {
169         /* Run or stop the array. array must already be configured
170          * required >= 0.90.0
171          * Only print failure messages if quiet == 0;
172          * quiet > 0 means really be quiet
173          * quiet < 0 means we will try again if it fails.
174          */
175         mdu_param_t param; /* unused */
176
177         if (runstop == -1 && md_get_version(fd) < 9000) {
178                 if (ioctl(fd, STOP_MD, 0)) {
179                         if (quiet == 0) fprintf(stderr,
180                                                 Name ": stopping device %s "
181                                                 "failed: %s\n",
182                                                 devname, strerror(errno));
183                         return 1;
184                 }
185         }
186
187         if (md_get_version(fd) < 9000) {
188                 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
189                 return 1;
190         }
191         /*
192         if (ioctl(fd, GET_ARRAY_INFO, &array)) {
193                 fprintf(stderr, Name ": %s does not appear to be active.\n",
194                         devname);
195                 return 1;
196         }
197         */
198         if (runstop>0) {
199                 if (ioctl(fd, RUN_ARRAY, &param)) {
200                         fprintf(stderr, Name ": failed to run array %s: %s\n",
201                                 devname, strerror(errno));
202                         return 1;
203                 }
204                 if (quiet <= 0)
205                         fprintf(stderr, Name ": started %s\n", devname);
206         } else if (runstop < 0){
207                 struct map_ent *map = NULL;
208                 struct stat stb;
209                 struct mdinfo *mdi;
210                 int devnum;
211                 int err;
212                 int count;
213                 /* If this is an mdmon managed array, just write 'inactive'
214                  * to the array state and let mdmon clear up.
215                  */
216                 devnum = fd2devnum(fd);
217                 /* Get EXCL access first.  If this fails, then attempting
218                  * to stop is probably a bad idea.
219                  */
220                 close(fd);
221                 fd = open(devname, O_RDONLY|O_EXCL);
222                 if (fd < 0 || fd2devnum(fd) != devnum) {
223                         if (fd >= 0)
224                                 close(fd);
225                         fprintf(stderr,
226                                 Name ": Cannot get exclusive access to %s:"
227                                 "Perhaps a running "
228                                 "process, mounted filesystem "
229                                 "or active volume group?\n",
230                                 devname);
231                         return 1;
232                 }
233                 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
234                 if (mdi &&
235                     mdi->array.level > 0 &&
236                     is_subarray(mdi->text_version)) {
237                         int err;
238                         /* This is mdmon managed. */
239                         close(fd);
240
241                         count = 25;
242                         while (count &&
243                                (err = sysfs_set_str(mdi, NULL,
244                                                     "array_state",
245                                                     "inactive")) < 0
246                                && errno == EBUSY) {
247                                 usleep(200000);
248                                 count--;
249                         }
250                         if (err && !quiet) {
251                                 fprintf(stderr, Name
252                                         ": failed to stop array %s: %s\n",
253                                         devname, strerror(errno));
254                                 return 1;
255                         }
256
257                         /* Give monitor a chance to act */
258                         ping_monitor(mdi->text_version);
259
260                         fd = open_dev_excl(devnum);
261                         if (fd < 0) {
262                                 fprintf(stderr, Name
263                                         ": failed to completely stop %s"
264                                         ": Device is busy\n",
265                                         devname);
266                                 return 1;
267                         }
268                 } else if (mdi &&
269                            mdi->array.major_version == -1 &&
270                            mdi->array.minor_version == -2 &&
271                            !is_subarray(mdi->text_version)) {
272                         struct mdstat_ent *mds, *m;
273                         /* container, possibly mdmon-managed.
274                          * Make sure mdmon isn't opening it, which
275                          * would interfere with the 'stop'
276                          */
277                         ping_monitor(mdi->sys_name);
278
279                         /* now check that there are no existing arrays
280                          * which are members of this array
281                          */
282                         mds = mdstat_read(0, 0);
283                         for (m=mds; m; m=m->next)
284                                 if (m->metadata_version &&
285                                     strncmp(m->metadata_version, "external:", 9)==0 &&
286                                     is_subarray(m->metadata_version+9) &&
287                                     devname2devnum(m->metadata_version+10) == devnum) {
288                                         if (!quiet)
289                                                 fprintf(stderr, Name
290                                                         ": Cannot stop container %s: "
291                                                         "member %s still active\n",
292                                                         devname, m->dev);
293                                         free_mdstat(mds);
294                                         if (mdi)
295                                                 sysfs_free(mdi);
296                                         return 1;
297                                 }
298                 }
299
300                 /* As we have an O_EXCL open, any use of the device
301                  * which blocks STOP_ARRAY is probably a transient use,
302                  * so it is reasonable to retry for a while - 5 seconds.
303                  */
304                 count = 25; err = 0;
305                 while (count && fd >= 0
306                        && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0
307                        && errno == EBUSY) {
308                         usleep(200000);
309                         count --;
310                 }
311                 if (fd >= 0 && err) {
312                         if (quiet == 0) {
313                                 fprintf(stderr, Name
314                                         ": failed to stop array %s: %s\n",
315                                         devname, strerror(errno));
316                                 if (errno == EBUSY)
317                                         fprintf(stderr, "Perhaps a running "
318                                                 "process, mounted filesystem "
319                                                 "or active volume group?\n");
320                         }
321                         if (mdi)
322                                 sysfs_free(mdi);
323                         return 1;
324                 }
325                 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
326                  * was stopped, so We'll do it here just to be sure.  Drop any
327                  * partitions as well...
328                  */
329                 if (fd >= 0)
330                         ioctl(fd, BLKRRPART, 0);
331                 if (mdi)
332                         sysfs_uevent(mdi, "change");
333
334                 
335                 if (devnum != NoMdDev &&
336                     (stat("/dev/.udev", &stb) != 0 ||
337                      check_env("MDADM_NO_UDEV"))) {
338                         struct map_ent *mp = map_by_devnum(&map, devnum);
339                         remove_devices(devnum, mp ? mp->path : NULL);
340                 }
341
342
343                 if (quiet <= 0)
344                         fprintf(stderr, Name ": stopped %s\n", devname);
345                 map_lock(&map);
346                 map_remove(&map, devnum);
347                 map_unlock(&map);
348         }
349         return 0;
350 }
351
352 int Manage_resize(char *devname, int fd, long long size, int raid_disks)
353 {
354         mdu_array_info_t info;
355         if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
356                 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
357                         devname, strerror(errno));
358                 return 1;
359         }
360         if (size >= 0)
361                 info.size = size;
362         if (raid_disks > 0)
363                 info.raid_disks = raid_disks;
364         if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
365                 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
366                         devname, strerror(errno));
367                 return 1;
368         }
369         return 0;
370 }
371
372 int Manage_subdevs(char *devname, int fd,
373                    struct mddev_dev *devlist, int verbose, int test,
374                    char *update)
375 {
376         /* do something to each dev.
377          * devmode can be
378          *  'a' - add the device
379          *         try HOT_ADD_DISK
380          *         If that fails EINVAL, try ADD_NEW_DISK
381          *  'r' - remove the device HOT_REMOVE_DISK
382          *        device can be 'faulty' or 'detached' in which case all
383          *        matching devices are removed.
384          *  'f' - set the device faulty SET_DISK_FAULTY
385          *        device can be 'detached' in which case any device that
386          *        is inaccessible will be marked faulty.
387          * For 'f' and 'r', the device can also be a kernel-internal
388          * name such as 'sdb'.
389          */
390         struct mddev_dev *add_devlist = NULL;
391         mdu_array_info_t array;
392         mdu_disk_info_t disc;
393         unsigned long long array_size;
394         struct mddev_dev *dv, *next = NULL;
395         struct stat stb;
396         int j, jnext = 0;
397         int tfd = -1;
398         struct supertype *st, *tst;
399         char *subarray = NULL;
400         int duuid[4];
401         int ouuid[4];
402         int lfd = -1;
403         int sysfd = -1;
404         int count = 0; /* number of actions taken */
405
406         if (ioctl(fd, GET_ARRAY_INFO, &array)) {
407                 fprintf(stderr, Name ": cannot get array info for %s\n",
408                         devname);
409                 return 1;
410         }
411
412         /* array.size is only 32 bit and may be truncated.
413          * So read from sysfs if possible, and record number of sectors
414          */
415
416         array_size = get_component_size(fd);
417         if (array_size <= 0)
418                 array_size = array.size * 2;
419
420         tst = super_by_fd(fd, &subarray);
421         if (!tst) {
422                 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
423                         array.major_version, array.minor_version);
424                 return 1;
425         }
426
427         stb.st_rdev = 0;
428         for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
429                 unsigned long long ldsize;
430                 char dvname[20];
431                 char *dnprintable = dv->devname;
432                 char *add_dev = dv->devname;
433                 int err;
434                 int re_add_failed = 0;
435
436                 next = dv->next;
437                 jnext = 0;
438
439                 if (strcmp(dv->devname, "failed")==0 ||
440                     strcmp(dv->devname, "faulty")==0) {
441                         int remaining_disks = array.nr_disks;
442                         if (dv->disposition != 'r') {
443                                 fprintf(stderr, Name ": %s only meaningful "
444                                         "with -r, not -%c\n",
445                                         dv->devname, dv->disposition);
446                                 return 1;
447                         }
448                         for (; j < 1024 && remaining_disks > 0; j++) {
449                                 unsigned dev;
450                                 disc.number = j;
451                                 if (ioctl(fd, GET_DISK_INFO, &disc))
452                                         continue;
453                                 if (disc.major == 0 && disc.minor == 0)
454                                         continue;
455                                 remaining_disks --;
456                                 if ((disc.state & 1) == 0) /* faulty */
457                                         continue;
458                                 dev = makedev(disc.major, disc.minor);
459                                 if (stb.st_rdev == dev)
460                                         /* already did that one */
461                                         continue;
462                                 stb.st_rdev = dev;
463                                 next = dv;
464                                 /* same slot again next time - things might
465                                  * have reshuffled */
466                                 jnext = j;
467                                 sprintf(dvname,"%d:%d", disc.major, disc.minor);
468                                 dnprintable = dvname;
469                                 break;
470                         }
471                         if (next != dv)
472                                 continue;
473                 } else if (strcmp(dv->devname, "detached") == 0) {
474                         int remaining_disks = array.nr_disks;
475                         if (dv->disposition != 'r' && dv->disposition != 'f') {
476                                 fprintf(stderr, Name ": %s only meaningful "
477                                         "with -r of -f, not -%c\n",
478                                         dv->devname, dv->disposition);
479                                 return 1;
480                         }
481                         for (; j < 1024 && remaining_disks > 0; j++) {
482                                 int sfd;
483                                 unsigned dev;
484                                 disc.number = j;
485                                 if (ioctl(fd, GET_DISK_INFO, &disc))
486                                         continue;
487                                 if (disc.major == 0 && disc.minor == 0)
488                                         continue;
489                                 remaining_disks --;
490                                 sprintf(dvname,"%d:%d", disc.major, disc.minor);
491                                 sfd = dev_open(dvname, O_RDONLY);
492                                 if (sfd >= 0) {
493                                         close(sfd);
494                                         continue;
495                                 }
496                                 if (dv->disposition == 'f' &&
497                                     (disc.state & 1) == 1) /* already faulty */
498                                         continue;
499                                 if (errno != ENXIO)
500                                         continue;
501                                 dev = makedev(disc.major, disc.minor);
502                                 if (stb.st_rdev == dev)
503                                         /* already did that one */
504                                         continue;
505                                 stb.st_rdev = dev;
506                                 next = dv;
507                                 /* same slot again next time - things might
508                                  * have reshuffled */
509                                 jnext = j;
510                                 dnprintable = dvname;
511                                 break;
512                         }
513                         if (next != dv)
514                                 continue;
515                 } else if (strcmp(dv->devname, "missing") == 0) {
516                         if (dv->disposition != 'a' || dv->re_add == 0) {
517                                 fprintf(stderr, Name ": 'missing' only meaningful "
518                                         "with --re-add\n");
519                                 return 1;
520                         }
521                         if (add_devlist == NULL)
522                                 add_devlist = conf_get_devs();
523                         if (add_devlist == NULL) {
524                                 fprintf(stderr, Name ": no devices to scan for missing members.");
525                                 continue;
526                         }
527                         add_dev = add_devlist->devname;
528                         add_devlist = add_devlist->next;
529                         if (add_devlist != NULL)
530                                 next = dv;
531                         if (stat(add_dev, &stb) < 0)
532                                 continue;
533                 } else if (strchr(dv->devname, '/') == NULL &&
534                            strchr(dv->devname, ':') == NULL &&
535                            strlen(dv->devname) < 50) {
536                         /* Assume this is a kernel-internal name like 'sda1' */
537                         int found = 0;
538                         char dname[55];
539                         if (dv->disposition != 'r' && dv->disposition != 'f') {
540                                 fprintf(stderr, Name ": %s only meaningful "
541                                         "with -r or -f, not -%c\n",
542                                         dv->devname, dv->disposition);
543                                 return 1;
544                         }
545
546                         sprintf(dname, "dev-%s", dv->devname);
547                         sysfd = sysfs_open(fd2devnum(fd), dname, "block/dev");
548                         if (sysfd >= 0) {
549                                 char dn[20];
550                                 int mj,mn;
551                                 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
552                                     sscanf(dn, "%d:%d", &mj,&mn) == 2) {
553                                         stb.st_rdev = makedev(mj,mn);
554                                         found = 1;
555                                 }
556                                 close(sysfd);
557                                 sysfd = -1;
558                         }
559                         if (!found) {
560                                 sysfd = sysfs_open(fd2devnum(fd), dname, "state");
561                                 if (sysfd < 0) {
562                                         fprintf(stderr, Name ": %s does not appear "
563                                                 "to be a component of %s\n",
564                                                 dv->devname, devname);
565                                         return 1;
566                                 }
567                         }
568                 } else {
569                         j = 0;
570
571                         tfd = dev_open(dv->devname, O_RDONLY);
572                         if (tfd < 0 && dv->disposition == 'r' &&
573                             lstat(dv->devname, &stb) == 0)
574                                 /* Be happy, the lstat worked, that is
575                                  * enough for --remove
576                                  */
577                                 ;
578                         else {
579                                 if (tfd < 0 || fstat(tfd, &stb) != 0) {
580                                         fprintf(stderr, Name ": cannot find %s: %s\n",
581                                                 dv->devname, strerror(errno));
582                                         if (tfd >= 0)
583                                                 close(tfd);
584                                         return 1;
585                                 }
586                                 close(tfd);
587                                 tfd = -1;
588                         }
589                         if ((stb.st_mode & S_IFMT) != S_IFBLK) {
590                                 fprintf(stderr, Name ": %s is not a "
591                                         "block device.\n",
592                                         dv->devname);
593                                 return 1;
594                         }
595                 }
596                 switch(dv->disposition){
597                 default:
598                         fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
599                                 dv->devname, dv->disposition);
600                         return 1;
601                 case 'a':
602                         /* add the device */
603                         if (subarray) {
604                                 fprintf(stderr, Name ": Cannot add disks to a"
605                                         " \'member\' array, perform this"
606                                         " operation on the parent container\n");
607                                 return 1;
608                         }
609                         /* Make sure it isn't in use (in 2.6 or later) */
610                         tfd = dev_open(add_dev, O_RDONLY|O_EXCL|O_DIRECT);
611                         if (tfd < 0 && add_dev != dv->devname)
612                                 continue;
613                         if (tfd < 0) {
614                                 fprintf(stderr, Name ": Cannot open %s: %s\n",
615                                         dv->devname, strerror(errno));
616                                 return 1;
617                         }
618
619                         st = dup_super(tst);
620
621                         if (array.not_persistent==0)
622                                 st->ss->load_super(st, tfd, NULL);
623
624                         if (add_dev == dv->devname) {
625                                 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
626                                         close(tfd);
627                                         return 1;
628                                 }
629                         } else if (!get_dev_size(tfd, NULL, &ldsize)) {
630                                 close(tfd);
631                                 tfd = -1;
632                                 continue;
633                         }
634
635                         if (!tst->ss->external &&
636                             array.major_version == 0 &&
637                             md_get_version(fd)%100 < 2) {
638                                 close(tfd);
639                                 tfd = -1;
640                                 if (ioctl(fd, HOT_ADD_DISK,
641                                           (unsigned long)stb.st_rdev)==0) {
642                                         if (verbose >= 0)
643                                                 fprintf(stderr, Name ": hot added %s\n",
644                                                         add_dev);
645                                         continue;
646                                 }
647
648                                 fprintf(stderr, Name ": hot add failed for %s: %s\n",
649                                         add_dev, strerror(errno));
650                                 return 1;
651                         }
652
653                         if (array.not_persistent == 0 || tst->ss->external) {
654
655                                 /* need to find a sample superblock to copy, and
656                                  * a spare slot to use.
657                                  * For 'external' array (well, container based),
658                                  * We can just load the metadata for the array.
659                                  */
660                                 if (tst->sb)
661                                         /* already loaded */;
662                                 else if (tst->ss->external) {
663                                         tst->ss->load_container(tst, fd, NULL);
664                                 } else for (j = 0; j < tst->max_devs; j++) {
665                                         char *dev;
666                                         int dfd;
667                                         disc.number = j;
668                                         if (ioctl(fd, GET_DISK_INFO, &disc))
669                                                 continue;
670                                         if (disc.major==0 && disc.minor==0)
671                                                 continue;
672                                         if ((disc.state & 4)==0) continue; /* sync */
673                                         /* Looks like a good device to try */
674                                         dev = map_dev(disc.major, disc.minor, 1);
675                                         if (!dev) continue;
676                                         dfd = dev_open(dev, O_RDONLY);
677                                         if (dfd < 0) continue;
678                                         if (tst->ss->load_super(tst, dfd,
679                                                                 NULL)) {
680                                                 close(dfd);
681                                                 continue;
682                                         }
683                                         close(dfd);
684                                         break;
685                                 }
686                                 /* FIXME this is a bad test to be using */
687                                 if (!tst->sb) {
688                                         close(tfd);
689                                         fprintf(stderr, Name ": cannot load array metadata from %s\n", devname);
690                                         return 1;
691                                 }
692
693                                 /* Make sure device is large enough */
694                                 if (tst->ss->avail_size(tst, ldsize/512) <
695                                     array_size) {
696                                         close(tfd);
697                                         tfd = -1;
698                                         if (add_dev != dv->devname)
699                                                 continue;
700                                         fprintf(stderr, Name ": %s not large enough to join array\n",
701                                                 dv->devname);
702                                         return 1;
703                                 }
704
705                                 /* Possibly this device was recently part of the array
706                                  * and was temporarily removed, and is now being re-added.
707                                  * If so, we can simply re-add it.
708                                  */
709                                 tst->ss->uuid_from_super(tst, duuid);
710
711                                 if (st->sb) {
712                                         struct mdinfo mdi;
713                                         st->ss->getinfo_super(st, &mdi, NULL);
714                                         st->ss->uuid_from_super(st, ouuid);
715                                         if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
716                                             !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
717                                             memcmp(duuid, ouuid, sizeof(ouuid))==0) {
718                                                 /* look like it is worth a try.  Need to
719                                                  * make sure kernel will accept it though.
720                                                  */
721                                                 /* re-add doesn't work for version-1 superblocks
722                                                  * before 2.6.18 :-(
723                                                  */
724                                                 if (array.major_version == 1 &&
725                                                     get_linux_version() <= 2006018)
726                                                         goto skip_re_add;
727                                                 disc.number = mdi.disk.number;
728                                                 if (ioctl(fd, GET_DISK_INFO, &disc) != 0
729                                                     || disc.major != 0 || disc.minor != 0
730                                                     || !enough_fd(fd))
731                                                         goto skip_re_add;
732                                                 disc.major = major(stb.st_rdev);
733                                                 disc.minor = minor(stb.st_rdev);
734                                                 disc.number = mdi.disk.number;
735                                                 disc.raid_disk = mdi.disk.raid_disk;
736                                                 disc.state = mdi.disk.state;
737                                                 if (dv->writemostly == 1)
738                                                         disc.state |= 1 << MD_DISK_WRITEMOSTLY;
739                                                 if (dv->writemostly == 2)
740                                                         disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
741                                                 remove_partitions(tfd);
742                                                 close(tfd);
743                                                 tfd = -1;
744                                                 if (update) {
745                                                         int rv = -1;
746                                                         tfd = dev_open(dv->devname, O_RDWR);
747
748                                                         if (tfd >= 0)
749                                                                 rv = st->ss->update_super(
750                                                                         st, NULL, update,
751                                                                         devname, verbose, 0, NULL);
752                                                         if (rv == 0)
753                                                                 rv = st->ss->store_super(st, tfd);
754                                                         close(tfd);
755                                                         tfd = -1;
756                                                         if (rv != 0) {
757                                                                 fprintf(stderr, Name ": failed to update"
758                                                                         " superblock during re-add\n");
759                                                                 return 1;
760                                                         }
761                                                 }
762                                                 /* don't even try if disk is marked as faulty */
763                                                 errno = 0;
764                                                 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
765                                                         if (verbose >= 0)
766                                                                 fprintf(stderr, Name ": re-added %s\n", add_dev);
767                                                         count++;
768                                                         continue;
769                                                 }
770                                                 if (errno == ENOMEM || errno == EROFS) {
771                                                         fprintf(stderr, Name ": add new device failed for %s: %s\n",
772                                                                 add_dev, strerror(errno));
773                                                         if (add_dev != dv->devname)
774                                                                 continue;
775                                                         return 1;
776                                                 }
777                                         skip_re_add:
778                                                 re_add_failed = 1;
779                                         }
780                                         st->ss->free_super(st);
781                                 }
782                                 if (add_dev != dv->devname) {
783                                         if (verbose > 0)
784                                                 fprintf(stderr, Name
785                                                         ": --re-add for %s to %s is not possible\n",
786                                                         add_dev, devname);
787                                         if (tfd >= 0) {
788                                                 close(tfd);
789                                                 tfd = -1;
790                                         }
791                                         continue;
792                                 }
793                                 if (dv->re_add) {
794                                         if (tfd >= 0)
795                                                 close(tfd);
796                                         fprintf(stderr, Name
797                                                 ": --re-add for %s to %s is not possible\n",
798                                                 dv->devname, devname);
799                                         return 1;
800                                 }
801                                 if (re_add_failed) {
802                                         fprintf(stderr, Name ": %s reports being an active member for %s, but a --re-add fails.\n",
803                                                 dv->devname, devname);
804                                         fprintf(stderr, Name ": not performing --add as that would convert %s in to a spare.\n",
805                                                 dv->devname);
806                                         fprintf(stderr, Name ": To make this a spare, use \"mdadm --zero-superblock %s\" first.\n",     
807                                                 dv->devname);
808                                         if (tfd >= 0)
809                                                 close(tfd);
810                                         return 1;
811                                 }
812                         } else {
813                                 /* non-persistent. Must ensure that new drive
814                                  * is at least array.size big.
815                                  */
816                                 if (ldsize/512 < array_size) {
817                                         fprintf(stderr, Name ": %s not large enough to join array\n",
818                                                 dv->devname);
819                                         if (tfd >= 0)
820                                                 close(tfd);
821                                         return 1;
822                                 }
823                         }
824                         /* committed to really trying this device now*/
825                         if (tfd >= 0) {
826                                 remove_partitions(tfd);
827                                 close(tfd);
828                                 tfd = -1;
829                         }
830                         /* in 2.6.17 and earlier, version-1 superblocks won't
831                          * use the number we write, but will choose a free number.
832                          * we must choose the same free number, which requires
833                          * starting at 'raid_disks' and counting up
834                          */
835                         for (j = array.raid_disks; j< tst->max_devs; j++) {
836                                 disc.number = j;
837                                 if (ioctl(fd, GET_DISK_INFO, &disc))
838                                         break;
839                                 if (disc.major==0 && disc.minor==0)
840                                         break;
841                                 if (disc.state & 8) /* removed */
842                                         break;
843                         }
844                         disc.major = major(stb.st_rdev);
845                         disc.minor = minor(stb.st_rdev);
846                         disc.number =j;
847                         disc.state = 0;
848                         if (array.not_persistent==0) {
849                                 int dfd;
850                                 if (dv->writemostly == 1)
851                                         disc.state |= 1 << MD_DISK_WRITEMOSTLY;
852                                 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
853                                 if (tst->ss->add_to_super(tst, &disc, dfd,
854                                                           dv->devname)) {
855                                         close(dfd);
856                                         return 1;
857                                 }
858                                 if (tst->ss->write_init_super(tst)) {
859                                         close(dfd);
860                                         return 1;
861                                 }
862                         } else if (dv->re_add) {
863                                 /*  this had better be raid1.
864                                  * As we are "--re-add"ing we must find a spare slot
865                                  * to fill.
866                                  */
867                                 char *used = malloc(array.raid_disks);
868                                 memset(used, 0, array.raid_disks);
869                                 for (j=0; j< tst->max_devs; j++) {
870                                         mdu_disk_info_t disc2;
871                                         disc2.number = j;
872                                         if (ioctl(fd, GET_DISK_INFO, &disc2))
873                                                 continue;
874                                         if (disc2.major==0 && disc2.minor==0)
875                                                 continue;
876                                         if (disc2.state & 8) /* removed */
877                                                 continue;
878                                         if (disc2.raid_disk < 0)
879                                                 continue;
880                                         if (disc2.raid_disk > array.raid_disks)
881                                                 continue;
882                                         used[disc2.raid_disk] = 1;
883                                 }
884                                 for (j=0 ; j<array.raid_disks; j++)
885                                         if (!used[j]) {
886                                                 disc.raid_disk = j;
887                                                 disc.state |= (1<<MD_DISK_SYNC);
888                                                 break;
889                                         }
890                                 free(used);
891                         }
892                         if (dv->writemostly == 1)
893                                 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
894                         if (tst->ss->external) {
895                                 /* add a disk
896                                  * to an external metadata container */
897                                 struct mdinfo new_mdi;
898                                 struct mdinfo *sra;
899                                 int container_fd;
900                                 int devnum = fd2devnum(fd);
901                                 int dfd;
902
903                                 container_fd = open_dev_excl(devnum);
904                                 if (container_fd < 0) {
905                                         fprintf(stderr, Name ": add failed for %s:"
906                                                 " could not get exclusive access to container\n",
907                                                 dv->devname);
908                                         tst->ss->free_super(tst);
909                                         return 1;
910                                 }
911
912                                 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
913                                 if (mdmon_running(tst->container_dev))
914                                         tst->update_tail = &tst->updates;
915                                 if (tst->ss->add_to_super(tst, &disc, dfd,
916                                                           dv->devname)) {
917                                         close(dfd);
918                                         close(container_fd);
919                                         return 1;
920                                 }
921                                 if (tst->update_tail)
922                                         flush_metadata_updates(tst);
923                                 else
924                                         tst->ss->sync_metadata(tst);
925
926                                 sra = sysfs_read(container_fd, -1, 0);
927                                 if (!sra) {
928                                         fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
929                                                 dv->devname);
930                                         close(container_fd);
931                                         tst->ss->free_super(tst);
932                                         return 1;
933                                 }
934                                 sra->array.level = LEVEL_CONTAINER;
935                                 /* Need to set data_offset and component_size */
936                                 tst->ss->getinfo_super(tst, &new_mdi, NULL);
937                                 new_mdi.disk.major = disc.major;
938                                 new_mdi.disk.minor = disc.minor;
939                                 new_mdi.recovery_start = 0;
940                                 /* Make sure fds are closed as they are O_EXCL which
941                                  * would block add_disk */
942                                 tst->ss->free_super(tst);
943                                 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
944                                         fprintf(stderr, Name ": add new device to external metadata"
945                                                 " failed for %s\n", dv->devname);
946                                         close(container_fd);
947                                         sysfs_free(sra);
948                                         return 1;
949                                 }
950                                 ping_monitor_by_id(devnum);
951                                 sysfs_free(sra);
952                                 close(container_fd);
953                         } else {
954                                 tst->ss->free_super(tst);
955                                 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
956                                         fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
957                                                 dv->devname, j, strerror(errno));
958                                         return 1;
959                                 }
960                         }
961                         if (verbose >= 0)
962                                 fprintf(stderr, Name ": added %s\n", dv->devname);
963                         break;
964
965                 case 'r':
966                         /* hot remove */
967                         if (subarray) {
968                                 fprintf(stderr, Name ": Cannot remove disks from a"
969                                         " \'member\' array, perform this"
970                                         " operation on the parent container\n");
971                                 if (sysfd >= 0)
972                                         close(sysfd);
973                                 return 1;
974                         }
975                         if (tst->ss->external) {
976                                 /* To remove a device from a container, we must
977                                  * check that it isn't in use in an array.
978                                  * This involves looking in the 'holders'
979                                  * directory - there must be just one entry,
980                                  * the container.
981                                  * To ensure that it doesn't get used as a
982                                  * hold spare while we are checking, we
983                                  * get an O_EXCL open on the container
984                                  */
985                                 int dnum = fd2devnum(fd);
986                                 lfd = open_dev_excl(dnum);
987                                 if (lfd < 0) {
988                                         fprintf(stderr, Name
989                                                 ": Cannot get exclusive access "
990                                                 " to container - odd\n");
991                                         if (sysfd >= 0)
992                                                 close(sysfd);
993                                         return 1;
994                                 }
995                                 /* in the detached case it is not possible to
996                                  * check if we are the unique holder, so just
997                                  * rely on the 'detached' checks
998                                  */
999                                 if (strcmp(dv->devname, "detached") == 0 ||
1000                                     sysfd >= 0 ||
1001                                     sysfs_unique_holder(dnum, stb.st_rdev))
1002                                         /* pass */;
1003                                 else {
1004                                         fprintf(stderr, Name
1005                                                 ": %s is %s, cannot remove.\n",
1006                                                 dnprintable,
1007                                                 errno == EEXIST ? "still in use":
1008                                                 "not a member");
1009                                         close(lfd);
1010                                         return 1;
1011                                 }
1012                         }
1013                         /* FIXME check that it is a current member */
1014                         if (sysfd >= 0) {
1015                                 /* device has been removed and we don't know
1016                                  * the major:minor number
1017                                  */
1018                                 int n = write(sysfd, "remove", 6);
1019                                 if (n != 6)
1020                                         err = -1;
1021                                 else
1022                                         err = 0;
1023                                 close(sysfd);
1024                                 sysfd = -1;
1025                         } else {
1026                                 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
1027                                 if (err && errno == ENODEV) {
1028                                         /* Old kernels rejected this if no personality
1029                                          * registered */
1030                                         struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
1031                                         struct mdinfo *dv = NULL;
1032                                         if (sra)
1033                                                 dv = sra->devs;
1034                                         for ( ; dv ; dv=dv->next)
1035                                                 if (dv->disk.major == (int)major(stb.st_rdev) &&
1036                                                     dv->disk.minor == (int)minor(stb.st_rdev))
1037                                                         break;
1038                                         if (dv)
1039                                                 err = sysfs_set_str(sra, dv,
1040                                                                     "state", "remove");
1041                                         else
1042                                                 err = -1;
1043                                         if (sra)
1044                                                 sysfs_free(sra);
1045                                 }
1046                         }
1047                         if (err) {
1048                                 fprintf(stderr, Name ": hot remove failed "
1049                                         "for %s: %s\n", dnprintable,
1050                                         strerror(errno));
1051                                 if (lfd >= 0)
1052                                         close(lfd);
1053                                 return 1;
1054                         }
1055                         if (tst->ss->external) {
1056                                 /*
1057                                  * Before dropping our exclusive open we make an
1058                                  * attempt at preventing mdmon from seeing an
1059                                  * 'add' event before reconciling this 'remove'
1060                                  * event.
1061                                  */
1062                                 char *name = devnum2devname(fd2devnum(fd));
1063
1064                                 if (!name) {
1065                                         fprintf(stderr, Name ": unable to get container name\n");
1066                                         return 1;
1067                                 }
1068
1069                                 ping_manager(name);
1070                                 free(name);
1071                         }
1072                         if (lfd >= 0)
1073                                 close(lfd);
1074                         count++;
1075                         if (verbose >= 0)
1076                                 fprintf(stderr, Name ": hot removed %s from %s\n",
1077                                         dnprintable, devname);
1078                         break;
1079
1080                 case 'f': /* set faulty */
1081                         /* FIXME check current member */
1082                         if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1083                             (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1084                                                 (unsigned long) stb.st_rdev))) {
1085                                 fprintf(stderr, Name ": set device faulty failed for %s:  %s\n",
1086                                         dnprintable, strerror(errno));
1087                                 if (sysfd >= 0)
1088                                         close(sysfd);
1089                                 return 1;
1090                         }
1091                         if (sysfd >= 0)
1092                                 close(sysfd);
1093                         sysfd = -1;
1094                         count++;
1095                         if (verbose >= 0)
1096                                 fprintf(stderr, Name ": set %s faulty in %s\n",
1097                                         dnprintable, devname);
1098                         break;
1099                 }
1100         }
1101         if (test && count == 0)
1102                 return 2;
1103         return 0;
1104 }
1105
1106 int autodetect(void)
1107 {
1108         /* Open any md device, and issue the RAID_AUTORUN ioctl */
1109         int rv = 1;
1110         int fd = dev_open("9:0", O_RDONLY);
1111         if (fd >= 0) {
1112                 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1113                         rv = 0;
1114                 close(fd);
1115         }
1116         return rv;
1117 }
1118
1119 int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int quiet)
1120 {
1121         struct supertype supertype, *st = &supertype;
1122         int fd, rv = 2;
1123
1124         memset(st, 0, sizeof(*st));
1125
1126         fd = open_subarray(dev, subarray, st, quiet);
1127         if (fd < 0)
1128                 return 2;
1129
1130         if (!st->ss->update_subarray) {
1131                 if (!quiet)
1132                         fprintf(stderr,
1133                                 Name ": Operation not supported for %s metadata\n",
1134                                 st->ss->name);
1135                 goto free_super;
1136         }
1137
1138         if (mdmon_running(st->devnum))
1139                 st->update_tail = &st->updates;
1140
1141         rv = st->ss->update_subarray(st, subarray, update, ident);
1142
1143         if (rv) {
1144                 if (!quiet)
1145                         fprintf(stderr, Name ": Failed to update %s of subarray-%s in %s\n",
1146                                 update, subarray, dev);
1147         } else if (st->update_tail)
1148                 flush_metadata_updates(st);
1149         else
1150                 st->ss->sync_metadata(st);
1151
1152         if (rv == 0 && strcmp(update, "name") == 0 && !quiet)
1153                 fprintf(stderr,
1154                         Name ": Updated subarray-%s name from %s, UUIDs may have changed\n",
1155                         subarray, dev);
1156
1157  free_super:
1158         st->ss->free_super(st);
1159         close(fd);
1160
1161         return rv;
1162 }
1163
1164 /* Move spare from one array to another
1165  * If adding to destination array fails
1166  * add back to original array
1167  * Returns 1 on success, 0 on failure */
1168 int move_spare(char *from_devname, char *to_devname, dev_t devid)
1169 {
1170         struct mddev_dev devlist;
1171         char devname[20];
1172
1173         /* try to remove and add */
1174         int fd1 = open(to_devname, O_RDONLY);
1175         int fd2 = open(from_devname, O_RDONLY);
1176
1177         if (fd1 < 0 || fd2 < 0) {
1178                 if (fd1>=0) close(fd1);
1179                 if (fd2>=0) close(fd2);
1180                 return 0;
1181         }
1182
1183         devlist.next = NULL;
1184         devlist.used = 0;
1185         devlist.re_add = 0;
1186         devlist.writemostly = 0;
1187         devlist.devname = devname;
1188         sprintf(devname, "%d:%d", major(devid), minor(devid));
1189
1190         devlist.disposition = 'r';
1191         if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL) == 0) {
1192                 devlist.disposition = 'a';
1193                 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL) == 0) {
1194                         /* make sure manager is aware of changes */
1195                         ping_manager(to_devname);
1196                         ping_manager(from_devname);
1197                         close(fd1);
1198                         close(fd2);
1199                         return 1;
1200                 }
1201                 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL);
1202         }
1203         close(fd1);
1204         close(fd2);
1205         return 0;
1206 }
1207 #endif