blob: b24c0d427a150cabbcdedf85e7dde105e6c6aa74 [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
Brian Swetland03ee9472010-08-12 18:01:08 -070026#define FUSE_UNKNOWN_INO 0xffffffff
27
Jeff Brown6249b902012-05-26 14:32:54 -070028/* Pseudo-error constant used to indicate that no fuse status is needed
29 * or that a reply has already been written. */
30#define NO_STATUS 1
31
Jeff Brown6249b902012-05-26 14:32:54 -070032static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -070033{
Jeff Brown6249b902012-05-26 14:32:54 -070034 return (void *) (uintptr_t) nid;
35}
Brian Swetland03ee9472010-08-12 18:01:08 -070036
Jeff Brown6249b902012-05-26 14:32:54 -070037static inline __u64 ptr_to_id(void *ptr)
38{
39 return (__u64) (uintptr_t) ptr;
40}
Brian Swetland03ee9472010-08-12 18:01:08 -070041
Jeff Brown6249b902012-05-26 14:32:54 -070042static void acquire_node_locked(struct node* node)
43{
44 node->refcount++;
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040045 DLOG(INFO) << "ACQUIRE " << std::hex << node << std::dec
46 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070047}
48
49static void remove_node_from_parent_locked(struct node* node);
50
51static void release_node_locked(struct node* node)
52{
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040053 DLOG(INFO) << "RELEASE " << std::hex << node << std::dec
54 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070055 if (node->refcount > 0) {
56 node->refcount--;
57 if (!node->refcount) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040058 DLOG(INFO) << "DESTROY " << std::hex << node << std::dec << " (" << node->name << ")";
Jeff Brown6249b902012-05-26 14:32:54 -070059 remove_node_from_parent_locked(node);
60
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040061 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070062 memset(node->name, 0xef, node->namelen);
63 free(node->name);
64 free(node->actual_name);
65 memset(node, 0xfc, sizeof(*node));
66 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080067 }
Jeff Brown6249b902012-05-26 14:32:54 -070068 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040069 LOG(ERROR) << std::hex << node << std::dec << " refcount=0";
Jeff Brown6249b902012-05-26 14:32:54 -070070 }
71}
72
73static void add_node_to_parent_locked(struct node *node, struct node *parent) {
74 node->parent = parent;
75 node->next = parent->child;
76 parent->child = node;
77 acquire_node_locked(parent);
78}
79
80static void remove_node_from_parent_locked(struct node* node)
81{
82 if (node->parent) {
83 if (node->parent->child == node) {
84 node->parent->child = node->parent->child->next;
85 } else {
86 struct node *node2;
87 node2 = node->parent->child;
88 while (node2->next != node)
89 node2 = node2->next;
90 node2->next = node->next;
91 }
92 release_node_locked(node->parent);
93 node->parent = NULL;
94 node->next = NULL;
95 }
96}
97
98/* Gets the absolute path to a node into the provided buffer.
99 *
100 * Populates 'buf' with the path and returns the length of the path on success,
101 * or returns -1 if the path is too long for the provided buffer.
102 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700103static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
104 const char* name;
105 size_t namelen;
106 if (node->graft_path) {
107 name = node->graft_path;
108 namelen = node->graft_pathlen;
109 } else if (node->actual_name) {
110 name = node->actual_name;
111 namelen = node->namelen;
112 } else {
113 name = node->name;
114 namelen = node->namelen;
115 }
116
Jeff Brown6249b902012-05-26 14:32:54 -0700117 if (bufsize < namelen + 1) {
118 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700119 }
120
Jeff Brown6249b902012-05-26 14:32:54 -0700121 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700122 if (node->parent && node->graft_path == NULL) {
Daniel Rosenbergdb4638e2016-04-12 16:30:28 -0700123 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700124 if (pathlen < 0) {
125 return -1;
126 }
127 buf[pathlen++] = '/';
128 }
129
Jeff Brown6249b902012-05-26 14:32:54 -0700130 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
131 return pathlen + namelen;
132}
133
134/* Finds the absolute path of a file within a given directory.
135 * Performs a case-insensitive search for the file and sets the buffer to the path
136 * of the first matching file. If 'search' is zero or if no match is found, sets
137 * the buffer to the path that the file would have, assuming the name were case-sensitive.
138 *
139 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
140 * or returns NULL if the path is too long for the provided buffer.
141 */
142static char* find_file_within(const char* path, const char* name,
143 char* buf, size_t bufsize, int search)
144{
145 size_t pathlen = strlen(path);
146 size_t namelen = strlen(name);
147 size_t childlen = pathlen + namelen + 1;
148 char* actual;
149
150 if (bufsize <= childlen) {
151 return NULL;
152 }
153
154 memcpy(buf, path, pathlen);
155 buf[pathlen] = '/';
156 actual = buf + pathlen + 1;
157 memcpy(actual, name, namelen + 1);
158
159 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800160 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700161 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800162 if (!dir) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400163 PLOG(ERROR) << "opendir(" << path << ") failed";
Jeff Brown6249b902012-05-26 14:32:54 -0700164 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800165 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800166 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700167 if (!strcasecmp(entry->d_name, name)) {
168 /* we have a match - replace the name, don't need to copy the null again */
169 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800170 break;
171 }
172 }
173 closedir(dir);
174 }
Jeff Brown6249b902012-05-26 14:32:54 -0700175 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800176}
177
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700178static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
179 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000180 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700181 attr->size = s->st_size;
182 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800183 attr->atime = s->st_atim.tv_sec;
184 attr->mtime = s->st_mtim.tv_sec;
185 attr->ctime = s->st_ctim.tv_sec;
186 attr->atimensec = s->st_atim.tv_nsec;
187 attr->mtimensec = s->st_mtim.tv_nsec;
188 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700189 attr->mode = s->st_mode;
190 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700191
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700192 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700193
194 if (fuse->gid == AID_SDCARD_RW) {
195 /* As an optimization, certain trusted system components only run
196 * as owner but operate across all users. Since we're now handing
197 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
198 * the user boundary enforcement for the default view. The UIDs
199 * assigned to app directories are still multiuser aware. */
200 attr->gid = AID_SDCARD_RW;
201 } else {
202 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
203 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700204
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700205 int visible_mode = 0775 & ~fuse->mask;
206 if (node->perm == PERM_PRE_ROOT) {
207 /* Top of multi-user view should always be visible to ensure
208 * secondary users can traverse inside. */
209 visible_mode = 0711;
210 } else if (node->under_android) {
211 /* Block "other" access to Android directories, since only apps
212 * belonging to a specific user should be in there; we still
213 * leave +x open for the default view. */
214 if (fuse->gid == AID_SDCARD_RW) {
215 visible_mode = visible_mode & ~0006;
216 } else {
217 visible_mode = visible_mode & ~0007;
218 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700219 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700220 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700221 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700222 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
223}
224
Jeff Sharkey44d63422013-09-12 09:44:48 -0700225static int touch(char* path, mode_t mode) {
226 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
227 if (fd == -1) {
228 if (errno == EEXIST) {
229 return 0;
230 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400231 PLOG(ERROR) << "open(" << path << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700232 return -1;
233 }
234 }
235 close(fd);
236 return 0;
237}
238
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700239static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
240 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700241 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700242
243 /* By default, each node inherits from its parent */
244 node->perm = PERM_INHERIT;
245 node->userid = parent->userid;
246 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700247 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700248
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700249 /* Derive custom permissions based on parent and current node */
250 switch (parent->perm) {
251 case PERM_INHERIT:
252 /* Already inherited above */
253 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700254 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700255 /* Legacy internal layout places users at top level */
256 node->perm = PERM_ROOT;
257 node->userid = strtoul(node->name, NULL, 10);
258 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700259 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700260 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700261 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700262 /* App-specific directories inside; let anyone traverse */
263 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700264 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700265 }
266 break;
267 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700268 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700269 /* App-specific directories inside; let anyone traverse */
270 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700271 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700272 /* App-specific directories inside; let anyone traverse */
273 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700274 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700275 node->graft_path = fuse->global->obb_path;
276 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700277 } else if (!strcasecmp(node->name, "media")) {
278 /* App-specific directories inside; let anyone traverse */
279 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700280 }
281 break;
282 case PERM_ANDROID_DATA:
283 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700284 case PERM_ANDROID_MEDIA:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400285 const auto& iter = fuse->global->package_to_appid->find(node->name);
286 if (iter != fuse->global->package_to_appid->end()) {
287 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700288 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700289 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700290 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700291 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700292}
293
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400294void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700295 struct node *node;
296 for (node = parent->child; node; node = node->next) {
297 derive_permissions_locked(fuse, parent, node);
298 if (node->child) {
299 derive_permissions_recursive_locked(fuse, node);
300 }
301 }
302}
303
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700304/* Kernel has already enforced everything we returned through
305 * derive_permissions_locked(), so this is used to lock down access
306 * even further, such as enforcing that apps hold sdcard_rw. */
307static bool check_caller_access_to_name(struct fuse* fuse,
308 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700309 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700310 /* Always block security-sensitive files at root */
311 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700312 if (!strcasecmp(name, "autorun.inf")
313 || !strcasecmp(name, ".android_secure")
314 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700315 return false;
316 }
317 }
318
Jeff Sharkey44d63422013-09-12 09:44:48 -0700319 /* Root always has access; access for any other UIDs should always
320 * be controlled through packages.list. */
321 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700322 return true;
323 }
324
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700325 /* No extra permissions to enforce */
326 return true;
327}
328
329static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700330 const struct fuse_in_header *hdr, const struct node* node, int mode) {
331 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700332}
333
Jeff Brown6249b902012-05-26 14:32:54 -0700334struct node *create_node_locked(struct fuse* fuse,
335 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700336{
337 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700338 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700339
Narayan Kamathfaa09352015-01-13 18:21:10 +0000340 // Detect overflows in the inode counter. "4 billion nodes should be enough
341 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700342 if (fuse->global->inode_ctr == 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400343 LOG(ERROR) << "No more inode numbers available";
Narayan Kamathfaa09352015-01-13 18:21:10 +0000344 return NULL;
345 }
346
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400347 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700348 if (!node) {
349 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700350 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400351 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700352 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700353 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700354 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700355 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700356 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700357 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400358 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700359 if (!node->actual_name) {
360 free(node->name);
361 free(node);
362 return NULL;
363 }
364 memcpy(node->actual_name, actual_name, namelen + 1);
365 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700366 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700367 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700368 node->ino = fuse->global->inode_ctr++;
369 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700370
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200371 node->deleted = false;
372
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700373 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700374 acquire_node_locked(node);
375 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700376 return node;
377}
378
Jeff Brown6249b902012-05-26 14:32:54 -0700379static int rename_node_locked(struct node *node, const char *name,
380 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700381{
Jeff Brown6249b902012-05-26 14:32:54 -0700382 size_t namelen = strlen(name);
383 int need_actual_name = strcmp(name, actual_name);
384
385 /* make the storage bigger without actually changing the name
386 * in case an error occurs part way */
387 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400388 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700389 if (!new_name) {
390 return -ENOMEM;
391 }
392 node->name = new_name;
393 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400394 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700395 if (!new_actual_name) {
396 return -ENOMEM;
397 }
398 node->actual_name = new_actual_name;
399 }
400 }
401
402 /* update the name, taking care to allocate storage before overwriting the old name */
403 if (need_actual_name) {
404 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400405 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700406 if (!node->actual_name) {
407 return -ENOMEM;
408 }
409 }
410 memcpy(node->actual_name, actual_name, namelen + 1);
411 } else {
412 free(node->actual_name);
413 node->actual_name = NULL;
414 }
415 memcpy(node->name, name, namelen + 1);
416 node->namelen = namelen;
417 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700418}
419
Jeff Brown6249b902012-05-26 14:32:54 -0700420static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700421{
Jeff Brown6249b902012-05-26 14:32:54 -0700422 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700423 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700424 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400425 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700426 }
427}
428
Jeff Brown6249b902012-05-26 14:32:54 -0700429static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
430 char* buf, size_t bufsize)
431{
432 struct node* node = lookup_node_by_id_locked(fuse, nid);
433 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
434 node = NULL;
435 }
436 return node;
437}
438
439static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700440{
441 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700442 /* use exact string comparison, nodes that differ by case
443 * must be considered distinct even if they refer to the same
444 * underlying file as otherwise operations such as "mv x x"
445 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200446 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700447 return node;
448 }
449 }
450 return 0;
451}
452
Jeff Brown6249b902012-05-26 14:32:54 -0700453static struct node* acquire_or_create_child_locked(
454 struct fuse* fuse, struct node* parent,
455 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700456{
Jeff Brown6249b902012-05-26 14:32:54 -0700457 struct node* child = lookup_child_by_name_locked(parent, name);
458 if (child) {
459 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800460 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700461 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800462 }
Jeff Brown6249b902012-05-26 14:32:54 -0700463 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700464}
465
Jeff Brown6249b902012-05-26 14:32:54 -0700466static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700467{
468 struct fuse_out_header hdr;
469 hdr.len = sizeof(hdr);
470 hdr.error = err;
471 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700472 write(fuse->fd, &hdr, sizeof(hdr));
473}
474
Jeff Brown6249b902012-05-26 14:32:54 -0700475static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700476{
477 struct fuse_out_header hdr;
478 struct iovec vec[2];
479 int res;
480
481 hdr.len = len + sizeof(hdr);
482 hdr.error = 0;
483 hdr.unique = unique;
484
485 vec[0].iov_base = &hdr;
486 vec[0].iov_len = sizeof(hdr);
487 vec[1].iov_base = data;
488 vec[1].iov_len = len;
489
490 res = writev(fuse->fd, vec, 2);
491 if (res < 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400492 PLOG(ERROR) << "*** REPLY FAILED ***";
Brian Swetland03ee9472010-08-12 18:01:08 -0700493 }
494}
495
Jeff Brown6249b902012-05-26 14:32:54 -0700496static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
497 struct node* parent, const char* name, const char* actual_name,
498 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700499{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700500 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700501 struct fuse_entry_out out;
502 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700503
Jeff Brown6249b902012-05-26 14:32:54 -0700504 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700505 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700506 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700507
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700508 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700509 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
510 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700511 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700512 return -ENOMEM;
513 }
514 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700515 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700516 out.attr_valid = 10;
517 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700518 out.nodeid = node->nid;
519 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700520 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700521 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700522 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700523}
524
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700525static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700526 const char* path)
527{
528 struct fuse_attr_out out;
529 struct stat s;
530
531 if (lstat(path, &s) < 0) {
532 return -errno;
533 }
534 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700535 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700536 out.attr_valid = 10;
537 fuse_reply(fuse, unique, &out, sizeof(out));
538 return NO_STATUS;
539}
540
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700541static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
542 const __u64 child, const char* name) {
543 struct fuse_out_header hdr;
544 struct fuse_notify_delete_out data;
545 struct iovec vec[3];
546 size_t namelen = strlen(name);
547 int res;
548
549 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
550 hdr.error = FUSE_NOTIFY_DELETE;
551 hdr.unique = 0;
552
553 data.parent = parent;
554 data.child = child;
555 data.namelen = namelen;
556 data.padding = 0;
557
558 vec[0].iov_base = &hdr;
559 vec[0].iov_len = sizeof(hdr);
560 vec[1].iov_base = &data;
561 vec[1].iov_len = sizeof(data);
562 vec[2].iov_base = (void*) name;
563 vec[2].iov_len = namelen + 1;
564
565 res = writev(fuse->fd, vec, 3);
566 /* Ignore ENOENT, since other views may not have seen the entry */
567 if (res < 0 && errno != ENOENT) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400568 PLOG(ERROR) << "*** NOTIFY FAILED ***";
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700569 }
570}
571
Jeff Brown6249b902012-05-26 14:32:54 -0700572static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700573 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700574{
Jeff Brown6249b902012-05-26 14:32:54 -0700575 struct node* parent_node;
576 char parent_path[PATH_MAX];
577 char child_path[PATH_MAX];
578 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700579
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700580 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700581 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
582 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400583 DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid
584 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700585 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700586
587 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
588 child_path, sizeof(child_path), 1))) {
589 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700590 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700591 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700592 return -EACCES;
593 }
594
Jeff Brown6249b902012-05-26 14:32:54 -0700595 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700596}
597
Jeff Brown6249b902012-05-26 14:32:54 -0700598static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700599 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
600{
Jeff Brown6249b902012-05-26 14:32:54 -0700601 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700602
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700603 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700604 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400605 DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup
606 << " @ " << std::hex << hdr->nodeid
607 << " (" << (node ? node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700608 if (node) {
609 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400610 while (n) {
611 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700612 release_node_locked(node);
613 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700614 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700615 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700616 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700617}
618
Jeff Brown6249b902012-05-26 14:32:54 -0700619static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700620 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
621{
Jeff Brown6249b902012-05-26 14:32:54 -0700622 struct node* node;
623 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700624
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700625 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700626 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400627 DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400628 << " fh=" << std::hex << req->fh << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400629 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700630 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700631
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700632 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700633 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700634 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700635 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700636 return -EACCES;
637 }
638
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700639 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700640}
641
Jeff Brown6249b902012-05-26 14:32:54 -0700642static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700643 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
644{
Jeff Brown6249b902012-05-26 14:32:54 -0700645 struct node* node;
646 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700647 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700648
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700649 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700650 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400651 DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400652 << " valid=" << std::hex << req->valid << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400653 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700654 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700655
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700656 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700657 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700658 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800659
660 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700661 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700662 return -EACCES;
663 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700664
Jeff Brown6249b902012-05-26 14:32:54 -0700665 /* XXX: incomplete implementation on purpose.
666 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700667
Elliott Hughes853574d2014-07-31 12:03:03 -0700668 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700669 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700670 }
671
672 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
673 * are both set, then set it to the current time. Else, set it to the
674 * time specified in the request. Same goes for mtime. Use utimensat(2)
675 * as it allows ATIME and MTIME to be changed independently, and has
676 * nanosecond resolution which fuse also has.
677 */
678 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
679 times[0].tv_nsec = UTIME_OMIT;
680 times[1].tv_nsec = UTIME_OMIT;
681 if (req->valid & FATTR_ATIME) {
682 if (req->valid & FATTR_ATIME_NOW) {
683 times[0].tv_nsec = UTIME_NOW;
684 } else {
685 times[0].tv_sec = req->atime;
686 times[0].tv_nsec = req->atimensec;
687 }
688 }
689 if (req->valid & FATTR_MTIME) {
690 if (req->valid & FATTR_MTIME_NOW) {
691 times[1].tv_nsec = UTIME_NOW;
692 } else {
693 times[1].tv_sec = req->mtime;
694 times[1].tv_nsec = req->mtimensec;
695 }
696 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400697 DLOG(INFO) << "[" << handler->token << "] Calling utimensat on " << path
698 << " with atime " << times[0].tv_sec << ", mtime=" << times[1].tv_sec;
Jeff Brown6249b902012-05-26 14:32:54 -0700699 if (utimensat(-1, path, times, 0) < 0) {
700 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700701 }
702 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700703 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700704}
705
Jeff Brown6249b902012-05-26 14:32:54 -0700706static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700707 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
708{
Jeff Brown6249b902012-05-26 14:32:54 -0700709 struct node* parent_node;
710 char parent_path[PATH_MAX];
711 char child_path[PATH_MAX];
712 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700713
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700714 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700715 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
716 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400717 DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode
718 << " @ " << std::hex << hdr->nodeid
719 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700720 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700721
722 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
723 child_path, sizeof(child_path), 1))) {
724 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700725 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700726 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700727 return -EACCES;
728 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700729 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700730 if (mknod(child_path, mode, req->rdev) < 0) {
731 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700732 }
Jeff Brown6249b902012-05-26 14:32:54 -0700733 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700734}
735
Jeff Brown6249b902012-05-26 14:32:54 -0700736static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700737 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
738{
Jeff Brown6249b902012-05-26 14:32:54 -0700739 struct node* parent_node;
740 char parent_path[PATH_MAX];
741 char child_path[PATH_MAX];
742 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700743
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700744 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700745 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
746 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400747 DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode
748 << " @ " << std::hex << hdr->nodeid
749 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700750 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700751
752 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
753 child_path, sizeof(child_path), 1))) {
754 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700755 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700756 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700757 return -EACCES;
758 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700759 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700760 if (mkdir(child_path, mode) < 0) {
761 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700762 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700763
764 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
765 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
766 char nomedia[PATH_MAX];
767 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
768 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400769 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700770 return -ENOENT;
771 }
772 }
773 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
774 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700775 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700776 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400777 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700778 return -ENOENT;
779 }
780 }
781
Jeff Brown6249b902012-05-26 14:32:54 -0700782 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700783}
784
Jeff Brown6249b902012-05-26 14:32:54 -0700785static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700786 const struct fuse_in_header* hdr, const char* name)
787{
Jeff Brown6249b902012-05-26 14:32:54 -0700788 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200789 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700790 char parent_path[PATH_MAX];
791 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700792
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700793 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700794 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
795 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400796 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
797 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700798 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700799
800 if (!parent_node || !find_file_within(parent_path, name,
801 child_path, sizeof(child_path), 1)) {
802 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700803 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700804 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700805 return -EACCES;
806 }
Jeff Brown6249b902012-05-26 14:32:54 -0700807 if (unlink(child_path) < 0) {
808 return -errno;
809 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700810 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200811 child_node = lookup_child_by_name_locked(parent_node, name);
812 if (child_node) {
813 child_node->deleted = true;
814 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700815 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700816 if (parent_node && child_node) {
817 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400818 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
819 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400820 << ", child=" << std::hex << child_node->nid << std::dec
821 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700822 if (fuse != fuse->global->fuse_default) {
823 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
824 }
825 if (fuse != fuse->global->fuse_read) {
826 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
827 }
828 if (fuse != fuse->global->fuse_write) {
829 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
830 }
831 }
Jeff Brown6249b902012-05-26 14:32:54 -0700832 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700833}
834
Jeff Brown6249b902012-05-26 14:32:54 -0700835static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836 const struct fuse_in_header* hdr, const char* name)
837{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200838 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700839 struct node* parent_node;
840 char parent_path[PATH_MAX];
841 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700842
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700843 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700844 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
845 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400846 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
847 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700848 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700849
850 if (!parent_node || !find_file_within(parent_path, name,
851 child_path, sizeof(child_path), 1)) {
852 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700853 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700854 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700855 return -EACCES;
856 }
Jeff Brown6249b902012-05-26 14:32:54 -0700857 if (rmdir(child_path) < 0) {
858 return -errno;
859 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700860 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200861 child_node = lookup_child_by_name_locked(parent_node, name);
862 if (child_node) {
863 child_node->deleted = true;
864 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700865 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700866 if (parent_node && child_node) {
867 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400868 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
869 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400870 << ", child=" << std::hex << child_node->nid << std::dec
871 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700872 if (fuse != fuse->global->fuse_default) {
873 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
874 }
875 if (fuse != fuse->global->fuse_read) {
876 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
877 }
878 if (fuse != fuse->global->fuse_write) {
879 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
880 }
881 }
Jeff Brown6249b902012-05-26 14:32:54 -0700882 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883}
884
Jeff Brown6249b902012-05-26 14:32:54 -0700885static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700887 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888{
Jeff Brown6249b902012-05-26 14:32:54 -0700889 struct node* old_parent_node;
890 struct node* new_parent_node;
891 struct node* child_node;
892 char old_parent_path[PATH_MAX];
893 char new_parent_path[PATH_MAX];
894 char old_child_path[PATH_MAX];
895 char new_child_path[PATH_MAX];
896 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400897 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700898 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700899
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700900 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700901 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
902 old_parent_path, sizeof(old_parent_path));
903 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
904 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400905 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name
906 << " @ " << std::hex << hdr->nodeid
907 << " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> "
908 << std::hex << req->newdir
909 << " (" << (new_parent_node ? new_parent_node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700910 if (!old_parent_node || !new_parent_node) {
911 res = -ENOENT;
912 goto lookup_error;
913 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700914 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700915 res = -EACCES;
916 goto lookup_error;
917 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700918 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700919 res = -EACCES;
920 goto lookup_error;
921 }
Jeff Brown6249b902012-05-26 14:32:54 -0700922 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
923 if (!child_node || get_node_path_locked(child_node,
924 old_child_path, sizeof(old_child_path)) < 0) {
925 res = -ENOENT;
926 goto lookup_error;
927 }
928 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700929 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700930
931 /* Special case for renaming a file where destination is same path
932 * differing only by case. In this case we don't want to look for a case
933 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
934 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400935 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700936 || strcasecmp(old_name, new_name);
937 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
938 new_child_path, sizeof(new_child_path), search))) {
939 res = -ENOENT;
940 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941 }
942
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400943 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_child_path << "->" << new_child_path;
Jeff Brown6249b902012-05-26 14:32:54 -0700944 res = rename(old_child_path, new_child_path);
945 if (res < 0) {
946 res = -errno;
947 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948 }
949
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700950 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700951 res = rename_node_locked(child_node, new_name, new_actual_name);
952 if (!res) {
953 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700954 derive_permissions_locked(fuse, new_parent_node, child_node);
955 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700956 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700957 }
Jeff Brown6249b902012-05-26 14:32:54 -0700958 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700959
Jeff Brown6249b902012-05-26 14:32:54 -0700960io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700961 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700962done:
963 release_node_locked(child_node);
964lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700965 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700966 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967}
968
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700969static int open_flags_to_access_mode(int open_flags) {
970 if ((open_flags & O_ACCMODE) == O_RDONLY) {
971 return R_OK;
972 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
973 return W_OK;
974 } else {
975 /* Probably O_RDRW, but treat as default to be safe */
976 return R_OK | W_OK;
977 }
978}
979
Jeff Brown6249b902012-05-26 14:32:54 -0700980static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700981 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
982{
Jeff Brown6249b902012-05-26 14:32:54 -0700983 struct node* node;
984 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700985 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700986 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700987
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700988 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700989 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400990 DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400991 << " @ " << std::hex << hdr->nodeid << std::dec
992 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700993 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700994
995 if (!node) {
996 return -ENOENT;
997 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700998 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700999 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001000 return -EACCES;
1001 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001002 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001003 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001004 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001006 DLOG(INFO) << "[" << handler->token << "] OPEN " << path;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001007 h->fd = open(path, req->flags);
1008 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001009 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001010 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001011 }
1012 out.fh = ptr_to_id(h);
1013 out.open_flags = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001014
1015#ifdef FUSE_SHORTCIRCUIT
1016 out.lower_fd = h->fd;
1017#else
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 out.padding = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001019#endif
1020
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001021 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001022 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023}
1024
Jeff Brown6249b902012-05-26 14:32:54 -07001025static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1027{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001028 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001029 __u64 unique = hdr->unique;
1030 __u32 size = req->size;
1031 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001032 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001033 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001034
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001035 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1036 * overlaps the request buffer and will clobber data in the request. This
1037 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001038
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001039 DLOG(INFO) << "[" << handler->token << "] READ " << std::hex << h << std::dec
1040 << "(" << h->fd << ") " << size << "@" << offset;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001041 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001042 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001043 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001044 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001045 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001046 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001047 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001048 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001049 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050}
1051
Jeff Brown6249b902012-05-26 14:32:54 -07001052static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001053 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1054 const void* buffer)
1055{
1056 struct fuse_write_out out;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001057 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001058 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001059 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001060
Arpad Horvath49e93442014-02-18 10:18:25 +01001061 if (req->flags & O_DIRECT) {
1062 memcpy(aligned_buffer, buffer, req->size);
1063 buffer = (const __u8*) aligned_buffer;
1064 }
Jeff Brown6249b902012-05-26 14:32:54 -07001065
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001066 DLOG(INFO) << "[" << handler->token << "] WRITE " << std::hex << h << std::dec
1067 << "(" << h->fd << ") " << req->size << "@" << req->offset;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068 res = pwrite64(h->fd, buffer, req->size, req->offset);
1069 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001070 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001071 }
1072 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001073 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001074 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001075 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001076}
1077
Jeff Brown6249b902012-05-26 14:32:54 -07001078static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001079 const struct fuse_in_header* hdr)
1080{
Jeff Brown6249b902012-05-26 14:32:54 -07001081 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001082 struct statfs stat;
1083 struct fuse_statfs_out out;
1084 int res;
1085
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001086 pthread_mutex_lock(&fuse->global->lock);
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001087 DLOG(INFO) << "[" << handler->token << "] STATFS";
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001088 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001089 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001090 if (res < 0) {
1091 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001093 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001094 return -errno;
1095 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001096 memset(&out, 0, sizeof(out));
1097 out.st.blocks = stat.f_blocks;
1098 out.st.bfree = stat.f_bfree;
1099 out.st.bavail = stat.f_bavail;
1100 out.st.files = stat.f_files;
1101 out.st.ffree = stat.f_ffree;
1102 out.st.bsize = stat.f_bsize;
1103 out.st.namelen = stat.f_namelen;
1104 out.st.frsize = stat.f_frsize;
1105 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001106 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107}
1108
Jeff Brown6249b902012-05-26 14:32:54 -07001109static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001110 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1111{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001112 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001113
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001114 DLOG(INFO) << "[" << handler->token << "] RELEASE " << std::hex << h << std::dec
1115 << "(" << h->fd << ")";
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001116 close(h->fd);
1117 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001118 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001119}
1120
Jeff Brown6249b902012-05-26 14:32:54 -07001121static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001122 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1123{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001124 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1125 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001126
Elliott Hughesf6d67372014-07-08 14:38:26 -07001127 int fd = -1;
1128 if (is_dir) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001129 struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001130 fd = dirfd(dh->d);
1131 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001132 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001133 fd = h->fd;
1134 }
1135
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001136 DLOG(INFO) << "[" << handler->token << "] " << (is_dir ? "FSYNCDIR" : "FSYNC") << " "
1137 << std::hex << req->fh << std::dec << "(" << fd << ") is_data_sync=" << is_data_sync;
Elliott Hughesf6d67372014-07-08 14:38:26 -07001138 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1139 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001140 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001141 }
Jeff Brown6249b902012-05-26 14:32:54 -07001142 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001143}
1144
Jeff Brown6249b902012-05-26 14:32:54 -07001145static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001146 const struct fuse_in_header* hdr)
1147{
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001148 DLOG(INFO) << "[" << handler->token << "] FLUSH";
Jeff Brown6249b902012-05-26 14:32:54 -07001149 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150}
1151
Jeff Brown6249b902012-05-26 14:32:54 -07001152static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001153 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1154{
Jeff Brown6249b902012-05-26 14:32:54 -07001155 struct node* node;
1156 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001157 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001159
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001160 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001161 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001162 DLOG(INFO) << "[" << handler->token << "] OPENDIR @ " << std::hex << hdr->nodeid
1163 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001164 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001165
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001166 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001167 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001168 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001169 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001170 return -EACCES;
1171 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001172 h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001173 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001174 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001175 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001176 DLOG(INFO) << "[" << handler->token << "] OPENDIR " << path;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001177 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001178 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001179 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001180 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001181 }
1182 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001183 out.open_flags = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001184
1185#ifdef FUSE_SHORTCIRCUIT
1186 out.lower_fd = -1;
1187#else
Ken Sumrall3a876882013-08-14 20:02:13 -07001188 out.padding = 0;
Thierry Strudel55cec582016-09-26 21:25:32 +00001189#endif
1190
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001191 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001192 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193}
1194
Jeff Brown6249b902012-05-26 14:32:54 -07001195static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001196 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1197{
1198 char buffer[8192];
1199 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1200 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001201 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001202
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001203 DLOG(INFO) << "[" << handler->token << "] READDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001204 if (req->offset == 0) {
1205 /* rewinddir() might have been called above us, so rewind here too */
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001206 DLOG(INFO) << "[" << handler->token << "] calling rewinddir()";
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 rewinddir(h->d);
1208 }
1209 de = readdir(h->d);
1210 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001211 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 }
1213 fde->ino = FUSE_UNKNOWN_INO;
1214 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1215 fde->off = req->offset + 1;
1216 fde->type = de->d_type;
1217 fde->namelen = strlen(de->d_name);
1218 memcpy(fde->name, de->d_name, fde->namelen + 1);
1219 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001220 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1221 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222}
1223
Jeff Brown6249b902012-05-26 14:32:54 -07001224static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1226{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001227 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001228
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001229 DLOG(INFO) << "[" << handler->token << "] RELEASEDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001230 closedir(h->d);
1231 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001232 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001233}
1234
Jeff Brown6249b902012-05-26 14:32:54 -07001235static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001236 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1237{
1238 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001239 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001240
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001241 DLOG(INFO) << "[" << handler->token << "] INIT ver=" << req->major << "." << req->minor
1242 << " maxread=" << req->max_readahead << " flags=" << std::hex << req->flags;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001243
1244 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1245 * defined (fuse version 7.6). The structure is the same from 7.6 through
1246 * 7.22. Beginning with 7.23, the structure increased in size and added
1247 * new parameters.
1248 */
1249 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001250 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version "
1251 << req->major << "." << req->minor
1252 << ", Expected at least " << FUSE_KERNEL_VERSION << ".6";
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001253 return -1;
1254 }
1255
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001256 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1257 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001258 fuse_struct_size = sizeof(out);
1259#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1260 /* FUSE_KERNEL_VERSION >= 23. */
1261
1262 /* If the kernel only works on minor revs older than or equal to 22,
1263 * then use the older structure size since this code only uses the 7.22
1264 * version of the structure. */
1265 if (req->minor <= 22) {
1266 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1267 }
1268#endif
1269
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001270 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001271 out.max_readahead = req->max_readahead;
1272 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Thierry Strudel55cec582016-09-26 21:25:32 +00001273
1274#ifdef FUSE_SHORTCIRCUIT
1275 out.flags |= FUSE_SHORTCIRCUIT;
1276#endif
1277
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001278 out.max_background = 32;
1279 out.congestion_threshold = 32;
1280 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001281 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001282 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283}
1284
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001285static int handle_canonical_path(struct fuse* fuse, struct fuse_handler* handler,
1286 const struct fuse_in_header *hdr)
1287{
1288 struct node* node;
1289 char path[PATH_MAX];
1290 int len;
1291
1292 pthread_mutex_lock(&fuse->global->lock);
1293 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1294 path, sizeof(path));
Jorge Lucangeli Obes8df46542016-07-26 20:07:10 -04001295 DLOG(INFO) << "[" << handler->token << "] CANONICAL_PATH @ " << std::hex << hdr->nodeid
1296 << std::dec << " (" << (node ? node->name : "?") << ")";
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001297 pthread_mutex_unlock(&fuse->global->lock);
1298
1299 if (!node) {
1300 return -ENOENT;
1301 }
1302 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
1303 return -EACCES;
1304 }
1305 len = strlen(path);
1306 if (len + 1 > PATH_MAX)
1307 len = PATH_MAX - 1;
1308 path[PATH_MAX - 1] = 0;
1309 fuse_reply(fuse, hdr->unique, path, len + 1);
1310 return NO_STATUS;
1311}
1312
1313
Jeff Brown6249b902012-05-26 14:32:54 -07001314static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001315 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1316{
Brian Swetland03ee9472010-08-12 18:01:08 -07001317 switch (hdr->opcode) {
1318 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001319 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001320 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001321 }
Jeff Brown84715842012-05-25 14:07:47 -07001322
Brian Swetland03ee9472010-08-12 18:01:08 -07001323 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001324 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001325 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001326 }
Jeff Brown84715842012-05-25 14:07:47 -07001327
Brian Swetland03ee9472010-08-12 18:01:08 -07001328 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001329 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001330 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001331 }
Jeff Brown84715842012-05-25 14:07:47 -07001332
Brian Swetland03ee9472010-08-12 18:01:08 -07001333 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001334 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001335 return handle_setattr(fuse, handler, hdr, req);
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_READLINK:
1339// case FUSE_SYMLINK:
1340 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001341 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001342 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001343 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001344 }
Jeff Brown84715842012-05-25 14:07:47 -07001345
Brian Swetland03ee9472010-08-12 18:01:08 -07001346 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001347 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001348 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001349 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001350 }
Jeff Brown84715842012-05-25 14:07:47 -07001351
Brian Swetland03ee9472010-08-12 18:01:08 -07001352 case FUSE_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001353 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001354 return handle_unlink(fuse, handler, hdr, 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_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001358 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001359 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001360 }
Jeff Brown84715842012-05-25 14:07:47 -07001361
Brian Swetland03ee9472010-08-12 18:01:08 -07001362 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001363 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001364 const char *old_name = ((const char*) data) + sizeof(*req);
1365 const char *new_name = old_name + strlen(old_name) + 1;
1366 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001367 }
Jeff Brown84715842012-05-25 14:07:47 -07001368
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001370 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001371 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001372 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001373 }
Jeff Brown84715842012-05-25 14:07:47 -07001374
Brian Swetland03ee9472010-08-12 18:01:08 -07001375 case FUSE_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001376 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001377 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001378 }
Jeff Brown84715842012-05-25 14:07:47 -07001379
Brian Swetland03ee9472010-08-12 18:01:08 -07001380 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001381 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001382 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001383 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001384 }
Jeff Brown84715842012-05-25 14:07:47 -07001385
Mike Lockwood4553b082010-08-16 14:14:44 -04001386 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001387 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001388 }
Jeff Brown84715842012-05-25 14:07:47 -07001389
Brian Swetland03ee9472010-08-12 18:01:08 -07001390 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001391 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001392 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001393 }
Jeff Brown84715842012-05-25 14:07:47 -07001394
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001395 case FUSE_FSYNC:
1396 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001397 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001398 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001399 }
1400
Brian Swetland03ee9472010-08-12 18:01:08 -07001401// case FUSE_SETXATTR:
1402// case FUSE_GETXATTR:
1403// case FUSE_LISTXATTR:
1404// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001405 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001406 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001407 }
1408
Brian Swetland03ee9472010-08-12 18:01:08 -07001409 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001410 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001411 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001412 }
Jeff Brown84715842012-05-25 14:07:47 -07001413
Brian Swetland03ee9472010-08-12 18:01:08 -07001414 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001415 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001416 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001417 }
Jeff Brown84715842012-05-25 14:07:47 -07001418
Brian Swetland03ee9472010-08-12 18:01:08 -07001419 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001420 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001421 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001422 }
Jeff Brown84715842012-05-25 14:07:47 -07001423
Brian Swetland03ee9472010-08-12 18:01:08 -07001424 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001425 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001426 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001427 }
Jeff Brown84715842012-05-25 14:07:47 -07001428
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001429 case FUSE_CANONICAL_PATH: { /* nodeid -> bytez[] */
1430 return handle_canonical_path(fuse, handler, hdr);
1431 }
1432
Brian Swetland03ee9472010-08-12 18:01:08 -07001433 default: {
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001434 DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001435 << "uniq=" << std::hex << hdr->unique << "nid=" << hdr->nodeid << std::dec;
Jeff Brown6249b902012-05-26 14:32:54 -07001436 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001437 }
Jeff Brown84715842012-05-25 14:07:47 -07001438 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001439}
1440
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001441void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001442{
Jeff Brown6249b902012-05-26 14:32:54 -07001443 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001444 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001445 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1446 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001447 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001448 if (errno == ENODEV) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001449 LOG(ERROR) << "[" << handler->token << "] someone stole our marbles!";
Jeff Sharkey4a485812015-06-30 16:02:40 -07001450 exit(2);
1451 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001452 PLOG(ERROR) << "[" << handler->token << "] handle_fuse_requests";
Jeff Brown6249b902012-05-26 14:32:54 -07001453 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001454 }
Jeff Brown84715842012-05-25 14:07:47 -07001455
1456 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001457 LOG(ERROR) << "[" << handler->token << "] request too short: len=" << len;
Jeff Brown6249b902012-05-26 14:32:54 -07001458 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001459 }
1460
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001461 const struct fuse_in_header* hdr =
1462 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001463 if (hdr->len != (size_t)len) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001464 LOG(ERROR) << "[" << handler->token << "] malformed header: len=" << len
1465 << ", hdr->len=" << hdr->len;
Jeff Brown6249b902012-05-26 14:32:54 -07001466 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001467 }
1468
Jeff Brown7729d242012-05-25 15:35:28 -07001469 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001470 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001471 __u64 unique = hdr->unique;
1472 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001473
1474 /* We do not access the request again after this point because the underlying
1475 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001476
1477 if (res != NO_STATUS) {
1478 if (res) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001479 DLOG(INFO) << "[" << handler->token << "] ERROR " << res;
Jeff Brown6249b902012-05-26 14:32:54 -07001480 }
1481 fuse_status(fuse, unique, res);
1482 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001483 }
1484}