blob: d3afcb1c5461e690e8ad05ce42007fa930c0a16c [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>
23#include <limits.h>
24#include <linux/fuse.h>
25#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070029#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070030#include <sys/mount.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070031#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070032#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040033#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080034#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070035#include <sys/uio.h>
36#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070037
Jeff Sharkey44d63422013-09-12 09:44:48 -070038#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070040#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070042
Brian Swetlandb14a2c62010-08-12 18:21:12 -070043#include <private/android_filesystem_config.h>
44
Brian Swetland03ee9472010-08-12 18:01:08 -070045/* README
46 *
47 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070048 *
Brian Swetland03ee9472010-08-12 18:01:08 -070049 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070050 * directory permissions (all files are given fixed owner, group, and
51 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070052 * changeable, symlinks and hardlinks are not createable, etc.
53 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070054 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070055 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * It must be run as root, but will drop to requested UID/GID as soon as it
57 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070058 *
59 * Things I believe to be true:
60 *
61 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
62 * CREAT) must bump that node's refcount
63 * - don't forget that FORGET can forget multiple references (req->nlookup)
64 * - if an op that returns a fuse_entry fails writing the reply to the
65 * kernel, you must rollback the refcount to reflect the reference the
66 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070067 *
68 * This daemon can also derive custom filesystem permissions based on directory
69 * structure when requested. These custom permissions support several features:
70 *
71 * - Apps can access their own files in /Android/data/com.example/ without
72 * requiring any additional GIDs.
73 * - Separate permissions for protecting directories like Pictures and Music.
74 * - Multi-user separation on the same physical device.
75 *
76 * The derived permissions look like this:
77 *
78 * rwxrwx--x root:sdcard_rw /
79 * rwxrwx--- root:sdcard_pics /Pictures
80 * rwxrwx--- root:sdcard_av /Music
81 *
82 * rwxrwx--x root:sdcard_rw /Android
83 * rwxrwx--x root:sdcard_rw /Android/data
84 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
85 * rwxrwx--x root:sdcard_rw /Android/obb/
86 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
87 *
88 * rwxrwx--- root:sdcard_all /Android/user
89 * rwxrwx--x root:sdcard_rw /Android/user/10
90 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070091 */
92
93#define FUSE_TRACE 0
94
95#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070096#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070097#else
98#define TRACE(x...) do {} while (0)
99#endif
100
Elliott Hughes300d5642014-07-08 13:53:26 -0700101#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700102
103#define FUSE_UNKNOWN_INO 0xffffffff
104
Jeff Brown84715842012-05-25 14:07:47 -0700105/* Maximum number of bytes to write in one request. */
106#define MAX_WRITE (256 * 1024)
107
108/* Maximum number of bytes to read in one request. */
109#define MAX_READ (128 * 1024)
110
111/* Largest possible request.
112 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
113 * the largest possible data payload. */
114#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
115
Jeff Brown6249b902012-05-26 14:32:54 -0700116/* Default number of threads. */
117#define DEFAULT_NUM_THREADS 2
118
119/* Pseudo-error constant used to indicate that no fuse status is needed
120 * or that a reply has already been written. */
121#define NO_STATUS 1
122
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700123/* Path to system-provided mapping of package name to appIds */
124static const char* const kPackagesListFile = "/data/system/packages.list";
125
126/* Supplementary groups to execute with */
127static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
128
129/* Permission mode for a specific node. Controls how file permissions
130 * are derived for children nodes. */
131typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700132 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700133 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700134 /* This node is one level above a normal root; used for legacy layouts
135 * which use the first level to represent user_id. */
136 PERM_LEGACY_PRE_ROOT,
137 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700138 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700139 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700145 /* This node is "/Android/media" */
146 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700147 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700148 PERM_ANDROID_USER,
149} perm_t;
150
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700151/* Permissions structure to derive */
152typedef enum {
153 DERIVE_NONE,
154 DERIVE_LEGACY,
155 DERIVE_UNIFIED,
156} derive_t;
157
Brian Swetland03ee9472010-08-12 18:01:08 -0700158struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700159 int fd;
160};
161
162struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700163 DIR *d;
164};
165
166struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700167 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700168 __u64 nid;
169 __u64 gen;
170
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700171 /* State derived based on current position in hierarchy. */
172 perm_t perm;
173 userid_t userid;
174 uid_t uid;
175 gid_t gid;
176 mode_t mode;
177
Paul Eastham11ccdb32010-10-14 11:04:26 -0700178 struct node *next; /* per-dir sibling list */
179 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700180 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700181
Jeff Brown6249b902012-05-26 14:32:54 -0700182 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700183 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800184 /* If non-null, this is the real name of the file in the underlying storage.
185 * This may differ from the field "name" only by case.
186 * strlen(actual_name) will always equal strlen(name), so it is safe to use
187 * namelen for both fields.
188 */
189 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700190
191 /* If non-null, an exact underlying path that should be grafted into this
192 * position. Used to support things like OBB. */
193 char* graft_path;
194 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700195};
196
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700197static int str_hash(void *key) {
198 return hashmapHash(key, strlen(key));
199}
200
Jeff Sharkey44d63422013-09-12 09:44:48 -0700201/** Test if two string keys are equal ignoring case */
202static bool str_icase_equals(void *keyA, void *keyB) {
203 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700204}
205
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700206static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800207 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700208}
209
210static bool int_equals(void *keyA, void *keyB) {
211 return keyA == keyB;
212}
213
Jeff Brown7729d242012-05-25 15:35:28 -0700214/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700215struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700216 pthread_mutex_t lock;
217
Brian Swetland03ee9472010-08-12 18:01:08 -0700218 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700219 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700220 derive_t derive;
221 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700222 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700223 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700224 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700225
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700226 Hashmap* package_to_appid;
227 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700228};
229
Jeff Brown7729d242012-05-25 15:35:28 -0700230/* Private data used by a single fuse handler. */
231struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700232 struct fuse* fuse;
233 int token;
234
Jeff Brown7729d242012-05-25 15:35:28 -0700235 /* To save memory, we never use the contents of the request buffer and the read
236 * buffer at the same time. This allows us to share the underlying storage. */
237 union {
238 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800239 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700240 };
241};
Brian Swetland03ee9472010-08-12 18:01:08 -0700242
Jeff Brown6249b902012-05-26 14:32:54 -0700243static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700244{
Jeff Brown6249b902012-05-26 14:32:54 -0700245 return (void *) (uintptr_t) nid;
246}
Brian Swetland03ee9472010-08-12 18:01:08 -0700247
Jeff Brown6249b902012-05-26 14:32:54 -0700248static inline __u64 ptr_to_id(void *ptr)
249{
250 return (__u64) (uintptr_t) ptr;
251}
Brian Swetland03ee9472010-08-12 18:01:08 -0700252
Jeff Brown6249b902012-05-26 14:32:54 -0700253static void acquire_node_locked(struct node* node)
254{
255 node->refcount++;
256 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
257}
258
259static void remove_node_from_parent_locked(struct node* node);
260
261static void release_node_locked(struct node* node)
262{
263 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
264 if (node->refcount > 0) {
265 node->refcount--;
266 if (!node->refcount) {
267 TRACE("DESTROY %p (%s)\n", node, node->name);
268 remove_node_from_parent_locked(node);
269
270 /* TODO: remove debugging - poison memory */
271 memset(node->name, 0xef, node->namelen);
272 free(node->name);
273 free(node->actual_name);
274 memset(node, 0xfc, sizeof(*node));
275 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800276 }
Jeff Brown6249b902012-05-26 14:32:54 -0700277 } else {
278 ERROR("Zero refcnt %p\n", node);
279 }
280}
281
282static void add_node_to_parent_locked(struct node *node, struct node *parent) {
283 node->parent = parent;
284 node->next = parent->child;
285 parent->child = node;
286 acquire_node_locked(parent);
287}
288
289static void remove_node_from_parent_locked(struct node* node)
290{
291 if (node->parent) {
292 if (node->parent->child == node) {
293 node->parent->child = node->parent->child->next;
294 } else {
295 struct node *node2;
296 node2 = node->parent->child;
297 while (node2->next != node)
298 node2 = node2->next;
299 node2->next = node->next;
300 }
301 release_node_locked(node->parent);
302 node->parent = NULL;
303 node->next = NULL;
304 }
305}
306
307/* Gets the absolute path to a node into the provided buffer.
308 *
309 * Populates 'buf' with the path and returns the length of the path on success,
310 * or returns -1 if the path is too long for the provided buffer.
311 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700312static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
313 const char* name;
314 size_t namelen;
315 if (node->graft_path) {
316 name = node->graft_path;
317 namelen = node->graft_pathlen;
318 } else if (node->actual_name) {
319 name = node->actual_name;
320 namelen = node->namelen;
321 } else {
322 name = node->name;
323 namelen = node->namelen;
324 }
325
Jeff Brown6249b902012-05-26 14:32:54 -0700326 if (bufsize < namelen + 1) {
327 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700328 }
329
Jeff Brown6249b902012-05-26 14:32:54 -0700330 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700331 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700332 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
333 if (pathlen < 0) {
334 return -1;
335 }
336 buf[pathlen++] = '/';
337 }
338
Jeff Brown6249b902012-05-26 14:32:54 -0700339 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
340 return pathlen + namelen;
341}
342
343/* Finds the absolute path of a file within a given directory.
344 * Performs a case-insensitive search for the file and sets the buffer to the path
345 * of the first matching file. If 'search' is zero or if no match is found, sets
346 * the buffer to the path that the file would have, assuming the name were case-sensitive.
347 *
348 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
349 * or returns NULL if the path is too long for the provided buffer.
350 */
351static char* find_file_within(const char* path, const char* name,
352 char* buf, size_t bufsize, int search)
353{
354 size_t pathlen = strlen(path);
355 size_t namelen = strlen(name);
356 size_t childlen = pathlen + namelen + 1;
357 char* actual;
358
359 if (bufsize <= childlen) {
360 return NULL;
361 }
362
363 memcpy(buf, path, pathlen);
364 buf[pathlen] = '/';
365 actual = buf + pathlen + 1;
366 memcpy(actual, name, namelen + 1);
367
368 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800369 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700370 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800371 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700372 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700373 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800374 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800375 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700376 if (!strcasecmp(entry->d_name, name)) {
377 /* we have a match - replace the name, don't need to copy the null again */
378 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800379 break;
380 }
381 }
382 closedir(dir);
383 }
Jeff Brown6249b902012-05-26 14:32:54 -0700384 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800385}
386
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700387static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800388{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700389 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700390 attr->size = s->st_size;
391 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400392 attr->atime = s->st_atime;
393 attr->mtime = s->st_mtime;
394 attr->ctime = s->st_ctime;
395 attr->atimensec = s->st_atime_nsec;
396 attr->mtimensec = s->st_mtime_nsec;
397 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700398 attr->mode = s->st_mode;
399 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700400
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700401 attr->uid = node->uid;
402 attr->gid = node->gid;
403
404 /* Filter requested mode based on underlying file, and
405 * pass through file type. */
406 int owner_mode = s->st_mode & 0700;
407 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
408 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
409}
410
Jeff Sharkey44d63422013-09-12 09:44:48 -0700411static int touch(char* path, mode_t mode) {
412 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
413 if (fd == -1) {
414 if (errno == EEXIST) {
415 return 0;
416 } else {
417 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
418 return -1;
419 }
420 }
421 close(fd);
422 return 0;
423}
424
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700425static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
426 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700427 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700428
429 /* By default, each node inherits from its parent */
430 node->perm = PERM_INHERIT;
431 node->userid = parent->userid;
432 node->uid = parent->uid;
433 node->gid = parent->gid;
434 node->mode = parent->mode;
435
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700436 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700437 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700438 }
439
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700440 /* Derive custom permissions based on parent and current node */
441 switch (parent->perm) {
442 case PERM_INHERIT:
443 /* Already inherited above */
444 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700445 case PERM_LEGACY_PRE_ROOT:
446 /* Legacy internal layout places users at top level */
447 node->perm = PERM_ROOT;
448 node->userid = strtoul(node->name, NULL, 10);
449 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700450 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700451 /* Assume masked off by default. */
452 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700453 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700454 /* App-specific directories inside; let anyone traverse */
455 node->perm = PERM_ANDROID;
456 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700457 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700458 if (!strcasecmp(node->name, "DCIM")
459 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700460 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700461 } else if (!strcasecmp(node->name, "Alarms")
462 || !strcasecmp(node->name, "Movies")
463 || !strcasecmp(node->name, "Music")
464 || !strcasecmp(node->name, "Notifications")
465 || !strcasecmp(node->name, "Podcasts")
466 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700467 node->gid = AID_SDCARD_AV;
468 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700469 }
470 break;
471 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700472 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700473 /* App-specific directories inside; let anyone traverse */
474 node->perm = PERM_ANDROID_DATA;
475 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700476 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700477 /* App-specific directories inside; let anyone traverse */
478 node->perm = PERM_ANDROID_OBB;
479 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700480 /* Single OBB directory is always shared */
481 node->graft_path = fuse->obbpath;
482 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700483 } else if (!strcasecmp(node->name, "media")) {
484 /* App-specific directories inside; let anyone traverse */
485 node->perm = PERM_ANDROID_MEDIA;
486 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700487 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700488 /* User directories must only be accessible to system, protected
489 * by sdcard_all. Zygote will bind mount the appropriate user-
490 * specific path. */
491 node->perm = PERM_ANDROID_USER;
492 node->gid = AID_SDCARD_ALL;
493 node->mode = 0770;
494 }
495 break;
496 case PERM_ANDROID_DATA:
497 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700498 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800499 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700500 if (appid != 0) {
501 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700502 }
503 node->mode = 0770;
504 break;
505 case PERM_ANDROID_USER:
506 /* Root of a secondary user */
507 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700508 node->userid = strtoul(node->name, NULL, 10);
509 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700510 node->mode = 0771;
511 break;
512 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700513}
514
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700515/* Return if the calling UID holds sdcard_rw. */
516static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700517 /* No additional permissions enforcement */
518 if (fuse->derive == DERIVE_NONE) {
519 return true;
520 }
521
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700522 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800523 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700524}
525
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700526/* Kernel has already enforced everything we returned through
527 * derive_permissions_locked(), so this is used to lock down access
528 * even further, such as enforcing that apps hold sdcard_rw. */
529static bool check_caller_access_to_name(struct fuse* fuse,
530 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700531 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700532 /* Always block security-sensitive files at root */
533 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700534 if (!strcasecmp(name, "autorun.inf")
535 || !strcasecmp(name, ".android_secure")
536 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700537 return false;
538 }
539 }
540
541 /* No additional permissions enforcement */
542 if (fuse->derive == DERIVE_NONE) {
543 return true;
544 }
545
Jeff Sharkey44d63422013-09-12 09:44:48 -0700546 /* Root always has access; access for any other UIDs should always
547 * be controlled through packages.list. */
548 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700549 return true;
550 }
551
552 /* If asking to write, verify that caller either owns the
553 * parent or holds sdcard_rw. */
554 if (mode & W_OK) {
555 if (parent_node && hdr->uid == parent_node->uid) {
556 return true;
557 }
558
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700559 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700560 }
561
562 /* No extra permissions to enforce */
563 return true;
564}
565
566static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700567 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
568 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700569}
570
Jeff Brown6249b902012-05-26 14:32:54 -0700571struct node *create_node_locked(struct fuse* fuse,
572 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700573{
574 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700575 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700576
Paul Eastham11ccdb32010-10-14 11:04:26 -0700577 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700578 if (!node) {
579 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700580 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700581 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700582 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700583 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700584 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700585 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700586 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700587 if (strcmp(name, actual_name)) {
588 node->actual_name = malloc(namelen + 1);
589 if (!node->actual_name) {
590 free(node->name);
591 free(node);
592 return NULL;
593 }
594 memcpy(node->actual_name, actual_name, namelen + 1);
595 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700596 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700597 node->nid = ptr_to_id(node);
598 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700599
600 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700601 acquire_node_locked(node);
602 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700603 return node;
604}
605
Jeff Brown6249b902012-05-26 14:32:54 -0700606static int rename_node_locked(struct node *node, const char *name,
607 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700608{
Jeff Brown6249b902012-05-26 14:32:54 -0700609 size_t namelen = strlen(name);
610 int need_actual_name = strcmp(name, actual_name);
611
612 /* make the storage bigger without actually changing the name
613 * in case an error occurs part way */
614 if (namelen > node->namelen) {
615 char* new_name = realloc(node->name, namelen + 1);
616 if (!new_name) {
617 return -ENOMEM;
618 }
619 node->name = new_name;
620 if (need_actual_name && node->actual_name) {
621 char* new_actual_name = realloc(node->actual_name, namelen + 1);
622 if (!new_actual_name) {
623 return -ENOMEM;
624 }
625 node->actual_name = new_actual_name;
626 }
627 }
628
629 /* update the name, taking care to allocate storage before overwriting the old name */
630 if (need_actual_name) {
631 if (!node->actual_name) {
632 node->actual_name = malloc(namelen + 1);
633 if (!node->actual_name) {
634 return -ENOMEM;
635 }
636 }
637 memcpy(node->actual_name, actual_name, namelen + 1);
638 } else {
639 free(node->actual_name);
640 node->actual_name = NULL;
641 }
642 memcpy(node->name, name, namelen + 1);
643 node->namelen = namelen;
644 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700645}
646
Jeff Brown6249b902012-05-26 14:32:54 -0700647static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700648{
Jeff Brown6249b902012-05-26 14:32:54 -0700649 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700650 return &fuse->root;
651 } else {
652 return id_to_ptr(nid);
653 }
654}
655
Jeff Brown6249b902012-05-26 14:32:54 -0700656static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
657 char* buf, size_t bufsize)
658{
659 struct node* node = lookup_node_by_id_locked(fuse, nid);
660 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
661 node = NULL;
662 }
663 return node;
664}
665
666static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700667{
668 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700669 /* use exact string comparison, nodes that differ by case
670 * must be considered distinct even if they refer to the same
671 * underlying file as otherwise operations such as "mv x x"
672 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700673 if (!strcmp(name, node->name)) {
674 return node;
675 }
676 }
677 return 0;
678}
679
Jeff Brown6249b902012-05-26 14:32:54 -0700680static struct node* acquire_or_create_child_locked(
681 struct fuse* fuse, struct node* parent,
682 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700683{
Jeff Brown6249b902012-05-26 14:32:54 -0700684 struct node* child = lookup_child_by_name_locked(parent, name);
685 if (child) {
686 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800687 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700688 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800689 }
Jeff Brown6249b902012-05-26 14:32:54 -0700690 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700691}
692
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700693static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700694 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700695 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700696
Jeff Brown6249b902012-05-26 14:32:54 -0700697 fuse->fd = fd;
698 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700699 fuse->derive = derive;
700 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700701 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700702
Jeff Brown6249b902012-05-26 14:32:54 -0700703 memset(&fuse->root, 0, sizeof(fuse->root));
704 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
705 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700706 fuse->root.namelen = strlen(source_path);
707 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700708 fuse->root.userid = 0;
709 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700710
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700711 /* Set up root node for various modes of operation */
712 switch (derive) {
713 case DERIVE_NONE:
714 /* Traditional behavior that treats entire device as being accessible
715 * to sdcard_rw, and no permissions are derived. */
716 fuse->root.perm = PERM_ROOT;
717 fuse->root.mode = 0775;
718 fuse->root.gid = AID_SDCARD_RW;
719 break;
720 case DERIVE_LEGACY:
721 /* Legacy behavior used to support internal multiuser layout which
722 * places user_id at the top directory level, with the actual roots
723 * just below that. Shared OBB path is also at top level. */
724 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
725 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700726 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700727 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700728 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
729 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700730 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700731 break;
732 case DERIVE_UNIFIED:
733 /* Unified multiuser layout which places secondary user_id under
734 * /Android/user and shared OBB path under /Android/obb. */
735 fuse->root.perm = PERM_ROOT;
736 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700737 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700738 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700739 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
740 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
741 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700742 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700743}
744
Jeff Brown6249b902012-05-26 14:32:54 -0700745static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700746{
747 struct fuse_out_header hdr;
748 hdr.len = sizeof(hdr);
749 hdr.error = err;
750 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700751 write(fuse->fd, &hdr, sizeof(hdr));
752}
753
Jeff Brown6249b902012-05-26 14:32:54 -0700754static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700755{
756 struct fuse_out_header hdr;
757 struct iovec vec[2];
758 int res;
759
760 hdr.len = len + sizeof(hdr);
761 hdr.error = 0;
762 hdr.unique = unique;
763
764 vec[0].iov_base = &hdr;
765 vec[0].iov_len = sizeof(hdr);
766 vec[1].iov_base = data;
767 vec[1].iov_len = len;
768
769 res = writev(fuse->fd, vec, 2);
770 if (res < 0) {
771 ERROR("*** REPLY FAILED *** %d\n", errno);
772 }
773}
774
Jeff Brown6249b902012-05-26 14:32:54 -0700775static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
776 struct node* parent, const char* name, const char* actual_name,
777 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700778{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700779 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700780 struct fuse_entry_out out;
781 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700782
Jeff Brown6249b902012-05-26 14:32:54 -0700783 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700784 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700785 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700786
Jeff Brown6249b902012-05-26 14:32:54 -0700787 pthread_mutex_lock(&fuse->lock);
788 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
789 if (!node) {
790 pthread_mutex_unlock(&fuse->lock);
791 return -ENOMEM;
792 }
793 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700794 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700795 out.attr_valid = 10;
796 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700797 out.nodeid = node->nid;
798 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700799 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700800 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700801 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700802}
803
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700804static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700805 const char* path)
806{
807 struct fuse_attr_out out;
808 struct stat s;
809
810 if (lstat(path, &s) < 0) {
811 return -errno;
812 }
813 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700814 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700815 out.attr_valid = 10;
816 fuse_reply(fuse, unique, &out, sizeof(out));
817 return NO_STATUS;
818}
819
820static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700821 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700822{
Jeff Brown6249b902012-05-26 14:32:54 -0700823 struct node* parent_node;
824 char parent_path[PATH_MAX];
825 char child_path[PATH_MAX];
826 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700827
Jeff Brown6249b902012-05-26 14:32:54 -0700828 pthread_mutex_lock(&fuse->lock);
829 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
830 parent_path, sizeof(parent_path));
831 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
832 parent_node ? parent_node->name : "?");
833 pthread_mutex_unlock(&fuse->lock);
834
835 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
836 child_path, sizeof(child_path), 1))) {
837 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700838 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700839 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700840 return -EACCES;
841 }
842
Jeff Brown6249b902012-05-26 14:32:54 -0700843 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700844}
845
Jeff Brown6249b902012-05-26 14:32:54 -0700846static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700847 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
848{
Jeff Brown6249b902012-05-26 14:32:54 -0700849 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700850
Jeff Brown6249b902012-05-26 14:32:54 -0700851 pthread_mutex_lock(&fuse->lock);
852 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
853 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
854 hdr->nodeid, node ? node->name : "?");
855 if (node) {
856 __u64 n = req->nlookup;
857 while (n--) {
858 release_node_locked(node);
859 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860 }
Jeff Brown6249b902012-05-26 14:32:54 -0700861 pthread_mutex_unlock(&fuse->lock);
862 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700863}
864
Jeff Brown6249b902012-05-26 14:32:54 -0700865static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700866 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
867{
Jeff Brown6249b902012-05-26 14:32:54 -0700868 struct node* node;
869 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700870
Jeff Brown6249b902012-05-26 14:32:54 -0700871 pthread_mutex_lock(&fuse->lock);
872 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
873 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
874 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
875 pthread_mutex_unlock(&fuse->lock);
876
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700877 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700878 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700880 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700881 return -EACCES;
882 }
883
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700884 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885}
886
Jeff Brown6249b902012-05-26 14:32:54 -0700887static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
889{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700890 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700891 struct node* node;
892 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700893 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894
Jeff Brown6249b902012-05-26 14:32:54 -0700895 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700896 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700897 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
898 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
899 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
900 pthread_mutex_unlock(&fuse->lock);
901
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700902 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700903 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700904 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700905 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700906 return -EACCES;
907 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700908
Jeff Brown6249b902012-05-26 14:32:54 -0700909 /* XXX: incomplete implementation on purpose.
910 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700911
Jeff Brown6249b902012-05-26 14:32:54 -0700912 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
913 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700914 }
915
916 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
917 * are both set, then set it to the current time. Else, set it to the
918 * time specified in the request. Same goes for mtime. Use utimensat(2)
919 * as it allows ATIME and MTIME to be changed independently, and has
920 * nanosecond resolution which fuse also has.
921 */
922 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
923 times[0].tv_nsec = UTIME_OMIT;
924 times[1].tv_nsec = UTIME_OMIT;
925 if (req->valid & FATTR_ATIME) {
926 if (req->valid & FATTR_ATIME_NOW) {
927 times[0].tv_nsec = UTIME_NOW;
928 } else {
929 times[0].tv_sec = req->atime;
930 times[0].tv_nsec = req->atimensec;
931 }
932 }
933 if (req->valid & FATTR_MTIME) {
934 if (req->valid & FATTR_MTIME_NOW) {
935 times[1].tv_nsec = UTIME_NOW;
936 } else {
937 times[1].tv_sec = req->mtime;
938 times[1].tv_nsec = req->mtimensec;
939 }
940 }
Jeff Brown6249b902012-05-26 14:32:54 -0700941 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
942 handler->token, path, times[0].tv_sec, times[1].tv_sec);
943 if (utimensat(-1, path, times, 0) < 0) {
944 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945 }
946 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700947 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948}
949
Jeff Brown6249b902012-05-26 14:32:54 -0700950static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
952{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700953 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700954 struct node* parent_node;
955 char parent_path[PATH_MAX];
956 char child_path[PATH_MAX];
957 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700958
Jeff Brown6249b902012-05-26 14:32:54 -0700959 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700960 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700961 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
962 parent_path, sizeof(parent_path));
963 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
964 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
965 pthread_mutex_unlock(&fuse->lock);
966
967 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
968 child_path, sizeof(child_path), 1))) {
969 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700971 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700972 return -EACCES;
973 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700975 if (mknod(child_path, mode, req->rdev) < 0) {
976 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700977 }
Jeff Brown6249b902012-05-26 14:32:54 -0700978 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979}
980
Jeff Brown6249b902012-05-26 14:32:54 -0700981static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700982 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
983{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700984 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700985 struct node* parent_node;
986 char parent_path[PATH_MAX];
987 char child_path[PATH_MAX];
988 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700989
Jeff Brown6249b902012-05-26 14:32:54 -0700990 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700991 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700992 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
993 parent_path, sizeof(parent_path));
994 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
995 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
996 pthread_mutex_unlock(&fuse->lock);
997
998 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
999 child_path, sizeof(child_path), 1))) {
1000 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001002 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001003 return -EACCES;
1004 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001006 if (mkdir(child_path, mode) < 0) {
1007 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001008 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001009
1010 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1011 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1012 char nomedia[PATH_MAX];
1013 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1014 if (touch(nomedia, 0664) != 0) {
1015 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1016 return -ENOENT;
1017 }
1018 }
1019 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1020 char nomedia[PATH_MAX];
1021 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1022 if (touch(nomedia, 0664) != 0) {
1023 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1024 return -ENOENT;
1025 }
1026 }
1027
Jeff Brown6249b902012-05-26 14:32:54 -07001028 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001029}
1030
Jeff Brown6249b902012-05-26 14:32:54 -07001031static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001032 const struct fuse_in_header* hdr, const char* name)
1033{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001034 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001035 struct node* parent_node;
1036 char parent_path[PATH_MAX];
1037 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001038
Jeff Brown6249b902012-05-26 14:32:54 -07001039 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001040 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001041 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1042 parent_path, sizeof(parent_path));
1043 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1044 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1045 pthread_mutex_unlock(&fuse->lock);
1046
1047 if (!parent_node || !find_file_within(parent_path, name,
1048 child_path, sizeof(child_path), 1)) {
1049 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001051 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001052 return -EACCES;
1053 }
Jeff Brown6249b902012-05-26 14:32:54 -07001054 if (unlink(child_path) < 0) {
1055 return -errno;
1056 }
1057 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001058}
1059
Jeff Brown6249b902012-05-26 14:32:54 -07001060static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001061 const struct fuse_in_header* hdr, const char* name)
1062{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001063 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001064 struct node* parent_node;
1065 char parent_path[PATH_MAX];
1066 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001067
Jeff Brown6249b902012-05-26 14:32:54 -07001068 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001069 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001070 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1071 parent_path, sizeof(parent_path));
1072 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1073 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1074 pthread_mutex_unlock(&fuse->lock);
1075
1076 if (!parent_node || !find_file_within(parent_path, name,
1077 child_path, sizeof(child_path), 1)) {
1078 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001079 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001080 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001081 return -EACCES;
1082 }
Jeff Brown6249b902012-05-26 14:32:54 -07001083 if (rmdir(child_path) < 0) {
1084 return -errno;
1085 }
1086 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001087}
1088
Jeff Brown6249b902012-05-26 14:32:54 -07001089static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001090 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001091 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001093 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001094 struct node* old_parent_node;
1095 struct node* new_parent_node;
1096 struct node* child_node;
1097 char old_parent_path[PATH_MAX];
1098 char new_parent_path[PATH_MAX];
1099 char old_child_path[PATH_MAX];
1100 char new_child_path[PATH_MAX];
1101 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001102 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001103
Jeff Brown6249b902012-05-26 14:32:54 -07001104 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001105 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001106 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1107 old_parent_path, sizeof(old_parent_path));
1108 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1109 new_parent_path, sizeof(new_parent_path));
1110 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1111 old_name, new_name,
1112 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1113 req->newdir, new_parent_node ? new_parent_node->name : "?");
1114 if (!old_parent_node || !new_parent_node) {
1115 res = -ENOENT;
1116 goto lookup_error;
1117 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001118 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001119 res = -EACCES;
1120 goto lookup_error;
1121 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001122 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001123 res = -EACCES;
1124 goto lookup_error;
1125 }
Jeff Brown6249b902012-05-26 14:32:54 -07001126 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1127 if (!child_node || get_node_path_locked(child_node,
1128 old_child_path, sizeof(old_child_path)) < 0) {
1129 res = -ENOENT;
1130 goto lookup_error;
1131 }
1132 acquire_node_locked(child_node);
1133 pthread_mutex_unlock(&fuse->lock);
1134
1135 /* Special case for renaming a file where destination is same path
1136 * differing only by case. In this case we don't want to look for a case
1137 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1138 */
1139 int search = old_parent_node != new_parent_node
1140 || strcasecmp(old_name, new_name);
1141 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1142 new_child_path, sizeof(new_child_path), search))) {
1143 res = -ENOENT;
1144 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001145 }
1146
Jeff Brown6249b902012-05-26 14:32:54 -07001147 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1148 res = rename(old_child_path, new_child_path);
1149 if (res < 0) {
1150 res = -errno;
1151 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001152 }
1153
Jeff Brown6249b902012-05-26 14:32:54 -07001154 pthread_mutex_lock(&fuse->lock);
1155 res = rename_node_locked(child_node, new_name, new_actual_name);
1156 if (!res) {
1157 remove_node_from_parent_locked(child_node);
1158 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001159 }
Jeff Brown6249b902012-05-26 14:32:54 -07001160 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001161
Jeff Brown6249b902012-05-26 14:32:54 -07001162io_error:
1163 pthread_mutex_lock(&fuse->lock);
1164done:
1165 release_node_locked(child_node);
1166lookup_error:
1167 pthread_mutex_unlock(&fuse->lock);
1168 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001169}
1170
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001171static int open_flags_to_access_mode(int open_flags) {
1172 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1173 return R_OK;
1174 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1175 return W_OK;
1176 } else {
1177 /* Probably O_RDRW, but treat as default to be safe */
1178 return R_OK | W_OK;
1179 }
1180}
1181
Jeff Brown6249b902012-05-26 14:32:54 -07001182static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1184{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001185 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001186 struct node* node;
1187 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001188 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190
Jeff Brown6249b902012-05-26 14:32:54 -07001191 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001192 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001193 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1194 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1195 req->flags, hdr->nodeid, node ? node->name : "?");
1196 pthread_mutex_unlock(&fuse->lock);
1197
1198 if (!node) {
1199 return -ENOENT;
1200 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001201 if (!check_caller_access_to_node(fuse, hdr, node,
1202 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001203 return -EACCES;
1204 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205 h = malloc(sizeof(*h));
1206 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001207 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001208 }
Jeff Brown6249b902012-05-26 14:32:54 -07001209 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001210 h->fd = open(path, req->flags);
1211 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001213 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214 }
1215 out.fh = ptr_to_id(h);
1216 out.open_flags = 0;
1217 out.padding = 0;
1218 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001219 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001220}
1221
Jeff Brown6249b902012-05-26 14:32:54 -07001222static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001223 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1224{
1225 struct handle *h = id_to_ptr(req->fh);
1226 __u64 unique = hdr->unique;
1227 __u32 size = req->size;
1228 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001229 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001230 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001231
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001232 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1233 * overlaps the request buffer and will clobber data in the request. This
1234 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001235
1236 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1237 h, h->fd, size, offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001238 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001239 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001240 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001241 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001242 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001243 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001245 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001246 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001247}
1248
Jeff Brown6249b902012-05-26 14:32:54 -07001249static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001250 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1251 const void* buffer)
1252{
1253 struct fuse_write_out out;
1254 struct handle *h = id_to_ptr(req->fh);
1255 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001256 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001257
Arpad Horvath49e93442014-02-18 10:18:25 +01001258 if (req->flags & O_DIRECT) {
1259 memcpy(aligned_buffer, buffer, req->size);
1260 buffer = (const __u8*) aligned_buffer;
1261 }
Jeff Brown6249b902012-05-26 14:32:54 -07001262
1263 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1264 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001265 res = pwrite64(h->fd, buffer, req->size, req->offset);
1266 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001267 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001268 }
1269 out.size = res;
1270 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001271 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272}
1273
Jeff Brown6249b902012-05-26 14:32:54 -07001274static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275 const struct fuse_in_header* hdr)
1276{
Jeff Brown6249b902012-05-26 14:32:54 -07001277 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001278 struct statfs stat;
1279 struct fuse_statfs_out out;
1280 int res;
1281
Jeff Brown6249b902012-05-26 14:32:54 -07001282 pthread_mutex_lock(&fuse->lock);
1283 TRACE("[%d] STATFS\n", handler->token);
1284 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1285 pthread_mutex_unlock(&fuse->lock);
1286 if (res < 0) {
1287 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001288 }
Jeff Brown6249b902012-05-26 14:32:54 -07001289 if (statfs(fuse->root.name, &stat) < 0) {
1290 return -errno;
1291 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001292 memset(&out, 0, sizeof(out));
1293 out.st.blocks = stat.f_blocks;
1294 out.st.bfree = stat.f_bfree;
1295 out.st.bavail = stat.f_bavail;
1296 out.st.files = stat.f_files;
1297 out.st.ffree = stat.f_ffree;
1298 out.st.bsize = stat.f_bsize;
1299 out.st.namelen = stat.f_namelen;
1300 out.st.frsize = stat.f_frsize;
1301 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_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001306 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1307{
1308 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001309
1310 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001311 close(h->fd);
1312 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001313 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001314}
1315
Jeff Brown6249b902012-05-26 14:32:54 -07001316static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001317 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1318{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001319 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1320 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001321
Elliott Hughesf6d67372014-07-08 14:38:26 -07001322 int fd = -1;
1323 if (is_dir) {
1324 struct dirhandle *dh = id_to_ptr(req->fh);
1325 fd = dirfd(dh->d);
1326 } else {
1327 struct handle *h = id_to_ptr(req->fh);
1328 fd = h->fd;
1329 }
1330
1331 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1332 is_dir ? "FSYNCDIR" : "FSYNC",
1333 id_to_ptr(req->fh), fd, is_data_sync);
1334 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1335 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001336 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337 }
Jeff Brown6249b902012-05-26 14:32:54 -07001338 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001339}
1340
Jeff Brown6249b902012-05-26 14:32:54 -07001341static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001342 const struct fuse_in_header* hdr)
1343{
Jeff Brown6249b902012-05-26 14:32:54 -07001344 TRACE("[%d] FLUSH\n", handler->token);
1345 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346}
1347
Jeff Brown6249b902012-05-26 14:32:54 -07001348static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1350{
Jeff Brown6249b902012-05-26 14:32:54 -07001351 struct node* node;
1352 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001353 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355
Jeff Brown6249b902012-05-26 14:32:54 -07001356 pthread_mutex_lock(&fuse->lock);
1357 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1358 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1359 hdr->nodeid, node ? node->name : "?");
1360 pthread_mutex_unlock(&fuse->lock);
1361
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001363 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001364 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001365 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001366 return -EACCES;
1367 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001368 h = malloc(sizeof(*h));
1369 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001370 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001371 }
Jeff Brown6249b902012-05-26 14:32:54 -07001372 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001373 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001374 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001376 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377 }
1378 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001379 out.open_flags = 0;
1380 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001381 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001382 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001383}
1384
Jeff Brown6249b902012-05-26 14:32:54 -07001385static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001386 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1387{
1388 char buffer[8192];
1389 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1390 struct dirent *de;
1391 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001392
1393 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001394 if (req->offset == 0) {
1395 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001396 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001397 rewinddir(h->d);
1398 }
1399 de = readdir(h->d);
1400 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001401 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001402 }
1403 fde->ino = FUSE_UNKNOWN_INO;
1404 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1405 fde->off = req->offset + 1;
1406 fde->type = de->d_type;
1407 fde->namelen = strlen(de->d_name);
1408 memcpy(fde->name, de->d_name, fde->namelen + 1);
1409 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001410 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1411 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001412}
1413
Jeff Brown6249b902012-05-26 14:32:54 -07001414static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001415 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1416{
1417 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001418
1419 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 closedir(h->d);
1421 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001422 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001423}
1424
Jeff Brown6249b902012-05-26 14:32:54 -07001425static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001426 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1427{
1428 struct fuse_init_out out;
1429
Jeff Brown6249b902012-05-26 14:32:54 -07001430 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1431 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001432 out.major = FUSE_KERNEL_VERSION;
1433 out.minor = FUSE_KERNEL_MINOR_VERSION;
1434 out.max_readahead = req->max_readahead;
1435 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1436 out.max_background = 32;
1437 out.congestion_threshold = 32;
1438 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001439 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001440 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001441}
1442
Jeff Brown6249b902012-05-26 14:32:54 -07001443static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001444 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1445{
Brian Swetland03ee9472010-08-12 18:01:08 -07001446 switch (hdr->opcode) {
1447 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001448 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001449 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001450 }
Jeff Brown84715842012-05-25 14:07:47 -07001451
Brian Swetland03ee9472010-08-12 18:01:08 -07001452 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001453 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001454 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001455 }
Jeff Brown84715842012-05-25 14:07:47 -07001456
Brian Swetland03ee9472010-08-12 18:01:08 -07001457 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001458 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001459 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001460 }
Jeff Brown84715842012-05-25 14:07:47 -07001461
Brian Swetland03ee9472010-08-12 18:01:08 -07001462 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001463 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001464 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001465 }
Jeff Brown84715842012-05-25 14:07:47 -07001466
Brian Swetland03ee9472010-08-12 18:01:08 -07001467// case FUSE_READLINK:
1468// case FUSE_SYMLINK:
1469 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001470 const struct fuse_mknod_in *req = data;
1471 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001472 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001473 }
Jeff Brown84715842012-05-25 14:07:47 -07001474
Brian Swetland03ee9472010-08-12 18:01:08 -07001475 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001476 const struct fuse_mkdir_in *req = data;
1477 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001478 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001479 }
Jeff Brown84715842012-05-25 14:07:47 -07001480
Brian Swetland03ee9472010-08-12 18:01:08 -07001481 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001482 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001483 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001484 }
Jeff Brown84715842012-05-25 14:07:47 -07001485
Brian Swetland03ee9472010-08-12 18:01:08 -07001486 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001487 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001488 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001489 }
Jeff Brown84715842012-05-25 14:07:47 -07001490
Brian Swetland03ee9472010-08-12 18:01:08 -07001491 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001492 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001493 const char *old_name = ((const char*) data) + sizeof(*req);
1494 const char *new_name = old_name + strlen(old_name) + 1;
1495 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001496 }
Jeff Brown84715842012-05-25 14:07:47 -07001497
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001498// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001499 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001500 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001501 return handle_open(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 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001505 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001506 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001507 }
Jeff Brown84715842012-05-25 14:07:47 -07001508
Brian Swetland03ee9472010-08-12 18:01:08 -07001509 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001510 const struct fuse_write_in *req = data;
1511 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001512 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001513 }
Jeff Brown84715842012-05-25 14:07:47 -07001514
Mike Lockwood4553b082010-08-16 14:14:44 -04001515 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001516 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001517 }
Jeff Brown84715842012-05-25 14:07:47 -07001518
Brian Swetland03ee9472010-08-12 18:01:08 -07001519 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001520 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001521 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001522 }
Jeff Brown84715842012-05-25 14:07:47 -07001523
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001524 case FUSE_FSYNC:
1525 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001526 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001527 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001528 }
1529
Brian Swetland03ee9472010-08-12 18:01:08 -07001530// case FUSE_SETXATTR:
1531// case FUSE_GETXATTR:
1532// case FUSE_LISTXATTR:
1533// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001534 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001535 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001536 }
1537
Brian Swetland03ee9472010-08-12 18:01:08 -07001538 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001539 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001540 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001541 }
Jeff Brown84715842012-05-25 14:07:47 -07001542
Brian Swetland03ee9472010-08-12 18:01:08 -07001543 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001544 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001545 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001546 }
Jeff Brown84715842012-05-25 14:07:47 -07001547
Brian Swetland03ee9472010-08-12 18:01:08 -07001548 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001549 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001550 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001551 }
Jeff Brown84715842012-05-25 14:07:47 -07001552
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001554 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001555 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001556 }
Jeff Brown84715842012-05-25 14:07:47 -07001557
Brian Swetland03ee9472010-08-12 18:01:08 -07001558 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001559 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1560 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1561 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001562 }
Jeff Brown84715842012-05-25 14:07:47 -07001563 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001564}
1565
Jeff Brown6249b902012-05-26 14:32:54 -07001566static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001567{
Jeff Brown6249b902012-05-26 14:32:54 -07001568 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001569 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001570 ssize_t len = read(fuse->fd,
1571 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001572 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001573 if (errno != EINTR) {
1574 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1575 }
1576 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001577 }
Jeff Brown84715842012-05-25 14:07:47 -07001578
1579 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001580 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1581 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001582 }
1583
Jeff Brown7729d242012-05-25 15:35:28 -07001584 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001585 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001586 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1587 handler->token, (size_t)len, hdr->len);
1588 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001589 }
1590
Jeff Brown7729d242012-05-25 15:35:28 -07001591 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001592 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001593 __u64 unique = hdr->unique;
1594 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001595
1596 /* We do not access the request again after this point because the underlying
1597 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001598
1599 if (res != NO_STATUS) {
1600 if (res) {
1601 TRACE("[%d] ERROR %d\n", handler->token, res);
1602 }
1603 fuse_status(fuse, unique, res);
1604 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001605 }
1606}
1607
Jeff Brown6249b902012-05-26 14:32:54 -07001608static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001609{
Jeff Brown6249b902012-05-26 14:32:54 -07001610 struct fuse_handler* handler = data;
1611 handle_fuse_requests(handler);
1612 return NULL;
1613}
1614
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001615static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001616 Hashmap* map = context;
1617 hashmapRemove(map, key);
1618 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001619 return true;
1620}
1621
1622static bool remove_int_to_null(void *key, void *value, void *context) {
1623 Hashmap* map = context;
1624 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001625 return true;
1626}
1627
1628static int read_package_list(struct fuse *fuse) {
1629 pthread_mutex_lock(&fuse->lock);
1630
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001631 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1632 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001633
1634 FILE* file = fopen(kPackagesListFile, "r");
1635 if (!file) {
1636 ERROR("failed to open package list: %s\n", strerror(errno));
1637 pthread_mutex_unlock(&fuse->lock);
1638 return -1;
1639 }
1640
1641 char buf[512];
1642 while (fgets(buf, sizeof(buf), file) != NULL) {
1643 char package_name[512];
1644 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001645 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001646
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001647 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1648 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001649 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001650
1651 char* token = strtok(gids, ",");
1652 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001653 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001654 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001655 break;
1656 }
1657 token = strtok(NULL, ",");
1658 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001659 }
1660 }
1661
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001662 TRACE("read_package_list: found %d packages, %d with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001663 hashmapSize(fuse->package_to_appid),
1664 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001665 fclose(file);
1666 pthread_mutex_unlock(&fuse->lock);
1667 return 0;
1668}
1669
1670static void watch_package_list(struct fuse* fuse) {
1671 struct inotify_event *event;
1672 char event_buf[512];
1673
1674 int nfd = inotify_init();
1675 if (nfd < 0) {
1676 ERROR("inotify_init failed: %s\n", strerror(errno));
1677 return;
1678 }
1679
1680 bool active = false;
1681 while (1) {
1682 if (!active) {
1683 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1684 if (res == -1) {
1685 if (errno == ENOENT || errno == EACCES) {
1686 /* Framework may not have created yet, sleep and retry */
1687 ERROR("missing packages.list; retrying\n");
1688 sleep(3);
1689 continue;
1690 } else {
1691 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1692 return;
1693 }
1694 }
1695
1696 /* Watch above will tell us about any future changes, so
1697 * read the current state. */
1698 if (read_package_list(fuse) == -1) {
1699 ERROR("read_package_list failed: %s\n", strerror(errno));
1700 return;
1701 }
1702 active = true;
1703 }
1704
1705 int event_pos = 0;
1706 int res = read(nfd, event_buf, sizeof(event_buf));
1707 if (res < (int) sizeof(*event)) {
1708 if (errno == EINTR)
1709 continue;
1710 ERROR("failed to read inotify event: %s\n", strerror(errno));
1711 return;
1712 }
1713
1714 while (res >= (int) sizeof(*event)) {
1715 int event_size;
1716 event = (struct inotify_event *) (event_buf + event_pos);
1717
1718 TRACE("inotify event: %08x\n", event->mask);
1719 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1720 /* Previously watched file was deleted, probably due to move
1721 * that swapped in new data; re-arm the watch and read. */
1722 active = false;
1723 }
1724
1725 event_size = sizeof(*event) + event->len;
1726 res -= event_size;
1727 event_pos += event_size;
1728 }
1729 }
1730}
1731
Jeff Brown6249b902012-05-26 14:32:54 -07001732static int ignite_fuse(struct fuse* fuse, int num_threads)
1733{
1734 struct fuse_handler* handlers;
1735 int i;
1736
1737 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1738 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001739 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001740 return -ENOMEM;
1741 }
1742
1743 for (i = 0; i < num_threads; i++) {
1744 handlers[i].fuse = fuse;
1745 handlers[i].token = i;
1746 }
1747
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001748 /* When deriving permissions, this thread is used to process inotify events,
1749 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001750 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001751 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001752 pthread_t thread;
1753 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1754 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001755 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001756 goto quit;
1757 }
1758 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001759
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001760 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001761 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001762 } else {
1763 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001764 }
1765
1766 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001767
1768 /* don't bother killing all of the other threads or freeing anything,
1769 * should never get here anyhow */
1770quit:
1771 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001772}
1773
Mike Lockwood4f35e622011-01-12 14:39:44 -05001774static int usage()
1775{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001776 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1777 " -u: specify UID to run as\n"
1778 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001779 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001780 " -t: specify number of threads to use (default %d)\n"
1781 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001782 " -l: derive file permissions based on legacy internal layout\n"
1783 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001784 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001785 return 1;
1786}
1787
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001788static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001789 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1790 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001791 int fd;
1792 char opts[256];
1793 int res;
1794 struct fuse fuse;
1795
1796 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001797 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001798
1799 fd = open("/dev/fuse", O_RDWR);
1800 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001801 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001802 return -1;
1803 }
1804
1805 snprintf(opts, sizeof(opts),
1806 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1807 fd, uid, gid);
1808
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001809 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001810 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001811 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1812 goto error;
1813 }
1814
1815 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1816 if (res < 0) {
1817 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001818 goto error;
1819 }
1820
1821 res = setgid(gid);
1822 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001823 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001824 goto error;
1825 }
1826
1827 res = setuid(uid);
1828 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001829 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001830 goto error;
1831 }
1832
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001833 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001834
1835 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001836 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001837
1838 /* we do not attempt to umount the file system here because we are no longer
1839 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001840
1841error:
1842 close(fd);
1843 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001844}
1845
Brian Swetland03ee9472010-08-12 18:01:08 -07001846int main(int argc, char **argv)
1847{
Brian Swetland03ee9472010-08-12 18:01:08 -07001848 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001849 const char *source_path = NULL;
1850 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001851 uid_t uid = 0;
1852 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001853 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001854 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001855 derive_t derive = DERIVE_NONE;
1856 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001857 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001858 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001859
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001860 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001861 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001862 switch (opt) {
1863 case 'u':
1864 uid = strtoul(optarg, NULL, 10);
1865 break;
1866 case 'g':
1867 gid = strtoul(optarg, NULL, 10);
1868 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001869 case 'w':
1870 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001871 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001872 case 't':
1873 num_threads = strtoul(optarg, NULL, 10);
1874 break;
1875 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001876 derive = DERIVE_UNIFIED;
1877 break;
1878 case 'l':
1879 derive = DERIVE_LEGACY;
1880 break;
1881 case 's':
1882 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001883 break;
1884 case '?':
1885 default:
1886 return usage();
1887 }
1888 }
1889
1890 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001891 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001892 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001893 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001894 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001895 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001896 } else if (!uid) {
1897 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001898 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001899 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001900 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001901 ERROR("too many arguments\n");
1902 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001903 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001904 }
1905
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001906 if (!source_path) {
1907 ERROR("no source path specified\n");
1908 return usage();
1909 }
1910 if (!dest_path) {
1911 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001912 return usage();
1913 }
Jeff Brown26567352012-05-25 13:27:43 -07001914 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001915 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001916 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001917 }
Jeff Brown6249b902012-05-26 14:32:54 -07001918 if (num_threads < 1) {
1919 ERROR("number of threads must be at least 1\n");
1920 return usage();
1921 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001922 if (split_perms && derive == DERIVE_NONE) {
1923 ERROR("cannot split permissions without deriving\n");
1924 return usage();
1925 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001926
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001927 rlim.rlim_cur = 8192;
1928 rlim.rlim_max = 8192;
1929 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1930 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1931 }
1932
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001933 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001934 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001935}