2016-10-05 21:33:37 +08:00
|
|
|
/*
|
|
|
|
FUSE: Filesystem in Userspace
|
|
|
|
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
|
|
|
|
|
|
|
|
This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
|
See the file COPYING.LIB
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* For pthread_rwlock_t */
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
#include "crc32b.h"
|
2021-08-30 19:59:21 +08:00
|
|
|
#include "fuse_node.h"
|
2021-10-01 11:28:54 +08:00
|
|
|
#include "khash.h"
|
2021-08-30 19:59:21 +08:00
|
|
|
#include "kvec.h"
|
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
#include "node.h"
|
2016-10-05 21:33:37 +08:00
|
|
|
#include "config.h"
|
2021-10-12 10:47:59 +08:00
|
|
|
#include "fuse_dirents.h"
|
2016-10-05 21:33:37 +08:00
|
|
|
#include "fuse_i.h"
|
2021-10-12 10:47:59 +08:00
|
|
|
#include "fuse_kernel.h"
|
2016-10-05 21:33:37 +08:00
|
|
|
#include "fuse_lowlevel.h"
|
|
|
|
#include "fuse_misc.h"
|
2021-10-12 10:47:59 +08:00
|
|
|
#include "fuse_opt.h"
|
2022-11-13 11:26:26 +08:00
|
|
|
#include "fuse_pollhandle.h"
|
2023-02-27 04:16:18 +08:00
|
|
|
#include "fuse_msgbuf.hpp"
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
2016-10-05 21:33:37 +08:00
|
|
|
#include <fcntl.h>
|
2022-12-09 11:04:15 +08:00
|
|
|
#include <inttypes.h>
|
2016-10-05 21:33:37 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <poll.h>
|
2019-10-01 06:45:58 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/mman.h>
|
2016-10-05 21:33:37 +08:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
2019-10-01 06:45:58 +08:00
|
|
|
#include <sys/uio.h>
|
2023-09-10 04:05:17 +08:00
|
|
|
#include <syslog.h>
|
2019-10-01 06:45:58 +08:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
#ifdef HAVE_MALLOC_TRIM
|
|
|
|
#include <malloc.h>
|
2016-10-05 21:33:37 +08:00
|
|
|
#endif
|
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
#define FUSE_UNKNOWN_INO UINT64_MAX
|
2016-10-05 21:33:37 +08:00
|
|
|
#define OFFSET_MAX 0x7fffffffffffffffLL
|
|
|
|
|
|
|
|
#define NODE_TABLE_MIN_SIZE 8192
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
#define PARAM(inarg) ((void*)(((char*)(inarg)) + sizeof(*(inarg))))
|
|
|
|
|
2021-10-11 06:37:14 +08:00
|
|
|
static int g_LOG_METRICS = 0;
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_config
|
|
|
|
{
|
|
|
|
unsigned int uid;
|
|
|
|
unsigned int gid;
|
2020-11-16 08:12:33 +08:00
|
|
|
unsigned int umask;
|
2020-08-01 11:50:47 +08:00
|
|
|
int remember;
|
|
|
|
int debug;
|
2022-03-08 07:47:28 +08:00
|
|
|
int nogc;
|
2020-08-01 11:50:47 +08:00
|
|
|
int set_mode;
|
|
|
|
int set_uid;
|
|
|
|
int set_gid;
|
|
|
|
int help;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_fs
|
|
|
|
{
|
|
|
|
struct fuse_operations op;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct lock_queue_element
|
|
|
|
{
|
|
|
|
struct lock_queue_element *next;
|
|
|
|
pthread_cond_t cond;
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid1;
|
2020-08-01 11:50:47 +08:00
|
|
|
const char *name1;
|
|
|
|
char **path1;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **wnode1;
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid2;
|
2020-08-01 11:50:47 +08:00
|
|
|
const char *name2;
|
|
|
|
char **path2;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **wnode2;
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
|
|
|
bool done : 1;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct node_table
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **array;
|
|
|
|
size_t use;
|
|
|
|
size_t size;
|
|
|
|
size_t split;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
#define container_of(ptr,type,member) ({ \
|
2020-08-01 11:50:47 +08:00
|
|
|
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
|
|
|
(type *)( (char *)__mptr - offsetof(type,member) );})
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
#define list_entry(ptr,type,member) \
|
|
|
|
container_of(ptr,type,member)
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct list_head
|
|
|
|
{
|
|
|
|
struct list_head *next;
|
|
|
|
struct list_head *prev;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
typedef struct remembered_node_t remembered_node_t;
|
|
|
|
struct remembered_node_t
|
2020-08-01 11:50:47 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
|
|
|
time_t time;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
typedef struct nodeid_gen_t nodeid_gen_t;
|
|
|
|
struct nodeid_gen_t
|
|
|
|
{
|
|
|
|
uint64_t nodeid;
|
|
|
|
uint64_t generation;
|
|
|
|
};
|
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
struct fuse
|
|
|
|
{
|
|
|
|
struct fuse_session *se;
|
|
|
|
struct node_table name_table;
|
|
|
|
struct node_table id_table;
|
2021-10-01 11:28:54 +08:00
|
|
|
nodeid_gen_t nodeid_gen;
|
2019-10-01 06:45:58 +08:00
|
|
|
unsigned int hidectr;
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
struct fuse_config conf;
|
|
|
|
struct fuse_fs *fs;
|
|
|
|
struct lock_queue_element *lockq;
|
2021-08-30 19:59:21 +08:00
|
|
|
|
|
|
|
pthread_t maintenance_thread;
|
|
|
|
kvec_t(remembered_node_t) remembered_nodes;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct lock
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
off_t start;
|
|
|
|
off_t end;
|
|
|
|
pid_t pid;
|
|
|
|
uint64_t owner;
|
|
|
|
struct lock *next;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define TREELOCK_WRITE -1
|
|
|
|
#define TREELOCK_WAIT_OFFSET INT_MIN
|
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
struct fuse_dh
|
|
|
|
{
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
uint64_t fh;
|
|
|
|
fuse_dirents_t d;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_context_i
|
|
|
|
{
|
|
|
|
struct fuse_context ctx;
|
|
|
|
fuse_req_t req;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static pthread_key_t fuse_context_key;
|
|
|
|
static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static int fuse_context_ref;
|
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
/*
|
|
|
|
Why was the nodeid:generation logic simplified?
|
|
|
|
|
|
|
|
nodeid is uint64_t: max value of 18446744073709551616
|
|
|
|
If nodes were created at a rate of 1048576 per second it would take
|
|
|
|
over 500 thousand years to roll over. I'm fine with risking that.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
uint64_t
|
|
|
|
generate_nodeid(nodeid_gen_t *ng_)
|
|
|
|
{
|
|
|
|
ng_->nodeid++;
|
|
|
|
|
|
|
|
return ng_->nodeid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
char*
|
|
|
|
filename_strdup(struct fuse *f_,
|
|
|
|
const char *fn_)
|
|
|
|
{
|
|
|
|
return strdup(fn_);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
filename_free(struct fuse *f_,
|
|
|
|
char *fn_)
|
|
|
|
{
|
|
|
|
free(fn_);
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
static
|
|
|
|
void*
|
|
|
|
fuse_hdr_arg(const struct fuse_in_header *hdr_)
|
|
|
|
{
|
|
|
|
return (void*)&hdr_[1];
|
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
list_add(struct list_head *new,
|
|
|
|
struct list_head *prev,
|
|
|
|
struct list_head *next)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
next->prev = new;
|
|
|
|
new->next = next;
|
|
|
|
new->prev = prev;
|
|
|
|
prev->next = new;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
inline
|
|
|
|
void
|
|
|
|
list_add_head(struct list_head *new,
|
|
|
|
struct list_head *head)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
list_add(new,head,head->next);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
inline
|
|
|
|
void
|
|
|
|
list_add_tail(struct list_head *new,
|
|
|
|
struct list_head *head)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
list_add(new,head->prev,head);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
inline
|
|
|
|
void
|
|
|
|
list_del(struct list_head *entry)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct list_head *prev = entry->prev;
|
|
|
|
struct list_head *next = entry->next;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
next->prev = prev;
|
|
|
|
prev->next = next;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
size_t
|
|
|
|
id_hash(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
uint64_t hash = ((uint32_t)ino * 2654435761U) % f->id_table.size;
|
2020-08-01 11:50:47 +08:00
|
|
|
uint64_t oldhash = hash % (f->id_table.size / 2);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(oldhash >= f->id_table.split)
|
2020-08-01 11:50:47 +08:00
|
|
|
return oldhash;
|
|
|
|
else
|
|
|
|
return hash;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t*
|
2020-08-01 11:50:47 +08:00
|
|
|
get_node_nocheck(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
size_t hash = id_hash(f,nodeid);
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(node = f->id_table.array[hash]; node != NULL; node = node->id_next)
|
|
|
|
if(node->nodeid == nodeid)
|
2020-08-01 11:50:47 +08:00
|
|
|
return node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return NULL;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
static
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t*
|
2019-10-01 06:45:58 +08:00
|
|
|
get_node(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
const uint64_t nodeid)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node = get_node_nocheck(f,nodeid);
|
2019-10-01 06:45:58 +08:00
|
|
|
|
|
|
|
if(!node)
|
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
fprintf(stderr,"fuse internal error: node %llu not found\n",
|
|
|
|
(unsigned long long)nodeid);
|
2019-10-01 06:45:58 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
void
|
2021-08-30 19:59:21 +08:00
|
|
|
remove_remembered_node(struct fuse *f_,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
for(size_t i = 0; i < kv_size(f_->remembered_nodes); i++)
|
|
|
|
{
|
|
|
|
if(kv_A(f_->remembered_nodes,i).node != node_)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
kv_delete(f_->remembered_nodes,i);
|
|
|
|
break;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
static
|
|
|
|
uint32_t
|
|
|
|
stat_crc32b(const struct stat *st_)
|
|
|
|
{
|
|
|
|
uint32_t crc;
|
|
|
|
|
|
|
|
crc = crc32b_start();
|
|
|
|
crc = crc32b_continue(&st_->st_ino,sizeof(st_->st_ino),crc);
|
|
|
|
crc = crc32b_continue(&st_->st_size,sizeof(st_->st_size),crc);
|
|
|
|
crc = crc32b_continue(&st_->st_mtim,sizeof(st_->st_mtim),crc);
|
|
|
|
crc = crc32b_finish(crc);
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
#ifndef CLOCK_MONOTONIC
|
|
|
|
# define CLOCK_MONOTONIC CLOCK_REALTIME
|
|
|
|
#endif
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
2021-08-30 19:59:21 +08:00
|
|
|
time_t
|
|
|
|
current_time()
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
int rv;
|
|
|
|
struct timespec now;
|
|
|
|
static clockid_t clockid = CLOCK_MONOTONIC;
|
|
|
|
|
|
|
|
rv = clock_gettime(clockid,&now);
|
|
|
|
if((rv == -1) && (errno == EINVAL))
|
|
|
|
{
|
|
|
|
clockid = CLOCK_REALTIME;
|
|
|
|
rv = clock_gettime(clockid,&now);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rv == -1)
|
|
|
|
now.tv_sec = time(NULL);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
return now.tv_sec;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-22 10:57:02 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
free_node(struct fuse *f_,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-10-29 11:01:47 +08:00
|
|
|
filename_free(f_,node_->name);
|
2019-05-22 10:57:02 +08:00
|
|
|
|
2023-04-05 12:28:34 +08:00
|
|
|
if(node_->hidden_fh)
|
2022-11-13 11:26:26 +08:00
|
|
|
f_->fs->op.free_hide(node_->hidden_fh);
|
2019-05-22 10:57:02 +08:00
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
node_free(node_);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
node_table_reduce(struct node_table *t)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
size_t newsize = t->size / 2;
|
|
|
|
void *newarray;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(newsize < NODE_TABLE_MIN_SIZE)
|
2020-08-01 11:50:47 +08:00
|
|
|
return;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
newarray = realloc(t->array,sizeof(node_t*) * newsize);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(newarray != NULL)
|
2020-08-01 11:50:47 +08:00
|
|
|
t->array = newarray;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
t->size = newsize;
|
|
|
|
t->split = t->size / 2;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
remerge_id(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct node_table *t = &f->id_table;
|
|
|
|
int iter;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(t->split == 0)
|
2020-08-01 11:50:47 +08:00
|
|
|
node_table_reduce(t);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(iter = 8; t->split > 0 && iter; iter--)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **upper;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
t->split--;
|
|
|
|
upper = &t->array[t->split + t->size / 2];
|
|
|
|
if(*upper)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **nodep;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(nodep = &t->array[t->split]; *nodep;
|
|
|
|
nodep = &(*nodep)->id_next);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
*nodep = *upper;
|
|
|
|
*upper = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
static
|
|
|
|
void
|
2020-11-16 08:12:33 +08:00
|
|
|
unhash_id(struct fuse *f,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **nodep = &f->id_table.array[id_hash(f,node->nodeid)];
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(; *nodep != NULL; nodep = &(*nodep)->id_next)
|
|
|
|
if(*nodep == node)
|
|
|
|
{
|
|
|
|
*nodep = node->id_next;
|
|
|
|
f->id_table.use--;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f->id_table.use < f->id_table.size / 4)
|
|
|
|
remerge_id(f);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
node_table_resize(struct node_table *t)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
size_t newsize = t->size * 2;
|
|
|
|
void *newarray;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
newarray = realloc(t->array,sizeof(node_t*) * newsize);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(newarray == NULL)
|
2020-08-01 11:50:47 +08:00
|
|
|
return -1;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
t->array = newarray;
|
2023-04-24 11:33:48 +08:00
|
|
|
memset(t->array + t->size,0,t->size * sizeof(node_t*));
|
2020-08-01 11:50:47 +08:00
|
|
|
t->size = newsize;
|
|
|
|
t->split = 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
rehash_id(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct node_table *t = &f->id_table;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **nodep;
|
|
|
|
node_t **next;
|
2020-08-01 11:50:47 +08:00
|
|
|
size_t hash;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(t->split == t->size / 2)
|
2020-08-01 11:50:47 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
hash = t->split;
|
|
|
|
t->split++;
|
2020-11-16 08:12:33 +08:00
|
|
|
for(nodep = &t->array[hash]; *nodep != NULL; nodep = next)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node = *nodep;
|
2020-11-16 08:12:33 +08:00
|
|
|
size_t newhash = id_hash(f,node->nodeid);
|
|
|
|
|
|
|
|
if(newhash != hash)
|
|
|
|
{
|
|
|
|
next = nodep;
|
|
|
|
*nodep = node->id_next;
|
|
|
|
node->id_next = t->array[newhash];
|
|
|
|
t->array[newhash] = node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = &node->id_next;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
if(t->split == t->size / 2)
|
2020-08-01 11:50:47 +08:00
|
|
|
node_table_resize(t);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
hash_id(struct fuse *f,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
size_t hash;
|
|
|
|
|
|
|
|
hash = id_hash(f,node->nodeid);
|
2020-08-01 11:50:47 +08:00
|
|
|
node->id_next = f->id_table.array[hash];
|
|
|
|
f->id_table.array[hash] = node;
|
|
|
|
f->id_table.use++;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f->id_table.use >= f->id_table.size / 2)
|
2020-08-01 11:50:47 +08:00
|
|
|
rehash_id(f);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
size_t
|
|
|
|
name_hash(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t parent,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
uint64_t hash = parent;
|
|
|
|
uint64_t oldhash;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(; *name; name++)
|
|
|
|
hash = hash * 31 + (unsigned char)*name;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
hash %= f->name_table.size;
|
|
|
|
oldhash = hash % (f->name_table.size / 2);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(oldhash >= f->name_table.split)
|
2020-08-01 11:50:47 +08:00
|
|
|
return oldhash;
|
|
|
|
else
|
|
|
|
return hash;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
unref_node(struct fuse *f,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
remerge_name(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int iter;
|
2020-11-16 08:12:33 +08:00
|
|
|
struct node_table *t = &f->name_table;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(t->split == 0)
|
2020-08-01 11:50:47 +08:00
|
|
|
node_table_reduce(t);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(iter = 8; t->split > 0 && iter; iter--)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **upper;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
t->split--;
|
|
|
|
upper = &t->array[t->split + t->size / 2];
|
|
|
|
if(*upper)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **nodep;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(nodep = &t->array[t->split]; *nodep; nodep = &(*nodep)->name_next);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
*nodep = *upper;
|
|
|
|
*upper = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
unhash_name(struct fuse *f,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
if(node->name)
|
|
|
|
{
|
|
|
|
size_t hash = name_hash(f,node->parent->nodeid,node->name);
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **nodep = &f->name_table.array[hash];
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
for(; *nodep != NULL; nodep = &(*nodep)->name_next)
|
|
|
|
if(*nodep == node)
|
|
|
|
{
|
|
|
|
*nodep = node->name_next;
|
|
|
|
node->name_next = NULL;
|
|
|
|
unref_node(f,node->parent);
|
2021-10-29 11:01:47 +08:00
|
|
|
filename_free(f,node->name);
|
2020-11-16 08:12:33 +08:00
|
|
|
node->name = NULL;
|
|
|
|
node->parent = NULL;
|
|
|
|
f->name_table.use--;
|
|
|
|
|
|
|
|
if(f->name_table.use < f->name_table.size / 4)
|
|
|
|
remerge_name(f);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"fuse internal error: unable to unhash node: %llu\n",
|
|
|
|
(unsigned long long)node->nodeid);
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
abort();
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
rehash_name(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct node_table *t = &f->name_table;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **nodep;
|
|
|
|
node_t **next;
|
2020-08-01 11:50:47 +08:00
|
|
|
size_t hash;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(t->split == t->size / 2)
|
2020-08-01 11:50:47 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
hash = t->split;
|
|
|
|
t->split++;
|
2020-11-16 08:12:33 +08:00
|
|
|
for(nodep = &t->array[hash]; *nodep != NULL; nodep = next)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node = *nodep;
|
2020-11-16 08:12:33 +08:00
|
|
|
size_t newhash = name_hash(f,node->parent->nodeid,node->name);
|
|
|
|
|
|
|
|
if(newhash != hash)
|
|
|
|
{
|
|
|
|
next = nodep;
|
|
|
|
*nodep = node->name_next;
|
|
|
|
node->name_next = t->array[newhash];
|
|
|
|
t->array[newhash] = node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next = &node->name_next;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
if(t->split == t->size / 2)
|
2020-08-01 11:50:47 +08:00
|
|
|
node_table_resize(t);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
hash_name(struct fuse *f,
|
2023-08-12 14:50:37 +08:00
|
|
|
node_t *node,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t parentid,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
size_t hash = name_hash(f,parentid,name);
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *parent = get_node(f,parentid);
|
2021-10-29 11:01:47 +08:00
|
|
|
node->name = filename_strdup(f,name);
|
|
|
|
if(node->name == NULL)
|
|
|
|
return -1;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
parent->refctr++;
|
2020-08-01 11:50:47 +08:00
|
|
|
node->parent = parent;
|
|
|
|
node->name_next = f->name_table.array[hash];
|
|
|
|
f->name_table.array[hash] = node;
|
|
|
|
f->name_table.use++;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f->name_table.use >= f->name_table.size / 2)
|
2020-08-01 11:50:47 +08:00
|
|
|
rehash_name(f);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
|
|
|
remember_nodes(struct fuse *f_)
|
|
|
|
{
|
|
|
|
return (f_->conf.remember > 0);
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
delete_node(struct fuse *f,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
assert(node->treelock == 0);
|
2020-11-16 08:12:33 +08:00
|
|
|
unhash_name(f,node);
|
2021-08-30 19:59:21 +08:00
|
|
|
if(remember_nodes(f))
|
|
|
|
remove_remembered_node(f,node);
|
2020-11-16 08:12:33 +08:00
|
|
|
unhash_id(f,node);
|
2023-04-24 11:33:48 +08:00
|
|
|
node_free(node);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
unref_node(struct fuse *f,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
assert(node->refctr > 0);
|
2020-11-16 08:12:33 +08:00
|
|
|
node->refctr--;
|
|
|
|
if(!node->refctr)
|
|
|
|
delete_node(f,node);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
static
|
|
|
|
uint64_t
|
|
|
|
rand64(void)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-10-01 06:45:58 +08:00
|
|
|
uint64_t rv;
|
|
|
|
|
|
|
|
rv = rand();
|
|
|
|
rv <<= 32;
|
|
|
|
rv |= rand();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t*
|
2020-11-16 08:12:33 +08:00
|
|
|
lookup_node(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t parent,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
size_t hash;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
hash = name_hash(f,parent,name);
|
|
|
|
for(node = f->name_table.array[hash]; node != NULL; node = node->name_next)
|
|
|
|
if(node->parent->nodeid == parent && strcmp(node->name,name) == 0)
|
2020-08-01 11:50:47 +08:00
|
|
|
return node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return NULL;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2023-04-24 11:33:48 +08:00
|
|
|
inc_nlookup(node_t *node)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!node->nlookup)
|
2020-08-01 11:50:47 +08:00
|
|
|
node->refctr++;
|
|
|
|
node->nlookup++;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t*
|
2020-11-16 08:12:33 +08:00
|
|
|
find_node(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t parent,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!name)
|
|
|
|
node = get_node(f,parent);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2020-11-16 08:12:33 +08:00
|
|
|
node = lookup_node(f,parent,name);
|
2021-08-30 19:59:21 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(node == NULL)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node = node_alloc();
|
2020-11-16 08:12:33 +08:00
|
|
|
if(node == NULL)
|
|
|
|
goto out_err;
|
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
node->nodeid = generate_nodeid(&f->nodeid_gen);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f->conf.remember)
|
|
|
|
inc_nlookup(node);
|
|
|
|
|
|
|
|
if(hash_name(f,node,parent,name) == -1)
|
|
|
|
{
|
|
|
|
free_node(f,node);
|
|
|
|
node = NULL;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
hash_id(f,node);
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2021-08-30 19:59:21 +08:00
|
|
|
else if((node->nlookup == 1) && remember_nodes(f))
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
remove_remembered_node(f,node);
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
|
|
|
inc_nlookup(node);
|
|
|
|
out_err:
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
return node;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
char*
|
|
|
|
add_name(char **buf,
|
|
|
|
unsigned *bufsize,
|
|
|
|
char *s,
|
|
|
|
const char *name)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
size_t len = strlen(name);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(s - len <= *buf)
|
|
|
|
{
|
|
|
|
unsigned pathlen = *bufsize - (s - *buf);
|
|
|
|
unsigned newbufsize = *bufsize;
|
|
|
|
char *newbuf;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
while(newbufsize < pathlen + len + 1)
|
|
|
|
{
|
|
|
|
if(newbufsize >= 0x80000000)
|
|
|
|
newbufsize = 0xffffffff;
|
|
|
|
else
|
|
|
|
newbufsize *= 2;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
newbuf = realloc(*buf,newbufsize);
|
|
|
|
if(newbuf == NULL)
|
|
|
|
return NULL;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
*buf = newbuf;
|
|
|
|
s = newbuf + newbufsize - pathlen;
|
|
|
|
memmove(s,newbuf + *bufsize - pathlen,pathlen);
|
|
|
|
*bufsize = newbufsize;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
s -= len;
|
2020-11-16 08:12:33 +08:00
|
|
|
strncpy(s,name,len);
|
2020-08-01 11:50:47 +08:00
|
|
|
s--;
|
|
|
|
*s = '/';
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return s;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
unlock_path(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *wnode,
|
|
|
|
node_t *end)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(wnode)
|
|
|
|
{
|
|
|
|
assert(wnode->treelock == TREELOCK_WRITE);
|
|
|
|
wnode->treelock = 0;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(node = get_node(f,nodeid); node != end && node->nodeid != FUSE_ROOT_ID; node = node->parent)
|
|
|
|
{
|
|
|
|
assert(node->treelock != 0);
|
|
|
|
assert(node->treelock != TREELOCK_WAIT_OFFSET);
|
|
|
|
assert(node->treelock != TREELOCK_WRITE);
|
|
|
|
node->treelock--;
|
|
|
|
if(node->treelock == TREELOCK_WAIT_OFFSET)
|
|
|
|
node->treelock = 0;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
try_get_path(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name,
|
|
|
|
char **path,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **wnodep,
|
2020-11-16 08:12:33 +08:00
|
|
|
bool need_lock)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
unsigned bufsize = 256;
|
|
|
|
char *buf;
|
|
|
|
char *s;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
|
|
|
node_t *wnode = NULL;
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
*path = NULL;
|
|
|
|
|
|
|
|
err = -ENOMEM;
|
|
|
|
buf = malloc(bufsize);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(buf == NULL)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
s = buf + bufsize - 1;
|
|
|
|
*s = '\0';
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(name != NULL)
|
|
|
|
{
|
|
|
|
s = add_name(&buf,&bufsize,s,name);
|
|
|
|
err = -ENOMEM;
|
|
|
|
if(s == NULL)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(wnodep)
|
|
|
|
{
|
|
|
|
assert(need_lock);
|
|
|
|
wnode = lookup_node(f,nodeid,name);
|
|
|
|
if(wnode)
|
|
|
|
{
|
|
|
|
if(wnode->treelock != 0)
|
|
|
|
{
|
|
|
|
if(wnode->treelock > 0)
|
|
|
|
wnode->treelock += TREELOCK_WAIT_OFFSET;
|
|
|
|
err = -EAGAIN;
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
wnode->treelock = TREELOCK_WRITE;
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(node = get_node(f,nodeid); node->nodeid != FUSE_ROOT_ID; node = node->parent)
|
|
|
|
{
|
2022-03-21 00:09:22 +08:00
|
|
|
err = -ESTALE;
|
2020-11-16 08:12:33 +08:00
|
|
|
if(node->name == NULL || node->parent == NULL)
|
|
|
|
goto out_unlock;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
err = -ENOMEM;
|
|
|
|
s = add_name(&buf,&bufsize,s,node->name);
|
|
|
|
if(s == NULL)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_unlock;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(need_lock)
|
|
|
|
{
|
|
|
|
err = -EAGAIN;
|
|
|
|
if(node->treelock < 0)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
node->treelock++;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(s[0])
|
|
|
|
memmove(buf,s,bufsize - (s - buf));
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2020-11-16 08:12:33 +08:00
|
|
|
strcpy(buf,"/");
|
2020-08-01 11:50:47 +08:00
|
|
|
|
|
|
|
*path = buf;
|
2020-11-16 08:12:33 +08:00
|
|
|
if(wnodep)
|
2020-08-01 11:50:47 +08:00
|
|
|
*wnodep = wnode;
|
|
|
|
|
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
|
|
|
out_unlock:
|
2020-11-16 08:12:33 +08:00
|
|
|
if(need_lock)
|
|
|
|
unlock_path(f,nodeid,wnode,node);
|
2016-10-05 21:33:37 +08:00
|
|
|
out_free:
|
2020-08-01 11:50:47 +08:00
|
|
|
free(buf);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
|
|
|
out_err:
|
2020-08-01 11:50:47 +08:00
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
2023-01-28 14:17:07 +08:00
|
|
|
int
|
|
|
|
try_get_path2(struct fuse *f,
|
|
|
|
uint64_t nodeid1,
|
|
|
|
const char *name1,
|
|
|
|
uint64_t nodeid2,
|
|
|
|
const char *name2,
|
|
|
|
char **path1,
|
|
|
|
char **path2,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **wnode1,
|
|
|
|
node_t **wnode2)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-01-28 14:17:07 +08:00
|
|
|
int err;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-01-28 14:17:07 +08:00
|
|
|
err = try_get_path(f,nodeid1,name1,path1,wnode1,true);
|
|
|
|
if(!err)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2023-01-28 14:17:07 +08:00
|
|
|
err = try_get_path(f,nodeid2,name2,path2,wnode2,true);
|
|
|
|
if(err)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *wn1 = wnode1 ? *wnode1 : NULL;
|
2020-11-16 08:12:33 +08:00
|
|
|
|
2023-01-28 14:17:07 +08:00
|
|
|
unlock_path(f,nodeid1,wn1,NULL);
|
|
|
|
free(*path1);
|
|
|
|
}
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2023-01-28 14:17:07 +08:00
|
|
|
|
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
queue_element_wakeup(struct fuse *f,
|
|
|
|
struct lock_queue_element *qe)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!qe->path1)
|
|
|
|
{
|
|
|
|
/* Just waiting for it to be unlocked */
|
|
|
|
if(get_node(f,qe->nodeid1)->treelock == 0)
|
|
|
|
pthread_cond_signal(&qe->cond);
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2023-01-28 14:17:07 +08:00
|
|
|
if(qe->done)
|
|
|
|
return;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2023-01-28 14:17:07 +08:00
|
|
|
if(!qe->path2)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2023-01-28 14:17:07 +08:00
|
|
|
err = try_get_path(f,
|
|
|
|
qe->nodeid1,
|
|
|
|
qe->name1,
|
|
|
|
qe->path1,
|
|
|
|
qe->wnode1,
|
|
|
|
true);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2023-01-28 14:17:07 +08:00
|
|
|
else
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2023-01-28 14:17:07 +08:00
|
|
|
err = try_get_path2(f,
|
|
|
|
qe->nodeid1,
|
|
|
|
qe->name1,
|
|
|
|
qe->nodeid2,
|
|
|
|
qe->name2,
|
|
|
|
qe->path1,
|
|
|
|
qe->path2,
|
|
|
|
qe->wnode1,
|
|
|
|
qe->wnode2);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2023-01-28 14:17:07 +08:00
|
|
|
if(err == -EAGAIN)
|
|
|
|
return;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
|
|
|
qe->err = err;
|
|
|
|
qe->done = true;
|
|
|
|
pthread_cond_signal(&qe->cond);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
wake_up_queued(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct lock_queue_element *qe;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(qe = f->lockq; qe != NULL; qe = qe->next)
|
|
|
|
queue_element_wakeup(f,qe);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
queue_path(struct fuse *f,
|
|
|
|
struct lock_queue_element *qe)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct lock_queue_element **qp;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
qe->done = false;
|
2020-11-16 08:12:33 +08:00
|
|
|
pthread_cond_init(&qe->cond,NULL);
|
2020-08-01 11:50:47 +08:00
|
|
|
qe->next = NULL;
|
2020-11-16 08:12:33 +08:00
|
|
|
for(qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
|
2020-08-01 11:50:47 +08:00
|
|
|
*qp = qe;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
dequeue_path(struct fuse *f,
|
|
|
|
struct lock_queue_element *qe)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct lock_queue_element **qp;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_cond_destroy(&qe->cond);
|
2020-11-16 08:12:33 +08:00
|
|
|
for(qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
|
2020-08-01 11:50:47 +08:00
|
|
|
*qp = qe->next;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
wait_path(struct fuse *f,
|
|
|
|
struct lock_queue_element *qe)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
queue_path(f,qe);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
pthread_cond_wait(&qe->cond,&f->lock);
|
|
|
|
} while(!qe->done);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
dequeue_path(f,qe);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return qe->err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
get_path_common(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name,
|
|
|
|
char **path,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **wnode)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
err = try_get_path(f,nodeid,name,path,wnode,true);
|
|
|
|
if(err == -EAGAIN)
|
|
|
|
{
|
2020-12-10 06:26:22 +08:00
|
|
|
struct lock_queue_element qe = {0};
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
qe.nodeid1 = nodeid;
|
|
|
|
qe.name1 = name;
|
|
|
|
qe.path1 = path;
|
|
|
|
qe.wnode1 = wnode;
|
|
|
|
|
|
|
|
err = wait_path(f,&qe);
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
get_path(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-11-16 08:12:33 +08:00
|
|
|
char **path)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
return get_path_common(f,nodeid,NULL,path,NULL);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
get_path_name(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name,
|
|
|
|
char **path)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
return get_path_common(f,nodeid,name,path,NULL);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
get_path_wrlock(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name,
|
|
|
|
char **path,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **wnode)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
return get_path_common(f,nodeid,name,path,wnode);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
get_path2(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid1,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name1,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid2,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name2,
|
|
|
|
char **path1,
|
|
|
|
char **path2,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t **wnode1,
|
|
|
|
node_t **wnode2)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
err = try_get_path2(f,nodeid1,name1,nodeid2,name2,
|
|
|
|
path1,path2,wnode1,wnode2);
|
|
|
|
if(err == -EAGAIN)
|
|
|
|
{
|
2020-12-10 06:26:22 +08:00
|
|
|
struct lock_queue_element qe = {0};
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
qe.nodeid1 = nodeid1;
|
|
|
|
qe.name1 = name1;
|
|
|
|
qe.path1 = path1;
|
|
|
|
qe.wnode1 = wnode1;
|
|
|
|
qe.nodeid2 = nodeid2;
|
|
|
|
qe.name2 = name2;
|
|
|
|
qe.path2 = path2;
|
|
|
|
qe.wnode2 = wnode2;
|
|
|
|
|
|
|
|
err = wait_path(f,&qe);
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
|
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
free_path_wrlock(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *wnode,
|
2020-11-16 08:12:33 +08:00
|
|
|
char *path)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
unlock_path(f,nodeid,wnode,NULL);
|
|
|
|
if(f->lockq)
|
2020-08-01 11:50:47 +08:00
|
|
|
wake_up_queued(f);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
free(path);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
free_path(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-11-16 08:12:33 +08:00
|
|
|
char *path)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
if(path)
|
|
|
|
free_path_wrlock(f,nodeid,NULL,path);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
free_path2(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid1,
|
|
|
|
uint64_t nodeid2,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *wnode1,
|
|
|
|
node_t *wnode2,
|
2020-11-16 08:12:33 +08:00
|
|
|
char *path1,
|
|
|
|
char *path2)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
unlock_path(f,nodeid1,wnode1,NULL);
|
|
|
|
unlock_path(f,nodeid2,wnode2,NULL);
|
2020-08-01 11:50:47 +08:00
|
|
|
wake_up_queued(f);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
free(path1);
|
|
|
|
free(path2);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
forget_node(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
const uint64_t nodeid,
|
2019-10-01 06:45:58 +08:00
|
|
|
const uint64_t nlookup)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
if(nodeid == FUSE_ROOT_ID)
|
|
|
|
return;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
node = get_node(f,nodeid);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
/*
|
|
|
|
* Node may still be locked due to interrupt idiocy in open,
|
|
|
|
* create and opendir
|
|
|
|
*/
|
|
|
|
while(node->nlookup == nlookup && node->treelock)
|
|
|
|
{
|
2020-12-10 06:26:22 +08:00
|
|
|
struct lock_queue_element qe = {0};
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
qe.nodeid1 = nodeid;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
queue_path(f,&qe);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
do
|
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
pthread_cond_wait(&qe.cond,&f->lock);
|
2019-10-01 06:45:58 +08:00
|
|
|
}
|
|
|
|
while((node->nlookup == nlookup) && node->treelock);
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
dequeue_path(f,&qe);
|
2019-10-01 06:45:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(node->nlookup >= nlookup);
|
|
|
|
node->nlookup -= nlookup;
|
2021-08-30 19:59:21 +08:00
|
|
|
|
|
|
|
if(node->nlookup == 0)
|
|
|
|
{
|
|
|
|
unref_node(f,node);
|
|
|
|
}
|
|
|
|
else if((node->nlookup == 1) && remember_nodes(f))
|
|
|
|
{
|
|
|
|
remembered_node_t fn;
|
|
|
|
|
|
|
|
fn.node = node;
|
|
|
|
fn.time = current_time();
|
|
|
|
|
|
|
|
kv_push(remembered_node_t,f->remembered_nodes,fn);
|
|
|
|
}
|
2019-10-01 06:45:58 +08:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
unlink_node(struct fuse *f,
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-10-01 10:13:29 +08:00
|
|
|
if(remember_nodes(f))
|
|
|
|
{
|
|
|
|
assert(node->nlookup > 1);
|
|
|
|
node->nlookup--;
|
|
|
|
}
|
2020-11-16 08:12:33 +08:00
|
|
|
unhash_name(f,node);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
remove_node(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t dir,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
node = lookup_node(f,dir,name);
|
|
|
|
if(node != NULL)
|
|
|
|
unlink_node(f,node);
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
rename_node(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t olddir,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *oldname,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t newdir,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *newname)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
|
|
|
node_t *newnode;
|
2020-08-01 11:50:47 +08:00
|
|
|
int err = 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
node = lookup_node(f,olddir,oldname);
|
|
|
|
newnode = lookup_node(f,newdir,newname);
|
|
|
|
if(node == NULL)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(newnode != NULL)
|
|
|
|
unlink_node(f,newnode);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
unhash_name(f,node);
|
|
|
|
if(hash_name(f,node,newdir,newname) == -1)
|
|
|
|
{
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
set_stat(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-11-16 08:12:33 +08:00
|
|
|
struct stat *stbuf)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f->conf.set_mode)
|
|
|
|
stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask);
|
|
|
|
if(f->conf.set_uid)
|
2020-08-01 11:50:47 +08:00
|
|
|
stbuf->st_uid = f->conf.uid;
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f->conf.set_gid)
|
2020-08-01 11:50:47 +08:00
|
|
|
stbuf->st_gid = f->conf.gid;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
struct fuse*
|
|
|
|
req_fuse(fuse_req_t req)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
return (struct fuse*)fuse_req_userdata(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
2019-03-04 12:26:20 +08:00
|
|
|
int
|
2023-04-24 11:33:48 +08:00
|
|
|
node_open(const node_t *node_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
return ((node_ != NULL) && (node_->open_count > 0));
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
static
|
|
|
|
void
|
2023-04-24 11:33:48 +08:00
|
|
|
update_stat(node_t *node_,
|
2019-05-24 11:22:36 +08:00
|
|
|
const struct stat *stnew_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-10-01 11:28:54 +08:00
|
|
|
uint32_t crc32b;
|
|
|
|
|
|
|
|
crc32b = stat_crc32b(stnew_);
|
|
|
|
|
|
|
|
if(node_->is_stat_cache_valid && (crc32b != node_->stat_crc32b))
|
|
|
|
node_->is_stat_cache_valid = 0;
|
2019-05-24 11:22:36 +08:00
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
node_->stat_crc32b = crc32b;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
set_path_info(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2020-06-22 02:45:14 +08:00
|
|
|
const char *name,
|
|
|
|
struct fuse_entry_param *e)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2020-06-22 02:45:14 +08:00
|
|
|
|
|
|
|
node = find_node(f,nodeid,name);
|
|
|
|
if(node == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
e->ino = node->nodeid;
|
2021-10-01 11:28:54 +08:00
|
|
|
e->generation = f->nodeid_gen.generation;
|
2020-06-22 02:45:14 +08:00
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
update_stat(node,&e->attr);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
|
|
|
|
set_stat(f,e->ino,&e->attr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-24 22:27:46 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
lookup_path(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t nodeid,
|
2019-09-24 22:27:46 +08:00
|
|
|
const char *name,
|
|
|
|
const char *path,
|
|
|
|
struct fuse_entry_param *e,
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t *fi)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-06-22 02:45:14 +08:00
|
|
|
int rv;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-09-24 22:27:46 +08:00
|
|
|
memset(e,0,sizeof(struct fuse_entry_param));
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
rv = ((fi == NULL) ?
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.getattr(path,&e->attr,&e->timeout) :
|
|
|
|
f->fs->op.fgetattr(fi,&e->attr,&e->timeout));
|
2019-09-24 22:27:46 +08:00
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
if(rv)
|
|
|
|
return rv;
|
2019-09-24 22:27:46 +08:00
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
return set_path_info(f,nodeid,name,e);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
struct fuse_context_i*
|
|
|
|
fuse_get_context_internal(void)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_context_i *c;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
c = (struct fuse_context_i *)pthread_getspecific(fuse_context_key);
|
|
|
|
if(c == NULL)
|
|
|
|
{
|
|
|
|
c = (struct fuse_context_i*)calloc(1,sizeof(struct fuse_context_i));
|
|
|
|
if(c == NULL)
|
|
|
|
{
|
|
|
|
/* This is hard to deal with properly,so just
|
|
|
|
abort. If memory is so low that the
|
|
|
|
context cannot be allocated,there's not
|
|
|
|
much hope for the filesystem anyway */
|
|
|
|
fprintf(stderr,"fuse: failed to allocate thread specific data\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
pthread_setspecific(fuse_context_key,c);
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
|
|
|
return c;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_freecontext(void *data)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
free(data);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
fuse_create_context_key(void)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err = 0;
|
|
|
|
pthread_mutex_lock(&fuse_context_lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!fuse_context_ref)
|
|
|
|
{
|
|
|
|
err = pthread_key_create(&fuse_context_key,fuse_freecontext);
|
|
|
|
if(err)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"fuse: failed to create thread specific key: %s\n",
|
|
|
|
strerror(err));
|
|
|
|
pthread_mutex_unlock(&fuse_context_lock);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
|
|
|
fuse_context_ref++;
|
|
|
|
pthread_mutex_unlock(&fuse_context_lock);
|
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_delete_context_key(void)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&fuse_context_lock);
|
|
|
|
fuse_context_ref--;
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!fuse_context_ref)
|
|
|
|
{
|
|
|
|
free(pthread_getspecific(fuse_context_key));
|
|
|
|
pthread_key_delete(fuse_context_key);
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_unlock(&fuse_context_lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
struct fuse*
|
|
|
|
req_fuse_prepare(fuse_req_t req)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_context_i *c = fuse_get_context_internal();
|
|
|
|
const struct fuse_ctx *ctx = fuse_req_ctx(req);
|
|
|
|
c->req = req;
|
|
|
|
c->ctx.fuse = req_fuse(req);
|
|
|
|
c->ctx.uid = ctx->uid;
|
|
|
|
c->ctx.gid = ctx->gid;
|
|
|
|
c->ctx.pid = ctx->pid;
|
|
|
|
c->ctx.umask = ctx->umask;
|
|
|
|
return c->ctx.fuse;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
reply_entry(fuse_req_t req,
|
|
|
|
const struct fuse_entry_param *e,
|
|
|
|
int err)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
struct fuse *f = req_fuse(req);
|
|
|
|
if(fuse_reply_entry(req,e) == -ENOENT)
|
|
|
|
{
|
|
|
|
/* Skip forget for negative result */
|
|
|
|
if(e->ino != 0)
|
|
|
|
forget_node(f,e->ino,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_init(void *data,
|
|
|
|
struct fuse_conn_info *conn)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse *f = (struct fuse *)data;
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_context_i *c = fuse_get_context_internal();
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
memset(c,0,sizeof(*c));
|
2020-08-01 11:50:47 +08:00
|
|
|
c->ctx.fuse = f;
|
|
|
|
conn->want |= FUSE_CAP_EXPORT_SUPPORT;
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.init(conn);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_destroy(void *data)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse *f = (struct fuse *)data;
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_context_i *c = fuse_get_context_internal();
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
memset(c,0,sizeof(*c));
|
2020-08-01 11:50:47 +08:00
|
|
|
c->ctx.fuse = f;
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.destroy();
|
|
|
|
free(f->fs);
|
2020-08-01 11:50:47 +08:00
|
|
|
f->fs = NULL;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 22:27:46 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_lookup(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-09-24 22:27:46 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
uint64_t nodeid;
|
2022-04-08 01:39:20 +08:00
|
|
|
char *path;
|
2022-11-13 11:26:26 +08:00
|
|
|
const char *name;
|
|
|
|
struct fuse *f;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *dot = NULL;
|
2022-04-08 01:39:20 +08:00
|
|
|
struct fuse_entry_param e = {0};
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
name = fuse_hdr_arg(hdr_);
|
|
|
|
nodeid = hdr_->nodeid;
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(name[0] == '.')
|
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
if(name[1] == '\0')
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
name = NULL;
|
2020-11-16 08:12:33 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
dot = get_node_nocheck(f,nodeid);
|
2021-08-30 19:59:21 +08:00
|
|
|
if(dot == NULL)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
reply_entry(req,&e,-ESTALE);
|
|
|
|
return;
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2021-08-30 19:59:21 +08:00
|
|
|
dot->refctr++;
|
2019-09-24 22:27:46 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2021-08-30 19:59:21 +08:00
|
|
|
}
|
|
|
|
else if((name[1] == '.') && (name[2] == '\0'))
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
if(nodeid == 1)
|
2022-04-08 01:39:20 +08:00
|
|
|
{
|
|
|
|
reply_entry(req,&e,-ENOENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
name = NULL;
|
2021-08-30 19:59:21 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
nodeid = get_node(f,nodeid)->parent->nodeid;
|
2021-08-30 19:59:21 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2019-09-24 22:27:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = get_path_name(f,nodeid,name,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = lookup_path(f,nodeid,name,path,&e,NULL);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(err == -ENOENT)
|
|
|
|
{
|
|
|
|
e.ino = 0;
|
|
|
|
err = 0;
|
|
|
|
}
|
2022-11-13 11:26:26 +08:00
|
|
|
free_path(f,nodeid,path);
|
2019-09-24 22:27:46 +08:00
|
|
|
}
|
2021-08-30 19:59:21 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(dot)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
unref_node(f,dot);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
}
|
2021-08-30 19:59:21 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
reply_entry(req,&e,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_forget(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_forget_in *arg;
|
|
|
|
|
|
|
|
f = req_fuse(req);
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
|
2022-11-28 12:23:38 +08:00
|
|
|
forget_node(f,hdr_->nodeid,arg->nlookup);
|
|
|
|
|
2019-10-01 06:45:58 +08:00
|
|
|
fuse_reply_none(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_forget_multi(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_batch_forget_in *arg;
|
|
|
|
struct fuse_forget_one *entry;
|
|
|
|
|
|
|
|
f = req_fuse(req);
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
entry = PARAM(arg);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
for(uint32_t i = 0; i < arg->count; i++)
|
2023-04-24 11:33:48 +08:00
|
|
|
{
|
|
|
|
forget_node(f,
|
2022-11-28 12:23:38 +08:00
|
|
|
entry[i].nodeid,
|
|
|
|
entry[i].nlookup);
|
2023-04-24 11:33:48 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
fuse_reply_none(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-24 22:27:46 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_getattr(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-09-24 22:27:46 +08:00
|
|
|
int err;
|
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
|
|
|
struct stat buf;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2019-09-24 22:27:46 +08:00
|
|
|
fuse_timeouts_t timeout;
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t ffi = {0};
|
2022-11-13 11:26:26 +08:00
|
|
|
const struct fuse_getattr_in *arg;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
if(arg->getattr_flags & FUSE_GETATTR_FH)
|
|
|
|
{
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
}
|
|
|
|
else
|
2019-09-24 22:27:46 +08:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
node = get_node(f,hdr_->nodeid);
|
2023-04-05 12:28:34 +08:00
|
|
|
if(node->hidden_fh)
|
2022-11-13 11:26:26 +08:00
|
|
|
ffi.fh = node->hidden_fh;
|
2019-09-24 22:27:46 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
memset(&buf,0,sizeof(buf));
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2020-07-09 07:25:23 +08:00
|
|
|
err = 0;
|
2019-09-24 22:27:46 +08:00
|
|
|
path = NULL;
|
2022-11-13 11:26:26 +08:00
|
|
|
if(ffi.fh == 0)
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2019-09-24 22:27:46 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = ((ffi.fh == 0) ?
|
|
|
|
f->fs->op.getattr(path,&buf,&timeout) :
|
|
|
|
f->fs->op.fgetattr(&ffi,&buf,&timeout));
|
2019-09-24 22:27:46 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
free_path(f,hdr_->nodeid,path);
|
2019-09-24 22:27:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
node = get_node(f,hdr_->nodeid);
|
2019-09-24 22:27:46 +08:00
|
|
|
update_stat(node,&buf);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
set_stat(f,hdr_->nodeid,&buf);
|
2019-09-24 22:27:46 +08:00
|
|
|
fuse_reply_attr(req,&buf,timeout.attr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2019-09-24 22:27:46 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-02-11 07:39:15 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_setattr(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2020-02-11 07:39:15 +08:00
|
|
|
{
|
|
|
|
struct fuse *f = req_fuse_prepare(req);
|
2022-11-13 11:26:26 +08:00
|
|
|
struct stat stbuf = {0};
|
2020-02-11 07:39:15 +08:00
|
|
|
char *path;
|
|
|
|
int err;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2019-09-24 22:27:46 +08:00
|
|
|
fuse_timeouts_t timeout;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t *fi;
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t ffi = {0};
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse_setattr_in *arg;
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
|
|
|
|
fi = NULL;
|
|
|
|
if(arg->valid & FATTR_FH)
|
|
|
|
{
|
|
|
|
fi = &ffi;
|
|
|
|
fi->fh = arg->fh;
|
|
|
|
}
|
|
|
|
else
|
2020-02-11 07:39:15 +08:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
node = get_node(f,hdr_->nodeid);
|
2023-04-05 12:28:34 +08:00
|
|
|
if(node->hidden_fh)
|
2020-02-11 07:39:15 +08:00
|
|
|
{
|
|
|
|
fi = &ffi;
|
|
|
|
fi->fh = node->hidden_fh;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-07-09 07:25:23 +08:00
|
|
|
err = 0;
|
2020-02-11 07:39:15 +08:00
|
|
|
path = NULL;
|
2020-07-09 07:25:23 +08:00
|
|
|
if(fi == NULL)
|
2022-11-13 11:26:26 +08:00
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2020-02-11 07:39:15 +08:00
|
|
|
|
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
err = 0;
|
2022-11-13 11:26:26 +08:00
|
|
|
if(!err && (arg->valid & FATTR_MODE))
|
2019-03-04 12:26:20 +08:00
|
|
|
err = ((fi == NULL) ?
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.chmod(path,arg->mode) :
|
|
|
|
f->fs->op.fchmod(fi,arg->mode));
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(!err && (arg->valid & (FATTR_UID | FATTR_GID)))
|
2020-02-11 07:39:15 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
uid_t uid = ((arg->valid & FATTR_UID) ? arg->uid : (uid_t)-1);
|
|
|
|
gid_t gid = ((arg->valid & FATTR_GID) ? arg->gid : (gid_t)-1);
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2020-02-11 07:39:15 +08:00
|
|
|
err = ((fi == NULL) ?
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.chown(path,uid,gid) :
|
|
|
|
f->fs->op.fchown(fi,uid,gid));
|
2020-02-11 07:39:15 +08:00
|
|
|
}
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(!err && (arg->valid & FATTR_SIZE))
|
2020-02-11 07:39:15 +08:00
|
|
|
err = ((fi == NULL) ?
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.truncate(path,arg->size) :
|
|
|
|
f->fs->op.ftruncate(fi,arg->size));
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2016-10-05 21:33:37 +08:00
|
|
|
#ifdef HAVE_UTIMENSAT
|
2022-11-13 11:26:26 +08:00
|
|
|
if(!err && (arg->valid & (FATTR_ATIME | FATTR_MTIME)))
|
2020-02-11 07:39:15 +08:00
|
|
|
{
|
|
|
|
struct timespec tv[2];
|
|
|
|
|
|
|
|
tv[0].tv_sec = 0;
|
|
|
|
tv[1].tv_sec = 0;
|
|
|
|
tv[0].tv_nsec = UTIME_OMIT;
|
|
|
|
tv[1].tv_nsec = UTIME_OMIT;
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(arg->valid & FATTR_ATIME_NOW)
|
2020-02-11 07:39:15 +08:00
|
|
|
tv[0].tv_nsec = UTIME_NOW;
|
2022-11-13 11:26:26 +08:00
|
|
|
else if(arg->valid & FATTR_ATIME)
|
|
|
|
tv[0] = (struct timespec){ arg->atime, arg->atimensec };
|
2020-02-11 07:39:15 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(arg->valid & FATTR_MTIME_NOW)
|
2020-02-11 07:39:15 +08:00
|
|
|
tv[1].tv_nsec = UTIME_NOW;
|
2022-11-13 11:26:26 +08:00
|
|
|
else if(arg->valid & FATTR_MTIME)
|
|
|
|
tv[1] = (struct timespec){ arg->mtime, arg->mtimensec };
|
2020-02-11 07:39:15 +08:00
|
|
|
|
|
|
|
err = ((fi == NULL) ?
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.utimens(path,tv) :
|
|
|
|
f->fs->op.futimens(fi,tv));
|
2020-02-11 07:39:15 +08:00
|
|
|
}
|
|
|
|
else
|
2016-10-05 21:33:37 +08:00
|
|
|
#endif
|
2022-11-13 11:26:26 +08:00
|
|
|
if(!err && ((arg->valid & (FATTR_ATIME|FATTR_MTIME)) == (FATTR_ATIME|FATTR_MTIME)))
|
2020-02-11 07:39:15 +08:00
|
|
|
{
|
|
|
|
struct timespec tv[2];
|
2022-11-13 11:26:26 +08:00
|
|
|
tv[0].tv_sec = arg->atime;
|
|
|
|
tv[0].tv_nsec = arg->atimensec;
|
|
|
|
tv[1].tv_sec = arg->mtime;
|
|
|
|
tv[1].tv_nsec = arg->mtimensec;
|
2020-02-11 07:39:15 +08:00
|
|
|
err = ((fi == NULL) ?
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.utimens(path,tv) :
|
|
|
|
f->fs->op.futimens(fi,tv));
|
2020-02-11 07:39:15 +08:00
|
|
|
}
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
2020-02-11 07:39:15 +08:00
|
|
|
err = ((fi == NULL) ?
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.getattr(path,&stbuf,&timeout) :
|
|
|
|
f->fs->op.fgetattr(fi,&stbuf,&timeout));
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-02-11 07:39:15 +08:00
|
|
|
}
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2020-02-11 07:39:15 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
update_stat(get_node(f,hdr_->nodeid),&stbuf);
|
2020-02-11 07:39:15 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
set_stat(f,hdr_->nodeid,&stbuf);
|
|
|
|
fuse_reply_attr(req,&stbuf,timeout.attr);
|
2020-02-11 07:39:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2020-02-11 07:39:15 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_access(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_access_in *arg;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.access(path,arg->mask);
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_readlink(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
|
|
|
char linkname[PATH_MAX + 1];
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.readlink(path,linkname,sizeof(linkname));
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
linkname[PATH_MAX] = '\0';
|
|
|
|
fuse_reply_readlink(req,linkname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_mknod(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
|
|
|
const char* name;
|
|
|
|
struct fuse_entry_param e;
|
|
|
|
struct fuse_mknod_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
name = PARAM(arg);
|
|
|
|
if(req->f->conn.proto_minor >= 12)
|
|
|
|
req->ctx.umask = arg->umask;
|
|
|
|
else
|
|
|
|
name = (char*)arg + FUSE_COMPAT_MKNOD_IN_SIZE;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = get_path_name(f,hdr_->nodeid,name,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
err = -ENOSYS;
|
2022-11-13 11:26:26 +08:00
|
|
|
if(S_ISREG(arg->mode))
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
|
|
|
fuse_file_info_t fi;
|
|
|
|
|
|
|
|
memset(&fi,0,sizeof(fi));
|
|
|
|
fi.flags = O_CREAT | O_EXCL | O_WRONLY;
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.create(path,arg->mode,&fi);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = lookup_path(f,hdr_->nodeid,name,path,&e,&fi);
|
|
|
|
f->fs->op.release(&fi);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-13 11:26:26 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(err == -ENOSYS)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.mknod(path,arg->mode,arg->rdev);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
2022-11-13 11:26:26 +08:00
|
|
|
err = lookup_path(f,hdr_->nodeid,name,path,&e,NULL);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2022-11-13 11:26:26 +08:00
|
|
|
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2022-11-13 11:26:26 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
reply_entry(req,&e,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_mkdir(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
|
|
|
const char *name;
|
|
|
|
struct fuse_entry_param e;
|
|
|
|
struct fuse_mkdir_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
name = PARAM(arg);
|
|
|
|
if(req->f->conn.proto_minor >= 12)
|
|
|
|
req->ctx.umask = arg->umask;
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = get_path_name(f,hdr_->nodeid,name,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.mkdir(path,arg->mode);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
2022-11-13 11:26:26 +08:00
|
|
|
err = lookup_path(f,hdr_->nodeid,name,path,&e,NULL);
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2022-11-13 11:26:26 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
reply_entry(req,&e,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:25:46 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_unlink(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2019-05-20 11:25:46 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
2022-11-13 11:26:26 +08:00
|
|
|
const char *name;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *wnode;
|
2019-05-20 11:25:46 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
name = PARAM(hdr_);
|
|
|
|
|
2019-05-20 11:25:46 +08:00
|
|
|
f = req_fuse_prepare(req);
|
2022-11-13 11:26:26 +08:00
|
|
|
err = get_path_wrlock(f,hdr_->nodeid,name,&path,&wnode);
|
2019-05-20 11:25:46 +08:00
|
|
|
|
|
|
|
if(!err)
|
|
|
|
{
|
2019-05-22 10:57:02 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
if(node_open(wnode))
|
2023-04-05 12:28:34 +08:00
|
|
|
err = f->fs->op.prepare_hide(path,&wnode->hidden_fh);
|
2019-05-22 10:57:02 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.unlink(path);
|
2019-05-22 10:57:02 +08:00
|
|
|
if(!err)
|
2022-11-13 11:26:26 +08:00
|
|
|
remove_node(f,hdr_->nodeid,name);
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
free_path_wrlock(f,hdr_->nodeid,wnode,path);
|
2019-05-20 11:25:46 +08:00
|
|
|
}
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_rmdir(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
|
|
|
const char *name;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *wnode;
|
2022-11-13 11:26:26 +08:00
|
|
|
|
|
|
|
name = PARAM(hdr_);
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = get_path_wrlock(f,hdr_->nodeid,name,&path,&wnode);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.rmdir(path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
2022-11-13 11:26:26 +08:00
|
|
|
remove_node(f,hdr_->nodeid,name);
|
|
|
|
free_path_wrlock(f,hdr_->nodeid,wnode,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_symlink(fuse_req_t req_,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-06-22 02:45:14 +08:00
|
|
|
int rv;
|
2020-08-01 11:50:47 +08:00
|
|
|
char *path;
|
2020-06-22 02:45:14 +08:00
|
|
|
struct fuse *f;
|
2022-11-13 11:26:26 +08:00
|
|
|
const char *name;
|
|
|
|
const char *linkname;
|
2020-06-22 02:45:14 +08:00
|
|
|
struct fuse_entry_param e = {0};
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
name = fuse_hdr_arg(hdr_);
|
|
|
|
linkname = (name + strlen(name) + 1);
|
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
f = req_fuse_prepare(req_);
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
rv = get_path_name(f,hdr_->nodeid,name,&path);
|
2020-06-22 02:45:14 +08:00
|
|
|
if(rv == 0)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
rv = f->fs->op.symlink(linkname,path,&e.attr,&e.timeout);
|
2020-06-22 02:45:14 +08:00
|
|
|
if(rv == 0)
|
2022-11-13 11:26:26 +08:00
|
|
|
rv = set_path_info(f,hdr_->nodeid,name,&e);
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
reply_entry(req_,&e,rv);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:25:46 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_rename(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2019-05-20 11:25:46 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct fuse *f;
|
|
|
|
char *oldpath;
|
|
|
|
char *newpath;
|
2022-11-13 11:26:26 +08:00
|
|
|
const char *oldname;
|
|
|
|
const char *newname;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *wnode1;
|
|
|
|
node_t *wnode2;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse_rename_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
oldname = PARAM(arg);
|
|
|
|
newname = (oldname + strlen(oldname) + 1);
|
2019-05-20 11:25:46 +08:00
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
2022-11-13 11:26:26 +08:00
|
|
|
err = get_path2(f,hdr_->nodeid,oldname,arg->newdir,newname,
|
2019-05-20 11:25:46 +08:00
|
|
|
&oldpath,&newpath,&wnode1,&wnode2);
|
|
|
|
|
|
|
|
if(!err)
|
|
|
|
{
|
2019-05-22 10:57:02 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
if(node_open(wnode2))
|
2023-04-05 12:28:34 +08:00
|
|
|
err = f->fs->op.prepare_hide(newpath,&wnode2->hidden_fh);
|
2019-05-22 10:57:02 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.rename(oldpath,newpath);
|
2019-05-20 11:25:46 +08:00
|
|
|
if(!err)
|
2022-11-13 11:26:26 +08:00
|
|
|
err = rename_node(f,hdr_->nodeid,oldname,arg->newdir,newname);
|
2019-05-17 02:38:07 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
free_path2(f,hdr_->nodeid,arg->newdir,wnode1,wnode2,oldpath,newpath);
|
2019-05-20 11:25:46 +08:00
|
|
|
}
|
2019-05-17 02:38:07 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_link(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-06-22 02:45:14 +08:00
|
|
|
int rv;
|
2020-08-01 11:50:47 +08:00
|
|
|
char *oldpath;
|
|
|
|
char *newpath;
|
2020-06-22 02:45:14 +08:00
|
|
|
struct fuse *f;
|
2022-11-13 11:26:26 +08:00
|
|
|
const char *newname;
|
|
|
|
struct fuse_link_in *arg;
|
2020-06-22 02:45:14 +08:00
|
|
|
struct fuse_entry_param e = {0};
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
newname = PARAM(arg);
|
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
f = req_fuse_prepare(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
rv = get_path2(f,
|
|
|
|
arg->oldnodeid,NULL,
|
|
|
|
hdr_->nodeid,newname,
|
2021-08-25 09:24:51 +08:00
|
|
|
&oldpath,&newpath,NULL,NULL);
|
2020-06-22 02:45:14 +08:00
|
|
|
if(!rv)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
rv = f->fs->op.link(oldpath,newpath,&e.attr,&e.timeout);
|
2020-06-22 02:45:14 +08:00
|
|
|
if(rv == 0)
|
2022-11-13 11:26:26 +08:00
|
|
|
rv = set_path_info(f,hdr_->nodeid,newname,&e);
|
|
|
|
free_path2(f,arg->oldnodeid,hdr_->nodeid,NULL,NULL,oldpath,newpath);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 02:45:14 +08:00
|
|
|
reply_entry(req,&e,rv);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_do_release(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t *fi)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
uint64_t fh;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
fh = 0;
|
2023-04-05 12:28:34 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.release(fi);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2023-04-05 12:28:34 +08:00
|
|
|
{
|
|
|
|
node = get_node(f,ino);
|
|
|
|
assert(node->open_count > 0);
|
|
|
|
node->open_count--;
|
|
|
|
|
|
|
|
if(node->hidden_fh && (node->open_count == 0))
|
|
|
|
{
|
|
|
|
fh = node->hidden_fh;
|
|
|
|
node->hidden_fh = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-04-05 12:28:34 +08:00
|
|
|
if(fh)
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.free_hide(fh);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_create(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-05-24 11:22:36 +08:00
|
|
|
int err;
|
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
2022-11-13 11:26:26 +08:00
|
|
|
const char *name;
|
|
|
|
fuse_file_info_t ffi = {0};
|
2019-05-24 11:22:36 +08:00
|
|
|
struct fuse_entry_param e;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse_create_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
name = PARAM(arg);
|
|
|
|
|
|
|
|
ffi.flags = arg->flags;
|
|
|
|
|
|
|
|
if(req->f->conn.proto_minor >= 12)
|
|
|
|
req->ctx.umask = arg->umask;
|
|
|
|
else
|
|
|
|
name = (char*)arg + sizeof(struct fuse_open_in);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
f = req_fuse_prepare(req);
|
2022-11-13 11:26:26 +08:00
|
|
|
|
|
|
|
err = get_path_name(f,hdr_->nodeid,name,&path);
|
2019-05-24 11:22:36 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.create(path,arg->mode,&ffi);
|
2019-05-24 11:22:36 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = lookup_path(f,hdr_->nodeid,name,path,&e,&ffi);
|
2019-05-24 11:22:36 +08:00
|
|
|
if(err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.release(&ffi);
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
|
|
|
else if(!S_ISREG(e.attr.st_mode))
|
|
|
|
{
|
|
|
|
err = -EIO;
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.release(&ffi);
|
2020-11-16 08:12:33 +08:00
|
|
|
forget_node(f,e.ino,1);
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
get_node(f,e.ino)->open_count++;
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
2019-03-04 12:26:20 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(fuse_reply_create(req,&e,&ffi) == -ENOENT)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
|
|
|
/* The open syscall was interrupted,so it
|
|
|
|
must be cancelled */
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_do_release(f,e.ino,&ffi);
|
2020-11-16 08:12:33 +08:00
|
|
|
forget_node(f,e.ino,1);
|
|
|
|
}
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
free_path(f,hdr_->nodeid,path);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
static
|
|
|
|
void
|
2020-11-16 08:12:33 +08:00
|
|
|
open_auto_cache(struct fuse *f,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *path,
|
|
|
|
fuse_file_info_t *fi)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2019-09-24 22:27:46 +08:00
|
|
|
fuse_timeouts_t timeout;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
node = get_node(f,ino);
|
2021-10-01 11:28:54 +08:00
|
|
|
if(node->is_stat_cache_valid)
|
2019-05-24 11:22:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct stat stbuf;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.fgetattr(fi,&stbuf,&timeout);
|
2019-05-24 11:22:36 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
|
|
|
|
if(!err)
|
|
|
|
update_stat(node,&stbuf);
|
|
|
|
else
|
2021-10-01 11:28:54 +08:00
|
|
|
node->is_stat_cache_valid = 0;
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
if(node->is_stat_cache_valid)
|
2019-05-24 11:22:36 +08:00
|
|
|
fi->keep_cache = 1;
|
|
|
|
|
2021-10-01 11:28:54 +08:00
|
|
|
node->is_stat_cache_valid = 1;
|
2019-05-24 11:22:36 +08:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_open(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-05-24 11:22:36 +08:00
|
|
|
int err;
|
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
struct fuse_open_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
|
|
|
|
ffi.flags = arg->flags;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
f = req_fuse_prepare(req);
|
2022-11-13 11:26:26 +08:00
|
|
|
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2019-05-24 11:22:36 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.open(path,&ffi);
|
2019-05-24 11:22:36 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
if(ffi.auto_cache)
|
|
|
|
open_auto_cache(f,hdr_->nodeid,path,&ffi);
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-05-24 11:22:36 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
get_node(f,hdr_->nodeid)->open_count++;
|
2019-05-24 11:22:36 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
/* The open syscall was interrupted,so it must be cancelled */
|
2022-11-13 11:26:26 +08:00
|
|
|
if(fuse_reply_open(req,&ffi) == -ENOENT)
|
|
|
|
fuse_do_release(f,hdr_->nodeid,&ffi);
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2019-05-24 11:22:36 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
free_path(f,hdr_->nodeid,path);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_read(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int res;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
struct fuse_read_in *arg;
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_msgbuf_t *msgbuf;
|
2022-11-13 11:26:26 +08:00
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
if(req->f->conn.proto_minor >= 9)
|
|
|
|
{
|
|
|
|
ffi.flags = arg->flags;
|
|
|
|
ffi.lock_owner = arg->lock_owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-07-09 15:04:32 +08:00
|
|
|
msgbuf = msgbuf_alloc_page_aligned();
|
2023-02-27 04:16:18 +08:00
|
|
|
|
|
|
|
res = f->fs->op.read(&ffi,msgbuf->mem,arg->size,arg->offset);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(res >= 0)
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_data(req,msgbuf->mem,res);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,res);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
msgbuf_free(msgbuf);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_write(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int res;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *data;
|
|
|
|
struct fuse *f;
|
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
struct fuse_write_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
ffi.writepage = !!(arg->write_flags & 1);
|
|
|
|
if(req->f->conn.proto_minor < 9)
|
|
|
|
{
|
|
|
|
data = ((char*)arg) + FUSE_COMPAT_WRITE_IN_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ffi.flags = arg->flags;
|
|
|
|
ffi.lock_owner = arg->lock_owner;
|
|
|
|
data = PARAM(arg);
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
res = f->fs->op.write(&ffi,data,arg->size,arg->offset);
|
|
|
|
free_path(f,hdr_->nodeid,NULL);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(res >= 0)
|
|
|
|
fuse_reply_write(req,res);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,res);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_fsync(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_fsync_in *arg;
|
|
|
|
fuse_file_info_t ffi = {0};
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = f->fs->op.fsync(&ffi,
|
|
|
|
!!(arg->fsync_flags & 1));
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
struct fuse_dh*
|
|
|
|
get_dirhandle(const fuse_file_info_t *llfi,
|
|
|
|
fuse_file_info_t *fi)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse_dh *dh = (struct fuse_dh *)(uintptr_t)llfi->fh;
|
|
|
|
memset(fi,0,sizeof(fuse_file_info_t));
|
2020-08-01 11:50:47 +08:00
|
|
|
fi->fh = dh->fh;
|
|
|
|
return dh;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_opendir(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2020-11-16 08:12:33 +08:00
|
|
|
char *path;
|
|
|
|
struct fuse_dh *dh;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t llffi = {0};
|
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_open_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
llffi.flags = arg->flags;
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
dh = (struct fuse_dh *)calloc(1,sizeof(struct fuse_dh));
|
|
|
|
if(dh == NULL)
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,ENOMEM);
|
2020-11-16 08:12:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
|
|
|
fuse_dirents_init(&dh->d);
|
|
|
|
fuse_mutex_init(&dh->lock);
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
llffi.fh = (uintptr_t)dh;
|
|
|
|
ffi.flags = llffi.flags;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.opendir(path,&ffi);
|
|
|
|
dh->fh = ffi.fh;
|
|
|
|
llffi.keep_cache = ffi.keep_cache;
|
|
|
|
llffi.cache_readdir = ffi.cache_readdir;
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
if(fuse_reply_open(req,&llffi) == -ENOENT)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
|
|
|
/* The opendir syscall was interrupted,so it
|
|
|
|
must be cancelled */
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.releasedir(&ffi);
|
2020-11-16 08:12:33 +08:00
|
|
|
pthread_mutex_destroy(&dh->lock);
|
|
|
|
free(dh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_destroy(&dh->lock);
|
|
|
|
free(dh);
|
|
|
|
}
|
2022-11-13 11:26:26 +08:00
|
|
|
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
static
|
2020-07-11 01:50:14 +08:00
|
|
|
size_t
|
|
|
|
readdir_buf_size(fuse_dirents_t *d_,
|
|
|
|
size_t size_,
|
|
|
|
off_t off_)
|
2019-06-14 20:59:04 +08:00
|
|
|
{
|
2020-07-11 01:50:14 +08:00
|
|
|
if(off_ >= kv_size(d_->offs))
|
|
|
|
return 0;
|
2021-09-19 10:22:47 +08:00
|
|
|
if((kv_A(d_->offs,off_) + size_) > kv_size(d_->data))
|
|
|
|
return (kv_size(d_->data) - kv_A(d_->offs,off_));
|
2020-07-11 01:50:14 +08:00
|
|
|
return size_;
|
|
|
|
}
|
2019-06-14 20:59:04 +08:00
|
|
|
|
2020-07-11 01:50:14 +08:00
|
|
|
static
|
|
|
|
char*
|
|
|
|
readdir_buf(fuse_dirents_t *d_,
|
|
|
|
off_t off_)
|
|
|
|
{
|
2021-09-19 10:22:47 +08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
i = kv_A(d_->offs,off_);
|
|
|
|
|
|
|
|
return &kv_A(d_->data,i);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_readdir(fuse_req_t req_,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-06-14 20:59:04 +08:00
|
|
|
int rv;
|
2022-11-13 11:26:26 +08:00
|
|
|
size_t size;
|
2019-06-14 20:59:04 +08:00
|
|
|
struct fuse *f;
|
|
|
|
fuse_dirents_t *d;
|
|
|
|
struct fuse_dh *dh;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
fuse_file_info_t llffi = {0};
|
|
|
|
struct fuse_read_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
size = arg->size;
|
|
|
|
llffi.fh = arg->fh;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
f = req_fuse_prepare(req_);
|
2022-11-13 11:26:26 +08:00
|
|
|
dh = get_dirhandle(&llffi,&ffi);
|
2019-06-14 20:59:04 +08:00
|
|
|
d = &dh->d;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
pthread_mutex_lock(&dh->lock);
|
|
|
|
|
|
|
|
rv = 0;
|
2022-11-13 11:26:26 +08:00
|
|
|
if((arg->offset == 0) || (kv_size(d->data) == 0))
|
|
|
|
rv = f->fs->op.readdir(&ffi,d);
|
2019-06-14 20:59:04 +08:00
|
|
|
|
|
|
|
if(rv)
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req_,rv);
|
2019-06-14 20:59:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
size = readdir_buf_size(d,size,arg->offset);
|
2019-06-14 20:59:04 +08:00
|
|
|
|
|
|
|
fuse_reply_buf(req_,
|
2022-11-13 11:26:26 +08:00
|
|
|
readdir_buf(d,arg->offset),
|
|
|
|
size);
|
2019-06-14 20:59:04 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&dh->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_readdir_plus(fuse_req_t req_,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-06-14 20:59:04 +08:00
|
|
|
int rv;
|
2022-11-13 11:26:26 +08:00
|
|
|
size_t size;
|
2019-06-14 20:59:04 +08:00
|
|
|
struct fuse *f;
|
|
|
|
fuse_dirents_t *d;
|
|
|
|
struct fuse_dh *dh;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
fuse_file_info_t llffi = {0};
|
|
|
|
struct fuse_read_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
size = arg->size;
|
|
|
|
llffi.fh = arg->fh;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
f = req_fuse_prepare(req_);
|
2022-11-13 11:26:26 +08:00
|
|
|
dh = get_dirhandle(&llffi,&ffi);
|
2019-06-14 20:59:04 +08:00
|
|
|
d = &dh->d;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
pthread_mutex_lock(&dh->lock);
|
|
|
|
|
|
|
|
rv = 0;
|
2022-11-13 11:26:26 +08:00
|
|
|
if((arg->offset == 0) || (kv_size(d->data) == 0))
|
|
|
|
rv = f->fs->op.readdir_plus(&ffi,d);
|
2019-06-14 20:59:04 +08:00
|
|
|
|
|
|
|
if(rv)
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req_,rv);
|
2019-06-14 20:59:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
size = readdir_buf_size(d,size,arg->offset);
|
2019-06-14 20:59:04 +08:00
|
|
|
|
|
|
|
fuse_reply_buf(req_,
|
2022-11-13 11:26:26 +08:00
|
|
|
readdir_buf(d,arg->offset),
|
|
|
|
size);
|
2019-06-14 20:59:04 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&dh->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_releasedir(fuse_req_t req_,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2019-06-14 20:59:04 +08:00
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_dh *dh;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi;
|
|
|
|
fuse_file_info_t llffi = {0};
|
|
|
|
struct fuse_release_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
llffi.fh = arg->fh;
|
|
|
|
llffi.flags = arg->flags;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2019-06-14 20:59:04 +08:00
|
|
|
f = req_fuse_prepare(req_);
|
2022-11-13 11:26:26 +08:00
|
|
|
dh = get_dirhandle(&llffi,&ffi);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f->fs->op.releasedir(&ffi);
|
2019-06-14 20:59:04 +08:00
|
|
|
|
|
|
|
/* Done to keep race condition between last readdir reply and the unlock */
|
|
|
|
pthread_mutex_lock(&dh->lock);
|
|
|
|
pthread_mutex_unlock(&dh->lock);
|
|
|
|
pthread_mutex_destroy(&dh->lock);
|
|
|
|
fuse_dirents_free(&dh->d);
|
|
|
|
free(dh);
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req_,0);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_fsyncdir(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
fuse_file_info_t ffi;
|
|
|
|
fuse_file_info_t llffi = {0};
|
|
|
|
struct fuse_fsync_in *arg;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
llffi.fh = arg->fh;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
get_dirhandle(&llffi,&ffi);
|
|
|
|
|
|
|
|
err = f->fs->op.fsyncdir(&ffi,
|
|
|
|
!!(arg->fsync_flags & FUSE_FSYNC_FDATASYNC));
|
2020-07-09 07:25:23 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_statfs(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err = 0;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path = NULL;
|
|
|
|
struct fuse *f;
|
|
|
|
struct statvfs buf = {0};
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
if(hdr_->nodeid)
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.statfs(path ? path : "/",&buf);
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
fuse_reply_statfs(req,&buf);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_setxattr(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path;
|
|
|
|
const char *name;
|
|
|
|
const char *value;
|
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_setxattr_in *arg;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
2022-12-07 12:26:08 +08:00
|
|
|
if((req->f->conn.capable & FUSE_SETXATTR_EXT) && (req->f->conn.want & FUSE_SETXATTR_EXT))
|
|
|
|
name = PARAM(arg);
|
|
|
|
else
|
|
|
|
name = (((char*)arg) + FUSE_COMPAT_SETXATTR_IN_SIZE);
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
value = (name + strlen(name) + 1);
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.setxattr(path,name,value,arg->size,arg->flags);
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
common_getxattr(struct fuse *f,
|
|
|
|
fuse_req_t req,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
const char *name,
|
|
|
|
char *value,
|
|
|
|
size_t size)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
|
|
|
char *path;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
err = get_path(f,ino,&path);
|
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.getxattr(path,name,value,size);
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
free_path(f,ino,path);
|
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_getxattr(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int res;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
const char* name;
|
|
|
|
struct fuse_getxattr_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
name = PARAM(arg);
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(arg->size)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
char *value = (char*)malloc(arg->size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(value == NULL)
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,ENOMEM);
|
2020-11-16 08:12:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
res = common_getxattr(f,req,hdr_->nodeid,name,value,arg->size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(res > 0)
|
|
|
|
fuse_reply_buf(req,value,res);
|
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,res);
|
2020-11-16 08:12:33 +08:00
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
res = common_getxattr(f,req,hdr_->nodeid,name,NULL,0);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(res >= 0)
|
|
|
|
fuse_reply_xattr(req,res);
|
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,res);
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
common_listxattr(struct fuse *f,
|
|
|
|
fuse_req_t req,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
char *list,
|
|
|
|
size_t size)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
char *path;
|
|
|
|
int err;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
err = get_path(f,ino,&path);
|
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.listxattr(path,list,size);
|
2020-11-16 08:12:33 +08:00
|
|
|
free_path(f,ino,path);
|
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_listxattr(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int res;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
struct fuse_getxattr_in *arg;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
if(arg->size)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
char *list = (char*)malloc(arg->size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(list == NULL)
|
|
|
|
{
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,ENOMEM);
|
2020-11-16 08:12:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
res = common_listxattr(f,req,hdr_->nodeid,list,arg->size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(res > 0)
|
|
|
|
fuse_reply_buf(req,list,res);
|
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,res);
|
2020-11-16 08:12:33 +08:00
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
res = common_listxattr(f,req,hdr_->nodeid,NULL,0);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(res >= 0)
|
|
|
|
fuse_reply_xattr(req,res);
|
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,res);
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_removexattr(fuse_req_t req,
|
|
|
|
const struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
char *path;
|
|
|
|
const char *name;
|
|
|
|
struct fuse *f;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
name = fuse_hdr_arg(hdr_);
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.removexattr(path,name);
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-26 02:25:57 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_copy_file_range(fuse_req_t req_,
|
|
|
|
const struct fuse_in_header *hdr_)
|
2019-05-26 02:25:57 +08:00
|
|
|
{
|
|
|
|
ssize_t rv;
|
|
|
|
struct fuse *f;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi_in = {0};
|
|
|
|
fuse_file_info_t ffi_out = {0};
|
|
|
|
const struct fuse_copy_file_range_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi_in.fh = arg->fh_in;
|
|
|
|
ffi_out.fh = arg->fh_out;
|
2019-05-26 02:25:57 +08:00
|
|
|
|
|
|
|
f = req_fuse_prepare(req_);
|
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
rv = f->fs->op.copy_file_range(&ffi_in,
|
|
|
|
arg->off_in,
|
|
|
|
&ffi_out,
|
|
|
|
arg->off_out,
|
|
|
|
arg->len,
|
|
|
|
arg->flags);
|
2019-05-26 02:25:57 +08:00
|
|
|
|
|
|
|
if(rv >= 0)
|
|
|
|
fuse_reply_write(req_,rv);
|
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req_,rv);
|
2019-05-26 02:25:57 +08:00
|
|
|
}
|
|
|
|
|
2023-08-12 14:50:37 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_setupmapping(fuse_req_t req_,
|
|
|
|
const struct fuse_in_header *hdr_)
|
|
|
|
{
|
|
|
|
fuse_reply_err(req_,ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_removemapping(fuse_req_t req_,
|
|
|
|
const struct fuse_in_header *hdr_)
|
|
|
|
{
|
|
|
|
fuse_reply_err(req_,ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_syncfs(fuse_req_t req_,
|
|
|
|
const struct fuse_in_header *hdr_)
|
|
|
|
{
|
|
|
|
fuse_reply_err(req_,ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This is just a copy of fuse_lib_create. Needs to be rewritten
|
|
|
|
// so a nameless node can be setup.
|
|
|
|
// name is always '/'
|
|
|
|
// nodeid is the base directory
|
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_tmpfile(fuse_req_t req_,
|
|
|
|
const struct fuse_in_header *hdr_)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char *path;
|
|
|
|
struct fuse *f;
|
|
|
|
const char *name;
|
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
struct fuse_entry_param e;
|
|
|
|
struct fuse_create_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
name = PARAM(arg);
|
|
|
|
|
|
|
|
ffi.flags = arg->flags;
|
|
|
|
|
|
|
|
if(req_->f->conn.proto_minor >= 12)
|
|
|
|
req_->ctx.umask = arg->umask;
|
|
|
|
else
|
|
|
|
name = (char*)arg + sizeof(struct fuse_open_in);
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req_);
|
|
|
|
|
|
|
|
err = get_path_name(f,hdr_->nodeid,name,&path);
|
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
err = f->fs->op.tmpfile(path,arg->mode,&ffi);
|
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
err = lookup_path(f,hdr_->nodeid,name,path,&e,&ffi);
|
|
|
|
if(err)
|
|
|
|
{
|
|
|
|
f->fs->op.release(&ffi);
|
|
|
|
}
|
|
|
|
else if(!S_ISREG(e.attr.st_mode))
|
|
|
|
{
|
|
|
|
err = -EIO;
|
|
|
|
f->fs->op.release(&ffi);
|
|
|
|
forget_node(f,e.ino,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
get_node(f,e.ino)->open_count++;
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
|
|
|
|
if(fuse_reply_create(req_,&e,&ffi) == -ENOENT)
|
|
|
|
{
|
|
|
|
/* The open syscall was interrupted,so it
|
|
|
|
must be cancelled */
|
|
|
|
fuse_do_release(f,e.ino,&ffi);
|
|
|
|
forget_node(f,e.ino,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fuse_reply_err(req_,err);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t*
|
|
|
|
locks_conflict(node_t *node,
|
|
|
|
const lock_t *lock)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t *l;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(l = node->locks; l; l = l->next)
|
|
|
|
if(l->owner != lock->owner &&
|
|
|
|
lock->start <= l->end && l->start <= lock->end &&
|
|
|
|
(l->type == F_WRLCK || lock->type == F_WRLCK))
|
2020-08-01 11:50:47 +08:00
|
|
|
break;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return l;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2023-04-24 11:33:48 +08:00
|
|
|
delete_lock(lock_t **lockp)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t *l = *lockp;
|
2020-08-01 11:50:47 +08:00
|
|
|
*lockp = l->next;
|
|
|
|
free(l);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2023-04-24 11:33:48 +08:00
|
|
|
insert_lock(lock_t **pos,
|
|
|
|
lock_t *lock)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
lock->next = *pos;
|
2020-11-16 08:12:33 +08:00
|
|
|
*pos = lock;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
2023-04-24 11:33:48 +08:00
|
|
|
locks_insert(node_t *node,
|
|
|
|
lock_t *lock)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t **lp;
|
|
|
|
lock_t *newl1 = NULL;
|
|
|
|
lock_t *newl2 = NULL;
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
newl1 = malloc(sizeof(lock_t));
|
|
|
|
newl2 = malloc(sizeof(lock_t));
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!newl1 || !newl2)
|
|
|
|
{
|
|
|
|
free(newl1);
|
|
|
|
free(newl2);
|
|
|
|
return -ENOLCK;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(lp = &node->locks; *lp;)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t *l = *lp;
|
2020-11-16 08:12:33 +08:00
|
|
|
if(l->owner != lock->owner)
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
if(lock->type == l->type)
|
|
|
|
{
|
|
|
|
if(l->end < lock->start - 1)
|
|
|
|
goto skip;
|
|
|
|
if(lock->end < l->start - 1)
|
|
|
|
break;
|
|
|
|
if(l->start <= lock->start && lock->end <= l->end)
|
|
|
|
goto out;
|
|
|
|
if(l->start < lock->start)
|
|
|
|
lock->start = l->start;
|
|
|
|
if(lock->end < l->end)
|
|
|
|
lock->end = l->end;
|
|
|
|
goto delete;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(l->end < lock->start)
|
|
|
|
goto skip;
|
|
|
|
if(lock->end < l->start)
|
|
|
|
break;
|
|
|
|
if(lock->start <= l->start && l->end <= lock->end)
|
|
|
|
goto delete;
|
|
|
|
if(l->end <= lock->end)
|
|
|
|
{
|
|
|
|
l->end = lock->start - 1;
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
if(lock->start <= l->start)
|
|
|
|
{
|
|
|
|
l->start = lock->end + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*newl2 = *l;
|
|
|
|
newl2->start = lock->end + 1;
|
|
|
|
l->end = lock->start - 1;
|
|
|
|
insert_lock(&l->next,newl2);
|
|
|
|
newl2 = NULL;
|
|
|
|
}
|
|
|
|
skip:
|
|
|
|
lp = &l->next;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
delete:
|
|
|
|
delete_lock(lp);
|
|
|
|
}
|
|
|
|
if(lock->type != F_UNLCK)
|
|
|
|
{
|
|
|
|
*newl1 = *lock;
|
|
|
|
insert_lock(lp,newl1);
|
|
|
|
newl1 = NULL;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
out:
|
|
|
|
free(newl1);
|
|
|
|
free(newl2);
|
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
flock_to_lock(struct flock *flock,
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t *lock)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
memset(lock,0,sizeof(lock_t));
|
2020-08-01 11:50:47 +08:00
|
|
|
lock->type = flock->l_type;
|
|
|
|
lock->start = flock->l_start;
|
2020-11-16 08:12:33 +08:00
|
|
|
lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
|
2020-08-01 11:50:47 +08:00
|
|
|
lock->pid = flock->l_pid;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_to_flock(lock_t *lock,
|
2020-11-16 08:12:33 +08:00
|
|
|
struct flock *flock)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
flock->l_type = lock->type;
|
|
|
|
flock->l_start = lock->start;
|
2020-11-16 08:12:33 +08:00
|
|
|
flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
|
2020-08-01 11:50:47 +08:00
|
|
|
flock->l_pid = lock->pid;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
fuse_flush_common(struct fuse *f,
|
|
|
|
fuse_req_t req,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t *fi)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct flock lock;
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t l;
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
|
|
|
int errlock;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
memset(&lock,0,sizeof(lock));
|
2020-08-01 11:50:47 +08:00
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.flush(fi);
|
|
|
|
errlock = f->fs->op.lock(fi,F_SETLK,&lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
|
|
|
|
if(errlock != -ENOSYS)
|
|
|
|
{
|
|
|
|
flock_to_lock(&lock,&l);
|
|
|
|
l.owner = fi->lock_owner;
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
locks_insert(get_node(f,ino),&l);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
|
|
|
|
/* if op.lock() is defined FLUSH is needed regardless
|
|
|
|
of op.flush() */
|
|
|
|
if(err == -ENOSYS)
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_release(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err = 0;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
struct fuse_release_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
ffi.flags = arg->flags;
|
|
|
|
if(req->f->conn.proto_minor >= 8)
|
|
|
|
{
|
|
|
|
ffi.flush = !!(arg->release_flags & FUSE_RELEASE_FLUSH);
|
|
|
|
ffi.lock_owner = arg->lock_owner;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ffi.flock_release = 1;
|
|
|
|
ffi.lock_owner = arg->lock_owner;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
if(ffi.flush)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = fuse_flush_common(f,req,hdr_->nodeid,&ffi);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(err == -ENOSYS)
|
|
|
|
err = 0;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_do_release(f,hdr_->nodeid,&ffi);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_flush(fuse_req_t req,
|
|
|
|
struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
struct fuse_flush_in *arg;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
ffi.flush = 1;
|
|
|
|
if(req->f->conn.proto_minor >= 7)
|
|
|
|
ffi.lock_owner = arg->lock_owner;
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = fuse_flush_common(f,req,hdr_->nodeid,&ffi);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
fuse_lock_common(fuse_req_t req,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t *fi,
|
|
|
|
struct flock *lock,
|
|
|
|
int cmd)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse *f = req_fuse_prepare(req);
|
2020-07-09 07:25:23 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.lock(fi,cmd,lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return err;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
convert_fuse_file_lock(const struct fuse_file_lock *fl,
|
|
|
|
struct flock *flock)
|
|
|
|
{
|
|
|
|
memset(flock, 0, sizeof(struct flock));
|
|
|
|
flock->l_type = fl->type;
|
|
|
|
flock->l_whence = SEEK_SET;
|
|
|
|
flock->l_start = fl->start;
|
|
|
|
if (fl->end == OFFSET_MAX)
|
|
|
|
flock->l_len = 0;
|
|
|
|
else
|
|
|
|
flock->l_len = fl->end - fl->start + 1;
|
|
|
|
flock->l_pid = fl->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_getlk(fuse_req_t req,
|
|
|
|
const struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t lk;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct flock flk;
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t *conflict;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
const struct fuse_lk_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
ffi.lock_owner = arg->owner;
|
|
|
|
|
|
|
|
convert_fuse_file_lock(&arg->lk,&flk);
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
f = req_fuse(req);
|
|
|
|
|
|
|
|
flock_to_lock(&flk,&lk);
|
|
|
|
lk.owner = ffi.lock_owner;
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_lock(&f->lock);
|
2022-11-13 11:26:26 +08:00
|
|
|
conflict = locks_conflict(get_node(f,hdr_->nodeid),&lk);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(conflict)
|
2022-11-13 11:26:26 +08:00
|
|
|
lock_to_flock(conflict,&flk);
|
2020-08-01 11:50:47 +08:00
|
|
|
pthread_mutex_unlock(&f->lock);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!conflict)
|
2022-11-13 11:26:26 +08:00
|
|
|
err = fuse_lock_common(req,hdr_->nodeid,&ffi,&flk,F_GETLK);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_reply_lock(req,&flk);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_setlk(fuse_req_t req,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t *fi,
|
|
|
|
struct flock *lock,
|
|
|
|
int sleep)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
int err = fuse_lock_common(req,ino,fi,lock,
|
2020-08-01 11:50:47 +08:00
|
|
|
sleep ? F_SETLKW : F_SETLK);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
|
|
|
struct fuse *f = req_fuse(req);
|
2023-04-24 11:33:48 +08:00
|
|
|
lock_t l;
|
2020-11-16 08:12:33 +08:00
|
|
|
flock_to_lock(lock,&l);
|
|
|
|
l.owner = fi->lock_owner;
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
locks_insert(get_node(f,ino),&l);
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
}
|
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_lib_flock(fuse_req_t req,
|
2021-10-01 11:28:54 +08:00
|
|
|
uint64_t ino,
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_file_info_t *fi,
|
|
|
|
int op)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse *f = req_fuse_prepare(req);
|
2020-07-09 07:25:23 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.flock(fi,op);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_bmap(fuse_req_t req,
|
|
|
|
const struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2020-11-16 08:12:33 +08:00
|
|
|
char *path;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
uint64_t block;
|
|
|
|
const struct fuse_bmap_in *arg;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
block = arg->block;
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
|
|
|
|
|
|
|
err = get_path(f,hdr_->nodeid,&path);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
{
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.bmap(path,arg->blocksize,&block);
|
|
|
|
free_path(f,hdr_->nodeid,path);
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!err)
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_reply_bmap(req,block);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_ioctl(fuse_req_t req,
|
|
|
|
const struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2020-11-16 08:12:33 +08:00
|
|
|
char *out_buf = NULL;
|
|
|
|
struct fuse *f = req_fuse_prepare(req);
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi;
|
|
|
|
fuse_file_info_t llffi = {0};
|
|
|
|
const void *in_buf;
|
|
|
|
uint32_t out_size;
|
|
|
|
const struct fuse_ioctl_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
if((arg->flags & FUSE_IOCTL_DIR) && !(req->f->conn.want & FUSE_CAP_IOCTL_DIR))
|
|
|
|
{
|
|
|
|
fuse_reply_err(req,ENOTTY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((sizeof(void*) == 4) &&
|
|
|
|
(req->f->conn.proto_minor >= 16) &&
|
|
|
|
!(arg->flags & FUSE_IOCTL_32BIT))
|
|
|
|
{
|
|
|
|
req->ioctl_64bit = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
llffi.fh = arg->fh;
|
|
|
|
out_size = arg->out_size;
|
|
|
|
in_buf = (arg->in_size ? PARAM(arg) : NULL);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
err = -EPERM;
|
2022-11-13 11:26:26 +08:00
|
|
|
if(arg->flags & FUSE_IOCTL_UNRESTRICTED)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto err;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(arg->flags & FUSE_IOCTL_DIR)
|
|
|
|
get_dirhandle(&llffi,&ffi);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2022-11-13 11:26:26 +08:00
|
|
|
ffi = llffi;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
if(out_size)
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
|
|
|
err = -ENOMEM;
|
2022-11-13 11:26:26 +08:00
|
|
|
out_buf = malloc(out_size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!out_buf)
|
|
|
|
goto err;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
assert(!arg->in_size || !out_size || arg->in_size == out_size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(out_buf)
|
2022-11-13 11:26:26 +08:00
|
|
|
memcpy(out_buf,in_buf,arg->in_size);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
err = f->fs->op.ioctl(&ffi,
|
|
|
|
arg->cmd,
|
|
|
|
(void*)(uintptr_t)arg->arg,
|
|
|
|
arg->flags,
|
|
|
|
out_buf ?: (void *)in_buf,
|
|
|
|
&out_size);
|
2023-01-28 14:17:07 +08:00
|
|
|
if(err < 0)
|
|
|
|
goto err;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_reply_ioctl(req,err,out_buf,out_size);
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out;
|
|
|
|
err:
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2020-08-01 11:50:47 +08:00
|
|
|
out:
|
|
|
|
free(out_buf);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_poll(fuse_req_t req,
|
|
|
|
const struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse *f = req_fuse_prepare(req);
|
2020-08-01 11:50:47 +08:00
|
|
|
unsigned revents = 0;
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
fuse_pollhandle_t *ph = NULL;
|
|
|
|
const struct fuse_poll_in *arg;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-13 11:26:26 +08:00
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
|
|
|
|
if(arg->flags & FUSE_POLL_SCHEDULE_NOTIFY)
|
|
|
|
{
|
|
|
|
ph = (fuse_pollhandle_t*)malloc(sizeof(fuse_pollhandle_t));
|
|
|
|
if(ph == NULL)
|
|
|
|
{
|
|
|
|
fuse_reply_err(req,ENOMEM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ph->kh = arg->kh;
|
|
|
|
ph->ch = req->ch;
|
|
|
|
ph->f = req->f;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f->fs->op.poll(&ffi,ph,&revents);
|
2020-07-09 07:25:23 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!err)
|
|
|
|
fuse_reply_poll(req,revents);
|
2020-08-01 11:50:47 +08:00
|
|
|
else
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void
|
2022-11-13 11:26:26 +08:00
|
|
|
fuse_lib_fallocate(fuse_req_t req,
|
|
|
|
const struct fuse_in_header *hdr_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int err;
|
2022-11-13 11:26:26 +08:00
|
|
|
struct fuse *f;
|
|
|
|
fuse_file_info_t ffi = {0};
|
|
|
|
const struct fuse_fallocate_in *arg;
|
|
|
|
|
|
|
|
arg = fuse_hdr_arg(hdr_);
|
|
|
|
ffi.fh = arg->fh;
|
|
|
|
|
|
|
|
f = req_fuse_prepare(req);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-28 12:23:38 +08:00
|
|
|
err = f->fs->op.fallocate(&ffi,
|
|
|
|
arg->mode,
|
2022-11-13 11:26:26 +08:00
|
|
|
arg->offset,
|
2022-11-28 12:23:38 +08:00
|
|
|
arg->length);
|
2020-07-09 07:25:23 +08:00
|
|
|
|
2023-02-27 04:16:18 +08:00
|
|
|
fuse_reply_err(req,err);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
2021-08-30 19:59:21 +08:00
|
|
|
remembered_node_cmp(const void *a_,
|
|
|
|
const void *b_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
const remembered_node_t *a = a_;
|
|
|
|
const remembered_node_t *b = b_;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
return (a->time - b->time);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
remembered_nodes_sort(struct fuse *f_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
pthread_mutex_lock(&f_->lock);
|
|
|
|
qsort(&kv_first(f_->remembered_nodes),
|
|
|
|
kv_size(f_->remembered_nodes),
|
|
|
|
sizeof(remembered_node_t),
|
|
|
|
remembered_node_cmp);
|
|
|
|
pthread_mutex_unlock(&f_->lock);
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
#define MAX_PRUNE 100
|
|
|
|
#define MAX_CHECK 1000
|
|
|
|
|
|
|
|
int
|
|
|
|
fuse_prune_some_remembered_nodes(struct fuse *f_,
|
|
|
|
int *offset_)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
int pruned;
|
|
|
|
int checked;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
pthread_mutex_lock(&f_->lock);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
pruned = 0;
|
|
|
|
checked = 0;
|
|
|
|
now = current_time();
|
|
|
|
while(*offset_ < kv_size(f_->remembered_nodes))
|
2020-11-16 08:12:33 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
time_t age;
|
|
|
|
remembered_node_t *fn = &kv_A(f_->remembered_nodes,*offset_);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
if(pruned >= MAX_PRUNE)
|
|
|
|
break;
|
|
|
|
if(checked >= MAX_CHECK)
|
|
|
|
break;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
checked++;
|
|
|
|
age = (now - fn->time);
|
|
|
|
if(f_->conf.remember > age)
|
2021-08-25 09:24:51 +08:00
|
|
|
break;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
assert(fn->node->nlookup == 1);
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-25 09:24:51 +08:00
|
|
|
/* Don't forget active directories */
|
2021-08-30 19:59:21 +08:00
|
|
|
if(fn->node->refctr > 1)
|
|
|
|
{
|
|
|
|
(*offset_)++;
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
fn->node->nlookup = 0;
|
|
|
|
unref_node(f_,fn->node);
|
|
|
|
kv_delete(f_->remembered_nodes,*offset_);
|
|
|
|
pruned++;
|
2021-08-25 09:24:51 +08:00
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
pthread_mutex_unlock(&f_->lock);
|
|
|
|
|
|
|
|
if((pruned < MAX_PRUNE) && (checked < MAX_CHECK))
|
|
|
|
*offset_ = -1;
|
|
|
|
|
|
|
|
return pruned;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MAX_PRUNE
|
|
|
|
#undef MAX_CHECK
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
sleep_100ms(void)
|
|
|
|
{
|
|
|
|
const struct timespec ms100 = {0,100 * 1000000};
|
|
|
|
|
|
|
|
nanosleep(&ms100,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fuse_prune_remembered_nodes(struct fuse *f_)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int pruned;
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
pruned = 0;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
pruned += fuse_prune_some_remembered_nodes(f_,&offset);
|
|
|
|
if(offset >= 0)
|
|
|
|
{
|
|
|
|
sleep_100ms();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pruned > 0)
|
|
|
|
remembered_nodes_sort(f_);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static struct fuse_lowlevel_ops fuse_path_ops =
|
|
|
|
{
|
2021-10-01 11:28:54 +08:00
|
|
|
.access = fuse_lib_access,
|
|
|
|
.bmap = fuse_lib_bmap,
|
|
|
|
.copy_file_range = fuse_lib_copy_file_range,
|
|
|
|
.create = fuse_lib_create,
|
|
|
|
.destroy = fuse_lib_destroy,
|
|
|
|
.fallocate = fuse_lib_fallocate,
|
|
|
|
.flock = fuse_lib_flock,
|
|
|
|
.flush = fuse_lib_flush,
|
|
|
|
.forget = fuse_lib_forget,
|
|
|
|
.forget_multi = fuse_lib_forget_multi,
|
|
|
|
.fsync = fuse_lib_fsync,
|
|
|
|
.fsyncdir = fuse_lib_fsyncdir,
|
|
|
|
.getattr = fuse_lib_getattr,
|
|
|
|
.getlk = fuse_lib_getlk,
|
|
|
|
.getxattr = fuse_lib_getxattr,
|
|
|
|
.init = fuse_lib_init,
|
|
|
|
.ioctl = fuse_lib_ioctl,
|
|
|
|
.link = fuse_lib_link,
|
|
|
|
.listxattr = fuse_lib_listxattr,
|
|
|
|
.lookup = fuse_lib_lookup,
|
|
|
|
.mkdir = fuse_lib_mkdir,
|
|
|
|
.mknod = fuse_lib_mknod,
|
|
|
|
.open = fuse_lib_open,
|
|
|
|
.opendir = fuse_lib_opendir,
|
|
|
|
.poll = fuse_lib_poll,
|
|
|
|
.read = fuse_lib_read,
|
|
|
|
.readdir = fuse_lib_readdir,
|
|
|
|
.readdir_plus = fuse_lib_readdir_plus,
|
|
|
|
.readlink = fuse_lib_readlink,
|
|
|
|
.release = fuse_lib_release,
|
|
|
|
.releasedir = fuse_lib_releasedir,
|
2023-08-12 14:50:37 +08:00
|
|
|
.removemapping = fuse_lib_removemapping,
|
2021-10-01 11:28:54 +08:00
|
|
|
.removexattr = fuse_lib_removexattr,
|
|
|
|
.rename = fuse_lib_rename,
|
|
|
|
.retrieve_reply = NULL,
|
|
|
|
.rmdir = fuse_lib_rmdir,
|
|
|
|
.setattr = fuse_lib_setattr,
|
|
|
|
.setlk = fuse_lib_setlk,
|
2023-08-12 14:50:37 +08:00
|
|
|
.setupmapping = fuse_lib_setupmapping,
|
2021-10-01 11:28:54 +08:00
|
|
|
.setxattr = fuse_lib_setxattr,
|
|
|
|
.statfs = fuse_lib_statfs,
|
|
|
|
.symlink = fuse_lib_symlink,
|
2023-08-12 14:50:37 +08:00
|
|
|
.syncfs = fuse_lib_syncfs,
|
|
|
|
.tmpfile = fuse_lib_tmpfile,
|
2021-10-01 11:28:54 +08:00
|
|
|
.unlink = fuse_lib_unlink,
|
2022-11-13 11:26:26 +08:00
|
|
|
.write = fuse_lib_write,
|
2020-11-16 08:12:33 +08:00
|
|
|
};
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
int
|
|
|
|
fuse_notify_poll(fuse_pollhandle_t *ph)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return fuse_lowlevel_notify_poll(ph);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
int
|
|
|
|
fuse_exited(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return fuse_session_exited(f->se);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse_session*
|
|
|
|
fuse_get_session(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return f->se;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
void
|
|
|
|
fuse_exit(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
f->se->exited = 1;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse_context*
|
|
|
|
fuse_get_context(void)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return &fuse_get_context_internal()->ctx;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
2021-10-01 11:28:54 +08:00
|
|
|
KEY_HELP,
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
#define FUSE_LIB_OPT(t,p,v) { t,offsetof(struct fuse_config,p),v }
|
|
|
|
|
|
|
|
static const struct fuse_opt fuse_lib_opts[] =
|
|
|
|
{
|
2021-10-01 11:28:54 +08:00
|
|
|
FUSE_OPT_KEY("-h", KEY_HELP),
|
|
|
|
FUSE_OPT_KEY("--help", KEY_HELP),
|
|
|
|
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
|
|
|
|
FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
|
|
|
|
FUSE_LIB_OPT("debug", debug,1),
|
|
|
|
FUSE_LIB_OPT("-d", debug,1),
|
2022-03-08 07:47:28 +08:00
|
|
|
FUSE_LIB_OPT("nogc", nogc,1),
|
2021-10-01 11:28:54 +08:00
|
|
|
FUSE_LIB_OPT("umask=", set_mode,1),
|
|
|
|
FUSE_LIB_OPT("umask=%o", umask,0),
|
2022-11-30 07:24:09 +08:00
|
|
|
FUSE_LIB_OPT("uid=", set_uid,1),
|
2021-10-01 11:28:54 +08:00
|
|
|
FUSE_LIB_OPT("uid=%d", uid,0),
|
2022-11-30 07:24:09 +08:00
|
|
|
FUSE_LIB_OPT("gid=", set_gid,1),
|
2021-10-01 11:28:54 +08:00
|
|
|
FUSE_LIB_OPT("gid=%d", gid,0),
|
2022-11-30 07:24:09 +08:00
|
|
|
FUSE_LIB_OPT("noforget", remember,-1),
|
|
|
|
FUSE_LIB_OPT("remember=%u", remember,0),
|
2021-10-01 11:28:54 +08:00
|
|
|
FUSE_OPT_END
|
2020-11-16 08:12:33 +08:00
|
|
|
};
|
2016-10-05 21:33:37 +08:00
|
|
|
|
|
|
|
static void fuse_lib_help(void)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
" -o umask=M set file permissions (octal)\n"
|
|
|
|
" -o uid=N set file owner\n"
|
|
|
|
" -o gid=N set file group\n"
|
|
|
|
" -o noforget never forget cached inodes\n"
|
|
|
|
" -o remember=T remember cached inodes for T seconds (0s)\n"
|
|
|
|
" -o threads=NUM number of worker threads. 0 = autodetect.\n"
|
|
|
|
" Negative values autodetect then divide by\n"
|
|
|
|
" absolute value. default = 0\n"
|
2020-11-16 08:12:33 +08:00
|
|
|
"\n");
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
fuse_lib_opt_proc(void *data,
|
|
|
|
const char *arg,
|
|
|
|
int key,
|
|
|
|
struct fuse_args *outargs)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
(void)arg; (void)outargs;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(key == KEY_HELP)
|
|
|
|
{
|
|
|
|
struct fuse_config *conf = (struct fuse_config *)data;
|
|
|
|
fuse_lib_help();
|
|
|
|
conf->help = 1;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return 1;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
int
|
|
|
|
fuse_is_lib_option(const char *opt)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
return fuse_lowlevel_is_lib_option(opt) || fuse_opt_match(fuse_lib_opts,opt);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse_fs*
|
|
|
|
fuse_fs_new(const struct fuse_operations *op,
|
2021-08-25 09:24:51 +08:00
|
|
|
size_t op_size)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_fs *fs;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(sizeof(struct fuse_operations) < op_size)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"fuse: warning: library too old,some operations may not not work\n");
|
|
|
|
op_size = sizeof(struct fuse_operations);
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
fs = (struct fuse_fs *)calloc(1,sizeof(struct fuse_fs));
|
|
|
|
if(!fs)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"fuse: failed to allocate fuse_fs object\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(op)
|
|
|
|
memcpy(&fs->op,op,op_size);
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return fs;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
node_table_init(struct node_table *t)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
t->size = NODE_TABLE_MIN_SIZE;
|
2023-04-24 11:33:48 +08:00
|
|
|
t->array = (node_t **)calloc(1,sizeof(node_t *) * t->size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(t->array == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"fuse: memory allocation failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
t->use = 0;
|
|
|
|
t->split = 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
static
|
|
|
|
struct fuse*
|
|
|
|
fuse_get_fuse_obj()
|
|
|
|
{
|
|
|
|
static struct fuse f = {0};
|
|
|
|
|
|
|
|
return &f;
|
|
|
|
}
|
|
|
|
|
2021-10-11 06:37:14 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
metrics_log_nodes_info(struct fuse *f_,
|
|
|
|
FILE *file_)
|
|
|
|
{
|
2022-03-08 07:47:28 +08:00
|
|
|
char buf[1024];
|
2023-04-24 11:33:48 +08:00
|
|
|
char time_str[64];
|
|
|
|
struct tm tm;
|
|
|
|
struct timeval tv;
|
|
|
|
uint64_t sizeof_node;
|
|
|
|
float node_usage_ratio;
|
|
|
|
uint64_t node_slab_count;
|
|
|
|
uint64_t node_avail_objs;
|
|
|
|
uint64_t node_total_alloc_mem;
|
|
|
|
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
localtime_r(&tv.tv_sec,&tm);
|
|
|
|
strftime(time_str,sizeof(time_str),"%Y-%m-%dT%H:%M:%S.000%z",&tm);
|
|
|
|
|
|
|
|
sizeof_node = sizeof(node_t);
|
|
|
|
|
|
|
|
lfmp_t *lfmp;
|
|
|
|
lfmp = node_lfmp();
|
|
|
|
lfmp_lock(lfmp);
|
|
|
|
node_slab_count = fmp_slab_count(&lfmp->fmp);
|
|
|
|
node_usage_ratio = fmp_slab_usage_ratio(&lfmp->fmp);
|
|
|
|
node_avail_objs = fmp_avail_objs(&lfmp->fmp);
|
|
|
|
node_total_alloc_mem = fmp_total_allocated_memory(&lfmp->fmp);
|
|
|
|
lfmp_unlock(lfmp);
|
2021-10-11 06:37:14 +08:00
|
|
|
|
2022-03-08 07:47:28 +08:00
|
|
|
snprintf(buf,sizeof(buf),
|
2023-04-24 11:33:48 +08:00
|
|
|
"time: %s\n"
|
2022-12-09 11:04:15 +08:00
|
|
|
"sizeof(node): %"PRIu64"\n"
|
|
|
|
"node id_table size: %"PRIu64"\n"
|
|
|
|
"node id_table usage: %"PRIu64"\n"
|
|
|
|
"node id_table total allocated memory: %"PRIu64"\n"
|
|
|
|
"node name_table size: %"PRIu64"\n"
|
|
|
|
"node name_table usage: %"PRIu64"\n"
|
|
|
|
"node name_table total allocated memory: %"PRIu64"\n"
|
|
|
|
"node memory pool slab count: %"PRIu64"\n"
|
2022-03-08 07:47:28 +08:00
|
|
|
"node memory pool usage ratio: %f\n"
|
2022-12-09 11:04:15 +08:00
|
|
|
"node memory pool avail objs: %"PRIu64"\n"
|
|
|
|
"node memory pool total allocated memory: %"PRIu64"\n"
|
2023-04-24 11:33:48 +08:00
|
|
|
"msgbuf bufsize: %"PRIu64"\n"
|
|
|
|
"msgbuf allocation count: %"PRIu64"\n"
|
|
|
|
"msgbuf available count: %"PRIu64"\n"
|
|
|
|
"msgbuf total allocated memory: %"PRIu64"\n"
|
2022-03-08 07:47:28 +08:00
|
|
|
"\n"
|
|
|
|
,
|
2023-04-24 11:33:48 +08:00
|
|
|
time_str,
|
|
|
|
sizeof_node,
|
2022-12-09 11:04:15 +08:00
|
|
|
(uint64_t)f_->id_table.size,
|
|
|
|
(uint64_t)f_->id_table.use,
|
2023-04-24 11:33:48 +08:00
|
|
|
(uint64_t)(f_->id_table.size * sizeof(node_t*)),
|
2022-12-09 11:04:15 +08:00
|
|
|
(uint64_t)f_->name_table.size,
|
|
|
|
(uint64_t)f_->name_table.use,
|
2023-04-24 11:33:48 +08:00
|
|
|
(uint64_t)(f_->name_table.size * sizeof(node_t*)),
|
|
|
|
node_slab_count,
|
|
|
|
node_usage_ratio,
|
|
|
|
node_avail_objs,
|
|
|
|
node_total_alloc_mem,
|
|
|
|
msgbuf_get_bufsize(),
|
|
|
|
msgbuf_alloc_count(),
|
|
|
|
msgbuf_avail_count(),
|
|
|
|
msgbuf_alloc_count() * msgbuf_get_bufsize()
|
2022-03-08 07:47:28 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
fputs(buf,file_);
|
2021-10-11 06:37:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
metrics_log_nodes_info_to_tmp_dir(struct fuse *f_)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
int rv;
|
2021-10-11 06:37:14 +08:00
|
|
|
FILE *file;
|
|
|
|
char filepath[256];
|
2023-04-24 11:33:48 +08:00
|
|
|
struct stat st;
|
|
|
|
char const *mode = "a";
|
|
|
|
off_t const max_size = (1024 * 1024);
|
2021-10-11 06:37:14 +08:00
|
|
|
|
|
|
|
sprintf(filepath,"/tmp/mergerfs.%d.info",getpid());
|
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
rv = lstat(filepath,&st);
|
|
|
|
if((rv == 0) && (st.st_size > max_size))
|
|
|
|
mode = "w";
|
|
|
|
|
|
|
|
file = fopen(filepath,mode);
|
2021-10-11 06:37:14 +08:00
|
|
|
if(file == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
metrics_log_nodes_info(f_,file);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
fuse_malloc_trim(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MALLOC_TRIM
|
|
|
|
malloc_trim(1024 * 1024);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
void
|
|
|
|
fuse_invalidate_all_nodes()
|
|
|
|
{
|
|
|
|
struct fuse *f = fuse_get_fuse_obj();
|
|
|
|
|
2023-09-10 04:05:17 +08:00
|
|
|
syslog(LOG_INFO,"invalidating file entries");
|
2023-04-24 11:33:48 +08:00
|
|
|
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
for(int i = 0; i < f->id_table.size; i++)
|
|
|
|
{
|
|
|
|
node_t *node;
|
|
|
|
|
|
|
|
for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
|
|
|
|
{
|
|
|
|
if(node->nodeid == FUSE_ROOT_ID)
|
|
|
|
continue;
|
|
|
|
if(node->parent->nodeid != FUSE_ROOT_ID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fuse_lowlevel_notify_inval_entry(f->se->ch,
|
|
|
|
node->parent->nodeid,
|
|
|
|
node->name,
|
|
|
|
strlen(node->name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fuse_gc()
|
|
|
|
{
|
2023-09-10 04:05:17 +08:00
|
|
|
syslog(LOG_INFO,"running thorough garbage collection");
|
2023-04-24 11:33:48 +08:00
|
|
|
node_gc();
|
|
|
|
msgbuf_gc();
|
|
|
|
fuse_malloc_trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fuse_gc1()
|
|
|
|
{
|
2023-09-10 04:05:17 +08:00
|
|
|
syslog(LOG_INFO,"running basic garbage collection");
|
2023-04-24 11:33:48 +08:00
|
|
|
node_gc1();
|
|
|
|
msgbuf_gc_10percent();
|
|
|
|
fuse_malloc_trim();
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
static
|
|
|
|
void*
|
2021-08-30 19:59:21 +08:00
|
|
|
fuse_maintenance_loop(void *fuse_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
int loops;
|
2020-08-01 11:50:47 +08:00
|
|
|
int sleep_time;
|
2021-08-30 19:59:21 +08:00
|
|
|
struct fuse *f = (struct fuse*)fuse_;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2023-08-15 06:51:41 +08:00
|
|
|
pthread_setname_np(pthread_self(),"fuse.maint");
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
loops = 0;
|
|
|
|
sleep_time = 60;
|
2020-11-16 08:12:33 +08:00
|
|
|
while(1)
|
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
if(remember_nodes(f))
|
|
|
|
fuse_prune_remembered_nodes(f);
|
|
|
|
|
2022-03-08 07:47:28 +08:00
|
|
|
if((loops % 15) == 0)
|
2023-04-24 11:33:48 +08:00
|
|
|
fuse_gc1();
|
2021-08-30 19:59:21 +08:00
|
|
|
|
2021-10-11 06:37:14 +08:00
|
|
|
if(g_LOG_METRICS)
|
|
|
|
metrics_log_nodes_info_to_tmp_dir(f);
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
loops++;
|
2020-11-16 08:12:33 +08:00
|
|
|
sleep(sleep_time);
|
|
|
|
}
|
2021-08-30 19:59:21 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return NULL;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
int
|
2021-08-30 19:59:21 +08:00
|
|
|
fuse_start_maintenance_thread(struct fuse *f_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
return fuse_start_thread(&f_->maintenance_thread,fuse_maintenance_loop,f_);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
void
|
2021-08-30 19:59:21 +08:00
|
|
|
fuse_stop_maintenance_thread(struct fuse *f_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-30 19:59:21 +08:00
|
|
|
pthread_mutex_lock(&f_->lock);
|
|
|
|
pthread_cancel(f_->maintenance_thread);
|
|
|
|
pthread_mutex_unlock(&f_->lock);
|
|
|
|
pthread_join(f_->maintenance_thread,NULL);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse*
|
|
|
|
fuse_new_common(struct fuse_chan *ch,
|
|
|
|
struct fuse_args *args,
|
|
|
|
const struct fuse_operations *op,
|
2021-08-25 09:24:51 +08:00
|
|
|
size_t op_size)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse *f;
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *root;
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_fs *fs;
|
|
|
|
struct fuse_lowlevel_ops llop = fuse_path_ops;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(fuse_create_context_key() == -1)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out;
|
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
f = fuse_get_fuse_obj();
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"fuse: failed to allocate fuse object\n");
|
|
|
|
goto out_delete_context_key;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2021-08-25 09:24:51 +08:00
|
|
|
fs = fuse_fs_new(op,op_size);
|
2020-11-16 08:12:33 +08:00
|
|
|
if(!fs)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
f->fs = fs;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
/* Oh f**k,this is ugly! */
|
|
|
|
if(!fs->op.lock)
|
|
|
|
{
|
|
|
|
llop.getlk = NULL;
|
|
|
|
llop.setlk = NULL;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(fuse_opt_parse(args,&f->conf,fuse_lib_opts,fuse_lib_opt_proc) == -1)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_free_fs;
|
|
|
|
|
2021-10-11 06:37:14 +08:00
|
|
|
g_LOG_METRICS = f->conf.debug;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
f->se = fuse_lowlevel_new_common(args,&llop,sizeof(llop),f);
|
|
|
|
if(f->se == NULL)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_free_fs;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
fuse_session_add_chan(f->se,ch);
|
2020-08-01 11:50:47 +08:00
|
|
|
|
|
|
|
/* Trace topmost layer by default */
|
|
|
|
srand(time(NULL));
|
2021-10-01 11:28:54 +08:00
|
|
|
f->nodeid_gen.nodeid = FUSE_ROOT_ID;
|
|
|
|
f->nodeid_gen.generation = rand64();
|
2020-11-16 08:12:33 +08:00
|
|
|
if(node_table_init(&f->name_table) == -1)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_free_session;
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(node_table_init(&f->id_table) == -1)
|
2020-08-01 11:50:47 +08:00
|
|
|
goto out_free_name_table;
|
|
|
|
|
|
|
|
fuse_mutex_init(&f->lock);
|
|
|
|
|
2021-08-30 19:59:21 +08:00
|
|
|
kv_init(f->remembered_nodes);
|
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
root = node_alloc();
|
2020-11-16 08:12:33 +08:00
|
|
|
if(root == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"fuse: memory allocation failed\n");
|
|
|
|
goto out_free_id_table;
|
|
|
|
}
|
2020-08-01 11:50:47 +08:00
|
|
|
|
2021-10-29 11:01:47 +08:00
|
|
|
root->name = filename_strdup(f,"/");
|
2020-08-01 11:50:47 +08:00
|
|
|
|
|
|
|
root->parent = NULL;
|
|
|
|
root->nodeid = FUSE_ROOT_ID;
|
|
|
|
inc_nlookup(root);
|
2020-11-16 08:12:33 +08:00
|
|
|
hash_id(f,root);
|
2020-08-01 11:50:47 +08:00
|
|
|
|
|
|
|
return f;
|
|
|
|
|
|
|
|
out_free_id_table:
|
|
|
|
free(f->id_table.array);
|
|
|
|
out_free_name_table:
|
|
|
|
free(f->name_table.array);
|
|
|
|
out_free_session:
|
|
|
|
fuse_session_destroy(f->se);
|
|
|
|
out_free_fs:
|
|
|
|
/* Horrible compatibility hack to stop the destructor from being
|
|
|
|
called on the filesystem without init being called first */
|
|
|
|
fs->op.destroy = NULL;
|
2022-11-13 11:26:26 +08:00
|
|
|
free(f->fs);
|
2020-08-01 11:50:47 +08:00
|
|
|
out_free:
|
2023-04-24 11:33:48 +08:00
|
|
|
// free(f);
|
2020-08-01 11:50:47 +08:00
|
|
|
out_delete_context_key:
|
|
|
|
fuse_delete_context_key();
|
|
|
|
out:
|
|
|
|
return NULL;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
struct fuse*
|
|
|
|
fuse_new(struct fuse_chan *ch,
|
|
|
|
struct fuse_args *args,
|
|
|
|
const struct fuse_operations *op,
|
2021-08-25 09:24:51 +08:00
|
|
|
size_t op_size)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2021-08-25 09:24:51 +08:00
|
|
|
return fuse_new_common(ch,args,op,op_size);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
void
|
|
|
|
fuse_destroy(struct fuse *f)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
size_t i;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
if(f->fs)
|
|
|
|
{
|
|
|
|
struct fuse_context_i *c = fuse_get_context_internal();
|
|
|
|
|
|
|
|
memset(c,0,sizeof(*c));
|
|
|
|
c->ctx.fuse = f;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(i = 0; i < f->id_table.size; i++)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
|
|
|
|
{
|
2023-04-05 12:28:34 +08:00
|
|
|
if(!node->hidden_fh)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
f->fs->op.free_hide(node->hidden_fh);
|
|
|
|
node->hidden_fh = 0;
|
2020-11-16 08:12:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(i = 0; i < f->id_table.size; i++)
|
|
|
|
{
|
2023-04-24 11:33:48 +08:00
|
|
|
node_t *node;
|
|
|
|
node_t *next;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-11-16 08:12:33 +08:00
|
|
|
for(node = f->id_table.array[i]; node != NULL; node = next)
|
2020-08-01 11:50:47 +08:00
|
|
|
{
|
2020-11-16 08:12:33 +08:00
|
|
|
next = node->id_next;
|
|
|
|
free_node(f,node);
|
|
|
|
f->id_table.use--;
|
2020-08-01 11:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 08:12:33 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
free(f->id_table.array);
|
|
|
|
free(f->name_table.array);
|
|
|
|
pthread_mutex_destroy(&f->lock);
|
|
|
|
fuse_session_destroy(f->se);
|
2021-08-30 19:59:21 +08:00
|
|
|
kv_destroy(f->remembered_nodes);
|
2020-08-01 11:50:47 +08:00
|
|
|
fuse_delete_context_key();
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 06:37:14 +08:00
|
|
|
void
|
2021-10-12 10:47:59 +08:00
|
|
|
fuse_log_metrics_set(int log_)
|
2021-10-11 06:37:14 +08:00
|
|
|
{
|
|
|
|
g_LOG_METRICS = log_;
|
|
|
|
}
|
2021-10-12 10:47:59 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
fuse_log_metrics_get(void)
|
|
|
|
{
|
|
|
|
return g_LOG_METRICS;
|
|
|
|
}
|