blob: a136232e26e5bc485ec0ca312a9bd4a21dc7a49e [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 Sharkeyf38f29c2015-06-23 14:30:37 -0700187/* Global data for all FUSE mounts */
188struct fuse_global {
Jeff Brown6249b902012-05-26 14:32:54 -0700189 pthread_mutex_t lock;
190
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700191 uid_t uid;
192 gid_t gid;
193 bool multi_user;
194
195 Hashmap* package_to_appid;
196};
197
198/* Single FUSE mount */
199struct fuse {
200 struct fuse_global* global;
201
202 char source_path[PATH_MAX];
203 char dest_path[PATH_MAX];
204 char obb_path[PATH_MAX];
205
Brian Swetland03ee9472010-08-12 18:01:08 -0700206 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700207 int fd;
Brian Swetland03ee9472010-08-12 18:01:08 -0700208 struct node root;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700209 gid_t gid;
210 mode_t mask;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700211
Narayan Kamathfaa09352015-01-13 18:21:10 +0000212 /* Used to allocate unique inode numbers for fuse nodes. We use
213 * a simple counter based scheme where inode numbers from deleted
214 * nodes aren't reused. Note that inode allocations are not stable
215 * across multiple invocation of the sdcard daemon, but that shouldn't
216 * be a huge problem in practice.
217 *
218 * Note that we restrict inodes to 32 bit unsigned integers to prevent
219 * truncation on 32 bit processes when unsigned long long stat.st_ino is
220 * assigned to an unsigned long ino_t type in an LP32 process.
221 *
222 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
223 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
224 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
225 * in fs/fuse/inode.c).
226 *
227 * Accesses must be guarded by |lock|.
228 */
229 __u32 inode_ctr;
Brian Swetland03ee9472010-08-12 18:01:08 -0700230};
231
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700232/* Private data used by a single FUSE handler */
Jeff Brown7729d242012-05-25 15:35:28 -0700233struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700234 struct fuse* fuse;
235 int token;
236
Jeff Brown7729d242012-05-25 15:35:28 -0700237 /* To save memory, we never use the contents of the request buffer and the read
238 * buffer at the same time. This allows us to share the underlying storage. */
239 union {
240 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800241 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700242 };
243};
Brian Swetland03ee9472010-08-12 18:01:08 -0700244
Jeff Brown6249b902012-05-26 14:32:54 -0700245static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700246{
Jeff Brown6249b902012-05-26 14:32:54 -0700247 return (void *) (uintptr_t) nid;
248}
Brian Swetland03ee9472010-08-12 18:01:08 -0700249
Jeff Brown6249b902012-05-26 14:32:54 -0700250static inline __u64 ptr_to_id(void *ptr)
251{
252 return (__u64) (uintptr_t) ptr;
253}
Brian Swetland03ee9472010-08-12 18:01:08 -0700254
Jeff Brown6249b902012-05-26 14:32:54 -0700255static void acquire_node_locked(struct node* node)
256{
257 node->refcount++;
258 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
259}
260
261static void remove_node_from_parent_locked(struct node* node);
262
263static void release_node_locked(struct node* node)
264{
265 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
266 if (node->refcount > 0) {
267 node->refcount--;
268 if (!node->refcount) {
269 TRACE("DESTROY %p (%s)\n", node, node->name);
270 remove_node_from_parent_locked(node);
271
272 /* TODO: remove debugging - poison memory */
273 memset(node->name, 0xef, node->namelen);
274 free(node->name);
275 free(node->actual_name);
276 memset(node, 0xfc, sizeof(*node));
277 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800278 }
Jeff Brown6249b902012-05-26 14:32:54 -0700279 } else {
280 ERROR("Zero refcnt %p\n", node);
281 }
282}
283
284static void add_node_to_parent_locked(struct node *node, struct node *parent) {
285 node->parent = parent;
286 node->next = parent->child;
287 parent->child = node;
288 acquire_node_locked(parent);
289}
290
291static void remove_node_from_parent_locked(struct node* node)
292{
293 if (node->parent) {
294 if (node->parent->child == node) {
295 node->parent->child = node->parent->child->next;
296 } else {
297 struct node *node2;
298 node2 = node->parent->child;
299 while (node2->next != node)
300 node2 = node2->next;
301 node2->next = node->next;
302 }
303 release_node_locked(node->parent);
304 node->parent = NULL;
305 node->next = NULL;
306 }
307}
308
309/* Gets the absolute path to a node into the provided buffer.
310 *
311 * Populates 'buf' with the path and returns the length of the path on success,
312 * or returns -1 if the path is too long for the provided buffer.
313 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700314static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
315 const char* name;
316 size_t namelen;
317 if (node->graft_path) {
318 name = node->graft_path;
319 namelen = node->graft_pathlen;
320 } else if (node->actual_name) {
321 name = node->actual_name;
322 namelen = node->namelen;
323 } else {
324 name = node->name;
325 namelen = node->namelen;
326 }
327
Jeff Brown6249b902012-05-26 14:32:54 -0700328 if (bufsize < namelen + 1) {
329 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700330 }
331
Jeff Brown6249b902012-05-26 14:32:54 -0700332 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700333 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700334 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
335 if (pathlen < 0) {
336 return -1;
337 }
338 buf[pathlen++] = '/';
339 }
340
Jeff Brown6249b902012-05-26 14:32:54 -0700341 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
342 return pathlen + namelen;
343}
344
345/* Finds the absolute path of a file within a given directory.
346 * Performs a case-insensitive search for the file and sets the buffer to the path
347 * of the first matching file. If 'search' is zero or if no match is found, sets
348 * the buffer to the path that the file would have, assuming the name were case-sensitive.
349 *
350 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
351 * or returns NULL if the path is too long for the provided buffer.
352 */
353static char* find_file_within(const char* path, const char* name,
354 char* buf, size_t bufsize, int search)
355{
356 size_t pathlen = strlen(path);
357 size_t namelen = strlen(name);
358 size_t childlen = pathlen + namelen + 1;
359 char* actual;
360
361 if (bufsize <= childlen) {
362 return NULL;
363 }
364
365 memcpy(buf, path, pathlen);
366 buf[pathlen] = '/';
367 actual = buf + pathlen + 1;
368 memcpy(actual, name, namelen + 1);
369
370 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800371 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700372 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800373 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700374 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700375 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800376 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800377 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700378 if (!strcasecmp(entry->d_name, name)) {
379 /* we have a match - replace the name, don't need to copy the null again */
380 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800381 break;
382 }
383 }
384 closedir(dir);
385 }
Jeff Brown6249b902012-05-26 14:32:54 -0700386 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800387}
388
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700389static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800390{
Narayan Kamathfaa09352015-01-13 18:21:10 +0000391 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700392 attr->size = s->st_size;
393 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800394 attr->atime = s->st_atim.tv_sec;
395 attr->mtime = s->st_mtim.tv_sec;
396 attr->ctime = s->st_ctim.tv_sec;
397 attr->atimensec = s->st_atim.tv_nsec;
398 attr->mtimensec = s->st_mtim.tv_nsec;
399 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700400 attr->mode = s->st_mode;
401 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700402
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700403 attr->uid = node->uid;
404 attr->gid = node->gid;
405
406 /* Filter requested mode based on underlying file, and
407 * pass through file type. */
408 int owner_mode = s->st_mode & 0700;
409 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
410 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
411}
412
Jeff Sharkey44d63422013-09-12 09:44:48 -0700413static int touch(char* path, mode_t mode) {
414 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
415 if (fd == -1) {
416 if (errno == EEXIST) {
417 return 0;
418 } else {
419 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
420 return -1;
421 }
422 }
423 close(fd);
424 return 0;
425}
426
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700427static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
428 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700429 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700430
431 /* By default, each node inherits from its parent */
432 node->perm = PERM_INHERIT;
433 node->userid = parent->userid;
434 node->uid = parent->uid;
435 node->gid = parent->gid;
436 node->mode = parent->mode;
437
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700438 /* Derive custom permissions based on parent and current node */
439 switch (parent->perm) {
440 case PERM_INHERIT:
441 /* Already inherited above */
442 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700443 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700444 /* Legacy internal layout places users at top level */
445 node->perm = PERM_ROOT;
446 node->userid = strtoul(node->name, NULL, 10);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700447 node->gid = multiuser_get_uid(node->userid, fuse->gid);
Jeff Sharkeyfc000482015-03-16 10:17:47 -0700448 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700449 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700450 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700451 /* Assume masked off by default. */
452 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700453 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700454 /* App-specific directories inside; let anyone traverse */
455 node->perm = PERM_ANDROID;
456 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700457 }
458 break;
459 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700460 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700461 /* App-specific directories inside; let anyone traverse */
462 node->perm = PERM_ANDROID_DATA;
463 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700464 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700465 /* App-specific directories inside; let anyone traverse */
466 node->perm = PERM_ANDROID_OBB;
467 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700468 /* Single OBB directory is always shared */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700469 node->graft_path = fuse->obb_path;
470 node->graft_pathlen = strlen(fuse->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700471 } else if (!strcasecmp(node->name, "media")) {
472 /* App-specific directories inside; let anyone traverse */
473 node->perm = PERM_ANDROID_MEDIA;
474 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700475 }
476 break;
477 case PERM_ANDROID_DATA:
478 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700479 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700480 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700481 if (appid != 0) {
482 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700483 }
484 node->mode = 0770;
485 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700486 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700487
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700488 node->mode = node->mode & ~fuse->mask;
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700489}
490
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700491/* Kernel has already enforced everything we returned through
492 * derive_permissions_locked(), so this is used to lock down access
493 * even further, such as enforcing that apps hold sdcard_rw. */
494static bool check_caller_access_to_name(struct fuse* fuse,
495 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700496 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700497 /* Always block security-sensitive files at root */
498 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700499 if (!strcasecmp(name, "autorun.inf")
500 || !strcasecmp(name, ".android_secure")
501 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700502 return false;
503 }
504 }
505
Jeff Sharkey44d63422013-09-12 09:44:48 -0700506 /* Root always has access; access for any other UIDs should always
507 * be controlled through packages.list. */
508 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700509 return true;
510 }
511
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700512 /* No extra permissions to enforce */
513 return true;
514}
515
516static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700517 const struct fuse_in_header *hdr, const struct node* node, int mode) {
518 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700519}
520
Jeff Brown6249b902012-05-26 14:32:54 -0700521struct node *create_node_locked(struct fuse* fuse,
522 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700523{
524 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700525 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700526
Narayan Kamathfaa09352015-01-13 18:21:10 +0000527 // Detect overflows in the inode counter. "4 billion nodes should be enough
528 // for everybody".
529 if (fuse->inode_ctr == 0) {
530 ERROR("No more inode numbers available");
531 return NULL;
532 }
533
Paul Eastham11ccdb32010-10-14 11:04:26 -0700534 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700535 if (!node) {
536 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700537 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700538 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700539 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700540 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700541 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700542 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700543 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700544 if (strcmp(name, actual_name)) {
545 node->actual_name = malloc(namelen + 1);
546 if (!node->actual_name) {
547 free(node->name);
548 free(node);
549 return NULL;
550 }
551 memcpy(node->actual_name, actual_name, namelen + 1);
552 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700553 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700554 node->nid = ptr_to_id(node);
Narayan Kamathfaa09352015-01-13 18:21:10 +0000555 node->ino = fuse->inode_ctr++;
Jeff Brown6249b902012-05-26 14:32:54 -0700556 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700557
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200558 node->deleted = false;
559
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700560 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700561 acquire_node_locked(node);
562 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700563 return node;
564}
565
Jeff Brown6249b902012-05-26 14:32:54 -0700566static int rename_node_locked(struct node *node, const char *name,
567 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700568{
Jeff Brown6249b902012-05-26 14:32:54 -0700569 size_t namelen = strlen(name);
570 int need_actual_name = strcmp(name, actual_name);
571
572 /* make the storage bigger without actually changing the name
573 * in case an error occurs part way */
574 if (namelen > node->namelen) {
575 char* new_name = realloc(node->name, namelen + 1);
576 if (!new_name) {
577 return -ENOMEM;
578 }
579 node->name = new_name;
580 if (need_actual_name && node->actual_name) {
581 char* new_actual_name = realloc(node->actual_name, namelen + 1);
582 if (!new_actual_name) {
583 return -ENOMEM;
584 }
585 node->actual_name = new_actual_name;
586 }
587 }
588
589 /* update the name, taking care to allocate storage before overwriting the old name */
590 if (need_actual_name) {
591 if (!node->actual_name) {
592 node->actual_name = malloc(namelen + 1);
593 if (!node->actual_name) {
594 return -ENOMEM;
595 }
596 }
597 memcpy(node->actual_name, actual_name, namelen + 1);
598 } else {
599 free(node->actual_name);
600 node->actual_name = NULL;
601 }
602 memcpy(node->name, name, namelen + 1);
603 node->namelen = namelen;
604 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700605}
606
Jeff Brown6249b902012-05-26 14:32:54 -0700607static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700608{
Jeff Brown6249b902012-05-26 14:32:54 -0700609 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700610 return &fuse->root;
611 } else {
612 return id_to_ptr(nid);
613 }
614}
615
Jeff Brown6249b902012-05-26 14:32:54 -0700616static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
617 char* buf, size_t bufsize)
618{
619 struct node* node = lookup_node_by_id_locked(fuse, nid);
620 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
621 node = NULL;
622 }
623 return node;
624}
625
626static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700627{
628 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700629 /* use exact string comparison, nodes that differ by case
630 * must be considered distinct even if they refer to the same
631 * underlying file as otherwise operations such as "mv x x"
632 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200633 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700634 return node;
635 }
636 }
637 return 0;
638}
639
Jeff Brown6249b902012-05-26 14:32:54 -0700640static struct node* acquire_or_create_child_locked(
641 struct fuse* fuse, struct node* parent,
642 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700643{
Jeff Brown6249b902012-05-26 14:32:54 -0700644 struct node* child = lookup_child_by_name_locked(parent, name);
645 if (child) {
646 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800647 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700648 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800649 }
Jeff Brown6249b902012-05-26 14:32:54 -0700650 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700651}
652
Jeff Brown6249b902012-05-26 14:32:54 -0700653static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700654{
655 struct fuse_out_header hdr;
656 hdr.len = sizeof(hdr);
657 hdr.error = err;
658 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700659 write(fuse->fd, &hdr, sizeof(hdr));
660}
661
Jeff Brown6249b902012-05-26 14:32:54 -0700662static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700663{
664 struct fuse_out_header hdr;
665 struct iovec vec[2];
666 int res;
667
668 hdr.len = len + sizeof(hdr);
669 hdr.error = 0;
670 hdr.unique = unique;
671
672 vec[0].iov_base = &hdr;
673 vec[0].iov_len = sizeof(hdr);
674 vec[1].iov_base = data;
675 vec[1].iov_len = len;
676
677 res = writev(fuse->fd, vec, 2);
678 if (res < 0) {
679 ERROR("*** REPLY FAILED *** %d\n", errno);
680 }
681}
682
Jeff Brown6249b902012-05-26 14:32:54 -0700683static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
684 struct node* parent, const char* name, const char* actual_name,
685 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700686{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700687 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700688 struct fuse_entry_out out;
689 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700690
Jeff Brown6249b902012-05-26 14:32:54 -0700691 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700692 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700693 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700694
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700695 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700696 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
697 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700698 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700699 return -ENOMEM;
700 }
701 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700702 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700703 out.attr_valid = 10;
704 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700705 out.nodeid = node->nid;
706 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700707 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700708 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700709 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700710}
711
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700712static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700713 const char* path)
714{
715 struct fuse_attr_out out;
716 struct stat s;
717
718 if (lstat(path, &s) < 0) {
719 return -errno;
720 }
721 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700722 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700723 out.attr_valid = 10;
724 fuse_reply(fuse, unique, &out, sizeof(out));
725 return NO_STATUS;
726}
727
728static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700729 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700730{
Jeff Brown6249b902012-05-26 14:32:54 -0700731 struct node* parent_node;
732 char parent_path[PATH_MAX];
733 char child_path[PATH_MAX];
734 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700735
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700736 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700737 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
738 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100739 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700740 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700741 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700742
743 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
744 child_path, sizeof(child_path), 1))) {
745 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700746 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700747 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700748 return -EACCES;
749 }
750
Jeff Brown6249b902012-05-26 14:32:54 -0700751 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700752}
753
Jeff Brown6249b902012-05-26 14:32:54 -0700754static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700755 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
756{
Jeff Brown6249b902012-05-26 14:32:54 -0700757 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700758
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700759 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700760 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100761 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700762 hdr->nodeid, node ? node->name : "?");
763 if (node) {
764 __u64 n = req->nlookup;
765 while (n--) {
766 release_node_locked(node);
767 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700768 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700769 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700770 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700771}
772
Jeff Brown6249b902012-05-26 14:32:54 -0700773static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700774 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
775{
Jeff Brown6249b902012-05-26 14:32:54 -0700776 struct node* node;
777 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700778
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700779 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700780 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100781 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700782 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700783 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700784
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700785 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700786 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700787 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700788 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700789 return -EACCES;
790 }
791
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700792 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700793}
794
Jeff Brown6249b902012-05-26 14:32:54 -0700795static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700796 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
797{
Jeff Brown6249b902012-05-26 14:32:54 -0700798 struct node* node;
799 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700800 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700801
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700802 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700803 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100804 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700805 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700806 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700807
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700808 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700809 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700810 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800811
812 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700813 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700814 return -EACCES;
815 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700816
Jeff Brown6249b902012-05-26 14:32:54 -0700817 /* XXX: incomplete implementation on purpose.
818 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700819
Elliott Hughes853574d2014-07-31 12:03:03 -0700820 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700821 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700822 }
823
824 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
825 * are both set, then set it to the current time. Else, set it to the
826 * time specified in the request. Same goes for mtime. Use utimensat(2)
827 * as it allows ATIME and MTIME to be changed independently, and has
828 * nanosecond resolution which fuse also has.
829 */
830 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
831 times[0].tv_nsec = UTIME_OMIT;
832 times[1].tv_nsec = UTIME_OMIT;
833 if (req->valid & FATTR_ATIME) {
834 if (req->valid & FATTR_ATIME_NOW) {
835 times[0].tv_nsec = UTIME_NOW;
836 } else {
837 times[0].tv_sec = req->atime;
838 times[0].tv_nsec = req->atimensec;
839 }
840 }
841 if (req->valid & FATTR_MTIME) {
842 if (req->valid & FATTR_MTIME_NOW) {
843 times[1].tv_nsec = UTIME_NOW;
844 } else {
845 times[1].tv_sec = req->mtime;
846 times[1].tv_nsec = req->mtimensec;
847 }
848 }
Jeff Brown6249b902012-05-26 14:32:54 -0700849 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
850 handler->token, path, times[0].tv_sec, times[1].tv_sec);
851 if (utimensat(-1, path, times, 0) < 0) {
852 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700853 }
854 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700855 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700856}
857
Jeff Brown6249b902012-05-26 14:32:54 -0700858static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700859 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
860{
Jeff Brown6249b902012-05-26 14:32:54 -0700861 struct node* parent_node;
862 char parent_path[PATH_MAX];
863 char child_path[PATH_MAX];
864 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700866 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700867 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
868 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100869 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700870 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700871 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700872
873 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
874 child_path, sizeof(child_path), 1))) {
875 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700877 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700878 return -EACCES;
879 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700881 if (mknod(child_path, mode, req->rdev) < 0) {
882 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883 }
Jeff Brown6249b902012-05-26 14:32:54 -0700884 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885}
886
Jeff Brown6249b902012-05-26 14:32:54 -0700887static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
889{
Jeff Brown6249b902012-05-26 14:32:54 -0700890 struct node* parent_node;
891 char parent_path[PATH_MAX];
892 char child_path[PATH_MAX];
893 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700895 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700896 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
897 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100898 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700899 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700900 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700901
902 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
903 child_path, sizeof(child_path), 1))) {
904 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700906 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700907 return -EACCES;
908 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700909 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700910 if (mkdir(child_path, mode) < 0) {
911 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700912 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700913
914 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
915 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
916 char nomedia[PATH_MAX];
917 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
918 if (touch(nomedia, 0664) != 0) {
919 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
920 return -ENOENT;
921 }
922 }
923 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
924 char nomedia[PATH_MAX];
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700925 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700926 if (touch(nomedia, 0664) != 0) {
927 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
928 return -ENOENT;
929 }
930 }
931
Jeff Brown6249b902012-05-26 14:32:54 -0700932 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700933}
934
Jeff Brown6249b902012-05-26 14:32:54 -0700935static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700936 const struct fuse_in_header* hdr, const char* name)
937{
Jeff Brown6249b902012-05-26 14:32:54 -0700938 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200939 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700940 char parent_path[PATH_MAX];
941 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700942
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700943 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700944 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
945 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100946 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700947 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700948 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700949
950 if (!parent_node || !find_file_within(parent_path, name,
951 child_path, sizeof(child_path), 1)) {
952 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700953 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700954 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700955 return -EACCES;
956 }
Jeff Brown6249b902012-05-26 14:32:54 -0700957 if (unlink(child_path) < 0) {
958 return -errno;
959 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700960 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200961 child_node = lookup_child_by_name_locked(parent_node, name);
962 if (child_node) {
963 child_node->deleted = true;
964 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700965 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700966 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967}
968
Jeff Brown6249b902012-05-26 14:32:54 -0700969static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970 const struct fuse_in_header* hdr, const char* name)
971{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200972 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700973 struct node* parent_node;
974 char parent_path[PATH_MAX];
975 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700976
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700977 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700978 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
979 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100980 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700981 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700982 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700983
984 if (!parent_node || !find_file_within(parent_path, name,
985 child_path, sizeof(child_path), 1)) {
986 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700987 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700988 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700989 return -EACCES;
990 }
Jeff Brown6249b902012-05-26 14:32:54 -0700991 if (rmdir(child_path) < 0) {
992 return -errno;
993 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700994 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200995 child_node = lookup_child_by_name_locked(parent_node, name);
996 if (child_node) {
997 child_node->deleted = true;
998 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700999 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001000 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001}
1002
Jeff Brown6249b902012-05-26 14:32:54 -07001003static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001004 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001005 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001006{
Jeff Brown6249b902012-05-26 14:32:54 -07001007 struct node* old_parent_node;
1008 struct node* new_parent_node;
1009 struct node* child_node;
1010 char old_parent_path[PATH_MAX];
1011 char new_parent_path[PATH_MAX];
1012 char old_child_path[PATH_MAX];
1013 char new_child_path[PATH_MAX];
1014 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001015 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001016
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001017 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001018 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1019 old_parent_path, sizeof(old_parent_path));
1020 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1021 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001022 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001023 old_name, new_name,
1024 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1025 req->newdir, new_parent_node ? new_parent_node->name : "?");
1026 if (!old_parent_node || !new_parent_node) {
1027 res = -ENOENT;
1028 goto lookup_error;
1029 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001030 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001031 res = -EACCES;
1032 goto lookup_error;
1033 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001034 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001035 res = -EACCES;
1036 goto lookup_error;
1037 }
Jeff Brown6249b902012-05-26 14:32:54 -07001038 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1039 if (!child_node || get_node_path_locked(child_node,
1040 old_child_path, sizeof(old_child_path)) < 0) {
1041 res = -ENOENT;
1042 goto lookup_error;
1043 }
1044 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001045 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001046
1047 /* Special case for renaming a file where destination is same path
1048 * differing only by case. In this case we don't want to look for a case
1049 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1050 */
1051 int search = old_parent_node != new_parent_node
1052 || strcasecmp(old_name, new_name);
1053 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1054 new_child_path, sizeof(new_child_path), search))) {
1055 res = -ENOENT;
1056 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001057 }
1058
Jeff Brown6249b902012-05-26 14:32:54 -07001059 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1060 res = rename(old_child_path, new_child_path);
1061 if (res < 0) {
1062 res = -errno;
1063 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001064 }
1065
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001066 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001067 res = rename_node_locked(child_node, new_name, new_actual_name);
1068 if (!res) {
1069 remove_node_from_parent_locked(child_node);
1070 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001071 }
Jeff Brown6249b902012-05-26 14:32:54 -07001072 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073
Jeff Brown6249b902012-05-26 14:32:54 -07001074io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001075 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001076done:
1077 release_node_locked(child_node);
1078lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001079 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001080 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001081}
1082
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001083static int open_flags_to_access_mode(int open_flags) {
1084 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1085 return R_OK;
1086 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1087 return W_OK;
1088 } else {
1089 /* Probably O_RDRW, but treat as default to be safe */
1090 return R_OK | W_OK;
1091 }
1092}
1093
Jeff Brown6249b902012-05-26 14:32:54 -07001094static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001095 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1096{
Jeff Brown6249b902012-05-26 14:32:54 -07001097 struct node* node;
1098 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001099 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001100 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001101
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001102 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001103 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001104 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001105 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001106 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001107
1108 if (!node) {
1109 return -ENOENT;
1110 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001111 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001112 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001113 return -EACCES;
1114 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001115 h = malloc(sizeof(*h));
1116 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001117 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001118 }
Jeff Brown6249b902012-05-26 14:32:54 -07001119 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001120 h->fd = open(path, req->flags);
1121 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001122 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001123 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001124 }
1125 out.fh = ptr_to_id(h);
1126 out.open_flags = 0;
1127 out.padding = 0;
1128 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001129 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001130}
1131
Jeff Brown6249b902012-05-26 14:32:54 -07001132static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001133 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1134{
1135 struct handle *h = id_to_ptr(req->fh);
1136 __u64 unique = hdr->unique;
1137 __u32 size = req->size;
1138 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001139 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001140 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001141
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1143 * overlaps the request buffer and will clobber data in the request. This
1144 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001145
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001146 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1147 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001148 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001149 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001151 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001152 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001153 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001154 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001155 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001156 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001157}
1158
Jeff Brown6249b902012-05-26 14:32:54 -07001159static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1161 const void* buffer)
1162{
1163 struct fuse_write_out out;
1164 struct handle *h = id_to_ptr(req->fh);
1165 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001166 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001167
Arpad Horvath49e93442014-02-18 10:18:25 +01001168 if (req->flags & O_DIRECT) {
1169 memcpy(aligned_buffer, buffer, req->size);
1170 buffer = (const __u8*) aligned_buffer;
1171 }
Jeff Brown6249b902012-05-26 14:32:54 -07001172
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001173 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001174 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001175 res = pwrite64(h->fd, buffer, req->size, req->offset);
1176 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001177 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001178 }
1179 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001180 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001181 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001182 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183}
1184
Jeff Brown6249b902012-05-26 14:32:54 -07001185static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001186 const struct fuse_in_header* hdr)
1187{
Jeff Brown6249b902012-05-26 14:32:54 -07001188 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 struct statfs stat;
1190 struct fuse_statfs_out out;
1191 int res;
1192
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001193 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001194 TRACE("[%d] STATFS\n", handler->token);
1195 res = get_node_path_locked(&fuse->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001196 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001197 if (res < 0) {
1198 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199 }
Jeff Brown6249b902012-05-26 14:32:54 -07001200 if (statfs(fuse->root.name, &stat) < 0) {
1201 return -errno;
1202 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203 memset(&out, 0, sizeof(out));
1204 out.st.blocks = stat.f_blocks;
1205 out.st.bfree = stat.f_bfree;
1206 out.st.bavail = stat.f_bavail;
1207 out.st.files = stat.f_files;
1208 out.st.ffree = stat.f_ffree;
1209 out.st.bsize = stat.f_bsize;
1210 out.st.namelen = stat.f_namelen;
1211 out.st.frsize = stat.f_frsize;
1212 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001213 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214}
1215
Jeff Brown6249b902012-05-26 14:32:54 -07001216static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1218{
1219 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001220
1221 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 close(h->fd);
1223 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001224 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225}
1226
Jeff Brown6249b902012-05-26 14:32:54 -07001227static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001228 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1229{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001230 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1231 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001232
Elliott Hughesf6d67372014-07-08 14:38:26 -07001233 int fd = -1;
1234 if (is_dir) {
1235 struct dirhandle *dh = id_to_ptr(req->fh);
1236 fd = dirfd(dh->d);
1237 } else {
1238 struct handle *h = id_to_ptr(req->fh);
1239 fd = h->fd;
1240 }
1241
1242 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1243 is_dir ? "FSYNCDIR" : "FSYNC",
1244 id_to_ptr(req->fh), fd, is_data_sync);
1245 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1246 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001247 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248 }
Jeff Brown6249b902012-05-26 14:32:54 -07001249 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001250}
1251
Jeff Brown6249b902012-05-26 14:32:54 -07001252static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001253 const struct fuse_in_header* hdr)
1254{
Jeff Brown6249b902012-05-26 14:32:54 -07001255 TRACE("[%d] FLUSH\n", handler->token);
1256 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001257}
1258
Jeff Brown6249b902012-05-26 14:32:54 -07001259static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001260 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1261{
Jeff Brown6249b902012-05-26 14:32:54 -07001262 struct node* node;
1263 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001264 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001265 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001267 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001268 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001269 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001270 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001271 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001272
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001273 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001274 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001276 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001277 return -EACCES;
1278 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001279 h = malloc(sizeof(*h));
1280 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001281 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001282 }
Jeff Brown6249b902012-05-26 14:32:54 -07001283 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001284 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001285 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001286 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001287 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001288 }
1289 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001290 out.open_flags = 0;
1291 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001292 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001293 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001294}
1295
Jeff Brown6249b902012-05-26 14:32:54 -07001296static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1298{
1299 char buffer[8192];
1300 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1301 struct dirent *de;
1302 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001303
1304 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001305 if (req->offset == 0) {
1306 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001307 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308 rewinddir(h->d);
1309 }
1310 de = readdir(h->d);
1311 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001312 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001313 }
1314 fde->ino = FUSE_UNKNOWN_INO;
1315 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1316 fde->off = req->offset + 1;
1317 fde->type = de->d_type;
1318 fde->namelen = strlen(de->d_name);
1319 memcpy(fde->name, de->d_name, fde->namelen + 1);
1320 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001321 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1322 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001323}
1324
Jeff Brown6249b902012-05-26 14:32:54 -07001325static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1327{
1328 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001329
1330 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001331 closedir(h->d);
1332 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001333 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001334}
1335
Jeff Brown6249b902012-05-26 14:32:54 -07001336static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1338{
1339 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001340 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341
Jeff Brown6249b902012-05-26 14:32:54 -07001342 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1343 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001344
1345 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1346 * defined (fuse version 7.6). The structure is the same from 7.6 through
1347 * 7.22. Beginning with 7.23, the structure increased in size and added
1348 * new parameters.
1349 */
1350 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1351 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1352 req->major, req->minor, FUSE_KERNEL_VERSION);
1353 return -1;
1354 }
1355
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001356 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1357 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001358 fuse_struct_size = sizeof(out);
1359#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1360 /* FUSE_KERNEL_VERSION >= 23. */
1361
1362 /* If the kernel only works on minor revs older than or equal to 22,
1363 * then use the older structure size since this code only uses the 7.22
1364 * version of the structure. */
1365 if (req->minor <= 22) {
1366 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1367 }
1368#endif
1369
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001370 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001371 out.max_readahead = req->max_readahead;
1372 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1373 out.max_background = 32;
1374 out.congestion_threshold = 32;
1375 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001376 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001377 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378}
1379
Jeff Brown6249b902012-05-26 14:32:54 -07001380static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001381 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1382{
Brian Swetland03ee9472010-08-12 18:01:08 -07001383 switch (hdr->opcode) {
1384 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001385 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001386 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001387 }
Jeff Brown84715842012-05-25 14:07:47 -07001388
Brian Swetland03ee9472010-08-12 18:01:08 -07001389 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001390 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001391 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001392 }
Jeff Brown84715842012-05-25 14:07:47 -07001393
Brian Swetland03ee9472010-08-12 18:01:08 -07001394 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001395 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001396 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001397 }
Jeff Brown84715842012-05-25 14:07:47 -07001398
Brian Swetland03ee9472010-08-12 18:01:08 -07001399 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001400 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001401 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001402 }
Jeff Brown84715842012-05-25 14:07:47 -07001403
Brian Swetland03ee9472010-08-12 18:01:08 -07001404// case FUSE_READLINK:
1405// case FUSE_SYMLINK:
1406 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001407 const struct fuse_mknod_in *req = data;
1408 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001409 return handle_mknod(fuse, handler, hdr, req, name);
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_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001413 const struct fuse_mkdir_in *req = data;
1414 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001415 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001416 }
Jeff Brown84715842012-05-25 14:07:47 -07001417
Brian Swetland03ee9472010-08-12 18:01:08 -07001418 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001419 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001420 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001421 }
Jeff Brown84715842012-05-25 14:07:47 -07001422
Brian Swetland03ee9472010-08-12 18:01:08 -07001423 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001424 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001425 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001426 }
Jeff Brown84715842012-05-25 14:07:47 -07001427
Brian Swetland03ee9472010-08-12 18:01:08 -07001428 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001429 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001430 const char *old_name = ((const char*) data) + sizeof(*req);
1431 const char *new_name = old_name + strlen(old_name) + 1;
1432 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001433 }
Jeff Brown84715842012-05-25 14:07:47 -07001434
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001435// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001436 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001437 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001438 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001439 }
Jeff Brown84715842012-05-25 14:07:47 -07001440
Brian Swetland03ee9472010-08-12 18:01:08 -07001441 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001442 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001443 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001444 }
Jeff Brown84715842012-05-25 14:07:47 -07001445
Brian Swetland03ee9472010-08-12 18:01:08 -07001446 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001447 const struct fuse_write_in *req = data;
1448 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001449 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001450 }
Jeff Brown84715842012-05-25 14:07:47 -07001451
Mike Lockwood4553b082010-08-16 14:14:44 -04001452 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001453 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001454 }
Jeff Brown84715842012-05-25 14:07:47 -07001455
Brian Swetland03ee9472010-08-12 18:01:08 -07001456 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001457 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001458 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001459 }
Jeff Brown84715842012-05-25 14:07:47 -07001460
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001461 case FUSE_FSYNC:
1462 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001463 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001464 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001465 }
1466
Brian Swetland03ee9472010-08-12 18:01:08 -07001467// case FUSE_SETXATTR:
1468// case FUSE_GETXATTR:
1469// case FUSE_LISTXATTR:
1470// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001471 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001472 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001473 }
1474
Brian Swetland03ee9472010-08-12 18:01:08 -07001475 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001476 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001477 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001478 }
Jeff Brown84715842012-05-25 14:07:47 -07001479
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001481 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001482 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001483 }
Jeff Brown84715842012-05-25 14:07:47 -07001484
Brian Swetland03ee9472010-08-12 18:01:08 -07001485 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001486 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001487 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001488 }
Jeff Brown84715842012-05-25 14:07:47 -07001489
Brian Swetland03ee9472010-08-12 18:01:08 -07001490 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001491 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001492 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001493 }
Jeff Brown84715842012-05-25 14:07:47 -07001494
Brian Swetland03ee9472010-08-12 18:01:08 -07001495 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001496 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001497 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1498 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001499 }
Jeff Brown84715842012-05-25 14:07:47 -07001500 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001501}
1502
Jeff Brown6249b902012-05-26 14:32:54 -07001503static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001504{
Jeff Brown6249b902012-05-26 14:32:54 -07001505 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001506 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001507 ssize_t len = read(fuse->fd,
1508 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001509 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001510 if (errno != EINTR) {
1511 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1512 }
1513 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001514 }
Jeff Brown84715842012-05-25 14:07:47 -07001515
1516 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001517 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1518 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001519 }
1520
Jeff Brown7729d242012-05-25 15:35:28 -07001521 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001522 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001523 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1524 handler->token, (size_t)len, hdr->len);
1525 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001526 }
1527
Jeff Brown7729d242012-05-25 15:35:28 -07001528 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001529 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001530 __u64 unique = hdr->unique;
1531 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001532
1533 /* We do not access the request again after this point because the underlying
1534 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001535
1536 if (res != NO_STATUS) {
1537 if (res) {
1538 TRACE("[%d] ERROR %d\n", handler->token, res);
1539 }
1540 fuse_status(fuse, unique, res);
1541 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001542 }
1543}
1544
Jeff Brown6249b902012-05-26 14:32:54 -07001545static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001546{
Jeff Brown6249b902012-05-26 14:32:54 -07001547 struct fuse_handler* handler = data;
1548 handle_fuse_requests(handler);
1549 return NULL;
1550}
1551
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001552static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001553 Hashmap* map = context;
1554 hashmapRemove(map, key);
1555 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001556 return true;
1557}
1558
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001559static int read_package_list(struct fuse_global* global) {
1560 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001561
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001562 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001563
1564 FILE* file = fopen(kPackagesListFile, "r");
1565 if (!file) {
1566 ERROR("failed to open package list: %s\n", strerror(errno));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001567 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001568 return -1;
1569 }
1570
1571 char buf[512];
1572 while (fgets(buf, sizeof(buf), file) != NULL) {
1573 char package_name[512];
1574 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001575 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001576
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001577 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1578 char* package_name_dup = strdup(package_name);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001579 hashmapPut(global->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001580 }
1581 }
1582
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001583 TRACE("read_package_list: found %zu packages\n",
1584 hashmapSize(global->package_to_appid));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001585 fclose(file);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001586 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001587 return 0;
1588}
1589
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001590static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001591 struct inotify_event *event;
1592 char event_buf[512];
1593
1594 int nfd = inotify_init();
1595 if (nfd < 0) {
1596 ERROR("inotify_init failed: %s\n", strerror(errno));
1597 return;
1598 }
1599
1600 bool active = false;
1601 while (1) {
1602 if (!active) {
1603 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1604 if (res == -1) {
1605 if (errno == ENOENT || errno == EACCES) {
1606 /* Framework may not have created yet, sleep and retry */
1607 ERROR("missing packages.list; retrying\n");
1608 sleep(3);
1609 continue;
1610 } else {
1611 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1612 return;
1613 }
1614 }
1615
1616 /* Watch above will tell us about any future changes, so
1617 * read the current state. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001618 if (read_package_list(global) == -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001619 ERROR("read_package_list failed: %s\n", strerror(errno));
1620 return;
1621 }
1622 active = true;
1623 }
1624
1625 int event_pos = 0;
1626 int res = read(nfd, event_buf, sizeof(event_buf));
1627 if (res < (int) sizeof(*event)) {
1628 if (errno == EINTR)
1629 continue;
1630 ERROR("failed to read inotify event: %s\n", strerror(errno));
1631 return;
1632 }
1633
1634 while (res >= (int) sizeof(*event)) {
1635 int event_size;
1636 event = (struct inotify_event *) (event_buf + event_pos);
1637
1638 TRACE("inotify event: %08x\n", event->mask);
1639 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1640 /* Previously watched file was deleted, probably due to move
1641 * that swapped in new data; re-arm the watch and read. */
1642 active = false;
1643 }
1644
1645 event_size = sizeof(*event) + event->len;
1646 res -= event_size;
1647 event_pos += event_size;
1648 }
1649 }
1650}
1651
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001652static int usage() {
1653 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001654 " -u: specify UID to run as\n"
1655 " -g: specify GID to run as\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001656 " -m: source_path is multi-user\n"
1657 " -w: runtime_write mount has full write access\n"
1658 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001659 return 1;
1660}
1661
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001662static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001663 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001664
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001665 fuse->fd = open("/dev/fuse", O_RDWR);
1666 if (fuse->fd == -1) {
1667 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001668 return -1;
1669 }
1670
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001671 umount2(fuse->dest_path, MNT_DETACH);
1672
Jeff Brown26567352012-05-25 13:27:43 -07001673 snprintf(opts, sizeof(opts),
1674 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001675 fuse->fd, fuse->global->uid, fuse->global->gid);
1676 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1677 MS_NOATIME, opts) != 0) {
1678 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1679 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001680 }
1681
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001682 fuse->next_generation = 0;
1683 fuse->inode_ctr = 1;
1684 fuse->gid = gid;
1685 fuse->mask = mask;
1686
1687 memset(&fuse->root, 0, sizeof(fuse->root));
1688 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
1689 fuse->root.refcount = 2;
1690 fuse->root.namelen = strlen(fuse->source_path);
1691 fuse->root.name = strdup(fuse->source_path);
1692 fuse->root.userid = 0;
1693 fuse->root.uid = AID_ROOT;
1694 fuse->root.gid = fuse->gid;
1695
1696 if (fuse->global->multi_user) {
1697 fuse->root.perm = PERM_PRE_ROOT;
1698 fuse->root.mode = 0711;
1699 snprintf(fuse->obb_path, sizeof(fuse->obb_path), "%s/obb", fuse->source_path);
1700 } else {
1701 fuse->root.perm = PERM_ROOT;
1702 fuse->root.mode = 0771 & ~mask;
1703 snprintf(fuse->obb_path, sizeof(fuse->obb_path), "%s/Android/obb", fuse->source_path);
Jeff Brown26567352012-05-25 13:27:43 -07001704 }
1705
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001706 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001707}
1708
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001709static void run(const char* source_path, const char* label, uid_t uid,
1710 gid_t gid, bool multi_user, bool full_write) {
1711 struct fuse_global global;
1712 struct fuse fuse_default;
1713 struct fuse fuse_read;
1714 struct fuse fuse_write;
1715 struct fuse_handler handler_default;
1716 struct fuse_handler handler_read;
1717 struct fuse_handler handler_write;
1718 pthread_t thread_default;
1719 pthread_t thread_read;
1720 pthread_t thread_write;
1721
1722 memset(&global, 0, sizeof(global));
1723 memset(&fuse_default, 0, sizeof(fuse_default));
1724 memset(&fuse_read, 0, sizeof(fuse_read));
1725 memset(&fuse_write, 0, sizeof(fuse_write));
1726 memset(&handler_default, 0, sizeof(handler_default));
1727 memset(&handler_read, 0, sizeof(handler_read));
1728 memset(&handler_write, 0, sizeof(handler_write));
1729
1730 pthread_mutex_init(&global.lock, NULL);
1731 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1732 global.uid = uid;
1733 global.gid = gid;
1734 global.multi_user = multi_user;
1735
1736 fuse_default.global = &global;
1737 strcpy(fuse_default.source_path, source_path);
1738 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime_default/%s", label);
1739
1740 fuse_read.global = &global;
1741 strcpy(fuse_read.source_path, source_path);
1742 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime_read/%s", label);
1743
1744 fuse_write.global = &global;
1745 strcpy(fuse_write.source_path, source_path);
1746 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime_write/%s", label);
1747
1748 handler_default.fuse = &fuse_default;
1749 handler_read.fuse = &fuse_read;
1750 handler_write.fuse = &fuse_write;
1751
1752 umask(0);
1753
1754 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1755 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1756 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1757 ERROR("failed to fuse_setup\n");
1758 exit(1);
1759 }
1760
1761 /* Drop privs */
1762 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1763 ERROR("cannot setgroups: %s\n", strerror(errno));
1764 exit(1);
1765 }
1766 if (setgid(gid) < 0) {
1767 ERROR("cannot setgid: %s\n", strerror(errno));
1768 exit(1);
1769 }
1770 if (setuid(uid) < 0) {
1771 ERROR("cannot setuid: %s\n", strerror(errno));
1772 exit(1);
1773 }
1774
1775 if (multi_user) {
1776 fs_prepare_dir(fuse_default.obb_path, 0775, uid, gid);
1777 fs_prepare_dir(fuse_read.obb_path, 0775, uid, gid);
1778 fs_prepare_dir(fuse_write.obb_path, 0775, uid, gid);
1779 }
1780
1781 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1782 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1783 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1784 ERROR("failed to pthread_create\n");
1785 exit(1);
1786 }
1787
1788 watch_package_list(&global);
1789 ERROR("terminated prematurely\n");
1790 exit(1);
1791}
1792
1793int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001794 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001795 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001796 uid_t uid = 0;
1797 gid_t gid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001798 bool multi_user = false;
1799 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001800 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001801 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001802 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001803
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001804 int opt;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001805 while ((opt = getopt(argc, argv, "u:g:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001806 switch (opt) {
1807 case 'u':
1808 uid = strtoul(optarg, NULL, 10);
1809 break;
1810 case 'g':
1811 gid = strtoul(optarg, NULL, 10);
1812 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001813 case 'm':
1814 multi_user = true;
1815 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001816 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001817 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001818 break;
1819 case '?':
1820 default:
1821 return usage();
1822 }
1823 }
1824
1825 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001826 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001827 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001828 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001829 } else if (!label) {
1830 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001831 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001832 ERROR("too many arguments\n");
1833 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001834 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001835 }
1836
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001837 if (!source_path) {
1838 ERROR("no source path specified\n");
1839 return usage();
1840 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001841 if (!label) {
1842 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001843 return usage();
1844 }
Jeff Brown26567352012-05-25 13:27:43 -07001845 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001846 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001847 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001848 }
1849
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001850 rlim.rlim_cur = 8192;
1851 rlim.rlim_max = 8192;
1852 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1853 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1854 }
1855
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001856 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1857 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1858 sleep(1);
1859 }
1860
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001861 run(source_path, label, uid, gid, multi_user, full_write);
1862 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07001863}