|
|
903fa7 |
commit 3c8db52fe77267109a6f01c178370747a23e64fd
|
|
|
903fa7 |
Author: Andrew Price <anprice@redhat.com>
|
|
|
903fa7 |
Date: Sat Nov 16 02:10:52 2013 -0600
|
|
|
903fa7 |
|
|
|
903fa7 |
libgfs2: Add lgfs2_open_mnt* functions
|
|
|
903fa7 |
|
|
|
903fa7 |
lgfs2_open_mnt is a replacement for is_pathname_mounted which tries to reduce
|
|
|
903fa7 |
races by opening paths speculatively and passing back the open fds once they're
|
|
|
903fa7 |
known to be correct. lgfs2_open_mnt_{dev,dir} build on this to provide a
|
|
|
903fa7 |
convenient way to open just the device or mount directory relating to a path
|
|
|
903fa7 |
which could be either.
|
|
|
903fa7 |
|
|
|
903fa7 |
Resolves: bz#991204
|
|
|
903fa7 |
|
|
|
903fa7 |
Signed-off-by: Andrew Price <anprice@redhat.com>
|
|
|
903fa7 |
|
|
|
903fa7 |
diff --git a/gfs2/libgfs2/libgfs2.h b/gfs2/libgfs2/libgfs2.h
|
|
|
903fa7 |
index f864a08..3e5d09c 100644
|
|
|
903fa7 |
--- a/gfs2/libgfs2/libgfs2.h
|
|
|
903fa7 |
+++ b/gfs2/libgfs2/libgfs2.h
|
|
|
903fa7 |
@@ -12,6 +12,7 @@
|
|
|
903fa7 |
#include <linux/limits.h>
|
|
|
903fa7 |
#include <endian.h>
|
|
|
903fa7 |
#include <byteswap.h>
|
|
|
903fa7 |
+#include <mntent.h>
|
|
|
903fa7 |
|
|
|
903fa7 |
#include <linux/gfs2_ondisk.h>
|
|
|
903fa7 |
#include "osi_list.h"
|
|
|
903fa7 |
@@ -715,6 +716,9 @@ extern int compute_heightsize(struct gfs2_sbd *sdp, uint64_t *heightsize,
|
|
|
903fa7 |
uint32_t *maxheight, uint32_t bsize1, int diptrs, int inptrs);
|
|
|
903fa7 |
extern int compute_constants(struct gfs2_sbd *sdp);
|
|
|
903fa7 |
extern int is_pathname_mounted(char *path_name, char *device_name, int *ro_mount);
|
|
|
903fa7 |
+extern int lgfs2_open_mnt(const char *path, int dirflags, int *dirfd, int devflags, int *devfd, struct mntent **mnt);
|
|
|
903fa7 |
+extern int lgfs2_open_mnt_dev(const char *path, int flags, struct mntent **mnt);
|
|
|
903fa7 |
+extern int lgfs2_open_mnt_dir(const char *path, int flags, struct mntent **mnt);
|
|
|
903fa7 |
extern int find_gfs2_meta(struct gfs2_sbd *sdp);
|
|
|
903fa7 |
extern int dir_exists(const char *dir);
|
|
|
903fa7 |
extern int mount_gfs2_meta(struct gfs2_sbd *sdp);
|
|
|
903fa7 |
diff --git a/gfs2/libgfs2/misc.c b/gfs2/libgfs2/misc.c
|
|
|
903fa7 |
index 7f500e6..195b983 100644
|
|
|
903fa7 |
--- a/gfs2/libgfs2/misc.c
|
|
|
903fa7 |
+++ b/gfs2/libgfs2/misc.c
|
|
|
903fa7 |
@@ -163,6 +163,100 @@ int is_pathname_mounted(char *path_name, char *device_name, int *ro_mount)
|
|
|
903fa7 |
return 1; /* mounted */
|
|
|
903fa7 |
}
|
|
|
903fa7 |
|
|
|
903fa7 |
+/* Returns 0 if fd1 and fd2 refer to the same device/file, 1 otherwise, or -1 on error */
|
|
|
903fa7 |
+static int fdcmp(int fd1, int fd2)
|
|
|
903fa7 |
+{
|
|
|
903fa7 |
+ struct stat st1, st2;
|
|
|
903fa7 |
+ if ((fstat(fd1, &st1) != 0) || (fstat(fd2, &st2) != 0))
|
|
|
903fa7 |
+ return -1;
|
|
|
903fa7 |
+ if (S_ISBLK(st1.st_mode) && S_ISBLK(st2.st_mode)) {
|
|
|
903fa7 |
+ if (st1.st_rdev == st2.st_rdev) {
|
|
|
903fa7 |
+ return 0;
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ } else if ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino)) {
|
|
|
903fa7 |
+ return 0;
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ return 1;
|
|
|
903fa7 |
+}
|
|
|
903fa7 |
+
|
|
|
903fa7 |
+int lgfs2_open_mnt(const char *path, int dirflags, int *dirfd, int devflags, int *devfd, struct mntent **mnt)
|
|
|
903fa7 |
+{
|
|
|
903fa7 |
+ FILE *fp = setmntent("/proc/mounts", "r");
|
|
|
903fa7 |
+ if (fp == NULL) {
|
|
|
903fa7 |
+ perror("open: /proc/mounts");
|
|
|
903fa7 |
+ return 1;
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ /* Assume path is mount point until we know better. */
|
|
|
903fa7 |
+ *dirfd = open(path, dirflags);
|
|
|
903fa7 |
+ if (*dirfd < 0)
|
|
|
903fa7 |
+ return 1;
|
|
|
903fa7 |
+
|
|
|
903fa7 |
+ while ((*mnt = getmntent(fp)) != NULL) {
|
|
|
903fa7 |
+ int fd;
|
|
|
903fa7 |
+ if (strcmp((*mnt)->mnt_type, "gfs2") != 0)
|
|
|
903fa7 |
+ continue;
|
|
|
903fa7 |
+ *devfd = open((*mnt)->mnt_fsname, devflags);
|
|
|
903fa7 |
+ /* Defer checking *devfd until later: whether it's ok to ignore
|
|
|
903fa7 |
+ * the error depends on whether we find the mount point. */
|
|
|
903fa7 |
+
|
|
|
903fa7 |
+ if (strcmp(path, (*mnt)->mnt_dir) == 0)
|
|
|
903fa7 |
+ break;
|
|
|
903fa7 |
+ if (strcmp(path, (*mnt)->mnt_fsname) == 0 || fdcmp(*dirfd, *devfd) == 0) {
|
|
|
903fa7 |
+ /* We have a match but our above assumption was
|
|
|
903fa7 |
+ incorrect and *dirfd is actually the device. */
|
|
|
903fa7 |
+ close(*dirfd);
|
|
|
903fa7 |
+ *dirfd = open((*mnt)->mnt_dir, dirflags);
|
|
|
903fa7 |
+ break;
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+
|
|
|
903fa7 |
+ fd = open((*mnt)->mnt_dir, dirflags);
|
|
|
903fa7 |
+ if (fd >= 0) {
|
|
|
903fa7 |
+ int diff = fdcmp(*dirfd, fd);
|
|
|
903fa7 |
+ close(fd);
|
|
|
903fa7 |
+ if (diff == 0)
|
|
|
903fa7 |
+ break;
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ if (*devfd >= 0)
|
|
|
903fa7 |
+ close(*devfd);
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ endmntent(fp);
|
|
|
903fa7 |
+ if (*mnt == NULL) {
|
|
|
903fa7 |
+ close(*dirfd);
|
|
|
903fa7 |
+ return 0; /* Success. Answer is no. Both fds closed. */
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ if (*dirfd < 0) {
|
|
|
903fa7 |
+ close(*devfd);
|
|
|
903fa7 |
+ return 1;
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ if (*devfd < 0) {
|
|
|
903fa7 |
+ close(*dirfd);
|
|
|
903fa7 |
+ return 1;
|
|
|
903fa7 |
+ }
|
|
|
903fa7 |
+ return 0; /* Success. Answer is yes. Both fds open. */
|
|
|
903fa7 |
+}
|
|
|
903fa7 |
+
|
|
|
903fa7 |
+int lgfs2_open_mnt_dev(const char *path, int flags, struct mntent **mnt)
|
|
|
903fa7 |
+{
|
|
|
903fa7 |
+ int dirfd = -1;
|
|
|
903fa7 |
+ int devfd = -1;
|
|
|
903fa7 |
+ if (lgfs2_open_mnt(path, O_RDONLY, &dirfd, flags, &devfd, mnt) != 0)
|
|
|
903fa7 |
+ return -1;
|
|
|
903fa7 |
+ if (*mnt != NULL)
|
|
|
903fa7 |
+ close(dirfd);
|
|
|
903fa7 |
+ return devfd;
|
|
|
903fa7 |
+}
|
|
|
903fa7 |
+
|
|
|
903fa7 |
+int lgfs2_open_mnt_dir(const char *path, int flags, struct mntent **mnt)
|
|
|
903fa7 |
+{
|
|
|
903fa7 |
+ int dirfd = -1;
|
|
|
903fa7 |
+ int devfd = -1;
|
|
|
903fa7 |
+ if (lgfs2_open_mnt(path, flags, &dirfd, O_RDONLY, &devfd, mnt) != 0)
|
|
|
903fa7 |
+ return -1;
|
|
|
903fa7 |
+ if (*mnt != NULL)
|
|
|
903fa7 |
+ close(devfd);
|
|
|
903fa7 |
+ return dirfd;
|
|
|
903fa7 |
+}
|
|
|
903fa7 |
+
|
|
|
903fa7 |
static int lock_for_admin(struct gfs2_sbd *sdp)
|
|
|
903fa7 |
{
|
|
|
903fa7 |
int error;
|