blob: 4b8e0c05e7afc3c1473dae66bdfced8792405cf5 [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 Sharkey25aabb92015-07-06 09:46:08 -0700447 if (fuse->gid == AID_SDCARD_RW) {
448 /* As an optimization, certain trusted system components only run
449 * as owner but operate across all users. Since we're now handing
450 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
451 * the user boundary enforcement for the default view. The UIDs
452 * assigned to app directories are still multiuser aware. */
453 node->gid = fuse->gid;
454 } else {
455 node->gid = multiuser_get_uid(node->userid, fuse->gid);
456 }
Jeff Sharkeyfc000482015-03-16 10:17:47 -0700457 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700458 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700459 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700460 /* Assume masked off by default. */
461 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700462 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700463 /* App-specific directories inside; let anyone traverse */
464 node->perm = PERM_ANDROID;
465 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700466 }
467 break;
468 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700469 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700470 /* App-specific directories inside; let anyone traverse */
471 node->perm = PERM_ANDROID_DATA;
472 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700473 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700474 /* App-specific directories inside; let anyone traverse */
475 node->perm = PERM_ANDROID_OBB;
476 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700477 /* Single OBB directory is always shared */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700478 node->graft_path = fuse->obb_path;
479 node->graft_pathlen = strlen(fuse->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700480 } else if (!strcasecmp(node->name, "media")) {
481 /* App-specific directories inside; let anyone traverse */
482 node->perm = PERM_ANDROID_MEDIA;
483 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700484 }
485 break;
486 case PERM_ANDROID_DATA:
487 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700488 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700489 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700490 if (appid != 0) {
491 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700492 }
493 node->mode = 0770;
494 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700495 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700496
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700497 node->mode = node->mode & ~fuse->mask;
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700498}
499
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700500/* Kernel has already enforced everything we returned through
501 * derive_permissions_locked(), so this is used to lock down access
502 * even further, such as enforcing that apps hold sdcard_rw. */
503static bool check_caller_access_to_name(struct fuse* fuse,
504 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700505 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700506 /* Always block security-sensitive files at root */
507 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700508 if (!strcasecmp(name, "autorun.inf")
509 || !strcasecmp(name, ".android_secure")
510 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700511 return false;
512 }
513 }
514
Jeff Sharkey44d63422013-09-12 09:44:48 -0700515 /* Root always has access; access for any other UIDs should always
516 * be controlled through packages.list. */
517 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700518 return true;
519 }
520
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700521 /* No extra permissions to enforce */
522 return true;
523}
524
525static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700526 const struct fuse_in_header *hdr, const struct node* node, int mode) {
527 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700528}
529
Jeff Brown6249b902012-05-26 14:32:54 -0700530struct node *create_node_locked(struct fuse* fuse,
531 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700532{
533 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700534 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700535
Narayan Kamathfaa09352015-01-13 18:21:10 +0000536 // Detect overflows in the inode counter. "4 billion nodes should be enough
537 // for everybody".
538 if (fuse->inode_ctr == 0) {
539 ERROR("No more inode numbers available");
540 return NULL;
541 }
542
Paul Eastham11ccdb32010-10-14 11:04:26 -0700543 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700544 if (!node) {
545 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700546 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700547 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700548 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700549 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700550 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700551 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700552 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700553 if (strcmp(name, actual_name)) {
554 node->actual_name = malloc(namelen + 1);
555 if (!node->actual_name) {
556 free(node->name);
557 free(node);
558 return NULL;
559 }
560 memcpy(node->actual_name, actual_name, namelen + 1);
561 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700562 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700563 node->nid = ptr_to_id(node);
Narayan Kamathfaa09352015-01-13 18:21:10 +0000564 node->ino = fuse->inode_ctr++;
Jeff Brown6249b902012-05-26 14:32:54 -0700565 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700566
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200567 node->deleted = false;
568
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700569 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700570 acquire_node_locked(node);
571 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700572 return node;
573}
574
Jeff Brown6249b902012-05-26 14:32:54 -0700575static int rename_node_locked(struct node *node, const char *name,
576 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700577{
Jeff Brown6249b902012-05-26 14:32:54 -0700578 size_t namelen = strlen(name);
579 int need_actual_name = strcmp(name, actual_name);
580
581 /* make the storage bigger without actually changing the name
582 * in case an error occurs part way */
583 if (namelen > node->namelen) {
584 char* new_name = realloc(node->name, namelen + 1);
585 if (!new_name) {
586 return -ENOMEM;
587 }
588 node->name = new_name;
589 if (need_actual_name && node->actual_name) {
590 char* new_actual_name = realloc(node->actual_name, namelen + 1);
591 if (!new_actual_name) {
592 return -ENOMEM;
593 }
594 node->actual_name = new_actual_name;
595 }
596 }
597
598 /* update the name, taking care to allocate storage before overwriting the old name */
599 if (need_actual_name) {
600 if (!node->actual_name) {
601 node->actual_name = malloc(namelen + 1);
602 if (!node->actual_name) {
603 return -ENOMEM;
604 }
605 }
606 memcpy(node->actual_name, actual_name, namelen + 1);
607 } else {
608 free(node->actual_name);
609 node->actual_name = NULL;
610 }
611 memcpy(node->name, name, namelen + 1);
612 node->namelen = namelen;
613 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700614}
615
Jeff Brown6249b902012-05-26 14:32:54 -0700616static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700617{
Jeff Brown6249b902012-05-26 14:32:54 -0700618 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700619 return &fuse->root;
620 } else {
621 return id_to_ptr(nid);
622 }
623}
624
Jeff Brown6249b902012-05-26 14:32:54 -0700625static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
626 char* buf, size_t bufsize)
627{
628 struct node* node = lookup_node_by_id_locked(fuse, nid);
629 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
630 node = NULL;
631 }
632 return node;
633}
634
635static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700636{
637 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700638 /* use exact string comparison, nodes that differ by case
639 * must be considered distinct even if they refer to the same
640 * underlying file as otherwise operations such as "mv x x"
641 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200642 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700643 return node;
644 }
645 }
646 return 0;
647}
648
Jeff Brown6249b902012-05-26 14:32:54 -0700649static struct node* acquire_or_create_child_locked(
650 struct fuse* fuse, struct node* parent,
651 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700652{
Jeff Brown6249b902012-05-26 14:32:54 -0700653 struct node* child = lookup_child_by_name_locked(parent, name);
654 if (child) {
655 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800656 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700657 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800658 }
Jeff Brown6249b902012-05-26 14:32:54 -0700659 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700660}
661
Jeff Brown6249b902012-05-26 14:32:54 -0700662static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700663{
664 struct fuse_out_header hdr;
665 hdr.len = sizeof(hdr);
666 hdr.error = err;
667 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700668 write(fuse->fd, &hdr, sizeof(hdr));
669}
670
Jeff Brown6249b902012-05-26 14:32:54 -0700671static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700672{
673 struct fuse_out_header hdr;
674 struct iovec vec[2];
675 int res;
676
677 hdr.len = len + sizeof(hdr);
678 hdr.error = 0;
679 hdr.unique = unique;
680
681 vec[0].iov_base = &hdr;
682 vec[0].iov_len = sizeof(hdr);
683 vec[1].iov_base = data;
684 vec[1].iov_len = len;
685
686 res = writev(fuse->fd, vec, 2);
687 if (res < 0) {
688 ERROR("*** REPLY FAILED *** %d\n", errno);
689 }
690}
691
Jeff Brown6249b902012-05-26 14:32:54 -0700692static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
693 struct node* parent, const char* name, const char* actual_name,
694 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700695{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700696 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700697 struct fuse_entry_out out;
698 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700699
Jeff Brown6249b902012-05-26 14:32:54 -0700700 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700701 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700702 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700703
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700704 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700705 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
706 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700707 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700708 return -ENOMEM;
709 }
710 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700711 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700712 out.attr_valid = 10;
713 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700714 out.nodeid = node->nid;
715 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700716 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700717 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700718 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700719}
720
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700721static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700722 const char* path)
723{
724 struct fuse_attr_out out;
725 struct stat s;
726
727 if (lstat(path, &s) < 0) {
728 return -errno;
729 }
730 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700731 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700732 out.attr_valid = 10;
733 fuse_reply(fuse, unique, &out, sizeof(out));
734 return NO_STATUS;
735}
736
737static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700738 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700739{
Jeff Brown6249b902012-05-26 14:32:54 -0700740 struct node* parent_node;
741 char parent_path[PATH_MAX];
742 char child_path[PATH_MAX];
743 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700744
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700745 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700746 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
747 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100748 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700749 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700750 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700751
752 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
753 child_path, sizeof(child_path), 1))) {
754 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700755 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700756 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700757 return -EACCES;
758 }
759
Jeff Brown6249b902012-05-26 14:32:54 -0700760 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700761}
762
Jeff Brown6249b902012-05-26 14:32:54 -0700763static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700764 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
765{
Jeff Brown6249b902012-05-26 14:32:54 -0700766 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700767
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700768 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700769 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100770 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700771 hdr->nodeid, node ? node->name : "?");
772 if (node) {
773 __u64 n = req->nlookup;
774 while (n--) {
775 release_node_locked(node);
776 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700777 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700778 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700779 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700780}
781
Jeff Brown6249b902012-05-26 14:32:54 -0700782static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700783 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
784{
Jeff Brown6249b902012-05-26 14:32:54 -0700785 struct node* node;
786 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700787
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700788 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700789 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100790 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700791 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700792 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700793
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700794 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700795 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700796 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700797 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700798 return -EACCES;
799 }
800
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700801 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700802}
803
Jeff Brown6249b902012-05-26 14:32:54 -0700804static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700805 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
806{
Jeff Brown6249b902012-05-26 14:32:54 -0700807 struct node* node;
808 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700809 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700810
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700811 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700812 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100813 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700814 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700815 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700816
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700817 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700818 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700819 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800820
821 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700822 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700823 return -EACCES;
824 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700825
Jeff Brown6249b902012-05-26 14:32:54 -0700826 /* XXX: incomplete implementation on purpose.
827 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700828
Elliott Hughes853574d2014-07-31 12:03:03 -0700829 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700830 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700831 }
832
833 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
834 * are both set, then set it to the current time. Else, set it to the
835 * time specified in the request. Same goes for mtime. Use utimensat(2)
836 * as it allows ATIME and MTIME to be changed independently, and has
837 * nanosecond resolution which fuse also has.
838 */
839 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
840 times[0].tv_nsec = UTIME_OMIT;
841 times[1].tv_nsec = UTIME_OMIT;
842 if (req->valid & FATTR_ATIME) {
843 if (req->valid & FATTR_ATIME_NOW) {
844 times[0].tv_nsec = UTIME_NOW;
845 } else {
846 times[0].tv_sec = req->atime;
847 times[0].tv_nsec = req->atimensec;
848 }
849 }
850 if (req->valid & FATTR_MTIME) {
851 if (req->valid & FATTR_MTIME_NOW) {
852 times[1].tv_nsec = UTIME_NOW;
853 } else {
854 times[1].tv_sec = req->mtime;
855 times[1].tv_nsec = req->mtimensec;
856 }
857 }
Jeff Brown6249b902012-05-26 14:32:54 -0700858 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
859 handler->token, path, times[0].tv_sec, times[1].tv_sec);
860 if (utimensat(-1, path, times, 0) < 0) {
861 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700862 }
863 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700864 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865}
866
Jeff Brown6249b902012-05-26 14:32:54 -0700867static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
869{
Jeff Brown6249b902012-05-26 14:32:54 -0700870 struct node* parent_node;
871 char parent_path[PATH_MAX];
872 char child_path[PATH_MAX];
873 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700874
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700875 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700876 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
877 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100878 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700879 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700880 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700881
882 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
883 child_path, sizeof(child_path), 1))) {
884 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700886 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700887 return -EACCES;
888 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700889 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700890 if (mknod(child_path, mode, req->rdev) < 0) {
891 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700892 }
Jeff Brown6249b902012-05-26 14:32:54 -0700893 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894}
895
Jeff Brown6249b902012-05-26 14:32:54 -0700896static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
898{
Jeff Brown6249b902012-05-26 14:32:54 -0700899 struct node* parent_node;
900 char parent_path[PATH_MAX];
901 char child_path[PATH_MAX];
902 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700903
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700904 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700905 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
906 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100907 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700908 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700909 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700910
911 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
912 child_path, sizeof(child_path), 1))) {
913 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700914 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700915 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700916 return -EACCES;
917 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700918 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700919 if (mkdir(child_path, mode) < 0) {
920 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700921 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700922
923 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
924 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
925 char nomedia[PATH_MAX];
926 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
927 if (touch(nomedia, 0664) != 0) {
928 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
929 return -ENOENT;
930 }
931 }
932 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
933 char nomedia[PATH_MAX];
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700934 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700935 if (touch(nomedia, 0664) != 0) {
936 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
937 return -ENOENT;
938 }
939 }
940
Jeff Brown6249b902012-05-26 14:32:54 -0700941 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700942}
943
Jeff Brown6249b902012-05-26 14:32:54 -0700944static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945 const struct fuse_in_header* hdr, const char* name)
946{
Jeff Brown6249b902012-05-26 14:32:54 -0700947 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200948 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700949 char parent_path[PATH_MAX];
950 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700952 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700953 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
954 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100955 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700956 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700957 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700958
959 if (!parent_node || !find_file_within(parent_path, name,
960 child_path, sizeof(child_path), 1)) {
961 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700962 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700963 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700964 return -EACCES;
965 }
Jeff Brown6249b902012-05-26 14:32:54 -0700966 if (unlink(child_path) < 0) {
967 return -errno;
968 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700969 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200970 child_node = lookup_child_by_name_locked(parent_node, name);
971 if (child_node) {
972 child_node->deleted = true;
973 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700974 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700975 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700976}
977
Jeff Brown6249b902012-05-26 14:32:54 -0700978static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979 const struct fuse_in_header* hdr, const char* name)
980{
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200981 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700982 struct node* parent_node;
983 char parent_path[PATH_MAX];
984 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700985
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700986 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700987 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
988 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100989 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700990 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700991 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700992
993 if (!parent_node || !find_file_within(parent_path, name,
994 child_path, sizeof(child_path), 1)) {
995 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700997 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700998 return -EACCES;
999 }
Jeff Brown6249b902012-05-26 14:32:54 -07001000 if (rmdir(child_path) < 0) {
1001 return -errno;
1002 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001003 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001004 child_node = lookup_child_by_name_locked(parent_node, name);
1005 if (child_node) {
1006 child_node->deleted = true;
1007 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001008 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001009 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001010}
1011
Jeff Brown6249b902012-05-26 14:32:54 -07001012static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001013 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001014 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001015{
Jeff Brown6249b902012-05-26 14:32:54 -07001016 struct node* old_parent_node;
1017 struct node* new_parent_node;
1018 struct node* child_node;
1019 char old_parent_path[PATH_MAX];
1020 char new_parent_path[PATH_MAX];
1021 char old_child_path[PATH_MAX];
1022 char new_child_path[PATH_MAX];
1023 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001024 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001025
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001026 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001027 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1028 old_parent_path, sizeof(old_parent_path));
1029 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1030 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001031 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001032 old_name, new_name,
1033 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1034 req->newdir, new_parent_node ? new_parent_node->name : "?");
1035 if (!old_parent_node || !new_parent_node) {
1036 res = -ENOENT;
1037 goto lookup_error;
1038 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001039 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001040 res = -EACCES;
1041 goto lookup_error;
1042 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001043 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001044 res = -EACCES;
1045 goto lookup_error;
1046 }
Jeff Brown6249b902012-05-26 14:32:54 -07001047 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1048 if (!child_node || get_node_path_locked(child_node,
1049 old_child_path, sizeof(old_child_path)) < 0) {
1050 res = -ENOENT;
1051 goto lookup_error;
1052 }
1053 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001054 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001055
1056 /* Special case for renaming a file where destination is same path
1057 * differing only by case. In this case we don't want to look for a case
1058 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1059 */
1060 int search = old_parent_node != new_parent_node
1061 || strcasecmp(old_name, new_name);
1062 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1063 new_child_path, sizeof(new_child_path), search))) {
1064 res = -ENOENT;
1065 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001066 }
1067
Jeff Brown6249b902012-05-26 14:32:54 -07001068 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1069 res = rename(old_child_path, new_child_path);
1070 if (res < 0) {
1071 res = -errno;
1072 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073 }
1074
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001075 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001076 res = rename_node_locked(child_node, new_name, new_actual_name);
1077 if (!res) {
1078 remove_node_from_parent_locked(child_node);
1079 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001080 }
Jeff Brown6249b902012-05-26 14:32:54 -07001081 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001082
Jeff Brown6249b902012-05-26 14:32:54 -07001083io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001084 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001085done:
1086 release_node_locked(child_node);
1087lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001088 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001089 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001090}
1091
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001092static int open_flags_to_access_mode(int open_flags) {
1093 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1094 return R_OK;
1095 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1096 return W_OK;
1097 } else {
1098 /* Probably O_RDRW, but treat as default to be safe */
1099 return R_OK | W_OK;
1100 }
1101}
1102
Jeff Brown6249b902012-05-26 14:32:54 -07001103static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001104 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1105{
Jeff Brown6249b902012-05-26 14:32:54 -07001106 struct node* node;
1107 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001108 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001109 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001110
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001111 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001112 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001113 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001114 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001115 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001116
1117 if (!node) {
1118 return -ENOENT;
1119 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001120 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001121 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001122 return -EACCES;
1123 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001124 h = malloc(sizeof(*h));
1125 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001126 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001127 }
Jeff Brown6249b902012-05-26 14:32:54 -07001128 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001129 h->fd = open(path, req->flags);
1130 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001131 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001132 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001133 }
1134 out.fh = ptr_to_id(h);
1135 out.open_flags = 0;
1136 out.padding = 0;
1137 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001138 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001139}
1140
Jeff Brown6249b902012-05-26 14:32:54 -07001141static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1143{
1144 struct handle *h = id_to_ptr(req->fh);
1145 __u64 unique = hdr->unique;
1146 __u32 size = req->size;
1147 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001148 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001149 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001150
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001151 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1152 * overlaps the request buffer and will clobber data in the request. This
1153 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001154
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001155 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1156 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001157 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001158 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001159 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001160 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001161 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001162 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001163 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001164 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001165 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001166}
1167
Jeff Brown6249b902012-05-26 14:32:54 -07001168static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001169 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1170 const void* buffer)
1171{
1172 struct fuse_write_out out;
1173 struct handle *h = id_to_ptr(req->fh);
1174 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001175 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001176
Arpad Horvath49e93442014-02-18 10:18:25 +01001177 if (req->flags & O_DIRECT) {
1178 memcpy(aligned_buffer, buffer, req->size);
1179 buffer = (const __u8*) aligned_buffer;
1180 }
Jeff Brown6249b902012-05-26 14:32:54 -07001181
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001182 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001183 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001184 res = pwrite64(h->fd, buffer, req->size, req->offset);
1185 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001186 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001187 }
1188 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001189 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001191 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001192}
1193
Jeff Brown6249b902012-05-26 14:32:54 -07001194static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195 const struct fuse_in_header* hdr)
1196{
Jeff Brown6249b902012-05-26 14:32:54 -07001197 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198 struct statfs stat;
1199 struct fuse_statfs_out out;
1200 int res;
1201
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001202 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001203 TRACE("[%d] STATFS\n", handler->token);
1204 res = get_node_path_locked(&fuse->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001205 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001206 if (res < 0) {
1207 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001208 }
Jeff Brown6249b902012-05-26 14:32:54 -07001209 if (statfs(fuse->root.name, &stat) < 0) {
1210 return -errno;
1211 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 memset(&out, 0, sizeof(out));
1213 out.st.blocks = stat.f_blocks;
1214 out.st.bfree = stat.f_bfree;
1215 out.st.bavail = stat.f_bavail;
1216 out.st.files = stat.f_files;
1217 out.st.ffree = stat.f_ffree;
1218 out.st.bsize = stat.f_bsize;
1219 out.st.namelen = stat.f_namelen;
1220 out.st.frsize = stat.f_frsize;
1221 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001222 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001223}
1224
Jeff Brown6249b902012-05-26 14:32:54 -07001225static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001226 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1227{
1228 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001229
1230 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001231 close(h->fd);
1232 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001233 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001234}
1235
Jeff Brown6249b902012-05-26 14:32:54 -07001236static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1238{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001239 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1240 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001241
Elliott Hughesf6d67372014-07-08 14:38:26 -07001242 int fd = -1;
1243 if (is_dir) {
1244 struct dirhandle *dh = id_to_ptr(req->fh);
1245 fd = dirfd(dh->d);
1246 } else {
1247 struct handle *h = id_to_ptr(req->fh);
1248 fd = h->fd;
1249 }
1250
1251 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1252 is_dir ? "FSYNCDIR" : "FSYNC",
1253 id_to_ptr(req->fh), fd, is_data_sync);
1254 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1255 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001256 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001257 }
Jeff Brown6249b902012-05-26 14:32:54 -07001258 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001259}
1260
Jeff Brown6249b902012-05-26 14:32:54 -07001261static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262 const struct fuse_in_header* hdr)
1263{
Jeff Brown6249b902012-05-26 14:32:54 -07001264 TRACE("[%d] FLUSH\n", handler->token);
1265 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266}
1267
Jeff Brown6249b902012-05-26 14:32:54 -07001268static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1270{
Jeff Brown6249b902012-05-26 14:32:54 -07001271 struct node* node;
1272 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001273 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001276 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001277 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001278 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001279 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001280 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001281
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001282 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001283 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001284 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001285 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001286 return -EACCES;
1287 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001288 h = malloc(sizeof(*h));
1289 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001290 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001291 }
Jeff Brown6249b902012-05-26 14:32:54 -07001292 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001293 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001294 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001295 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001296 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 }
1298 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001299 out.open_flags = 0;
1300 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001301 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001302 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001303}
1304
Jeff Brown6249b902012-05-26 14:32:54 -07001305static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001306 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1307{
1308 char buffer[8192];
1309 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1310 struct dirent *de;
1311 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001312
1313 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001314 if (req->offset == 0) {
1315 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001316 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001317 rewinddir(h->d);
1318 }
1319 de = readdir(h->d);
1320 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001321 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001322 }
1323 fde->ino = FUSE_UNKNOWN_INO;
1324 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1325 fde->off = req->offset + 1;
1326 fde->type = de->d_type;
1327 fde->namelen = strlen(de->d_name);
1328 memcpy(fde->name, de->d_name, fde->namelen + 1);
1329 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001330 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1331 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001332}
1333
Jeff Brown6249b902012-05-26 14:32:54 -07001334static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001335 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1336{
1337 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001338
1339 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001340 closedir(h->d);
1341 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001342 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001343}
1344
Jeff Brown6249b902012-05-26 14:32:54 -07001345static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1347{
1348 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001349 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001350
Jeff Brown6249b902012-05-26 14:32:54 -07001351 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1352 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001353
1354 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1355 * defined (fuse version 7.6). The structure is the same from 7.6 through
1356 * 7.22. Beginning with 7.23, the structure increased in size and added
1357 * new parameters.
1358 */
1359 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1360 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1361 req->major, req->minor, FUSE_KERNEL_VERSION);
1362 return -1;
1363 }
1364
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001365 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1366 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001367 fuse_struct_size = sizeof(out);
1368#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1369 /* FUSE_KERNEL_VERSION >= 23. */
1370
1371 /* If the kernel only works on minor revs older than or equal to 22,
1372 * then use the older structure size since this code only uses the 7.22
1373 * version of the structure. */
1374 if (req->minor <= 22) {
1375 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1376 }
1377#endif
1378
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001379 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 out.max_readahead = req->max_readahead;
1381 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1382 out.max_background = 32;
1383 out.congestion_threshold = 32;
1384 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001385 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001386 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001387}
1388
Jeff Brown6249b902012-05-26 14:32:54 -07001389static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001390 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1391{
Brian Swetland03ee9472010-08-12 18:01:08 -07001392 switch (hdr->opcode) {
1393 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001394 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001395 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001396 }
Jeff Brown84715842012-05-25 14:07:47 -07001397
Brian Swetland03ee9472010-08-12 18:01:08 -07001398 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001399 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001400 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001401 }
Jeff Brown84715842012-05-25 14:07:47 -07001402
Brian Swetland03ee9472010-08-12 18:01:08 -07001403 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001404 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001405 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001406 }
Jeff Brown84715842012-05-25 14:07:47 -07001407
Brian Swetland03ee9472010-08-12 18:01:08 -07001408 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001409 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001410 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001411 }
Jeff Brown84715842012-05-25 14:07:47 -07001412
Brian Swetland03ee9472010-08-12 18:01:08 -07001413// case FUSE_READLINK:
1414// case FUSE_SYMLINK:
1415 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001416 const struct fuse_mknod_in *req = data;
1417 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001418 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001419 }
Jeff Brown84715842012-05-25 14:07:47 -07001420
Brian Swetland03ee9472010-08-12 18:01:08 -07001421 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001422 const struct fuse_mkdir_in *req = data;
1423 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001424 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001425 }
Jeff Brown84715842012-05-25 14:07:47 -07001426
Brian Swetland03ee9472010-08-12 18:01:08 -07001427 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001428 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001429 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001430 }
Jeff Brown84715842012-05-25 14:07:47 -07001431
Brian Swetland03ee9472010-08-12 18:01:08 -07001432 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001433 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001434 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001435 }
Jeff Brown84715842012-05-25 14:07:47 -07001436
Brian Swetland03ee9472010-08-12 18:01:08 -07001437 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001438 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001439 const char *old_name = ((const char*) data) + sizeof(*req);
1440 const char *new_name = old_name + strlen(old_name) + 1;
1441 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001442 }
Jeff Brown84715842012-05-25 14:07:47 -07001443
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001444// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001445 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001446 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001447 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001448 }
Jeff Brown84715842012-05-25 14:07:47 -07001449
Brian Swetland03ee9472010-08-12 18:01:08 -07001450 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001451 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001452 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001453 }
Jeff Brown84715842012-05-25 14:07:47 -07001454
Brian Swetland03ee9472010-08-12 18:01:08 -07001455 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001456 const struct fuse_write_in *req = data;
1457 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001458 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001459 }
Jeff Brown84715842012-05-25 14:07:47 -07001460
Mike Lockwood4553b082010-08-16 14:14:44 -04001461 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001462 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001463 }
Jeff Brown84715842012-05-25 14:07:47 -07001464
Brian Swetland03ee9472010-08-12 18:01:08 -07001465 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001466 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001467 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001468 }
Jeff Brown84715842012-05-25 14:07:47 -07001469
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001470 case FUSE_FSYNC:
1471 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001472 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001473 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001474 }
1475
Brian Swetland03ee9472010-08-12 18:01:08 -07001476// case FUSE_SETXATTR:
1477// case FUSE_GETXATTR:
1478// case FUSE_LISTXATTR:
1479// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001480 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001481 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001482 }
1483
Brian Swetland03ee9472010-08-12 18:01:08 -07001484 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001485 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001486 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001487 }
Jeff Brown84715842012-05-25 14:07:47 -07001488
Brian Swetland03ee9472010-08-12 18:01:08 -07001489 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001490 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001491 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001492 }
Jeff Brown84715842012-05-25 14:07:47 -07001493
Brian Swetland03ee9472010-08-12 18:01:08 -07001494 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001495 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001496 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001497 }
Jeff Brown84715842012-05-25 14:07:47 -07001498
Brian Swetland03ee9472010-08-12 18:01:08 -07001499 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001500 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001501 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001502 }
Jeff Brown84715842012-05-25 14:07:47 -07001503
Brian Swetland03ee9472010-08-12 18:01:08 -07001504 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001505 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001506 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1507 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001508 }
Jeff Brown84715842012-05-25 14:07:47 -07001509 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001510}
1511
Jeff Brown6249b902012-05-26 14:32:54 -07001512static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001513{
Jeff Brown6249b902012-05-26 14:32:54 -07001514 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001515 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001516 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1517 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001518 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001519 if (errno == ENODEV) {
1520 ERROR("[%d] someone stole our marbles!\n", handler->token);
1521 exit(2);
1522 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001523 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001524 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001525 }
Jeff Brown84715842012-05-25 14:07:47 -07001526
1527 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001528 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1529 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001530 }
1531
Jeff Brown7729d242012-05-25 15:35:28 -07001532 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001533 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001534 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1535 handler->token, (size_t)len, hdr->len);
1536 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001537 }
1538
Jeff Brown7729d242012-05-25 15:35:28 -07001539 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001540 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001541 __u64 unique = hdr->unique;
1542 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001543
1544 /* We do not access the request again after this point because the underlying
1545 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001546
1547 if (res != NO_STATUS) {
1548 if (res) {
1549 TRACE("[%d] ERROR %d\n", handler->token, res);
1550 }
1551 fuse_status(fuse, unique, res);
1552 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 }
1554}
1555
Jeff Brown6249b902012-05-26 14:32:54 -07001556static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001557{
Jeff Brown6249b902012-05-26 14:32:54 -07001558 struct fuse_handler* handler = data;
1559 handle_fuse_requests(handler);
1560 return NULL;
1561}
1562
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001563static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001564 Hashmap* map = context;
1565 hashmapRemove(map, key);
1566 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001567 return true;
1568}
1569
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001570static int read_package_list(struct fuse_global* global) {
1571 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001572
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001573 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001574
1575 FILE* file = fopen(kPackagesListFile, "r");
1576 if (!file) {
1577 ERROR("failed to open package list: %s\n", strerror(errno));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001578 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001579 return -1;
1580 }
1581
1582 char buf[512];
1583 while (fgets(buf, sizeof(buf), file) != NULL) {
1584 char package_name[512];
1585 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001586 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001587
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001588 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1589 char* package_name_dup = strdup(package_name);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001590 hashmapPut(global->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001591 }
1592 }
1593
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001594 TRACE("read_package_list: found %zu packages\n",
1595 hashmapSize(global->package_to_appid));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001596 fclose(file);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001597 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001598 return 0;
1599}
1600
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001601static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001602 struct inotify_event *event;
1603 char event_buf[512];
1604
1605 int nfd = inotify_init();
1606 if (nfd < 0) {
1607 ERROR("inotify_init failed: %s\n", strerror(errno));
1608 return;
1609 }
1610
1611 bool active = false;
1612 while (1) {
1613 if (!active) {
1614 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1615 if (res == -1) {
1616 if (errno == ENOENT || errno == EACCES) {
1617 /* Framework may not have created yet, sleep and retry */
1618 ERROR("missing packages.list; retrying\n");
1619 sleep(3);
1620 continue;
1621 } else {
1622 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1623 return;
1624 }
1625 }
1626
1627 /* Watch above will tell us about any future changes, so
1628 * read the current state. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001629 if (read_package_list(global) == -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001630 ERROR("read_package_list failed: %s\n", strerror(errno));
1631 return;
1632 }
1633 active = true;
1634 }
1635
1636 int event_pos = 0;
1637 int res = read(nfd, event_buf, sizeof(event_buf));
1638 if (res < (int) sizeof(*event)) {
1639 if (errno == EINTR)
1640 continue;
1641 ERROR("failed to read inotify event: %s\n", strerror(errno));
1642 return;
1643 }
1644
1645 while (res >= (int) sizeof(*event)) {
1646 int event_size;
1647 event = (struct inotify_event *) (event_buf + event_pos);
1648
1649 TRACE("inotify event: %08x\n", event->mask);
1650 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1651 /* Previously watched file was deleted, probably due to move
1652 * that swapped in new data; re-arm the watch and read. */
1653 active = false;
1654 }
1655
1656 event_size = sizeof(*event) + event->len;
1657 res -= event_size;
1658 event_pos += event_size;
1659 }
1660 }
1661}
1662
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001663static int usage() {
1664 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001665 " -u: specify UID to run as\n"
1666 " -g: specify GID to run as\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001667 " -m: source_path is multi-user\n"
1668 " -w: runtime_write mount has full write access\n"
1669 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001670 return 1;
1671}
1672
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001673static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001674 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001675
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001676 fuse->fd = open("/dev/fuse", O_RDWR);
1677 if (fuse->fd == -1) {
1678 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001679 return -1;
1680 }
1681
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001682 umount2(fuse->dest_path, MNT_DETACH);
1683
Jeff Brown26567352012-05-25 13:27:43 -07001684 snprintf(opts, sizeof(opts),
1685 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001686 fuse->fd, fuse->global->uid, fuse->global->gid);
1687 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1688 MS_NOATIME, opts) != 0) {
1689 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1690 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001691 }
1692
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001693 fuse->next_generation = 0;
1694 fuse->inode_ctr = 1;
1695 fuse->gid = gid;
1696 fuse->mask = mask;
1697
1698 memset(&fuse->root, 0, sizeof(fuse->root));
1699 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
1700 fuse->root.refcount = 2;
1701 fuse->root.namelen = strlen(fuse->source_path);
1702 fuse->root.name = strdup(fuse->source_path);
1703 fuse->root.userid = 0;
1704 fuse->root.uid = AID_ROOT;
1705 fuse->root.gid = fuse->gid;
1706
1707 if (fuse->global->multi_user) {
1708 fuse->root.perm = PERM_PRE_ROOT;
1709 fuse->root.mode = 0711;
1710 snprintf(fuse->obb_path, sizeof(fuse->obb_path), "%s/obb", fuse->source_path);
1711 } else {
1712 fuse->root.perm = PERM_ROOT;
1713 fuse->root.mode = 0771 & ~mask;
1714 snprintf(fuse->obb_path, sizeof(fuse->obb_path), "%s/Android/obb", fuse->source_path);
Jeff Brown26567352012-05-25 13:27:43 -07001715 }
1716
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001717 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001718}
1719
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001720static void run(const char* source_path, const char* label, uid_t uid,
1721 gid_t gid, bool multi_user, bool full_write) {
1722 struct fuse_global global;
1723 struct fuse fuse_default;
1724 struct fuse fuse_read;
1725 struct fuse fuse_write;
1726 struct fuse_handler handler_default;
1727 struct fuse_handler handler_read;
1728 struct fuse_handler handler_write;
1729 pthread_t thread_default;
1730 pthread_t thread_read;
1731 pthread_t thread_write;
1732
1733 memset(&global, 0, sizeof(global));
1734 memset(&fuse_default, 0, sizeof(fuse_default));
1735 memset(&fuse_read, 0, sizeof(fuse_read));
1736 memset(&fuse_write, 0, sizeof(fuse_write));
1737 memset(&handler_default, 0, sizeof(handler_default));
1738 memset(&handler_read, 0, sizeof(handler_read));
1739 memset(&handler_write, 0, sizeof(handler_write));
1740
1741 pthread_mutex_init(&global.lock, NULL);
1742 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1743 global.uid = uid;
1744 global.gid = gid;
1745 global.multi_user = multi_user;
1746
1747 fuse_default.global = &global;
1748 strcpy(fuse_default.source_path, source_path);
1749 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime_default/%s", label);
1750
1751 fuse_read.global = &global;
1752 strcpy(fuse_read.source_path, source_path);
1753 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime_read/%s", label);
1754
1755 fuse_write.global = &global;
1756 strcpy(fuse_write.source_path, source_path);
1757 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime_write/%s", label);
1758
1759 handler_default.fuse = &fuse_default;
1760 handler_read.fuse = &fuse_read;
1761 handler_write.fuse = &fuse_write;
1762
1763 umask(0);
1764
1765 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1766 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1767 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1768 ERROR("failed to fuse_setup\n");
1769 exit(1);
1770 }
1771
1772 /* Drop privs */
1773 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1774 ERROR("cannot setgroups: %s\n", strerror(errno));
1775 exit(1);
1776 }
1777 if (setgid(gid) < 0) {
1778 ERROR("cannot setgid: %s\n", strerror(errno));
1779 exit(1);
1780 }
1781 if (setuid(uid) < 0) {
1782 ERROR("cannot setuid: %s\n", strerror(errno));
1783 exit(1);
1784 }
1785
1786 if (multi_user) {
1787 fs_prepare_dir(fuse_default.obb_path, 0775, uid, gid);
1788 fs_prepare_dir(fuse_read.obb_path, 0775, uid, gid);
1789 fs_prepare_dir(fuse_write.obb_path, 0775, uid, gid);
1790 }
1791
1792 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1793 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1794 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1795 ERROR("failed to pthread_create\n");
1796 exit(1);
1797 }
1798
1799 watch_package_list(&global);
1800 ERROR("terminated prematurely\n");
1801 exit(1);
1802}
1803
1804int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001805 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001806 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001807 uid_t uid = 0;
1808 gid_t gid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001809 bool multi_user = false;
1810 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001811 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001812 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001813 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001814
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001815 int opt;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001816 while ((opt = getopt(argc, argv, "u:g:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001817 switch (opt) {
1818 case 'u':
1819 uid = strtoul(optarg, NULL, 10);
1820 break;
1821 case 'g':
1822 gid = strtoul(optarg, NULL, 10);
1823 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001824 case 'm':
1825 multi_user = true;
1826 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001827 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001828 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001829 break;
1830 case '?':
1831 default:
1832 return usage();
1833 }
1834 }
1835
1836 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001837 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001838 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001839 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001840 } else if (!label) {
1841 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001842 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001843 ERROR("too many arguments\n");
1844 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001845 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001846 }
1847
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001848 if (!source_path) {
1849 ERROR("no source path specified\n");
1850 return usage();
1851 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001852 if (!label) {
1853 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001854 return usage();
1855 }
Jeff Brown26567352012-05-25 13:27:43 -07001856 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001857 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001858 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001859 }
1860
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001861 rlim.rlim_cur = 8192;
1862 rlim.rlim_max = 8192;
1863 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1864 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1865 }
1866
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001867 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1868 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1869 sleep(1);
1870 }
1871
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001872 run(source_path, label, uid, gid, multi_user, full_write);
1873 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07001874}