Commits

Anonymous committed 32fdf70

Update for OpenBSD 5.0, start the rework

Comments (0)

Files changed (8)

mount/mount_o9fs.c

 
 	s = socket(PF_UNIX, SOCK_STREAM, 0);
 	if (s < 0)
-		err(1, "socket");
+		err(1, "Failed to create UNIX socket");
 
 	bzero(&channel, sizeof(channel));
 	channel.sun_family = PF_UNIX;
 	channel.sun_len = strlen(path);
-	strlcpy(channel.sun_path, path, 104); /* XXX openbsd specific? */
+	strlcpy(channel.sun_path, path, sizeof(channel.sun_path));
 
 	if ((connect(s, (struct sockaddr *) &channel, sizeof(channel))) < 0)
-		err(1, "connect");
-
+		err(1, "Failed to connect");
 	return s;
 }
 
 	struct sockaddr_in con;
 
 	hp = gethostbyname(host);
-	if (!hp)
-		err(1, "gethostbyname");
+	if (hp == NULL)
+		err(1, "Failed to resolve name %s", host);
 
 	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 	if (s < 0)
-		err(1, "socket");
+		err(1, "Failed to create INET socket");
 
 	bzero(&con, sizeof(con));
 	con.sin_family = AF_INET;
-	memcpy(&con.sin_addr.s_addr, hp->h_addr, hp->h_length);
+	memmove(&con.sin_addr.s_addr, hp->h_addr, hp->h_length);
 	con.sin_port = htons(port);
+
 	if((connect(s, (struct sockaddr *) &con, sizeof(struct sockaddr_in))) < 0)
-		err(1, "connect");
+		err(1, "Failed to connect");
 	return s;
 }
 
-struct addr9p {
-	char type[4];	   /* unix or net */
-	char addr[256];	 /* address or path */
-	int port;				/* port if net */
-};
+/*
+ * Parse either unix or network address argument and connect accordingly.
+ * Return the connected file descriptor.
+ */
+int
+dial(char *arg)
+{
+	char *p, addr[MAXPATHLEN];
+	int i, port;
 
-struct addr9p *
-parseaddr(char *arg)
-{
-	struct addr9p *a;
-	char *p, *s;
-	int i;
+	if (arg == NULL)
+		return -1;
 
-	s = strdup(arg);
-	printf("duped: %s\n", s);
-	p = s;
-	if (!p)
-		return NULL;
-	a = malloc(sizeof(struct addr9p));
-	if (!a)
-		err(1, "parseaddr: malloc");
+	p = arg;
+	i = 0;
+	while (*p != '\0' && *p != '!')
+		addr[i++] = *p++;
+	addr[i] = '\0';
 
-	/* get type */
-	for (i = 0; *p && *p != '!'; i++)
-		a->type[i] = *p++;
-	a->type[i] = 0;
-	p++; /* skip ! */
+	if (*p == '!') {
+		p++;
+		port = atoi(p);
+		return conninet(addr, port);
+	}
 
-	/* get address */
-	for (i = 0; *p && *p != '!'; i++)
-		a->addr[i] = *p++;
-	a->addr[i] = 0;
-	p++; /* skip ! */
-
-	p[5] = 0; /* port are 5 chars max */
-		a->port = -1;
-	if (!strcmp(a->type, "net"))
-		a->port = atoi(p);
-	free(s);
-	return a;
-}
-
-	
-int
-dial(char *addr)
-{
-	struct addr9p *a;
-	
-	a = parseaddr(addr);
-	if (a->port != -1)
-		return conninet(a->addr, a->port);
-	return connunix(a->addr);
+	return connunix(addr);
 }
 	
 	
 
 	args.hostname = argv[0];
 	args.fd = dial(argv[0]);
-
-	printf("args.hostname = %s\n", args.hostname);
-	printf("args.fd = %d\n", args.fd);
+	if (args.fd < 0)
+		err(1, "Failed to dial");
 
 	if (realpath(argv[1], node) == NULL)
 		err(1, "realpath %s", argv[1]);
 	u_char			*stat;				/* Twstat, Rstat */
 };
 
