1*0a6a1f1dSLionel Sambuc /* $NetBSD: puffs.c,v 1.120 2015/06/17 00:15:26 christos Exp $ */
2490e0de5SThomas Veerman
3490e0de5SThomas Veerman /*
4490e0de5SThomas Veerman * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
5490e0de5SThomas Veerman *
6490e0de5SThomas Veerman * Development of this software was supported by the
7490e0de5SThomas Veerman * Google Summer of Code program and the Ulla Tuominen Foundation.
8490e0de5SThomas Veerman * The Google SoC project was mentored by Bill Studenmund.
9490e0de5SThomas Veerman *
10490e0de5SThomas Veerman * Redistribution and use in source and binary forms, with or without
11490e0de5SThomas Veerman * modification, are permitted provided that the following conditions
12490e0de5SThomas Veerman * are met:
13490e0de5SThomas Veerman * 1. Redistributions of source code must retain the above copyright
14490e0de5SThomas Veerman * notice, this list of conditions and the following disclaimer.
15490e0de5SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
16490e0de5SThomas Veerman * notice, this list of conditions and the following disclaimer in the
17490e0de5SThomas Veerman * documentation and/or other materials provided with the distribution.
18490e0de5SThomas Veerman *
19490e0de5SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20490e0de5SThomas Veerman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21490e0de5SThomas Veerman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22490e0de5SThomas Veerman * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23490e0de5SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24490e0de5SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25490e0de5SThomas Veerman * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26490e0de5SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27490e0de5SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28490e0de5SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29490e0de5SThomas Veerman * SUCH DAMAGE.
30490e0de5SThomas Veerman */
31490e0de5SThomas Veerman
32490e0de5SThomas Veerman #include <sys/cdefs.h>
33490e0de5SThomas Veerman #if !defined(lint)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: puffs.c,v 1.120 2015/06/17 00:15:26 christos Exp $");
35490e0de5SThomas Veerman #endif /* !lint */
36490e0de5SThomas Veerman
37490e0de5SThomas Veerman #include <sys/param.h>
38490e0de5SThomas Veerman #include <sys/mount.h>
39490e0de5SThomas Veerman
40490e0de5SThomas Veerman #include <assert.h>
41490e0de5SThomas Veerman #include <err.h>
42490e0de5SThomas Veerman #include <errno.h>
43490e0de5SThomas Veerman #include <fcntl.h>
4484d9c625SLionel Sambuc #include <mntopts.h>
45490e0de5SThomas Veerman #include <paths.h>
46*0a6a1f1dSLionel Sambuc #if !defined(__minix)
47ba736c79SDavid van Moolenbroek #include <pthread.h>
48*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
4984d9c625SLionel Sambuc #include <puffs.h>
50490e0de5SThomas Veerman #include <stdio.h>
51490e0de5SThomas Veerman #include <stdlib.h>
52490e0de5SThomas Veerman #include <string.h>
53490e0de5SThomas Veerman #include <syslog.h>
54490e0de5SThomas Veerman #include <unistd.h>
55490e0de5SThomas Veerman
56490e0de5SThomas Veerman #include "puffs_priv.h"
57490e0de5SThomas Veerman
5884d9c625SLionel Sambuc /* Most file systems want this for opts, so just give it to them */
5984d9c625SLionel Sambuc const struct mntopt puffsmopts[] = {
6084d9c625SLionel Sambuc MOPT_STDOPTS,
6184d9c625SLionel Sambuc PUFFSMOPT_STD,
6284d9c625SLionel Sambuc MOPT_NULL,
6384d9c625SLionel Sambuc };
64ba736c79SDavid van Moolenbroek
65*0a6a1f1dSLionel Sambuc #if !defined(__minix)
66490e0de5SThomas Veerman pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
67*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
68490e0de5SThomas Veerman
69490e0de5SThomas Veerman #define FILLOP(lower, upper) \
70490e0de5SThomas Veerman do { \
71490e0de5SThomas Veerman if (pops->puffs_node_##lower) \
72490e0de5SThomas Veerman opmask[PUFFS_VN_##upper] = 1; \
73490e0de5SThomas Veerman } while (/*CONSTCOND*/0)
74490e0de5SThomas Veerman static void
fillvnopmask(struct puffs_ops * pops,struct puffs_kargs * pa)7584d9c625SLionel Sambuc fillvnopmask(struct puffs_ops *pops, struct puffs_kargs *pa)
76490e0de5SThomas Veerman {
7784d9c625SLionel Sambuc uint8_t *opmask = pa->pa_vnopmask;
78490e0de5SThomas Veerman
7984d9c625SLionel Sambuc memset(opmask, 0, sizeof(pa->pa_vnopmask));
80490e0de5SThomas Veerman
81490e0de5SThomas Veerman FILLOP(create, CREATE);
82490e0de5SThomas Veerman FILLOP(mknod, MKNOD);
83490e0de5SThomas Veerman FILLOP(open, OPEN);
84490e0de5SThomas Veerman FILLOP(close, CLOSE);
85490e0de5SThomas Veerman FILLOP(access, ACCESS);
86490e0de5SThomas Veerman FILLOP(getattr, GETATTR);
87490e0de5SThomas Veerman FILLOP(setattr, SETATTR);
8884d9c625SLionel Sambuc FILLOP(poll, POLL);
89490e0de5SThomas Veerman FILLOP(mmap, MMAP);
90490e0de5SThomas Veerman FILLOP(fsync, FSYNC);
91490e0de5SThomas Veerman FILLOP(seek, SEEK);
92490e0de5SThomas Veerman FILLOP(remove, REMOVE);
93490e0de5SThomas Veerman FILLOP(link, LINK);
94490e0de5SThomas Veerman FILLOP(rename, RENAME);
95490e0de5SThomas Veerman FILLOP(mkdir, MKDIR);
96490e0de5SThomas Veerman FILLOP(rmdir, RMDIR);
97490e0de5SThomas Veerman FILLOP(symlink, SYMLINK);
98490e0de5SThomas Veerman FILLOP(readdir, READDIR);
99490e0de5SThomas Veerman FILLOP(readlink, READLINK);
100490e0de5SThomas Veerman FILLOP(reclaim, RECLAIM);
101490e0de5SThomas Veerman FILLOP(inactive, INACTIVE);
102490e0de5SThomas Veerman FILLOP(print, PRINT);
103490e0de5SThomas Veerman FILLOP(read, READ);
104490e0de5SThomas Veerman FILLOP(write, WRITE);
105ba736c79SDavid van Moolenbroek FILLOP(advlock, ADVLOCK);
106490e0de5SThomas Veerman FILLOP(abortop, ABORTOP);
107ba736c79SDavid van Moolenbroek FILLOP(pathconf, PATHCONF);
108ba736c79SDavid van Moolenbroek
109ba736c79SDavid van Moolenbroek FILLOP(getextattr, GETEXTATTR);
110ba736c79SDavid van Moolenbroek FILLOP(setextattr, SETEXTATTR);
111ba736c79SDavid van Moolenbroek FILLOP(listextattr, LISTEXTATTR);
112ba736c79SDavid van Moolenbroek FILLOP(deleteextattr, DELETEEXTATTR);
113*0a6a1f1dSLionel Sambuc FILLOP(fallocate, FALLOCATE);
114*0a6a1f1dSLionel Sambuc FILLOP(fdiscard, FDISCARD);
115490e0de5SThomas Veerman }
116490e0de5SThomas Veerman #undef FILLOP
117490e0de5SThomas Veerman
118ba736c79SDavid van Moolenbroek /*
119ba736c79SDavid van Moolenbroek * Go over all framev entries and write everything we can. This is
120ba736c79SDavid van Moolenbroek * mostly for the benefit of delivering "unmount" to the kernel.
121ba736c79SDavid van Moolenbroek */
122ba736c79SDavid van Moolenbroek static void
finalpush(struct puffs_usermount * pu)123ba736c79SDavid van Moolenbroek finalpush(struct puffs_usermount *pu)
124ba736c79SDavid van Moolenbroek {
125*0a6a1f1dSLionel Sambuc #if !defined(__minix)
126ba736c79SDavid van Moolenbroek struct puffs_fctrl_io *fio;
127ba736c79SDavid van Moolenbroek
128ba736c79SDavid van Moolenbroek LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
129ba736c79SDavid van Moolenbroek if (fio->stat & FIO_WRGONE)
130ba736c79SDavid van Moolenbroek continue;
131ba736c79SDavid van Moolenbroek
132ba736c79SDavid van Moolenbroek puffs__framev_output(pu, fio->fctrl, fio);
133ba736c79SDavid van Moolenbroek }
134*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
135ba736c79SDavid van Moolenbroek }
136490e0de5SThomas Veerman
137490e0de5SThomas Veerman /*ARGSUSED*/
138ba736c79SDavid van Moolenbroek void
puffs_kernerr_abort(struct puffs_usermount * pu,uint8_t type,int error,const char * str,puffs_cookie_t cookie)139ba736c79SDavid van Moolenbroek puffs_kernerr_abort(struct puffs_usermount *pu, uint8_t type,
140490e0de5SThomas Veerman int error, const char *str, puffs_cookie_t cookie)
141490e0de5SThomas Veerman {
142490e0de5SThomas Veerman
143*0a6a1f1dSLionel Sambuc #if !defined(__minix)
144*0a6a1f1dSLionel Sambuc warnx("abort: type %d, error %d, cookie %p (%s)",
145*0a6a1f1dSLionel Sambuc #else
146490e0de5SThomas Veerman lpuffs_debug("abort: type %d, error %d, cookie %p (%s)\n",
147*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
148490e0de5SThomas Veerman type, error, cookie, str);
149490e0de5SThomas Veerman abort();
150490e0de5SThomas Veerman }
151490e0de5SThomas Veerman
152ba736c79SDavid van Moolenbroek /*ARGSUSED*/
153ba736c79SDavid van Moolenbroek void
puffs_kernerr_log(struct puffs_usermount * pu,uint8_t type,int error,const char * str,puffs_cookie_t cookie)154ba736c79SDavid van Moolenbroek puffs_kernerr_log(struct puffs_usermount *pu, uint8_t type,
155ba736c79SDavid van Moolenbroek int error, const char *str, puffs_cookie_t cookie)
156ba736c79SDavid van Moolenbroek {
157ba736c79SDavid van Moolenbroek
158*0a6a1f1dSLionel Sambuc syslog(LOG_WARNING, "kernel: type %d, error %d, cookie %p (%s)",
159ba736c79SDavid van Moolenbroek type, error, cookie, str);
160ba736c79SDavid van Moolenbroek }
161ba736c79SDavid van Moolenbroek
162*0a6a1f1dSLionel Sambuc #if !defined(__minix)
163ba736c79SDavid van Moolenbroek int
puffs_getselectable(struct puffs_usermount * pu)164ba736c79SDavid van Moolenbroek puffs_getselectable(struct puffs_usermount *pu)
165ba736c79SDavid van Moolenbroek {
166ba736c79SDavid van Moolenbroek
167ba736c79SDavid van Moolenbroek return pu->pu_fd;
168ba736c79SDavid van Moolenbroek }
169ba736c79SDavid van Moolenbroek
170ba736c79SDavid van Moolenbroek uint64_t
puffs__nextreq(struct puffs_usermount * pu)171ba736c79SDavid van Moolenbroek puffs__nextreq(struct puffs_usermount *pu)
172ba736c79SDavid van Moolenbroek {
173ba736c79SDavid van Moolenbroek uint64_t rv;
174ba736c79SDavid van Moolenbroek
175ba736c79SDavid van Moolenbroek PU_LOCK();
176ba736c79SDavid van Moolenbroek rv = pu->pu_nextreq++ | (uint64_t)1<<63;
177ba736c79SDavid van Moolenbroek PU_UNLOCK();
178ba736c79SDavid van Moolenbroek
179ba736c79SDavid van Moolenbroek return rv;
180ba736c79SDavid van Moolenbroek }
181ba736c79SDavid van Moolenbroek
182ba736c79SDavid van Moolenbroek int
puffs_setblockingmode(struct puffs_usermount * pu,int mode)183ba736c79SDavid van Moolenbroek puffs_setblockingmode(struct puffs_usermount *pu, int mode)
184ba736c79SDavid van Moolenbroek {
185ba736c79SDavid van Moolenbroek int rv, x;
186ba736c79SDavid van Moolenbroek
187ba736c79SDavid van Moolenbroek assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
188ba736c79SDavid van Moolenbroek
189ba736c79SDavid van Moolenbroek if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
190ba736c79SDavid van Moolenbroek errno = EINVAL;
191ba736c79SDavid van Moolenbroek return -1;
192ba736c79SDavid van Moolenbroek }
193ba736c79SDavid van Moolenbroek
194ba736c79SDavid van Moolenbroek x = mode;
195ba736c79SDavid van Moolenbroek rv = ioctl(pu->pu_fd, FIONBIO, &x);
196ba736c79SDavid van Moolenbroek
197ba736c79SDavid van Moolenbroek if (rv == 0) {
198ba736c79SDavid van Moolenbroek if (mode == PUFFSDEV_BLOCK)
199ba736c79SDavid van Moolenbroek pu->pu_state &= ~PU_ASYNCFD;
200ba736c79SDavid van Moolenbroek else
201ba736c79SDavid van Moolenbroek pu->pu_state |= PU_ASYNCFD;
202ba736c79SDavid van Moolenbroek }
203ba736c79SDavid van Moolenbroek
204ba736c79SDavid van Moolenbroek return rv;
205ba736c79SDavid van Moolenbroek }
206*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
207490e0de5SThomas Veerman
208490e0de5SThomas Veerman int
puffs_getstate(struct puffs_usermount * pu)209490e0de5SThomas Veerman puffs_getstate(struct puffs_usermount *pu)
210490e0de5SThomas Veerman {
211490e0de5SThomas Veerman
212490e0de5SThomas Veerman return pu->pu_state & PU_STATEMASK;
213490e0de5SThomas Veerman }
214490e0de5SThomas Veerman
215490e0de5SThomas Veerman void
puffs_setstacksize(struct puffs_usermount * pu,size_t ss)216490e0de5SThomas Veerman puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
217490e0de5SThomas Veerman {
21884d9c625SLionel Sambuc long psize, minsize;
219490e0de5SThomas Veerman int stackshift;
220490e0de5SThomas Veerman int bonus;
221490e0de5SThomas Veerman
222490e0de5SThomas Veerman assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
223490e0de5SThomas Veerman
224490e0de5SThomas Veerman psize = sysconf(_SC_PAGESIZE);
225490e0de5SThomas Veerman minsize = 4*psize;
22684d9c625SLionel Sambuc if (ss < (size_t)minsize || ss == PUFFS_STACKSIZE_MIN) {
227490e0de5SThomas Veerman if (ss != PUFFS_STACKSIZE_MIN)
228*0a6a1f1dSLionel Sambuc #if !defined(__minix)
229*0a6a1f1dSLionel Sambuc warnx("%s: adjusting " "stacksize to minimum %ld",
230*0a6a1f1dSLionel Sambuc __func__, minsize);
231*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
232490e0de5SThomas Veerman ss = 4*psize;
233490e0de5SThomas Veerman }
234490e0de5SThomas Veerman
235490e0de5SThomas Veerman stackshift = -1;
236490e0de5SThomas Veerman bonus = 0;
237490e0de5SThomas Veerman while (ss) {
238490e0de5SThomas Veerman if (ss & 0x1)
239490e0de5SThomas Veerman bonus++;
240490e0de5SThomas Veerman ss >>= 1;
241490e0de5SThomas Veerman stackshift++;
242490e0de5SThomas Veerman }
243490e0de5SThomas Veerman if (bonus > 1) {
244490e0de5SThomas Veerman stackshift++;
245*0a6a1f1dSLionel Sambuc #if !defined(__minix)
246*0a6a1f1dSLionel Sambuc warnx("%s: using next power of two: %d", __func__,
247*0a6a1f1dSLionel Sambuc 1 << stackshift);
248*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
249490e0de5SThomas Veerman }
250490e0de5SThomas Veerman
251490e0de5SThomas Veerman pu->pu_cc_stackshift = stackshift;
252490e0de5SThomas Veerman }
253490e0de5SThomas Veerman
254490e0de5SThomas Veerman struct puffs_pathobj *
puffs_getrootpathobj(struct puffs_usermount * pu)255490e0de5SThomas Veerman puffs_getrootpathobj(struct puffs_usermount *pu)
256490e0de5SThomas Veerman {
257490e0de5SThomas Veerman struct puffs_node *pnr;
258490e0de5SThomas Veerman
259490e0de5SThomas Veerman pnr = pu->pu_pn_root;
260490e0de5SThomas Veerman if (pnr == NULL) {
261490e0de5SThomas Veerman errno = ENOENT;
262490e0de5SThomas Veerman return NULL;
263490e0de5SThomas Veerman }
264490e0de5SThomas Veerman
265490e0de5SThomas Veerman return &pnr->pn_po;
266490e0de5SThomas Veerman }
267490e0de5SThomas Veerman
268490e0de5SThomas Veerman void
puffs_setroot(struct puffs_usermount * pu,struct puffs_node * pn)269490e0de5SThomas Veerman puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
270490e0de5SThomas Veerman {
271490e0de5SThomas Veerman
272490e0de5SThomas Veerman pu->pu_pn_root = pn;
273490e0de5SThomas Veerman }
274490e0de5SThomas Veerman
275490e0de5SThomas Veerman struct puffs_node *
puffs_getroot(struct puffs_usermount * pu)276490e0de5SThomas Veerman puffs_getroot(struct puffs_usermount *pu)
277490e0de5SThomas Veerman {
278490e0de5SThomas Veerman
279490e0de5SThomas Veerman return pu->pu_pn_root;
280490e0de5SThomas Veerman }
281490e0de5SThomas Veerman
282490e0de5SThomas Veerman void
puffs_setrootinfo(struct puffs_usermount * pu,enum vtype vt,vsize_t vsize,dev_t rdev)283490e0de5SThomas Veerman puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
284490e0de5SThomas Veerman vsize_t vsize, dev_t rdev)
285490e0de5SThomas Veerman {
286490e0de5SThomas Veerman struct puffs_kargs *pargs = pu->pu_kargp;
287490e0de5SThomas Veerman
288490e0de5SThomas Veerman if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
289*0a6a1f1dSLionel Sambuc warnx("%s: call has effect only before mount", __func__);
290490e0de5SThomas Veerman return;
291490e0de5SThomas Veerman }
292490e0de5SThomas Veerman
293490e0de5SThomas Veerman pargs->pa_root_vtype = vt;
294490e0de5SThomas Veerman pargs->pa_root_vsize = vsize;
295490e0de5SThomas Veerman pargs->pa_root_rdev = rdev;
296490e0de5SThomas Veerman }
297490e0de5SThomas Veerman
298490e0de5SThomas Veerman void *
puffs_getspecific(struct puffs_usermount * pu)299490e0de5SThomas Veerman puffs_getspecific(struct puffs_usermount *pu)
300490e0de5SThomas Veerman {
301490e0de5SThomas Veerman
302490e0de5SThomas Veerman return pu->pu_privdata;
303490e0de5SThomas Veerman }
304490e0de5SThomas Veerman
305490e0de5SThomas Veerman void
puffs_setspecific(struct puffs_usermount * pu,void * privdata)306490e0de5SThomas Veerman puffs_setspecific(struct puffs_usermount *pu, void *privdata)
307490e0de5SThomas Veerman {
308490e0de5SThomas Veerman
309490e0de5SThomas Veerman pu->pu_privdata = privdata;
310490e0de5SThomas Veerman }
311490e0de5SThomas Veerman
312490e0de5SThomas Veerman void
puffs_setmntinfo(struct puffs_usermount * pu,const char * mntfromname,const char * puffsname)313490e0de5SThomas Veerman puffs_setmntinfo(struct puffs_usermount *pu,
314490e0de5SThomas Veerman const char *mntfromname, const char *puffsname)
315490e0de5SThomas Veerman {
316490e0de5SThomas Veerman struct puffs_kargs *pargs = pu->pu_kargp;
317490e0de5SThomas Veerman
318490e0de5SThomas Veerman (void)strlcpy(pargs->pa_mntfromname, mntfromname,
319490e0de5SThomas Veerman sizeof(pargs->pa_mntfromname));
320490e0de5SThomas Veerman (void)strlcpy(pargs->pa_typename, puffsname,
321490e0de5SThomas Veerman sizeof(pargs->pa_typename));
322490e0de5SThomas Veerman }
323490e0de5SThomas Veerman
324490e0de5SThomas Veerman size_t
puffs_getmaxreqlen(struct puffs_usermount * pu)325490e0de5SThomas Veerman puffs_getmaxreqlen(struct puffs_usermount *pu)
326490e0de5SThomas Veerman {
327490e0de5SThomas Veerman
328490e0de5SThomas Veerman return pu->pu_maxreqlen;
329490e0de5SThomas Veerman }
330490e0de5SThomas Veerman
331490e0de5SThomas Veerman void
puffs_setmaxreqlen(struct puffs_usermount * pu,size_t reqlen)332490e0de5SThomas Veerman puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
333490e0de5SThomas Veerman {
334490e0de5SThomas Veerman
335490e0de5SThomas Veerman if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
336*0a6a1f1dSLionel Sambuc warnx("%s: call has effect only before mount", __func__);
337490e0de5SThomas Veerman
338490e0de5SThomas Veerman pu->pu_kargp->pa_maxmsglen = reqlen;
339490e0de5SThomas Veerman }
340490e0de5SThomas Veerman
341490e0de5SThomas Veerman void
puffs_setfhsize(struct puffs_usermount * pu,size_t fhsize,int flags)342490e0de5SThomas Veerman puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
343490e0de5SThomas Veerman {
344490e0de5SThomas Veerman
345490e0de5SThomas Veerman if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
346*0a6a1f1dSLionel Sambuc warnx("%s: call has effect only before mount", __func__);
347490e0de5SThomas Veerman
348490e0de5SThomas Veerman pu->pu_kargp->pa_fhsize = fhsize;
349490e0de5SThomas Veerman pu->pu_kargp->pa_fhflags = flags;
350490e0de5SThomas Veerman }
351490e0de5SThomas Veerman
352490e0de5SThomas Veerman void
puffs_setncookiehash(struct puffs_usermount * pu,int nhash)353490e0de5SThomas Veerman puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
354490e0de5SThomas Veerman {
355490e0de5SThomas Veerman
356490e0de5SThomas Veerman if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
357*0a6a1f1dSLionel Sambuc warnx("%s: call has effect only before mount", __func__);
358490e0de5SThomas Veerman
359490e0de5SThomas Veerman pu->pu_kargp->pa_nhashbuckets = nhash;
360490e0de5SThomas Veerman }
361490e0de5SThomas Veerman
362490e0de5SThomas Veerman void
puffs_set_pathbuild(struct puffs_usermount * pu,pu_pathbuild_fn fn)363490e0de5SThomas Veerman puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
364490e0de5SThomas Veerman {
365490e0de5SThomas Veerman
366490e0de5SThomas Veerman pu->pu_pathbuild = fn;
367490e0de5SThomas Veerman }
368490e0de5SThomas Veerman
369490e0de5SThomas Veerman void
puffs_set_pathtransform(struct puffs_usermount * pu,pu_pathtransform_fn fn)370490e0de5SThomas Veerman puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
371490e0de5SThomas Veerman {
372490e0de5SThomas Veerman
373490e0de5SThomas Veerman pu->pu_pathtransform = fn;
374490e0de5SThomas Veerman }
375490e0de5SThomas Veerman
376490e0de5SThomas Veerman void
puffs_set_pathcmp(struct puffs_usermount * pu,pu_pathcmp_fn fn)377490e0de5SThomas Veerman puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
378490e0de5SThomas Veerman {
379490e0de5SThomas Veerman
380490e0de5SThomas Veerman pu->pu_pathcmp = fn;
381490e0de5SThomas Veerman }
382490e0de5SThomas Veerman
383490e0de5SThomas Veerman void
puffs_set_pathfree(struct puffs_usermount * pu,pu_pathfree_fn fn)384490e0de5SThomas Veerman puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
385490e0de5SThomas Veerman {
386490e0de5SThomas Veerman
387490e0de5SThomas Veerman pu->pu_pathfree = fn;
388490e0de5SThomas Veerman }
389490e0de5SThomas Veerman
390490e0de5SThomas Veerman void
puffs_set_namemod(struct puffs_usermount * pu,pu_namemod_fn fn)391490e0de5SThomas Veerman puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
392490e0de5SThomas Veerman {
393490e0de5SThomas Veerman
394490e0de5SThomas Veerman pu->pu_namemod = fn;
395490e0de5SThomas Veerman }
396490e0de5SThomas Veerman
397490e0de5SThomas Veerman void
puffs_set_errnotify(struct puffs_usermount * pu,pu_errnotify_fn fn)398490e0de5SThomas Veerman puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
399490e0de5SThomas Veerman {
400490e0de5SThomas Veerman
401490e0de5SThomas Veerman pu->pu_errnotify = fn;
402490e0de5SThomas Veerman }
403490e0de5SThomas Veerman
404490e0de5SThomas Veerman void
puffs_set_cmap(struct puffs_usermount * pu,pu_cmap_fn fn)405490e0de5SThomas Veerman puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
406490e0de5SThomas Veerman {
407490e0de5SThomas Veerman
408490e0de5SThomas Veerman pu->pu_cmap = fn;
409490e0de5SThomas Veerman }
410490e0de5SThomas Veerman
411490e0de5SThomas Veerman void
puffs_ml_setloopfn(struct puffs_usermount * pu,puffs_ml_loop_fn lfn)412490e0de5SThomas Veerman puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
413490e0de5SThomas Veerman {
414490e0de5SThomas Veerman
415490e0de5SThomas Veerman pu->pu_ml_lfn = lfn;
416490e0de5SThomas Veerman }
417490e0de5SThomas Veerman
418490e0de5SThomas Veerman void
puffs_ml_settimeout(struct puffs_usermount * pu,struct timespec * ts)419490e0de5SThomas Veerman puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
420490e0de5SThomas Veerman {
421490e0de5SThomas Veerman
422490e0de5SThomas Veerman if (ts == NULL) {
423490e0de5SThomas Veerman pu->pu_ml_timep = NULL;
424490e0de5SThomas Veerman } else {
425490e0de5SThomas Veerman pu->pu_ml_timeout = *ts;
426490e0de5SThomas Veerman pu->pu_ml_timep = &pu->pu_ml_timeout;
427490e0de5SThomas Veerman }
428490e0de5SThomas Veerman }
429490e0de5SThomas Veerman
430490e0de5SThomas Veerman void
puffs_set_prepost(struct puffs_usermount * pu,pu_prepost_fn pre,pu_prepost_fn pst)431490e0de5SThomas Veerman puffs_set_prepost(struct puffs_usermount *pu,
432490e0de5SThomas Veerman pu_prepost_fn pre, pu_prepost_fn pst)
433490e0de5SThomas Veerman {
434490e0de5SThomas Veerman
435490e0de5SThomas Veerman pu->pu_oppre = pre;
436490e0de5SThomas Veerman pu->pu_oppost = pst;
437490e0de5SThomas Veerman }
438490e0de5SThomas Veerman
439*0a6a1f1dSLionel Sambuc #if !defined(__minix)
440ba736c79SDavid van Moolenbroek void
puffs_setback(struct puffs_cc * pcc,int whatback)441ba736c79SDavid van Moolenbroek puffs_setback(struct puffs_cc *pcc, int whatback)
442ba736c79SDavid van Moolenbroek {
443ba736c79SDavid van Moolenbroek struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
444ba736c79SDavid van Moolenbroek
445ba736c79SDavid van Moolenbroek assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
446ba736c79SDavid van Moolenbroek preq->preq_optype == PUFFS_VN_OPEN ||
447ba736c79SDavid van Moolenbroek preq->preq_optype == PUFFS_VN_MMAP ||
448ba736c79SDavid van Moolenbroek preq->preq_optype == PUFFS_VN_REMOVE ||
449ba736c79SDavid van Moolenbroek preq->preq_optype == PUFFS_VN_RMDIR ||
450ba736c79SDavid van Moolenbroek preq->preq_optype == PUFFS_VN_INACTIVE));
451ba736c79SDavid van Moolenbroek
452ba736c79SDavid van Moolenbroek preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
453ba736c79SDavid van Moolenbroek }
454ba736c79SDavid van Moolenbroek
455ba736c79SDavid van Moolenbroek int
puffs_daemon(struct puffs_usermount * pu,int nochdir,int noclose)456ba736c79SDavid van Moolenbroek puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
457ba736c79SDavid van Moolenbroek {
458ba736c79SDavid van Moolenbroek long int n;
459ba736c79SDavid van Moolenbroek int parent, value, fd;
460ba736c79SDavid van Moolenbroek
461ba736c79SDavid van Moolenbroek if (pipe(pu->pu_dpipe) == -1)
462ba736c79SDavid van Moolenbroek return -1;
463ba736c79SDavid van Moolenbroek
464ba736c79SDavid van Moolenbroek switch (fork()) {
465ba736c79SDavid van Moolenbroek case -1:
466ba736c79SDavid van Moolenbroek return -1;
467ba736c79SDavid van Moolenbroek case 0:
468ba736c79SDavid van Moolenbroek parent = 0;
469ba736c79SDavid van Moolenbroek break;
470ba736c79SDavid van Moolenbroek default:
471ba736c79SDavid van Moolenbroek parent = 1;
472ba736c79SDavid van Moolenbroek break;
473ba736c79SDavid van Moolenbroek }
474ba736c79SDavid van Moolenbroek pu->pu_state |= PU_PUFFSDAEMON;
475ba736c79SDavid van Moolenbroek
476ba736c79SDavid van Moolenbroek if (parent) {
477ba736c79SDavid van Moolenbroek close(pu->pu_dpipe[1]);
478ba736c79SDavid van Moolenbroek n = read(pu->pu_dpipe[0], &value, sizeof(int));
479ba736c79SDavid van Moolenbroek if (n == -1)
480ba736c79SDavid van Moolenbroek err(1, "puffs_daemon");
481ba736c79SDavid van Moolenbroek if (n != sizeof(value))
482ba736c79SDavid van Moolenbroek errx(1, "puffs_daemon got %ld bytes", n);
483ba736c79SDavid van Moolenbroek if (value) {
484ba736c79SDavid van Moolenbroek errno = value;
485ba736c79SDavid van Moolenbroek err(1, "puffs_daemon");
486ba736c79SDavid van Moolenbroek }
487ba736c79SDavid van Moolenbroek exit(0);
488ba736c79SDavid van Moolenbroek } else {
489ba736c79SDavid van Moolenbroek if (setsid() == -1)
490ba736c79SDavid van Moolenbroek goto fail;
491ba736c79SDavid van Moolenbroek
492ba736c79SDavid van Moolenbroek if (!nochdir)
493ba736c79SDavid van Moolenbroek chdir("/");
494ba736c79SDavid van Moolenbroek
495ba736c79SDavid van Moolenbroek if (!noclose) {
496ba736c79SDavid van Moolenbroek fd = open(_PATH_DEVNULL, O_RDWR, 0);
497ba736c79SDavid van Moolenbroek if (fd == -1)
498ba736c79SDavid van Moolenbroek goto fail;
499ba736c79SDavid van Moolenbroek dup2(fd, STDIN_FILENO);
500ba736c79SDavid van Moolenbroek dup2(fd, STDOUT_FILENO);
501ba736c79SDavid van Moolenbroek dup2(fd, STDERR_FILENO);
502ba736c79SDavid van Moolenbroek if (fd > STDERR_FILENO)
503ba736c79SDavid van Moolenbroek close(fd);
504ba736c79SDavid van Moolenbroek }
505ba736c79SDavid van Moolenbroek return 0;
506ba736c79SDavid van Moolenbroek }
507ba736c79SDavid van Moolenbroek
508ba736c79SDavid van Moolenbroek fail:
509ba736c79SDavid van Moolenbroek n = write(pu->pu_dpipe[1], &errno, sizeof(int));
510ba736c79SDavid van Moolenbroek assert(n == 4);
511ba736c79SDavid van Moolenbroek return -1;
512ba736c79SDavid van Moolenbroek }
513*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
514ba736c79SDavid van Moolenbroek
515ba736c79SDavid van Moolenbroek static void
shutdaemon(struct puffs_usermount * pu,int error)516ba736c79SDavid van Moolenbroek shutdaemon(struct puffs_usermount *pu, int error)
517ba736c79SDavid van Moolenbroek {
518*0a6a1f1dSLionel Sambuc #if !defined(__minix)
519ba736c79SDavid van Moolenbroek ssize_t n;
520ba736c79SDavid van Moolenbroek
521ba736c79SDavid van Moolenbroek n = write(pu->pu_dpipe[1], &error, sizeof(int));
522ba736c79SDavid van Moolenbroek assert(n == 4);
523ba736c79SDavid van Moolenbroek close(pu->pu_dpipe[0]);
524ba736c79SDavid van Moolenbroek close(pu->pu_dpipe[1]);
525*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
526ba736c79SDavid van Moolenbroek pu->pu_state &= ~PU_PUFFSDAEMON;
527ba736c79SDavid van Moolenbroek }
528ba736c79SDavid van Moolenbroek
529490e0de5SThomas Veerman int
puffs_mount(struct puffs_usermount * pu,const char * dir,int mntflags,puffs_cookie_t cookie)530490e0de5SThomas Veerman puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
531490e0de5SThomas Veerman puffs_cookie_t cookie)
532490e0de5SThomas Veerman {
533*0a6a1f1dSLionel Sambuc #if !defined(__minix)
534ba736c79SDavid van Moolenbroek int rv, fd, sverrno;
535ba736c79SDavid van Moolenbroek char *comfd;
536*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
537ba736c79SDavid van Moolenbroek
538490e0de5SThomas Veerman pu->pu_kargp->pa_root_cookie = cookie;
539490e0de5SThomas Veerman
540*0a6a1f1dSLionel Sambuc #if !defined(__minix)
541ba736c79SDavid van Moolenbroek /* XXXkludgehere */
542ba736c79SDavid van Moolenbroek /* kauth doesn't provide this service any longer */
543ba736c79SDavid van Moolenbroek if (geteuid() != 0)
544ba736c79SDavid van Moolenbroek mntflags |= MNT_NOSUID | MNT_NODEV;
545490e0de5SThomas Veerman
546ba736c79SDavid van Moolenbroek /*
547ba736c79SDavid van Moolenbroek * Undocumented... Well, documented only here.
548ba736c79SDavid van Moolenbroek *
549ba736c79SDavid van Moolenbroek * This is used for imaginative purposes. If the env variable is
550ba736c79SDavid van Moolenbroek * set, puffs_mount() doesn't do the regular mount procedure.
551ba736c79SDavid van Moolenbroek * Rather, it crams the mount data down the comfd and sets comfd as
552ba736c79SDavid van Moolenbroek * the puffs descriptor.
553ba736c79SDavid van Moolenbroek *
554ba736c79SDavid van Moolenbroek * This shouldn't be used unless you can read my mind ( ... or write
555ba736c79SDavid van Moolenbroek * it, not to mention execute it, but that's starting to get silly).
556ba736c79SDavid van Moolenbroek */
557ba736c79SDavid van Moolenbroek if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
558ba736c79SDavid van Moolenbroek size_t len;
559ba736c79SDavid van Moolenbroek
560ba736c79SDavid van Moolenbroek if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
561ba736c79SDavid van Moolenbroek errno = EINVAL;
562ba736c79SDavid van Moolenbroek rv = -1;
563ba736c79SDavid van Moolenbroek goto out;
564ba736c79SDavid van Moolenbroek }
565ba736c79SDavid van Moolenbroek /* check that what we got at least resembles an fd */
566ba736c79SDavid van Moolenbroek if (fcntl(pu->pu_fd, F_GETFL) == -1) {
567ba736c79SDavid van Moolenbroek rv = -1;
568ba736c79SDavid van Moolenbroek goto out;
569ba736c79SDavid van Moolenbroek }
570ba736c79SDavid van Moolenbroek
571ba736c79SDavid van Moolenbroek #define allwrite(buf, len) \
572ba736c79SDavid van Moolenbroek do { \
573ba736c79SDavid van Moolenbroek ssize_t al_rv; \
574ba736c79SDavid van Moolenbroek al_rv = write(pu->pu_fd, buf, len); \
575ba736c79SDavid van Moolenbroek if ((size_t)al_rv != len) { \
576ba736c79SDavid van Moolenbroek if (al_rv != -1) \
577ba736c79SDavid van Moolenbroek errno = EIO; \
578ba736c79SDavid van Moolenbroek rv = -1; \
579ba736c79SDavid van Moolenbroek goto out; \
580ba736c79SDavid van Moolenbroek } \
581ba736c79SDavid van Moolenbroek } while (/*CONSTCOND*/0)
582ba736c79SDavid van Moolenbroek len = strlen(dir)+1;
583ba736c79SDavid van Moolenbroek allwrite(&len, sizeof(len));
584ba736c79SDavid van Moolenbroek allwrite(dir, len);
585ba736c79SDavid van Moolenbroek len = strlen(pu->pu_kargp->pa_mntfromname)+1;
586ba736c79SDavid van Moolenbroek allwrite(&len, sizeof(len));
587ba736c79SDavid van Moolenbroek allwrite(pu->pu_kargp->pa_mntfromname, len);
588ba736c79SDavid van Moolenbroek allwrite(&mntflags, sizeof(mntflags));
589ba736c79SDavid van Moolenbroek len = sizeof(*pu->pu_kargp);
590ba736c79SDavid van Moolenbroek allwrite(&len, sizeof(len));
591ba736c79SDavid van Moolenbroek allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
592ba736c79SDavid van Moolenbroek allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
593ba736c79SDavid van Moolenbroek #undef allwrite
594ba736c79SDavid van Moolenbroek
595ba736c79SDavid van Moolenbroek rv = 0;
596ba736c79SDavid van Moolenbroek } else {
597ba736c79SDavid van Moolenbroek char rp[MAXPATHLEN];
598*0a6a1f1dSLionel Sambuc size_t rplen,dirlen;
599ba736c79SDavid van Moolenbroek
600ba736c79SDavid van Moolenbroek if (realpath(dir, rp) == NULL) {
601ba736c79SDavid van Moolenbroek rv = -1;
602ba736c79SDavid van Moolenbroek goto out;
603ba736c79SDavid van Moolenbroek }
604ba736c79SDavid van Moolenbroek
605*0a6a1f1dSLionel Sambuc rplen = strlen(rp);
606*0a6a1f1dSLionel Sambuc dirlen = strlen(dir);
607*0a6a1f1dSLionel Sambuc if (strncmp(dir, rp, rplen) != 0 ||
608*0a6a1f1dSLionel Sambuc strspn(dir + rplen, "/") != dirlen - rplen) {
609ba736c79SDavid van Moolenbroek warnx("puffs_mount: \"%s\" is a relative path.", dir);
610ba736c79SDavid van Moolenbroek warnx("puffs_mount: using \"%s\" instead.", rp);
611ba736c79SDavid van Moolenbroek }
612ba736c79SDavid van Moolenbroek
613ba736c79SDavid van Moolenbroek fd = open(_PATH_PUFFS, O_RDWR);
614ba736c79SDavid van Moolenbroek if (fd == -1) {
615ba736c79SDavid van Moolenbroek warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
616ba736c79SDavid van Moolenbroek rv = -1;
617ba736c79SDavid van Moolenbroek goto out;
618ba736c79SDavid van Moolenbroek }
619ba736c79SDavid van Moolenbroek if (fd <= 2)
620ba736c79SDavid van Moolenbroek warnx("puffs_mount: device fd %d (<= 2), sure this is "
621ba736c79SDavid van Moolenbroek "what you want?", fd);
622ba736c79SDavid van Moolenbroek
623ba736c79SDavid van Moolenbroek pu->pu_kargp->pa_fd = pu->pu_fd = fd;
624ba736c79SDavid van Moolenbroek if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
625ba736c79SDavid van Moolenbroek pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
626ba736c79SDavid van Moolenbroek goto out;
627ba736c79SDavid van Moolenbroek }
628*0a6a1f1dSLionel Sambuc #else
629ba736c79SDavid van Moolenbroek /* Process the already-received mount request. */
630ba736c79SDavid van Moolenbroek if (!lpuffs_pump()) {
631ba736c79SDavid van Moolenbroek /* Not mounted? This should never happen.. */
632490e0de5SThomas Veerman free(pu->pu_kargp);
633490e0de5SThomas Veerman pu->pu_kargp = NULL;
634ba736c79SDavid van Moolenbroek errno = EINVAL;
635490e0de5SThomas Veerman return -1;
636490e0de5SThomas Veerman }
637*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
638490e0de5SThomas Veerman
639490e0de5SThomas Veerman PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
640ba736c79SDavid van Moolenbroek
641*0a6a1f1dSLionel Sambuc #if !defined(__minix)
642ba736c79SDavid van Moolenbroek out:
643ba736c79SDavid van Moolenbroek if (rv != 0)
644ba736c79SDavid van Moolenbroek sverrno = errno;
645ba736c79SDavid van Moolenbroek else
646ba736c79SDavid van Moolenbroek sverrno = 0;
647ba736c79SDavid van Moolenbroek free(pu->pu_kargp);
648ba736c79SDavid van Moolenbroek pu->pu_kargp = NULL;
649ba736c79SDavid van Moolenbroek
650ba736c79SDavid van Moolenbroek if (pu->pu_state & PU_PUFFSDAEMON)
651ba736c79SDavid van Moolenbroek shutdaemon(pu, sverrno);
652ba736c79SDavid van Moolenbroek
653ba736c79SDavid van Moolenbroek errno = sverrno;
654ba736c79SDavid van Moolenbroek return rv;
655*0a6a1f1dSLionel Sambuc #else
656490e0de5SThomas Veerman return 0;
657*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
658490e0de5SThomas Veerman }
659490e0de5SThomas Veerman
660490e0de5SThomas Veerman struct puffs_usermount *
puffs_init(struct puffs_ops * pops,const char * mntfromname,const char * puffsname,void * priv,uint32_t pflags)66181db4f2cSDavid van Moolenbroek puffs_init(struct puffs_ops *pops, const char *mntfromname,
662490e0de5SThomas Veerman const char *puffsname, void *priv, uint32_t pflags)
663490e0de5SThomas Veerman {
664490e0de5SThomas Veerman struct puffs_usermount *pu;
665490e0de5SThomas Veerman struct puffs_kargs *pargs;
666490e0de5SThomas Veerman int sverrno;
667490e0de5SThomas Veerman
668490e0de5SThomas Veerman if (puffsname == PUFFS_DEFER)
669490e0de5SThomas Veerman puffsname = "n/a";
670490e0de5SThomas Veerman if (mntfromname == PUFFS_DEFER)
671490e0de5SThomas Veerman mntfromname = "n/a";
672490e0de5SThomas Veerman if (priv == PUFFS_DEFER)
673490e0de5SThomas Veerman priv = NULL;
674490e0de5SThomas Veerman
675490e0de5SThomas Veerman pu = malloc(sizeof(struct puffs_usermount));
676490e0de5SThomas Veerman if (pu == NULL)
677490e0de5SThomas Veerman goto failfree;
678490e0de5SThomas Veerman memset(pu, 0, sizeof(struct puffs_usermount));
679490e0de5SThomas Veerman
680490e0de5SThomas Veerman pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
681490e0de5SThomas Veerman if (pargs == NULL)
682490e0de5SThomas Veerman goto failfree;
683490e0de5SThomas Veerman memset(pargs, 0, sizeof(struct puffs_kargs));
684490e0de5SThomas Veerman
68584d9c625SLionel Sambuc pargs->pa_vers = PUFFSVERSION;
686490e0de5SThomas Veerman pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
68781db4f2cSDavid van Moolenbroek fillvnopmask(pops, pargs);
688490e0de5SThomas Veerman puffs_setmntinfo(pu, mntfromname, puffsname);
689490e0de5SThomas Veerman
690490e0de5SThomas Veerman puffs_zerostatvfs(&pargs->pa_svfsb);
691490e0de5SThomas Veerman pargs->pa_root_cookie = NULL;
692490e0de5SThomas Veerman pargs->pa_root_vtype = VDIR;
693490e0de5SThomas Veerman pargs->pa_root_vsize = 0;
694490e0de5SThomas Veerman pargs->pa_root_rdev = 0;
695490e0de5SThomas Veerman pargs->pa_maxmsglen = 0;
69684d9c625SLionel Sambuc if (/*CONSTCOND*/ sizeof(time_t) == 4)
69784d9c625SLionel Sambuc pargs->pa_time32 = 1;
69884d9c625SLionel Sambuc else
69984d9c625SLionel Sambuc pargs->pa_time32 = 0;
700490e0de5SThomas Veerman
701490e0de5SThomas Veerman pu->pu_flags = pflags;
702490e0de5SThomas Veerman pu->pu_ops = *pops;
703490e0de5SThomas Veerman free(pops); /* XXX */
704490e0de5SThomas Veerman
705490e0de5SThomas Veerman pu->pu_privdata = priv;
706490e0de5SThomas Veerman pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
707490e0de5SThomas Veerman LIST_INIT(&pu->pu_pnodelst);
708490e0de5SThomas Veerman LIST_INIT(&pu->pu_ios);
709490e0de5SThomas Veerman LIST_INIT(&pu->pu_ios_rmlist);
710490e0de5SThomas Veerman LIST_INIT(&pu->pu_ccmagazin);
711490e0de5SThomas Veerman TAILQ_INIT(&pu->pu_sched);
712490e0de5SThomas Veerman
713*0a6a1f1dSLionel Sambuc #if !defined(__minix)
714ba736c79SDavid van Moolenbroek pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
715ba736c79SDavid van Moolenbroek pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
716ba736c79SDavid van Moolenbroek pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
717ba736c79SDavid van Moolenbroek pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
718ba736c79SDavid van Moolenbroek pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
719*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
720ba736c79SDavid van Moolenbroek
721490e0de5SThomas Veerman /* defaults for some user-settable translation functions */
722490e0de5SThomas Veerman pu->pu_cmap = NULL; /* identity translation */
723490e0de5SThomas Veerman
724490e0de5SThomas Veerman pu->pu_pathbuild = puffs_stdpath_buildpath;
725490e0de5SThomas Veerman pu->pu_pathfree = puffs_stdpath_freepath;
726490e0de5SThomas Veerman pu->pu_pathcmp = puffs_stdpath_cmppath;
727490e0de5SThomas Veerman pu->pu_pathtransform = NULL;
728490e0de5SThomas Veerman pu->pu_namemod = NULL;
729490e0de5SThomas Veerman
730ba736c79SDavid van Moolenbroek pu->pu_errnotify = puffs_kernerr_log;
731490e0de5SThomas Veerman
732490e0de5SThomas Veerman PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
733490e0de5SThomas Veerman
734*0a6a1f1dSLionel Sambuc #if defined(__minix)
735ba736c79SDavid van Moolenbroek /* Do the MINIX3-specific side of the initialization. */
736ba736c79SDavid van Moolenbroek lpuffs_init(pu);
737*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
738490e0de5SThomas Veerman
739490e0de5SThomas Veerman return pu;
740490e0de5SThomas Veerman
741490e0de5SThomas Veerman failfree:
742490e0de5SThomas Veerman /* can't unmount() from here for obvious reasons */
743490e0de5SThomas Veerman sverrno = errno;
744490e0de5SThomas Veerman free(pu);
745490e0de5SThomas Veerman errno = sverrno;
746490e0de5SThomas Veerman return NULL;
747490e0de5SThomas Veerman }
748490e0de5SThomas Veerman
749490e0de5SThomas Veerman void
puffs_cancel(struct puffs_usermount * pu,int error)750490e0de5SThomas Veerman puffs_cancel(struct puffs_usermount *pu, int error)
751490e0de5SThomas Veerman {
75284d9c625SLionel Sambuc
753490e0de5SThomas Veerman assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
754ba736c79SDavid van Moolenbroek shutdaemon(pu, error);
755490e0de5SThomas Veerman free(pu);
756490e0de5SThomas Veerman }
757490e0de5SThomas Veerman
758490e0de5SThomas Veerman /*ARGSUSED1*/
759490e0de5SThomas Veerman int
puffs_exit(struct puffs_usermount * pu,int unused)760ba736c79SDavid van Moolenbroek puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
761490e0de5SThomas Veerman {
762*0a6a1f1dSLionel Sambuc #if !defined(__minix)
763ba736c79SDavid van Moolenbroek struct puffs_framebuf *pb;
764ba736c79SDavid van Moolenbroek struct puffs_req *preq;
765ba736c79SDavid van Moolenbroek void *winp;
766ba736c79SDavid van Moolenbroek size_t winlen;
767ba736c79SDavid van Moolenbroek int sverrno;
768ba736c79SDavid van Moolenbroek
769ba736c79SDavid van Moolenbroek pb = puffs_framebuf_make();
770ba736c79SDavid van Moolenbroek if (pb == NULL) {
771ba736c79SDavid van Moolenbroek errno = ENOMEM;
772ba736c79SDavid van Moolenbroek return -1;
773ba736c79SDavid van Moolenbroek }
774ba736c79SDavid van Moolenbroek
775ba736c79SDavid van Moolenbroek winlen = sizeof(struct puffs_req);
776ba736c79SDavid van Moolenbroek if (puffs_framebuf_getwindow(pb, 0, &winp, &winlen) == -1) {
777ba736c79SDavid van Moolenbroek sverrno = errno;
778ba736c79SDavid van Moolenbroek puffs_framebuf_destroy(pb);
779ba736c79SDavid van Moolenbroek errno = sverrno;
780ba736c79SDavid van Moolenbroek return -1;
781ba736c79SDavid van Moolenbroek }
782ba736c79SDavid van Moolenbroek preq = winp;
783ba736c79SDavid van Moolenbroek
784ba736c79SDavid van Moolenbroek preq->preq_buflen = sizeof(struct puffs_req);
785ba736c79SDavid van Moolenbroek preq->preq_opclass = PUFFSOP_UNMOUNT;
786ba736c79SDavid van Moolenbroek preq->preq_id = puffs__nextreq(pu);
787ba736c79SDavid van Moolenbroek
788ba736c79SDavid van Moolenbroek puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
789*0a6a1f1dSLionel Sambuc #else
790490e0de5SThomas Veerman struct puffs_node *pn;
791490e0de5SThomas Veerman
792490e0de5SThomas Veerman lpuffs_debug("puffs_exit\n");
793490e0de5SThomas Veerman
794490e0de5SThomas Veerman while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
795490e0de5SThomas Veerman puffs_pn_put(pn);
796490e0de5SThomas Veerman
797490e0de5SThomas Veerman while ((pn = LIST_FIRST(&pu->pu_pnode_removed_lst)) != NULL)
798490e0de5SThomas Veerman puffs_pn_put(pn);
799490e0de5SThomas Veerman
800490e0de5SThomas Veerman puffs__cc_exit(pu);
801490e0de5SThomas Veerman if (pu->pu_state & PU_HASKQ)
802490e0de5SThomas Veerman close(pu->pu_kq);
803490e0de5SThomas Veerman free(pu);
804*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
805490e0de5SThomas Veerman
806ba736c79SDavid van Moolenbroek return 0;
807490e0de5SThomas Veerman }
808490e0de5SThomas Veerman
809*0a6a1f1dSLionel Sambuc #if !defined(__minix)
810ba736c79SDavid van Moolenbroek /* no sigset_t static intializer */
811ba736c79SDavid van Moolenbroek static int sigs[NSIG] = { 0, };
812ba736c79SDavid van Moolenbroek static int sigcatch = 0;
813ba736c79SDavid van Moolenbroek
814ba736c79SDavid van Moolenbroek int
puffs_unmountonsignal(int sig,bool sigignore)815ba736c79SDavid van Moolenbroek puffs_unmountonsignal(int sig, bool sigignore)
816ba736c79SDavid van Moolenbroek {
817ba736c79SDavid van Moolenbroek
818ba736c79SDavid van Moolenbroek if (sig < 0 || sig >= (int)NSIG) {
819ba736c79SDavid van Moolenbroek errno = EINVAL;
820ba736c79SDavid van Moolenbroek return -1;
821ba736c79SDavid van Moolenbroek }
822ba736c79SDavid van Moolenbroek if (sigignore)
823ba736c79SDavid van Moolenbroek if (signal(sig, SIG_IGN) == SIG_ERR)
824ba736c79SDavid van Moolenbroek return -1;
825ba736c79SDavid van Moolenbroek
826ba736c79SDavid van Moolenbroek if (!sigs[sig])
827ba736c79SDavid van Moolenbroek sigcatch++;
828ba736c79SDavid van Moolenbroek sigs[sig] = 1;
829ba736c79SDavid van Moolenbroek
830ba736c79SDavid van Moolenbroek return 0;
831ba736c79SDavid van Moolenbroek }
832*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
833ba736c79SDavid van Moolenbroek
834490e0de5SThomas Veerman /*
835490e0de5SThomas Veerman * Actual mainloop. This is called from a context which can block.
836490e0de5SThomas Veerman * It is called either from puffs_mainloop (indirectly, via
837490e0de5SThomas Veerman * puffs_cc_continue() or from puffs_cc_yield()).
838490e0de5SThomas Veerman */
839490e0de5SThomas Veerman void
puffs__theloop(struct puffs_cc * pcc)840490e0de5SThomas Veerman puffs__theloop(struct puffs_cc *pcc)
841490e0de5SThomas Veerman {
842490e0de5SThomas Veerman struct puffs_usermount *pu = pcc->pcc_pu;
843*0a6a1f1dSLionel Sambuc #if !defined(__minix)
844ba736c79SDavid van Moolenbroek struct puffs_framectrl *pfctrl;
845ba736c79SDavid van Moolenbroek struct puffs_fctrl_io *fio;
846ba736c79SDavid van Moolenbroek struct kevent *curev;
847ba736c79SDavid van Moolenbroek size_t nchanges;
848ba736c79SDavid van Moolenbroek int ndone;
849*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
850490e0de5SThomas Veerman
851*0a6a1f1dSLionel Sambuc #if !defined(__minix)
852ba736c79SDavid van Moolenbroek while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
853*0a6a1f1dSLionel Sambuc #else
854ba736c79SDavid van Moolenbroek do {
855*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
856ba736c79SDavid van Moolenbroek
857490e0de5SThomas Veerman /*
858490e0de5SThomas Veerman * Schedule existing requests.
859490e0de5SThomas Veerman */
860490e0de5SThomas Veerman while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
861490e0de5SThomas Veerman TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
862490e0de5SThomas Veerman puffs__goto(pcc);
863490e0de5SThomas Veerman }
864490e0de5SThomas Veerman
865ba736c79SDavid van Moolenbroek if (pu->pu_ml_lfn)
866490e0de5SThomas Veerman pu->pu_ml_lfn(pu);
867ba736c79SDavid van Moolenbroek
868*0a6a1f1dSLionel Sambuc #if !defined(__minix)
869ba736c79SDavid van Moolenbroek /* XXX: can we still do these optimizations? */
870ba736c79SDavid van Moolenbroek #if 0
871ba736c79SDavid van Moolenbroek /*
872ba736c79SDavid van Moolenbroek * Do this here, because:
873ba736c79SDavid van Moolenbroek * a) loopfunc might generate some results
874ba736c79SDavid van Moolenbroek * b) it's still "after" event handling (except for round 1)
875ba736c79SDavid van Moolenbroek */
876ba736c79SDavid van Moolenbroek if (puffs_req_putput(ppr) == -1)
877ba736c79SDavid van Moolenbroek goto out;
878ba736c79SDavid van Moolenbroek puffs_req_resetput(ppr);
879ba736c79SDavid van Moolenbroek
880ba736c79SDavid van Moolenbroek /* micro optimization: skip kevent syscall if possible */
881ba736c79SDavid van Moolenbroek if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
882ba736c79SDavid van Moolenbroek && (pu->pu_state & PU_ASYNCFD) == 0) {
883ba736c79SDavid van Moolenbroek pfctrl = XXX->fctrl;
884ba736c79SDavid van Moolenbroek puffs_framev_input(pu, pfctrl, XXX);
885ba736c79SDavid van Moolenbroek continue;
886ba736c79SDavid van Moolenbroek }
887ba736c79SDavid van Moolenbroek #endif
888ba736c79SDavid van Moolenbroek
889ba736c79SDavid van Moolenbroek /* else: do full processing */
890ba736c79SDavid van Moolenbroek /* Don't bother worrying about O(n) for now */
891ba736c79SDavid van Moolenbroek LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
892ba736c79SDavid van Moolenbroek if (fio->stat & FIO_WRGONE)
893ba736c79SDavid van Moolenbroek continue;
894ba736c79SDavid van Moolenbroek
895ba736c79SDavid van Moolenbroek pfctrl = fio->fctrl;
896ba736c79SDavid van Moolenbroek
897ba736c79SDavid van Moolenbroek /*
898ba736c79SDavid van Moolenbroek * Try to write out everything to avoid the
899ba736c79SDavid van Moolenbroek * need for enabling EVFILT_WRITE. The likely
900ba736c79SDavid van Moolenbroek * case is that we can fit everything into the
901ba736c79SDavid van Moolenbroek * socket buffer.
902ba736c79SDavid van Moolenbroek */
903ba736c79SDavid van Moolenbroek puffs__framev_output(pu, pfctrl, fio);
904490e0de5SThomas Veerman }
905490e0de5SThomas Veerman
906ba736c79SDavid van Moolenbroek /*
907ba736c79SDavid van Moolenbroek * Build list of which to enable/disable in writecheck.
908ba736c79SDavid van Moolenbroek */
909ba736c79SDavid van Moolenbroek nchanges = 0;
910ba736c79SDavid van Moolenbroek LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
911ba736c79SDavid van Moolenbroek if (fio->stat & FIO_WRGONE)
912ba736c79SDavid van Moolenbroek continue;
913490e0de5SThomas Veerman
914ba736c79SDavid van Moolenbroek /* en/disable write checks for kqueue as needed */
915ba736c79SDavid van Moolenbroek assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
916ba736c79SDavid van Moolenbroek if (FIO_EN_WRITE(fio)) {
917ba736c79SDavid van Moolenbroek EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
918ba736c79SDavid van Moolenbroek EVFILT_WRITE, EV_ENABLE, 0, 0,
919ba736c79SDavid van Moolenbroek (uintptr_t)fio);
920ba736c79SDavid van Moolenbroek fio->stat |= FIO_WR;
921ba736c79SDavid van Moolenbroek nchanges++;
922490e0de5SThomas Veerman }
923ba736c79SDavid van Moolenbroek if (FIO_RM_WRITE(fio)) {
924ba736c79SDavid van Moolenbroek EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
925ba736c79SDavid van Moolenbroek EVFILT_WRITE, EV_DISABLE, 0, 0,
926ba736c79SDavid van Moolenbroek (uintptr_t)fio);
927ba736c79SDavid van Moolenbroek fio->stat &= ~FIO_WR;
928ba736c79SDavid van Moolenbroek nchanges++;
929ba736c79SDavid van Moolenbroek }
930ba736c79SDavid van Moolenbroek }
931ba736c79SDavid van Moolenbroek
932ba736c79SDavid van Moolenbroek ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
933ba736c79SDavid van Moolenbroek pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
934ba736c79SDavid van Moolenbroek
935ba736c79SDavid van Moolenbroek if (ndone == -1) {
936ba736c79SDavid van Moolenbroek if (errno != EINTR)
937ba736c79SDavid van Moolenbroek break;
938ba736c79SDavid van Moolenbroek else
939ba736c79SDavid van Moolenbroek continue;
940ba736c79SDavid van Moolenbroek }
941ba736c79SDavid van Moolenbroek
942ba736c79SDavid van Moolenbroek /* uoptimize */
943ba736c79SDavid van Moolenbroek if (ndone == 0)
944ba736c79SDavid van Moolenbroek continue;
945ba736c79SDavid van Moolenbroek
946ba736c79SDavid van Moolenbroek /* iterate over the results */
947ba736c79SDavid van Moolenbroek for (curev = pu->pu_evs; ndone--; curev++) {
948ba736c79SDavid van Moolenbroek int what;
949ba736c79SDavid van Moolenbroek
950ba736c79SDavid van Moolenbroek #if 0
951ba736c79SDavid van Moolenbroek /* get & possibly dispatch events from kernel */
952ba736c79SDavid van Moolenbroek if (curev->ident == puffsfd) {
953ba736c79SDavid van Moolenbroek if (puffs_req_handle(pgr, ppr, 0) == -1)
954ba736c79SDavid van Moolenbroek goto out;
955ba736c79SDavid van Moolenbroek continue;
956ba736c79SDavid van Moolenbroek }
957ba736c79SDavid van Moolenbroek #endif
958ba736c79SDavid van Moolenbroek
959ba736c79SDavid van Moolenbroek fio = (void *)curev->udata;
960ba736c79SDavid van Moolenbroek if (__predict_true(fio))
961ba736c79SDavid van Moolenbroek pfctrl = fio->fctrl;
962ba736c79SDavid van Moolenbroek else
963ba736c79SDavid van Moolenbroek pfctrl = NULL;
964ba736c79SDavid van Moolenbroek if (curev->flags & EV_ERROR) {
965ba736c79SDavid van Moolenbroek assert(curev->filter == EVFILT_WRITE);
966ba736c79SDavid van Moolenbroek fio->stat &= ~FIO_WR;
967ba736c79SDavid van Moolenbroek
968ba736c79SDavid van Moolenbroek /* XXX: how to know if it's a transient error */
969ba736c79SDavid van Moolenbroek puffs__framev_writeclose(pu, fio,
970ba736c79SDavid van Moolenbroek (int)curev->data);
971ba736c79SDavid van Moolenbroek puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
972ba736c79SDavid van Moolenbroek continue;
973ba736c79SDavid van Moolenbroek }
974ba736c79SDavid van Moolenbroek
975ba736c79SDavid van Moolenbroek what = 0;
976ba736c79SDavid van Moolenbroek if (curev->filter == EVFILT_READ) {
977ba736c79SDavid van Moolenbroek puffs__framev_input(pu, pfctrl, fio);
978ba736c79SDavid van Moolenbroek what |= PUFFS_FBIO_READ;
979ba736c79SDavid van Moolenbroek }
980ba736c79SDavid van Moolenbroek
981ba736c79SDavid van Moolenbroek else if (curev->filter == EVFILT_WRITE) {
982ba736c79SDavid van Moolenbroek puffs__framev_output(pu, pfctrl, fio);
983ba736c79SDavid van Moolenbroek what |= PUFFS_FBIO_WRITE;
984ba736c79SDavid van Moolenbroek }
985ba736c79SDavid van Moolenbroek
986ba736c79SDavid van Moolenbroek else if (__predict_false(curev->filter==EVFILT_SIGNAL)){
987ba736c79SDavid van Moolenbroek if ((pu->pu_state & PU_DONEXIT) == 0) {
988ba736c79SDavid van Moolenbroek PU_SETSFLAG(pu, PU_DONEXIT);
989ba736c79SDavid van Moolenbroek puffs_exit(pu, 0);
990ba736c79SDavid van Moolenbroek }
991ba736c79SDavid van Moolenbroek }
992ba736c79SDavid van Moolenbroek if (what)
993ba736c79SDavid van Moolenbroek puffs__framev_notify(fio, what);
994ba736c79SDavid van Moolenbroek }
995ba736c79SDavid van Moolenbroek
996ba736c79SDavid van Moolenbroek /*
997ba736c79SDavid van Moolenbroek * Really free fd's now that we don't have references
998ba736c79SDavid van Moolenbroek * to them.
999ba736c79SDavid van Moolenbroek */
1000ba736c79SDavid van Moolenbroek while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
1001ba736c79SDavid van Moolenbroek LIST_REMOVE(fio, fio_entries);
1002ba736c79SDavid van Moolenbroek free(fio);
1003ba736c79SDavid van Moolenbroek }
1004*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
1005ba736c79SDavid van Moolenbroek }
1006*0a6a1f1dSLionel Sambuc #if defined(__minix)
1007ba736c79SDavid van Moolenbroek while (lpuffs_pump());
1008*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
1009490e0de5SThomas Veerman
1010490e0de5SThomas Veerman if (puffs__cc_restoremain(pu) == -1)
1011490e0de5SThomas Veerman warn("cannot restore main context. impending doom");
1012490e0de5SThomas Veerman }
1013490e0de5SThomas Veerman int
1014490e0de5SThomas Veerman puffs_mainloop(struct puffs_usermount *pu)
1015490e0de5SThomas Veerman {
1016*0a6a1f1dSLionel Sambuc #if !defined(__minix)
1017ba736c79SDavid van Moolenbroek struct puffs_fctrl_io *fio;
1018*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
1019490e0de5SThomas Veerman struct puffs_cc *pcc;
1020*0a6a1f1dSLionel Sambuc #if !defined(__minix)
1021ba736c79SDavid van Moolenbroek struct kevent *curev;
1022ba736c79SDavid van Moolenbroek size_t nevs;
1023ba736c79SDavid van Moolenbroek int sverrno, i;
1024*0a6a1f1dSLionel Sambuc #else
1025490e0de5SThomas Veerman int sverrno;
1026*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
1027490e0de5SThomas Veerman
1028490e0de5SThomas Veerman assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
1029490e0de5SThomas Veerman
1030*0a6a1f1dSLionel Sambuc #if !defined(__minix)
1031ba736c79SDavid van Moolenbroek pu->pu_kq = kqueue();
1032ba736c79SDavid van Moolenbroek if (pu->pu_kq == -1)
1033ba736c79SDavid van Moolenbroek goto out;
1034*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
1035ba736c79SDavid van Moolenbroek pu->pu_state |= PU_HASKQ;
1036ba736c79SDavid van Moolenbroek
1037*0a6a1f1dSLionel Sambuc #if !defined(__minix)
1038ba736c79SDavid van Moolenbroek puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
1039ba736c79SDavid van Moolenbroek if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
1040ba736c79SDavid van Moolenbroek PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
1041ba736c79SDavid van Moolenbroek &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
1042ba736c79SDavid van Moolenbroek goto out;
1043ba736c79SDavid van Moolenbroek
1044ba736c79SDavid van Moolenbroek nevs = pu->pu_nevs + sigcatch;
1045ba736c79SDavid van Moolenbroek curev = realloc(pu->pu_evs, nevs * sizeof(struct kevent));
1046ba736c79SDavid van Moolenbroek if (curev == NULL)
1047ba736c79SDavid van Moolenbroek goto out;
1048ba736c79SDavid van Moolenbroek pu->pu_evs = curev;
1049ba736c79SDavid van Moolenbroek pu->pu_nevs = nevs;
1050ba736c79SDavid van Moolenbroek
1051ba736c79SDavid van Moolenbroek LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
1052ba736c79SDavid van Moolenbroek EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
1053ba736c79SDavid van Moolenbroek 0, 0, (uintptr_t)fio);
1054ba736c79SDavid van Moolenbroek curev++;
1055ba736c79SDavid van Moolenbroek EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
1056ba736c79SDavid van Moolenbroek 0, 0, (uintptr_t)fio);
1057ba736c79SDavid van Moolenbroek curev++;
1058ba736c79SDavid van Moolenbroek }
1059ba736c79SDavid van Moolenbroek for (i = 0; i < NSIG; i++) {
1060ba736c79SDavid van Moolenbroek if (sigs[i]) {
1061ba736c79SDavid van Moolenbroek EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
1062ba736c79SDavid van Moolenbroek 0, 0, 0);
1063ba736c79SDavid van Moolenbroek curev++;
1064ba736c79SDavid van Moolenbroek }
1065ba736c79SDavid van Moolenbroek }
1066ba736c79SDavid van Moolenbroek assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
1067ba736c79SDavid van Moolenbroek if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
1068ba736c79SDavid van Moolenbroek goto out;
1069*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
1070ba736c79SDavid van Moolenbroek
1071ba736c79SDavid van Moolenbroek pu->pu_state |= PU_INLOOP;
1072490e0de5SThomas Veerman
1073490e0de5SThomas Veerman /*
1074490e0de5SThomas Veerman * Create alternate execution context and jump to it. Note
1075490e0de5SThomas Veerman * that we come "out" of savemain twice. Where we come out
1076490e0de5SThomas Veerman * of it depends on the architecture. If the return address is
1077490e0de5SThomas Veerman * stored on the stack, we jump out from puffs_cc_continue(),
1078490e0de5SThomas Veerman * for a register return address from puffs__cc_savemain().
1079490e0de5SThomas Veerman * PU_MAINRESTORE makes sure we DTRT in both cases.
1080490e0de5SThomas Veerman */
1081490e0de5SThomas Veerman if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
1082490e0de5SThomas Veerman goto out;
1083490e0de5SThomas Veerman }
1084ba736c79SDavid van Moolenbroek
1085ba736c79SDavid van Moolenbroek #if 0
1086490e0de5SThomas Veerman if (puffs__cc_savemain(pu) == -1) {
1087490e0de5SThomas Veerman goto out;
1088490e0de5SThomas Veerman }
1089ba736c79SDavid van Moolenbroek #else
1090ba736c79SDavid van Moolenbroek /*
1091ba736c79SDavid van Moolenbroek * XXX
1092ba736c79SDavid van Moolenbroek * puffs__cc_savemain() uses getcontext() and then returns.
1093ba736c79SDavid van Moolenbroek * the caller (this function) may overwrite the stack frame
1094ba736c79SDavid van Moolenbroek * of puffs__cc_savemain(), so when we call setcontext() later and
1095ba736c79SDavid van Moolenbroek * return from puffs__cc_savemain() again, the return address or
1096ba736c79SDavid van Moolenbroek * saved stack pointer can be garbage.
1097ba736c79SDavid van Moolenbroek * avoid this by calling getcontext() directly here.
1098ba736c79SDavid van Moolenbroek */
1099ba736c79SDavid van Moolenbroek extern int puffs_fakecc;
1100ba736c79SDavid van Moolenbroek if (!puffs_fakecc) {
1101ba736c79SDavid van Moolenbroek PU_CLRSFLAG(pu, PU_MAINRESTORE);
1102ba736c79SDavid van Moolenbroek if (getcontext(&pu->pu_mainctx) == -1) {
1103ba736c79SDavid van Moolenbroek goto out;
1104ba736c79SDavid van Moolenbroek }
1105ba736c79SDavid van Moolenbroek }
1106ba736c79SDavid van Moolenbroek #endif
1107ba736c79SDavid van Moolenbroek
1108490e0de5SThomas Veerman if ((pu->pu_state & PU_MAINRESTORE) == 0)
1109490e0de5SThomas Veerman puffs_cc_continue(pcc);
1110490e0de5SThomas Veerman
1111ba736c79SDavid van Moolenbroek finalpush(pu);
1112490e0de5SThomas Veerman errno = 0;
1113490e0de5SThomas Veerman
1114490e0de5SThomas Veerman out:
1115490e0de5SThomas Veerman /* store the real error for a while */
1116490e0de5SThomas Veerman sverrno = errno;
1117490e0de5SThomas Veerman
1118490e0de5SThomas Veerman errno = sverrno;
1119490e0de5SThomas Veerman if (errno)
1120490e0de5SThomas Veerman return -1;
1121490e0de5SThomas Veerman else
1122490e0de5SThomas Veerman return 0;
1123490e0de5SThomas Veerman }
1124