summaryrefslogtreecommitdiff
path: root/src/mount
diff options
context:
space:
mode:
authorSage Weil <sage@newdream.net>2009-07-21 16:42:19 -0700
committerSage Weil <sage@newdream.net>2009-07-21 16:42:19 -0700
commitd9d3d6254bed21c166cc457f832a39bebe807b4c (patch)
treef22affb7b28873c07952b6d773fa8470282c3700 /src/mount
parent8c5836153694459aa09528bb73c23503eb97a93e (diff)
downloadceph-d9d3d6254bed21c166cc457f832a39bebe807b4c.tar.gz
mount.ceph: update /etc/mtab
Diffstat (limited to 'src/mount')
-rw-r--r--src/mount/mount.ceph.c247
-rw-r--r--src/mount/mtab.c277
2 files changed, 524 insertions, 0 deletions
diff --git a/src/mount/mount.ceph.c b/src/mount/mount.ceph.c
new file mode 100644
index 00000000000..29417124334
--- /dev/null
+++ b/src/mount/mount.ceph.c
@@ -0,0 +1,247 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <netdb.h>
+#include <errno.h>
+#include <sys/mount.h>
+
+#define BUF_SIZE 128
+
+int verboseflag = 0;
+
+#include "mtab.c"
+
+static int safe_cat(char **pstr, int *plen, int pos, const char *str2)
+{
+ int len2 = strlen(str2);
+
+ while (*plen < pos + len2 + 1) {
+ *plen += BUF_SIZE;
+ *pstr = realloc(*pstr, (size_t)*plen);
+
+ if (!*pstr) {
+ printf("Out of memory\n");
+ exit(1);
+ }
+ }
+
+ strcpy((*pstr)+pos, str2);
+
+ return pos + len2;
+}
+
+char *mount_resolve_dest(char *orig_str)
+{
+ char *new_str;
+ char *mount_path;
+ char *tok, *p, *port_str;
+ int len, pos;
+
+ mount_path = strrchr(orig_str, ':');
+ if (!mount_path) {
+ printf("source mount path was not specified\n");
+ return NULL;
+ }
+ if (mount_path == orig_str) {
+ printf("server address expected\n");
+ return NULL;
+ }
+
+ *mount_path = '\0';
+ mount_path++;
+
+ if (!*mount_path) {
+ printf("incorrect source mount path\n");
+ return NULL;
+ }
+
+ len = BUF_SIZE;
+ new_str = (char *)malloc(len);
+
+ p = new_str;
+ pos = 0;
+
+ tok = strtok(orig_str, ",");
+
+ while (tok) {
+ struct hostent *ent;
+ char addr[16];
+
+ port_str = strchr(tok, ':');
+ if (port_str) {
+ *port_str = 0;
+ port_str++;
+ if (!*port_str)
+ port_str = NULL;
+ }
+
+ ent = gethostbyname(tok);
+
+ if (!ent) {
+ printf("server name not found: %s\n", tok);
+ free(new_str);
+ return 0;
+ }
+
+ snprintf(addr, sizeof(addr), "%u.%u.%u.%u",
+ (unsigned char)ent->h_addr[0],
+ (unsigned char)ent->h_addr[1],
+ (unsigned char)ent->h_addr[2],
+ (unsigned char)ent->h_addr[3]);
+
+ pos = safe_cat(&new_str, &len, pos, addr);
+
+ if (port_str) {
+ pos = safe_cat(&new_str, &len, pos, ":");
+ pos = safe_cat(&new_str, &len, pos, port_str);
+ }
+
+ tok = strtok(NULL, ",");
+ if (tok)
+ pos = safe_cat(&new_str, &len, pos, ",");
+
+ }
+
+ pos = safe_cat(&new_str, &len, pos, ":");
+ pos = safe_cat(&new_str, &len, pos, mount_path);
+
+ return new_str;
+}
+
+/*
+ * this one is partialy based on parse_options() from cifs.mount.c
+ */
+static int parse_options(char ** optionsp, int * filesys_flags)
+{
+ const char * data;
+ char * value = NULL;
+ char * next_keyword = NULL;
+ char * out = NULL;
+ int out_len = 0;
+ int word_len;
+ int skip;
+ int pos = 0;
+
+ if (!optionsp || !*optionsp)
+ return 1;
+ data = *optionsp;
+
+ if(verboseflag)
+ printf("parsing options: %s\n", data);
+
+ while(data != NULL) {
+ /* check if ends with trailing comma */
+ if(*data == 0)
+ break;
+
+ next_keyword = strchr(data,',');
+
+ /* temporarily null terminate end of keyword=value pair */
+ if(next_keyword)
+ *next_keyword++ = 0;
+
+ /* temporarily null terminate keyword to make keyword and value distinct */
+ if ((value = strchr(data, '=')) != NULL) {
+ *value = '\0';
+ value++;
+ }
+
+ skip = 1;
+
+ if (strncmp(data, "nosuid", 6) == 0) {
+ *filesys_flags |= MS_NOSUID;
+ } else if (strncmp(data, "suid", 4) == 0) {
+ *filesys_flags &= ~MS_NOSUID;
+ } else if (strncmp(data, "nodev", 5) == 0) {
+ *filesys_flags |= MS_NODEV;
+ } else if ((strncmp(data, "nobrl", 5) == 0) ||
+ (strncmp(data, "nolock", 6) == 0)) {
+ *filesys_flags &= ~MS_MANDLOCK;
+ } else if (strncmp(data, "dev", 3) == 0) {
+ *filesys_flags &= ~MS_NODEV;
+ } else if (strncmp(data, "noexec", 6) == 0) {
+ *filesys_flags |= MS_NOEXEC;
+ } else if (strncmp(data, "exec", 4) == 0) {
+ *filesys_flags &= ~MS_NOEXEC;
+ } else if (strncmp(data, "ro", 2) == 0) {
+ *filesys_flags |= MS_RDONLY;
+ } else if (strncmp(data, "rw", 2) == 0) {
+ *filesys_flags &= ~MS_RDONLY;
+ } else if (strncmp(data, "remount", 7) == 0) {
+ *filesys_flags |= MS_REMOUNT;
+ } else {
+ skip = 0;
+ /* printf("ceph: Unknown mount option %s\n",data); */
+ }
+
+ /* Copy (possibly modified) option to out */
+ if (!skip) {
+ word_len = strlen(data);
+ if (value)
+ word_len += 1 + strlen(value);
+
+ if (pos)
+ pos = safe_cat(&out, &out_len, pos, ",");
+
+ if (value) {
+ pos = safe_cat(&out, &out_len, pos, data);
+ pos = safe_cat(&out, &out_len, pos, "=");
+ pos = safe_cat(&out, &out_len, pos, value);
+ } else {
+ pos = safe_cat(&out, &out_len, pos, data);
+ }
+
+ }
+ data = next_keyword;
+ }
+
+ *optionsp = out;
+ return 0;
+}
+
+
+int main(int argc, char *argv[])
+{
+ int i;
+ char **new_argv;
+ int flags = 0;
+ int options_pos = 0;
+
+ if (argc < 5)
+ exit(1);
+
+ new_argv = (char **)malloc(sizeof(char *)*argc);
+
+ for (i=0; i<argc; i++) {
+ new_argv[i] = argv[i];
+ if (strcmp(new_argv[i], "-o") == 0) {
+ options_pos = i+1;
+ if (options_pos >= argc) {
+ printf("usage error\n");
+ exit(1);
+ }
+ } else if (strcmp(new_argv[i], "-v") == 0) {
+ verboseflag = 1;
+ }
+ }
+
+ new_argv[1] = mount_resolve_dest(argv[1]);
+
+ parse_options(&new_argv[options_pos], &flags);
+
+ if (mount(new_argv[1], new_argv[2], "ceph", flags, new_argv[options_pos])) {
+ switch (errno) {
+ case ENODEV:
+ printf("mount error: ceph filesystem not supported by the system\n");
+ break;
+ default:
+ printf("mount error %d = %s\n",errno,strerror(errno));
+ }
+ } else {
+ update_mtab_entry(new_argv[1], new_argv[2], "ceph", new_argv[options_pos], flags, 0, 0);
+ }
+
+ free(new_argv);
+ exit(0);
+}
+
diff --git a/src/mount/mtab.c b/src/mount/mtab.c
new file mode 100644
index 00000000000..48c68971a46
--- /dev/null
+++ b/src/mount/mtab.c
@@ -0,0 +1,277 @@
+
+/*
+ * this code lifted from util-linux-ng, licensed GPLv2+,
+ *
+ * git://git.kernel.org/pub/scm/utils/util-linux-ng/util-linux-ng.git
+ *
+ * whoever decided that each special mount program is responsible
+ * for updating /etc/mtab should be spanked.
+ *
+ * <sage@newdream.net>
+ */
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <time.h>
+#include <mntent.h>
+#include <stdarg.h>
+
+
+/* Updating mtab ----------------------------------------------*/
+
+/* Flag for already existing lock file. */
+static int we_created_lockfile = 0;
+static int lockfile_fd = -1;
+
+/* Flag to indicate that signals have been set up. */
+static int signals_have_been_setup = 0;
+
+/* Ensure that the lock is released if we are interrupted. */
+extern char *strsignal(int sig); /* not always in <string.h> */
+
+static void
+setlkw_timeout (int sig) {
+ /* nothing, fcntl will fail anyway */
+}
+
+#define _PATH_MOUNTED "/etc/mtab"
+#define _PATH_MOUNTED_LOCK "/etc/mtab~"
+
+/* exit status - bits below are ORed */
+#define EX_USAGE 1 /* incorrect invocation or permission */
+#define EX_SYSERR 2 /* out of memory, cannot fork, ... */
+#define EX_SOFTWARE 4 /* internal mount bug or wrong version */
+#define EX_USER 8 /* user interrupt */
+#define EX_FILEIO 16 /* problems writing, locking, ... mtab/fstab */
+#define EX_FAIL 32 /* mount failure */
+#define EX_SOMEOK 64 /* some mount succeeded */
+
+int die(int err, const char *fmt, ...) {
+ va_list args;
+
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ fprintf(stderr, "\n");
+ va_end(args);
+
+ exit(err);
+}
+
+static void
+handler (int sig) {
+ die(EX_USER, "%s", strsignal(sig));
+}
+
+/* Remove lock file. */
+void
+unlock_mtab (void) {
+ if (we_created_lockfile) {
+ close(lockfile_fd);
+ lockfile_fd = -1;
+ unlink (_PATH_MOUNTED_LOCK);
+ we_created_lockfile = 0;
+ }
+}
+
+/* Create the lock file.
+ The lock file will be removed if we catch a signal or when we exit. */
+/* The old code here used flock on a lock file /etc/mtab~ and deleted
+ this lock file afterwards. However, as rgooch remarks, that has a
+ race: a second mount may be waiting on the lock and proceed as
+ soon as the lock file is deleted by the first mount, and immediately
+ afterwards a third mount comes, creates a new /etc/mtab~, applies
+ flock to that, and also proceeds, so that the second and third mount
+ now both are scribbling in /etc/mtab.
+ The new code uses a link() instead of a creat(), where we proceed
+ only if it was us that created the lock, and hence we always have
+ to delete the lock afterwards. Now the use of flock() is in principle
+ superfluous, but avoids an arbitrary sleep(). */
+
+/* Where does the link point to? Obvious choices are mtab and mtab~~.
+ HJLu points out that the latter leads to races. Right now we use
+ mtab~.<pid> instead. Use 20 as upper bound for the length of %d. */
+#define MOUNTLOCK_LINKTARGET _PATH_MOUNTED_LOCK "%d"
+#define MOUNTLOCK_LINKTARGET_LTH (sizeof(_PATH_MOUNTED_LOCK)+20)
+
+/*
+ * The original mount locking code has used sleep(1) between attempts and
+ * maximal number of attemps has been 5.
+ *
+ * There was very small number of attempts and extremely long waiting (1s)
+ * that is useless on machines with large number of concurret mount processes.
+ *
+ * Now we wait few thousand microseconds between attempts and we have global
+ * time limit (30s) rather than limit for number of attempts. The advantage
+ * is that this method also counts time which we spend in fcntl(F_SETLKW) and
+ * number of attempts is not so much restricted.
+ *
+ * -- kzak@redhat.com [2007-Mar-2007]
+ */
+
+/* maximum seconds between first and last attempt */
+#define MOUNTLOCK_MAXTIME 30
+
+/* sleep time (in microseconds, max=999999) between attempts */
+#define MOUNTLOCK_WAITTIME 5000
+
+void
+lock_mtab (void) {
+ int i;
+ struct timespec waittime;
+ struct timeval maxtime;
+ char linktargetfile[MOUNTLOCK_LINKTARGET_LTH];
+
+ if (!signals_have_been_setup) {
+ int sig = 0;
+ struct sigaction sa;
+
+ sa.sa_handler = handler;
+ sa.sa_flags = 0;
+ sigfillset (&sa.sa_mask);
+
+ while (sigismember (&sa.sa_mask, ++sig) != -1
+ && sig != SIGCHLD) {
+ if (sig == SIGALRM)
+ sa.sa_handler = setlkw_timeout;
+ else
+ sa.sa_handler = handler;
+ sigaction (sig, &sa, (struct sigaction *) 0);
+ }
+ signals_have_been_setup = 1;
+ }
+
+ sprintf(linktargetfile, MOUNTLOCK_LINKTARGET, getpid ());
+
+ i = open (linktargetfile, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
+ if (i < 0) {
+ int errsv = errno;
+ /* linktargetfile does not exist (as a file)
+ and we cannot create it. Read-only filesystem?
+ Too many files open in the system?
+ Filesystem full? */
+ die (EX_FILEIO, "can't create lock file %s: %s "
+ "(use -n flag to override)",
+ linktargetfile, strerror (errsv));
+ }
+ close(i);
+
+ gettimeofday(&maxtime, NULL);
+ maxtime.tv_sec += MOUNTLOCK_MAXTIME;
+
+ waittime.tv_sec = 0;
+ waittime.tv_nsec = (1000 * MOUNTLOCK_WAITTIME);
+
+ /* Repeat until it was us who made the link */
+ while (!we_created_lockfile) {
+ struct timeval now;
+ struct flock flock;
+ int errsv, j;
+
+ j = link(linktargetfile, _PATH_MOUNTED_LOCK);
+ errsv = errno;
+
+ if (j == 0)
+ we_created_lockfile = 1;
+
+ if (j < 0 && errsv != EEXIST) {
+ (void) unlink(linktargetfile);
+ die (EX_FILEIO, "can't link lock file %s: %s "
+ "(use -n flag to override)",
+ _PATH_MOUNTED_LOCK, strerror (errsv));
+ }
+
+ lockfile_fd = open (_PATH_MOUNTED_LOCK, O_WRONLY);
+
+ if (lockfile_fd < 0) {
+ /* Strange... Maybe the file was just deleted? */
+ int errsv = errno;
+ gettimeofday(&now, NULL);
+ if (errno == ENOENT && now.tv_sec < maxtime.tv_sec) {
+ we_created_lockfile = 0;
+ continue;
+ }
+ (void) unlink(linktargetfile);
+ die (EX_FILEIO, "can't open lock file %s: %s "
+ "(use -n flag to override)",
+ _PATH_MOUNTED_LOCK, strerror (errsv));
+ }
+
+ flock.l_type = F_WRLCK;
+ flock.l_whence = SEEK_SET;
+ flock.l_start = 0;
+ flock.l_len = 0;
+
+ if (j == 0) {
+ /* We made the link. Now claim the lock. */
+ if (fcntl (lockfile_fd, F_SETLK, &flock) == -1) {
+ /* proceed, since it was us who created the lockfile anyway */
+ }
+ (void) unlink(linktargetfile);
+ } else {
+ /* Someone else made the link. Wait. */
+ gettimeofday(&now, NULL);
+ if (now.tv_sec < maxtime.tv_sec) {
+ alarm(maxtime.tv_sec - now.tv_sec);
+ if (fcntl (lockfile_fd, F_SETLKW, &flock) == -1) {
+ int errsv = errno;
+ (void) unlink(linktargetfile);
+ die (EX_FILEIO, "can't lock lock file %s: %s",
+ _PATH_MOUNTED_LOCK, (errno == EINTR) ?
+ "timed out" : strerror (errsv));
+ }
+ alarm(0);
+
+ nanosleep(&waittime, NULL);
+ } else {
+ (void) unlink(linktargetfile);
+ die (EX_FILEIO, "Cannot create link %s\n"
+ "Perhaps there is a stale lock file?\n",
+ _PATH_MOUNTED_LOCK);
+ }
+ close(lockfile_fd);
+ }
+ }
+}
+
+static void
+update_mtab_entry(const char *spec, const char *node, const char *type,
+ const char *opts, int flags, int freq, int pass) {
+ struct mntent mnt;
+
+ if (!opts)
+ opts = "rw";
+
+ mnt.mnt_fsname = strdup(spec);
+ mnt.mnt_dir = strdup(node);
+ mnt.mnt_type = strdup(type);
+ mnt.mnt_opts = strdup(opts);
+ mnt.mnt_freq = freq;
+ mnt.mnt_passno = pass;
+
+ FILE *fp;
+
+ lock_mtab();
+ fp = setmntent(_PATH_MOUNTED, "a+");
+ if (fp == NULL) {
+ int errsv = errno;
+ printf("mount: can't open %s: %s", _PATH_MOUNTED,
+ strerror (errsv));
+ } else {
+ if ((addmntent (fp, &mnt)) == 1) {
+ int errsv = errno;
+ printf("mount: error writing %s: %s",
+ _PATH_MOUNTED, strerror (errsv));
+ }
+ }
+ endmntent(fp);
+ unlock_mtab();
+
+ free(mnt.mnt_fsname);
+ free(mnt.mnt_dir);
+}