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