walters / rpms / nfs-utils

Forked from rpms/nfs-utils 6 years ago
Clone
f777afd
diff --git a/utils/mount/parse_opt.c b/utils/mount/parse_opt.c
f777afd
index 1dfee8a..f0918f7 100644
f777afd
--- a/utils/mount/parse_opt.c
f777afd
+++ b/utils/mount/parse_opt.c
f777afd
@@ -101,6 +101,37 @@ fail:
f777afd
 	return NULL;
f777afd
 }
f777afd
 
f777afd
+static struct mount_option *option_dup(const struct mount_option *option)
f777afd
+{
f777afd
+	struct mount_option *new;
f777afd
+
f777afd
+	new = malloc(sizeof(*new));
f777afd
+	if (!new)
f777afd
+		return NULL;
f777afd
+	
f777afd
+	new->next = NULL;
f777afd
+	new->prev = NULL;
f777afd
+
f777afd
+	new->keyword = strdup(option->keyword);
f777afd
+	if (!new->keyword)
f777afd
+		goto fail;
f777afd
+
f777afd
+	new->value = NULL;
f777afd
+	if (option->value) {
f777afd
+		new->value = strdup(option->value);
f777afd
+		if (!new->value) {
f777afd
+			free(new->keyword);
f777afd
+			goto fail;
f777afd
+		}
f777afd
+	}
f777afd
+
f777afd
+	return new;
f777afd
+
f777afd
+fail:
f777afd
+	free(new);
f777afd
+	return NULL;
f777afd
+}
f777afd
+
f777afd
 static void option_destroy(struct mount_option *option)
f777afd
 {
f777afd
 	free(option->keyword);
f777afd
@@ -229,6 +260,40 @@ fail:
f777afd
 }
f777afd
 
f777afd
 /**
f777afd
+ * po_dup - duplicate an existing list of options
f777afd
+ * @options: pointer to mount options
f777afd
+ *
f777afd
+ */
f777afd
+struct mount_options *po_dup(struct mount_options *source)
f777afd
+{
f777afd
+	struct mount_options *target;
f777afd
+	struct mount_option *current;
f777afd
+
f777afd
+	if (!source)
f777afd
+		return NULL;
f777afd
+
f777afd
+	target = options_create();
f777afd
+	if (options_empty(source) || target == NULL)
f777afd
+		return target;
f777afd
+
f777afd
+	current = source->head;
f777afd
+	while (target->count < source->count) {
f777afd
+		struct mount_option *option;
f777afd
+
f777afd
+		option = option_dup(current);
f777afd
+		if (!option) {
f777afd
+			po_destroy(target);
f777afd
+			return NULL;
f777afd
+		}
f777afd
+
f777afd
+		options_tail_insert(target, option);
f777afd
+		current = current->next;
f777afd
+	}
f777afd
+
f777afd
+	return target;
f777afd
+}
f777afd
+
f777afd
+/**
f777afd
  * po_replace - replace mount options in one mount_options object with another
f777afd
  * @target: pointer to previously instantiated object to replace
f777afd
  * @source: pointer to object containing source mount options
f777afd
diff --git a/utils/mount/parse_opt.h b/utils/mount/parse_opt.h
f777afd
index f9243c3..2c0b5f4 100644
f777afd
--- a/utils/mount/parse_opt.h
f777afd
+++ b/utils/mount/parse_opt.h
f777afd
@@ -38,6 +38,7 @@ typedef enum {
f777afd
 struct mount_options;
f777afd
 
f777afd
 struct mount_options *	po_split(char *);
f777afd
+struct mount_options *	po_dup(struct mount_options *);
f777afd
 void			po_replace(struct mount_options *,
f777afd
 				   struct mount_options *);
f777afd
 po_return_t		po_join(struct mount_options *, char **);
f777afd
diff --git a/utils/mount/stropts.c b/utils/mount/stropts.c
f777afd
index 3eb661e..069bdc1 100644
f777afd
--- a/utils/mount/stropts.c
f777afd
+++ b/utils/mount/stropts.c
f777afd
@@ -80,6 +80,8 @@ struct nfsmount_info {
f777afd
 				*node,		/* mounted-on dir */
f777afd
 				*type;		/* "nfs" or "nfs4" */
f777afd
 	char			*hostname;	/* server's hostname */
