blob: 54ac9194c89f0b5f67bef464fc0fa5cf99925011 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040019#include "fuse.h"
Brian Swetlandb14a2c62010-08-12 18:21:12 -070020
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -040021#include <android-base/logging.h>
22
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -070023/* FUSE_CANONICAL_PATH is not currently upstreamed */
24#define FUSE_CANONICAL_PATH 2016
25
Jeff Sharkey20ca9832016-04-07 11:05:21 -060026#define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
27#define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
28
Brian Swetland03ee9472010-08-12 18:01:08 -070029#define FUSE_UNKNOWN_INO 0xffffffff
30
Jeff Brown6249b902012-05-26 14:32:54 -070031/* Pseudo-error constant used to indicate that no fuse status is needed
32 * or that a reply has already been written. */
33#define NO_STATUS 1
34
Jeff Brown6249b902012-05-26 14:32:54 -070035static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -070036{
Jeff Brown6249b902012-05-26 14:32:54 -070037 return (void *) (uintptr_t) nid;
38}
Brian Swetland03ee9472010-08-12 18:01:08 -070039
Jeff Brown6249b902012-05-26 14:32:54 -070040static inline __u64 ptr_to_id(void *ptr)
41{
42 return (__u64) (uintptr_t) ptr;
43}
Brian Swetland03ee9472010-08-12 18:01:08 -070044
Jeff Brown6249b902012-05-26 14:32:54 -070045static void acquire_node_locked(struct node* node)
46{
47 node->refcount++;
48 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
49}
50
51static void remove_node_from_parent_locked(struct node* node);
52
53static void release_node_locked(struct node* node)
54{
55 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
56 if (node->refcount > 0) {
57 node->refcount--;
58 if (!node->refcount) {
59 TRACE("DESTROY %p (%s)\n", node, node->name);
60 remove_node_from_parent_locked(node);
61
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040062 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070063 memset(node->name, 0xef, node->namelen);
64 free(node->name);
65 free(node->actual_name);
66 memset(node, 0xfc, sizeof(*node));
67 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080068 }
Jeff Brown6249b902012-05-26 14:32:54 -070069 } else {
70 ERROR("Zero refcnt %p\n", node);
71 }
72}
73
74static void add_node_to_parent_locked(struct node *node, struct node *parent) {
75 node->parent = parent;
76 node->next = parent->child;
77 parent->child = node;
78 acquire_node_locked(parent);
79}
80
81static void remove_node_from_parent_locked(struct node* node)
82{
83 if (node->parent) {
84 if (node->parent->child == node) {
85 node->parent->child = node->parent->child->next;
86 } else {
87 struct node *node2;
88 node2 = node->parent->child;
89 while (node2->next != node)
90 node2 = node2->next;
91 node2->next = node->next;
92 }
93 release_node_locked(node->parent);
94 node->parent = NULL;
95 node->next = NULL;
96 }
97}
98
99/* Gets the absolute path to a node into the provided buffer.
100 *
101 * Populates 'buf' with the path and returns the length of the path on success,
102 * or returns -1 if the path is too long for the provided buffer.
103 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700104static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
105 const char* name;
106 size_t namelen;
107 if (node->graft_path) {
108 name = node->graft_path;
109 namelen = node->graft_pathlen;
110 } else if (node->actual_name) {
111 name = node->actual_name;
112 namelen = node->namelen;
113 } else {
114 name = node->name;
115 namelen = node->namelen;
116 }
117
Jeff Brown6249b902012-05-26 14:32:54 -0700118 if (bufsize < namelen + 1) {
119 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700120 }
121
Jeff Brown6249b902012-05-26 14:32:54 -0700122 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700123 if (node->parent && node->graft_path == NULL) {
Daniel Rosenbergdb4638e2016-04-12 16:30:28 -0700124 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700125 if (pathlen < 0) {
126 return -1;
127 }
128 buf[pathlen++] = '/';
129 }
130
Jeff Brown6249b902012-05-26 14:32:54 -0700131 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
132 return pathlen + namelen;
133}
134
135/* Finds the absolute path of a file within a given directory.
136 * Performs a case-insensitive search for the file and sets the buffer to the path
137 * of the first matching file. If 'search' is zero or if no match is found, sets
138 * the buffer to the path that the file would have, assuming the name were case-sensitive.
139 *
140 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
141 * or returns NULL if the path is too long for the provided buffer.
142 */
143static char* find_file_within(const char* path, const char* name,
144 char* buf, size_t bufsize, int search)
145{
146 size_t pathlen = strlen(path);
147 size_t namelen = strlen(name);
148 size_t childlen = pathlen + namelen + 1;
149 char* actual;
150
151 if (bufsize <= childlen) {
152 return NULL;
153 }
154
155 memcpy(buf, path, pathlen);
156 buf[pathlen] = '/';
157 actual = buf + pathlen + 1;
158 memcpy(actual, name, namelen + 1);
159
160 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800161 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700162 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800163 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700164 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700165 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800166 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800167 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700168 if (!strcasecmp(entry->d_name, name)) {
169 /* we have a match - replace the name, don't need to copy the null again */
170 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800171 break;
172 }
173 }
174 closedir(dir);
175 }
Jeff Brown6249b902012-05-26 14:32:54 -0700176 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800177}
178
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700179static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
180 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000181 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700182 attr->size = s->st_size;
183 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800184 attr->atime = s->st_atim.tv_sec;
185 attr->mtime = s->st_mtim.tv_sec;
186 attr->ctime = s->st_ctim.tv_sec;
187 attr->atimensec = s->st_atim.tv_nsec;
188 attr->mtimensec = s->st_mtim.tv_nsec;
189 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700190 attr->mode = s->st_mode;
191 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700192
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700193 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700194
195 if (fuse->gid == AID_SDCARD_RW) {
196 /* As an optimization, certain trusted system components only run
197 * as owner but operate across all users. Since we're now handing
198 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
199 * the user boundary enforcement for the default view. The UIDs
200 * assigned to app directories are still multiuser aware. */
201 attr->gid = AID_SDCARD_RW;
202 } else {
203 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
204 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700205
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700206 int visible_mode = 0775 & ~fuse->mask;
207 if (node->perm == PERM_PRE_ROOT) {
208 /* Top of multi-user view should always be visible to ensure
209 * secondary users can traverse inside. */
210 visible_mode = 0711;
211 } else if (node->under_android) {
212 /* Block "other" access to Android directories, since only apps
213 * belonging to a specific user should be in there; we still
214 * leave +x open for the default view. */
215 if (fuse->gid == AID_SDCARD_RW) {
216 visible_mode = visible_mode & ~0006;
217 } else {
218 visible_mode = visible_mode & ~0007;
219 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700220 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700221 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700222 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700223 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
224}
225
Jeff Sharkey44d63422013-09-12 09:44:48 -0700226static int touch(char* path, mode_t mode) {
227 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
228 if (fd == -1) {
229 if (errno == EEXIST) {
230 return 0;
231 } else {
232 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
233 return -1;
234 }
235 }
236 close(fd);
237 return 0;
238}
239
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700240static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
241 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700242 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700243
244 /* By default, each node inherits from its parent */
245 node->perm = PERM_INHERIT;
246 node->userid = parent->userid;
247 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700248 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700249
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700250 /* Derive custom permissions based on parent and current node */
251 switch (parent->perm) {
252 case PERM_INHERIT:
253 /* Already inherited above */
254 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700255 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700256 /* Legacy internal layout places users at top level */
257 node->perm = PERM_ROOT;
258 node->userid = strtoul(node->name, NULL, 10);
259 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700260 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700261 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700262 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700263 /* App-specific directories inside; let anyone traverse */
264 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700265 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700266 }
267 break;
268 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700269 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700270 /* App-specific directories inside; let anyone traverse */
271 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700272 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700273 /* App-specific directories inside; let anyone traverse */
274 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700275 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700276 node->graft_path = fuse->global->obb_path;
277 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700278 } else if (!strcasecmp(node->name, "media")) {
279 /* App-specific directories inside; let anyone traverse */
280 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700281 }
282 break;
283 case PERM_ANDROID_DATA:
284 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700285 case PERM_ANDROID_MEDIA:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400286 const auto& iter = fuse->global->package_to_appid->find(node->name);
287 if (iter != fuse->global->package_to_appid->end()) {
288 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700289 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700290 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700291 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700292 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700293}
294
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400295void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700296 struct node *node;
297 for (node = parent->child; node; node = node->next) {
298 derive_permissions_locked(fuse, parent, node);
299 if (node->child) {
300 derive_permissions_recursive_locked(fuse, node);
301 }
302 }
303}
304
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700305/* Kernel has already enforced everything we returned through
306 * derive_permissions_locked(), so this is used to lock down access
307 * even further, such as enforcing that apps hold sdcard_rw. */
308static bool check_caller_access_to_name(struct fuse* fuse,
309 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700310 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700311 /* Always block security-sensitive files at root */
312 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700313 if (!strcasecmp(name, "autorun.inf")
314 || !strcasecmp(name, ".android_secure")
315 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700316 return false;
317 }
318 }
319
Jeff Sharkey44d63422013-09-12 09:44:48 -0700320 /* Root always has access; access for any other UIDs should always
321 * be controlled through packages.list. */
322 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700323 return true;
324 }
325
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700326 /* No extra permissions to enforce */
327 return true;
328}
329
330static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700331 const struct fuse_in_header *hdr, const struct node* node, int mode) {
332 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700333}
334
Jeff Brown6249b902012-05-26 14:32:54 -0700335struct node *create_node_locked(struct fuse* fuse,
336 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700337{
338 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700339 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700340
Narayan Kamathfaa09352015-01-13 18:21:10 +0000341 // Detect overflows in the inode counter. "4 billion nodes should be enough
342 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700343 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000344 ERROR("No more inode numbers available");
345 return NULL;
346 }
347
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400348 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700349 if (!node) {
350 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700351 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400352 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700353 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700354 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700355 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700356 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700357 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700358 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400359 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700360 if (!node->actual_name) {
361 free(node->name);
362 free(node);
363 return NULL;
364 }
365 memcpy(node->actual_name, actual_name, namelen + 1);
366 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700367 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700368 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700369 node->ino = fuse->global->inode_ctr++;
370 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700371
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200372 node->deleted = false;
373
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700374 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700375 acquire_node_locked(node);
376 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700377 return node;
378}
379
Jeff Brown6249b902012-05-26 14:32:54 -0700380static int rename_node_locked(struct node *node, const char *name,
381 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700382{
Jeff Brown6249b902012-05-26 14:32:54 -0700383 size_t namelen = strlen(name);
384 int need_actual_name = strcmp(name, actual_name);
385
386 /* make the storage bigger without actually changing the name
387 * in case an error occurs part way */
388 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400389 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700390 if (!new_name) {
391 return -ENOMEM;
392 }
393 node->name = new_name;
394 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400395 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700396 if (!new_actual_name) {
397 return -ENOMEM;
398 }
399 node->actual_name = new_actual_name;
400 }
401 }
402
403 /* update the name, taking care to allocate storage before overwriting the old name */
404 if (need_actual_name) {
405 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400406 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700407 if (!node->actual_name) {
408 return -ENOMEM;
409 }
410 }
411 memcpy(node->actual_name, actual_name, namelen + 1);
412 } else {
413 free(node->actual_name);
414 node->actual_name = NULL;
415 }
416 memcpy(node->name, name, namelen + 1);
417 node->namelen = namelen;
418 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700419}
420
Jeff Brown6249b902012-05-26 14:32:54 -0700421static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700422{
Jeff Brown6249b902012-05-26 14:32:54 -0700423 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700424 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700425 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400426 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700427 }
428}
429
Jeff Brown6249b902012-05-26 14:32:54 -0700430static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
431 char* buf, size_t bufsize)
432{
433 struct node* node = lookup_node_by_id_locked(fuse, nid);
434 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
435 node = NULL;
436 }
437 return node;
438}
439
440static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700441{
442 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700443 /* use exact string comparison, nodes that differ by case
444 * must be considered distinct even if they refer to the same
445 * underlying file as otherwise operations such as "mv x x"
446 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200447 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700448 return node;
449 }
450 }
451 return 0;
452}
453
Jeff Brown6249b902012-05-26 14:32:54 -0700454static struct node* acquire_or_create_child_locked(
455 struct fuse* fuse, struct node* parent,
456 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700457{
Jeff Brown6249b902012-05-26 14:32:54 -0700458 struct node* child = lookup_child_by_name_locked(parent, name);
459 if (child) {
460 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800461 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700462 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800463 }
Jeff Brown6249b902012-05-26 14:32:54 -0700464 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700465}
466
Jeff Brown6249b902012-05-26 14:32:54 -0700467static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700468{
469 struct fuse_out_header hdr;
470 hdr.len = sizeof(hdr);
471 hdr.error = err;
472 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700473 write(fuse->fd, &hdr, sizeof(hdr));
474}
475
Jeff Brown6249b902012-05-26 14:32:54 -0700476static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700477{
478 struct fuse_out_header hdr;
479 struct iovec vec[2];
480 int res;
481
482 hdr.len = len + sizeof(hdr);
483 hdr.error = 0;
484 hdr.unique = unique;
485
486 vec[0].iov_base = &hdr;
487 vec[0].iov_len = sizeof(hdr);
488 vec[1].iov_base = data;
489 vec[1].iov_len = len;
490
491 res = writev(fuse->fd, vec, 2);
492 if (res < 0) {
493 ERROR("*** REPLY FAILED *** %d\n", errno);
494 }
495}
496
Jeff Brown6249b902012-05-26 14:32:54 -0700497static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
498 struct node* parent, const char* name, const char* actual_name,
499 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700500{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700501 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700502 struct fuse_entry_out out;
503 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700504
Jeff Brown6249b902012-05-26 14:32:54 -0700505 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700506 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700507 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700508
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700509 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700510 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
511 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700512 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700513 return -ENOMEM;
514 }
515 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700516 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700517 out.attr_valid = 10;
518 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700519 out.nodeid = node->nid;
520 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700521 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700522 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700523 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700524}
525
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700526static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700527 const char* path)
528{
529 struct fuse_attr_out out;
530 struct stat s;
531
532 if (lstat(path, &s) < 0) {
533 return -errno;
534 }
535 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700536 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700537 out.attr_valid = 10;
538 fuse_reply(fuse, unique, &out, sizeof(out));
539 return NO_STATUS;
540}
541
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700542static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
543 const __u64 child, const char* name) {
544 struct fuse_out_header hdr;
545 struct fuse_notify_delete_out data;
546 struct iovec vec[3];
547 size_t namelen = strlen(name);
548 int res;
549
550 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
551 hdr.error = FUSE_NOTIFY_DELETE;
552 hdr.unique = 0;
553
554 data.parent = parent;
555 data.child = child;
556 data.namelen = namelen;
557 data.padding = 0;
558
559 vec[0].iov_base = &hdr;
560 vec[0].iov_len = sizeof(hdr);
561 vec[1].iov_base = &data;
562 vec[1].iov_len = sizeof(data);
563 vec[2].iov_base = (void*) name;
564 vec[2].iov_len = namelen + 1;
565
566 res = writev(fuse->fd, vec, 3);
567 /* Ignore ENOENT, since other views may not have seen the entry */
568 if (res < 0 && errno != ENOENT) {
569 ERROR("*** NOTIFY FAILED *** %d\n", errno);
570 }
571}
572
Jeff Brown6249b902012-05-26 14:32:54 -0700573static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700574 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700575{
Jeff Brown6249b902012-05-26 14:32:54 -0700576 struct node* parent_node;
577 char parent_path[PATH_MAX];
578 char child_path[PATH_MAX];
579 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700580
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700581 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700582 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
583 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400584 DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid
585 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700586 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700587
588 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
589 child_path, sizeof(child_path), 1))) {
590 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700591 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700592 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700593 return -EACCES;
594 }
595
Jeff Brown6249b902012-05-26 14:32:54 -0700596 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700597}
598
Jeff Brown6249b902012-05-26 14:32:54 -0700599static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700600 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
601{
Jeff Brown6249b902012-05-26 14:32:54 -0700602 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700603
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700604 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700605 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400606 DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup
607 << " @ " << std::hex << hdr->nodeid
608 << " (" << (node ? node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700609 if (node) {
610 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400611 while (n) {
612 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700613 release_node_locked(node);
614 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700615 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700616 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700617 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700618}
619
Jeff Brown6249b902012-05-26 14:32:54 -0700620static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700621 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
622{
Jeff Brown6249b902012-05-26 14:32:54 -0700623 struct node* node;
624 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700625
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700626 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700627 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400628 DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags
629 << " fh=" << std::hex << req->fh << " @ " << std::hex << hdr->nodeid
630 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700631 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700632
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700633 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700634 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700635 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700636 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700637 return -EACCES;
638 }
639
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700640 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700641}
642
Jeff Brown6249b902012-05-26 14:32:54 -0700643static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700644 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
645{
Jeff Brown6249b902012-05-26 14:32:54 -0700646 struct node* node;
647 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700648 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700649
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700650 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700651 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400652 DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh
653 << " valid=" << std::hex << req->valid << " @ " << std::hex << hdr->nodeid
654 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700655 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700656
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700657 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700658 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700659 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800660
661 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700662 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700663 return -EACCES;
664 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700665
Jeff Brown6249b902012-05-26 14:32:54 -0700666 /* XXX: incomplete implementation on purpose.
667 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700668
Elliott Hughes853574d2014-07-31 12:03:03 -0700669 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700670 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700671 }
672
673 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
674 * are both set, then set it to the current time. Else, set it to the
675 * time specified in the request. Same goes for mtime. Use utimensat(2)
676 * as it allows ATIME and MTIME to be changed independently, and has
677 * nanosecond resolution which fuse also has.
678 */
679 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
680 times[0].tv_nsec = UTIME_OMIT;
681 times[1].tv_nsec = UTIME_OMIT;
682 if (req->valid & FATTR_ATIME) {
683 if (req->valid & FATTR_ATIME_NOW) {
684 times[0].tv_nsec = UTIME_NOW;
685 } else {
686 times[0].tv_sec = req->atime;
687 times[0].tv_nsec = req->atimensec;
688 }
689 }
690 if (req->valid & FATTR_MTIME) {
691 if (req->valid & FATTR_MTIME_NOW) {
692 times[1].tv_nsec = UTIME_NOW;
693 } else {
694 times[1].tv_sec = req->mtime;
695 times[1].tv_nsec = req->mtimensec;
696 }
697 }
Jeff Brown6249b902012-05-26 14:32:54 -0700698 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
699 handler->token, path, times[0].tv_sec, times[1].tv_sec);
700 if (utimensat(-1, path, times, 0) < 0) {
701 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700702 }
703 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700704 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700705}
706
Jeff Brown6249b902012-05-26 14:32:54 -0700707static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700708 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
709{
Jeff Brown6249b902012-05-26 14:32:54 -0700710 struct node* parent_node;
711 char parent_path[PATH_MAX];
712 char child_path[PATH_MAX];
713 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700714
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700715 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700716 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
717 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400718 DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode
719 << " @ " << std::hex << hdr->nodeid
720 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700721 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700722
723 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
724 child_path, sizeof(child_path), 1))) {
725 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700726 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700727 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700728 return -EACCES;
729 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700730 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700731 if (mknod(child_path, mode, req->rdev) < 0) {
732 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700733 }
Jeff Brown6249b902012-05-26 14:32:54 -0700734 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700735}
736
Jeff Brown6249b902012-05-26 14:32:54 -0700737static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700738 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
739{
Jeff Brown6249b902012-05-26 14:32:54 -0700740 struct node* parent_node;
741 char parent_path[PATH_MAX];
742 char child_path[PATH_MAX];
743 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700744
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700745 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700746 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
747 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400748 DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode
749 << " @ " << std::hex << hdr->nodeid
750 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700751 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700752
753 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
754 child_path, sizeof(child_path), 1))) {
755 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700756 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700757 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700758 return -EACCES;
759 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700760 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700761 if (mkdir(child_path, mode) < 0) {
762 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700763 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700764
765 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
766 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
767 char nomedia[PATH_MAX];
768 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
769 if (touch(nomedia, 0664) != 0) {
770 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
771 return -ENOENT;
772 }
773 }
774 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
775 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700776 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700777 if (touch(nomedia, 0664) != 0) {
778 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
779 return -ENOENT;
780 }
781 }
782
Jeff Brown6249b902012-05-26 14:32:54 -0700783 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700784}
785
Jeff Brown6249b902012-05-26 14:32:54 -0700786static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700787 const struct fuse_in_header* hdr, const char* name)
788{
Jeff Brown6249b902012-05-26 14:32:54 -0700789 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200790 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700791 char parent_path[PATH_MAX];
792 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700793
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700794 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700795 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
796 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400797 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
798 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700799 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700800
801 if (!parent_node || !find_file_within(parent_path, name,
802 child_path, sizeof(child_path), 1)) {
803 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700804 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700805 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700806 return -EACCES;
807 }
Jeff Brown6249b902012-05-26 14:32:54 -0700808 if (unlink(child_path) < 0) {
809 return -errno;
810 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700811 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200812 child_node = lookup_child_by_name_locked(parent_node, name);
813 if (child_node) {
814 child_node->deleted = true;
815 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700816 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700817 if (parent_node && child_node) {
818 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400819 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
820 << " parent=" << std::hex << parent_node->nid
821 << ", child=" << std::hex << child_node->nid << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700822 if (fuse != fuse->global->fuse_default) {
823 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
824 }
825 if (fuse != fuse->global->fuse_read) {
826 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
827 }
828 if (fuse != fuse->global->fuse_write) {
829 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
830 }
831 }
Jeff Brown6249b902012-05-26 14:32:54 -0700832 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700833}
834
Jeff Brown6249b902012-05-26 14:32:54 -0700835static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836 const struct fuse_in_header* hdr, const char* name)
837{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200838 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700839 struct node* parent_node;
840 char parent_path[PATH_MAX];
841 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700842
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700843 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700844 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
845 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400846 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
847 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700848 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700849
850 if (!parent_node || !find_file_within(parent_path, name,
851 child_path, sizeof(child_path), 1)) {
852 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700853 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700854 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700855 return -EACCES;
856 }
Jeff Brown6249b902012-05-26 14:32:54 -0700857 if (rmdir(child_path) < 0) {
858 return -errno;
859 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700860 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200861 child_node = lookup_child_by_name_locked(parent_node, name);
862 if (child_node) {
863 child_node->deleted = true;
864 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700865 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700866 if (parent_node && child_node) {
867 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400868 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
869 << " parent=" << std::hex << parent_node->nid
870 << ", child=" << std::hex << child_node->nid << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700871 if (fuse != fuse->global->fuse_default) {
872 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
873 }
874 if (fuse != fuse->global->fuse_read) {
875 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
876 }
877 if (fuse != fuse->global->fuse_write) {
878 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
879 }
880 }
Jeff Brown6249b902012-05-26 14:32:54 -0700881 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882}
883
Jeff Brown6249b902012-05-26 14:32:54 -0700884static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700886 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700887{
Jeff Brown6249b902012-05-26 14:32:54 -0700888 struct node* old_parent_node;
889 struct node* new_parent_node;
890 struct node* child_node;
891 char old_parent_path[PATH_MAX];
892 char new_parent_path[PATH_MAX];
893 char old_child_path[PATH_MAX];
894 char new_child_path[PATH_MAX];
895 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400896 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700898
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700899 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700900 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
901 old_parent_path, sizeof(old_parent_path));
902 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
903 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400904 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name
905 << " @ " << std::hex << hdr->nodeid
906 << " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> "
907 << std::hex << req->newdir
908 << " (" << (new_parent_node ? new_parent_node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700909 if (!old_parent_node || !new_parent_node) {
910 res = -ENOENT;
911 goto lookup_error;
912 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700913 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700914 res = -EACCES;
915 goto lookup_error;
916 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700917 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700918 res = -EACCES;
919 goto lookup_error;
920 }
Jeff Brown6249b902012-05-26 14:32:54 -0700921 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
922 if (!child_node || get_node_path_locked(child_node,
923 old_child_path, sizeof(old_child_path)) < 0) {
924 res = -ENOENT;
925 goto lookup_error;
926 }
927 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700928 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700929
930 /* Special case for renaming a file where destination is same path
931 * differing only by case. In this case we don't want to look for a case
932 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
933 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400934 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700935 || strcasecmp(old_name, new_name);
936 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
937 new_child_path, sizeof(new_child_path), search))) {
938 res = -ENOENT;
939 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700940 }
941
Jeff Brown6249b902012-05-26 14:32:54 -0700942 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
943 res = rename(old_child_path, new_child_path);
944 if (res < 0) {
945 res = -errno;
946 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700947 }
948
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700949 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700950 res = rename_node_locked(child_node, new_name, new_actual_name);
951 if (!res) {
952 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700953 derive_permissions_locked(fuse, new_parent_node, child_node);
954 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700955 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700956 }
Jeff Brown6249b902012-05-26 14:32:54 -0700957 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700958
Jeff Brown6249b902012-05-26 14:32:54 -0700959io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700960 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700961done:
962 release_node_locked(child_node);
963lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700964 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700965 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700966}
967
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700968static int open_flags_to_access_mode(int open_flags) {
969 if ((open_flags & O_ACCMODE) == O_RDONLY) {
970 return R_OK;
971 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
972 return W_OK;
973 } else {
974 /* Probably O_RDRW, but treat as default to be safe */
975 return R_OK | W_OK;
976 }
977}
978
Jeff Brown6249b902012-05-26 14:32:54 -0700979static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
981{
Jeff Brown6249b902012-05-26 14:32:54 -0700982 struct node* node;
983 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700985 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700986
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700987 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700988 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400989 DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags
990 << " @ " << std::hex << hdr->nodeid << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700991 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700992
993 if (!node) {
994 return -ENOENT;
995 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700996 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700997 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700998 return -EACCES;
999 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001000 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001002 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001003 }
Jeff Brown6249b902012-05-26 14:32:54 -07001004 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005 h->fd = open(path, req->flags);
1006 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001007 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001008 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001009 }
1010 out.fh = ptr_to_id(h);
1011 out.open_flags = 0;
1012 out.padding = 0;
1013 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001014 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001015}
1016
Jeff Brown6249b902012-05-26 14:32:54 -07001017static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1019{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001020 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001021 __u64 unique = hdr->unique;
1022 __u32 size = req->size;
1023 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001024 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001025 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001026
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001027 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1028 * overlaps the request buffer and will clobber data in the request. This
1029 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001030
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001031 DLOG(INFO) << "[" << handler->token << "] READ " << std::hex << h << "(" << h->fd << ") "
1032 << size << "@" << offset;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001033 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001034 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001035 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001036 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001037 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001038 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001039 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001040 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001041 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042}
1043
Jeff Brown6249b902012-05-26 14:32:54 -07001044static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001045 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1046 const void* buffer)
1047{
1048 struct fuse_write_out out;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001049 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001051 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001052
Arpad Horvath49e93442014-02-18 10:18:25 +01001053 if (req->flags & O_DIRECT) {
1054 memcpy(aligned_buffer, buffer, req->size);
1055 buffer = (const __u8*) aligned_buffer;
1056 }
Jeff Brown6249b902012-05-26 14:32:54 -07001057
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001058 DLOG(INFO) << "[" << handler->token << "] WRITE " << std::hex << h << "(" << h->fd << ") "
1059 << req->size << "@" << req->offset;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001060 res = pwrite64(h->fd, buffer, req->size, req->offset);
1061 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001062 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001063 }
1064 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001065 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001066 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001067 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068}
1069
Jeff Brown6249b902012-05-26 14:32:54 -07001070static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001071 const struct fuse_in_header* hdr)
1072{
Jeff Brown6249b902012-05-26 14:32:54 -07001073 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001074 struct statfs stat;
1075 struct fuse_statfs_out out;
1076 int res;
1077
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001078 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001079 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001080 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001081 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001082 if (res < 0) {
1083 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001084 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001085 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001086 return -errno;
1087 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001088 memset(&out, 0, sizeof(out));
1089 out.st.blocks = stat.f_blocks;
1090 out.st.bfree = stat.f_bfree;
1091 out.st.bavail = stat.f_bavail;
1092 out.st.files = stat.f_files;
1093 out.st.ffree = stat.f_ffree;
1094 out.st.bsize = stat.f_bsize;
1095 out.st.namelen = stat.f_namelen;
1096 out.st.frsize = stat.f_frsize;
1097 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001098 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001099}
1100
Jeff Brown6249b902012-05-26 14:32:54 -07001101static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001102 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1103{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001104 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001105
1106 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107 close(h->fd);
1108 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001109 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001110}
1111
Jeff Brown6249b902012-05-26 14:32:54 -07001112static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001113 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1114{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001115 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1116 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001117
Elliott Hughesf6d67372014-07-08 14:38:26 -07001118 int fd = -1;
1119 if (is_dir) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001120 struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001121 fd = dirfd(dh->d);
1122 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001123 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001124 fd = h->fd;
1125 }
1126
1127 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1128 is_dir ? "FSYNCDIR" : "FSYNC",
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001129 static_cast<struct node*>(id_to_ptr(req->fh)), fd, is_data_sync);
Elliott Hughesf6d67372014-07-08 14:38:26 -07001130 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1131 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001132 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001133 }
Jeff Brown6249b902012-05-26 14:32:54 -07001134 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001135}
1136
Jeff Brown6249b902012-05-26 14:32:54 -07001137static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001138 const struct fuse_in_header* hdr)
1139{
Jeff Brown6249b902012-05-26 14:32:54 -07001140 TRACE("[%d] FLUSH\n", handler->token);
1141 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142}
1143
Jeff Brown6249b902012-05-26 14:32:54 -07001144static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001145 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1146{
Jeff Brown6249b902012-05-26 14:32:54 -07001147 struct node* node;
1148 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001149 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001151
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001152 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001153 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001154 DLOG(INFO) << "[" << handler->token << "] OPENDIR @ " << std::hex << hdr->nodeid
1155 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001156 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001157
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001159 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001161 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001162 return -EACCES;
1163 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001164 h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001166 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001167 }
Jeff Brown6249b902012-05-26 14:32:54 -07001168 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001169 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001170 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001171 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001172 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001173 }
1174 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001175 out.open_flags = 0;
1176 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001177 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001178 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001179}
1180
Jeff Brown6249b902012-05-26 14:32:54 -07001181static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001182 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1183{
1184 char buffer[8192];
1185 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1186 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001187 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001188
1189 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 if (req->offset == 0) {
1191 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001192 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 rewinddir(h->d);
1194 }
1195 de = readdir(h->d);
1196 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001197 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198 }
1199 fde->ino = FUSE_UNKNOWN_INO;
1200 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1201 fde->off = req->offset + 1;
1202 fde->type = de->d_type;
1203 fde->namelen = strlen(de->d_name);
1204 memcpy(fde->name, de->d_name, fde->namelen + 1);
1205 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001206 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1207 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001208}
1209
Jeff Brown6249b902012-05-26 14:32:54 -07001210static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1212{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001213 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001214
1215 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216 closedir(h->d);
1217 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001218 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001219}
1220
Jeff Brown6249b902012-05-26 14:32:54 -07001221static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1223{
1224 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001225 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001226
Jeff Brown6249b902012-05-26 14:32:54 -07001227 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1228 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001229
1230 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1231 * defined (fuse version 7.6). The structure is the same from 7.6 through
1232 * 7.22. Beginning with 7.23, the structure increased in size and added
1233 * new parameters.
1234 */
1235 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1236 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1237 req->major, req->minor, FUSE_KERNEL_VERSION);
1238 return -1;
1239 }
1240
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001241 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1242 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001243 fuse_struct_size = sizeof(out);
1244#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1245 /* FUSE_KERNEL_VERSION >= 23. */
1246
1247 /* If the kernel only works on minor revs older than or equal to 22,
1248 * then use the older structure size since this code only uses the 7.22
1249 * version of the structure. */
1250 if (req->minor <= 22) {
1251 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1252 }
1253#endif
1254
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001255 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001256 out.max_readahead = req->max_readahead;
1257 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1258 out.max_background = 32;
1259 out.congestion_threshold = 32;
1260 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001261 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001262 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001263}
1264
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001265static int handle_canonical_path(struct fuse* fuse, struct fuse_handler* handler,
1266 const struct fuse_in_header *hdr)
1267{
1268 struct node* node;
1269 char path[PATH_MAX];
1270 int len;
1271
1272 pthread_mutex_lock(&fuse->global->lock);
1273 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1274 path, sizeof(path));
1275 TRACE("[%d] CANONICAL_PATH @ %" PRIx64 " (%s)\n", handler->token, hdr->nodeid,
1276 node ? node->name : "?");
1277 pthread_mutex_unlock(&fuse->global->lock);
1278
1279 if (!node) {
1280 return -ENOENT;
1281 }
1282 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
1283 return -EACCES;
1284 }
1285 len = strlen(path);
1286 if (len + 1 > PATH_MAX)
1287 len = PATH_MAX - 1;
1288 path[PATH_MAX - 1] = 0;
1289 fuse_reply(fuse, hdr->unique, path, len + 1);
1290 return NO_STATUS;
1291}
1292
1293
Jeff Brown6249b902012-05-26 14:32:54 -07001294static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001295 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1296{
Brian Swetland03ee9472010-08-12 18:01:08 -07001297 switch (hdr->opcode) {
1298 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001299 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001300 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001301 }
Jeff Brown84715842012-05-25 14:07:47 -07001302
Brian Swetland03ee9472010-08-12 18:01:08 -07001303 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001304 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001305 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001306 }
Jeff Brown84715842012-05-25 14:07:47 -07001307
Brian Swetland03ee9472010-08-12 18:01:08 -07001308 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001309 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001310 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001311 }
Jeff Brown84715842012-05-25 14:07:47 -07001312
Brian Swetland03ee9472010-08-12 18:01:08 -07001313 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001314 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001315 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001316 }
Jeff Brown84715842012-05-25 14:07:47 -07001317
Brian Swetland03ee9472010-08-12 18:01:08 -07001318// case FUSE_READLINK:
1319// case FUSE_SYMLINK:
1320 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001321 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001322 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001323 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001324 }
Jeff Brown84715842012-05-25 14:07:47 -07001325
Brian Swetland03ee9472010-08-12 18:01:08 -07001326 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001327 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001328 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001329 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001330 }
Jeff Brown84715842012-05-25 14:07:47 -07001331
Brian Swetland03ee9472010-08-12 18:01:08 -07001332 case FUSE_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001333 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001334 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001335 }
Jeff Brown84715842012-05-25 14:07:47 -07001336
Brian Swetland03ee9472010-08-12 18:01:08 -07001337 case FUSE_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001338 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001339 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001340 }
Jeff Brown84715842012-05-25 14:07:47 -07001341
Brian Swetland03ee9472010-08-12 18:01:08 -07001342 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001343 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001344 const char *old_name = ((const char*) data) + sizeof(*req);
1345 const char *new_name = old_name + strlen(old_name) + 1;
1346 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001347 }
Jeff Brown84715842012-05-25 14:07:47 -07001348
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001350 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001351 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001352 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001353 }
Jeff Brown84715842012-05-25 14:07:47 -07001354
Brian Swetland03ee9472010-08-12 18:01:08 -07001355 case FUSE_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001356 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001357 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001358 }
Jeff Brown84715842012-05-25 14:07:47 -07001359
Brian Swetland03ee9472010-08-12 18:01:08 -07001360 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001361 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001362 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001363 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001364 }
Jeff Brown84715842012-05-25 14:07:47 -07001365
Mike Lockwood4553b082010-08-16 14:14:44 -04001366 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001367 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001368 }
Jeff Brown84715842012-05-25 14:07:47 -07001369
Brian Swetland03ee9472010-08-12 18:01:08 -07001370 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001371 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001372 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001373 }
Jeff Brown84715842012-05-25 14:07:47 -07001374
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001375 case FUSE_FSYNC:
1376 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001377 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001378 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001379 }
1380
Brian Swetland03ee9472010-08-12 18:01:08 -07001381// case FUSE_SETXATTR:
1382// case FUSE_GETXATTR:
1383// case FUSE_LISTXATTR:
1384// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001385 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001386 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001387 }
1388
Brian Swetland03ee9472010-08-12 18:01:08 -07001389 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001390 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001391 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001392 }
Jeff Brown84715842012-05-25 14:07:47 -07001393
Brian Swetland03ee9472010-08-12 18:01:08 -07001394 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001395 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001396 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001397 }
Jeff Brown84715842012-05-25 14:07:47 -07001398
Brian Swetland03ee9472010-08-12 18:01:08 -07001399 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001400 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001401 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001402 }
Jeff Brown84715842012-05-25 14:07:47 -07001403
Brian Swetland03ee9472010-08-12 18:01:08 -07001404 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001405 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001406 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001407 }
Jeff Brown84715842012-05-25 14:07:47 -07001408
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001409 case FUSE_CANONICAL_PATH: { /* nodeid -> bytez[] */
1410 return handle_canonical_path(fuse, handler, hdr);
1411 }
1412
Brian Swetland03ee9472010-08-12 18:01:08 -07001413 default: {
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001414 DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode
1415 << "uniq=" << std::hex << hdr->unique << "nid=" << std::hex << hdr->nodeid;
Jeff Brown6249b902012-05-26 14:32:54 -07001416 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001417 }
Jeff Brown84715842012-05-25 14:07:47 -07001418 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001419}
1420
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001421void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001422{
Jeff Brown6249b902012-05-26 14:32:54 -07001423 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001424 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001425 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1426 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001427 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001428 if (errno == ENODEV) {
1429 ERROR("[%d] someone stole our marbles!\n", handler->token);
1430 exit(2);
1431 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001432 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001433 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001434 }
Jeff Brown84715842012-05-25 14:07:47 -07001435
1436 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001437 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1438 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001439 }
1440
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001441 const struct fuse_in_header* hdr =
1442 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001443 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001444 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1445 handler->token, (size_t)len, hdr->len);
1446 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001447 }
1448
Jeff Brown7729d242012-05-25 15:35:28 -07001449 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001450 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001451 __u64 unique = hdr->unique;
1452 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001453
1454 /* We do not access the request again after this point because the underlying
1455 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001456
1457 if (res != NO_STATUS) {
1458 if (res) {
1459 TRACE("[%d] ERROR %d\n", handler->token, res);
1460 }
1461 fuse_status(fuse, unique, res);
1462 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001463 }
1464}