puffs.h revision 1.29 1 /* $NetBSD: puffs.h,v 1.29 2007/01/26 23:00:33 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 void (*puffs_fs_suspend)(struct puffs_cc *, int);
136
137 int (*puffs_node_lookup)(struct puffs_cc *,
138 void *, void **, enum vtype *, voff_t *, dev_t *,
139 const struct puffs_cn *);
140 int (*puffs_node_create)(struct puffs_cc *,
141 void *, void **, const struct puffs_cn *, const struct vattr *);
142 int (*puffs_node_mknod)(struct puffs_cc *,
143 void *, void **, const struct puffs_cn *, const struct vattr *);
144 int (*puffs_node_open)(struct puffs_cc *,
145 void *, int, const struct puffs_cred *, pid_t);
146 int (*puffs_node_close)(struct puffs_cc *,
147 void *, int, const struct puffs_cred *, pid_t);
148 int (*puffs_node_access)(struct puffs_cc *,
149 void *, int, const struct puffs_cred *, pid_t);
150 int (*puffs_node_getattr)(struct puffs_cc *,
151 void *, struct vattr *, const struct puffs_cred *, pid_t);
152 int (*puffs_node_setattr)(struct puffs_cc *,
153 void *, const struct vattr *, const struct puffs_cred *, pid_t);
154 int (*puffs_node_poll)(struct puffs_cc *,
155 void *, struct puffs_vnreq_poll *);
156 int (*puffs_node_mmap)(struct puffs_cc *,
157 void *, int, const struct puffs_cred *, pid_t);
158 int (*puffs_node_fsync)(struct puffs_cc *,
159 void *, const struct puffs_cred *, int, off_t, off_t, pid_t);
160 int (*puffs_node_seek)(struct puffs_cc *,
161 void *, off_t, off_t, const struct puffs_cred *);
162 int (*puffs_node_remove)(struct puffs_cc *,
163 void *, void *, const struct puffs_cn *);
164 int (*puffs_node_link)(struct puffs_cc *,
165 void *, void *, const struct puffs_cn *);
166 int (*puffs_node_rename)(struct puffs_cc *,
167 void *, void *, const struct puffs_cn *, void *, void *,
168 const struct puffs_cn *);
169 int (*puffs_node_mkdir)(struct puffs_cc *,
170 void *, void **, const struct puffs_cn *, const struct vattr *);
171 int (*puffs_node_rmdir)(struct puffs_cc *,
172 void *, void *, const struct puffs_cn *);
173 int (*puffs_node_symlink)(struct puffs_cc *,
174 void *, void **, const struct puffs_cn *, const struct vattr *,
175 const char *);
176 int (*puffs_node_readdir)(struct puffs_cc *,
177 void *, struct dirent *, const struct puffs_cred *,
178 off_t *, size_t *);
179 int (*puffs_node_readlink)(struct puffs_cc *,
180 void *, const struct puffs_cred *, char *, size_t *);
181 int (*puffs_node_reclaim)(struct puffs_cc *,
182 void *, pid_t);
183 int (*puffs_node_inactive)(struct puffs_cc *,
184 void *, pid_t, int *);
185 int (*puffs_node_print)(struct puffs_cc *,
186 void *);
187 int (*puffs_node_pathconf)(struct puffs_cc *,
188 void *, int, int *);
189 int (*puffs_node_advlock)(struct puffs_cc *,
190 void *, void *, int, struct flock *, int);
191 int (*puffs_node_getextattr)(struct puffs_cc *,
192 void *, struct puffs_vnreq_getextattr *);
193 int (*puffs_node_setextattr)(struct puffs_cc *,
194 void *, struct puffs_vnreq_setextattr *);
195 int (*puffs_node_listextattr)(struct puffs_cc *,
196 void *, struct puffs_vnreq_listextattr *);
197 int (*puffs_node_read)(struct puffs_cc *, void *,
198 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
199 int (*puffs_node_write)(struct puffs_cc *, void *,
200 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
201 };
202
203 typedef int (*pu_pathbuild_fn)(struct puffs_usermount *, struct puffs_pathobj *,
204 struct puffs_pathobj *, size_t,
205 struct puffs_pathobj *);
206 typedef int (*pu_pathtransform_fn)(struct puffs_usermount *,
207 struct puffs_pathobj *,
208 const struct puffs_cn *,
209 struct puffs_pathobj *);
210 typedef int (*pu_pathcmp_fn)(struct puffs_usermount *, struct puffs_pathobj *,
211 struct puffs_pathobj *, size_t);
212 typedef void (*pu_pathfree_fn)(struct puffs_usermount *,
213 struct puffs_pathobj *);
214 typedef int (*pu_namemod_fn)(struct puffs_usermount *,
215 struct puffs_pathobj *, struct puffs_cn *);
216
217 struct puffs_usermount {
218 struct puffs_ops pu_ops;
219
220 int pu_fd;
221 uint32_t pu_flags;
222 size_t pu_maxreqlen;
223 size_t pu_cc_stacksize;
224
225 int pu_state;
226
227 struct puffs_node *pu_pn_root;
228
229 LIST_HEAD(, puffs_node) pu_pnodelst;
230
231 struct puffs_node *(*pu_cmap)(void *);
232
233 pu_pathbuild_fn pu_pathbuild;
234 pu_pathtransform_fn pu_pathtransform;
235 pu_pathcmp_fn pu_pathcmp;
236 pu_pathfree_fn pu_pathfree;
237 pu_namemod_fn pu_namemod;
238
239 void *pu_privdata;
240 };
241
242 enum {
243 PUFFS_STATE_MOUNTING, PUFFS_STATE_RUNNING,
244 PUFFS_STATE_UNMOUNTING, PUFFS_STATE_UNMOUNTED
245 };
246
247 #define PUFFS_FLAG_KERN(a) ((a) & 0x0000ffff)
248 #define PUFFS_FLAG_LIB(a) ((a) & 0xffff0000)
249
250 #define PUFFS_FLAG_BUILDPATH 0x80000000 /* node paths in pnode */
251 #define PUFFS_FLAG_OPDUMP 0x40000000 /* dump all operations */
252
253 /* blocking mode argument */
254 #define PUFFSDEV_BLOCK 0
255 #define PUFFSDEV_NONBLOCK 1
256
257 /* mainloop flags */
258 #define PUFFSLOOP_NODAEMON 0x01
259
260 int puffs_fsnop_unmount(struct puffs_cc *, int, pid_t);
261 int puffs_fsnop_statvfs(struct puffs_cc *, struct statvfs *, pid_t);
262 void puffs_zerostatvfs(struct statvfs *);
263 int puffs_fsnop_sync(struct puffs_cc *, int waitfor,
264 const struct puffs_cred *, pid_t);
265
266 #define DENT_DOT 0
267 #define DENT_DOTDOT 1
268 #define DENT_ADJ(a) ((a)-2) /* nth request means dir's n-2th */
269
270
271 /*
272 * Operation credentials
273 */
274
275 /* Credential fetch */
276 int puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *);
277 int puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *);
278 int puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *, short *);
279
280 /* Credential check */
281 int puffs_cred_isuid(const struct puffs_cred *pcr, uid_t);
282 int puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t);
283 /* kernel internal NOCRED */
284 int puffs_cred_iskernel(const struct puffs_cred *pcr);
285 /* kernel internal FSCRED */
286 int puffs_cred_isfs(const struct puffs_cred *pcr);
287 /* root || NOCRED || FSCRED */
288 int puffs_cred_isjuggernaut(const struct puffs_cred *pcr);
289
290
291 /*
292 * protos
293 */
294
295 #define PUFFSOP_PROTOS(fsname) \
296 int fsname##_fs_mount(struct puffs_usermount *, void **, \
297 struct statvfs *); \
298 int fsname##_fs_unmount(struct puffs_cc *, int, pid_t); \
299 int fsname##_fs_statvfs(struct puffs_cc *, \
300 struct statvfs *, pid_t); \
301 int fsname##_fs_sync(struct puffs_cc *, int, \
302 const struct puffs_cred *cred, pid_t); \
303 void fsname##_fs_suspend(struct puffs_cc *, int); \
304 \
305 int fsname##_node_lookup(struct puffs_cc *, \
306 void *, void **, enum vtype *, voff_t *, dev_t *, \
307 const struct puffs_cn *); \
308 int fsname##_node_create(struct puffs_cc *, \
309 void *, void **, const struct puffs_cn *, \
310 const struct vattr *); \
311 int fsname##_node_mknod(struct puffs_cc *, \
312 void *, void **, const struct puffs_cn *, \
313 const struct vattr *); \
314 int fsname##_node_open(struct puffs_cc *, \
315 void *, int, const struct puffs_cred *, pid_t); \
316 int fsname##_node_close(struct puffs_cc *, \
317 void *, int, const struct puffs_cred *, pid_t); \
318 int fsname##_node_access(struct puffs_cc *, \
319 void *, int, const struct puffs_cred *, pid_t); \
320 int fsname##_node_getattr(struct puffs_cc *, \
321 void *, struct vattr *, const struct puffs_cred *, pid_t); \
322 int fsname##_node_setattr(struct puffs_cc *, \
323 void *, const struct vattr *, const struct puffs_cred *, \
324 pid_t); \
325 int fsname##_node_poll(struct puffs_cc *, \
326 void *, struct puffs_vnreq_poll *); \
327 int fsname##_node_mmap(struct puffs_cc *, \
328 void *, int, const struct puffs_cred *, pid_t); \
329 int fsname##_node_fsync(struct puffs_cc *, \
330 void *, const struct puffs_cred *, int, off_t, off_t, \
331 pid_t); \
332 int fsname##_node_seek(struct puffs_cc *, \
333 void *, off_t, off_t, const struct puffs_cred *); \
334 int fsname##_node_remove(struct puffs_cc *, \
335 void *, void *, const struct puffs_cn *); \
336 int fsname##_node_link(struct puffs_cc *, \
337 void *, void *, const struct puffs_cn *); \
338 int fsname##_node_rename(struct puffs_cc *, \
339 void *, void *, const struct puffs_cn *, void *, void *, \
340 const struct puffs_cn *); \
341 int fsname##_node_mkdir(struct puffs_cc *, \
342 void *, void **, const struct puffs_cn *, \
343 const struct vattr *); \
344 int fsname##_node_rmdir(struct puffs_cc *, \
345 void *, void *, const struct puffs_cn *); \
346 int fsname##_node_symlink(struct puffs_cc *, \
347 void *, void **, const struct puffs_cn *, \
348 const struct vattr *, const char *); \
349 int fsname##_node_readdir(struct puffs_cc *, \
350 void *, struct dirent *, const struct puffs_cred *, \
351 off_t *, size_t *); \
352 int fsname##_node_readlink(struct puffs_cc *, \
353 void *, const struct puffs_cred *, char *, size_t *); \
354 int fsname##_node_reclaim(struct puffs_cc *, \
355 void *, pid_t); \
356 int fsname##_node_inactive(struct puffs_cc *, \
357 void *, pid_t, int *); \
358 int fsname##_node_print(struct puffs_cc *, \
359 void *); \
360 int fsname##_node_pathconf(struct puffs_cc *, \
361 void *, int, int *); \
362 int fsname##_node_advlock(struct puffs_cc *, \
363 void *, void *, int, struct flock *, int); \
364 int fsname##_node_getextattr(struct puffs_cc *, \
365 void *, struct puffs_vnreq_getextattr *); \
366 int fsname##_node_setextattr(struct puffs_cc *, \
367 void *, struct puffs_vnreq_setextattr *); \
368 int fsname##_node_listextattr(struct puffs_cc *, \
369 void *, struct puffs_vnreq_listextattr *); \
370 int fsname##_node_read(struct puffs_cc *, void *, \
371 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);\
372 int fsname##_node_write(struct puffs_cc *, void *, \
373 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
374
375 #define PUFFSOP_INIT(ops) \
376 ops = malloc(sizeof(struct puffs_ops)); \
377 memset(ops, 0, sizeof(struct puffs_ops))
378 #define PUFFSOP_SET(ops, fsname, fsornode, opname) \
379 (ops)->puffs_##fsornode##_##opname = fsname##_##fsornode##_##opname
380 #define PUFFSOP_SETFSNOP(ops, opname) \
381 (ops)->puffs_fs_##opname = puffs_fsnop_##opname
382
383 #define PUFFS_DEVEL_LIBVERSION 6
384 #define puffs_mount(a,b,c,d,e,f,g) \
385 _puffs_mount(PUFFS_DEVEL_LIBVERSION,a,b,c,d,e,f,g)
386
387
388 #define PNPATH(pnode) ((pnode)->pn_po.po_path)
389 #define PNPLEN(pnode) ((pnode)->pn_po.po_len)
390 #define PCNPATH(pcnode) ((pcnode)->pcn_po_full.po_path)
391 #define PCNPLEN(pcnode) ((pcnode)->pcn_po_full.po_len)
392
393 __BEGIN_DECLS
394
395 struct puffs_usermount *_puffs_mount(int, struct puffs_ops *, const char *, int,
396 const char *, void *, uint32_t, size_t);
397 int puffs_start(struct puffs_usermount *, void *, struct statvfs *);
398 int puffs_exit(struct puffs_usermount *, int);
399 int puffs_mainloop(struct puffs_usermount *, int);
400
401
402 int puffs_getselectable(struct puffs_usermount *);
403 int puffs_setblockingmode(struct puffs_usermount *, int);
404 int puffs_getstate(struct puffs_usermount *);
405 void puffs_setstacksize(struct puffs_usermount *, size_t);
406
407 struct puffs_pathobj *puffs_getrootpathobj(struct puffs_usermount *);
408
409 struct puffs_node *puffs_pn_new(struct puffs_usermount *, void *);
410 void puffs_pn_put(struct puffs_node *);
411
412 typedef void * (*puffs_nodewalk_fn)(struct puffs_usermount *,
413 struct puffs_node *, void *);
414 void *puffs_pn_nodewalk(struct puffs_usermount *,
415 puffs_nodewalk_fn, void *);
416
417 void puffs_setvattr(struct vattr *, const struct vattr *);
418 void puffs_vattr_null(struct vattr *);
419
420 /*
421 * Subroutine stuff
422 */
423
424 int puffs_gendotdent(struct dirent **, ino_t, int, size_t *);
425 int puffs_nextdent(struct dirent **, const char *, ino_t,
426 uint8_t, size_t *);
427 int puffs_vtype2dt(enum vtype);
428 enum vtype puffs_mode2vt(mode_t);
429 void puffs_stat2vattr(struct vattr *va, const struct stat *);
430
431 /*
432 * Requests
433 */
434
435 struct puffs_getreq *puffs_req_makeget(struct puffs_usermount *,
436 size_t, int);
437 int puffs_req_loadget(struct puffs_getreq *);
438 struct puffs_req *puffs_req_get(struct puffs_getreq *);
439 int puffs_req_remainingget(struct puffs_getreq *);
440 void puffs_req_setmaxget(struct puffs_getreq *, int);
441 void puffs_req_destroyget(struct puffs_getreq *);
442
443 struct puffs_putreq *puffs_req_makeput(struct puffs_usermount *);
444 void puffs_req_put(struct puffs_putreq *,struct puffs_req *);
445 void puffs_req_putcc(struct puffs_putreq *,struct puffs_cc*);
446 int puffs_req_putput(struct puffs_putreq *);
447 void puffs_req_resetput(struct puffs_putreq *);
448 void puffs_req_destroyput(struct puffs_putreq *);
449
450 int puffs_req_handle(struct puffs_usermount *,
451 struct puffs_getreq *,
452 struct puffs_putreq *, int);
453
454 /*
455 * Call Context interfaces relevant for user.
456 */
457
458 void puffs_cc_yield(struct puffs_cc *);
459 void puffs_cc_continue(struct puffs_cc *);
460 struct puffs_usermount *puffs_cc_getusermount(struct puffs_cc *);
461 struct puffs_cc *puffs_cc_create(struct puffs_usermount *);
462 void puffs_cc_destroy(struct puffs_cc *);
463
464 /*
465 * Execute or continue a request
466 */
467
468 int puffs_dopreq(struct puffs_usermount *, struct puffs_req *,
469 struct puffs_putreq *);
470 int puffs_docc(struct puffs_cc *, struct puffs_putreq *);
471
472 /*
473 * Flushing / invalidation routines
474 */
475
476 int puffs_inval_namecache_dir(struct puffs_usermount *, void *);
477 int puffs_inval_namecache_all(struct puffs_usermount *);
478
479 /*
480 * Path constructicons
481 */
482
483 int puffs_path_buildpath(struct puffs_usermount *, struct puffs_pathobj *,
484 struct puffs_pathobj *, size_t,
485 struct puffs_pathobj *);
486 int puffs_path_cmppath(struct puffs_usermount *, struct puffs_pathobj *,
487 struct puffs_pathobj *, size_t);
488 int puffs_path_transformpath(struct puffs_usermount *,
489 struct puffs_pathobj *, const struct puffs_cn *,
490 struct puffs_pathobj *);
491 void puffs_path_freepath(struct puffs_usermount *, struct puffs_pathobj *);
492 int puffs_path_modname(struct puffs_usermount *, struct puffs_pathobj *,
493 struct puffs_cn *);
494
495 void *puffs_path_prefixadj(struct puffs_usermount *,
496 struct puffs_node *, void *);
497 int puffs_path_pcnbuild(struct puffs_usermount *,
498 struct puffs_cn *, void *);
499
500 void puffs_set_pathbuild(struct puffs_usermount *, pu_pathbuild_fn);
501 void puffs_set_pathtransform(struct puffs_usermount *, pu_pathtransform_fn);
502 void puffs_set_pathcmp(struct puffs_usermount *, pu_pathcmp_fn);
503 void puffs_set_pathfree(struct puffs_usermount *, pu_pathfree_fn);
504 void puffs_set_namemod(struct puffs_usermount *, pu_namemod_fn);
505
506 /*
507 * Suspension
508 */
509
510 int puffs_fs_suspend(struct puffs_usermount *);
511
512 __END_DECLS
513
514 #endif /* _PUFFS_H_ */
515