xref: /netbsd-src/share/examples/rump/umserv/umserv.c (revision d35b86acad83e0ac06b9bb041ea23ab3c4f42d20)
1 /*	$NetBSD: umserv.c,v 1.3 2016/01/25 11:45:58 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 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 /*
29  * This is a [quick, simple & dirty] userspace sd@umass server.
30  * We probe USB devices using rump and attach them to the host kernel
31  * using pud(4).  The resulting block devices can be e.g. read
32  * and/or mounted.
33  *
34  * Since there is no devfs support in NetBSD, we create crudo & cotto
35  * device nodes in the current directory.  Operating on these in the
36  * host OS will direct operations to this userspace server, e.g.:
37  *   golem> disklabel ./rumpsd0d
38  *   golem> mount_msdos ./rumpsd0e /mnt
39  * will cause file system access to /mnt be backed by the umass server
40  * in userspace.  Due to the relatively experimental nature of this
41  * server, rump file servers are recommended for mounting experiments.
42  */
43 
44 #include <sys/types.h>
45 #include <sys/syslimits.h>
46 
47 #include <dev/pud/pud_msgif.h>
48 
49 #include <rump/rump.h>
50 #include <rump/rumpvnode_if.h>
51 
52 #include <assert.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <paths.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 /*
63  * No devfs?  No problem.  We just hack a bit & wait for the dust to settle.
64  */
65 #define NODEBASE "rumpsd0"
66 #define MYMAJOR 411
67 static int
makenodes(void)68 makenodes(void)
69 {
70 	char path[PATH_MAX];
71 	struct stat sb;
72 	int i, j, rv;
73 
74 	for (i = 0; i < 2; i++) {
75 		int minnum = 0;
76 
77 		for (j = 0; j < 8; j++, minnum++) {
78 			sprintf(path, "%s%s%c",
79 			    i == 0 ? "" : "r", NODEBASE, minnum + 'a');
80 			if (stat(path, &sb) == 0)
81 				continue;
82 			rv = mknod(path, (i == 0 ? S_IFBLK : S_IFCHR) | 0666,
83 			    makedev(MYMAJOR, minnum));
84 			if (rv != 0 && !(rv == -1 && errno == EEXIST))
85 				return rv;
86 		}
87 	}
88 
89 	return 0;
90 }
91 
92 int
main(int argc,char * argv[])93 main(int argc, char *argv[])
94 {
95 	char path[PATH_MAX];
96 	struct pud_conf_reg pcr;
97 	struct pud_req *pdr;
98 	struct vnode *devvps[8], *devvp;
99 	kauth_cred_t rootcred;
100 	ssize_t n;
101 	int fd, rv, i;
102 
103 	if (makenodes() == -1)
104 		err(1, "makenodes");
105 
106 	fd = open(_PATH_PUD, O_RDWR);
107 	if (fd == -1)
108 		err(1, "open");
109 
110 #define PDRSIZE (64*1024+1024)
111 	pdr = malloc(PDRSIZE);
112 	if (pdr == NULL)
113 		err(1, "malloc");
114 
115 	memset(&pcr, 0, sizeof(pcr));
116 	pcr.pm_pdr.pdr_pth.pth_framelen = sizeof(struct pud_conf_reg);
117 	pcr.pm_version = PUD_DEVELVERSION | PUD_VERSION;
118 	pcr.pm_pdr.pdr_reqclass = PUD_REQ_CONF;
119 	pcr.pm_pdr.pdr_reqtype = PUD_CONF_REG;
120 
121 	pcr.pm_regdev = makedev(MYMAJOR, 0);
122 	pcr.pm_flags = PUD_CONFFLAG_BDEV;
123 	strlcpy(pcr.pm_devname, "youmass", sizeof(pcr.pm_devname));
124 
125 	n = write(fd, &pcr, pcr.pm_pdr.pdr_pth.pth_framelen);
126 	if (n == -1)
127 		err(1, "configure"); /* XXX: doubles as protocol error */
128 
129 	rump_boot_sethowto(RUMP_AB_VERBOSE);
130 	rump_init();
131 
132 	/*
133 	 * We use the raw devices.  This avoids undesired block caching
134 	 * in read/write.  It's also simpler.  It's even mostly correct.
135 	 *
136 	 * As is probably obvious by now, we execute ops through specfs.
137 	 * Why?  Can't say it wasn't because specfs-via-vnodes was
138 	 * already supported by rump.  But, that's mostly how the
139 	 * kernel does it (cf. mounting file system etc).
140 	 */
141 	for (i = 0; i < 8; i++) {
142 		sprintf(path, "/dev/rsd0%c", 'a' + i);
143 		if ((rv = rump_pub_namei(RUMP_NAMEI_LOOKUP, 0, path,
144 		    NULL, &devvps[i], NULL)) != 0)
145 			errx(1, "raw device lookup failed %d", rv);
146 	}
147 
148 	rootcred = rump_pub_cred_create(0, 0, 0, NULL);
149 
150 	/* process requests ad infinitum */
151 	for (;;) {
152 		struct pud_creq_open *pr_open;
153 		struct pud_creq_close *pr_close;
154 		struct pud_req_readwrite *pr_rw;
155 		struct pud_req_ioctl *pr_ioctl;
156 		struct uio *uio;
157 		size_t reslen;
158 		int minordev;
159 
160 		n = read(fd, pdr, PDRSIZE);
161 		if (n == -1)
162 			err(1, "read");
163 
164 		minordev = minor(pdr->pdr_dev);
165 		if (minordev < 0 || minordev >= 8) {
166 			rv = ENXIO;
167 			goto sendresponse;
168 		}
169 		devvp = devvps[minordev];
170 
171 		switch (pdr->pdr_reqtype) {
172 		case PUD_BDEV_OPEN:
173 			pr_open = (void *)pdr;
174 			RUMP_VOP_LOCK(devvp, RUMP_LK_EXCLUSIVE);
175 			rv = RUMP_VOP_OPEN(devvp, pr_open->pm_fmt, rootcred);
176 			RUMP_VOP_UNLOCK(devvp);
177 			break;
178 
179 		case PUD_BDEV_CLOSE:
180 			pr_close = (void *)pdr;
181 			RUMP_VOP_LOCK(devvp, RUMP_LK_EXCLUSIVE);
182 			rv = RUMP_VOP_CLOSE(devvp, pr_close->pm_fmt, rootcred);
183 			RUMP_VOP_UNLOCK(devvp);
184 			break;
185 
186 		case PUD_BDEV_IOCTL:
187 			pr_ioctl = (void *)pdr;
188 			rv = RUMP_VOP_IOCTL(devvp, pr_ioctl->pm_iocmd,
189 			    pr_ioctl->pm_data, pr_ioctl->pm_flag, rootcred);
190 			break;
191 
192 		case PUD_BDEV_STRATREAD:
193 			pr_rw = (void *)pdr;
194 			assert(pr_rw->pm_resid <= 64*1024);
195 			uio = rump_pub_uio_setup(&pr_rw->pm_data[0],
196 			    pr_rw->pm_resid, pr_rw->pm_offset, RUMPUIO_READ);
197 			RUMP_VOP_LOCK(devvp, RUMP_LK_SHARED);
198 			rv = RUMP_VOP_READ(devvp, uio, 0, rootcred);
199 			RUMP_VOP_UNLOCK(devvp);
200 			reslen = rump_pub_uio_free(uio);
201 			pdr->pdr_pth.pth_framelen -= reslen;
202 			pr_rw->pm_resid = reslen;
203 			break;
204 
205 		case PUD_BDEV_STRATWRITE:
206 			pr_rw = (void *)pdr;
207 			uio = rump_pub_uio_setup(&pr_rw->pm_data[0],
208 			    pr_rw->pm_resid, pr_rw->pm_offset, RUMPUIO_WRITE);
209 			RUMP_VOP_LOCK(devvp, RUMP_LK_EXCLUSIVE);
210 			rv = RUMP_VOP_WRITE(devvp, uio, 0, rootcred);
211 			RUMP_VOP_UNLOCK(devvp);
212 			reslen = rump_pub_uio_free(uio);
213 			pr_rw->pm_resid = reslen;
214 			pdr->pdr_pth.pth_framelen=sizeof(struct pud_creq_write);
215 			rv = 0;
216 			break;
217 
218 		}
219 
220  sendresponse:
221 		pdr->pdr_rv = rv;
222 		n = write(fd, pdr, pdr->pdr_pth.pth_framelen);
223 		assert(n == (ssize_t)pdr->pdr_pth.pth_framelen);
224 	}
225 }
226