xref: /netbsd/lib/libpuffs/puffs.h (revision 396ccf64)
1 /*	$NetBSD: puffs.h,v 1.130 2021/12/03 17:12:17 pho 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 #ifndef _PUFFS_H_
33 #define _PUFFS_H_
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/namei.h>
39 #include <sys/stat.h>
40 #include <sys/statvfs.h>
41 #include <sys/time.h>
42 #include <sys/vnode.h>
43 
44 #include <fs/puffs/puffs_msgif.h>
45 
46 #include <mntopts.h>
47 #include <stdbool.h>
48 #include <string.h>
49 
50 /* forwards */
51 struct puffs_cc;
52 
53 struct puffs_getreq;
54 struct puffs_cred;
55 struct puffs_newinfo;
56 
57 /* paths */
58 struct puffs_pathobj {
59 	void 		*po_path;
60 	size_t		po_len;
61 	uint32_t	po_hash;
62 };
63 
64 /* for prefix rename */
65 struct puffs_pathinfo {
66 	struct puffs_pathobj *pi_old;
67 	struct puffs_pathobj *pi_new;
68 };
69 
70 /* describes one segment cached in the kernel */
71 struct puffs_kcache {
72 	off_t	pkc_start;
73 	off_t	pkc_end;
74 
75 	LIST_ENTRY(puffs_kcache) pkc_entries;
76 };
77 
78 /* XXX: might disappear from here into a private header */
79 struct puffs_node {
80 	off_t			pn_size;
81 	int			pn_flags;
82 	int			pn_nlookup;
83 	struct vattr		pn_va;
84 
85 	void			*pn_data;	/* private data		*/
86 
87 	struct puffs_pathobj	pn_po;		/* PUFFS_FLAG_BUILDPATH */
88 
89 	struct puffs_usermount 	*pn_mnt;
90 	LIST_ENTRY(puffs_node)	pn_entries;
91 
92 	LIST_HEAD(,puffs_kcache)pn_cacheinfo;	/* PUFFS_KFLAG_CACHE	*/
93 };
94 #define PUFFS_NODE_REMOVED	0x01		/* not on entry list	*/
95 
96 
97 struct puffs_usermount;
98 
99 /*
100  * megaXXX: these are values from inside _KERNEL
101  * need to work on the translation for ALL the necessary values.
102  */
103 #define PUFFS_VNOVAL (-1)
104 
105 #define PUFFS_IO_APPEND 0x020
106 #define PUFFS_IO_NDELAY	0x100
107 
108 #define PUFFS_VEXEC	01
109 #define PUFFS_VWRITE	02
110 #define PUFFS_VREAD	04
111 
112 #define PUFFS_FSYNC_DATAONLY 0x0002
113 #define PUFFS_FSYNC_CACHE    0x0100
114 
115 /*
116  * xflags for setattr_ttl and write2
117  */
118 #define PUFFS_SETATTR_FAF    0x1
119 #define PUFFS_WRITE_FAF      0x1
120 
121 #define PUFFS_EXTATTR_LIST_LENPREFIX 1
122 /*
123  * Magic constants
124  */
125 #define PUFFS_CC_STACKSHIFT_DEFAULT 18
126 
127 struct puffs_cn {
128 	struct puffs_kcn	*pcn_pkcnp;	/* kernel input */
129 	struct puffs_cred	*pcn_cred;	/* cred used for lookup */
130 
131 	struct puffs_pathobj	pcn_po_full;	/* PUFFS_FLAG_BUILDPATH */
132 };
133 #define pcn_nameiop	pcn_pkcnp->pkcn_nameiop
134 #define pcn_flags	pcn_pkcnp->pkcn_flags
135 #define pcn_name	pcn_pkcnp->pkcn_name
136 #define pcn_namelen	pcn_pkcnp->pkcn_namelen
137 #define pcn_consume	pcn_pkcnp->pkcn_consume
138 
139 /*
140  * Puffs options to mount
141  */
142 /* kernel */
143 #define	PUFFSMOPT_NAMECACHE	{ "namecache", 1, PUFFS_KFLAG_NOCACHE_NAME, 1 }
144 #define	PUFFSMOPT_PAGECACHE	{ "pagecache", 1, PUFFS_KFLAG_NOCACHE_PAGE, 1 }
145 #define	PUFFSMOPT_ATTRCACHE	{ "attrcache", 1, PUFFS_KFLAG_NOCACHE_ATTR, 1 }
146 #define	PUFFSMOPT_CACHE		{ "cache", 1, PUFFS_KFLAG_NOCACHE, 1 }
147 #define PUFFSMOPT_ALLOPS	{ "allops", 0, PUFFS_KFLAG_ALLOPS, 1 }
148 
149 /* libpuffs */
150 #define PUFFSMOPT_DUMP		{ "dump", 0, PUFFS_FLAG_OPDUMP, 1 }
151 
152 #define PUFFSMOPT_STD							\
153 	PUFFSMOPT_NAMECACHE,						\
154 	PUFFSMOPT_PAGECACHE,						\
155 	PUFFSMOPT_ATTRCACHE,						\
156 	PUFFSMOPT_CACHE,						\
157 	PUFFSMOPT_ALLOPS,						\
158 	PUFFSMOPT_DUMP
159 
160 extern const struct mntopt puffsmopts[]; /* puffs.c */
161 
162 /* callbacks for operations */
163 struct puffs_ops {
164 	int (*puffs_fs_unmount)(struct puffs_usermount *, int);
165 	int (*puffs_fs_statvfs)(struct puffs_usermount *,
166 	    struct puffs_statvfs *);
167 	int (*puffs_fs_sync)(struct puffs_usermount *, int,
168 	    const struct puffs_cred *);
169 	int (*puffs_fs_fhtonode)(struct puffs_usermount *, void *, size_t,
170 	    struct puffs_newinfo *);
171 	int (*puffs_fs_nodetofh)(struct puffs_usermount *, puffs_cookie_t,
172 	    void *, size_t *);
173 	int (*puffs_fs_extattrctl)(struct puffs_usermount *, int,
174 	    puffs_cookie_t, int, int, const char *);
175 
176 	int (*puffs_node_lookup)(struct puffs_usermount *,
177 	    puffs_cookie_t, struct puffs_newinfo *, const struct puffs_cn *);
178 	int (*puffs_node_create)(struct puffs_usermount *,
179 	    puffs_cookie_t, struct puffs_newinfo *, const struct puffs_cn *,
180 	    const struct vattr *);
181 	int (*puffs_node_mknod)(struct puffs_usermount *,
182 	    puffs_cookie_t, struct puffs_newinfo *, const struct puffs_cn *,
183 	    const struct vattr *);
184 	int (*puffs_node_open)(struct puffs_usermount *,
185 	    puffs_cookie_t, int, const struct puffs_cred *);
186 	int (*puffs_node_close)(struct puffs_usermount *,
187 	    puffs_cookie_t, int, const struct puffs_cred *);
188 	int (*puffs_node_access)(struct puffs_usermount *,
189 	    puffs_cookie_t, int, const struct puffs_cred *);
190 	int (*puffs_node_getattr)(struct puffs_usermount *,
191 	    puffs_cookie_t, struct vattr *, const struct puffs_cred *);
192 	int (*puffs_node_setattr)(struct puffs_usermount *,
193 	    puffs_cookie_t, const struct vattr *, const struct puffs_cred *);
194 	int (*puffs_node_poll)(struct puffs_usermount *, puffs_cookie_t, int *);
195 	int (*puffs_node_mmap)(struct puffs_usermount *,
196 	    puffs_cookie_t, vm_prot_t, const struct puffs_cred *);
197 	int (*puffs_node_fsync)(struct puffs_usermount *,
198 	    puffs_cookie_t, const struct puffs_cred *, int, off_t, off_t);
199 	int (*puffs_node_seek)(struct puffs_usermount *,
200 	    puffs_cookie_t, off_t, off_t, const struct puffs_cred *);
201 	int (*puffs_node_remove)(struct puffs_usermount *,
202 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);
203 	int (*puffs_node_link)(struct puffs_usermount *,
204 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);
205 	int (*puffs_node_rename)(struct puffs_usermount *,
206 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *,
207 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);
208 	int (*puffs_node_mkdir)(struct puffs_usermount *,
209 	    puffs_cookie_t, struct puffs_newinfo *, const struct puffs_cn *,
210 	    const struct vattr *);
211 	int (*puffs_node_rmdir)(struct puffs_usermount *,
212 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);
213 	int (*puffs_node_symlink)(struct puffs_usermount *,
214 	    puffs_cookie_t, struct puffs_newinfo *, const struct puffs_cn *,
215 	    const struct vattr *,
216 	    const char *);
217 	int (*puffs_node_readdir)(struct puffs_usermount *,
218 	    puffs_cookie_t, struct dirent *, off_t *, size_t *,
219 	    const struct puffs_cred *, int *, off_t *, size_t *);
220 	int (*puffs_node_readlink)(struct puffs_usermount *,
221 	    puffs_cookie_t, const struct puffs_cred *, char *, size_t *);
222 	int (*puffs_node_reclaim)(struct puffs_usermount *, puffs_cookie_t);
223 	int (*puffs_node_inactive)(struct puffs_usermount *, puffs_cookie_t);
224 	int (*puffs_node_print)(struct puffs_usermount *, puffs_cookie_t);
225 	int (*puffs_node_pathconf)(struct puffs_usermount *,
226 	    puffs_cookie_t, int, __register_t *);
227 	int (*puffs_node_advlock)(struct puffs_usermount *,
228 	    puffs_cookie_t, void *, int, struct flock *, int);
229 	int (*puffs_node_read)(struct puffs_usermount *, puffs_cookie_t,
230 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
231 	int (*puffs_node_write)(struct puffs_usermount *, puffs_cookie_t,
232 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
233 	int (*puffs_node_abortop)(struct puffs_usermount *, puffs_cookie_t,
234 	    const struct puffs_cn *);
235 	int (*puffs_node_getextattr)(struct puffs_usermount *, puffs_cookie_t,
236 	    int, const char *, size_t *, uint8_t *, size_t *,
237 	    const struct puffs_cred *);
238 	int (*puffs_node_setextattr)(struct puffs_usermount *, puffs_cookie_t,
239 	    int, const char *, uint8_t *, size_t *, const struct puffs_cred *);
240 	int (*puffs_node_listextattr)(struct puffs_usermount *, puffs_cookie_t,
241 	    int, size_t *, uint8_t *, size_t *, int, const struct puffs_cred *);
242 	int (*puffs_node_deleteextattr)(struct puffs_usermount *,
243 	    puffs_cookie_t, int, const char *, const struct puffs_cred *);
244 	int (*puffs_node_getattr_ttl)(struct puffs_usermount *,
245 	    puffs_cookie_t, struct vattr *, const struct puffs_cred *,
246 	    struct timespec *);
247 	int (*puffs_node_setattr_ttl)(struct puffs_usermount *,
248 	    puffs_cookie_t, struct vattr *, const struct puffs_cred *,
249 	    struct timespec *, int);
250 	int (*puffs_node_write2)(struct puffs_usermount *, puffs_cookie_t,
251 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int, int);
252 	int (*puffs_node_reclaim2)(struct puffs_usermount *,
253 	    puffs_cookie_t, int);
254 	int (*puffs_node_open2)(struct puffs_usermount *,
255 	    puffs_cookie_t, int, const struct puffs_cred *, int *);
256 	int (*puffs_node_fallocate)(struct puffs_usermount *,
257 	    puffs_cookie_t, off_t, off_t);
258 	int (*puffs_node_fdiscard)(struct puffs_usermount *,
259 	    puffs_cookie_t, off_t, off_t);
260 
261 	void *puffs_ops_spare[26];
262 };
263 
264 typedef	int (*pu_pathbuild_fn)(struct puffs_usermount *,
265 			       const struct puffs_pathobj *,
266 			       const struct puffs_pathobj *, size_t,
267 			       struct puffs_pathobj *);
268 typedef int (*pu_pathtransform_fn)(struct puffs_usermount *,
269 				   const struct puffs_pathobj *,
270 				   const struct puffs_cn *,
271 				   struct puffs_pathobj *);
272 typedef int (*pu_pathcmp_fn)(struct puffs_usermount *, struct puffs_pathobj *,
273 			  struct puffs_pathobj *, size_t, int);
274 typedef void (*pu_pathfree_fn)(struct puffs_usermount *,
275 			       struct puffs_pathobj *);
276 typedef int (*pu_namemod_fn)(struct puffs_usermount *,
277 			     struct puffs_pathobj *, struct puffs_cn *);
278 
279 typedef void (*pu_errnotify_fn)(struct puffs_usermount *,
280 				uint8_t, int, const char *, puffs_cookie_t);
281 
282 typedef void (*pu_prepost_fn)(struct puffs_usermount *);
283 
284 typedef struct puffs_node *(*pu_cmap_fn)(struct puffs_usermount *,
285 					 puffs_cookie_t);
286 
287 enum {
288 	PUFFS_STATE_BEFOREMOUNT,	PUFFS_STATE_RUNNING,
289 	PUFFS_STATE_UNMOUNTING,		PUFFS_STATE_UNMOUNTED
290 };
291 
292 #define PUFFS_FLAG_BUILDPATH	0x80000000	/* node paths in pnode */
293 #define PUFFS_FLAG_OPDUMP	0x40000000	/* dump all operations */
294 #define PUFFS_FLAG_HASHPATH	0x20000000	/* speedup: hash paths */
295 #define PUFFS_FLAG_PNCOOKIE	0x10000000	/* cookies are pnodes */
296 #define PUFFS_FLAG_MASK		0xf0000000
297 
298 #define PUFFS_FLAG_KERN(a)	((a) & PUFFS_KFLAG_MASK)
299 #define PUFFS_FLAG_LIB(a)	((a) & PUFFS_FLAG_MASK)
300 
301 /* blocking mode argument */
302 #define PUFFSDEV_BLOCK 0
303 #define PUFFSDEV_NONBLOCK 1
304 
305 #define PUFFS_STACKSIZE_DEFAULT (1<<PUFFS_CC_STACKSHIFT_DEFAULT)
306 #define PUFFS_STACKSIZE_MIN ((size_t)-1)
307 
308 #define		DENT_DOT	0
309 #define		DENT_DOTDOT	1
310 #define		DENT_ADJ(a)	((a)-2)	/* nth request means dir's n-2th */
311 
312 
313 /*
314  * protos
315  */
316 
317 #define PUFFSOP_PROTOS(fsname)						\
318 	int fsname##_fs_unmount(struct puffs_usermount *, int);		\
319 	int fsname##_fs_statvfs(struct puffs_usermount *,		\
320 	    struct puffs_statvfs *);					\
321 	int fsname##_fs_sync(struct puffs_usermount *, int,		\
322 	    const struct puffs_cred *cred);				\
323 	int fsname##_fs_fhtonode(struct puffs_usermount *, void *,	\
324 	    size_t, struct puffs_newinfo *);				\
325 	int fsname##_fs_nodetofh(struct puffs_usermount *,		\
326 	    puffs_cookie_t, void *, size_t *);				\
327 	int fsname##_fs_extattrctl(struct puffs_usermount *, int,	\
328 	    puffs_cookie_t, int, int, const char *);			\
329 									\
330 	int fsname##_node_lookup(struct puffs_usermount *,		\
331 	    puffs_cookie_t, struct puffs_newinfo *,			\
332 	    const struct puffs_cn *);					\
333 	int fsname##_node_create(struct puffs_usermount *,		\
334 	    puffs_cookie_t, struct puffs_newinfo *,			\
335 	    const struct puffs_cn *, const struct vattr *);		\
336 	int fsname##_node_mknod(struct puffs_usermount *,		\
337 	    puffs_cookie_t, struct puffs_newinfo *,			\
338 	    const struct puffs_cn *, const struct vattr *);		\
339 	int fsname##_node_open(struct puffs_usermount *,		\
340 	    puffs_cookie_t, int, const struct puffs_cred *);		\
341 	int fsname##_node_close(struct puffs_usermount *,		\
342 	    puffs_cookie_t, int, const struct puffs_cred *);		\
343 	int fsname##_node_access(struct puffs_usermount *,		\
344 	    puffs_cookie_t, int, const struct puffs_cred *);		\
345 	int fsname##_node_getattr(struct puffs_usermount *,		\
346 	    puffs_cookie_t, struct vattr *, const struct puffs_cred *);	\
347 	int fsname##_node_setattr(struct puffs_usermount *,		\
348 	    puffs_cookie_t, const struct vattr *,			\
349 	    const struct puffs_cred *);					\
350 	int fsname##_node_poll(struct puffs_usermount *,		\
351 	    puffs_cookie_t, int *);					\
352 	int fsname##_node_mmap(struct puffs_usermount *,		\
353 	    puffs_cookie_t, vm_prot_t, const struct puffs_cred *);	\
354 	int fsname##_node_fsync(struct puffs_usermount *,		\
355 	    puffs_cookie_t, const struct puffs_cred *, int,		\
356 	    off_t, off_t);						\
357 	int fsname##_node_seek(struct puffs_usermount *,		\
358 	    puffs_cookie_t, off_t, off_t, const struct puffs_cred *);	\
359 	int fsname##_node_remove(struct puffs_usermount *,		\
360 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);	\
361 	int fsname##_node_link(struct puffs_usermount *,		\
362 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);	\
363 	int fsname##_node_rename(struct puffs_usermount *,		\
364 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *,	\
365 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);	\
366 	int fsname##_node_mkdir(struct puffs_usermount *,		\
367 	    puffs_cookie_t, struct puffs_newinfo *,			\
368 	    const struct puffs_cn *, const struct vattr *);		\
369 	int fsname##_node_rmdir(struct puffs_usermount *,		\
370 	    puffs_cookie_t, puffs_cookie_t, const struct puffs_cn *);	\
371 	int fsname##_node_symlink(struct puffs_usermount *,		\
372 	    puffs_cookie_t, struct puffs_newinfo *,			\
373 	    const struct puffs_cn *, const struct vattr *,		\
374 	    const char *);						\
375 	int fsname##_node_readdir(struct puffs_usermount *,		\
376 	    puffs_cookie_t, struct dirent *, off_t *, size_t *,		\
377 	    const struct puffs_cred *, int *, off_t *, size_t *);	\
378 	int fsname##_node_readlink(struct puffs_usermount *,		\
379 	    puffs_cookie_t, const struct puffs_cred *, char *,		\
380 	    size_t *);							\
381 	int fsname##_node_reclaim(struct puffs_usermount *,		\
382 	    puffs_cookie_t);						\
383 	int fsname##_node_inactive(struct puffs_usermount *,		\
384 	    puffs_cookie_t);						\
385 	int fsname##_node_print(struct puffs_usermount *,		\
386 	    puffs_cookie_t);						\
387 	int fsname##_node_pathconf(struct puffs_usermount *,		\
388 	    puffs_cookie_t, int, __register_t *);				\
389 	int fsname##_node_advlock(struct puffs_usermount *,		\
390 	    puffs_cookie_t, void *, int, struct flock *, int);		\
391 	int fsname##_node_read(struct puffs_usermount *, puffs_cookie_t,\
392 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);\
393 	int fsname##_node_write(struct puffs_usermount *,		\
394 	    puffs_cookie_t, uint8_t *, off_t, size_t *,			\
395 	    const struct puffs_cred *, int);				\
396 	int fsname##_node_abortop(struct puffs_usermount *,		\
397 	    puffs_cookie_t, const struct puffs_cn *);			\
398 	int fsname##_node_getextattr(struct puffs_usermount *,		\
399 	    puffs_cookie_t, int, const char *, size_t *, uint8_t *,	\
400 	    size_t *, const struct puffs_cred *);			\
401 	int fsname##_node_setextattr(struct puffs_usermount *,		\
402 	    puffs_cookie_t, int, const char *, uint8_t *, size_t *,	\
403 	    const struct puffs_cred *);					\
404 	int fsname##_node_listextattr(struct puffs_usermount *,		\
405 	    puffs_cookie_t, int, size_t *, uint8_t *, size_t *,		\
406 	    int, const struct puffs_cred *);				\
407 	int fsname##_node_deleteextattr(struct puffs_usermount *,	\
408 	    puffs_cookie_t, int, const char *,				\
409 	    const struct puffs_cred *);					\
410 	int fsname##_node_getattr_ttl(struct puffs_usermount *,		\
411 	    puffs_cookie_t, struct vattr *, const struct puffs_cred *,	\
412 	    struct timespec *);						\
413 	int fsname##_node_setattr_ttl(struct puffs_usermount *,		\
414 	    puffs_cookie_t, struct vattr *, const struct puffs_cred *,	\
415 	    struct timespec *, int);					\
416 	int fsname##_node_write2(struct puffs_usermount *,		\
417 	    puffs_cookie_t, uint8_t *, off_t, size_t *,			\
418 	    const struct puffs_cred *, int, int);			\
419 	int fsname##_node_reclaim2(struct puffs_usermount *,		\
420 	    puffs_cookie_t, int);					\
421 	int fsname##_node_open2(struct puffs_usermount *,		\
422 	    puffs_cookie_t, int, const struct puffs_cred *, int *);	\
423 	int fsname##_node_fallocate(struct puffs_usermount *,		\
424 	    puffs_cookie_t, off_t, off_t);				\
425 	int fsname##_node_fdiscard(struct puffs_usermount *,		\
426 	    puffs_cookie_t, off_t, off_t);
427 
428 
429 #define PUFFSOP_INIT(ops)						\
430     ops = malloc(sizeof(struct puffs_ops));				\
431     memset(ops, 0, sizeof(struct puffs_ops))
432 #define PUFFSOP_SET(ops, fsname, fsornode, opname)			\
433     (ops)->puffs_##fsornode##_##opname = fsname##_##fsornode##_##opname
434 #define PUFFSOP_SETFSNOP(ops, opname)					\
435     (ops)->puffs_fs_##opname = puffs_fsnop_##opname
436 
437 PUFFSOP_PROTOS(puffs_null)	/* XXX */
438 
439 #define PNPATH(pnode)	((pnode)->pn_po.po_path)
440 #define PNPLEN(pnode)	((pnode)->pn_po.po_len)
441 #define PCNPATH(pcnode)	((pcnode)->pcn_po_full.po_path)
442 #define PCNPLEN(pcnode)	((pcnode)->pcn_po_full.po_len)
443 #define PCNISDOTDOT(pcnode) \
444 	((pcnode)->pcn_namelen == 2 && strcmp((pcnode)->pcn_name, "..") == 0)
445 
446 #define PUFFS_STORE_DCOOKIE(cp, ncp, off)				\
447 if (cp)	{								\
448 	*((cp)++) = off;						\
449 	(*(ncp))++;							\
450 }
451 
452 /* mainloop */
453 typedef void (*puffs_ml_loop_fn)(struct puffs_usermount *);
454 
455 /* framebuf stuff */
456 struct puffs_framebuf;
457 typedef int (*puffs_framev_readframe_fn)(struct puffs_usermount *,
458 					   struct puffs_framebuf *,
459 					   int, int *);
460 typedef	int (*puffs_framev_writeframe_fn)(struct puffs_usermount *,
461 					    struct puffs_framebuf *,
462 					    int, int *);
463 typedef int (*puffs_framev_cmpframe_fn)(struct puffs_usermount *,
464 					 struct puffs_framebuf *,
465 					 struct puffs_framebuf *,
466 					 int *);
467 typedef void (*puffs_framev_fdnotify_fn)(struct puffs_usermount *, int, int);
468 typedef void (*puffs_framev_gotframe_fn)(struct puffs_usermount *,
469 					struct puffs_framebuf *);
470 typedef void (*puffs_framev_cb)(struct puffs_usermount *,
471 				struct puffs_framebuf *,
472 				void *, int);
473 #define PUFFS_FBIO_READ		0x01
474 #define PUFFS_FBIO_WRITE	0x02
475 #define PUFFS_FBIO_ERROR	0x04
476 
477 #define PUFFS_FBQUEUE_URGENT	0x01
478 
479 
480 __BEGIN_DECLS
481 
482 #define PUFFS_DEFER ((void *)-1)
483 struct puffs_usermount *puffs_init(struct puffs_ops *, const char *,
484 				    const char *, void *, uint32_t);
485 int		puffs_mount(struct puffs_usermount *, const char *, int, void*);
486 int		puffs_exit(struct puffs_usermount *, int);
487 void		puffs_cancel(struct puffs_usermount *, int);
488 int		puffs_mainloop(struct puffs_usermount *);
489 int		puffs_daemon(struct puffs_usermount *, int, int);
490 
491 int		puffs_unmountonsignal(int, bool);
492 
493 
494 int	puffs_getselectable(struct puffs_usermount *);
495 int	puffs_setblockingmode(struct puffs_usermount *, int);
496 int	puffs_getstate(struct puffs_usermount *);
497 void	puffs_setstacksize(struct puffs_usermount *, size_t);
498 
499 void	puffs_ml_setloopfn(struct puffs_usermount *, puffs_ml_loop_fn);
500 void	puffs_ml_settimeout(struct puffs_usermount *, struct timespec *);
501 
502 void			puffs_setroot(struct puffs_usermount *,
503 				      struct puffs_node *);
504 struct puffs_node 	*puffs_getroot(struct puffs_usermount *);
505 void			puffs_setrootinfo(struct puffs_usermount *,
506 					  enum vtype, size_t, dev_t);
507 
508 void			*puffs_getspecific(struct puffs_usermount *);
509 void			puffs_setspecific(struct puffs_usermount *, void *);
510 void			puffs_setmaxreqlen(struct puffs_usermount *, size_t);
511 size_t			puffs_getmaxreqlen(struct puffs_usermount *);
512 void			puffs_setfhsize(struct puffs_usermount *, size_t, int);
513 void			puffs_setmntinfo(struct puffs_usermount *,
514 					 const char *, const char *);
515 
516 void			puffs_setncookiehash(struct puffs_usermount *, int);
517 
518 struct puffs_pathobj	*puffs_getrootpathobj(struct puffs_usermount *);
519 
520 void			puffs_setback(struct puffs_cc *, int);
521 
522 struct puffs_node	*puffs_pn_new(struct puffs_usermount *, void *);
523 void			puffs_pn_remove(struct puffs_node *);
524 void			puffs_pn_put(struct puffs_node *);
525 struct vattr		*puffs_pn_getvap(struct puffs_node *);
526 void *			puffs_pn_getpriv(struct puffs_node *);
527 void			puffs_pn_setpriv(struct puffs_node *, void *);
528 struct puffs_pathobj	*puffs_pn_getpo(struct puffs_node *);
529 struct puffs_usermount	*puffs_pn_getmnt(struct puffs_node *);
530 struct timespec		*puffs_pn_getvattl(struct puffs_node *);
531 struct timespec		*puffs_pn_getcnttl(struct puffs_node *);
532 
533 void	puffs_newinfo_setcookie(struct puffs_newinfo *, puffs_cookie_t);
534 void	puffs_newinfo_setvtype(struct puffs_newinfo *, enum vtype);
535 void	puffs_newinfo_setsize(struct puffs_newinfo *, voff_t);
536 void	puffs_newinfo_setrdev(struct puffs_newinfo *, dev_t);
537 void	puffs_newinfo_setva(struct puffs_newinfo *, struct vattr *);
538 void	puffs_newinfo_setvattl(struct puffs_newinfo *, struct timespec *);
539 void	puffs_newinfo_setcnttl(struct puffs_newinfo *, struct timespec *);
540 
541 void			*puffs_pn_getmntspecific(struct puffs_node *);
542 
543 typedef		void *	(*puffs_nodewalk_fn)(struct puffs_usermount *,
544 					     struct puffs_node *, void *);
545 void			*puffs_pn_nodewalk(struct puffs_usermount *,
546 					   puffs_nodewalk_fn, void *);
547 
548 void			puffs_setvattr(struct vattr *, const struct vattr *);
549 void			puffs_vattr_null(struct vattr *);
550 
551 void			puffs_null_setops(struct puffs_ops *);
552 
553 int			puffs_dispatch_create(struct puffs_usermount *,
554 					      struct puffs_framebuf *,
555 					      struct puffs_cc **);
556 int			puffs_dispatch_exec(struct puffs_cc *,
557 					    struct puffs_framebuf **);
558 
559 /*
560  * generic/dummy routines applicable for some file systems
561  */
562 int  puffs_fsnop_unmount(struct puffs_usermount *, int);
563 int  puffs_fsnop_statvfs(struct puffs_usermount *, struct puffs_statvfs *);
564 void puffs_zerostatvfs(struct puffs_statvfs *);
565 int  puffs_fsnop_sync(struct puffs_usermount *, int waitfor,
566 		      const struct puffs_cred *);
567 
568 int  puffs_genfs_node_getattr(struct puffs_usermount *, puffs_cookie_t,
569 			      struct vattr *, const struct puffs_cred *);
570 int  puffs_genfs_node_reclaim(struct puffs_usermount *, puffs_cookie_t);
571 
572 /*
573  * Subroutine stuff
574  */
575 
576 int		puffs_gendotdent(struct dirent **, ino_t, int, size_t *);
577 int		puffs_nextdent(struct dirent **, const char *, ino_t,
578 			       uint8_t, size_t *);
579 int		puffs_vtype2dt(enum vtype);
580 enum vtype	puffs_mode2vt(mode_t);
581 void		puffs_stat2vattr(struct vattr *va, const struct stat *);
582 mode_t		puffs_addvtype2mode(mode_t, enum vtype);
583 
584 
585 /*
586  * credentials & permissions
587  */
588 
589 /* Credential fetch */
590 int	puffs_cred_getuid(const struct puffs_cred *, uid_t *);
591 int	puffs_cred_getgid(const struct puffs_cred *, gid_t *);
592 int	puffs_cred_getgroups(const struct puffs_cred *, gid_t *, short *);
593 
594 /* Credential check */
595 bool	puffs_cred_isuid(const struct puffs_cred *, uid_t);
596 bool	puffs_cred_hasgroup(const struct puffs_cred *, gid_t);
597 bool	puffs_cred_isregular(const struct puffs_cred *);
598 bool	puffs_cred_iskernel(const struct puffs_cred *);
599 bool	puffs_cred_isfs(const struct puffs_cred *);
600 bool	puffs_cred_isjuggernaut(const struct puffs_cred *);
601 
602 /* misc */
603 int	puffs_access(enum vtype, mode_t, uid_t, gid_t, mode_t,
604 		     const struct puffs_cred *);
605 int	puffs_access_chown(uid_t, gid_t, uid_t, gid_t,
606 			   const struct puffs_cred *);
607 int	puffs_access_chmod(uid_t, gid_t, enum vtype, mode_t,
608 			   const struct puffs_cred *);
609 int	puffs_access_times(uid_t, gid_t, mode_t, int,
610 			   const struct puffs_cred *);
611 
612 
613 /*
614  * Call Context interfaces relevant for user.
615  */
616 
617 void			puffs_cc_yield(struct puffs_cc *);
618 void			puffs_cc_continue(struct puffs_cc *);
619 void			puffs_cc_schedule(struct puffs_cc *);
620 int			puffs_cc_getcaller(struct puffs_cc *,pid_t *,lwpid_t *);
621 struct puffs_cc		*puffs_cc_getcc(struct puffs_usermount *);
622 
623 /*
624  * Flushing / invalidation routines
625  */
626 
627 int	puffs_inval_namecache_dir(struct puffs_usermount *, puffs_cookie_t);
628 int	puffs_inval_namecache_all(struct puffs_usermount *);
629 
630 int	puffs_inval_pagecache_node(struct puffs_usermount *, puffs_cookie_t);
631 int	puffs_inval_pagecache_node_range(struct puffs_usermount *,
632 					 puffs_cookie_t, off_t, off_t);
633 int	puffs_flush_pagecache_node(struct puffs_usermount *, puffs_cookie_t);
634 int	puffs_flush_pagecache_node_range(struct puffs_usermount *,
635 					 puffs_cookie_t, off_t, off_t);
636 
637 /*
638  * Path constructicons
639  */
640 
641 int	puffs_stdpath_buildpath(struct puffs_usermount *,
642 			     const struct puffs_pathobj *,
643 			     const struct puffs_pathobj *, size_t,
644 			     struct puffs_pathobj *);
645 int	puffs_stdpath_cmppath(struct puffs_usermount *, struct puffs_pathobj *,
646 			   struct puffs_pathobj *, size_t, int);
647 void	puffs_stdpath_freepath(struct puffs_usermount *,struct puffs_pathobj *);
648 
649 void	*puffs_path_walkcmp(struct puffs_usermount *,
650 			    struct puffs_node *, void *);
651 void	*puffs_path_prefixadj(struct puffs_usermount *,
652 			      struct puffs_node *, void *);
653 int	puffs_path_pcnbuild(struct puffs_usermount *,
654 			    struct puffs_cn *, void *);
655 void	puffs_path_buildhash(struct puffs_usermount *, struct puffs_pathobj *);
656 void	puffs_set_pathbuild(struct puffs_usermount *, pu_pathbuild_fn); void	puffs_set_pathtransform(struct puffs_usermount *, pu_pathtransform_fn);
657 void	puffs_set_pathcmp(struct puffs_usermount *, pu_pathcmp_fn);
658 void	puffs_set_pathfree(struct puffs_usermount *, pu_pathfree_fn);
659 void	puffs_set_namemod(struct puffs_usermount *, pu_namemod_fn);
660 
661 void	puffs_set_errnotify(struct puffs_usermount *, pu_errnotify_fn);
662 void	puffs_kernerr_log(struct puffs_usermount *, uint8_t, int,
663 			  const char *, puffs_cookie_t);
664 __dead void	puffs_kernerr_abort(struct puffs_usermount *, uint8_t, int,
665 			    const char *, puffs_cookie_t);
666 void	puffs_set_prepost(struct puffs_usermount *,
667 			  pu_prepost_fn, pu_prepost_fn);
668 void	puffs_set_cmap(struct puffs_usermount *, pu_cmap_fn);
669 
670 /*
671  * Suspension
672  */
673 
674 int	puffs_fs_suspend(struct puffs_usermount *);
675 
676 /*
677  * Frame buffering
678  */
679 
680 void	puffs_framev_init(struct puffs_usermount *,
681 			  puffs_framev_readframe_fn,
682 			  puffs_framev_writeframe_fn,
683 			  puffs_framev_cmpframe_fn,
684 			  puffs_framev_gotframe_fn,
685 			  puffs_framev_fdnotify_fn);
686 
687 struct puffs_framebuf 	*puffs_framebuf_make(void);
688 void			puffs_framebuf_destroy(struct puffs_framebuf *);
689 int			puffs_framebuf_dup(struct puffs_framebuf *,
690 					   struct puffs_framebuf **);
691 void			puffs_framebuf_recycle(struct puffs_framebuf *);
692 int			puffs_framebuf_reserve_space(struct puffs_framebuf *,
693 						     size_t);
694 
695 int	puffs_framebuf_putdata(struct puffs_framebuf *, const void *, size_t);
696 int	puffs_framebuf_putdata_atoff(struct puffs_framebuf *, size_t,
697 				     const void *, size_t);
698 int	puffs_framebuf_getdata(struct puffs_framebuf *, void *, size_t);
699 int	puffs_framebuf_getdata_atoff(struct puffs_framebuf *, size_t,
700 				     void *, size_t);
701 
702 size_t	puffs_framebuf_telloff(struct puffs_framebuf *);
703 size_t	puffs_framebuf_tellsize(struct puffs_framebuf *);
704 size_t	puffs_framebuf_remaining(struct puffs_framebuf *);
705 int	puffs_framebuf_seekset(struct puffs_framebuf *, size_t);
706 int	puffs_framebuf_getwindow(struct puffs_framebuf *, size_t,
707 				 void **, size_t *);
708 
709 int	puffs_framev_enqueue_cc(struct puffs_cc *, int,
710 				struct puffs_framebuf *, int);
711 int	puffs_framev_enqueue_cb(struct puffs_usermount *, int,
712 				struct puffs_framebuf *,
713 				puffs_framev_cb, void *, int);
714 int	puffs_framev_enqueue_justsend(struct puffs_usermount *, int,
715 				      struct puffs_framebuf *, int, int);
716 int	puffs_framev_enqueue_directreceive(struct puffs_cc *, int,
717 					   struct puffs_framebuf *, int);
718 int	puffs_framev_enqueue_directsend(struct puffs_cc *, int,
719 					   struct puffs_framebuf *, int);
720 int	puffs_framev_enqueue_waitevent(struct puffs_cc *, int, int *);
721 int	puffs_framev_framebuf_ccpromote(struct puffs_framebuf *,
722 					struct puffs_cc *);
723 
724 int	puffs_framev_addfd(struct puffs_usermount *, int, int);
725 int	puffs_framev_enablefd(struct puffs_usermount *, int, int);
726 int	puffs_framev_disablefd(struct puffs_usermount *, int, int);
727 int	puffs_framev_removefd(struct puffs_usermount *, int, int);
728 void	puffs_framev_removeonclose(struct puffs_usermount *, int, int);
729 void	puffs_framev_unmountonclose(struct puffs_usermount *, int, int);
730 
731 __END_DECLS
732 
733 #endif /* _PUFFS_H_ */
734