puffs.h revision 1.27 1 /* $NetBSD: puffs.h,v 1.27 2007/01/20 13:52:14 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006 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 * 3. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef _PUFFS_H_
36 #define _PUFFS_H_
37
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 #include <sys/statvfs.h>
42 #include <sys/vnode.h>
43
44 #include <fs/puffs/puffs_msgif.h>
45
46 #include <mntopts.h>
47 #include <string.h>
48
49 /* forwards */
50 struct puffs_cc;
51
52 struct puffs_getreq;
53 struct puffs_putreq;
54
55 /* paths */
56 struct puffs_pathobj {
57 void *po_path;
58 size_t po_len;
59 };
60
61 /* for prefix rename */
62 struct puffs_pathinfo {
63 struct puffs_pathobj *pi_old;
64 struct puffs_pathobj *pi_new;
65 };
66
67 /* XXX: might disappear from here into a private header */
68 struct puffs_node {
69 off_t pn_size;
70 int pn_flag;
71 struct vattr pn_va;
72
73 void *pn_data; /* private data */
74
75 struct puffs_pathobj pn_po; /* PUFFS_FLAG_BUILDPATH */
76
77 struct puffs_usermount *pn_mnt;
78 LIST_ENTRY(puffs_node) pn_entries;
79 };
80
81
82 struct puffs_usermount;
83
84 /*
85 * megaXXX: these are values from inside _KERNEL
86 * need to work on the translation for ALL the necessary values.
87 */
88 #define PUFFS_VNOVAL (-1)
89 #define PUFFS_ISDOTDOT 0x2000
90 #define PUFFS_IO_APPEND 0x20
91
92 #define PUFFS_FSYNC_DATAONLY 0x0002
93 #define PUFFS_FSYNC_CACHE 0x0100
94
95 /*
96 * Magic constants
97 */
98 #define PUFFS_CC_STACKSIZE_DEFAULT (1024*1024)
99
100 struct puffs_cn {
101 struct puffs_kcn *pcn_pkcnp; /* kernel input */
102 struct puffs_pathobj pcn_po_full; /* PUFFS_FLAG_BUILDPATH */
103 };
104 #define pcn_nameiop pcn_pkcnp->pkcn_nameiop
105 #define pcn_flags pcn_pkcnp->pkcn_flags
106 #define pcn_pid pcn_pkcnp->pkcn_pid
107 #define pcn_cred pcn_pkcnp->pkcn_cred
108 #define pcn_name pcn_pkcnp->pkcn_name
109 #define pcn_namelen pcn_pkcnp->pkcn_namelen
110
111 /*
112 * Puffs options to mount
113 */
114 /* kernel */
115 #define PUFFSMOPT_CACHE { "cache", 1, PUFFS_KFLAG_NOCACHE, 1 }
116 #define PUFFSMOPT_ALLOPS { "allops", 0, PUFFS_KFLAG_ALLOPS, 1 }
117
118 /* libpuffs */
119 #define PUFFSMOPT_DUMP { "dump", 0, PUFFS_FLAG_OPDUMP, 1 }
120
121 #define PUFFSMOPT_STD \
122 PUFFSMOPT_CACHE, \
123 PUFFSMOPT_ALLOPS, \
124 PUFFSMOPT_DUMP
125
126 extern const struct mntopt puffsmopts[]; /* puffs.c */
127
128 /* callbacks for operations */
129 struct puffs_ops {
130 int (*puffs_fs_unmount)(struct puffs_cc *, int, pid_t);
131 int (*puffs_fs_statvfs)(struct puffs_cc *,
132 struct statvfs *, pid_t);
133 int (*puffs_fs_sync)(struct puffs_cc *, int,
134 const struct puffs_cred *, pid_t);
135
136 int (*puffs_node_lookup)(struct puffs_cc *,
137 void *, void **, enum vtype *, voff_t *, dev_t *,
138 const struct puffs_cn *);
139 int (*puffs_node_create)(struct puffs_cc *,
140 void *, void **, const struct puffs_cn *, const struct vattr *);
141 int (*puffs_node_mknod)(struct puffs_cc *,
142 void *, void **, const struct puffs_cn *, const struct vattr *);
143 int (*puffs_node_open)(struct puffs_cc *,
144 void *, int, const struct puffs_cred *, pid_t);
145 int (*puffs_node_close)(struct puffs_cc *,
146 void *, int, const struct puffs_cred *, pid_t);
147 int (*puffs_node_access)(struct puffs_cc *,
148 void *, int, const struct puffs_cred *, pid_t);
149 int (*puffs_node_getattr)(struct puffs_cc *,
150 void *, struct vattr *, const struct puffs_cred *, pid_t);
151 int (*puffs_node_setattr)(struct puffs_cc *,
152 void *, const struct vattr *, const struct puffs_cred *, pid_t);
153 int (*puffs_node_poll)(struct puffs_cc *,
154 void *, struct puffs_vnreq_poll *);
155 int (*puffs_node_mmap)(struct puffs_cc *,
156 void *, int, const struct puffs_cred *, pid_t);
157 int (*puffs_node_fsync)(struct puffs_cc *,
158 void *, const struct puffs_cred *, int, off_t, off_t, pid_t);
159 int (*puffs_node_seek)(struct puffs_cc *,
160 void *, off_t, off_t, const struct puffs_cred *);
161 int (*puffs_node_remove)(struct puffs_cc *,
162 void *, void *, const struct puffs_cn *);
163 int (*puffs_node_link)(struct puffs_cc *,
164 void *, void *, const struct puffs_cn *);
165 int (*puffs_node_rename)(struct puffs_cc *,
166 void *, void *, const struct puffs_cn *, void *, void *,
167 const struct puffs_cn *);
168 int (*puffs_node_mkdir)(struct puffs_cc *,
169 void *, void **, const struct puffs_cn *, const struct vattr *);
170 int (*puffs_node_rmdir)(struct puffs_cc *,
171 void *, void *, const struct puffs_cn *);
172 int (*puffs_node_symlink)(struct puffs_cc *,
173 void *, void **, const struct puffs_cn *, const struct vattr *,
174 const char *);
175 int (*puffs_node_readdir)(struct puffs_cc *,
176 void *, struct dirent *, const struct puffs_cred *,
177 off_t *, size_t *);
178 int (*puffs_node_readlink)(struct puffs_cc *,
179 void *, const struct puffs_cred *, char *, size_t *);
180 int (*puffs_node_reclaim)(struct puffs_cc *,
181 void *, pid_t);
182 int (*puffs_node_inactive)(struct puffs_cc *,
183 void *, pid_t, int *);
184 int (*puffs_node_print)(struct puffs_cc *,
185 void *);
186 int (*puffs_node_pathconf)(struct puffs_cc *,
187 void *, int, int *);
188 int (*puffs_node_advlock)(struct puffs_cc *,
189 void *, void *, int, struct flock *, int);
190 int (*puffs_node_getextattr)(struct puffs_cc *,
191 void *, struct puffs_vnreq_getextattr *);
192 int (*puffs_node_setextattr)(struct puffs_cc *,
193 void *, struct puffs_vnreq_setextattr *);
194 int (*puffs_node_listextattr)(struct puffs_cc *,
195 void *, struct puffs_vnreq_listextattr *);
196 int (*puffs_node_read)(struct puffs_cc *, void *,
197 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
198 int (*puffs_node_write)(struct puffs_cc *, void *,
199 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
200 };
201
202 typedef int (*pu_pathbuild_fn)(struct puffs_usermount *, struct puffs_pathobj *,
203 struct puffs_pathobj *, size_t,
204 struct puffs_pathobj *);
205 typedef int (*pu_pathtransform_fn)(struct puffs_usermount *,
206 struct puffs_pathobj *,
207 const struct puffs_cn *,
208 struct puffs_pathobj *);
209 typedef int (*pu_pathcmp_fn)(struct puffs_usermount *, struct puffs_pathobj *,
210 struct puffs_pathobj *, size_t);
211 typedef void (*pu_pathfree_fn)(struct puffs_usermount *,
212 struct puffs_pathobj *);
213 typedef int (*pu_namemod_fn)(struct puffs_usermount *,
214 struct puffs_pathobj *, struct puffs_cn *);
215
216 struct puffs_usermount {
217 struct puffs_ops pu_ops;
218
219 int pu_fd;
220 uint32_t pu_flags;
221 size_t pu_maxreqlen;
222 size_t pu_cc_stacksize;
223
224 int pu_state;
225
226 struct puffs_node *pu_pn_root;
227
228 LIST_HEAD(, puffs_node) pu_pnodelst;
229
230 struct puffs_node *(*pu_cmap)(void *);
231
232 pu_pathbuild_fn pu_pathbuild;
233 pu_pathtransform_fn pu_pathtransform;
234 pu_pathcmp_fn pu_pathcmp;
235 pu_pathfree_fn pu_pathfree;
236 pu_namemod_fn pu_namemod;
237
238 void *pu_privdata;
239 };
240
241 enum {
242 PUFFS_STATE_MOUNTING, PUFFS_STATE_RUNNING,
243 PUFFS_STATE_UNMOUNTING, PUFFS_STATE_UNMOUNTED
244 };
245
246 #define PUFFS_FLAG_KERN(a) ((a) & 0x0000ffff)
247 #define PUFFS_FLAG_LIB(a) ((a) & 0xffff0000)
248
249 #define PUFFS_FLAG_BUILDPATH 0x80000000 /* node paths in pnode */
250 #define PUFFS_FLAG_OPDUMP 0x40000000 /* dump all operations */
251
252 /* blocking mode argument */
253 #define PUFFSDEV_BLOCK 0
254 #define PUFFSDEV_NONBLOCK 1
255
256 /* mainloop flags */
257 #define PUFFSLOOP_NODAEMON 0x01
258
259 int puffs_fsnop_unmount(struct puffs_cc *, int, pid_t);
260 int puffs_fsnop_statvfs(struct puffs_cc *, struct statvfs *, pid_t);
261 void puffs_zerostatvfs(struct statvfs *);
262 int puffs_fsnop_sync(struct puffs_cc *, int waitfor,
263 const struct puffs_cred *, pid_t);
264
265 #define DENT_DOT 0
266 #define DENT_DOTDOT 1
267 #define DENT_ADJ(a) ((a)-2) /* nth request means dir's n-2th */
268
269
270 /*
271 * Operation credentials
272 */
273
274 /* Credential fetch */
275 int puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *);
276 int puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *);
277 int puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *, short *);
278
279 /* Credential check */
280 int puffs_cred_isuid(const struct puffs_cred *pcr, uid_t);
281 int puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t);
282 /* kernel internal NOCRED */
283 int puffs_cred_iskernel(const struct puffs_cred *pcr);
284 /* kernel internal FSCRED */
285 int puffs_cred_isfs(const struct puffs_cred *pcr);
286 /* root || NOCRED || FSCRED */
287 int puffs_cred_isjuggernaut(const struct puffs_cred *pcr);
288
289
290 /*
291 * protos
292 */
293
294 #define PUFFSOP_PROTOS(fsname) \
295 int fsname##_fs_mount(struct puffs_usermount *, void **, \
296 struct statvfs *); \
297 int fsname##_fs_unmount(struct puffs_cc *, int, pid_t); \
298 int fsname##_fs_statvfs(struct puffs_cc *, \
299 struct statvfs *, pid_t); \
300 int fsname##_fs_sync(struct puffs_cc *, int, \
301 const struct puffs_cred *cred, pid_t); \
302 \
303 int fsname##_node_lookup(struct puffs_cc *, \
304 void *, void **, enum vtype *, voff_t *, dev_t *, \
305 const struct puffs_cn *); \
306 int fsname##_node_create(struct puffs_cc *, \
307 void *, void **, const struct puffs_cn *, \
308 const struct vattr *); \
309 int fsname##_node_mknod(struct puffs_cc *, \
310 void *, void **, const struct puffs_cn *, \
311 const struct vattr *); \
312 int fsname##_node_open(struct puffs_cc *, \
313 void *, int, const struct puffs_cred *, pid_t); \
314 int fsname##_node_close(struct puffs_cc *, \
315 void *, int, const struct puffs_cred *, pid_t); \
316 int fsname##_node_access(struct puffs_cc *, \
317 void *, int, const struct puffs_cred *, pid_t); \
318 int fsname##_node_getattr(struct puffs_cc *, \
319 void *, struct vattr *, const struct puffs_cred *, pid_t); \
320 int fsname##_node_setattr(struct puffs_cc *, \
321 void *, const struct vattr *, const struct puffs_cred *, \
322 pid_t); \
323 int fsname##_node_poll(struct puffs_cc *, \
324 void *, struct puffs_vnreq_poll *); \
325 int fsname##_node_mmap(struct puffs_cc *, \
326 void *, int, const struct puffs_cred *, pid_t); \
327 int fsname##_node_fsync(struct puffs_cc *, \
328 void *, const struct puffs_cred *, int, off_t, off_t, \
329 pid_t); \
330 int fsname##_node_seek(struct puffs_cc *, \
331 void *, off_t, off_t, const struct puffs_cred *); \
332 int fsname##_node_remove(struct puffs_cc *, \
333 void *, void *, const struct puffs_cn *); \
334 int fsname##_node_link(struct puffs_cc *, \
335 void *, void *, const struct puffs_cn *); \
336 int fsname##_node_rename(struct puffs_cc *, \
337 void *, void *, const struct puffs_cn *, void *, void *, \
338 const struct puffs_cn *); \
339 int fsname##_node_mkdir(struct puffs_cc *, \
340 void *, void **, const struct puffs_cn *, \
341 const struct vattr *); \
342 int fsname##_node_rmdir(struct puffs_cc *, \
343 void *, void *, const struct puffs_cn *); \
344 int fsname##_node_symlink(struct puffs_cc *, \
345 void *, void **, const struct puffs_cn *, \
346 const struct vattr *, const char *); \
347 int fsname##_node_readdir(struct puffs_cc *, \
348 void *, struct dirent *, const struct puffs_cred *, \
349 off_t *, size_t *); \
350 int fsname##_node_readlink(struct puffs_cc *, \
351 void *, const struct puffs_cred *, char *, size_t *); \
352 int fsname##_node_reclaim(struct puffs_cc *, \
353 void *, pid_t); \
354 int fsname##_node_inactive(struct puffs_cc *, \
355 void *, pid_t, int *); \
356 int fsname##_node_print(struct puffs_cc *, \
357 void *); \
358 int fsname##_node_pathconf(struct puffs_cc *, \
359 void *, int, int *); \
360 int fsname##_node_advlock(struct puffs_cc *, \
361 void *, void *, int, struct flock *, int); \
362 int fsname##_node_getextattr(struct puffs_cc *, \
363 void *, struct puffs_vnreq_getextattr *); \
364 int fsname##_node_setextattr(struct puffs_cc *, \
365 void *, struct puffs_vnreq_setextattr *); \
366 int fsname##_node_listextattr(struct puffs_cc *, \
367 void *, struct puffs_vnreq_listextattr *); \
368 int fsname##_node_read(struct puffs_cc *, void *, \
369 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);\
370 int fsname##_node_write(struct puffs_cc *, void *, \
371 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
372
373 #define PUFFSOP_INIT(ops) \
374 ops = malloc(sizeof(struct puffs_ops)); \
375 memset(ops, 0, sizeof(struct puffs_ops))
376 #define PUFFSOP_SET(ops, fsname, fsornode, opname) \
377 (ops)->puffs_##fsornode##_##opname = fsname##_##fsornode##_##opname
378 #define PUFFSOP_SETFSNOP(ops, opname) \
379 (ops)->puffs_fs_##opname = puffs_fsnop_##opname
380
381 #define PUFFS_DEVEL_LIBVERSION 5
382 #define puffs_mount(a,b,c,d,e,f,g) \
383 _puffs_mount(PUFFS_DEVEL_LIBVERSION,a,b,c,d,e,f,g)
384
385
386 #define PNPATH(pnode) ((pnode)->pn_po.po_path)
387 #define PNPLEN(pnode) ((pnode)->pn_po.po_len)
388 #define PCNPATH(pcnode) ((pcnode)->pcn_po_full.po_path)
389 #define PCNPLEN(pcnode) ((pcnode)->pcn_po_full.po_len)
390
391 __BEGIN_DECLS
392
393 struct puffs_usermount *_puffs_mount(int, struct puffs_ops *, const char *, int,
394 const char *, void *, uint32_t, size_t);
395 int puffs_start(struct puffs_usermount *, void *, struct statvfs *);
396 int puffs_exit(struct puffs_usermount *, int);
397 int puffs_mainloop(struct puffs_usermount *, int);
398
399
400 int puffs_getselectable(struct puffs_usermount *);
401 int puffs_setblockingmode(struct puffs_usermount *, int);
402 int puffs_getstate(struct puffs_usermount *);
403 void puffs_setstacksize(struct puffs_usermount *, size_t);
404
405 struct puffs_pathobj *puffs_getrootpathobj(struct puffs_usermount *);
406
407 struct puffs_node *puffs_pn_new(struct puffs_usermount *, void *);
408 void puffs_pn_put(struct puffs_node *);
409
410 typedef void * (*puffs_nodewalk_fn)(struct puffs_usermount *,
411 struct puffs_node *, void *);
412 void *puffs_pn_nodewalk(struct puffs_usermount *,
413 puffs_nodewalk_fn, void *);
414
415 void puffs_setvattr(struct vattr *, const struct vattr *);
416 void puffs_vattr_null(struct vattr *);
417
418 /*
419 * Subroutine stuff
420 */
421
422 int puffs_gendotdent(struct dirent **, ino_t, int, size_t *);
423 int puffs_nextdent(struct dirent **, const char *, ino_t,
424 uint8_t, size_t *);
425 int puffs_vtype2dt(enum vtype);
426 enum vtype puffs_mode2vt(mode_t);
427 void puffs_stat2vattr(struct vattr *va, const struct stat *);
428
429 /*
430 * Requests
431 */
432
433 struct puffs_getreq *puffs_req_makeget(struct puffs_usermount *,
434 size_t, int);
435 int puffs_req_loadget(struct puffs_getreq *);
436 struct puffs_req *puffs_req_get(struct puffs_getreq *);
437 int puffs_req_remainingget(struct puffs_getreq *);
438 void puffs_req_setmaxget(struct puffs_getreq *, int);
439 void puffs_req_destroyget(struct puffs_getreq *);
440
441 struct puffs_putreq *puffs_req_makeput(struct puffs_usermount *);
442 void puffs_req_put(struct puffs_putreq *,struct puffs_req *);
443 void puffs_req_putcc(struct puffs_putreq *,struct puffs_cc*);
444 int puffs_req_putput(struct puffs_putreq *);
445 void puffs_req_resetput(struct puffs_putreq *);
446 void puffs_req_destroyput(struct puffs_putreq *);
447
448 int puffs_req_handle(struct puffs_usermount *,
449 struct puffs_getreq *,
450 struct puffs_putreq *, int);
451
452 /*
453 * Call Context interfaces relevant for user.
454 */
455
456 void puffs_cc_yield(struct puffs_cc *);
457 void puffs_cc_continue(struct puffs_cc *);
458 struct puffs_usermount *puffs_cc_getusermount(struct puffs_cc *);
459 struct puffs_cc *puffs_cc_create(struct puffs_usermount *);
460 void puffs_cc_destroy(struct puffs_cc *);
461
462 /*
463 * Execute or continue a request
464 */
465
466 int puffs_dopreq(struct puffs_usermount *, struct puffs_putreq *,
467 struct puffs_req *);
468 int puffs_docc(struct puffs_putreq *, struct puffs_cc *);
469
470 /*
471 * Flushing / invalidation routines
472 */
473
474 int puffs_inval_namecache_dir(struct puffs_usermount *, void *);
475 int puffs_inval_namecache_all(struct puffs_usermount *);
476
477 /*
478 * Path constructicons
479 */
480
481 int puffs_path_buildpath(struct puffs_usermount *, struct puffs_pathobj *,
482 struct puffs_pathobj *, size_t,
483 struct puffs_pathobj *);
484 int puffs_path_cmppath(struct puffs_usermount *, struct puffs_pathobj *,
485 struct puffs_pathobj *, size_t);
486 int puffs_path_transformpath(struct puffs_usermount *,
487 struct puffs_pathobj *, const struct puffs_cn *,
488 struct puffs_pathobj *);
489 void puffs_path_freepath(struct puffs_usermount *, struct puffs_pathobj *);
490 int puffs_path_modname(struct puffs_usermount *, struct puffs_pathobj *,
491 struct puffs_cn *);
492
493 void *puffs_path_prefixadj(struct puffs_usermount *,
494 struct puffs_node *, void *);
495 int puffs_path_pcnbuild(struct puffs_usermount *,
496 struct puffs_cn *, void *);
497
498 void puffs_set_pathbuild(struct puffs_usermount *, pu_pathbuild_fn);
499 void puffs_set_pathtransform(struct puffs_usermount *, pu_pathtransform_fn);
500 void puffs_set_pathcmp(struct puffs_usermount *, pu_pathcmp_fn);
501 void puffs_set_pathfree(struct puffs_usermount *, pu_pathfree_fn);
502 void puffs_set_namemod(struct puffs_usermount *, pu_namemod_fn);
503
504 __END_DECLS
505
506 #endif /* _PUFFS_H_ */
507