blob: 6a972ea7616640d80328d15a12c56aad0ad08219 [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++;
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040048 DLOG(INFO) << "ACQUIRE " << std::hex << node << std::dec
49 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070050}
51
52static void remove_node_from_parent_locked(struct node* node);
53
54static void release_node_locked(struct node* node)
55{
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040056 DLOG(INFO) << "RELEASE " << std::hex << node << std::dec
57 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070058 if (node->refcount > 0) {
59 node->refcount--;
60 if (!node->refcount) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040061 DLOG(INFO) << "DESTROY " << std::hex << node << std::dec << " (" << node->name << ")";
Jeff Brown6249b902012-05-26 14:32:54 -070062 remove_node_from_parent_locked(node);
63
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040064 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070065 memset(node->name, 0xef, node->namelen);
66 free(node->name);
67 free(node->actual_name);
68 memset(node, 0xfc, sizeof(*node));
69 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080070 }
Jeff Brown6249b902012-05-26 14:32:54 -070071 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040072 LOG(ERROR) << std::hex << node << std::dec << " refcount=0";
Jeff Brown6249b902012-05-26 14:32:54 -070073 }
74}
75
76static void add_node_to_parent_locked(struct node *node, struct node *parent) {
77 node->parent = parent;
78 node->next = parent->child;
79 parent->child = node;
80 acquire_node_locked(parent);
81}
82
83static void remove_node_from_parent_locked(struct node* node)
84{
85 if (node->parent) {
86 if (node->parent->child == node) {
87 node->parent->child = node->parent->child->next;
88 } else {
89 struct node *node2;
90 node2 = node->parent->child;
91 while (node2->next != node)
92 node2 = node2->next;
93 node2->next = node->next;
94 }
95 release_node_locked(node->parent);
96 node->parent = NULL;
97 node->next = NULL;
98 }
99}
100
101/* Gets the absolute path to a node into the provided buffer.
102 *
103 * Populates 'buf' with the path and returns the length of the path on success,
104 * or returns -1 if the path is too long for the provided buffer.
105 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700106static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
107 const char* name;
108 size_t namelen;
109 if (node->graft_path) {
110 name = node->graft_path;
111 namelen = node->graft_pathlen;
112 } else if (node->actual_name) {
113 name = node->actual_name;
114 namelen = node->namelen;
115 } else {
116 name = node->name;
117 namelen = node->namelen;
118 }
119
Jeff Brown6249b902012-05-26 14:32:54 -0700120 if (bufsize < namelen + 1) {
121 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700122 }
123
Jeff Brown6249b902012-05-26 14:32:54 -0700124 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700125 if (node->parent && node->graft_path == NULL) {
Daniel Rosenbergdb4638e2016-04-12 16:30:28 -0700126 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700127 if (pathlen < 0) {
128 return -1;
129 }
130 buf[pathlen++] = '/';
131 }
132
Jeff Brown6249b902012-05-26 14:32:54 -0700133 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
134 return pathlen + namelen;
135}
136
137/* Finds the absolute path of a file within a given directory.
138 * Performs a case-insensitive search for the file and sets the buffer to the path
139 * of the first matching file. If 'search' is zero or if no match is found, sets
140 * the buffer to the path that the file would have, assuming the name were case-sensitive.
141 *
142 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
143 * or returns NULL if the path is too long for the provided buffer.
144 */
145static char* find_file_within(const char* path, const char* name,
146 char* buf, size_t bufsize, int search)
147{
148 size_t pathlen = strlen(path);
149 size_t namelen = strlen(name);
150 size_t childlen = pathlen + namelen + 1;
151 char* actual;
152
153 if (bufsize <= childlen) {
154 return NULL;
155 }
156
157 memcpy(buf, path, pathlen);
158 buf[pathlen] = '/';
159 actual = buf + pathlen + 1;
160 memcpy(actual, name, namelen + 1);
161
162 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800163 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700164 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800165 if (!dir) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400166 PLOG(ERROR) << "opendir(" << path << ") failed";
Jeff Brown6249b902012-05-26 14:32:54 -0700167 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800168 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800169 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700170 if (!strcasecmp(entry->d_name, name)) {
171 /* we have a match - replace the name, don't need to copy the null again */
172 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800173 break;
174 }
175 }
176 closedir(dir);
177 }
Jeff Brown6249b902012-05-26 14:32:54 -0700178 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800179}
180
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700181static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
182 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000183 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700184 attr->size = s->st_size;
185 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800186 attr->atime = s->st_atim.tv_sec;
187 attr->mtime = s->st_mtim.tv_sec;
188 attr->ctime = s->st_ctim.tv_sec;
189 attr->atimensec = s->st_atim.tv_nsec;
190 attr->mtimensec = s->st_mtim.tv_nsec;
191 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700192 attr->mode = s->st_mode;
193 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700194
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700195 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700196
197 if (fuse->gid == AID_SDCARD_RW) {
198 /* As an optimization, certain trusted system components only run
199 * as owner but operate across all users. Since we're now handing
200 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
201 * the user boundary enforcement for the default view. The UIDs
202 * assigned to app directories are still multiuser aware. */
203 attr->gid = AID_SDCARD_RW;
204 } else {
205 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
206 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700207
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700208 int visible_mode = 0775 & ~fuse->mask;
209 if (node->perm == PERM_PRE_ROOT) {
210 /* Top of multi-user view should always be visible to ensure
211 * secondary users can traverse inside. */
212 visible_mode = 0711;
213 } else if (node->under_android) {
214 /* Block "other" access to Android directories, since only apps
215 * belonging to a specific user should be in there; we still
216 * leave +x open for the default view. */
217 if (fuse->gid == AID_SDCARD_RW) {
218 visible_mode = visible_mode & ~0006;
219 } else {
220 visible_mode = visible_mode & ~0007;
221 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700222 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700223 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700224 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700225 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
226}
227
Jeff Sharkey44d63422013-09-12 09:44:48 -0700228static int touch(char* path, mode_t mode) {
229 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
230 if (fd == -1) {
231 if (errno == EEXIST) {
232 return 0;
233 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400234 PLOG(ERROR) << "open(" << path << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700235 return -1;
236 }
237 }
238 close(fd);
239 return 0;
240}
241
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700242static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
243 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700244 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700245
246 /* By default, each node inherits from its parent */
247 node->perm = PERM_INHERIT;
248 node->userid = parent->userid;
249 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700250 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700251
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700252 /* Derive custom permissions based on parent and current node */
253 switch (parent->perm) {
254 case PERM_INHERIT:
255 /* Already inherited above */
256 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700257 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700258 /* Legacy internal layout places users at top level */
259 node->perm = PERM_ROOT;
260 node->userid = strtoul(node->name, NULL, 10);
261 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700262 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700263 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700264 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700265 /* App-specific directories inside; let anyone traverse */
266 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700267 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700268 }
269 break;
270 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700271 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700272 /* App-specific directories inside; let anyone traverse */
273 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700274 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700275 /* App-specific directories inside; let anyone traverse */
276 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700277 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700278 node->graft_path = fuse->global->obb_path;
279 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700280 } else if (!strcasecmp(node->name, "media")) {
281 /* App-specific directories inside; let anyone traverse */
282 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700283 }
284 break;
285 case PERM_ANDROID_DATA:
286 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700287 case PERM_ANDROID_MEDIA:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400288 const auto& iter = fuse->global->package_to_appid->find(node->name);
289 if (iter != fuse->global->package_to_appid->end()) {
290 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700291 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700292 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700293 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700294 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700295}
296
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400297void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700298 struct node *node;
299 for (node = parent->child; node; node = node->next) {
300 derive_permissions_locked(fuse, parent, node);
301 if (node->child) {
302 derive_permissions_recursive_locked(fuse, node);
303 }
304 }
305}
306
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700307/* Kernel has already enforced everything we returned through
308 * derive_permissions_locked(), so this is used to lock down access
309 * even further, such as enforcing that apps hold sdcard_rw. */
310static bool check_caller_access_to_name(struct fuse* fuse,
311 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700312 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700313 /* Always block security-sensitive files at root */
314 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700315 if (!strcasecmp(name, "autorun.inf")
316 || !strcasecmp(name, ".android_secure")
317 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700318 return false;
319 }
320 }
321
Jeff Sharkey44d63422013-09-12 09:44:48 -0700322 /* Root always has access; access for any other UIDs should always
323 * be controlled through packages.list. */
324 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700325 return true;
326 }
327
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700328 /* No extra permissions to enforce */
329 return true;
330}
331
332static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700333 const struct fuse_in_header *hdr, const struct node* node, int mode) {
334 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700335}
336
Jeff Brown6249b902012-05-26 14:32:54 -0700337struct node *create_node_locked(struct fuse* fuse,
338 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700339{
340 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700341 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700342
Narayan Kamathfaa09352015-01-13 18:21:10 +0000343 // Detect overflows in the inode counter. "4 billion nodes should be enough
344 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700345 if (fuse->global->inode_ctr == 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400346 LOG(ERROR) << "No more inode numbers available";
Narayan Kamathfaa09352015-01-13 18:21:10 +0000347 return NULL;
348 }
349
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400350 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700351 if (!node) {
352 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700353 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400354 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700355 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700356 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700357 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700358 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700359 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700360 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400361 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700362 if (!node->actual_name) {
363 free(node->name);
364 free(node);
365 return NULL;
366 }
367 memcpy(node->actual_name, actual_name, namelen + 1);
368 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700369 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700370 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700371 node->ino = fuse->global->inode_ctr++;
372 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700373
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200374 node->deleted = false;
375
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700376 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700377 acquire_node_locked(node);
378 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700379 return node;
380}
381
Jeff Brown6249b902012-05-26 14:32:54 -0700382static int rename_node_locked(struct node *node, const char *name,
383 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700384{
Jeff Brown6249b902012-05-26 14:32:54 -0700385 size_t namelen = strlen(name);
386 int need_actual_name = strcmp(name, actual_name);
387
388 /* make the storage bigger without actually changing the name
389 * in case an error occurs part way */
390 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400391 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700392 if (!new_name) {
393 return -ENOMEM;
394 }
395 node->name = new_name;
396 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400397 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700398 if (!new_actual_name) {
399 return -ENOMEM;
400 }
401 node->actual_name = new_actual_name;
402 }
403 }
404
405 /* update the name, taking care to allocate storage before overwriting the old name */
406 if (need_actual_name) {
407 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400408 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700409 if (!node->actual_name) {
410 return -ENOMEM;
411 }
412 }
413 memcpy(node->actual_name, actual_name, namelen + 1);
414 } else {
415 free(node->actual_name);
416 node->actual_name = NULL;
417 }
418 memcpy(node->name, name, namelen + 1);
419 node->namelen = namelen;
420 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700421}
422
Jeff Brown6249b902012-05-26 14:32:54 -0700423static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700424{
Jeff Brown6249b902012-05-26 14:32:54 -0700425 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700426 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700427 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400428 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700429 }
430}
431
Jeff Brown6249b902012-05-26 14:32:54 -0700432static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
433 char* buf, size_t bufsize)
434{
435 struct node* node = lookup_node_by_id_locked(fuse, nid);
436 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
437 node = NULL;
438 }
439 return node;
440}
441
442static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700443{
444 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700445 /* use exact string comparison, nodes that differ by case
446 * must be considered distinct even if they refer to the same
447 * underlying file as otherwise operations such as "mv x x"
448 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200449 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700450 return node;
451 }
452 }
453 return 0;
454}
455
Jeff Brown6249b902012-05-26 14:32:54 -0700456static struct node* acquire_or_create_child_locked(
457 struct fuse* fuse, struct node* parent,
458 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700459{
Jeff Brown6249b902012-05-26 14:32:54 -0700460 struct node* child = lookup_child_by_name_locked(parent, name);
461 if (child) {
462 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800463 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700464 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800465 }
Jeff Brown6249b902012-05-26 14:32:54 -0700466 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700467}
468
Jeff Brown6249b902012-05-26 14:32:54 -0700469static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700470{
471 struct fuse_out_header hdr;
472 hdr.len = sizeof(hdr);
473 hdr.error = err;
474 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700475 write(fuse->fd, &hdr, sizeof(hdr));
476}
477
Jeff Brown6249b902012-05-26 14:32:54 -0700478static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700479{
480 struct fuse_out_header hdr;
481 struct iovec vec[2];
482 int res;
483
484 hdr.len = len + sizeof(hdr);
485 hdr.error = 0;
486 hdr.unique = unique;
487
488 vec[0].iov_base = &hdr;
489 vec[0].iov_len = sizeof(hdr);
490 vec[1].iov_base = data;
491 vec[1].iov_len = len;
492
493 res = writev(fuse->fd, vec, 2);
494 if (res < 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400495 PLOG(ERROR) << "*** REPLY FAILED ***";
Brian Swetland03ee9472010-08-12 18:01:08 -0700496 }
497}
498
Jeff Brown6249b902012-05-26 14:32:54 -0700499static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
500 struct node* parent, const char* name, const char* actual_name,
501 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700502{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700503 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700504 struct fuse_entry_out out;
505 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700506
Jeff Brown6249b902012-05-26 14:32:54 -0700507 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700508 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700509 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700510
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700511 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700512 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
513 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700514 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700515 return -ENOMEM;
516 }
517 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700518 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700519 out.attr_valid = 10;
520 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700521 out.nodeid = node->nid;
522 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700523 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700524 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700525 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700526}
527
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700528static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700529 const char* path)
530{
531 struct fuse_attr_out out;
532 struct stat s;
533
534 if (lstat(path, &s) < 0) {
535 return -errno;
536 }
537 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700538 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700539 out.attr_valid = 10;
540 fuse_reply(fuse, unique, &out, sizeof(out));
541 return NO_STATUS;
542}
543
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700544static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
545 const __u64 child, const char* name) {
546 struct fuse_out_header hdr;
547 struct fuse_notify_delete_out data;
548 struct iovec vec[3];
549 size_t namelen = strlen(name);
550 int res;
551
552 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
553 hdr.error = FUSE_NOTIFY_DELETE;
554 hdr.unique = 0;
555
556 data.parent = parent;
557 data.child = child;
558 data.namelen = namelen;
559 data.padding = 0;
560
561 vec[0].iov_base = &hdr;
562 vec[0].iov_len = sizeof(hdr);
563 vec[1].iov_base = &data;
564 vec[1].iov_len = sizeof(data);
565 vec[2].iov_base = (void*) name;
566 vec[2].iov_len = namelen + 1;
567
568 res = writev(fuse->fd, vec, 3);
569 /* Ignore ENOENT, since other views may not have seen the entry */
570 if (res < 0 && errno != ENOENT) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400571 PLOG(ERROR) << "*** NOTIFY FAILED ***";
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700572 }
573}
574
Jeff Brown6249b902012-05-26 14:32:54 -0700575static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700576 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700577{
Jeff Brown6249b902012-05-26 14:32:54 -0700578 struct node* parent_node;
579 char parent_path[PATH_MAX];
580 char child_path[PATH_MAX];
581 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700582
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700583 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700584 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
585 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400586 DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid
587 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700588 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700589
590 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
591 child_path, sizeof(child_path), 1))) {
592 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700593 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700594 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700595 return -EACCES;
596 }
597
Jeff Brown6249b902012-05-26 14:32:54 -0700598 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700599}
600
Jeff Brown6249b902012-05-26 14:32:54 -0700601static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700602 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
603{
Jeff Brown6249b902012-05-26 14:32:54 -0700604 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700605
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700606 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700607 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400608 DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup
609 << " @ " << std::hex << hdr->nodeid
610 << " (" << (node ? node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700611 if (node) {
612 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400613 while (n) {
614 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700615 release_node_locked(node);
616 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700617 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700618 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700619 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700620}
621
Jeff Brown6249b902012-05-26 14:32:54 -0700622static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700623 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
624{
Jeff Brown6249b902012-05-26 14:32:54 -0700625 struct node* node;
626 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700627
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700628 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700629 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400630 DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400631 << " fh=" << std::hex << req->fh << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400632 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700633 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700634
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700635 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700636 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700637 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700638 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700639 return -EACCES;
640 }
641
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700642 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700643}
644
Jeff Brown6249b902012-05-26 14:32:54 -0700645static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700646 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
647{
Jeff Brown6249b902012-05-26 14:32:54 -0700648 struct node* node;
649 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700650 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700651
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700652 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700653 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400654 DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400655 << " valid=" << std::hex << req->valid << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400656 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700657 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700658
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700659 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700660 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700661 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800662
663 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700664 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700665 return -EACCES;
666 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700667
Jeff Brown6249b902012-05-26 14:32:54 -0700668 /* XXX: incomplete implementation on purpose.
669 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700670
Elliott Hughes853574d2014-07-31 12:03:03 -0700671 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700672 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700673 }
674
675 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
676 * are both set, then set it to the current time. Else, set it to the
677 * time specified in the request. Same goes for mtime. Use utimensat(2)
678 * as it allows ATIME and MTIME to be changed independently, and has
679 * nanosecond resolution which fuse also has.
680 */
681 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
682 times[0].tv_nsec = UTIME_OMIT;
683 times[1].tv_nsec = UTIME_OMIT;
684 if (req->valid & FATTR_ATIME) {
685 if (req->valid & FATTR_ATIME_NOW) {
686 times[0].tv_nsec = UTIME_NOW;
687 } else {
688 times[0].tv_sec = req->atime;
689 times[0].tv_nsec = req->atimensec;
690 }
691 }
692 if (req->valid & FATTR_MTIME) {
693 if (req->valid & FATTR_MTIME_NOW) {
694 times[1].tv_nsec = UTIME_NOW;
695 } else {
696 times[1].tv_sec = req->mtime;
697 times[1].tv_nsec = req->mtimensec;
698 }
699 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400700 DLOG(INFO) << "[" << handler->token << "] Calling utimensat on " << path
701 << " with atime " << times[0].tv_sec << ", mtime=" << times[1].tv_sec;
Jeff Brown6249b902012-05-26 14:32:54 -0700702 if (utimensat(-1, path, times, 0) < 0) {
703 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700704 }
705 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700706 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700707}
708
Jeff Brown6249b902012-05-26 14:32:54 -0700709static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700710 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
711{
Jeff Brown6249b902012-05-26 14:32:54 -0700712 struct node* parent_node;
713 char parent_path[PATH_MAX];
714 char child_path[PATH_MAX];
715 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700716
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700717 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700718 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
719 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400720 DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode
721 << " @ " << std::hex << hdr->nodeid
722 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700723 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700724
725 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
726 child_path, sizeof(child_path), 1))) {
727 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700728 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700729 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700730 return -EACCES;
731 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700732 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700733 if (mknod(child_path, mode, req->rdev) < 0) {
734 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700735 }
Jeff Brown6249b902012-05-26 14:32:54 -0700736 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700737}
738
Jeff Brown6249b902012-05-26 14:32:54 -0700739static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700740 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
741{
Jeff Brown6249b902012-05-26 14:32:54 -0700742 struct node* parent_node;
743 char parent_path[PATH_MAX];
744 char child_path[PATH_MAX];
745 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700746
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700747 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700748 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
749 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400750 DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode
751 << " @ " << std::hex << hdr->nodeid
752 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700753 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700754
755 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
756 child_path, sizeof(child_path), 1))) {
757 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700758 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700759 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700760 return -EACCES;
761 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700762 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700763 if (mkdir(child_path, mode) < 0) {
764 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700765 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700766
767 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
768 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
769 char nomedia[PATH_MAX];
770 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
771 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400772 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700773 return -ENOENT;
774 }
775 }
776 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
777 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700778 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700779 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400780 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700781 return -ENOENT;
782 }
783 }
784
Jeff Brown6249b902012-05-26 14:32:54 -0700785 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700786}
787
Jeff Brown6249b902012-05-26 14:32:54 -0700788static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700789 const struct fuse_in_header* hdr, const char* name)
790{
Jeff Brown6249b902012-05-26 14:32:54 -0700791 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200792 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700793 char parent_path[PATH_MAX];
794 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700795
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700796 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700797 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
798 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400799 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
800 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700801 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700802
803 if (!parent_node || !find_file_within(parent_path, name,
804 child_path, sizeof(child_path), 1)) {
805 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700806 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700807 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700808 return -EACCES;
809 }
Jeff Brown6249b902012-05-26 14:32:54 -0700810 if (unlink(child_path) < 0) {
811 return -errno;
812 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700813 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200814 child_node = lookup_child_by_name_locked(parent_node, name);
815 if (child_node) {
816 child_node->deleted = true;
817 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700818 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700819 if (parent_node && child_node) {
820 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400821 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
822 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400823 << ", child=" << std::hex << child_node->nid << std::dec
824 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700825 if (fuse != fuse->global->fuse_default) {
826 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
827 }
828 if (fuse != fuse->global->fuse_read) {
829 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
830 }
831 if (fuse != fuse->global->fuse_write) {
832 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
833 }
834 }
Jeff Brown6249b902012-05-26 14:32:54 -0700835 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836}
837
Jeff Brown6249b902012-05-26 14:32:54 -0700838static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839 const struct fuse_in_header* hdr, const char* name)
840{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200841 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700842 struct node* parent_node;
843 char parent_path[PATH_MAX];
844 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700845
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700846 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700847 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
848 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400849 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
850 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700851 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700852
853 if (!parent_node || !find_file_within(parent_path, name,
854 child_path, sizeof(child_path), 1)) {
855 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700856 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700857 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700858 return -EACCES;
859 }
Jeff Brown6249b902012-05-26 14:32:54 -0700860 if (rmdir(child_path) < 0) {
861 return -errno;
862 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700863 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200864 child_node = lookup_child_by_name_locked(parent_node, name);
865 if (child_node) {
866 child_node->deleted = true;
867 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700868 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700869 if (parent_node && child_node) {
870 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400871 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
872 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400873 << ", child=" << std::hex << child_node->nid << std::dec
874 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700875 if (fuse != fuse->global->fuse_default) {
876 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
877 }
878 if (fuse != fuse->global->fuse_read) {
879 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
880 }
881 if (fuse != fuse->global->fuse_write) {
882 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
883 }
884 }
Jeff Brown6249b902012-05-26 14:32:54 -0700885 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886}
887
Jeff Brown6249b902012-05-26 14:32:54 -0700888static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700889 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700890 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700891{
Jeff Brown6249b902012-05-26 14:32:54 -0700892 struct node* old_parent_node;
893 struct node* new_parent_node;
894 struct node* child_node;
895 char old_parent_path[PATH_MAX];
896 char new_parent_path[PATH_MAX];
897 char old_child_path[PATH_MAX];
898 char new_child_path[PATH_MAX];
899 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400900 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700901 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700902
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700903 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700904 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
905 old_parent_path, sizeof(old_parent_path));
906 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
907 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400908 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name
909 << " @ " << std::hex << hdr->nodeid
910 << " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> "
911 << std::hex << req->newdir
912 << " (" << (new_parent_node ? new_parent_node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700913 if (!old_parent_node || !new_parent_node) {
914 res = -ENOENT;
915 goto lookup_error;
916 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700917 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700918 res = -EACCES;
919 goto lookup_error;
920 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700921 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700922 res = -EACCES;
923 goto lookup_error;
924 }
Jeff Brown6249b902012-05-26 14:32:54 -0700925 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
926 if (!child_node || get_node_path_locked(child_node,
927 old_child_path, sizeof(old_child_path)) < 0) {
928 res = -ENOENT;
929 goto lookup_error;
930 }
931 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700932 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700933
934 /* Special case for renaming a file where destination is same path
935 * differing only by case. In this case we don't want to look for a case
936 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
937 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400938 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700939 || strcasecmp(old_name, new_name);
940 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
941 new_child_path, sizeof(new_child_path), search))) {
942 res = -ENOENT;
943 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700944 }
945
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400946 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_child_path << "->" << new_child_path;
Jeff Brown6249b902012-05-26 14:32:54 -0700947 res = rename(old_child_path, new_child_path);
948 if (res < 0) {
949 res = -errno;
950 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951 }
952
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700953 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700954 res = rename_node_locked(child_node, new_name, new_actual_name);
955 if (!res) {
956 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700957 derive_permissions_locked(fuse, new_parent_node, child_node);
958 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700959 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700960 }
Jeff Brown6249b902012-05-26 14:32:54 -0700961 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700962
Jeff Brown6249b902012-05-26 14:32:54 -0700963io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700964 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700965done:
966 release_node_locked(child_node);
967lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700968 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700969 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970}
971
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700972static int open_flags_to_access_mode(int open_flags) {
973 if ((open_flags & O_ACCMODE) == O_RDONLY) {
974 return R_OK;
975 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
976 return W_OK;
977 } else {
978 /* Probably O_RDRW, but treat as default to be safe */
979 return R_OK | W_OK;
980 }
981}
982
Jeff Brown6249b902012-05-26 14:32:54 -0700983static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
985{
Jeff Brown6249b902012-05-26 14:32:54 -0700986 struct node* node;
987 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700988 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700989 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700990
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700991 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700992 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400993 DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400994 << " @ " << std::hex << hdr->nodeid << std::dec
995 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700996 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700997
998 if (!node) {
999 return -ENOENT;
1000 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001001 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001002 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001003 return -EACCES;
1004 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001005 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001006 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001007 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001008 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001009 DLOG(INFO) << "[" << handler->token << "] OPEN " << path;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001010 h->fd = open(path, req->flags);
1011 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001012 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001013 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001014 }
1015 out.fh = ptr_to_id(h);
1016 out.open_flags = 0;
1017 out.padding = 0;
1018 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001019 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001020}
1021
Jeff Brown6249b902012-05-26 14:32:54 -07001022static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1024{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001025 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026 __u64 unique = hdr->unique;
1027 __u32 size = req->size;
1028 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001029 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001030 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001031
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001032 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1033 * overlaps the request buffer and will clobber data in the request. This
1034 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001035
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001036 DLOG(INFO) << "[" << handler->token << "] READ " << std::hex << h << std::dec
1037 << "(" << h->fd << ") " << size << "@" << offset;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001038 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001039 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001040 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001041 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001043 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001044 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001045 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001046 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001047}
1048
Jeff Brown6249b902012-05-26 14:32:54 -07001049static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1051 const void* buffer)
1052{
1053 struct fuse_write_out out;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001054 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001055 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001056 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001057
Arpad Horvath49e93442014-02-18 10:18:25 +01001058 if (req->flags & O_DIRECT) {
1059 memcpy(aligned_buffer, buffer, req->size);
1060 buffer = (const __u8*) aligned_buffer;
1061 }
Jeff Brown6249b902012-05-26 14:32:54 -07001062
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001063 DLOG(INFO) << "[" << handler->token << "] WRITE " << std::hex << h << std::dec
1064 << "(" << h->fd << ") " << req->size << "@" << req->offset;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001065 res = pwrite64(h->fd, buffer, req->size, req->offset);
1066 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001067 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068 }
1069 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001070 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001071 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001072 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073}
1074
Jeff Brown6249b902012-05-26 14:32:54 -07001075static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001076 const struct fuse_in_header* hdr)
1077{
Jeff Brown6249b902012-05-26 14:32:54 -07001078 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001079 struct statfs stat;
1080 struct fuse_statfs_out out;
1081 int res;
1082
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001083 pthread_mutex_lock(&fuse->global->lock);
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001084 DLOG(INFO) << "[" << handler->token << "] STATFS";
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001085 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001086 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001087 if (res < 0) {
1088 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001090 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001091 return -errno;
1092 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001093 memset(&out, 0, sizeof(out));
1094 out.st.blocks = stat.f_blocks;
1095 out.st.bfree = stat.f_bfree;
1096 out.st.bavail = stat.f_bavail;
1097 out.st.files = stat.f_files;
1098 out.st.ffree = stat.f_ffree;
1099 out.st.bsize = stat.f_bsize;
1100 out.st.namelen = stat.f_namelen;
1101 out.st.frsize = stat.f_frsize;
1102 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001103 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001104}
1105
Jeff Brown6249b902012-05-26 14:32:54 -07001106static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1108{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001109 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001110
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001111 DLOG(INFO) << "[" << handler->token << "] RELEASE " << std::hex << h << std::dec
1112 << "(" << h->fd << ")";
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001113 close(h->fd);
1114 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001115 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001116}
1117
Jeff Brown6249b902012-05-26 14:32:54 -07001118static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001119 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1120{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001121 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1122 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001123
Elliott Hughesf6d67372014-07-08 14:38:26 -07001124 int fd = -1;
1125 if (is_dir) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001126 struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001127 fd = dirfd(dh->d);
1128 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001129 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001130 fd = h->fd;
1131 }
1132
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001133 DLOG(INFO) << "[" << handler->token << "] " << (is_dir ? "FSYNCDIR" : "FSYNC") << " "
1134 << std::hex << req->fh << std::dec << "(" << fd << ") is_data_sync=" << is_data_sync;
Elliott Hughesf6d67372014-07-08 14:38:26 -07001135 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1136 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001137 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001138 }
Jeff Brown6249b902012-05-26 14:32:54 -07001139 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001140}
1141
Jeff Brown6249b902012-05-26 14:32:54 -07001142static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001143 const struct fuse_in_header* hdr)
1144{
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001145 DLOG(INFO) << "[" << handler->token << "] FLUSH";
Jeff Brown6249b902012-05-26 14:32:54 -07001146 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001147}
1148
Jeff Brown6249b902012-05-26 14:32:54 -07001149static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1151{
Jeff Brown6249b902012-05-26 14:32:54 -07001152 struct node* node;
1153 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001154 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001155 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001157 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001158 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001159 DLOG(INFO) << "[" << handler->token << "] OPENDIR @ " << std::hex << hdr->nodeid
1160 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001161 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001162
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001163 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001164 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001166 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001167 return -EACCES;
1168 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001169 h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001170 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001171 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001172 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001173 DLOG(INFO) << "[" << handler->token << "] OPENDIR " << path;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001174 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001175 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001176 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001177 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001178 }
1179 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001180 out.open_flags = 0;
1181 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001182 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001183 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001184}
1185
Jeff Brown6249b902012-05-26 14:32:54 -07001186static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001187 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1188{
1189 char buffer[8192];
1190 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1191 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001192 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001193
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001194 DLOG(INFO) << "[" << handler->token << "] READDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195 if (req->offset == 0) {
1196 /* rewinddir() might have been called above us, so rewind here too */
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001197 DLOG(INFO) << "[" << handler->token << "] calling rewinddir()";
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198 rewinddir(h->d);
1199 }
1200 de = readdir(h->d);
1201 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001202 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203 }
1204 fde->ino = FUSE_UNKNOWN_INO;
1205 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1206 fde->off = req->offset + 1;
1207 fde->type = de->d_type;
1208 fde->namelen = strlen(de->d_name);
1209 memcpy(fde->name, de->d_name, fde->namelen + 1);
1210 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001211 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1212 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001213}
1214
Jeff Brown6249b902012-05-26 14:32:54 -07001215static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1217{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001218 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001219
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001220 DLOG(INFO) << "[" << handler->token << "] RELEASEDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001221 closedir(h->d);
1222 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001223 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224}
1225
Jeff Brown6249b902012-05-26 14:32:54 -07001226static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1228{
1229 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001230 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001231
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001232 DLOG(INFO) << "[" << handler->token << "] INIT ver=" << req->major << "." << req->minor
1233 << " maxread=" << req->max_readahead << " flags=" << std::hex << req->flags;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001234
1235 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1236 * defined (fuse version 7.6). The structure is the same from 7.6 through
1237 * 7.22. Beginning with 7.23, the structure increased in size and added
1238 * new parameters.
1239 */
1240 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001241 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version "
1242 << req->major << "." << req->minor
1243 << ", Expected at least " << FUSE_KERNEL_VERSION << ".6";
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001244 return -1;
1245 }
1246
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001247 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1248 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001249 fuse_struct_size = sizeof(out);
1250#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1251 /* FUSE_KERNEL_VERSION >= 23. */
1252
1253 /* If the kernel only works on minor revs older than or equal to 22,
1254 * then use the older structure size since this code only uses the 7.22
1255 * version of the structure. */
1256 if (req->minor <= 22) {
1257 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1258 }
1259#endif
1260
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001261 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262 out.max_readahead = req->max_readahead;
1263 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1264 out.max_background = 32;
1265 out.congestion_threshold = 32;
1266 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001267 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001268 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269}
1270
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001271static int handle_canonical_path(struct fuse* fuse, struct fuse_handler* handler,
1272 const struct fuse_in_header *hdr)
1273{
1274 struct node* node;
1275 char path[PATH_MAX];
1276 int len;
1277
1278 pthread_mutex_lock(&fuse->global->lock);
1279 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1280 path, sizeof(path));
Jorge Lucangeli Obes8df46542016-07-26 20:07:10 -04001281 DLOG(INFO) << "[" << handler->token << "] CANONICAL_PATH @ " << std::hex << hdr->nodeid
1282 << std::dec << " (" << (node ? node->name : "?") << ")";
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001283 pthread_mutex_unlock(&fuse->global->lock);
1284
1285 if (!node) {
1286 return -ENOENT;
1287 }
1288 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
1289 return -EACCES;
1290 }
1291 len = strlen(path);
1292 if (len + 1 > PATH_MAX)
1293 len = PATH_MAX - 1;
1294 path[PATH_MAX - 1] = 0;
1295 fuse_reply(fuse, hdr->unique, path, len + 1);
1296 return NO_STATUS;
1297}
1298
1299
Jeff Brown6249b902012-05-26 14:32:54 -07001300static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001301 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1302{
Brian Swetland03ee9472010-08-12 18:01:08 -07001303 switch (hdr->opcode) {
1304 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001305 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001306 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001307 }
Jeff Brown84715842012-05-25 14:07:47 -07001308
Brian Swetland03ee9472010-08-12 18:01:08 -07001309 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001310 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001311 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001312 }
Jeff Brown84715842012-05-25 14:07:47 -07001313
Brian Swetland03ee9472010-08-12 18:01:08 -07001314 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001315 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001316 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001317 }
Jeff Brown84715842012-05-25 14:07:47 -07001318
Brian Swetland03ee9472010-08-12 18:01:08 -07001319 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001320 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001321 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001322 }
Jeff Brown84715842012-05-25 14:07:47 -07001323
Brian Swetland03ee9472010-08-12 18:01:08 -07001324// case FUSE_READLINK:
1325// case FUSE_SYMLINK:
1326 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001327 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_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_mknod(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_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001333 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001334 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001335 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001336 }
Jeff Brown84715842012-05-25 14:07:47 -07001337
Brian Swetland03ee9472010-08-12 18:01:08 -07001338 case FUSE_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001339 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001340 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001341 }
Jeff Brown84715842012-05-25 14:07:47 -07001342
Brian Swetland03ee9472010-08-12 18:01:08 -07001343 case FUSE_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001344 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001345 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001346 }
Jeff Brown84715842012-05-25 14:07:47 -07001347
Brian Swetland03ee9472010-08-12 18:01:08 -07001348 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001349 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001350 const char *old_name = ((const char*) data) + sizeof(*req);
1351 const char *new_name = old_name + strlen(old_name) + 1;
1352 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001353 }
Jeff Brown84715842012-05-25 14:07:47 -07001354
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001356 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001357 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001358 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001359 }
Jeff Brown84715842012-05-25 14:07:47 -07001360
Brian Swetland03ee9472010-08-12 18:01:08 -07001361 case FUSE_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001362 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001363 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001364 }
Jeff Brown84715842012-05-25 14:07:47 -07001365
Brian Swetland03ee9472010-08-12 18:01:08 -07001366 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001367 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001368 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001369 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001370 }
Jeff Brown84715842012-05-25 14:07:47 -07001371
Mike Lockwood4553b082010-08-16 14:14:44 -04001372 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001373 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001374 }
Jeff Brown84715842012-05-25 14:07:47 -07001375
Brian Swetland03ee9472010-08-12 18:01:08 -07001376 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001377 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001378 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001379 }
Jeff Brown84715842012-05-25 14:07:47 -07001380
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001381 case FUSE_FSYNC:
1382 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001383 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001384 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001385 }
1386
Brian Swetland03ee9472010-08-12 18:01:08 -07001387// case FUSE_SETXATTR:
1388// case FUSE_GETXATTR:
1389// case FUSE_LISTXATTR:
1390// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001391 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001392 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001393 }
1394
Brian Swetland03ee9472010-08-12 18:01:08 -07001395 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001396 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001397 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001398 }
Jeff Brown84715842012-05-25 14:07:47 -07001399
Brian Swetland03ee9472010-08-12 18:01:08 -07001400 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001401 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001402 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001403 }
Jeff Brown84715842012-05-25 14:07:47 -07001404
Brian Swetland03ee9472010-08-12 18:01:08 -07001405 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001406 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001407 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001408 }
Jeff Brown84715842012-05-25 14:07:47 -07001409
Brian Swetland03ee9472010-08-12 18:01:08 -07001410 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001411 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001412 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001413 }
Jeff Brown84715842012-05-25 14:07:47 -07001414
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001415 case FUSE_CANONICAL_PATH: { /* nodeid -> bytez[] */
1416 return handle_canonical_path(fuse, handler, hdr);
1417 }
1418
Brian Swetland03ee9472010-08-12 18:01:08 -07001419 default: {
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001420 DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001421 << "uniq=" << std::hex << hdr->unique << "nid=" << hdr->nodeid << std::dec;
Jeff Brown6249b902012-05-26 14:32:54 -07001422 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001423 }
Jeff Brown84715842012-05-25 14:07:47 -07001424 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001425}
1426
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001427void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001428{
Jeff Brown6249b902012-05-26 14:32:54 -07001429 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001430 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001431 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1432 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001433 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001434 if (errno == ENODEV) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001435 LOG(ERROR) << "[" << handler->token << "] someone stole our marbles!";
Jeff Sharkey4a485812015-06-30 16:02:40 -07001436 exit(2);
1437 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001438 PLOG(ERROR) << "[" << handler->token << "] handle_fuse_requests";
Jeff Brown6249b902012-05-26 14:32:54 -07001439 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001440 }
Jeff Brown84715842012-05-25 14:07:47 -07001441
1442 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001443 LOG(ERROR) << "[" << handler->token << "] request too short: len=" << len;
Jeff Brown6249b902012-05-26 14:32:54 -07001444 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001445 }
1446
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001447 const struct fuse_in_header* hdr =
1448 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001449 if (hdr->len != (size_t)len) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001450 LOG(ERROR) << "[" << handler->token << "] malformed header: len=" << len
1451 << ", hdr->len=" << hdr->len;
Jeff Brown6249b902012-05-26 14:32:54 -07001452 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001453 }
1454
Jeff Brown7729d242012-05-25 15:35:28 -07001455 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001456 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001457 __u64 unique = hdr->unique;
1458 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001459
1460 /* We do not access the request again after this point because the underlying
1461 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001462
1463 if (res != NO_STATUS) {
1464 if (res) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001465 DLOG(INFO) << "[" << handler->token << "] ERROR " << res;
Jeff Brown6249b902012-05-26 14:32:54 -07001466 }
1467 fuse_status(fuse, unique, res);
1468 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001469 }
1470}