e9aa863
diff -up nfs-utils-1.2.1/support/export/xtab.c.orig nfs-utils-1.2.1/support/export/xtab.c
e9aa863
--- nfs-utils-1.2.1/support/export/xtab.c.orig	2009-11-04 06:13:56.000000000 -0500
e9aa863
+++ nfs-utils-1.2.1/support/export/xtab.c	2009-11-11 14:01:56.370963000 -0500
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
8152f82
@@ -36,6 +38,8 @@ xtab_read(char *xtab, char *lockfn, int 
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))) {
8152f82
@@ -48,6 +52,8 @@ xtab_read(char *xtab, char *lockfn, int 
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 */
e9aa863
diff -up nfs-utils-1.2.1/support/include/exportfs.h.orig nfs-utils-1.2.1/support/include/exportfs.h
e9aa863
--- nfs-utils-1.2.1/support/include/exportfs.h.orig	2009-11-04 06:13:56.000000000 -0500
e9aa863
+++ nfs-utils-1.2.1/support/include/exportfs.h	2009-11-11 14:01:56.377960000 -0500
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,
e9aa863
diff -up nfs-utils-1.2.1/support/include/nfs/export.h.orig nfs-utils-1.2.1/support/include/nfs/export.h
e9aa863
--- nfs-utils-1.2.1/support/include/nfs/export.h.orig	2009-11-04 06:13:56.000000000 -0500
e9aa863
+++ nfs-utils-1.2.1/support/include/nfs/export.h	2009-11-11 14:01:56.383967000 -0500
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 */
e9aa863
diff -up nfs-utils-1.2.1/support/include/nfslib.h.orig nfs-utils-1.2.1/support/include/nfslib.h
e9aa863
--- nfs-utils-1.2.1/support/include/nfslib.h.orig	2009-11-04 06:13:56.000000000 -0500
e9aa863
+++ nfs-utils-1.2.1/support/include/nfslib.h	2009-11-11 14:01:56.390963000 -0500
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
 
e9aa863
diff -up /dev/null nfs-utils-1.2.1/support/include/v4root.h
e9aa863
--- /dev/null	2009-11-11 11:00:17.075766506 -0500
e9aa863
+++ nfs-utils-1.2.1/support/include/v4root.h	2009-11-11 14:01:56.399965000 -0500
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);
8152f82
+extern struct exportent *v4root_lookup(char *, nfs_export *);
faa7f39
+extern void v4root_free(struct exportent *);
faa7f39
+extern void v4root_unset(void), v4root_set(void);
faa7f39
+
faa7f39
+#endif /* V4ROOT_H */
e9aa863
diff -up nfs-utils-1.2.1/utils/mountd/auth.c.orig nfs-utils-1.2.1/utils/mountd/auth.c
e9aa863
--- nfs-utils-1.2.1/utils/mountd/auth.c.orig	2009-11-04 06:13:56.000000000 -0500
e9aa863
+++ nfs-utils-1.2.1/utils/mountd/auth.c	2009-11-11 14:01:56.405963000 -0500
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;
e9aa863
diff -up nfs-utils-1.2.1/utils/mountd/cache.c.orig nfs-utils-1.2.1/utils/mountd/cache.c
e9aa863
--- nfs-utils-1.2.1/utils/mountd/cache.c.orig	2009-11-04 06:13:56.000000000 -0500
e9aa863
+++ nfs-utils-1.2.1/utils/mountd/cache.c	2009-11-11 14:01:56.414960000 -0500
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) 
8152f82
+						v4root = v4root_lookup(path, exp); 
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) 
8152f82
+					v4root = v4root_lookup(path, exp); 
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);
8152f82
@@ -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
 
e9aa863
diff -up nfs-utils-1.2.1/utils/mountd/Makefile.am.orig nfs-utils-1.2.1/utils/mountd/Makefile.am
e9aa863
--- nfs-utils-1.2.1/utils/mountd/Makefile.am.orig	2009-11-04 06:13:56.000000000 -0500
e9aa863
+++ nfs-utils-1.2.1/utils/mountd/Makefile.am	2009-11-11 14:01:56.421960000 -0500
8152f82
@@ -8,7 +8,7 @@ KPREFIX		= @kprefix@
8152f82
 sbin_PROGRAMS	= mountd
8152f82
 
8152f82
 mountd_SOURCES = mountd.c mount_dispatch.c auth.c rmtab.c cache.c \
8152f82
-		 svc_run.c fsloc.c mountd.h
8152f82
+		 svc_run.c fsloc.c v4root.c mountd.h
8152f82
 mountd_LDADD = ../../support/export/libexport.a \
8152f82
 	       ../../support/nfs/libnfs.a \
8152f82
 	       ../../support/misc/libmisc.a \
