2010-08-13 03:01:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-07-08 22:53:26 +02:00
|
|
|
#define LOG_TAG "sdcard"
|
|
|
|
|
2016-07-12 21:13:05 +02:00
|
|
|
#include "fuse.h"
|
2010-08-13 03:21:12 +02:00
|
|
|
|
2016-07-20 21:19:44 +02:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
|
2016-04-20 03:33:08 +02:00
|
|
|
/* FUSE_CANONICAL_PATH is not currently upstreamed */
|
|
|
|
#define FUSE_CANONICAL_PATH 2016
|
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
#define FUSE_UNKNOWN_INO 0xffffffff
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
/* Pseudo-error constant used to indicate that no fuse status is needed
|
|
|
|
* or that a reply has already been written. */
|
|
|
|
#define NO_STATUS 1
|
|
|
|
|
|
|
|
static inline void *id_to_ptr(__u64 nid)
|
|
|
|
{
|
|
|
|
return (void *) (uintptr_t) nid;
|
|
|
|
}
|
2011-01-18 06:06:26 +01:00
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static inline __u64 ptr_to_id(void *ptr)
|
|
|
|
{
|
|
|
|
return (__u64) (uintptr_t) ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void acquire_node_locked(struct node* node)
|
|
|
|
{
|
|
|
|
node->refcount++;
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "ACQUIRE " << std::hex << node << std::dec
|
|
|
|
<< " (" << node->name << ") rc=" << node->refcount;
|
2012-05-26 23:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_node_from_parent_locked(struct node* node);
|
|
|
|
|
|
|
|
static void release_node_locked(struct node* node)
|
|
|
|
{
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "RELEASE " << std::hex << node << std::dec
|
|
|
|
<< " (" << node->name << ") rc=" << node->refcount;
|
2012-05-26 23:32:54 +02:00
|
|
|
if (node->refcount > 0) {
|
|
|
|
node->refcount--;
|
|
|
|
if (!node->refcount) {
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "DESTROY " << std::hex << node << std::dec << " (" << node->name << ")";
|
2012-05-26 23:32:54 +02:00
|
|
|
remove_node_from_parent_locked(node);
|
|
|
|
|
2016-07-13 22:52:24 +02:00
|
|
|
/* TODO: remove debugging - poison memory */
|
2012-05-26 23:32:54 +02:00
|
|
|
memset(node->name, 0xef, node->namelen);
|
|
|
|
free(node->name);
|
|
|
|
free(node->actual_name);
|
|
|
|
memset(node, 0xfc, sizeof(*node));
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
} else {
|
2016-07-26 21:22:31 +02:00
|
|
|
LOG(ERROR) << std::hex << node << std::dec << " refcount=0";
|
2012-05-26 23:32:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_node_to_parent_locked(struct node *node, struct node *parent) {
|
|
|
|
node->parent = parent;
|
|
|
|
node->next = parent->child;
|
|
|
|
parent->child = node;
|
|
|
|
acquire_node_locked(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_node_from_parent_locked(struct node* node)
|
|
|
|
{
|
|
|
|
if (node->parent) {
|
|
|
|
if (node->parent->child == node) {
|
|
|
|
node->parent->child = node->parent->child->next;
|
|
|
|
} else {
|
|
|
|
struct node *node2;
|
|
|
|
node2 = node->parent->child;
|
|
|
|
while (node2->next != node)
|
|
|
|
node2 = node2->next;
|
|
|
|
node2->next = node->next;
|
|
|
|
}
|
|
|
|
release_node_locked(node->parent);
|
|
|
|
node->parent = NULL;
|
|
|
|
node->next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets the absolute path to a node into the provided buffer.
|
|
|
|
*
|
|
|
|
* Populates 'buf' with the path and returns the length of the path on success,
|
|
|
|
* or returns -1 if the path is too long for the provided buffer.
|
2010-09-22 02:14:31 +02:00
|
|
|
*/
|
2013-08-13 05:23:49 +02:00
|
|
|
static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
|
|
|
|
const char* name;
|
|
|
|
size_t namelen;
|
|
|
|
if (node->graft_path) {
|
|
|
|
name = node->graft_path;
|
|
|
|
namelen = node->graft_pathlen;
|
|
|
|
} else if (node->actual_name) {
|
|
|
|
name = node->actual_name;
|
|
|
|
namelen = node->namelen;
|
|
|
|
} else {
|
|
|
|
name = node->name;
|
|
|
|
namelen = node->namelen;
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
if (bufsize < namelen + 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t pathlen = 0;
|
2013-08-13 05:23:49 +02:00
|
|
|
if (node->parent && node->graft_path == NULL) {
|
2016-04-13 01:30:28 +02:00
|
|
|
pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
|
2012-05-26 23:32:54 +02:00
|
|
|
if (pathlen < 0) {
|
|
|
|
return -1;
|
2011-01-23 23:46:30 +01:00
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
buf[pathlen++] = '/';
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
|
|
|
|
return pathlen + namelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finds the absolute path of a file within a given directory.
|
|
|
|
* Performs a case-insensitive search for the file and sets the buffer to the path
|
|
|
|
* of the first matching file. If 'search' is zero or if no match is found, sets
|
|
|
|
* the buffer to the path that the file would have, assuming the name were case-sensitive.
|
|
|
|
*
|
|
|
|
* Populates 'buf' with the path and returns the actual name (within 'buf') on success,
|
|
|
|
* or returns NULL if the path is too long for the provided buffer.
|
|
|
|
*/
|
|
|
|
static char* find_file_within(const char* path, const char* name,
|
|
|
|
char* buf, size_t bufsize, int search)
|
|
|
|
{
|
|
|
|
size_t pathlen = strlen(path);
|
|
|
|
size_t namelen = strlen(name);
|
|
|
|
size_t childlen = pathlen + namelen + 1;
|
|
|
|
char* actual;
|
|
|
|
|
|
|
|
if (bufsize <= childlen) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf, path, pathlen);
|
|
|
|
buf[pathlen] = '/';
|
|
|
|
actual = buf + pathlen + 1;
|
|
|
|
memcpy(actual, name, namelen + 1);
|
|
|
|
|
|
|
|
if (search && access(buf, F_OK)) {
|
2011-01-23 23:46:30 +01:00
|
|
|
struct dirent* entry;
|
2012-05-26 23:32:54 +02:00
|
|
|
DIR* dir = opendir(path);
|
2011-01-23 23:46:30 +01:00
|
|
|
if (!dir) {
|
2016-07-26 21:22:31 +02:00
|
|
|
PLOG(ERROR) << "opendir(" << path << ") failed";
|
2012-05-26 23:32:54 +02:00
|
|
|
return actual;
|
2011-01-23 23:46:30 +01:00
|
|
|
}
|
|
|
|
while ((entry = readdir(dir))) {
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!strcasecmp(entry->d_name, name)) {
|
|
|
|
/* we have a match - replace the name, don't need to copy the null again */
|
|
|
|
memcpy(actual, entry->d_name, namelen);
|
2011-01-23 23:46:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
return actual;
|
2011-01-23 23:46:30 +01:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:13:52 +02:00
|
|
|
static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
|
|
|
|
const struct stat *s, const struct node* node) {
|
2015-01-13 19:21:10 +01:00
|
|
|
attr->ino = node->ino;
|
2010-08-13 03:01:08 +02:00
|
|
|
attr->size = s->st_size;
|
|
|
|
attr->blocks = s->st_blocks;
|
2014-11-10 20:03:38 +01:00
|
|
|
attr->atime = s->st_atim.tv_sec;
|
|
|
|
attr->mtime = s->st_mtim.tv_sec;
|
|
|
|
attr->ctime = s->st_ctim.tv_sec;
|
|
|
|
attr->atimensec = s->st_atim.tv_nsec;
|
|
|
|
attr->mtimensec = s->st_mtim.tv_nsec;
|
|
|
|
attr->ctimensec = s->st_ctim.tv_nsec;
|
2010-08-13 03:01:08 +02:00
|
|
|
attr->mode = s->st_mode;
|
|
|
|
attr->nlink = s->st_nlink;
|
|
|
|
|
2013-07-04 02:08:29 +02:00
|
|
|
attr->uid = node->uid;
|
2015-07-16 18:13:52 +02:00
|
|
|
|
|
|
|
if (fuse->gid == AID_SDCARD_RW) {
|
|
|
|
/* As an optimization, certain trusted system components only run
|
|
|
|
* as owner but operate across all users. Since we're now handing
|
|
|
|
* out the sdcard_rw GID only to trusted apps, we're okay relaxing
|
|
|
|
* the user boundary enforcement for the default view. The UIDs
|
|
|
|
* assigned to app directories are still multiuser aware. */
|
|
|
|
attr->gid = AID_SDCARD_RW;
|
|
|
|
} else {
|
|
|
|
attr->gid = multiuser_get_uid(node->userid, fuse->gid);
|
|
|
|
}
|
2010-08-13 03:21:12 +02:00
|
|
|
|
2015-07-28 19:49:41 +02:00
|
|
|
int visible_mode = 0775 & ~fuse->mask;
|
|
|
|
if (node->perm == PERM_PRE_ROOT) {
|
|
|
|
/* Top of multi-user view should always be visible to ensure
|
|
|
|
* secondary users can traverse inside. */
|
|
|
|
visible_mode = 0711;
|
|
|
|
} else if (node->under_android) {
|
|
|
|
/* Block "other" access to Android directories, since only apps
|
|
|
|
* belonging to a specific user should be in there; we still
|
|
|
|
* leave +x open for the default view. */
|
|
|
|
if (fuse->gid == AID_SDCARD_RW) {
|
|
|
|
visible_mode = visible_mode & ~0006;
|
|
|
|
} else {
|
|
|
|
visible_mode = visible_mode & ~0007;
|
|
|
|
}
|
2015-07-16 18:13:52 +02:00
|
|
|
}
|
2013-07-04 02:08:29 +02:00
|
|
|
int owner_mode = s->st_mode & 0700;
|
2015-07-16 18:13:52 +02:00
|
|
|
int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
|
2013-07-04 02:08:29 +02:00
|
|
|
attr->mode = (attr->mode & S_IFMT) | filtered_mode;
|
|
|
|
}
|
|
|
|
|
2013-09-12 18:44:48 +02:00
|
|
|
static int touch(char* path, mode_t mode) {
|
2016-10-06 21:51:20 +02:00
|
|
|
int fd = TEMP_FAILURE_RETRY(open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
|
|
|
|
mode));
|
2013-09-12 18:44:48 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2016-07-26 21:22:31 +02:00
|
|
|
PLOG(ERROR) << "open(" << path << ") failed";
|
2013-09-12 18:44:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-04 02:08:29 +02:00
|
|
|
static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
|
|
|
|
struct node *node) {
|
2013-08-13 05:23:49 +02:00
|
|
|
appid_t appid;
|
2013-07-04 02:08:29 +02:00
|
|
|
|
|
|
|
/* By default, each node inherits from its parent */
|
|
|
|
node->perm = PERM_INHERIT;
|
|
|
|
node->userid = parent->userid;
|
|
|
|
node->uid = parent->uid;
|
2015-07-28 19:49:41 +02:00
|
|
|
node->under_android = parent->under_android;
|
2013-07-04 02:08:29 +02:00
|
|
|
|
|
|
|
/* Derive custom permissions based on parent and current node */
|
|
|
|
switch (parent->perm) {
|
|
|
|
case PERM_INHERIT:
|
|
|
|
/* Already inherited above */
|
|
|
|
break;
|
2015-06-23 23:30:37 +02:00
|
|
|
case PERM_PRE_ROOT:
|
2013-08-13 05:23:49 +02:00
|
|
|
/* Legacy internal layout places users at top level */
|
|
|
|
node->perm = PERM_ROOT;
|
|
|
|
node->userid = strtoul(node->name, NULL, 10);
|
|
|
|
break;
|
2013-07-04 02:08:29 +02:00
|
|
|
case PERM_ROOT:
|
2013-08-13 05:23:49 +02:00
|
|
|
/* Assume masked off by default. */
|
2013-09-12 18:44:48 +02:00
|
|
|
if (!strcasecmp(node->name, "Android")) {
|
2013-07-04 02:08:29 +02:00
|
|
|
/* App-specific directories inside; let anyone traverse */
|
|
|
|
node->perm = PERM_ANDROID;
|
2015-07-28 19:49:41 +02:00
|
|
|
node->under_android = true;
|
2013-07-04 02:08:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERM_ANDROID:
|
2013-09-12 18:44:48 +02:00
|
|
|
if (!strcasecmp(node->name, "data")) {
|
2013-07-04 02:08:29 +02:00
|
|
|
/* App-specific directories inside; let anyone traverse */
|
|
|
|
node->perm = PERM_ANDROID_DATA;
|
2013-09-12 18:44:48 +02:00
|
|
|
} else if (!strcasecmp(node->name, "obb")) {
|
2013-07-04 02:08:29 +02:00
|
|
|
/* App-specific directories inside; let anyone traverse */
|
|
|
|
node->perm = PERM_ANDROID_OBB;
|
2013-08-13 05:23:49 +02:00
|
|
|
/* Single OBB directory is always shared */
|
2015-07-16 18:13:52 +02:00
|
|
|
node->graft_path = fuse->global->obb_path;
|
|
|
|
node->graft_pathlen = strlen(fuse->global->obb_path);
|
2014-05-31 00:38:31 +02:00
|
|
|
} else if (!strcasecmp(node->name, "media")) {
|
|
|
|
/* App-specific directories inside; let anyone traverse */
|
|
|
|
node->perm = PERM_ANDROID_MEDIA;
|
2013-07-04 02:08:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERM_ANDROID_DATA:
|
|
|
|
case PERM_ANDROID_OBB:
|
2014-05-31 00:38:31 +02:00
|
|
|
case PERM_ANDROID_MEDIA:
|
2016-07-19 18:10:26 +02:00
|
|
|
const auto& iter = fuse->global->package_to_appid->find(node->name);
|
|
|
|
if (iter != fuse->global->package_to_appid->end()) {
|
|
|
|
appid = iter->second;
|
2013-08-13 05:23:49 +02:00
|
|
|
node->uid = multiuser_get_uid(parent->userid, appid);
|
2013-07-04 02:08:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-08-30 19:26:15 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 21:13:05 +02:00
|
|
|
void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
|
2015-12-14 19:02:01 +01:00
|
|
|
struct node *node;
|
|
|
|
for (node = parent->child; node; node = node->next) {
|
|
|
|
derive_permissions_locked(fuse, parent, node);
|
|
|
|
if (node->child) {
|
|
|
|
derive_permissions_recursive_locked(fuse, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:23:49 +02:00
|
|
|
/* Kernel has already enforced everything we returned through
|
|
|
|
* derive_permissions_locked(), so this is used to lock down access
|
|
|
|
* even further, such as enforcing that apps hold sdcard_rw. */
|
|
|
|
static bool check_caller_access_to_name(struct fuse* fuse,
|
|
|
|
const struct fuse_in_header *hdr, const struct node* parent_node,
|
2015-06-23 23:30:37 +02:00
|
|
|
const char* name, int mode) {
|
2013-08-13 05:23:49 +02:00
|
|
|
/* Always block security-sensitive files at root */
|
|
|
|
if (parent_node && parent_node->perm == PERM_ROOT) {
|
2013-09-12 18:44:48 +02:00
|
|
|
if (!strcasecmp(name, "autorun.inf")
|
|
|
|
|| !strcasecmp(name, ".android_secure")
|
|
|
|
|| !strcasecmp(name, "android_secure")) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 18:44:48 +02:00
|
|
|
/* Root always has access; access for any other UIDs should always
|
|
|
|
* be controlled through packages.list. */
|
|
|
|
if (hdr->uid == 0) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No extra permissions to enforce */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_caller_access_to_node(struct fuse* fuse,
|
2015-06-23 23:30:37 +02:00
|
|
|
const struct fuse_in_header *hdr, const struct node* node, int mode) {
|
|
|
|
return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
|
2013-08-13 05:23:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node *create_node_locked(struct fuse* fuse,
|
|
|
|
struct node *parent, const char *name, const char* actual_name)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
|
|
|
struct node *node;
|
2012-05-26 23:32:54 +02:00
|
|
|
size_t namelen = strlen(name);
|
2010-08-13 03:01:08 +02:00
|
|
|
|
2015-01-13 19:21:10 +01:00
|
|
|
// Detect overflows in the inode counter. "4 billion nodes should be enough
|
|
|
|
// for everybody".
|
2015-07-16 18:13:52 +02:00
|
|
|
if (fuse->global->inode_ctr == 0) {
|
2016-07-26 21:22:31 +02:00
|
|
|
LOG(ERROR) << "No more inode numbers available";
|
2015-01-13 19:21:10 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:52:24 +02:00
|
|
|
node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!node) {
|
|
|
|
return NULL;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2016-07-13 22:52:24 +02:00
|
|
|
node->name = static_cast<char*>(malloc(namelen + 1));
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!node->name) {
|
2010-10-14 20:04:26 +02:00
|
|
|
free(node);
|
2012-05-26 23:32:54 +02:00
|
|
|
return NULL;
|
2010-10-14 20:04:26 +02:00
|
|
|
}
|
2010-08-13 03:01:08 +02:00
|
|
|
memcpy(node->name, name, namelen + 1);
|
2012-05-26 23:32:54 +02:00
|
|
|
if (strcmp(name, actual_name)) {
|
2016-07-13 22:52:24 +02:00
|
|
|
node->actual_name = static_cast<char*>(malloc(namelen + 1));
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!node->actual_name) {
|
|
|
|
free(node->name);
|
|
|
|
free(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(node->actual_name, actual_name, namelen + 1);
|
|
|
|
}
|
2010-08-13 03:01:08 +02:00
|
|
|
node->namelen = namelen;
|
2012-05-26 23:32:54 +02:00
|
|
|
node->nid = ptr_to_id(node);
|
2015-07-16 18:13:52 +02:00
|
|
|
node->ino = fuse->global->inode_ctr++;
|
|
|
|
node->gen = fuse->global->next_generation++;
|
2013-07-04 02:08:29 +02:00
|
|
|
|
2014-07-16 08:34:30 +02:00
|
|
|
node->deleted = false;
|
|
|
|
|
2013-07-04 02:08:29 +02:00
|
|
|
derive_permissions_locked(fuse, parent, node);
|
2012-05-26 23:32:54 +02:00
|
|
|
acquire_node_locked(node);
|
|
|
|
add_node_to_parent_locked(node, parent);
|
2010-08-13 03:01:08 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int rename_node_locked(struct node *node, const char *name,
|
|
|
|
const char* actual_name)
|
2010-10-14 20:04:26 +02:00
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
size_t namelen = strlen(name);
|
|
|
|
int need_actual_name = strcmp(name, actual_name);
|
|
|
|
|
|
|
|
/* make the storage bigger without actually changing the name
|
|
|
|
* in case an error occurs part way */
|
|
|
|
if (namelen > node->namelen) {
|
2016-07-13 22:52:24 +02:00
|
|
|
char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!new_name) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
node->name = new_name;
|
|
|
|
if (need_actual_name && node->actual_name) {
|
2016-07-13 22:52:24 +02:00
|
|
|
char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!new_actual_name) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
node->actual_name = new_actual_name;
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 03:01:08 +02:00
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
/* update the name, taking care to allocate storage before overwriting the old name */
|
|
|
|
if (need_actual_name) {
|
|
|
|
if (!node->actual_name) {
|
2016-07-13 22:52:24 +02:00
|
|
|
node->actual_name = static_cast<char*>(malloc(namelen + 1));
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!node->actual_name) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(node->actual_name, actual_name, namelen + 1);
|
|
|
|
} else {
|
|
|
|
free(node->actual_name);
|
|
|
|
node->actual_name = NULL;
|
|
|
|
}
|
|
|
|
memcpy(node->name, name, namelen + 1);
|
|
|
|
node->namelen = namelen;
|
|
|
|
return 0;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
if (nid == FUSE_ROOT_ID) {
|
2015-07-16 18:13:52 +02:00
|
|
|
return &fuse->global->root;
|
2010-08-13 03:01:08 +02:00
|
|
|
} else {
|
2016-07-13 22:52:24 +02:00
|
|
|
return static_cast<struct node*>(id_to_ptr(nid));
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
|
|
|
|
char* buf, size_t bufsize)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* node = lookup_node_by_id_locked(fuse, nid);
|
|
|
|
if (node && get_node_path_locked(node, buf, bufsize) < 0) {
|
|
|
|
node = NULL;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
return node;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
|
|
|
for (node = node->child; node; node = node->next) {
|
2012-05-26 23:32:54 +02:00
|
|
|
/* use exact string comparison, nodes that differ by case
|
|
|
|
* must be considered distinct even if they refer to the same
|
|
|
|
* underlying file as otherwise operations such as "mv x x"
|
|
|
|
* will not work because the source and target nodes are the same. */
|
2014-07-16 08:34:30 +02:00
|
|
|
if (!strcmp(name, node->name) && !node->deleted) {
|
2010-10-14 20:04:26 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static struct node* acquire_or_create_child_locked(
|
|
|
|
struct fuse* fuse, struct node* parent,
|
|
|
|
const char* name, const char* actual_name)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* child = lookup_child_by_name_locked(parent, name);
|
|
|
|
if (child) {
|
|
|
|
acquire_node_locked(child);
|
|
|
|
} else {
|
|
|
|
child = create_node_locked(fuse, parent, name, actual_name);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
return child;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static void fuse_status(struct fuse *fuse, __u64 unique, int err)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
|
|
|
struct fuse_out_header hdr;
|
|
|
|
hdr.len = sizeof(hdr);
|
|
|
|
hdr.error = err;
|
|
|
|
hdr.unique = unique;
|
2016-10-06 21:51:20 +02:00
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(write(fuse->fd, &hdr, sizeof(hdr)));
|
|
|
|
if (ret == -1) {
|
|
|
|
PLOG(ERROR) << "*** STATUS FAILED ***";
|
|
|
|
} else if (static_cast<size_t>(ret) != sizeof(hdr)) {
|
|
|
|
LOG(ERROR) << "*** STATUS FAILED: written " << ret << " expected "
|
|
|
|
<< sizeof(hdr) << " ***";
|
|
|
|
}
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
|
|
|
struct fuse_out_header hdr;
|
|
|
|
hdr.len = len + sizeof(hdr);
|
|
|
|
hdr.error = 0;
|
|
|
|
hdr.unique = unique;
|
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
struct iovec vec[2];
|
2010-08-13 03:01:08 +02:00
|
|
|
vec[0].iov_base = &hdr;
|
|
|
|
vec[0].iov_len = sizeof(hdr);
|
|
|
|
vec[1].iov_base = data;
|
|
|
|
vec[1].iov_len = len;
|
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(writev(fuse->fd, vec, 2));
|
|
|
|
if (ret == -1) {
|
2016-07-26 21:22:31 +02:00
|
|
|
PLOG(ERROR) << "*** REPLY FAILED ***";
|
2016-10-06 21:51:20 +02:00
|
|
|
} else if (static_cast<size_t>(ret) != sizeof(hdr) + len) {
|
|
|
|
LOG(ERROR) << "*** REPLY FAILED: written " << ret << " expected "
|
|
|
|
<< sizeof(hdr) + len << " ***";
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
|
|
|
|
struct node* parent, const char* name, const char* actual_name,
|
|
|
|
const char* path)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
2012-05-26 02:24:17 +02:00
|
|
|
struct node* node;
|
2012-05-26 23:32:54 +02:00
|
|
|
struct fuse_entry_out out;
|
|
|
|
struct stat s;
|
2010-08-13 03:01:08 +02:00
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
if (lstat(path, &s) == -1) {
|
2013-09-12 18:44:48 +02:00
|
|
|
return -errno;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
|
|
|
|
if (!node) {
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
memset(&out, 0, sizeof(out));
|
2015-07-16 18:13:52 +02:00
|
|
|
attr_from_stat(fuse, &out.attr, &s, node);
|
2012-05-26 23:32:54 +02:00
|
|
|
out.attr_valid = 10;
|
|
|
|
out.entry_valid = 10;
|
2010-08-13 03:01:08 +02:00
|
|
|
out.nodeid = node->nid;
|
|
|
|
out.generation = node->gen;
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2010-08-13 03:01:08 +02:00
|
|
|
fuse_reply(fuse, unique, &out, sizeof(out));
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
|
2013-07-04 02:08:29 +02:00
|
|
|
static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
|
2012-05-26 23:32:54 +02:00
|
|
|
const char* path)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct fuse_attr_out out;
|
|
|
|
struct stat s;
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
if (lstat(path, &s) == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
memset(&out, 0, sizeof(out));
|
2015-07-16 18:13:52 +02:00
|
|
|
attr_from_stat(fuse, &out.attr, &s, node);
|
2012-05-26 23:32:54 +02:00
|
|
|
out.attr_valid = 10;
|
|
|
|
fuse_reply(fuse, unique, &out, sizeof(out));
|
|
|
|
return NO_STATUS;
|
|
|
|
}
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2015-07-16 18:13:52 +02:00
|
|
|
static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
|
|
|
|
const __u64 child, const char* name) {
|
|
|
|
struct fuse_out_header hdr;
|
|
|
|
struct fuse_notify_delete_out data;
|
|
|
|
size_t namelen = strlen(name);
|
|
|
|
hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
|
|
|
|
hdr.error = FUSE_NOTIFY_DELETE;
|
|
|
|
hdr.unique = 0;
|
|
|
|
|
|
|
|
data.parent = parent;
|
|
|
|
data.child = child;
|
|
|
|
data.namelen = namelen;
|
|
|
|
data.padding = 0;
|
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
struct iovec vec[3];
|
2015-07-16 18:13:52 +02:00
|
|
|
vec[0].iov_base = &hdr;
|
|
|
|
vec[0].iov_len = sizeof(hdr);
|
|
|
|
vec[1].iov_base = &data;
|
|
|
|
vec[1].iov_len = sizeof(data);
|
|
|
|
vec[2].iov_base = (void*) name;
|
|
|
|
vec[2].iov_len = namelen + 1;
|
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(writev(fuse->fd, vec, 3));
|
2015-07-16 18:13:52 +02:00
|
|
|
/* Ignore ENOENT, since other views may not have seen the entry */
|
2016-10-06 21:51:20 +02:00
|
|
|
if (ret == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
PLOG(ERROR) << "*** NOTIFY FAILED ***";
|
|
|
|
}
|
|
|
|
} else if (static_cast<size_t>(ret) != sizeof(hdr) + sizeof(data) + namelen + 1) {
|
|
|
|
LOG(ERROR) << "*** NOTIFY FAILED: written " << ret << " expected "
|
|
|
|
<< sizeof(hdr) + sizeof(data) + namelen + 1 << " ***";
|
2015-07-16 18:13:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
|
|
|
|
const struct fuse_in_header *hdr, const char* name)
|
|
|
|
{
|
|
|
|
struct node* parent_node;
|
|
|
|
char parent_path[PATH_MAX];
|
|
|
|
char child_path[PATH_MAX];
|
|
|
|
const char* actual_name;
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
|
|
|
|
parent_path, sizeof(parent_path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid
|
|
|
|
<< " (" << (parent_node ? parent_node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
|
|
|
|
if (!parent_node || !(actual_name = find_file_within(parent_path, name,
|
|
|
|
child_path, sizeof(child_path), 1))) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* node;
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
node = lookup_node_by_id_locked(fuse, hdr->nodeid);
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup
|
|
|
|
<< " @ " << std::hex << hdr->nodeid
|
|
|
|
<< " (" << (node ? node->name : "?") << ")";
|
2012-05-26 23:32:54 +02:00
|
|
|
if (node) {
|
|
|
|
__u64 n = req->nlookup;
|
2016-04-26 17:42:08 +02:00
|
|
|
while (n) {
|
|
|
|
n--;
|
2012-05-26 23:32:54 +02:00
|
|
|
release_node_locked(node);
|
|
|
|
}
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS; /* no reply */
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* node;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags
|
2016-07-26 21:22:31 +02:00
|
|
|
<< " fh=" << std::hex << req->fh << " @ " << hdr->nodeid << std::dec
|
2016-07-20 21:19:44 +02:00
|
|
|
<< " (" << (node ? node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 02:24:17 +02:00
|
|
|
|
|
|
|
if (!node) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -ENOENT;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2013-07-04 02:08:29 +02:00
|
|
|
return fuse_reply_attr(fuse, hdr->unique, node, path);
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* node;
|
|
|
|
char path[PATH_MAX];
|
2012-05-26 02:24:17 +02:00
|
|
|
struct timespec times[2];
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh
|
2016-07-26 21:22:31 +02:00
|
|
|
<< " valid=" << std::hex << req->valid << " @ " << hdr->nodeid << std::dec
|
2016-07-20 21:19:44 +02:00
|
|
|
<< " (" << (node ? node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
if (!node) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -ENOENT;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2014-12-10 19:44:20 +01:00
|
|
|
|
|
|
|
if (!(req->valid & FATTR_FH) &&
|
2015-06-23 23:30:37 +02:00
|
|
|
!check_caller_access_to_node(fuse, hdr, node, W_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
/* XXX: incomplete implementation on purpose.
|
|
|
|
* chmod/chown should NEVER be implemented.*/
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
if ((req->valid & FATTR_SIZE) && TEMP_FAILURE_RETRY(truncate64(path, req->size)) == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
|
|
|
|
* are both set, then set it to the current time. Else, set it to the
|
|
|
|
* time specified in the request. Same goes for mtime. Use utimensat(2)
|
|
|
|
* as it allows ATIME and MTIME to be changed independently, and has
|
|
|
|
* nanosecond resolution which fuse also has.
|
|
|
|
*/
|
|
|
|
if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
|
|
|
|
times[0].tv_nsec = UTIME_OMIT;
|
|
|
|
times[1].tv_nsec = UTIME_OMIT;
|
|
|
|
if (req->valid & FATTR_ATIME) {
|
|
|
|
if (req->valid & FATTR_ATIME_NOW) {
|
|
|
|
times[0].tv_nsec = UTIME_NOW;
|
|
|
|
} else {
|
|
|
|
times[0].tv_sec = req->atime;
|
|
|
|
times[0].tv_nsec = req->atimensec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req->valid & FATTR_MTIME) {
|
|
|
|
if (req->valid & FATTR_MTIME_NOW) {
|
|
|
|
times[1].tv_nsec = UTIME_NOW;
|
|
|
|
} else {
|
|
|
|
times[1].tv_sec = req->mtime;
|
|
|
|
times[1].tv_nsec = req->mtimensec;
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] Calling utimensat on " << path
|
|
|
|
<< " with atime " << times[0].tv_sec << ", mtime=" << times[1].tv_sec;
|
2012-05-26 23:32:54 +02:00
|
|
|
if (utimensat(-1, path, times, 0) < 0) {
|
|
|
|
return -errno;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2013-07-04 02:08:29 +02:00
|
|
|
return fuse_reply_attr(fuse, hdr->unique, node, path);
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* parent_node;
|
|
|
|
char parent_path[PATH_MAX];
|
|
|
|
char child_path[PATH_MAX];
|
|
|
|
const char* actual_name;
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
|
|
|
|
parent_path, sizeof(parent_path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode
|
|
|
|
<< " @ " << std::hex << hdr->nodeid
|
|
|
|
<< " (" << (parent_node ? parent_node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
|
|
|
|
if (!parent_node || !(actual_name = find_file_within(parent_path, name,
|
|
|
|
child_path, sizeof(child_path), 1))) {
|
|
|
|
return -ENOENT;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2012-05-26 02:24:17 +02:00
|
|
|
__u32 mode = (req->mode & (~0777)) | 0664;
|
2016-10-06 21:51:20 +02:00
|
|
|
if (mknod(child_path, mode, req->rdev) == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* parent_node;
|
|
|
|
char parent_path[PATH_MAX];
|
|
|
|
char child_path[PATH_MAX];
|
|
|
|
const char* actual_name;
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
|
|
|
|
parent_path, sizeof(parent_path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode
|
|
|
|
<< " @ " << std::hex << hdr->nodeid
|
|
|
|
<< " (" << (parent_node ? parent_node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
|
|
|
|
if (!parent_node || !(actual_name = find_file_within(parent_path, name,
|
|
|
|
child_path, sizeof(child_path), 1))) {
|
|
|
|
return -ENOENT;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2012-05-26 02:24:17 +02:00
|
|
|
__u32 mode = (req->mode & (~0777)) | 0775;
|
2016-10-06 21:51:20 +02:00
|
|
|
if (mkdir(child_path, mode) == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2013-09-12 18:44:48 +02:00
|
|
|
|
|
|
|
/* When creating /Android/data and /Android/obb, mark them as .nomedia */
|
|
|
|
if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
|
|
|
|
char nomedia[PATH_MAX];
|
|
|
|
snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
|
|
|
|
if (touch(nomedia, 0664) != 0) {
|
2016-07-26 21:22:31 +02:00
|
|
|
PLOG(ERROR) << "touch(" << nomedia << ") failed";
|
2013-09-12 18:44:48 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
|
|
|
|
char nomedia[PATH_MAX];
|
2015-07-16 18:13:52 +02:00
|
|
|
snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
|
2013-09-12 18:44:48 +02:00
|
|
|
if (touch(nomedia, 0664) != 0) {
|
2016-07-26 21:22:31 +02:00
|
|
|
PLOG(ERROR) << "touch(" << nomedia << ") failed";
|
2013-09-12 18:44:48 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const char* name)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* parent_node;
|
2014-07-16 08:34:30 +02:00
|
|
|
struct node* child_node;
|
2012-05-26 23:32:54 +02:00
|
|
|
char parent_path[PATH_MAX];
|
|
|
|
char child_path[PATH_MAX];
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
|
|
|
|
parent_path, sizeof(parent_path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
|
|
|
|
<< " (" << (parent_node ? parent_node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!parent_node || !find_file_within(parent_path, name,
|
|
|
|
child_path, sizeof(child_path), 1)) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2016-10-06 21:51:20 +02:00
|
|
|
if (unlink(child_path) == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2014-07-16 08:34:30 +02:00
|
|
|
child_node = lookup_child_by_name_locked(parent_node, name);
|
|
|
|
if (child_node) {
|
|
|
|
child_node->deleted = true;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2015-07-16 18:13:52 +02:00
|
|
|
if (parent_node && child_node) {
|
|
|
|
/* Tell all other views that node is gone */
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
|
|
|
|
<< " parent=" << std::hex << parent_node->nid
|
2016-07-26 21:22:31 +02:00
|
|
|
<< ", child=" << std::hex << child_node->nid << std::dec
|
|
|
|
<< ", name=" << name;
|
2015-07-16 18:13:52 +02:00
|
|
|
if (fuse != fuse->global->fuse_default) {
|
|
|
|
fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
|
|
|
|
}
|
|
|
|
if (fuse != fuse->global->fuse_read) {
|
|
|
|
fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
|
|
|
|
}
|
|
|
|
if (fuse != fuse->global->fuse_write) {
|
|
|
|
fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
|
|
|
|
}
|
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
return 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const char* name)
|
|
|
|
{
|
2014-07-16 08:34:30 +02:00
|
|
|
struct node* child_node;
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* parent_node;
|
|
|
|
char parent_path[PATH_MAX];
|
|
|
|
char child_path[PATH_MAX];
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
|
|
|
|
parent_path, sizeof(parent_path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
|
|
|
|
<< " (" << (parent_node ? parent_node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!parent_node || !find_file_within(parent_path, name,
|
|
|
|
child_path, sizeof(child_path), 1)) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2016-10-06 21:51:20 +02:00
|
|
|
if (rmdir(child_path) == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2014-07-16 08:34:30 +02:00
|
|
|
child_node = lookup_child_by_name_locked(parent_node, name);
|
|
|
|
if (child_node) {
|
|
|
|
child_node->deleted = true;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2015-07-16 18:13:52 +02:00
|
|
|
if (parent_node && child_node) {
|
|
|
|
/* Tell all other views that node is gone */
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
|
|
|
|
<< " parent=" << std::hex << parent_node->nid
|
2016-07-26 21:22:31 +02:00
|
|
|
<< ", child=" << std::hex << child_node->nid << std::dec
|
|
|
|
<< ", name=" << name;
|
2015-07-16 18:13:52 +02:00
|
|
|
if (fuse != fuse->global->fuse_default) {
|
|
|
|
fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
|
|
|
|
}
|
|
|
|
if (fuse != fuse->global->fuse_read) {
|
|
|
|
fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
|
|
|
|
}
|
|
|
|
if (fuse != fuse->global->fuse_write) {
|
|
|
|
fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
|
|
|
|
}
|
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
return 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
|
2012-05-26 23:32:54 +02:00
|
|
|
const char* old_name, const char* new_name)
|
2012-05-26 02:24:17 +02:00
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* old_parent_node;
|
|
|
|
struct node* new_parent_node;
|
|
|
|
struct node* child_node;
|
|
|
|
char old_parent_path[PATH_MAX];
|
|
|
|
char new_parent_path[PATH_MAX];
|
|
|
|
char old_child_path[PATH_MAX];
|
|
|
|
char new_child_path[PATH_MAX];
|
|
|
|
const char* new_actual_name;
|
2016-07-13 22:52:24 +02:00
|
|
|
int search;
|
2012-05-26 02:24:17 +02:00
|
|
|
int res;
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
|
|
|
|
old_parent_path, sizeof(old_parent_path));
|
|
|
|
new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
|
|
|
|
new_parent_path, sizeof(new_parent_path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name
|
|
|
|
<< " @ " << std::hex << hdr->nodeid
|
|
|
|
<< " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> "
|
|
|
|
<< std::hex << req->newdir
|
|
|
|
<< " (" << (new_parent_node ? new_parent_node->name : "?") << ")";
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!old_parent_node || !new_parent_node) {
|
|
|
|
res = -ENOENT;
|
|
|
|
goto lookup_error;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
res = -EACCES;
|
|
|
|
goto lookup_error;
|
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
res = -EACCES;
|
|
|
|
goto lookup_error;
|
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
child_node = lookup_child_by_name_locked(old_parent_node, old_name);
|
|
|
|
if (!child_node || get_node_path_locked(child_node,
|
|
|
|
old_child_path, sizeof(old_child_path)) < 0) {
|
|
|
|
res = -ENOENT;
|
|
|
|
goto lookup_error;
|
|
|
|
}
|
|
|
|
acquire_node_locked(child_node);
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
|
|
|
|
/* Special case for renaming a file where destination is same path
|
|
|
|
* differing only by case. In this case we don't want to look for a case
|
|
|
|
* insensitive match. This allows commands like "mv foo FOO" to work as expected.
|
|
|
|
*/
|
2016-07-13 22:52:24 +02:00
|
|
|
search = old_parent_node != new_parent_node
|
2012-05-26 23:32:54 +02:00
|
|
|
|| strcasecmp(old_name, new_name);
|
|
|
|
if (!(new_actual_name = find_file_within(new_parent_path, new_name,
|
|
|
|
new_child_path, sizeof(new_child_path), search))) {
|
|
|
|
res = -ENOENT;
|
|
|
|
goto io_error;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] RENAME " << old_child_path << "->" << new_child_path;
|
2012-05-26 23:32:54 +02:00
|
|
|
res = rename(old_child_path, new_child_path);
|
2016-10-06 21:51:20 +02:00
|
|
|
if (res == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
res = -errno;
|
|
|
|
goto io_error;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
res = rename_node_locked(child_node, new_name, new_actual_name);
|
|
|
|
if (!res) {
|
|
|
|
remove_node_from_parent_locked(child_node);
|
2015-12-14 19:02:01 +01:00
|
|
|
derive_permissions_locked(fuse, new_parent_node, child_node);
|
|
|
|
derive_permissions_recursive_locked(fuse, child_node);
|
2012-05-26 23:32:54 +02:00
|
|
|
add_node_to_parent_locked(child_node, new_parent_node);
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
goto done;
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
io_error:
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
done:
|
|
|
|
release_node_locked(child_node);
|
|
|
|
lookup_error:
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
return res;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:23:49 +02:00
|
|
|
static int open_flags_to_access_mode(int open_flags) {
|
|
|
|
if ((open_flags & O_ACCMODE) == O_RDONLY) {
|
|
|
|
return R_OK;
|
|
|
|
} else if ((open_flags & O_ACCMODE) == O_WRONLY) {
|
|
|
|
return W_OK;
|
|
|
|
} else {
|
|
|
|
/* Probably O_RDRW, but treat as default to be safe */
|
|
|
|
return R_OK | W_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_open_in* req)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* node;
|
|
|
|
char path[PATH_MAX];
|
2012-05-26 02:24:17 +02:00
|
|
|
struct fuse_open_out out;
|
|
|
|
struct handle *h;
|
2010-08-13 03:01:08 +02:00
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags
|
2016-07-26 21:22:31 +02:00
|
|
|
<< " @ " << std::hex << hdr->nodeid << std::dec
|
|
|
|
<< " (" << (node ? node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 02:24:17 +02:00
|
|
|
|
|
|
|
if (!node) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -ENOENT;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2013-08-30 19:26:15 +02:00
|
|
|
if (!check_caller_access_to_node(fuse, hdr, node,
|
2015-06-23 23:30:37 +02:00
|
|
|
open_flags_to_access_mode(req->flags))) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2016-07-13 22:52:24 +02:00
|
|
|
h = static_cast<struct handle*>(malloc(sizeof(*h)));
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!h) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] OPEN " << path;
|
2016-10-06 21:51:20 +02:00
|
|
|
h->fd = TEMP_FAILURE_RETRY(open(path, req->flags));
|
|
|
|
if (h->fd == -1) {
|
2012-05-26 02:24:17 +02:00
|
|
|
free(h);
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
out.fh = ptr_to_id(h);
|
|
|
|
out.open_flags = 0;
|
2016-01-14 00:11:35 +01:00
|
|
|
|
|
|
|
#ifdef FUSE_SHORTCIRCUIT
|
|
|
|
out.lower_fd = h->fd;
|
|
|
|
#else
|
2012-05-26 02:24:17 +02:00
|
|
|
out.padding = 0;
|
2016-01-14 00:11:35 +01:00
|
|
|
#endif
|
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
fuse_reply(fuse, hdr->unique, &out, sizeof(out));
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_read_in* req)
|
|
|
|
{
|
2016-07-13 22:52:24 +02:00
|
|
|
struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
|
2012-05-26 02:24:17 +02:00
|
|
|
__u64 unique = hdr->unique;
|
|
|
|
__u32 size = req->size;
|
|
|
|
__u64 offset = req->offset;
|
2012-05-26 23:32:54 +02:00
|
|
|
int res;
|
2015-07-29 01:36:47 +02:00
|
|
|
__u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
/* Don't access any other fields of hdr or req beyond this point, the read buffer
|
|
|
|
* overlaps the request buffer and will clobber data in the request. This
|
|
|
|
* saves us 128KB per request handler thread at the cost of this scary comment. */
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] READ " << std::hex << h << std::dec
|
|
|
|
<< "(" << h->fd << ") " << size << "@" << offset;
|
2014-02-15 01:42:27 +01:00
|
|
|
if (size > MAX_READ) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -EINVAL;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2016-10-06 21:51:20 +02:00
|
|
|
res = TEMP_FAILURE_RETRY(pread64(h->fd, read_buffer, size, offset));
|
|
|
|
if (res == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2014-02-15 01:42:27 +01:00
|
|
|
fuse_reply(fuse, unique, read_buffer, res);
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_write_in* req,
|
|
|
|
const void* buffer)
|
|
|
|
{
|
|
|
|
struct fuse_write_out out;
|
2016-07-13 22:52:24 +02:00
|
|
|
struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
|
2012-05-26 02:24:17 +02:00
|
|
|
int res;
|
2015-07-29 01:36:47 +02:00
|
|
|
__u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
|
2014-05-07 23:39:58 +02:00
|
|
|
|
2014-02-18 10:18:25 +01:00
|
|
|
if (req->flags & O_DIRECT) {
|
|
|
|
memcpy(aligned_buffer, buffer, req->size);
|
|
|
|
buffer = (const __u8*) aligned_buffer;
|
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] WRITE " << std::hex << h << std::dec
|
|
|
|
<< "(" << h->fd << ") " << req->size << "@" << req->offset;
|
2016-10-06 21:51:20 +02:00
|
|
|
res = TEMP_FAILURE_RETRY(pwrite64(h->fd, buffer, req->size, req->offset));
|
|
|
|
if (res == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
out.size = res;
|
2013-08-05 05:18:15 +02:00
|
|
|
out.padding = 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
fuse_reply(fuse, hdr->unique, &out, sizeof(out));
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
char path[PATH_MAX];
|
2012-05-26 02:24:17 +02:00
|
|
|
struct statfs stat;
|
|
|
|
struct fuse_statfs_out out;
|
|
|
|
int res;
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] STATFS";
|
2015-07-16 18:13:52 +02:00
|
|
|
res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
if (res < 0) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2016-10-06 21:51:20 +02:00
|
|
|
if (TEMP_FAILURE_RETRY(statfs(fuse->global->root.name, &stat)) == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
memset(&out, 0, sizeof(out));
|
|
|
|
out.st.blocks = stat.f_blocks;
|
|
|
|
out.st.bfree = stat.f_bfree;
|
|
|
|
out.st.bavail = stat.f_bavail;
|
|
|
|
out.st.files = stat.f_files;
|
|
|
|
out.st.ffree = stat.f_ffree;
|
|
|
|
out.st.bsize = stat.f_bsize;
|
|
|
|
out.st.namelen = stat.f_namelen;
|
|
|
|
out.st.frsize = stat.f_frsize;
|
|
|
|
fuse_reply(fuse, hdr->unique, &out, sizeof(out));
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_release_in* req)
|
|
|
|
{
|
2016-07-13 22:52:24 +02:00
|
|
|
struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] RELEASE " << std::hex << h << std::dec
|
|
|
|
<< "(" << h->fd << ")";
|
2012-05-26 02:24:17 +02:00
|
|
|
close(h->fd);
|
|
|
|
free(h);
|
2012-05-26 23:32:54 +02:00
|
|
|
return 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
|
|
|
|
{
|
2014-07-08 23:38:26 +02:00
|
|
|
bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
|
|
|
|
bool is_data_sync = req->fsync_flags & 1;
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2014-07-08 23:38:26 +02:00
|
|
|
int fd = -1;
|
|
|
|
if (is_dir) {
|
2016-07-13 22:52:24 +02:00
|
|
|
struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
|
2014-07-08 23:38:26 +02:00
|
|
|
fd = dirfd(dh->d);
|
|
|
|
} else {
|
2016-07-13 22:52:24 +02:00
|
|
|
struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
|
2014-07-08 23:38:26 +02:00
|
|
|
fd = h->fd;
|
|
|
|
}
|
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] " << (is_dir ? "FSYNCDIR" : "FSYNC") << " "
|
|
|
|
<< std::hex << req->fh << std::dec << "(" << fd << ") is_data_sync=" << is_data_sync;
|
2014-07-08 23:38:26 +02:00
|
|
|
int res = is_data_sync ? fdatasync(fd) : fsync(fd);
|
|
|
|
if (res == -1) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2012-05-26 23:32:54 +02:00
|
|
|
return 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr)
|
|
|
|
{
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] FLUSH";
|
2012-05-26 23:32:54 +02:00
|
|
|
return 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_open_in* req)
|
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct node* node;
|
|
|
|
char path[PATH_MAX];
|
2012-05-26 02:24:17 +02:00
|
|
|
struct fuse_open_out out;
|
|
|
|
struct dirhandle *h;
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] OPENDIR @ " << std::hex << hdr->nodeid
|
|
|
|
<< " (" << (node ? node->name : "?") << ")";
|
2015-06-23 23:30:37 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
if (!node) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -ENOENT;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2015-06-23 23:30:37 +02:00
|
|
|
if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
|
2013-08-13 05:23:49 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2016-07-13 22:52:24 +02:00
|
|
|
h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
|
2012-05-26 02:24:17 +02:00
|
|
|
if (!h) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return -ENOMEM;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] OPENDIR " << path;
|
2012-05-26 02:24:17 +02:00
|
|
|
h->d = opendir(path);
|
2012-05-26 23:32:54 +02:00
|
|
|
if (!h->d) {
|
2012-05-26 02:24:17 +02:00
|
|
|
free(h);
|
2012-05-26 23:32:54 +02:00
|
|
|
return -errno;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
out.fh = ptr_to_id(h);
|
2013-08-15 05:02:13 +02:00
|
|
|
out.open_flags = 0;
|
2016-01-14 00:11:35 +01:00
|
|
|
|
|
|
|
#ifdef FUSE_SHORTCIRCUIT
|
|
|
|
out.lower_fd = -1;
|
|
|
|
#else
|
2013-08-15 05:02:13 +02:00
|
|
|
out.padding = 0;
|
2016-01-14 00:11:35 +01:00
|
|
|
#endif
|
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
fuse_reply(fuse, hdr->unique, &out, sizeof(out));
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_read_in* req)
|
|
|
|
{
|
|
|
|
char buffer[8192];
|
|
|
|
struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
|
|
|
|
struct dirent *de;
|
2016-07-13 22:52:24 +02:00
|
|
|
struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] READDIR " << h;
|
2012-05-26 02:24:17 +02:00
|
|
|
if (req->offset == 0) {
|
|
|
|
/* rewinddir() might have been called above us, so rewind here too */
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] calling rewinddir()";
|
2012-05-26 02:24:17 +02:00
|
|
|
rewinddir(h->d);
|
|
|
|
}
|
|
|
|
de = readdir(h->d);
|
|
|
|
if (!de) {
|
2012-05-26 23:32:54 +02:00
|
|
|
return 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
fde->ino = FUSE_UNKNOWN_INO;
|
|
|
|
/* increment the offset so we can detect when rewinddir() seeks back to the beginning */
|
|
|
|
fde->off = req->offset + 1;
|
|
|
|
fde->type = de->d_type;
|
|
|
|
fde->namelen = strlen(de->d_name);
|
|
|
|
memcpy(fde->name, de->d_name, fde->namelen + 1);
|
|
|
|
fuse_reply(fuse, hdr->unique, fde,
|
2012-05-26 23:32:54 +02:00
|
|
|
FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
|
|
|
|
return NO_STATUS;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_release_in* req)
|
|
|
|
{
|
2016-07-13 22:52:24 +02:00
|
|
|
struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
|
2012-05-26 23:32:54 +02:00
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] RELEASEDIR " << h;
|
2012-05-26 02:24:17 +02:00
|
|
|
closedir(h->d);
|
|
|
|
free(h);
|
2012-05-26 23:32:54 +02:00
|
|
|
return 0;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header* hdr, const struct fuse_init_in* req)
|
|
|
|
{
|
|
|
|
struct fuse_init_out out;
|
2014-09-13 22:53:08 +02:00
|
|
|
size_t fuse_struct_size;
|
2012-05-26 02:24:17 +02:00
|
|
|
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] INIT ver=" << req->major << "." << req->minor
|
|
|
|
<< " maxread=" << req->max_readahead << " flags=" << std::hex << req->flags;
|
2014-09-13 22:53:08 +02:00
|
|
|
|
|
|
|
/* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
|
|
|
|
* defined (fuse version 7.6). The structure is the same from 7.6 through
|
|
|
|
* 7.22. Beginning with 7.23, the structure increased in size and added
|
|
|
|
* new parameters.
|
|
|
|
*/
|
|
|
|
if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
|
2016-07-26 21:22:31 +02:00
|
|
|
LOG(ERROR) << "Fuse kernel version mismatch: Kernel version "
|
|
|
|
<< req->major << "." << req->minor
|
|
|
|
<< ", Expected at least " << FUSE_KERNEL_VERSION << ".6";
|
2014-09-13 22:53:08 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 23:30:37 +02:00
|
|
|
/* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
|
|
|
|
out.minor = MIN(req->minor, 15);
|
2014-09-13 22:53:08 +02:00
|
|
|
fuse_struct_size = sizeof(out);
|
|
|
|
#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
|
|
|
|
/* FUSE_KERNEL_VERSION >= 23. */
|
|
|
|
|
2016-11-10 02:22:42 +01:00
|
|
|
/* Since we return minor version 15, the kernel does not accept the latest
|
|
|
|
* fuse_init_out size. We need to use FUSE_COMPAT_22_INIT_OUT_SIZE always.*/
|
|
|
|
fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
|
2014-09-13 22:53:08 +02:00
|
|
|
#endif
|
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
out.major = FUSE_KERNEL_VERSION;
|
|
|
|
out.max_readahead = req->max_readahead;
|
|
|
|
out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
|
2016-01-14 00:11:35 +01:00
|
|
|
|
|
|
|
#ifdef FUSE_SHORTCIRCUIT
|
|
|
|
out.flags |= FUSE_SHORTCIRCUIT;
|
|
|
|
#endif
|
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
out.max_background = 32;
|
|
|
|
out.congestion_threshold = 32;
|
|
|
|
out.max_write = MAX_WRITE;
|
2014-09-13 22:53:08 +02:00
|
|
|
fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
|
2012-05-26 23:32:54 +02:00
|
|
|
return NO_STATUS;
|
2012-05-26 02:24:17 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 03:33:08 +02:00
|
|
|
static int handle_canonical_path(struct fuse* fuse, struct fuse_handler* handler,
|
|
|
|
const struct fuse_in_header *hdr)
|
|
|
|
{
|
|
|
|
struct node* node;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&fuse->global->lock);
|
|
|
|
node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
|
|
|
|
path, sizeof(path));
|
2016-07-27 02:07:10 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] CANONICAL_PATH @ " << std::hex << hdr->nodeid
|
|
|
|
<< std::dec << " (" << (node ? node->name : "?") << ")";
|
2016-04-20 03:33:08 +02:00
|
|
|
pthread_mutex_unlock(&fuse->global->lock);
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
len = strlen(path);
|
|
|
|
if (len + 1 > PATH_MAX)
|
|
|
|
len = PATH_MAX - 1;
|
|
|
|
path[PATH_MAX - 1] = 0;
|
|
|
|
fuse_reply(fuse, hdr->unique, path, len + 1);
|
|
|
|
return NO_STATUS;
|
|
|
|
}
|
|
|
|
|
2012-05-26 23:32:54 +02:00
|
|
|
static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
|
2012-05-26 02:24:17 +02:00
|
|
|
const struct fuse_in_header *hdr, const void *data, size_t data_len)
|
|
|
|
{
|
2010-08-13 03:01:08 +02:00
|
|
|
switch (hdr->opcode) {
|
|
|
|
case FUSE_LOOKUP: { /* bytez[] -> entry_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const char *name = static_cast<const char*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_lookup(fuse, handler, hdr, name);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_FORGET: {
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_forget(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_GETATTR: { /* getattr_in -> attr_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_getattr(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_SETATTR: { /* setattr_in -> attr_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_setattr(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
// case FUSE_READLINK:
|
|
|
|
// case FUSE_SYMLINK:
|
|
|
|
case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
|
2012-05-25 23:07:47 +02:00
|
|
|
const char *name = ((const char*) data) + sizeof(*req);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_mknod(fuse, handler, hdr, req, name);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
|
2012-05-25 23:07:47 +02:00
|
|
|
const char *name = ((const char*) data) + sizeof(*req);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_mkdir(fuse, handler, hdr, req, name);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_UNLINK: { /* bytez[] -> */
|
2016-07-13 22:52:24 +02:00
|
|
|
const char *name = static_cast<const char*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_unlink(fuse, handler, hdr, name);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_RMDIR: { /* bytez[] -> */
|
2016-07-13 22:52:24 +02:00
|
|
|
const char *name = static_cast<const char*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_rmdir(fuse, handler, hdr, name);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_RENAME: { /* rename_in, oldname, newname -> */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
const char *old_name = ((const char*) data) + sizeof(*req);
|
|
|
|
const char *new_name = old_name + strlen(old_name) + 1;
|
|
|
|
return handle_rename(fuse, handler, hdr, req, old_name, new_name);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2012-05-26 02:24:17 +02:00
|
|
|
// case FUSE_LINK:
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_OPEN: { /* open_in -> open_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_open(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_READ: { /* read_in -> byte[] */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_read(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
|
2012-05-25 23:07:47 +02:00
|
|
|
const void* buffer = (const __u8*)data + sizeof(*req);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_write(fuse, handler, hdr, req, buffer);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-16 20:14:44 +02:00
|
|
|
case FUSE_STATFS: { /* getattr_in -> attr_out */
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_statfs(fuse, handler, hdr);
|
2010-08-16 20:14:44 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_RELEASE: { /* release_in -> */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_release(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2014-02-17 10:33:11 +01:00
|
|
|
case FUSE_FSYNC:
|
|
|
|
case FUSE_FSYNCDIR: {
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_fsync(fuse, handler, hdr, req);
|
2012-05-26 00:01:21 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
// case FUSE_SETXATTR:
|
|
|
|
// case FUSE_GETXATTR:
|
|
|
|
// case FUSE_LISTXATTR:
|
|
|
|
// case FUSE_REMOVEXATTR:
|
2012-05-25 23:07:47 +02:00
|
|
|
case FUSE_FLUSH: {
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_flush(fuse, handler, hdr);
|
2012-05-25 23:07:47 +02:00
|
|
|
}
|
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_OPENDIR: { /* open_in -> open_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_opendir(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_READDIR: {
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_readdir(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_RELEASEDIR: { /* release_in -> */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_releasedir(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
case FUSE_INIT: { /* init_in -> init_out */
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
|
2012-05-26 23:32:54 +02:00
|
|
|
return handle_init(fuse, handler, hdr, req);
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2016-04-20 03:33:08 +02:00
|
|
|
case FUSE_CANONICAL_PATH: { /* nodeid -> bytez[] */
|
|
|
|
return handle_canonical_path(fuse, handler, hdr);
|
|
|
|
}
|
|
|
|
|
2010-08-13 03:01:08 +02:00
|
|
|
default: {
|
2016-07-20 21:19:44 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode
|
2016-07-26 21:22:31 +02:00
|
|
|
<< "uniq=" << std::hex << hdr->unique << "nid=" << hdr->nodeid << std::dec;
|
2012-05-26 23:32:54 +02:00
|
|
|
return -ENOSYS;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
}
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 21:13:05 +02:00
|
|
|
void handle_fuse_requests(struct fuse_handler* handler)
|
2010-08-13 03:01:08 +02:00
|
|
|
{
|
2012-05-26 23:32:54 +02:00
|
|
|
struct fuse* fuse = handler->fuse;
|
2010-08-13 03:01:08 +02:00
|
|
|
for (;;) {
|
2015-07-06 19:00:36 +02:00
|
|
|
ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
|
|
|
|
handler->request_buffer, sizeof(handler->request_buffer)));
|
2016-10-06 21:51:20 +02:00
|
|
|
if (len == -1) {
|
2015-07-01 01:02:40 +02:00
|
|
|
if (errno == ENODEV) {
|
2016-07-26 21:22:31 +02:00
|
|
|
LOG(ERROR) << "[" << handler->token << "] someone stole our marbles!";
|
2015-07-01 01:02:40 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
2016-07-26 21:22:31 +02:00
|
|
|
PLOG(ERROR) << "[" << handler->token << "] handle_fuse_requests";
|
2012-05-26 23:32:54 +02:00
|
|
|
continue;
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
2012-05-25 23:07:47 +02:00
|
|
|
|
2016-10-06 21:51:20 +02:00
|
|
|
if (static_cast<size_t>(len) < sizeof(struct fuse_in_header)) {
|
2016-07-26 21:22:31 +02:00
|
|
|
LOG(ERROR) << "[" << handler->token << "] request too short: len=" << len;
|
2012-05-26 23:32:54 +02:00
|
|
|
continue;
|
2012-05-25 23:07:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 22:52:24 +02:00
|
|
|
const struct fuse_in_header* hdr =
|
|
|
|
reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
|
2016-10-06 21:51:20 +02:00
|
|
|
if (hdr->len != static_cast<size_t>(len)) {
|
2016-07-26 21:22:31 +02:00
|
|
|
LOG(ERROR) << "[" << handler->token << "] malformed header: len=" << len
|
|
|
|
<< ", hdr->len=" << hdr->len;
|
2012-05-26 23:32:54 +02:00
|
|
|
continue;
|
2012-05-25 23:07:47 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 00:35:28 +02:00
|
|
|
const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
|
2012-05-25 23:07:47 +02:00
|
|
|
size_t data_len = len - sizeof(struct fuse_in_header);
|
2012-05-26 23:32:54 +02:00
|
|
|
__u64 unique = hdr->unique;
|
|
|
|
int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
|
2012-05-26 00:35:28 +02:00
|
|
|
|
|
|
|
/* We do not access the request again after this point because the underlying
|
|
|
|
* buffer storage may have been reused while processing the request. */
|
2012-05-26 23:32:54 +02:00
|
|
|
|
|
|
|
if (res != NO_STATUS) {
|
|
|
|
if (res) {
|
2016-07-26 21:22:31 +02:00
|
|
|
DLOG(INFO) << "[" << handler->token << "] ERROR " << res;
|
2012-05-26 23:32:54 +02:00
|
|
|
}
|
|
|
|
fuse_status(fuse, unique, res);
|
|
|
|
}
|
2010-08-13 03:01:08 +02:00
|
|
|
}
|
|
|
|
}
|