+enum {
+	Offtype	= 4,
+	Offtag	= 5,
+
+	Minhd	= Offtag + 2,		/* Minimum 9P header size, independent of message type */
+	Maxhd	= 23,						/* Maximum 9P header size */
+};
+
 struct o9fs {
-	/* mount info */
-	struct	mount *mp;			/* generic mount info */
-	struct	vnode *vroot;		/* local root of the tree */
-	struct	file *servfp;
-	char		version[7];				/* version we are speaking */
-	long		msize;					/* max size of our payload */
+	struct	mount *mp;
+	struct	vnode *vroot;		/* Local root of the tree */
+	struct	file *servfp;		/* File pointing to the server */
+	long	msize;				/* Maximum size of our payload */
 	
 	struct o9fsfcall	request;	/* request we are doing */
 	struct o9fsfcall	reply;		/* reply we received */
 	u_char	*rpc;
-	long	rpclen;
 
-/*	int	ref;
-	int	fd; */
+	/* 
+     * Buffers for I/O
+	 * Both buffers have the client requested msize,
+	 * but only the number of bytes in this structure's msize
+	 * should be used.
+	 */
+	u_char	*inbuf;
+	u_char	*outbuf;
+
 	int	nextfid;
 	struct	o9fsfid *rootfid;
 	struct	o9fsfid *freefid;
 #define O9FS_GBIT64(p)	((uint32_t)((p)[0]|((p)[1]<<8)|((p)[2]<<16)|((p)[3]<<24)) |\
 						((uint64_t)((p)[4]|((p)[5]<<8)|((p)[6]<<16)|((p)[7]<<24)) << 32))
  
-#define O9FS_PBIT8(p,v)		(p)[0]=(v)
-#define O9FS_PBIT16(p,v)	(p)[0]=(v);(p)[1]=(v)>>8   
-#define O9FS_PBIT32(p,v)	(p)[0]=(v);(p)[1]=(v)>>8;(p)[2]=(v)>>16;(p)[3]=(v)>>24
-#define O9FS_PBIT64(p,v)	(p)[0]=(v);(p)[1]=(v)>>8;(p)[2]=(v)>>16;(p)[3]=(v)>>24;\
-							(p)[4]=(v)>>32;(p)[5]=(v)>>40;(p)[6]=(v)>>48;(p)[7]=(v)>>56
+#define O9FS_PBIT8(p,v)		((qaddr_t)(p))[0]=(v)
+#define O9FS_PBIT16(p,v)	((qaddr_t)(p))[0]=(v);((qaddr_t)(p))[1]=(v)>>8   
+#define O9FS_PBIT32(p,v)	((qaddr_t)(p))[0]=(v);((qaddr_t)(p))[1]=(v)>>8;((qaddr_t)(p))[2]=(v)>>16;((qaddr_t)(p))[3]=(v)>>24
+#define O9FS_PBIT64(p,v)	((qaddr_t)(p))[0]=(v);((qaddr_t)(p))[1]=(v)>>8;((qaddr_t)(p))[2]=(v)>>16;((qaddr_t)(p))[3]=(v)>>24;\
+							((qaddr_t)(p))[4]=(v)>>32;((qaddr_t)(p))[5]=(v)>>40;((qaddr_t)(p))[6]=(v)>>48;((qaddr_t)(p))[7]=(v)>>56
 
 #define O9FS_BIT8SZ		1
 #define O9FS_BIT16SZ	2
 long	o9fs_rdwr(struct o9fs *, int, struct o9fsfid *, void *, u_long, int64_t);
 void	o9fs_fidclunk(struct o9fs *, struct o9fsfid *);
 struct	o9fsfid *o9fs_twalk(struct o9fs *, struct o9fsfid *, struct o9fsfid *, char *);
-int		o9fs_opencreate(int, struct o9fs *, struct o9fsfid *, int, ulong);
+int		o9fs_opencreate(int, struct o9fs *, struct o9fsfid *, int, ulong, char *);
 
 /* o9fs_conv* */
 u_char	*pstring(u_char *, char *);
 
 /* o9fs_rpc.c */
 int		o9fs_rpc(struct o9fs *, struct o9fsfcall *, struct o9fsfcall *);
+long	o9fs_mio(struct o9fs *, u_long);
 
 extern int (**o9fs_vnodeop_p)(void *);
 int
 o9fs_lkmentry(struct lkm_table *table, int cmd, int ver)
 {
-	vfs_opv_init_explicit(&o9fs_vnodeop_opv_desc);
-	vfs_opv_init_default(&o9fs_vnodeop_opv_desc);
 	DISPATCH(table, cmd, ver, lkm_nofunc, lkm_nofunc, lkm_nofunc);
 }
 	return cnt;
 }
 
