puffs.h revision 1.31 1 /* $NetBSD: puffs.h,v 1.31 2007/02/15 17:04:46 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/types.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 #include <sys/statvfs.h>
43 #include <sys/vnode.h>
44
45 #include <fs/puffs/puffs_msgif.h>
46
47 #include <mntopts.h>
48 #include <string.h>
49
50 /* forwards */
51 struct puffs_cc;
52
53 struct puffs_getreq;
54 struct puffs_putreq;
55
56 /* paths */
57 struct puffs_pathobj {
58 void *po_path;
59 size_t po_len;
60 };
61
62 /* for prefix rename */
63 struct puffs_pathinfo {
64 struct puffs_pathobj *pi_old;
65 struct puffs_pathobj *pi_new;
66 };
67
68 /* XXX: might disappear from here into a private header */
69 struct puffs_node {
70 off_t pn_size;
71 int pn_flag;
72 struct vattr pn_va;
73
74 void *pn_data; /* private data */
75
76 struct puffs_pathobj pn_po; /* PUFFS_FLAG_BUILDPATH */
77
78 struct puffs_usermount *pn_mnt;
79 LIST_ENTRY(puffs_node) pn_entries;
80 };
81
82
83 struct puffs_usermount;
84
85 /*
86 * megaXXX: these are values from inside _KERNEL
87 * need to work on the translation for ALL the necessary values.
88 */
89 #define PUFFS_VNOVAL (-1)
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 *,
204 const struct puffs_pathobj *,
205 const struct puffs_pathobj *, size_t,
206 struct puffs_pathobj *);
207 typedef int (*pu_pathtransform_fn)(struct puffs_usermount *,
208 const struct puffs_pathobj *,
209 const struct puffs_cn *,
210 struct puffs_pathobj *);
211 typedef int (*pu_pathcmp_fn)(struct puffs_usermount *, struct puffs_pathobj *,
212 struct puffs_pathobj *, size_t, int);
213 typedef void (*pu_pathfree_fn)(struct puffs_usermount *,
214 struct puffs_pathobj *);
215 typedef int (*pu_namemod_fn)(struct puffs_usermount *,
216 struct puffs_pathobj *, struct puffs_cn *);
217
218 struct puffs_usermount {
219 struct puffs_ops pu_ops;
220
221 int pu_fd;
222 uint32_t pu_flags;
223 size_t pu_maxreqlen;
224 size_t pu_cc_stacksize;
225
226 int pu_state;
227
228 struct puffs_node *pu_pn_root;
229
230 LIST_HEAD(, puffs_node) pu_pnodelst;
231
232 struct puffs_node *(*pu_cmap)(void *);
233
234 pu_pathbuild_fn pu_pathbuild;
235 pu_pathtransform_fn pu_pathtransform;
236 pu_pathcmp_fn pu_pathcmp;
237 pu_pathfree_fn pu_pathfree;
238 pu_namemod_fn pu_namemod;
239
240 void *pu_privdata;
241 };
242
243 enum {
244 PUFFS_STATE_MOUNTING, PUFFS_STATE_RUNNING,
245 PUFFS_STATE_UNMOUNTING, PUFFS_STATE_UNMOUNTED
246 };
247
248 #define PUFFS_FLAG_KERN(a) ((a) & 0x0000ffff)
249 #define PUFFS_FLAG_LIB(a) ((a) & 0xffff0000)
250
251 #define PUFFS_FLAG_BUILDPATH 0x80000000 /* node paths in pnode */
252 #define PUFFS_FLAG_OPDUMP 0x40000000 /* dump all operations */
253
254 /* blocking mode argument */
255 #define PUFFSDEV_BLOCK 0
256 #define PUFFSDEV_NONBLOCK 1
257
258 /* mainloop flags */
259 #define PUFFSLOOP_NODAEMON 0x01
260
261 #define DENT_DOT 0
262 #define DENT_DOTDOT 1
263 #define DENT_ADJ(a) ((a)-2) /* nth request means dir's n-2th */
264
265
266 /*
267 * Operation credentials
268 */
269
270 /* Credential fetch */
271 int puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *);
272 int puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *);
273 int puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *, short *);
274
275 /* Credential check */
276 int puffs_cred_isuid(const struct puffs_cred *pcr, uid_t);
277 int puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t);
278 /* kernel internal NOCRED */
279 int puffs_cred_iskernel(const struct puffs_cred *pcr);
280 /* kernel internal FSCRED */
281 int puffs_cred_isfs(const struct puffs_cred *pcr);
282 /* root || NOCRED || FSCRED */
283 int puffs_cred_isjuggernaut(const struct puffs_cred *pcr);
284
285
286 /*
287 * protos
288 */
289
290 #define PUFFSOP_PROTOS(fsname) \
291 int fsname##_fs_mount(struct puffs_usermount *, void **, \
292 struct statvfs *); \
293 int fsname##_fs_unmount(struct puffs_cc *, int, pid_t); \
294 int fsname##_fs_statvfs(struct puffs_cc *, \
295 struct statvfs *, pid_t); \
296 int fsname##_fs_sync(struct puffs_cc *, int, \
297 const struct puffs_cred *cred, pid_t); \
298 void fsname##_fs_suspend(struct puffs_cc *, int); \
299 \
300 int fsname##_node_lookup(struct puffs_cc *, \
301 void *, void **, enum vtype *, voff_t *, dev_t *, \
302 const struct puffs_cn *); \
303 int fsname##_node_create(struct puffs_cc *, \
304 void *, void **, const struct puffs_cn *, \
305 const struct vattr *); \
306 int fsname##_node_mknod(struct puffs_cc *, \
307 void *, void **, const struct puffs_cn *, \
308 const struct vattr *); \
309 int fsname##_node_open(struct puffs_cc *, \
310 void *, int, const struct puffs_cred *, pid_t); \
311 int fsname##_node_close(struct puffs_cc *, \
312 void *, int, const struct puffs_cred *, pid_t); \
313 int fsname##_node_access(struct puffs_cc *, \
314 void *, int, const struct puffs_cred *, pid_t); \
315 int fsname##_node_getattr(struct puffs_cc *, \
316 void *, struct vattr *, const struct puffs_cred *, pid_t); \
317 int fsname##_node_setattr(struct puffs_cc *, \
318 void *, const struct vattr *, const struct puffs_cred *, \
319 pid_t); \
320 int fsname##_node_poll(struct puffs_cc *, \
321 void *, struct puffs_vnreq_poll *); \
322 int fsname##_node_mmap(struct puffs_cc *, \
323 void *, int, const struct puffs_cred *, pid_t); \
324 int fsname##_node_fsync(struct puffs_cc *, \
325 void *, const struct puffs_cred *, int, off_t, off_t, \
326 pid_t); \
327 int fsname##_node_seek(struct puffs_cc *, \
328 void *, off_t, off_t, const struct puffs_cred *); \
329 int fsname##_node_remove(struct puffs_cc *, \
330 void *, void *, const struct puffs_cn *); \
331 int fsname##_node_link(struct puffs_cc *, \
332 void *, void *, const struct puffs_cn *); \
333 int fsname##_node_rename(struct puffs_cc *, \
334 void *, void *, const struct puffs_cn *, void *, void *, \
335 const struct puffs_cn *); \
336 int fsname##_node_mkdir(struct puffs_cc *, \
337 void *, void **, const struct puffs_cn *, \
338 const struct vattr *); \
339 int fsname##_node_rmdir(struct puffs_cc *, \
340 void *, void *, const struct puffs_cn *); \
341 int fsname##_node_symlink(struct puffs_cc *, \
342 void *, void **, const struct puffs_cn *, \
343 const struct vattr *, const char *); \
344 int fsname##_node_readdir(struct puffs_cc *, \
345 void *, struct dirent *, const struct puffs_cred *, \
346 off_t *, size_t *); \
347 int fsname##_node_readlink(struct puffs_cc *, \
348 void *, const struct puffs_cred *, char *, size_t *); \
349 int fsname##_node_reclaim(struct puffs_cc *, \
350 void *, pid_t); \
351 int fsname##_node_inactive(struct puffs_cc *, \
352 void *, pid_t, int *); \
353 int fsname##_node_print(struct puffs_cc *, \
354 void *); \
355 int fsname##_node_pathconf(struct puffs_cc *, \
356 void *, int, int *); \
357 int fsname##_node_advlock(struct puffs_cc *, \
358 void *, void *, int, struct flock *, int); \
359 int fsname##_node_getextattr(struct puffs_cc *, \
360 void *, struct puffs_vnreq_getextattr *); \
361 int fsname##_node_setextattr(struct puffs_cc *, \
362 void *, struct puffs_vnreq_setextattr *); \
363 int fsname##_node_listextattr(struct puffs_cc *, \
364 void *, struct puffs_vnreq_listextattr *); \
365 int fsname##_node_read(struct puffs_cc *, void *, \
366 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);\
367 int fsname##_node_write(struct puffs_cc *, void *, \
368 uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
369
370 #define PUFFSOP_INIT(ops) \
371 ops = malloc(sizeof(struct puffs_ops)); \
372 memset(ops, 0, sizeof(struct puffs_ops))
373 #define PUFFSOP_SET(ops, fsname, fsornode, opname) \
374 (ops)->puffs_##fsornode##_##opname = fsname##_##fsornode##_##opname
375 #define PUFFSOP_SETFSNOP(ops, opname) \
376 (ops)->puffs_fs_##opname = puffs_fsnop_##opname
377
378 #define PUFFS_DEVEL_LIBVERSION 8
379 #define puffs_mount(a,b,c,d,e,f,g) \
380 _puffs_mount(PUFFS_DEVEL_LIBVERSION,a,b,c,d,e,f,g)
381
382
383 #define PNPATH(pnode) ((pnode)->pn_po.po_path)
384 #define PNPLEN(pnode) ((pnode)->pn_po.po_len)
385 #define PCNPATH(pcnode) ((pcnode)->pcn_po_full.po_path)
386 #define PCNPLEN(pcnode) ((pcnode)->pcn_po_full.po_len)
387 #define PCNISDOTDOT(pcnode) \
388 ((pcnode)->pcn_namelen == 2 && strcmp((pcnode)->pcn_name, "..") == 0)
389
390 __BEGIN_DECLS
391
392 struct puffs_usermount *_puffs_mount(int, struct puffs_ops *, const char *, int,
393 const char *, void *, uint32_t, size_t);
394 int puffs_start(struct puffs_usermount *, void *, struct statvfs *);
395 int puffs_exit(struct puffs_usermount *, int);
396 int puffs_mainloop(struct puffs_usermount *, int);
397
398
399 int puffs_getselectable(struct puffs_usermount *);
400 int puffs_setblockingmode(struct puffs_usermount *, int);
401 int puffs_getstate(struct puffs_usermount *);
402 void puffs_setstacksize(struct puffs_usermount *, size_t);
403
404 struct puffs_pathobj *puffs_getrootpathobj(struct puffs_usermount *);
405
406 struct puffs_node *puffs_pn_new(struct puffs_usermount *, void *);
407 void puffs_pn_put(struct puffs_node *);
408
409 typedef void * (*puffs_nodewalk_fn)(struct puffs_usermount *,
410 struct puffs_node *, void *);
411 void *puffs_pn_nodewalk(struct puffs_usermount *,
412 puffs_nodewalk_fn, void *);
413
414 void puffs_setvattr(struct vattr *, const struct vattr *);
415 void puffs_vattr_null(struct vattr *);
416
417 /*
418 * generic/dummy routines applicable for some file systems
419 */
420 int puffs_fsnop_unmount(struct puffs_cc *, int, pid_t);
421 int puffs_fsnop_statvfs(struct puffs_cc *, struct statvfs *, pid_t);
422 void puffs_zerostatvfs(struct statvfs *);
423 int puffs_fsnop_sync(struct puffs_cc *, int waitfor,
424 const struct puffs_cred *, pid_t);
425 int puffs_genfs_node_reclaim(struct puffs_cc *, void *, pid_t);
426
427 /*
428 * Subroutine stuff
429 */
430
431 int puffs_gendotdent(struct dirent **, ino_t, int, size_t *);
432 int puffs_nextdent(struct dirent **, const char *, ino_t,
433 uint8_t, size_t *);
434 int puffs_vtype2dt(enum vtype);
435 enum vtype puffs_mode2vt(mode_t);
436 void puffs_stat2vattr(struct vattr *va, const struct stat *);
437
438 /*
439 * Requests
440 */
441
442 struct puffs_getreq *puffs_req_makeget(struct puffs_usermount *,
443 size_t, int);
444 int puffs_req_loadget(struct puffs_getreq *);
445 struct puffs_req *puffs_req_get(struct puffs_getreq *);
446 int puffs_req_remainingget(struct puffs_getreq *);
447 void puffs_req_setmaxget(struct puffs_getreq *, int);
448 void puffs_req_destroyget(struct puffs_getreq *);
449
450 struct puffs_putreq *puffs_req_makeput(struct puffs_usermount *);
451 void puffs_req_put(struct puffs_putreq *,struct puffs_req *);
452 void puffs_req_putcc(struct puffs_putreq *,struct puffs_cc*);
453 int puffs_req_putput(struct puffs_putreq *);
454 void puffs_req_resetput(struct puffs_putreq *);
455 void puffs_req_destroyput(struct puffs_putreq *);
456
457 int puffs_req_handle(struct puffs_usermount *,
458 struct puffs_getreq *,
459 struct puffs_putreq *, int);
460
461 /*
462 * Call Context interfaces relevant for user.
463 */
464
465 void puffs_cc_yield(struct puffs_cc *);
466 void puffs_cc_continue(struct puffs_cc *);
467 struct puffs_usermount *puffs_cc_getusermount(struct puffs_cc *);
468 struct puffs_cc *puffs_cc_create(struct puffs_usermount *);
469 void puffs_cc_destroy(struct puffs_cc *);
470
471 /*
472 * Execute or continue a request
473 */
474
475 int puffs_dopreq(struct puffs_usermount *, struct puffs_req *,
476 struct puffs_putreq *);
477 int puffs_docc(struct puffs_cc *, struct puffs_putreq *);
478
479 /*
480 * Flushing / invalidation routines
481 */
482
483 int puffs_inval_namecache_dir(struct puffs_usermount *, void *);
484 int puffs_inval_namecache_all(struct puffs_usermount *);
485
486 /*
487 * Path constructicons
488 */
489
490 int puffs_stdpath_buildpath(struct puffs_usermount *,
491 const struct puffs_pathobj *,
492 const struct puffs_pathobj *, size_t,
493 struct puffs_pathobj *);
494 int puffs_stdpath_cmppath(struct puffs_usermount *, struct puffs_pathobj *,
495 struct puffs_pathobj *, size_t, int);
496 void puffs_stdpath_freepath(struct puffs_usermount *,struct puffs_pathobj *);
497
498 void *puffs_path_walkcmp(struct puffs_usermount *,
499 struct puffs_node *, void *);
500 void *puffs_path_prefixadj(struct puffs_usermount *,
501 struct puffs_node *, void *);
502 int puffs_path_pcnbuild(struct puffs_usermount *,
503 struct puffs_cn *, void *);
504
505 void puffs_set_pathbuild(struct puffs_usermount *, pu_pathbuild_fn);
506 void puffs_set_pathtransform(struct puffs_usermount *, pu_pathtransform_fn);
507 void puffs_set_pathcmp(struct puffs_usermount *, pu_pathcmp_fn);
508 void puffs_set_pathfree(struct puffs_usermount *, pu_pathfree_fn);
509 void puffs_set_namemod(struct puffs_usermount *, pu_namemod_fn);
510
511 /*
512 * Suspension
513 */
514
515 int puffs_fs_suspend(struct puffs_usermount *);
516
517 __END_DECLS
518
519 #endif /* _PUFFS_H_ */
520