blob: f371901614c162f9af94d74468e4af291d9e6d3f [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040019#include "fuse.h"
Brian Swetlandb14a2c62010-08-12 18:21:12 -070020
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -070021/* FUSE_CANONICAL_PATH is not currently upstreamed */
22#define FUSE_CANONICAL_PATH 2016
23
Jeff Sharkey20ca9832016-04-07 11:05:21 -060024#define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
25#define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
26
Brian Swetland03ee9472010-08-12 18:01:08 -070027#define FUSE_UNKNOWN_INO 0xffffffff
28
Jeff Brown6249b902012-05-26 14:32:54 -070029/* Pseudo-error constant used to indicate that no fuse status is needed
30 * or that a reply has already been written. */
31#define NO_STATUS 1
32
Jeff Brown6249b902012-05-26 14:32:54 -070033static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -070034{
Jeff Brown6249b902012-05-26 14:32:54 -070035 return (void *) (uintptr_t) nid;
36}
Brian Swetland03ee9472010-08-12 18:01:08 -070037
Jeff Brown6249b902012-05-26 14:32:54 -070038static inline __u64 ptr_to_id(void *ptr)
39{
40 return (__u64) (uintptr_t) ptr;
41}
Brian Swetland03ee9472010-08-12 18:01:08 -070042
Jeff Brown6249b902012-05-26 14:32:54 -070043static void acquire_node_locked(struct node* node)
44{
45 node->refcount++;
46 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
47}
48
49static void remove_node_from_parent_locked(struct node* node);
50
51static void release_node_locked(struct node* node)
52{
53 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
54 if (node->refcount > 0) {
55 node->refcount--;
56 if (!node->refcount) {
57 TRACE("DESTROY %p (%s)\n", node, node->name);
58 remove_node_from_parent_locked(node);
59
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040060 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070061 memset(node->name, 0xef, node->namelen);
62 free(node->name);
63 free(node->actual_name);
64 memset(node, 0xfc, sizeof(*node));
65 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080066 }
Jeff Brown6249b902012-05-26 14:32:54 -070067 } else {
68 ERROR("Zero refcnt %p\n", node);
69 }
70}
71
72static void add_node_to_parent_locked(struct node *node, struct node *parent) {
73 node->parent = parent;
74 node->next = parent->child;
75 parent->child = node;
76 acquire_node_locked(parent);
77}
78
79static void remove_node_from_parent_locked(struct node* node)
80{
81 if (node->parent) {
82 if (node->parent->child == node) {
83 node->parent->child = node->parent->child->next;
84 } else {
85 struct node *node2;
86 node2 = node->parent->child;
87 while (node2->next != node)
88 node2 = node2->next;
89 node2->next = node->next;
90 }
91 release_node_locked(node->parent);
92 node->parent = NULL;
93 node->next = NULL;
94 }
95}
96
97/* Gets the absolute path to a node into the provided buffer.
98 *
99 * Populates 'buf' with the path and returns the length of the path on success,
100 * or returns -1 if the path is too long for the provided buffer.
101 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700102static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
103 const char* name;
104 size_t namelen;
105 if (node->graft_path) {
106 name = node->graft_path;
107 namelen = node->graft_pathlen;
108 } else if (node->actual_name) {
109 name = node->actual_name;
110 namelen = node->namelen;
111 } else {
112 name = node->name;
113 namelen = node->namelen;
114 }
115
Jeff Brown6249b902012-05-26 14:32:54 -0700116 if (bufsize < namelen + 1) {
117 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700118 }
119
Jeff Brown6249b902012-05-26 14:32:54 -0700120 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700121 if (node->parent && node->graft_path == NULL) {
Daniel Rosenbergdb4638e2016-04-12 16:30:28 -0700122 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700123 if (pathlen < 0) {
124 return -1;
125 }
126 buf[pathlen++] = '/';
127 }
128
Jeff Brown6249b902012-05-26 14:32:54 -0700129 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
130 return pathlen + namelen;
131}
132
133/* Finds the absolute path of a file within a given directory.
134 * Performs a case-insensitive search for the file and sets the buffer to the path
135 * of the first matching file. If 'search' is zero or if no match is found, sets
136 * the buffer to the path that the file would have, assuming the name were case-sensitive.
137 *
138 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
139 * or returns NULL if the path is too long for the provided buffer.
140 */
141static char* find_file_within(const char* path, const char* name,
142 char* buf, size_t bufsize, int search)
143{
144 size_t pathlen = strlen(path);
145 size_t namelen = strlen(name);
146 size_t childlen = pathlen + namelen + 1;
147 char* actual;
148
149 if (bufsize <= childlen) {
150 return NULL;
151 }
152
153 memcpy(buf, path, pathlen);
154 buf[pathlen] = '/';
155 actual = buf + pathlen + 1;
156 memcpy(actual, name, namelen + 1);
157
158 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800159 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700160 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800161 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700162 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700163 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800164 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800165 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700166 if (!strcasecmp(entry->d_name, name)) {
167 /* we have a match - replace the name, don't need to copy the null again */
168 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800169 break;
170 }
171 }
172 closedir(dir);
173 }
Jeff Brown6249b902012-05-26 14:32:54 -0700174 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800175}
176
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700177static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
178 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000179 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700180 attr->size = s->st_size;
181 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800182 attr->atime = s->st_atim.tv_sec;
183 attr->mtime = s->st_mtim.tv_sec;
184 attr->ctime = s->st_ctim.tv_sec;
185 attr->atimensec = s->st_atim.tv_nsec;
186 attr->mtimensec = s->st_mtim.tv_nsec;
187 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700188 attr->mode = s->st_mode;
189 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700190
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700191 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700192
193 if (fuse->gid == AID_SDCARD_RW) {
194 /* As an optimization, certain trusted system components only run
195 * as owner but operate across all users. Since we're now handing
196 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
197 * the user boundary enforcement for the default view. The UIDs
198 * assigned to app directories are still multiuser aware. */
199 attr->gid = AID_SDCARD_RW;
200 } else {
201 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
202 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700203
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700204 int visible_mode = 0775 & ~fuse->mask;
205 if (node->perm == PERM_PRE_ROOT) {
206 /* Top of multi-user view should always be visible to ensure
207 * secondary users can traverse inside. */
208 visible_mode = 0711;
209 } else if (node->under_android) {
210 /* Block "other" access to Android directories, since only apps
211 * belonging to a specific user should be in there; we still
212 * leave +x open for the default view. */
213 if (fuse->gid == AID_SDCARD_RW) {
214 visible_mode = visible_mode & ~0006;
215 } else {
216 visible_mode = visible_mode & ~0007;
217 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700218 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700219 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700220 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700221 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
222}
223
Jeff Sharkey44d63422013-09-12 09:44:48 -0700224static int touch(char* path, mode_t mode) {
225 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
226 if (fd == -1) {
227 if (errno == EEXIST) {
228 return 0;
229 } else {
230 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
231 return -1;
232 }
233 }
234 close(fd);
235 return 0;
236}
237
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700238static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
239 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700240 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700241
242 /* By default, each node inherits from its parent */
243 node->perm = PERM_INHERIT;
244 node->userid = parent->userid;
245 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700246 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700247
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700248 /* Derive custom permissions based on parent and current node */
249 switch (parent->perm) {
250 case PERM_INHERIT:
251 /* Already inherited above */
252 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700253 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700254 /* Legacy internal layout places users at top level */
255 node->perm = PERM_ROOT;
256 node->userid = strtoul(node->name, NULL, 10);
257 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700258 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700259 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700260 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700261 /* App-specific directories inside; let anyone traverse */
262 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700263 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700264 }
265 break;
266 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700267 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700268 /* App-specific directories inside; let anyone traverse */
269 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700270 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700271 /* App-specific directories inside; let anyone traverse */
272 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700273 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700274 node->graft_path = fuse->global->obb_path;
275 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700276 } else if (!strcasecmp(node->name, "media")) {
277 /* App-specific directories inside; let anyone traverse */
278 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700279 }
280 break;
281 case PERM_ANDROID_DATA:
282 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700283 case PERM_ANDROID_MEDIA:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400284 const auto& iter = fuse->global->package_to_appid->find(node->name);
285 if (iter != fuse->global->package_to_appid->end()) {
286 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700287 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700288 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700289 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700290 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700291}
292
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400293void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700294 struct node *node;
295 for (node = parent->child; node; node = node->next) {
296 derive_permissions_locked(fuse, parent, node);
297 if (node->child) {
298 derive_permissions_recursive_locked(fuse, node);
299 }
300 }
301}
302
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700303/* Kernel has already enforced everything we returned through
304 * derive_permissions_locked(), so this is used to lock down access
305 * even further, such as enforcing that apps hold sdcard_rw. */
306static bool check_caller_access_to_name(struct fuse* fuse,
307 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700308 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700309 /* Always block security-sensitive files at root */
310 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700311 if (!strcasecmp(name, "autorun.inf")
312 || !strcasecmp(name, ".android_secure")
313 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700314 return false;
315 }
316 }
317
Jeff Sharkey44d63422013-09-12 09:44:48 -0700318 /* Root always has access; access for any other UIDs should always
319 * be controlled through packages.list. */
320 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700321 return true;
322 }
323
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700324 /* No extra permissions to enforce */
325 return true;
326}
327
328static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700329 const struct fuse_in_header *hdr, const struct node* node, int mode) {
330 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700331}
332
Jeff Brown6249b902012-05-26 14:32:54 -0700333struct node *create_node_locked(struct fuse* fuse,
334 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700335{
336 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700337 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700338
Narayan Kamathfaa09352015-01-13 18:21:10 +0000339 // Detect overflows in the inode counter. "4 billion nodes should be enough
340 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700341 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000342 ERROR("No more inode numbers available");
343 return NULL;
344 }
345
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400346 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700347 if (!node) {
348 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700349 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400350 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700351 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700352 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700353 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700354 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700355 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700356 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400357 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700358 if (!node->actual_name) {
359 free(node->name);
360 free(node);
361 return NULL;
362 }
363 memcpy(node->actual_name, actual_name, namelen + 1);
364 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700365 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700366 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700367 node->ino = fuse->global->inode_ctr++;
368 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700369
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200370 node->deleted = false;
371
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700372 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700373 acquire_node_locked(node);
374 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700375 return node;
376}
377
Jeff Brown6249b902012-05-26 14:32:54 -0700378static int rename_node_locked(struct node *node, const char *name,
379 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700380{
Jeff Brown6249b902012-05-26 14:32:54 -0700381 size_t namelen = strlen(name);
382 int need_actual_name = strcmp(name, actual_name);
383
384 /* make the storage bigger without actually changing the name
385 * in case an error occurs part way */
386 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400387 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700388 if (!new_name) {
389 return -ENOMEM;
390 }
391 node->name = new_name;
392 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400393 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700394 if (!new_actual_name) {
395 return -ENOMEM;
396 }
397 node->actual_name = new_actual_name;
398 }
399 }
400
401 /* update the name, taking care to allocate storage before overwriting the old name */
402 if (need_actual_name) {
403 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400404 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700405 if (!node->actual_name) {
406 return -ENOMEM;
407 }
408 }
409 memcpy(node->actual_name, actual_name, namelen + 1);
410 } else {
411 free(node->actual_name);
412 node->actual_name = NULL;
413 }
414 memcpy(node->name, name, namelen + 1);
415 node->namelen = namelen;
416 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700417}
418
Jeff Brown6249b902012-05-26 14:32:54 -0700419static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700420{
Jeff Brown6249b902012-05-26 14:32:54 -0700421 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700422 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700423 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400424 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700425 }
426}
427
Jeff Brown6249b902012-05-26 14:32:54 -0700428static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
429 char* buf, size_t bufsize)
430{
431 struct node* node = lookup_node_by_id_locked(fuse, nid);
432 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
433 node = NULL;
434 }
435 return node;
436}
437
438static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700439{
440 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700441 /* use exact string comparison, nodes that differ by case
442 * must be considered distinct even if they refer to the same
443 * underlying file as otherwise operations such as "mv x x"
444 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200445 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700446 return node;
447 }
448 }
449 return 0;
450}
451
Jeff Brown6249b902012-05-26 14:32:54 -0700452static struct node* acquire_or_create_child_locked(
453 struct fuse* fuse, struct node* parent,
454 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700455{
Jeff Brown6249b902012-05-26 14:32:54 -0700456 struct node* child = lookup_child_by_name_locked(parent, name);
457 if (child) {
458 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800459 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700460 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800461 }
Jeff Brown6249b902012-05-26 14:32:54 -0700462 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700463}
464
Jeff Brown6249b902012-05-26 14:32:54 -0700465static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700466{
467 struct fuse_out_header hdr;
468 hdr.len = sizeof(hdr);
469 hdr.error = err;
470 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700471 write(fuse->fd, &hdr, sizeof(hdr));
472}
473
Jeff Brown6249b902012-05-26 14:32:54 -0700474static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700475{
476 struct fuse_out_header hdr;
477 struct iovec vec[2];
478 int res;
479
480 hdr.len = len + sizeof(hdr);
481 hdr.error = 0;
482 hdr.unique = unique;
483
484 vec[0].iov_base = &hdr;
485 vec[0].iov_len = sizeof(hdr);
486 vec[1].iov_base = data;
487 vec[1].iov_len = len;
488
489 res = writev(fuse->fd, vec, 2);
490 if (res < 0) {
491 ERROR("*** REPLY FAILED *** %d\n", errno);
492 }
493}
494
Jeff Brown6249b902012-05-26 14:32:54 -0700495static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
496 struct node* parent, const char* name, const char* actual_name,
497 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700498{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700499 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700500 struct fuse_entry_out out;
501 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700502
Jeff Brown6249b902012-05-26 14:32:54 -0700503 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700504 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700505 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700506
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700507 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700508 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
509 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700510 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700511 return -ENOMEM;
512 }
513 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700514 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700515 out.attr_valid = 10;
516 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700517 out.nodeid = node->nid;
518 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700519 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700520 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700521 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700522}
523
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700524static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700525 const char* path)
526{
527 struct fuse_attr_out out;
528 struct stat s;
529
530 if (lstat(path, &s) < 0) {
531 return -errno;
532 }
533 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700534 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700535 out.attr_valid = 10;
536 fuse_reply(fuse, unique, &out, sizeof(out));
537 return NO_STATUS;
538}
539
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700540static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
541 const __u64 child, const char* name) {
542 struct fuse_out_header hdr;
543 struct fuse_notify_delete_out data;
544 struct iovec vec[3];
545 size_t namelen = strlen(name);
546 int res;
547
548 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
549 hdr.error = FUSE_NOTIFY_DELETE;
550 hdr.unique = 0;
551
552 data.parent = parent;
553 data.child = child;
554 data.namelen = namelen;
555 data.padding = 0;
556
557 vec[0].iov_base = &hdr;
558 vec[0].iov_len = sizeof(hdr);
559 vec[1].iov_base = &data;
560 vec[1].iov_len = sizeof(data);
561 vec[2].iov_base = (void*) name;
562 vec[2].iov_len = namelen + 1;
563
564 res = writev(fuse->fd, vec, 3);
565 /* Ignore ENOENT, since other views may not have seen the entry */
566 if (res < 0 && errno != ENOENT) {
567 ERROR("*** NOTIFY FAILED *** %d\n", errno);
568 }
569}
570
Jeff Brown6249b902012-05-26 14:32:54 -0700571static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700572 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700573{
Jeff Brown6249b902012-05-26 14:32:54 -0700574 struct node* parent_node;
575 char parent_path[PATH_MAX];
576 char child_path[PATH_MAX];
577 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700578
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700579 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700580 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
581 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400582 TRACE("[%d] LOOKUP %s @ %" PRIx64 " (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700583 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700584 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700585
586 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
587 child_path, sizeof(child_path), 1))) {
588 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700589 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700590 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700591 return -EACCES;
592 }
593
Jeff Brown6249b902012-05-26 14:32:54 -0700594 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700595}
596
Jeff Brown6249b902012-05-26 14:32:54 -0700597static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700598 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
599{
Jeff Brown6249b902012-05-26 14:32:54 -0700600 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700601
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700602 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700603 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400604 TRACE("[%d] FORGET #%" PRIu64 " @ %" PRIx64 " (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700605 hdr->nodeid, node ? node->name : "?");
606 if (node) {
607 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400608 while (n) {
609 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700610 release_node_locked(node);
611 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700612 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700613 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700614 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700615}
616
Jeff Brown6249b902012-05-26 14:32:54 -0700617static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700618 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
619{
Jeff Brown6249b902012-05-26 14:32:54 -0700620 struct node* node;
621 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700622
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700623 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700624 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400625 TRACE("[%d] GETATTR flags=%x fh=%" PRIx64 " @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700626 req->getattr_flags, req->fh, hdr->nodeid, 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 Obesf08ba052016-07-13 16:52:24 -0400648 TRACE("[%d] SETATTR fh=%" PRIx64 " valid=%x @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700649 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700650 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700651
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700652 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700653 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700654 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800655
656 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700657 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700658 return -EACCES;
659 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700660
Jeff Brown6249b902012-05-26 14:32:54 -0700661 /* XXX: incomplete implementation on purpose.
662 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700663
Elliott Hughes853574d2014-07-31 12:03:03 -0700664 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700665 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700666 }
667
668 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
669 * are both set, then set it to the current time. Else, set it to the
670 * time specified in the request. Same goes for mtime. Use utimensat(2)
671 * as it allows ATIME and MTIME to be changed independently, and has
672 * nanosecond resolution which fuse also has.
673 */
674 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
675 times[0].tv_nsec = UTIME_OMIT;
676 times[1].tv_nsec = UTIME_OMIT;
677 if (req->valid & FATTR_ATIME) {
678 if (req->valid & FATTR_ATIME_NOW) {
679 times[0].tv_nsec = UTIME_NOW;
680 } else {
681 times[0].tv_sec = req->atime;
682 times[0].tv_nsec = req->atimensec;
683 }
684 }
685 if (req->valid & FATTR_MTIME) {
686 if (req->valid & FATTR_MTIME_NOW) {
687 times[1].tv_nsec = UTIME_NOW;
688 } else {
689 times[1].tv_sec = req->mtime;
690 times[1].tv_nsec = req->mtimensec;
691 }
692 }
Jeff Brown6249b902012-05-26 14:32:54 -0700693 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
694 handler->token, path, times[0].tv_sec, times[1].tv_sec);
695 if (utimensat(-1, path, times, 0) < 0) {
696 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700697 }
698 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700699 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700700}
701
Jeff Brown6249b902012-05-26 14:32:54 -0700702static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700703 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
704{
Jeff Brown6249b902012-05-26 14:32:54 -0700705 struct node* parent_node;
706 char parent_path[PATH_MAX];
707 char child_path[PATH_MAX];
708 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700709
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700710 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700711 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
712 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400713 TRACE("[%d] MKNOD %s 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700714 name, req->mode, hdr->nodeid, 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 Obesf08ba052016-07-13 16:52:24 -0400742 TRACE("[%d] MKDIR %s 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700743 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700744 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700745
746 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
747 child_path, sizeof(child_path), 1))) {
748 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700749 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700750 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700751 return -EACCES;
752 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700753 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700754 if (mkdir(child_path, mode) < 0) {
755 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700756 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700757
758 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
759 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
760 char nomedia[PATH_MAX];
761 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
762 if (touch(nomedia, 0664) != 0) {
763 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
764 return -ENOENT;
765 }
766 }
767 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
768 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700769 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700770 if (touch(nomedia, 0664) != 0) {
771 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
772 return -ENOENT;
773 }
774 }
775
Jeff Brown6249b902012-05-26 14:32:54 -0700776 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700777}
778
Jeff Brown6249b902012-05-26 14:32:54 -0700779static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700780 const struct fuse_in_header* hdr, const char* name)
781{
Jeff Brown6249b902012-05-26 14:32:54 -0700782 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200783 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700784 char parent_path[PATH_MAX];
785 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700786
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700787 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700788 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
789 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400790 TRACE("[%d] UNLINK %s @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700791 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700792 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700793
794 if (!parent_node || !find_file_within(parent_path, name,
795 child_path, sizeof(child_path), 1)) {
796 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700797 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700798 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700799 return -EACCES;
800 }
Jeff Brown6249b902012-05-26 14:32:54 -0700801 if (unlink(child_path) < 0) {
802 return -errno;
803 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700804 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200805 child_node = lookup_child_by_name_locked(parent_node, name);
806 if (child_node) {
807 child_node->deleted = true;
808 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700809 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700810 if (parent_node && child_node) {
811 /* Tell all other views that node is gone */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400812 TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n",
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700813 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
814 if (fuse != fuse->global->fuse_default) {
815 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
816 }
817 if (fuse != fuse->global->fuse_read) {
818 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
819 }
820 if (fuse != fuse->global->fuse_write) {
821 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
822 }
823 }
Jeff Brown6249b902012-05-26 14:32:54 -0700824 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700825}
826
Jeff Brown6249b902012-05-26 14:32:54 -0700827static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700828 const struct fuse_in_header* hdr, const char* name)
829{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200830 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700831 struct node* parent_node;
832 char parent_path[PATH_MAX];
833 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700834
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700835 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700836 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
837 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400838 TRACE("[%d] RMDIR %s @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700839 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700840 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700841
842 if (!parent_node || !find_file_within(parent_path, name,
843 child_path, sizeof(child_path), 1)) {
844 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700845 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700846 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700847 return -EACCES;
848 }
Jeff Brown6249b902012-05-26 14:32:54 -0700849 if (rmdir(child_path) < 0) {
850 return -errno;
851 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700852 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200853 child_node = lookup_child_by_name_locked(parent_node, name);
854 if (child_node) {
855 child_node->deleted = true;
856 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700857 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700858 if (parent_node && child_node) {
859 /* Tell all other views that node is gone */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400860 TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n",
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700861 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
862 if (fuse != fuse->global->fuse_default) {
863 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
864 }
865 if (fuse != fuse->global->fuse_read) {
866 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
867 }
868 if (fuse != fuse->global->fuse_write) {
869 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
870 }
871 }
Jeff Brown6249b902012-05-26 14:32:54 -0700872 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700873}
874
Jeff Brown6249b902012-05-26 14:32:54 -0700875static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700877 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700878{
Jeff Brown6249b902012-05-26 14:32:54 -0700879 struct node* old_parent_node;
880 struct node* new_parent_node;
881 struct node* child_node;
882 char old_parent_path[PATH_MAX];
883 char new_parent_path[PATH_MAX];
884 char old_child_path[PATH_MAX];
885 char new_child_path[PATH_MAX];
886 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400887 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700889
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700890 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700891 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
892 old_parent_path, sizeof(old_parent_path));
893 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
894 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400895 TRACE("[%d] RENAME %s->%s @ %" PRIx64 " (%s) -> %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700896 old_name, new_name,
897 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
898 req->newdir, new_parent_node ? new_parent_node->name : "?");
899 if (!old_parent_node || !new_parent_node) {
900 res = -ENOENT;
901 goto lookup_error;
902 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700903 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700904 res = -EACCES;
905 goto lookup_error;
906 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700907 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700908 res = -EACCES;
909 goto lookup_error;
910 }
Jeff Brown6249b902012-05-26 14:32:54 -0700911 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
912 if (!child_node || get_node_path_locked(child_node,
913 old_child_path, sizeof(old_child_path)) < 0) {
914 res = -ENOENT;
915 goto lookup_error;
916 }
917 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700918 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700919
920 /* Special case for renaming a file where destination is same path
921 * differing only by case. In this case we don't want to look for a case
922 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
923 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400924 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700925 || strcasecmp(old_name, new_name);
926 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
927 new_child_path, sizeof(new_child_path), search))) {
928 res = -ENOENT;
929 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700930 }
931
Jeff Brown6249b902012-05-26 14:32:54 -0700932 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
933 res = rename(old_child_path, new_child_path);
934 if (res < 0) {
935 res = -errno;
936 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700937 }
938
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700939 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700940 res = rename_node_locked(child_node, new_name, new_actual_name);
941 if (!res) {
942 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700943 derive_permissions_locked(fuse, new_parent_node, child_node);
944 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700945 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700946 }
Jeff Brown6249b902012-05-26 14:32:54 -0700947 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948
Jeff Brown6249b902012-05-26 14:32:54 -0700949io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700950 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700951done:
952 release_node_locked(child_node);
953lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700954 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700955 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700956}
957
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700958static int open_flags_to_access_mode(int open_flags) {
959 if ((open_flags & O_ACCMODE) == O_RDONLY) {
960 return R_OK;
961 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
962 return W_OK;
963 } else {
964 /* Probably O_RDRW, but treat as default to be safe */
965 return R_OK | W_OK;
966 }
967}
968
Jeff Brown6249b902012-05-26 14:32:54 -0700969static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
971{
Jeff Brown6249b902012-05-26 14:32:54 -0700972 struct node* node;
973 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700975 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700976
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700977 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700978 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400979 TRACE("[%d] OPEN 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700980 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700981 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700982
983 if (!node) {
984 return -ENOENT;
985 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700986 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700987 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700988 return -EACCES;
989 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400990 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700991 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700992 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700993 }
Jeff Brown6249b902012-05-26 14:32:54 -0700994 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700995 h->fd = open(path, req->flags);
996 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700997 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700998 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700999 }
1000 out.fh = ptr_to_id(h);
1001 out.open_flags = 0;
1002 out.padding = 0;
1003 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001004 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005}
1006
Jeff Brown6249b902012-05-26 14:32:54 -07001007static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001008 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1009{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001010 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001011 __u64 unique = hdr->unique;
1012 __u32 size = req->size;
1013 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001014 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001015 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001016
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001017 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1018 * overlaps the request buffer and will clobber data in the request. This
1019 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001020
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001021 TRACE("[%d] READ %p(%d) %u@%" PRIu64 "\n", handler->token,
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001022 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001023 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001024 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001025 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001026 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001027 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001028 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001029 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001030 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001031 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001032}
1033
Jeff Brown6249b902012-05-26 14:32:54 -07001034static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001035 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1036 const void* buffer)
1037{
1038 struct fuse_write_out out;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001039 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001040 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001041 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001042
Arpad Horvath49e93442014-02-18 10:18:25 +01001043 if (req->flags & O_DIRECT) {
1044 memcpy(aligned_buffer, buffer, req->size);
1045 buffer = (const __u8*) aligned_buffer;
1046 }
Jeff Brown6249b902012-05-26 14:32:54 -07001047
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001048 TRACE("[%d] WRITE %p(%d) %u@%" PRIu64 "\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001049 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050 res = pwrite64(h->fd, buffer, req->size, req->offset);
1051 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001052 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001053 }
1054 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001055 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001056 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001057 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001058}
1059
Jeff Brown6249b902012-05-26 14:32:54 -07001060static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001061 const struct fuse_in_header* hdr)
1062{
Jeff Brown6249b902012-05-26 14:32:54 -07001063 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001064 struct statfs stat;
1065 struct fuse_statfs_out out;
1066 int res;
1067
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001068 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001069 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001070 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001071 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001072 if (res < 0) {
1073 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001074 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001075 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001076 return -errno;
1077 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001078 memset(&out, 0, sizeof(out));
1079 out.st.blocks = stat.f_blocks;
1080 out.st.bfree = stat.f_bfree;
1081 out.st.bavail = stat.f_bavail;
1082 out.st.files = stat.f_files;
1083 out.st.ffree = stat.f_ffree;
1084 out.st.bsize = stat.f_bsize;
1085 out.st.namelen = stat.f_namelen;
1086 out.st.frsize = stat.f_frsize;
1087 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001088 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089}
1090
Jeff Brown6249b902012-05-26 14:32:54 -07001091static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1093{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001094 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001095
1096 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001097 close(h->fd);
1098 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001099 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001100}
1101
Jeff Brown6249b902012-05-26 14:32:54 -07001102static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001103 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1104{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001105 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1106 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001107
Elliott Hughesf6d67372014-07-08 14:38:26 -07001108 int fd = -1;
1109 if (is_dir) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001110 struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001111 fd = dirfd(dh->d);
1112 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001113 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001114 fd = h->fd;
1115 }
1116
1117 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1118 is_dir ? "FSYNCDIR" : "FSYNC",
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001119 static_cast<struct node*>(id_to_ptr(req->fh)), fd, is_data_sync);
Elliott Hughesf6d67372014-07-08 14:38:26 -07001120 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1121 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001122 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001123 }
Jeff Brown6249b902012-05-26 14:32:54 -07001124 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001125}
1126
Jeff Brown6249b902012-05-26 14:32:54 -07001127static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001128 const struct fuse_in_header* hdr)
1129{
Jeff Brown6249b902012-05-26 14:32:54 -07001130 TRACE("[%d] FLUSH\n", handler->token);
1131 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001132}
1133
Jeff Brown6249b902012-05-26 14:32:54 -07001134static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001135 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1136{
Jeff Brown6249b902012-05-26 14:32:54 -07001137 struct node* node;
1138 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001139 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001140 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001141
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001142 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001143 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001144 TRACE("[%d] OPENDIR @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001145 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001146 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001147
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001148 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001149 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001151 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001152 return -EACCES;
1153 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001154 h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001155 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001156 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001157 }
Jeff Brown6249b902012-05-26 14:32:54 -07001158 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001159 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001160 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001161 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001162 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001163 }
1164 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001165 out.open_flags = 0;
1166 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001167 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001168 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001169}
1170
Jeff Brown6249b902012-05-26 14:32:54 -07001171static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001172 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1173{
1174 char buffer[8192];
1175 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1176 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001177 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001178
1179 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001180 if (req->offset == 0) {
1181 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001182 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183 rewinddir(h->d);
1184 }
1185 de = readdir(h->d);
1186 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001187 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001188 }
1189 fde->ino = FUSE_UNKNOWN_INO;
1190 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1191 fde->off = req->offset + 1;
1192 fde->type = de->d_type;
1193 fde->namelen = strlen(de->d_name);
1194 memcpy(fde->name, de->d_name, fde->namelen + 1);
1195 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001196 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1197 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198}
1199
Jeff Brown6249b902012-05-26 14:32:54 -07001200static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001201 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1202{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001203 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001204
1205 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001206 closedir(h->d);
1207 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001208 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209}
1210
Jeff Brown6249b902012-05-26 14:32:54 -07001211static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1213{
1214 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001215 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216
Jeff Brown6249b902012-05-26 14:32:54 -07001217 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1218 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001219
1220 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1221 * defined (fuse version 7.6). The structure is the same from 7.6 through
1222 * 7.22. Beginning with 7.23, the structure increased in size and added
1223 * new parameters.
1224 */
1225 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1226 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1227 req->major, req->minor, FUSE_KERNEL_VERSION);
1228 return -1;
1229 }
1230
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001231 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1232 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001233 fuse_struct_size = sizeof(out);
1234#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1235 /* FUSE_KERNEL_VERSION >= 23. */
1236
1237 /* If the kernel only works on minor revs older than or equal to 22,
1238 * then use the older structure size since this code only uses the 7.22
1239 * version of the structure. */
1240 if (req->minor <= 22) {
1241 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1242 }
1243#endif
1244
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001246 out.max_readahead = req->max_readahead;
1247 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1248 out.max_background = 32;
1249 out.congestion_threshold = 32;
1250 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001251 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001252 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001253}
1254
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001255static int handle_canonical_path(struct fuse* fuse, struct fuse_handler* handler,
1256 const struct fuse_in_header *hdr)
1257{
1258 struct node* node;
1259 char path[PATH_MAX];
1260 int len;
1261
1262 pthread_mutex_lock(&fuse->global->lock);
1263 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1264 path, sizeof(path));
1265 TRACE("[%d] CANONICAL_PATH @ %" PRIx64 " (%s)\n", handler->token, hdr->nodeid,
1266 node ? node->name : "?");
1267 pthread_mutex_unlock(&fuse->global->lock);
1268
1269 if (!node) {
1270 return -ENOENT;
1271 }
1272 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
1273 return -EACCES;
1274 }
1275 len = strlen(path);
1276 if (len + 1 > PATH_MAX)
1277 len = PATH_MAX - 1;
1278 path[PATH_MAX - 1] = 0;
1279 fuse_reply(fuse, hdr->unique, path, len + 1);
1280 return NO_STATUS;
1281}
1282
1283
Jeff Brown6249b902012-05-26 14:32:54 -07001284static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001285 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1286{
Brian Swetland03ee9472010-08-12 18:01:08 -07001287 switch (hdr->opcode) {
1288 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001289 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001290 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001291 }
Jeff Brown84715842012-05-25 14:07:47 -07001292
Brian Swetland03ee9472010-08-12 18:01:08 -07001293 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001294 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001295 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001296 }
Jeff Brown84715842012-05-25 14:07:47 -07001297
Brian Swetland03ee9472010-08-12 18:01:08 -07001298 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001299 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001300 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001301 }
Jeff Brown84715842012-05-25 14:07:47 -07001302
Brian Swetland03ee9472010-08-12 18:01:08 -07001303 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001304 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001305 return handle_setattr(fuse, handler, hdr, req);
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_READLINK:
1309// case FUSE_SYMLINK:
1310 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001311 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001312 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001313 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001314 }
Jeff Brown84715842012-05-25 14:07:47 -07001315
Brian Swetland03ee9472010-08-12 18:01:08 -07001316 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001317 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001318 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001319 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001320 }
Jeff Brown84715842012-05-25 14:07:47 -07001321
Brian Swetland03ee9472010-08-12 18:01:08 -07001322 case FUSE_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001323 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001324 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001325 }
Jeff Brown84715842012-05-25 14:07:47 -07001326
Brian Swetland03ee9472010-08-12 18:01:08 -07001327 case FUSE_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001328 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001329 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001330 }
Jeff Brown84715842012-05-25 14:07:47 -07001331
Brian Swetland03ee9472010-08-12 18:01:08 -07001332 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001333 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001334 const char *old_name = ((const char*) data) + sizeof(*req);
1335 const char *new_name = old_name + strlen(old_name) + 1;
1336 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001337 }
Jeff Brown84715842012-05-25 14:07:47 -07001338
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001339// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001340 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001341 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001342 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001343 }
Jeff Brown84715842012-05-25 14:07:47 -07001344
Brian Swetland03ee9472010-08-12 18:01:08 -07001345 case FUSE_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001346 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001347 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001348 }
Jeff Brown84715842012-05-25 14:07:47 -07001349
Brian Swetland03ee9472010-08-12 18:01:08 -07001350 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001351 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001352 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001353 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001354 }
Jeff Brown84715842012-05-25 14:07:47 -07001355
Mike Lockwood4553b082010-08-16 14:14:44 -04001356 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001357 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001358 }
Jeff Brown84715842012-05-25 14:07:47 -07001359
Brian Swetland03ee9472010-08-12 18:01:08 -07001360 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001361 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001362 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001363 }
Jeff Brown84715842012-05-25 14:07:47 -07001364
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001365 case FUSE_FSYNC:
1366 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001367 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001368 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001369 }
1370
Brian Swetland03ee9472010-08-12 18:01:08 -07001371// case FUSE_SETXATTR:
1372// case FUSE_GETXATTR:
1373// case FUSE_LISTXATTR:
1374// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001375 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001376 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001377 }
1378
Brian Swetland03ee9472010-08-12 18:01:08 -07001379 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001380 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001381 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001382 }
Jeff Brown84715842012-05-25 14:07:47 -07001383
Brian Swetland03ee9472010-08-12 18:01:08 -07001384 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001385 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001386 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001387 }
Jeff Brown84715842012-05-25 14:07:47 -07001388
Brian Swetland03ee9472010-08-12 18:01:08 -07001389 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001390 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001391 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001392 }
Jeff Brown84715842012-05-25 14:07:47 -07001393
Brian Swetland03ee9472010-08-12 18:01:08 -07001394 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001395 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001396 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001397 }
Jeff Brown84715842012-05-25 14:07:47 -07001398
Daniel Rosenberg2abee9e2016-04-19 18:33:08 -07001399 case FUSE_CANONICAL_PATH: { /* nodeid -> bytez[] */
1400 return handle_canonical_path(fuse, handler, hdr);
1401 }
1402
Brian Swetland03ee9472010-08-12 18:01:08 -07001403 default: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001404 TRACE("[%d] NOTIMPL op=%d uniq=%" PRIx64 " nid=%" PRIx64 "\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001405 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1406 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001407 }
Jeff Brown84715842012-05-25 14:07:47 -07001408 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001409}
1410
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001411void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001412{
Jeff Brown6249b902012-05-26 14:32:54 -07001413 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001414 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001415 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1416 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001417 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001418 if (errno == ENODEV) {
1419 ERROR("[%d] someone stole our marbles!\n", handler->token);
1420 exit(2);
1421 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001422 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001423 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001424 }
Jeff Brown84715842012-05-25 14:07:47 -07001425
1426 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001427 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1428 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001429 }
1430
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001431 const struct fuse_in_header* hdr =
1432 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001433 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001434 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1435 handler->token, (size_t)len, hdr->len);
1436 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001437 }
1438
Jeff Brown7729d242012-05-25 15:35:28 -07001439 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001440 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001441 __u64 unique = hdr->unique;
1442 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001443
1444 /* We do not access the request again after this point because the underlying
1445 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001446
1447 if (res != NO_STATUS) {
1448 if (res) {
1449 TRACE("[%d] ERROR %d\n", handler->token, res);
1450 }
1451 fuse_status(fuse, unique, res);
1452 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001453 }
1454}