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