]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mapfile.c
Fix all the confusion over directories once and for all.
[thirdparty/mdadm.git] / mapfile.c
index 5444452df2914f3fc0b0fc128512d92e818e6627..79a6bd80cb70bb4e33c84617cce5b8469a0e17ff 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:
@@ -42,8 +42,8 @@
  * 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       <sys/file.h>
 #include       <ctype.h>
 
-#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)
+#define mapnames(base) { base, base ".new", base ".lock"}
+char *mapname[2][3] = {
+       mapnames(MAP_DIR "/" MAP_FILE),
+       mapnames("/var/run/mdadm.map")
 };
+char *mapdir[2] = { MAP_DIR, NULL };
 
-int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT | O_TRUNC };
+int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC };
 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);
+
+       for (i = 0 ; i < 2 ; i++) {
+               int fd;
+               if ((mapmode[modenum] & O_CREAT) && mapdir[i])
+                       /* Attempt to create directory, don't worry about
+                        * failure.
+                        */
+                       mkdir(mapdir[i], 0755);
+               fd = open(mapname[i][modenum], mapmode[modenum], 0600);
                if (fd >= 0) {
                        *choice = i;
                        return fdopen(fd, mapsmode[modenum]);
@@ -242,6 +249,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;
@@ -349,7 +366,7 @@ void RebuildMap(void)
        }
 
        for (md = mdstat ; md ; md = md->next) {
-               struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS|SKIP_GONE_DEVS);
+               struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
                struct mdinfo *sd;
 
                if (!sra)
@@ -462,12 +479,15 @@ 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);
+                       if (sra)
+                               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);
 }