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