blob: 309184155a5fd5e26a641661456365e9156aee28 [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
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070032#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070033#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070034#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040035#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080036#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070037#include <sys/uio.h>
38#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039
Jeff Sharkey44d63422013-09-12 09:44:48 -070040#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070042#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070044
Brian Swetlandb14a2c62010-08-12 18:21:12 -070045#include <private/android_filesystem_config.h>
46
Brian Swetland03ee9472010-08-12 18:01:08 -070047/* README
48 *
49 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070050 *
Brian Swetland03ee9472010-08-12 18:01:08 -070051 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070052 * directory permissions (all files are given fixed owner, group, and
53 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070054 * changeable, symlinks and hardlinks are not createable, etc.
55 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070057 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070058 * It must be run as root, but will drop to requested UID/GID as soon as it
59 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070060 *
61 * Things I believe to be true:
62 *
63 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
64 * CREAT) must bump that node's refcount
65 * - don't forget that FORGET can forget multiple references (req->nlookup)
66 * - if an op that returns a fuse_entry fails writing the reply to the
67 * kernel, you must rollback the refcount to reflect the reference the
68 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070069 *
70 * This daemon can also derive custom filesystem permissions based on directory
71 * structure when requested. These custom permissions support several features:
72 *
73 * - Apps can access their own files in /Android/data/com.example/ without
74 * requiring any additional GIDs.
75 * - Separate permissions for protecting directories like Pictures and Music.
76 * - Multi-user separation on the same physical device.
Brian Swetland03ee9472010-08-12 18:01:08 -070077 */
78
79#define FUSE_TRACE 0
80
81#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070082#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070083#else
84#define TRACE(x...) do {} while (0)
85#endif
86
Elliott Hughes300d5642014-07-08 13:53:26 -070087#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070088
89#define FUSE_UNKNOWN_INO 0xffffffff
90
Jeff Brown84715842012-05-25 14:07:47 -070091/* Maximum number of bytes to write in one request. */
92#define MAX_WRITE (256 * 1024)
93
94/* Maximum number of bytes to read in one request. */
95#define MAX_READ (128 * 1024)
96
97/* Largest possible request.
98 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
99 * the largest possible data payload. */
100#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
101
Jeff Brown6249b902012-05-26 14:32:54 -0700102/* Pseudo-error constant used to indicate that no fuse status is needed
103 * or that a reply has already been written. */
104#define NO_STATUS 1
105
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700106/* Path to system-provided mapping of package name to appIds */
107static const char* const kPackagesListFile = "/data/system/packages.list";
108
109/* Supplementary groups to execute with */
110static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
111
112/* Permission mode for a specific node. Controls how file permissions
113 * are derived for children nodes. */
114typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700115 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700116 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700117 /* This node is one level above a normal root; used for legacy layouts
118 * which use the first level to represent user_id. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700119 PERM_PRE_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700120 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700121 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700122 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700123 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700124 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700126 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700127 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700128 /* This node is "/Android/media" */
129 PERM_ANDROID_MEDIA,
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700130} perm_t;
131
Brian Swetland03ee9472010-08-12 18:01:08 -0700132struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700133 int fd;
134};
135
136struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700137 DIR *d;
138};
139
140struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700141 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700142 __u64 nid;
143 __u64 gen;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000144 /*
145 * The inode number for this FUSE node. Note that this isn't stable across
146 * multiple invocations of the FUSE daemon.
147 */
148 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700149
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700150 /* State derived based on current position in hierarchy. */
151 perm_t perm;
152 userid_t userid;
153 uid_t uid;
154 gid_t gid;
155 mode_t mode;
156
Paul Eastham11ccdb32010-10-14 11:04:26 -0700157 struct node *next; /* per-dir sibling list */
158 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700159 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700160
Jeff Brown6249b902012-05-26 14:32:54 -0700161 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700162 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800163 /* If non-null, this is the real name of the file in the underlying storage.
164 * This may differ from the field "name" only by case.
165 * strlen(actual_name) will always equal strlen(name), so it is safe to use
166 * namelen for both fields.
167 */
168 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700169
170 /* If non-null, an exact underlying path that should be grafted into this
171 * position. Used to support things like OBB. */
172 char* graft_path;
173 size_t graft_pathlen;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200174
175 bool deleted;
Brian Swetland03ee9472010-08-12 18:01:08 -0700176};
177
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700178static int str_hash(void *key) {
179 return hashmapHash(key, strlen(key));
180}
181
Jeff Sharkey44d63422013-09-12 09:44:48 -0700182/** Test if two string keys are equal ignoring case */
183static bool str_icase_equals(void *keyA, void *keyB) {
184 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700185}
186
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700187static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800188 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700189}
190
191static bool int_equals(void *keyA, void *keyB) {
192 return keyA == keyB;
193}
194
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700195/* Global data for all FUSE mounts */
196struct fuse_global {
Jeff Brown6249b902012-05-26 14:32:54 -0700197 pthread_mutex_t lock;
198
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700199 uid_t uid;
200 gid_t gid;
201 bool multi_user;
202
203 Hashmap* package_to_appid;
204};
205
206/* Single FUSE mount */
207struct fuse {
208 struct fuse_global* global;
209
210 char source_path[PATH_MAX];
211 char dest_path[PATH_MAX];
212 char obb_path[PATH_MAX];
213
Brian Swetland03ee9472010-08-12 18:01:08 -0700214 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700215 int fd;
Brian Swetland03ee9472010-08-12 18:01:08 -0700216 struct node root;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700217 gid_t gid;
218 mode_t mask;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700219
Narayan Kamathfaa09352015-01-13 18:21:10 +0000220 /* Used to allocate unique inode numbers for fuse nodes. We use
221 * a simple counter based scheme where inode numbers from deleted
222 * nodes aren't reused. Note that inode allocations are not stable
223 * across multiple invocation of the sdcard daemon, but that shouldn't
224 * be a huge problem in practice.
225 *
226 * Note that we restrict inodes to 32 bit unsigned integers to prevent
227 * truncation on 32 bit processes when unsigned long long stat.st_ino is
228 * assigned to an unsigned long ino_t type in an LP32 process.
229 *
230 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
231 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
232 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
233 * in fs/fuse/inode.c).
234 *
235 * Accesses must be guarded by |lock|.
236 */
237 __u32 inode_ctr;
Brian Swetland03ee9472010-08-12 18:01:08 -0700238};
239
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700240/* Private data used by a single FUSE handler */
Jeff Brown7729d242012-05-25 15:35:28 -0700241struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700242 struct fuse* fuse;
243 int token;
244
Jeff Brown7729d242012-05-25 15:35:28 -0700245 /* To save memory, we never use the contents of the request buffer and the read
246 * buffer at the same time. This allows us to share the underlying storage. */
247 union {
248 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800249 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700250 };
251};
Brian Swetland03ee9472010-08-12 18:01:08 -0700252
Jeff Brown6249b902012-05-26 14:32:54 -0700253static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700254{
Jeff Brown6249b902012-05-26 14:32:54 -0700255 return (void *) (uintptr_t) nid;
256}
Brian Swetland03ee9472010-08-12 18:01:08 -0700257
Jeff Brown6249b902012-05-26 14:32:54 -0700258static inline __u64 ptr_to_id(void *ptr)
259{
260 return (__u64) (uintptr_t) ptr;
261}
Brian Swetland03ee9472010-08-12 18:01:08 -0700262
Jeff Brown6249b902012-05-26 14:32:54 -0700263static void acquire_node_locked(struct node* node)
264{
265 node->refcount++;
266 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
267}
268
269static void remove_node_from_parent_locked(struct node* node);
270
271static void release_node_locked(struct node* node)
272{
273 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
274 if (node->refcount > 0) {
275 node->refcount--;
276 if (!node->refcount) {
277 TRACE("DESTROY %p (%s)\n", node, node->name);
278 remove_node_from_parent_locked(node);
279
280 /* TODO: remove debugging - poison memory */
281 memset(node->name, 0xef, node->namelen);
282 free(node->name);
283 free(node->actual_name);
284 memset(node, 0xfc, sizeof(*node));
285 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800286 }
Jeff Brown6249b902012-05-26 14:32:54 -0700287 } else {
288 ERROR("Zero refcnt %p\n", node);
289 }
290}
291
292static void add_node_to_parent_locked(struct node *node, struct node *parent) {
293 node->parent = parent;
294 node->next = parent->child;
295 parent->child = node;
296 acquire_node_locked(parent);
297}
298
299static void remove_node_from_parent_locked(struct node* node)
300{
301 if (node->parent) {
302 if (node->parent->child == node) {
303 node->parent->child = node->parent->child->next;
304 } else {
305 struct node *node2;
306 node2 = node->parent->child;
307 while (node2->next != node)
308 node2 = node2->next;
309 node2->next = node->next;
310 }
311 release_node_locked(node->parent);
312 node->parent = NULL;
313 node->next = NULL;
314 }
315}
316
317/* Gets the absolute path to a node into the provided buffer.
318 *
319 * Populates 'buf' with the path and returns the length of the path on success,
320 * or returns -1 if the path is too long for the provided buffer.
321 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700322static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
323 const char* name;
324 size_t namelen;
325 if (node->graft_path) {
326 name = node->graft_path;
327 namelen = node->graft_pathlen;
328 } else if (node->actual_name) {
329 name = node->actual_name;
330 namelen = node->namelen;
331 } else {
332 name = node->name;
333 namelen = node->namelen;
334 }
335
Jeff Brown6249b902012-05-26 14:32:54 -0700336 if (bufsize < namelen + 1) {
337 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700338 }
339
Jeff Brown6249b902012-05-26 14:32:54 -0700340 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700341 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700342 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
343 if (pathlen < 0) {
344 return -1;
345 }
346 buf[pathlen++] = '/';
347 }
348
Jeff Brown6249b902012-05-26 14:32:54 -0700349 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
350 return pathlen + namelen;
351}
352
353/* Finds the absolute path of a file within a given directory.
354 * Performs a case-insensitive search for the file and sets the buffer to the path
355 * of the first matching file. If 'search' is zero or if no match is found, sets
356 * the buffer to the path that the file would have, assuming the name were case-sensitive.
357 *
358 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
359 * or returns NULL if the path is too long for the provided buffer.
360 */
361static char* find_file_within(const char* path, const char* name,
362 char* buf, size_t bufsize, int search)
363{
364 size_t pathlen = strlen(path);
365 size_t namelen = strlen(name);
366 size_t childlen = pathlen + namelen + 1;
367 char* actual;
368
369 if (bufsize <= childlen) {
370 return NULL;
371 }
372
373 memcpy(buf, path, pathlen);
374 buf[pathlen] = '/';
375 actual = buf + pathlen + 1;
376 memcpy(actual, name, namelen + 1);
377
378 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800379 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700380 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800381 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700382 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700383 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800384 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800385 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700386 if (!strcasecmp(entry->d_name, name)) {
387 /* we have a match - replace the name, don't need to copy the null again */
388 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800389 break;
390 }
391 }
392 closedir(dir);
393 }
Jeff Brown6249b902012-05-26 14:32:54 -0700394 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800395}
396
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700397static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800398{
Narayan Kamathfaa09352015-01-13 18:21:10 +0000399 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700400 attr->size = s->st_size;
401 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800402 attr->atime = s->st_atim.tv_sec;
403 attr->mtime = s->st_mtim.tv_sec;
404 attr->ctime = s->st_ctim.tv_sec;
405 attr->atimensec = s->st_atim.tv_nsec;
406 attr->mtimensec = s->st_mtim.tv_nsec;
407 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700408 attr->mode = s->st_mode;
409 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700410
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700411 attr->uid = node->uid;
412 attr->gid = node->gid;
413
414 /* Filter requested mode based on underlying file, and
415 * pass through file type. */
416 int owner_mode = s->st_mode & 0700;
417 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
418 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
419}
420
Jeff Sharkey44d63422013-09-12 09:44:48 -0700421static int touch(char* path, mode_t mode) {
422 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
423 if (fd == -1) {
424 if (errno == EEXIST) {
425 return 0;
426 } else {
427 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
428 return -1;
429 }
430 }
431 close(fd);
432 return 0;
433}
434
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700435static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
436 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700437 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700438
439 /* By default, each node inherits from its parent */
440 node->perm = PERM_INHERIT;
441 node->userid = parent->userid;
442 node->uid = parent->uid;
443 node->gid = parent->gid;
444 node->mode = parent->mode;
445
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700446 /* Derive custom permissions based on parent and current node */
447 switch (parent->perm) {
448 case PERM_INHERIT:
449 /* Already inherited above */
450 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700451 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700452 /* Legacy internal layout places users at top level */
453 node->perm = PERM_ROOT;
454 node->userid = strtoul(node->name, NULL, 10);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700455 node->gid = multiuser_get_uid(node->userid, fuse->gid);
Jeff Sharkeyfc000482015-03-16 10:17:47 -0700456 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700457 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700458 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700459 /* Assume masked off by default. */
460 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700461 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700462 /* App-specific directories inside; let anyone traverse */
463 node->perm = PERM_ANDROID;
464 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700465 }
466 break;
467 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700468 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700469 /* App-specific directories inside; let anyone traverse */
470 node->perm = PERM_ANDROID_DATA;
471 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700472 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700473 /* App-specific directories inside; let anyone traverse */
474 node->perm = PERM_ANDROID_OBB;
475 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700476 /* Single OBB directory is always shared */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700477 node->graft_path = fuse->obb_path;
478 node->graft_pathlen = strlen(fuse->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700479 } else if (!strcasecmp(node->name, "media")) {
480 /* App-specific directories inside; let anyone traverse */
481 node->perm = PERM_ANDROID_MEDIA;
482 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700483 }
484 break;
485 case PERM_ANDROID_DATA:
486 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700487 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700488 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700489 if (appid != 0) {
490 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700491 }
492 node->mode = 0770;
493 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700494 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700495
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700496 node->mode = node->mode & ~fuse->mask;
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700497}
498
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700499/* Kernel has already enforced everything we returned through
500 * derive_permissions_locked(), so this is used to lock down access
501 * even further, such as enforcing that apps hold sdcard_rw. */
502static bool check_caller_access_to_name(struct fuse* fuse,
503 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700504 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700505 /* Always block security-sensitive files at root */
506 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700507 if (!strcasecmp(name, "autorun.inf")
508 || !strcasecmp(name, ".android_secure")
509 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700510 return false;
511 }
512 }
513
Jeff Sharkey44d63422013-09-12 09:44:48 -0700514 /* Root always has access; access for any other UIDs should always
515 * be controlled through packages.list. */
516 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700517 return true;
518 }
519
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700520 /* No extra permissions to enforce */
521 return true;
522}
523
524static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700525 const struct fuse_in_header *hdr, const struct node* node, int mode) {
526 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700527}
528
Jeff Brown6249b902012-05-26 14:32:54 -0700529struct node *create_node_locked(struct fuse* fuse,
530 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700531{
532 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700533 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700534
Narayan Kamathfaa09352015-01-13 18:21:10 +0000535 // Detect overflows in the inode counter. "4 billion nodes should be enough
536 // for everybody".
537 if (fuse->inode_ctr == 0) {
538 ERROR("No more inode numbers available");
539 return NULL;
540 }
541
Paul Eastham11ccdb32010-10-14 11:04:26 -0700542 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700543 if (!node) {
544 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700545 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700546 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700547 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700548 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700549 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700550 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700551 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700552 if (strcmp(name, actual_name)) {
553 node->actual_name = malloc(namelen + 1);
554 if (!node->actual_name) {
555 free(node->name);
556 free(node);
557 return NULL;
558 }
559 memcpy(node->actual_name, actual_name, namelen + 1);
560 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700561 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700562 node->nid = ptr_to_id(node);
Narayan Kamathfaa09352015-01-13 18:21:10 +0000563 node->ino = fuse->inode_ctr++;
Jeff Brown6249b902012-05-26 14:32:54 -0700564 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700565
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200566 node->deleted = false;
567
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700568 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700569 acquire_node_locked(node);
570 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700571 return node;
572}
573
Jeff Brown6249b902012-05-26 14:32:54 -0700574static int rename_node_locked(struct node *node, const char *name,
575 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700576{
Jeff Brown6249b902012-05-26 14:32:54 -0700577 size_t namelen = strlen(name);
578 int need_actual_name = strcmp(name, actual_name);
579
580 /* make the storage bigger without actually changing the name
581 * in case an error occurs part way */
582 if (namelen > node->namelen) {
583 char* new_name = realloc(node->name, namelen + 1);
584 if (!new_name) {
585 return -ENOMEM;
586 }
587 node->name = new_name;
588 if (need_actual_name && node->actual_name) {
589 char* new_actual_name = realloc(node->actual_name, namelen + 1);
590 if (!new_actual_name) {
591 return -ENOMEM;
592 }
593 node->actual_name = new_actual_name;
594 }
595 }
596
597 /* update the name, taking care to allocate storage before overwriting the old name */
598 if (need_actual_name) {
599 if (!node->actual_name) {
600 node->actual_name = malloc(namelen + 1);
601 if (!node->actual_name) {
602 return -ENOMEM;
603 }
604 }
605 memcpy(node->actual_name, actual_name, namelen + 1);
606 } else {
607 free(node->actual_name);
608 node->actual_name = NULL;
609 }
610 memcpy(node->name, name, namelen + 1);
611 node->namelen = namelen;
612 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700613}
614
Jeff Brown6249b902012-05-26 14:32:54 -0700615static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700616{
Jeff Brown6249b902012-05-26 14:32:54 -0700617 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700618 return &fuse->root;
619 } else {
620 return id_to_ptr(nid);
621 }
622}
623
Jeff Brown6249b902012-05-26 14:32:54 -0700624static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
625 char* buf, size_t bufsize)
626{
627 struct node* node = lookup_node_by_id_locked(fuse, nid);
628 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
629 node = NULL;
630 }
631 return node;
632}
633
634static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700635{
636 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700637 /* use exact string comparison, nodes that differ by case
638 * must be considered distinct even if they refer to the same
639 * underlying file as otherwise operations such as "mv x x"
640 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200641 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700642 return node;
643 }
644 }
645 return 0;
646}
647
Jeff Brown6249b902012-05-26 14:32:54 -0700648static struct node* acquire_or_create_child_locked(
649 struct fuse* fuse, struct node* parent,
650 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700651{
Jeff Brown6249b902012-05-26 14:32:54 -0700652 struct node* child = lookup_child_by_name_locked(parent, name);
653 if (child) {
654 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800655 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700656 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800657 }
Jeff Brown6249b902012-05-26 14:32:54 -0700658 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700659}
660
Jeff Brown6249b902012-05-26 14:32:54 -0700661static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700662{
663 struct fuse_out_header hdr;
664 hdr.len = sizeof(hdr);
665 hdr.error = err;
666 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700667 write(fuse->fd, &hdr, sizeof(hdr));
668}
669
Jeff Brown6249b902012-05-26 14:32:54 -0700670static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700671{
672 struct fuse_out_header hdr;
673 struct iovec vec[2];
674 int res;
675
676 hdr.len = len + sizeof(hdr);
677 hdr.error = 0;
678 hdr.unique = unique;
679
680 vec[0].iov_base = &hdr;
681 vec[0].iov_len = sizeof(hdr);
682 vec[1].iov_base = data;
683 vec[1].iov_len = len;
684
685 res = writev(fuse->fd, vec, 2);
686 if (res < 0) {
687 ERROR("*** REPLY FAILED *** %d\n", errno);
688 }
689}
690
Jeff Brown6249b902012-05-26 14:32:54 -0700691static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
692 struct node* parent, const char* name, const char* actual_name,
693 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700694{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700695 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700696 struct fuse_entry_out out;
697 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700698
Jeff Brown6249b902012-05-26 14:32:54 -0700699 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700700 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700701 }
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 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
705 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700706 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700707 return -ENOMEM;
708 }
709 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700710 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700711 out.attr_valid = 10;
712 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700713 out.nodeid = node->nid;
714 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700715 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700716 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700717 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700718}
719
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700720static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700721 const char* path)
722{
723 struct fuse_attr_out out;
724 struct stat s;
725
726 if (lstat(path, &s) < 0) {
727 return -errno;
728 }
729 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700730 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700731 out.attr_valid = 10;
732 fuse_reply(fuse, unique, &out, sizeof(out));
733 return NO_STATUS;
734}
735
736static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700737 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700738{
Jeff Brown6249b902012-05-26 14:32:54 -0700739 struct node* parent_node;
740 char parent_path[PATH_MAX];
741 char child_path[PATH_MAX];
742 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700743
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700744 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700745 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
746 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100747 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700748 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700749 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700750
751 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
752 child_path, sizeof(child_path), 1))) {
753 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700754 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700755 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700756 return -EACCES;
757 }
758
Jeff Brown6249b902012-05-26 14:32:54 -0700759 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700760}
761
Jeff Brown6249b902012-05-26 14:32:54 -0700762static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700763 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
764{
Jeff Brown6249b902012-05-26 14:32:54 -0700765 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700766
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700767 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700768 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100769 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700770 hdr->nodeid, node ? node->name : "?");
771 if (node) {
772 __u64 n = req->nlookup;
773 while (n--) {
774 release_node_locked(node);
775 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700776 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700777 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700778 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700779}
780
Jeff Brown6249b902012-05-26 14:32:54 -0700781static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700782 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
783{
Jeff Brown6249b902012-05-26 14:32:54 -0700784 struct node* node;
785 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700786
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700787 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700788 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100789 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700790 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700791 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700792
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700793 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700794 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700795 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700796 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700797 return -EACCES;
798 }
799
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700800 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700801}
802
Jeff Brown6249b902012-05-26 14:32:54 -0700803static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700804 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
805{
Jeff Brown6249b902012-05-26 14:32:54 -0700806 struct node* node;
807 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700808 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700809
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700810 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700811 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100812 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700813 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700814 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700815
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700816 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700817 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800819
820 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700821 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700822 return -EACCES;
823 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700824
Jeff Brown6249b902012-05-26 14:32:54 -0700825 /* XXX: incomplete implementation on purpose.
826 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700827
Elliott Hughes853574d2014-07-31 12:03:03 -0700828 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700829 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700830 }
831
832 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
833 * are both set, then set it to the current time. Else, set it to the
834 * time specified in the request. Same goes for mtime. Use utimensat(2)
835 * as it allows ATIME and MTIME to be changed independently, and has
836 * nanosecond resolution which fuse also has.
837 */
838 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
839 times[0].tv_nsec = UTIME_OMIT;
840 times[1].tv_nsec = UTIME_OMIT;
841 if (req->valid & FATTR_ATIME) {
842 if (req->valid & FATTR_ATIME_NOW) {
843 times[0].tv_nsec = UTIME_NOW;
844 } else {
845 times[0].tv_sec = req->atime;
846 times[0].tv_nsec = req->atimensec;
847 }
848 }
849 if (req->valid & FATTR_MTIME) {
850 if (req->valid & FATTR_MTIME_NOW) {
851 times[1].tv_nsec = UTIME_NOW;
852 } else {
853 times[1].tv_sec = req->mtime;
854 times[1].tv_nsec = req->mtimensec;
855 }
856 }
Jeff Brown6249b902012-05-26 14:32:54 -0700857 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
858 handler->token, path, times[0].tv_sec, times[1].tv_sec);
859 if (utimensat(-1, path, times, 0) < 0) {
860 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861 }
862 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700863 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700864}
865
Jeff Brown6249b902012-05-26 14:32:54 -0700866static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700867 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
868{
Jeff Brown6249b902012-05-26 14:32:54 -0700869 struct node* parent_node;
870 char parent_path[PATH_MAX];
871 char child_path[PATH_MAX];
872 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700873
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700874 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700875 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
876 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100877 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700878 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700879 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700880
881 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
882 child_path, sizeof(child_path), 1))) {
883 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700884 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700885 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700886 return -EACCES;
887 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700889 if (mknod(child_path, mode, req->rdev) < 0) {
890 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700891 }
Jeff Brown6249b902012-05-26 14:32:54 -0700892 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700893}
894
Jeff Brown6249b902012-05-26 14:32:54 -0700895static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700896 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
897{
Jeff Brown6249b902012-05-26 14:32:54 -0700898 struct node* parent_node;
899 char parent_path[PATH_MAX];
900 char child_path[PATH_MAX];
901 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700902
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700903 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700904 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
905 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100906 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700907 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700908 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700909
910 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
911 child_path, sizeof(child_path), 1))) {
912 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700913 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700914 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700915 return -EACCES;
916 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700917 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700918 if (mkdir(child_path, mode) < 0) {
919 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700920 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700921
922 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
923 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
924 char nomedia[PATH_MAX];
925 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
926 if (touch(nomedia, 0664) != 0) {
927 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
928 return -ENOENT;
929 }
930 }
931 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
932 char nomedia[PATH_MAX];
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700933 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700934 if (touch(nomedia, 0664) != 0) {
935 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
936 return -ENOENT;
937 }
938 }
939
Jeff Brown6249b902012-05-26 14:32:54 -0700940 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941}
942
Jeff Brown6249b902012-05-26 14:32:54 -0700943static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700944 const struct fuse_in_header* hdr, const char* name)
945{
Jeff Brown6249b902012-05-26 14:32:54 -0700946 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200947 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700948 char parent_path[PATH_MAX];
949 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700950
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700951 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700952 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
953 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100954 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700955 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700956 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700957
958 if (!parent_node || !find_file_within(parent_path, name,
959 child_path, sizeof(child_path), 1)) {
960 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700961 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700962 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700963 return -EACCES;
964 }
Jeff Brown6249b902012-05-26 14:32:54 -0700965 if (unlink(child_path) < 0) {
966 return -errno;
967 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700968 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200969 child_node = lookup_child_by_name_locked(parent_node, name);
970 if (child_node) {
971 child_node->deleted = true;
972 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700973 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700974 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700975}
976
Jeff Brown6249b902012-05-26 14:32:54 -0700977static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700978 const struct fuse_in_header* hdr, const char* name)
979{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200980 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700981 struct node* parent_node;
982 char parent_path[PATH_MAX];
983 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700985 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700986 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
987 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100988 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700989 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700990 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700991
992 if (!parent_node || !find_file_within(parent_path, name,
993 child_path, sizeof(child_path), 1)) {
994 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700995 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700996 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700997 return -EACCES;
998 }
Jeff Brown6249b902012-05-26 14:32:54 -0700999 if (rmdir(child_path) < 0) {
1000 return -errno;
1001 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001002 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001003 child_node = lookup_child_by_name_locked(parent_node, name);
1004 if (child_node) {
1005 child_node->deleted = true;
1006 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001007 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001008 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001009}
1010
Jeff Brown6249b902012-05-26 14:32:54 -07001011static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001012 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001013 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001014{
Jeff Brown6249b902012-05-26 14:32:54 -07001015 struct node* old_parent_node;
1016 struct node* new_parent_node;
1017 struct node* child_node;
1018 char old_parent_path[PATH_MAX];
1019 char new_parent_path[PATH_MAX];
1020 char old_child_path[PATH_MAX];
1021 char new_child_path[PATH_MAX];
1022 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001024
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001025 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001026 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1027 old_parent_path, sizeof(old_parent_path));
1028 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1029 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001030 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001031 old_name, new_name,
1032 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1033 req->newdir, new_parent_node ? new_parent_node->name : "?");
1034 if (!old_parent_node || !new_parent_node) {
1035 res = -ENOENT;
1036 goto lookup_error;
1037 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001038 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001039 res = -EACCES;
1040 goto lookup_error;
1041 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001042 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001043 res = -EACCES;
1044 goto lookup_error;
1045 }
Jeff Brown6249b902012-05-26 14:32:54 -07001046 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1047 if (!child_node || get_node_path_locked(child_node,
1048 old_child_path, sizeof(old_child_path)) < 0) {
1049 res = -ENOENT;
1050 goto lookup_error;
1051 }
1052 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001053 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001054
1055 /* Special case for renaming a file where destination is same path
1056 * differing only by case. In this case we don't want to look for a case
1057 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1058 */
1059 int search = old_parent_node != new_parent_node
1060 || strcasecmp(old_name, new_name);
1061 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1062 new_child_path, sizeof(new_child_path), search))) {
1063 res = -ENOENT;
1064 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001065 }
1066
Jeff Brown6249b902012-05-26 14:32:54 -07001067 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1068 res = rename(old_child_path, new_child_path);
1069 if (res < 0) {
1070 res = -errno;
1071 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001072 }
1073
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001074 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001075 res = rename_node_locked(child_node, new_name, new_actual_name);
1076 if (!res) {
1077 remove_node_from_parent_locked(child_node);
1078 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001079 }
Jeff Brown6249b902012-05-26 14:32:54 -07001080 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001081
Jeff Brown6249b902012-05-26 14:32:54 -07001082io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001083 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001084done:
1085 release_node_locked(child_node);
1086lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001087 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001088 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089}
1090
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001091static int open_flags_to_access_mode(int open_flags) {
1092 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1093 return R_OK;
1094 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1095 return W_OK;
1096 } else {
1097 /* Probably O_RDRW, but treat as default to be safe */
1098 return R_OK | W_OK;
1099 }
1100}
1101
Jeff Brown6249b902012-05-26 14:32:54 -07001102static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001103 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1104{
Jeff Brown6249b902012-05-26 14:32:54 -07001105 struct node* node;
1106 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001108 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001109
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001110 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001111 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001112 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001113 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001114 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001115
1116 if (!node) {
1117 return -ENOENT;
1118 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001119 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001120 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001121 return -EACCES;
1122 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001123 h = malloc(sizeof(*h));
1124 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001125 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001126 }
Jeff Brown6249b902012-05-26 14:32:54 -07001127 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001128 h->fd = open(path, req->flags);
1129 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001130 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001131 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001132 }
1133 out.fh = ptr_to_id(h);
1134 out.open_flags = 0;
1135 out.padding = 0;
1136 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001137 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001138}
1139
Jeff Brown6249b902012-05-26 14:32:54 -07001140static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001141 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1142{
1143 struct handle *h = id_to_ptr(req->fh);
1144 __u64 unique = hdr->unique;
1145 __u32 size = req->size;
1146 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001147 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001148 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001149
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1151 * overlaps the request buffer and will clobber data in the request. This
1152 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001153
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001154 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1155 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001156 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001157 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001159 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001161 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001162 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001163 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001164 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165}
1166
Jeff Brown6249b902012-05-26 14:32:54 -07001167static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001168 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1169 const void* buffer)
1170{
1171 struct fuse_write_out out;
1172 struct handle *h = id_to_ptr(req->fh);
1173 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001174 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001175
Arpad Horvath49e93442014-02-18 10:18:25 +01001176 if (req->flags & O_DIRECT) {
1177 memcpy(aligned_buffer, buffer, req->size);
1178 buffer = (const __u8*) aligned_buffer;
1179 }
Jeff Brown6249b902012-05-26 14:32:54 -07001180
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001181 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001182 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183 res = pwrite64(h->fd, buffer, req->size, req->offset);
1184 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001185 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001186 }
1187 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001188 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001190 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001191}
1192
Jeff Brown6249b902012-05-26 14:32:54 -07001193static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001194 const struct fuse_in_header* hdr)
1195{
Jeff Brown6249b902012-05-26 14:32:54 -07001196 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001197 struct statfs stat;
1198 struct fuse_statfs_out out;
1199 int res;
1200
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001201 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001202 TRACE("[%d] STATFS\n", handler->token);
1203 res = get_node_path_locked(&fuse->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001204 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001205 if (res < 0) {
1206 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 }
Jeff Brown6249b902012-05-26 14:32:54 -07001208 if (statfs(fuse->root.name, &stat) < 0) {
1209 return -errno;
1210 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211 memset(&out, 0, sizeof(out));
1212 out.st.blocks = stat.f_blocks;
1213 out.st.bfree = stat.f_bfree;
1214 out.st.bavail = stat.f_bavail;
1215 out.st.files = stat.f_files;
1216 out.st.ffree = stat.f_ffree;
1217 out.st.bsize = stat.f_bsize;
1218 out.st.namelen = stat.f_namelen;
1219 out.st.frsize = stat.f_frsize;
1220 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001221 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222}
1223
Jeff Brown6249b902012-05-26 14:32:54 -07001224static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1226{
1227 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001228
1229 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001230 close(h->fd);
1231 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001232 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001233}
1234
Jeff Brown6249b902012-05-26 14:32:54 -07001235static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001236 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1237{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001238 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1239 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001240
Elliott Hughesf6d67372014-07-08 14:38:26 -07001241 int fd = -1;
1242 if (is_dir) {
1243 struct dirhandle *dh = id_to_ptr(req->fh);
1244 fd = dirfd(dh->d);
1245 } else {
1246 struct handle *h = id_to_ptr(req->fh);
1247 fd = h->fd;
1248 }
1249
1250 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1251 is_dir ? "FSYNCDIR" : "FSYNC",
1252 id_to_ptr(req->fh), fd, is_data_sync);
1253 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1254 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001255 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001256 }
Jeff Brown6249b902012-05-26 14:32:54 -07001257 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001258}
1259
Jeff Brown6249b902012-05-26 14:32:54 -07001260static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001261 const struct fuse_in_header* hdr)
1262{
Jeff Brown6249b902012-05-26 14:32:54 -07001263 TRACE("[%d] FLUSH\n", handler->token);
1264 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001265}
1266
Jeff Brown6249b902012-05-26 14:32:54 -07001267static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001268 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1269{
Jeff Brown6249b902012-05-26 14:32:54 -07001270 struct node* node;
1271 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001273 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001275 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001276 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001277 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001278 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001279 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001280
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001281 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001282 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001284 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001285 return -EACCES;
1286 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001287 h = malloc(sizeof(*h));
1288 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001289 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001290 }
Jeff Brown6249b902012-05-26 14:32:54 -07001291 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001292 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001293 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001294 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001295 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001296 }
1297 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001298 out.open_flags = 0;
1299 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001300 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001301 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001302}
1303
Jeff Brown6249b902012-05-26 14:32:54 -07001304static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001305 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1306{
1307 char buffer[8192];
1308 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1309 struct dirent *de;
1310 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001311
1312 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001313 if (req->offset == 0) {
1314 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001315 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001316 rewinddir(h->d);
1317 }
1318 de = readdir(h->d);
1319 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001320 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001321 }
1322 fde->ino = FUSE_UNKNOWN_INO;
1323 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1324 fde->off = req->offset + 1;
1325 fde->type = de->d_type;
1326 fde->namelen = strlen(de->d_name);
1327 memcpy(fde->name, de->d_name, fde->namelen + 1);
1328 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001329 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1330 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001331}
1332
Jeff Brown6249b902012-05-26 14:32:54 -07001333static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001334 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1335{
1336 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001337
1338 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001339 closedir(h->d);
1340 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001341 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001342}
1343
Jeff Brown6249b902012-05-26 14:32:54 -07001344static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001345 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1346{
1347 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001348 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349
Jeff Brown6249b902012-05-26 14:32:54 -07001350 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1351 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001352
1353 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1354 * defined (fuse version 7.6). The structure is the same from 7.6 through
1355 * 7.22. Beginning with 7.23, the structure increased in size and added
1356 * new parameters.
1357 */
1358 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1359 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1360 req->major, req->minor, FUSE_KERNEL_VERSION);
1361 return -1;
1362 }
1363
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001364 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1365 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001366 fuse_struct_size = sizeof(out);
1367#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1368 /* FUSE_KERNEL_VERSION >= 23. */
1369
1370 /* If the kernel only works on minor revs older than or equal to 22,
1371 * then use the older structure size since this code only uses the 7.22
1372 * version of the structure. */
1373 if (req->minor <= 22) {
1374 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1375 }
1376#endif
1377
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001379 out.max_readahead = req->max_readahead;
1380 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1381 out.max_background = 32;
1382 out.congestion_threshold = 32;
1383 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001384 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001385 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001386}
1387
Jeff Brown6249b902012-05-26 14:32:54 -07001388static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001389 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1390{
Brian Swetland03ee9472010-08-12 18:01:08 -07001391 switch (hdr->opcode) {
1392 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001393 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001394 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001395 }
Jeff Brown84715842012-05-25 14:07:47 -07001396
Brian Swetland03ee9472010-08-12 18:01:08 -07001397 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001398 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001399 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001400 }
Jeff Brown84715842012-05-25 14:07:47 -07001401
Brian Swetland03ee9472010-08-12 18:01:08 -07001402 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001403 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001404 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001405 }
Jeff Brown84715842012-05-25 14:07:47 -07001406
Brian Swetland03ee9472010-08-12 18:01:08 -07001407 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001408 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001409 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001410 }
Jeff Brown84715842012-05-25 14:07:47 -07001411
Brian Swetland03ee9472010-08-12 18:01:08 -07001412// case FUSE_READLINK:
1413// case FUSE_SYMLINK:
1414 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001415 const struct fuse_mknod_in *req = data;
1416 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001417 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001418 }
Jeff Brown84715842012-05-25 14:07:47 -07001419
Brian Swetland03ee9472010-08-12 18:01:08 -07001420 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001421 const struct fuse_mkdir_in *req = data;
1422 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001423 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001424 }
Jeff Brown84715842012-05-25 14:07:47 -07001425
Brian Swetland03ee9472010-08-12 18:01:08 -07001426 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001427 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001428 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001429 }
Jeff Brown84715842012-05-25 14:07:47 -07001430
Brian Swetland03ee9472010-08-12 18:01:08 -07001431 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001432 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001433 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001434 }
Jeff Brown84715842012-05-25 14:07:47 -07001435
Brian Swetland03ee9472010-08-12 18:01:08 -07001436 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001437 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001438 const char *old_name = ((const char*) data) + sizeof(*req);
1439 const char *new_name = old_name + strlen(old_name) + 1;
1440 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001441 }
Jeff Brown84715842012-05-25 14:07:47 -07001442
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001443// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001444 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001445 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001446 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001447 }
Jeff Brown84715842012-05-25 14:07:47 -07001448
Brian Swetland03ee9472010-08-12 18:01:08 -07001449 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001450 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001451 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001452 }
Jeff Brown84715842012-05-25 14:07:47 -07001453
Brian Swetland03ee9472010-08-12 18:01:08 -07001454 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001455 const struct fuse_write_in *req = data;
1456 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001457 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001458 }
Jeff Brown84715842012-05-25 14:07:47 -07001459
Mike Lockwood4553b082010-08-16 14:14:44 -04001460 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001461 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001462 }
Jeff Brown84715842012-05-25 14:07:47 -07001463
Brian Swetland03ee9472010-08-12 18:01:08 -07001464 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001465 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001466 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001467 }
Jeff Brown84715842012-05-25 14:07:47 -07001468
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001469 case FUSE_FSYNC:
1470 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001471 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001472 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001473 }
1474
Brian Swetland03ee9472010-08-12 18:01:08 -07001475// case FUSE_SETXATTR:
1476// case FUSE_GETXATTR:
1477// case FUSE_LISTXATTR:
1478// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001479 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001480 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001481 }
1482
Brian Swetland03ee9472010-08-12 18:01:08 -07001483 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001484 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001485 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001486 }
Jeff Brown84715842012-05-25 14:07:47 -07001487
Brian Swetland03ee9472010-08-12 18:01:08 -07001488 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001489 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001490 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001491 }
Jeff Brown84715842012-05-25 14:07:47 -07001492
Brian Swetland03ee9472010-08-12 18:01:08 -07001493 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001494 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001495 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001496 }
Jeff Brown84715842012-05-25 14:07:47 -07001497
Brian Swetland03ee9472010-08-12 18:01:08 -07001498 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001499 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001500 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001501 }
Jeff Brown84715842012-05-25 14:07:47 -07001502
Brian Swetland03ee9472010-08-12 18:01:08 -07001503 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001504 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001505 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1506 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001507 }
Jeff Brown84715842012-05-25 14:07:47 -07001508 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001509}
1510
Jeff Brown6249b902012-05-26 14:32:54 -07001511static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001512{
Jeff Brown6249b902012-05-26 14:32:54 -07001513 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001514 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001515 ssize_t len = read(fuse->fd,
1516 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001517 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001518 if (errno != EINTR) {
1519 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1520 }
1521 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001522 }
Jeff Brown84715842012-05-25 14:07:47 -07001523
1524 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001525 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1526 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001527 }
1528
Jeff Brown7729d242012-05-25 15:35:28 -07001529 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001530 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001531 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1532 handler->token, (size_t)len, hdr->len);
1533 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001534 }
1535
Jeff Brown7729d242012-05-25 15:35:28 -07001536 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001537 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001538 __u64 unique = hdr->unique;
1539 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001540
1541 /* We do not access the request again after this point because the underlying
1542 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001543
1544 if (res != NO_STATUS) {
1545 if (res) {
1546 TRACE("[%d] ERROR %d\n", handler->token, res);
1547 }
1548 fuse_status(fuse, unique, res);
1549 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 }
1551}
1552
Jeff Brown6249b902012-05-26 14:32:54 -07001553static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001554{
Jeff Brown6249b902012-05-26 14:32:54 -07001555 struct fuse_handler* handler = data;
1556 handle_fuse_requests(handler);
1557 return NULL;
1558}
1559
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001560static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001561 Hashmap* map = context;
1562 hashmapRemove(map, key);
1563 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001564 return true;
1565}
1566
1567static bool remove_int_to_null(void *key, void *value, void *context) {
1568 Hashmap* map = context;
1569 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001570 return true;
1571}
1572
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001573static int read_package_list(struct fuse_global* global) {
1574 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001575
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001576 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001577
1578 FILE* file = fopen(kPackagesListFile, "r");
1579 if (!file) {
1580 ERROR("failed to open package list: %s\n", strerror(errno));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001581 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001582 return -1;
1583 }
1584
1585 char buf[512];
1586 while (fgets(buf, sizeof(buf), file) != NULL) {
1587 char package_name[512];
1588 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001589 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001590
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001591 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1592 char* package_name_dup = strdup(package_name);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001593 hashmapPut(global->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001594 }
1595 }
1596
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001597 TRACE("read_package_list: found %zu packages\n",
1598 hashmapSize(global->package_to_appid));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001599 fclose(file);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001600 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001601 return 0;
1602}
1603
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001604static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001605 struct inotify_event *event;
1606 char event_buf[512];
1607
1608 int nfd = inotify_init();
1609 if (nfd < 0) {
1610 ERROR("inotify_init failed: %s\n", strerror(errno));
1611 return;
1612 }
1613
1614 bool active = false;
1615 while (1) {
1616 if (!active) {
1617 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1618 if (res == -1) {
1619 if (errno == ENOENT || errno == EACCES) {
1620 /* Framework may not have created yet, sleep and retry */
1621 ERROR("missing packages.list; retrying\n");
1622 sleep(3);
1623 continue;
1624 } else {
1625 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1626 return;
1627 }
1628 }
1629
1630 /* Watch above will tell us about any future changes, so
1631 * read the current state. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001632 if (read_package_list(global) == -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001633 ERROR("read_package_list failed: %s\n", strerror(errno));
1634 return;
1635 }
1636 active = true;
1637 }
1638
1639 int event_pos = 0;
1640 int res = read(nfd, event_buf, sizeof(event_buf));
1641 if (res < (int) sizeof(*event)) {
1642 if (errno == EINTR)
1643 continue;
1644 ERROR("failed to read inotify event: %s\n", strerror(errno));
1645 return;
1646 }
1647
1648 while (res >= (int) sizeof(*event)) {
1649 int event_size;
1650 event = (struct inotify_event *) (event_buf + event_pos);
1651
1652 TRACE("inotify event: %08x\n", event->mask);
1653 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1654 /* Previously watched file was deleted, probably due to move
1655 * that swapped in new data; re-arm the watch and read. */
1656 active = false;
1657 }
1658
1659 event_size = sizeof(*event) + event->len;
1660 res -= event_size;
1661 event_pos += event_size;
1662 }
1663 }
1664}
1665
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001666static int usage() {
1667 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001668 " -u: specify UID to run as\n"
1669 " -g: specify GID to run as\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001670 " -m: source_path is multi-user\n"
1671 " -w: runtime_write mount has full write access\n"
1672 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001673 return 1;
1674}
1675
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001676static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001677 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001678
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001679 fuse->fd = open("/dev/fuse", O_RDWR);
1680 if (fuse->fd == -1) {
1681 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001682 return -1;
1683 }
1684
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001685 umount2(fuse->dest_path, MNT_DETACH);
1686
Jeff Brown26567352012-05-25 13:27:43 -07001687 snprintf(opts, sizeof(opts),
1688 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001689 fuse->fd, fuse->global->uid, fuse->global->gid);
1690 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1691 MS_NOATIME, opts) != 0) {
1692 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1693 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001694 }
1695
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001696 fuse->next_generation = 0;
1697 fuse->inode_ctr = 1;
1698 fuse->gid = gid;
1699 fuse->mask = mask;
1700
1701 memset(&fuse->root, 0, sizeof(fuse->root));
1702 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
1703 fuse->root.refcount = 2;
1704 fuse->root.namelen = strlen(fuse->source_path);
1705 fuse->root.name = strdup(fuse->source_path);
1706 fuse->root.userid = 0;
1707 fuse->root.uid = AID_ROOT;
1708 fuse->root.gid = fuse->gid;
1709
1710 if (fuse->global->multi_user) {
1711 fuse->root.perm = PERM_PRE_ROOT;
1712 fuse->root.mode = 0711;
1713 snprintf(fuse->obb_path, sizeof(fuse->obb_path), "%s/obb", fuse->source_path);
1714 } else {
1715 fuse->root.perm = PERM_ROOT;
1716 fuse->root.mode = 0771 & ~mask;
1717 snprintf(fuse->obb_path, sizeof(fuse->obb_path), "%s/Android/obb", fuse->source_path);
Jeff Brown26567352012-05-25 13:27:43 -07001718 }
1719
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001720 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001721}
1722
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001723static void run(const char* source_path, const char* label, uid_t uid,
1724 gid_t gid, bool multi_user, bool full_write) {
1725 struct fuse_global global;
1726 struct fuse fuse_default;
1727 struct fuse fuse_read;
1728 struct fuse fuse_write;
1729 struct fuse_handler handler_default;
1730 struct fuse_handler handler_read;
1731 struct fuse_handler handler_write;
1732 pthread_t thread_default;
1733 pthread_t thread_read;
1734 pthread_t thread_write;
1735
1736 memset(&global, 0, sizeof(global));
1737 memset(&fuse_default, 0, sizeof(fuse_default));
1738 memset(&fuse_read, 0, sizeof(fuse_read));
1739 memset(&fuse_write, 0, sizeof(fuse_write));
1740 memset(&handler_default, 0, sizeof(handler_default));
1741 memset(&handler_read, 0, sizeof(handler_read));
1742 memset(&handler_write, 0, sizeof(handler_write));
1743
1744 pthread_mutex_init(&global.lock, NULL);
1745 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1746 global.uid = uid;
1747 global.gid = gid;
1748 global.multi_user = multi_user;
1749
1750 fuse_default.global = &global;
1751 strcpy(fuse_default.source_path, source_path);
1752 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime_default/%s", label);
1753
1754 fuse_read.global = &global;
1755 strcpy(fuse_read.source_path, source_path);
1756 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime_read/%s", label);
1757
1758 fuse_write.global = &global;
1759 strcpy(fuse_write.source_path, source_path);
1760 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime_write/%s", label);
1761
1762 handler_default.fuse = &fuse_default;
1763 handler_read.fuse = &fuse_read;
1764 handler_write.fuse = &fuse_write;
1765
1766 umask(0);
1767
1768 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1769 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1770 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1771 ERROR("failed to fuse_setup\n");
1772 exit(1);
1773 }
1774
1775 /* Drop privs */
1776 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1777 ERROR("cannot setgroups: %s\n", strerror(errno));
1778 exit(1);
1779 }
1780 if (setgid(gid) < 0) {
1781 ERROR("cannot setgid: %s\n", strerror(errno));
1782 exit(1);
1783 }
1784 if (setuid(uid) < 0) {
1785 ERROR("cannot setuid: %s\n", strerror(errno));
1786 exit(1);
1787 }
1788
1789 if (multi_user) {
1790 fs_prepare_dir(fuse_default.obb_path, 0775, uid, gid);
1791 fs_prepare_dir(fuse_read.obb_path, 0775, uid, gid);
1792 fs_prepare_dir(fuse_write.obb_path, 0775, uid, gid);
1793 }
1794
1795 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1796 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1797 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1798 ERROR("failed to pthread_create\n");
1799 exit(1);
1800 }
1801
1802 watch_package_list(&global);
1803 ERROR("terminated prematurely\n");
1804 exit(1);
1805}
1806
1807int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001808 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001809 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001810 uid_t uid = 0;
1811 gid_t gid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001812 bool multi_user = false;
1813 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001814 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001815 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001816 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001817
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001818 int opt;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001819 while ((opt = getopt(argc, argv, "u:g:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001820 switch (opt) {
1821 case 'u':
1822 uid = strtoul(optarg, NULL, 10);
1823 break;
1824 case 'g':
1825 gid = strtoul(optarg, NULL, 10);
1826 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001827 case 'm':
1828 multi_user = true;
1829 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001830 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001831 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001832 break;
1833 case '?':
1834 default:
1835 return usage();
1836 }
1837 }
1838
1839 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001840 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001841 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001842 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001843 } else if (!label) {
1844 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001845 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001846 ERROR("too many arguments\n");
1847 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001848 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001849 }
1850
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001851 if (!source_path) {
1852 ERROR("no source path specified\n");
1853 return usage();
1854 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001855 if (!label) {
1856 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001857 return usage();
1858 }
Jeff Brown26567352012-05-25 13:27:43 -07001859 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001860 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001861 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001862 }
1863
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001864 rlim.rlim_cur = 8192;
1865 rlim.rlim_max = 8192;
1866 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1867 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1868 }
1869
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001870 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1871 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1872 sleep(1);
1873 }
1874
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001875 run(source_path, label, uid, gid, multi_user, full_write);
1876 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07001877}