blob: 1b6a5f19b3a6adeaa6fa041e2b4d53bda402bbc2 [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
21#define FUSE_UNKNOWN_INO 0xffffffff
22
Jeff Brown6249b902012-05-26 14:32:54 -070023/* Pseudo-error constant used to indicate that no fuse status is needed
24 * or that a reply has already been written. */
25#define NO_STATUS 1
26
Jeff Brown6249b902012-05-26 14:32:54 -070027static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -070028{
Jeff Brown6249b902012-05-26 14:32:54 -070029 return (void *) (uintptr_t) nid;
30}
Brian Swetland03ee9472010-08-12 18:01:08 -070031
Jeff Brown6249b902012-05-26 14:32:54 -070032static inline __u64 ptr_to_id(void *ptr)
33{
34 return (__u64) (uintptr_t) ptr;
35}
Brian Swetland03ee9472010-08-12 18:01:08 -070036
Jeff Brown6249b902012-05-26 14:32:54 -070037static void acquire_node_locked(struct node* node)
38{
39 node->refcount++;
40 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
41}
42
43static void remove_node_from_parent_locked(struct node* node);
44
45static void release_node_locked(struct node* node)
46{
47 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
48 if (node->refcount > 0) {
49 node->refcount--;
50 if (!node->refcount) {
51 TRACE("DESTROY %p (%s)\n", node, node->name);
52 remove_node_from_parent_locked(node);
53
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -040054 /* TODO: remove debugging - poison memory */
Jeff Brown6249b902012-05-26 14:32:54 -070055 memset(node->name, 0xef, node->namelen);
56 free(node->name);
57 free(node->actual_name);
58 memset(node, 0xfc, sizeof(*node));
59 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -080060 }
Jeff Brown6249b902012-05-26 14:32:54 -070061 } else {
62 ERROR("Zero refcnt %p\n", node);
63 }
64}
65
66static void add_node_to_parent_locked(struct node *node, struct node *parent) {
67 node->parent = parent;
68 node->next = parent->child;
69 parent->child = node;
70 acquire_node_locked(parent);
71}
72
73static void remove_node_from_parent_locked(struct node* node)
74{
75 if (node->parent) {
76 if (node->parent->child == node) {
77 node->parent->child = node->parent->child->next;
78 } else {
79 struct node *node2;
80 node2 = node->parent->child;
81 while (node2->next != node)
82 node2 = node2->next;
83 node2->next = node->next;
84 }
85 release_node_locked(node->parent);
86 node->parent = NULL;
87 node->next = NULL;
88 }
89}
90
91/* Gets the absolute path to a node into the provided buffer.
92 *
93 * Populates 'buf' with the path and returns the length of the path on success,
94 * or returns -1 if the path is too long for the provided buffer.
95 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -070096static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
97 const char* name;
98 size_t namelen;
99 if (node->graft_path) {
100 name = node->graft_path;
101 namelen = node->graft_pathlen;
102 } else if (node->actual_name) {
103 name = node->actual_name;
104 namelen = node->namelen;
105 } else {
106 name = node->name;
107 namelen = node->namelen;
108 }
109
Jeff Brown6249b902012-05-26 14:32:54 -0700110 if (bufsize < namelen + 1) {
111 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700112 }
113
Jeff Brown6249b902012-05-26 14:32:54 -0700114 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700115 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700116 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
117 if (pathlen < 0) {
118 return -1;
119 }
120 buf[pathlen++] = '/';
121 }
122
Jeff Brown6249b902012-05-26 14:32:54 -0700123 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
124 return pathlen + namelen;
125}
126
127/* Finds the absolute path of a file within a given directory.
128 * Performs a case-insensitive search for the file and sets the buffer to the path
129 * of the first matching file. If 'search' is zero or if no match is found, sets
130 * the buffer to the path that the file would have, assuming the name were case-sensitive.
131 *
132 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
133 * or returns NULL if the path is too long for the provided buffer.
134 */
135static char* find_file_within(const char* path, const char* name,
136 char* buf, size_t bufsize, int search)
137{
138 size_t pathlen = strlen(path);
139 size_t namelen = strlen(name);
140 size_t childlen = pathlen + namelen + 1;
141 char* actual;
142
143 if (bufsize <= childlen) {
144 return NULL;
145 }
146
147 memcpy(buf, path, pathlen);
148 buf[pathlen] = '/';
149 actual = buf + pathlen + 1;
150 memcpy(actual, name, namelen + 1);
151
152 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800153 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700154 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800155 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700156 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700157 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800158 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800159 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700160 if (!strcasecmp(entry->d_name, name)) {
161 /* we have a match - replace the name, don't need to copy the null again */
162 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800163 break;
164 }
165 }
166 closedir(dir);
167 }
Jeff Brown6249b902012-05-26 14:32:54 -0700168 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800169}
170
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700171static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
172 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000173 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700174 attr->size = s->st_size;
175 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800176 attr->atime = s->st_atim.tv_sec;
177 attr->mtime = s->st_mtim.tv_sec;
178 attr->ctime = s->st_ctim.tv_sec;
179 attr->atimensec = s->st_atim.tv_nsec;
180 attr->mtimensec = s->st_mtim.tv_nsec;
181 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700182 attr->mode = s->st_mode;
183 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700184
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700185 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700186
187 if (fuse->gid == AID_SDCARD_RW) {
188 /* As an optimization, certain trusted system components only run
189 * as owner but operate across all users. Since we're now handing
190 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
191 * the user boundary enforcement for the default view. The UIDs
192 * assigned to app directories are still multiuser aware. */
193 attr->gid = AID_SDCARD_RW;
194 } else {
195 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
196 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700197
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700198 int visible_mode = 0775 & ~fuse->mask;
199 if (node->perm == PERM_PRE_ROOT) {
200 /* Top of multi-user view should always be visible to ensure
201 * secondary users can traverse inside. */
202 visible_mode = 0711;
203 } else if (node->under_android) {
204 /* Block "other" access to Android directories, since only apps
205 * belonging to a specific user should be in there; we still
206 * leave +x open for the default view. */
207 if (fuse->gid == AID_SDCARD_RW) {
208 visible_mode = visible_mode & ~0006;
209 } else {
210 visible_mode = visible_mode & ~0007;
211 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700212 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700213 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700214 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700215 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
216}
217
Jeff Sharkey44d63422013-09-12 09:44:48 -0700218static int touch(char* path, mode_t mode) {
219 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
220 if (fd == -1) {
221 if (errno == EEXIST) {
222 return 0;
223 } else {
224 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
225 return -1;
226 }
227 }
228 close(fd);
229 return 0;
230}
231
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700232static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
233 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700234 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700235
236 /* By default, each node inherits from its parent */
237 node->perm = PERM_INHERIT;
238 node->userid = parent->userid;
239 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700240 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700241
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700242 /* Derive custom permissions based on parent and current node */
243 switch (parent->perm) {
244 case PERM_INHERIT:
245 /* Already inherited above */
246 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700247 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700248 /* Legacy internal layout places users at top level */
249 node->perm = PERM_ROOT;
250 node->userid = strtoul(node->name, NULL, 10);
251 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700252 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700253 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700254 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700255 /* App-specific directories inside; let anyone traverse */
256 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700257 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700258 }
259 break;
260 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700261 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700262 /* App-specific directories inside; let anyone traverse */
263 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700264 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700265 /* App-specific directories inside; let anyone traverse */
266 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700267 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700268 node->graft_path = fuse->global->obb_path;
269 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700270 } else if (!strcasecmp(node->name, "media")) {
271 /* App-specific directories inside; let anyone traverse */
272 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700273 }
274 break;
275 case PERM_ANDROID_DATA:
276 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700277 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700278 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700279 if (appid != 0) {
280 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700281 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700282 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700283 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700284}
285
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400286void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkey22b91262015-12-14 11:02:01 -0700287 struct node *node;
288 for (node = parent->child; node; node = node->next) {
289 derive_permissions_locked(fuse, parent, node);
290 if (node->child) {
291 derive_permissions_recursive_locked(fuse, node);
292 }
293 }
294}
295
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700296/* Kernel has already enforced everything we returned through
297 * derive_permissions_locked(), so this is used to lock down access
298 * even further, such as enforcing that apps hold sdcard_rw. */
299static bool check_caller_access_to_name(struct fuse* fuse,
300 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700301 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700302 /* Always block security-sensitive files at root */
303 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700304 if (!strcasecmp(name, "autorun.inf")
305 || !strcasecmp(name, ".android_secure")
306 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700307 return false;
308 }
309 }
310
Jeff Sharkey44d63422013-09-12 09:44:48 -0700311 /* Root always has access; access for any other UIDs should always
312 * be controlled through packages.list. */
313 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700314 return true;
315 }
316
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700317 /* No extra permissions to enforce */
318 return true;
319}
320
321static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700322 const struct fuse_in_header *hdr, const struct node* node, int mode) {
323 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700324}
325
Jeff Brown6249b902012-05-26 14:32:54 -0700326struct node *create_node_locked(struct fuse* fuse,
327 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700328{
329 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700330 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700331
Narayan Kamathfaa09352015-01-13 18:21:10 +0000332 // Detect overflows in the inode counter. "4 billion nodes should be enough
333 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700334 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000335 ERROR("No more inode numbers available");
336 return NULL;
337 }
338
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400339 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700340 if (!node) {
341 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700342 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400343 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700344 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700345 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700346 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700347 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700348 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700349 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400350 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700351 if (!node->actual_name) {
352 free(node->name);
353 free(node);
354 return NULL;
355 }
356 memcpy(node->actual_name, actual_name, namelen + 1);
357 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700358 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700359 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700360 node->ino = fuse->global->inode_ctr++;
361 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700362
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200363 node->deleted = false;
364
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700365 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700366 acquire_node_locked(node);
367 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700368 return node;
369}
370
Jeff Brown6249b902012-05-26 14:32:54 -0700371static int rename_node_locked(struct node *node, const char *name,
372 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700373{
Jeff Brown6249b902012-05-26 14:32:54 -0700374 size_t namelen = strlen(name);
375 int need_actual_name = strcmp(name, actual_name);
376
377 /* make the storage bigger without actually changing the name
378 * in case an error occurs part way */
379 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400380 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700381 if (!new_name) {
382 return -ENOMEM;
383 }
384 node->name = new_name;
385 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400386 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700387 if (!new_actual_name) {
388 return -ENOMEM;
389 }
390 node->actual_name = new_actual_name;
391 }
392 }
393
394 /* update the name, taking care to allocate storage before overwriting the old name */
395 if (need_actual_name) {
396 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400397 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700398 if (!node->actual_name) {
399 return -ENOMEM;
400 }
401 }
402 memcpy(node->actual_name, actual_name, namelen + 1);
403 } else {
404 free(node->actual_name);
405 node->actual_name = NULL;
406 }
407 memcpy(node->name, name, namelen + 1);
408 node->namelen = namelen;
409 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700410}
411
Jeff Brown6249b902012-05-26 14:32:54 -0700412static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700413{
Jeff Brown6249b902012-05-26 14:32:54 -0700414 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700415 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700416 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400417 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700418 }
419}
420
Jeff Brown6249b902012-05-26 14:32:54 -0700421static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
422 char* buf, size_t bufsize)
423{
424 struct node* node = lookup_node_by_id_locked(fuse, nid);
425 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
426 node = NULL;
427 }
428 return node;
429}
430
431static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700432{
433 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700434 /* use exact string comparison, nodes that differ by case
435 * must be considered distinct even if they refer to the same
436 * underlying file as otherwise operations such as "mv x x"
437 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200438 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700439 return node;
440 }
441 }
442 return 0;
443}
444
Jeff Brown6249b902012-05-26 14:32:54 -0700445static struct node* acquire_or_create_child_locked(
446 struct fuse* fuse, struct node* parent,
447 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700448{
Jeff Brown6249b902012-05-26 14:32:54 -0700449 struct node* child = lookup_child_by_name_locked(parent, name);
450 if (child) {
451 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800452 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700453 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800454 }
Jeff Brown6249b902012-05-26 14:32:54 -0700455 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700456}
457
Jeff Brown6249b902012-05-26 14:32:54 -0700458static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700459{
460 struct fuse_out_header hdr;
461 hdr.len = sizeof(hdr);
462 hdr.error = err;
463 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700464 write(fuse->fd, &hdr, sizeof(hdr));
465}
466
Jeff Brown6249b902012-05-26 14:32:54 -0700467static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700468{
469 struct fuse_out_header hdr;
470 struct iovec vec[2];
471 int res;
472
473 hdr.len = len + sizeof(hdr);
474 hdr.error = 0;
475 hdr.unique = unique;
476
477 vec[0].iov_base = &hdr;
478 vec[0].iov_len = sizeof(hdr);
479 vec[1].iov_base = data;
480 vec[1].iov_len = len;
481
482 res = writev(fuse->fd, vec, 2);
483 if (res < 0) {
484 ERROR("*** REPLY FAILED *** %d\n", errno);
485 }
486}
487
Jeff Brown6249b902012-05-26 14:32:54 -0700488static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
489 struct node* parent, const char* name, const char* actual_name,
490 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700491{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700492 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700493 struct fuse_entry_out out;
494 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700495
Jeff Brown6249b902012-05-26 14:32:54 -0700496 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700497 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700498 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700499
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700500 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700501 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
502 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700503 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700504 return -ENOMEM;
505 }
506 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700507 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700508 out.attr_valid = 10;
509 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700510 out.nodeid = node->nid;
511 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700512 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700513 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700514 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700515}
516
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700517static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700518 const char* path)
519{
520 struct fuse_attr_out out;
521 struct stat s;
522
523 if (lstat(path, &s) < 0) {
524 return -errno;
525 }
526 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700527 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700528 out.attr_valid = 10;
529 fuse_reply(fuse, unique, &out, sizeof(out));
530 return NO_STATUS;
531}
532
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700533static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
534 const __u64 child, const char* name) {
535 struct fuse_out_header hdr;
536 struct fuse_notify_delete_out data;
537 struct iovec vec[3];
538 size_t namelen = strlen(name);
539 int res;
540
541 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
542 hdr.error = FUSE_NOTIFY_DELETE;
543 hdr.unique = 0;
544
545 data.parent = parent;
546 data.child = child;
547 data.namelen = namelen;
548 data.padding = 0;
549
550 vec[0].iov_base = &hdr;
551 vec[0].iov_len = sizeof(hdr);
552 vec[1].iov_base = &data;
553 vec[1].iov_len = sizeof(data);
554 vec[2].iov_base = (void*) name;
555 vec[2].iov_len = namelen + 1;
556
557 res = writev(fuse->fd, vec, 3);
558 /* Ignore ENOENT, since other views may not have seen the entry */
559 if (res < 0 && errno != ENOENT) {
560 ERROR("*** NOTIFY FAILED *** %d\n", errno);
561 }
562}
563
Jeff Brown6249b902012-05-26 14:32:54 -0700564static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700565 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700566{
Jeff Brown6249b902012-05-26 14:32:54 -0700567 struct node* parent_node;
568 char parent_path[PATH_MAX];
569 char child_path[PATH_MAX];
570 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700571
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700572 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700573 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
574 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400575 TRACE("[%d] LOOKUP %s @ %" PRIx64 " (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700576 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700577 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700578
579 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
580 child_path, sizeof(child_path), 1))) {
581 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700582 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700583 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700584 return -EACCES;
585 }
586
Jeff Brown6249b902012-05-26 14:32:54 -0700587 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700588}
589
Jeff Brown6249b902012-05-26 14:32:54 -0700590static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700591 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
592{
Jeff Brown6249b902012-05-26 14:32:54 -0700593 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700594
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700595 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700596 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400597 TRACE("[%d] FORGET #%" PRIu64 " @ %" PRIx64 " (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700598 hdr->nodeid, node ? node->name : "?");
599 if (node) {
600 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400601 while (n) {
602 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700603 release_node_locked(node);
604 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700605 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700606 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700607 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700608}
609
Jeff Brown6249b902012-05-26 14:32:54 -0700610static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700611 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
612{
Jeff Brown6249b902012-05-26 14:32:54 -0700613 struct node* node;
614 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700615
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700616 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700617 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400618 TRACE("[%d] GETATTR flags=%x fh=%" PRIx64 " @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700619 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700620 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700621
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700622 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700623 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700624 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700625 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700626 return -EACCES;
627 }
628
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700629 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700630}
631
Jeff Brown6249b902012-05-26 14:32:54 -0700632static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700633 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
634{
Jeff Brown6249b902012-05-26 14:32:54 -0700635 struct node* node;
636 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700637 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700638
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700639 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700640 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400641 TRACE("[%d] SETATTR fh=%" PRIx64 " valid=%x @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700642 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700643 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700644
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700645 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700646 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700647 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800648
649 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700650 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700651 return -EACCES;
652 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700653
Jeff Brown6249b902012-05-26 14:32:54 -0700654 /* XXX: incomplete implementation on purpose.
655 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700656
Elliott Hughes853574d2014-07-31 12:03:03 -0700657 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700658 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700659 }
660
661 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
662 * are both set, then set it to the current time. Else, set it to the
663 * time specified in the request. Same goes for mtime. Use utimensat(2)
664 * as it allows ATIME and MTIME to be changed independently, and has
665 * nanosecond resolution which fuse also has.
666 */
667 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
668 times[0].tv_nsec = UTIME_OMIT;
669 times[1].tv_nsec = UTIME_OMIT;
670 if (req->valid & FATTR_ATIME) {
671 if (req->valid & FATTR_ATIME_NOW) {
672 times[0].tv_nsec = UTIME_NOW;
673 } else {
674 times[0].tv_sec = req->atime;
675 times[0].tv_nsec = req->atimensec;
676 }
677 }
678 if (req->valid & FATTR_MTIME) {
679 if (req->valid & FATTR_MTIME_NOW) {
680 times[1].tv_nsec = UTIME_NOW;
681 } else {
682 times[1].tv_sec = req->mtime;
683 times[1].tv_nsec = req->mtimensec;
684 }
685 }
Jeff Brown6249b902012-05-26 14:32:54 -0700686 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
687 handler->token, path, times[0].tv_sec, times[1].tv_sec);
688 if (utimensat(-1, path, times, 0) < 0) {
689 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700690 }
691 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700692 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700693}
694
Jeff Brown6249b902012-05-26 14:32:54 -0700695static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700696 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
697{
Jeff Brown6249b902012-05-26 14:32:54 -0700698 struct node* parent_node;
699 char parent_path[PATH_MAX];
700 char child_path[PATH_MAX];
701 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700702
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700703 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700704 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
705 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400706 TRACE("[%d] MKNOD %s 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700707 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700708 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700709
710 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
711 child_path, sizeof(child_path), 1))) {
712 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700713 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700714 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700715 return -EACCES;
716 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700717 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700718 if (mknod(child_path, mode, req->rdev) < 0) {
719 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700720 }
Jeff Brown6249b902012-05-26 14:32:54 -0700721 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700722}
723
Jeff Brown6249b902012-05-26 14:32:54 -0700724static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700725 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
726{
Jeff Brown6249b902012-05-26 14:32:54 -0700727 struct node* parent_node;
728 char parent_path[PATH_MAX];
729 char child_path[PATH_MAX];
730 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700731
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700732 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700733 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
734 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400735 TRACE("[%d] MKDIR %s 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700736 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700737 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700738
739 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
740 child_path, sizeof(child_path), 1))) {
741 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700742 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700743 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700744 return -EACCES;
745 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700746 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700747 if (mkdir(child_path, mode) < 0) {
748 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700749 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700750
751 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
752 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
753 char nomedia[PATH_MAX];
754 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
755 if (touch(nomedia, 0664) != 0) {
756 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
757 return -ENOENT;
758 }
759 }
760 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
761 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700762 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700763 if (touch(nomedia, 0664) != 0) {
764 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
765 return -ENOENT;
766 }
767 }
768
Jeff Brown6249b902012-05-26 14:32:54 -0700769 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700770}
771
Jeff Brown6249b902012-05-26 14:32:54 -0700772static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700773 const struct fuse_in_header* hdr, const char* name)
774{
Jeff Brown6249b902012-05-26 14:32:54 -0700775 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200776 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700777 char parent_path[PATH_MAX];
778 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700779
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700780 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700781 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
782 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400783 TRACE("[%d] UNLINK %s @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700784 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700785 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700786
787 if (!parent_node || !find_file_within(parent_path, name,
788 child_path, sizeof(child_path), 1)) {
789 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700790 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700791 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700792 return -EACCES;
793 }
Jeff Brown6249b902012-05-26 14:32:54 -0700794 if (unlink(child_path) < 0) {
795 return -errno;
796 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700797 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200798 child_node = lookup_child_by_name_locked(parent_node, name);
799 if (child_node) {
800 child_node->deleted = true;
801 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700802 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700803 if (parent_node && child_node) {
804 /* Tell all other views that node is gone */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400805 TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n",
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700806 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
807 if (fuse != fuse->global->fuse_default) {
808 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
809 }
810 if (fuse != fuse->global->fuse_read) {
811 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
812 }
813 if (fuse != fuse->global->fuse_write) {
814 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
815 }
816 }
Jeff Brown6249b902012-05-26 14:32:54 -0700817 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818}
819
Jeff Brown6249b902012-05-26 14:32:54 -0700820static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700821 const struct fuse_in_header* hdr, const char* name)
822{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200823 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700824 struct node* parent_node;
825 char parent_path[PATH_MAX];
826 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700827
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700828 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700829 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
830 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400831 TRACE("[%d] RMDIR %s @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700832 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700833 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700834
835 if (!parent_node || !find_file_within(parent_path, name,
836 child_path, sizeof(child_path), 1)) {
837 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700838 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700839 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700840 return -EACCES;
841 }
Jeff Brown6249b902012-05-26 14:32:54 -0700842 if (rmdir(child_path) < 0) {
843 return -errno;
844 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700845 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200846 child_node = lookup_child_by_name_locked(parent_node, name);
847 if (child_node) {
848 child_node->deleted = true;
849 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700850 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700851 if (parent_node && child_node) {
852 /* Tell all other views that node is gone */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400853 TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n",
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700854 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
855 if (fuse != fuse->global->fuse_default) {
856 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
857 }
858 if (fuse != fuse->global->fuse_read) {
859 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
860 }
861 if (fuse != fuse->global->fuse_write) {
862 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
863 }
864 }
Jeff Brown6249b902012-05-26 14:32:54 -0700865 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700866}
867
Jeff Brown6249b902012-05-26 14:32:54 -0700868static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700869 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700870 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700871{
Jeff Brown6249b902012-05-26 14:32:54 -0700872 struct node* old_parent_node;
873 struct node* new_parent_node;
874 struct node* child_node;
875 char old_parent_path[PATH_MAX];
876 char new_parent_path[PATH_MAX];
877 char old_child_path[PATH_MAX];
878 char new_child_path[PATH_MAX];
879 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400880 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700883 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700884 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
885 old_parent_path, sizeof(old_parent_path));
886 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
887 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400888 TRACE("[%d] RENAME %s->%s @ %" PRIx64 " (%s) -> %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700889 old_name, new_name,
890 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
891 req->newdir, new_parent_node ? new_parent_node->name : "?");
892 if (!old_parent_node || !new_parent_node) {
893 res = -ENOENT;
894 goto lookup_error;
895 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700896 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700897 res = -EACCES;
898 goto lookup_error;
899 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700900 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700901 res = -EACCES;
902 goto lookup_error;
903 }
Jeff Brown6249b902012-05-26 14:32:54 -0700904 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
905 if (!child_node || get_node_path_locked(child_node,
906 old_child_path, sizeof(old_child_path)) < 0) {
907 res = -ENOENT;
908 goto lookup_error;
909 }
910 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700911 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700912
913 /* Special case for renaming a file where destination is same path
914 * differing only by case. In this case we don't want to look for a case
915 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
916 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400917 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700918 || strcasecmp(old_name, new_name);
919 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
920 new_child_path, sizeof(new_child_path), search))) {
921 res = -ENOENT;
922 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700923 }
924
Jeff Brown6249b902012-05-26 14:32:54 -0700925 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
926 res = rename(old_child_path, new_child_path);
927 if (res < 0) {
928 res = -errno;
929 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700930 }
931
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700932 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700933 res = rename_node_locked(child_node, new_name, new_actual_name);
934 if (!res) {
935 remove_node_from_parent_locked(child_node);
Jeff Sharkey22b91262015-12-14 11:02:01 -0700936 derive_permissions_locked(fuse, new_parent_node, child_node);
937 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700938 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700939 }
Jeff Brown6249b902012-05-26 14:32:54 -0700940 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941
Jeff Brown6249b902012-05-26 14:32:54 -0700942io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700943 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700944done:
945 release_node_locked(child_node);
946lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700947 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700948 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700949}
950
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700951static int open_flags_to_access_mode(int open_flags) {
952 if ((open_flags & O_ACCMODE) == O_RDONLY) {
953 return R_OK;
954 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
955 return W_OK;
956 } else {
957 /* Probably O_RDRW, but treat as default to be safe */
958 return R_OK | W_OK;
959 }
960}
961
Jeff Brown6249b902012-05-26 14:32:54 -0700962static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700963 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
964{
Jeff Brown6249b902012-05-26 14:32:54 -0700965 struct node* node;
966 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700968 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700970 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700971 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400972 TRACE("[%d] OPEN 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700973 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700974 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700975
976 if (!node) {
977 return -ENOENT;
978 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700979 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700980 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700981 return -EACCES;
982 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400983 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700985 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700986 }
Jeff Brown6249b902012-05-26 14:32:54 -0700987 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700988 h->fd = open(path, req->flags);
989 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700990 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700991 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700992 }
993 out.fh = ptr_to_id(h);
994 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -0800995
996#ifdef FUSE_STACKED_IO
997 out.lower_fd = h->fd;
998#else
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700999 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001000#endif
1001
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001002 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001003 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001004}
1005
Jeff Brown6249b902012-05-26 14:32:54 -07001006static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001007 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1008{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001009 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001010 __u64 unique = hdr->unique;
1011 __u32 size = req->size;
1012 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001013 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001014 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001015
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001016 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1017 * overlaps the request buffer and will clobber data in the request. This
1018 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001019
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001020 TRACE("[%d] READ %p(%d) %u@%" PRIu64 "\n", handler->token,
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001021 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001022 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001023 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001024 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001025 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001027 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001028 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001029 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001030 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001031}
1032
Jeff Brown6249b902012-05-26 14:32:54 -07001033static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001034 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1035 const void* buffer)
1036{
1037 struct fuse_write_out out;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001038 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001039 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001040 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001041
Arpad Horvath49e93442014-02-18 10:18:25 +01001042 if (req->flags & O_DIRECT) {
1043 memcpy(aligned_buffer, buffer, req->size);
1044 buffer = (const __u8*) aligned_buffer;
1045 }
Jeff Brown6249b902012-05-26 14:32:54 -07001046
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001047 TRACE("[%d] WRITE %p(%d) %u@%" PRIu64 "\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001048 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001049 res = pwrite64(h->fd, buffer, req->size, req->offset);
1050 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001051 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001052 }
1053 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001054 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001055 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001056 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001057}
1058
Jeff Brown6249b902012-05-26 14:32:54 -07001059static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001060 const struct fuse_in_header* hdr)
1061{
Jeff Brown6249b902012-05-26 14:32:54 -07001062 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001063 struct statfs stat;
1064 struct fuse_statfs_out out;
1065 int res;
1066
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001067 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001068 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001069 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001070 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001071 if (res < 0) {
1072 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001074 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001075 return -errno;
1076 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001077 memset(&out, 0, sizeof(out));
1078 out.st.blocks = stat.f_blocks;
1079 out.st.bfree = stat.f_bfree;
1080 out.st.bavail = stat.f_bavail;
1081 out.st.files = stat.f_files;
1082 out.st.ffree = stat.f_ffree;
1083 out.st.bsize = stat.f_bsize;
1084 out.st.namelen = stat.f_namelen;
1085 out.st.frsize = stat.f_frsize;
1086 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001087 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001088}
1089
Jeff Brown6249b902012-05-26 14:32:54 -07001090static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001091 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1092{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001093 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001094
1095 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001096 close(h->fd);
1097 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001098 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001099}
1100
Jeff Brown6249b902012-05-26 14:32:54 -07001101static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001102 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1103{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001104 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1105 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001106
Elliott Hughesf6d67372014-07-08 14:38:26 -07001107 int fd = -1;
1108 if (is_dir) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001109 struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001110 fd = dirfd(dh->d);
1111 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001112 struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh));
Elliott Hughesf6d67372014-07-08 14:38:26 -07001113 fd = h->fd;
1114 }
1115
1116 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1117 is_dir ? "FSYNCDIR" : "FSYNC",
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001118 static_cast<struct node*>(id_to_ptr(req->fh)), fd, is_data_sync);
Elliott Hughesf6d67372014-07-08 14:38:26 -07001119 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1120 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001121 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001122 }
Jeff Brown6249b902012-05-26 14:32:54 -07001123 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001124}
1125
Jeff Brown6249b902012-05-26 14:32:54 -07001126static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001127 const struct fuse_in_header* hdr)
1128{
Jeff Brown6249b902012-05-26 14:32:54 -07001129 TRACE("[%d] FLUSH\n", handler->token);
1130 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001131}
1132
Jeff Brown6249b902012-05-26 14:32:54 -07001133static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001134 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1135{
Jeff Brown6249b902012-05-26 14:32:54 -07001136 struct node* node;
1137 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001138 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001139 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001140
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001141 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001142 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001143 TRACE("[%d] OPENDIR @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001144 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001145 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001146
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001147 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001148 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001149 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001150 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001151 return -EACCES;
1152 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001153 h = static_cast<struct dirhandle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001154 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001155 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156 }
Jeff Brown6249b902012-05-26 14:32:54 -07001157 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001159 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001161 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001162 }
1163 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001164 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001165
1166#ifdef FUSE_STACKED_IO
1167 out.lower_fd = -1;
1168#else
Ken Sumrall3a876882013-08-14 20:02:13 -07001169 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001170#endif
1171
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001172 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001173 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001174}
1175
Jeff Brown6249b902012-05-26 14:32:54 -07001176static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001177 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1178{
1179 char buffer[8192];
1180 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1181 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001182 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001183
1184 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001185 if (req->offset == 0) {
1186 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001187 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001188 rewinddir(h->d);
1189 }
1190 de = readdir(h->d);
1191 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001192 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 }
1194 fde->ino = FUSE_UNKNOWN_INO;
1195 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1196 fde->off = req->offset + 1;
1197 fde->type = de->d_type;
1198 fde->namelen = strlen(de->d_name);
1199 memcpy(fde->name, de->d_name, fde->namelen + 1);
1200 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001201 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1202 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203}
1204
Jeff Brown6249b902012-05-26 14:32:54 -07001205static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001206 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1207{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001208 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001209
1210 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211 closedir(h->d);
1212 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001213 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214}
1215
Jeff Brown6249b902012-05-26 14:32:54 -07001216static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1218{
1219 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001220 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001221
Jeff Brown6249b902012-05-26 14:32:54 -07001222 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1223 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001224
1225 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1226 * defined (fuse version 7.6). The structure is the same from 7.6 through
1227 * 7.22. Beginning with 7.23, the structure increased in size and added
1228 * new parameters.
1229 */
1230 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1231 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1232 req->major, req->minor, FUSE_KERNEL_VERSION);
1233 return -1;
1234 }
1235
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001236 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1237 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001238 fuse_struct_size = sizeof(out);
1239#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1240 /* FUSE_KERNEL_VERSION >= 23. */
1241
1242 /* If the kernel only works on minor revs older than or equal to 22,
1243 * then use the older structure size since this code only uses the 7.22
1244 * version of the structure. */
1245 if (req->minor <= 22) {
1246 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1247 }
1248#endif
1249
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001250 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001251 out.max_readahead = req->max_readahead;
1252 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001253
1254#ifdef FUSE_STACKED_IO
1255 out.flags |= FUSE_STACKED_IO;
1256#endif
1257
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001258 out.max_background = 32;
1259 out.congestion_threshold = 32;
1260 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001261 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001262 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001263}
1264
Jeff Brown6249b902012-05-26 14:32:54 -07001265static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1267{
Brian Swetland03ee9472010-08-12 18:01:08 -07001268 switch (hdr->opcode) {
1269 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001270 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001271 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001272 }
Jeff Brown84715842012-05-25 14:07:47 -07001273
Brian Swetland03ee9472010-08-12 18:01:08 -07001274 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001275 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001276 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001277 }
Jeff Brown84715842012-05-25 14:07:47 -07001278
Brian Swetland03ee9472010-08-12 18:01:08 -07001279 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001280 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001281 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001282 }
Jeff Brown84715842012-05-25 14:07:47 -07001283
Brian Swetland03ee9472010-08-12 18:01:08 -07001284 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001285 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001286 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001287 }
Jeff Brown84715842012-05-25 14:07:47 -07001288
Brian Swetland03ee9472010-08-12 18:01:08 -07001289// case FUSE_READLINK:
1290// case FUSE_SYMLINK:
1291 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001292 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001293 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001294 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001295 }
Jeff Brown84715842012-05-25 14:07:47 -07001296
Brian Swetland03ee9472010-08-12 18:01:08 -07001297 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001298 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001299 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001300 return handle_mkdir(fuse, handler, hdr, req, name);
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_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001304 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001305 return handle_unlink(fuse, handler, hdr, 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_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001309 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001310 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001311 }
Jeff Brown84715842012-05-25 14:07:47 -07001312
Brian Swetland03ee9472010-08-12 18:01:08 -07001313 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001314 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001315 const char *old_name = ((const char*) data) + sizeof(*req);
1316 const char *new_name = old_name + strlen(old_name) + 1;
1317 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001318 }
Jeff Brown84715842012-05-25 14:07:47 -07001319
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001320// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001321 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001322 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001323 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001324 }
Jeff Brown84715842012-05-25 14:07:47 -07001325
Brian Swetland03ee9472010-08-12 18:01:08 -07001326 case FUSE_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001327 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001328 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001329 }
Jeff Brown84715842012-05-25 14:07:47 -07001330
Brian Swetland03ee9472010-08-12 18:01:08 -07001331 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001332 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001333 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001334 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001335 }
Jeff Brown84715842012-05-25 14:07:47 -07001336
Mike Lockwood4553b082010-08-16 14:14:44 -04001337 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001338 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001339 }
Jeff Brown84715842012-05-25 14:07:47 -07001340
Brian Swetland03ee9472010-08-12 18:01:08 -07001341 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001342 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001343 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001344 }
Jeff Brown84715842012-05-25 14:07:47 -07001345
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001346 case FUSE_FSYNC:
1347 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001348 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001349 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001350 }
1351
Brian Swetland03ee9472010-08-12 18:01:08 -07001352// case FUSE_SETXATTR:
1353// case FUSE_GETXATTR:
1354// case FUSE_LISTXATTR:
1355// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001356 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001357 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001358 }
1359
Brian Swetland03ee9472010-08-12 18:01:08 -07001360 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001361 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001362 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001363 }
Jeff Brown84715842012-05-25 14:07:47 -07001364
Brian Swetland03ee9472010-08-12 18:01:08 -07001365 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001366 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001367 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001368 }
Jeff Brown84715842012-05-25 14:07:47 -07001369
Brian Swetland03ee9472010-08-12 18:01:08 -07001370 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001371 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001372 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001373 }
Jeff Brown84715842012-05-25 14:07:47 -07001374
Brian Swetland03ee9472010-08-12 18:01:08 -07001375 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001376 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001377 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001378 }
Jeff Brown84715842012-05-25 14:07:47 -07001379
Brian Swetland03ee9472010-08-12 18:01:08 -07001380 default: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001381 TRACE("[%d] NOTIMPL op=%d uniq=%" PRIx64 " nid=%" PRIx64 "\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001382 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1383 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001384 }
Jeff Brown84715842012-05-25 14:07:47 -07001385 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001386}
1387
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001388void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001389{
Jeff Brown6249b902012-05-26 14:32:54 -07001390 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001391 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001392 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1393 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001394 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001395 if (errno == ENODEV) {
1396 ERROR("[%d] someone stole our marbles!\n", handler->token);
1397 exit(2);
1398 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001399 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001400 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001401 }
Jeff Brown84715842012-05-25 14:07:47 -07001402
1403 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001404 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1405 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001406 }
1407
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001408 const struct fuse_in_header* hdr =
1409 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001410 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001411 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1412 handler->token, (size_t)len, hdr->len);
1413 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001414 }
1415
Jeff Brown7729d242012-05-25 15:35:28 -07001416 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001417 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001418 __u64 unique = hdr->unique;
1419 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001420
1421 /* We do not access the request again after this point because the underlying
1422 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001423
1424 if (res != NO_STATUS) {
1425 if (res) {
1426 TRACE("[%d] ERROR %d\n", handler->token, res);
1427 }
1428 fuse_status(fuse, unique, res);
1429 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001430 }
1431}