]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mapfile.c
Update man page info about files used by mdmon
[thirdparty/mdadm.git] / mapfile.c
index d47fde1463b8adf9758e9391f58fe79967cc8633..8e40b6b7c14cbadf7f0e00c3a53c853ecea2a60e 100644 (file)
--- a/mapfile.c
+++ b/mapfile.c
 #include       <ctype.h>
 
 #define mapnames(base) { base, base ".new", base ".lock"}
-char *mapname[3][3] = {
-       mapnames(VAR_RUN "/map"),
-       mapnames("/var/run/mdadm.map"),
-       mapnames(ALT_RUN "/" ALT_MAPFILE)
+char *mapname[2][3] = {
+       mapnames(MAP_DIR "/" MAP_FILE),
+       mapnames("/var/run/mdadm.map")
 };
-char *mapdir[3] = { VAR_RUN, NULL, ALT_RUN };
+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++) {
+       for (i = 0 ; i < 2 ; i++) {
                int fd;
-               if ((mapmode[modenum] & O_CREAT) &&
-                   mapdir[modenum])
+               if ((mapmode[modenum] & O_CREAT) && mapdir[i])
                        /* Attempt to create directory, don't worry about
                         * failure.
                         */
-                       mkdir(mapdir[modenum], 0755);
+                       mkdir(mapdir[i], 0755);
                fd = open(mapname[i][modenum], mapmode[modenum], 0600);
                if (fd >= 0) {
                        *choice = i;
@@ -121,7 +119,8 @@ static FILE *lf = NULL;
 static int lwhich = 0;
 int map_lock(struct map_ent **melp)
 {
-       if (lf == NULL) {
+       while (lf == NULL) {
+               struct stat buf;
                lf = open_map(2, &lwhich);
                if (lf == NULL)
                        return -1;
@@ -130,6 +129,15 @@ int map_lock(struct map_ent **melp)
                        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);
@@ -140,10 +148,13 @@ int map_lock(struct map_ent **melp)
 void map_unlock(struct map_ent **melp)
 {
        if (lf) {
-               flock(fileno(lf), LOCK_UN);
+               /* 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;
 }
 
@@ -251,6 +262,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;
@@ -358,7 +379,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)
@@ -476,7 +497,8 @@ void RebuildMap(void)
                for (md = mdstat ; md ; md = md->next) {
                        struct mdinfo *sra = sysfs_read(-1, md->devnum,
                                                        GET_VERSION);
-                       sysfs_uevent(sra, "change");
+                       if (sra)
+                               sysfs_uevent(sra, "change");
                        sysfs_free(sra);
                }
        map_free(map);