xref: /minix3/lib/libpuffs/puffs.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: puffs.c,v 1.120 2015/06/17 00:15:26 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
5  *
6  * Development of this software was supported by the
7  * Google Summer of Code program and the Ulla Tuominen Foundation.
8  * The Google SoC project was mentored by Bill Studenmund.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #if !defined(lint)
34 __RCSID("$NetBSD: puffs.c,v 1.120 2015/06/17 00:15:26 christos Exp $");
35 #endif /* !lint */
36 
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 
40 #include <assert.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <mntopts.h>
45 #include <paths.h>
46 #if !defined(__minix)
47 #include <pthread.h>
48 #endif /* !defined(__minix) */
49 #include <puffs.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <syslog.h>
54 #include <unistd.h>
55 
56 #include "puffs_priv.h"
57 
58 /* Most file systems want this for opts, so just give it to them */
59 const struct mntopt puffsmopts[] = {
60 	MOPT_STDOPTS,
61 	PUFFSMOPT_STD,
62 	MOPT_NULL,
63 };
64 
65 #if !defined(__minix)
66 pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
67 #endif /* !defined(__minix) */
68 
69 #define FILLOP(lower, upper)						\
70 do {									\
71 	if (pops->puffs_node_##lower)					\
72 		opmask[PUFFS_VN_##upper] = 1;				\
73 } while (/*CONSTCOND*/0)
74 static void
fillvnopmask(struct puffs_ops * pops,struct puffs_kargs * pa)75 fillvnopmask(struct puffs_ops *pops, struct puffs_kargs *pa)
76 {
77 	uint8_t *opmask = pa->pa_vnopmask;
78 
79 	memset(opmask, 0, sizeof(pa->pa_vnopmask));
80 
81 	FILLOP(create,   CREATE);
82 	FILLOP(mknod,    MKNOD);
83 	FILLOP(open,     OPEN);
84 	FILLOP(close,    CLOSE);
85 	FILLOP(access,   ACCESS);
86 	FILLOP(getattr,  GETATTR);
87 	FILLOP(setattr,  SETATTR);
88 	FILLOP(poll,     POLL);
89 	FILLOP(mmap,     MMAP);
90 	FILLOP(fsync,    FSYNC);
91 	FILLOP(seek,     SEEK);
92 	FILLOP(remove,   REMOVE);
93 	FILLOP(link,     LINK);
94 	FILLOP(rename,   RENAME);
95 	FILLOP(mkdir,    MKDIR);
96 	FILLOP(rmdir,    RMDIR);
97 	FILLOP(symlink,  SYMLINK);
98 	FILLOP(readdir,  READDIR);
99 	FILLOP(readlink, READLINK);
100 	FILLOP(reclaim,  RECLAIM);
101 	FILLOP(inactive, INACTIVE);
102 	FILLOP(print,    PRINT);
103 	FILLOP(read,     READ);
104 	FILLOP(write,    WRITE);
105 	FILLOP(advlock,  ADVLOCK);
106 	FILLOP(abortop,  ABORTOP);
107 	FILLOP(pathconf, PATHCONF);
108 
109 	FILLOP(getextattr,  GETEXTATTR);
110 	FILLOP(setextattr,  SETEXTATTR);
111 	FILLOP(listextattr, LISTEXTATTR);
112 	FILLOP(deleteextattr, DELETEEXTATTR);
113 	FILLOP(fallocate, FALLOCATE);
114 	FILLOP(fdiscard, FDISCARD);
115 }
116 #undef FILLOP
117 
118 /*
119  * Go over all framev entries and write everything we can.  This is
120  * mostly for the benefit of delivering "unmount" to the kernel.
121  */
122 static void
finalpush(struct puffs_usermount * pu)123 finalpush(struct puffs_usermount *pu)
124 {
125 #if !defined(__minix)
126 	struct puffs_fctrl_io *fio;
127 
128 	LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
129 		if (fio->stat & FIO_WRGONE)
130 			continue;
131 
132 		puffs__framev_output(pu, fio->fctrl, fio);
133 	}
134 #endif /* !defined(__minix) */
135 }
136 
137 /*ARGSUSED*/
138 void
puffs_kernerr_abort(struct puffs_usermount * pu,uint8_t type,int error,const char * str,puffs_cookie_t cookie)139 puffs_kernerr_abort(struct puffs_usermount *pu, uint8_t type,
140 	int error, const char *str, puffs_cookie_t cookie)
141 {
142 
143 #if !defined(__minix)
144 	warnx("abort: type %d, error %d, cookie %p (%s)",
145 #else
146 	lpuffs_debug("abort: type %d, error %d, cookie %p (%s)\n",
147 #endif /* !defined(__minix) */
148 	    type, error, cookie, str);
149 	abort();
150 }
151 
152 /*ARGSUSED*/
153 void
puffs_kernerr_log(struct puffs_usermount * pu,uint8_t type,int error,const char * str,puffs_cookie_t cookie)154 puffs_kernerr_log(struct puffs_usermount *pu, uint8_t type,
155 	int error, const char *str, puffs_cookie_t cookie)
156 {
157 
158 	syslog(LOG_WARNING, "kernel: type %d, error %d, cookie %p (%s)",
159 	    type, error, cookie, str);
160 }
161 
162 #if !defined(__minix)
163 int
puffs_getselectable(struct puffs_usermount * pu)164 puffs_getselectable(struct puffs_usermount *pu)
165 {
166 
167 	return pu->pu_fd;
168 }
169 
170 uint64_t
puffs__nextreq(struct puffs_usermount * pu)171 puffs__nextreq(struct puffs_usermount *pu)
172 {
173 	uint64_t rv;
174 
175 	PU_LOCK();
176 	rv = pu->pu_nextreq++ | (uint64_t)1<<63;
177 	PU_UNLOCK();
178 
179 	return rv;
180 }
181 
182 int
puffs_setblockingmode(struct puffs_usermount * pu,int mode)183 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
184 {
185 	int rv, x;
186 
187 	assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
188 
189 	if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
190 		errno = EINVAL;
191 		return -1;
192 	}
193 
194 	x = mode;
195 	rv = ioctl(pu->pu_fd, FIONBIO, &x);
196 
197 	if (rv == 0) {
198 		if (mode == PUFFSDEV_BLOCK)
199 			pu->pu_state &= ~PU_ASYNCFD;
200 		else
201 			pu->pu_state |= PU_ASYNCFD;
202 	}
203 
204 	return rv;
205 }
206 #endif /* !defined(__minix) */
207 
208 int
puffs_getstate(struct puffs_usermount * pu)209 puffs_getstate(struct puffs_usermount *pu)
210 {
211 
212 	return pu->pu_state & PU_STATEMASK;
213 }
214 
215 void
puffs_setstacksize(struct puffs_usermount * pu,size_t ss)216 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
217 {
218 	long psize, minsize;
219 	int stackshift;
220 	int bonus;
221 
222 	assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
223 
224 	psize = sysconf(_SC_PAGESIZE);
225 	minsize = 4*psize;
226 	if (ss < (size_t)minsize || ss == PUFFS_STACKSIZE_MIN) {
227 		if (ss != PUFFS_STACKSIZE_MIN)
228 #if !defined(__minix)
229 			warnx("%s: adjusting " "stacksize to minimum %ld",
230 			    __func__, minsize);
231 #endif /* !defined(__minix) */
232 		ss = 4*psize;
233 	}
234 
235 	stackshift = -1;
236 	bonus = 0;
237 	while (ss) {
238 		if (ss & 0x1)
239 			bonus++;
240 		ss >>= 1;
241 		stackshift++;
242 	}
243 	if (bonus > 1) {
244 		stackshift++;
245 #if !defined(__minix)
246 		warnx("%s: using next power of two: %d", __func__,
247 		    1 << stackshift);
248 #endif /* !defined(__minix) */
249 	}
250 
251 	pu->pu_cc_stackshift = stackshift;
252 }
253 
254 struct puffs_pathobj *
puffs_getrootpathobj(struct puffs_usermount * pu)255 puffs_getrootpathobj(struct puffs_usermount *pu)
256 {
257 	struct puffs_node *pnr;
258 
259 	pnr = pu->pu_pn_root;
260 	if (pnr == NULL) {
261 		errno = ENOENT;
262 		return NULL;
263 	}
264 
265 	return &pnr->pn_po;
266 }
267 
268 void
puffs_setroot(struct puffs_usermount * pu,struct puffs_node * pn)269 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
270 {
271 
272 	pu->pu_pn_root = pn;
273 }
274 
275 struct puffs_node *
puffs_getroot(struct puffs_usermount * pu)276 puffs_getroot(struct puffs_usermount *pu)
277 {
278 
279 	return pu->pu_pn_root;
280 }
281 
282 void
puffs_setrootinfo(struct puffs_usermount * pu,enum vtype vt,vsize_t vsize,dev_t rdev)283 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
284 	vsize_t vsize, dev_t rdev)
285 {
286 	struct puffs_kargs *pargs = pu->pu_kargp;
287 
288 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
289 		warnx("%s: call has effect only before mount", __func__);
290 		return;
291 	}
292 
293 	pargs->pa_root_vtype = vt;
294 	pargs->pa_root_vsize = vsize;
295 	pargs->pa_root_rdev = rdev;
296 }
297 
298 void *
puffs_getspecific(struct puffs_usermount * pu)299 puffs_getspecific(struct puffs_usermount *pu)
300 {
301 
302 	return pu->pu_privdata;
303 }
304 
305 void
puffs_setspecific(struct puffs_usermount * pu,void * privdata)306 puffs_setspecific(struct puffs_usermount *pu, void *privdata)
307 {
308 
309 	pu->pu_privdata = privdata;
310 }
311 
312 void
puffs_setmntinfo(struct puffs_usermount * pu,const char * mntfromname,const char * puffsname)313 puffs_setmntinfo(struct puffs_usermount *pu,
314 	const char *mntfromname, const char *puffsname)
315 {
316 	struct puffs_kargs *pargs = pu->pu_kargp;
317 
318 	(void)strlcpy(pargs->pa_mntfromname, mntfromname,
319 	    sizeof(pargs->pa_mntfromname));
320 	(void)strlcpy(pargs->pa_typename, puffsname,
321 	    sizeof(pargs->pa_typename));
322 }
323 
324 size_t
puffs_getmaxreqlen(struct puffs_usermount * pu)325 puffs_getmaxreqlen(struct puffs_usermount *pu)
326 {
327 
328 	return pu->pu_maxreqlen;
329 }
330 
331 void
puffs_setmaxreqlen(struct puffs_usermount * pu,size_t reqlen)332 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
333 {
334 
335 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
336 		warnx("%s: call has effect only before mount", __func__);
337 
338 	pu->pu_kargp->pa_maxmsglen = reqlen;
339 }
340 
341 void
puffs_setfhsize(struct puffs_usermount * pu,size_t fhsize,int flags)342 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
343 {
344 
345 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
346 		warnx("%s: call has effect only before mount", __func__);
347 
348 	pu->pu_kargp->pa_fhsize = fhsize;
349 	pu->pu_kargp->pa_fhflags = flags;
350 }
351 
352 void
puffs_setncookiehash(struct puffs_usermount * pu,int nhash)353 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
354 {
355 
356 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
357 		warnx("%s: call has effect only before mount", __func__);
358 
359 	pu->pu_kargp->pa_nhashbuckets = nhash;
360 }
361 
362 void
puffs_set_pathbuild(struct puffs_usermount * pu,pu_pathbuild_fn fn)363 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
364 {
365 
366 	pu->pu_pathbuild = fn;
367 }
368 
369 void
puffs_set_pathtransform(struct puffs_usermount * pu,pu_pathtransform_fn fn)370 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
371 {
372 
373 	pu->pu_pathtransform = fn;
374 }
375 
376 void
puffs_set_pathcmp(struct puffs_usermount * pu,pu_pathcmp_fn fn)377 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
378 {
379 
380 	pu->pu_pathcmp = fn;
381 }
382 
383 void
puffs_set_pathfree(struct puffs_usermount * pu,pu_pathfree_fn fn)384 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
385 {
386 
387 	pu->pu_pathfree = fn;
388 }
389 
390 void
puffs_set_namemod(struct puffs_usermount * pu,pu_namemod_fn fn)391 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
392 {
393 
394 	pu->pu_namemod = fn;
395 }
396 
397 void
puffs_set_errnotify(struct puffs_usermount * pu,pu_errnotify_fn fn)398 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
399 {
400 
401 	pu->pu_errnotify = fn;
402 }
403 
404 void
puffs_set_cmap(struct puffs_usermount * pu,pu_cmap_fn fn)405 puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
406 {
407 
408 	pu->pu_cmap = fn;
409 }
410 
411 void
puffs_ml_setloopfn(struct puffs_usermount * pu,puffs_ml_loop_fn lfn)412 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
413 {
414 
415 	pu->pu_ml_lfn = lfn;
416 }
417 
418 void
puffs_ml_settimeout(struct puffs_usermount * pu,struct timespec * ts)419 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
420 {
421 
422 	if (ts == NULL) {
423 		pu->pu_ml_timep = NULL;
424 	} else {
425 		pu->pu_ml_timeout = *ts;
426 		pu->pu_ml_timep = &pu->pu_ml_timeout;
427 	}
428 }
429 
430 void
puffs_set_prepost(struct puffs_usermount * pu,pu_prepost_fn pre,pu_prepost_fn pst)431 puffs_set_prepost(struct puffs_usermount *pu,
432 	pu_prepost_fn pre, pu_prepost_fn pst)
433 {
434 
435 	pu->pu_oppre = pre;
436 	pu->pu_oppost = pst;
437 }
438 
439 #if !defined(__minix)
440 void
puffs_setback(struct puffs_cc * pcc,int whatback)441 puffs_setback(struct puffs_cc *pcc, int whatback)
442 {
443 	struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
444 
445 	assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
446 	    preq->preq_optype == PUFFS_VN_OPEN ||
447 	    preq->preq_optype == PUFFS_VN_MMAP ||
448 	    preq->preq_optype == PUFFS_VN_REMOVE ||
449 	    preq->preq_optype == PUFFS_VN_RMDIR ||
450 	    preq->preq_optype == PUFFS_VN_INACTIVE));
451 
452 	preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
453 }
454 
455 int
puffs_daemon(struct puffs_usermount * pu,int nochdir,int noclose)456 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
457 {
458 	long int n;
459 	int parent, value, fd;
460 
461 	if (pipe(pu->pu_dpipe) == -1)
462 		return -1;
463 
464 	switch (fork()) {
465 	case -1:
466 		return -1;
467 	case 0:
468 		parent = 0;
469 		break;
470 	default:
471 		parent = 1;
472 		break;
473 	}
474 	pu->pu_state |= PU_PUFFSDAEMON;
475 
476 	if (parent) {
477 		close(pu->pu_dpipe[1]);
478 		n = read(pu->pu_dpipe[0], &value, sizeof(int));
479 		if (n == -1)
480 			err(1, "puffs_daemon");
481 		if (n != sizeof(value))
482 			errx(1, "puffs_daemon got %ld bytes", n);
483 		if (value) {
484 			errno = value;
485 			err(1, "puffs_daemon");
486 		}
487 		exit(0);
488 	} else {
489 		if (setsid() == -1)
490 			goto fail;
491 
492 		if (!nochdir)
493 			chdir("/");
494 
495 		if (!noclose) {
496 			fd = open(_PATH_DEVNULL, O_RDWR, 0);
497 			if (fd == -1)
498 				goto fail;
499 			dup2(fd, STDIN_FILENO);
500 			dup2(fd, STDOUT_FILENO);
501 			dup2(fd, STDERR_FILENO);
502 			if (fd > STDERR_FILENO)
503 				close(fd);
504 		}
505 		return 0;
506 	}
507 
508  fail:
509 	n = write(pu->pu_dpipe[1], &errno, sizeof(int));
510 	assert(n == 4);
511 	return -1;
512 }
513 #endif /* !defined(__minix) */
514 
515 static void
shutdaemon(struct puffs_usermount * pu,int error)516 shutdaemon(struct puffs_usermount *pu, int error)
517 {
518 #if !defined(__minix)
519 	ssize_t n;
520 
521 	n = write(pu->pu_dpipe[1], &error, sizeof(int));
522 	assert(n == 4);
523 	close(pu->pu_dpipe[0]);
524 	close(pu->pu_dpipe[1]);
525 #endif /* !defined(__minix) */
526 	pu->pu_state &= ~PU_PUFFSDAEMON;
527 }
528 
529 int
puffs_mount(struct puffs_usermount * pu,const char * dir,int mntflags,puffs_cookie_t cookie)530 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
531 	puffs_cookie_t cookie)
532 {
533 #if !defined(__minix)
534 	int rv, fd, sverrno;
535 	char *comfd;
536 #endif /* !defined(__minix) */
537 
538 	pu->pu_kargp->pa_root_cookie = cookie;
539 
540 #if !defined(__minix)
541 	/* XXXkludgehere */
542 	/* kauth doesn't provide this service any longer */
543 	if (geteuid() != 0)
544 		mntflags |= MNT_NOSUID | MNT_NODEV;
545 
546 	/*
547 	 * Undocumented...  Well, documented only here.
548 	 *
549 	 * This is used for imaginative purposes.  If the env variable is
550 	 * set, puffs_mount() doesn't do the regular mount procedure.
551 	 * Rather, it crams the mount data down the comfd and sets comfd as
552 	 * the puffs descriptor.
553 	 *
554 	 * This shouldn't be used unless you can read my mind ( ... or write
555 	 * it, not to mention execute it, but that's starting to get silly).
556 	 */
557 	if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
558 		size_t len;
559 
560 		if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
561 			errno = EINVAL;
562 			rv = -1;
563 			goto out;
564 		}
565 		/* check that what we got at least resembles an fd */
566 		if (fcntl(pu->pu_fd, F_GETFL) == -1) {
567 			rv = -1;
568 			goto out;
569 		}
570 
571 #define allwrite(buf, len)						\
572 do {									\
573 	ssize_t al_rv;							\
574 	al_rv = write(pu->pu_fd, buf, len);				\
575 	if ((size_t)al_rv != len) {					\
576 		if (al_rv != -1)					\
577 			errno = EIO;					\
578 		rv = -1;						\
579 		goto out;						\
580 	}								\
581 } while (/*CONSTCOND*/0)
582 		len = strlen(dir)+1;
583 		allwrite(&len, sizeof(len));
584 		allwrite(dir, len);
585 		len = strlen(pu->pu_kargp->pa_mntfromname)+1;
586 		allwrite(&len, sizeof(len));
587 		allwrite(pu->pu_kargp->pa_mntfromname, len);
588 		allwrite(&mntflags, sizeof(mntflags));
589 		len = sizeof(*pu->pu_kargp);
590 		allwrite(&len, sizeof(len));
591 		allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
592 		allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
593 #undef allwrite
594 
595 		rv = 0;
596 	} else {
597 		char rp[MAXPATHLEN];
598 		size_t rplen,dirlen;
599 
600 		if (realpath(dir, rp) == NULL) {
601 			rv = -1;
602 			goto out;
603 		}
604 
605 		rplen = strlen(rp);
606 		dirlen = strlen(dir);
607 		if (strncmp(dir, rp, rplen) != 0 ||
608 		    strspn(dir + rplen, "/") != dirlen - rplen) {
609 			warnx("puffs_mount: \"%s\" is a relative path.", dir);
610 			warnx("puffs_mount: using \"%s\" instead.", rp);
611 		}
612 
613 		fd = open(_PATH_PUFFS, O_RDWR);
614 		if (fd == -1) {
615 			warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
616 			rv = -1;
617 			goto out;
618 		}
619 		if (fd <= 2)
620 			warnx("puffs_mount: device fd %d (<= 2), sure this is "
621 			    "what you want?", fd);
622 
623 		pu->pu_kargp->pa_fd = pu->pu_fd = fd;
624 		if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
625 		    pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
626 			goto out;
627 	}
628 #else
629 	/* Process the already-received mount request. */
630 	if (!lpuffs_pump()) {
631 		/* Not mounted?  This should never happen.. */
632 		free(pu->pu_kargp);
633 		pu->pu_kargp = NULL;
634 		errno = EINVAL;
635 		return -1;
636 	}
637 #endif /* !defined(__minix) */
638 
639 	PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
640 
641 #if !defined(__minix)
642  out:
643 	if (rv != 0)
644 		sverrno = errno;
645 	else
646 		sverrno = 0;
647 	free(pu->pu_kargp);
648 	pu->pu_kargp = NULL;
649 
650 	if (pu->pu_state & PU_PUFFSDAEMON)
651 		shutdaemon(pu, sverrno);
652 
653 	errno = sverrno;
654 	return rv;
655 #else
656 	return 0;
657 #endif /* !defined(__minix) */
658 }
659 
660 struct puffs_usermount *
puffs_init(struct puffs_ops * pops,const char * mntfromname,const char * puffsname,void * priv,uint32_t pflags)661 puffs_init(struct puffs_ops *pops, const char *mntfromname,
662 	const char *puffsname, void *priv, uint32_t pflags)
663 {
664 	struct puffs_usermount *pu;
665 	struct puffs_kargs *pargs;
666 	int sverrno;
667 
668 	if (puffsname == PUFFS_DEFER)
669 		puffsname = "n/a";
670 	if (mntfromname == PUFFS_DEFER)
671 		mntfromname = "n/a";
672 	if (priv == PUFFS_DEFER)
673 		priv = NULL;
674 
675 	pu = malloc(sizeof(struct puffs_usermount));
676 	if (pu == NULL)
677 		goto failfree;
678 	memset(pu, 0, sizeof(struct puffs_usermount));
679 
680 	pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
681 	if (pargs == NULL)
682 		goto failfree;
683 	memset(pargs, 0, sizeof(struct puffs_kargs));
684 
685 	pargs->pa_vers = PUFFSVERSION;
686 	pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
687 	fillvnopmask(pops, pargs);
688 	puffs_setmntinfo(pu, mntfromname, puffsname);
689 
690 	puffs_zerostatvfs(&pargs->pa_svfsb);
691 	pargs->pa_root_cookie = NULL;
692 	pargs->pa_root_vtype = VDIR;
693 	pargs->pa_root_vsize = 0;
694 	pargs->pa_root_rdev = 0;
695 	pargs->pa_maxmsglen = 0;
696 	if (/*CONSTCOND*/ sizeof(time_t) == 4)
697 		pargs->pa_time32 = 1;
698 	else
699 		pargs->pa_time32 = 0;
700 
701 	pu->pu_flags = pflags;
702 	pu->pu_ops = *pops;
703 	free(pops); /* XXX */
704 
705 	pu->pu_privdata = priv;
706 	pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
707 	LIST_INIT(&pu->pu_pnodelst);
708 	LIST_INIT(&pu->pu_ios);
709 	LIST_INIT(&pu->pu_ios_rmlist);
710 	LIST_INIT(&pu->pu_ccmagazin);
711 	TAILQ_INIT(&pu->pu_sched);
712 
713 #if !defined(__minix)
714 	pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
715 	pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
716 	pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
717 	pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
718 	pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
719 #endif /* !defined(__minix) */
720 
721 	/* defaults for some user-settable translation functions */
722 	pu->pu_cmap = NULL; /* identity translation */
723 
724 	pu->pu_pathbuild = puffs_stdpath_buildpath;
725 	pu->pu_pathfree = puffs_stdpath_freepath;
726 	pu->pu_pathcmp = puffs_stdpath_cmppath;
727 	pu->pu_pathtransform = NULL;
728 	pu->pu_namemod = NULL;
729 
730 	pu->pu_errnotify = puffs_kernerr_log;
731 
732 	PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
733 
734 #if defined(__minix)
735 	/* Do the MINIX3-specific side of the initialization. */
736 	lpuffs_init(pu);
737 #endif /* defined(__minix) */
738 
739 	return pu;
740 
741  failfree:
742 	/* can't unmount() from here for obvious reasons */
743 	sverrno = errno;
744 	free(pu);
745 	errno = sverrno;
746 	return NULL;
747 }
748 
749 void
puffs_cancel(struct puffs_usermount * pu,int error)750 puffs_cancel(struct puffs_usermount *pu, int error)
751 {
752 
753 	assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
754 	shutdaemon(pu, error);
755 	free(pu);
756 }
757 
758 /*ARGSUSED1*/
759 int
puffs_exit(struct puffs_usermount * pu,int unused)760 puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
761 {
762 #if !defined(__minix)
763 	struct puffs_framebuf *pb;
764 	struct puffs_req *preq;
765 	void *winp;
766 	size_t winlen;
767 	int sverrno;
768 
769 	pb = puffs_framebuf_make();
770 	if (pb == NULL) {
771 		errno = ENOMEM;
772 		return -1;
773 	}
774 
775 	winlen = sizeof(struct puffs_req);
776 	if (puffs_framebuf_getwindow(pb, 0, &winp, &winlen) == -1) {
777 		sverrno = errno;
778 		puffs_framebuf_destroy(pb);
779 		errno = sverrno;
780 		return -1;
781 	}
782 	preq = winp;
783 
784 	preq->preq_buflen = sizeof(struct puffs_req);
785 	preq->preq_opclass = PUFFSOP_UNMOUNT;
786 	preq->preq_id = puffs__nextreq(pu);
787 
788 	puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
789 #else
790 	struct puffs_node *pn;
791 
792 	lpuffs_debug("puffs_exit\n");
793 
794 	while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
795 		puffs_pn_put(pn);
796 
797 	while ((pn = LIST_FIRST(&pu->pu_pnode_removed_lst)) != NULL)
798 		puffs_pn_put(pn);
799 
800 	puffs__cc_exit(pu);
801 	if (pu->pu_state & PU_HASKQ)
802 		close(pu->pu_kq);
803 	free(pu);
804 #endif /* !defined(__minix) */
805 
806 	return 0;
807 }
808 
809 #if !defined(__minix)
810 /* no sigset_t static intializer */
811 static int sigs[NSIG] = { 0, };
812 static int sigcatch = 0;
813 
814 int
puffs_unmountonsignal(int sig,bool sigignore)815 puffs_unmountonsignal(int sig, bool sigignore)
816 {
817 
818 	if (sig < 0 || sig >= (int)NSIG) {
819 		errno = EINVAL;
820 		return -1;
821 	}
822 	if (sigignore)
823 		if (signal(sig, SIG_IGN) == SIG_ERR)
824 			return -1;
825 
826 	if (!sigs[sig])
827 		sigcatch++;
828 	sigs[sig] = 1;
829 
830 	return 0;
831 }
832 #endif /* !defined(__minix) */
833 
834 /*
835  * Actual mainloop.  This is called from a context which can block.
836  * It is called either from puffs_mainloop (indirectly, via
837  * puffs_cc_continue() or from puffs_cc_yield()).
838  */
839 void
puffs__theloop(struct puffs_cc * pcc)840 puffs__theloop(struct puffs_cc *pcc)
841 {
842 	struct puffs_usermount *pu = pcc->pcc_pu;
843 #if !defined(__minix)
844 	struct puffs_framectrl *pfctrl;
845 	struct puffs_fctrl_io *fio;
846 	struct kevent *curev;
847 	size_t nchanges;
848 	int ndone;
849 #endif /* !defined(__minix) */
850 
851 #if !defined(__minix)
852 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
853 #else
854 	do {
855 #endif /* !defined(__minix) */
856 
857 		/*
858 		 * Schedule existing requests.
859 		 */
860 		while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
861 			TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
862 			puffs__goto(pcc);
863 		}
864 
865 		if (pu->pu_ml_lfn)
866 			pu->pu_ml_lfn(pu);
867 
868 #if !defined(__minix)
869 		/* XXX: can we still do these optimizations? */
870 #if 0
871 		/*
872 		 * Do this here, because:
873 		 *  a) loopfunc might generate some results
874 		 *  b) it's still "after" event handling (except for round 1)
875 		 */
876 		if (puffs_req_putput(ppr) == -1)
877 			goto out;
878 		puffs_req_resetput(ppr);
879 
880 		/* micro optimization: skip kevent syscall if possible */
881 		if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
882 		    && (pu->pu_state & PU_ASYNCFD) == 0) {
883 			pfctrl = XXX->fctrl;
884 			puffs_framev_input(pu, pfctrl, XXX);
885 			continue;
886 		}
887 #endif
888 
889 		/* else: do full processing */
890 		/* Don't bother worrying about O(n) for now */
891 		LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
892 			if (fio->stat & FIO_WRGONE)
893 				continue;
894 
895 			pfctrl = fio->fctrl;
896 
897 			/*
898 			 * Try to write out everything to avoid the
899 			 * need for enabling EVFILT_WRITE.  The likely
900 			 * case is that we can fit everything into the
901 			 * socket buffer.
902 			 */
903 			puffs__framev_output(pu, pfctrl, fio);
904 		}
905 
906 		/*
907 		 * Build list of which to enable/disable in writecheck.
908 		 */
909 		nchanges = 0;
910 		LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
911 			if (fio->stat & FIO_WRGONE)
912 				continue;
913 
914 			/* en/disable write checks for kqueue as needed */
915 			assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
916 			if (FIO_EN_WRITE(fio)) {
917 				EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
918 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
919 				    (uintptr_t)fio);
920 				fio->stat |= FIO_WR;
921 				nchanges++;
922 			}
923 			if (FIO_RM_WRITE(fio)) {
924 				EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
925 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
926 				    (uintptr_t)fio);
927 				fio->stat &= ~FIO_WR;
928 				nchanges++;
929 			}
930 		}
931 
932 		ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
933 		    pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
934 
935 		if (ndone == -1) {
936 			if (errno != EINTR)
937 				break;
938 			else
939 				continue;
940 		}
941 
942 		/* uoptimize */
943 		if (ndone == 0)
944 			continue;
945 
946 		/* iterate over the results */
947 		for (curev = pu->pu_evs; ndone--; curev++) {
948 			int what;
949 
950 #if 0
951 			/* get & possibly dispatch events from kernel */
952 			if (curev->ident == puffsfd) {
953 				if (puffs_req_handle(pgr, ppr, 0) == -1)
954 					goto out;
955 				continue;
956 			}
957 #endif
958 
959 			fio = (void *)curev->udata;
960 			if (__predict_true(fio))
961 				pfctrl = fio->fctrl;
962 			else
963 				pfctrl = NULL;
964 			if (curev->flags & EV_ERROR) {
965 				assert(curev->filter == EVFILT_WRITE);
966 				fio->stat &= ~FIO_WR;
967 
968 				/* XXX: how to know if it's a transient error */
969 				puffs__framev_writeclose(pu, fio,
970 				    (int)curev->data);
971 				puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
972 				continue;
973 			}
974 
975 			what = 0;
976 			if (curev->filter == EVFILT_READ) {
977 				puffs__framev_input(pu, pfctrl, fio);
978 				what |= PUFFS_FBIO_READ;
979 			}
980 
981 			else if (curev->filter == EVFILT_WRITE) {
982 				puffs__framev_output(pu, pfctrl, fio);
983 				what |= PUFFS_FBIO_WRITE;
984 			}
985 
986 			else if (__predict_false(curev->filter==EVFILT_SIGNAL)){
987 				if ((pu->pu_state & PU_DONEXIT) == 0) {
988 					PU_SETSFLAG(pu, PU_DONEXIT);
989 					puffs_exit(pu, 0);
990 				}
991 			}
992 			if (what)
993 				puffs__framev_notify(fio, what);
994 		}
995 
996 		/*
997 		 * Really free fd's now that we don't have references
998 		 * to them.
999 		 */
1000 		while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
1001 			LIST_REMOVE(fio, fio_entries);
1002 			free(fio);
1003 		}
1004 #endif /* !defined(__minix) */
1005 	}
1006 #if defined(__minix)
1007 		while (lpuffs_pump());
1008 #endif /* defined(__minix) */
1009 
1010 	if (puffs__cc_restoremain(pu) == -1)
1011 		warn("cannot restore main context.  impending doom");
1012 }
1013 int
1014 puffs_mainloop(struct puffs_usermount *pu)
1015 {
1016 #if !defined(__minix)
1017 	struct puffs_fctrl_io *fio;
1018 #endif /* !defined(__minix) */
1019 	struct puffs_cc *pcc;
1020 #if !defined(__minix)
1021 	struct kevent *curev;
1022 	size_t nevs;
1023 	int sverrno, i;
1024 #else
1025 	int sverrno;
1026 #endif /* !defined(__minix) */
1027 
1028 	assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
1029 
1030 #if !defined(__minix)
1031 	pu->pu_kq = kqueue();
1032 	if (pu->pu_kq == -1)
1033 		goto out;
1034 #endif /* !defined(__minix) */
1035 	pu->pu_state |= PU_HASKQ;
1036 
1037 #if !defined(__minix)
1038 	puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
1039 	if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
1040 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
1041 	    &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
1042 		goto out;
1043 
1044 	nevs = pu->pu_nevs + sigcatch;
1045 	curev = realloc(pu->pu_evs, nevs * sizeof(struct kevent));
1046 	if (curev == NULL)
1047 		goto out;
1048 	pu->pu_evs = curev;
1049 	pu->pu_nevs = nevs;
1050 
1051 	LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
1052 		EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
1053 		    0, 0, (uintptr_t)fio);
1054 		curev++;
1055 		EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
1056 		    0, 0, (uintptr_t)fio);
1057 		curev++;
1058 	}
1059 	for (i = 0; i < NSIG; i++) {
1060 		if (sigs[i]) {
1061 			EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
1062 			    0, 0, 0);
1063 			curev++;
1064 		}
1065 	}
1066 	assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
1067 	if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
1068 		goto out;
1069 #endif /* !defined(__minix) */
1070 
1071 	pu->pu_state |= PU_INLOOP;
1072 
1073 	/*
1074 	 * Create alternate execution context and jump to it.  Note
1075 	 * that we come "out" of savemain twice.  Where we come out
1076 	 * of it depends on the architecture.  If the return address is
1077 	 * stored on the stack, we jump out from puffs_cc_continue(),
1078 	 * for a register return address from puffs__cc_savemain().
1079 	 * PU_MAINRESTORE makes sure we DTRT in both cases.
1080 	 */
1081 	if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
1082 		goto out;
1083 	}
1084 
1085 #if 0
1086 	if (puffs__cc_savemain(pu) == -1) {
1087 		goto out;
1088 	}
1089 #else
1090 	/*
1091 	 * XXX
1092 	 * puffs__cc_savemain() uses getcontext() and then returns.
1093 	 * the caller (this function) may overwrite the stack frame
1094 	 * of puffs__cc_savemain(), so when we call setcontext() later and
1095 	 * return from puffs__cc_savemain() again, the return address or
1096 	 * saved stack pointer can be garbage.
1097 	 * avoid this by calling getcontext() directly here.
1098 	 */
1099 	extern int puffs_fakecc;
1100 	if (!puffs_fakecc) {
1101 		PU_CLRSFLAG(pu, PU_MAINRESTORE);
1102 		if (getcontext(&pu->pu_mainctx) == -1) {
1103 			goto out;
1104 		}
1105 	}
1106 #endif
1107 
1108 	if ((pu->pu_state & PU_MAINRESTORE) == 0)
1109 		puffs_cc_continue(pcc);
1110 
1111 	finalpush(pu);
1112 	errno = 0;
1113 
1114  out:
1115 	/* store the real error for a while */
1116 	sverrno = errno;
1117 
1118 	errno = sverrno;
1119 	if (errno)
1120 		return -1;
1121 	else
1122 		return 0;
1123 }
1124