/*
* Copyright � 2007 Alistair Crooks. All rights reserved.
* Copyright � 2007 Antti Kantee. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/***************** start of pthread context routines ************************/
/*
* Notes on fuse_context:
* we follow fuse's lead and use the pthread specific information to hold
* a reference to the fuse_context structure for this thread.
*/
#ifdef MULTITHREADED_REFUSE
static pthread_mutex_t context_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_key_t context_key;
static unsigned long context_refc;
#endif
/* return the fuse_context struct related to this thread */
struct fuse_context *
fuse_get_context(void)
{
#ifdef MULTITHREADED_REFUSE
struct fuse_context *ctxt;
/* used as a callback function */
#ifdef MULTITHREADED_REFUSE
static void
free_context(void *ctxt)
{
free(ctxt);
}
#endif
/*
* Create the pthread key. The reason for the complexity is to
* enable use of multiple fuse instances within a single process.
*/
static int
create_context_key(void)
{
#ifdef MULTITHREADED_REFUSE
int rv;
/* struct fuse_context is potentially reused among different
* invocations of fuse_new() / fuse_destroy() pair. Clear its content
* on fuse_destroy() so that no dangling pointers remain in the
* context. */
static void
clear_context(void)
{
struct fuse_context *ctx;
static void
delete_context_key(void)
{
#ifdef MULTITHREADED_REFUSE
pthread_mutex_lock(&context_mutex);
/* If we are the last fuse instances using the key, delete it */
if (--context_refc == 0) {
free(pthread_getspecific(context_key));
pthread_key_delete(context_key);
}
pthread_mutex_unlock(&context_mutex);
#endif
}
/* set the uid and gid of the calling process in the current fuse context */
static void
set_fuse_context_uid_gid(const struct puffs_cred *cred)
{
struct fuse_context *fusectx;
uid_t uid;
gid_t gid;
/* set the pid of the calling process in the current fuse context */
static void
set_fuse_context_pid(struct puffs_usermount *pu)
{
struct puffs_cc *pcc = puffs_cc_getcc(pu);
struct fuse_context *fusectx;
/* ARGSUSED3 */
/* XXX: I have no idea how "off" is supposed to be used */
static int
puffs_fuse_fill_dir(void *buf, const char *name,
const struct stat *stbuf, off_t off, enum fuse_fill_dir_flags flags)
{
struct puffs_fuse_dirh *deh = buf;
ino_t dino;
uint8_t dtype;
/*
* Some FUSE file systems like to always use 0 as the
* inode number. Our readdir() doesn't like to show
* directory entries with inode number 0 ==> workaround.
*/
if (dino == 0) {
dino = fakeino++;
}
}
/* wrap up return code */
ret = fuse_fs_mkdir(fuse->fs, path, mode);
if (ret == 0) {
ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
}
return -ret;
}
/*
* create a regular file
*
* since linux/fuse sports using mknod for creating regular files
* instead of having a separate call for it in some versions, if
* we don't have create, just jump to op->mknod.
*/
/*ARGSUSED1*/
static int
puffs_fuse_node_create(struct puffs_usermount *pu, void *opc,
struct puffs_newinfo *pni, const struct puffs_cn *pcn,
const struct vattr *va)
{
struct fuse *fuse;
struct fuse_file_info fi;
struct puffs_node *pn;
mode_t mode = va->va_mode;
const char *path = PCNPATH(pcn);
int ret, created;
fuse = puffs_getspecific(pu);
set_fuse_context_uid_gid(pcn->pcn_cred);
memset(&fi, 0, sizeof(fi));
/* In puffs "create" and "open" are two separate operations
* with atomicity achieved by locking the parent vnode. In
* fuse, on the other hand, "create" is actually a
* create-and-open-atomically and the open flags (O_RDWR,
* O_APPEND, ...) are passed via fi.flags. So the only way to
* emulate the fuse semantics is to open the file with dummy
* flags and then immediately close it.
*
* You might think that we could simply use fuse->op.mknod all
* the time but no, that's not possible because most file
* systems nowadays expect op.mknod to be called only for
* non-regular files and many don't even support it. */
created = 0;
fi.flags = O_WRONLY | O_CREAT | O_EXCL;
ret = fuse_fs_create(fuse->fs, path, mode | S_IFREG, &fi);
if (ret == 0) {
created = 1;
}
else if (ret == -ENOSYS) {
ret = fuse_fs_mknod(fuse->fs, path, mode | S_IFREG, 0);
}
if (ret == 0) {
ret = fuse_newnode(pu, path, va, &fi, pni, &pn);
/* sweet.. create also open the file */
if (created) {
struct refusenode *rn = pn->pn_data;
/* The return value of op.release is expected to be
* discarded. */
(void)fuse_fs_release(fuse->fs, path, &rn->file_info);
}
}
ret = fuse_fs_rename_v30(fuse->fs, path_src, path_dest, 0);
return -ret;
}
/* create a link in the file system */
/* ARGSUSED1 */
static int
puffs_fuse_node_link(struct puffs_usermount *pu, void *opc, void *targ,
const struct puffs_cn *pcn)
{
struct puffs_node *pn = targ;
struct fuse *fuse;
int ret;
fuse = puffs_getspecific(pu);
set_fuse_context_uid_gid(pcn->pcn_cred);
/* wrap up return code */
ret = fuse_fs_link(fuse->fs, PNPATH(pn), PCNPATH(pcn));
return -ret;
}
/*
* fuse's regular interface provides chmod(), chown(), utimes()
* and truncate() + some variations, so try to fit the square block
* in the circle hole and the circle block .... something like that
*/
/* ARGSUSED3 */
static int
puffs_fuse_node_setattr(struct puffs_usermount *pu, void *opc,
const struct vattr *va, const struct puffs_cred *pcr)
{
struct puffs_node *pn = opc;
struct fuse *fuse;
const char *path = PNPATH(pn);
fuse = puffs_getspecific(pu);
set_fuse_context_uid_gid(pcr);
return fuse_setattr(fuse, pn, path, va);
}
static int
puffs_fuse_node_pathconf(struct puffs_usermount *pu, void *opc,
int name, __register_t *retval)
{
/* Returning EINVAL for pathconf(2) means that this filesystem
* does not support an association of the given name with the
* file. This is necessary because the default error code
* returned by the puffs kernel module (ENOTSUPP) is not
* suitable for an errno from pathconf(2), and "ls -l"
* complains about it. */
return EINVAL;
}
/*
* if we are starting from the beginning, slurp entire directory
* into our buffers
*/
if (*readoff == 0) {
/* free old buffers */
free(dirh->dbuf);
memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
/* ARGSUSED0 */
static int
puffs_fuse_fs_sync(struct puffs_usermount *pu, int flags,
const struct puffs_cred *cr)
{
set_fuse_context_uid_gid(cr);
return 0;
}
/* ARGSUSED2 */
static int
puffs_fuse_fs_statvfs(struct puffs_usermount *pu, struct puffs_statvfs *svfsb)
{
struct fuse *fuse;
int ret;
struct statvfs sb;
/* fuse_fs_statfs() is special: it returns 0 even if the
* filesystem doesn't support statfs. So clear the struct
* before calling it. */
memset(&sb, 0, sizeof(sb));
fuse = puffs_getspecific(pu);
ret = fuse_fs_statfs(fuse->fs, PNPATH(puffs_getroot(pu)), &sb);
if (ret == 0)
statvfs_to_puffs_statvfs(&sb, svfsb);
if (opts->show_version) {
fuse_lowlevel_version();
goto free_args;
}
if (opts->show_help) {
switch (opts->show_help) {
case REFUSE_SHOW_HELP_FULL:
if (args.argv[0] != NULL && args.argv[0][0] != '\0') {
/* argv[0] being empty means that the application doesn't
* want us to print the usage string.
*/
printf("Usage: %s [options] mountpoint\n\n", args.argv[0]);
}
break;
case REFUSE_SHOW_HELP_NO_HEADER:
break;
}
fuse_cmdline_help();
goto free_args;
}
if (opts->mountpoint == NULL) {
fprintf(stderr, "fuse: no mountpoint specified\n");
goto free_args;
}
if (opts->debug) {
if (fuse_opt_add_arg(&args, "-odebug") != 0)
goto free_args;
}
/* sane defaults */
puffs_vattr_null(&pn_root->pn_va);
pn_root->pn_va.va_type = VDIR;
pn_root->pn_va.va_mode = 0755;
/* It might be tempting to call op.getattr("/") here to
* populate pn_root->pa_va, but that would mean invoking an
* operation callback without initializing the filesystem. We
* cannot call op.init() either, because that is supposed to
* be called right before entering the main loop. */
int fuse_daemonize(int foreground)
{
/* There is an impedance mismatch here: FUSE wants to
* daemonize the process without any contexts but puffs wants
* one. */
struct fuse *fuse = fuse_get_context()->fuse;
if (!fuse)
/* FUSE would probably allow this, but we cannot. */
errx(EXIT_FAILURE,
"%s: librefuse doesn't allow calling"
" this function before fuse_new().", __func__);
if (!foreground)
return puffs_daemon(fuse->pu, 0, 0);
/* struct fuse_conn_info is a part of the FUSE API so we must
* expose it to users, but we currently don't use them at
* all. The same goes for struct fuse_config. */
memset(&conn, 0, sizeof(conn));
memset(&cfg, 0, sizeof(cfg));
fuse_fs_init_v30(fuse->fs, &conn, &cfg);
return puffs_mainloop(fuse->pu);
}
int
__fuse_loop_mt(struct fuse *fuse,
struct fuse_loop_config *config __attribute__((__unused__)))
{
/* TODO: Implement a proper multi-threaded loop. */
return fuse_loop(fuse);
}
void
__fuse_destroy(struct fuse *fuse)
{
/*
* TODO: needs to assert the fs is quiescent, i.e. no other
* threads exist
*/
/*
* XXX: obviously not the most perfect of functions, but needs some
* puffs tweaking for a better tomorrow
*/
void
__fuse_unmount(struct fuse *fuse)
{
/* XXX: puffs_exit() is WRONG */
if (fuse->dead == 0)
puffs_exit(fuse->pu, 1);
fuse->dead = 1;
}
int
fuse_interrupted(void)
{
/* ReFUSE doesn't support request interruption at the
* moment. */
return 0;
}
int
fuse_invalidate_path(struct fuse *fuse __attribute__((__unused__)),
const char *path __attribute__((__unused__)))
{
/* ReFUSE doesn't cache anything at the moment. No need to do
* anything. */
return -ENOENT;
}
int
fuse_version(void)
{
return _REFUSE_VERSION_;
}
int
fuse_getgroups(int size, gid_t list[])
{
/* XXX: In order to implement this, we need to save a pointer
* to struct puffs_cred in struct fuse upon entering a puffs
* callback, and set it back to NULL upon leaving it. Then we
* can use puffs_cred_getgroups(3) here. */
return -ENOSYS;
}
int
fuse_start_cleanup_thread(struct fuse *fuse)
{
/* XXX: ReFUSE doesn't support -oremember at the moment. */
return 0;
}
void
fuse_stop_cleanup_thread(struct fuse *fuse) {
/* XXX: ReFUSE doesn't support -oremember at the moment. */
}
int
fuse_clean_cache(struct fuse *fuse) {
/* XXX: ReFUSE doesn't support -oremember at the moment. */
return 3600;
}
/* This is a legacy function that has been removed from the FUSE API,
* but is defined here because it needs to access refuse_opts. */
int
fuse_is_lib_option(const char *opt)
{
return fuse_opt_match(refuse_opts, opt);
}