libfuse/lib/fuse.c

2424 lines
65 KiB
C
Raw Normal View History

2001-10-29 03:44:14 +08:00
/*
FUSE: Filesystem in Userspace
2006-01-07 02:29:40 +08:00
Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
2001-10-29 03:44:14 +08:00
2002-10-25 20:41:16 +08:00
This program can be distributed under the terms of the GNU LGPL.
See the file COPYING.LIB
2001-10-29 03:44:14 +08:00
*/
2005-07-15 21:31:36 +08:00
/* For pthread_rwlock_t */
#define _GNU_SOURCE
2006-05-08 20:35:04 +08:00
#include "config.h"
2005-08-15 21:19:07 +08:00
#include "fuse_i.h"
2005-07-15 21:31:36 +08:00
#include "fuse_lowlevel.h"
2005-12-10 01:41:42 +08:00
#include "fuse_opt.h"
2001-10-29 03:44:14 +08:00
2005-01-04 20:45:54 +08:00
#include <stdio.h>
2001-10-29 03:44:14 +08:00
#include <string.h>
2001-11-07 20:09:43 +08:00
#include <stdlib.h>
2005-12-10 01:41:42 +08:00
#include <stddef.h>
2001-10-29 03:44:14 +08:00
#include <unistd.h>
2006-01-31 02:14:51 +08:00
#include <time.h>
2005-11-16 21:00:24 +08:00
#include <fcntl.h>
2001-11-07 20:09:43 +08:00
#include <limits.h>
2001-10-29 03:44:14 +08:00
#include <errno.h>
2005-01-04 20:45:54 +08:00
#include <assert.h>
2005-07-15 21:31:36 +08:00
#include <pthread.h>
2001-12-27 02:08:09 +08:00
#include <sys/param.h>
2005-04-07 23:40:21 +08:00
#include <sys/uio.h>
2001-10-29 03:44:14 +08:00
2001-11-07 20:09:43 +08:00
#define FUSE_MAX_PATH 4096
2004-03-02 19:11:24 +08:00
2005-12-15 00:18:32 +08:00
#define FUSE_UNKNOWN_INO 0xffffffff
2005-12-10 01:41:42 +08:00
struct fuse_config {
unsigned int uid;
unsigned int gid;
unsigned int umask;
double entry_timeout;
double negative_timeout;
double attr_timeout;
2006-02-17 00:59:39 +08:00
double ac_attr_timeout;
int ac_attr_timeout_set;
2005-12-10 01:41:42 +08:00
int debug;
int hard_remove;
int use_ino;
int readdir_ino;
int set_mode;
int set_uid;
int set_gid;
int direct_io;
int kernel_cache;
2006-01-31 02:14:51 +08:00
int auto_cache;
2005-12-10 01:41:42 +08:00
};
2005-07-15 21:31:36 +08:00
struct fuse {
2005-08-15 07:00:27 +08:00
struct fuse_session *se;
2005-07-15 21:31:36 +08:00
struct fuse_operations op;
int compat;
struct node **name_table;
size_t name_table_size;
struct node **id_table;
size_t id_table_size;
fuse_ino_t ctr;
unsigned int generation;
unsigned int hidectr;
pthread_mutex_t lock;
pthread_rwlock_t tree_lock;
void *user_data;
2005-12-10 01:41:42 +08:00
struct fuse_config conf;
2005-07-15 21:31:36 +08:00
};
2005-01-04 20:45:54 +08:00
struct node {
struct node *name_next;
struct node *id_next;
2005-07-15 17:59:59 +08:00
fuse_ino_t nodeid;
2005-01-04 20:45:54 +08:00
unsigned int generation;
int refctr;
2005-07-15 17:59:59 +08:00
fuse_ino_t parent;
2005-01-04 20:45:54 +08:00
char *name;
2005-05-09 03:47:22 +08:00
uint64_t nlookup;
2005-01-04 20:45:54 +08:00
int open_count;
int is_hidden;
2006-01-31 02:14:51 +08:00
struct timespec stat_updated;
struct timespec mtime;
off_t size;
int cache_valid;
2005-01-04 20:45:54 +08:00
};
struct fuse_dirhandle {
2005-04-07 23:40:21 +08:00
pthread_mutex_t lock;
2005-01-04 20:45:54 +08:00
struct fuse *fuse;
2006-02-17 23:49:25 +08:00
fuse_req_t req;
2005-07-28 19:07:29 +08:00
char *contents;
2005-04-07 23:40:21 +08:00
int allocated;
2005-02-08 00:10:49 +08:00
unsigned len;
2005-10-20 22:48:50 +08:00
unsigned size;
2005-04-07 23:40:21 +08:00
unsigned needlen;
2005-01-19 05:23:41 +08:00
int filled;
2005-11-12 05:32:42 +08:00
uint64_t fh;
2005-04-07 23:40:21 +08:00
int error;
2005-07-15 17:59:59 +08:00
fuse_ino_t nodeid;
2005-01-04 20:45:54 +08:00
};
2004-09-22 16:48:26 +08:00
static struct fuse_context *(*fuse_getcontext)(void) = NULL;
2005-11-18 01:11:48 +08:00
static int fuse_do_open(struct fuse *, char *, struct fuse_file_info *);
static void fuse_do_release(struct fuse *, char *, struct fuse_file_info *);
static int fuse_do_opendir(struct fuse *, char *, struct fuse_file_info *);
2006-08-19 02:43:50 +08:00
static int fuse_do_statfs(struct fuse *, struct statvfs *);
2005-11-18 01:11:48 +08:00
2005-04-07 23:40:21 +08:00
#ifndef USE_UCLIBC
#define mutex_init(mut) pthread_mutex_init(mut, NULL)
#else
2005-06-20 21:48:42 +08:00
static void mutex_init(pthread_mutex_t *mut)
2005-04-07 23:40:21 +08:00
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
pthread_mutex_init(mut, &attr);
pthread_mutexattr_destroy(&attr);
}
#endif
2005-07-15 17:59:59 +08:00
static struct node *get_node_nocheck(struct fuse *f, fuse_ino_t nodeid)
2001-11-06 20:03:23 +08:00
{
size_t hash = nodeid % f->id_table_size;
2001-11-07 20:09:43 +08:00
struct node *node;
for (node = f->id_table[hash]; node != NULL; node = node->id_next)
if (node->nodeid == nodeid)
2001-11-07 20:09:43 +08:00
return node;
2005-02-02 19:14:04 +08:00
2001-11-07 20:09:43 +08:00
return NULL;
2001-11-06 20:03:23 +08:00
}
2005-07-15 17:59:59 +08:00
static struct node *get_node(struct fuse *f, fuse_ino_t nodeid)
2001-11-06 20:03:23 +08:00
{
2004-12-08 00:46:42 +08:00
struct node *node = get_node_nocheck(f, nodeid);
2005-01-04 20:45:54 +08:00
if (!node) {
2005-11-12 05:32:42 +08:00
fprintf(stderr, "fuse internal error: node %llu not found\n",
(unsigned long long) nodeid);
2005-01-04 20:45:54 +08:00
abort();
}
return node;
2001-11-06 20:03:23 +08:00
}
2005-01-04 20:45:54 +08:00
static void free_node(struct node *node)
2001-10-29 03:44:14 +08:00
{
2005-01-04 20:45:54 +08:00
free(node->name);
free(node);
2001-10-29 03:44:14 +08:00
}
static void unhash_id(struct fuse *f, struct node *node)
2001-10-29 03:44:14 +08:00
{
size_t hash = node->nodeid % f->id_table_size;
struct node **nodep = &f->id_table[hash];
2001-11-07 20:09:43 +08:00
2005-02-02 19:14:04 +08:00
for (; *nodep != NULL; nodep = &(*nodep)->id_next)
2004-06-20 06:42:38 +08:00
if (*nodep == node) {
*nodep = node->id_next;
2001-11-07 20:09:43 +08:00
return;
}
2001-10-29 03:44:14 +08:00
}
2005-01-04 20:45:54 +08:00
static void hash_id(struct fuse *f, struct node *node)
2001-11-07 20:09:43 +08:00
{
2005-01-04 20:45:54 +08:00
size_t hash = node->nodeid % f->id_table_size;
node->id_next = f->id_table[hash];
2005-02-02 19:14:04 +08:00
f->id_table[hash] = node;
2001-11-07 20:09:43 +08:00
}
2005-07-15 17:59:59 +08:00
static unsigned int name_hash(struct fuse *f, fuse_ino_t parent, const char *name)
2001-11-07 20:09:43 +08:00
{
unsigned int hash = *name;
2004-06-20 06:42:38 +08:00
if (hash)
for (name += 1; *name != '\0'; name++)
2001-11-07 20:09:43 +08:00
hash = (hash << 5) - hash + *name;
return (hash + parent) % f->name_table_size;
2001-10-29 03:44:14 +08:00
}
2005-01-04 20:45:54 +08:00
static void unref_node(struct fuse *f, struct node *node);
2001-11-06 20:03:23 +08:00
2001-11-07 20:09:43 +08:00
static void unhash_name(struct fuse *f, struct node *node)
2001-10-31 22:52:35 +08:00
{
2005-01-04 20:45:54 +08:00
if (node->name) {
2001-11-07 20:09:43 +08:00
size_t hash = name_hash(f, node->parent, node->name);
struct node **nodep = &f->name_table[hash];
2005-02-02 19:14:04 +08:00
2004-06-20 06:42:38 +08:00
for (; *nodep != NULL; nodep = &(*nodep)->name_next)
if (*nodep == node) {
2001-11-07 20:09:43 +08:00
*nodep = node->name_next;
node->name_next = NULL;
2005-01-04 20:45:54 +08:00
unref_node(f, get_node(f, node->parent));
2001-11-07 20:09:43 +08:00
free(node->name);
node->name = NULL;
node->parent = 0;
return;
}
2005-11-12 05:32:42 +08:00
fprintf(stderr, "fuse internal error: unable to unhash node: %llu\n",
(unsigned long long) node->nodeid);
2001-11-07 20:09:43 +08:00
abort();
2001-11-06 20:03:23 +08:00
}
2001-10-31 22:52:35 +08:00
}
2005-07-15 17:59:59 +08:00
static int hash_name(struct fuse *f, struct node *node, fuse_ino_t parent,
2005-01-04 20:45:54 +08:00
const char *name)
{
size_t hash = name_hash(f, parent, name);
node->name = strdup(name);
if (node->name == NULL)
return -1;
get_node(f, parent)->refctr ++;
node->parent = parent;
node->name_next = f->name_table[hash];
f->name_table[hash] = node;
return 0;
}
static void delete_node(struct fuse *f, struct node *node)
{
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf("delete: %llu\n", (unsigned long long) node->nodeid);
2005-05-09 03:47:22 +08:00
fflush(stdout);
}
2005-01-04 20:45:54 +08:00
assert(!node->name);
unhash_id(f, node);
free_node(node);
}
static void unref_node(struct fuse *f, struct node *node)
{
assert(node->refctr > 0);
node->refctr --;
if (!node->refctr)
delete_node(f, node);
}
2005-07-15 17:59:59 +08:00
static fuse_ino_t next_id(struct fuse *f)
2005-01-04 20:45:54 +08:00
{
do {
f->ctr++;
if (!f->ctr)
f->generation ++;
} while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
return f->ctr;
}
2005-07-15 17:59:59 +08:00
static struct node *lookup_node(struct fuse *f, fuse_ino_t parent,
2005-01-04 20:45:54 +08:00
const char *name)
{
size_t hash = name_hash(f, parent, name);
struct node *node;
for (node = f->name_table[hash]; node != NULL; node = node->name_next)
if (node->parent == parent && strcmp(node->name, name) == 0)
return node;
return NULL;
}
2005-07-15 17:59:59 +08:00
static struct node *find_node(struct fuse *f, fuse_ino_t parent,
const char *name)
2001-10-30 23:06:52 +08:00
{
struct node *node;
2001-11-06 20:03:23 +08:00
pthread_mutex_lock(&f->lock);
2004-12-08 00:46:42 +08:00
node = lookup_node(f, parent, name);
2005-07-06 21:34:02 +08:00
if (node == NULL) {
2005-01-04 20:45:54 +08:00
node = (struct node *) calloc(1, sizeof(struct node));
if (node == NULL)
goto out_err;
2005-02-02 19:14:04 +08:00
2005-01-04 20:45:54 +08:00
node->refctr = 1;
node->nodeid = next_id(f);
node->open_count = 0;
node->is_hidden = 0;
node->generation = f->generation;
if (hash_name(f, node, parent, name) == -1) {
free(node);
node = NULL;
goto out_err;
}
hash_id(f, node);
2001-10-31 22:52:35 +08:00
}
2005-05-09 03:47:22 +08:00
node->nlookup ++;
2004-09-21 21:40:38 +08:00
out_err:
2001-11-06 20:03:23 +08:00
pthread_mutex_unlock(&f->lock);
2004-02-20 00:55:40 +08:00
return node;
2001-10-29 03:44:14 +08:00
}
2001-11-07 20:09:43 +08:00
static char *add_name(char *buf, char *s, const char *name)
2001-10-29 03:44:14 +08:00
{
2001-11-07 20:09:43 +08:00
size_t len = strlen(name);
s -= len;
2004-06-20 06:42:38 +08:00
if (s <= buf) {
2001-11-07 20:09:43 +08:00
fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
return NULL;
2001-10-29 03:44:14 +08:00
}
2001-11-07 20:09:43 +08:00
strncpy(s, name, len);
s--;
*s = '/';
2001-10-29 03:44:14 +08:00
2001-11-07 20:09:43 +08:00
return s;
2001-10-29 03:44:14 +08:00
}
2005-07-15 17:59:59 +08:00
static char *get_path_name(struct fuse *f, fuse_ino_t nodeid, const char *name)
2001-10-29 22:57:57 +08:00
{
2001-11-07 20:09:43 +08:00
char buf[FUSE_MAX_PATH];
char *s = buf + FUSE_MAX_PATH - 1;
struct node *node;
2005-02-02 19:14:04 +08:00
2001-11-07 20:09:43 +08:00
*s = '\0';
2004-06-20 06:42:38 +08:00
if (name != NULL) {
2001-11-07 20:09:43 +08:00
s = add_name(buf, s, name);
2004-06-20 06:42:38 +08:00
if (s == NULL)
2001-11-07 20:09:43 +08:00
return NULL;
}
2001-11-06 20:03:23 +08:00
2001-11-07 20:09:43 +08:00
pthread_mutex_lock(&f->lock);
2005-01-04 20:45:54 +08:00
for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
node = get_node(f, node->parent)) {
2004-06-20 06:42:38 +08:00
if (node->name == NULL) {
2001-11-07 20:09:43 +08:00
s = NULL;
break;
}
2005-02-02 19:14:04 +08:00
2001-11-07 20:09:43 +08:00
s = add_name(buf, s, node->name);
2004-06-20 06:42:38 +08:00
if (s == NULL)
2001-11-07 20:09:43 +08:00
break;
}
pthread_mutex_unlock(&f->lock);
2005-01-04 20:45:54 +08:00
if (node == NULL || s == NULL)
2001-10-31 22:52:35 +08:00
return NULL;
2004-06-20 06:42:38 +08:00
else if (*s == '\0')
2001-11-07 20:09:43 +08:00
return strdup("/");
else
return strdup(s);
}
2001-11-06 20:03:23 +08:00
2005-07-15 17:59:59 +08:00
static char *get_path(struct fuse *f, fuse_ino_t nodeid)
2001-11-07 20:09:43 +08:00
{
return get_path_name(f, nodeid, NULL);
2001-10-29 22:57:57 +08:00
}
2005-07-15 17:59:59 +08:00
static void forget_node(struct fuse *f, fuse_ino_t nodeid, uint64_t nlookup)
2005-05-09 03:47:22 +08:00
{
struct node *node;
if (nodeid == FUSE_ROOT_ID)
return;
pthread_mutex_lock(&f->lock);
node = get_node(f, nodeid);
assert(node->nlookup >= nlookup);
node->nlookup -= nlookup;
if (!node->nlookup) {
unhash_name(f, node);
unref_node(f, node);
}
pthread_mutex_unlock(&f->lock);
}
2005-07-15 17:59:59 +08:00
static void remove_node(struct fuse *f, fuse_ino_t dir, const char *name)
2001-10-31 22:52:35 +08:00
{
2001-11-06 20:03:23 +08:00
struct node *node;
pthread_mutex_lock(&f->lock);
2004-12-08 00:46:42 +08:00
node = lookup_node(f, dir, name);
2005-01-04 20:45:54 +08:00
if (node != NULL)
unhash_name(f, node);
2001-11-06 20:03:23 +08:00
pthread_mutex_unlock(&f->lock);
2001-10-31 22:52:35 +08:00
}
2005-07-15 17:59:59 +08:00
static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname,
fuse_ino_t newdir, const char *newname, int hide)
2001-10-30 23:06:52 +08:00
{
2001-11-06 20:03:23 +08:00
struct node *node;
struct node *newnode;
2004-06-25 05:00:00 +08:00
int err = 0;
2005-02-02 19:14:04 +08:00
2001-11-06 20:03:23 +08:00
pthread_mutex_lock(&f->lock);
2004-12-08 00:46:42 +08:00
node = lookup_node(f, olddir, oldname);
newnode = lookup_node(f, newdir, newname);
2005-01-04 20:45:54 +08:00
if (node == NULL)
goto out;
2001-10-30 23:06:52 +08:00
2004-06-25 05:00:00 +08:00
if (newnode != NULL) {
if (hide) {
fprintf(stderr, "fuse: hidden file got created during hiding\n");
2004-09-16 16:42:40 +08:00
err = -EBUSY;
2004-06-25 05:00:00 +08:00
goto out;
}
2001-11-07 20:09:43 +08:00
unhash_name(f, newnode);
2004-06-25 05:00:00 +08:00
}
2005-02-02 19:14:04 +08:00
2001-11-07 20:09:43 +08:00
unhash_name(f, node);
2004-09-16 16:42:40 +08:00
if (hash_name(f, node, newdir, newname) == -1) {
err = -ENOMEM;
goto out;
}
2005-02-02 19:14:04 +08:00
2004-06-25 05:00:00 +08:00
if (hide)
node->is_hidden = 1;
out:
2001-11-06 20:03:23 +08:00
pthread_mutex_unlock(&f->lock);
2004-06-25 05:00:00 +08:00
return err;
2001-10-30 23:06:52 +08:00
}
2005-07-15 17:59:59 +08:00
static void set_stat(struct fuse *f, fuse_ino_t nodeid, struct stat *stbuf)
2001-10-29 03:44:14 +08:00
{
2005-12-10 01:41:42 +08:00
if (!f->conf.use_ino)
2005-07-15 17:59:59 +08:00
stbuf->st_ino = nodeid;
2005-12-10 01:41:42 +08:00
if (f->conf.set_mode)
stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask);
if (f->conf.set_uid)
stbuf->st_uid = f->conf.uid;
if (f->conf.set_gid)
stbuf->st_gid = f->conf.gid;
2001-10-29 03:44:14 +08:00
}
2005-07-15 17:59:59 +08:00
static int is_open(struct fuse *f, fuse_ino_t dir, const char *name)
2004-06-25 05:00:00 +08:00
{
struct node *node;
int isopen = 0;
pthread_mutex_lock(&f->lock);
2004-12-08 00:46:42 +08:00
node = lookup_node(f, dir, name);
2004-06-25 05:00:00 +08:00
if (node && node->open_count > 0)
isopen = 1;
pthread_mutex_unlock(&f->lock);
return isopen;
}
2005-07-15 17:59:59 +08:00
static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname,
2004-06-25 05:00:00 +08:00
char *newname, size_t bufsize)
{
struct stat buf;
struct node *node;
struct node *newnode;
char *newpath;
int res;
int failctr = 10;
if (!f->op.getattr)
return NULL;
do {
pthread_mutex_lock(&f->lock);
2004-12-08 00:46:42 +08:00
node = lookup_node(f, dir, oldname);
if (node == NULL) {
2005-01-04 20:45:54 +08:00
pthread_mutex_unlock(&f->lock);
return NULL;
2004-12-08 00:46:42 +08:00
}
2004-06-25 05:00:00 +08:00
do {
f->hidectr ++;
snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
(unsigned int) node->nodeid, f->hidectr);
2004-12-08 00:46:42 +08:00
newnode = lookup_node(f, dir, newname);
2004-06-25 05:00:00 +08:00
} while(newnode);
pthread_mutex_unlock(&f->lock);
2005-02-02 19:14:04 +08:00
2004-06-25 05:00:00 +08:00
newpath = get_path_name(f, dir, newname);
if (!newpath)
break;
2005-02-02 19:14:04 +08:00
2004-06-25 05:00:00 +08:00
res = f->op.getattr(newpath, &buf);
if (res != 0)
break;
free(newpath);
newpath = NULL;
} while(--failctr);
return newpath;
}
2005-07-15 17:59:59 +08:00
static int hide_node(struct fuse *f, const char *oldpath, fuse_ino_t dir,
2004-06-25 05:00:00 +08:00
const char *oldname)
{
char newname[64];
char *newpath;
2004-09-16 16:42:40 +08:00
int err = -EBUSY;
2004-06-25 05:00:00 +08:00
2004-09-16 16:42:40 +08:00
if (f->op.rename && f->op.unlink) {
newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
if (newpath) {
int res = f->op.rename(oldpath, newpath);
if (res == 0)
err = rename_node(f, dir, oldname, dir, newname, 1);
free(newpath);
}
2004-06-25 05:00:00 +08:00
}
2004-09-16 16:42:40 +08:00
return err;
2004-06-25 05:00:00 +08:00
}
2006-01-31 02:14:51 +08:00
static int mtime_eq(const struct stat *stbuf, const struct timespec *ts)
{
return stbuf->st_mtime == ts->tv_sec
#ifdef HAVE_STRUCT_STAT_ST_ATIM
&& stbuf->st_mtim.tv_nsec == ts->tv_nsec
#endif
;
}
static void mtime_set(const struct stat *stbuf, struct timespec *ts)
{
#ifdef HAVE_STRUCT_STAT_ST_ATIM
*ts = stbuf->st_mtim;
#else
ts->tv_sec = stbuf->st_mtime;
#endif
}
2006-05-03 22:54:59 +08:00
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC CLOCK_REALTIME
#endif
2006-01-31 02:14:51 +08:00
static void curr_time(struct timespec *now)
{
static clockid_t clockid = CLOCK_MONOTONIC;
int res = clock_gettime(clockid, now);
if (res == -1 && errno == EINVAL) {
clockid = CLOCK_REALTIME;
res = clock_gettime(clockid, now);
}
if (res == -1) {
perror("fuse: clock_gettime");
abort();
}
}
static void update_stat(struct node *node, const struct stat *stbuf)
{
if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) ||
stbuf->st_size != node->size))
node->cache_valid = 0;
mtime_set(stbuf, &node->mtime);
node->size = stbuf->st_size;
curr_time(&node->stat_updated);
}
2005-07-15 17:59:59 +08:00
static int lookup_path(struct fuse *f, fuse_ino_t nodeid, const char *name,
2005-10-28 21:09:50 +08:00
const char *path, struct fuse_entry_param *e,
struct fuse_file_info *fi)
2004-02-20 00:55:40 +08:00
{
int res;
2005-07-15 17:59:59 +08:00
memset(e, 0, sizeof(struct fuse_entry_param));
2005-10-28 21:09:50 +08:00
if (fi && f->op.fgetattr)
res = f->op.fgetattr(path, &e->attr, fi);
else
res = f->op.getattr(path, &e->attr);
2004-06-20 06:42:38 +08:00
if (res == 0) {
2004-02-20 00:55:40 +08:00
struct node *node;
2005-07-15 17:59:59 +08:00
node = find_node(f, nodeid, name);
2004-09-16 16:42:40 +08:00
if (node == NULL)
res = -ENOMEM;
else {
2005-07-15 17:59:59 +08:00
e->ino = node->nodeid;
e->generation = node->generation;
2005-12-10 01:41:42 +08:00
e->entry_timeout = f->conf.entry_timeout;
e->attr_timeout = f->conf.attr_timeout;
2006-01-31 02:14:51 +08:00
if (f->conf.auto_cache) {
pthread_mutex_lock(&f->lock);
update_stat(node, &e->attr);
pthread_mutex_unlock(&f->lock);
}
2005-07-15 17:59:59 +08:00
set_stat(f, e->ino, &e->attr);
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-07-15 17:59:59 +08:00
printf(" NODEID: %lu\n", (unsigned long) e->ino);
2004-09-16 16:42:40 +08:00
fflush(stdout);
}
2004-02-20 00:55:40 +08:00
}
}
return res;
}
2005-07-15 17:59:59 +08:00
static struct fuse *req_fuse(fuse_req_t req)
2001-10-29 03:44:14 +08:00
{
2005-07-15 17:59:59 +08:00
return (struct fuse *) fuse_req_userdata(req);
}
static struct fuse *req_fuse_prepare(fuse_req_t req)
{
struct fuse_context *c = fuse_get_context();
const struct fuse_ctx *ctx = fuse_req_ctx(req);
c->fuse = req_fuse(req);
c->uid = ctx->uid;
c->gid = ctx->gid;
c->pid = ctx->pid;
c->private_data = c->fuse->user_data;
return c->fuse;
}
static inline void reply_err(fuse_req_t req, int err)
{
/* fuse_reply_err() uses non-negated errno values */
fuse_reply_err(req, -err);
}
static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e,
int err)
{
if (!err) {
2006-02-20 18:55:33 +08:00
struct fuse *f = req_fuse(req);
2005-07-21 15:59:37 +08:00
if (fuse_reply_entry(req, e) == -ENOENT)
2006-02-20 18:55:33 +08:00
forget_node(f, e->ino, 1);
2005-07-15 17:59:59 +08:00
} else
reply_err(req, err);
}
2006-01-20 23:15:21 +08:00
static void fuse_data_init(void *data, struct fuse_conn_info *conn)
2005-07-15 17:59:59 +08:00
{
struct fuse *f = (struct fuse *) data;
2005-10-07 20:39:58 +08:00
struct fuse_context *c = fuse_get_context();
memset(c, 0, sizeof(*c));
c->fuse = f;
2006-03-17 23:05:40 +08:00
c->private_data = f->user_data;
2005-07-15 17:59:59 +08:00
if (f->op.init)
2006-01-20 23:15:21 +08:00
f->user_data = f->op.init(conn);
2005-07-15 17:59:59 +08:00
}
static void fuse_data_destroy(void *data)
{
struct fuse *f = (struct fuse *) data;
2005-10-07 20:39:58 +08:00
struct fuse_context *c = fuse_get_context();
memset(c, 0, sizeof(*c));
c->fuse = f;
c->private_data = f->user_data;
2005-07-15 17:59:59 +08:00
if (f->op.destroy)
f->op.destroy(f->user_data);
}
static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
{
struct fuse *f = req_fuse_prepare(req);
struct fuse_entry_param e;
2001-10-30 23:06:52 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2001-10-29 03:44:14 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path_name(f, parent, name);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2002-01-07 05:44:16 +08:00
printf("LOOKUP %s\n", path);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2005-11-28 21:27:10 +08:00
if (f->op.getattr) {
2005-10-28 21:09:50 +08:00
err = lookup_path(f, parent, name, path, &e, NULL);
2005-12-10 01:41:42 +08:00
if (err == -ENOENT && f->conf.negative_timeout != 0.0) {
2005-11-28 21:27:10 +08:00
e.ino = 0;
2005-12-10 01:41:42 +08:00
e.entry_timeout = f->conf.negative_timeout;
2005-11-28 21:27:10 +08:00
err = 0;
}
}
2001-11-07 20:09:43 +08:00
free(path);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_entry(req, &e, err);
2001-10-29 03:44:14 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_forget(fuse_req_t req, fuse_ino_t ino, unsigned long nlookup)
2001-10-29 03:44:14 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse(req);
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf("FORGET %llu/%lu\n", (unsigned long long) ino, nlookup);
2001-11-19 03:15:05 +08:00
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
forget_node(f, ino, nlookup);
fuse_reply_none(req);
2001-10-29 03:44:14 +08:00
}
2005-08-25 23:19:06 +08:00
static void fuse_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
2001-10-29 03:44:14 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2001-10-30 23:06:52 +08:00
struct stat buf;
2005-07-15 17:59:59 +08:00
char *path;
int err;
2001-10-29 03:44:14 +08:00
2005-08-25 23:19:06 +08:00
(void) fi;
2006-09-02 21:20:40 +08:00
memset(&buf, 0, sizeof(buf));
2005-08-25 23:19:06 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.getattr)
2005-07-15 17:59:59 +08:00
err = f->op.getattr(path, &buf);
2001-11-07 20:09:43 +08:00
free(path);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
if (!err) {
2006-02-01 21:39:15 +08:00
if (f->conf.auto_cache) {
pthread_mutex_lock(&f->lock);
update_stat(get_node(f, ino), &buf);
pthread_mutex_unlock(&f->lock);
}
2005-07-15 17:59:59 +08:00
set_stat(f, ino, &buf);
2005-12-10 01:41:42 +08:00
fuse_reply_attr(req, &buf, f->conf.attr_timeout);
2005-07-15 17:59:59 +08:00
} else
reply_err(req, err);
2001-10-29 03:44:14 +08:00
}
2005-07-15 17:59:59 +08:00
static int do_chmod(struct fuse *f, const char *path, struct stat *attr)
2001-11-07 22:55:16 +08:00
{
2005-07-15 17:59:59 +08:00
int err;
2001-11-07 22:55:16 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.chmod)
2005-07-15 17:59:59 +08:00
err = f->op.chmod(path, attr->st_mode);
2001-11-07 22:55:16 +08:00
2005-07-15 17:59:59 +08:00
return err;
2005-02-02 19:14:04 +08:00
}
2001-11-07 22:55:16 +08:00
2005-07-15 17:59:59 +08:00
static int do_chown(struct fuse *f, const char *path, struct stat *attr,
2001-12-20 20:17:25 +08:00
int valid)
2001-11-07 22:55:16 +08:00
{
2005-07-15 17:59:59 +08:00
int err;
uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t) -1;
gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t) -1;
2005-02-02 19:14:04 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.chown)
2005-07-15 17:59:59 +08:00
err = f->op.chown(path, uid, gid);
2001-11-07 22:55:16 +08:00
2005-07-15 17:59:59 +08:00
return err;
2001-11-07 22:55:16 +08:00
}
2005-10-27 00:04:04 +08:00
static int do_truncate(struct fuse *f, const char *path, struct stat *attr,
struct fuse_file_info *fi)
2001-11-07 22:55:16 +08:00
{
2005-07-15 17:59:59 +08:00
int err;
2001-11-07 22:55:16 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2005-10-27 00:04:04 +08:00
if (fi && f->op.ftruncate)
err = f->op.ftruncate(path, attr->st_size, fi);
else if (f->op.truncate)
2005-07-15 17:59:59 +08:00
err = f->op.truncate(path, attr->st_size);
2001-11-07 22:55:16 +08:00
2005-07-15 17:59:59 +08:00
return err;
2001-11-07 22:55:16 +08:00
}
2006-09-02 17:51:08 +08:00
static int do_utimes(struct fuse *f, const char *path, struct stat *attr)
2001-11-07 22:55:16 +08:00
{
2005-07-15 17:59:59 +08:00
int err;
2006-09-02 17:51:08 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2006-09-02 17:51:08 +08:00
if (f->op.utimes) {
struct timespec tv[2];
tv[0] = attr->st_atim;
tv[1] = attr->st_mtim;
err = f->op.utimes(path, tv);
} else if (f->op.utime) {
struct utimbuf buf;
buf.actime = attr->st_atime;
buf.modtime = attr->st_mtime;
2005-07-15 17:59:59 +08:00
err = f->op.utime(path, &buf);
2006-09-02 17:51:08 +08:00
}
2001-11-07 22:55:16 +08:00
2005-07-15 17:59:59 +08:00
return err;
2001-11-07 22:55:16 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
2005-08-25 23:19:06 +08:00
int valid, struct fuse_file_info *fi)
2001-10-31 22:52:35 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
struct stat buf;
2001-10-31 22:52:35 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2001-10-31 22:52:35 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.getattr) {
2005-07-15 17:59:59 +08:00
err = 0;
if (!err && (valid & FUSE_SET_ATTR_MODE))
err = do_chmod(f, path, attr);
if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)))
err = do_chown(f, path, attr, valid);
if (!err && (valid & FUSE_SET_ATTR_SIZE))
2005-10-27 00:04:04 +08:00
err = do_truncate(f, path, attr, fi);
2005-07-15 17:59:59 +08:00
if (!err && (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))
2006-09-02 17:51:08 +08:00
err = do_utimes(f, path, attr);
2005-07-15 17:59:59 +08:00
if (!err)
err = f->op.getattr(path, &buf);
2001-10-31 22:52:35 +08:00
}
2001-11-07 20:09:43 +08:00
free(path);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
if (!err) {
2006-02-01 21:39:15 +08:00
if (f->conf.auto_cache) {
pthread_mutex_lock(&f->lock);
update_stat(get_node(f, ino), &buf);
pthread_mutex_unlock(&f->lock);
}
2005-07-15 17:59:59 +08:00
set_stat(f, ino, &buf);
2005-12-10 01:41:42 +08:00
fuse_reply_attr(req, &buf, f->conf.attr_timeout);
2005-07-15 17:59:59 +08:00
} else
reply_err(req, err);
2001-10-31 22:52:35 +08:00
}
2005-10-26 20:53:25 +08:00
static void fuse_access(fuse_req_t req, fuse_ino_t ino, int mask)
{
struct fuse *f = req_fuse_prepare(req);
char *path;
int err;
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-10-26 20:53:25 +08:00
printf("ACCESS %s 0%o\n", path, mask);
fflush(stdout);
}
err = -ENOSYS;
if (f->op.access)
err = f->op.access(path, mask);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
reply_err(req, err);
}
2005-07-15 17:59:59 +08:00
static void fuse_readlink(fuse_req_t req, fuse_ino_t ino)
2001-10-29 03:44:14 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2005-08-03 17:11:06 +08:00
char linkname[PATH_MAX + 1];
2001-10-29 22:57:57 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2001-10-29 22:57:57 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.readlink)
2005-08-03 17:11:06 +08:00
err = f->op.readlink(path, linkname, sizeof(linkname));
2001-11-07 20:09:43 +08:00
free(path);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
if (!err) {
2005-08-03 17:11:06 +08:00
linkname[PATH_MAX] = '\0';
fuse_reply_readlink(req, linkname);
2005-07-15 17:59:59 +08:00
} else
reply_err(req, err);
2001-10-29 03:44:14 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, dev_t rdev)
2001-10-29 03:44:14 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
struct fuse_entry_param e;
2001-10-29 22:57:57 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2001-10-29 03:44:14 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path_name(f, parent, name);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2004-02-20 00:55:40 +08:00
printf("MKNOD %s\n", path);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2005-11-16 21:00:24 +08:00
if (S_ISREG(mode) && f->op.create && f->op.getattr) {
struct fuse_file_info fi;
memset(&fi, 0, sizeof(fi));
fi.flags = O_CREAT | O_EXCL | O_WRONLY;
err = f->op.create(path, mode, &fi);
if (!err) {
err = lookup_path(f, parent, name, path, &e, &fi);
if (f->op.release)
f->op.release(path, &fi);
}
} else if (f->op.mknod && f->op.getattr) {
2005-07-15 17:59:59 +08:00
err = f->op.mknod(path, mode, rdev);
if (!err)
2005-10-28 21:09:50 +08:00
err = lookup_path(f, parent, name, path, &e, NULL);
2001-10-31 22:52:35 +08:00
}
2001-11-07 20:09:43 +08:00
free(path);
2001-10-29 03:44:14 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_entry(req, &e, err);
2001-10-29 03:44:14 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode)
2001-10-29 03:44:14 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
struct fuse_entry_param e;
2001-10-29 22:57:57 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2001-10-29 03:44:14 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path_name(f, parent, name);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2004-02-20 00:55:40 +08:00
printf("MKDIR %s\n", path);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.mkdir && f->op.getattr) {
2005-07-15 17:59:59 +08:00
err = f->op.mkdir(path, mode);
if (!err)
2005-10-28 21:09:50 +08:00
err = lookup_path(f, parent, name, path, &e, NULL);
2004-02-20 00:55:40 +08:00
}
2001-11-07 20:09:43 +08:00
free(path);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_entry(req, &e, err);
2001-10-29 22:57:57 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
2001-10-29 22:57:57 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2001-10-29 22:57:57 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2001-10-29 22:57:57 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_wrlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path_name(f, parent, name);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2004-07-25 03:56:16 +08:00
printf("UNLINK %s\n", path);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.unlink) {
2005-12-10 01:41:42 +08:00
if (!f->conf.hard_remove && is_open(f, parent, name))
2005-07-15 17:59:59 +08:00
err = hide_node(f, path, parent, name);
2004-06-25 05:00:00 +08:00
else {
2005-07-15 17:59:59 +08:00
err = f->op.unlink(path);
if (!err)
remove_node(f, parent, name);
2004-06-25 05:00:00 +08:00
}
2001-10-31 22:52:35 +08:00
}
2004-02-20 22:10:49 +08:00
free(path);
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2004-02-20 22:10:49 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
2004-02-20 22:10:49 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2004-02-20 22:10:49 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2004-02-20 22:10:49 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_wrlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path_name(f, parent, name);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2004-07-25 03:56:16 +08:00
printf("RMDIR %s\n", path);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.rmdir) {
2005-07-15 17:59:59 +08:00
err = f->op.rmdir(path);
if (!err)
remove_node(f, parent, name);
2001-10-31 22:52:35 +08:00
}
2001-11-07 20:09:43 +08:00
free(path);
2001-10-29 22:57:57 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2001-10-29 22:57:57 +08:00
}
2005-08-03 17:11:06 +08:00
static void fuse_symlink(fuse_req_t req, const char *linkname,
fuse_ino_t parent, const char *name)
2001-10-29 22:57:57 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
struct fuse_entry_param e;
2001-10-29 22:57:57 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2001-10-29 22:57:57 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path_name(f, parent, name);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2004-02-20 00:55:40 +08:00
printf("SYMLINK %s\n", path);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.symlink && f->op.getattr) {
2005-08-03 17:11:06 +08:00
err = f->op.symlink(linkname, path);
2005-07-15 17:59:59 +08:00
if (!err)
2005-10-28 21:09:50 +08:00
err = lookup_path(f, parent, name, path, &e, NULL);
2004-02-20 00:55:40 +08:00
}
2001-11-07 20:09:43 +08:00
free(path);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_entry(req, &e, err);
2001-10-29 03:44:14 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_rename(fuse_req_t req, fuse_ino_t olddir, const char *oldname,
fuse_ino_t newdir, const char *newname)
2001-10-30 23:06:52 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2001-10-31 22:52:35 +08:00
char *oldpath;
char *newpath;
2005-07-15 17:59:59 +08:00
int err;
2001-10-31 22:52:35 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_wrlock(&f->tree_lock);
2001-11-06 20:03:23 +08:00
oldpath = get_path_name(f, olddir, oldname);
2004-06-20 06:42:38 +08:00
if (oldpath != NULL) {
2001-11-06 20:03:23 +08:00
newpath = get_path_name(f, newdir, newname);
2004-06-20 06:42:38 +08:00
if (newpath != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2004-07-25 03:56:16 +08:00
printf("RENAME %s -> %s\n", oldpath, newpath);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-25 05:00:00 +08:00
if (f->op.rename) {
2005-07-15 17:59:59 +08:00
err = 0;
2005-12-10 01:41:42 +08:00
if (!f->conf.hard_remove &&
is_open(f, newdir, newname))
2005-07-15 17:59:59 +08:00
err = hide_node(f, newpath, newdir, newname);
if (!err) {
err = f->op.rename(oldpath, newpath);
if (!err)
err = rename_node(f, olddir, oldname, newdir, newname, 0);
2004-06-25 05:00:00 +08:00
}
}
2001-11-07 20:09:43 +08:00
free(newpath);
2001-10-31 22:52:35 +08:00
}
2001-11-07 20:09:43 +08:00
free(oldpath);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2001-10-30 23:06:52 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent,
const char *newname)
2001-10-30 23:06:52 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
struct fuse_entry_param e;
2001-10-31 22:52:35 +08:00
char *oldpath;
char *newpath;
2005-07-15 17:59:59 +08:00
int err;
2001-10-31 22:52:35 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
oldpath = get_path(f, ino);
2004-06-20 06:42:38 +08:00
if (oldpath != NULL) {
2005-07-15 17:59:59 +08:00
newpath = get_path_name(f, newparent, newname);
2004-06-20 06:42:38 +08:00
if (newpath != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2004-02-20 00:55:40 +08:00
printf("LINK %s\n", newpath);
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.link && f->op.getattr) {
2005-07-15 17:59:59 +08:00
err = f->op.link(oldpath, newpath);
if (!err)
2005-10-28 21:09:50 +08:00
err = lookup_path(f, newparent, newname, newpath, &e, NULL);
2004-02-20 00:55:40 +08:00
}
2001-11-07 20:09:43 +08:00
free(newpath);
2001-10-31 22:52:35 +08:00
}
2001-11-07 20:09:43 +08:00
free(oldpath);
2001-10-31 22:52:35 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_entry(req, &e, err);
2001-10-31 22:52:35 +08:00
}
2001-10-30 23:06:52 +08:00
2005-10-26 23:29:06 +08:00
static void fuse_create(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, struct fuse_file_info *fi)
{
struct fuse *f = req_fuse_prepare(req);
struct fuse_entry_param e;
char *path;
int err;
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path_name(f, parent, name);
if (path != NULL) {
err = -ENOSYS;
if (f->op.create && f->op.getattr) {
err = f->op.create(path, mode, fi);
if (!err) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf("CREATE[%llu] flags: 0x%x %s\n",
(unsigned long long) fi->fh, fi->flags, path);
2005-10-26 23:29:06 +08:00
fflush(stdout);
}
2005-10-28 21:09:50 +08:00
err = lookup_path(f, parent, name, path, &e, fi);
2005-10-26 23:29:06 +08:00
if (err) {
if (f->op.release)
f->op.release(path, fi);
} else if (!S_ISREG(e.attr.st_mode)) {
err = -EIO;
if (f->op.release)
f->op.release(path, fi);
forget_node(f, e.ino, 1);
}
}
}
}
if (!err) {
2005-12-10 01:41:42 +08:00
if (f->conf.direct_io)
2005-10-26 23:29:06 +08:00
fi->direct_io = 1;
2005-12-10 01:41:42 +08:00
if (f->conf.kernel_cache)
2005-10-26 23:29:06 +08:00
fi->keep_cache = 1;
pthread_mutex_lock(&f->lock);
if (fuse_reply_create(req, &e, fi) == -ENOENT) {
/* The open syscall was interrupted, so it must be cancelled */
if(f->op.release)
f->op.release(path, fi);
forget_node(f, e.ino, 1);
} else {
struct node *node = get_node(f, e.ino);
node->open_count ++;
}
pthread_mutex_unlock(&f->lock);
} else
reply_err(req, err);
if (path)
free(path);
pthread_rwlock_unlock(&f->tree_lock);
}
2006-01-31 02:14:51 +08:00
static double diff_timespec(const struct timespec *t1,
const struct timespec *t2)
{
return (t1->tv_sec - t2->tv_sec) +
((double) t1->tv_nsec - (double) t2->tv_nsec) / 1000000000.0;
}
static void open_auto_cache(struct fuse *f, fuse_ino_t ino, const char *path,
struct fuse_file_info *fi)
{
struct node *node = get_node(f, ino);
2006-02-01 21:39:15 +08:00
if (node->cache_valid) {
struct timespec now;
curr_time(&now);
2006-02-17 00:59:39 +08:00
if (diff_timespec(&now, &node->stat_updated) > f->conf.ac_attr_timeout) {
2006-02-01 21:39:15 +08:00
struct stat stbuf;
int err;
if (f->op.fgetattr)
err = f->op.fgetattr(path, &stbuf, fi);
else
err = f->op.getattr(path, &stbuf);
if (!err)
update_stat(node, &stbuf);
else
node->cache_valid = 0;
}
2006-01-31 02:14:51 +08:00
}
if (node->cache_valid)
fi->keep_cache = 1;
2006-02-01 21:39:15 +08:00
node->cache_valid = 1;
2006-01-31 02:14:51 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
2001-10-31 22:52:35 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2005-08-01 22:49:31 +08:00
char *path = NULL;
int err = 0;
2001-10-30 23:06:52 +08:00
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-08-01 22:49:31 +08:00
if (f->op.open) {
err = -ENOENT;
path = get_path(f, ino);
2005-11-18 01:11:48 +08:00
if (path != NULL)
err = fuse_do_open(f, path, fi);
2002-12-11 00:09:02 +08:00
}
2005-07-15 17:59:59 +08:00
if (!err) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf("OPEN[%llu] flags: 0x%x\n", (unsigned long long) fi->fh,
fi->flags);
2004-07-25 03:56:16 +08:00
fflush(stdout);
}
2005-02-02 19:14:04 +08:00
2005-12-10 01:41:42 +08:00
if (f->conf.direct_io)
2005-08-01 19:58:51 +08:00
fi->direct_io = 1;
2005-12-10 01:41:42 +08:00
if (f->conf.kernel_cache)
2005-08-01 19:58:51 +08:00
fi->keep_cache = 1;
2005-01-04 20:45:54 +08:00
pthread_mutex_lock(&f->lock);
2006-01-31 02:14:51 +08:00
if (f->conf.auto_cache)
open_auto_cache(f, ino, path, fi);
2005-07-15 17:59:59 +08:00
if (fuse_reply_open(req, fi) == -ENOENT) {
2004-06-25 05:00:00 +08:00
/* The open syscall was interrupted, so it must be cancelled */
2005-11-18 01:11:48 +08:00
if(f->op.release && path != NULL)
fuse_do_release(f, path, fi);
2005-01-04 20:45:54 +08:00
} else {
2005-07-15 17:59:59 +08:00
struct node *node = get_node(f, ino);
2005-01-04 20:45:54 +08:00
node->open_count ++;
}
pthread_mutex_unlock(&f->lock);
} else
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2004-06-27 05:11:25 +08:00
if (path)
free(path);
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2001-10-30 23:06:52 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
struct fuse_file_info *fi)
{
struct fuse *f = req_fuse_prepare(req);
char *path;
char *buf;
int res;
buf = (char *) malloc(size);
if (buf == NULL) {
reply_err(req, -ENOMEM);
return;
}
res = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2006-02-25 00:21:58 +08:00
printf("READ[%llu] %lu bytes from %llu\n",
(unsigned long long) fi->fh, (unsigned long) size,
(unsigned long long) off);
2005-07-15 17:59:59 +08:00
fflush(stdout);
}
res = -ENOSYS;
if (f->op.read)
res = f->op.read(path, buf, size, off, fi);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
if (res >= 0) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf(" READ[%llu] %u bytes\n", (unsigned long long) fi->fh,
res);
2005-07-15 17:59:59 +08:00
fflush(stdout);
}
2005-10-15 05:24:32 +08:00
if ((size_t) res > size)
fprintf(stderr, "fuse: read too many bytes");
2005-07-15 17:59:59 +08:00
fuse_reply_buf(req, buf, res);
} else
reply_err(req, res);
free(buf);
}
static void fuse_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
size_t size, off_t off, struct fuse_file_info *fi)
2004-05-18 16:45:28 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2004-05-18 16:45:28 +08:00
char *path;
int res;
res = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2004-06-20 06:42:38 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2006-02-25 00:21:58 +08:00
printf("WRITE%s[%llu] %lu bytes to %llu\n",
2005-11-12 05:32:42 +08:00
fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh,
2006-02-25 00:21:58 +08:00
(unsigned long) size, (unsigned long long) off);
2004-07-25 03:56:16 +08:00
fflush(stdout);
}
2005-07-15 17:59:59 +08:00
2004-05-18 16:45:28 +08:00
res = -ENOSYS;
2005-07-15 17:59:59 +08:00
if (f->op.write)
res = f->op.write(path, buf, size, off, fi);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
2005-10-15 05:24:32 +08:00
if (res >= 0) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf(" WRITE%s[%llu] %u bytes\n",
fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh,
res);
2005-10-15 05:24:32 +08:00
fflush(stdout);
}
if ((size_t) res > size)
fprintf(stderr, "fuse: wrote too many bytes");
2005-07-15 17:59:59 +08:00
fuse_reply_write(req, res);
2005-10-15 05:24:32 +08:00
} else
2005-07-15 17:59:59 +08:00
reply_err(req, res);
}
static void fuse_flush(fuse_req_t req, fuse_ino_t ino,
2006-06-28 22:51:20 +08:00
struct fuse_file_info *fi, uint64_t owner)
2005-07-15 17:59:59 +08:00
{
struct fuse *f = req_fuse_prepare(req);
char *path;
int err;
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf("FLUSH[%llu]\n", (unsigned long long) fi->fh);
2005-07-15 17:59:59 +08:00
fflush(stdout);
}
err = -ENOSYS;
2004-06-20 06:42:38 +08:00
if (f->op.flush)
2005-07-15 17:59:59 +08:00
err = f->op.flush(path, fi);
2004-05-18 16:45:28 +08:00
free(path);
}
2006-07-31 01:33:40 +08:00
if (f->op.lock) {
struct flock lock;
memset(&lock, 0, sizeof(lock));
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
f->op.lock(path, fi, F_SETLK, &lock, owner);
if (err == -ENOSYS)
err = 0;
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2004-05-18 16:45:28 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_release(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
2002-12-10 20:26:00 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2004-06-25 05:00:00 +08:00
char *path;
2005-07-15 17:59:59 +08:00
struct node *node;
2005-01-04 20:45:54 +08:00
int unlink_hidden;
2004-11-26 20:15:06 +08:00
2006-02-25 22:42:03 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf("RELEASE[%llu] flags: 0x%x\n", (unsigned long long) fi->fh,
fi->flags);
2005-01-04 20:45:54 +08:00
fflush(stdout);
}
2005-11-18 01:11:48 +08:00
if (f->op.release)
fuse_do_release(f, path, fi);
2005-02-02 19:14:04 +08:00
2006-07-31 01:33:40 +08:00
pthread_mutex_lock(&f->lock);
node = get_node(f, ino);
assert(node->open_count > 0);
--node->open_count;
unlink_hidden = (node->is_hidden && !node->open_count);
pthread_mutex_unlock(&f->lock);
2005-01-04 20:45:54 +08:00
if(unlink_hidden && path)
f->op.unlink(path);
2005-02-02 19:14:04 +08:00
2005-01-04 20:45:54 +08:00
if (path)
free(path);
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-01-04 20:45:54 +08:00
2005-07-15 17:59:59 +08:00
reply_err(req, 0);
2002-12-10 20:26:00 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
struct fuse_file_info *fi)
2001-10-31 22:52:35 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2001-10-31 22:52:35 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2005-02-02 19:14:04 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2005-04-07 23:40:21 +08:00
if (path != NULL) {
2005-12-10 01:41:42 +08:00
if (f->conf.debug) {
2005-11-12 05:32:42 +08:00
printf("FSYNC[%llu]\n", (unsigned long long) fi->fh);
2005-04-07 23:40:21 +08:00
fflush(stdout);
2002-01-07 05:44:16 +08:00
}
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
if (f->op.fsync)
err = f->op.fsync(path, datasync, fi);
2005-04-07 23:40:21 +08:00
free(path);
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2001-10-31 22:52:35 +08:00
}
2001-10-29 22:57:57 +08:00
2005-07-15 17:59:59 +08:00
static struct fuse_dirhandle *get_dirhandle(const struct fuse_file_info *llfi,
struct fuse_file_info *fi)
2001-11-06 20:03:23 +08:00
{
2005-11-12 05:32:42 +08:00
struct fuse_dirhandle *dh = (struct fuse_dirhandle *) (uintptr_t) llfi->fh;
2005-07-15 17:59:59 +08:00
memset(fi, 0, sizeof(struct fuse_file_info));
fi->fh = dh->fh;
2005-11-29 00:02:27 +08:00
fi->fh_old = dh->fh;
2005-07-15 17:59:59 +08:00
return dh;
}
2001-11-06 20:03:23 +08:00
2005-07-15 17:59:59 +08:00
static void fuse_opendir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *llfi)
{
struct fuse *f = req_fuse_prepare(req);
struct fuse_dirhandle *dh;
2002-01-07 05:44:16 +08:00
2005-07-15 17:59:59 +08:00
dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
if (dh == NULL) {
reply_err(req, -ENOMEM);
return;
2005-04-02 05:08:57 +08:00
}
memset(dh, 0, sizeof(struct fuse_dirhandle));
dh->fuse = f;
dh->contents = NULL;
dh->len = 0;
dh->filled = 0;
2005-07-15 17:59:59 +08:00
dh->nodeid = ino;
2005-04-07 23:40:21 +08:00
mutex_init(&dh->lock);
2005-04-02 05:08:57 +08:00
2005-11-12 05:32:42 +08:00
llfi->fh = (uintptr_t) dh;
2005-04-02 05:08:57 +08:00
2005-02-28 19:46:56 +08:00
if (f->op.opendir) {
2005-04-02 05:08:57 +08:00
struct fuse_file_info fi;
2005-02-28 19:46:56 +08:00
char *path;
2005-07-15 17:59:59 +08:00
int err;
2005-04-02 05:08:57 +08:00
memset(&fi, 0, sizeof(fi));
2005-07-15 17:59:59 +08:00
fi.flags = llfi->flags;
2005-04-02 05:08:57 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2005-02-28 19:46:56 +08:00
if (path != NULL) {
2005-11-18 01:11:48 +08:00
err = fuse_do_opendir(f, path, &fi);
dh->fh = fi.fh;
2005-02-28 19:46:56 +08:00
}
2005-07-15 17:59:59 +08:00
if (!err) {
2005-04-02 05:08:57 +08:00
pthread_mutex_lock(&f->lock);
2005-07-15 17:59:59 +08:00
if (fuse_reply_open(req, llfi) == -ENOENT) {
2005-04-02 05:08:57 +08:00
/* The opendir syscall was interrupted, so it must be
cancelled */
if(f->op.releasedir)
f->op.releasedir(path, &fi);
2005-04-07 23:40:21 +08:00
pthread_mutex_destroy(&dh->lock);
2005-04-02 05:08:57 +08:00
free(dh);
}
pthread_mutex_unlock(&f->lock);
} else {
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2005-04-02 05:08:57 +08:00
free(dh);
2005-02-28 19:46:56 +08:00
}
2005-04-02 05:08:57 +08:00
free(path);
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-04-07 23:40:21 +08:00
} else
2005-07-15 17:59:59 +08:00
fuse_reply_open(req, llfi);
2005-01-19 05:23:41 +08:00
}
2006-02-17 23:49:25 +08:00
static int extend_contents(struct fuse_dirhandle *dh, unsigned minsize)
{
if (minsize > dh->size) {
char *newptr;
unsigned newsize = dh->size;
if (!newsize)
newsize = 1024;
while (newsize < minsize)
newsize *= 2;
newptr = (char *) realloc(dh->contents, newsize);
if (!newptr) {
dh->error = -ENOMEM;
return -1;
}
dh->contents = newptr;
dh->size = newsize;
}
return 0;
}
2005-04-07 23:40:21 +08:00
static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
2005-08-03 17:11:06 +08:00
const struct stat *statp, off_t off)
2005-01-19 05:23:41 +08:00
{
2005-07-15 17:59:59 +08:00
struct stat stbuf;
2006-02-17 23:49:25 +08:00
size_t newlen;
2005-01-19 05:23:41 +08:00
2005-08-03 17:11:06 +08:00
if (statp)
stbuf = *statp;
2005-07-15 17:59:59 +08:00
else {
memset(&stbuf, 0, sizeof(stbuf));
2005-12-15 00:18:32 +08:00
stbuf.st_ino = FUSE_UNKNOWN_INO;
2005-07-15 17:59:59 +08:00
}
2005-12-10 01:41:42 +08:00
if (!dh->fuse->conf.use_ino) {
2005-12-15 00:18:32 +08:00
stbuf.st_ino = FUSE_UNKNOWN_INO;
2005-12-10 01:41:42 +08:00
if (dh->fuse->conf.readdir_ino) {
2005-05-27 17:12:43 +08:00
struct node *node;
pthread_mutex_lock(&dh->fuse->lock);
2005-07-06 18:07:52 +08:00
node = lookup_node(dh->fuse, dh->nodeid, name);
2005-05-27 17:12:43 +08:00
if (node)
2005-07-15 17:59:59 +08:00
stbuf.st_ino = (ino_t) node->nodeid;
2005-05-27 17:12:43 +08:00
pthread_mutex_unlock(&dh->fuse->lock);
}
}
2005-04-07 23:40:21 +08:00
2005-07-15 17:59:59 +08:00
if (off) {
2006-02-17 23:49:25 +08:00
if (extend_contents(dh, dh->needlen) == -1)
return 1;
2005-04-07 23:40:21 +08:00
dh->filled = 0;
2006-02-17 23:49:25 +08:00
newlen = dh->len + fuse_add_direntry(dh->req, dh->contents + dh->len,
dh->needlen - dh->len, name,
&stbuf, off);
2005-04-07 23:40:21 +08:00
if (newlen > dh->needlen)
return 1;
2006-02-17 23:49:25 +08:00
} else {
newlen = dh->len + fuse_add_direntry(dh->req, NULL, 0, name, NULL, 0);
if (extend_contents(dh, newlen) == -1)
2005-10-20 22:48:50 +08:00
return 1;
2006-02-17 23:49:25 +08:00
fuse_add_direntry(dh->req, dh->contents + dh->len, dh->size - dh->len,
name, &stbuf, newlen);
2005-04-07 23:40:21 +08:00
}
dh->len = newlen;
return 0;
}
2005-04-02 05:08:57 +08:00
2005-08-03 17:11:06 +08:00
static int fill_dir(void *buf, const char *name, const struct stat *stbuf,
2005-04-07 23:40:21 +08:00
off_t off)
{
2005-08-03 17:11:06 +08:00
return fill_dir_common((struct fuse_dirhandle *) buf, name, stbuf, off);
2005-04-07 23:40:21 +08:00
}
static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
ino_t ino)
{
2005-07-15 17:59:59 +08:00
struct stat stbuf;
memset(&stbuf, 0, sizeof(stbuf));
stbuf.st_mode = type << 12;
stbuf.st_ino = ino;
fill_dir_common(dh, name, &stbuf, 0);
2005-04-07 23:40:21 +08:00
return dh->error;
}
2006-02-17 23:49:25 +08:00
static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off, struct fuse_dirhandle *dh,
2005-07-15 17:59:59 +08:00
struct fuse_file_info *fi)
2005-04-07 23:40:21 +08:00
{
int err = -ENOENT;
2005-06-20 21:48:42 +08:00
char *path;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2005-04-07 23:40:21 +08:00
if (path != NULL) {
dh->len = 0;
dh->error = 0;
2005-07-15 17:59:59 +08:00
dh->needlen = size;
2005-04-07 23:40:21 +08:00
dh->filled = 1;
2006-02-17 23:49:25 +08:00
dh->req = req;
2005-04-07 23:40:21 +08:00
err = -ENOSYS;
2005-07-15 17:59:59 +08:00
if (f->op.readdir)
err = f->op.readdir(path, dh, fill_dir, off, fi);
else if (f->op.getdir)
2005-04-07 23:40:21 +08:00
err = f->op.getdir(path, dh, fill_dir_old);
2006-02-17 23:49:25 +08:00
dh->req = NULL;
2005-04-07 23:40:21 +08:00
if (!err)
err = dh->error;
if (err)
dh->filled = 0;
2005-04-02 05:08:57 +08:00
free(path);
2005-04-07 23:40:21 +08:00
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-04-07 23:40:21 +08:00
return err;
}
2005-04-02 05:08:57 +08:00
2005-07-15 17:59:59 +08:00
static void fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *llfi)
2005-04-07 23:40:21 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
struct fuse_file_info fi;
struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi);
2005-04-02 05:08:57 +08:00
2005-04-07 23:40:21 +08:00
pthread_mutex_lock(&dh->lock);
2005-08-19 22:40:27 +08:00
/* According to SUS, directory contents need to be refreshed on
rewinddir() */
if (!off)
dh->filled = 0;
2005-04-07 23:40:21 +08:00
if (!dh->filled) {
2006-02-17 23:49:25 +08:00
int err = readdir_fill(f, req, ino, size, off, dh, &fi);
2005-07-15 17:59:59 +08:00
if (err) {
reply_err(req, err);
2005-04-07 23:40:21 +08:00
goto out;
2005-07-15 17:59:59 +08:00
}
2005-04-07 23:40:21 +08:00
}
if (dh->filled) {
2005-07-15 17:59:59 +08:00
if (off < dh->len) {
if (off + size > dh->len)
size = dh->len - off;
} else
size = 0;
2005-04-07 23:40:21 +08:00
} else {
size = dh->len;
2005-07-15 17:59:59 +08:00
off = 0;
2005-01-19 05:23:41 +08:00
}
2005-07-15 17:59:59 +08:00
fuse_reply_buf(req, dh->contents + off, size);
2005-04-07 23:40:21 +08:00
out:
pthread_mutex_unlock(&dh->lock);
2005-01-19 05:23:41 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_releasedir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *llfi)
2005-01-19 05:23:41 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2005-07-21 15:59:37 +08:00
struct fuse_file_info fi;
2005-07-15 17:59:59 +08:00
struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi);
2005-04-02 05:08:57 +08:00
if (f->op.releasedir) {
char *path;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2005-04-02 05:08:57 +08:00
f->op.releasedir(path ? path : "-", &fi);
free(path);
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-04-02 05:08:57 +08:00
}
2005-04-07 23:40:21 +08:00
pthread_mutex_lock(&dh->lock);
pthread_mutex_unlock(&dh->lock);
pthread_mutex_destroy(&dh->lock);
2005-02-08 00:10:49 +08:00
free(dh->contents);
2005-01-19 05:23:41 +08:00
free(dh);
2005-07-15 17:59:59 +08:00
reply_err(req, 0);
2005-01-19 05:23:41 +08:00
}
2005-07-15 17:59:59 +08:00
static void fuse_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
struct fuse_file_info *llfi)
2005-03-21 20:09:04 +08:00
{
2005-07-15 17:59:59 +08:00
struct fuse *f = req_fuse_prepare(req);
2005-03-21 20:09:04 +08:00
struct fuse_file_info fi;
2005-07-15 17:59:59 +08:00
char *path;
int err;
2005-03-21 20:09:04 +08:00
2005-07-15 17:59:59 +08:00
get_dirhandle(llfi, &fi);
2005-04-02 05:08:57 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
2005-05-09 21:29:17 +08:00
pthread_rwlock_rdlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
path = get_path(f, ino);
2005-03-21 20:09:04 +08:00
if (path != NULL) {
2005-07-15 17:59:59 +08:00
err = -ENOSYS;
2005-03-21 20:09:04 +08:00
if (f->op.fsyncdir)
2005-07-15 17:59:59 +08:00
err = f->op.fsyncdir(path, datasync, &fi);
2005-03-21 20:09:04 +08:00
free(path);
}
2005-05-09 21:29:17 +08:00
pthread_rwlock_unlock(&f->tree_lock);
2005-07-15 17:59:59 +08:00
reply_err(req, err);
2005-03-21 20:09:04 +08:00
}
2005-01-19 05:23:41 +08:00
2005-11-07 19:59:00 +08:00
static int default_statfs(struct statvfs *buf)
2001-11-19 03:15:05 +08:00
{
2005-11-07 19:59:00 +08:00
buf->f_namemax = 255;
2005-07-15 17:59:59 +08:00
buf->f_bsize = 512;
return 0;
2001-11-19 03:15:05 +08:00
}
2006-08-19 02:43:50 +08:00
static void fuse_statfs(fuse_req_t req, fuse_ino_t ino)
2005-07-15 17:59:59 +08:00
{
struct fuse *f = req_fuse_prepare(req);
2005-11-07 19:59:00 +08:00
struct statvfs buf;
2005-07-15 17:59:59 +08:00
int err;
2005-01-05 03:24:31 +08:00
2005-11-07 19:59:00 +08:00
memset(&buf, 0, sizeof(buf));
2005-07-15 17:59:59 +08:00
if (f->op.statfs) {
2006-08-19 02:43:50 +08:00
if (ino && (!f->compat || f->compat >= 26)) {
char *path;
pthread_rwlock_rdlock(&f->tree_lock);
err = -ENOENT;
path = get_path(f, ino);
if (path) {
err = f->op.statfs(path, &buf);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
} else
err = fuse_do_statfs(f, &buf);
2005-11-07 19:59:00 +08:00
} else
2005-07-15 17:59:59 +08:00
err = default_statfs(&buf);
2005-04-22 20:04:55 +08:00
2005-07-15 17:59:59 +08:00
if (!err)
fuse_reply_statfs(req, &buf);
else
reply_err(req, err);
}
2001-11-06 20:03:23 +08:00
2005-07-15 17:59:59 +08:00
static void fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
const char *value, size_t size, int flags)
{
struct fuse *f = req_fuse_prepare(req);
char *path;
int err;
2005-07-15 17:59:59 +08:00
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
err = -ENOSYS;
if (f->op.setxattr)
err = f->op.setxattr(path, name, value, size, flags);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
reply_err(req, err);
}
2003-12-12 22:06:41 +08:00
2005-07-15 17:59:59 +08:00
static int common_getxattr(struct fuse *f, fuse_ino_t ino, const char *name,
char *value, size_t size)
{
int err;
char *path;
2004-03-30 23:17:26 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
err = -ENOSYS;
if (f->op.getxattr)
err = f->op.getxattr(path, name, value, size);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
return err;
}
2004-03-30 23:17:26 +08:00
2005-07-15 17:59:59 +08:00
static void fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
size_t size)
{
struct fuse *f = req_fuse_prepare(req);
int res;
2004-03-30 23:17:26 +08:00
2005-07-15 17:59:59 +08:00
if (size) {
char *value = (char *) malloc(size);
if (value == NULL) {
reply_err(req, -ENOMEM);
return;
}
res = common_getxattr(f, ino, name, value, size);
if (res > 0)
fuse_reply_buf(req, value, res);
else
reply_err(req, res);
free(value);
} else {
res = common_getxattr(f, ino, name, NULL, 0);
if (res >= 0)
fuse_reply_xattr(req, res);
else
reply_err(req, res);
}
}
2004-03-30 23:17:26 +08:00
2005-07-15 17:59:59 +08:00
static int common_listxattr(struct fuse *f, fuse_ino_t ino, char *list,
size_t size)
{
char *path;
int err;
2005-01-05 03:24:31 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
err = -ENOSYS;
if (f->op.listxattr)
err = f->op.listxattr(path, list, size);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
return err;
}
2005-01-19 05:23:41 +08:00
2005-07-15 17:59:59 +08:00
static void fuse_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
{
struct fuse *f = req_fuse_prepare(req);
int res;
2005-01-19 05:23:41 +08:00
2005-07-15 17:59:59 +08:00
if (size) {
char *list = (char *) malloc(size);
if (list == NULL) {
reply_err(req, -ENOMEM);
return;
}
res = common_listxattr(f, ino, list, size);
if (res > 0)
fuse_reply_buf(req, list, res);
else
reply_err(req, res);
free(list);
} else {
res = common_listxattr(f, ino, NULL, 0);
if (res >= 0)
fuse_reply_xattr(req, res);
else
reply_err(req, res);
}
}
2005-01-19 05:23:41 +08:00
2005-07-15 17:59:59 +08:00
static void fuse_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
{
struct fuse *f = req_fuse_prepare(req);
char *path;
int err;
2005-03-21 20:09:04 +08:00
2005-07-15 17:59:59 +08:00
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
err = -ENOSYS;
if (f->op.removexattr)
err = f->op.removexattr(path, name);
free(path);
2001-11-06 20:03:23 +08:00
}
2005-07-15 17:59:59 +08:00
pthread_rwlock_unlock(&f->tree_lock);
reply_err(req, err);
}
2006-07-31 01:33:40 +08:00
static int fuse_lock_common(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi, struct flock *lock,
uint64_t owner, int cmd)
{
struct fuse *f = req_fuse_prepare(req);
char *path;
int err;
err = -ENOENT;
pthread_rwlock_rdlock(&f->tree_lock);
path = get_path(f, ino);
if (path != NULL) {
err = f->op.lock(path, fi, cmd, lock, owner);
free(path);
}
pthread_rwlock_unlock(&f->tree_lock);
return err;
}
static void fuse_getlk(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi, struct flock *lock,
uint64_t owner)
{
int err = fuse_lock_common(req, ino, fi, lock, owner, F_GETLK);
if (!err)
fuse_reply_lock(req, lock);
else
reply_err(req, err);
}
static void fuse_setlk(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi, struct flock *lock,
uint64_t owner, int sleep)
{
reply_err(req, fuse_lock_common(req, ino, fi, lock, owner,
sleep ? F_SETLKW : F_SETLK));
}
2005-08-15 07:00:27 +08:00
static struct fuse_lowlevel_ops fuse_path_ops = {
2005-07-15 17:59:59 +08:00
.init = fuse_data_init,
.destroy = fuse_data_destroy,
.lookup = fuse_lookup,
.forget = fuse_forget,
.getattr = fuse_getattr,
.setattr = fuse_setattr,
2005-10-26 20:53:25 +08:00
.access = fuse_access,
2005-07-15 17:59:59 +08:00
.readlink = fuse_readlink,
.mknod = fuse_mknod,
.mkdir = fuse_mkdir,
.unlink = fuse_unlink,
.rmdir = fuse_rmdir,
.symlink = fuse_symlink,
.rename = fuse_rename,
.link = fuse_link,
2005-10-26 23:29:06 +08:00
.create = fuse_create,
2005-07-15 17:59:59 +08:00
.open = fuse_open,
.read = fuse_read,
.write = fuse_write,
.flush = fuse_flush,
.release = fuse_release,
.fsync = fuse_fsync,
.opendir = fuse_opendir,
.readdir = fuse_readdir,
.releasedir = fuse_releasedir,
.fsyncdir = fuse_fsyncdir,
.statfs = fuse_statfs,
.setxattr = fuse_setxattr,
.getxattr = fuse_getxattr,
.listxattr = fuse_listxattr,
.removexattr = fuse_removexattr,
2006-07-31 01:33:40 +08:00
.getlk = fuse_getlk,
.setlk = fuse_setlk,
2005-07-15 17:59:59 +08:00
};
2001-11-19 03:15:05 +08:00
2005-08-15 07:00:27 +08:00
static void free_cmd(struct fuse_cmd *cmd)
{
free(cmd->buf);
free(cmd);
}
2005-07-15 17:59:59 +08:00
void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
{
2005-08-15 21:19:07 +08:00
fuse_session_process(f->se, cmd->buf, cmd->buflen, cmd->ch);
2005-12-02 19:05:41 +08:00
free_cmd(cmd);
2001-11-06 20:03:23 +08:00
}
2005-07-15 17:59:59 +08:00
int fuse_exited(struct fuse *f)
2004-06-30 19:34:56 +08:00
{
2005-08-15 07:00:27 +08:00
return fuse_session_exited(f->se);
}
struct fuse_session *fuse_get_session(struct fuse *f)
{
return f->se;
}
static struct fuse_cmd *fuse_alloc_cmd(size_t bufsize)
{
struct fuse_cmd *cmd = (struct fuse_cmd *) malloc(sizeof(*cmd));
if (cmd == NULL) {
fprintf(stderr, "fuse: failed to allocate cmd\n");
return NULL;
}
cmd->buf = (char *) malloc(bufsize);
if (cmd->buf == NULL) {
fprintf(stderr, "fuse: failed to allocate read buffer\n");
free(cmd);
return NULL;
}
return cmd;
2004-06-30 19:34:56 +08:00
}
2004-12-08 00:46:42 +08:00
struct fuse_cmd *fuse_read_cmd(struct fuse *f)
2001-11-06 23:07:17 +08:00
{
2005-08-15 07:00:27 +08:00
struct fuse_chan *ch = fuse_session_next_chan(f->se, NULL);
size_t bufsize = fuse_chan_bufsize(ch);
struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize);
if (cmd != NULL) {
2006-03-17 23:56:05 +08:00
int res = fuse_chan_recv(&ch, cmd->buf, bufsize);
2005-08-15 07:00:27 +08:00
if (res <= 0) {
free_cmd(cmd);
2006-03-01 20:10:13 +08:00
if (res < 0 && res != -EINTR && res != -EAGAIN)
2005-12-02 19:05:41 +08:00
fuse_exit(f);
2005-08-15 07:00:27 +08:00
return NULL;
}
cmd->buflen = res;
2005-08-15 21:19:07 +08:00
cmd->ch = ch;
2005-08-15 07:00:27 +08:00
}
return cmd;
2001-11-06 23:07:17 +08:00
}
2004-09-16 16:42:40 +08:00
int fuse_loop(struct fuse *f)
2001-10-29 03:44:14 +08:00
{
2005-07-15 17:59:59 +08:00
if (f)
2005-08-15 07:00:27 +08:00
return fuse_session_loop(f->se);
2005-07-15 17:59:59 +08:00
else
2004-09-16 16:42:40 +08:00
return -1;
2001-10-29 03:44:14 +08:00
}
2004-07-29 17:27:49 +08:00
int fuse_invalidate(struct fuse *f, const char *path)
{
2004-12-12 19:45:24 +08:00
(void) f;
(void) path;
return -EINVAL;
2004-07-29 17:27:49 +08:00
}
2002-12-05 22:23:01 +08:00
void fuse_exit(struct fuse *f)
{
2005-08-15 07:00:27 +08:00
fuse_session_exit(f->se);
2002-12-05 22:23:01 +08:00
}
2004-09-22 16:48:26 +08:00
struct fuse_context *fuse_get_context()
2001-12-20 20:17:25 +08:00
{
2004-09-22 16:48:26 +08:00
static struct fuse_context context;
if (fuse_getcontext)
return fuse_getcontext();
2001-12-20 20:17:25 +08:00
else
2004-09-22 16:48:26 +08:00
return &context;
}
2004-12-08 00:46:42 +08:00
void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
2004-09-22 16:48:26 +08:00
{
fuse_getcontext = func;
2001-12-20 20:17:25 +08:00
}
2006-01-07 18:14:34 +08:00
enum {
KEY_HELP,
};
2005-12-10 01:41:42 +08:00
#define FUSE_LIB_OPT(t, p, v) { t, offsetof(struct fuse_config, p), v }
static const struct fuse_opt fuse_lib_opts[] = {
2006-01-07 18:14:34 +08:00
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
2006-01-20 23:15:21 +08:00
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
2005-12-10 01:41:42 +08:00
FUSE_LIB_OPT("debug", debug, 1),
2006-01-07 02:29:40 +08:00
FUSE_LIB_OPT("-d", debug, 1),
2005-12-10 01:41:42 +08:00
FUSE_LIB_OPT("hard_remove", hard_remove, 1),
FUSE_LIB_OPT("use_ino", use_ino, 1),
FUSE_LIB_OPT("readdir_ino", readdir_ino, 1),
FUSE_LIB_OPT("direct_io", direct_io, 1),
FUSE_LIB_OPT("kernel_cache", kernel_cache, 1),
2006-01-31 02:14:51 +08:00
FUSE_LIB_OPT("auto_cache", auto_cache, 1),
FUSE_LIB_OPT("noauto_cache", auto_cache, 0),
2005-12-10 01:41:42 +08:00
FUSE_LIB_OPT("umask=", set_mode, 1),
FUSE_LIB_OPT("umask=%o", umask, 0),
FUSE_LIB_OPT("uid=", set_uid, 1),
FUSE_LIB_OPT("uid=%d", uid, 0),
FUSE_LIB_OPT("gid=", set_gid, 1),
FUSE_LIB_OPT("gid=%d", gid, 0),
FUSE_LIB_OPT("entry_timeout=%lf", entry_timeout, 0),
FUSE_LIB_OPT("attr_timeout=%lf", attr_timeout, 0),
2006-02-17 00:59:39 +08:00
FUSE_LIB_OPT("ac_attr_timeout=%lf", ac_attr_timeout, 0),
FUSE_LIB_OPT("ac_attr_timeout=", ac_attr_timeout_set, 1),
2005-12-10 01:41:42 +08:00
FUSE_LIB_OPT("negative_timeout=%lf", negative_timeout, 0),
FUSE_OPT_END
};
2005-07-06 21:34:02 +08:00
2006-01-07 18:14:34 +08:00
static void fuse_lib_help(void)
{
fprintf(stderr,
2006-02-17 00:38:34 +08:00
" -o hard_remove immediate removal (don't hide files)\n"
" -o use_ino let filesystem set inode numbers\n"
" -o readdir_ino try to fill in d_ino in readdir\n"
" -o direct_io use direct I/O\n"
" -o kernel_cache cache files in kernel\n"
" -o [no]auto_cache enable caching based on modification times\n"
" -o umask=M set file permissions (octal)\n"
" -o uid=N set file owner\n"
" -o gid=N set file group\n"
" -o entry_timeout=T cache timeout for names (1.0s)\n"
" -o negative_timeout=T cache timeout for deleted names (0.0s)\n"
2006-02-17 00:59:39 +08:00
" -o attr_timeout=T cache timeout for attributes (1.0s)\n"
" -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n"
"\n");
2006-01-07 18:14:34 +08:00
}
static int fuse_lib_opt_proc(void *data, const char *arg, int key,
struct fuse_args *outargs)
{
(void) data; (void) arg; (void) outargs;
if (key == KEY_HELP)
fuse_lib_help();
return 1;
}
2004-07-24 01:16:29 +08:00
int fuse_is_lib_option(const char *opt)
{
2005-12-10 01:41:42 +08:00
return fuse_lowlevel_is_lib_option(opt) ||
fuse_opt_match(fuse_lib_opts, opt);
2004-07-24 01:16:29 +08:00
}
2006-03-17 23:05:40 +08:00
struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args,
2004-12-04 08:40:50 +08:00
const struct fuse_operations *op,
2006-03-17 23:05:40 +08:00
size_t op_size, void *user_data, int compat)
2001-10-29 03:44:14 +08:00
{
2001-11-07 20:09:43 +08:00
struct fuse *f;
struct node *root;
2006-07-31 01:33:40 +08:00
struct fuse_lowlevel_ops llop = fuse_path_ops;
2001-10-29 03:44:14 +08:00
2005-01-04 20:45:54 +08:00
if (sizeof(struct fuse_operations) < op_size) {
2004-12-04 08:40:50 +08:00
fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
2005-01-04 20:45:54 +08:00
op_size = sizeof(struct fuse_operations);
2004-12-04 08:40:50 +08:00
}
2001-11-07 20:09:43 +08:00
f = (struct fuse *) calloc(1, sizeof(struct fuse));
2004-12-12 19:45:24 +08:00
if (f == NULL) {
fprintf(stderr, "fuse: failed to allocate fuse object\n");
2004-09-16 16:42:40 +08:00
goto out;
2004-12-12 19:45:24 +08:00
}
2001-11-06 23:07:17 +08:00
2006-03-17 23:05:40 +08:00
f->user_data = user_data;
2005-12-10 01:41:42 +08:00
f->conf.entry_timeout = 1.0;
f->conf.attr_timeout = 1.0;
f->conf.negative_timeout = 0.0;
2005-08-02 21:31:28 +08:00
2006-01-07 18:14:34 +08:00
if (fuse_opt_parse(args, &f->conf, fuse_lib_opts, fuse_lib_opt_proc) == -1)
2005-12-10 01:41:42 +08:00
goto out_free;
2006-02-17 00:59:39 +08:00
if (!f->conf.ac_attr_timeout_set)
f->conf.ac_attr_timeout = f->conf.attr_timeout;
2005-12-10 01:41:42 +08:00
#ifdef __FreeBSD__
/*
* In FreeBSD, we always use these settings as inode numbers are needed to
* make getcwd(3) work.
*/
2005-12-12 17:34:45 +08:00
f->conf.readdir_ino = 1;
2005-12-10 01:41:42 +08:00
#endif
2005-07-15 17:59:59 +08:00
2006-01-20 23:15:21 +08:00
if (compat && compat <= 25) {
if (fuse_sync_compat_args(args) == -1)
goto out_free;
}
2006-07-31 01:33:40 +08:00
memcpy(&f->op, op, op_size);
if (!f->op.lock) {
llop.getlk = NULL;
llop.setlk = NULL;
}
f->se = fuse_lowlevel_new_common(args, &llop,sizeof(llop), f);
2005-08-15 07:00:27 +08:00
if (f->se == NULL)
2004-09-16 16:42:40 +08:00
goto out_free;
2005-10-03 22:54:24 +08:00
2005-08-15 07:00:27 +08:00
fuse_session_add_chan(f->se, ch);
2004-02-21 00:38:45 +08:00
2001-11-07 20:09:43 +08:00
f->ctr = 0;
2004-02-20 00:55:40 +08:00
f->generation = 0;
2001-11-16 18:12:59 +08:00
/* FIXME: Dynamic hash table */
2001-11-07 20:09:43 +08:00
f->name_table_size = 14057;
f->name_table = (struct node **)
calloc(1, sizeof(struct node *) * f->name_table_size);
2004-12-12 19:45:24 +08:00
if (f->name_table == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
2005-08-15 07:00:27 +08:00
goto out_free_session;
2004-12-12 19:45:24 +08:00
}
2004-09-16 16:42:40 +08:00
f->id_table_size = 14057;
f->id_table = (struct node **)
calloc(1, sizeof(struct node *) * f->id_table_size);
2004-12-12 19:45:24 +08:00
if (f->id_table == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
2004-09-16 16:42:40 +08:00
goto out_free_name_table;
2004-12-12 19:45:24 +08:00
}
2004-09-16 16:42:40 +08:00
2005-04-07 23:40:21 +08:00
mutex_init(&f->lock);
2006-04-05 15:18:00 +08:00
pthread_rwlock_init(&f->tree_lock, NULL);
2004-12-04 08:40:50 +08:00
f->compat = compat;
2001-10-29 03:44:14 +08:00
2001-11-07 20:09:43 +08:00
root = (struct node *) calloc(1, sizeof(struct node));
2004-12-12 19:45:24 +08:00
if (root == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
goto out_free_id_table;
2004-12-12 19:45:24 +08:00
}
2004-09-16 16:42:40 +08:00
2001-11-07 20:09:43 +08:00
root->name = strdup("/");
2004-12-12 19:45:24 +08:00
if (root->name == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
2004-09-16 16:42:40 +08:00
goto out_free_root;
2004-12-12 19:45:24 +08:00
}
2004-09-16 16:42:40 +08:00
2001-11-07 20:09:43 +08:00
root->parent = 0;
root->nodeid = FUSE_ROOT_ID;
2004-02-20 00:55:40 +08:00
root->generation = 0;
2005-01-04 20:45:54 +08:00
root->refctr = 1;
2005-05-09 03:47:22 +08:00
root->nlookup = 1;
hash_id(f, root);
2001-11-07 20:09:43 +08:00
2001-10-29 03:44:14 +08:00
return f;
2004-09-16 16:42:40 +08:00
out_free_root:
free(root);
out_free_id_table:
free(f->id_table);
2004-09-16 16:42:40 +08:00
out_free_name_table:
free(f->name_table);
2005-08-15 07:00:27 +08:00
out_free_session:
fuse_session_destroy(f->se);
2004-09-16 16:42:40 +08:00
out_free:
free(f);
out:
return NULL;
2001-10-29 03:44:14 +08:00
}
2006-03-17 23:05:40 +08:00
struct fuse *fuse_new(struct fuse_chan *ch, struct fuse_args *args,
const struct fuse_operations *op, size_t op_size,
void *user_data)
2004-12-04 08:40:50 +08:00
{
2006-03-17 23:05:40 +08:00
return fuse_new_common(ch, args, op, op_size, user_data, 0);
2004-12-04 08:40:50 +08:00
}
2001-10-29 03:44:14 +08:00
void fuse_destroy(struct fuse *f)
{
2001-11-07 20:09:43 +08:00
size_t i;
2006-07-31 19:07:40 +08:00
struct fuse_context *c = fuse_get_context();
memset(c, 0, sizeof(*c));
c->fuse = f;
c->private_data = f->user_data;
for (i = 0; i < f->id_table_size; i++) {
2004-06-25 05:00:00 +08:00
struct node *node;
for (node = f->id_table[i]; node != NULL; node = node->id_next) {
2004-06-25 05:00:00 +08:00
if (node->is_hidden) {
char *path = get_path(f, node->nodeid);
2005-05-09 19:22:41 +08:00
if (path) {
2004-06-25 05:00:00 +08:00
f->op.unlink(path);
2005-05-09 19:22:41 +08:00
free(path);
}
2004-06-25 05:00:00 +08:00
}
}
}
for (i = 0; i < f->id_table_size; i++) {
2001-11-07 20:09:43 +08:00
struct node *node;
struct node *next;
2004-06-25 05:00:00 +08:00
for (node = f->id_table[i]; node != NULL; node = next) {
next = node->id_next;
2001-11-07 20:09:43 +08:00
free_node(node);
}
}
free(f->id_table);
2001-11-07 20:09:43 +08:00
free(f->name_table);
2001-11-06 20:03:23 +08:00
pthread_mutex_destroy(&f->lock);
2006-06-06 18:16:38 +08:00
pthread_rwlock_destroy(&f->tree_lock);
2005-08-15 07:00:27 +08:00
fuse_session_destroy(f->se);
2001-11-07 20:09:43 +08:00
free(f);
2001-10-29 03:44:14 +08:00
}
2004-12-04 08:40:50 +08:00
2005-11-18 01:11:48 +08:00
#include "fuse_compat.h"
2006-03-17 23:05:40 +08:00
static struct fuse *fuse_new_common_compat25(int fd, struct fuse_args *args,
const struct fuse_operations *op,
size_t op_size, int compat)
{
struct fuse *f = NULL;
struct fuse_chan *ch = fuse_kern_chan_new(fd);
if (ch)
f = fuse_new_common(ch, args, op, op_size, NULL, compat);
return f;
}
2006-01-20 23:15:21 +08:00
#ifndef __FreeBSD__
2005-11-18 01:11:48 +08:00
static int fuse_do_open(struct fuse *f, char *path, struct fuse_file_info *fi)
{
2006-01-20 23:15:21 +08:00
if (!f->compat || f->compat >= 25)
2005-11-18 01:11:48 +08:00
return f->op.open(path, fi);
else if (f->compat == 22) {
int err;
struct fuse_file_info_compat22 tmp;
memcpy(&tmp, fi, sizeof(tmp));
err = ((struct fuse_operations_compat22 *) &f->op)->open(path, &tmp);
memcpy(fi, &tmp, sizeof(tmp));
fi->fh = tmp.fh;
return err;
} else
2005-11-29 00:02:27 +08:00
return
2005-11-18 01:11:48 +08:00
((struct fuse_operations_compat2 *) &f->op)->open(path, fi->flags);
}
static void fuse_do_release(struct fuse *f, char *path,
struct fuse_file_info *fi)
{
if (!f->compat || f->compat >= 22)
f->op.release(path ? path : "-", fi);
else if (path)
((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags);
}
static int fuse_do_opendir(struct fuse *f, char *path,
struct fuse_file_info *fi)
{
2006-01-20 23:15:21 +08:00
if (!f->compat || f->compat >= 25) {
2005-11-18 01:11:48 +08:00
return f->op.opendir(path, fi);
} else {
int err;
struct fuse_file_info_compat22 tmp;
memcpy(&tmp, fi, sizeof(tmp));
err = ((struct fuse_operations_compat22 *) &f->op)->opendir(path, &tmp);
memcpy(fi, &tmp, sizeof(tmp));
fi->fh = tmp.fh;
return err;
}
}
static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
struct statvfs *stbuf)
{
stbuf->f_bsize = compatbuf->block_size;
stbuf->f_blocks = compatbuf->blocks;
stbuf->f_bfree = compatbuf->blocks_free;
stbuf->f_bavail = compatbuf->blocks_free;
stbuf->f_files = compatbuf->files;
stbuf->f_ffree = compatbuf->files_free;
stbuf->f_namemax = compatbuf->namelen;
}
static void convert_statfs_old(struct statfs *oldbuf, struct statvfs *stbuf)
{
stbuf->f_bsize = oldbuf->f_bsize;
stbuf->f_blocks = oldbuf->f_blocks;
stbuf->f_bfree = oldbuf->f_bfree;
stbuf->f_bavail = oldbuf->f_bavail;
stbuf->f_files = oldbuf->f_files;
stbuf->f_ffree = oldbuf->f_ffree;
stbuf->f_namemax = oldbuf->f_namelen;
}
2006-08-19 02:43:50 +08:00
static int fuse_do_statfs(struct fuse *f, struct statvfs *buf)
2005-11-18 01:11:48 +08:00
{
int err;
2006-01-20 23:15:21 +08:00
if (!f->compat || f->compat >= 25) {
2006-08-19 02:43:50 +08:00
err = f->op.statfs("/", buf);
2005-11-18 01:11:48 +08:00
} else if (f->compat > 11) {
struct statfs oldbuf;
err = ((struct fuse_operations_compat22 *) &f->op)->statfs("/", &oldbuf);
if (!err)
convert_statfs_old(&oldbuf, buf);
} else {
struct fuse_statfs_compat1 compatbuf;
memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
err = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
if (!err)
convert_statfs_compat(&compatbuf, buf);
}
return err;
}
2006-01-07 02:29:40 +08:00
static struct fuse *fuse_new_common_compat(int fd, const char *opts,
const struct fuse_operations *op,
size_t op_size, int compat)
{
struct fuse *f;
struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
if (opts &&
(fuse_opt_add_arg(&args, "") == -1 ||
fuse_opt_add_arg(&args, "-o") == -1 ||
fuse_opt_add_arg(&args, opts) == -1)) {
fuse_opt_free_args(&args);
return NULL;
}
2006-03-17 23:05:40 +08:00
f = fuse_new_common_compat25(fd, &args, op, op_size, compat);
2006-01-07 02:29:40 +08:00
fuse_opt_free_args(&args);
return f;
}
2005-11-18 01:11:48 +08:00
struct fuse *fuse_new_compat22(int fd, const char *opts,
const struct fuse_operations_compat22 *op,
size_t op_size)
{
2006-01-07 02:29:40 +08:00
return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op,
op_size, 22);
2005-11-18 01:11:48 +08:00
}
struct fuse *fuse_new_compat2(int fd, const char *opts,
const struct fuse_operations_compat2 *op)
{
2006-01-07 02:29:40 +08:00
return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op,
sizeof(struct fuse_operations_compat2), 21);
2005-11-18 01:11:48 +08:00
}
struct fuse *fuse_new_compat1(int fd, int flags,
const struct fuse_operations_compat1 *op)
{
const char *opts = NULL;
if (flags & FUSE_DEBUG_COMPAT1)
opts = "debug";
2006-01-07 02:29:40 +08:00
return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op,
sizeof(struct fuse_operations_compat1), 11);
2005-11-18 01:11:48 +08:00
}
2004-12-08 00:46:42 +08:00
__asm__(".symver fuse_exited,__fuse_exited@");
__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
__asm__(".symver fuse_new_compat2,fuse_new@");
2005-11-12 05:32:42 +08:00
__asm__(".symver fuse_new_compat22,fuse_new@FUSE_2.2");
2005-11-18 01:11:48 +08:00
#else /* __FreeBSD__ */
static int fuse_do_open(struct fuse *f, char *path, struct fuse_file_info *fi)
{
2005-11-29 00:02:27 +08:00
return f->op.open(path, fi);
2005-11-18 01:11:48 +08:00
}
static void fuse_do_release(struct fuse *f, char *path,
struct fuse_file_info *fi)
{
f->op.release(path ? path : "-", fi);
}
static int fuse_do_opendir(struct fuse *f, char *path,
struct fuse_file_info *fi)
{
2005-11-29 00:02:27 +08:00
return f->op.opendir(path, fi);
2005-11-18 01:11:48 +08:00
}
2006-08-19 02:43:50 +08:00
static int fuse_do_statfs(struct fuse *f, struct statvfs *buf)
2005-11-18 01:11:48 +08:00
{
2006-08-19 02:43:50 +08:00
return f->op.statfs("/", buf);
2005-11-18 01:11:48 +08:00
}
#endif /* __FreeBSD__ */
2006-01-20 23:15:21 +08:00
struct fuse *fuse_new_compat25(int fd, struct fuse_args *args,
const struct fuse_operations_compat25 *op,
size_t op_size)
{
2006-03-17 23:05:40 +08:00
return fuse_new_common_compat25(fd, args, (struct fuse_operations *) op,
op_size, 25);
2006-01-20 23:15:21 +08:00
}
__asm__(".symver fuse_new_compat25,fuse_new@FUSE_2.5");