blob: 47e4257f17ef1d9753d010fce73ba01d4727605c [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 Swetland03ee9472010-08-12 18:01:08 -070020
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -040021#include <android-base/logging.h>
22
Brian Swetland03ee9472010-08-12 18:01:08 -070023#define FUSE_UNKNOWN_INO 0xffffffff
24
Jeff Brown6249b902012-05-26 14:32:54 -070025/* Pseudo-error constant used to indicate that no fuse status is needed
26 * or that a reply has already been written. */
27#define NO_STATUS 1
28
Jeff Brown6249b902012-05-26 14:32:54 -070029static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -070030{
Jeff Brown6249b902012-05-26 14:32:54 -070031 return (void *) (uintptr_t) nid;
32}
Brian Swetland03ee9472010-08-12 18:01:08 -070033
Jeff Brown6249b902012-05-26 14:32:54 -070034static inline __u64 ptr_to_id(void *ptr)
35{
36 return (__u64) (uintptr_t) ptr;
37}
Brian Swetland03ee9472010-08-12 18:01:08 -070038
Jeff Brown6249b902012-05-26 14:32:54 -070039static void acquire_node_locked(struct node* node)
40{
41 node->refcount++;
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040042 DLOG(INFO) << "ACQUIRE " << std::hex << node << std::dec
43 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070044}
45
46static void remove_node_from_parent_locked(struct node* node);
47
48static void release_node_locked(struct node* node)
49{
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040050 DLOG(INFO) << "RELEASE " << std::hex << node << std::dec
51 << " (" << node->name << ") rc=" << node->refcount;
Jeff Brown6249b902012-05-26 14:32:54 -070052 if (node->refcount > 0) {
53 node->refcount--;
54 if (!node->refcount) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040055 DLOG(INFO) << "DESTROY " << std::hex << node << std::dec << " (" << node->name << ")";
Jeff Brown6249b902012-05-26 14:32:54 -070056 remove_node_from_parent_locked(node);
57
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040058 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070059 memset(node->name, 0xef, node->namelen);
60 free(node->name);
61 free(node->actual_name);
62 memset(node, 0xfc, sizeof(*node));
63 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080064 }
Jeff Brown6249b902012-05-26 14:32:54 -070065 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -040066 LOG(ERROR) << std::hex << node << std::dec << " refcount=0";
Jeff Brown6249b902012-05-26 14:32:54 -070067 }
68}
69
70static void add_node_to_parent_locked(struct node *node, struct node *parent) {
71 node->parent = parent;
72 node->next = parent->child;
73 parent->child = node;
74 acquire_node_locked(parent);
75}
76
77static void remove_node_from_parent_locked(struct node* node)
78{
79 if (node->parent) {
80 if (node->parent->child == node) {
81 node->parent->child = node->parent->child->next;
82 } else {
83 struct node *node2;
84 node2 = node->parent->child;
85 while (node2->next != node)
86 node2 = node2->next;
87 node2->next = node->next;
88 }
89 release_node_locked(node->parent);
90 node->parent = NULL;
91 node->next = NULL;
92 }
93}
94
95/* Gets the absolute path to a node into the provided buffer.
96 *
97 * Populates 'buf' with the path and returns the length of the path on success,
98 * or returns -1 if the path is too long for the provided buffer.
99 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700100static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
101 const char* name;
102 size_t namelen;
103 if (node->graft_path) {
104 name = node->graft_path;
105 namelen = node->graft_pathlen;
106 } else if (node->actual_name) {
107 name = node->actual_name;
108 namelen = node->namelen;
109 } else {
110 name = node->name;
111 namelen = node->namelen;
112 }
113
Jeff Brown6249b902012-05-26 14:32:54 -0700114 if (bufsize < namelen + 1) {
115 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700116 }
117
Jeff Brown6249b902012-05-26 14:32:54 -0700118 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700119 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700120 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
121 if (pathlen < 0) {
122 return -1;
123 }
124 buf[pathlen++] = '/';
125 }
126
Jeff Brown6249b902012-05-26 14:32:54 -0700127 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
128 return pathlen + namelen;
129}
130
131/* Finds the absolute path of a file within a given directory.
132 * Performs a case-insensitive search for the file and sets the buffer to the path
133 * of the first matching file. If 'search' is zero or if no match is found, sets
134 * the buffer to the path that the file would have, assuming the name were case-sensitive.
135 *
136 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
137 * or returns NULL if the path is too long for the provided buffer.
138 */
139static char* find_file_within(const char* path, const char* name,
140 char* buf, size_t bufsize, int search)
141{
142 size_t pathlen = strlen(path);
143 size_t namelen = strlen(name);
144 size_t childlen = pathlen + namelen + 1;
145 char* actual;
146
147 if (bufsize <= childlen) {
148 return NULL;
149 }
150
151 memcpy(buf, path, pathlen);
152 buf[pathlen] = '/';
153 actual = buf + pathlen + 1;
154 memcpy(actual, name, namelen + 1);
155
156 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800157 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700158 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800159 if (!dir) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400160 PLOG(ERROR) << "opendir(" << path << ") failed";
Jeff Brown6249b902012-05-26 14:32:54 -0700161 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800162 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800163 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700164 if (!strcasecmp(entry->d_name, name)) {
165 /* we have a match - replace the name, don't need to copy the null again */
166 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800167 break;
168 }
169 }
170 closedir(dir);
171 }
Jeff Brown6249b902012-05-26 14:32:54 -0700172 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800173}
174
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700175static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
176 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000177 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700178 attr->size = s->st_size;
179 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800180 attr->atime = s->st_atim.tv_sec;
181 attr->mtime = s->st_mtim.tv_sec;
182 attr->ctime = s->st_ctim.tv_sec;
183 attr->atimensec = s->st_atim.tv_nsec;
184 attr->mtimensec = s->st_mtim.tv_nsec;
185 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700186 attr->mode = s->st_mode;
187 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700188
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700189 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700190
191 if (fuse->gid == AID_SDCARD_RW) {
192 /* As an optimization, certain trusted system components only run
193 * as owner but operate across all users. Since we're now handing
194 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
195 * the user boundary enforcement for the default view. The UIDs
196 * assigned to app directories are still multiuser aware. */
197 attr->gid = AID_SDCARD_RW;
198 } else {
199 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
200 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700201
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700202 int visible_mode = 0775 & ~fuse->mask;
203 if (node->perm == PERM_PRE_ROOT) {
204 /* Top of multi-user view should always be visible to ensure
205 * secondary users can traverse inside. */
206 visible_mode = 0711;
207 } else if (node->under_android) {
208 /* Block "other" access to Android directories, since only apps
209 * belonging to a specific user should be in there; we still
210 * leave +x open for the default view. */
211 if (fuse->gid == AID_SDCARD_RW) {
212 visible_mode = visible_mode & ~0006;
213 } else {
214 visible_mode = visible_mode & ~0007;
215 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700216 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700217 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700218 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700219 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
220}
221
Jeff Sharkey44d63422013-09-12 09:44:48 -0700222static int touch(char* path, mode_t mode) {
223 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
224 if (fd == -1) {
225 if (errno == EEXIST) {
226 return 0;
227 } else {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400228 PLOG(ERROR) << "open(" << path << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700229 return -1;
230 }
231 }
232 close(fd);
233 return 0;
234}
235
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700236static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
237 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700238 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700239
240 /* By default, each node inherits from its parent */
241 node->perm = PERM_INHERIT;
242 node->userid = parent->userid;
243 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700244 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700245
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700246 /* Derive custom permissions based on parent and current node */
247 switch (parent->perm) {
248 case PERM_INHERIT:
249 /* Already inherited above */
250 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700251 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700252 /* Legacy internal layout places users at top level */
253 node->perm = PERM_ROOT;
254 node->userid = strtoul(node->name, NULL, 10);
255 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700256 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700257 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700258 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700259 /* App-specific directories inside; let anyone traverse */
260 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700261 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700262 }
263 break;
264 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700265 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700266 /* App-specific directories inside; let anyone traverse */
267 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700268 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700269 /* App-specific directories inside; let anyone traverse */
270 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700271 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700272 node->graft_path = fuse->global->obb_path;
273 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700274 } else if (!strcasecmp(node->name, "media")) {
275 /* App-specific directories inside; let anyone traverse */
276 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700277 }
278 break;
279 case PERM_ANDROID_DATA:
280 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700281 case PERM_ANDROID_MEDIA:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400282 const auto& iter = fuse->global->package_to_appid->find(node->name);
283 if (iter != fuse->global->package_to_appid->end()) {
284 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700285 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700286 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700287 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700288 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700289}
290
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400291void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkey22b91262015-12-14 11:02:01 -0700292 struct node *node;
293 for (node = parent->child; node; node = node->next) {
294 derive_permissions_locked(fuse, parent, node);
295 if (node->child) {
296 derive_permissions_recursive_locked(fuse, node);
297 }
298 }
299}
300
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700301/* Kernel has already enforced everything we returned through
302 * derive_permissions_locked(), so this is used to lock down access
303 * even further, such as enforcing that apps hold sdcard_rw. */
304static bool check_caller_access_to_name(struct fuse* fuse,
305 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700306 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700307 /* Always block security-sensitive files at root */
308 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700309 if (!strcasecmp(name, "autorun.inf")
310 || !strcasecmp(name, ".android_secure")
311 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700312 return false;
313 }
314 }
315
Jeff Sharkey44d63422013-09-12 09:44:48 -0700316 /* Root always has access; access for any other UIDs should always
317 * be controlled through packages.list. */
318 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700319 return true;
320 }
321
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700322 /* No extra permissions to enforce */
323 return true;
324}
325
326static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700327 const struct fuse_in_header *hdr, const struct node* node, int mode) {
328 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700329}
330
Jeff Brown6249b902012-05-26 14:32:54 -0700331struct node *create_node_locked(struct fuse* fuse,
332 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700333{
334 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700335 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700336
Narayan Kamathfaa09352015-01-13 18:21:10 +0000337 // Detect overflows in the inode counter. "4 billion nodes should be enough
338 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700339 if (fuse->global->inode_ctr == 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400340 LOG(ERROR) << "No more inode numbers available";
Narayan Kamathfaa09352015-01-13 18:21:10 +0000341 return NULL;
342 }
343
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400344 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700345 if (!node) {
346 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700347 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400348 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700349 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700350 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700351 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700352 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700353 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700354 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400355 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700356 if (!node->actual_name) {
357 free(node->name);
358 free(node);
359 return NULL;
360 }
361 memcpy(node->actual_name, actual_name, namelen + 1);
362 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700363 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700364 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700365 node->ino = fuse->global->inode_ctr++;
366 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700367
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200368 node->deleted = false;
369
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700370 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700371 acquire_node_locked(node);
372 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700373 return node;
374}
375
Jeff Brown6249b902012-05-26 14:32:54 -0700376static int rename_node_locked(struct node *node, const char *name,
377 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700378{
Jeff Brown6249b902012-05-26 14:32:54 -0700379 size_t namelen = strlen(name);
380 int need_actual_name = strcmp(name, actual_name);
381
382 /* make the storage bigger without actually changing the name
383 * in case an error occurs part way */
384 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400385 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700386 if (!new_name) {
387 return -ENOMEM;
388 }
389 node->name = new_name;
390 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400391 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700392 if (!new_actual_name) {
393 return -ENOMEM;
394 }
395 node->actual_name = new_actual_name;
396 }
397 }
398
399 /* update the name, taking care to allocate storage before overwriting the old name */
400 if (need_actual_name) {
401 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400402 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700403 if (!node->actual_name) {
404 return -ENOMEM;
405 }
406 }
407 memcpy(node->actual_name, actual_name, namelen + 1);
408 } else {
409 free(node->actual_name);
410 node->actual_name = NULL;
411 }
412 memcpy(node->name, name, namelen + 1);
413 node->namelen = namelen;
414 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700415}
416
Jeff Brown6249b902012-05-26 14:32:54 -0700417static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700418{
Jeff Brown6249b902012-05-26 14:32:54 -0700419 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700420 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700421 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400422 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700423 }
424}
425
Jeff Brown6249b902012-05-26 14:32:54 -0700426static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
427 char* buf, size_t bufsize)
428{
429 struct node* node = lookup_node_by_id_locked(fuse, nid);
430 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
431 node = NULL;
432 }
433 return node;
434}
435
436static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700437{
438 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700439 /* use exact string comparison, nodes that differ by case
440 * must be considered distinct even if they refer to the same
441 * underlying file as otherwise operations such as "mv x x"
442 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200443 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700444 return node;
445 }
446 }
447 return 0;
448}
449
Jeff Brown6249b902012-05-26 14:32:54 -0700450static struct node* acquire_or_create_child_locked(
451 struct fuse* fuse, struct node* parent,
452 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700453{
Jeff Brown6249b902012-05-26 14:32:54 -0700454 struct node* child = lookup_child_by_name_locked(parent, name);
455 if (child) {
456 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800457 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700458 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800459 }
Jeff Brown6249b902012-05-26 14:32:54 -0700460 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700461}
462
Jeff Brown6249b902012-05-26 14:32:54 -0700463static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700464{
465 struct fuse_out_header hdr;
466 hdr.len = sizeof(hdr);
467 hdr.error = err;
468 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700469 write(fuse->fd, &hdr, sizeof(hdr));
470}
471
Jeff Brown6249b902012-05-26 14:32:54 -0700472static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700473{
474 struct fuse_out_header hdr;
475 struct iovec vec[2];
476 int res;
477
478 hdr.len = len + sizeof(hdr);
479 hdr.error = 0;
480 hdr.unique = unique;
481
482 vec[0].iov_base = &hdr;
483 vec[0].iov_len = sizeof(hdr);
484 vec[1].iov_base = data;
485 vec[1].iov_len = len;
486
487 res = writev(fuse->fd, vec, 2);
488 if (res < 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400489 PLOG(ERROR) << "*** REPLY FAILED ***";
Brian Swetland03ee9472010-08-12 18:01:08 -0700490 }
491}
492
Jeff Brown6249b902012-05-26 14:32:54 -0700493static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
494 struct node* parent, const char* name, const char* actual_name,
495 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700496{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700497 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700498 struct fuse_entry_out out;
499 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700500
Jeff Brown6249b902012-05-26 14:32:54 -0700501 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700502 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700503 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700504
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700505 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700506 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
507 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700508 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700509 return -ENOMEM;
510 }
511 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700512 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700513 out.attr_valid = 10;
514 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700515 out.nodeid = node->nid;
516 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700517 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700518 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700519 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700520}
521
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700522static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700523 const char* path)
524{
525 struct fuse_attr_out out;
526 struct stat s;
527
528 if (lstat(path, &s) < 0) {
529 return -errno;
530 }
531 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700532 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700533 out.attr_valid = 10;
534 fuse_reply(fuse, unique, &out, sizeof(out));
535 return NO_STATUS;
536}
537
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700538static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
539 const __u64 child, const char* name) {
540 struct fuse_out_header hdr;
541 struct fuse_notify_delete_out data;
542 struct iovec vec[3];
543 size_t namelen = strlen(name);
544 int res;
545
546 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
547 hdr.error = FUSE_NOTIFY_DELETE;
548 hdr.unique = 0;
549
550 data.parent = parent;
551 data.child = child;
552 data.namelen = namelen;
553 data.padding = 0;
554
555 vec[0].iov_base = &hdr;
556 vec[0].iov_len = sizeof(hdr);
557 vec[1].iov_base = &data;
558 vec[1].iov_len = sizeof(data);
559 vec[2].iov_base = (void*) name;
560 vec[2].iov_len = namelen + 1;
561
562 res = writev(fuse->fd, vec, 3);
563 /* Ignore ENOENT, since other views may not have seen the entry */
564 if (res < 0 && errno != ENOENT) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400565 PLOG(ERROR) << "*** NOTIFY FAILED ***";
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700566 }
567}
568
Jeff Brown6249b902012-05-26 14:32:54 -0700569static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700570 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700571{
Jeff Brown6249b902012-05-26 14:32:54 -0700572 struct node* parent_node;
573 char parent_path[PATH_MAX];
574 char child_path[PATH_MAX];
575 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700576
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700577 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700578 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
579 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400580 DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid
581 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700582 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700583
584 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
585 child_path, sizeof(child_path), 1))) {
586 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700587 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700588 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700589 return -EACCES;
590 }
591
Jeff Brown6249b902012-05-26 14:32:54 -0700592 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700593}
594
Jeff Brown6249b902012-05-26 14:32:54 -0700595static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700596 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
597{
Jeff Brown6249b902012-05-26 14:32:54 -0700598 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700599
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700600 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700601 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400602 DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup
603 << " @ " << std::hex << hdr->nodeid
604 << " (" << (node ? node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700605 if (node) {
606 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400607 while (n) {
608 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700609 release_node_locked(node);
610 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700611 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700612 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700613 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700614}
615
Jeff Brown6249b902012-05-26 14:32:54 -0700616static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700617 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
618{
Jeff Brown6249b902012-05-26 14:32:54 -0700619 struct node* node;
620 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700621
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700622 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700623 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400624 DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400625 << " fh=" << std::hex << req->fh << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400626 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700627 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700628
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700629 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700630 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700631 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700632 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700633 return -EACCES;
634 }
635
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700636 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700637}
638
Jeff Brown6249b902012-05-26 14:32:54 -0700639static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700640 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
641{
Jeff Brown6249b902012-05-26 14:32:54 -0700642 struct node* node;
643 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700644 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700645
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700646 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700647 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400648 DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400649 << " valid=" << std::hex << req->valid << " @ " << hdr->nodeid << std::dec
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400650 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700651 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700652
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700653 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700654 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700655 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800656
657 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700658 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700659 return -EACCES;
660 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700661
Jeff Brown6249b902012-05-26 14:32:54 -0700662 /* XXX: incomplete implementation on purpose.
663 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700664
Elliott Hughes853574d2014-07-31 12:03:03 -0700665 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700666 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700667 }
668
669 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
670 * are both set, then set it to the current time. Else, set it to the
671 * time specified in the request. Same goes for mtime. Use utimensat(2)
672 * as it allows ATIME and MTIME to be changed independently, and has
673 * nanosecond resolution which fuse also has.
674 */
675 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
676 times[0].tv_nsec = UTIME_OMIT;
677 times[1].tv_nsec = UTIME_OMIT;
678 if (req->valid & FATTR_ATIME) {
679 if (req->valid & FATTR_ATIME_NOW) {
680 times[0].tv_nsec = UTIME_NOW;
681 } else {
682 times[0].tv_sec = req->atime;
683 times[0].tv_nsec = req->atimensec;
684 }
685 }
686 if (req->valid & FATTR_MTIME) {
687 if (req->valid & FATTR_MTIME_NOW) {
688 times[1].tv_nsec = UTIME_NOW;
689 } else {
690 times[1].tv_sec = req->mtime;
691 times[1].tv_nsec = req->mtimensec;
692 }
693 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400694 DLOG(INFO) << "[" << handler->token << "] Calling utimensat on " << path
695 << " with atime " << times[0].tv_sec << ", mtime=" << times[1].tv_sec;
Jeff Brown6249b902012-05-26 14:32:54 -0700696 if (utimensat(-1, path, times, 0) < 0) {
697 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700698 }
699 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700700 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700701}
702
Jeff Brown6249b902012-05-26 14:32:54 -0700703static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700704 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
705{
Jeff Brown6249b902012-05-26 14:32:54 -0700706 struct node* parent_node;
707 char parent_path[PATH_MAX];
708 char child_path[PATH_MAX];
709 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700710
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700711 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700712 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
713 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400714 DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode
715 << " @ " << std::hex << hdr->nodeid
716 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700717 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700718
719 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
720 child_path, sizeof(child_path), 1))) {
721 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700722 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700723 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700724 return -EACCES;
725 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700726 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700727 if (mknod(child_path, mode, req->rdev) < 0) {
728 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700729 }
Jeff Brown6249b902012-05-26 14:32:54 -0700730 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700731}
732
Jeff Brown6249b902012-05-26 14:32:54 -0700733static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700734 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
735{
Jeff Brown6249b902012-05-26 14:32:54 -0700736 struct node* parent_node;
737 char parent_path[PATH_MAX];
738 char child_path[PATH_MAX];
739 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700740
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700741 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700742 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
743 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400744 DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode
745 << " @ " << std::hex << hdr->nodeid
746 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700747 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700748
749 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
750 child_path, sizeof(child_path), 1))) {
751 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700752 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700753 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700754 return -EACCES;
755 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700756 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700757 if (mkdir(child_path, mode) < 0) {
758 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700759 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700760
761 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
762 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
763 char nomedia[PATH_MAX];
764 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
765 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400766 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700767 return -ENOENT;
768 }
769 }
770 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
771 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700772 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700773 if (touch(nomedia, 0664) != 0) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400774 PLOG(ERROR) << "touch(" << nomedia << ") failed";
Jeff Sharkey44d63422013-09-12 09:44:48 -0700775 return -ENOENT;
776 }
777 }
778
Jeff Brown6249b902012-05-26 14:32:54 -0700779 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700780}
781
Jeff Brown6249b902012-05-26 14:32:54 -0700782static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700783 const struct fuse_in_header* hdr, const char* name)
784{
Jeff Brown6249b902012-05-26 14:32:54 -0700785 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200786 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700787 char parent_path[PATH_MAX];
788 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700789
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700790 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700791 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
792 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400793 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
794 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700795 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700796
797 if (!parent_node || !find_file_within(parent_path, name,
798 child_path, sizeof(child_path), 1)) {
799 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700800 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700801 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700802 return -EACCES;
803 }
Jeff Brown6249b902012-05-26 14:32:54 -0700804 if (unlink(child_path) < 0) {
805 return -errno;
806 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700807 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200808 child_node = lookup_child_by_name_locked(parent_node, name);
809 if (child_node) {
810 child_node->deleted = true;
811 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700812 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700813 if (parent_node && child_node) {
814 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400815 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
816 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400817 << ", child=" << std::hex << child_node->nid << std::dec
818 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700819 if (fuse != fuse->global->fuse_default) {
820 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
821 }
822 if (fuse != fuse->global->fuse_read) {
823 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
824 }
825 if (fuse != fuse->global->fuse_write) {
826 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
827 }
828 }
Jeff Brown6249b902012-05-26 14:32:54 -0700829 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700830}
831
Jeff Brown6249b902012-05-26 14:32:54 -0700832static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700833 const struct fuse_in_header* hdr, const char* name)
834{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200835 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700836 struct node* parent_node;
837 char parent_path[PATH_MAX];
838 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700840 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700841 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
842 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400843 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
844 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700845 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700846
847 if (!parent_node || !find_file_within(parent_path, name,
848 child_path, sizeof(child_path), 1)) {
849 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700850 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700851 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700852 return -EACCES;
853 }
Jeff Brown6249b902012-05-26 14:32:54 -0700854 if (rmdir(child_path) < 0) {
855 return -errno;
856 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700857 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200858 child_node = lookup_child_by_name_locked(parent_node, name);
859 if (child_node) {
860 child_node->deleted = true;
861 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700862 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700863 if (parent_node && child_node) {
864 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400865 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
866 << " parent=" << std::hex << parent_node->nid
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400867 << ", child=" << std::hex << child_node->nid << std::dec
868 << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700869 if (fuse != fuse->global->fuse_default) {
870 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
871 }
872 if (fuse != fuse->global->fuse_read) {
873 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
874 }
875 if (fuse != fuse->global->fuse_write) {
876 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
877 }
878 }
Jeff Brown6249b902012-05-26 14:32:54 -0700879 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880}
881
Jeff Brown6249b902012-05-26 14:32:54 -0700882static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700884 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885{
Jeff Brown6249b902012-05-26 14:32:54 -0700886 struct node* old_parent_node;
887 struct node* new_parent_node;
888 struct node* child_node;
889 char old_parent_path[PATH_MAX];
890 char new_parent_path[PATH_MAX];
891 char old_child_path[PATH_MAX];
892 char new_child_path[PATH_MAX];
893 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400894 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700895 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700896
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700897 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700898 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
899 old_parent_path, sizeof(old_parent_path));
900 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
901 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400902 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name
903 << " @ " << std::hex << hdr->nodeid
904 << " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> "
905 << std::hex << req->newdir
906 << " (" << (new_parent_node ? new_parent_node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700907 if (!old_parent_node || !new_parent_node) {
908 res = -ENOENT;
909 goto lookup_error;
910 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700911 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700912 res = -EACCES;
913 goto lookup_error;
914 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700915 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700916 res = -EACCES;
917 goto lookup_error;
918 }
Jeff Brown6249b902012-05-26 14:32:54 -0700919 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
920 if (!child_node || get_node_path_locked(child_node,
921 old_child_path, sizeof(old_child_path)) < 0) {
922 res = -ENOENT;
923 goto lookup_error;
924 }
925 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700926 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700927
928 /* Special case for renaming a file where destination is same path
929 * differing only by case. In this case we don't want to look for a case
930 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
931 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400932 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700933 || strcasecmp(old_name, new_name);
934 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
935 new_child_path, sizeof(new_child_path), search))) {
936 res = -ENOENT;
937 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700938 }
939
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400940 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_child_path << "->" << new_child_path;
Jeff Brown6249b902012-05-26 14:32:54 -0700941 res = rename(old_child_path, new_child_path);
942 if (res < 0) {
943 res = -errno;
944 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945 }
946
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700947 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700948 res = rename_node_locked(child_node, new_name, new_actual_name);
949 if (!res) {
950 remove_node_from_parent_locked(child_node);
Jeff Sharkey22b91262015-12-14 11:02:01 -0700951 derive_permissions_locked(fuse, new_parent_node, child_node);
952 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700953 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700954 }
Jeff Brown6249b902012-05-26 14:32:54 -0700955 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700956
Jeff Brown6249b902012-05-26 14:32:54 -0700957io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700958 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700959done:
960 release_node_locked(child_node);
961lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700962 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700963 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700964}
965
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700966static int open_flags_to_access_mode(int open_flags) {
967 if ((open_flags & O_ACCMODE) == O_RDONLY) {
968 return R_OK;
969 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
970 return W_OK;
971 } else {
972 /* Probably O_RDRW, but treat as default to be safe */
973 return R_OK | W_OK;
974 }
975}
976
Jeff Brown6249b902012-05-26 14:32:54 -0700977static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700978 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
979{
Jeff Brown6249b902012-05-26 14:32:54 -0700980 struct node* node;
981 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700982 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700983 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700985 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700986 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400987 DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -0400988 << " @ " << std::hex << hdr->nodeid << std::dec
989 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700990 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700991
992 if (!node) {
993 return -ENOENT;
994 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700995 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700996 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700997 return -EACCES;
998 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400999 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001000 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001001 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001002 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001003 DLOG(INFO) << "[" << handler->token << "] OPEN " << path;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001004 h->fd = open(path, req->flags);
1005 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001006 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001007 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001008 }
1009 out.fh = ptr_to_id(h);
1010 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001011
1012#ifdef FUSE_STACKED_IO
1013 out.lower_fd = h->fd;
1014#else
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001015 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001016#endif
1017
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 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;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001181
1182#ifdef FUSE_STACKED_IO
1183 out.lower_fd = -1;
1184#else
Ken Sumrall3a876882013-08-14 20:02:13 -07001185 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001186#endif
1187
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001188 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001189 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190}
1191
Jeff Brown6249b902012-05-26 14:32:54 -07001192static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1194{
1195 char buffer[8192];
1196 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1197 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001198 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001199
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001200 DLOG(INFO) << "[" << handler->token << "] READDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001201 if (req->offset == 0) {
1202 /* rewinddir() might have been called above us, so rewind here too */
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001203 DLOG(INFO) << "[" << handler->token << "] calling rewinddir()";
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001204 rewinddir(h->d);
1205 }
1206 de = readdir(h->d);
1207 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001208 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209 }
1210 fde->ino = FUSE_UNKNOWN_INO;
1211 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1212 fde->off = req->offset + 1;
1213 fde->type = de->d_type;
1214 fde->namelen = strlen(de->d_name);
1215 memcpy(fde->name, de->d_name, fde->namelen + 1);
1216 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001217 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1218 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001219}
1220
Jeff Brown6249b902012-05-26 14:32:54 -07001221static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1223{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001224 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001225
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001226 DLOG(INFO) << "[" << handler->token << "] RELEASEDIR " << h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 closedir(h->d);
1228 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001229 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001230}
1231
Jeff Brown6249b902012-05-26 14:32:54 -07001232static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001233 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1234{
1235 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001236 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001238 DLOG(INFO) << "[" << handler->token << "] INIT ver=" << req->major << "." << req->minor
1239 << " maxread=" << req->max_readahead << " flags=" << std::hex << req->flags;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001240
1241 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1242 * defined (fuse version 7.6). The structure is the same from 7.6 through
1243 * 7.22. Beginning with 7.23, the structure increased in size and added
1244 * new parameters.
1245 */
1246 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001247 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version "
1248 << req->major << "." << req->minor
1249 << ", Expected at least " << FUSE_KERNEL_VERSION << ".6";
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001250 return -1;
1251 }
1252
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001253 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1254 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001255 fuse_struct_size = sizeof(out);
1256#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1257 /* FUSE_KERNEL_VERSION >= 23. */
1258
1259 /* If the kernel only works on minor revs older than or equal to 22,
1260 * then use the older structure size since this code only uses the 7.22
1261 * version of the structure. */
1262 if (req->minor <= 22) {
1263 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1264 }
1265#endif
1266
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001267 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001268 out.max_readahead = req->max_readahead;
1269 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001270
1271#ifdef FUSE_STACKED_IO
1272 out.flags |= FUSE_STACKED_IO;
1273#endif
1274
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275 out.max_background = 32;
1276 out.congestion_threshold = 32;
1277 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001278 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001279 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001280}
1281
Jeff Brown6249b902012-05-26 14:32:54 -07001282static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1284{
Brian Swetland03ee9472010-08-12 18:01:08 -07001285 switch (hdr->opcode) {
1286 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001287 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001288 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001289 }
Jeff Brown84715842012-05-25 14:07:47 -07001290
Brian Swetland03ee9472010-08-12 18:01:08 -07001291 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001292 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001293 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001294 }
Jeff Brown84715842012-05-25 14:07:47 -07001295
Brian Swetland03ee9472010-08-12 18:01:08 -07001296 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001297 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001298 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001299 }
Jeff Brown84715842012-05-25 14:07:47 -07001300
Brian Swetland03ee9472010-08-12 18:01:08 -07001301 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001302 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001303 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001304 }
Jeff Brown84715842012-05-25 14:07:47 -07001305
Brian Swetland03ee9472010-08-12 18:01:08 -07001306// case FUSE_READLINK:
1307// case FUSE_SYMLINK:
1308 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001309 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001310 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001311 return handle_mknod(fuse, handler, hdr, req, name);
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_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001315 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001316 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001317 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001318 }
Jeff Brown84715842012-05-25 14:07:47 -07001319
Brian Swetland03ee9472010-08-12 18:01:08 -07001320 case FUSE_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001321 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001322 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001323 }
Jeff Brown84715842012-05-25 14:07:47 -07001324
Brian Swetland03ee9472010-08-12 18:01:08 -07001325 case FUSE_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001326 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001327 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001328 }
Jeff Brown84715842012-05-25 14:07:47 -07001329
Brian Swetland03ee9472010-08-12 18:01:08 -07001330 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001331 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001332 const char *old_name = ((const char*) data) + sizeof(*req);
1333 const char *new_name = old_name + strlen(old_name) + 1;
1334 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001335 }
Jeff Brown84715842012-05-25 14:07:47 -07001336
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001338 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001339 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001340 return handle_open(fuse, handler, hdr, req);
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_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001344 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001345 return handle_read(fuse, handler, hdr, req);
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_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001349 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001350 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001351 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001352 }
Jeff Brown84715842012-05-25 14:07:47 -07001353
Mike Lockwood4553b082010-08-16 14:14:44 -04001354 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001355 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001356 }
Jeff Brown84715842012-05-25 14:07:47 -07001357
Brian Swetland03ee9472010-08-12 18:01:08 -07001358 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001359 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001360 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001361 }
Jeff Brown84715842012-05-25 14:07:47 -07001362
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001363 case FUSE_FSYNC:
1364 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001365 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001366 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001367 }
1368
Brian Swetland03ee9472010-08-12 18:01:08 -07001369// case FUSE_SETXATTR:
1370// case FUSE_GETXATTR:
1371// case FUSE_LISTXATTR:
1372// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001373 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001374 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001375 }
1376
Brian Swetland03ee9472010-08-12 18:01:08 -07001377 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001378 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001379 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001380 }
Jeff Brown84715842012-05-25 14:07:47 -07001381
Brian Swetland03ee9472010-08-12 18:01:08 -07001382 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001383 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001384 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001385 }
Jeff Brown84715842012-05-25 14:07:47 -07001386
Brian Swetland03ee9472010-08-12 18:01:08 -07001387 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001388 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001389 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001390 }
Jeff Brown84715842012-05-25 14:07:47 -07001391
Brian Swetland03ee9472010-08-12 18:01:08 -07001392 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001393 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001394 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001395 }
Jeff Brown84715842012-05-25 14:07:47 -07001396
Brian Swetland03ee9472010-08-12 18:01:08 -07001397 default: {
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001398 DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001399 << "uniq=" << std::hex << hdr->unique << "nid=" << hdr->nodeid << std::dec;
Jeff Brown6249b902012-05-26 14:32:54 -07001400 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001401 }
Jeff Brown84715842012-05-25 14:07:47 -07001402 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001403}
1404
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001405void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001406{
Jeff Brown6249b902012-05-26 14:32:54 -07001407 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001408 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001409 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1410 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001411 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001412 if (errno == ENODEV) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001413 LOG(ERROR) << "[" << handler->token << "] someone stole our marbles!";
Jeff Sharkey4a485812015-06-30 16:02:40 -07001414 exit(2);
1415 }
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001416 PLOG(ERROR) << "[" << handler->token << "] handle_fuse_requests";
Jeff Brown6249b902012-05-26 14:32:54 -07001417 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001418 }
Jeff Brown84715842012-05-25 14:07:47 -07001419
1420 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001421 LOG(ERROR) << "[" << handler->token << "] request too short: len=" << len;
Jeff Brown6249b902012-05-26 14:32:54 -07001422 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001423 }
1424
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001425 const struct fuse_in_header* hdr =
1426 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001427 if (hdr->len != (size_t)len) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001428 LOG(ERROR) << "[" << handler->token << "] malformed header: len=" << len
1429 << ", hdr->len=" << hdr->len;
Jeff Brown6249b902012-05-26 14:32:54 -07001430 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001431 }
1432
Jeff Brown7729d242012-05-25 15:35:28 -07001433 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001434 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001435 __u64 unique = hdr->unique;
1436 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001437
1438 /* We do not access the request again after this point because the underlying
1439 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001440
1441 if (res != NO_STATUS) {
1442 if (res) {
Jorge Lucangeli Obese157b252016-07-26 15:22:31 -04001443 DLOG(INFO) << "[" << handler->token << "] ERROR " << res;
Jeff Brown6249b902012-05-26 14:32:54 -07001444 }
1445 fuse_status(fuse, unique, res);
1446 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001447 }
1448}