blob: 3f0f95fe2e1c123239081f9a41e89a685b1d58f5 [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
Christopher Ferrisd6b0d372016-10-06 12:51:20 -070017#include <errno.h>
18#include <string.h>
19#include <unistd.h>
20
Elliott Hughes300d5642014-07-08 13:53:26 -070021#define LOG_TAG "sdcard"
22
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040023#include "fuse.h"
Brian Swetlandb14a2c62010-08-12 18:21:12 -070024
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -040025#include <android-base/logging.h>
26
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -070027/* FUSE_CANONICAL_PATH is not currently upstreamed */
28#define FUSE_CANONICAL_PATH 2016
29
Brian Swetland03ee9472010-08-12 18:01:08 -070030#define FUSE_UNKNOWN_INO 0xffffffff
31
Jeff Brown6249b902012-05-26 14:32:54 -070032/* Pseudo-error constant used to indicate that no fuse status is needed
33 * or that a reply has already been written. */
34#define NO_STATUS 1
35
Jeff Brown6249b902012-05-26 14:32:54 -070036static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -070037{
Jeff Brown6249b902012-05-26 14:32:54 -070038 return (void *) (uintptr_t) nid;
39}
Brian Swetland03ee9472010-08-12 18:01:08 -070040
Jeff Brown6249b902012-05-26 14:32:54 -070041static inline __u64 ptr_to_id(void *ptr)
42{
43 return (__u64) (uintptr_t) ptr;
44}
Brian Swetland03ee9472010-08-12 18:01:08 -070045
Jeff Brown6249b902012-05-26 14:32:54 -070046static void acquire_node_locked(struct node* node)
47{
48 node->refcount++;
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040049 DLOG(INFO) << "ACQUIRE " << std::hex << node << std::dec
50 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070051}
52
53static void remove_node_from_parent_locked(struct node* node);
54
55static void release_node_locked(struct node* node)
56{
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040057 DLOG(INFO) << "RELEASE " << std::hex << node << std::dec
58 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070059 if (node->refcount > 0) {
60 node->refcount--;
61 if (!node->refcount) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040062 DLOG(INFO) << "DESTROY " << std::hex << node << std::dec << " (" << node->name << ")";
Jeff Brown6249b902012-05-26 14:32:54 -070063 remove_node_from_parent_locked(node);
64
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040065 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070066 memset(node->name, 0xef, node->namelen);
67 free(node->name);
68 free(node->actual_name);
69 memset(node, 0xfc, sizeof(*node));
70 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080071 }
Jeff Brown6249b902012-05-26 14:32:54 -070072 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040073 LOG(ERROR) << std::hex << node << std::dec << " refcount=0";
Jeff Brown6249b902012-05-26 14:32:54 -070074 }
75}
76
77static void add_node_to_parent_locked(struct node *node, struct node *parent) {
78 node->parent = parent;
79 node->next = parent->child;
80 parent->child = node;
81 acquire_node_locked(parent);
82}
83
84static void remove_node_from_parent_locked(struct node* node)
85{
86 if (node->parent) {
87 if (node->parent->child == node) {
88 node->parent->child = node->parent->child->next;
89 } else {
90 struct node *node2;
91 node2 = node->parent->child;
92 while (node2->next != node)
93 node2 = node2->next;
94 node2->next = node->next;
95 }
96 release_node_locked(node->parent);
97 node->parent = NULL;
98 node->next = NULL;
99 }
100}
101
102/* Gets the absolute path to a node into the provided buffer.
103 *
104 * Populates 'buf' with the path and returns the length of the path on success,
105 * or returns -1 if the path is too long for the provided buffer.
106 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700107static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
108 const char* name;
109 size_t namelen;
110 if (node->graft_path) {
111 name = node->graft_path;
112 namelen = node->graft_pathlen;
113 } else if (node->actual_name) {
114 name = node->actual_name;
115 namelen = node->namelen;
116 } else {
117 name = node->name;
118 namelen = node->namelen;
119 }
120
Jeff Brown6249b902012-05-26 14:32:54 -0700121 if (bufsize < namelen + 1) {
122 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700123 }
124
Jeff Brown6249b902012-05-26 14:32:54 -0700125 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700126 if (node->parent && node->graft_path == NULL) {
Daniel Rosenbergdb4638e2016-04-12 16:30:28 -0700127 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700128 if (pathlen < 0) {
129 return -1;
130 }
131 buf[pathlen++] = '/';
132 }
133
Jeff Brown6249b902012-05-26 14:32:54 -0700134 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
135 return pathlen + namelen;
136}
137
138/* Finds the absolute path of a file within a given directory.
139 * Performs a case-insensitive search for the file and sets the buffer to the path
140 * of the first matching file. If 'search' is zero or if no match is found, sets
141 * the buffer to the path that the file would have, assuming the name were case-sensitive.
142 *
143 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
144 * or returns NULL if the path is too long for the provided buffer.
145 */
146static char* find_file_within(const char* path, const char* name,
147 char* buf, size_t bufsize, int search)
148{
149 size_t pathlen = strlen(path);
150 size_t namelen = strlen(name);
151 size_t childlen = pathlen + namelen + 1;
152 char* actual;
153
154 if (bufsize <= childlen) {
155 return NULL;
156 }
157
158 memcpy(buf, path, pathlen);
159 buf[pathlen] = '/';
160 actual = buf + pathlen + 1;
161 memcpy(actual, name, namelen + 1);
162
163 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800164 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700165 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800166 if (!dir) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400167 PLOG(ERROR) << "opendir(" << path << ") failed";
Jeff Brown6249b902012-05-26 14:32:54 -0700168 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800169 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800170 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700171 if (!strcasecmp(entry->d_name, name)) {
172 /* we have a match - replace the name, don't need to copy the null again */
173 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800174 break;
175 }
176 }
177 closedir(dir);
178 }
Jeff Brown6249b902012-05-26 14:32:54 -0700179 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800180}
181
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700182static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
183 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000184 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700185 attr->size = s->st_size;
186 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800187 attr->atime = s->st_atim.tv_sec;
188 attr->mtime = s->st_mtim.tv_sec;
189 attr->ctime = s->st_ctim.tv_sec;
190 attr->atimensec = s->st_atim.tv_nsec;
191 attr->mtimensec = s->st_mtim.tv_nsec;
192 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700193 attr->mode = s->st_mode;
194 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700195
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700196 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700197
198 if (fuse->gid == AID_SDCARD_RW) {
199 /* As an optimization, certain trusted system components only run
200 * as owner but operate across all users. Since we're now handing
201 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
202 * the user boundary enforcement for the default view. The UIDs
203 * assigned to app directories are still multiuser aware. */
204 attr->gid = AID_SDCARD_RW;
205 } else {
206 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
207 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700208
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700209 int visible_mode = 0775 & ~fuse->mask;
210 if (node->perm == PERM_PRE_ROOT) {
211 /* Top of multi-user view should always be visible to ensure
212 * secondary users can traverse inside. */
213 visible_mode = 0711;
214 } else if (node->under_android) {
215 /* Block "other" access to Android directories, since only apps
216 * belonging to a specific user should be in there; we still
217 * leave +x open for the default view. */
218 if (fuse->gid == AID_SDCARD_RW) {
219 visible_mode = visible_mode & ~0006;
220 } else {
221 visible_mode = visible_mode & ~0007;
222 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700223 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700224 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700225 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700226 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
227}
228
Jeff Sharkey44d63422013-09-12 09:44:48 -0700229static int touch(char* path, mode_t mode) {
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700230 int fd = TEMP_FAILURE_RETRY(open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
231 mode));
Jeff Sharkey44d63422013-09-12 09:44:48 -0700232 if (fd == -1) {
233 if (errno == EEXIST) {
234 return 0;
235 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400236 PLOG(ERROR) << "open(" << path << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700237 return -1;
238 }
239 }
240 close(fd);
241 return 0;
242}
243
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700244static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
245 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700246 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700247
248 /* By default, each node inherits from its parent */
249 node->perm = PERM_INHERIT;
250 node->userid = parent->userid;
251 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700252 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700253
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700254 /* Derive custom permissions based on parent and current node */
255 switch (parent->perm) {
256 case PERM_INHERIT:
257 /* Already inherited above */
258 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700259 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700260 /* Legacy internal layout places users at top level */
261 node->perm = PERM_ROOT;
262 node->userid = strtoul(node->name, NULL, 10);
263 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700264 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700265 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700266 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700267 /* App-specific directories inside; let anyone traverse */
268 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700269 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700270 }
271 break;
272 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700273 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700274 /* App-specific directories inside; let anyone traverse */
275 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700276 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700277 /* App-specific directories inside; let anyone traverse */
278 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700279 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700280 node->graft_path = fuse->global->obb_path;
281 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700282 } else if (!strcasecmp(node->name, "media")) {
283 /* App-specific directories inside; let anyone traverse */
284 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700285 }
286 break;
287 case PERM_ANDROID_DATA:
288 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700289 case PERM_ANDROID_MEDIA:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400290 const auto& iter = fuse->global->package_to_appid->find(node->name);
291 if (iter != fuse->global->package_to_appid->end()) {
292 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700293 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700294 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700295 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700296 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700297}
298
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400299void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700300 struct node *node;
301 for (node = parent->child; node; node = node->next) {
302 derive_permissions_locked(fuse, parent, node);
303 if (node->child) {
304 derive_permissions_recursive_locked(fuse, node);
305 }
306 }
307}
308
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700309/* Kernel has already enforced everything we returned through
310 * derive_permissions_locked(), so this is used to lock down access
311 * even further, such as enforcing that apps hold sdcard_rw. */
312static bool check_caller_access_to_name(struct fuse* fuse,
313 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700314 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700315 /* Always block security-sensitive files at root */
316 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700317 if (!strcasecmp(name, "autorun.inf")
318 || !strcasecmp(name, ".android_secure")
319 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700320 return false;
321 }
322 }
323
Jeff Sharkey44d63422013-09-12 09:44:48 -0700324 /* Root always has access; access for any other UIDs should always
325 * be controlled through packages.list. */
326 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700327 return true;
328 }
329
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700330 /* No extra permissions to enforce */
331 return true;
332}
333
334static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700335 const struct fuse_in_header *hdr, const struct node* node, int mode) {
336 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700337}
338
Jeff Brown6249b902012-05-26 14:32:54 -0700339struct node *create_node_locked(struct fuse* fuse,
340 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700341{
342 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700343 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700344
Narayan Kamathfaa09352015-01-13 18:21:10 +0000345 // Detect overflows in the inode counter. "4 billion nodes should be enough
346 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700347 if (fuse->global->inode_ctr == 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400348 LOG(ERROR) << "No more inode numbers available";
Narayan Kamathfaa09352015-01-13 18:21:10 +0000349 return NULL;
350 }
351
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400352 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700353 if (!node) {
354 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700355 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400356 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700357 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700358 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700359 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700360 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700361 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700362 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400363 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700364 if (!node->actual_name) {
365 free(node->name);
366 free(node);
367 return NULL;
368 }
369 memcpy(node->actual_name, actual_name, namelen + 1);
370 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700371 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700372 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700373 node->ino = fuse->global->inode_ctr++;
374 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700375
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200376 node->deleted = false;
377
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700378 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700379 acquire_node_locked(node);
380 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700381 return node;
382}
383
Jeff Brown6249b902012-05-26 14:32:54 -0700384static int rename_node_locked(struct node *node, const char *name,
385 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700386{
Jeff Brown6249b902012-05-26 14:32:54 -0700387 size_t namelen = strlen(name);
388 int need_actual_name = strcmp(name, actual_name);
389
390 /* make the storage bigger without actually changing the name
391 * in case an error occurs part way */
392 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400393 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700394 if (!new_name) {
395 return -ENOMEM;
396 }
397 node->name = new_name;
398 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400399 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700400 if (!new_actual_name) {
401 return -ENOMEM;
402 }
403 node->actual_name = new_actual_name;
404 }
405 }
406
407 /* update the name, taking care to allocate storage before overwriting the old name */
408 if (need_actual_name) {
409 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400410 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700411 if (!node->actual_name) {
412 return -ENOMEM;
413 }
414 }
415 memcpy(node->actual_name, actual_name, namelen + 1);
416 } else {
417 free(node->actual_name);
418 node->actual_name = NULL;
419 }
420 memcpy(node->name, name, namelen + 1);
421 node->namelen = namelen;
422 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700423}
424
Jeff Brown6249b902012-05-26 14:32:54 -0700425static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700426{
Jeff Brown6249b902012-05-26 14:32:54 -0700427 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700428 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700429 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400430 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700431 }
432}
433
Jeff Brown6249b902012-05-26 14:32:54 -0700434static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
435 char* buf, size_t bufsize)
436{
437 struct node* node = lookup_node_by_id_locked(fuse, nid);
438 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
439 node = NULL;
440 }
441 return node;
442}
443
444static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700445{
446 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700447 /* use exact string comparison, nodes that differ by case
448 * must be considered distinct even if they refer to the same
449 * underlying file as otherwise operations such as "mv x x"
450 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200451 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700452 return node;
453 }
454 }
455 return 0;
456}
457
Jeff Brown6249b902012-05-26 14:32:54 -0700458static struct node* acquire_or_create_child_locked(
459 struct fuse* fuse, struct node* parent,
460 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700461{
Jeff Brown6249b902012-05-26 14:32:54 -0700462 struct node* child = lookup_child_by_name_locked(parent, name);
463 if (child) {
464 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800465 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700466 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800467 }
Jeff Brown6249b902012-05-26 14:32:54 -0700468 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700469}
470
Jeff Brown6249b902012-05-26 14:32:54 -0700471static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700472{
473 struct fuse_out_header hdr;
474 hdr.len = sizeof(hdr);
475 hdr.error = err;
476 hdr.unique = unique;
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700477 ssize_t ret = TEMP_FAILURE_RETRY(write(fuse->fd, &hdr, sizeof(hdr)));
478 if (ret == -1) {
479 PLOG(ERROR) << "*** STATUS FAILED ***";
480 } else if (static_cast<size_t>(ret) != sizeof(hdr)) {
481 LOG(ERROR) << "*** STATUS FAILED: written " << ret << " expected "
482 << sizeof(hdr) << " ***";
483 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700484}
485
Jeff Brown6249b902012-05-26 14:32:54 -0700486static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700487{
488 struct fuse_out_header hdr;
Brian Swetland03ee9472010-08-12 18:01:08 -0700489 hdr.len = len + sizeof(hdr);
490 hdr.error = 0;
491 hdr.unique = unique;
492
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700493 struct iovec vec[2];
Brian Swetland03ee9472010-08-12 18:01:08 -0700494 vec[0].iov_base = &hdr;
495 vec[0].iov_len = sizeof(hdr);
496 vec[1].iov_base = data;
497 vec[1].iov_len = len;
498
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700499 ssize_t ret = TEMP_FAILURE_RETRY(writev(fuse->fd, vec, 2));
500 if (ret == -1) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400501 PLOG(ERROR) << "*** REPLY FAILED ***";
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700502 } else if (static_cast<size_t>(ret) != sizeof(hdr) + len) {
503 LOG(ERROR) << "*** REPLY FAILED: written " << ret << " expected "
504 << sizeof(hdr) + len << " ***";
Brian Swetland03ee9472010-08-12 18:01:08 -0700505 }
506}
507
Jeff Brown6249b902012-05-26 14:32:54 -0700508static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
509 struct node* parent, const char* name, const char* actual_name,
510 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700511{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700512 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700513 struct fuse_entry_out out;
514 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700515
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700516 if (lstat(path, &s) == -1) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700517 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700518 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700519
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700520 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700521 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
522 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700523 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700524 return -ENOMEM;
525 }
526 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700527 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700528 out.attr_valid = 10;
529 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700530 out.nodeid = node->nid;
531 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700532 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700533 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700534 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700535}
536
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700537static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700538 const char* path)
539{
540 struct fuse_attr_out out;
541 struct stat s;
542
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700543 if (lstat(path, &s) == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -0700544 return -errno;
545 }
546 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700547 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700548 out.attr_valid = 10;
549 fuse_reply(fuse, unique, &out, sizeof(out));
550 return NO_STATUS;
551}
552
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700553static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
554 const __u64 child, const char* name) {
555 struct fuse_out_header hdr;
556 struct fuse_notify_delete_out data;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700557 size_t namelen = strlen(name);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700558 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
559 hdr.error = FUSE_NOTIFY_DELETE;
560 hdr.unique = 0;
561
562 data.parent = parent;
563 data.child = child;
564 data.namelen = namelen;
565 data.padding = 0;
566
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700567 struct iovec vec[3];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700568 vec[0].iov_base = &hdr;
569 vec[0].iov_len = sizeof(hdr);
570 vec[1].iov_base = &data;
571 vec[1].iov_len = sizeof(data);
572 vec[2].iov_base = (void*) name;
573 vec[2].iov_len = namelen + 1;
574
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700575 ssize_t ret = TEMP_FAILURE_RETRY(writev(fuse->fd, vec, 3));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700576 /* Ignore ENOENT, since other views may not have seen the entry */
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700577 if (ret == -1) {
578 if (errno != ENOENT) {
579 PLOG(ERROR) << "*** NOTIFY FAILED ***";
580 }
581 } else if (static_cast<size_t>(ret) != sizeof(hdr) + sizeof(data) + namelen + 1) {
582 LOG(ERROR) << "*** NOTIFY FAILED: written " << ret << " expected "
583 << sizeof(hdr) + sizeof(data) + namelen + 1 << " ***";
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700584 }
585}
586
Jeff Brown6249b902012-05-26 14:32:54 -0700587static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700588 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700589{
Jeff Brown6249b902012-05-26 14:32:54 -0700590 struct node* parent_node;
591 char parent_path[PATH_MAX];
592 char child_path[PATH_MAX];
593 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700594
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700595 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700596 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
597 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400598 DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid
599 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700600 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700601
602 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
603 child_path, sizeof(child_path), 1))) {
604 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700605 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700606 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700607 return -EACCES;
608 }
609
Jeff Brown6249b902012-05-26 14:32:54 -0700610 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700611}
612
Jeff Brown6249b902012-05-26 14:32:54 -0700613static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700614 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
615{
Jeff Brown6249b902012-05-26 14:32:54 -0700616 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700617
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700618 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700619 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400620 DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup
621 << " @ " << std::hex << hdr->nodeid
622 << " (" << (node ? node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700623 if (node) {
624 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400625 while (n) {
626 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700627 release_node_locked(node);
628 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700629 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700630 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700631 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700632}
633
Jeff Brown6249b902012-05-26 14:32:54 -0700634static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700635 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
636{
Jeff Brown6249b902012-05-26 14:32:54 -0700637 struct node* node;
638 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700639
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700640 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700641 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400642 DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400643 << " fh=" << std::hex << req->fh << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400644 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700645 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700646
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700647 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700648 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700649 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700650 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700651 return -EACCES;
652 }
653
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700654 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700655}
656
Jeff Brown6249b902012-05-26 14:32:54 -0700657static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700658 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
659{
Jeff Brown6249b902012-05-26 14:32:54 -0700660 struct node* node;
661 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700662 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700663
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700664 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700665 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400666 DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400667 << " valid=" << std::hex << req->valid << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400668 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700669 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700670
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700671 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700672 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700673 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800674
675 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700676 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700677 return -EACCES;
678 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700679
Jeff Brown6249b902012-05-26 14:32:54 -0700680 /* XXX: incomplete implementation on purpose.
681 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700682
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700683 if ((req->valid & FATTR_SIZE) && TEMP_FAILURE_RETRY(truncate64(path, req->size)) == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -0700684 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700685 }
686
687 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
688 * are both set, then set it to the current time. Else, set it to the
689 * time specified in the request. Same goes for mtime. Use utimensat(2)
690 * as it allows ATIME and MTIME to be changed independently, and has
691 * nanosecond resolution which fuse also has.
692 */
693 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
694 times[0].tv_nsec = UTIME_OMIT;
695 times[1].tv_nsec = UTIME_OMIT;
696 if (req->valid & FATTR_ATIME) {
697 if (req->valid & FATTR_ATIME_NOW) {
698 times[0].tv_nsec = UTIME_NOW;
699 } else {
700 times[0].tv_sec = req->atime;
701 times[0].tv_nsec = req->atimensec;
702 }
703 }
704 if (req->valid & FATTR_MTIME) {
705 if (req->valid & FATTR_MTIME_NOW) {
706 times[1].tv_nsec = UTIME_NOW;
707 } else {
708 times[1].tv_sec = req->mtime;
709 times[1].tv_nsec = req->mtimensec;
710 }
711 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400712 DLOG(INFO) << "[" << handler->token << "] Calling utimensat on " << path
713 << " with atime " << times[0].tv_sec << ", mtime=" << times[1].tv_sec;
Jeff Brown6249b902012-05-26 14:32:54 -0700714 if (utimensat(-1, path, times, 0) < 0) {
715 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700716 }
717 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700718 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700719}
720
Jeff Brown6249b902012-05-26 14:32:54 -0700721static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700722 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
723{
Jeff Brown6249b902012-05-26 14:32:54 -0700724 struct node* parent_node;
725 char parent_path[PATH_MAX];
726 char child_path[PATH_MAX];
727 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700728
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700729 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700730 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
731 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400732 DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode
733 << " @ " << std::hex << hdr->nodeid
734 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700735 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700736
737 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
738 child_path, sizeof(child_path), 1))) {
739 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700740 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700741 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700742 return -EACCES;
743 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700744 __u32 mode = (req->mode & (~0777)) | 0664;
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700745 if (mknod(child_path, mode, req->rdev) == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -0700746 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700747 }
Jeff Brown6249b902012-05-26 14:32:54 -0700748 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700749}
750
Jeff Brown6249b902012-05-26 14:32:54 -0700751static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700752 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
753{
Jeff Brown6249b902012-05-26 14:32:54 -0700754 struct node* parent_node;
755 char parent_path[PATH_MAX];
756 char child_path[PATH_MAX];
757 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700758
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700759 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700760 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
761 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400762 DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode
763 << " @ " << std::hex << hdr->nodeid
764 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700765 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700766
767 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
768 child_path, sizeof(child_path), 1))) {
769 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700770 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700771 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700772 return -EACCES;
773 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700774 __u32 mode = (req->mode & (~0777)) | 0775;
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700775 if (mkdir(child_path, mode) == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -0700776 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700777 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700778
779 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
780 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
781 char nomedia[PATH_MAX];
782 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
783 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400784 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700785 return -ENOENT;
786 }
787 }
788 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
789 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700790 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700791 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400792 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700793 return -ENOENT;
794 }
795 }
796
Jeff Brown6249b902012-05-26 14:32:54 -0700797 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700798}
799
Jeff Brown6249b902012-05-26 14:32:54 -0700800static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700801 const struct fuse_in_header* hdr, const char* name)
802{
Jeff Brown6249b902012-05-26 14:32:54 -0700803 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200804 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700805 char parent_path[PATH_MAX];
806 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700807
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700808 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700809 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
810 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400811 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
812 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700813 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700814
815 if (!parent_node || !find_file_within(parent_path, name,
816 child_path, sizeof(child_path), 1)) {
817 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700819 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700820 return -EACCES;
821 }
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700822 if (unlink(child_path) == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -0700823 return -errno;
824 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700825 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200826 child_node = lookup_child_by_name_locked(parent_node, name);
827 if (child_node) {
828 child_node->deleted = true;
829 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700830 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700831 if (parent_node && child_node) {
832 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400833 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
834 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400835 << ", child=" << std::hex << child_node->nid << std::dec
836 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700837 if (fuse != fuse->global->fuse_default) {
838 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
839 }
840 if (fuse != fuse->global->fuse_read) {
841 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
842 }
843 if (fuse != fuse->global->fuse_write) {
844 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
845 }
846 }
Jeff Brown6249b902012-05-26 14:32:54 -0700847 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700848}
849
Jeff Brown6249b902012-05-26 14:32:54 -0700850static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700851 const struct fuse_in_header* hdr, const char* name)
852{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200853 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700854 struct node* parent_node;
855 char parent_path[PATH_MAX];
856 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700857
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700858 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700859 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
860 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400861 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
862 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700863 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700864
865 if (!parent_node || !find_file_within(parent_path, name,
866 child_path, sizeof(child_path), 1)) {
867 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700869 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700870 return -EACCES;
871 }
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700872 if (rmdir(child_path) == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -0700873 return -errno;
874 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700875 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200876 child_node = lookup_child_by_name_locked(parent_node, name);
877 if (child_node) {
878 child_node->deleted = true;
879 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700880 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700881 if (parent_node && child_node) {
882 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400883 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
884 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400885 << ", child=" << std::hex << child_node->nid << std::dec
886 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700887 if (fuse != fuse->global->fuse_default) {
888 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
889 }
890 if (fuse != fuse->global->fuse_read) {
891 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
892 }
893 if (fuse != fuse->global->fuse_write) {
894 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
895 }
896 }
Jeff Brown6249b902012-05-26 14:32:54 -0700897 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700898}
899
Jeff Brown6249b902012-05-26 14:32:54 -0700900static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700901 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700902 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700903{
Jeff Brown6249b902012-05-26 14:32:54 -0700904 struct node* old_parent_node;
905 struct node* new_parent_node;
906 struct node* child_node;
907 char old_parent_path[PATH_MAX];
908 char new_parent_path[PATH_MAX];
909 char old_child_path[PATH_MAX];
910 char new_child_path[PATH_MAX];
911 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400912 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700913 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700914
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700915 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700916 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
917 old_parent_path, sizeof(old_parent_path));
918 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
919 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400920 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name
921 << " @ " << std::hex << hdr->nodeid
922 << " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> "
923 << std::hex << req->newdir
924 << " (" << (new_parent_node ? new_parent_node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700925 if (!old_parent_node || !new_parent_node) {
926 res = -ENOENT;
927 goto lookup_error;
928 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700929 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700930 res = -EACCES;
931 goto lookup_error;
932 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700933 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700934 res = -EACCES;
935 goto lookup_error;
936 }
Jeff Brown6249b902012-05-26 14:32:54 -0700937 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
938 if (!child_node || get_node_path_locked(child_node,
939 old_child_path, sizeof(old_child_path)) < 0) {
940 res = -ENOENT;
941 goto lookup_error;
942 }
943 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700944 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700945
946 /* Special case for renaming a file where destination is same path
947 * differing only by case. In this case we don't want to look for a case
948 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
949 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400950 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700951 || strcasecmp(old_name, new_name);
952 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
953 new_child_path, sizeof(new_child_path), search))) {
954 res = -ENOENT;
955 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700956 }
957
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400958 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_child_path << "->" << new_child_path;
Jeff Brown6249b902012-05-26 14:32:54 -0700959 res = rename(old_child_path, new_child_path);
Christopher Ferrisd6b0d372016-10-06 12:51:20 -0700960 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -0700961 res = -errno;
962 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700963 }
964
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700965 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700966 res = rename_node_locked(child_node, new_name, new_actual_name);
967 if (!res) {
968 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700969 derive_permissions_locked(fuse, new_parent_node, child_node);
970 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700971 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700972 }
Jeff Brown6249b902012-05-26 14:32:54 -0700973 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974
Jeff Brown6249b902012-05-26 14:32:54 -0700975io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700976 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700977done:
978 release_node_locked(child_node);
979lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700980 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700981 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700982}
983
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700984static int open_flags_to_access_mode(int open_flags) {
985 if ((open_flags & O_ACCMODE) == O_RDONLY) {
986 return R_OK;
987 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
988 return W_OK;
989 } else {
990 /* Probably O_RDRW, but treat as default to be safe */
991 return R_OK | W_OK;
992 }
993}
994
Jeff Brown6249b902012-05-26 14:32:54 -0700995static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
997{
Jeff Brown6249b902012-05-26 14:32:54 -0700998 struct node* node;
999 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001000 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001002
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001003 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001004 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001005 DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001006 << " @ " << std::hex << hdr->nodeid << std::dec
1007 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001008 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001009
1010 if (!node) {
1011 return -ENOENT;
1012 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001013 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001014 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001015 return -EACCES;
1016 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001017 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001019 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001020 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001021 DLOG(INFO) << "[" << handler->token << "] OPEN " << path;
Christopher Ferrisd6b0d372016-10-06 12:51:20 -07001022 h->fd = TEMP_FAILURE_RETRY(open(path, req->flags));
1023 if (h->fd == -1) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001024 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001025 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026 }
1027 out.fh = ptr_to_id(h);
1028 out.open_flags = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001029
1030#ifdef FUSE_SHORTCIRCUIT
1031 out.lower_fd = h->fd;
1032#else
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001033 out.padding = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001034#endif
1035
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001036 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001037 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001038}
1039
Jeff Brown6249b902012-05-26 14:32:54 -07001040static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001041 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1042{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001043 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001044 __u64 unique = hdr->unique;
1045 __u32 size = req->size;
1046 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001047 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001048 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001049
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1051 * overlaps the request buffer and will clobber data in the request. This
1052 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001053
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001054 DLOG(INFO) << "[" << handler->token << "] READ " << std::hex << h << std::dec
1055 << "(" << h->fd << ") " << size << "@" << offset;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001056 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001057 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001058 }
Christopher Ferrisd6b0d372016-10-06 12:51:20 -07001059 res = TEMP_FAILURE_RETRY(pread64(h->fd, read_buffer, size, offset));
1060 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001061 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001062 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001063 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001064 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001065}
1066
Jeff Brown6249b902012-05-26 14:32:54 -07001067static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1069 const void* buffer)
1070{
1071 struct fuse_write_out out;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001072 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001074 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001075
Arpad Horvath49e93442014-02-18 10:18:25 +01001076 if (req->flags & O_DIRECT) {
1077 memcpy(aligned_buffer, buffer, req->size);
1078 buffer = (const __u8*) aligned_buffer;
1079 }
Jeff Brown6249b902012-05-26 14:32:54 -07001080
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001081 DLOG(INFO) << "[" << handler->token << "] WRITE " << std::hex << h << std::dec
1082 << "(" << h->fd << ") " << req->size << "@" << req->offset;
Christopher Ferrisd6b0d372016-10-06 12:51:20 -07001083 res = TEMP_FAILURE_RETRY(pwrite64(h->fd, buffer, req->size, req->offset));
1084 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001085 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001086 }
1087 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001088 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001090 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001091}
1092
Jeff Brown6249b902012-05-26 14:32:54 -07001093static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001094 const struct fuse_in_header* hdr)
1095{
Jeff Brown6249b902012-05-26 14:32:54 -07001096 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001097 struct statfs stat;
1098 struct fuse_statfs_out out;
1099 int res;
1100
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001101 pthread_mutex_lock(&fuse->global->lock);
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001102 DLOG(INFO) << "[" << handler->token << "] STATFS";
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001103 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001104 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001105 if (res < 0) {
1106 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107 }
Christopher Ferrisd6b0d372016-10-06 12:51:20 -07001108 if (TEMP_FAILURE_RETRY(statfs(fuse->global->root.name, &stat)) == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001109 return -errno;
1110 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001111 memset(&out, 0, sizeof(out));
1112 out.st.blocks = stat.f_blocks;
1113 out.st.bfree = stat.f_bfree;
1114 out.st.bavail = stat.f_bavail;
1115 out.st.files = stat.f_files;
1116 out.st.ffree = stat.f_ffree;
1117 out.st.bsize = stat.f_bsize;
1118 out.st.namelen = stat.f_namelen;
1119 out.st.frsize = stat.f_frsize;
1120 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001121 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001122}
1123
Jeff Brown6249b902012-05-26 14:32:54 -07001124static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001125 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1126{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001127 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001128
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001129 DLOG(INFO) << "[" << handler->token << "] RELEASE " << std::hex << h << std::dec
1130 << "(" << h->fd << ")";
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001131 close(h->fd);
1132 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001133 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001134}
1135
Jeff Brown6249b902012-05-26 14:32:54 -07001136static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001137 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1138{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001139 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1140 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001141
Elliott Hughesf6d67372014-07-08 14:38:26 -07001142 int fd = -1;
1143 if (is_dir) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001144 struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001145 fd = dirfd(dh->d);
1146 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001147 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001148 fd = h->fd;
1149 }
1150
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001151 DLOG(INFO) << "[" << handler->token << "] " << (is_dir ? "FSYNCDIR" : "FSYNC") << " "
1152 << std::hex << req->fh << std::dec << "(" << fd << ") is_data_sync=" << is_data_sync;
Elliott Hughesf6d67372014-07-08 14:38:26 -07001153 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1154 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001155 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156 }
Jeff Brown6249b902012-05-26 14:32:54 -07001157 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158}
1159
Jeff Brown6249b902012-05-26 14:32:54 -07001160static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001161 const struct fuse_in_header* hdr)
1162{
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001163 DLOG(INFO) << "[" << handler->token << "] FLUSH";
Jeff Brown6249b902012-05-26 14:32:54 -07001164 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165}
1166
Jeff Brown6249b902012-05-26 14:32:54 -07001167static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001168 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1169{
Jeff Brown6249b902012-05-26 14:32:54 -07001170 struct node* node;
1171 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001172 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001173 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001174
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001175 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001176 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001177 DLOG(INFO) << "[" << handler->token << "] OPENDIR @ " << std::hex << hdr->nodeid
1178 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001179 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001180
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001181 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001182 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001184 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001185 return -EACCES;
1186 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001187 h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001188 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001189 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001191 DLOG(INFO) << "[" << handler->token << "] OPENDIR " << path;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001192 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001193 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001194 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001195 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001196 }
1197 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001198 out.open_flags = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001199
1200#ifdef FUSE_SHORTCIRCUIT
1201 out.lower_fd = -1;
1202#else
Ken Sumrall3a876882013-08-14 20:02:13 -07001203 out.padding = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001204#endif
1205
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001206 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001207 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001208}
1209
Jeff Brown6249b902012-05-26 14:32:54 -07001210static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1212{
1213 char buffer[8192];
1214 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1215 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001216 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001217
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001218 DLOG(INFO) << "[" << handler->token << "] READDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001219 if (req->offset == 0) {
1220 /* rewinddir() might have been called above us, so rewind here too */
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001221 DLOG(INFO) << "[" << handler->token << "] calling rewinddir()";
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 rewinddir(h->d);
1223 }
1224 de = readdir(h->d);
1225 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001226 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 }
1228 fde->ino = FUSE_UNKNOWN_INO;
1229 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1230 fde->off = req->offset + 1;
1231 fde->type = de->d_type;
1232 fde->namelen = strlen(de->d_name);
1233 memcpy(fde->name, de->d_name, fde->namelen + 1);
1234 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001235 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1236 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237}
1238
Jeff Brown6249b902012-05-26 14:32:54 -07001239static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001240 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1241{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001242 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001243
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001244 DLOG(INFO) << "[" << handler->token << "] RELEASEDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245 closedir(h->d);
1246 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001247 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248}
1249
Jeff Brown6249b902012-05-26 14:32:54 -07001250static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001251 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1252{
1253 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001254 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001255
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001256 DLOG(INFO) << "[" << handler->token << "] INIT ver=" << req->major << "." << req->minor
1257 << " maxread=" << req->max_readahead << " flags=" << std::hex << req->flags;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001258
1259 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1260 * defined (fuse version 7.6). The structure is the same from 7.6 through
1261 * 7.22. Beginning with 7.23, the structure increased in size and added
1262 * new parameters.
1263 */
1264 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001265 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version "
1266 << req->major << "." << req->minor
1267 << ", Expected at least " << FUSE_KERNEL_VERSION << ".6";
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001268 return -1;
1269 }
1270
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001271 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1272 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001273 fuse_struct_size = sizeof(out);
1274#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1275 /* FUSE_KERNEL_VERSION >= 23. */
1276
Daichi Hirono16d0b422016-11-10 10:22:42 +09001277 /* Since we return minor version 15, the kernel does not accept the latest
1278 * fuse_init_out size. We need to use FUSE_COMPAT_22_INIT_OUT_SIZE always.*/
1279 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001280#endif
1281
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001282 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283 out.max_readahead = req->max_readahead;
1284 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Thierry Strudel55cec582016-09-26 21:25:32 +00001285
1286#ifdef FUSE_SHORTCIRCUIT
1287 out.flags |= FUSE_SHORTCIRCUIT;
1288#endif
1289
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001290 out.max_background = 32;
1291 out.congestion_threshold = 32;
1292 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001293 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001294 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001295}
1296
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001297static int handle_canonical_path(struct fuse* fuse, struct fuse_handler* handler,
1298 const struct fuse_in_header *hdr)
1299{
1300 struct node* node;
1301 char path[PATH_MAX];
1302 int len;
1303
1304 pthread_mutex_lock(&fuse->global->lock);
1305 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1306 path, sizeof(path));
Jorge Lucangeli Obes8df46542016-07-26 20:07:10 -04001307 DLOG(INFO) << "[" << handler->token << "] CANONICAL_PATH @ " << std::hex << hdr->nodeid
1308 << std::dec << " (" << (node ? node->name : "?") << ")";
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001309 pthread_mutex_unlock(&fuse->global->lock);
1310
1311 if (!node) {
1312 return -ENOENT;
1313 }
1314 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
1315 return -EACCES;
1316 }
1317 len = strlen(path);
1318 if (len + 1 > PATH_MAX)
1319 len = PATH_MAX - 1;
1320 path[PATH_MAX - 1] = 0;
1321 fuse_reply(fuse, hdr->unique, path, len + 1);
1322 return NO_STATUS;
1323}
1324
Jeff Brown6249b902012-05-26 14:32:54 -07001325static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1327{
Brian Swetland03ee9472010-08-12 18:01:08 -07001328 switch (hdr->opcode) {
1329 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001330 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001331 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001332 }
Jeff Brown84715842012-05-25 14:07:47 -07001333
Brian Swetland03ee9472010-08-12 18:01:08 -07001334 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001335 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001336 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001337 }
Jeff Brown84715842012-05-25 14:07:47 -07001338
Brian Swetland03ee9472010-08-12 18:01:08 -07001339 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001340 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001341 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001342 }
Jeff Brown84715842012-05-25 14:07:47 -07001343
Brian Swetland03ee9472010-08-12 18:01:08 -07001344 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001345 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001346 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001347 }
Jeff Brown84715842012-05-25 14:07:47 -07001348
Brian Swetland03ee9472010-08-12 18:01:08 -07001349// case FUSE_READLINK:
1350// case FUSE_SYMLINK:
1351 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001352 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001353 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001354 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001355 }
Jeff Brown84715842012-05-25 14:07:47 -07001356
Brian Swetland03ee9472010-08-12 18:01:08 -07001357 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001358 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001359 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001360 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001361 }
Jeff Brown84715842012-05-25 14:07:47 -07001362
Brian Swetland03ee9472010-08-12 18:01:08 -07001363 case FUSE_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001364 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001365 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001366 }
Jeff Brown84715842012-05-25 14:07:47 -07001367
Brian Swetland03ee9472010-08-12 18:01:08 -07001368 case FUSE_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001369 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001370 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001371 }
Jeff Brown84715842012-05-25 14:07:47 -07001372
Brian Swetland03ee9472010-08-12 18:01:08 -07001373 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001374 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001375 const char *old_name = ((const char*) data) + sizeof(*req);
1376 const char *new_name = old_name + strlen(old_name) + 1;
1377 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001378 }
Jeff Brown84715842012-05-25 14:07:47 -07001379
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001381 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001382 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001383 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001384 }
Jeff Brown84715842012-05-25 14:07:47 -07001385
Brian Swetland03ee9472010-08-12 18:01:08 -07001386 case FUSE_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001387 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001388 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001389 }
Jeff Brown84715842012-05-25 14:07:47 -07001390
Brian Swetland03ee9472010-08-12 18:01:08 -07001391 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001392 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001393 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001394 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001395 }
Jeff Brown84715842012-05-25 14:07:47 -07001396
Mike Lockwood4553b082010-08-16 14:14:44 -04001397 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001398 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001399 }
Jeff Brown84715842012-05-25 14:07:47 -07001400
Brian Swetland03ee9472010-08-12 18:01:08 -07001401 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001402 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001403 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001404 }
Jeff Brown84715842012-05-25 14:07:47 -07001405
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001406 case FUSE_FSYNC:
1407 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001408 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001409 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001410 }
1411
Brian Swetland03ee9472010-08-12 18:01:08 -07001412// case FUSE_SETXATTR:
1413// case FUSE_GETXATTR:
1414// case FUSE_LISTXATTR:
1415// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001416 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001417 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001418 }
1419
Brian Swetland03ee9472010-08-12 18:01:08 -07001420 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001421 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001422 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001423 }
Jeff Brown84715842012-05-25 14:07:47 -07001424
Brian Swetland03ee9472010-08-12 18:01:08 -07001425 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001426 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001427 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001428 }
Jeff Brown84715842012-05-25 14:07:47 -07001429
Brian Swetland03ee9472010-08-12 18:01:08 -07001430 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001431 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001432 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001433 }
Jeff Brown84715842012-05-25 14:07:47 -07001434
Brian Swetland03ee9472010-08-12 18:01:08 -07001435 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001436 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001437 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001438 }
Jeff Brown84715842012-05-25 14:07:47 -07001439
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001440 case FUSE_CANONICAL_PATH: { /* nodeid -> bytez[] */
1441 return handle_canonical_path(fuse, handler, hdr);
1442 }
1443
Brian Swetland03ee9472010-08-12 18:01:08 -07001444 default: {
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001445 DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001446 << "uniq=" << std::hex << hdr->unique << "nid=" << hdr->nodeid << std::dec;
Jeff Brown6249b902012-05-26 14:32:54 -07001447 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001448 }
Jeff Brown84715842012-05-25 14:07:47 -07001449 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001450}
1451
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001452void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001453{
Jeff Brown6249b902012-05-26 14:32:54 -07001454 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001455 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001456 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1457 handler->request_buffer, sizeof(handler->request_buffer)));
Christopher Ferrisd6b0d372016-10-06 12:51:20 -07001458 if (len == -1) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001459 if (errno == ENODEV) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001460 LOG(ERROR) << "[" << handler->token << "] someone stole our marbles!";
Jeff Sharkey4a485812015-06-30 16:02:40 -07001461 exit(2);
1462 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001463 PLOG(ERROR) << "[" << handler->token << "] handle_fuse_requests";
Jeff Brown6249b902012-05-26 14:32:54 -07001464 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001465 }
Jeff Brown84715842012-05-25 14:07:47 -07001466
Christopher Ferrisd6b0d372016-10-06 12:51:20 -07001467 if (static_cast<size_t>(len) < sizeof(struct fuse_in_header)) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001468 LOG(ERROR) << "[" << handler->token << "] request too short: len=" << len;
Jeff Brown6249b902012-05-26 14:32:54 -07001469 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001470 }
1471
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001472 const struct fuse_in_header* hdr =
1473 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Christopher Ferrisd6b0d372016-10-06 12:51:20 -07001474 if (hdr->len != static_cast<size_t>(len)) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001475 LOG(ERROR) << "[" << handler->token << "] malformed header: len=" << len
1476 << ", hdr->len=" << hdr->len;
Jeff Brown6249b902012-05-26 14:32:54 -07001477 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001478 }
1479
Jeff Brown7729d242012-05-25 15:35:28 -07001480 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001481 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001482 __u64 unique = hdr->unique;
1483 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001484
1485 /* We do not access the request again after this point because the underlying
1486 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001487
1488 if (res != NO_STATUS) {
1489 if (res) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001490 DLOG(INFO) << "[" << handler->token << "] ERROR " << res;
Jeff Brown6249b902012-05-26 14:32:54 -07001491 }
1492 fuse_status(fuse, unique, res);
1493 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001494 }
1495}