walters / rpms / nfs-utils

Forked from rpms/nfs-utils 6 years ago
Clone
faa7f39
diff --git a/support/export/xtab.c b/support/export/xtab.c
faa7f39
index 3b1dcce..2a43193 100644
faa7f39
--- a/support/export/xtab.c
faa7f39
+++ b/support/export/xtab.c
faa7f39
@@ -19,7 +19,9 @@
faa7f39
 #include "exportfs.h"
faa7f39
 #include "xio.h"
faa7f39
 #include "xlog.h"
faa7f39
+#include "v4root.h"
faa7f39
 
faa7f39
+int v4root_needed;
faa7f39
 static void cond_rename(char *newfile, char *oldfile);
faa7f39
 
faa7f39
 static int
faa7f39
@@ -36,6 +38,8 @@ xtab_read(char *xtab, char *lockfn, int is_export)
faa7f39
 	if ((lockid = xflock(lockfn, "r")) < 0)
faa7f39
 		return 0;
faa7f39
 	setexportent(xtab, "r");
faa7f39
+	if (is_export == 1)
faa7f39
+		v4root_needed = 1;
faa7f39
 	while ((xp = getexportent(is_export==0, 0)) != NULL) {
faa7f39
 		if (!(exp = export_lookup(xp->e_hostname, xp->e_path, is_export != 1)) &&
faa7f39
 		    !(exp = export_create(xp, is_export!=1))) {
faa7f39
@@ -48,6 +52,8 @@ xtab_read(char *xtab, char *lockfn, int is_export)
faa7f39
 		case 1:
faa7f39
 			exp->m_xtabent = 1;
faa7f39
 			exp->m_mayexport = 1;
faa7f39
+			if ((xp->e_flags & NFSEXP_FSID) && xp->e_fsid == 0)
faa7f39
+				v4root_needed = 0;
faa7f39
 			break;
faa7f39
 		case 2:
faa7f39
 			exp->m_exported = -1;/* may be exported */
faa7f39
diff --git a/support/include/exportfs.h b/support/include/exportfs.h
faa7f39
index a5cf482..1ad41e2 100644
faa7f39
--- a/support/include/exportfs.h
faa7f39
+++ b/support/include/exportfs.h
faa7f39
@@ -12,6 +12,17 @@
faa7f39
 #include <netdb.h>
faa7f39
 #include "nfslib.h"
faa7f39
 
faa7f39
+enum nfsd_fsid {
faa7f39
+	FSID_DEV = 0,
faa7f39
+	FSID_NUM,
faa7f39
+	FSID_MAJOR_MINOR,
faa7f39
+	FSID_ENCODE_DEV,
faa7f39
+	FSID_UUID4_INUM,
faa7f39
+	FSID_UUID8,
faa7f39
+	FSID_UUID16,
faa7f39
+	FSID_UUID16_INUM,
faa7f39
+};
faa7f39
+
faa7f39
 enum {
faa7f39
 	MCL_FQDN = 0,
faa7f39
 	MCL_SUBNETWORK,
faa7f39
diff --git a/support/include/nfs/export.h b/support/include/nfs/export.h
faa7f39
index f7a99ba..76953ac 100644
faa7f39
--- a/support/include/nfs/export.h
faa7f39
+++ b/support/include/nfs/export.h
faa7f39
@@ -24,6 +24,7 @@
faa7f39
 #define NFSEXP_FSID		0x2000
faa7f39
 #define	NFSEXP_CROSSMOUNT	0x4000
faa7f39
 #define NFSEXP_NOACL		0x8000 /* reserved for possible ACL related use */
faa7f39
-#define NFSEXP_ALLFLAGS		0xFFFF
faa7f39
+#define NFSEXP_V4ROOT       0x10000
faa7f39
+#define NFSEXP_ALLFLAGS		0x1FFFF
faa7f39
 
faa7f39
 #endif /* _NSF_EXPORT_H */
faa7f39
diff --git a/support/include/nfslib.h b/support/include/nfslib.h
faa7f39
index 537a31e..e4777dd 100644
faa7f39
--- a/support/include/nfslib.h
faa7f39
+++ b/support/include/nfslib.h
faa7f39
@@ -88,6 +88,7 @@ struct exportent {
faa7f39
 	int             e_fslocmethod;
faa7f39
 	char *          e_fslocdata;
faa7f39
 	char *		e_uuid;
faa7f39
+	void *		e_v4root;
faa7f39
 	struct sec_entry e_secinfo[SECFLAVOR_COUNT+1];
faa7f39
 };
faa7f39
 
faa7f39
diff --git a/support/include/v4root.h b/support/include/v4root.h
faa7f39
new file mode 100644
faa7f39
index 0000000..43b1d2e
faa7f39
--- /dev/null
faa7f39
+++ b/support/include/v4root.h
faa7f39
@@ -0,0 +1,20 @@
faa7f39
+/*
faa7f39
+ * Copyright (C) 2009 Red Hat <nfs@redhat.com>
faa7f39
+ * support/include/v4root.h
faa7f39
+ *
faa7f39
+ * Support routines for dynamic pseudo roots.
faa7f39
+ *
faa7f39
+ */
faa7f39
+
faa7f39
+#ifndef V4ROOT_H
faa7f39
+#define V4ROOT_H
faa7f39
+
faa7f39
+extern int v4root_needed;
faa7f39
+
faa7f39
+extern struct exportent *v4root_chkroot(int , unsigned int , char *);
faa7f39
+extern struct exportent *v4root_export(char *, int);
faa7f39
+extern struct exportent *v4root_create(char *, nfs_export *, int);
faa7f39
+extern void v4root_free(struct exportent *);
faa7f39
+extern void v4root_unset(void), v4root_set(void);
faa7f39
+
faa7f39
+#endif /* V4ROOT_H */
faa7f39
diff --git a/utils/mountd/Makefile.am b/utils/mountd/Makefile.am
faa7f39
index 1e76cf8..eba81fc 100644
faa7f39
--- a/utils/mountd/Makefile.am
faa7f39
+++ b/utils/mountd/Makefile.am
faa7f39
@@ -8,7 +8,7 @@ KPREFIX		= @kprefix@
faa7f39
 sbin_PROGRAMS	= mountd
faa7f39
 
faa7f39
 mountd_SOURCES = mountd.c mount_dispatch.c auth.c rmtab.c cache.c \
faa7f39
-		 svc_run.c fsloc.c mountd.h
faa7f39
+		 svc_run.c fsloc.c v4root.c mountd.h
faa7f39
 mountd_LDADD = ../../support/export/libexport.a \
faa7f39
 	       ../../support/nfs/libnfs.a \
faa7f39
 	       ../../support/misc/libmisc.a \
faa7f39
diff --git a/utils/mountd/auth.c b/utils/mountd/auth.c
faa7f39
index 575f207..041a745 100644
faa7f39
--- a/utils/mountd/auth.c
faa7f39
+++ b/utils/mountd/auth.c
faa7f39
@@ -20,6 +20,7 @@
faa7f39
 #include "exportfs.h"
faa7f39
 #include "mountd.h"
faa7f39
 #include "xmalloc.h"
faa7f39
+#include "v4root.h"
faa7f39
 
faa7f39
 enum auth_error
faa7f39
 {
faa7f39
@@ -98,10 +99,13 @@ auth_reload()
faa7f39
 		last_inode = stb.st_ino;
faa7f39
 	}
faa7f39
 
faa7f39
+	v4root_unset();
faa7f39
 	export_freeall();
faa7f39
 	memset(&my_client, 0, sizeof(my_client));
faa7f39
 	xtab_export_read();
faa7f39
 	check_useipaddr();
faa7f39
+	v4root_set();
faa7f39
+
faa7f39
 	++counter;
faa7f39
 
faa7f39
 	return counter;
faa7f39
diff --git a/utils/mountd/cache.c b/utils/mountd/cache.c
faa7f39
index e4e2f22..d631570 100644
faa7f39
--- a/utils/mountd/cache.c
faa7f39
+++ b/utils/mountd/cache.c
faa7f39
@@ -32,23 +32,12 @@
faa7f39
 #include "xmalloc.h"
faa7f39
 #include "fsloc.h"
faa7f39
 #include "pseudoflavors.h"
faa7f39
+#include "v4root.h"
faa7f39
 
faa7f39
 #ifdef USE_BLKID
faa7f39
 #include "blkid/blkid.h"
faa7f39
 #endif
faa7f39
 
faa7f39
-
faa7f39
-enum nfsd_fsid {
faa7f39
-	FSID_DEV = 0,
faa7f39
-	FSID_NUM,
faa7f39
-	FSID_MAJOR_MINOR,
faa7f39
-	FSID_ENCODE_DEV,
faa7f39
-	FSID_UUID4_INUM,
faa7f39
-	FSID_UUID8,
faa7f39
-	FSID_UUID16,
faa7f39
-	FSID_UUID16_INUM,
faa7f39
-};
faa7f39
-
faa7f39
 /*
faa7f39
  * Support routines for text-based upcalls.
faa7f39
  * Fields are separated by spaces.
faa7f39
@@ -135,6 +124,8 @@ void auth_unix_gid(FILE *f)
faa7f39
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
faa7f39
 		return;
faa7f39
 
faa7f39
+	xlog(D_CALL, "auth_unix_gid: '%s'", lbuf);
faa7f39
+
faa7f39
 	cp = lbuf;
faa7f39
 	if (qword_get_int(&cp, &uid) != 0)
faa7f39
 		return;
faa7f39
@@ -391,6 +382,12 @@ void nfsd_fh(FILE *f)
faa7f39
 
faa7f39
 	auth_reload();
faa7f39
 
faa7f39
+	/* Check to see if the kenel is looking for the pseudo root */
faa7f39
+	if ((found = v4root_chkroot(fsidtype, fsidnum, fhuuid))) {
faa7f39
+		found_path = strdup(found->e_path);
faa7f39
+		goto found;
faa7f39
+	}
faa7f39
+
faa7f39
 	/* Now determine export point for this fsid/domain */
faa7f39
 	for (i=0 ; i < MCL_MAXTYPES; i++) {
faa7f39
 		nfs_export *next_exp;
faa7f39
@@ -511,7 +508,23 @@ void nfsd_fh(FILE *f)
faa7f39
 		 */
faa7f39
 		goto out;
faa7f39
 	}
faa7f39
+	if (!found) {
faa7f39
+		/*
faa7f39
+		 * See if this is a pesudo export
faa7f39
+		 */
faa7f39
+		switch(fsidtype) {
faa7f39
+		case FSID_UUID4_INUM:
faa7f39
+		case FSID_UUID8:
faa7f39
+		case FSID_UUID16:
faa7f39
+		case FSID_UUID16_INUM:
faa7f39
+			found = v4root_export(fhuuid, uuidlen);
faa7f39
+			break;
faa7f39
+		}
faa7f39
+		if (found)
faa7f39
+			found_path = strdup(found->e_path);
faa7f39
+	}
faa7f39
 
faa7f39
+found:
faa7f39
 	if (found)
faa7f39
 		if (cache_export_ent(dom, found, found_path) < 0)
faa7f39
 			found = 0;
faa7f39
@@ -629,6 +642,7 @@ void nfsd_export(FILE *f)
faa7f39
 	int found_type = 0;
faa7f39
 	struct in_addr addr;
faa7f39
 	struct hostent *he = NULL;
faa7f39
+	struct exportent *v4root = NULL;
faa7f39
 
faa7f39
 
faa7f39
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
faa7f39
@@ -663,10 +677,18 @@ void nfsd_export(FILE *f)
faa7f39
 				     path[l] == '/' &&
faa7f39
 				     is_mountpoint(path)))
faa7f39
 					/* ok */;
faa7f39
-				else
faa7f39
+				else {
faa7f39
+					/* See if the path is part of the psuedo root */
faa7f39
+					if (v4root_needed && !v4root) 
faa7f39
+						v4root = v4root_create(path, exp, TRUE); 
faa7f39
 					continue;
faa7f39
-			} else if (strcmp(path, exp->m_export.e_path) != 0)
faa7f39
+				}
faa7f39
+			} else if (strcmp(path, exp->m_export.e_path) != 0) {
faa7f39
+				/* See if the path is part of the psuedo root */
faa7f39
+				if (v4root_needed && !v4root) 
faa7f39
+					v4root = v4root_create(path, exp, TRUE); 
faa7f39
 				continue;
faa7f39
+			}
faa7f39
 			if (use_ipaddr) {
faa7f39
 				if (he == NULL) {
faa7f39
 					if (!inet_aton(dom, &addr))
faa7f39
@@ -705,17 +727,28 @@ void nfsd_export(FILE *f)
faa7f39
 	}
faa7f39
 
faa7f39
 	if (found) {
faa7f39
+		xlog(D_CALL, "nfsd_export: found: path %s", path);
faa7f39
 		if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
faa7f39
 			xlog(L_WARNING,
faa7f39
 			     "Cannot export %s, possibly unsupported filesystem"
faa7f39
 			     " or fsid= required", path);
faa7f39
 			dump_to_cache(f, dom, path, NULL);
faa7f39
 		}
faa7f39
-	} else {
faa7f39
+	} else if (v4root) {
faa7f39
+		xlog(D_CALL, "nfsd_export: vroot: path %s", path);
faa7f39
+		dump_to_cache(f, dom, path, v4root);
faa7f39
+		found = (nfs_export *)v4root;
faa7f39
+	} else { 
faa7f39
 		dump_to_cache(f, dom, path, NULL);
faa7f39
 	}
