- struct mdstat_ent *mdstat = mdstat_read(0, 0);
- struct mdstat_ent *md;
- struct map_ent *map = NULL;
- int mdp = get_mdp_major();
-
- for (md = mdstat ; md ; md = md->next) {
- struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
- struct mdinfo *sd;
-
- for (sd = sra->devs ; sd ; sd = sd->next) {
- char dn[30];
- int dfd;
- int ok;
- struct supertype *st;
- char *path;
- struct mdinfo info;
-
- sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
- dfd = dev_open(dn, O_RDONLY);
- if (dfd < 0)
+ /* This device doesn't have any md metadata
+ * If it is 'bare' and theh device policy allows 'spare' look for
+ * an array or container to attach it to.
+ * If st is set, then only arrays of that type are considered
+ * Return 0 on success, or some exit code on failure, probably 1.
+ */
+ int rv = -1;
+ char bufpad[4096 + 4096];
+ char *buf = (char*)(((long)bufpad + 4096) & ~4095);
+ struct stat stb;
+ struct map_ent *mp, *map = NULL;
+ struct mdinfo *chosen = NULL;
+ int dfd = *dfdp;
+
+ /* First check policy */
+ if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
+ return 1;
+
+ if (fstat(dfd, &stb) != 0)
+ return 1;
+ /* Now check if the device is bare - we don't add non-bare devices
+ * yet even if action=-spare
+ */
+
+ if (lseek(dfd, 0, SEEK_SET) != 0 ||
+ read(dfd, buf, 4096) != 4096) {
+ not_bare:
+ if (verbose > 1)
+ fprintf(stderr, Name ": %s is not bare, so not considering as a spare\n",
+ devname);
+ return 1;
+ }
+ if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
+ goto not_bare;
+ if (memcmp(buf, buf+1, 4095) != 0)
+ goto not_bare;
+
+ /* OK, first 4K appear blank, try the end. */
+ if (lseek(dfd, -4096, SEEK_END) < 0 ||
+ read(dfd, buf, 4096) != 4096)
+ goto not_bare;
+
+ if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
+ goto not_bare;
+ if (memcmp(buf, buf+1, 4095) != 0)
+ goto not_bare;
+
+ /* This device passes our test for 'is bare'.
+ * Now we need to find a suitable array to add this to.
+ * We only accept arrays that:
+ * - match 'st'
+ * - are in the same domains as the device
+ * - are of an size for which the device will be useful
+ * and we choose the one that is the most degraded
+ */
+
+ if (map_lock(&map)) {
+ fprintf(stderr, Name ": failed to get exclusive lock on "
+ "mapfile\n");
+ return 1;
+ }
+ for (mp = map ; mp ; mp = mp->next) {
+ struct supertype *st2;
+ struct domainlist *dl = NULL;
+ struct mdinfo *sra;
+ unsigned long long devsize;
+
+ if (is_subarray(mp->metadata))
+ continue;
+ if (st) {
+ st2 = st->ss->match_metadata_desc(mp->metadata);
+ if (!st2 ||
+ (st->minor_version >= 0 &&
+ st->minor_version != st2->minor_version)) {
+ if (verbose > 1)
+ fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n",
+ devname, mp->path);
+ free(st2);