refuse.c revision 1.65 1 1.64 agc /* $NetBSD: refuse.c,v 1.65 2007/06/12 18:54:36 agc Exp $ */
2 1.7 pooka
3 1.1 agc /*
4 1.1 agc * Copyright 2007 Alistair Crooks. All rights reserved.
5 1.1 agc *
6 1.1 agc * Redistribution and use in source and binary forms, with or without
7 1.1 agc * modification, are permitted provided that the following conditions
8 1.1 agc * are met:
9 1.1 agc * 1. Redistributions of source code must retain the above copyright
10 1.1 agc * notice, this list of conditions and the following disclaimer.
11 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 agc * notice, this list of conditions and the following disclaimer in the
13 1.1 agc * documentation and/or other materials provided with the distribution.
14 1.1 agc * 3. The name of the author may not be used to endorse or promote
15 1.1 agc * products derived from this software without specific prior written
16 1.1 agc * permission.
17 1.1 agc *
18 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 agc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 agc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 1.1 agc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 1.1 agc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 1.1 agc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 1.1 agc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 1.1 agc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 agc */
30 1.7 pooka
31 1.7 pooka #include <sys/cdefs.h>
32 1.7 pooka #if !defined(lint)
33 1.64 agc __RCSID("$NetBSD: refuse.c,v 1.65 2007/06/12 18:54:36 agc Exp $");
34 1.7 pooka #endif /* !lint */
35 1.7 pooka
36 1.25 pooka #include <assert.h>
37 1.1 agc #include <err.h>
38 1.1 agc #include <errno.h>
39 1.1 agc #include <fuse.h>
40 1.1 agc #include <unistd.h>
41 1.1 agc
42 1.1 agc #include "defs.h"
43 1.1 agc
44 1.1 agc typedef uint64_t fuse_ino_t;
45 1.1 agc
46 1.1 agc struct fuse_config {
47 1.1 agc uid_t uid;
48 1.1 agc gid_t gid;
49 1.1 agc mode_t umask;
50 1.1 agc double entry_timeout;
51 1.1 agc double negative_timeout;
52 1.1 agc double attr_timeout;
53 1.1 agc double ac_attr_timeout;
54 1.1 agc int ac_attr_timeout_set;
55 1.1 agc int debug;
56 1.1 agc int hard_remove;
57 1.1 agc int use_ino;
58 1.1 agc int readdir_ino;
59 1.1 agc int set_mode;
60 1.1 agc int set_uid;
61 1.1 agc int set_gid;
62 1.1 agc int direct_io;
63 1.1 agc int kernel_cache;
64 1.1 agc int auto_cache;
65 1.1 agc int intr;
66 1.1 agc int intr_signal;
67 1.1 agc };
68 1.1 agc
69 1.38 pooka struct fuse_chan {
70 1.51 agc const char *dir;
71 1.51 agc struct fuse_args *args;
72 1.51 agc struct puffs_usermount *pu;
73 1.60 pooka int dead;
74 1.38 pooka };
75 1.38 pooka
76 1.1 agc /* this is the private fuse structure */
77 1.1 agc struct fuse {
78 1.38 pooka struct fuse_chan *fc; /* fuse channel pointer */
79 1.1 agc struct fuse_operations op; /* switch table of operations */
80 1.7 pooka int compat; /* compat level -
81 1.7 pooka * not used in puffs_fuse */
82 1.1 agc struct node **name_table;
83 1.1 agc size_t name_table_size;
84 1.1 agc struct node **id_table;
85 1.1 agc size_t id_table_size;
86 1.1 agc fuse_ino_t ctr;
87 1.1 agc unsigned int generation;
88 1.1 agc unsigned int hidectr;
89 1.1 agc pthread_mutex_t lock;
90 1.1 agc pthread_rwlock_t tree_lock;
91 1.1 agc void *user_data;
92 1.1 agc struct fuse_config conf;
93 1.1 agc int intr_installed;
94 1.1 agc };
95 1.1 agc
96 1.36 pooka struct puffs_fuse_dirh {
97 1.43 agc void *dbuf;
98 1.43 agc struct dirent *d;
99 1.43 agc
100 1.43 agc size_t reslen;
101 1.43 agc size_t bufsize;
102 1.36 pooka };
103 1.36 pooka
104 1.43 agc struct refusenode {
105 1.43 agc struct fuse_file_info file_info;
106 1.43 agc struct puffs_fuse_dirh dirh;
107 1.43 agc int opencount;
108 1.43 agc int flags;
109 1.43 agc };
110 1.30 pooka #define RN_ROOT 0x01
111 1.31 pooka #define RN_OPEN 0x02 /* XXX: could just use opencount */
112 1.5 pooka
113 1.43 agc static int fuse_setattr(struct fuse *, struct puffs_node *,
114 1.43 agc const char *, const struct vattr *);
115 1.26 pooka
116 1.5 pooka static struct puffs_node *
117 1.5 pooka newrn(struct puffs_usermount *pu)
118 1.5 pooka {
119 1.5 pooka struct puffs_node *pn;
120 1.5 pooka struct refusenode *rn;
121 1.5 pooka
122 1.62 agc if ((rn = calloc(1, sizeof(*rn))) == NULL) {
123 1.62 agc err(EXIT_FAILURE, "newrn");
124 1.62 agc }
125 1.5 pooka pn = puffs_pn_new(pu, rn);
126 1.5 pooka
127 1.5 pooka return pn;
128 1.5 pooka }
129 1.5 pooka
130 1.5 pooka static void
131 1.5 pooka nukern(struct puffs_node *pn)
132 1.5 pooka {
133 1.36 pooka struct refusenode *rn = pn->pn_data;
134 1.5 pooka
135 1.36 pooka free(rn->dirh.dbuf);
136 1.36 pooka free(rn);
137 1.5 pooka puffs_pn_put(pn);
138 1.5 pooka }
139 1.5 pooka
140 1.1 agc static ino_t fakeino = 3;
141 1.1 agc
142 1.21 pooka /*
143 1.43 agc * XXX: do this otherwise if/when we grow thread support
144 1.21 pooka */
145 1.21 pooka static struct fuse_context fcon;
146 1.21 pooka
147 1.64 agc #define SET_FUSE_CONTEXT_UID_GID(fusectx, cred) do { \
148 1.63 agc uid_t _uid; \
149 1.63 agc gid_t _gid; \
150 1.64 agc if (puffs_cred_getuid(cred, &_uid) == 0) { \
151 1.63 agc (fusectx)->uid = _uid; \
152 1.63 agc } \
153 1.64 agc if (puffs_cred_getgid(cred, &_gid) == 0) { \
154 1.63 agc (fusectx)->gid = _gid; \
155 1.63 agc } \
156 1.62 agc } while (/* CONSTCOND */ 0)
157 1.62 agc
158 1.62 agc #define SET_FUSE_CONTEXT_PID(fusectx, p) do { \
159 1.62 agc (fusectx)->pid = p; \
160 1.62 agc } while (/* CONSTCOND */ 0)
161 1.62 agc
162 1.36 pooka #define DIR_CHUNKSIZE 4096
163 1.36 pooka static int
164 1.36 pooka fill_dirbuf(struct puffs_fuse_dirh *dh, const char *name, ino_t dino,
165 1.36 pooka uint8_t dtype)
166 1.36 pooka {
167 1.36 pooka
168 1.36 pooka /* initial? */
169 1.36 pooka if (dh->bufsize == 0) {
170 1.65 agc if ((dh->dbuf = calloc(1, DIR_CHUNKSIZE)) == NULL) {
171 1.62 agc err(EXIT_FAILURE, "fill_dirbuf");
172 1.62 agc }
173 1.36 pooka dh->d = dh->dbuf;
174 1.36 pooka dh->reslen = dh->bufsize = DIR_CHUNKSIZE;
175 1.36 pooka }
176 1.21 pooka
177 1.62 agc if (puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen)) {
178 1.36 pooka return 0;
179 1.62 agc }
180 1.36 pooka
181 1.36 pooka /* try to increase buffer space */
182 1.36 pooka dh->dbuf = realloc(dh->dbuf, dh->bufsize + DIR_CHUNKSIZE);
183 1.62 agc if (dh->dbuf == NULL) {
184 1.62 agc err(EXIT_FAILURE, "fill_dirbuf realloc");
185 1.62 agc }
186 1.36 pooka dh->d = (void *)((uint8_t *)dh->dbuf + (dh->bufsize - dh->reslen));
187 1.36 pooka dh->reslen += DIR_CHUNKSIZE;
188 1.36 pooka dh->bufsize += DIR_CHUNKSIZE;
189 1.36 pooka
190 1.36 pooka return !puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen);
191 1.36 pooka }
192 1.1 agc
193 1.36 pooka /* ARGSUSED3 */
194 1.36 pooka /* XXX: I have no idea how "off" is supposed to be used */
195 1.1 agc static int
196 1.4 pooka puffs_fuse_fill_dir(void *buf, const char *name,
197 1.1 agc const struct stat *stbuf, off_t off)
198 1.1 agc {
199 1.36 pooka struct puffs_fuse_dirh *deh = buf;
200 1.13 pooka ino_t dino;
201 1.1 agc uint8_t dtype;
202 1.1 agc
203 1.13 pooka if (stbuf == NULL) {
204 1.13 pooka dtype = DT_UNKNOWN;
205 1.13 pooka dino = fakeino++;
206 1.13 pooka } else {
207 1.13 pooka dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
208 1.13 pooka dino = stbuf->st_ino;
209 1.50 pooka
210 1.50 pooka /*
211 1.50 pooka * Some FUSE file systems like to always use 0 as the
212 1.50 pooka * inode number. Our readdir() doesn't like to show
213 1.50 pooka * directory entries with inode number 0 ==> workaround.
214 1.50 pooka */
215 1.62 agc if (dino == 0) {
216 1.50 pooka dino = fakeino++;
217 1.62 agc }
218 1.13 pooka }
219 1.1 agc
220 1.36 pooka return fill_dirbuf(deh, name, dino, dtype);
221 1.4 pooka }
222 1.4 pooka
223 1.4 pooka static int
224 1.4 pooka puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
225 1.4 pooka {
226 1.43 agc ino_t dino;
227 1.43 agc int dtype;
228 1.4 pooka
229 1.62 agc if ((dtype = type) == 0) {
230 1.13 pooka dtype = DT_UNKNOWN;
231 1.62 agc }
232 1.43 agc
233 1.62 agc dino = (ino) ? ino : fakeino++;
234 1.4 pooka
235 1.36 pooka return fill_dirbuf(h, name, dino, dtype);
236 1.1 agc }
237 1.1 agc
238 1.62 agc /* place the refuse file system name into `name' */
239 1.53 agc static void
240 1.53 agc set_refuse_mount_name(char **argv, char *name, size_t size)
241 1.53 agc {
242 1.53 agc char *slash;
243 1.53 agc
244 1.53 agc if (argv == NULL || *argv == NULL) {
245 1.53 agc (void) strlcpy(name, "refuse", size);
246 1.53 agc } else {
247 1.53 agc if ((slash = strrchr(*argv, '/')) == NULL) {
248 1.53 agc slash = *argv;
249 1.53 agc } else {
250 1.53 agc slash += 1;
251 1.53 agc }
252 1.53 agc if (strncmp(*argv, "refuse:", 7) == 0) {
253 1.53 agc /* we've already done this */
254 1.53 agc (void) strlcpy(name, *argv, size);
255 1.53 agc } else {
256 1.53 agc (void) snprintf(name, size, "refuse:%s", slash);
257 1.53 agc }
258 1.53 agc }
259 1.53 agc }
260 1.53 agc
261 1.53 agc
262 1.51 agc /* this function exposes struct fuse to userland */
263 1.51 agc struct fuse *
264 1.51 agc fuse_setup(int argc, char **argv, const struct fuse_operations *ops,
265 1.51 agc size_t size, char **mountpoint, int *multithreaded, int *fd)
266 1.51 agc {
267 1.51 agc struct fuse_chan *fc;
268 1.51 agc struct fuse_args *args;
269 1.51 agc struct fuse *fuse;
270 1.51 agc char name[64];
271 1.53 agc int i;
272 1.51 agc
273 1.53 agc set_refuse_mount_name(argv, name, sizeof(name));
274 1.51 agc
275 1.51 agc /* stuff name into fuse_args */
276 1.56 christos args = fuse_opt_deep_copy_args(argc, argv);
277 1.51 agc if (args->argc > 0) {
278 1.55 christos free(args->argv[0]);
279 1.51 agc }
280 1.55 christos if ((args->argv[0] = strdup(name)) == NULL)
281 1.55 christos err(1, "fuse_setup");
282 1.51 agc
283 1.62 agc /* count back from the end over arguments starting with '-' */
284 1.53 agc for (i = argc - 1 ; i > 0 && *argv[i] == '-' ; --i) {
285 1.53 agc }
286 1.53 agc
287 1.53 agc fc = fuse_mount(*mountpoint = argv[i], args);
288 1.51 agc fuse = fuse_new(fc, args, ops, size, NULL);
289 1.51 agc
290 1.56 christos fuse_opt_free_args(args);
291 1.56 christos free(args);
292 1.51 agc
293 1.51 agc /* XXX - wait for puffs to become multi-threaded */
294 1.51 agc if (multithreaded) {
295 1.51 agc *multithreaded = 0;
296 1.51 agc }
297 1.51 agc
298 1.51 agc /* XXX - this is unused */
299 1.51 agc if (fd) {
300 1.51 agc *fd = 0;
301 1.51 agc }
302 1.51 agc
303 1.51 agc return fuse;
304 1.51 agc }
305 1.51 agc
306 1.18 pooka #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
307 1.18 pooka #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
308 1.18 pooka
309 1.26 pooka /* ARGSUSED1 */
310 1.26 pooka static int
311 1.26 pooka fuse_getattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
312 1.26 pooka struct vattr *va)
313 1.26 pooka {
314 1.26 pooka struct stat st;
315 1.26 pooka int ret;
316 1.26 pooka
317 1.26 pooka if (fuse->op.getattr == NULL) {
318 1.26 pooka return ENOSYS;
319 1.26 pooka }
320 1.26 pooka
321 1.26 pooka /* wrap up return code */
322 1.26 pooka ret = (*fuse->op.getattr)(path, &st);
323 1.26 pooka
324 1.26 pooka if (ret == 0) {
325 1.26 pooka puffs_stat2vattr(va, &st);
326 1.26 pooka }
327 1.26 pooka
328 1.26 pooka return -ret;
329 1.26 pooka }
330 1.26 pooka
331 1.26 pooka static int
332 1.26 pooka fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
333 1.26 pooka const struct vattr *va)
334 1.26 pooka {
335 1.26 pooka struct refusenode *rn = pn->pn_data;
336 1.26 pooka mode_t mode;
337 1.26 pooka uid_t uid;
338 1.26 pooka gid_t gid;
339 1.26 pooka int error, ret;
340 1.26 pooka
341 1.26 pooka error = 0;
342 1.26 pooka
343 1.26 pooka mode = va->va_mode;
344 1.26 pooka uid = va->va_uid;
345 1.26 pooka gid = va->va_gid;
346 1.26 pooka
347 1.26 pooka if (mode != (mode_t)PUFFS_VNOVAL) {
348 1.26 pooka ret = 0;
349 1.26 pooka
350 1.26 pooka if (fuse->op.chmod == NULL) {
351 1.26 pooka error = -ENOSYS;
352 1.26 pooka } else {
353 1.26 pooka ret = fuse->op.chmod(path, mode);
354 1.26 pooka if (ret)
355 1.26 pooka error = ret;
356 1.26 pooka }
357 1.26 pooka }
358 1.26 pooka if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
359 1.26 pooka ret = 0;
360 1.26 pooka
361 1.26 pooka if (fuse->op.chown == NULL) {
362 1.26 pooka error = -ENOSYS;
363 1.26 pooka } else {
364 1.26 pooka ret = fuse->op.chown(path, uid, gid);
365 1.26 pooka if (ret)
366 1.26 pooka error = ret;
367 1.26 pooka }
368 1.26 pooka }
369 1.26 pooka if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
370 1.26 pooka || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
371 1.26 pooka ret = 0;
372 1.26 pooka
373 1.26 pooka if (fuse->op.utimens) {
374 1.26 pooka struct timespec tv[2];
375 1.26 pooka
376 1.26 pooka tv[0].tv_sec = va->va_atime.tv_sec;
377 1.26 pooka tv[0].tv_nsec = va->va_atime.tv_nsec;
378 1.26 pooka tv[1].tv_sec = va->va_mtime.tv_sec;
379 1.26 pooka tv[1].tv_nsec = va->va_mtime.tv_nsec;
380 1.26 pooka
381 1.26 pooka ret = fuse->op.utimens(path, tv);
382 1.26 pooka } else if (fuse->op.utime) {
383 1.26 pooka struct utimbuf timbuf;
384 1.26 pooka
385 1.26 pooka timbuf.actime = va->va_atime.tv_sec;
386 1.26 pooka timbuf.modtime = va->va_mtime.tv_sec;
387 1.26 pooka
388 1.26 pooka ret = fuse->op.utime(path, &timbuf);
389 1.26 pooka } else {
390 1.26 pooka error = -ENOSYS;
391 1.26 pooka }
392 1.26 pooka
393 1.26 pooka if (ret)
394 1.26 pooka error = ret;
395 1.26 pooka }
396 1.26 pooka if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
397 1.26 pooka ret = 0;
398 1.26 pooka
399 1.26 pooka if (fuse->op.truncate) {
400 1.26 pooka ret = fuse->op.truncate(path, (off_t)va->va_size);
401 1.26 pooka } else if (fuse->op.ftruncate) {
402 1.26 pooka ret = fuse->op.ftruncate(path, (off_t)va->va_size,
403 1.26 pooka &rn->file_info);
404 1.26 pooka } else {
405 1.26 pooka error = -ENOSYS;
406 1.26 pooka }
407 1.26 pooka
408 1.26 pooka if (ret)
409 1.26 pooka error = ret;
410 1.26 pooka }
411 1.26 pooka /* XXX: no reflection with reality */
412 1.26 pooka puffs_setvattr(&pn->pn_va, va);
413 1.26 pooka
414 1.26 pooka return -error;
415 1.26 pooka
416 1.26 pooka }
417 1.26 pooka
418 1.26 pooka static int
419 1.26 pooka fuse_newnode(struct puffs_usermount *pu, const char *path,
420 1.26 pooka const struct vattr *va, struct fuse_file_info *fi, void **newnode)
421 1.26 pooka {
422 1.26 pooka struct vattr newva;
423 1.26 pooka struct fuse *fuse;
424 1.26 pooka struct puffs_node *pn;
425 1.26 pooka struct refusenode *rn;
426 1.26 pooka
427 1.46 pooka fuse = puffs_getspecific(pu);
428 1.26 pooka
429 1.26 pooka /* fix up nodes */
430 1.26 pooka pn = newrn(pu);
431 1.26 pooka if (pn == NULL) {
432 1.26 pooka if (va->va_type == VDIR) {
433 1.26 pooka FUSE_ERR_RMDIR(fuse, path);
434 1.26 pooka } else {
435 1.26 pooka FUSE_ERR_UNLINK(fuse, path);
436 1.26 pooka }
437 1.26 pooka return ENOMEM;
438 1.26 pooka }
439 1.26 pooka fuse_setattr(fuse, pn, path, va);
440 1.26 pooka if (fuse_getattr(fuse, pn, path, &newva) == 0)
441 1.26 pooka puffs_setvattr(&pn->pn_va, &newva);
442 1.26 pooka
443 1.26 pooka rn = pn->pn_data;
444 1.26 pooka if (fi)
445 1.26 pooka memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
446 1.26 pooka
447 1.26 pooka *newnode = pn;
448 1.26 pooka
449 1.26 pooka return 0;
450 1.26 pooka }
451 1.26 pooka
452 1.26 pooka
453 1.1 agc /* operation wrappers start here */
454 1.1 agc
455 1.1 agc /* lookup the path */
456 1.1 agc /* ARGSUSED1 */
457 1.1 agc static int
458 1.1 agc puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
459 1.1 agc enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
460 1.1 agc const struct puffs_cn *pcn)
461 1.1 agc {
462 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
463 1.12 pooka struct puffs_node *pn_res;
464 1.1 agc struct stat st;
465 1.1 agc struct fuse *fuse;
466 1.1 agc const char *path = PCNPATH(pcn);
467 1.1 agc int ret;
468 1.1 agc
469 1.46 pooka fuse = puffs_getspecific(pu);
470 1.62 agc
471 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn->pcn_cred);
472 1.62 agc
473 1.1 agc ret = fuse->op.getattr(path, &st);
474 1.12 pooka
475 1.1 agc if (ret != 0) {
476 1.18 pooka return -ret;
477 1.12 pooka }
478 1.12 pooka
479 1.12 pooka /* XXX: fiXXXme unconst */
480 1.12 pooka pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
481 1.12 pooka __UNCONST(&pcn->pcn_po_full));
482 1.12 pooka if (pn_res == NULL) {
483 1.12 pooka pn_res = newrn(pu);
484 1.12 pooka if (pn_res == NULL)
485 1.12 pooka return errno;
486 1.26 pooka puffs_stat2vattr(&pn_res->pn_va, &st);
487 1.1 agc }
488 1.12 pooka
489 1.12 pooka *newnode = pn_res;
490 1.12 pooka *newtype = pn_res->pn_va.va_type;
491 1.12 pooka *newsize = pn_res->pn_va.va_size;
492 1.12 pooka *newrdev = pn_res->pn_va.va_rdev;
493 1.12 pooka
494 1.12 pooka return 0;
495 1.1 agc }
496 1.1 agc
497 1.1 agc /* get attributes for the path name */
498 1.1 agc /* ARGSUSED3 */
499 1.1 agc static int
500 1.1 agc puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
501 1.1 agc const struct puffs_cred *pcr, pid_t pid)
502 1.1 agc {
503 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
504 1.1 agc struct puffs_node *pn = opc;
505 1.1 agc struct fuse *fuse;
506 1.1 agc const char *path = PNPATH(pn);
507 1.1 agc
508 1.46 pooka fuse = puffs_getspecific(pu);
509 1.62 agc
510 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, pcr);
511 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
512 1.62 agc
513 1.26 pooka return fuse_getattr(fuse, pn, path, va);
514 1.1 agc }
515 1.1 agc
516 1.1 agc /* read the contents of the symbolic link */
517 1.1 agc /* ARGSUSED2 */
518 1.1 agc static int
519 1.1 agc puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
520 1.1 agc const struct puffs_cred *cred, char *linkname, size_t *linklen)
521 1.1 agc {
522 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
523 1.1 agc struct puffs_node *pn = opc;
524 1.1 agc struct fuse *fuse;
525 1.13 pooka const char *path = PNPATH(pn), *p;
526 1.1 agc int ret;
527 1.1 agc
528 1.46 pooka fuse = puffs_getspecific(pu);
529 1.1 agc if (fuse->op.readlink == NULL) {
530 1.1 agc return ENOSYS;
531 1.1 agc }
532 1.1 agc
533 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, cred);
534 1.62 agc
535 1.1 agc /* wrap up return code */
536 1.1 agc ret = (*fuse->op.readlink)(path, linkname, *linklen);
537 1.1 agc
538 1.1 agc if (ret == 0) {
539 1.43 agc p = memchr(linkname, '\0', *linklen);
540 1.13 pooka if (!p)
541 1.13 pooka return EINVAL;
542 1.13 pooka
543 1.14 pooka *linklen = p - linkname;
544 1.1 agc }
545 1.1 agc
546 1.13 pooka return -ret;
547 1.1 agc }
548 1.1 agc
549 1.1 agc /* make the special node */
550 1.1 agc /* ARGSUSED1 */
551 1.1 agc static int
552 1.1 agc puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
553 1.1 agc const struct puffs_cn *pcn, const struct vattr *va)
554 1.1 agc {
555 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
556 1.1 agc struct fuse *fuse;
557 1.44 pooka mode_t mode;
558 1.1 agc const char *path = PCNPATH(pcn);
559 1.1 agc int ret;
560 1.1 agc
561 1.46 pooka fuse = puffs_getspecific(pu);
562 1.1 agc if (fuse->op.mknod == NULL) {
563 1.1 agc return ENOSYS;
564 1.1 agc }
565 1.1 agc
566 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn->pcn_cred);
567 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn->pcn_pid);
568 1.62 agc
569 1.1 agc /* wrap up return code */
570 1.44 pooka mode = puffs_addvtype2mode(va->va_mode, va->va_type);
571 1.1 agc ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
572 1.1 agc
573 1.1 agc if (ret == 0) {
574 1.26 pooka ret = fuse_newnode(pu, path, va, NULL, newnode);
575 1.1 agc }
576 1.1 agc
577 1.18 pooka return -ret;
578 1.1 agc }
579 1.1 agc
580 1.1 agc /* make a directory */
581 1.1 agc /* ARGSUSED1 */
582 1.1 agc static int
583 1.1 agc puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
584 1.1 agc const struct puffs_cn *pcn, const struct vattr *va)
585 1.1 agc {
586 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
587 1.1 agc struct fuse *fuse;
588 1.1 agc mode_t mode = va->va_mode;
589 1.1 agc const char *path = PCNPATH(pcn);
590 1.1 agc int ret;
591 1.1 agc
592 1.46 pooka fuse = puffs_getspecific(pu);
593 1.62 agc
594 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn->pcn_cred);
595 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn->pcn_pid);
596 1.62 agc
597 1.1 agc if (fuse->op.mkdir == NULL) {
598 1.1 agc return ENOSYS;
599 1.1 agc }
600 1.1 agc
601 1.1 agc /* wrap up return code */
602 1.1 agc ret = (*fuse->op.mkdir)(path, mode);
603 1.1 agc
604 1.1 agc if (ret == 0) {
605 1.26 pooka ret = fuse_newnode(pu, path, va, NULL, newnode);
606 1.1 agc }
607 1.1 agc
608 1.18 pooka return -ret;
609 1.1 agc }
610 1.1 agc
611 1.21 pooka /*
612 1.21 pooka * create a regular file
613 1.21 pooka *
614 1.21 pooka * since linux/fuse sports using mknod for creating regular files
615 1.21 pooka * instead of having a separate call for it in some versions, if
616 1.21 pooka * we don't have create, just jump to op->mknod.
617 1.21 pooka */
618 1.16 pooka /*ARGSUSED1*/
619 1.16 pooka static int
620 1.16 pooka puffs_fuse_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
621 1.16 pooka const struct puffs_cn *pcn, const struct vattr *va)
622 1.16 pooka {
623 1.16 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
624 1.16 pooka struct fuse *fuse;
625 1.16 pooka struct fuse_file_info fi;
626 1.16 pooka mode_t mode = va->va_mode;
627 1.16 pooka const char *path = PCNPATH(pcn);
628 1.31 pooka int ret, created;
629 1.16 pooka
630 1.46 pooka fuse = puffs_getspecific(pu);
631 1.21 pooka
632 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn->pcn_cred);
633 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn->pcn_pid);
634 1.62 agc
635 1.31 pooka created = 0;
636 1.21 pooka if (fuse->op.create) {
637 1.21 pooka ret = fuse->op.create(path, mode, &fi);
638 1.31 pooka if (ret == 0)
639 1.31 pooka created = 1;
640 1.21 pooka
641 1.21 pooka } else if (fuse->op.mknod) {
642 1.62 agc /* XXX - no-one can remember why the context uid/gid are taken from the vattr uid/gid */
643 1.21 pooka fcon.uid = va->va_uid; /*XXX*/
644 1.21 pooka fcon.gid = va->va_gid; /*XXX*/
645 1.21 pooka
646 1.21 pooka ret = fuse->op.mknod(path, mode | S_IFREG, 0);
647 1.21 pooka
648 1.21 pooka } else {
649 1.21 pooka ret = -ENOSYS;
650 1.16 pooka }
651 1.16 pooka
652 1.16 pooka if (ret == 0) {
653 1.26 pooka ret = fuse_newnode(pu, path, va, &fi, newnode);
654 1.31 pooka
655 1.31 pooka /* sweet.. create also open the file */
656 1.31 pooka if (created) {
657 1.31 pooka struct puffs_node *pn;
658 1.31 pooka struct refusenode *rn;
659 1.31 pooka
660 1.31 pooka pn = *newnode;
661 1.31 pooka rn = pn->pn_data;
662 1.31 pooka rn->flags |= RN_OPEN;
663 1.31 pooka rn->opencount++;
664 1.31 pooka }
665 1.16 pooka }
666 1.16 pooka
667 1.18 pooka return -ret;
668 1.16 pooka }
669 1.16 pooka
670 1.1 agc /* remove the directory entry */
671 1.23 pooka /* ARGSUSED1 */
672 1.1 agc static int
673 1.1 agc puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
674 1.1 agc const struct puffs_cn *pcn)
675 1.1 agc {
676 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
677 1.23 pooka struct puffs_node *pn_targ = targ;
678 1.1 agc struct fuse *fuse;
679 1.23 pooka const char *path = PNPATH(pn_targ);
680 1.1 agc int ret;
681 1.1 agc
682 1.46 pooka fuse = puffs_getspecific(pu);
683 1.62 agc
684 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn->pcn_cred);
685 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn->pcn_pid);
686 1.62 agc
687 1.1 agc if (fuse->op.unlink == NULL) {
688 1.1 agc return ENOSYS;
689 1.1 agc }
690 1.1 agc
691 1.1 agc /* wrap up return code */
692 1.1 agc ret = (*fuse->op.unlink)(path);
693 1.1 agc
694 1.18 pooka return -ret;
695 1.1 agc }
696 1.1 agc
697 1.1 agc /* remove the directory */
698 1.1 agc /* ARGSUSED1 */
699 1.1 agc static int
700 1.1 agc puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
701 1.1 agc const struct puffs_cn *pcn)
702 1.1 agc {
703 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
704 1.23 pooka struct puffs_node *pn_targ = targ;
705 1.1 agc struct fuse *fuse;
706 1.23 pooka const char *path = PNPATH(pn_targ);
707 1.1 agc int ret;
708 1.1 agc
709 1.46 pooka fuse = puffs_getspecific(pu);
710 1.62 agc
711 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn->pcn_cred);
712 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn->pcn_pid);
713 1.62 agc
714 1.1 agc if (fuse->op.rmdir == NULL) {
715 1.1 agc return ENOSYS;
716 1.1 agc }
717 1.1 agc
718 1.1 agc /* wrap up return code */
719 1.1 agc ret = (*fuse->op.rmdir)(path);
720 1.1 agc
721 1.18 pooka return -ret;
722 1.1 agc }
723 1.1 agc
724 1.1 agc /* create a symbolic link */
725 1.1 agc /* ARGSUSED1 */
726 1.1 agc static int
727 1.1 agc puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
728 1.1 agc const struct puffs_cn *pcn_src, const struct vattr *va,
729 1.1 agc const char *link_target)
730 1.1 agc {
731 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
732 1.1 agc struct fuse *fuse;
733 1.1 agc const char *path = PCNPATH(pcn_src);
734 1.1 agc int ret;
735 1.1 agc
736 1.46 pooka fuse = puffs_getspecific(pu);
737 1.62 agc
738 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn_src->pcn_cred);
739 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn_src->pcn_pid);
740 1.62 agc
741 1.1 agc if (fuse->op.symlink == NULL) {
742 1.1 agc return ENOSYS;
743 1.1 agc }
744 1.1 agc
745 1.1 agc /* wrap up return code */
746 1.32 pooka ret = fuse->op.symlink(link_target, path);
747 1.1 agc
748 1.1 agc if (ret == 0) {
749 1.26 pooka ret = fuse_newnode(pu, path, va, NULL, newnode);
750 1.1 agc }
751 1.1 agc
752 1.18 pooka return -ret;
753 1.1 agc }
754 1.1 agc
755 1.1 agc /* rename a directory entry */
756 1.1 agc /* ARGSUSED1 */
757 1.1 agc static int
758 1.1 agc puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
759 1.1 agc const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
760 1.1 agc const struct puffs_cn *pcn_targ)
761 1.1 agc {
762 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
763 1.1 agc struct fuse *fuse;
764 1.24 pooka const char *path_src = PCNPATH(pcn_src);
765 1.24 pooka const char *path_dest = PCNPATH(pcn_targ);
766 1.1 agc int ret;
767 1.1 agc
768 1.46 pooka fuse = puffs_getspecific(pu);
769 1.62 agc
770 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn_targ->pcn_cred);
771 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn_targ->pcn_pid);
772 1.62 agc
773 1.1 agc if (fuse->op.rename == NULL) {
774 1.1 agc return ENOSYS;
775 1.1 agc }
776 1.1 agc
777 1.24 pooka ret = fuse->op.rename(path_src, path_dest);
778 1.1 agc
779 1.1 agc if (ret == 0) {
780 1.1 agc }
781 1.1 agc
782 1.18 pooka return -ret;
783 1.1 agc }
784 1.1 agc
785 1.1 agc /* create a link in the file system */
786 1.1 agc /* ARGSUSED1 */
787 1.1 agc static int
788 1.1 agc puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
789 1.1 agc const struct puffs_cn *pcn)
790 1.1 agc {
791 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
792 1.1 agc struct puffs_node *pn = targ;
793 1.1 agc struct fuse *fuse;
794 1.1 agc int ret;
795 1.1 agc
796 1.46 pooka fuse = puffs_getspecific(pu);
797 1.62 agc
798 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, &pcn->pcn_cred);
799 1.64 agc SET_FUSE_CONTEXT_PID(&fcon, pcn->pcn_pid);
800 1.62 agc
801 1.1 agc if (fuse->op.link == NULL) {
802 1.1 agc return ENOSYS;
803 1.1 agc }
804 1.1 agc
805 1.1 agc /* wrap up return code */
806 1.1 agc ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
807 1.1 agc
808 1.18 pooka return -ret;
809 1.1 agc }
810 1.1 agc
811 1.1 agc /*
812 1.19 pooka * fuse's regular interface provides chmod(), chown(), utimes()
813 1.19 pooka * and truncate() + some variations, so try to fit the square block
814 1.19 pooka * in the circle hole and the circle block .... something like that
815 1.7 pooka */
816 1.1 agc /* ARGSUSED3 */
817 1.1 agc static int
818 1.1 agc puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
819 1.1 agc const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
820 1.1 agc {
821 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
822 1.1 agc struct puffs_node *pn = opc;
823 1.1 agc struct fuse *fuse;
824 1.1 agc const char *path = PNPATH(pn);
825 1.1 agc
826 1.46 pooka fuse = puffs_getspecific(pu);
827 1.1 agc
828 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, pcr);
829 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
830 1.62 agc
831 1.26 pooka return fuse_setattr(fuse, pn, path, va);
832 1.1 agc }
833 1.1 agc
834 1.1 agc /* ARGSUSED2 */
835 1.1 agc static int
836 1.31 pooka puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int mode,
837 1.1 agc const struct puffs_cred *cred, pid_t pid)
838 1.1 agc {
839 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
840 1.1 agc struct puffs_node *pn = opc;
841 1.5 pooka struct refusenode *rn = pn->pn_data;
842 1.31 pooka struct fuse_file_info *fi = &rn->file_info;
843 1.1 agc struct fuse *fuse;
844 1.1 agc const char *path = PNPATH(pn);
845 1.1 agc
846 1.46 pooka fuse = puffs_getspecific(pu);
847 1.1 agc
848 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, cred);
849 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
850 1.62 agc
851 1.30 pooka /* if open, don't open again, lest risk nuking file private info */
852 1.31 pooka if (rn->flags & RN_OPEN) {
853 1.31 pooka rn->opencount++;
854 1.1 agc return 0;
855 1.31 pooka }
856 1.1 agc
857 1.37 pooka /* OFLAGS(), need to convert FREAD/FWRITE to O_RD/WR */
858 1.37 pooka fi->flags = (mode & ~(O_CREAT | O_EXCL | O_TRUNC)) - 1;
859 1.37 pooka
860 1.30 pooka if (pn->pn_va.va_type == VDIR) {
861 1.30 pooka if (fuse->op.opendir)
862 1.33 pooka fuse->op.opendir(path, fi);
863 1.30 pooka } else {
864 1.30 pooka if (fuse->op.open)
865 1.33 pooka fuse->op.open(path, fi);
866 1.30 pooka }
867 1.1 agc
868 1.33 pooka rn->flags |= RN_OPEN;
869 1.33 pooka rn->opencount++;
870 1.1 agc
871 1.33 pooka return 0;
872 1.1 agc }
873 1.1 agc
874 1.31 pooka /* ARGSUSED2 */
875 1.31 pooka static int
876 1.31 pooka puffs_fuse_node_close(struct puffs_cc *pcc, void *opc, int fflag,
877 1.31 pooka const struct puffs_cred *pcr, pid_t pid)
878 1.31 pooka {
879 1.31 pooka struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
880 1.31 pooka struct puffs_node *pn = opc;
881 1.31 pooka struct refusenode *rn = pn->pn_data;
882 1.31 pooka struct fuse *fuse;
883 1.31 pooka struct fuse_file_info *fi;
884 1.31 pooka const char *path = PNPATH(pn);
885 1.31 pooka int ret;
886 1.31 pooka
887 1.46 pooka fuse = puffs_getspecific(pu);
888 1.31 pooka fi = &rn->file_info;
889 1.31 pooka ret = 0;
890 1.31 pooka
891 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, pcr);
892 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
893 1.62 agc
894 1.31 pooka if (rn->flags & RN_OPEN) {
895 1.31 pooka if (pn->pn_va.va_type == VDIR) {
896 1.31 pooka if (fuse->op.releasedir)
897 1.31 pooka ret = fuse->op.releasedir(path, fi);
898 1.31 pooka } else {
899 1.31 pooka if (fuse->op.release)
900 1.31 pooka ret = fuse->op.release(path, fi);
901 1.31 pooka }
902 1.31 pooka }
903 1.31 pooka rn->flags &= ~RN_OPEN;
904 1.31 pooka rn->opencount--;
905 1.31 pooka
906 1.31 pooka return ret;
907 1.31 pooka }
908 1.31 pooka
909 1.1 agc /* read some more from the file */
910 1.1 agc /* ARGSUSED5 */
911 1.1 agc static int
912 1.1 agc puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
913 1.1 agc off_t offset, size_t *resid, const struct puffs_cred *pcr,
914 1.1 agc int ioflag)
915 1.1 agc {
916 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
917 1.1 agc struct puffs_node *pn = opc;
918 1.5 pooka struct refusenode *rn = pn->pn_data;
919 1.1 agc struct fuse *fuse;
920 1.1 agc const char *path = PNPATH(pn);
921 1.25 pooka size_t maxread;
922 1.1 agc int ret;
923 1.1 agc
924 1.46 pooka fuse = puffs_getspecific(pu);
925 1.1 agc if (fuse->op.read == NULL) {
926 1.1 agc return ENOSYS;
927 1.1 agc }
928 1.1 agc
929 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, pcr);
930 1.62 agc
931 1.25 pooka maxread = *resid;
932 1.26 pooka if (maxread > pn->pn_va.va_size - offset) {
933 1.26 pooka /*LINTED*/
934 1.25 pooka maxread = pn->pn_va.va_size - offset;
935 1.26 pooka }
936 1.25 pooka if (maxread == 0)
937 1.25 pooka return 0;
938 1.25 pooka
939 1.25 pooka ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
940 1.7 pooka &rn->file_info);
941 1.1 agc
942 1.1 agc if (ret > 0) {
943 1.1 agc *resid -= ret;
944 1.16 pooka ret = 0;
945 1.1 agc }
946 1.1 agc
947 1.16 pooka return -ret;
948 1.1 agc }
949 1.1 agc
950 1.1 agc /* write to the file */
951 1.1 agc /* ARGSUSED0 */
952 1.1 agc static int
953 1.1 agc puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
954 1.1 agc off_t offset, size_t *resid, const struct puffs_cred *pcr,
955 1.1 agc int ioflag)
956 1.1 agc {
957 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
958 1.1 agc struct puffs_node *pn = opc;
959 1.8 pooka struct refusenode *rn = pn->pn_data;
960 1.1 agc struct fuse *fuse;
961 1.1 agc const char *path = PNPATH(pn);
962 1.1 agc int ret;
963 1.1 agc
964 1.46 pooka fuse = puffs_getspecific(pu);
965 1.1 agc if (fuse->op.write == NULL) {
966 1.1 agc return ENOSYS;
967 1.1 agc }
968 1.1 agc
969 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, pcr);
970 1.62 agc
971 1.17 pooka if (ioflag & PUFFS_IO_APPEND)
972 1.17 pooka offset = pn->pn_va.va_size;
973 1.17 pooka
974 1.8 pooka ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
975 1.8 pooka &rn->file_info);
976 1.1 agc
977 1.1 agc if (ret > 0) {
978 1.23 pooka if (offset + ret > pn->pn_va.va_size)
979 1.23 pooka pn->pn_va.va_size = offset + ret;
980 1.26 pooka *resid -= ret;
981 1.16 pooka ret = 0;
982 1.1 agc }
983 1.1 agc
984 1.16 pooka return -ret;
985 1.1 agc }
986 1.1 agc
987 1.1 agc
988 1.1 agc /* ARGSUSED3 */
989 1.1 agc static int
990 1.45 pooka puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
991 1.45 pooka off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
992 1.45 pooka int *eofflag, off_t *cookies, size_t *ncookies)
993 1.1 agc {
994 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
995 1.1 agc struct puffs_node *pn = opc;
996 1.8 pooka struct refusenode *rn = pn->pn_data;
997 1.43 agc struct puffs_fuse_dirh *dirh;
998 1.43 agc struct fuse *fuse;
999 1.41 agc struct dirent *fromdent;
1000 1.1 agc const char *path = PNPATH(pn);
1001 1.1 agc int ret;
1002 1.1 agc
1003 1.46 pooka fuse = puffs_getspecific(pu);
1004 1.4 pooka if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
1005 1.1 agc return ENOSYS;
1006 1.1 agc }
1007 1.1 agc
1008 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, pcr);
1009 1.62 agc
1010 1.36 pooka if (pn->pn_va.va_type != VDIR)
1011 1.36 pooka return ENOTDIR;
1012 1.36 pooka
1013 1.36 pooka dirh = &rn->dirh;
1014 1.36 pooka
1015 1.36 pooka /*
1016 1.36 pooka * if we are starting from the beginning, slurp entire directory
1017 1.36 pooka * into our buffers
1018 1.36 pooka */
1019 1.36 pooka if (*readoff == 0) {
1020 1.36 pooka /* free old buffers */
1021 1.36 pooka free(dirh->dbuf);
1022 1.36 pooka memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
1023 1.36 pooka
1024 1.36 pooka if (fuse->op.readdir)
1025 1.36 pooka ret = fuse->op.readdir(path, dirh, puffs_fuse_fill_dir,
1026 1.36 pooka 0, &rn->file_info);
1027 1.36 pooka else
1028 1.36 pooka ret = fuse->op.getdir(path, dirh, puffs_fuse_dirfil);
1029 1.36 pooka if (ret)
1030 1.36 pooka return -ret;
1031 1.35 pooka }
1032 1.35 pooka
1033 1.36 pooka /* now, stuff results into the kernel buffers */
1034 1.36 pooka while (*readoff < dirh->bufsize - dirh->reslen) {
1035 1.36 pooka /*LINTED*/
1036 1.36 pooka fromdent = (struct dirent *)((uint8_t *)dirh->dbuf + *readoff);
1037 1.36 pooka
1038 1.36 pooka if (*reslen < _DIRENT_SIZE(fromdent))
1039 1.36 pooka break;
1040 1.36 pooka
1041 1.36 pooka memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
1042 1.36 pooka *readoff += _DIRENT_SIZE(fromdent);
1043 1.36 pooka *reslen -= _DIRENT_SIZE(fromdent);
1044 1.1 agc
1045 1.36 pooka dent = _DIRENT_NEXT(dent);
1046 1.1 agc }
1047 1.1 agc
1048 1.36 pooka return 0;
1049 1.1 agc }
1050 1.1 agc
1051 1.26 pooka /* ARGSUSED */
1052 1.26 pooka static int
1053 1.26 pooka puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
1054 1.26 pooka {
1055 1.26 pooka struct puffs_node *pn = opc;
1056 1.3 pooka
1057 1.5 pooka nukern(pn);
1058 1.3 pooka
1059 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
1060 1.62 agc
1061 1.26 pooka return 0;
1062 1.3 pooka }
1063 1.3 pooka
1064 1.1 agc /* ARGSUSED1 */
1065 1.1 agc static int
1066 1.1 agc puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
1067 1.1 agc {
1068 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1069 1.1 agc struct fuse *fuse;
1070 1.1 agc
1071 1.46 pooka fuse = puffs_getspecific(pu);
1072 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
1073 1.1 agc if (fuse->op.destroy == NULL) {
1074 1.2 pooka return 0;
1075 1.1 agc }
1076 1.1 agc (*fuse->op.destroy)(fuse);
1077 1.1 agc return 0;
1078 1.1 agc }
1079 1.1 agc
1080 1.1 agc /* ARGSUSED0 */
1081 1.1 agc static int
1082 1.1 agc puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
1083 1.1 agc const struct puffs_cred *cr, pid_t pid)
1084 1.1 agc {
1085 1.64 agc SET_FUSE_CONTEXT_UID_GID(&fcon, cr);
1086 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
1087 1.1 agc return 0;
1088 1.1 agc }
1089 1.1 agc
1090 1.1 agc /* ARGSUSED2 */
1091 1.1 agc static int
1092 1.1 agc puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
1093 1.1 agc {
1094 1.1 agc struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1095 1.1 agc struct fuse *fuse;
1096 1.1 agc int ret;
1097 1.1 agc
1098 1.46 pooka fuse = puffs_getspecific(pu);
1099 1.62 agc SET_FUSE_CONTEXT_PID(&fcon, pid);
1100 1.1 agc if (fuse->op.statfs == NULL) {
1101 1.46 pooka if ((ret = statvfs(PNPATH(puffs_getroot(pu)), svfsb)) == -1) {
1102 1.1 agc return errno;
1103 1.1 agc }
1104 1.1 agc } else {
1105 1.46 pooka ret = fuse->op.statfs(PNPATH(puffs_getroot(pu)), svfsb);
1106 1.1 agc }
1107 1.1 agc
1108 1.1 agc return ret;
1109 1.1 agc }
1110 1.1 agc
1111 1.1 agc
1112 1.1 agc /* End of puffs_fuse operations */
1113 1.1 agc /* ARGSUSED3 */
1114 1.1 agc int
1115 1.1 agc fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
1116 1.1 agc size_t size, void *userdata)
1117 1.1 agc {
1118 1.51 agc struct fuse *fuse;
1119 1.51 agc char *mountpoint;
1120 1.51 agc int multithreaded;
1121 1.51 agc int fd;
1122 1.38 pooka
1123 1.51 agc fuse = fuse_setup(argc, argv, ops, size, &mountpoint, &multithreaded,
1124 1.51 agc &fd);
1125 1.38 pooka
1126 1.51 agc return fuse_loop(fuse);
1127 1.38 pooka }
1128 1.38 pooka
1129 1.38 pooka /*
1130 1.38 pooka * XXX: just defer the operation until fuse_new() when we have more
1131 1.38 pooka * info on our hands. The real beef is why's this separate in fuse in
1132 1.38 pooka * the first place?
1133 1.38 pooka */
1134 1.38 pooka /* ARGSUSED1 */
1135 1.38 pooka struct fuse_chan *
1136 1.38 pooka fuse_mount(const char *dir, struct fuse_args *args)
1137 1.38 pooka {
1138 1.48 agc struct fuse_chan *fc;
1139 1.53 agc char name[64];
1140 1.38 pooka
1141 1.65 agc if ((fc = calloc(1, sizeof(*fc))) == NULL) {
1142 1.65 agc err(EXIT_FAILURE, "fuse_mount");
1143 1.65 agc }
1144 1.60 pooka fc->dead = 0;
1145 1.38 pooka
1146 1.65 agc if ((fc->dir = strdup(dir)) == NULL) {
1147 1.65 agc err(EXIT_FAILURE, "fuse_mount");
1148 1.65 agc }
1149 1.48 agc
1150 1.48 agc /*
1151 1.48 agc * we need to deep copy the args struct - some fuse file
1152 1.48 agc * systems "clean up" the argument vector for "security
1153 1.48 agc * reasons"
1154 1.48 agc */
1155 1.56 christos fc->args = fuse_opt_deep_copy_args(args->argc, args->argv);
1156 1.53 agc
1157 1.53 agc if (args->argc > 0) {
1158 1.53 agc set_refuse_mount_name(args->argv, name, sizeof(name));
1159 1.55 christos if ((args->argv[0] = strdup(name)) == NULL)
1160 1.55 christos err(1, "fuse_mount");
1161 1.53 agc }
1162 1.53 agc
1163 1.38 pooka return fc;
1164 1.38 pooka }
1165 1.38 pooka
1166 1.38 pooka /* ARGSUSED1 */
1167 1.38 pooka struct fuse *
1168 1.38 pooka fuse_new(struct fuse_chan *fc, struct fuse_args *args,
1169 1.38 pooka const struct fuse_operations *ops, size_t size, void *userdata)
1170 1.38 pooka {
1171 1.1 agc struct puffs_usermount *pu;
1172 1.1 agc struct puffs_pathobj *po_root;
1173 1.46 pooka struct puffs_node *pn_root;
1174 1.43 agc struct puffs_ops *pops;
1175 1.41 agc struct refusenode *rn_root;
1176 1.62 agc struct statvfs svfsb;
1177 1.62 agc struct stat st;
1178 1.1 agc struct fuse *fuse;
1179 1.62 agc extern int puffs_fakecc;
1180 1.53 agc char name[64];
1181 1.53 agc char *argv0;
1182 1.38 pooka
1183 1.65 agc if ((fuse = calloc(1, sizeof(*fuse))) == NULL) {
1184 1.65 agc err(EXIT_FAILURE, "fuse_new");
1185 1.65 agc }
1186 1.38 pooka
1187 1.38 pooka /* copy fuse ops to their own stucture */
1188 1.38 pooka (void) memcpy(&fuse->op, ops, sizeof(fuse->op));
1189 1.38 pooka
1190 1.38 pooka fcon.fuse = fuse;
1191 1.62 agc fcon.uid = 0;
1192 1.62 agc fcon.gid = 0;
1193 1.62 agc fcon.pid = 0;
1194 1.38 pooka fcon.private_data = userdata;
1195 1.38 pooka
1196 1.38 pooka fuse->fc = fc;
1197 1.1 agc
1198 1.1 agc /* initialise the puffs operations structure */
1199 1.1 agc PUFFSOP_INIT(pops);
1200 1.1 agc
1201 1.1 agc PUFFSOP_SET(pops, puffs_fuse, fs, sync);
1202 1.1 agc PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
1203 1.1 agc PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
1204 1.1 agc
1205 1.2 pooka /*
1206 1.2 pooka * XXX: all of these don't possibly need to be
1207 1.2 pooka * unconditionally set
1208 1.2 pooka */
1209 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, lookup);
1210 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, getattr);
1211 1.15 pooka PUFFSOP_SET(pops, puffs_fuse, node, setattr);
1212 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, readdir);
1213 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, readlink);
1214 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, mknod);
1215 1.16 pooka PUFFSOP_SET(pops, puffs_fuse, node, create);
1216 1.15 pooka PUFFSOP_SET(pops, puffs_fuse, node, remove);
1217 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
1218 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
1219 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, symlink);
1220 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, rename);
1221 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, link);
1222 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, open);
1223 1.31 pooka PUFFSOP_SET(pops, puffs_fuse, node, close);
1224 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, read);
1225 1.1 agc PUFFSOP_SET(pops, puffs_fuse, node, write);
1226 1.3 pooka PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
1227 1.1 agc
1228 1.53 agc argv0 = (*args->argv[0] == 0x0) ? fc->args->argv[0] : args->argv[0];
1229 1.53 agc set_refuse_mount_name(&argv0, name, sizeof(name));
1230 1.53 agc
1231 1.54 pooka puffs_fakecc = 1; /* XXX */
1232 1.57 pooka pu = puffs_init(pops, name, fuse,
1233 1.38 pooka PUFFS_FLAG_BUILDPATH
1234 1.49 pooka | PUFFS_FLAG_HASHPATH
1235 1.38 pooka | PUFFS_FLAG_OPDUMP
1236 1.47 pooka | PUFFS_KFLAG_NOCACHE);
1237 1.1 agc if (pu == NULL) {
1238 1.57 pooka err(EXIT_FAILURE, "puffs_init");
1239 1.1 agc }
1240 1.38 pooka fc->pu = pu;
1241 1.1 agc
1242 1.46 pooka pn_root = newrn(pu);
1243 1.46 pooka puffs_setroot(pu, pn_root);
1244 1.46 pooka rn_root = pn_root->pn_data;
1245 1.30 pooka rn_root->flags |= RN_ROOT;
1246 1.30 pooka
1247 1.1 agc po_root = puffs_getrootpathobj(pu);
1248 1.55 christos if ((po_root->po_path = strdup("/")) == NULL)
1249 1.55 christos err(1, "fuse_new");
1250 1.1 agc po_root->po_len = 1;
1251 1.59 pooka puffs_path_buildhash(pu, po_root);
1252 1.1 agc
1253 1.25 pooka /* sane defaults */
1254 1.46 pooka puffs_vattr_null(&pn_root->pn_va);
1255 1.46 pooka pn_root->pn_va.va_type = VDIR;
1256 1.46 pooka pn_root->pn_va.va_mode = 0755;
1257 1.25 pooka if (fuse->op.getattr)
1258 1.25 pooka if (fuse->op.getattr(po_root->po_path, &st) == 0)
1259 1.46 pooka puffs_stat2vattr(&pn_root->pn_va, &st);
1260 1.46 pooka assert(pn_root->pn_va.va_type == VDIR);
1261 1.25 pooka
1262 1.11 pooka if (fuse->op.init)
1263 1.11 pooka fcon.private_data = fuse->op.init(NULL); /* XXX */
1264 1.11 pooka
1265 1.38 pooka puffs_zerostatvfs(&svfsb);
1266 1.57 pooka if (puffs_mount(pu, fc->dir, MNT_NODEV | MNT_NOSUID, pn_root) == -1) {
1267 1.57 pooka err(EXIT_FAILURE, "puffs_mount: directory \"%s\"", fc->dir);
1268 1.1 agc }
1269 1.1 agc
1270 1.38 pooka return fuse;
1271 1.38 pooka }
1272 1.38 pooka
1273 1.38 pooka int
1274 1.38 pooka fuse_loop(struct fuse *fuse)
1275 1.38 pooka {
1276 1.38 pooka
1277 1.38 pooka return puffs_mainloop(fuse->fc->pu, PUFFSLOOP_NODAEMON);
1278 1.38 pooka }
1279 1.38 pooka
1280 1.38 pooka void
1281 1.38 pooka fuse_destroy(struct fuse *fuse)
1282 1.38 pooka {
1283 1.38 pooka
1284 1.1 agc
1285 1.38 pooka /* XXXXXX: missing stuff */
1286 1.55 christos free(fuse);
1287 1.1 agc }
1288 1.1 agc
1289 1.11 pooka /* XXX: threads */
1290 1.11 pooka struct fuse_context *
1291 1.62 agc fuse_get_context(void)
1292 1.11 pooka {
1293 1.11 pooka
1294 1.11 pooka return &fcon;
1295 1.11 pooka }
1296 1.20 agc
1297 1.20 agc void
1298 1.38 pooka fuse_exit(struct fuse *fuse)
1299 1.20 agc {
1300 1.20 agc
1301 1.60 pooka /* XXX: puffs_exit() is WRONG */
1302 1.60 pooka if (fuse->fc->dead == 0)
1303 1.60 pooka puffs_exit(fuse->fc->pu, 1);
1304 1.60 pooka fuse->fc->dead = 1;
1305 1.20 agc }
1306 1.29 pooka
1307 1.29 pooka /*
1308 1.29 pooka * XXX: obviously not the most perfect of functions, but needs some
1309 1.29 pooka * puffs tweaking for a better tomorrow
1310 1.29 pooka */
1311 1.31 pooka /*ARGSUSED*/
1312 1.29 pooka void
1313 1.38 pooka fuse_unmount(const char *mp, struct fuse_chan *fc)
1314 1.38 pooka {
1315 1.38 pooka
1316 1.60 pooka /* XXX: puffs_exit() is WRONG */
1317 1.60 pooka if (fc->dead == 0)
1318 1.60 pooka puffs_exit(fc->pu, 1);
1319 1.60 pooka fc->dead = 1;
1320 1.38 pooka }
1321 1.38 pooka
1322 1.38 pooka /*ARGSUSED*/
1323 1.38 pooka void
1324 1.38 pooka fuse_unmount_compat22(const char *mp)
1325 1.29 pooka {
1326 1.29 pooka
1327 1.29 pooka return;
1328 1.29 pooka }
1329 1.51 agc
1330 1.51 agc /* The next function "exposes" struct fuse to userland. Not much
1331 1.51 agc * that we can do about this, as we're conforming to a defined
1332 1.51 agc * interface. */
1333 1.51 agc
1334 1.51 agc void
1335 1.51 agc fuse_teardown(struct fuse *fuse, char *mountpoint)
1336 1.51 agc {
1337 1.51 agc fuse_unmount(mountpoint, fuse->fc);
1338 1.51 agc fuse_destroy(fuse);
1339 1.51 agc }
1340