faa7f39
  out:
faa7f39
-	xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
faa7f39
+	/*
faa7f39
+	 * If a psuedo export was create and its not needed
faa7f39
+	 * free it up.
faa7f39
+	 */
faa7f39
+	if (v4root && found != (nfs_export *)v4root)
faa7f39
+		v4root_free(v4root);
faa7f39
+
faa7f39
 	if (dom) free(dom);
faa7f39
 	if (path) free(path);
faa7f39
 	if (he) free(he);
faa7f39
@@ -743,7 +776,9 @@ void cache_open(void)
faa7f39
 		if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
faa7f39
 			continue;
faa7f39
 		sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
faa7f39
-		cachelist[i].f = fopen(path, "r+");
faa7f39
+		if ((cachelist[i].f = fopen(path, "r+")) == NULL)
faa7f39
+			xlog(L_ERROR, "cache_open: Unable to open '%s': errno %d (%s)", 
faa7f39
+				path, errno, strerror(errno));
faa7f39
 	}
faa7f39
 }
faa7f39
 
faa7f39
diff --git a/utils/mountd/v4root.c b/utils/mountd/v4root.c
faa7f39
new file mode 100644
faa7f39
index 0000000..8de2ab6
faa7f39
--- /dev/null
faa7f39
+++ b/utils/mountd/v4root.c
faa7f39
@@ -0,0 +1,420 @@
faa7f39
+/*
faa7f39
+ * Copyright (C) 2009 Red Hat <nfs@redhat.com>
faa7f39
+ *
faa7f39
+ * support/export/v4root.c
faa7f39
+ *
faa7f39
+ * Routines used to support NFSv4 pseudo roots
faa7f39
+ *
faa7f39
+ */
faa7f39
+
faa7f39
+#ifdef HAVE_CONFIG_H
faa7f39
+#include <config.h>
faa7f39
+#endif
faa7f39
+
faa7f39
+#include <sys/types.h>
faa7f39
+#include <sys/stat.h>
faa7f39
+#include <sys/queue.h>
faa7f39
+#include <stdio.h>
faa7f39
+#include <stdlib.h>
faa7f39
+#include <ctype.h>
faa7f39
+
faa7f39
+#include <uuid/uuid.h>
faa7f39
+#include <unistd.h>
faa7f39
+#include <errno.h>
faa7f39
+
faa7f39
+#include "xlog.h"
faa7f39
+#include "exportfs.h"
faa7f39
+#include "nfslib.h"
faa7f39
+#include "misc.h"
faa7f39
+#include "v4root.h"
faa7f39
+
faa7f39
+#ifndef _PATH_PSEUDO_ROOT
faa7f39
+#define _PATH_PSEUDO_ROOT		"/"
faa7f39
+#endif
faa7f39
+
faa7f39
+#ifndef _PSEUDO_ROOT_FSID
faa7f39
+#define _PSEUDO_ROOT_FSID	0
faa7f39
+#endif
faa7f39
+
faa7f39
+extern int get_uuid(char *path, char *uuid, int uuidlen, char *u);
faa7f39
+
faa7f39
+typedef struct _exports_t {
faa7f39
+	TAILQ_ENTRY(_exports_t) list;
faa7f39
+	char *path;
faa7f39
+	void *head;
faa7f39
+	char uuid_len;
faa7f39
+	char uuid[sizeof(uuid_t)];
faa7f39
+	struct exportent p_export;
faa7f39
+} exports_t;
faa7f39
+
faa7f39
+#define HASH_TABLE_SIZE 1021
faa7f39
+typedef struct _hash_head {
faa7f39
+	TAILQ_HEAD(export_list, _exports_t) h_head;
faa7f39
+} hash_head;
faa7f39
+hash_head exports_tbl[HASH_TABLE_SIZE];
faa7f39
+
faa7f39
+
faa7f39
+static exports_t *hash_export_lookup(char *, unsigned int);
faa7f39
+static void hash_export_add(struct _exports_t *, int);
faa7f39
+static void hash_mount_free(void);
faa7f39
+
faa7f39
+static inline unsigned int strtoint(char *str, int len)
faa7f39
+{
faa7f39
+	unsigned int n = 0;
faa7f39
+	int i;
faa7f39
+
faa7f39
+	for (i=0; i < len; i++)
faa7f39
+		n+=((int)str[i])*i;
faa7f39
+	return n;
faa7f39
+}
faa7f39
+static inline int hashint(unsigned int num)
faa7f39
+{
faa7f39
+	return num % HASH_TABLE_SIZE;
faa7f39
+}
faa7f39
+#define HASH(_s, _l) hashint(strtoint((_s), (_l)))
faa7f39
+void v4root_set(void);
faa7f39
+void v4root_unset(void);
faa7f39
+static int v4root_support(void);
faa7f39
+
faa7f39
+int v4root_needed;
faa7f39
+
faa7f39
+static nfs_export pr_export = {
faa7f39
+	.m_next = NULL,
faa7f39
+	.m_client = NULL,
faa7f39
+	.m_export = {
faa7f39
+		.e_hostname = "*",
faa7f39
+		.e_path = _PATH_PSEUDO_ROOT,
faa7f39
+		.e_flags = NFSEXP_READONLY | NFSEXP_ROOTSQUASH
faa7f39
+				| NFSEXP_NOSUBTREECHECK | NFSEXP_FSID
faa7f39
+				| NFSEXP_CROSSMOUNT | NFSEXP_V4ROOT,
faa7f39
+		.e_anonuid = 65534,
faa7f39
+		.e_anongid = 65534,
faa7f39
+		.e_squids = NULL,
faa7f39
+		.e_nsquids = 0,
faa7f39
+		.e_sqgids = NULL,
faa7f39
+		.e_nsqgids = 0,
faa7f39
+		.e_fsid = 0,
faa7f39
+		.e_mountpoint = NULL,
faa7f39
+	},
faa7f39
+	.m_exported = 0,
faa7f39
+	.m_xtabent = 1,
faa7f39
+	.m_mayexport = 1,
faa7f39
+	.m_changed = 0,
faa7f39
+	.m_warned = 0,
faa7f39
+};
faa7f39
+static nfs_export *pseudo_root;
faa7f39
+
faa7f39
+/*
faa7f39
+ * Return the number '/' in the path
faa7f39
+ */
faa7f39
+inline static int slash_count(char *path)
faa7f39
+{
faa7f39
+	int i, slashs=0;
faa7f39
+
faa7f39
+	for (i=0; i < strlen(path); i++) {
faa7f39
+			if (path[i] == '/')
faa7f39
+				slashs++;
faa7f39
+	}
faa7f39
+	return slashs;
faa7f39
+}
faa7f39
+/*
faa7f39
+ * Make sure the kernel has pseudo root support.
faa7f39
+ */
faa7f39
+static int
faa7f39
+v4root_support()
faa7f39
+{
faa7f39
+	static int kernel_support = -1;
faa7f39
+	char *ptr, version[64];
faa7f39
+	int major, minor;
faa7f39
+	FILE *fp;
faa7f39
+
faa7f39
+	if (kernel_support != -1)
faa7f39
+		return kernel_support;
faa7f39
+
faa7f39
+	kernel_support = 0;
faa7f39
+	fp = fopen("/proc/fs/nfsd/exports", "r");
faa7f39
+	if (fp == NULL)
faa7f39
+		goto out;
faa7f39
+
faa7f39
+	ptr = fgets(version, 64, fp);
faa7f39
+	fclose(fp);
faa7f39
+	if (ptr == NULL)
faa7f39
+		goto out;
faa7f39
+
faa7f39
+	while(*ptr && isdigit(*ptr) == 0)
faa7f39
+		ptr++;
faa7f39
+	if (*ptr == '\0')
faa7f39
+		goto out;
faa7f39
+
faa7f39
+	major = minor = 0;
faa7f39
+	sscanf(ptr, " %d.%d",&major, &minor);
faa7f39
+	if (major >= 1 && minor >= 2)
faa7f39
+		kernel_support = 1;
faa7f39
+out:
faa7f39
+	if (!kernel_support) {
faa7f39
+		xlog(L_WARNING, "Kernel does not have pseudo root support.");
faa7f39
+		xlog(L_WARNING, "NFS v4 mounts will be disabled unless fsid=0");
faa7f39
+		xlog(L_WARNING, "is specfied in /etc/exports file.");
faa7f39
+	}
faa7f39
+
faa7f39
+	return kernel_support;
faa7f39
+}
faa7f39
+/*
faa7f39
+ * Build a table of pseudo exports by running through
faa7f39
+ * the real export looking at the components of the path
faa7f39
+ * that make up the export. Those path components, if
faa7f39
+ * not exported, will become pseudo exports allowing them 
faa7f39
+ * to be found when the kernel does an upcall looking for 
faa7f39
+ * components of the v4 mount.
faa7f39
+ */
faa7f39
+void
faa7f39
+v4root_set()
faa7f39
+{
faa7f39
+	nfs_export	*exp, *nxt;
faa7f39
+	int	i, slcnt;
faa7f39
+	char *e_path, *path, *ptr, *comp;
faa7f39
+	char *hostname;
faa7f39
+
faa7f39
+	if (!v4root_needed)
faa7f39
+		return;
faa7f39
+
faa7f39
+	if (!v4root_support())
faa7f39
+		return;
faa7f39
+
faa7f39
+	pseudo_root = &pr_export;
faa7f39
+
faa7f39
+	for (i = 0; i < MCL_MAXTYPES; i++) {
faa7f39
+		for (exp = exportlist[i].p_head; exp; exp = nxt) {
faa7f39
+			nxt = exp->m_next;
faa7f39
+			e_path = exp->m_export.e_path;
faa7f39
+			hostname = exp->m_export.e_hostname;
faa7f39
+
faa7f39
+			slcnt = slash_count(e_path);
faa7f39
+			if (slcnt == 1)
faa7f39
+				continue;
faa7f39
+			slcnt--; /* knock off the leanding '/' */
faa7f39
+
faa7f39
+			path = strdup(e_path);
faa7f39
+			if (path == NULL) { 
faa7f39
+				xlog(L_WARNING, "v4root_set: No memory for pseudo export");
faa7f39
+				return;
faa7f39
+			}
faa7f39
+
faa7f39
+			/*
faa7f39
+			 * Run through each component of the path to
faa7f39
+			 * see if a  pseudo export should be created.
faa7f39
+			 */
faa7f39
+			comp = path+1;
faa7f39
+			do { 
faa7f39
+				if ((ptr = strchr(comp, '/')) != NULL)
faa7f39
+					*ptr = '\0';
faa7f39
+
faa7f39
+	  			if (export_lookup(hostname, path, 0) == NULL)
faa7f39
+					if (v4root_create(path, exp, FALSE) == NULL) {
faa7f39
+						xlog(L_WARNING, 
faa7f39
+							"v4root_set: Unable to create pseudo export"
faa7f39
+							"for '%s'", path);
faa7f39
+						break;
faa7f39
+					}
faa7f39
+
faa7f39
+				if (ptr) {
faa7f39
+					*ptr = '/';
faa7f39
+					comp = ptr+1; 
faa7f39
+				}
faa7f39
+			} while (--slcnt > 0);
faa7f39
+			free(path);
faa7f39
+		}
faa7f39
+	}
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Unset the pseudo root export
faa7f39
+ */
faa7f39
+void
faa7f39
+v4root_unset()
faa7f39
+{
faa7f39
+	pseudo_root = NULL;
faa7f39
+	hash_mount_free();
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * The kernel will do an upcall looking for the pseudo
faa7f39
+ * root via its fsid. When the wanted fsid equals 
faa7f39
+ * PSEUDO_ROOT_FSID return the pseudo root export.
faa7f39
+ */
faa7f39
+struct exportent *
faa7f39
+v4root_chkroot(int fsidtype, unsigned int fsidnum, char *fhuuid)
faa7f39
+{
faa7f39
+	struct exportent *p_export = NULL;
faa7f39
+
faa7f39
+	if (pseudo_root == NULL)
faa7f39
+		return NULL;
faa7f39
+
faa7f39
+	switch(fsidtype) {
faa7f39
+		case FSID_NUM:
faa7f39
+			if (fsidnum == _PSEUDO_ROOT_FSID) {
faa7f39
+				p_export = &pseudo_root->m_export;
faa7f39
+				strncpy(p_export->e_path, _PATH_PSEUDO_ROOT, 
faa7f39
+					NFS_MAXPATHLEN);
faa7f39
+			}
faa7f39
+			break;
faa7f39
+	}
faa7f39
+
faa7f39
+	return p_export;
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Create a pseudo export, if one does not 
faa7f39
+ * already exist.
faa7f39
+ */
faa7f39
+struct exportent *
faa7f39
+v4root_create(char *path, nfs_export *exp, int docheck)
faa7f39
+{
faa7f39
+	static struct exportent *p_export = NULL;
faa7f39
+	exports_t *pexp;
faa7f39
+	char *epath = exp->m_export.e_path;
faa7f39
+	int elen, plen;
faa7f39
+	char uuid_len = sizeof(uuid_t);
faa7f39
+	char uuid[sizeof(uuid_t)];
faa7f39
+
faa7f39
+	if (pseudo_root == NULL)
faa7f39
+		return NULL;
faa7f39
+
faa7f39
+	if (docheck) {
faa7f39
+
faa7f39
+		/* Path needs to be a subset of e_path */
faa7f39
+		elen = strlen(epath);
faa7f39
+		plen = strlen(path);
faa7f39
+		if (plen >= elen)
faa7f39
+			return NULL;
faa7f39
+
faa7f39
+		if (memcmp(path, epath, plen) != 0)
faa7f39
+			return NULL;
faa7f39
+	}
faa7f39
+
faa7f39
+	/* Check to see if the export already exists */
faa7f39
+	get_uuid(path, NULL, uuid_len, uuid);
faa7f39
+	if ((p_export = v4root_export(uuid, uuid_len)) != NULL)
faa7f39
+		return p_export;
faa7f39
+
faa7f39
+	pexp = (exports_t *)malloc(sizeof(exports_t));
faa7f39
+	if (pexp == NULL) {
faa7f39
+		xlog(L_WARNING, "v4root_create: No memory for pseudo export");
faa7f39
+		return NULL;
faa7f39
+	}
faa7f39
+	p_export = &pexp->p_export;
faa7f39
+	pexp->path = strdup(path);
faa7f39
+	if (pexp->path == 0) {
faa7f39
+		xlog(L_WARNING, "v4root_create: No memory for pseudo path");
faa7f39
+		free(pexp);
faa7f39
+		return NULL;
faa7f39
+	}
faa7f39
+	pexp->uuid_len = uuid_len;
faa7f39
+	memcpy(pexp->uuid, uuid, uuid_len);
faa7f39
+
faa7f39
+	dupexportent(&pexp->p_export, &pr_export.m_export);
faa7f39
+	strcpy(p_export->e_path, path);
faa7f39
+	p_export->e_flags &= ~NFSEXP_FSID;
faa7f39
+	p_export->e_v4root = (void *)pexp;
faa7f39
+
faa7f39
+	hash_export_add(pexp, HASH(pexp->uuid, sizeof(uuid_t)));
faa7f39
+
faa7f39
+	xlog(D_CALL, "v4root_create: path '%s'", p_export->e_path);
faa7f39
+
faa7f39
+	return p_export;
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Free a pseudo export
faa7f39
+ */
faa7f39
+void
faa7f39
+v4root_free(struct exportent *p_export)
faa7f39
+{
faa7f39
+	exports_t *pexp = (exports_t *)p_export->e_v4root;
faa7f39
+	hash_head *head = (hash_head *)pexp->head;
faa7f39
+
faa7f39
+	free(pexp->path);
faa7f39
+	TAILQ_REMOVE(&head->h_head, pexp, list);
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Return a pseudo export that match the given uuid
faa7f39
+ */
faa7f39
+struct exportent *
faa7f39
+v4root_export(char *fhuuid, int uuidlen)
faa7f39
+{
faa7f39
+	struct exportent *p_export = NULL;
faa7f39
+	exports_t *pexp;
faa7f39
+	int len = MIN(uuidlen, sizeof(uuid_t));
faa7f39
+
faa7f39
+	if (pseudo_root == NULL)
faa7f39
+		return NULL;
faa7f39
+
faa7f39
+	pexp = hash_export_lookup(fhuuid, len);
faa7f39
+	if (pexp) {
faa7f39
+		p_export = &pexp->p_export;
faa7f39
+		xlog(D_CALL, "v4root_export: path %s", p_export->e_path);
faa7f39
+	}
faa7f39
+	return p_export;
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Add pseudo export to export table
faa7f39
+ */
faa7f39
+static void hash_export_add(struct _exports_t *exp, int hash)
faa7f39
+{
faa7f39
+	hash_head *head;
faa7f39
+
faa7f39
+	head = &(exports_tbl[hash]);
faa7f39
+	exp->head = head;
faa7f39
+
faa7f39
+	if (TAILQ_EMPTY(&head->h_head))
faa7f39
+		TAILQ_INSERT_HEAD(&head->h_head, exp, list);
faa7f39
+	else
faa7f39
+		TAILQ_INSERT_TAIL(&head->h_head, exp, list);
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Lookup a pseudo export using the uuid and inode number
faa7f39
+ */
faa7f39
+static exports_t *
faa7f39
+hash_export_lookup(char *uuid, unsigned int uuidlen)
faa7f39
+{
faa7f39
+	exports_t *pexp;
faa7f39
+	hash_head *head;
faa7f39
+	int hash = HASH(uuid, uuidlen);
faa7f39
+
faa7f39
+	head = &(exports_tbl[hash]);
faa7f39
+
faa7f39
+	TAILQ_FOREACH(pexp, &head->h_head, list) {
faa7f39
+		if (memcmp(pexp->uuid, uuid, uuidlen) == 0)
faa7f39
+			return pexp;
faa7f39
+	}
faa7f39
+	return NULL;
faa7f39
+
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Free up pseudo export table
faa7f39
+ */
faa7f39
+static void hash_mount_free()
faa7f39
+{
faa7f39
+	hash_head *head;
faa7f39
+	exports_t *e1, *e2;
faa7f39
+	int hash;
faa7f39
+
faa7f39
+	for (hash=0; hash < HASH_TABLE_SIZE; hash++) {
faa7f39
+		head = &(exports_tbl[hash]);
faa7f39
+		if (head == NULL)
faa7f39
+			continue;
faa7f39
+		e1 = TAILQ_FIRST(&head->h_head);
faa7f39
+		while (e1 != NULL) {
faa7f39
+			free(e1->path);
faa7f39
+			e2 =  TAILQ_NEXT(e1, list);
faa7f39
+			TAILQ_REMOVE(&head->h_head, e1, list);
faa7f39
+			free(e1);
faa7f39
+			e1 = e2;
faa7f39
+		}
faa7f39
+		TAILQ_INIT(&head->h_head);
faa7f39
+	}
faa7f39
+}