]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mapfile.c
Merge branch 'subarray' into for-neil
[thirdparty/mdadm.git] / mapfile.c
index 601c4ccf378b53ae185e326b45aa6f884be167e3..0f12559777b26b0bbbe5c9dbf17ea7dac06e4d54 100644 (file)
--- a/mapfile.c
+++ b/mapfile.c
@@ -29,7 +29,7 @@
  */
 
 /* /var/run/mdadm.map is used to track arrays being created in --incremental
- * more.  It particularly allows lookup from UUID to array device, but
+ * mode.  It particularly allows lookup from UUID to array device, but
  * also allows the array device name to be easily found.
  *
  * The map file is line based with space separated fields.  The fields are:
  * However /var/run may not exist or be writable in early boot.  And if
  * no-one has created /var/run/mdadm, we still want to survive.
  * So possible locations are:
- *   /var/run/mdadm/map  /var/run/mdadm.map  /dev/.mdadm.map
- * the last, because udev requires a writable /dev very early.
+ *   /var/run/mdadm/map  /var/run/mdadm.map  /lib/initrw/madam/map
+ * The last can easily be change at compile to e.g. somewhere in /dev.
  * We read from the first one that exists and write to the first
  * one that we can.
  */
 #include       "mdadm.h"
+#include       <sys/file.h>
 #include       <ctype.h>
 
-#define mapnames(base) { #base, #base ".new", #base ".lock"}
+#define mapnames(base) { base, base ".new", base ".lock"}
 char *mapname[3][3] = {
-       mapnames(/var/run/mdadm/map),
-       mapnames(/var/run/mdadm.map),
-       mapnames(/dev/.mdadm.map)
+       mapnames(VAR_RUN "/map"),
+       mapnames("/var/run/mdadm.map"),
+       mapnames(ALT_RUN "/" ALT_MAPFILE)
 };
+char *mapdir[3] = { VAR_RUN, NULL, ALT_RUN };
 
 int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT | O_TRUNC };
 char *mapsmode[3] = { "r", "w", "w"};
@@ -63,8 +65,16 @@ char *mapsmode[3] = { "r", "w", "w"};
 FILE *open_map(int modenum, int *choice)
 {
        int i;
+
        for (i = 0 ; i < 3 ; i++) {
-               int fd = open(mapname[i][modenum], mapmode[modenum], 0600);
+               int fd;
+               if ((mapmode[modenum] & O_CREAT) &&
+                   mapdir[modenum])
+                       /* Attempt to create directory, don't worry about
+                        * failure.
+                        */
+                       mkdir(mapdir[modenum], 0755);
+               fd = open(mapname[i][modenum], mapmode[modenum], 0600);
                if (fd >= 0) {
                        *choice = i;
                        return fdopen(fd, mapsmode[modenum]);
@@ -115,7 +125,7 @@ int map_lock(struct map_ent **melp)
                lf = open_map(2, &lwhich);
                if (lf == NULL)
                        return -1;
-               if (lockf(fileno(lf), F_LOCK, 0) != 0) {
+               if (flock(fileno(lf), LOCK_EX) != 0) {
                        fclose(lf);
                        lf = NULL;
                        return -1;
@@ -129,8 +139,10 @@ int map_lock(struct map_ent **melp)
 
 void map_unlock(struct map_ent **melp)
 {
-       if (lf)
+       if (lf) {
+               flock(fileno(lf), LOCK_UN);
                fclose(lf);
+       }
        unlink(mapname[lwhich][2]);
        lf = NULL;
 }
@@ -239,6 +251,16 @@ void map_delete(struct map_ent **mapp, int devnum)
        }
 }
 
+void map_remove(struct map_ent **mapp, int devnum)
+{
+       if (devnum == NoMdDev)
+               return;
+
+       map_delete(mapp, devnum);
+       map_write(*mapp);
+       map_free(*mapp);
+}
+
 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
 {
        struct map_ent *mp;
@@ -297,6 +319,37 @@ struct map_ent *map_by_name(struct map_ent **map, char *name)
        return NULL;
 }
 
+/* sets the proper subarray and container_dev according to the metadata
+ * version super_by_fd does this automatically, this routine is meant as
+ * a supplement for guess_super()
+ */
+static void set_member_info(struct supertype *st, struct mdstat_ent *ent)
+{
+
+       st->subarray[0] = '\0';
+
+       if (ent->metadata_version == NULL ||
+           strncmp(ent->metadata_version, "external:", 9) != 0)
+               return;
+
+       if (is_subarray(&ent->metadata_version[9])) {
+               char version[strlen(ent->metadata_version)+1];
+               char *subarray;
+               char *name = &version[10];
+
+               strcpy(version, ent->metadata_version);
+               subarray = strrchr(version, '/');
+               name = &version[10];
+
+               if (!subarray)
+                       return;
+               *subarray++ = '\0';
+
+               st->container_dev = devname2devnum(name);
+               strncpy(st->subarray, subarray, sizeof(st->subarray));
+       }
+}
+
 void RebuildMap(void)
 {
        struct mdstat_ent *mdstat = mdstat_read(0, 0);
@@ -337,8 +390,10 @@ void RebuildMap(void)
                        st = guess_super(dfd);
                        if ( st == NULL)
                                ok = -1;
-                       else
+                       else {
+                               set_member_info(st, md);
                                ok = st->ss->load_super(st, dfd, NULL);
+                       }
                        close(dfd);
                        if (ok != 0)
                                continue;
@@ -426,12 +481,14 @@ void RebuildMap(void)
                }
                sysfs_free(sra);
        }
-       map_write(map);
+       /* Only trigger a change if we wrote a new map file */
+       if (map_write(map))
+               for (md = mdstat ; md ; md = md->next) {
+                       struct mdinfo *sra = sysfs_read(-1, md->devnum,
+                                                       GET_VERSION);
+                       sysfs_uevent(sra, "change");
+                       sysfs_free(sra);
+               }
        map_free(map);
-       for (md = mdstat ; md ; md = md->next) {
-               struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION);
-               sysfs_uevent(sra, "change");
-               sysfs_free(sra);
-       }
        free_mdstat(mdstat);
 }