[RFC][2.6 Patch 4/7] v9fs 2.0-rc2 - 9P2000 Support For Linux 2.6

Eric Van Hensbergen ericvh at gmail.com
Mon Apr 25 07:27:18 PDT 2005


This is part [4/7] of the v9fs patch against the Bitkeeper
tip as of 04/17/2005.  It represents v9fs as of 2.0-rc2.

This part of the patch contains the VFS superblock.
VFS address space interfaces, and VFS mapping code.

diffstats: 
 v9fs.c      |  486 
 v9fs.h      |   79 
 v9fs_vfs.h  |   58 
 vfs_addr.c  |  215 
 vfs_super.c |  273 
 5 files changed, 1111 insertions(+)

Signed-off-by: Eric Van Hensbergen <ericvh at gmail.com>

-------------

diff -Nru a/fs/9p/v9fs.c b/fs/9p/v9fs.c
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/fs/9p/v9fs.c	2005-04-25 07:38:03 -05:00
@@ -0,0 +1,486 @@
+/*
+ *  linux/fs/9p/v9fs.c
+ *
+ *  This file contains functions assisting in mapping VFS to 9P2000
+ *
+ *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh at gmail.com>
+ *  Copyright (C) 2002 by Ron Minnich <rminnich at lanl.gov>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+
+#include "debug.h"
+#include "alloc.h"
+#include "idpool.h"
+#include "v9fs.h"
+#include "9p.h"
+#include "v9fs_vfs.h"
+#include "transport.h"
+#include "mux.h"
+#include "conv.h"
+
+enum {
+	PROTO_TCP,
+	PROTO_UNIX,
+};
+
+static struct v9fs_slab v9fs_rpcreq_slab;
+int v9fs_debug_level = 0;	/* feature-rific global debug level  */
+				/* can only be set at any mount time */
+				/* TODO: sysfs interface             */
+
+/**
+ * v9fs_inode2v9ses - safely extract v9fs session info from super block
+ * @inode: inode to extract information from
+ *
+ * Paranoid function to extract v9ses information from superblock,
+ * if anything is missing it will report an error.
+ *
+ */
+
+struct v9fs_session_info *v9fs_inode2v9ses(struct inode *inode)
+{
+	if (inode) {
+		if (inode->i_sb) {
+			if (inode->i_sb->s_fs_info) 
+				return (inode->i_sb->s_fs_info);
+			else {
+				dprintk(DEBUG_ERROR, "no s_fs_info\n");
+				return NULL;
+			}
+		} else {
+			dprintk(DEBUG_ERROR, "no superblock\n");
+			return NULL;
+		}
+	}
+	dprintk(DEBUG_ERROR, "no inode\n");
+	return NULL;
+}
+
+/**
+ * v9fs_parse_tcp_devname - parse devname that was mounted
+ * @devname: passed from mount
+ * @addr: pointer to addr string
+ * @remotename: pointer to remotename string
+ *
+ * parse default net argument: ipaddr:path
+ *
+ */
+
+int v9fs_parse_tcp_devname(const char *devname, char **addr, char **remotename)
+{
+	char *dev = (char *)devname;
+
+	if (addr)
+		*addr = strsep(&dev, ":");
+	if (remotename) {
+		strsep(&dev, ":");
+		*remotename = strsep(&dev, ":");
+		if (*remotename == NULL) {
+			*remotename = v9fs_alloc(1, GFP_KERNEL);
+			if (*remotename)
+				**remotename = 0;
+		}
+	}
+
+	if (addr)
+		dprintk(DEBUG_TRANS, "addr: %s\n", *addr);
+	if (remotename)
+		dprintk(DEBUG_TRANS, "remotename %s\n", *remotename);
+
+	return 0;
+}
+
+/**
+ * v9fs_get_option - returns mount option
+ * @opts: option string
+ * @name: option to look for
+ * @val: string buffer to return the value
+ * @vlen: length of val buffer
+ *
+ */
+
+int v9fs_get_option(char *opts, char *name, char *val, int vlen)
+{
+	int nlen = strlen(name);
+	char *p = opts;
+	char *ep = opts ? opts + strlen(opts) : NULL;
+	char *s = NULL;
+
+	if (!opts)
+		return -1;
+
+	while (p < ep) {
+		s = strchr(p, ',');
+		if (s == NULL) 
+			s = ep;
+
+		if (strncmp(p, name, nlen) == 0) {
+			p += nlen;
+			if (*p == '=') {
+				p++;
+				if (vlen < (s - p))
+					return (s - p);
+
+				memcpy(val, p, s - p);
+				val[s - p] = 0;
+				return 0;
+			}
+		}
+		p = s + 1;
+	}
+	return -1;
+}
+
+/**
+ * v9fs_get_bin_option - returns a binary mount switch
+ * @opts: option string
+ * @name: option to look for
+ *
+ */
+
+int v9fs_get_bin_option(char *opts, char *name)
+{
+	int nlen = strlen(name);
+	char *p = opts;
+	char *ep = opts ? opts + strlen(opts) : NULL;
+	char *s = NULL;
+
+	if (!opts) 
+		return 0;
+
+	while (p < ep) {
+		s = strchr(p, ',');
+		if (s == NULL)
+			s = ep;
+	
+		if (strncmp(p, name, nlen) == 0)
+			return 1;
+		p = s + 1;
+	}
+	return 0;
+}
+
+/**
+ * v9fs_get_int_option - returns mount option
+ * @opts: option string
+ * @name: option to look for
+ * @dflt: default value
+ *
+ */
+
+long long v9fs_get_int_option(char *opts, char *name, long long dflt)
+{
+	char buf[24];
+	char *s = NULL;
+	int ret = dflt;
+	int n = v9fs_get_option(opts, name, buf, sizeof buf);
+
+	if (!n) 
+		ret = memparse(buf, &s);
+
+	return ret;
+}
+
+/**
+ * v9fs_session_setup_slabs - setup per-session slabs
+ * @v9ses: session information structure
+ * @dev_name: mount target
+ *
+ */
+
+inline int
+v9fs_session_setup_slabs(struct v9fs_session_info *v9ses, const char *dev_name)
+{
+	char tmpname[240];
+	char tmpdesc[255];
+	int n = sizeof(struct v9fs_fcall);
+
+	if (n < sizeof(struct v9fs_stat)) 
+		n = sizeof(struct v9fs_stat);
+
+	n += v9ses->maxdata;
+
+	strncpy(tmpname, dev_name, sizeof tmpname);
+
+	dprintk(DEBUG_SLABS, "Seting up session caches\n");
+
+	sprintf(tmpdesc, "v9fs_%s", tmpname);
+	if (v9fs_slab_create(&v9ses->slab, tmpdesc, n) < 0)
+		return -1;
+
+	v9ses->slab_size = n;
+	v9ses->rpcreq_slab = &v9fs_rpcreq_slab;
+
+	return 0;
+}
+
+inline void v9fs_session_destroy_slabs(struct v9fs_session_info *v9ses)
+{
+	v9fs_slab_destroy(&v9ses->slab);
+}
+
+/**
+ * v9fs_session_init - initialize session
+ * @v9ses: session information structure
+ * @dev_name: device being mounted
+ * @data: options
+ *
+ */
+
+int
+v9fs_session_init(struct v9fs_session_info *v9ses,
+		  const char *dev_name, char *data)
+{
+	struct v9fs_fcall *fcall = NULL;
+	char proto[10] = "tcp";
+	char name[32] = "root";
+	struct v9fs_transport *trans_proto;
+	int n = 0;
+	int newfid = -1;
+	int afid = -1;
+	int retval = -EINVAL;
+
+	/* set debug level */
+	v9fs_debug_level = v9fs_get_int_option(data, "debug", v9fs_debug_level);
+
+	/* id pools that are session-dependent: FIDs and TIDs */
+	v9fs_alloc_idpool(&v9ses->fidpool, V9FS_START_FIDS);
+	v9fs_alloc_idpool(&v9ses->tidpool, V9FS_START_TIDS);
+
+	v9fs_get_option(data, "name", name, sizeof name);
+	v9ses->name = (char *)v9fs_alloc(strlen(name) + 1, GFP_KERNEL);
+	if (v9ses->name) 
+		strcpy(v9ses->name, name);
+	else {
+		eprintk(KERN_WARNING,
+			"Couldn't allocate string for user name\n");
+		retval = -ENOMEM;
+		goto SessCleanUp;
+	}
+
+	/* Depending on protocol, establish a connection */
+
+	if (v9fs_get_option(data, "proto", proto, sizeof proto) > 0) {
+		eprintk(KERN_ERR, "Proto option value too long\n");
+		retval = -EINVAL;
+		goto SessCleanUp;
+	}
+
+	if (strcmp(proto, "tcp") == 0) {
+		trans_proto = &v9fs_trans_tcp;
+		v9fs_parse_tcp_devname(dev_name, 0, &v9ses->remotename);
+		if (!v9ses->remotename) {
+			eprintk(KERN_WARNING,
+				"Couldn't allocate string for remotename\n");
+			retval = -EINVAL;
+			goto SessCleanUp;
+		}
+	} else if (strcmp(proto, "unix") == 0) {
+		trans_proto = &v9fs_trans_unix;
+
+		v9ses->remotename = v9fs_alloc(1, GFP_KERNEL);
+		if (!v9ses->remotename) {
+			eprintk(KERN_WARNING,
+				"Couldn't allocate string for remotename\n");
+			retval = -EINVAL;
+			goto SessCleanUp;
+		}
+		*v9ses->remotename = 0;
+	} else {
+		printk(KERN_ERR "v9fs: Bad mount protocol %s\n", proto);
+		retval = -ENOPROTOOPT;
+		goto SessCleanUp;
+	}
+
+	v9ses->transport =
+	    v9fs_alloc(sizeof(struct v9fs_transport), GFP_KERNEL);
+	if (!v9ses->transport) {
+		eprintk(KERN_WARNING,
+			"Couldn't allocate string for transport struct\n");
+		retval = -ENOMEM;
+		goto SessCleanUp;
+	}
+
+	memcpy(v9ses->transport, trans_proto, sizeof(struct v9fs_transport));
+
+	if ((retval = v9ses->transport->init(v9ses->transport, dev_name, data)) < 0) {
+		eprintk(KERN_ERR, "problem initializing transport\n");
+		goto SessCleanUp;
+	}
+
+	v9ses->inprogress = 0;
+	v9ses->shutdown = 0;
+	v9ses->session_hung = 0;
+	v9ses->maxdata = v9fs_get_int_option(data, "maxdata", 8000);
+
+	v9fs_session_setup_slabs(v9ses, dev_name);
+
+	if ((retval = v9fs_mux_init(v9ses, dev_name)) < 0) {
+		dprintk(DEBUG_ERROR, "problem initializing mux\n");	
+		goto SessCleanUp;
+	}
+
+	v9ses->nodev = v9fs_get_bin_option(data, "nodevmap");
+
+	afid = v9fs_get_int_option(data, "afid", ~0);
+
+	if (afid == ~0) {
+		if (v9fs_get_bin_option(data, "noextend"))
+			retval = v9fs_t_version(v9ses, v9ses->maxdata, "9P2000",
+					   &fcall);
+		else
+			retval = v9fs_t_version(v9ses, v9ses->maxdata, "9P2000.u",
+					   &fcall);
+
+		if (retval < 0) {
+			dprintk(DEBUG_ERROR, "v9fs_t_version failed\n");
+			goto SessCleanUp;
+		}
+
+		/* Really should check for 9P1 and report error */
+		if (!strcmp(fcall->params.rversion.version, "9P2000.u")) {
+			dprintk(DEBUG_9P, "9P2000 UNIX extensions enabled\n");
+			v9ses->extended = 1;
+		} else {
+			dprintk(DEBUG_9P, "9P2000 legacy mode enabled\n");
+			v9ses->extended = 0;
+			v9ses->uid =
+			    v9fs_get_int_option(data, "uid", current->fsuid);
+			v9ses->gid =
+			    v9fs_get_int_option(data, "gid", current->fsgid);
+		}
+
+		n = fcall->params.rversion.msize;
+		v9fs_slab_free(&v9ses->slab, fcall);
+
+		if (n < v9ses->maxdata) {
+			v9ses->maxdata = n;
+			v9fs_session_destroy_slabs(v9ses);
+			v9fs_session_setup_slabs(v9ses, dev_name);
+		}
+	}
+
+	newfid = v9fs_get_idpool(&v9ses->fidpool);
+	if (newfid < 0) {
+		eprintk(KERN_WARNING, "couldn't allocate FID\n");
+		retval = -ENOMEM;
+		goto SessCleanUp;
+	}
+	/* it is a little bit ugly, but we have to prevent newfid */
+	/* being the same as afid, so if it is, get a new fid     */
+	if (afid != ~0 && newfid == afid) {
+		newfid = v9fs_get_idpool(&v9ses->fidpool);
+		if (newfid < 0) {
+			eprintk(KERN_WARNING, "couldn't allocate FID\n");
+			retval = -ENOMEM;
+			goto SessCleanUp;
+		}
+	}
+
+	if ((retval = v9fs_t_attach(v9ses, v9ses->name, v9ses->remotename,
newfid, afid, NULL))
+	    < 0) {
+		dprintk(DEBUG_ERROR, "cannot attach\n");
+		goto SessCleanUp;
+	}
+
+	if (afid != ~0) {
+		if (v9fs_t_clunk(v9ses, afid, NULL)) 
+			dprintk(DEBUG_ERROR, "clunk failed\n");
+	}
+
+	return newfid;
+
+      SessCleanUp:
+	v9fs_session_close(v9ses);
+	return retval;
+}
+
+/**
+ * v9fs_session_close - shutdown a session
+ * @v9ses: session information structure
+ *
+ */
+
+void v9fs_session_close(struct v9fs_session_info *v9ses)
+{
+	if (v9ses->recvproc) {
+		send_sig(SIGKILL, v9ses->recvproc, 1);
+		wait_for_completion(&v9ses->proccmpl);
+	}
+
+	if (v9ses->transport) {
+		v9ses->transport->close(v9ses->transport);
+		v9fs_free(v9ses->transport);
+	}
+
+	v9fs_session_destroy_slabs(v9ses);
+
+	if (v9ses->name) {
+		v9fs_free(v9ses->name);
+	}
+}
+
+/**
+ * v9fs_init - Initialize module
+ *
+ */
+
+static int __init init_v9fs(void)
+{
+	int error;
+
+	v9fs_alloc_init();
+	printk(KERN_INFO "Installing v9fs 9P2000 file system support\n");
+
+	if ((error = register_filesystem(&v9fs_fs_type))) {
+		printk(KERN_ERR "Could not register v9fs\n");
+		return error;
+	}
+
+	if (v9fs_slab_create
+	    (&v9fs_rpcreq_slab, "v9fs_rpcreq_info",
+	     sizeof(struct v9fs_rpcreq)) < 0) {
+		eprintk(KERN_WARNING, "Couldn't allocate v9fs_rpcreq cache\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+/**
+ * v9fs_init - shutdown module
+ *
+ */
+
+static void __exit exit_v9fs(void)
+{
+	v9fs_slab_destroy(&v9fs_rpcreq_slab);
+	unregister_filesystem(&v9fs_fs_type);
+	v9fs_alloc_exit();
+}
+
+module_init(init_v9fs)
+module_exit(exit_v9fs)
+
+MODULE_AUTHOR("Eric Van Hensbergen <ericvh at gmail.com>");
+MODULE_AUTHOR("Ron Minnich <rminnich at lanl.gov>");
+MODULE_LICENSE("GPL");

diff -Nru a/fs/9p/v9fs.h b/fs/9p/v9fs.h
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/fs/9p/v9fs.h	2005-04-25 07:38:03 -05:00
@@ -0,0 +1,79 @@
+/*
+ * V9FS definitions.
+ *
+ *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh at gmail.com>
+ *  Copyright (C) 2002 by Ron Minnich <rminnich at lanl.gov>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/*
+  * Session structure provides information for an opened session
+  *
+  */
+
+struct v9fs_session_info {
+	unsigned int maxdata;
+	unsigned char extended;	/* set to 1 if we are using UNIX extensions */
+	unsigned char nodev;	/* set to 1 if no disable device mapping */
+
+	char *name;		/* user name to mount as */
+	char *remotename;	/* name of remote hierarchy being mounted */
+	unsigned int uid;	/* default uid/muid for legacy support */
+	unsigned int gid;	/* default gid for legacy support */
+
+	struct idpool fidpool;	/* The FID pool for file descriptors */
+	struct idpool tidpool;	/* The TID pool for transactions ids */
+
+	int slab_size;
+	struct v9fs_slab slab;
+	struct v9fs_slab *rpcreq_slab;
+
+	/* transport information */
+	struct v9fs_transport *transport;
+
+	int inprogress;		/* session in progress => true */
+	int shutdown;		/* session shutting down. no more attaches. */
+	unsigned long session_hung;
+
+	/* mux private data */
+	struct v9fs_fcall *curfcall;
+	wait_queue_head_t read_wait;
+	struct completion fcread;
+	struct completion proccmpl;
+	struct task_struct *recvproc;
+
+	spinlock_t muxlock;
+	struct list_head mux_fcalls;
+};
+
+int v9fs_session_init(struct v9fs_session_info *, const char *, char *);
+struct v9fs_session_info *v9fs_inode2v9ses(struct inode *);
+void v9fs_session_close(struct v9fs_session_info *v9ses);
+
+int v9fs_get_option(char *opts, char *name, char *buf, int buflen);
+long long v9fs_get_int_option(char *opts, char *name, long long dflt);
+int v9fs_parse_tcp_devname(const char *devname, char **addr, char
**remotename);
+
+/* this is a very conservative number so we can use the slab allocator */
+#define MAX_MISTAT_DATA 1024
+#define V9FS_WIRE_MAGIC 0x01021997
+
+/* other default globals */
+
+/* inital pool sizes for fids and tags */
+#define V9FS_START_FIDS 8192
+#define V9FS_START_TIDS 256
diff -Nru a/fs/9p/v9fs_vfs.h b/fs/9p/v9fs_vfs.h
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/fs/9p/v9fs_vfs.h	2005-04-25 07:38:03 -05:00
@@ -0,0 +1,58 @@
+/*
+ * V9FS VFS extensions.
+ *
+ *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh at gmail.com>
+ *  Copyright (C) 2002 by Ron Minnich <rminnich at lanl.gov>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+/* plan9 semantics are that created files are implicitly opened.
+ * But linux semantics are that you call create, then open.
+ * the plan9 approach is superior as it provides an atomic 
+ * open. 
+ * we track the create fid here. When the file is opened, if fidopen is
+ * non-zero, we use the fid and can skip some steps. 
+ * there may be a better way to do this, but I don't know it. 
+ * one BAD way is to clunk the fid on create, then open it again:
+ * you lose the atomicity of file open
+ */
+
+/* special case: 
+ * unlink calls remove, which is an implicit clunk. So we have to track
+ * that kind of thing so that we don't try to clunk a dead fid. 
+ */
+
+extern struct super_operations v9fs_super_ops;
+extern struct file_system_type v9fs_fs_type;
+extern struct file_operations v9fs_file_operations;
+extern struct file_operations v9fs_dir_operations;
+extern struct address_space_operations v9fs_addr_operations;
+
+struct inode *v9fs_get_inode(struct super_block *sb, int mode);
+ino_t v9fs_qid2ino(struct v9fs_qid *qid);
+void v9fs_mistat2inode(struct v9fs_stat *, struct inode *, struct
super_block *);
+int v9fs_dir_release(struct inode *inode, struct file *filp);
+int v9fs_file_open(struct inode *inode, struct file *file);
+ssize_t v9fs_file_read(struct file *filp, char __user * data, size_t count,
+		       loff_t * offset);
+ssize_t v9fs_write(struct file *filp, const char __user * data,
+		   size_t count, loff_t * offset);
+void v9fs_inode2mistat(struct inode *inode, struct v9fs_stat *mistat);
+void v9fs_mistat2unix(struct v9fs_stat *, struct stat *, struct super_block *);
+void v9fs_dentry_release(struct dentry *);
+struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
+			       struct nameidata *nameidata);

diff -Nru a/fs/9p/vfs_addr.c b/fs/9p/vfs_addr.c
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/fs/9p/vfs_addr.c	2005-04-25 07:38:03 -05:00
@@ -0,0 +1,215 @@
+/*
+ *  linux/fs/9p/vfs_addr.c
+ *
+ * This file contians vfs address (mmap) ops for 9P2000. 
+ *
+ *  Copyright (C) 2005 by Eric Van Hensbergen <ericvh at gmail.com>
+ *  Copyright (C) 2002 by Ron Minnich <rminnich at lanl.gov>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/stat.h>
+#include <linux/string.h>
+#include <linux/smp_lock.h>
+#include <linux/inet.h>
+#include <linux/version.h>
+#include <linux/pagemap.h>
+#include "debug.h"
+#include "alloc.h"
+#include "idpool.h"
+#include "v9fs.h"
+#include "9p.h"
+#include "v9fs_vfs.h"
+#include "fid.h"
+
+/**
+ * v9fs_vfs_readpage - read an entire page in from 9P
+ * @file: file being read 
+ * @page: structure to page
+ *
+ */
+
+static int v9fs_vfs_readpage(struct file *file, struct page *page)
+{
+	char *buffer = NULL;
+	int retval = -EIO;
+	loff_t offset = ((loff_t) page->index) << PAGE_CACHE_SHIFT;
+	int count = PAGE_CACHE_SIZE;
+
+	dprintk(DEBUG_VFS, "file: %s  page: %p\n", file->f_dentry->d_iname,
+		page);
+
+	page_cache_get(page);
+
+	/* get buffer */
+	buffer = kmap(page);
+
+	/* v9fs_t_read from fid into buffer */
+	retval = v9fs_file_read(file, buffer, count, &offset);
+
+	if (retval < 0) {
+		printk(KERN_ERR "file_read failed in read_page\n");
+		dprintk(DEBUG_ERROR, "error reading file\n");
+		goto CleanUpPage;
+	}
+
+	if (retval < count) {
+		count = count - retval;
+		memset(buffer + retval, 0, count);
+	}
+
+	flush_dcache_page(page);
+	SetPageUptodate(page);
+	if (PageError(page))
+		ClearPageError(page);
+	retval = 0;
+
+      CleanUpPage:
+	kunmap(page);
+	unlock_page(page);
+	page_cache_release(page);
+	return retval;
+}
+
+/**
+ * v9fs_find_file - find a file pointer based on page
+ * @page: page to lookup file based on
+ *
+ */
+static struct file *v9fs_find_file(struct page *page)
+{
+	struct address_space *mapping = page->mapping;
+	struct inode *inode = NULL;
+	struct dentry *dentry = NULL;
+	struct v9fs_fid *fid = NULL;
+	struct list_head *p, *temp;
+
+	dprintk(DEBUG_VFS, " page: %p\n", page);
+	if (!mapping) {
+		dprintk(DEBUG_ERROR, "No mapping\n");
+		return NULL;
+	}
+
+	inode = mapping->host;
+	if (!inode) {
+		dprintk(DEBUG_ERROR, "No inode\n");
+		return NULL;
+	}
+
+	list_for_each_safe(p, temp, &inode->i_dentry) {
+		dentry = list_entry(p, struct dentry, d_alias);
+		fid = v9fs_fid_lookup(dentry, FID_OP);
+		if (fid) 
+			return fid->filp;
+	}
+
+	return NULL;
+}
+
+/**
+ * v9fs_vfs_writepage - write a mmaped page to server
+ * @page: page to write
+ * @wbc: writeback control?
+ *
+ */
+
+static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
+{
+	char *buffer = NULL;
+	struct address_space *mapping = page->mapping;
+	int retval = -EIO;
+	loff_t offset = 0;
+	loff_t pageoffset = 0;
+	unsigned long end_index;
+	int count = PAGE_CACHE_SIZE;
+	struct file *filp = v9fs_find_file(page);
+	struct inode *inode = mapping->host;
+
+	dprintk(DEBUG_VFS, "page: %p\n", page);
+
+	if ((!inode) || (!filp))
+		goto UnlockPage;
+
+	end_index = inode->i_size >> PAGE_CACHE_SHIFT;
+
+	/* complicated case at end of file */
+	if (page->index >= end_index) {
+		/* things got complicated... */
+		count = inode->i_size & (PAGE_CACHE_SIZE - 1); 
+		if (page->index >= end_index + 1 || !count)
+			return 0;	/* truncated - don't care */
+	}
+
+	/* get buffer */
+	buffer = kmap(page) + pageoffset;
+	offset = ((loff_t) page->index << PAGE_CACHE_SHIFT) + pageoffset;
+
+	page_cache_get(page);
+	retval = v9fs_write(filp, buffer, count, &offset);
+
+	if (retval < 0) {
+		dprintk(DEBUG_ERROR, "error: %d\n", retval);
+		ClearPageUptodate(page);
+		goto UnmapPage;
+	}
+
+	if (retval < count) {
+		dprintk(DEBUG_ERROR, "Short write\n");
+	}
+
+	if (offset > inode->i_size) {
+		inode->i_size = offset;
+	}
+
+	if (PageError(page))
+		ClearPageError(page);
+	SetPageUptodate(page);
+
+	retval = 0;
+
+      UnmapPage:
+	kunmap(page);
+      UnlockPage:
+	unlock_page(page);
+	page_cache_release(page);
+
+	return retval;
+}
+
+/**
+ * v9fs_vfs_dirtypage - mark a page as dirty
+ * @page: page to prepare write on
+ *
+ */
+
+static int v9fs_vfs_dirtypage(struct page *page)
+{
+	dprintk(DEBUG_VFS, "page: %p\n", page);
+	lock_page(page);
+	return v9fs_vfs_writepage(page, NULL);
+}
+
+struct address_space_operations v9fs_addr_operations = {
+      readpage:v9fs_vfs_readpage,
+      writepage:v9fs_vfs_writepage,
+      set_page_dirty:v9fs_vfs_dirtypage,
+};

diff -Nru a/fs/9p/vfs_super.c b/fs/9p/vfs_super.c
--- /dev/null	Wed Dec 31 16:00:00 196900
+++ b/fs/9p/vfs_super.c	2005-04-25 07:38:03 -05:00
@@ -0,0 +1,273 @@
+/*
+ *  linux/fs/9p/vfs_super.c
+ *
+ * This file contians superblock ops for 9P2000. It is intended that 
+ * you mount this file system on directories.
+ *
+ *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh at gmail.com>
+ *  Copyright (C) 2002 by Ron Minnich <rminnich at lanl.gov>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/stat.h>
+#include <linux/string.h>
+#include <linux/smp_lock.h>
+#include <linux/inet.h>
+
+#include "debug.h"
+#include "alloc.h"
+#include "idpool.h"
+#include "v9fs.h"
+#include "9p.h"
+#include "v9fs_vfs.h"
+#include "conv.h"
+#include "fid.h"
+
+/**
+ * v9fs_clear_inode - release an inode
+ * @inode: inode to release
+ *
+ */
+
+static void v9fs_clear_inode(struct inode *inode)
+{
+	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
+
+	if (v9ses == NULL) {
+		dprintk(DEBUG_ERROR, "No session for inode being dirtied\n");
+		return;
+	}
+
+	filemap_fdatawrite(inode->i_mapping);
+}
+
+/**
+ * v9fs_delete_inode - delete an inode
+ * @inode: inode to delete
+ *
+ */
+
+static void v9fs_delete_inode(struct inode *i)
+{
+	clear_inode(i);
+}
+
+/**
+ * v9fs_put_super - release super block
+ * @sb: super block to release
+ *
+ */
+
+static void v9fs_put_super(struct super_block *sb)
+{
+	struct v9fs_session_info *v9ses = sb ? sb->s_fs_info : 0;
+
+	dprintk(DEBUG_VFS, " %p\n", sb);
+
+	if (v9ses != 0) {
+		v9fs_free_idpool(&v9ses->fidpool);
+		v9fs_free_idpool(&v9ses->tidpool);
+	}
+}
+
+/**
+ * v9fs_set_super - set the superblock
+ * @s: super block
+ * @data: file system specific data
+ * 
+ */
+
+static int v9fs_set_super(struct super_block *s, void *data)
+{
+	if (s == NULL) {
+		dprintk(DEBUG_ERROR, "null superblock\n");
+		return -1;
+	}
+
+	s->s_fs_info = data;
+	return set_anon_super(s, data);
+}
+
+/**
+ * v9fs_block_bits - Determine bits in blocksize (from NFS Code)
+ * @bsize: blocksize
+ * @nrbitsp: number of bits
+ * 
+ * this bit from linux/fs/nfs/inode.c
+ * Copyright (C) 1992  Rick Sladkey
+ * XXX - shouldn't there be a global linux function for this?
+ * 
+ */
+
+static inline unsigned long
+v9fs_block_bits(unsigned long bsize, unsigned char *nrbitsp)
+{
+	/* make sure blocksize is a power of two */
+	if ((bsize & (bsize - 1)) || nrbitsp) {
+		unsigned char nrbits;
+
+		for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--) ;
+		bsize = 1 << nrbits;
+		if (nrbitsp)
+			*nrbitsp = nrbits;
+	}
+
+	return bsize;
+}
+
+/**
+ * v9fs_get_sb - mount a superblock
+ * @fs_type: file system type
+ * @flags: mount flags
+ * @dev_name: device name that was mounted
+ * @data: mount options
+ * 
+ */
+
+static struct super_block *v9fs_get_sb(struct file_system_type
+				       *fs_type, int flags,
+				       const char *dev_name, void *data)
+{
+	struct super_block *sb = 0;
+	struct v9fs_fcall *fcall = 0;
+	struct inode *inode = 0;
+	struct dentry *root = 0;
+	struct v9fs_session_info *v9ses = 0;
+	struct v9fs_fid *root_fid = 0;
+	int mode = S_IRWXUGO | S_ISVTX;
+	uid_t uid = current->fsuid;
+	gid_t gid = current->fsgid;
+	int result = 0;
+	int stat_result = 0;
+	int newfid = 0;
+
+	dprintk(DEBUG_VFS, " \n");
+
+	v9ses = v9fs_alloc(sizeof(struct v9fs_session_info), GFP_KERNEL);
+	if (!v9ses)
+		return ERR_PTR(-ENOMEM);
+	memset(v9ses, 0, sizeof(struct v9fs_session_info));
+
+	if ((newfid = v9fs_session_init(v9ses, dev_name, data)) < 0) {
+		dprintk(DEBUG_ERROR, "problem initiating session\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	sb = sget(fs_type, 0, v9fs_set_super, v9ses);
+
+/* FIXME: 1TB For Now */
+	sb->s_maxbytes = (unsigned long long)1024 *1024 * 1024 * 1024;
+	sb->s_blocksize =
+	    v9fs_block_bits(v9ses->maxdata, &sb->s_blocksize_bits);
+	sb->s_magic = V9FS_WIRE_MAGIC;
+	sb->s_op = &v9fs_super_ops;
+
+	sb->s_flags |=
+	    MS_ACTIVE | MS_SYNCHRONOUS | MS_DIRSYNC | MS_NODIRATIME |
+	    MS_NOATIME;
+
+	inode = v9fs_get_inode(sb, S_IFDIR | mode);
+	if ((!inode) || IS_ERR(inode))
+		return ERR_PTR(-EINVAL);
+
+	inode->i_uid = uid;
+	inode->i_gid = gid;
+
+	root = d_alloc_root(inode);
+
+	if (!root) {
+		iput(inode);
+		return ERR_PTR(-ENOMEM);
+	}
+	sb->s_root = root;
+
+	/* Setup the Root Inode */
+	root_fid = v9fs_fid_create(root);
+	if (root_fid == NULL)
+		return ERR_PTR(-ENOMEM);
+
+	root_fid->fidopen = 0;
+	root_fid->v9ses = v9ses;
+
+	if (newfid >= 0) {
+		stat_result = v9fs_t_stat(v9ses, newfid, &fcall);
+		if (stat_result < 0) {
+			dprintk(DEBUG_ERROR, "stat error\n");
+			v9fs_t_clunk(v9ses, newfid, NULL);
+			v9fs_put_idpool(newfid, &v9ses->fidpool);
+		} else {
+			root_fid->fid = newfid;
+			root_fid->qid = fcall->params.rstat.stat->qid;
+			root->d_inode->i_ino =
+			    v9fs_qid2ino(&fcall->params.rstat.stat->qid);
+			v9fs_mistat2inode(fcall->params.rstat.stat, root->d_inode,
+				     sb);
+		}
+		v9fs_slab_free(&v9ses->slab, fcall);
+	}
+
+	if ((stat_result < 0) || (newfid < 0)) {
+		up_write(&sb->s_umount);
+		deactivate_super(sb);
+		return ERR_PTR(result);
+	}
+
+	return sb;
+}
+
+/**
+ * v9fs_kill_super - Kill Superblock
+ * @s: superblock
+ * 
+ */
+
+static void v9fs_kill_super(struct super_block *s)
+{
+	struct v9fs_session_info *v9ses =
+	    (struct v9fs_session_info *)s->s_fs_info;
+
+	dprintk(DEBUG_VFS, " %p\n", s);
+
+	v9fs_dentry_release(s->s_root);	/* clunk root */
+
+	kill_anon_super(s);
+
+	v9fs_session_close(v9ses);
+	v9fs_free(v9ses);
+	dprintk(DEBUG_VFS, "exiting kill_super\n");
+}
+
+struct super_operations v9fs_super_ops = {
+      clear_inode:v9fs_clear_inode,
+      delete_inode:v9fs_delete_inode,
+      statfs:simple_statfs,
+      put_super:v9fs_put_super,
+};
+
+struct file_system_type v9fs_fs_type = {
+	.owner = THIS_MODULE,
+	.name = "9P",
+	.get_sb = v9fs_get_sb,
+	.kill_sb = v9fs_kill_super,
+
+	.fs_flags = 0
+};



More information about the Kernel-mentors mailing list