e9aa863
diff -up /dev/null nfs-utils-1.2.1/utils/mountd/v4root.c
e9aa863
--- /dev/null	2009-11-11 11:00:17.075766506 -0500
e9aa863
+++ nfs-utils-1.2.1/utils/mountd/v4root.c	2009-11-11 14:02:39.699100000 -0500
e9aa863
@@ -0,0 +1,432 @@
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
+
8152f82
+#define HASH_TABLE_SIZE 1021
8152f82
+typedef struct _hash_head {
8152f82
+	TAILQ_HEAD(export_list, _exports_t) h_head;
8152f82
+} hash_head;
8152f82
+hash_head exports_tbl[HASH_TABLE_SIZE];
8152f82
+
faa7f39
+typedef struct _exports_t {
faa7f39
+	TAILQ_ENTRY(_exports_t) list;
faa7f39
+	char *path;
8152f82
+	hash_head *head;
faa7f39
+	char uuid_len;
faa7f39
+	char uuid[sizeof(uuid_t)];
faa7f39
+	struct exportent p_export;
faa7f39
+} exports_t;
faa7f39
+
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
+
8152f82
+static struct exportent *v4root_create(char *, nfs_export *);
8152f82
+
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;
e9aa863
+	struct exportent *proot;
e9aa863
+	int	i, insecure = 0;
8152f82
+	char *path, *ptr;
faa7f39
+	char *hostname;
faa7f39
+
faa7f39
+	if (!v4root_needed)
faa7f39
+		return;
faa7f39
+
faa7f39
+	if (!v4root_support())
faa7f39
+		return;
faa7f39
+
faa7f39
+	pseudo_root = &pr_export;
e9aa863
+	if ((proot = v4root_create(_PATH_PSEUDO_ROOT, pseudo_root)) == NULL) {
e9aa863
+		xlog(L_WARNING, "v4root_set: Unable to create"
e9aa863
+			"pseudo export for '%s'", _PATH_PSEUDO_ROOT);
e9aa863
+		pseudo_root = NULL;
e9aa863
+		return;
e9aa863
+	}
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
+			hostname = exp->m_export.e_hostname;
faa7f39
+
8152f82
+			path = strdup(exp->m_export.e_path);
8152f82
+			ptr = path + 1;
8152f82
+			while ((ptr = strchr(ptr, '/')) != NULL) {
8152f82
+				*ptr = '\0';
8152f82
+				if (export_lookup(hostname, path, 0) == NULL)
8152f82
+					if (v4root_create(path, exp) == NULL) {
8152f82
+						xlog(L_WARNING, "v4root_set: Unable to create"
8152f82
+							"pseudo export for '%s'", path);
faa7f39
+						break;
faa7f39
+					}
8152f82
+				*ptr = '/';
8152f82
+				ptr++;
8152f82
+			}
e9aa863
+			/* Make note of insecure exports */
e9aa863
+			if (!insecure)
e9aa863
+				insecure = (exp->m_export.e_flags & NFSEXP_INSECURE_PORT);
faa7f39
+
faa7f39
+			free(path);
faa7f39
+		}
faa7f39
+	}
e9aa863
+	/*
e9aa863
+	 * If there are any insecure exports, the pseudo root
e9aa863
+	 * also has to be insecure
e9aa863
+	 */
e9aa863
+	if (insecure) {
e9aa863
+		pseudo_root->m_export.e_flags |= NFSEXP_INSECURE_PORT;
e9aa863
+		proot->e_flags |= NFSEXP_INSECURE_PORT;
e9aa863
+	}
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
+	if (pseudo_root == NULL)
faa7f39
+		return NULL;
faa7f39
+
8152f82
+	if (fsidtype != FSID_NUM)
8152f82
+		return NULL;
faa7f39
+
8152f82
+	if (fsidnum != _PSEUDO_ROOT_FSID)
8152f82
+		return NULL;
8152f82
+
8152f82
+	return &pseudo_root->m_export;
faa7f39
+}
faa7f39
+
faa7f39
+/*
faa7f39
+ * Create a pseudo export, if one does not 
faa7f39
+ * already exist.
faa7f39
+ */
8152f82
+static struct exportent *
8152f82
+v4root_create(char *path, nfs_export *exp)
faa7f39
+{
faa7f39
+	static struct exportent *p_export = NULL;
faa7f39
+	exports_t *pexp;
faa7f39
+	char uuid_len = sizeof(uuid_t);
faa7f39
+	char uuid[sizeof(uuid_t)];
faa7f39
+
faa7f39
+	if (pseudo_root == NULL)
faa7f39
+		return NULL;
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
+/*
8152f82
+ * See if the  pseudo export exists
8152f82
+ */
8152f82
+struct exportent *
8152f82
+v4root_lookup(char *path, nfs_export *exp)
8152f82
+{
8152f82
+	static struct exportent *p_export = NULL;
8152f82
+	char *epath = exp->m_export.e_path;
8152f82
+	int elen, plen;
8152f82
+	char uuid_len = sizeof(uuid_t);
8152f82
+	char uuid[sizeof(uuid_t)];
8152f82
+
8152f82
+	if (pseudo_root == NULL)
8152f82
+		return NULL;
8152f82
+
8152f82
+	/* Path needs to be a subset of e_path */
8152f82
+	elen = strlen(epath);
8152f82
+	plen = strlen(path);
8152f82
+	if (plen >= elen)
8152f82
+		return NULL;
8152f82
+
8152f82
+	if (memcmp(path, epath, plen) != 0)
8152f82
+		return NULL;
8152f82
+
8152f82
+	/* Now to see if the export exists */
8152f82
+	get_uuid(path, NULL, uuid_len, uuid);
8152f82
+	p_export = v4root_export(uuid, uuid_len);
8152f82
+
8152f82
+	return p_export;
8152f82
+}
8152f82
+
8152f82
+/*
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
+}