+long
+o9fs_mio(struct o9fs *fs, u_long len)
+{
+	long n;
+
+	n = rdwr(fs, fs->outbuf, len, &fs->servfp->f_offset, 1);
+	if (n <= 0)
+		return n;
+
+	n = rdwr(fs, fs->inbuf, 4, &fs->servfp->f_offset, 0);
+	if (n <= 0) {
+		printf("o9fs_mio: Error reading message size\n");
+		return n;
+	}
+	
+	len = O9FS_GBIT32(fs->inbuf);
+	if (len <= 4) {
+		printf("R-message with length < 4\n");
+		return -1;
+	}
+
+	n = rdwr(fs, fs->inbuf + Offtype, len - Offtype, &fs->servfp->f_offset, 0);
+	if (n <= 0)
+		return n;
+
+	if (O9FS_GBIT8(fs->inbuf + Offtype) == O9FS_RERROR) {
+		printf("%.*s\n", O9FS_GBIT16(fs->inbuf + Minhd), fs->inbuf + Minhd + 4);
+		return -1;
+	}
+
+	return len;
+}
+
 int
 o9fs_rpc(struct o9fs *fs, struct o9fsfcall *tx, struct o9fsfcall *rx)
 {
         fs->freefid = f->next;
         f->offset = 0;
         f->mode = -1;
-		f->ref = 0;
+	//	f->ref = 0;
         f->qid.path = 0;
         f->qid.vers = 0;
         f->qid.type = 0;
 #include <sys/mbuf.h>
 #include <sys/namei.h>
 #include <sys/vnode.h>
-#include <sys/malloc.h>
+#include <sys/malloc.h> 
 #include <sys/filedesc.h>
 #include <sys/file.h>
 
 struct o9fsfid *o9fs_attach(struct o9fs *, struct o9fsfid *, char *, char *);
 
 static long
-o9fs_version(struct o9fs *fs, char *version, int msize)
+o9fs_version(struct o9fs *fs, uint32_t msize)
 {
-	struct o9fsfcall f, rx;
-	
-	f.type = O9FS_TVERSION;
-	f.tag = O9FS_NOTAG;
-	f.msize = msize;
-	f.version = version;
+	long n;
+	u_char *p;
 
-	if ((o9fs_rpc(fs, &f, &rx)) == -1)
-		return 0;
+	if (fs == NULL)
+		return -1;
 
-	return fs->msize = rx.msize;
-}
+	p = fs->outbuf;
+
+	O9FS_PBIT32(p, 19);
+	O9FS_PBIT8(p + Offtype, O9FS_TVERSION);
+	O9FS_PBIT16(p + Offtag, O9FS_NOTAG);
+	O9FS_PBIT32(p + Minhd, msize);
+	O9FS_PBIT16(p + Minhd + 4, 6);
+	memmove(p + Minhd + 4 + 2, "9P2000", 6);
+
+	n = o9fs_mio(fs, 19);
+	if (n <= 0)
+		return -1;
+	return fs->msize = O9FS_GBIT16(fs->inbuf + Minhd + 4);
+}	
 
 static struct o9fsfid *
 o9fs_auth(struct o9fs *fs, char *user, char *aname)
 	fid->qid = rx.qid;
 	return fid;
 }
+
+int
+mounto9fs(struct mount *mp, struct file *fp)
+{
+	struct o9fs *fs;
+	struct vnode *rvp;
+	struct o9fsfid *fid;
+	int error;
+
+	error = 0;
+	fs = (struct o9fs *) malloc(sizeof(struct o9fs), M_MISCFSMNT, M_WAITOK | M_ZERO);
+	error = getnewvnode(VT_O9FS, mp, o9fs_vnodeop_p, &rvp);
+	if (error)
+		return error;
+
+	rvp->v_type = VDIR;
+	rvp->v_flag = VROOT;
+
+	fs->vroot = rvp;
+	fs->mp = mp;
+	fs->servfp = fp;
+
+	mp->mnt_data = (qaddr_t) fs;
+	vfs_getnewfsid(mp);	
+
+	fs->inbuf = malloc(8192+Maxhd, M_O9FS, M_WAITOK | M_ZERO);
+	fs->outbuf = malloc(8192+Maxhd, M_O9FS, M_WAITOK | M_ZERO);
+
+	if(o9fs_version(fs, 8192) < 0)
+		return EIO;
+	return EIO;
+
+	fid = o9fs_attach(fs, o9fs_auth(fs, "none", ""), "iru", "");
+	if (fid == NULL)
+		return EIO;
+
+	fs->rootfid = fid;
+	fs->vroot->v_data = fid;
+	error = o9fs_allocvp(fs->mp, fid, &fs->vroot, VROOT);
+	return error;
+}
 	
