]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mapfile.c
Fix test for imsm prodigal member scenario
[thirdparty/mdadm.git] / mapfile.c
index ed59db5fe62df9c4db2807906743f4bd7f740f19..cc222d295df2ae0c78239f19e399d57a09c06e22 100644 (file)
--- a/mapfile.c
+++ b/mapfile.c
@@ -1,5 +1,5 @@
 /*
- * mapfile - manage /var/run/mdadm.map. Part of:
+ * mapfile - manage /var/run/mdadm/map. Part of:
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
  * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
@@ -28,8 +28,8 @@
  *           Australia
  */
 
-/* /var/run/mdadm.map is used to track arrays being created in --incremental
- * more.  It particularly allows lookup from UUID to array device, but
+/* /var/run/mdadm/map is used to track arrays being created in --incremental
+ * 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:
  *  UUID       -  uuid of the array
  *  path       -  path where device created: /dev/md/home
  *
- * The preferred location for the map file is /var/run/mdadm.map.
+ * The preferred location for the map file is /var/run/mdadm/map.
  * 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  /dev/.mdadm/map(changable at compile time).
  * 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"}
-char *mapname[3][3] = {
-       mapnames(/var/run/mdadm/map),
-       mapnames(/var/run/mdadm.map),
-       mapnames(/dev/.mdadm.map)
+#define MAP_READ 0
+#define MAP_NEW 1
+#define MAP_LOCK 2
+#define MAP_DIRNAME 3
+#define mapnames(dir, base) { \
+                       dir "/" base,                           \
+                       dir "/" base ".new",                    \
+                       dir "/" base ".lock",                   \
+                       dir }
+
+#define MAP_DIRS 2
+char *mapname[MAP_DIRS][4] = {
+       mapnames("/var/run/mdadm", "map"),
+       mapnames(MAP_DIR, MAP_FILE),
 };
 
-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 <= MAP_DIRS ; i++) {
+               int fd;
+               if ((mapmode[modenum] & O_CREAT))
+                       /* Attempt to create directory, don't worry about
+                        * failure.
+                        */
+                       (void)mkdir(mapname[i][MAP_DIRNAME], 0755);
+               fd = open(mapname[i][modenum], mapmode[modenum], 0600);
                if (fd >= 0) {
                        *choice = i;
                        return fdopen(fd, mapsmode[modenum]);
@@ -79,7 +95,7 @@ int map_write(struct map_ent *mel)
        int err;
        int which;
 
-       f = open_map(1, &which);
+       f = open_map(MAP_NEW, &which);
 
        if (!f)
                return 0;
@@ -111,15 +127,25 @@ static FILE *lf = NULL;
 static int lwhich = 0;
 int map_lock(struct map_ent **melp)
 {
-       if (lf == NULL) {
-               lf = open_map(2, &lwhich);
+       while (lf == NULL) {
+               struct stat buf;
+               lf = open_map(MAP_LOCK, &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;
                }
+               if (fstat(fileno(lf), &buf) != 0 ||
+                   buf.st_nlink == 0) {
+                       /* The owner of the lock unlinked it,
+                        * so we have a lock on a stale file,
+                        * try again
+                        */
+                       fclose(lf);
+                       lf = NULL;
+               }
        }
        if (*melp)
                map_free(*melp);
@@ -129,9 +155,14 @@ int map_lock(struct map_ent **melp)
 
 void map_unlock(struct map_ent **melp)
 {
-       if (lf)
+       if (lf) {
+               /* must unlink before closing the file,
+                * as only the owner of the lock may
+                * unlink the file
+                */
+               unlink(mapname[lwhich][2]);
                fclose(lf);
-       unlink(mapname[lwhich][2]);
+       }
        lf = NULL;
 }
 
@@ -161,10 +192,10 @@ void map_read(struct map_ent **melp)
 
        *melp = NULL;
 
-       f = open_map(0, &which);
+       f = open_map(MAP_READ, &which);
        if (!f) {
                RebuildMap();
-               f = open_map(0, &which);
+               f = open_map(MAP_READ, &which);
        }
        if (!f)
                return;
@@ -239,6 +270,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;
@@ -346,7 +387,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)
@@ -459,12 +500,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);
 }