xref: /netbsd-src/lib/libperfuse/perfuse.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*  $NetBSD: perfuse.c,v 1.5 2010/09/07 02:11:04 manu Exp $ */
2 
3 /*-
4  *  Copyright (c) 2010 Emmanuel Dreyfus. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  *  POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <puffs.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 
39 #define LIBPERFUSE
40 #include "perfuse.h"
41 #include "perfuse_if.h"
42 #include "perfuse_priv.h"
43 
44 int perfuse_diagflags = 0; /* global used only in DPRINTF/DERR/DWARN */
45 
46 static struct perfuse_state *init_state(void);
47 static int get_fd(const char *);
48 
49 static struct perfuse_state *
50 init_state(void)
51 {
52 	struct perfuse_state *ps;
53 
54 	if ((ps = malloc(sizeof(*ps))) == NULL)
55 		DERR(EX_OSERR, "malloc failed");
56 
57 	(void)memset(ps, 0, sizeof(*ps));
58 	ps->ps_max_write = UINT_MAX;
59 	ps->ps_max_readahead = UINT_MAX;
60 
61 	return ps;
62 }
63 
64 
65 static int
66 get_fd(data)
67 	const char *data;
68 {
69 	char *string;
70 	const char fdopt[] = "fd=";
71 	char *lastp;
72 	char *opt;
73 	int fd = -1;
74 
75 	if ((string = strdup(data)) == NULL)
76 		return -1;
77 
78 	for (opt = strtok_r(string, ",", &lastp);
79 	     opt != NULL;
80 	     opt = strtok_r(NULL, ",", &lastp)) {
81 		if (strncmp(opt, fdopt, strlen(fdopt)) == 0) {
82 			fd =  atoi(opt + strlen(fdopt));
83 			break;
84 		}
85 	}
86 
87 	/*
88 	 * No file descriptor found
89 	 */
90 	if (fd == -1)
91 		errno = EINVAL;
92 
93 	free(string);
94 	return fd;
95 
96 }
97 
98 int
99 perfuse_open(path, flags, mode)
100 	const char *path;
101 	int flags;
102 	mode_t mode;
103 {
104 	int sv[2];
105 	struct sockaddr_un sun;
106 	struct sockaddr *sa;
107 	char progname[] = _PATH_PERFUSED;
108 	char minus_i[] = "-i";
109 	char fdstr[16];
110 	char *const argv[] = { progname, minus_i, fdstr, NULL};
111 	char *const envp[] = { NULL };
112 
113 	if (strcmp(path, _PATH_FUSE) != 0)
114 		return open(path, flags, mode);
115 
116 	if ((sv[0] = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1) {
117 #ifdef PERFUSE_DEBUG
118 		DWARN("%s:%d socket failed: %s", __func__, __LINE__);
119 #endif
120 		return -1;
121 	}
122 
123 	sa = (struct sockaddr *)(void *)&sun;
124 	sun.sun_len = sizeof(sun);
125 	sun.sun_family = AF_LOCAL;
126 	(void)strcpy(sun.sun_path, path);
127 
128 	if (connect(sv[0], sa, (socklen_t)sun.sun_len) == 0)
129 		return sv[0];
130 
131 
132 	/*
133 	 * Attempt to run perfused on our own
134 	 * if it does not run yet; In that case
135 	 * we will talk using a socketpair
136 	 * instead of /dev/fuse.
137 	 */
138 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) != 0) {
139 #ifdef PERFUSE_DEBUG
140 		DWARN("%s:%d: socketpair failed", __func__, __LINE__);
141 #endif
142 		return -1;
143 	}
144 
145 	(void)sprintf(fdstr, "%d", sv[1]);
146 
147 	switch(fork()) {
148 	case -1:
149 #ifdef PERFUSE_DEBUG
150 		DWARN("%s:%d: fork failed", __func__, __LINE__);
151 #endif
152 		return -1;
153 		/* NOTREACHED */
154 		break;
155 	case 0:
156 		(void)execve(argv[0], argv, envp);
157 #ifdef PERFUSE_DEBUG
158 		DWARN("%s:%d: execve failed", __func__, __LINE__);
159 #endif
160 		return -1;
161 		/* NOTREACHED */
162 		break;
163 	default:
164 		break;
165 	}
166 
167 	return sv[0];
168 }
169 
170 
171 int
172 perfuse_mount(source, target, filesystemtype, mountflags, data)
173 	const char *source;
174 	const char *target;
175 	const char *filesystemtype;
176 	long mountflags;
177 	const void *data;
178 {
179 	int s;
180 	size_t len;
181 	struct perfuse_mount_out pmo;
182 
183 #ifdef PERFUSE_DEBUG
184 	if (perfuse_diagflags & PDF_MISC)
185 		DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\")\n",
186 			__func__, source, target, filesystemtype,
187 			mountflags, (const char *)data);
188 #endif
189 
190 	if ((s = get_fd(data)) == -1)
191 		return -1;
192 
193 	pmo.pmo_len = sizeof(pmo);
194 	pmo.pmo_len += source ? (uint32_t)strlen(source) : 0;
195 	pmo.pmo_len += target ? (uint32_t)strlen(target) : 0;
196 	pmo.pmo_len += filesystemtype ? (uint32_t)strlen(filesystemtype) : 0;
197 	pmo.pmo_len += data ? (uint32_t)strlen(data) : 0;
198 	pmo.pmo_error = 0;
199 	pmo.pmo_unique = (uint64_t)-1;
200 
201 	(void)strcpy(pmo.pmo_magic, PERFUSE_MOUNT_MAGIC);
202 	pmo.pmo_source_len = source ? (uint32_t)strlen(source) : 0;
203 	pmo.pmo_target_len = target ? (uint32_t)strlen(target) : 0;
204 	pmo.pmo_filesystemtype_len =
205 	    filesystemtype ? (uint32_t)strlen(filesystemtype) : 0;
206 	pmo.pmo_mountflags = (uint32_t)mountflags;
207 	pmo.pmo_data_len = data ? (uint32_t)strlen(data) : 0;
208 
209 
210 	if (write(s, &pmo, sizeof(pmo)) != sizeof(pmo)) {
211 #ifdef PERFUSE_DEBUG
212 		if (perfuse_diagflags & PDF_MISC)
213 			DPRINTF("%s:%d short write\n", __func__, __LINE__);
214 #endif
215 		return -1;
216 	}
217 
218 	if (source) {
219 		len = pmo.pmo_source_len;
220 		if (write(s, source, len) != (ssize_t)len) {
221 #ifdef PERFUSE_DEBUG
222 			DWARNX("%s:%d short write\n", __func__, __LINE__);
223 #endif
224 			return -1;
225 		}
226 	}
227 
228 	if (target) {
229 		len = pmo.pmo_target_len;
230 		if (write(s, target, len) != (ssize_t)len) {
231 #ifdef PERFUSE_DEBUG
232 			DWARNX("%s:%d short write\n", __func__, __LINE__);
233 #endif
234 			return -1;
235 		}
236 	}
237 
238 	if (filesystemtype) {
239 		len = pmo.pmo_filesystemtype_len;
240 		if (write(s, filesystemtype, len) != (ssize_t)len) {
241 #ifdef PERFUSE_DEBUG
242 			DWARNX("%s:%d short write\n", __func__, __LINE__);
243 #endif
244 			return -1;
245 		}
246 	}
247 
248 	if (data) {
249 		len = pmo.pmo_data_len;
250 		if (write(s, data, len) != (ssize_t)len) {
251 #ifdef PERFUSE_DEBUG
252 			DWARNX("%s:%d short write\n", __func__, __LINE__);
253 #endif
254 			return -1;
255 		}
256 	}
257 
258 	return 0;
259 }
260 
261 
262 uint64_t
263 perfuse_next_unique(pu)
264 	struct puffs_usermount *pu;
265 {
266 	struct perfuse_state *ps;
267 
268 	ps = puffs_getspecific(pu);
269 
270 	return ps->ps_unique++;
271 }
272 
273 struct puffs_usermount *
274 perfuse_init(pc, pmi)
275 	struct perfuse_callbacks *pc;
276 	struct perfuse_mount_info *pmi;
277 {
278 	struct perfuse_state *ps;
279 	struct puffs_usermount *pu;
280 	struct puffs_ops *pops;
281 	char name[] = "perfuse";
282 	unsigned int puffs_flags;
283 	struct puffs_node *pn_root;
284 	struct puffs_pathobj *po_root;
285 
286 	ps = init_state();
287 	ps->ps_owner_uid = pmi->pmi_uid;
288 
289 	if (pmi->pmi_source)
290 		ps->ps_source = strdup(pmi->pmi_source);
291 	if (pmi->pmi_filesystemtype)
292 		ps->ps_filesystemtype = strdup(pmi->pmi_filesystemtype);
293 	ps->ps_target = strdup(pmi->pmi_target);
294 	ps->ps_mountflags = pmi->pmi_mountflags;
295 
296 	/*
297 	 * Some options are forbidden for non root users
298 	 */
299 	if (ps->ps_owner_uid != 0)
300 	    ps->ps_mountflags |= MNT_NOSUID|MNT_NODEV;
301 
302 	PUFFSOP_INIT(pops);
303 	PUFFSOP_SET(pops, perfuse, fs, unmount);
304 	PUFFSOP_SET(pops, perfuse, fs, statvfs);
305 	PUFFSOP_SET(pops, perfuse, fs, sync);
306 	PUFFSOP_SET(pops, perfuse, node, lookup);
307 	PUFFSOP_SET(pops, perfuse, node, create);
308 	PUFFSOP_SET(pops, perfuse, node, mknod);
309 	PUFFSOP_SET(pops, perfuse, node, open);
310 	PUFFSOP_SET(pops, perfuse, node, close);
311 	PUFFSOP_SET(pops, perfuse, node, access);
312 	PUFFSOP_SET(pops, perfuse, node, getattr);
313 	PUFFSOP_SET(pops, perfuse, node, setattr);
314 	PUFFSOP_SET(pops, perfuse, node, poll);
315 #if 0
316 	PUFFSOP_SET(pops, perfuse, node, mmap);
317 #endif
318 	PUFFSOP_SET(pops, perfuse, node, fsync);
319 	PUFFSOP_SET(pops, perfuse, node, seek);
320 	PUFFSOP_SET(pops, perfuse, node, remove);
321 	PUFFSOP_SET(pops, perfuse, node, link);
322 	PUFFSOP_SET(pops, perfuse, node, rename);
323 	PUFFSOP_SET(pops, perfuse, node, mkdir);
324 	PUFFSOP_SET(pops, perfuse, node, rmdir);
325 	PUFFSOP_SET(pops, perfuse, node, symlink);
326 	PUFFSOP_SET(pops, perfuse, node, readdir);
327 	PUFFSOP_SET(pops, perfuse, node, readlink);
328 	PUFFSOP_SET(pops, perfuse, node, reclaim);
329 	PUFFSOP_SET(pops, perfuse, node, inactive);
330 	PUFFSOP_SET(pops, perfuse, node, print);
331 	PUFFSOP_SET(pops, perfuse, node, advlock);
332 	PUFFSOP_SET(pops, perfuse, node, read);
333 	PUFFSOP_SET(pops, perfuse, node, write);
334 
335 	puffs_flags = PUFFS_FLAG_BUILDPATH | PUFFS_FLAG_HASHPATH;
336 	if (perfuse_diagflags & PDF_PUFFS)
337 		puffs_flags |= PUFFS_FLAG_OPDUMP;
338 
339 	if ((pu = puffs_init(pops, _PATH_PUFFS, name, ps, puffs_flags)) == NULL)
340 		DERR(EX_OSERR, "puffs_init failed");
341 
342 	ps->ps_pu = pu;
343 
344 	/*
345 	 * Setup filesystem root
346 	 */
347 	pn_root = perfuse_new_pn(pu, NULL);
348 	PERFUSE_NODE_DATA(pn_root)->pnd_ino = FUSE_ROOT_ID;
349 	PERFUSE_NODE_DATA(pn_root)->pnd_parent = pn_root;
350 	puffs_setroot(pu, pn_root);
351 	ps->ps_fsid = pn_root->pn_va.va_fsid;
352 
353 	po_root = puffs_getrootpathobj(pu);
354 	if ((po_root->po_path = strdup("/")) == NULL)
355 		DERRX(EX_OSERR, "perfuse_mount_start() failed");
356 
357 	po_root->po_len = 1;
358 	puffs_path_buildhash(pu, po_root);
359 
360 	puffs_vattr_null(&pn_root->pn_va);
361 	pn_root->pn_va.va_type = VDIR;
362 	pn_root->pn_va.va_mode = 0755;
363 
364 	ps->ps_root = pn_root;
365 
366 	/*
367 	 *  Callbacks
368 	 */
369 	ps->ps_new_msg = pc->pc_new_msg;
370 	ps->ps_xchg_msg = pc->pc_xchg_msg;
371 	ps->ps_destroy_msg = pc->pc_destroy_msg;
372 	ps->ps_get_inhdr = pc->pc_get_inhdr;
373 	ps->ps_get_inpayload = pc->pc_get_inpayload;
374 	ps->ps_get_outhdr = pc->pc_get_outhdr;
375 	ps->ps_get_outpayload = pc->pc_get_outpayload;
376 
377 	return pu;
378 }
379 
380 void
381 perfuse_setspecific(pu, priv)
382 	struct puffs_usermount *pu;
383 	void *priv;
384 {
385 	struct perfuse_state *ps;
386 
387 	ps = puffs_getspecific(pu);
388 	ps->ps_private = priv;
389 
390 	return;
391 }
392 
393 void *
394 perfuse_getspecific(pu)
395 	struct puffs_usermount *pu;
396 {
397 	struct perfuse_state *ps;
398 
399 	ps = puffs_getspecific(pu);
400 
401 	return ps->ps_private;
402 }
403 
404 int
405 perfuse_inloop(pu)
406 	struct puffs_usermount *pu;
407 {
408 	struct perfuse_state *ps;
409 
410 	ps = puffs_getspecific(pu);
411 
412 	return ps->ps_flags & PS_INLOOP;
413 }
414 
415 int
416 perfuse_mainloop(pu)
417 	struct puffs_usermount *pu;
418 {
419 	struct perfuse_state *ps;
420 
421 	ps = puffs_getspecific(pu);
422 
423 	ps->ps_flags |= PS_INLOOP;
424 	if (puffs_mainloop(ps->ps_pu) != 0)
425 		DERR(EX_OSERR, "puffs_mainloop failed");
426 	DERR(EX_OSERR, "puffs_mainloop exit");
427 
428 	/* NOTREACHED */
429 	return -1;
430 }
431 
432 /* ARGSUSED0 */
433 uint64_t
434 perfuse_get_ino(pu, opc)
435 	struct puffs_usermount *pu;
436 	puffs_cookie_t opc;
437 {
438 	return PERFUSE_NODE_DATA(opc)->pnd_ino;
439 }
440 
441 int
442 perfuse_unmount(pu)
443 	struct puffs_usermount *pu;
444 {
445 	struct perfuse_state *ps;
446 
447 	ps = puffs_getspecific(pu);
448 
449 	return unmount(ps->ps_target, MNT_FORCE);
450 }
451