+
 int
 o9fs_mount(struct mount *mp, const char *path, void *data, 
 		struct nameidata *ndp, struct proc *p)
 	return 0;
 }
 
-static int
-mounto9fs(struct mount *mp, struct file *fp)
-{
-	struct o9fs *fs;
-	struct vnode *rvp;
-	struct o9fsfid *fid;
-	int error;
-
-	error = 0;
-	fs = (struct o9fs *) malloc(sizeof(struct o9fs), M_MISCFSMNT, M_WAITOK);
-
-	error = getnewvnode(VT_O9FS, mp, o9fs_vnodeop_p, &rvp);
-	if (error)
-		return (error);
-
-	rvp->v_type = VDIR;
-	rvp->v_flag = VROOT;
-	fs->vroot = rvp;
-	fs->mp = mp;
-	fs->servfp = fp;
-	fs->nextfid = 0;
-	fs->freefid = NULL;
-
-	mp->mnt_data = (qaddr_t) fs;
-	vfs_getnewfsid(mp);	
-
-	fs->rpc = malloc(8192+O9FS_IOHDRSZ, M_O9FS, M_WAITOK);
-	if (!o9fs_version(fs, O9FS_VERSION9P, 8192))
-		return EIO;
-
-	fid = o9fs_attach(fs, o9fs_auth(fs, "none", ""), "iru", "");
-	if (fid == NULL)
-		return EIO;
-
-	fs->rootfid = fid;
-	fs->vroot->v_data = fid;
-	error = o9fs_allocvp(fs->mp, fid, &fs->vroot, VROOT);
-	return error;
-}
-	
 int
 o9fs_root(struct mount *mp, struct vnode **vpp)
 {
 int o9fs_close(void *);
 int o9fs_lookup(void *);
 int o9fs_create(void *);
-#define	o9fs_mknod	eopnotsupp
 int o9fs_access(void *);
 int o9fs_getattr(void *);
 int o9fs_setattr(void *);
 int o9fs_read(void *);
 int o9fs_write(void *);
 int o9fs_mkdir(void *);
-#define	o9fs_ioctl	(int (*)(void *))enoioctl
-#define	o9fs_fsync	nullop
-#define	o9fs_remove	eopnotsupp
-#define	o9fs_link	eopnotsupp
-#define	o9fs_rename	eopnotsupp
-#define	o9fs_rmdir	eopnotsupp
-#define	o9fs_symlink	eopnotsupp
 int o9fs_readdir(void *);
-#define o9fs_revoke	vop_generic_revoke
-#define	o9fs_readlink	eopnotsupp
 int o9fs_inactive(void *);
 int o9fs_reclaim(void *);
-#define	o9fs_lock	vop_generic_lock
-#define	o9fs_unlock	vop_generic_unlock
-#define	o9fs_bmap	eopnotsupp
-#define	o9fs_strategy	eopnotsupp	
-#define	o9fs_print	eopnotsupp
-#define	o9fs_islocked	vop_generic_islocked
-#define	o9fs_pathconf	eopnotsupp
-#define	o9fs_advlock	eopnotsupp
-#define	o9fs_bwrite	eopnotsupp
-#define	o9fs_poll	eopnotsupp
 
 int (**o9fs_vnodeop_p)(void *);
-struct vnodeopv_entry_desc o9fs_vnodeop_entries[] = {
-	{ &vop_default_desc, eopnotsupp },
-	{ &vop_lookup_desc, o9fs_lookup },		/* lookup */
-	{ &vop_create_desc, o9fs_create },		/* create */
-	{ &vop_mknod_desc, o9fs_mknod },		/* mknod */
-	{ &vop_open_desc, o9fs_open },		/* open */
-	{ &vop_close_desc, o9fs_close },		/* close */
-	{ &vop_access_desc, o9fs_access },		/* access */
-	{ &vop_getattr_desc, o9fs_getattr },		/* getattr */
-	{ &vop_setattr_desc, o9fs_setattr },		/* setattr */
-	{ &vop_read_desc, o9fs_read },		/* read */
-	{ &vop_write_desc, o9fs_write },		/* write */
-	{ &vop_ioctl_desc, o9fs_ioctl },		/* ioctl */
-	{ &vop_poll_desc, o9fs_poll },		/* poll */
-	{ &vop_revoke_desc, o9fs_revoke },            /* revoke */
-	{ &vop_fsync_desc, o9fs_fsync },		/* fsync */
-	{ &vop_remove_desc, o9fs_remove },		/* remove */
-	{ &vop_link_desc, o9fs_link },		/* link */
-	{ &vop_rename_desc, o9fs_rename },		/* rename */
-	{ &vop_mkdir_desc, o9fs_mkdir },		/* mkdir */
-	{ &vop_rmdir_desc, o9fs_rmdir },		/* rmdir */
-	{ &vop_symlink_desc, o9fs_symlink },		/* symlink */
-	{ &vop_readdir_desc, o9fs_readdir },		/* readdir */
-	{ &vop_readlink_desc, o9fs_readlink },	/* readlink */
-	{ &vop_abortop_desc, vop_generic_abortop },		/* abortop */
-	{ &vop_inactive_desc, o9fs_inactive },	/* inactive */
-	{ &vop_reclaim_desc, o9fs_reclaim },		/* reclaim */
-	{ &vop_lock_desc, o9fs_lock },		/* lock */
-	{ &vop_unlock_desc, o9fs_unlock },		/* unlock */
-	{ &vop_bmap_desc, o9fs_bmap },		/* bmap */
-	{ &vop_strategy_desc, o9fs_strategy },	/* strategy */
-	{ &vop_print_desc, o9fs_print },		/* print */
-	{ &vop_islocked_desc, o9fs_islocked },	/* islocked */
-	{ &vop_pathconf_desc, o9fs_pathconf },	/* pathconf */
-	{ &vop_advlock_desc, o9fs_advlock },		/* advlock */
-	{ &vop_bwrite_desc, o9fs_bwrite },		/* bwrite */
-	{ NULL, NULL }
+
+struct vops o9fs_vops = {
+	.vop_lock = vop_generic_lock,
+	.vop_unlock = vop_generic_unlock,
+	.vop_islocked = vop_generic_islocked,
+	.vop_abortop = vop_generic_abortop,
+	.vop_access = o9fs_access,
+	.vop_advlock = eopnotsupp,
+	.vop_bmap = eopnotsupp,
+	.vop_bwrite = eopnotsupp,
+	.vop_close = o9fs_close,
+	.vop_create = eopnotsupp,
+	.vop_fsync = nullop,
+	.vop_getattr = o9fs_getattr,
+	.vop_inactive = o9fs_inactive,
+	.vop_ioctl = (int (*)(void *))enoioctl,
+	.vop_link = eopnotsupp,
+	.vop_lookup = o9fs_lookup,
+	.vop_mknod = eopnotsupp, 
+	.vop_open = o9fs_open,
+	.vop_pathconf = eopnotsupp,
+	.vop_poll = eopnotsupp,
+	.vop_print = eopnotsupp,
+	.vop_read = o9fs_read,
+	.vop_readdir = o9fs_readdir,
+	.vop_readlink = eopnotsupp,
+	.vop_reallocblks = eopnotsupp,
+	.vop_reclaim = o9fs_reclaim,
+	.vop_remove = eopnotsupp,
+	.vop_rename = eopnotsupp,
+	.vop_revoke = vop_generic_revoke,
+	.vop_mkdir = eopnotsupp,
+	.vop_rmdir = eopnotsupp,
+	.vop_setattr = eopnotsupp,
+	.vop_strategy = eopnotsupp,
+	.vop_symlink = eopnotsupp,
+	.vop_write = o9fs_write,
+	.vop_kqfilter = eopnotsupp,
 };
-struct vnodeopv_desc o9fs_vnodeop_opv_desc =
-	{ &o9fs_vnodeop_p, o9fs_vnodeop_entries };
-
+	
+	
 int 
 o9fs_open(void *v)
 {