blob: 3f1e268e6f3056431abf5e246a30ca273b22dd84 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040025#include <sys/statfs.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070026#include <sys/uio.h>
27#include <dirent.h>
Jeff Brown7729d242012-05-25 15:35:28 -070028#include <limits.h>
Mike Lockwood1bedb732011-01-13 13:38:42 -050029#include <ctype.h>
Jeff Brown6249b902012-05-26 14:32:54 -070030#include <pthread.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080031#include <sys/time.h>
32#include <sys/resource.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070033#include <sys/inotify.h>
34
Jeff Sharkey44d63422013-09-12 09:44:48 -070035#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070036#include <cutils/hashmap.h>
37#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070038
Brian Swetlandb14a2c62010-08-12 18:21:12 -070039#include <private/android_filesystem_config.h>
40
Brian Swetland03ee9472010-08-12 18:01:08 -070041#include "fuse.h"
42
43/* README
44 *
45 * What is this?
46 *
47 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
48 * directory permissions (all files are given fixed owner, group, and
49 * permissions at creation, owner, group, and permissions are not
50 * changeable, symlinks and hardlinks are not createable, etc.
51 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070052 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070053 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070054 * It must be run as root, but will drop to requested UID/GID as soon as it
55 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070056 *
57 * Things I believe to be true:
58 *
59 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
60 * CREAT) must bump that node's refcount
61 * - don't forget that FORGET can forget multiple references (req->nlookup)
62 * - if an op that returns a fuse_entry fails writing the reply to the
63 * kernel, you must rollback the refcount to reflect the reference the
64 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070065 *
66 * This daemon can also derive custom filesystem permissions based on directory
67 * structure when requested. These custom permissions support several features:
68 *
69 * - Apps can access their own files in /Android/data/com.example/ without
70 * requiring any additional GIDs.
71 * - Separate permissions for protecting directories like Pictures and Music.
72 * - Multi-user separation on the same physical device.
73 *
74 * The derived permissions look like this:
75 *
76 * rwxrwx--x root:sdcard_rw /
77 * rwxrwx--- root:sdcard_pics /Pictures
78 * rwxrwx--- root:sdcard_av /Music
79 *
80 * rwxrwx--x root:sdcard_rw /Android
81 * rwxrwx--x root:sdcard_rw /Android/data
82 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
83 * rwxrwx--x root:sdcard_rw /Android/obb/
84 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
85 *
86 * rwxrwx--- root:sdcard_all /Android/user
87 * rwxrwx--x root:sdcard_rw /Android/user/10
88 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070089 */
90
91#define FUSE_TRACE 0
92
93#if FUSE_TRACE
94#define TRACE(x...) fprintf(stderr,x)
95#else
96#define TRACE(x...) do {} while (0)
97#endif
98
99#define ERROR(x...) fprintf(stderr,x)
100
101#define FUSE_UNKNOWN_INO 0xffffffff
102
Jeff Brown84715842012-05-25 14:07:47 -0700103/* Maximum number of bytes to write in one request. */
104#define MAX_WRITE (256 * 1024)
105
106/* Maximum number of bytes to read in one request. */
107#define MAX_READ (128 * 1024)
108
109/* Largest possible request.
110 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
111 * the largest possible data payload. */
112#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
113
Jeff Brown6249b902012-05-26 14:32:54 -0700114/* Default number of threads. */
115#define DEFAULT_NUM_THREADS 2
116
117/* Pseudo-error constant used to indicate that no fuse status is needed
118 * or that a reply has already been written. */
119#define NO_STATUS 1
120
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700121/* Path to system-provided mapping of package name to appIds */
122static const char* const kPackagesListFile = "/data/system/packages.list";
123
124/* Supplementary groups to execute with */
125static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
126
127/* Permission mode for a specific node. Controls how file permissions
128 * are derived for children nodes. */
129typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700130 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700131 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700132 /* This node is one level above a normal root; used for legacy layouts
133 * which use the first level to represent user_id. */
134 PERM_LEGACY_PRE_ROOT,
135 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700136 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700137 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700138 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700139 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID_OBB,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_USER,
145} perm_t;
146
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700147/* Permissions structure to derive */
148typedef enum {
149 DERIVE_NONE,
150 DERIVE_LEGACY,
151 DERIVE_UNIFIED,
152} derive_t;
153
Brian Swetland03ee9472010-08-12 18:01:08 -0700154struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700155 int fd;
156};
157
158struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700159 DIR *d;
160};
161
162struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700163 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700164 __u64 nid;
165 __u64 gen;
166
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700167 /* State derived based on current position in hierarchy. */
168 perm_t perm;
169 userid_t userid;
170 uid_t uid;
171 gid_t gid;
172 mode_t mode;
173
Paul Eastham11ccdb32010-10-14 11:04:26 -0700174 struct node *next; /* per-dir sibling list */
175 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700176 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700177
Jeff Brown6249b902012-05-26 14:32:54 -0700178 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700179 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800180 /* If non-null, this is the real name of the file in the underlying storage.
181 * This may differ from the field "name" only by case.
182 * strlen(actual_name) will always equal strlen(name), so it is safe to use
183 * namelen for both fields.
184 */
185 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700186
187 /* If non-null, an exact underlying path that should be grafted into this
188 * position. Used to support things like OBB. */
189 char* graft_path;
190 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700191};
192
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700193static int str_hash(void *key) {
194 return hashmapHash(key, strlen(key));
195}
196
Jeff Sharkey44d63422013-09-12 09:44:48 -0700197/** Test if two string keys are equal ignoring case */
198static bool str_icase_equals(void *keyA, void *keyB) {
199 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700200}
201
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700202static int int_hash(void *key) {
203 return (int) key;
204}
205
206static bool int_equals(void *keyA, void *keyB) {
207 return keyA == keyB;
208}
209
Jeff Brown7729d242012-05-25 15:35:28 -0700210/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700211struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700212 pthread_mutex_t lock;
213
Brian Swetland03ee9472010-08-12 18:01:08 -0700214 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700215 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700216 derive_t derive;
217 bool split_perms;
Brian Swetland03ee9472010-08-12 18:01:08 -0700218 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700219 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700220
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700221 Hashmap* package_to_appid;
222 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700223};
224
Jeff Brown7729d242012-05-25 15:35:28 -0700225/* Private data used by a single fuse handler. */
226struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700227 struct fuse* fuse;
228 int token;
229
Jeff Brown7729d242012-05-25 15:35:28 -0700230 /* To save memory, we never use the contents of the request buffer and the read
231 * buffer at the same time. This allows us to share the underlying storage. */
232 union {
233 __u8 request_buffer[MAX_REQUEST_SIZE];
234 __u8 read_buffer[MAX_READ];
235 };
236};
Brian Swetland03ee9472010-08-12 18:01:08 -0700237
Jeff Brown6249b902012-05-26 14:32:54 -0700238static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700239{
Jeff Brown6249b902012-05-26 14:32:54 -0700240 return (void *) (uintptr_t) nid;
241}
Brian Swetland03ee9472010-08-12 18:01:08 -0700242
Jeff Brown6249b902012-05-26 14:32:54 -0700243static inline __u64 ptr_to_id(void *ptr)
244{
245 return (__u64) (uintptr_t) ptr;
246}
Brian Swetland03ee9472010-08-12 18:01:08 -0700247
Jeff Brown6249b902012-05-26 14:32:54 -0700248static void acquire_node_locked(struct node* node)
249{
250 node->refcount++;
251 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
252}
253
254static void remove_node_from_parent_locked(struct node* node);
255
256static void release_node_locked(struct node* node)
257{
258 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
259 if (node->refcount > 0) {
260 node->refcount--;
261 if (!node->refcount) {
262 TRACE("DESTROY %p (%s)\n", node, node->name);
263 remove_node_from_parent_locked(node);
264
265 /* TODO: remove debugging - poison memory */
266 memset(node->name, 0xef, node->namelen);
267 free(node->name);
268 free(node->actual_name);
269 memset(node, 0xfc, sizeof(*node));
270 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800271 }
Jeff Brown6249b902012-05-26 14:32:54 -0700272 } else {
273 ERROR("Zero refcnt %p\n", node);
274 }
275}
276
277static void add_node_to_parent_locked(struct node *node, struct node *parent) {
278 node->parent = parent;
279 node->next = parent->child;
280 parent->child = node;
281 acquire_node_locked(parent);
282}
283
284static void remove_node_from_parent_locked(struct node* node)
285{
286 if (node->parent) {
287 if (node->parent->child == node) {
288 node->parent->child = node->parent->child->next;
289 } else {
290 struct node *node2;
291 node2 = node->parent->child;
292 while (node2->next != node)
293 node2 = node2->next;
294 node2->next = node->next;
295 }
296 release_node_locked(node->parent);
297 node->parent = NULL;
298 node->next = NULL;
299 }
300}
301
302/* Gets the absolute path to a node into the provided buffer.
303 *
304 * Populates 'buf' with the path and returns the length of the path on success,
305 * or returns -1 if the path is too long for the provided buffer.
306 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700307static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
308 const char* name;
309 size_t namelen;
310 if (node->graft_path) {
311 name = node->graft_path;
312 namelen = node->graft_pathlen;
313 } else if (node->actual_name) {
314 name = node->actual_name;
315 namelen = node->namelen;
316 } else {
317 name = node->name;
318 namelen = node->namelen;
319 }
320
Jeff Brown6249b902012-05-26 14:32:54 -0700321 if (bufsize < namelen + 1) {
322 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700323 }
324
Jeff Brown6249b902012-05-26 14:32:54 -0700325 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700326 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700327 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
328 if (pathlen < 0) {
329 return -1;
330 }
331 buf[pathlen++] = '/';
332 }
333
Jeff Brown6249b902012-05-26 14:32:54 -0700334 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
335 return pathlen + namelen;
336}
337
338/* Finds the absolute path of a file within a given directory.
339 * Performs a case-insensitive search for the file and sets the buffer to the path
340 * of the first matching file. If 'search' is zero or if no match is found, sets
341 * the buffer to the path that the file would have, assuming the name were case-sensitive.
342 *
343 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
344 * or returns NULL if the path is too long for the provided buffer.
345 */
346static char* find_file_within(const char* path, const char* name,
347 char* buf, size_t bufsize, int search)
348{
349 size_t pathlen = strlen(path);
350 size_t namelen = strlen(name);
351 size_t childlen = pathlen + namelen + 1;
352 char* actual;
353
354 if (bufsize <= childlen) {
355 return NULL;
356 }
357
358 memcpy(buf, path, pathlen);
359 buf[pathlen] = '/';
360 actual = buf + pathlen + 1;
361 memcpy(actual, name, namelen + 1);
362
363 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800364 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700365 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800366 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700367 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700368 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800369 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800370 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700371 if (!strcasecmp(entry->d_name, name)) {
372 /* we have a match - replace the name, don't need to copy the null again */
373 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800374 break;
375 }
376 }
377 closedir(dir);
378 }
Jeff Brown6249b902012-05-26 14:32:54 -0700379 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800380}
381
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700382static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800383{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700384 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700385 attr->size = s->st_size;
386 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400387 attr->atime = s->st_atime;
388 attr->mtime = s->st_mtime;
389 attr->ctime = s->st_ctime;
390 attr->atimensec = s->st_atime_nsec;
391 attr->mtimensec = s->st_mtime_nsec;
392 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700393 attr->mode = s->st_mode;
394 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700395
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700396 attr->uid = node->uid;
397 attr->gid = node->gid;
398
399 /* Filter requested mode based on underlying file, and
400 * pass through file type. */
401 int owner_mode = s->st_mode & 0700;
402 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
403 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
404}
405
Jeff Sharkey44d63422013-09-12 09:44:48 -0700406static int touch(char* path, mode_t mode) {
407 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
408 if (fd == -1) {
409 if (errno == EEXIST) {
410 return 0;
411 } else {
412 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
413 return -1;
414 }
415 }
416 close(fd);
417 return 0;
418}
419
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700420static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
421 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700422 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700423
424 /* By default, each node inherits from its parent */
425 node->perm = PERM_INHERIT;
426 node->userid = parent->userid;
427 node->uid = parent->uid;
428 node->gid = parent->gid;
429 node->mode = parent->mode;
430
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700431 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700432 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700433 }
434
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700435 /* Derive custom permissions based on parent and current node */
436 switch (parent->perm) {
437 case PERM_INHERIT:
438 /* Already inherited above */
439 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700440 case PERM_LEGACY_PRE_ROOT:
441 /* Legacy internal layout places users at top level */
442 node->perm = PERM_ROOT;
443 node->userid = strtoul(node->name, NULL, 10);
444 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700445 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700446 /* Assume masked off by default. */
447 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700448 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700449 /* App-specific directories inside; let anyone traverse */
450 node->perm = PERM_ANDROID;
451 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700452 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700453 if (!strcasecmp(node->name, "DCIM")
454 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700455 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700456 } else if (!strcasecmp(node->name, "Alarms")
457 || !strcasecmp(node->name, "Movies")
458 || !strcasecmp(node->name, "Music")
459 || !strcasecmp(node->name, "Notifications")
460 || !strcasecmp(node->name, "Podcasts")
461 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700462 node->gid = AID_SDCARD_AV;
463 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700464 }
465 break;
466 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700467 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700468 /* App-specific directories inside; let anyone traverse */
469 node->perm = PERM_ANDROID_DATA;
470 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700471 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700472 /* App-specific directories inside; let anyone traverse */
473 node->perm = PERM_ANDROID_OBB;
474 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700475 /* Single OBB directory is always shared */
476 node->graft_path = fuse->obbpath;
477 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* User directories must only be accessible to system, protected
480 * by sdcard_all. Zygote will bind mount the appropriate user-
481 * specific path. */
482 node->perm = PERM_ANDROID_USER;
483 node->gid = AID_SDCARD_ALL;
484 node->mode = 0770;
485 }
486 break;
487 case PERM_ANDROID_DATA:
488 case PERM_ANDROID_OBB:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700489 appid = (appid_t) hashmapGet(fuse->package_to_appid, node->name);
490 if (appid != 0) {
491 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700492 }
493 node->mode = 0770;
494 break;
495 case PERM_ANDROID_USER:
496 /* Root of a secondary user */
497 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700498 node->userid = strtoul(node->name, NULL, 10);
499 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700500 node->mode = 0771;
501 break;
502 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700503}
504
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700505/* Return if the calling UID holds sdcard_rw. */
506static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700507 /* No additional permissions enforcement */
508 if (fuse->derive == DERIVE_NONE) {
509 return true;
510 }
511
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700512 appid_t appid = multiuser_get_app_id(hdr->uid);
513 return hashmapContainsKey(fuse->appid_with_rw, (void*) appid);
514}
515
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700516/* Kernel has already enforced everything we returned through
517 * derive_permissions_locked(), so this is used to lock down access
518 * even further, such as enforcing that apps hold sdcard_rw. */
519static bool check_caller_access_to_name(struct fuse* fuse,
520 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700521 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700522 /* Always block security-sensitive files at root */
523 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700524 if (!strcasecmp(name, "autorun.inf")
525 || !strcasecmp(name, ".android_secure")
526 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700527 return false;
528 }
529 }
530
531 /* No additional permissions enforcement */
532 if (fuse->derive == DERIVE_NONE) {
533 return true;
534 }
535
Jeff Sharkey44d63422013-09-12 09:44:48 -0700536 /* Root always has access; access for any other UIDs should always
537 * be controlled through packages.list. */
538 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700539 return true;
540 }
541
542 /* If asking to write, verify that caller either owns the
543 * parent or holds sdcard_rw. */
544 if (mode & W_OK) {
545 if (parent_node && hdr->uid == parent_node->uid) {
546 return true;
547 }
548
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700549 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700550 }
551
552 /* No extra permissions to enforce */
553 return true;
554}
555
556static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700557 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
558 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700559}
560
Jeff Brown6249b902012-05-26 14:32:54 -0700561struct node *create_node_locked(struct fuse* fuse,
562 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700563{
564 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700565 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700566
Paul Eastham11ccdb32010-10-14 11:04:26 -0700567 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700568 if (!node) {
569 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700570 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700571 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700572 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700573 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700574 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700575 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700576 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700577 if (strcmp(name, actual_name)) {
578 node->actual_name = malloc(namelen + 1);
579 if (!node->actual_name) {
580 free(node->name);
581 free(node);
582 return NULL;
583 }
584 memcpy(node->actual_name, actual_name, namelen + 1);
585 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700586 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700587 node->nid = ptr_to_id(node);
588 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700589
590 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700591 acquire_node_locked(node);
592 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700593 return node;
594}
595
Jeff Brown6249b902012-05-26 14:32:54 -0700596static int rename_node_locked(struct node *node, const char *name,
597 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700598{
Jeff Brown6249b902012-05-26 14:32:54 -0700599 size_t namelen = strlen(name);
600 int need_actual_name = strcmp(name, actual_name);
601
602 /* make the storage bigger without actually changing the name
603 * in case an error occurs part way */
604 if (namelen > node->namelen) {
605 char* new_name = realloc(node->name, namelen + 1);
606 if (!new_name) {
607 return -ENOMEM;
608 }
609 node->name = new_name;
610 if (need_actual_name && node->actual_name) {
611 char* new_actual_name = realloc(node->actual_name, namelen + 1);
612 if (!new_actual_name) {
613 return -ENOMEM;
614 }
615 node->actual_name = new_actual_name;
616 }
617 }
618
619 /* update the name, taking care to allocate storage before overwriting the old name */
620 if (need_actual_name) {
621 if (!node->actual_name) {
622 node->actual_name = malloc(namelen + 1);
623 if (!node->actual_name) {
624 return -ENOMEM;
625 }
626 }
627 memcpy(node->actual_name, actual_name, namelen + 1);
628 } else {
629 free(node->actual_name);
630 node->actual_name = NULL;
631 }
632 memcpy(node->name, name, namelen + 1);
633 node->namelen = namelen;
634 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700635}
636
Jeff Brown6249b902012-05-26 14:32:54 -0700637static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700638{
Jeff Brown6249b902012-05-26 14:32:54 -0700639 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700640 return &fuse->root;
641 } else {
642 return id_to_ptr(nid);
643 }
644}
645
Jeff Brown6249b902012-05-26 14:32:54 -0700646static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
647 char* buf, size_t bufsize)
648{
649 struct node* node = lookup_node_by_id_locked(fuse, nid);
650 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
651 node = NULL;
652 }
653 return node;
654}
655
656static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700657{
658 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700659 /* use exact string comparison, nodes that differ by case
660 * must be considered distinct even if they refer to the same
661 * underlying file as otherwise operations such as "mv x x"
662 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700663 if (!strcmp(name, node->name)) {
664 return node;
665 }
666 }
667 return 0;
668}
669
Jeff Brown6249b902012-05-26 14:32:54 -0700670static struct node* acquire_or_create_child_locked(
671 struct fuse* fuse, struct node* parent,
672 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700673{
Jeff Brown6249b902012-05-26 14:32:54 -0700674 struct node* child = lookup_child_by_name_locked(parent, name);
675 if (child) {
676 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800677 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700678 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800679 }
Jeff Brown6249b902012-05-26 14:32:54 -0700680 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700681}
682
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700683static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
684 gid_t fs_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700685 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700686
Jeff Brown6249b902012-05-26 14:32:54 -0700687 fuse->fd = fd;
688 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700689 fuse->derive = derive;
690 fuse->split_perms = split_perms;
Brian Swetland03ee9472010-08-12 18:01:08 -0700691
Jeff Brown6249b902012-05-26 14:32:54 -0700692 memset(&fuse->root, 0, sizeof(fuse->root));
693 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
694 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700695 fuse->root.namelen = strlen(source_path);
696 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700697 fuse->root.userid = 0;
698 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700699
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700700 /* Set up root node for various modes of operation */
701 switch (derive) {
702 case DERIVE_NONE:
703 /* Traditional behavior that treats entire device as being accessible
704 * to sdcard_rw, and no permissions are derived. */
705 fuse->root.perm = PERM_ROOT;
706 fuse->root.mode = 0775;
707 fuse->root.gid = AID_SDCARD_RW;
708 break;
709 case DERIVE_LEGACY:
710 /* Legacy behavior used to support internal multiuser layout which
711 * places user_id at the top directory level, with the actual roots
712 * just below that. Shared OBB path is also at top level. */
713 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
714 fuse->root.mode = 0771;
715 fuse->root.gid = fs_gid;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700716 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700717 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
718 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700719 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700720 break;
721 case DERIVE_UNIFIED:
722 /* Unified multiuser layout which places secondary user_id under
723 * /Android/user and shared OBB path under /Android/obb. */
724 fuse->root.perm = PERM_ROOT;
725 fuse->root.mode = 0771;
726 fuse->root.gid = fs_gid;
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/Android/obb", source_path);
730 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700731 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700732}
733
Jeff Brown6249b902012-05-26 14:32:54 -0700734static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700735{
736 struct fuse_out_header hdr;
737 hdr.len = sizeof(hdr);
738 hdr.error = err;
739 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700740 write(fuse->fd, &hdr, sizeof(hdr));
741}
742
Jeff Brown6249b902012-05-26 14:32:54 -0700743static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700744{
745 struct fuse_out_header hdr;
746 struct iovec vec[2];
747 int res;
748
749 hdr.len = len + sizeof(hdr);
750 hdr.error = 0;
751 hdr.unique = unique;
752
753 vec[0].iov_base = &hdr;
754 vec[0].iov_len = sizeof(hdr);
755 vec[1].iov_base = data;
756 vec[1].iov_len = len;
757
758 res = writev(fuse->fd, vec, 2);
759 if (res < 0) {
760 ERROR("*** REPLY FAILED *** %d\n", errno);
761 }
762}
763
Jeff Brown6249b902012-05-26 14:32:54 -0700764static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
765 struct node* parent, const char* name, const char* actual_name,
766 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700767{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700768 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700769 struct fuse_entry_out out;
770 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700771
Jeff Brown6249b902012-05-26 14:32:54 -0700772 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700773 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700774 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700775
Jeff Brown6249b902012-05-26 14:32:54 -0700776 pthread_mutex_lock(&fuse->lock);
777 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
778 if (!node) {
779 pthread_mutex_unlock(&fuse->lock);
780 return -ENOMEM;
781 }
782 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700783 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700784 out.attr_valid = 10;
785 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700786 out.nodeid = node->nid;
787 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700788 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700789 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700790 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700791}
792
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700793static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700794 const char* path)
795{
796 struct fuse_attr_out out;
797 struct stat s;
798
799 if (lstat(path, &s) < 0) {
800 return -errno;
801 }
802 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700803 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700804 out.attr_valid = 10;
805 fuse_reply(fuse, unique, &out, sizeof(out));
806 return NO_STATUS;
807}
808
809static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700810 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700811{
Jeff Brown6249b902012-05-26 14:32:54 -0700812 struct node* parent_node;
813 char parent_path[PATH_MAX];
814 char child_path[PATH_MAX];
815 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700816
Jeff Brown6249b902012-05-26 14:32:54 -0700817 pthread_mutex_lock(&fuse->lock);
818 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
819 parent_path, sizeof(parent_path));
820 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
821 parent_node ? parent_node->name : "?");
822 pthread_mutex_unlock(&fuse->lock);
823
824 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
825 child_path, sizeof(child_path), 1))) {
826 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700827 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700828 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700829 return -EACCES;
830 }
831
Jeff Brown6249b902012-05-26 14:32:54 -0700832 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700833}
834
Jeff Brown6249b902012-05-26 14:32:54 -0700835static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
837{
Jeff Brown6249b902012-05-26 14:32:54 -0700838 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839
Jeff Brown6249b902012-05-26 14:32:54 -0700840 pthread_mutex_lock(&fuse->lock);
841 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
842 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
843 hdr->nodeid, node ? node->name : "?");
844 if (node) {
845 __u64 n = req->nlookup;
846 while (n--) {
847 release_node_locked(node);
848 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700849 }
Jeff Brown6249b902012-05-26 14:32:54 -0700850 pthread_mutex_unlock(&fuse->lock);
851 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700852}
853
Jeff Brown6249b902012-05-26 14:32:54 -0700854static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700855 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
856{
Jeff Brown6249b902012-05-26 14:32:54 -0700857 struct node* node;
858 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700859
Jeff Brown6249b902012-05-26 14:32:54 -0700860 pthread_mutex_lock(&fuse->lock);
861 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
862 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
863 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
864 pthread_mutex_unlock(&fuse->lock);
865
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700866 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700867 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700869 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700870 return -EACCES;
871 }
872
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700873 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700874}
875
Jeff Brown6249b902012-05-26 14:32:54 -0700876static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700877 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
878{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700879 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700880 struct node* node;
881 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883
Jeff Brown6249b902012-05-26 14:32:54 -0700884 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700885 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700886 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
887 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
888 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
889 pthread_mutex_unlock(&fuse->lock);
890
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700891 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700892 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700893 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700894 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700895 return -EACCES;
896 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897
Jeff Brown6249b902012-05-26 14:32:54 -0700898 /* XXX: incomplete implementation on purpose.
899 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700900
Jeff Brown6249b902012-05-26 14:32:54 -0700901 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
902 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700903 }
904
905 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
906 * are both set, then set it to the current time. Else, set it to the
907 * time specified in the request. Same goes for mtime. Use utimensat(2)
908 * as it allows ATIME and MTIME to be changed independently, and has
909 * nanosecond resolution which fuse also has.
910 */
911 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
912 times[0].tv_nsec = UTIME_OMIT;
913 times[1].tv_nsec = UTIME_OMIT;
914 if (req->valid & FATTR_ATIME) {
915 if (req->valid & FATTR_ATIME_NOW) {
916 times[0].tv_nsec = UTIME_NOW;
917 } else {
918 times[0].tv_sec = req->atime;
919 times[0].tv_nsec = req->atimensec;
920 }
921 }
922 if (req->valid & FATTR_MTIME) {
923 if (req->valid & FATTR_MTIME_NOW) {
924 times[1].tv_nsec = UTIME_NOW;
925 } else {
926 times[1].tv_sec = req->mtime;
927 times[1].tv_nsec = req->mtimensec;
928 }
929 }
Jeff Brown6249b902012-05-26 14:32:54 -0700930 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
931 handler->token, path, times[0].tv_sec, times[1].tv_sec);
932 if (utimensat(-1, path, times, 0) < 0) {
933 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700934 }
935 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700936 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700937}
938
Jeff Brown6249b902012-05-26 14:32:54 -0700939static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700940 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
941{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700942 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700943 struct node* parent_node;
944 char parent_path[PATH_MAX];
945 char child_path[PATH_MAX];
946 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700947
Jeff Brown6249b902012-05-26 14:32:54 -0700948 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700949 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700950 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
951 parent_path, sizeof(parent_path));
952 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
953 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
954 pthread_mutex_unlock(&fuse->lock);
955
956 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
957 child_path, sizeof(child_path), 1))) {
958 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700959 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700960 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700961 return -EACCES;
962 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700963 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700964 if (mknod(child_path, mode, req->rdev) < 0) {
965 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700966 }
Jeff Brown6249b902012-05-26 14:32:54 -0700967 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700968}
969
Jeff Brown6249b902012-05-26 14:32:54 -0700970static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700971 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
972{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700973 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700974 struct node* parent_node;
975 char parent_path[PATH_MAX];
976 char child_path[PATH_MAX];
977 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700978
Jeff Brown6249b902012-05-26 14:32:54 -0700979 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700980 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700981 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
982 parent_path, sizeof(parent_path));
983 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
984 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
985 pthread_mutex_unlock(&fuse->lock);
986
987 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
988 child_path, sizeof(child_path), 1))) {
989 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700990 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700991 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700992 return -EACCES;
993 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700994 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700995 if (mkdir(child_path, mode) < 0) {
996 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700997 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700998
999 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1000 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1001 char nomedia[PATH_MAX];
1002 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1003 if (touch(nomedia, 0664) != 0) {
1004 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1005 return -ENOENT;
1006 }
1007 }
1008 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1009 char nomedia[PATH_MAX];
1010 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1011 if (touch(nomedia, 0664) != 0) {
1012 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1013 return -ENOENT;
1014 }
1015 }
1016
Jeff Brown6249b902012-05-26 14:32:54 -07001017 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018}
1019
Jeff Brown6249b902012-05-26 14:32:54 -07001020static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001021 const struct fuse_in_header* hdr, const char* name)
1022{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001023 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001024 struct node* parent_node;
1025 char parent_path[PATH_MAX];
1026 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001027
Jeff Brown6249b902012-05-26 14:32:54 -07001028 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001029 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001030 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1031 parent_path, sizeof(parent_path));
1032 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1033 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1034 pthread_mutex_unlock(&fuse->lock);
1035
1036 if (!parent_node || !find_file_within(parent_path, name,
1037 child_path, sizeof(child_path), 1)) {
1038 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001039 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001040 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001041 return -EACCES;
1042 }
Jeff Brown6249b902012-05-26 14:32:54 -07001043 if (unlink(child_path) < 0) {
1044 return -errno;
1045 }
1046 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001047}
1048
Jeff Brown6249b902012-05-26 14:32:54 -07001049static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050 const struct fuse_in_header* hdr, const char* name)
1051{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001052 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001053 struct node* parent_node;
1054 char parent_path[PATH_MAX];
1055 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001056
Jeff Brown6249b902012-05-26 14:32:54 -07001057 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001058 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001059 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1060 parent_path, sizeof(parent_path));
1061 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1062 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1063 pthread_mutex_unlock(&fuse->lock);
1064
1065 if (!parent_node || !find_file_within(parent_path, name,
1066 child_path, sizeof(child_path), 1)) {
1067 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001069 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001070 return -EACCES;
1071 }
Jeff Brown6249b902012-05-26 14:32:54 -07001072 if (rmdir(child_path) < 0) {
1073 return -errno;
1074 }
1075 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001076}
1077
Jeff Brown6249b902012-05-26 14:32:54 -07001078static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001079 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001080 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001081{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001082 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001083 struct node* old_parent_node;
1084 struct node* new_parent_node;
1085 struct node* child_node;
1086 char old_parent_path[PATH_MAX];
1087 char new_parent_path[PATH_MAX];
1088 char old_child_path[PATH_MAX];
1089 char new_child_path[PATH_MAX];
1090 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001091 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092
Jeff Brown6249b902012-05-26 14:32:54 -07001093 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001094 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001095 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1096 old_parent_path, sizeof(old_parent_path));
1097 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1098 new_parent_path, sizeof(new_parent_path));
1099 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1100 old_name, new_name,
1101 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1102 req->newdir, new_parent_node ? new_parent_node->name : "?");
1103 if (!old_parent_node || !new_parent_node) {
1104 res = -ENOENT;
1105 goto lookup_error;
1106 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001107 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001108 res = -EACCES;
1109 goto lookup_error;
1110 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001111 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001112 res = -EACCES;
1113 goto lookup_error;
1114 }
Jeff Brown6249b902012-05-26 14:32:54 -07001115 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1116 if (!child_node || get_node_path_locked(child_node,
1117 old_child_path, sizeof(old_child_path)) < 0) {
1118 res = -ENOENT;
1119 goto lookup_error;
1120 }
1121 acquire_node_locked(child_node);
1122 pthread_mutex_unlock(&fuse->lock);
1123
1124 /* Special case for renaming a file where destination is same path
1125 * differing only by case. In this case we don't want to look for a case
1126 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1127 */
1128 int search = old_parent_node != new_parent_node
1129 || strcasecmp(old_name, new_name);
1130 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1131 new_child_path, sizeof(new_child_path), search))) {
1132 res = -ENOENT;
1133 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001134 }
1135
Jeff Brown6249b902012-05-26 14:32:54 -07001136 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1137 res = rename(old_child_path, new_child_path);
1138 if (res < 0) {
1139 res = -errno;
1140 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001141 }
1142
Jeff Brown6249b902012-05-26 14:32:54 -07001143 pthread_mutex_lock(&fuse->lock);
1144 res = rename_node_locked(child_node, new_name, new_actual_name);
1145 if (!res) {
1146 remove_node_from_parent_locked(child_node);
1147 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001148 }
Jeff Brown6249b902012-05-26 14:32:54 -07001149 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150
Jeff Brown6249b902012-05-26 14:32:54 -07001151io_error:
1152 pthread_mutex_lock(&fuse->lock);
1153done:
1154 release_node_locked(child_node);
1155lookup_error:
1156 pthread_mutex_unlock(&fuse->lock);
1157 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158}
1159
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001160static int open_flags_to_access_mode(int open_flags) {
1161 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1162 return R_OK;
1163 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1164 return W_OK;
1165 } else {
1166 /* Probably O_RDRW, but treat as default to be safe */
1167 return R_OK | W_OK;
1168 }
1169}
1170
Jeff Brown6249b902012-05-26 14:32:54 -07001171static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001172 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1173{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001174 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001175 struct node* node;
1176 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001177 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001178 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001179
Jeff Brown6249b902012-05-26 14:32:54 -07001180 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001181 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001182 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1183 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1184 req->flags, hdr->nodeid, node ? node->name : "?");
1185 pthread_mutex_unlock(&fuse->lock);
1186
1187 if (!node) {
1188 return -ENOENT;
1189 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001190 if (!check_caller_access_to_node(fuse, hdr, node,
1191 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001192 return -EACCES;
1193 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001194 h = malloc(sizeof(*h));
1195 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001196 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001197 }
Jeff Brown6249b902012-05-26 14:32:54 -07001198 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199 h->fd = open(path, req->flags);
1200 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001201 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001202 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203 }
1204 out.fh = ptr_to_id(h);
1205 out.open_flags = 0;
1206 out.padding = 0;
1207 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001208 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209}
1210
Jeff Brown6249b902012-05-26 14:32:54 -07001211static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1213{
1214 struct handle *h = id_to_ptr(req->fh);
1215 __u64 unique = hdr->unique;
1216 __u32 size = req->size;
1217 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001218 int res;
1219
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001220 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1221 * overlaps the request buffer and will clobber data in the request. This
1222 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001223
1224 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1225 h, h->fd, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001226 if (size > sizeof(handler->read_buffer)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001227 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001228 }
1229 res = pread64(h->fd, handler->read_buffer, size, offset);
1230 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001231 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001232 }
1233 fuse_reply(fuse, unique, handler->read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001234 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001235}
1236
Jeff Brown6249b902012-05-26 14:32:54 -07001237static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001238 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1239 const void* buffer)
1240{
1241 struct fuse_write_out out;
1242 struct handle *h = id_to_ptr(req->fh);
1243 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001244
1245 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1246 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001247 res = pwrite64(h->fd, buffer, req->size, req->offset);
1248 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001249 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001250 }
1251 out.size = res;
1252 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001253 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001254}
1255
Jeff Brown6249b902012-05-26 14:32:54 -07001256static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001257 const struct fuse_in_header* hdr)
1258{
Jeff Brown6249b902012-05-26 14:32:54 -07001259 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001260 struct statfs stat;
1261 struct fuse_statfs_out out;
1262 int res;
1263
Jeff Brown6249b902012-05-26 14:32:54 -07001264 pthread_mutex_lock(&fuse->lock);
1265 TRACE("[%d] STATFS\n", handler->token);
1266 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1267 pthread_mutex_unlock(&fuse->lock);
1268 if (res < 0) {
1269 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001270 }
Jeff Brown6249b902012-05-26 14:32:54 -07001271 if (statfs(fuse->root.name, &stat) < 0) {
1272 return -errno;
1273 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274 memset(&out, 0, sizeof(out));
1275 out.st.blocks = stat.f_blocks;
1276 out.st.bfree = stat.f_bfree;
1277 out.st.bavail = stat.f_bavail;
1278 out.st.files = stat.f_files;
1279 out.st.ffree = stat.f_ffree;
1280 out.st.bsize = stat.f_bsize;
1281 out.st.namelen = stat.f_namelen;
1282 out.st.frsize = stat.f_frsize;
1283 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001284 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001285}
1286
Jeff Brown6249b902012-05-26 14:32:54 -07001287static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001288 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1289{
1290 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001291
1292 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001293 close(h->fd);
1294 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001295 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001296}
1297
Jeff Brown6249b902012-05-26 14:32:54 -07001298static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001299 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1300{
1301 int is_data_sync = req->fsync_flags & 1;
1302 struct handle *h = id_to_ptr(req->fh);
1303 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001304
1305 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
1306 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001307 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
1308 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001309 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001310 }
Jeff Brown6249b902012-05-26 14:32:54 -07001311 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001312}
1313
Jeff Brown6249b902012-05-26 14:32:54 -07001314static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001315 const struct fuse_in_header* hdr)
1316{
Jeff Brown6249b902012-05-26 14:32:54 -07001317 TRACE("[%d] FLUSH\n", handler->token);
1318 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001319}
1320
Jeff Brown6249b902012-05-26 14:32:54 -07001321static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001322 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1323{
Jeff Brown6249b902012-05-26 14:32:54 -07001324 struct node* node;
1325 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001327 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001328
Jeff Brown6249b902012-05-26 14:32:54 -07001329 pthread_mutex_lock(&fuse->lock);
1330 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1331 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1332 hdr->nodeid, node ? node->name : "?");
1333 pthread_mutex_unlock(&fuse->lock);
1334
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001335 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001336 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001338 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001339 return -EACCES;
1340 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341 h = malloc(sizeof(*h));
1342 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001343 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344 }
Jeff Brown6249b902012-05-26 14:32:54 -07001345 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001347 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001349 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001350 }
1351 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001352 out.open_flags = 0;
1353 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001355 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001356}
1357
Jeff Brown6249b902012-05-26 14:32:54 -07001358static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1360{
1361 char buffer[8192];
1362 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1363 struct dirent *de;
1364 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001365
1366 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 if (req->offset == 0) {
1368 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001369 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001370 rewinddir(h->d);
1371 }
1372 de = readdir(h->d);
1373 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001374 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 }
1376 fde->ino = FUSE_UNKNOWN_INO;
1377 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1378 fde->off = req->offset + 1;
1379 fde->type = de->d_type;
1380 fde->namelen = strlen(de->d_name);
1381 memcpy(fde->name, de->d_name, fde->namelen + 1);
1382 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001383 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1384 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001385}
1386
Jeff Brown6249b902012-05-26 14:32:54 -07001387static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1389{
1390 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001391
1392 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001393 closedir(h->d);
1394 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001395 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001396}
1397
Jeff Brown6249b902012-05-26 14:32:54 -07001398static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001399 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1400{
1401 struct fuse_init_out out;
1402
Jeff Brown6249b902012-05-26 14:32:54 -07001403 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1404 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001405 out.major = FUSE_KERNEL_VERSION;
1406 out.minor = FUSE_KERNEL_MINOR_VERSION;
1407 out.max_readahead = req->max_readahead;
1408 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1409 out.max_background = 32;
1410 out.congestion_threshold = 32;
1411 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001412 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001413 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001414}
1415
Jeff Brown6249b902012-05-26 14:32:54 -07001416static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001417 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1418{
Brian Swetland03ee9472010-08-12 18:01:08 -07001419 switch (hdr->opcode) {
1420 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001421 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001422 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001423 }
Jeff Brown84715842012-05-25 14:07:47 -07001424
Brian Swetland03ee9472010-08-12 18:01:08 -07001425 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001426 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001427 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001428 }
Jeff Brown84715842012-05-25 14:07:47 -07001429
Brian Swetland03ee9472010-08-12 18:01:08 -07001430 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001431 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001432 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001433 }
Jeff Brown84715842012-05-25 14:07:47 -07001434
Brian Swetland03ee9472010-08-12 18:01:08 -07001435 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001436 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001437 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001438 }
Jeff Brown84715842012-05-25 14:07:47 -07001439
Brian Swetland03ee9472010-08-12 18:01:08 -07001440// case FUSE_READLINK:
1441// case FUSE_SYMLINK:
1442 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001443 const struct fuse_mknod_in *req = data;
1444 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001445 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001446 }
Jeff Brown84715842012-05-25 14:07:47 -07001447
Brian Swetland03ee9472010-08-12 18:01:08 -07001448 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001449 const struct fuse_mkdir_in *req = data;
1450 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001451 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001452 }
Jeff Brown84715842012-05-25 14:07:47 -07001453
Brian Swetland03ee9472010-08-12 18:01:08 -07001454 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001455 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001456 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001457 }
Jeff Brown84715842012-05-25 14:07:47 -07001458
Brian Swetland03ee9472010-08-12 18:01:08 -07001459 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001460 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001461 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001462 }
Jeff Brown84715842012-05-25 14:07:47 -07001463
Brian Swetland03ee9472010-08-12 18:01:08 -07001464 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001465 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001466 const char *old_name = ((const char*) data) + sizeof(*req);
1467 const char *new_name = old_name + strlen(old_name) + 1;
1468 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001469 }
Jeff Brown84715842012-05-25 14:07:47 -07001470
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001471// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001472 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001473 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001474 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001475 }
Jeff Brown84715842012-05-25 14:07:47 -07001476
Brian Swetland03ee9472010-08-12 18:01:08 -07001477 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001478 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001479 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 }
Jeff Brown84715842012-05-25 14:07:47 -07001481
Brian Swetland03ee9472010-08-12 18:01:08 -07001482 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001483 const struct fuse_write_in *req = data;
1484 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001485 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001486 }
Jeff Brown84715842012-05-25 14:07:47 -07001487
Mike Lockwood4553b082010-08-16 14:14:44 -04001488 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001489 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001490 }
Jeff Brown84715842012-05-25 14:07:47 -07001491
Brian Swetland03ee9472010-08-12 18:01:08 -07001492 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001493 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001494 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001495 }
Jeff Brown84715842012-05-25 14:07:47 -07001496
Jeff Brown6fd921a2012-05-25 15:01:21 -07001497 case FUSE_FSYNC: {
1498 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001499 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001500 }
1501
Brian Swetland03ee9472010-08-12 18:01:08 -07001502// case FUSE_SETXATTR:
1503// case FUSE_GETXATTR:
1504// case FUSE_LISTXATTR:
1505// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001506 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001507 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001508 }
1509
Brian Swetland03ee9472010-08-12 18:01:08 -07001510 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001511 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001512 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001513 }
Jeff Brown84715842012-05-25 14:07:47 -07001514
Brian Swetland03ee9472010-08-12 18:01:08 -07001515 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001516 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001517 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001518 }
Jeff Brown84715842012-05-25 14:07:47 -07001519
Brian Swetland03ee9472010-08-12 18:01:08 -07001520 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001521 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001522 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 }
Jeff Brown84715842012-05-25 14:07:47 -07001524
Brian Swetland03ee9472010-08-12 18:01:08 -07001525// case FUSE_FSYNCDIR:
1526 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001527 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001528 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 }
Jeff Brown84715842012-05-25 14:07:47 -07001530
Brian Swetland03ee9472010-08-12 18:01:08 -07001531 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001532 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1533 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1534 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001535 }
Jeff Brown84715842012-05-25 14:07:47 -07001536 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001537}
1538
Jeff Brown6249b902012-05-26 14:32:54 -07001539static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001540{
Jeff Brown6249b902012-05-26 14:32:54 -07001541 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001542 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001543 ssize_t len = read(fuse->fd,
1544 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001545 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001546 if (errno != EINTR) {
1547 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1548 }
1549 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 }
Jeff Brown84715842012-05-25 14:07:47 -07001551
1552 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001553 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1554 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001555 }
1556
Jeff Brown7729d242012-05-25 15:35:28 -07001557 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001558 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001559 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1560 handler->token, (size_t)len, hdr->len);
1561 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001562 }
1563
Jeff Brown7729d242012-05-25 15:35:28 -07001564 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001565 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001566 __u64 unique = hdr->unique;
1567 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001568
1569 /* We do not access the request again after this point because the underlying
1570 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001571
1572 if (res != NO_STATUS) {
1573 if (res) {
1574 TRACE("[%d] ERROR %d\n", handler->token, res);
1575 }
1576 fuse_status(fuse, unique, res);
1577 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001578 }
1579}
1580
Jeff Brown6249b902012-05-26 14:32:54 -07001581static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001582{
Jeff Brown6249b902012-05-26 14:32:54 -07001583 struct fuse_handler* handler = data;
1584 handle_fuse_requests(handler);
1585 return NULL;
1586}
1587
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001588static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001589 Hashmap* map = context;
1590 hashmapRemove(map, key);
1591 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001592 return true;
1593}
1594
1595static bool remove_int_to_null(void *key, void *value, void *context) {
1596 Hashmap* map = context;
1597 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001598 return true;
1599}
1600
1601static int read_package_list(struct fuse *fuse) {
1602 pthread_mutex_lock(&fuse->lock);
1603
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001604 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1605 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001606
1607 FILE* file = fopen(kPackagesListFile, "r");
1608 if (!file) {
1609 ERROR("failed to open package list: %s\n", strerror(errno));
1610 pthread_mutex_unlock(&fuse->lock);
1611 return -1;
1612 }
1613
1614 char buf[512];
1615 while (fgets(buf, sizeof(buf), file) != NULL) {
1616 char package_name[512];
1617 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001618 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001619
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001620 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1621 char* package_name_dup = strdup(package_name);
1622 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) appid);
1623
1624 char* token = strtok(gids, ",");
1625 while (token != NULL) {
1626 if (strtoul(token, NULL, 10) == AID_SDCARD_RW) {
1627 hashmapPut(fuse->appid_with_rw, (void*) appid, (void*) 1);
1628 break;
1629 }
1630 token = strtok(NULL, ",");
1631 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001632 }
1633 }
1634
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001635 TRACE("read_package_list: found %d packages, %d with sdcard_rw\n",
1636 hashmapSize(fuse->package_to_appid),
1637 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001638 fclose(file);
1639 pthread_mutex_unlock(&fuse->lock);
1640 return 0;
1641}
1642
1643static void watch_package_list(struct fuse* fuse) {
1644 struct inotify_event *event;
1645 char event_buf[512];
1646
1647 int nfd = inotify_init();
1648 if (nfd < 0) {
1649 ERROR("inotify_init failed: %s\n", strerror(errno));
1650 return;
1651 }
1652
1653 bool active = false;
1654 while (1) {
1655 if (!active) {
1656 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1657 if (res == -1) {
1658 if (errno == ENOENT || errno == EACCES) {
1659 /* Framework may not have created yet, sleep and retry */
1660 ERROR("missing packages.list; retrying\n");
1661 sleep(3);
1662 continue;
1663 } else {
1664 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1665 return;
1666 }
1667 }
1668
1669 /* Watch above will tell us about any future changes, so
1670 * read the current state. */
1671 if (read_package_list(fuse) == -1) {
1672 ERROR("read_package_list failed: %s\n", strerror(errno));
1673 return;
1674 }
1675 active = true;
1676 }
1677
1678 int event_pos = 0;
1679 int res = read(nfd, event_buf, sizeof(event_buf));
1680 if (res < (int) sizeof(*event)) {
1681 if (errno == EINTR)
1682 continue;
1683 ERROR("failed to read inotify event: %s\n", strerror(errno));
1684 return;
1685 }
1686
1687 while (res >= (int) sizeof(*event)) {
1688 int event_size;
1689 event = (struct inotify_event *) (event_buf + event_pos);
1690
1691 TRACE("inotify event: %08x\n", event->mask);
1692 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1693 /* Previously watched file was deleted, probably due to move
1694 * that swapped in new data; re-arm the watch and read. */
1695 active = false;
1696 }
1697
1698 event_size = sizeof(*event) + event->len;
1699 res -= event_size;
1700 event_pos += event_size;
1701 }
1702 }
1703}
1704
Jeff Brown6249b902012-05-26 14:32:54 -07001705static int ignite_fuse(struct fuse* fuse, int num_threads)
1706{
1707 struct fuse_handler* handlers;
1708 int i;
1709
1710 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1711 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001712 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001713 return -ENOMEM;
1714 }
1715
1716 for (i = 0; i < num_threads; i++) {
1717 handlers[i].fuse = fuse;
1718 handlers[i].token = i;
1719 }
1720
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001721 /* When deriving permissions, this thread is used to process inotify events,
1722 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001723 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001724 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001725 pthread_t thread;
1726 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1727 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001728 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001729 goto quit;
1730 }
1731 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001732
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001733 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001734 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001735 } else {
1736 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001737 }
1738
1739 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001740
1741 /* don't bother killing all of the other threads or freeing anything,
1742 * should never get here anyhow */
1743quit:
1744 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001745}
1746
Mike Lockwood4f35e622011-01-12 14:39:44 -05001747static int usage()
1748{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001749 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1750 " -u: specify UID to run as\n"
1751 " -g: specify GID to run as\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001752 " -G: specify default GID for files (default sdcard_r, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001753 " -t: specify number of threads to use (default %d)\n"
1754 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001755 " -l: derive file permissions based on legacy internal layout\n"
1756 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001757 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001758 return 1;
1759}
1760
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001761static int run(const char* source_path, const char* dest_path, uid_t uid,
1762 gid_t gid, gid_t fs_gid, int num_threads, derive_t derive, bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001763 int fd;
1764 char opts[256];
1765 int res;
1766 struct fuse fuse;
1767
1768 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001769 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001770
1771 fd = open("/dev/fuse", O_RDWR);
1772 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001773 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001774 return -1;
1775 }
1776
1777 snprintf(opts, sizeof(opts),
1778 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1779 fd, uid, gid);
1780
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001781 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001782 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001783 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1784 goto error;
1785 }
1786
1787 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1788 if (res < 0) {
1789 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001790 goto error;
1791 }
1792
1793 res = setgid(gid);
1794 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001795 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001796 goto error;
1797 }
1798
1799 res = setuid(uid);
1800 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001801 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001802 goto error;
1803 }
1804
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001805 fuse_init(&fuse, fd, source_path, fs_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001806
1807 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001808 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001809
1810 /* we do not attempt to umount the file system here because we are no longer
1811 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001812
1813error:
1814 close(fd);
1815 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001816}
1817
Brian Swetland03ee9472010-08-12 18:01:08 -07001818int main(int argc, char **argv)
1819{
Brian Swetland03ee9472010-08-12 18:01:08 -07001820 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001821 const char *source_path = NULL;
1822 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001823 uid_t uid = 0;
1824 gid_t gid = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001825 gid_t fs_gid = AID_SDCARD_R;
Jeff Brown6249b902012-05-26 14:32:54 -07001826 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001827 derive_t derive = DERIVE_NONE;
1828 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001829 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001830 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001831
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001832 int opt;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001833 while ((opt = getopt(argc, argv, "u:g:G:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001834 switch (opt) {
1835 case 'u':
1836 uid = strtoul(optarg, NULL, 10);
1837 break;
1838 case 'g':
1839 gid = strtoul(optarg, NULL, 10);
1840 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001841 case 'G':
1842 fs_gid = strtoul(optarg, NULL, 10);
1843 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001844 case 't':
1845 num_threads = strtoul(optarg, NULL, 10);
1846 break;
1847 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001848 derive = DERIVE_UNIFIED;
1849 break;
1850 case 'l':
1851 derive = DERIVE_LEGACY;
1852 break;
1853 case 's':
1854 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001855 break;
1856 case '?':
1857 default:
1858 return usage();
1859 }
1860 }
1861
1862 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001863 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001864 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001865 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001866 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001867 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001868 } else if (!uid) {
1869 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001870 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001871 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001872 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001873 ERROR("too many arguments\n");
1874 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001875 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001876 }
1877
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001878 if (!source_path) {
1879 ERROR("no source path specified\n");
1880 return usage();
1881 }
1882 if (!dest_path) {
1883 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001884 return usage();
1885 }
Jeff Brown26567352012-05-25 13:27:43 -07001886 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001887 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001888 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001889 }
Jeff Brown6249b902012-05-26 14:32:54 -07001890 if (num_threads < 1) {
1891 ERROR("number of threads must be at least 1\n");
1892 return usage();
1893 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001894 if (split_perms && derive == DERIVE_NONE) {
1895 ERROR("cannot split permissions without deriving\n");
1896 return usage();
1897 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001898
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001899 rlim.rlim_cur = 8192;
1900 rlim.rlim_max = 8192;
1901 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1902 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1903 }
1904
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001905 res = run(source_path, dest_path, uid, gid, fs_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001906 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001907}