2001-10-29 03:44:14 +08:00
|
|
|
/*
|
|
|
|
FUSE: Filesystem in Userspace
|
2004-07-08 03:19:53 +08:00
|
|
|
Copyright (C) 2001-2004 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
|
|
|
*/
|
|
|
|
|
2004-06-24 02:52:50 +08:00
|
|
|
#include <config.h>
|
2001-10-29 03:44:14 +08:00
|
|
|
#include "fuse_i.h"
|
|
|
|
#include <linux/fuse.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2001-11-07 20:09:43 +08:00
|
|
|
#include <stdlib.h>
|
2001-10-29 03:44:14 +08:00
|
|
|
#include <unistd.h>
|
2001-11-07 20:09:43 +08:00
|
|
|
#include <limits.h>
|
2001-10-29 03:44:14 +08:00
|
|
|
#include <errno.h>
|
2001-12-27 02:08:09 +08:00
|
|
|
#include <sys/param.h>
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
#define FUSE_MAX_PATH 4096
|
2002-10-28 16:49:39 +08:00
|
|
|
#define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg))
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2004-03-02 19:11:24 +08:00
|
|
|
#define ENTRY_REVALIDATE_TIME 1 /* sec */
|
|
|
|
#define ATTR_REVALIDATE_TIME 1 /* sec */
|
|
|
|
|
2002-12-10 20:26:00 +08:00
|
|
|
static const char *opname(enum fuse_opcode opcode)
|
|
|
|
{
|
2004-06-20 06:42:38 +08:00
|
|
|
switch (opcode) {
|
2004-03-30 23:17:26 +08:00
|
|
|
case FUSE_LOOKUP: return "LOOKUP";
|
|
|
|
case FUSE_FORGET: return "FORGET";
|
|
|
|
case FUSE_GETATTR: return "GETATTR";
|
|
|
|
case FUSE_SETATTR: return "SETATTR";
|
|
|
|
case FUSE_READLINK: return "READLINK";
|
|
|
|
case FUSE_SYMLINK: return "SYMLINK";
|
|
|
|
case FUSE_GETDIR: return "GETDIR";
|
|
|
|
case FUSE_MKNOD: return "MKNOD";
|
|
|
|
case FUSE_MKDIR: return "MKDIR";
|
|
|
|
case FUSE_UNLINK: return "UNLINK";
|
|
|
|
case FUSE_RMDIR: return "RMDIR";
|
|
|
|
case FUSE_RENAME: return "RENAME";
|
|
|
|
case FUSE_LINK: return "LINK";
|
|
|
|
case FUSE_OPEN: return "OPEN";
|
|
|
|
case FUSE_READ: return "READ";
|
|
|
|
case FUSE_WRITE: return "WRITE";
|
|
|
|
case FUSE_STATFS: return "STATFS";
|
2004-05-19 16:01:10 +08:00
|
|
|
case FUSE_FLUSH: return "FLUSH";
|
2004-03-30 23:17:26 +08:00
|
|
|
case FUSE_RELEASE: return "RELEASE";
|
|
|
|
case FUSE_FSYNC: return "FSYNC";
|
|
|
|
case FUSE_SETXATTR: return "SETXATTR";
|
|
|
|
case FUSE_GETXATTR: return "GETXATTR";
|
|
|
|
case FUSE_LISTXATTR: return "LISTXATTR";
|
|
|
|
case FUSE_REMOVEXATTR: return "REMOVEXATTR";
|
2004-05-19 16:01:10 +08:00
|
|
|
default: return "???";
|
2002-12-10 20:26:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-26 01:19:59 +08:00
|
|
|
|
|
|
|
static inline void dec_avail(struct fuse *f)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
f->numavail --;
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
}
|
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
static struct node *__get_node(struct fuse *f, fino_t ino)
|
2001-11-06 20:03:23 +08:00
|
|
|
{
|
2001-11-07 20:09:43 +08:00
|
|
|
size_t hash = ino % f->ino_table_size;
|
|
|
|
struct node *node;
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
for (node = f->ino_table[hash]; node != NULL; node = node->ino_next)
|
|
|
|
if (node->ino == ino)
|
2001-11-07 20:09:43 +08:00
|
|
|
return node;
|
|
|
|
|
|
|
|
return NULL;
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
static struct node *get_node(struct fuse *f, fino_t ino)
|
2001-11-06 20:03:23 +08:00
|
|
|
{
|
2001-11-07 20:09:43 +08:00
|
|
|
struct node *node = __get_node(f, ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (node != NULL)
|
2001-11-07 20:09:43 +08:00
|
|
|
return node;
|
|
|
|
|
|
|
|
fprintf(stderr, "fuse internal error: inode %lu not found\n", ino);
|
|
|
|
abort();
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
|
|
|
|
2004-02-20 00:55:40 +08:00
|
|
|
static void hash_ino(struct fuse *f, struct node *node)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
2004-02-20 00:55:40 +08:00
|
|
|
size_t hash = node->ino % f->ino_table_size;
|
2001-11-07 20:09:43 +08:00
|
|
|
node->ino_next = f->ino_table[hash];
|
|
|
|
f->ino_table[hash] = node;
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
static void unhash_ino(struct fuse *f, struct node *node)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
2001-11-07 20:09:43 +08:00
|
|
|
size_t hash = node->ino % f->ino_table_size;
|
|
|
|
struct node **nodep = &f->ino_table[hash];
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
for (; *nodep != NULL; nodep = &(*nodep)->ino_next)
|
|
|
|
if (*nodep == node) {
|
2001-11-07 20:09:43 +08:00
|
|
|
*nodep = node->ino_next;
|
|
|
|
return;
|
|
|
|
}
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
static fino_t next_ino(struct fuse *f)
|
|
|
|
{
|
2004-02-20 00:55:40 +08:00
|
|
|
do {
|
|
|
|
f->ctr++;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!f->ctr)
|
2004-02-20 00:55:40 +08:00
|
|
|
f->generation ++;
|
2004-06-20 06:42:38 +08:00
|
|
|
} while (f->ctr == 0 || __get_node(f, f->ctr) != NULL);
|
2001-11-07 20:09:43 +08:00
|
|
|
return f->ctr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_node(struct node *node)
|
|
|
|
{
|
|
|
|
free(node->name);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int name_hash(struct fuse *f, fino_t parent, const char *name)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-06-25 05:00:00 +08:00
|
|
|
static struct node *__lookup_node(struct fuse *f, fino_t parent,
|
2001-10-30 23:06:52 +08:00
|
|
|
const char *name)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
2001-11-07 20:09:43 +08:00
|
|
|
size_t hash = name_hash(f, parent, name);
|
|
|
|
struct node *node;
|
2001-10-30 23:06:52 +08:00
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
for (node = f->name_table[hash]; node != NULL; node = node->name_next)
|
|
|
|
if (node->parent == parent && strcmp(node->name, name) == 0)
|
2001-11-07 20:09:43 +08:00
|
|
|
return node;
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
return NULL;
|
2001-10-30 23:06:52 +08:00
|
|
|
}
|
|
|
|
|
2004-06-25 05:00:00 +08:00
|
|
|
static struct node *lookup_node(struct fuse *f, fino_t parent,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
node = __lookup_node(f, parent, name);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
if (node != NULL)
|
|
|
|
return node;
|
|
|
|
|
|
|
|
fprintf(stderr, "fuse internal error: node %lu/%s not found\n", parent,
|
|
|
|
name);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
static void hash_name(struct fuse *f, struct node *node, fino_t parent,
|
2001-11-06 20:03:23 +08:00
|
|
|
const char *name)
|
|
|
|
{
|
2001-11-07 20:09:43 +08:00
|
|
|
size_t hash = name_hash(f, parent, name);
|
2001-11-06 20:03:23 +08:00
|
|
|
node->parent = parent;
|
2001-11-07 20:09:43 +08:00
|
|
|
node->name = strdup(name);
|
|
|
|
node->name_next = f->name_table[hash];
|
|
|
|
f->name_table[hash] = 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
|
|
|
{
|
2004-06-20 06:42:38 +08:00
|
|
|
if (node->name != NULL) {
|
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];
|
|
|
|
|
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;
|
|
|
|
free(node->name);
|
|
|
|
node->name = NULL;
|
|
|
|
node->parent = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
|
|
|
|
node->ino);
|
|
|
|
abort();
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
|
|
|
|
2004-02-20 00:55:40 +08:00
|
|
|
static struct node *find_node(struct fuse *f, fino_t parent, char *name,
|
|
|
|
struct fuse_attr *attr, int version)
|
2001-10-30 23:06:52 +08:00
|
|
|
{
|
|
|
|
struct node *node;
|
2001-11-06 20:03:23 +08:00
|
|
|
int mode = attr->mode & S_IFMT;
|
|
|
|
int rdev = 0;
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (S_ISCHR(mode) || S_ISBLK(mode))
|
2001-11-06 20:03:23 +08:00
|
|
|
rdev = attr->rdev;
|
2001-10-30 23:06:52 +08:00
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2004-06-25 05:00:00 +08:00
|
|
|
node = __lookup_node(f, parent, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (node != NULL) {
|
|
|
|
if (node->mode == mode && node->rdev == rdev)
|
2001-11-06 20:03:23 +08:00
|
|
|
goto out;
|
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
unhash_name(f, node);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
node = (struct node *) calloc(1, sizeof(struct node));
|
2001-11-06 20:03:23 +08:00
|
|
|
node->mode = mode;
|
2001-11-07 20:09:43 +08:00
|
|
|
node->rdev = rdev;
|
2004-06-25 05:00:00 +08:00
|
|
|
node->open_count = 0;
|
|
|
|
node->is_hidden = 0;
|
2004-02-20 00:55:40 +08:00
|
|
|
node->ino = next_ino(f);
|
|
|
|
node->generation = f->generation;
|
|
|
|
hash_ino(f, node);
|
2001-11-07 20:09:43 +08:00
|
|
|
hash_name(f, node, parent, name);
|
2001-11-06 20:03:23 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
node->version = version;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
static char *get_path_name(struct fuse *f, fino_t ino, 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;
|
2001-10-31 22:52:35 +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);
|
2004-06-20 06:42:38 +08:00
|
|
|
for (node = get_node(f, ino); node->ino != FUSE_ROOT_INO;
|
2001-11-07 20:09:43 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (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
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
static char *get_path(struct fuse *f, fino_t ino)
|
|
|
|
{
|
|
|
|
return get_path_name(f, ino, NULL);
|
2001-10-29 22:57:57 +08:00
|
|
|
}
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
static void destroy_node(struct fuse *f, fino_t ino, int version)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
2001-11-06 20:03:23 +08:00
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2001-11-07 20:09:43 +08:00
|
|
|
node = get_node(f, ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (node->version == version && ino != FUSE_ROOT_INO) {
|
2001-11-07 20:09:43 +08:00
|
|
|
unhash_name(f, node);
|
|
|
|
unhash_ino(f, node);
|
2001-11-06 20:03:23 +08:00
|
|
|
free_node(node);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
static void remove_node(struct fuse *f, fino_t dir, const char *name)
|
|
|
|
{
|
2001-11-06 20:03:23 +08:00
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2004-06-25 05:00:00 +08:00
|
|
|
node = __lookup_node(f, dir, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (node == NULL) {
|
2001-11-07 20:09:43 +08:00
|
|
|
fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n",
|
|
|
|
dir, name);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-06-25 05:00:00 +08:00
|
|
|
static int rename_node(struct fuse *f, fino_t olddir, const char *oldname,
|
|
|
|
fino_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;
|
2001-11-06 20:03:23 +08:00
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2004-06-25 05:00:00 +08:00
|
|
|
node = __lookup_node(f, olddir, oldname);
|
|
|
|
newnode = __lookup_node(f, newdir, newname);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (node == NULL) {
|
2001-11-07 20:09:43 +08:00
|
|
|
fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n",
|
|
|
|
olddir, oldname);
|
|
|
|
abort();
|
|
|
|
}
|
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");
|
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-11-07 20:09:43 +08:00
|
|
|
unhash_name(f, newnode);
|
2004-06-25 05:00:00 +08:00
|
|
|
}
|
2001-10-30 23:06:52 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
unhash_name(f, node);
|
|
|
|
hash_name(f, node, newdir, newname);
|
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
|
|
|
}
|
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
|
|
|
|
{
|
2004-02-20 22:10:49 +08:00
|
|
|
attr->mode = stbuf->st_mode;
|
|
|
|
attr->nlink = stbuf->st_nlink;
|
|
|
|
attr->uid = stbuf->st_uid;
|
|
|
|
attr->gid = stbuf->st_gid;
|
|
|
|
attr->rdev = stbuf->st_rdev;
|
|
|
|
attr->size = stbuf->st_size;
|
|
|
|
attr->blocks = stbuf->st_blocks;
|
|
|
|
attr->atime = stbuf->st_atime;
|
|
|
|
attr->mtime = stbuf->st_mtime;
|
|
|
|
attr->ctime = stbuf->st_ctime;
|
2004-06-24 02:52:50 +08:00
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_ATIM
|
|
|
|
attr->atimensec = stbuf->st_atim.tv_nsec;
|
|
|
|
attr->mtimensec = stbuf->st_mtim.tv_nsec;
|
2004-02-20 22:10:49 +08:00
|
|
|
attr->ctimensec = stbuf->st_ctim.tv_nsec;
|
2004-06-24 02:52:50 +08:00
|
|
|
#endif
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
static int fill_dir(struct fuse_dirhandle *dh, char *name, int type)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
|
|
|
struct fuse_dirent dirent;
|
|
|
|
size_t reclen;
|
|
|
|
size_t res;
|
|
|
|
|
2001-11-19 03:15:05 +08:00
|
|
|
dirent.ino = (unsigned long) -1;
|
2001-10-29 03:44:14 +08:00
|
|
|
dirent.namelen = strlen(name);
|
|
|
|
strncpy(dirent.name, name, sizeof(dirent.name));
|
|
|
|
dirent.type = type;
|
|
|
|
reclen = FUSE_DIRENT_SIZE(&dirent);
|
|
|
|
res = fwrite(&dirent, reclen, 1, dh->fp);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0) {
|
2001-11-21 20:21:19 +08:00
|
|
|
perror("fuse: writing directory file");
|
2001-10-29 03:44:14 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-07-12 23:55:11 +08:00
|
|
|
static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize,
|
|
|
|
int locked)
|
2001-11-19 03:15:05 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if ((f->flags & FUSE_DEBUG)) {
|
2001-11-19 03:15:05 +08:00
|
|
|
struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
|
|
|
|
printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
|
|
|
|
out->error, strerror(-out->error), outsize);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-11-26 01:19:59 +08:00
|
|
|
|
2004-02-09 20:05:14 +08:00
|
|
|
/* This needs to be done before the reply, otherwise the scheduler
|
|
|
|
could play tricks with us, and only let the counter be increased
|
2001-11-26 01:19:59 +08:00
|
|
|
long after the operation is done */
|
2004-07-12 23:55:11 +08:00
|
|
|
if (!locked)
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
f->numavail ++;
|
|
|
|
if (!locked)
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
2001-11-26 01:19:59 +08:00
|
|
|
|
2001-11-19 03:15:05 +08:00
|
|
|
res = write(f->fd, outbuf, outsize);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == -1) {
|
2001-11-19 03:15:05 +08:00
|
|
|
/* ENOENT means the operation was interrupted */
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!f->exited && errno != ENOENT)
|
2001-11-21 20:21:19 +08:00
|
|
|
perror("fuse: writing device");
|
2002-12-11 00:09:02 +08:00
|
|
|
return -errno;
|
2001-11-19 03:15:05 +08:00
|
|
|
}
|
2002-12-11 00:09:02 +08:00
|
|
|
return 0;
|
2001-11-19 03:15:05 +08:00
|
|
|
}
|
|
|
|
|
2004-07-12 23:55:11 +08:00
|
|
|
static int __send_reply(struct fuse *f, struct fuse_in_header *in, int error,
|
|
|
|
void *arg, size_t argsize, int locked)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
2002-12-11 00:09:02 +08:00
|
|
|
int res;
|
2001-10-29 03:44:14 +08:00
|
|
|
char *outbuf;
|
|
|
|
size_t outsize;
|
|
|
|
struct fuse_out_header *out;
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (error <= -1000 || error > 0) {
|
2002-12-05 22:23:01 +08:00
|
|
|
fprintf(stderr, "fuse: bad error value: %i\n", error);
|
2001-10-30 23:06:52 +08:00
|
|
|
error = -ERANGE;
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (error)
|
2001-10-29 03:44:14 +08:00
|
|
|
argsize = 0;
|
|
|
|
|
|
|
|
outsize = sizeof(struct fuse_out_header) + argsize;
|
2001-11-07 20:09:43 +08:00
|
|
|
outbuf = (char *) malloc(outsize);
|
2001-10-29 03:44:14 +08:00
|
|
|
out = (struct fuse_out_header *) outbuf;
|
2002-12-05 22:23:01 +08:00
|
|
|
memset(out, 0, sizeof(struct fuse_out_header));
|
2001-10-29 03:44:14 +08:00
|
|
|
out->unique = in->unique;
|
2001-10-30 23:06:52 +08:00
|
|
|
out->error = error;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (argsize != 0)
|
2001-10-29 03:44:14 +08:00
|
|
|
memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
|
|
|
|
|
2004-07-12 23:55:11 +08:00
|
|
|
res = send_reply_raw(f, outbuf, outsize, locked);
|
2001-11-07 20:09:43 +08:00
|
|
|
free(outbuf);
|
2002-12-11 00:09:02 +08:00
|
|
|
|
|
|
|
return res;
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2004-07-12 23:55:11 +08:00
|
|
|
static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
|
|
|
|
void *arg, size_t argsize)
|
|
|
|
{
|
|
|
|
return __send_reply(f, in, error, arg, argsize, 0);
|
|
|
|
}
|
|
|
|
|
2004-06-25 05:00:00 +08:00
|
|
|
static int is_open(struct fuse *f, fino_t dir, const char *name)
|
|
|
|
{
|
|
|
|
struct node *node;
|
|
|
|
int isopen = 0;
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
node = __lookup_node(f, dir, name);
|
|
|
|
if (node && node->open_count > 0)
|
|
|
|
isopen = 1;
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
return isopen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *hidden_name(struct fuse *f, fino_t dir, const char *oldname,
|
|
|
|
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 {
|
|
|
|
node = lookup_node(f, dir, oldname);
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
do {
|
|
|
|
f->hidectr ++;
|
|
|
|
snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
|
|
|
|
(unsigned int) node->ino, f->hidectr);
|
|
|
|
newnode = __lookup_node(f, dir, newname);
|
|
|
|
} while(newnode);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
|
|
|
|
newpath = get_path_name(f, dir, newname);
|
|
|
|
if (!newpath)
|
|
|
|
break;
|
|
|
|
|
|
|
|
res = f->op.getattr(newpath, &buf);
|
|
|
|
if (res != 0)
|
|
|
|
break;
|
|
|
|
free(newpath);
|
|
|
|
newpath = NULL;
|
|
|
|
} while(--failctr);
|
|
|
|
|
|
|
|
return newpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hide_node(struct fuse *f, const char *oldpath, fino_t dir,
|
|
|
|
const char *oldname)
|
|
|
|
{
|
|
|
|
char newname[64];
|
|
|
|
char *newpath;
|
|
|
|
int err = -1;
|
|
|
|
|
|
|
|
if (!f->op.rename || !f->op.unlink)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
|
|
|
|
if (newpath) {
|
|
|
|
err = f->op.rename(oldpath, newpath);
|
|
|
|
if (!err)
|
|
|
|
err = rename_node(f, dir, oldname, dir, newname, 1);
|
|
|
|
free(newpath);
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-20 00:55:40 +08:00
|
|
|
static int lookup_path(struct fuse *f, fino_t ino, int version, char *name,
|
2004-03-02 19:11:24 +08:00
|
|
|
const char *path, struct fuse_entry_out *arg)
|
2004-02-20 00:55:40 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
res = f->op.getattr(path, &buf);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0) {
|
2004-02-20 00:55:40 +08:00
|
|
|
struct node *node;
|
|
|
|
|
2004-03-02 19:11:24 +08:00
|
|
|
memset(arg, 0, sizeof(struct fuse_entry_out));
|
2004-02-20 00:55:40 +08:00
|
|
|
convert_stat(&buf, &arg->attr);
|
|
|
|
node = find_node(f, ino, name, &arg->attr, version);
|
|
|
|
arg->ino = node->ino;
|
|
|
|
arg->generation = node->generation;
|
2004-03-02 19:11:24 +08:00
|
|
|
arg->entry_valid = ENTRY_REVALIDATE_TIME;
|
|
|
|
arg->entry_valid_nsec = 0;
|
|
|
|
arg->attr_valid = ATTR_REVALIDATE_TIME;
|
|
|
|
arg->attr_valid_nsec = 0;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2004-02-20 00:55:40 +08:00
|
|
|
printf(" INO: %li\n", arg->ino);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
|
|
|
|
{
|
|
|
|
int res;
|
2004-06-21 17:45:30 +08:00
|
|
|
int res2;
|
2001-10-30 23:06:52 +08:00
|
|
|
char *path;
|
2004-03-02 19:11:24 +08:00
|
|
|
struct fuse_entry_out arg;
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path_name(f, in->ino, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2002-01-07 05:44:16 +08:00
|
|
|
printf("LOOKUP %s\n", path);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.getattr)
|
2004-02-20 00:55:40 +08:00
|
|
|
res = lookup_path(f, in->ino, in->unique, name, path, &arg);
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2004-06-21 17:45:30 +08:00
|
|
|
res2 = send_reply(f, in, res, &arg, sizeof(arg));
|
|
|
|
if (res == 0 && res2 == -ENOENT)
|
|
|
|
destroy_node(f, arg.ino, in->unique);
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
static void do_forget(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_forget_in *arg)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2001-11-19 03:15:05 +08:00
|
|
|
printf("FORGET %li/%i\n", in->ino, arg->version);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-11-06 20:03:23 +08:00
|
|
|
destroy_node(f, in->ino, arg->version);
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_getattr(struct fuse *f, struct fuse_in_header *in)
|
|
|
|
{
|
|
|
|
int res;
|
2001-10-30 23:06:52 +08:00
|
|
|
char *path;
|
|
|
|
struct stat buf;
|
2004-03-02 19:11:24 +08:00
|
|
|
struct fuse_attr_out arg;
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.getattr)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.getattr(path, &buf);
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2002-12-05 22:23:01 +08:00
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0) {
|
2004-03-02 19:11:24 +08:00
|
|
|
memset(&arg, 0, sizeof(struct fuse_attr_out));
|
|
|
|
arg.attr_valid = ATTR_REVALIDATE_TIME;
|
|
|
|
arg.attr_valid_nsec = 0;
|
2001-10-30 23:06:52 +08:00
|
|
|
convert_stat(&buf, &arg.attr);
|
2002-12-05 22:23:01 +08:00
|
|
|
}
|
2001-10-30 23:06:52 +08:00
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
send_reply(f, in, res, &arg, sizeof(arg));
|
|
|
|
}
|
|
|
|
|
2001-11-16 21:31:14 +08:00
|
|
|
static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
|
2001-11-07 22:55:16 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.chmod)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.chmod(path, attr->mode);
|
2001-11-07 22:55:16 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-11-16 21:31:14 +08:00
|
|
|
static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
|
2001-12-20 20:17:25 +08:00
|
|
|
int valid)
|
2001-11-07 22:55:16 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
|
|
|
|
gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
|
|
|
|
|
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.chown)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.chown(path, uid, gid);
|
2001-11-07 22:55:16 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-11-16 21:31:14 +08:00
|
|
|
static int do_truncate(struct fuse *f, const char *path,
|
|
|
|
struct fuse_attr *attr)
|
2001-11-07 22:55:16 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.truncate)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.truncate(path, attr->size);
|
2001-11-07 22:55:16 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-11-16 21:31:14 +08:00
|
|
|
static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
|
2001-11-07 22:55:16 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct utimbuf buf;
|
|
|
|
buf.actime = attr->atime;
|
|
|
|
buf.modtime = attr->mtime;
|
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.utime)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.utime(path, &buf);
|
2001-11-07 22:55:16 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
static void do_setattr(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_setattr_in *arg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
int valid = arg->valid;
|
|
|
|
struct fuse_attr *attr = &arg->attr;
|
2004-03-02 19:11:24 +08:00
|
|
|
struct fuse_attr_out outarg;
|
2001-10-31 22:52:35 +08:00
|
|
|
|
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2001-11-07 22:55:16 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.getattr) {
|
2001-11-07 22:55:16 +08:00
|
|
|
res = 0;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!res && (valid & FATTR_MODE))
|
2001-11-12 02:20:17 +08:00
|
|
|
res = do_chmod(f, path, attr);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!res && (valid & (FATTR_UID | FATTR_GID)))
|
2001-11-12 02:20:17 +08:00
|
|
|
res = do_chown(f, path, attr, valid);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!res && (valid & FATTR_SIZE))
|
2001-11-12 02:20:17 +08:00
|
|
|
res = do_truncate(f, path, attr);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
|
2004-02-20 22:10:49 +08:00
|
|
|
(FATTR_ATIME | FATTR_MTIME))
|
2001-11-12 02:20:17 +08:00
|
|
|
res = do_utime(f, path, attr);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!res) {
|
2001-11-07 22:55:16 +08:00
|
|
|
struct stat buf;
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.getattr(path, &buf);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (!res) {
|
2004-03-02 19:11:24 +08:00
|
|
|
memset(&outarg, 0, sizeof(struct fuse_attr_out));
|
|
|
|
outarg.attr_valid = ATTR_REVALIDATE_TIME;
|
|
|
|
outarg.attr_valid_nsec = 0;
|
2001-11-07 22:55:16 +08:00
|
|
|
convert_stat(&buf, &outarg.attr);
|
2002-12-05 22:23:01 +08:00
|
|
|
}
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
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
|
|
|
}
|
2001-11-06 20:03:23 +08:00
|
|
|
send_reply(f, in, res, &outarg, sizeof(outarg));
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
static void do_readlink(struct fuse *f, struct fuse_in_header *in)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char link[PATH_MAX + 1];
|
2001-10-29 22:57:57 +08:00
|
|
|
char *path;
|
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.readlink)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.readlink(path, link, sizeof(link));
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2001-11-06 23:07:17 +08:00
|
|
|
link[PATH_MAX] = '\0';
|
2002-01-09 20:23:27 +08:00
|
|
|
send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-10-29 22:57:57 +08:00
|
|
|
static void do_getdir(struct fuse *f, struct fuse_in_header *in)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct fuse_getdir_out arg;
|
2001-11-06 20:03:23 +08:00
|
|
|
struct fuse_dirhandle dh;
|
2001-10-29 22:57:57 +08:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
dh.fuse = f;
|
|
|
|
dh.fp = tmpfile();
|
|
|
|
dh.dir = in->ino;
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.getdir)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir);
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2001-10-29 22:57:57 +08:00
|
|
|
fflush(dh.fp);
|
2002-12-05 22:23:01 +08:00
|
|
|
|
|
|
|
memset(&arg, 0, sizeof(struct fuse_getdir_out));
|
2001-10-29 22:57:57 +08:00
|
|
|
arg.fd = fileno(dh.fp);
|
|
|
|
send_reply(f, in, res, &arg, sizeof(arg));
|
|
|
|
fclose(dh.fp);
|
|
|
|
}
|
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
static void do_mknod(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_mknod_in *inarg)
|
|
|
|
{
|
|
|
|
int res;
|
2004-06-21 17:45:30 +08:00
|
|
|
int res2;
|
2001-10-29 22:57:57 +08:00
|
|
|
char *path;
|
2004-02-20 00:55:40 +08:00
|
|
|
char *name = PARAM(inarg);
|
2004-03-02 19:11:24 +08:00
|
|
|
struct fuse_entry_out outarg;
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2004-02-20 00:55:40 +08:00
|
|
|
path = get_path_name(f, in->ino, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2004-02-20 00:55:40 +08:00
|
|
|
printf("MKNOD %s\n", path);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.mknod && f->op.getattr) {
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.mknod(path, inarg->mode, inarg->rdev);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0)
|
2004-02-20 00:55:40 +08:00
|
|
|
res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
|
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
|
|
|
}
|
2004-06-21 17:45:30 +08:00
|
|
|
res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
|
|
|
|
if (res == 0 && res2 == -ENOENT)
|
|
|
|
destroy_node(f, outarg.ino, in->unique);
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-10-29 22:57:57 +08:00
|
|
|
static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_mkdir_in *inarg)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
|
|
|
int res;
|
2004-06-21 17:45:30 +08:00
|
|
|
int res2;
|
2001-10-29 22:57:57 +08:00
|
|
|
char *path;
|
2004-02-20 00:55:40 +08:00
|
|
|
char *name = PARAM(inarg);
|
2004-03-02 19:11:24 +08:00
|
|
|
struct fuse_entry_out outarg;
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2004-02-20 00:55:40 +08:00
|
|
|
path = get_path_name(f, in->ino, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2004-02-20 00:55:40 +08:00
|
|
|
printf("MKDIR %s\n", path);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.mkdir && f->op.getattr) {
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.mkdir(path, inarg->mode);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0)
|
2004-02-20 00:55:40 +08:00
|
|
|
res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
|
|
|
|
}
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2004-06-21 17:45:30 +08:00
|
|
|
res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
|
|
|
|
if (res == 0 && res2 == -ENOENT)
|
|
|
|
destroy_node(f, outarg.ino, in->unique);
|
2001-10-29 22:57:57 +08:00
|
|
|
}
|
|
|
|
|
2004-02-20 22:10:49 +08:00
|
|
|
static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
|
2001-10-29 22:57:57 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path_name(f, in->ino, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.unlink) {
|
2004-07-13 23:36:52 +08:00
|
|
|
if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->ino, name))
|
2004-06-25 05:00:00 +08:00
|
|
|
res = hide_node(f, path, in->ino, name);
|
|
|
|
else {
|
|
|
|
res = f->op.unlink(path);
|
|
|
|
if (res == 0)
|
|
|
|
remove_node(f, in->ino, name);
|
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2004-02-20 22:10:49 +08:00
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
|
|
|
path = get_path_name(f, in->ino, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2004-02-20 22:10:49 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.rmdir) {
|
2004-02-20 22:10:49 +08:00
|
|
|
res = f->op.rmdir(path);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0)
|
2004-02-20 22:10:49 +08:00
|
|
|
remove_node(f, in->ino, 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
|
|
|
}
|
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
|
|
|
|
char *link)
|
|
|
|
{
|
|
|
|
int res;
|
2004-06-21 17:45:30 +08:00
|
|
|
int res2;
|
2001-10-29 22:57:57 +08:00
|
|
|
char *path;
|
2004-03-02 19:11:24 +08:00
|
|
|
struct fuse_entry_out outarg;
|
2001-10-29 22:57:57 +08:00
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path_name(f, in->ino, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2004-02-20 00:55:40 +08:00
|
|
|
printf("SYMLINK %s\n", path);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.symlink && f->op.getattr) {
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.symlink(link, path);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0)
|
2004-02-20 00:55:40 +08:00
|
|
|
res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
|
|
|
|
}
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2004-06-21 17:45:30 +08:00
|
|
|
res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
|
|
|
|
if (res == 0 && res2 == -ENOENT)
|
|
|
|
destroy_node(f, outarg.ino, in->unique);
|
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
|
2001-10-30 23:06:52 +08:00
|
|
|
static void do_rename(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_rename_in *inarg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
fino_t olddir = in->ino;
|
|
|
|
fino_t newdir = inarg->newdir;
|
2002-10-28 16:49:39 +08:00
|
|
|
char *oldname = PARAM(inarg);
|
|
|
|
char *newname = oldname + strlen(oldname) + 1;
|
2001-10-31 22:52:35 +08:00
|
|
|
char *oldpath;
|
|
|
|
char *newpath;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
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) {
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-25 05:00:00 +08:00
|
|
|
if (f->op.rename) {
|
|
|
|
res = 0;
|
2004-07-13 23:36:52 +08:00
|
|
|
if (!(f->flags & FUSE_HARD_REMOVE) &&
|
|
|
|
is_open(f, newdir, newname))
|
2004-06-25 05:00:00 +08:00
|
|
|
res = hide_node(f, newpath, newdir, newname);
|
|
|
|
if (res == 0) {
|
|
|
|
res = f->op.rename(oldpath, newpath);
|
|
|
|
if (res == 0)
|
|
|
|
rename_node(f, olddir, oldname, newdir, newname, 0);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2001-10-30 23:06:52 +08:00
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_link(struct fuse *f, struct fuse_in_header *in,
|
2001-10-31 22:52:35 +08:00
|
|
|
struct fuse_link_in *arg)
|
2001-10-30 23:06:52 +08:00
|
|
|
{
|
|
|
|
int res;
|
2004-06-21 17:45:30 +08:00
|
|
|
int res2;
|
2001-10-31 22:52:35 +08:00
|
|
|
char *oldpath;
|
|
|
|
char *newpath;
|
2004-02-20 00:55:40 +08:00
|
|
|
char *name = PARAM(arg);
|
2004-03-02 19:11:24 +08:00
|
|
|
struct fuse_entry_out outarg;
|
2001-10-31 22:52:35 +08:00
|
|
|
|
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
oldpath = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (oldpath != NULL) {
|
2004-02-20 00:55:40 +08:00
|
|
|
newpath = get_path_name(f, arg->newdir, name);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (newpath != NULL) {
|
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2004-02-20 00:55:40 +08:00
|
|
|
printf("LINK %s\n", newpath);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.link && f->op.getattr) {
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.link(oldpath, newpath);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0)
|
2004-02-20 00:55:40 +08:00
|
|
|
res = lookup_path(f, arg->newdir, in->unique, name,
|
|
|
|
newpath, &outarg);
|
|
|
|
}
|
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
|
|
|
}
|
2004-06-21 17:45:30 +08:00
|
|
|
res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
|
|
|
|
if (res == 0 && res2 == -ENOENT)
|
|
|
|
destroy_node(f, outarg.ino, in->unique);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2001-10-30 23:06:52 +08:00
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
static void do_open(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_open_in *arg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
2001-10-30 23:06:52 +08:00
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.open)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.open(path, arg->flags);
|
2002-12-11 00:09:02 +08:00
|
|
|
}
|
2004-07-12 23:55:11 +08:00
|
|
|
if (res == 0) {
|
|
|
|
int res2;
|
|
|
|
|
|
|
|
/* If the request is interrupted the lock must be held until
|
|
|
|
the cancellation is finished. Otherwise there could be
|
|
|
|
races with rename/unlink, against which the kernel can't
|
|
|
|
protect */
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
res2 = __send_reply(f, in, res, NULL, 0, 1);
|
2004-06-25 05:00:00 +08:00
|
|
|
if(res2 == -ENOENT) {
|
|
|
|
/* The open syscall was interrupted, so it must be cancelled */
|
|
|
|
if(f->op.release)
|
|
|
|
f->op.release(path, arg->flags);
|
2004-07-12 23:55:11 +08:00
|
|
|
} else
|
|
|
|
get_node(f, in->ino)->open_count ++;
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
|
|
|
|
} else
|
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
|
2004-06-27 05:11:25 +08:00
|
|
|
if (path)
|
|
|
|
free(path);
|
2001-10-30 23:06:52 +08:00
|
|
|
}
|
|
|
|
|
2004-05-18 16:45:28 +08:00
|
|
|
static void do_flush(struct fuse *f, struct fuse_in_header *in)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2004-05-18 16:45:28 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.flush)
|
2004-05-18 16:45:28 +08:00
|
|
|
res = f->op.flush(path);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2002-12-11 17:50:26 +08:00
|
|
|
static void do_release(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_open_in *arg)
|
2002-12-10 20:26:00 +08:00
|
|
|
{
|
2004-06-25 05:00:00 +08:00
|
|
|
struct node *node;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
node = get_node(f, in->ino);
|
|
|
|
--node->open_count;
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
2004-06-20 06:42:38 +08:00
|
|
|
|
2004-06-25 05:00:00 +08:00
|
|
|
path = get_path(f, in->ino);
|
|
|
|
if (path != NULL) {
|
|
|
|
if (f->op.release)
|
2004-06-23 21:54:33 +08:00
|
|
|
f->op.release(path, arg->flags);
|
2004-06-25 05:00:00 +08:00
|
|
|
|
|
|
|
if(node->is_hidden && node->open_count == 0)
|
|
|
|
/* can now clean up this hidden file */
|
|
|
|
f->op.unlink(path);
|
|
|
|
|
|
|
|
free(path);
|
2002-12-10 20:26:00 +08:00
|
|
|
}
|
2004-06-30 19:13:41 +08:00
|
|
|
send_reply(f, in, 0, NULL, 0);
|
2002-12-10 20:26:00 +08:00
|
|
|
}
|
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
static void do_read(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_read_in *arg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
2001-11-19 03:15:05 +08:00
|
|
|
char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
|
|
|
|
struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
|
|
|
|
char *buf = outbuf + sizeof(struct fuse_out_header);
|
2001-10-31 22:52:35 +08:00
|
|
|
size_t size;
|
2001-11-19 03:15:05 +08:00
|
|
|
size_t outsize;
|
2001-10-31 22:52:35 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2002-01-07 05:44:16 +08:00
|
|
|
printf("READ %u bytes from %llu\n", arg->size, arg->offset);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2001-10-31 22:52:35 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.read)
|
2001-11-12 02:20:17 +08:00
|
|
|
res = f->op.read(path, buf, arg->size, arg->offset);
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size = 0;
|
2004-07-07 06:27:36 +08:00
|
|
|
if (res >= 0) {
|
2001-10-31 22:52:35 +08:00
|
|
|
size = res;
|
|
|
|
res = 0;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2002-01-07 05:44:16 +08:00
|
|
|
printf(" READ %u bytes\n", size);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2002-12-05 22:23:01 +08:00
|
|
|
memset(out, 0, sizeof(struct fuse_out_header));
|
2001-11-19 03:15:05 +08:00
|
|
|
out->unique = in->unique;
|
|
|
|
out->error = res;
|
|
|
|
outsize = sizeof(struct fuse_out_header) + size;
|
|
|
|
|
2004-07-12 23:55:11 +08:00
|
|
|
send_reply_raw(f, outbuf, outsize, 0);
|
2001-11-19 03:15:05 +08:00
|
|
|
free(outbuf);
|
2001-10-31 22:52:35 +08:00
|
|
|
}
|
2001-10-29 22:57:57 +08:00
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
static void do_write(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_write_in *arg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
2004-07-02 17:22:50 +08:00
|
|
|
struct fuse_write_out outarg;
|
2001-11-06 20:03:23 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
res = -ENOENT;
|
2001-11-06 20:03:23 +08:00
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
|
|
|
if (f->flags & FUSE_DEBUG) {
|
2002-01-07 05:44:16 +08:00
|
|
|
printf("WRITE %u bytes to %llu\n", arg->size, arg->offset);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.write)
|
2002-10-28 16:49:39 +08:00
|
|
|
res = f->op.write(path, PARAM(arg), arg->size, arg->offset);
|
2001-11-07 20:09:43 +08:00
|
|
|
free(path);
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
|
|
|
|
2004-07-02 17:22:50 +08:00
|
|
|
if (res >= 0) {
|
|
|
|
outarg.size = res;
|
|
|
|
res = 0;
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
|
|
|
|
2004-07-02 17:22:50 +08:00
|
|
|
send_reply(f, in, res, &outarg, sizeof(outarg));
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
|
|
|
|
2004-03-25 19:17:52 +08:00
|
|
|
static int default_statfs(struct statfs *buf)
|
|
|
|
{
|
|
|
|
buf->f_namelen = 255;
|
|
|
|
buf->f_bsize = 512;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-19 22:23:27 +08:00
|
|
|
static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
|
|
|
|
{
|
|
|
|
kstatfs->bsize = statfs->f_bsize;
|
|
|
|
kstatfs->blocks = statfs->f_blocks;
|
|
|
|
kstatfs->bfree = statfs->f_bfree;
|
|
|
|
kstatfs->bavail = statfs->f_bavail;
|
|
|
|
kstatfs->files = statfs->f_files;
|
|
|
|
kstatfs->ffree = statfs->f_ffree;
|
|
|
|
kstatfs->namelen = statfs->f_namelen;
|
|
|
|
}
|
|
|
|
|
2002-01-08 00:32:02 +08:00
|
|
|
static void do_statfs(struct fuse *f, struct fuse_in_header *in)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct fuse_statfs_out arg;
|
2004-02-19 22:23:27 +08:00
|
|
|
struct statfs buf;
|
2002-01-08 00:32:02 +08:00
|
|
|
|
2004-03-25 19:17:52 +08:00
|
|
|
memset(&buf, 0, sizeof(struct statfs));
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.statfs)
|
2004-02-19 22:23:27 +08:00
|
|
|
res = f->op.statfs("/", &buf);
|
2004-03-25 19:17:52 +08:00
|
|
|
else
|
|
|
|
res = default_statfs(&buf);
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res == 0)
|
2004-03-25 19:17:52 +08:00
|
|
|
convert_statfs(&buf, &arg.st);
|
2002-01-09 20:23:27 +08:00
|
|
|
|
2002-01-08 00:32:02 +08:00
|
|
|
send_reply(f, in, res, &arg, sizeof(arg));
|
|
|
|
}
|
|
|
|
|
2003-12-12 22:06:41 +08:00
|
|
|
static void do_fsync(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_fsync_in *inarg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
|
|
|
path = get_path(f, in->ino);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (path != NULL) {
|
2004-06-03 22:45:04 +08:00
|
|
|
res = -ENOSYS;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->op.fsync)
|
2003-12-12 22:06:41 +08:00
|
|
|
res = f->op.fsync(path, inarg->datasync);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2004-03-30 23:17:26 +08:00
|
|
|
static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_setxattr_in *arg)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
char *name = PARAM(arg);
|
|
|
|
unsigned char *value = name + strlen(name) + 1;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
|
|
|
path = get_path(f, in->ino);
|
|
|
|
if (path != NULL) {
|
2004-06-03 22:45:04 +08:00
|
|
|
res = -ENOSYS;
|
2004-03-30 23:17:26 +08:00
|
|
|
if (f->op.setxattr)
|
|
|
|
res = f->op.setxattr(path, name, value, arg->size, arg->flags);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2004-03-31 18:19:18 +08:00
|
|
|
static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
const char *name, char *value, size_t size)
|
2004-03-30 23:17:26 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
|
|
|
path = get_path(f, in->ino);
|
|
|
|
if (path != NULL) {
|
2004-06-03 22:45:04 +08:00
|
|
|
res = -ENOSYS;
|
2004-03-30 23:17:26 +08:00
|
|
|
if (f->op.getxattr)
|
2004-03-31 18:19:18 +08:00
|
|
|
res = f->op.getxattr(path, name, value, size);
|
2004-03-30 23:17:26 +08:00
|
|
|
free(path);
|
|
|
|
}
|
2004-03-31 18:19:18 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
const char *name, size_t size)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
|
|
|
|
struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
|
|
|
|
char *value = outbuf + sizeof(struct fuse_out_header);
|
|
|
|
|
|
|
|
res = common_getxattr(f, in, name, value, size);
|
2004-03-30 23:17:26 +08:00
|
|
|
size = 0;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res > 0) {
|
2004-03-30 23:17:26 +08:00
|
|
|
size = res;
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
memset(out, 0, sizeof(struct fuse_out_header));
|
|
|
|
out->unique = in->unique;
|
|
|
|
out->error = res;
|
|
|
|
|
2004-07-12 23:55:11 +08:00
|
|
|
send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
|
2004-03-30 23:17:26 +08:00
|
|
|
free(outbuf);
|
|
|
|
}
|
|
|
|
|
2004-03-31 18:19:18 +08:00
|
|
|
static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct fuse_getxattr_out arg;
|
|
|
|
|
|
|
|
res = common_getxattr(f, in, name, NULL, 0);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res >= 0) {
|
2004-03-31 18:19:18 +08:00
|
|
|
arg.size = res;
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
send_reply(f, in, res, &arg, sizeof(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_getxattr_in *arg)
|
|
|
|
{
|
|
|
|
char *name = PARAM(arg);
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (arg->size)
|
2004-03-31 18:19:18 +08:00
|
|
|
do_getxattr_read(f, in, name, arg->size);
|
|
|
|
else
|
|
|
|
do_getxattr_size(f, in, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
char *list, size_t size)
|
2004-03-30 23:17:26 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
|
|
|
path = get_path(f, in->ino);
|
|
|
|
if (path != NULL) {
|
2004-06-03 22:45:04 +08:00
|
|
|
res = -ENOSYS;
|
2004-03-30 23:17:26 +08:00
|
|
|
if (f->op.listxattr)
|
2004-03-31 18:19:18 +08:00
|
|
|
res = f->op.listxattr(path, list, size);
|
2004-03-30 23:17:26 +08:00
|
|
|
free(path);
|
|
|
|
}
|
2004-03-31 18:19:18 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
|
|
|
|
struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
|
|
|
|
char *list = outbuf + sizeof(struct fuse_out_header);
|
|
|
|
|
|
|
|
res = common_listxattr(f, in, list, size);
|
2004-03-30 23:17:26 +08:00
|
|
|
size = 0;
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res > 0) {
|
2004-03-30 23:17:26 +08:00
|
|
|
size = res;
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
memset(out, 0, sizeof(struct fuse_out_header));
|
|
|
|
out->unique = in->unique;
|
|
|
|
out->error = res;
|
|
|
|
|
2004-07-12 23:55:11 +08:00
|
|
|
send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
|
2004-03-30 23:17:26 +08:00
|
|
|
free(outbuf);
|
|
|
|
}
|
|
|
|
|
2004-03-31 18:19:18 +08:00
|
|
|
static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct fuse_getxattr_out arg;
|
|
|
|
|
|
|
|
res = common_listxattr(f, in, NULL, 0);
|
|
|
|
if (res >= 0) {
|
|
|
|
arg.size = res;
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
send_reply(f, in, res, &arg, sizeof(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
struct fuse_getxattr_in *arg)
|
|
|
|
{
|
2004-06-20 06:42:38 +08:00
|
|
|
if (arg->size)
|
2004-03-31 18:19:18 +08:00
|
|
|
do_listxattr_read(f, in, arg->size);
|
|
|
|
else
|
|
|
|
do_listxattr_size(f, in);
|
|
|
|
}
|
|
|
|
|
2004-03-30 23:17:26 +08:00
|
|
|
static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
|
|
|
|
char *name)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
res = -ENOENT;
|
|
|
|
path = get_path(f, in->ino);
|
|
|
|
if (path != NULL) {
|
2004-06-03 22:45:04 +08:00
|
|
|
res = -ENOSYS;
|
2004-03-30 23:17:26 +08:00
|
|
|
if (f->op.removexattr)
|
|
|
|
res = f->op.removexattr(path, name);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
send_reply(f, in, res, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-19 03:15:05 +08:00
|
|
|
static void free_cmd(struct fuse_cmd *cmd)
|
|
|
|
{
|
|
|
|
free(cmd->buf);
|
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
2001-11-16 18:12:59 +08:00
|
|
|
void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
|
2001-11-06 20:03:23 +08:00
|
|
|
{
|
|
|
|
struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
|
|
|
|
void *inarg = cmd->buf + sizeof(struct fuse_in_header);
|
|
|
|
size_t argsize;
|
2001-12-20 23:38:05 +08:00
|
|
|
struct fuse_context *ctx = fuse_get_context(f);
|
2001-11-06 20:03:23 +08:00
|
|
|
|
2001-11-26 01:19:59 +08:00
|
|
|
dec_avail(f);
|
2001-11-20 01:55:51 +08:00
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if ((f->flags & FUSE_DEBUG)) {
|
2002-12-10 20:26:00 +08:00
|
|
|
printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n",
|
|
|
|
in->unique, opname(in->opcode), in->opcode, in->ino,
|
|
|
|
cmd->buflen);
|
2001-11-07 20:35:06 +08:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-12-20 23:38:05 +08:00
|
|
|
|
|
|
|
ctx->uid = in->uid;
|
|
|
|
ctx->gid = in->gid;
|
2001-11-06 20:03:23 +08:00
|
|
|
|
|
|
|
argsize = cmd->buflen - sizeof(struct fuse_in_header);
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
switch (in->opcode) {
|
2001-11-06 20:03:23 +08:00
|
|
|
case FUSE_LOOKUP:
|
|
|
|
do_lookup(f, in, (char *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_GETATTR:
|
|
|
|
do_getattr(f, in);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_SETATTR:
|
|
|
|
do_setattr(f, in, (struct fuse_setattr_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_READLINK:
|
|
|
|
do_readlink(f, in);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_GETDIR:
|
|
|
|
do_getdir(f, in);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_MKNOD:
|
|
|
|
do_mknod(f, in, (struct fuse_mknod_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_MKDIR:
|
|
|
|
do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_UNLINK:
|
2004-02-20 22:10:49 +08:00
|
|
|
do_unlink(f, in, (char *) inarg);
|
|
|
|
break;
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
case FUSE_RMDIR:
|
2004-02-20 22:10:49 +08:00
|
|
|
do_rmdir(f, in, (char *) inarg);
|
2001-11-06 20:03:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_SYMLINK:
|
|
|
|
do_symlink(f, in, (char *) inarg,
|
|
|
|
((char *) inarg) + strlen((char *) inarg) + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_RENAME:
|
|
|
|
do_rename(f, in, (struct fuse_rename_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_LINK:
|
|
|
|
do_link(f, in, (struct fuse_link_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_OPEN:
|
|
|
|
do_open(f, in, (struct fuse_open_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
2004-05-18 16:45:28 +08:00
|
|
|
case FUSE_FLUSH:
|
|
|
|
do_flush(f, in);
|
|
|
|
break;
|
|
|
|
|
2002-12-11 17:50:26 +08:00
|
|
|
case FUSE_RELEASE:
|
|
|
|
do_release(f, in, (struct fuse_open_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
case FUSE_READ:
|
|
|
|
do_read(f, in, (struct fuse_read_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_WRITE:
|
|
|
|
do_write(f, in, (struct fuse_write_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
2002-01-08 00:32:02 +08:00
|
|
|
case FUSE_STATFS:
|
|
|
|
do_statfs(f, in);
|
|
|
|
break;
|
|
|
|
|
2003-12-12 22:06:41 +08:00
|
|
|
case FUSE_FSYNC:
|
|
|
|
do_fsync(f, in, (struct fuse_fsync_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
2004-03-30 23:17:26 +08:00
|
|
|
case FUSE_SETXATTR:
|
|
|
|
do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_GETXATTR:
|
2004-03-31 18:19:18 +08:00
|
|
|
do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
|
2004-03-30 23:17:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_LISTXATTR:
|
2004-03-31 18:19:18 +08:00
|
|
|
do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
|
2004-03-30 23:17:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_REMOVEXATTR:
|
|
|
|
do_removexattr(f, in, (char *) inarg);
|
|
|
|
break;
|
|
|
|
|
2001-11-06 20:03:23 +08:00
|
|
|
default:
|
2001-11-19 03:15:05 +08:00
|
|
|
send_reply(f, in, -ENOSYS, NULL, 0);
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
2001-11-19 03:15:05 +08:00
|
|
|
|
|
|
|
free_cmd(cmd);
|
2001-11-06 20:03:23 +08:00
|
|
|
}
|
|
|
|
|
2004-06-30 19:34:56 +08:00
|
|
|
int __fuse_exited(struct fuse* f)
|
|
|
|
{
|
|
|
|
return f->exited;
|
|
|
|
}
|
|
|
|
|
2001-11-16 18:12:59 +08:00
|
|
|
struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
|
2001-11-06 23:07:17 +08:00
|
|
|
{
|
2001-11-16 18:12:59 +08:00
|
|
|
ssize_t res;
|
|
|
|
struct fuse_cmd *cmd;
|
2001-11-26 01:19:59 +08:00
|
|
|
struct fuse_in_header *in;
|
|
|
|
void *inarg;
|
2001-11-16 18:12:59 +08:00
|
|
|
|
2001-11-19 03:15:05 +08:00
|
|
|
cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
|
|
|
|
cmd->buf = (char *) malloc(FUSE_MAX_IN);
|
2001-11-26 01:19:59 +08:00
|
|
|
in = (struct fuse_in_header *) cmd->buf;
|
|
|
|
inarg = cmd->buf + sizeof(struct fuse_in_header);
|
|
|
|
|
2004-06-30 19:34:56 +08:00
|
|
|
res = read(f->fd, cmd->buf, FUSE_MAX_IN);
|
|
|
|
if (res == -1) {
|
|
|
|
free_cmd(cmd);
|
|
|
|
if (__fuse_exited(f) || errno == EINTR)
|
2001-11-26 01:19:59 +08:00
|
|
|
return NULL;
|
2004-06-30 19:34:56 +08:00
|
|
|
|
|
|
|
/* ENODEV means we got unmounted, so we silenty return failure */
|
|
|
|
if (errno != ENODEV) {
|
|
|
|
/* BAD... This will happen again */
|
|
|
|
perror("fuse: reading device");
|
2001-11-26 01:19:59 +08:00
|
|
|
}
|
|
|
|
|
2004-06-30 19:34:56 +08:00
|
|
|
fuse_exit(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((size_t) res < sizeof(struct fuse_in_header)) {
|
|
|
|
free_cmd(cmd);
|
|
|
|
/* Cannot happen */
|
|
|
|
fprintf(stderr, "short read on fuse device\n");
|
|
|
|
fuse_exit(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cmd->buflen = res;
|
|
|
|
|
|
|
|
/* Forget is special, it can be done without messing with threads. */
|
|
|
|
if (in->opcode == FUSE_FORGET) {
|
|
|
|
do_forget(f, in, (struct fuse_forget_in *) inarg);
|
|
|
|
free_cmd(cmd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-26 01:19:59 +08:00
|
|
|
|
2001-11-16 18:12:59 +08:00
|
|
|
return cmd;
|
2001-11-06 23:07:17 +08:00
|
|
|
}
|
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
void fuse_loop(struct fuse *f)
|
|
|
|
{
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f == NULL)
|
2004-02-21 00:38:45 +08:00
|
|
|
return;
|
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
while (1) {
|
2002-12-05 22:23:01 +08:00
|
|
|
struct fuse_cmd *cmd;
|
|
|
|
|
2004-06-30 19:34:56 +08:00
|
|
|
if (__fuse_exited(f))
|
2002-12-05 22:23:01 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
cmd = __fuse_read_cmd(f);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (cmd == NULL)
|
2002-12-05 22:23:01 +08:00
|
|
|
continue;
|
2001-11-06 23:07:17 +08:00
|
|
|
|
2001-11-16 18:12:59 +08:00
|
|
|
__fuse_process_cmd(f, cmd);
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:23:01 +08:00
|
|
|
void fuse_exit(struct fuse *f)
|
|
|
|
{
|
|
|
|
f->exited = 1;
|
|
|
|
}
|
|
|
|
|
2001-12-20 20:17:25 +08:00
|
|
|
struct fuse_context *fuse_get_context(struct fuse *f)
|
|
|
|
{
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->getcontext)
|
2001-12-20 20:17:25 +08:00
|
|
|
return f->getcontext(f);
|
|
|
|
else
|
|
|
|
return &f->context;
|
|
|
|
}
|
|
|
|
|
2004-02-21 00:38:45 +08:00
|
|
|
static int check_version(struct fuse *f)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
FILE *vf = fopen(FUSE_VERSION_FILE, "r");
|
2004-06-20 06:42:38 +08:00
|
|
|
if (vf == NULL) {
|
2004-02-21 00:38:45 +08:00
|
|
|
fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
|
|
|
|
FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
|
|
|
|
fclose(vf);
|
2004-06-20 06:42:38 +08:00
|
|
|
if (res != 2) {
|
2004-02-21 00:38:45 +08:00
|
|
|
fprintf(stderr, "fuse: error reading %s\n", FUSE_VERSION_FILE);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->majorver != FUSE_KERNEL_VERSION) {
|
2004-02-21 00:38:45 +08:00
|
|
|
fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
|
|
|
|
FUSE_KERNEL_VERSION);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-06-20 06:42:38 +08:00
|
|
|
if (f->minorver < FUSE_KERNEL_MINOR_VERSION) {
|
2004-02-21 00:38:45 +08:00
|
|
|
fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i",
|
|
|
|
FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-07-24 01:16:29 +08:00
|
|
|
|
|
|
|
int fuse_is_lib_option(const char *opt)
|
|
|
|
{
|
|
|
|
if (strcmp(opt, "debug") == 0 ||
|
|
|
|
strcmp(opt, "hard_remove") == 0)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_lib_opts(struct fuse *f, const char *opts)
|
|
|
|
{
|
|
|
|
if (opts) {
|
|
|
|
char *xopts = strdup(opts);
|
|
|
|
char *s = xopts;
|
|
|
|
char *opt;
|
|
|
|
|
|
|
|
while((opt = strsep(&s, ","))) {
|
|
|
|
if (strcmp(opt, "debug") == 0)
|
|
|
|
f->flags |= FUSE_DEBUG;
|
|
|
|
else if (strcmp(opt, "hard_remove") == 0)
|
|
|
|
f->flags |= FUSE_HARD_REMOVE;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
|
|
|
|
}
|
|
|
|
free(xopts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fuse *fuse_new(int fd, const char *opts, const struct fuse_operations *op)
|
2001-10-29 03:44:14 +08:00
|
|
|
{
|
2001-11-07 20:09:43 +08:00
|
|
|
struct fuse *f;
|
|
|
|
struct node *root;
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
f = (struct fuse *) calloc(1, sizeof(struct fuse));
|
2001-11-06 23:07:17 +08:00
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
if (check_version(f) == -1) {
|
2004-02-21 00:38:45 +08:00
|
|
|
free(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-07-24 01:16:29 +08:00
|
|
|
parse_lib_opts(f, opts);
|
2001-11-09 22:49:18 +08:00
|
|
|
f->fd = fd;
|
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);
|
|
|
|
f->ino_table_size = 14057;
|
|
|
|
f->ino_table = (struct node **)
|
|
|
|
calloc(1, sizeof(struct node *) * f->ino_table_size);
|
2001-11-06 20:03:23 +08:00
|
|
|
pthread_mutex_init(&f->lock, NULL);
|
2001-11-20 01:55:51 +08:00
|
|
|
f->numworker = 0;
|
|
|
|
f->numavail = 0;
|
2001-12-20 20:17:25 +08:00
|
|
|
f->op = *op;
|
|
|
|
f->getcontext = NULL;
|
|
|
|
f->context.uid = 0;
|
|
|
|
f->context.gid = 0;
|
2002-12-05 22:23:01 +08:00
|
|
|
f->exited = 0;
|
2001-10-29 03:44:14 +08:00
|
|
|
|
2001-11-07 20:09:43 +08:00
|
|
|
root = (struct node *) calloc(1, sizeof(struct node));
|
2001-11-09 22:49:18 +08:00
|
|
|
root->mode = 0;
|
2001-11-07 20:09:43 +08:00
|
|
|
root->rdev = 0;
|
|
|
|
root->name = strdup("/");
|
|
|
|
root->parent = 0;
|
2004-02-20 00:55:40 +08:00
|
|
|
root->ino = FUSE_ROOT_INO;
|
|
|
|
root->generation = 0;
|
|
|
|
hash_ino(f, root);
|
2001-11-07 20:09:43 +08:00
|
|
|
|
2001-10-29 03:44:14 +08:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fuse_destroy(struct fuse *f)
|
|
|
|
{
|
2001-11-07 20:09:43 +08:00
|
|
|
size_t i;
|
2004-06-25 05:00:00 +08:00
|
|
|
for (i = 0; i < f->ino_table_size; i++) {
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
for (node = f->ino_table[i]; node != NULL; node = node->ino_next) {
|
|
|
|
if (node->is_hidden) {
|
|
|
|
char *path = get_path(f, node->ino);
|
|
|
|
if (path)
|
|
|
|
f->op.unlink(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-06-20 06:42:38 +08:00
|
|
|
for (i = 0; i < f->ino_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
|
|
|
|
2004-06-20 06:42:38 +08:00
|
|
|
for (node = f->ino_table[i]; node != NULL; node = next) {
|
2001-11-07 20:09:43 +08:00
|
|
|
next = node->ino_next;
|
|
|
|
free_node(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(f->ino_table);
|
|
|
|
free(f->name_table);
|
2001-11-06 20:03:23 +08:00
|
|
|
pthread_mutex_destroy(&f->lock);
|
2001-11-07 20:09:43 +08:00
|
|
|
free(f);
|
2001-10-29 03:44:14 +08:00
|
|
|
}
|