Home | History | Annotate | Line # | Download | only in mount_9p
fs.c revision 1.2
      1 /*	$NetBSD: fs.c,v 1.2 2007/05/05 15:49:51 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 #ifndef lint
     30 __RCSID("$NetBSD: fs.c,v 1.2 2007/05/05 15:49:51 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <err.h>
     34 #include <errno.h>
     35 #include <puffs.h>
     36 #include <signal.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <unistd.h>
     40 
     41 #include "ninepuffs.h"
     42 #include "nineproto.h"
     43 
     44 #define DO_IO(fname, a1, a2, a3, a4, rv)				\
     45 	puffs_framebuf_seekset(a2, 0);					\
     46 	*(a4) = 0;							\
     47 	rv = fname(a1, a2, a3, a4);					\
     48 	if (rv || a4 == 0) errx(1, "p9p_handshake_ io failed %d, %d", rv, *a4)
     49 
     50 struct puffs_node *
     51 p9p_handshake(struct puffs_usermount *pu, const char *username)
     52 {
     53 	struct puffs9p *p9p = puffs_getspecific(pu);
     54 	struct puffs_framebuf *pb;
     55 	struct puffs_node *pn;
     56 	struct vattr rootva;
     57 	uint32_t maxreq;
     58 	uint16_t dummy;
     59 	p9ptag_t tagid, rtagid;
     60 	uint8_t type;
     61 	int rv, done, x = 1;
     62 
     63 	/* send initial handshake */
     64 	pb = p9pbuf_makeout();
     65 	p9pbuf_put_1(pb, P9PROTO_T_VERSION);
     66 	p9pbuf_put_2(pb, P9PROTO_NOTAG);
     67 	p9pbuf_put_4(pb, p9p->maxreq);
     68 	p9pbuf_put_str(pb, P9PROTO_VERSION);
     69 	DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
     70 
     71 	puffs_framebuf_recycle(pb);
     72 	DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
     73 
     74 	if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_VERSION)
     75 		errx(1, "server invalid response to Tversion: %d", type);
     76 	if ((rtagid = p9pbuf_get_tag(pb)) != P9PROTO_NOTAG) {
     77 		errx(1, "server invalid tag: %d vs. %d\n",
     78 		    P9PROTO_NOTAG, tagid);
     79 		return NULL;
     80 	}
     81 	if (p9pbuf_get_4(pb, &maxreq))
     82 		errx(1, "server invalid response: no request length");
     83 	if (maxreq < P9P_MINREQLEN)
     84 		errx(1, "server request length below minimum accepted: "
     85 		    "%d vs. %d", P9P_MINREQLEN, maxreq);
     86 	p9p->maxreq = maxreq;
     87 
     88 	/* tell the server we don't support authentication */
     89 	p9pbuf_recycleout(pb);
     90 	tagid = NEXTTAG(p9p);
     91 	p9pbuf_put_1(pb, P9PROTO_T_AUTH);
     92 	p9pbuf_put_2(pb, tagid);
     93 	p9pbuf_put_4(pb, P9PROTO_NOFID);
     94 	p9pbuf_put_str(pb, username);
     95 	p9pbuf_put_str(pb, "");
     96 	DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
     97 
     98 	puffs_framebuf_recycle(pb);
     99 	DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
    100 
    101 	/* assume all Rerror is "no auth" */
    102 	if (p9pbuf_get_type(pb) != P9PROTO_R_ERROR)
    103 		errx(1, "mount_9p supports only NO auth");
    104 	if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
    105 		errx(1, "server invalid tag: %d vs. %d\n", tagid, rtagid);
    106 
    107 	/* build attach message */
    108 	p9pbuf_recycleout(pb);
    109 	tagid = NEXTTAG(p9p);
    110 	p9pbuf_put_1(pb, P9PROTO_T_ATTACH);
    111 	p9pbuf_put_2(pb, tagid);
    112 	p9pbuf_put_4(pb, P9P_ROOTFID);
    113 	p9pbuf_put_4(pb, P9PROTO_NOFID);
    114 	p9pbuf_put_str(pb, username);
    115 	p9pbuf_put_str(pb, "");
    116 	DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
    117 
    118 	puffs_framebuf_recycle(pb);
    119 	DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
    120 
    121 	if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_ATTACH)
    122 		errx(1, "Rattach not received, got %d", type);
    123 	if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
    124 		errx(1, "server invalid tag: %d vs. %d\n", tagid, rtagid);
    125 
    126 	/* finally, stat the rootnode */
    127 	p9pbuf_recycleout(pb);
    128 	tagid = NEXTTAG(p9p);
    129 	p9pbuf_put_1(pb, P9PROTO_T_STAT);
    130 	p9pbuf_put_2(pb, tagid);
    131 	p9pbuf_put_4(pb, P9P_ROOTFID);
    132 	DO_IO(p9pbuf_write, pu, pb, p9p->servsock, &done, rv);
    133 
    134 	puffs_framebuf_recycle(pb);
    135 	DO_IO(p9pbuf_read, pu, pb, p9p->servsock, &done, rv);
    136 
    137 	if ((type = p9pbuf_get_type(pb)) != P9PROTO_R_STAT)
    138 		errx(1, "Rstat not received, got %d", type);
    139 	if ((rtagid = p9pbuf_get_tag(pb)) != tagid)
    140 		errx(1, "server invalid tag: %d vs. %d\n", tagid, rtagid);
    141 	if (p9pbuf_get_2(pb, &dummy))
    142 		errx(1, "couldn't get stat len parameter");
    143 	if (proto_getstat(pb, &rootva, NULL, NULL))
    144 		errx(1, "could not parse root attributes");
    145 	puffs_framebuf_destroy(pb);
    146 
    147 	rootva.va_nlink = 0156; /* guess, will be fixed with first readdir */
    148 	pn = newp9pnode_va(pu, &rootva, P9P_ROOTFID);
    149 
    150 	if (ioctl(p9p->servsock, FIONBIO, &x) == -1)
    151 		err(1, "cannot set socket in nonblocking mode");
    152 
    153 	return pn;
    154 }
    155 
    156 int
    157 puffs9p_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
    158 {
    159 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    160 	struct puffs9p *p9p = puffs_getspecific(pu);
    161 
    162 	close(p9p->servsock);
    163 	return 0;
    164 }
    165