blob: 9b1b19065572bd15d7e5dc3be3528b27694afd0b [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:
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400278 const auto& iter = fuse->global->package_to_appid->find(node->name);
279 if (iter != fuse->global->package_to_appid->end()) {
280 appid = iter->second;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700281 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700282 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700283 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700284 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700285}
286
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400287void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
Jeff Sharkey22b91262015-12-14 11:02:01 -0700288 struct node *node;
289 for (node = parent->child; node; node = node->next) {
290 derive_permissions_locked(fuse, parent, node);
291 if (node->child) {
292 derive_permissions_recursive_locked(fuse, node);
293 }
294 }
295}
296
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700297/* Kernel has already enforced everything we returned through
298 * derive_permissions_locked(), so this is used to lock down access
299 * even further, such as enforcing that apps hold sdcard_rw. */
300static bool check_caller_access_to_name(struct fuse* fuse,
301 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700302 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700303 /* Always block security-sensitive files at root */
304 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700305 if (!strcasecmp(name, "autorun.inf")
306 || !strcasecmp(name, ".android_secure")
307 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700308 return false;
309 }
310 }
311
Jeff Sharkey44d63422013-09-12 09:44:48 -0700312 /* Root always has access; access for any other UIDs should always
313 * be controlled through packages.list. */
314 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700315 return true;
316 }
317
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700318 /* No extra permissions to enforce */
319 return true;
320}
321
322static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700323 const struct fuse_in_header *hdr, const struct node* node, int mode) {
324 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700325}
326
Jeff Brown6249b902012-05-26 14:32:54 -0700327struct node *create_node_locked(struct fuse* fuse,
328 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700329{
330 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700331 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700332
Narayan Kamathfaa09352015-01-13 18:21:10 +0000333 // Detect overflows in the inode counter. "4 billion nodes should be enough
334 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700335 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000336 ERROR("No more inode numbers available");
337 return NULL;
338 }
339
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400340 node = static_cast<struct node*>(calloc(1, sizeof(struct node)));
Jeff Brown6249b902012-05-26 14:32:54 -0700341 if (!node) {
342 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700343 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400344 node->name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700345 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700346 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700347 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700348 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700349 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700350 if (strcmp(name, actual_name)) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400351 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700352 if (!node->actual_name) {
353 free(node->name);
354 free(node);
355 return NULL;
356 }
357 memcpy(node->actual_name, actual_name, namelen + 1);
358 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700359 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700360 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700361 node->ino = fuse->global->inode_ctr++;
362 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700363
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200364 node->deleted = false;
365
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700366 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700367 acquire_node_locked(node);
368 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700369 return node;
370}
371
Jeff Brown6249b902012-05-26 14:32:54 -0700372static int rename_node_locked(struct node *node, const char *name,
373 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700374{
Jeff Brown6249b902012-05-26 14:32:54 -0700375 size_t namelen = strlen(name);
376 int need_actual_name = strcmp(name, actual_name);
377
378 /* make the storage bigger without actually changing the name
379 * in case an error occurs part way */
380 if (namelen > node->namelen) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400381 char* new_name = static_cast<char*>(realloc(node->name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700382 if (!new_name) {
383 return -ENOMEM;
384 }
385 node->name = new_name;
386 if (need_actual_name && node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400387 char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700388 if (!new_actual_name) {
389 return -ENOMEM;
390 }
391 node->actual_name = new_actual_name;
392 }
393 }
394
395 /* update the name, taking care to allocate storage before overwriting the old name */
396 if (need_actual_name) {
397 if (!node->actual_name) {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400398 node->actual_name = static_cast<char*>(malloc(namelen + 1));
Jeff Brown6249b902012-05-26 14:32:54 -0700399 if (!node->actual_name) {
400 return -ENOMEM;
401 }
402 }
403 memcpy(node->actual_name, actual_name, namelen + 1);
404 } else {
405 free(node->actual_name);
406 node->actual_name = NULL;
407 }
408 memcpy(node->name, name, namelen + 1);
409 node->namelen = namelen;
410 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700411}
412
Jeff Brown6249b902012-05-26 14:32:54 -0700413static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700414{
Jeff Brown6249b902012-05-26 14:32:54 -0700415 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700416 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700417 } else {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400418 return static_cast<struct node*>(id_to_ptr(nid));
Brian Swetland03ee9472010-08-12 18:01:08 -0700419 }
420}
421
Jeff Brown6249b902012-05-26 14:32:54 -0700422static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
423 char* buf, size_t bufsize)
424{
425 struct node* node = lookup_node_by_id_locked(fuse, nid);
426 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
427 node = NULL;
428 }
429 return node;
430}
431
432static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700433{
434 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700435 /* use exact string comparison, nodes that differ by case
436 * must be considered distinct even if they refer to the same
437 * underlying file as otherwise operations such as "mv x x"
438 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200439 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700440 return node;
441 }
442 }
443 return 0;
444}
445
Jeff Brown6249b902012-05-26 14:32:54 -0700446static struct node* acquire_or_create_child_locked(
447 struct fuse* fuse, struct node* parent,
448 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700449{
Jeff Brown6249b902012-05-26 14:32:54 -0700450 struct node* child = lookup_child_by_name_locked(parent, name);
451 if (child) {
452 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800453 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700454 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800455 }
Jeff Brown6249b902012-05-26 14:32:54 -0700456 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700457}
458
Jeff Brown6249b902012-05-26 14:32:54 -0700459static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700460{
461 struct fuse_out_header hdr;
462 hdr.len = sizeof(hdr);
463 hdr.error = err;
464 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700465 write(fuse->fd, &hdr, sizeof(hdr));
466}
467
Jeff Brown6249b902012-05-26 14:32:54 -0700468static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700469{
470 struct fuse_out_header hdr;
471 struct iovec vec[2];
472 int res;
473
474 hdr.len = len + sizeof(hdr);
475 hdr.error = 0;
476 hdr.unique = unique;
477
478 vec[0].iov_base = &hdr;
479 vec[0].iov_len = sizeof(hdr);
480 vec[1].iov_base = data;
481 vec[1].iov_len = len;
482
483 res = writev(fuse->fd, vec, 2);
484 if (res < 0) {
485 ERROR("*** REPLY FAILED *** %d\n", errno);
486 }
487}
488
Jeff Brown6249b902012-05-26 14:32:54 -0700489static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
490 struct node* parent, const char* name, const char* actual_name,
491 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700492{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700493 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700494 struct fuse_entry_out out;
495 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700496
Jeff Brown6249b902012-05-26 14:32:54 -0700497 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700498 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700499 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700500
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700501 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700502 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
503 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700504 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700505 return -ENOMEM;
506 }
507 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700508 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700509 out.attr_valid = 10;
510 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700511 out.nodeid = node->nid;
512 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700513 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700514 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700515 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700516}
517
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700518static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700519 const char* path)
520{
521 struct fuse_attr_out out;
522 struct stat s;
523
524 if (lstat(path, &s) < 0) {
525 return -errno;
526 }
527 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700528 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700529 out.attr_valid = 10;
530 fuse_reply(fuse, unique, &out, sizeof(out));
531 return NO_STATUS;
532}
533
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700534static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
535 const __u64 child, const char* name) {
536 struct fuse_out_header hdr;
537 struct fuse_notify_delete_out data;
538 struct iovec vec[3];
539 size_t namelen = strlen(name);
540 int res;
541
542 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
543 hdr.error = FUSE_NOTIFY_DELETE;
544 hdr.unique = 0;
545
546 data.parent = parent;
547 data.child = child;
548 data.namelen = namelen;
549 data.padding = 0;
550
551 vec[0].iov_base = &hdr;
552 vec[0].iov_len = sizeof(hdr);
553 vec[1].iov_base = &data;
554 vec[1].iov_len = sizeof(data);
555 vec[2].iov_base = (void*) name;
556 vec[2].iov_len = namelen + 1;
557
558 res = writev(fuse->fd, vec, 3);
559 /* Ignore ENOENT, since other views may not have seen the entry */
560 if (res < 0 && errno != ENOENT) {
561 ERROR("*** NOTIFY FAILED *** %d\n", errno);
562 }
563}
564
Jeff Brown6249b902012-05-26 14:32:54 -0700565static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700566 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700567{
Jeff Brown6249b902012-05-26 14:32:54 -0700568 struct node* parent_node;
569 char parent_path[PATH_MAX];
570 char child_path[PATH_MAX];
571 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700572
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700573 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700574 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
575 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400576 TRACE("[%d] LOOKUP %s @ %" PRIx64 " (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700577 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700578 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700579
580 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
581 child_path, sizeof(child_path), 1))) {
582 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700583 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700584 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700585 return -EACCES;
586 }
587
Jeff Brown6249b902012-05-26 14:32:54 -0700588 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700589}
590
Jeff Brown6249b902012-05-26 14:32:54 -0700591static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700592 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
593{
Jeff Brown6249b902012-05-26 14:32:54 -0700594 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700595
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700596 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700597 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400598 TRACE("[%d] FORGET #%" PRIu64 " @ %" PRIx64 " (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700599 hdr->nodeid, node ? node->name : "?");
600 if (node) {
601 __u64 n = req->nlookup;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400602 while (n) {
603 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700604 release_node_locked(node);
605 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700606 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700607 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700608 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700609}
610
Jeff Brown6249b902012-05-26 14:32:54 -0700611static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700612 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
613{
Jeff Brown6249b902012-05-26 14:32:54 -0700614 struct node* node;
615 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700616
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700617 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700618 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400619 TRACE("[%d] GETATTR flags=%x fh=%" PRIx64 " @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700620 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700621 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700622
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700623 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700624 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700625 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700626 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700627 return -EACCES;
628 }
629
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700630 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700631}
632
Jeff Brown6249b902012-05-26 14:32:54 -0700633static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700634 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
635{
Jeff Brown6249b902012-05-26 14:32:54 -0700636 struct node* node;
637 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700638 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700639
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700640 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700641 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400642 TRACE("[%d] SETATTR fh=%" PRIx64 " valid=%x @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700643 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700644 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700645
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700646 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700647 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700648 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800649
650 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700651 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700652 return -EACCES;
653 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700654
Jeff Brown6249b902012-05-26 14:32:54 -0700655 /* XXX: incomplete implementation on purpose.
656 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700657
Elliott Hughes853574d2014-07-31 12:03:03 -0700658 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700659 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700660 }
661
662 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
663 * are both set, then set it to the current time. Else, set it to the
664 * time specified in the request. Same goes for mtime. Use utimensat(2)
665 * as it allows ATIME and MTIME to be changed independently, and has
666 * nanosecond resolution which fuse also has.
667 */
668 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
669 times[0].tv_nsec = UTIME_OMIT;
670 times[1].tv_nsec = UTIME_OMIT;
671 if (req->valid & FATTR_ATIME) {
672 if (req->valid & FATTR_ATIME_NOW) {
673 times[0].tv_nsec = UTIME_NOW;
674 } else {
675 times[0].tv_sec = req->atime;
676 times[0].tv_nsec = req->atimensec;
677 }
678 }
679 if (req->valid & FATTR_MTIME) {
680 if (req->valid & FATTR_MTIME_NOW) {
681 times[1].tv_nsec = UTIME_NOW;
682 } else {
683 times[1].tv_sec = req->mtime;
684 times[1].tv_nsec = req->mtimensec;
685 }
686 }
Jeff Brown6249b902012-05-26 14:32:54 -0700687 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
688 handler->token, path, times[0].tv_sec, times[1].tv_sec);
689 if (utimensat(-1, path, times, 0) < 0) {
690 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700691 }
692 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700693 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700694}
695
Jeff Brown6249b902012-05-26 14:32:54 -0700696static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700697 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
698{
Jeff Brown6249b902012-05-26 14:32:54 -0700699 struct node* parent_node;
700 char parent_path[PATH_MAX];
701 char child_path[PATH_MAX];
702 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700703
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700704 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700705 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
706 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400707 TRACE("[%d] MKNOD %s 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700708 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700709 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700710
711 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
712 child_path, sizeof(child_path), 1))) {
713 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700714 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700715 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700716 return -EACCES;
717 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700718 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700719 if (mknod(child_path, mode, req->rdev) < 0) {
720 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700721 }
Jeff Brown6249b902012-05-26 14:32:54 -0700722 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700723}
724
Jeff Brown6249b902012-05-26 14:32:54 -0700725static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700726 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
727{
Jeff Brown6249b902012-05-26 14:32:54 -0700728 struct node* parent_node;
729 char parent_path[PATH_MAX];
730 char child_path[PATH_MAX];
731 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700732
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700733 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700734 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
735 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400736 TRACE("[%d] MKDIR %s 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700737 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700738 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700739
740 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
741 child_path, sizeof(child_path), 1))) {
742 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700743 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700744 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700745 return -EACCES;
746 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700747 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700748 if (mkdir(child_path, mode) < 0) {
749 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700750 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700751
752 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
753 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
754 char nomedia[PATH_MAX];
755 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
756 if (touch(nomedia, 0664) != 0) {
757 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
758 return -ENOENT;
759 }
760 }
761 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
762 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700763 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700764 if (touch(nomedia, 0664) != 0) {
765 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
766 return -ENOENT;
767 }
768 }
769
Jeff Brown6249b902012-05-26 14:32:54 -0700770 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700771}
772
Jeff Brown6249b902012-05-26 14:32:54 -0700773static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700774 const struct fuse_in_header* hdr, const char* name)
775{
Jeff Brown6249b902012-05-26 14:32:54 -0700776 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200777 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700778 char parent_path[PATH_MAX];
779 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700780
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700781 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700782 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
783 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400784 TRACE("[%d] UNLINK %s @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700785 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700786 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700787
788 if (!parent_node || !find_file_within(parent_path, name,
789 child_path, sizeof(child_path), 1)) {
790 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700791 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700792 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700793 return -EACCES;
794 }
Jeff Brown6249b902012-05-26 14:32:54 -0700795 if (unlink(child_path) < 0) {
796 return -errno;
797 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700798 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200799 child_node = lookup_child_by_name_locked(parent_node, name);
800 if (child_node) {
801 child_node->deleted = true;
802 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700803 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700804 if (parent_node && child_node) {
805 /* Tell all other views that node is gone */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400806 TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n",
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700807 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
808 if (fuse != fuse->global->fuse_default) {
809 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
810 }
811 if (fuse != fuse->global->fuse_read) {
812 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
813 }
814 if (fuse != fuse->global->fuse_write) {
815 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
816 }
817 }
Jeff Brown6249b902012-05-26 14:32:54 -0700818 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700819}
820
Jeff Brown6249b902012-05-26 14:32:54 -0700821static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700822 const struct fuse_in_header* hdr, const char* name)
823{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200824 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700825 struct node* parent_node;
826 char parent_path[PATH_MAX];
827 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700828
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700829 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700830 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
831 parent_path, sizeof(parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400832 TRACE("[%d] RMDIR %s @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700833 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700834 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700835
836 if (!parent_node || !find_file_within(parent_path, name,
837 child_path, sizeof(child_path), 1)) {
838 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700840 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700841 return -EACCES;
842 }
Jeff Brown6249b902012-05-26 14:32:54 -0700843 if (rmdir(child_path) < 0) {
844 return -errno;
845 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700846 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200847 child_node = lookup_child_by_name_locked(parent_node, name);
848 if (child_node) {
849 child_node->deleted = true;
850 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700851 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700852 if (parent_node && child_node) {
853 /* Tell all other views that node is gone */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400854 TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n",
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700855 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
856 if (fuse != fuse->global->fuse_default) {
857 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
858 }
859 if (fuse != fuse->global->fuse_read) {
860 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
861 }
862 if (fuse != fuse->global->fuse_write) {
863 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
864 }
865 }
Jeff Brown6249b902012-05-26 14:32:54 -0700866 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700867}
868
Jeff Brown6249b902012-05-26 14:32:54 -0700869static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700870 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700871 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700872{
Jeff Brown6249b902012-05-26 14:32:54 -0700873 struct node* old_parent_node;
874 struct node* new_parent_node;
875 struct node* child_node;
876 char old_parent_path[PATH_MAX];
877 char new_parent_path[PATH_MAX];
878 char old_child_path[PATH_MAX];
879 char new_child_path[PATH_MAX];
880 const char* new_actual_name;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400881 int search;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700884 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700885 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
886 old_parent_path, sizeof(old_parent_path));
887 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
888 new_parent_path, sizeof(new_parent_path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400889 TRACE("[%d] RENAME %s->%s @ %" PRIx64 " (%s) -> %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700890 old_name, new_name,
891 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
892 req->newdir, new_parent_node ? new_parent_node->name : "?");
893 if (!old_parent_node || !new_parent_node) {
894 res = -ENOENT;
895 goto lookup_error;
896 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700897 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700898 res = -EACCES;
899 goto lookup_error;
900 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700901 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700902 res = -EACCES;
903 goto lookup_error;
904 }
Jeff Brown6249b902012-05-26 14:32:54 -0700905 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
906 if (!child_node || get_node_path_locked(child_node,
907 old_child_path, sizeof(old_child_path)) < 0) {
908 res = -ENOENT;
909 goto lookup_error;
910 }
911 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700912 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700913
914 /* Special case for renaming a file where destination is same path
915 * differing only by case. In this case we don't want to look for a case
916 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
917 */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400918 search = old_parent_node != new_parent_node
Jeff Brown6249b902012-05-26 14:32:54 -0700919 || strcasecmp(old_name, new_name);
920 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
921 new_child_path, sizeof(new_child_path), search))) {
922 res = -ENOENT;
923 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700924 }
925
Jeff Brown6249b902012-05-26 14:32:54 -0700926 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
927 res = rename(old_child_path, new_child_path);
928 if (res < 0) {
929 res = -errno;
930 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700931 }
932
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700933 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700934 res = rename_node_locked(child_node, new_name, new_actual_name);
935 if (!res) {
936 remove_node_from_parent_locked(child_node);
Jeff Sharkey22b91262015-12-14 11:02:01 -0700937 derive_permissions_locked(fuse, new_parent_node, child_node);
938 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -0700939 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700940 }
Jeff Brown6249b902012-05-26 14:32:54 -0700941 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700942
Jeff Brown6249b902012-05-26 14:32:54 -0700943io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700944 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700945done:
946 release_node_locked(child_node);
947lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700948 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700949 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700950}
951
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700952static int open_flags_to_access_mode(int open_flags) {
953 if ((open_flags & O_ACCMODE) == O_RDONLY) {
954 return R_OK;
955 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
956 return W_OK;
957 } else {
958 /* Probably O_RDRW, but treat as default to be safe */
959 return R_OK | W_OK;
960 }
961}
962
Jeff Brown6249b902012-05-26 14:32:54 -0700963static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700964 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
965{
Jeff Brown6249b902012-05-26 14:32:54 -0700966 struct node* node;
967 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700968 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700971 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700972 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400973 TRACE("[%d] OPEN 0%o @ %" PRIx64 " (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700974 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700975 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700976
977 if (!node) {
978 return -ENOENT;
979 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700980 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700981 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700982 return -EACCES;
983 }
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -0400984 h = static_cast<struct handle*>(malloc(sizeof(*h)));
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700985 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700986 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700987 }
Jeff Brown6249b902012-05-26 14:32:54 -0700988 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700989 h->fd = open(path, req->flags);
990 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700991 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700992 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700993 }
994 out.fh = ptr_to_id(h);
995 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -0800996
997#ifdef FUSE_STACKED_IO
998 out.lower_fd = h->fd;
999#else
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001000 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001001#endif
1002
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001003 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;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001166
1167#ifdef FUSE_STACKED_IO
1168 out.lower_fd = -1;
1169#else
Ken Sumrall3a876882013-08-14 20:02:13 -07001170 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001171#endif
1172
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001173 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001174 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001175}
1176
Jeff Brown6249b902012-05-26 14:32:54 -07001177static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001178 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1179{
1180 char buffer[8192];
1181 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1182 struct dirent *de;
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001183 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001184
1185 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001186 if (req->offset == 0) {
1187 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001188 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 rewinddir(h->d);
1190 }
1191 de = readdir(h->d);
1192 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001193 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001194 }
1195 fde->ino = FUSE_UNKNOWN_INO;
1196 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1197 fde->off = req->offset + 1;
1198 fde->type = de->d_type;
1199 fde->namelen = strlen(de->d_name);
1200 memcpy(fde->name, de->d_name, fde->namelen + 1);
1201 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001202 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1203 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001204}
1205
Jeff Brown6249b902012-05-26 14:32:54 -07001206static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1208{
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001209 struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh));
Jeff Brown6249b902012-05-26 14:32:54 -07001210
1211 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 closedir(h->d);
1213 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001214 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001215}
1216
Jeff Brown6249b902012-05-26 14:32:54 -07001217static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001218 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1219{
1220 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001221 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222
Jeff Brown6249b902012-05-26 14:32:54 -07001223 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1224 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001225
1226 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1227 * defined (fuse version 7.6). The structure is the same from 7.6 through
1228 * 7.22. Beginning with 7.23, the structure increased in size and added
1229 * new parameters.
1230 */
1231 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1232 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1233 req->major, req->minor, FUSE_KERNEL_VERSION);
1234 return -1;
1235 }
1236
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001237 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1238 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001239 fuse_struct_size = sizeof(out);
1240#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1241 /* FUSE_KERNEL_VERSION >= 23. */
1242
1243 /* If the kernel only works on minor revs older than or equal to 22,
1244 * then use the older structure size since this code only uses the 7.22
1245 * version of the structure. */
1246 if (req->minor <= 22) {
1247 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1248 }
1249#endif
1250
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001251 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001252 out.max_readahead = req->max_readahead;
1253 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001254
1255#ifdef FUSE_STACKED_IO
1256 out.flags |= FUSE_STACKED_IO;
1257#endif
1258
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001259 out.max_background = 32;
1260 out.congestion_threshold = 32;
1261 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001262 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001263 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001264}
1265
Jeff Brown6249b902012-05-26 14:32:54 -07001266static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001267 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1268{
Brian Swetland03ee9472010-08-12 18:01:08 -07001269 switch (hdr->opcode) {
1270 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001271 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001272 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001273 }
Jeff Brown84715842012-05-25 14:07:47 -07001274
Brian Swetland03ee9472010-08-12 18:01:08 -07001275 case FUSE_FORGET: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001276 const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001277 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001278 }
Jeff Brown84715842012-05-25 14:07:47 -07001279
Brian Swetland03ee9472010-08-12 18:01:08 -07001280 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001281 const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001282 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001283 }
Jeff Brown84715842012-05-25 14:07:47 -07001284
Brian Swetland03ee9472010-08-12 18:01:08 -07001285 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001286 const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001287 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001288 }
Jeff Brown84715842012-05-25 14:07:47 -07001289
Brian Swetland03ee9472010-08-12 18:01:08 -07001290// case FUSE_READLINK:
1291// case FUSE_SYMLINK:
1292 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001293 const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001294 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001295 return handle_mknod(fuse, handler, hdr, req, name);
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_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001299 const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001300 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001301 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001302 }
Jeff Brown84715842012-05-25 14:07:47 -07001303
Brian Swetland03ee9472010-08-12 18:01:08 -07001304 case FUSE_UNLINK: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001305 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001306 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001307 }
Jeff Brown84715842012-05-25 14:07:47 -07001308
Brian Swetland03ee9472010-08-12 18:01:08 -07001309 case FUSE_RMDIR: { /* bytez[] -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001310 const char *name = static_cast<const char*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001311 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001312 }
Jeff Brown84715842012-05-25 14:07:47 -07001313
Brian Swetland03ee9472010-08-12 18:01:08 -07001314 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001315 const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001316 const char *old_name = ((const char*) data) + sizeof(*req);
1317 const char *new_name = old_name + strlen(old_name) + 1;
1318 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001319 }
Jeff Brown84715842012-05-25 14:07:47 -07001320
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001321// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001322 case FUSE_OPEN: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001323 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001324 return handle_open(fuse, handler, hdr, req);
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_READ: { /* read_in -> byte[] */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001328 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001329 return handle_read(fuse, handler, hdr, req);
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_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001333 const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data);
Jeff Brown84715842012-05-25 14:07:47 -07001334 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001335 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001336 }
Jeff Brown84715842012-05-25 14:07:47 -07001337
Mike Lockwood4553b082010-08-16 14:14:44 -04001338 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001339 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001340 }
Jeff Brown84715842012-05-25 14:07:47 -07001341
Brian Swetland03ee9472010-08-12 18:01:08 -07001342 case FUSE_RELEASE: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001343 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001344 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001345 }
Jeff Brown84715842012-05-25 14:07:47 -07001346
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001347 case FUSE_FSYNC:
1348 case FUSE_FSYNCDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001349 const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001350 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001351 }
1352
Brian Swetland03ee9472010-08-12 18:01:08 -07001353// case FUSE_SETXATTR:
1354// case FUSE_GETXATTR:
1355// case FUSE_LISTXATTR:
1356// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001357 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001358 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001359 }
1360
Brian Swetland03ee9472010-08-12 18:01:08 -07001361 case FUSE_OPENDIR: { /* open_in -> open_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001362 const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001363 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001364 }
Jeff Brown84715842012-05-25 14:07:47 -07001365
Brian Swetland03ee9472010-08-12 18:01:08 -07001366 case FUSE_READDIR: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001367 const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001368 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001369 }
Jeff Brown84715842012-05-25 14:07:47 -07001370
Brian Swetland03ee9472010-08-12 18:01:08 -07001371 case FUSE_RELEASEDIR: { /* release_in -> */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001372 const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001373 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001374 }
Jeff Brown84715842012-05-25 14:07:47 -07001375
Brian Swetland03ee9472010-08-12 18:01:08 -07001376 case FUSE_INIT: { /* init_in -> init_out */
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001377 const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data);
Jeff Brown6249b902012-05-26 14:32:54 -07001378 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001379 }
Jeff Brown84715842012-05-25 14:07:47 -07001380
Brian Swetland03ee9472010-08-12 18:01:08 -07001381 default: {
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001382 TRACE("[%d] NOTIMPL op=%d uniq=%" PRIx64 " nid=%" PRIx64 "\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001383 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1384 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001385 }
Jeff Brown84715842012-05-25 14:07:47 -07001386 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001387}
1388
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001389void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001390{
Jeff Brown6249b902012-05-26 14:32:54 -07001391 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001392 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001393 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1394 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001395 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001396 if (errno == ENODEV) {
1397 ERROR("[%d] someone stole our marbles!\n", handler->token);
1398 exit(2);
1399 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001400 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001401 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001402 }
Jeff Brown84715842012-05-25 14:07:47 -07001403
1404 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001405 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1406 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001407 }
1408
Jorge Lucangeli Obesf08ba052016-07-13 16:52:24 -04001409 const struct fuse_in_header* hdr =
1410 reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer);
Jeff Brown84715842012-05-25 14:07:47 -07001411 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001412 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1413 handler->token, (size_t)len, hdr->len);
1414 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001415 }
1416
Jeff Brown7729d242012-05-25 15:35:28 -07001417 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001418 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001419 __u64 unique = hdr->unique;
1420 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001421
1422 /* We do not access the request again after this point because the underlying
1423 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001424
1425 if (res != NO_STATUS) {
1426 if (res) {
1427 TRACE("[%d] ERROR %d\n", handler->token, res);
1428 }
1429 fuse_status(fuse, unique, res);
1430 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001431 }
1432}