blob: 03ca4dc34b8820a7018c02b35d962b46f611895e [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++;
42 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
43}
44
45static void remove_node_from_parent_locked(struct node* node);
46
47static void release_node_locked(struct node* node)
48{
49 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
50 if (node->refcount > 0) {
51 node->refcount--;
52 if (!node->refcount) {
53 TRACE("DESTROY %p (%s)\n", node, node->name);
54 remove_node_from_parent_locked(node);
55
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040056 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070057 memset(node->name, 0xef, node->namelen);
58 free(node->name);
59 free(node->actual_name);
60 memset(node, 0xfc, sizeof(*node));
61 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080062 }
Jeff Brown6249b902012-05-26 14:32:54 -070063 } else {
64 ERROR("Zero refcnt %p\n", node);
65 }
66}
67
68static void add_node_to_parent_locked(struct node *node, struct node *parent) {
69 node->parent = parent;
70 node->next = parent->child;
71 parent->child = node;
72 acquire_node_locked(parent);
73}
74
75static void remove_node_from_parent_locked(struct node* node)
76{
77 if (node->parent) {
78 if (node->parent->child == node) {
79 node->parent->child = node->parent->child->next;
80 } else {
81 struct node *node2;
82 node2 = node->parent->child;
83 while (node2->next != node)
84 node2 = node2->next;
85 node2->next = node->next;
86 }
87 release_node_locked(node->parent);
88 node->parent = NULL;
89 node->next = NULL;
90 }
91}
92
93/* Gets the absolute path to a node into the provided buffer.
94 *
95 * Populates 'buf' with the path and returns the length of the path on success,
96 * or returns -1 if the path is too long for the provided buffer.
97 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -070098static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
99 const char* name;
100 size_t namelen;
101 if (node->graft_path) {
102 name = node->graft_path;
103 namelen = node->graft_pathlen;
104 } else if (node->actual_name) {
105 name = node->actual_name;
106 namelen = node->namelen;
107 } else {
108 name = node->name;
109 namelen = node->namelen;
110 }
111
Jeff Brown6249b902012-05-26 14:32:54 -0700112 if (bufsize < namelen + 1) {
113 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700114 }
115
Jeff Brown6249b902012-05-26 14:32:54 -0700116 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700117 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700118 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
119 if (pathlen < 0) {
120 return -1;
121 }
122 buf[pathlen++] = '/';
123 }
124
Jeff Brown6249b902012-05-26 14:32:54 -0700125 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
126 return pathlen + namelen;
127}
128
129/* Finds the absolute path of a file within a given directory.
130 * Performs a case-insensitive search for the file and sets the buffer to the path
131 * of the first matching file. If 'search' is zero or if no match is found, sets
132 * the buffer to the path that the file would have, assuming the name were case-sensitive.
133 *
134 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
135 * or returns NULL if the path is too long for the provided buffer.
136 */
137static char* find_file_within(const char* path, const char* name,
138 char* buf, size_t bufsize, int search)
139{
140 size_t pathlen = strlen(path);
141 size_t namelen = strlen(name);
142 size_t childlen = pathlen + namelen + 1;
143 char* actual;
144
145 if (bufsize <= childlen) {
146 return NULL;
147 }
148
149 memcpy(buf, path, pathlen);
150 buf[pathlen] = '/';
151 actual = buf + pathlen + 1;
152 memcpy(actual, name, namelen + 1);
153
154 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800155 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700156 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800157 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700158 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700159 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800160 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800161 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700162 if (!strcasecmp(entry->d_name, name)) {
163 /* we have a match - replace the name, don't need to copy the null again */
164 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800165 break;
166 }
167 }
168 closedir(dir);
169 }
Jeff Brown6249b902012-05-26 14:32:54 -0700170 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800171}
172
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700173static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
174 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000175 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700176 attr->size = s->st_size;
177 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800178 attr->atime = s->st_atim.tv_sec;
179 attr->mtime = s->st_mtim.tv_sec;
180 attr->ctime = s->st_ctim.tv_sec;
181 attr->atimensec = s->st_atim.tv_nsec;
182 attr->mtimensec = s->st_mtim.tv_nsec;
183 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700184 attr->mode = s->st_mode;
185 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700186
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700187 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700188
189 if (fuse->gid == AID_SDCARD_RW) {
190 /* As an optimization, certain trusted system components only run
191 * as owner but operate across all users. Since we're now handing
192 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
193 * the user boundary enforcement for the default view. The UIDs
194 * assigned to app directories are still multiuser aware. */
195 attr->gid = AID_SDCARD_RW;
196 } else {
197 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
198 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700199
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700200 int visible_mode = 0775 & ~fuse->mask;
201 if (node->perm == PERM_PRE_ROOT) {
202 /* Top of multi-user view should always be visible to ensure
203 * secondary users can traverse inside. */
204 visible_mode = 0711;
205 } else if (node->under_android) {
206 /* Block "other" access to Android directories, since only apps
207 * belonging to a specific user should be in there; we still
208 * leave +x open for the default view. */
209 if (fuse->gid == AID_SDCARD_RW) {
210 visible_mode = visible_mode & ~0006;
211 } else {
212 visible_mode = visible_mode & ~0007;
213 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700214 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700215 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700216 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700217 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
218}
219
Jeff Sharkey44d63422013-09-12 09:44:48 -0700220static int touch(char* path, mode_t mode) {
221 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
222 if (fd == -1) {
223 if (errno == EEXIST) {
224 return 0;
225 } else {
226 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
227 return -1;
228 }
229 }
230 close(fd);
231 return 0;
232}
233
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700234static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
235 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700236 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700237
238 /* By default, each node inherits from its parent */
239 node->perm = PERM_INHERIT;
240 node->userid = parent->userid;
241 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700242 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700243
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700244 /* Derive custom permissions based on parent and current node */
245 switch (parent->perm) {
246 case PERM_INHERIT:
247 /* Already inherited above */
248 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700249 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700250 /* Legacy internal layout places users at top level */
251 node->perm = PERM_ROOT;
252 node->userid = strtoul(node->name, NULL, 10);
253 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700254 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700255 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700256 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700257 /* App-specific directories inside; let anyone traverse */
258 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700259 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700260 }
261 break;
262 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700263 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700264 /* App-specific directories inside; let anyone traverse */
265 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700266 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700267 /* App-specific directories inside; let anyone traverse */
268 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700269 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700270 node->graft_path = fuse->global->obb_path;
271 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700272 } else if (!strcasecmp(node->name, "media")) {
273 /* App-specific directories inside; let anyone traverse */
274 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700275 }
276 break;
277 case PERM_ANDROID_DATA:
278 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700279 case PERM_ANDROID_MEDIA:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400280 const auto& iter = fuse->global->package_to_appid->find(node->name);
281 if (iter != fuse->global->package_to_appid->end()) {
282 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700283 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700284 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700285 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700286 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700287}
288
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400289void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkey22b91262015-12-14 11:02:01 -0700290 struct node *node;
291 for (node = parent->child; node; node = node->next) {
292 derive_permissions_locked(fuse, parent, node);
293 if (node->child) {
294 derive_permissions_recursive_locked(fuse, node);
295 }
296 }
297}
298
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700299/* Kernel has already enforced everything we returned through
300 * derive_permissions_locked(), so this is used to lock down access
301 * even further, such as enforcing that apps hold sdcard_rw. */
302static bool check_caller_access_to_name(struct fuse* fuse,
303 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700304 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700305 /* Always block security-sensitive files at root */
306 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700307 if (!strcasecmp(name, "autorun.inf")
308 || !strcasecmp(name, ".android_secure")
309 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700310 return false;
311 }
312 }
313
Jeff Sharkey44d63422013-09-12 09:44:48 -0700314 /* Root always has access; access for any other UIDs should always
315 * be controlled through packages.list. */
316 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700317 return true;
318 }
319
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700320 /* No extra permissions to enforce */
321 return true;
322}
323
324static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700325 const struct fuse_in_header *hdr, const struct node* node, int mode) {
326 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700327}
328
Jeff Brown6249b902012-05-26 14:32:54 -0700329struct node *create_node_locked(struct fuse* fuse,
330 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700331{
332 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700333 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700334
Narayan Kamathfaa09352015-01-13 18:21:10 +0000335 // Detect overflows in the inode counter. "4 billion nodes should be enough
336 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700337 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000338 ERROR("No more inode numbers available");
339 return NULL;
340 }
341
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400342 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700343 if (!node) {
344 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700345 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400346 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700347 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700348 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700349 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700350 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700351 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700352 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400353 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700354 if (!node->actual_name) {
355 free(node->name);
356 free(node);
357 return NULL;
358 }
359 memcpy(node->actual_name, actual_name, namelen + 1);
360 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700361 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700362 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700363 node->ino = fuse->global->inode_ctr++;
364 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700365
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200366 node->deleted = false;
367
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700368 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700369 acquire_node_locked(node);
370 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700371 return node;
372}
373
Jeff Brown6249b902012-05-26 14:32:54 -0700374static int rename_node_locked(struct node *node, const char *name,
375 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700376{
Jeff Brown6249b902012-05-26 14:32:54 -0700377 size_t namelen = strlen(name);
378 int need_actual_name = strcmp(name, actual_name);
379
380 /* make the storage bigger without actually changing the name
381 * in case an error occurs part way */
382 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400383 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700384 if (!new_name) {
385 return -ENOMEM;
386 }
387 node->name = new_name;
388 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400389 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700390 if (!new_actual_name) {
391 return -ENOMEM;
392 }
393 node->actual_name = new_actual_name;
394 }
395 }
396
397 /* update the name, taking care to allocate storage before overwriting the old name */
398 if (need_actual_name) {
399 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400400 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700401 if (!node->actual_name) {
402 return -ENOMEM;
403 }
404 }
405 memcpy(node->actual_name, actual_name, namelen + 1);
406 } else {
407 free(node->actual_name);
408 node->actual_name = NULL;
409 }
410 memcpy(node->name, name, namelen + 1);
411 node->namelen = namelen;
412 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700413}
414
Jeff Brown6249b902012-05-26 14:32:54 -0700415static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700416{
Jeff Brown6249b902012-05-26 14:32:54 -0700417 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700418 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700419 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400420 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700421 }
422}
423
Jeff Brown6249b902012-05-26 14:32:54 -0700424static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
425 char* buf, size_t bufsize)
426{
427 struct node* node = lookup_node_by_id_locked(fuse, nid);
428 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
429 node = NULL;
430 }
431 return node;
432}
433
434static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700435{
436 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700437 /* use exact string comparison, nodes that differ by case
438 * must be considered distinct even if they refer to the same
439 * underlying file as otherwise operations such as "mv x x"
440 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200441 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700442 return node;
443 }
444 }
445 return 0;
446}
447
Jeff Brown6249b902012-05-26 14:32:54 -0700448static struct node* acquire_or_create_child_locked(
449 struct fuse* fuse, struct node* parent,
450 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700451{
Jeff Brown6249b902012-05-26 14:32:54 -0700452 struct node* child = lookup_child_by_name_locked(parent, name);
453 if (child) {
454 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800455 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700456 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800457 }
Jeff Brown6249b902012-05-26 14:32:54 -0700458 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700459}
460
Jeff Brown6249b902012-05-26 14:32:54 -0700461static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700462{
463 struct fuse_out_header hdr;
464 hdr.len = sizeof(hdr);
465 hdr.error = err;
466 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700467 write(fuse->fd, &hdr, sizeof(hdr));
468}
469
Jeff Brown6249b902012-05-26 14:32:54 -0700470static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700471{
472 struct fuse_out_header hdr;
473 struct iovec vec[2];
474 int res;
475
476 hdr.len = len + sizeof(hdr);
477 hdr.error = 0;
478 hdr.unique = unique;
479
480 vec[0].iov_base = &hdr;
481 vec[0].iov_len = sizeof(hdr);
482 vec[1].iov_base = data;
483 vec[1].iov_len = len;
484
485 res = writev(fuse->fd, vec, 2);
486 if (res < 0) {
487 ERROR("*** REPLY FAILED *** %d\n", errno);
488 }
489}
490
Jeff Brown6249b902012-05-26 14:32:54 -0700491static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
492 struct node* parent, const char* name, const char* actual_name,
493 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700494{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700495 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700496 struct fuse_entry_out out;
497 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700498
Jeff Brown6249b902012-05-26 14:32:54 -0700499 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700500 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700501 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700502
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700503 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700504 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
505 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700506 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700507 return -ENOMEM;
508 }
509 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700510 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700511 out.attr_valid = 10;
512 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700513 out.nodeid = node->nid;
514 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700515 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700516 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700517 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700518}
519
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700520static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700521 const char* path)
522{
523 struct fuse_attr_out out;
524 struct stat s;
525
526 if (lstat(path, &s) < 0) {
527 return -errno;
528 }
529 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700530 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700531 out.attr_valid = 10;
532 fuse_reply(fuse, unique, &out, sizeof(out));
533 return NO_STATUS;
534}
535
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700536static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
537 const __u64 child, const char* name) {
538 struct fuse_out_header hdr;
539 struct fuse_notify_delete_out data;
540 struct iovec vec[3];
541 size_t namelen = strlen(name);
542 int res;
543
544 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
545 hdr.error = FUSE_NOTIFY_DELETE;
546 hdr.unique = 0;
547
548 data.parent = parent;
549 data.child = child;
550 data.namelen = namelen;
551 data.padding = 0;
552
553 vec[0].iov_base = &hdr;
554 vec[0].iov_len = sizeof(hdr);
555 vec[1].iov_base = &data;
556 vec[1].iov_len = sizeof(data);
557 vec[2].iov_base = (void*) name;
558 vec[2].iov_len = namelen + 1;
559
560 res = writev(fuse->fd, vec, 3);
561 /* Ignore ENOENT, since other views may not have seen the entry */
562 if (res < 0 && errno != ENOENT) {
563 ERROR("*** NOTIFY FAILED *** %d\n", errno);
564 }
565}
566
Jeff Brown6249b902012-05-26 14:32:54 -0700567static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700568 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700569{
Jeff Brown6249b902012-05-26 14:32:54 -0700570 struct node* parent_node;
571 char parent_path[PATH_MAX];
572 char child_path[PATH_MAX];
573 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700574
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700575 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700576 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
577 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400578 DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid
579 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700580 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700581
582 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
583 child_path, sizeof(child_path), 1))) {
584 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700585 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700586 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700587 return -EACCES;
588 }
589
Jeff Brown6249b902012-05-26 14:32:54 -0700590 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700591}
592
Jeff Brown6249b902012-05-26 14:32:54 -0700593static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700594 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
595{
Jeff Brown6249b902012-05-26 14:32:54 -0700596 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700597
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700598 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700599 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400600 DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup
601 << " @ " << std::hex << hdr->nodeid
602 << " (" << (node ? node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700603 if (node) {
604 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400605 while (n) {
606 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700607 release_node_locked(node);
608 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700609 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700610 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700611 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700612}
613
Jeff Brown6249b902012-05-26 14:32:54 -0700614static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700615 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
616{
Jeff Brown6249b902012-05-26 14:32:54 -0700617 struct node* node;
618 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700619
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700620 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700621 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400622 DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags
623 << " fh=" << std::hex << req->fh << " @ " << std::hex << hdr->nodeid
624 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700625 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700626
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700627 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700628 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700629 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700630 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700631 return -EACCES;
632 }
633
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700634 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700635}
636
Jeff Brown6249b902012-05-26 14:32:54 -0700637static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700638 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
639{
Jeff Brown6249b902012-05-26 14:32:54 -0700640 struct node* node;
641 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700642 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700643
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700644 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700645 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400646 DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh
647 << " valid=" << std::hex << req->valid << " @ " << std::hex << hdr->nodeid
648 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700649 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700650
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700651 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700652 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700653 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800654
655 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700656 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700657 return -EACCES;
658 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700659
Jeff Brown6249b902012-05-26 14:32:54 -0700660 /* XXX: incomplete implementation on purpose.
661 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700662
Elliott Hughes853574d2014-07-31 12:03:03 -0700663 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700664 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700665 }
666
667 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
668 * are both set, then set it to the current time. Else, set it to the
669 * time specified in the request. Same goes for mtime. Use utimensat(2)
670 * as it allows ATIME and MTIME to be changed independently, and has
671 * nanosecond resolution which fuse also has.
672 */
673 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
674 times[0].tv_nsec = UTIME_OMIT;
675 times[1].tv_nsec = UTIME_OMIT;
676 if (req->valid & FATTR_ATIME) {
677 if (req->valid & FATTR_ATIME_NOW) {
678 times[0].tv_nsec = UTIME_NOW;
679 } else {
680 times[0].tv_sec = req->atime;
681 times[0].tv_nsec = req->atimensec;
682 }
683 }
684 if (req->valid & FATTR_MTIME) {
685 if (req->valid & FATTR_MTIME_NOW) {
686 times[1].tv_nsec = UTIME_NOW;
687 } else {
688 times[1].tv_sec = req->mtime;
689 times[1].tv_nsec = req->mtimensec;
690 }
691 }
Jeff Brown6249b902012-05-26 14:32:54 -0700692 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
693 handler->token, path, times[0].tv_sec, times[1].tv_sec);
694 if (utimensat(-1, path, times, 0) < 0) {
695 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700696 }
697 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700698 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700699}
700
Jeff Brown6249b902012-05-26 14:32:54 -0700701static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700702 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
703{
Jeff Brown6249b902012-05-26 14:32:54 -0700704 struct node* parent_node;
705 char parent_path[PATH_MAX];
706 char child_path[PATH_MAX];
707 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700708
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700709 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700710 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
711 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400712 DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode
713 << " @ " << std::hex << hdr->nodeid
714 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700715 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700716
717 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
718 child_path, sizeof(child_path), 1))) {
719 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700720 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700721 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700722 return -EACCES;
723 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700724 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700725 if (mknod(child_path, mode, req->rdev) < 0) {
726 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700727 }
Jeff Brown6249b902012-05-26 14:32:54 -0700728 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700729}
730
Jeff Brown6249b902012-05-26 14:32:54 -0700731static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700732 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
733{
Jeff Brown6249b902012-05-26 14:32:54 -0700734 struct node* parent_node;
735 char parent_path[PATH_MAX];
736 char child_path[PATH_MAX];
737 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700738
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700739 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700740 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
741 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400742 DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode
743 << " @ " << std::hex << hdr->nodeid
744 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700745 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700746
747 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
748 child_path, sizeof(child_path), 1))) {
749 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700750 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700751 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700752 return -EACCES;
753 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700754 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700755 if (mkdir(child_path, mode) < 0) {
756 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700757 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700758
759 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
760 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
761 char nomedia[PATH_MAX];
762 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
763 if (touch(nomedia, 0664) != 0) {
764 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
765 return -ENOENT;
766 }
767 }
768 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
769 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700770 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700771 if (touch(nomedia, 0664) != 0) {
772 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
773 return -ENOENT;
774 }
775 }
776
Jeff Brown6249b902012-05-26 14:32:54 -0700777 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700778}
779
Jeff Brown6249b902012-05-26 14:32:54 -0700780static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700781 const struct fuse_in_header* hdr, const char* name)
782{
Jeff Brown6249b902012-05-26 14:32:54 -0700783 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200784 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700785 char parent_path[PATH_MAX];
786 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700787
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700788 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700789 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
790 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400791 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
792 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700793 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700794
795 if (!parent_node || !find_file_within(parent_path, name,
796 child_path, sizeof(child_path), 1)) {
797 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700798 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700799 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700800 return -EACCES;
801 }
Jeff Brown6249b902012-05-26 14:32:54 -0700802 if (unlink(child_path) < 0) {
803 return -errno;
804 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700805 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200806 child_node = lookup_child_by_name_locked(parent_node, name);
807 if (child_node) {
808 child_node->deleted = true;
809 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700810 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700811 if (parent_node && child_node) {
812 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400813 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
814 << " parent=" << std::hex << parent_node->nid
815 << ", child=" << std::hex << child_node->nid << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700816 if (fuse != fuse->global->fuse_default) {
817 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
818 }
819 if (fuse != fuse->global->fuse_read) {
820 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
821 }
822 if (fuse != fuse->global->fuse_write) {
823 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
824 }
825 }
Jeff Brown6249b902012-05-26 14:32:54 -0700826 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700827}
828
Jeff Brown6249b902012-05-26 14:32:54 -0700829static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700830 const struct fuse_in_header* hdr, const char* name)
831{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200832 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700833 struct node* parent_node;
834 char parent_path[PATH_MAX];
835 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700837 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700838 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
839 parent_path, sizeof(parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400840 DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid
841 << " (" << (parent_node ? parent_node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700842 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700843
844 if (!parent_node || !find_file_within(parent_path, name,
845 child_path, sizeof(child_path), 1)) {
846 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700847 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700848 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700849 return -EACCES;
850 }
Jeff Brown6249b902012-05-26 14:32:54 -0700851 if (rmdir(child_path) < 0) {
852 return -errno;
853 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700854 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200855 child_node = lookup_child_by_name_locked(parent_node, name);
856 if (child_node) {
857 child_node->deleted = true;
858 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700859 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700860 if (parent_node && child_node) {
861 /* Tell all other views that node is gone */
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400862 DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete"
863 << " parent=" << std::hex << parent_node->nid
864 << ", child=" << std::hex << child_node->nid << ", name=" << name;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700865 if (fuse != fuse->global->fuse_default) {
866 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
867 }
868 if (fuse != fuse->global->fuse_read) {
869 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
870 }
871 if (fuse != fuse->global->fuse_write) {
872 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
873 }
874 }
Jeff Brown6249b902012-05-26 14:32:54 -0700875 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876}
877
Jeff Brown6249b902012-05-26 14:32:54 -0700878static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700880 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881{
Jeff Brown6249b902012-05-26 14:32:54 -0700882 struct node* old_parent_node;
883 struct node* new_parent_node;
884 struct node* child_node;
885 char old_parent_path[PATH_MAX];
886 char new_parent_path[PATH_MAX];
887 char old_child_path[PATH_MAX];
888 char new_child_path[PATH_MAX];
889 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400890 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700891 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700892
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700893 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700894 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
895 old_parent_path, sizeof(old_parent_path));
896 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
897 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400898 DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name
899 << " @ " << std::hex << hdr->nodeid
900 << " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> "
901 << std::hex << req->newdir
902 << " (" << (new_parent_node ? new_parent_node->name : "?") << ")";
Jeff Brown6249b902012-05-26 14:32:54 -0700903 if (!old_parent_node || !new_parent_node) {
904 res = -ENOENT;
905 goto lookup_error;
906 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700907 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700908 res = -EACCES;
909 goto lookup_error;
910 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700911 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700912 res = -EACCES;
913 goto lookup_error;
914 }
Jeff Brown6249b902012-05-26 14:32:54 -0700915 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
916 if (!child_node || get_node_path_locked(child_node,
917 old_child_path, sizeof(old_child_path)) < 0) {
918 res = -ENOENT;
919 goto lookup_error;
920 }
921 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700922 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700923
924 /* Special case for renaming a file where destination is same path
925 * differing only by case. In this case we don't want to look for a case
926 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
927 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400928 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700929 || strcasecmp(old_name, new_name);
930 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
931 new_child_path, sizeof(new_child_path), search))) {
932 res = -ENOENT;
933 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700934 }
935
Jeff Brown6249b902012-05-26 14:32:54 -0700936 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
937 res = rename(old_child_path, new_child_path);
938 if (res < 0) {
939 res = -errno;
940 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941 }
942
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700943 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700944 res = rename_node_locked(child_node, new_name, new_actual_name);
945 if (!res) {
946 remove_node_from_parent_locked(child_node);
Jeff Sharkey22b91262015-12-14 11:02:01 -0700947 derive_permissions_locked(fuse, new_parent_node, child_node);
948 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700949 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700950 }
Jeff Brown6249b902012-05-26 14:32:54 -0700951 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952
Jeff Brown6249b902012-05-26 14:32:54 -0700953io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700954 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700955done:
956 release_node_locked(child_node);
957lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700958 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700959 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700960}
961
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700962static int open_flags_to_access_mode(int open_flags) {
963 if ((open_flags & O_ACCMODE) == O_RDONLY) {
964 return R_OK;
965 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
966 return W_OK;
967 } else {
968 /* Probably O_RDRW, but treat as default to be safe */
969 return R_OK | W_OK;
970 }
971}
972
Jeff Brown6249b902012-05-26 14:32:54 -0700973static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
975{
Jeff Brown6249b902012-05-26 14:32:54 -0700976 struct node* node;
977 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700978 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700981 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700982 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -0400983 DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags
984 << " @ " << std::hex << hdr->nodeid << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700985 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700986
987 if (!node) {
988 return -ENOENT;
989 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700990 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700991 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700992 return -EACCES;
993 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400994 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700995 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700996 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700997 }
Jeff Brown6249b902012-05-26 14:32:54 -0700998 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700999 h->fd = open(path, req->flags);
1000 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001002 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001003 }
1004 out.fh = ptr_to_id(h);
1005 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001006
1007#ifdef FUSE_STACKED_IO
1008 out.lower_fd = h->fd;
1009#else
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001010 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001011#endif
1012
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001013 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001014 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001015}
1016
Jeff Brown6249b902012-05-26 14:32:54 -07001017static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1019{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001020 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001021 __u64 unique = hdr->unique;
1022 __u32 size = req->size;
1023 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001024 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001025 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001026
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001027 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1028 * overlaps the request buffer and will clobber data in the request. This
1029 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001030
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001031 DLOG(INFO) << "[" << handler->token << "] READ " << std::hex << h << "(" << h->fd << ") "
1032 << size << "@" << offset;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001033 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001034 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001035 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001036 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001037 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001038 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001039 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001040 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001041 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042}
1043
Jeff Brown6249b902012-05-26 14:32:54 -07001044static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001045 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1046 const void* buffer)
1047{
1048 struct fuse_write_out out;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001049 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001051 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001052
Arpad Horvath49e93442014-02-18 10:18:25 +01001053 if (req->flags & O_DIRECT) {
1054 memcpy(aligned_buffer, buffer, req->size);
1055 buffer = (const __u8*) aligned_buffer;
1056 }
Jeff Brown6249b902012-05-26 14:32:54 -07001057
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001058 DLOG(INFO) << "[" << handler->token << "] WRITE " << std::hex << h << "(" << h->fd << ") "
1059 << req->size << "@" << req->offset;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001060 res = pwrite64(h->fd, buffer, req->size, req->offset);
1061 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001062 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001063 }
1064 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001065 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001066 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001067 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068}
1069
Jeff Brown6249b902012-05-26 14:32:54 -07001070static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001071 const struct fuse_in_header* hdr)
1072{
Jeff Brown6249b902012-05-26 14:32:54 -07001073 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001074 struct statfs stat;
1075 struct fuse_statfs_out out;
1076 int res;
1077
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001078 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001079 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001080 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001081 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001082 if (res < 0) {
1083 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001084 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001085 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001086 return -errno;
1087 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001088 memset(&out, 0, sizeof(out));
1089 out.st.blocks = stat.f_blocks;
1090 out.st.bfree = stat.f_bfree;
1091 out.st.bavail = stat.f_bavail;
1092 out.st.files = stat.f_files;
1093 out.st.ffree = stat.f_ffree;
1094 out.st.bsize = stat.f_bsize;
1095 out.st.namelen = stat.f_namelen;
1096 out.st.frsize = stat.f_frsize;
1097 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001098 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001099}
1100
Jeff Brown6249b902012-05-26 14:32:54 -07001101static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001102 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1103{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001104 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001105
1106 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107 close(h->fd);
1108 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001109 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001110}
1111
Jeff Brown6249b902012-05-26 14:32:54 -07001112static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001113 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1114{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001115 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1116 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001117
Elliott Hughesf6d67372014-07-08 14:38:26 -07001118 int fd = -1;
1119 if (is_dir) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001120 struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001121 fd = dirfd(dh->d);
1122 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001123 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001124 fd = h->fd;
1125 }
1126
1127 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1128 is_dir ? "FSYNCDIR" : "FSYNC",
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001129 static_cast<struct node*>(id_to_ptr(req->fh)), fd, is_data_sync);
Elliott Hughesf6d67372014-07-08 14:38:26 -07001130 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1131 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001132 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001133 }
Jeff Brown6249b902012-05-26 14:32:54 -07001134 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001135}
1136
Jeff Brown6249b902012-05-26 14:32:54 -07001137static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001138 const struct fuse_in_header* hdr)
1139{
Jeff Brown6249b902012-05-26 14:32:54 -07001140 TRACE("[%d] FLUSH\n", handler->token);
1141 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142}
1143
Jeff Brown6249b902012-05-26 14:32:54 -07001144static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001145 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1146{
Jeff Brown6249b902012-05-26 14:32:54 -07001147 struct node* node;
1148 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001149 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001151
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001152 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001153 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001154 DLOG(INFO) << "[" << handler->token << "] OPENDIR @ " << std::hex << hdr->nodeid
1155 << " (" << (node ? node->name : "?") << ")";
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001156 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001157
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001159 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001161 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001162 return -EACCES;
1163 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001164 h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001166 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001167 }
Jeff Brown6249b902012-05-26 14:32:54 -07001168 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001169 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001170 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001171 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001172 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001173 }
1174 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001175 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001176
1177#ifdef FUSE_STACKED_IO
1178 out.lower_fd = -1;
1179#else
Ken Sumrall3a876882013-08-14 20:02:13 -07001180 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001181#endif
1182
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001184 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001185}
1186
Jeff Brown6249b902012-05-26 14:32:54 -07001187static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001188 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1189{
1190 char buffer[8192];
1191 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1192 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001193 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001194
1195 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001196 if (req->offset == 0) {
1197 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001198 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199 rewinddir(h->d);
1200 }
1201 de = readdir(h->d);
1202 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001203 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001204 }
1205 fde->ino = FUSE_UNKNOWN_INO;
1206 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1207 fde->off = req->offset + 1;
1208 fde->type = de->d_type;
1209 fde->namelen = strlen(de->d_name);
1210 memcpy(fde->name, de->d_name, fde->namelen + 1);
1211 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001212 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1213 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214}
1215
Jeff Brown6249b902012-05-26 14:32:54 -07001216static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1218{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001219 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001220
1221 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 closedir(h->d);
1223 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001224 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225}
1226
Jeff Brown6249b902012-05-26 14:32:54 -07001227static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001228 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1229{
1230 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001231 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001232
Jeff Brown6249b902012-05-26 14:32:54 -07001233 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1234 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001235
1236 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1237 * defined (fuse version 7.6). The structure is the same from 7.6 through
1238 * 7.22. Beginning with 7.23, the structure increased in size and added
1239 * new parameters.
1240 */
1241 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1242 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1243 req->major, req->minor, FUSE_KERNEL_VERSION);
1244 return -1;
1245 }
1246
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001247 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1248 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001249 fuse_struct_size = sizeof(out);
1250#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1251 /* FUSE_KERNEL_VERSION >= 23. */
1252
1253 /* If the kernel only works on minor revs older than or equal to 22,
1254 * then use the older structure size since this code only uses the 7.22
1255 * version of the structure. */
1256 if (req->minor <= 22) {
1257 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1258 }
1259#endif
1260
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001261 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262 out.max_readahead = req->max_readahead;
1263 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001264
1265#ifdef FUSE_STACKED_IO
1266 out.flags |= FUSE_STACKED_IO;
1267#endif
1268
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 out.max_background = 32;
1270 out.congestion_threshold = 32;
1271 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001272 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001273 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274}
1275
Jeff Brown6249b902012-05-26 14:32:54 -07001276static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001277 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1278{
Brian Swetland03ee9472010-08-12 18:01:08 -07001279 switch (hdr->opcode) {
1280 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001281 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001282 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001283 }
Jeff Brown84715842012-05-25 14:07:47 -07001284
Brian Swetland03ee9472010-08-12 18:01:08 -07001285 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001286 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001287 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001288 }
Jeff Brown84715842012-05-25 14:07:47 -07001289
Brian Swetland03ee9472010-08-12 18:01:08 -07001290 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001291 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001292 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001293 }
Jeff Brown84715842012-05-25 14:07:47 -07001294
Brian Swetland03ee9472010-08-12 18:01:08 -07001295 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001296 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001297 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001298 }
Jeff Brown84715842012-05-25 14:07:47 -07001299
Brian Swetland03ee9472010-08-12 18:01:08 -07001300// case FUSE_READLINK:
1301// case FUSE_SYMLINK:
1302 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001303 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001304 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001305 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001306 }
Jeff Brown84715842012-05-25 14:07:47 -07001307
Brian Swetland03ee9472010-08-12 18:01:08 -07001308 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001309 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_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_mkdir(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_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001315 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001316 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001317 }
Jeff Brown84715842012-05-25 14:07:47 -07001318
Brian Swetland03ee9472010-08-12 18:01:08 -07001319 case FUSE_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001320 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001321 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001322 }
Jeff Brown84715842012-05-25 14:07:47 -07001323
Brian Swetland03ee9472010-08-12 18:01:08 -07001324 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001325 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001326 const char *old_name = ((const char*) data) + sizeof(*req);
1327 const char *new_name = old_name + strlen(old_name) + 1;
1328 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001329 }
Jeff Brown84715842012-05-25 14:07:47 -07001330
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001331// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001332 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001333 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001334 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001335 }
Jeff Brown84715842012-05-25 14:07:47 -07001336
Brian Swetland03ee9472010-08-12 18:01:08 -07001337 case FUSE_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001338 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001339 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001340 }
Jeff Brown84715842012-05-25 14:07:47 -07001341
Brian Swetland03ee9472010-08-12 18:01:08 -07001342 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001343 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001344 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001345 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001346 }
Jeff Brown84715842012-05-25 14:07:47 -07001347
Mike Lockwood4553b082010-08-16 14:14:44 -04001348 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001349 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001350 }
Jeff Brown84715842012-05-25 14:07:47 -07001351
Brian Swetland03ee9472010-08-12 18:01:08 -07001352 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001353 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001354 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001355 }
Jeff Brown84715842012-05-25 14:07:47 -07001356
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001357 case FUSE_FSYNC:
1358 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001359 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001360 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001361 }
1362
Brian Swetland03ee9472010-08-12 18:01:08 -07001363// case FUSE_SETXATTR:
1364// case FUSE_GETXATTR:
1365// case FUSE_LISTXATTR:
1366// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001367 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001368 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001369 }
1370
Brian Swetland03ee9472010-08-12 18:01:08 -07001371 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001372 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001373 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001374 }
Jeff Brown84715842012-05-25 14:07:47 -07001375
Brian Swetland03ee9472010-08-12 18:01:08 -07001376 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001377 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001378 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001379 }
Jeff Brown84715842012-05-25 14:07:47 -07001380
Brian Swetland03ee9472010-08-12 18:01:08 -07001381 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001382 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001383 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001384 }
Jeff Brown84715842012-05-25 14:07:47 -07001385
Brian Swetland03ee9472010-08-12 18:01:08 -07001386 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001387 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001388 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001389 }
Jeff Brown84715842012-05-25 14:07:47 -07001390
Brian Swetland03ee9472010-08-12 18:01:08 -07001391 default: {
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -04001392 DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode
1393 << "uniq=" << std::hex << hdr->unique << "nid=" << std::hex << hdr->nodeid;
Jeff Brown6249b902012-05-26 14:32:54 -07001394 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001395 }
Jeff Brown84715842012-05-25 14:07:47 -07001396 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001397}
1398
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001399void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001400{
Jeff Brown6249b902012-05-26 14:32:54 -07001401 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001402 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001403 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1404 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001405 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001406 if (errno == ENODEV) {
1407 ERROR("[%d] someone stole our marbles!\n", handler->token);
1408 exit(2);
1409 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001410 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001411 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001412 }
Jeff Brown84715842012-05-25 14:07:47 -07001413
1414 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001415 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1416 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001417 }
1418
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001419 const struct fuse_in_header* hdr =
1420 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001421 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001422 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1423 handler->token, (size_t)len, hdr->len);
1424 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001425 }
1426
Jeff Brown7729d242012-05-25 15:35:28 -07001427 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001428 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001429 __u64 unique = hdr->unique;
1430 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001431
1432 /* We do not access the request again after this point because the underlying
1433 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001434
1435 if (res != NO_STATUS) {
1436 if (res) {
1437 TRACE("[%d] ERROR %d\n", handler->token, res);
1438 }
1439 fuse_status(fuse, unique, res);
1440 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001441 }
1442}