f777afd
+	struct sockaddr_storage	address;	/* server's address */
f777afd
+	socklen_t		salen;		/* size of server's address */
f777afd
 
f777afd
 	struct mount_options	*options;	/* parsed mount options */
f777afd
 	char			**extra_opts;	/* string for /etc/mtab */
f777afd
@@ -257,47 +259,35 @@ static int nfs_append_sloppy_option(struct mount_options *options)
f777afd
 }
f777afd
 
f777afd
 /*
f777afd
- * Set up mandatory NFS mount options.
f777afd
+ * Set up mandatory non-version specific NFS mount options.
f777afd
  *
f777afd
  * Returns 1 if successful; otherwise zero.
f777afd
  */
f777afd
 static int nfs_validate_options(struct nfsmount_info *mi)
f777afd
 {
f777afd
-	struct sockaddr_storage dummy;
f777afd
-	struct sockaddr *sap = (struct sockaddr *)&dummy;
f777afd
-	socklen_t salen = sizeof(dummy);
f777afd
+	struct sockaddr *sap = (struct sockaddr *)&mi->address;
f777afd
 
f777afd
 	if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
f777afd
 		return 0;
f777afd
 
f777afd
-	if (!nfs_name_to_address(mi->hostname, sap, &salen))
f777afd
+	mi->salen = sizeof(mi->address);
f777afd
+	if (!nfs_name_to_address(mi->hostname, sap, &mi->salen))
f777afd
 		return 0;
f777afd
 
f777afd
 	if (!nfs_nfs_version(mi->options, &mi->version))
f777afd
 		return 0;
f777afd
 	if (strncmp(mi->type, "nfs4", 4) == 0)
f777afd
 		mi->version = 4;
f777afd
-
f777afd
-	if (mi->version == 4) {
f777afd
-		if (!nfs_append_clientaddr_option(sap, salen, mi->options))
f777afd
-			return 0;
f777afd
-	} else {
f777afd
-		if (!nfs_fix_mounthost_option(mi->options))
f777afd
-			return 0;
f777afd
-		if (!mi->fake && !nfs_verify_lock_option(mi->options))
f777afd
-			return 0;
f777afd
+	else {
f777afd
+		char *option = po_get(mi->options, "proto");
f777afd
+		if (option && strcmp(option, "rdma") == 0)
f777afd
+			mi->version = 3;
f777afd
 	}
f777afd
 
f777afd
 	if (!nfs_append_sloppy_option(mi->options))
f777afd
 		return 0;
f777afd
 
f777afd
-	if (!nfs_append_addr_option(sap, salen, mi->options))
f777afd
-		return 0;
f777afd
-
f777afd
-	/*
f777afd
-	 * Update option string to be recorded in /etc/mnttab
f777afd
-	 */
f777afd
-	if (po_join(mi->options, mi->extra_opts) == PO_FAILED)
f777afd
+	if (!nfs_append_addr_option(sap, mi->salen, mi->options))
f777afd
 		return 0;
f777afd
 
f777afd
 	return 1;
f777afd
@@ -489,17 +479,12 @@ out:
f777afd
  * Returns TRUE if successful, otherwise FALSE.
f777afd
  * "errno" is set to reflect the individual error.
f777afd
  */
f777afd
-static int nfs_try_mount(struct nfsmount_info *mi)
f777afd
+static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
f777afd
 {
f777afd
 	char *options = NULL;
f777afd
 	int result;
f777afd
 
f777afd
-	if (mi->version != 4) {
f777afd
-		if (!nfs_rewrite_pmap_mount_options(mi->options))
f777afd
-			return 0;
f777afd
-	}
f777afd
-
f777afd
-	if (po_join(mi->options, &options) == PO_FAILED) {
f777afd
+	if (po_join(opts, &options) == PO_FAILED) {
f777afd
 		errno = EIO;
f777afd
 		return 0;
f777afd
 	}
f777afd
@@ -522,6 +507,121 @@ static int nfs_try_mount(struct nfsmount_info *mi)
f777afd
 }
f777afd
 
f777afd
 /*
f777afd
+ * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
f777afd
+ */
f777afd
+static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
f777afd
+{
f777afd
+	struct mount_options *options = po_dup(mi->options);
f777afd
+	int result = 0;
f777afd
+
f777afd
+	if (!options) {
f777afd
+		errno = ENOMEM;
f777afd
+		return result;
f777afd
+	}
f777afd
+
f777afd
+	if (!nfs_fix_mounthost_option(options)) {
f777afd
+		errno = EINVAL;
f777afd
+		goto out_fail;
f777afd
+	}
f777afd
+	if (!mi->fake && !nfs_verify_lock_option(options)) {
f777afd
+		errno = EINVAL;
f777afd
+		goto out_fail;
f777afd
+	}
f777afd
+
f777afd
+	/*
f777afd
+	 * Options we negotiate below may be stale by the time this
f777afd
+	 * file system is unmounted.  In order to force umount.nfs
f777afd
+	 * to renegotiate with the server, only write the user-
f777afd
+	 * specified options, and not negotiated options, to /etc/mtab.
f777afd
+	 */
f777afd
+	if (po_join(options, mi->extra_opts) == PO_FAILED) {
f777afd
+		errno = ENOMEM;
f777afd
+		goto out_fail;
f777afd
+	}
f777afd
+
f777afd
+	if (!nfs_rewrite_pmap_mount_options(options))
f777afd
+		goto out_fail;
f777afd
+
f777afd
+	result = nfs_sys_mount(mi, options);
f777afd
+
f777afd
+out_fail:
f777afd
+	po_destroy(options);
f777afd
+	return result;
f777afd
+}
f777afd
+
f777afd
+/*
f777afd
+ * For "-t nfs -o vers=4" or "-t nfs4" mounts.
f777afd
+ */
f777afd
+static int nfs_try_mount_v4(struct nfsmount_info *mi)
f777afd
+{
f777afd
+	struct sockaddr *sap = (struct sockaddr *)&mi->address;
f777afd
+	struct mount_options *options = po_dup(mi->options);
f777afd
+	int result = 0;
f777afd
+
f777afd
+	if (!options) {
f777afd
+		errno = ENOMEM;
f777afd
+		return result;
f777afd
+	}
f777afd
+
f777afd
+	if (mi->version == 0) {
f777afd
+		if (po_append(options, "vers=4") == PO_FAILED) {
f777afd
+			errno = EINVAL;
f777afd
+			goto out_fail;
f777afd
+		}
f777afd
+	}
f777afd
+
f777afd
+	if (!nfs_append_clientaddr_option(sap, mi->salen, options)) {
f777afd
+		errno = EINVAL;
f777afd
+		goto out_fail;
f777afd
+	}
f777afd
+	/*
f777afd
+	 * Update option string to be recorded in /etc/mtab.
f777afd
+	 */
f777afd
+	if (po_join(options, mi->extra_opts) == PO_FAILED) {
f777afd
+		errno = ENOMEM;
f777afd
+		return 0;
f777afd
+	}
f777afd
+
f777afd
+	result = nfs_sys_mount(mi, options);
f777afd
+
f777afd
+out_fail:
f777afd
+	po_destroy(options);
f777afd
+	return result;
f777afd
+}
f777afd
+
f777afd
+/*
f777afd
+ * This is a single pass through the fg/bg loop.
f777afd
+ *
f777afd
+ * Returns TRUE if successful, otherwise FALSE.
f777afd
+ * "errno" is set to reflect the individual error.
f777afd
+ */
f777afd
+static int nfs_try_mount(struct nfsmount_info *mi)
f777afd
+{
f777afd
+	int result = 0;
f777afd
+
f777afd
+	switch (mi->version) {
f777afd
+	case 0:
f777afd
+		if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
f777afd
+			errno = 0;
f777afd
+			result = nfs_try_mount_v4(mi);
f777afd
+			if (errno != EPROTONOSUPPORT)
f777afd
+				break;
f777afd
+		}
f777afd
+	case 2:
f777afd
+	case 3:
f777afd
+		result = nfs_try_mount_v3v2(mi);
f777afd
+		break;
f777afd
+	case 4:
f777afd
+		result = nfs_try_mount_v4(mi);
f777afd
+		break;
f777afd
+	default:
f777afd
+		errno = EIO;
f777afd
+	}
f777afd
+
f777afd
+	return result;
f777afd
+}
f777afd
+
f777afd
+/*
f777afd
  * Distinguish between permanent and temporary errors.
f777afd
  *
f777afd
  * Basically, we retry if communication with the server has