2 * Copyright (c) 2008-2010 Sine Nomine Associates
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Sine Nomine Associates nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
21 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * afsd_fuse.c - Driver for afsd.fuse, and glue between FUSE and libuafs
37 #include <afsconfig.h>
38 #include <afs/param.h>
40 #include <sysincludes.h>
41 #include <afs/afsutil.h>
42 #include <afs_usrops.h>
44 #include <afs/afs_args.h>
52 #define FUSE_USE_VERSION 27
55 /* command-line arguments to pass to afsd and the cmd_ library */
56 static struct fuse_args afsd_args = FUSE_ARGS_INIT(0, NULL);
58 /* command-line arguments to pass to FUSE */
59 static struct fuse_args fuse_args = FUSE_ARGS_INIT(0, NULL);
61 /* used for command-line parsing in fuafsd_fuse_opt below */
62 static int fuafsd_cmd_accept = 0;
64 static int stderr_save;
67 * Turn FUSE-y paths into libuafs-y paths.
69 * @param[in] apath a path in the form of /localcell/foo/bar
71 * @return a path (non-const) in the form /afs/localcell/foo/bar to be freed
75 afs_path(const char *apath)
78 static const char prefix[] = "/afs/";
81 len = strlen(apath) + sizeof(prefix);
85 sprintf(path, "%s%s", prefix, apath);
91 fuafsd_init(struct fuse_conn_info *conn)
97 /* Wrappers around libuafs calls for FUSE */
100 fuafsd_getattr(const char *apath, struct stat *stbuf)
103 char *path = afs_path(apath);
105 code = uafs_lstat(path, stbuf);
116 fuafsd_opendir(const char *apath, struct fuse_file_info * fi)
119 char *path = afs_path(apath);
121 dirp = uafs_opendir(path);
129 fi->fh = (uintptr_t)dirp;
135 fuafsd_readdir(const char *path, void * buf, fuse_fill_dir_t filler,
136 off_t offset, struct fuse_file_info * fi)
139 struct usr_dirent * direntP;
141 dirp = (usr_DIR *)(uintptr_t)fi->fh;
144 while ((direntP = uafs_readdir(dirp))) {
145 if (filler(buf, direntP->d_name, NULL, 0)) {
155 fuafsd_releasedir(const char *path, struct fuse_file_info * fi)
157 return uafs_closedir((usr_DIR *)(uintptr_t)fi->fh);
161 fuafsd_create(const char *apath, mode_t mode, struct fuse_file_info * fi)
164 char *path = afs_path(apath);
166 fd = uafs_open(path, fi->flags, mode);
180 fuafsd_open(const char *path, struct fuse_file_info * fi)
182 return fuafsd_create(path, 0, fi);
186 fuafsd_read(const char *path, char * buf, size_t len, off_t offset,
187 struct fuse_file_info * fi)
193 code = uafs_pread(fd, buf, len, offset);
202 fuafsd_readlink(const char *apath, char * buf, size_t len)
205 char *path = afs_path(apath);
207 code = uafs_readlink(path, buf, len);
221 fuafsd_mkdir(const char *apath, mode_t mode)
224 char *path = afs_path(apath);
226 code = uafs_mkdir(path, mode);
237 fuafsd_unlink(const char *apath)
240 char *path = afs_path(apath);
242 code = uafs_unlink(path);
253 fuafsd_rmdir(const char *apath)
256 char *path = afs_path(apath);
258 code = uafs_rmdir(path);
269 fuafsd_symlink(const char *atarget, const char *asource)
272 char *target = afs_path(atarget);
273 char *source = afs_path(asource);
275 code = uafs_symlink(target, source);
287 fuafsd_rename(const char *aold, const char *anew)
290 char *old = afs_path(aold);
291 char *new = afs_path(anew);
293 code = uafs_rename(old, new);
305 fuafsd_link(const char *aexisting, const char *anew)
308 char *existing = afs_path(aexisting);
309 char *new = afs_path(anew);
311 code = uafs_link(existing, new);
323 fuafsd_chmod(const char *apath, mode_t mode)
326 char *path = afs_path(apath);
328 code = uafs_chmod(path, mode);
339 fuafsd_truncate(const char *apath, off_t length)
342 char *path = afs_path(apath);
344 code = uafs_truncate(path, length);
355 fuafsd_write(const char *path, const char *abuf, size_t len, off_t offset,
356 struct fuse_file_info * fi)
359 char *buf = malloc(len);
362 memcpy(buf, abuf, len);
364 code = uafs_pwrite(fd, buf, len, offset);
376 fuafsd_statvfs(const char * path, struct statvfs * buf)
378 if (uafs_statvfs(buf) < 0) {
383 * FUSE ignores frsize, and uses bsize for displaying e.g. available
384 * space. Just set bsize to frsize so we get a consistent `df` output.
386 buf->f_bsize = buf->f_frsize;
391 fuafsd_release(const char *path, struct fuse_file_info * fi)
397 if (uafs_close(fd) < 0) {
405 fuafsd_destroy(void * private_data)
410 static struct fuse_operations fuafsd_oper = {
412 .getattr = fuafsd_getattr,
413 .opendir = fuafsd_opendir,
414 .readdir = fuafsd_readdir,
415 .releasedir = fuafsd_releasedir,
417 .create = fuafsd_create,
419 .readlink = fuafsd_readlink,
420 .mkdir = fuafsd_mkdir,
421 .rmdir = fuafsd_rmdir,
423 .unlink = fuafsd_unlink,
424 .symlink = fuafsd_symlink,
425 .rename = fuafsd_rename,
426 .chmod = fuafsd_chmod,
427 .truncate = fuafsd_truncate,
428 .write = fuafsd_write,
429 .statfs = fuafsd_statvfs,
430 .release = fuafsd_release,
431 .destroy = fuafsd_destroy
434 /* Command line argument processing */
437 * See fuafsd_fuse_opt below.
440 fuafsd_cmd_check(struct cmd_syndesc * ts, void *beforeRock)
442 fuafsd_cmd_accept = 1;
447 * Split arguments into FUSE and afsd/libcmd arguments. To determine whether an
448 * argument is meant for FUSE or for the cmd interface, we pass the given
449 * argument to cmd_Dispatch, and see if our beforeProc is run (which we set to
450 * fuafsd_cmd_check). If it was run, the argument is acceptable to cmd; if it
451 * was not run, cmd doesn't know what to do with the argument, so we assume the
452 * argument is meant for FUSE. We can tell if fuafsd_cmd_check was run by the
453 * global fuafsd_cmd_accept bool, which is set to true when fuafsd_cmd_check is
457 split_args(const struct fuse_args *args)
461 cmd_SetBeforeProc(fuafsd_cmd_check, NULL);
463 nullfd = open("/dev/null", O_WRONLY);
464 stderr_save = dup(2);
465 if (nullfd < 0 || stderr_save < 0) {
470 for (i = 0; args->argv[i]; ++i) {
472 char *arg = args->argv[i];
473 char *argv[3] = {args->argv[0], arg, NULL};
475 if (fuafsd_cmd_accept) {
476 fuse_opt_add_arg(&afsd_args, arg);
477 fuafsd_cmd_accept = 0;
481 /* redirect stderr to null, so libcmd doesn't print out
482 * an error message for unknown options */
483 if (dup2(nullfd, 2) < 0) {
488 code = cmd_Dispatch(2, argv);
490 if (dup2(stderr_save, 2) < 0) {
495 /* fuafsd_cmd_check should prevent the dispatch from succeeding;
496 * the only way we should be able to succeed is if -help was
497 * specified, so just exit */
502 if (fuafsd_cmd_accept || code == CMD_TOOFEW) {
503 /* libcmd accepted the argument; must go to afsd */
504 fuse_opt_add_arg(&afsd_args, arg);
506 if (code == CMD_TOOFEW) {
507 /* flag takes another argument; get the next one, too */
508 fuafsd_cmd_accept = 1;
510 fuafsd_cmd_accept = 0;
514 /* libcmd doesn't recognize the argument; give it to FUSE */
515 fuse_opt_add_arg(&fuse_args, arg);
519 if (close(nullfd) < 0) {
522 if (close(stderr_save) < 0) {
526 cmd_SetBeforeProc(NULL, NULL);
530 * First we divide the given arguments into FUSE and cmd arguments, pass the
531 * FUSE arguments to FUSE, and call cmd_Dispatch in the FUSE init function.
534 main(int argc, char **argv)
537 struct fuse_args args = FUSE_ARGS_INIT(argc-1, &argv[1]);
538 fuse_opt_add_arg(&afsd_args, argv[0]);
539 fuse_opt_add_arg(&fuse_args, argv[0]);
541 /* let us determine file inode numbers, not FUSE. also make "AFS" appear
542 * in df/mount/mnttab/etc output. */
543 fuse_opt_add_arg(&fuse_args, "-ouse_ino,fsname=AFS");
545 code = uafs_Setup("/afs");
554 uafs_ParseArgs(afsd_args.argc, afsd_args.argv);
556 /* pass "-- /mount/dir" to fuse to specify dir to mount; "--" is
557 * just to make sure fuse doesn't interpret the mount dir as a flag
559 fuse_opt_add_arg(&fuse_args, "--");
560 fuse_opt_add_arg(&fuse_args, uafs_MountDir());
562 return fuse_main(fuse_args.argc, fuse_args.argv, &fuafsd_oper, NULL);