blob: ba4d70c0ca42371391d4b41cedac03cc2dd96f6b [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070032#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070033#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070034#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040035#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080036#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070037#include <sys/uio.h>
38#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039
Jeff Sharkey44d63422013-09-12 09:44:48 -070040#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070042#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070044
Brian Swetlandb14a2c62010-08-12 18:21:12 -070045#include <private/android_filesystem_config.h>
46
Brian Swetland03ee9472010-08-12 18:01:08 -070047/* README
48 *
49 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070050 *
Brian Swetland03ee9472010-08-12 18:01:08 -070051 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070052 * directory permissions (all files are given fixed owner, group, and
53 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070054 * changeable, symlinks and hardlinks are not createable, etc.
55 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070057 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070058 * It must be run as root, but will drop to requested UID/GID as soon as it
59 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070060 *
61 * Things I believe to be true:
62 *
63 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
64 * CREAT) must bump that node's refcount
65 * - don't forget that FORGET can forget multiple references (req->nlookup)
66 * - if an op that returns a fuse_entry fails writing the reply to the
67 * kernel, you must rollback the refcount to reflect the reference the
68 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070069 *
70 * This daemon can also derive custom filesystem permissions based on directory
71 * structure when requested. These custom permissions support several features:
72 *
73 * - Apps can access their own files in /Android/data/com.example/ without
74 * requiring any additional GIDs.
75 * - Separate permissions for protecting directories like Pictures and Music.
76 * - Multi-user separation on the same physical device.
Brian Swetland03ee9472010-08-12 18:01:08 -070077 */
78
79#define FUSE_TRACE 0
80
81#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070082#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070083#else
84#define TRACE(x...) do {} while (0)
85#endif
86
Elliott Hughes300d5642014-07-08 13:53:26 -070087#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070088
89#define FUSE_UNKNOWN_INO 0xffffffff
90
Jeff Brown84715842012-05-25 14:07:47 -070091/* Maximum number of bytes to write in one request. */
92#define MAX_WRITE (256 * 1024)
93
94/* Maximum number of bytes to read in one request. */
95#define MAX_READ (128 * 1024)
96
97/* Largest possible request.
98 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
99 * the largest possible data payload. */
100#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
101
Jeff Brown6249b902012-05-26 14:32:54 -0700102/* Pseudo-error constant used to indicate that no fuse status is needed
103 * or that a reply has already been written. */
104#define NO_STATUS 1
105
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700106/* Path to system-provided mapping of package name to appIds */
107static const char* const kPackagesListFile = "/data/system/packages.list";
108
109/* Supplementary groups to execute with */
110static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
111
112/* Permission mode for a specific node. Controls how file permissions
113 * are derived for children nodes. */
114typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700115 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700116 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700117 /* This node is one level above a normal root; used for legacy layouts
118 * which use the first level to represent user_id. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700119 PERM_PRE_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700120 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700121 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700122 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700123 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700124 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700126 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700127 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700128 /* This node is "/Android/media" */
129 PERM_ANDROID_MEDIA,
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700130} perm_t;
131
Brian Swetland03ee9472010-08-12 18:01:08 -0700132struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700133 int fd;
134};
135
136struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700137 DIR *d;
138};
139
140struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700141 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700142 __u64 nid;
143 __u64 gen;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000144 /*
145 * The inode number for this FUSE node. Note that this isn't stable across
146 * multiple invocations of the FUSE daemon.
147 */
148 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700149
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700150 /* State derived based on current position in hierarchy. */
151 perm_t perm;
152 userid_t userid;
153 uid_t uid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700154 mode_t mode;
155
Paul Eastham11ccdb32010-10-14 11:04:26 -0700156 struct node *next; /* per-dir sibling list */
157 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700158 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700159
Jeff Brown6249b902012-05-26 14:32:54 -0700160 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700161 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800162 /* If non-null, this is the real name of the file in the underlying storage.
163 * This may differ from the field "name" only by case.
164 * strlen(actual_name) will always equal strlen(name), so it is safe to use
165 * namelen for both fields.
166 */
167 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700168
169 /* If non-null, an exact underlying path that should be grafted into this
170 * position. Used to support things like OBB. */
171 char* graft_path;
172 size_t graft_pathlen;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200173
174 bool deleted;
Brian Swetland03ee9472010-08-12 18:01:08 -0700175};
176
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700177static int str_hash(void *key) {
178 return hashmapHash(key, strlen(key));
179}
180
Jeff Sharkey44d63422013-09-12 09:44:48 -0700181/** Test if two string keys are equal ignoring case */
182static bool str_icase_equals(void *keyA, void *keyB) {
183 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700184}
185
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700186/* Global data for all FUSE mounts */
187struct fuse_global {
Jeff Brown6249b902012-05-26 14:32:54 -0700188 pthread_mutex_t lock;
189
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700190 uid_t uid;
191 gid_t gid;
192 bool multi_user;
193
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700194 char source_path[PATH_MAX];
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700195 char obb_path[PATH_MAX];
196
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700197 Hashmap* package_to_appid;
198
Brian Swetland03ee9472010-08-12 18:01:08 -0700199 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700200 struct node root;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700201
Narayan Kamathfaa09352015-01-13 18:21:10 +0000202 /* Used to allocate unique inode numbers for fuse nodes. We use
203 * a simple counter based scheme where inode numbers from deleted
204 * nodes aren't reused. Note that inode allocations are not stable
205 * across multiple invocation of the sdcard daemon, but that shouldn't
206 * be a huge problem in practice.
207 *
208 * Note that we restrict inodes to 32 bit unsigned integers to prevent
209 * truncation on 32 bit processes when unsigned long long stat.st_ino is
210 * assigned to an unsigned long ino_t type in an LP32 process.
211 *
212 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
213 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
214 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
215 * in fs/fuse/inode.c).
216 *
217 * Accesses must be guarded by |lock|.
218 */
219 __u32 inode_ctr;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700220
221 struct fuse* fuse_default;
222 struct fuse* fuse_read;
223 struct fuse* fuse_write;
224};
225
226/* Single FUSE mount */
227struct fuse {
228 struct fuse_global* global;
229
230 char dest_path[PATH_MAX];
231
232 int fd;
233
234 gid_t gid;
235 mode_t mask;
Brian Swetland03ee9472010-08-12 18:01:08 -0700236};
237
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700238/* Private data used by a single FUSE handler */
Jeff Brown7729d242012-05-25 15:35:28 -0700239struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700240 struct fuse* fuse;
241 int token;
242
Jeff Brown7729d242012-05-25 15:35:28 -0700243 /* To save memory, we never use the contents of the request buffer and the read
244 * buffer at the same time. This allows us to share the underlying storage. */
245 union {
246 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800247 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700248 };
249};
Brian Swetland03ee9472010-08-12 18:01:08 -0700250
Jeff Brown6249b902012-05-26 14:32:54 -0700251static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700252{
Jeff Brown6249b902012-05-26 14:32:54 -0700253 return (void *) (uintptr_t) nid;
254}
Brian Swetland03ee9472010-08-12 18:01:08 -0700255
Jeff Brown6249b902012-05-26 14:32:54 -0700256static inline __u64 ptr_to_id(void *ptr)
257{
258 return (__u64) (uintptr_t) ptr;
259}
Brian Swetland03ee9472010-08-12 18:01:08 -0700260
Jeff Brown6249b902012-05-26 14:32:54 -0700261static void acquire_node_locked(struct node* node)
262{
263 node->refcount++;
264 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
265}
266
267static void remove_node_from_parent_locked(struct node* node);
268
269static void release_node_locked(struct node* node)
270{
271 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
272 if (node->refcount > 0) {
273 node->refcount--;
274 if (!node->refcount) {
275 TRACE("DESTROY %p (%s)\n", node, node->name);
276 remove_node_from_parent_locked(node);
277
278 /* TODO: remove debugging - poison memory */
279 memset(node->name, 0xef, node->namelen);
280 free(node->name);
281 free(node->actual_name);
282 memset(node, 0xfc, sizeof(*node));
283 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800284 }
Jeff Brown6249b902012-05-26 14:32:54 -0700285 } else {
286 ERROR("Zero refcnt %p\n", node);
287 }
288}
289
290static void add_node_to_parent_locked(struct node *node, struct node *parent) {
291 node->parent = parent;
292 node->next = parent->child;
293 parent->child = node;
294 acquire_node_locked(parent);
295}
296
297static void remove_node_from_parent_locked(struct node* node)
298{
299 if (node->parent) {
300 if (node->parent->child == node) {
301 node->parent->child = node->parent->child->next;
302 } else {
303 struct node *node2;
304 node2 = node->parent->child;
305 while (node2->next != node)
306 node2 = node2->next;
307 node2->next = node->next;
308 }
309 release_node_locked(node->parent);
310 node->parent = NULL;
311 node->next = NULL;
312 }
313}
314
315/* Gets the absolute path to a node into the provided buffer.
316 *
317 * Populates 'buf' with the path and returns the length of the path on success,
318 * or returns -1 if the path is too long for the provided buffer.
319 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700320static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
321 const char* name;
322 size_t namelen;
323 if (node->graft_path) {
324 name = node->graft_path;
325 namelen = node->graft_pathlen;
326 } else if (node->actual_name) {
327 name = node->actual_name;
328 namelen = node->namelen;
329 } else {
330 name = node->name;
331 namelen = node->namelen;
332 }
333
Jeff Brown6249b902012-05-26 14:32:54 -0700334 if (bufsize < namelen + 1) {
335 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700336 }
337
Jeff Brown6249b902012-05-26 14:32:54 -0700338 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700339 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700340 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
341 if (pathlen < 0) {
342 return -1;
343 }
344 buf[pathlen++] = '/';
345 }
346
Jeff Brown6249b902012-05-26 14:32:54 -0700347 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
348 return pathlen + namelen;
349}
350
351/* Finds the absolute path of a file within a given directory.
352 * Performs a case-insensitive search for the file and sets the buffer to the path
353 * of the first matching file. If 'search' is zero or if no match is found, sets
354 * the buffer to the path that the file would have, assuming the name were case-sensitive.
355 *
356 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
357 * or returns NULL if the path is too long for the provided buffer.
358 */
359static char* find_file_within(const char* path, const char* name,
360 char* buf, size_t bufsize, int search)
361{
362 size_t pathlen = strlen(path);
363 size_t namelen = strlen(name);
364 size_t childlen = pathlen + namelen + 1;
365 char* actual;
366
367 if (bufsize <= childlen) {
368 return NULL;
369 }
370
371 memcpy(buf, path, pathlen);
372 buf[pathlen] = '/';
373 actual = buf + pathlen + 1;
374 memcpy(actual, name, namelen + 1);
375
376 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800377 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700378 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800379 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700380 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700381 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800382 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800383 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700384 if (!strcasecmp(entry->d_name, name)) {
385 /* we have a match - replace the name, don't need to copy the null again */
386 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800387 break;
388 }
389 }
390 closedir(dir);
391 }
Jeff Brown6249b902012-05-26 14:32:54 -0700392 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800393}
394
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700395static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
396 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000397 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700398 attr->size = s->st_size;
399 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800400 attr->atime = s->st_atim.tv_sec;
401 attr->mtime = s->st_mtim.tv_sec;
402 attr->ctime = s->st_ctim.tv_sec;
403 attr->atimensec = s->st_atim.tv_nsec;
404 attr->mtimensec = s->st_mtim.tv_nsec;
405 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700406 attr->mode = s->st_mode;
407 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700408
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700409 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700410
411 if (fuse->gid == AID_SDCARD_RW) {
412 /* As an optimization, certain trusted system components only run
413 * as owner but operate across all users. Since we're now handing
414 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
415 * the user boundary enforcement for the default view. The UIDs
416 * assigned to app directories are still multiuser aware. */
417 attr->gid = AID_SDCARD_RW;
418 } else {
419 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
420 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700421
422 /* Filter requested mode based on underlying file, and
423 * pass through file type. */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700424 int visible_mode = node->mode;
425 if (node->perm != PERM_PRE_ROOT) {
426 visible_mode = visible_mode & ~fuse->mask;
427 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700428 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700429 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700430 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
431}
432
Jeff Sharkey44d63422013-09-12 09:44:48 -0700433static int touch(char* path, mode_t mode) {
434 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
435 if (fd == -1) {
436 if (errno == EEXIST) {
437 return 0;
438 } else {
439 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
440 return -1;
441 }
442 }
443 close(fd);
444 return 0;
445}
446
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700447static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
448 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700449 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700450
451 /* By default, each node inherits from its parent */
452 node->perm = PERM_INHERIT;
453 node->userid = parent->userid;
454 node->uid = parent->uid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700455 node->mode = parent->mode;
456
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700457 /* Derive custom permissions based on parent and current node */
458 switch (parent->perm) {
459 case PERM_INHERIT:
460 /* Already inherited above */
461 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700462 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700463 /* Legacy internal layout places users at top level */
464 node->perm = PERM_ROOT;
465 node->userid = strtoul(node->name, NULL, 10);
Jeff Sharkeyfc000482015-03-16 10:17:47 -0700466 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700467 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700468 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700469 /* Assume masked off by default. */
470 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700471 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700472 /* App-specific directories inside; let anyone traverse */
473 node->perm = PERM_ANDROID;
474 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700475 }
476 break;
477 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* App-specific directories inside; let anyone traverse */
480 node->perm = PERM_ANDROID_DATA;
481 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700482 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700483 /* App-specific directories inside; let anyone traverse */
484 node->perm = PERM_ANDROID_OBB;
485 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700486 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700487 node->graft_path = fuse->global->obb_path;
488 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700489 } else if (!strcasecmp(node->name, "media")) {
490 /* App-specific directories inside; let anyone traverse */
491 node->perm = PERM_ANDROID_MEDIA;
492 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700493 }
494 break;
495 case PERM_ANDROID_DATA:
496 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700497 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700498 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700499 if (appid != 0) {
500 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700501 }
502 node->mode = 0770;
503 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700504 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700505}
506
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700507/* Kernel has already enforced everything we returned through
508 * derive_permissions_locked(), so this is used to lock down access
509 * even further, such as enforcing that apps hold sdcard_rw. */
510static bool check_caller_access_to_name(struct fuse* fuse,
511 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700512 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700513 /* Always block security-sensitive files at root */
514 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700515 if (!strcasecmp(name, "autorun.inf")
516 || !strcasecmp(name, ".android_secure")
517 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700518 return false;
519 }
520 }
521
Jeff Sharkey44d63422013-09-12 09:44:48 -0700522 /* Root always has access; access for any other UIDs should always
523 * be controlled through packages.list. */
524 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700525 return true;
526 }
527
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700528 /* No extra permissions to enforce */
529 return true;
530}
531
532static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700533 const struct fuse_in_header *hdr, const struct node* node, int mode) {
534 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700535}
536
Jeff Brown6249b902012-05-26 14:32:54 -0700537struct node *create_node_locked(struct fuse* fuse,
538 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700539{
540 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700541 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700542
Narayan Kamathfaa09352015-01-13 18:21:10 +0000543 // Detect overflows in the inode counter. "4 billion nodes should be enough
544 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700545 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000546 ERROR("No more inode numbers available");
547 return NULL;
548 }
549
Paul Eastham11ccdb32010-10-14 11:04:26 -0700550 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700551 if (!node) {
552 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700553 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700554 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700555 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700556 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700557 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700558 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700559 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700560 if (strcmp(name, actual_name)) {
561 node->actual_name = malloc(namelen + 1);
562 if (!node->actual_name) {
563 free(node->name);
564 free(node);
565 return NULL;
566 }
567 memcpy(node->actual_name, actual_name, namelen + 1);
568 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700569 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700570 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700571 node->ino = fuse->global->inode_ctr++;
572 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700573
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200574 node->deleted = false;
575
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700576 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700577 acquire_node_locked(node);
578 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700579 return node;
580}
581
Jeff Brown6249b902012-05-26 14:32:54 -0700582static int rename_node_locked(struct node *node, const char *name,
583 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700584{
Jeff Brown6249b902012-05-26 14:32:54 -0700585 size_t namelen = strlen(name);
586 int need_actual_name = strcmp(name, actual_name);
587
588 /* make the storage bigger without actually changing the name
589 * in case an error occurs part way */
590 if (namelen > node->namelen) {
591 char* new_name = realloc(node->name, namelen + 1);
592 if (!new_name) {
593 return -ENOMEM;
594 }
595 node->name = new_name;
596 if (need_actual_name && node->actual_name) {
597 char* new_actual_name = realloc(node->actual_name, namelen + 1);
598 if (!new_actual_name) {
599 return -ENOMEM;
600 }
601 node->actual_name = new_actual_name;
602 }
603 }
604
605 /* update the name, taking care to allocate storage before overwriting the old name */
606 if (need_actual_name) {
607 if (!node->actual_name) {
608 node->actual_name = malloc(namelen + 1);
609 if (!node->actual_name) {
610 return -ENOMEM;
611 }
612 }
613 memcpy(node->actual_name, actual_name, namelen + 1);
614 } else {
615 free(node->actual_name);
616 node->actual_name = NULL;
617 }
618 memcpy(node->name, name, namelen + 1);
619 node->namelen = namelen;
620 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700621}
622
Jeff Brown6249b902012-05-26 14:32:54 -0700623static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700624{
Jeff Brown6249b902012-05-26 14:32:54 -0700625 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700626 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700627 } else {
628 return id_to_ptr(nid);
629 }
630}
631
Jeff Brown6249b902012-05-26 14:32:54 -0700632static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
633 char* buf, size_t bufsize)
634{
635 struct node* node = lookup_node_by_id_locked(fuse, nid);
636 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
637 node = NULL;
638 }
639 return node;
640}
641
642static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700643{
644 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700645 /* use exact string comparison, nodes that differ by case
646 * must be considered distinct even if they refer to the same
647 * underlying file as otherwise operations such as "mv x x"
648 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200649 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700650 return node;
651 }
652 }
653 return 0;
654}
655
Jeff Brown6249b902012-05-26 14:32:54 -0700656static struct node* acquire_or_create_child_locked(
657 struct fuse* fuse, struct node* parent,
658 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700659{
Jeff Brown6249b902012-05-26 14:32:54 -0700660 struct node* child = lookup_child_by_name_locked(parent, name);
661 if (child) {
662 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800663 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700664 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800665 }
Jeff Brown6249b902012-05-26 14:32:54 -0700666 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700667}
668
Jeff Brown6249b902012-05-26 14:32:54 -0700669static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700670{
671 struct fuse_out_header hdr;
672 hdr.len = sizeof(hdr);
673 hdr.error = err;
674 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700675 write(fuse->fd, &hdr, sizeof(hdr));
676}
677
Jeff Brown6249b902012-05-26 14:32:54 -0700678static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700679{
680 struct fuse_out_header hdr;
681 struct iovec vec[2];
682 int res;
683
684 hdr.len = len + sizeof(hdr);
685 hdr.error = 0;
686 hdr.unique = unique;
687
688 vec[0].iov_base = &hdr;
689 vec[0].iov_len = sizeof(hdr);
690 vec[1].iov_base = data;
691 vec[1].iov_len = len;
692
693 res = writev(fuse->fd, vec, 2);
694 if (res < 0) {
695 ERROR("*** REPLY FAILED *** %d\n", errno);
696 }
697}
698
Jeff Brown6249b902012-05-26 14:32:54 -0700699static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
700 struct node* parent, const char* name, const char* actual_name,
701 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700702{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700703 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700704 struct fuse_entry_out out;
705 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700706
Jeff Brown6249b902012-05-26 14:32:54 -0700707 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700708 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700709 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700710
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700711 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700712 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
713 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700714 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700715 return -ENOMEM;
716 }
717 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700718 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700719 out.attr_valid = 10;
720 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700721 out.nodeid = node->nid;
722 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700723 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700724 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700725 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700726}
727
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700728static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700729 const char* path)
730{
731 struct fuse_attr_out out;
732 struct stat s;
733
734 if (lstat(path, &s) < 0) {
735 return -errno;
736 }
737 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700738 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700739 out.attr_valid = 10;
740 fuse_reply(fuse, unique, &out, sizeof(out));
741 return NO_STATUS;
742}
743
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700744static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
745 const __u64 child, const char* name) {
746 struct fuse_out_header hdr;
747 struct fuse_notify_delete_out data;
748 struct iovec vec[3];
749 size_t namelen = strlen(name);
750 int res;
751
752 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
753 hdr.error = FUSE_NOTIFY_DELETE;
754 hdr.unique = 0;
755
756 data.parent = parent;
757 data.child = child;
758 data.namelen = namelen;
759 data.padding = 0;
760
761 vec[0].iov_base = &hdr;
762 vec[0].iov_len = sizeof(hdr);
763 vec[1].iov_base = &data;
764 vec[1].iov_len = sizeof(data);
765 vec[2].iov_base = (void*) name;
766 vec[2].iov_len = namelen + 1;
767
768 res = writev(fuse->fd, vec, 3);
769 /* Ignore ENOENT, since other views may not have seen the entry */
770 if (res < 0 && errno != ENOENT) {
771 ERROR("*** NOTIFY FAILED *** %d\n", errno);
772 }
773}
774
Jeff Brown6249b902012-05-26 14:32:54 -0700775static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700776 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700777{
Jeff Brown6249b902012-05-26 14:32:54 -0700778 struct node* parent_node;
779 char parent_path[PATH_MAX];
780 char child_path[PATH_MAX];
781 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700782
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700783 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700784 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
785 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100786 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700787 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700788 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700789
790 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
791 child_path, sizeof(child_path), 1))) {
792 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700793 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700794 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700795 return -EACCES;
796 }
797
Jeff Brown6249b902012-05-26 14:32:54 -0700798 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700799}
800
Jeff Brown6249b902012-05-26 14:32:54 -0700801static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700802 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
803{
Jeff Brown6249b902012-05-26 14:32:54 -0700804 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700805
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700806 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700807 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100808 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700809 hdr->nodeid, node ? node->name : "?");
810 if (node) {
811 __u64 n = req->nlookup;
812 while (n--) {
813 release_node_locked(node);
814 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700815 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700816 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700817 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818}
819
Jeff Brown6249b902012-05-26 14:32:54 -0700820static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700821 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
822{
Jeff Brown6249b902012-05-26 14:32:54 -0700823 struct node* node;
824 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700825
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700826 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700827 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100828 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700829 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700830 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700831
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700832 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700833 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700834 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700835 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700836 return -EACCES;
837 }
838
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700839 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700840}
841
Jeff Brown6249b902012-05-26 14:32:54 -0700842static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700843 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
844{
Jeff Brown6249b902012-05-26 14:32:54 -0700845 struct node* node;
846 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700847 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700848
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700849 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700850 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100851 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700852 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700853 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700854
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700855 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700856 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700857 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800858
859 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700860 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700861 return -EACCES;
862 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700863
Jeff Brown6249b902012-05-26 14:32:54 -0700864 /* XXX: incomplete implementation on purpose.
865 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700866
Elliott Hughes853574d2014-07-31 12:03:03 -0700867 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700868 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700869 }
870
871 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
872 * are both set, then set it to the current time. Else, set it to the
873 * time specified in the request. Same goes for mtime. Use utimensat(2)
874 * as it allows ATIME and MTIME to be changed independently, and has
875 * nanosecond resolution which fuse also has.
876 */
877 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
878 times[0].tv_nsec = UTIME_OMIT;
879 times[1].tv_nsec = UTIME_OMIT;
880 if (req->valid & FATTR_ATIME) {
881 if (req->valid & FATTR_ATIME_NOW) {
882 times[0].tv_nsec = UTIME_NOW;
883 } else {
884 times[0].tv_sec = req->atime;
885 times[0].tv_nsec = req->atimensec;
886 }
887 }
888 if (req->valid & FATTR_MTIME) {
889 if (req->valid & FATTR_MTIME_NOW) {
890 times[1].tv_nsec = UTIME_NOW;
891 } else {
892 times[1].tv_sec = req->mtime;
893 times[1].tv_nsec = req->mtimensec;
894 }
895 }
Jeff Brown6249b902012-05-26 14:32:54 -0700896 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
897 handler->token, path, times[0].tv_sec, times[1].tv_sec);
898 if (utimensat(-1, path, times, 0) < 0) {
899 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700900 }
901 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700902 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700903}
904
Jeff Brown6249b902012-05-26 14:32:54 -0700905static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700906 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
907{
Jeff Brown6249b902012-05-26 14:32:54 -0700908 struct node* parent_node;
909 char parent_path[PATH_MAX];
910 char child_path[PATH_MAX];
911 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700912
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700913 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700914 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
915 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100916 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700917 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700918 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700919
920 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
921 child_path, sizeof(child_path), 1))) {
922 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700923 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700924 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700925 return -EACCES;
926 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700927 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700928 if (mknod(child_path, mode, req->rdev) < 0) {
929 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700930 }
Jeff Brown6249b902012-05-26 14:32:54 -0700931 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700932}
933
Jeff Brown6249b902012-05-26 14:32:54 -0700934static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700935 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
936{
Jeff Brown6249b902012-05-26 14:32:54 -0700937 struct node* parent_node;
938 char parent_path[PATH_MAX];
939 char child_path[PATH_MAX];
940 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700942 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700943 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
944 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100945 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700946 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700947 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700948
949 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
950 child_path, sizeof(child_path), 1))) {
951 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700953 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700954 return -EACCES;
955 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700956 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700957 if (mkdir(child_path, mode) < 0) {
958 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700959 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700960
961 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
962 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
963 char nomedia[PATH_MAX];
964 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
965 if (touch(nomedia, 0664) != 0) {
966 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
967 return -ENOENT;
968 }
969 }
970 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
971 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700972 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700973 if (touch(nomedia, 0664) != 0) {
974 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
975 return -ENOENT;
976 }
977 }
978
Jeff Brown6249b902012-05-26 14:32:54 -0700979 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980}
981
Jeff Brown6249b902012-05-26 14:32:54 -0700982static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700983 const struct fuse_in_header* hdr, const char* name)
984{
Jeff Brown6249b902012-05-26 14:32:54 -0700985 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200986 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -0700987 char parent_path[PATH_MAX];
988 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700989
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700990 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700991 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
992 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100993 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700994 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700995 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700996
997 if (!parent_node || !find_file_within(parent_path, name,
998 child_path, sizeof(child_path), 1)) {
999 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001000 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001001 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001002 return -EACCES;
1003 }
Jeff Brown6249b902012-05-26 14:32:54 -07001004 if (unlink(child_path) < 0) {
1005 return -errno;
1006 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001007 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001008 child_node = lookup_child_by_name_locked(parent_node, name);
1009 if (child_node) {
1010 child_node->deleted = true;
1011 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001012 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001013 if (parent_node && child_node) {
1014 /* Tell all other views that node is gone */
1015 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1016 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1017 if (fuse != fuse->global->fuse_default) {
1018 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1019 }
1020 if (fuse != fuse->global->fuse_read) {
1021 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1022 }
1023 if (fuse != fuse->global->fuse_write) {
1024 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1025 }
1026 }
Jeff Brown6249b902012-05-26 14:32:54 -07001027 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001028}
1029
Jeff Brown6249b902012-05-26 14:32:54 -07001030static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001031 const struct fuse_in_header* hdr, const char* name)
1032{
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001033 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001034 struct node* parent_node;
1035 char parent_path[PATH_MAX];
1036 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001037
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001038 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001039 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1040 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001041 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001042 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001043 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001044
1045 if (!parent_node || !find_file_within(parent_path, name,
1046 child_path, sizeof(child_path), 1)) {
1047 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001048 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001049 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001050 return -EACCES;
1051 }
Jeff Brown6249b902012-05-26 14:32:54 -07001052 if (rmdir(child_path) < 0) {
1053 return -errno;
1054 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001055 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001056 child_node = lookup_child_by_name_locked(parent_node, name);
1057 if (child_node) {
1058 child_node->deleted = true;
1059 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001060 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001061 if (parent_node && child_node) {
1062 /* Tell all other views that node is gone */
1063 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1064 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1065 if (fuse != fuse->global->fuse_default) {
1066 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1067 }
1068 if (fuse != fuse->global->fuse_read) {
1069 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1070 }
1071 if (fuse != fuse->global->fuse_write) {
1072 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1073 }
1074 }
Jeff Brown6249b902012-05-26 14:32:54 -07001075 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 Brown6249b902012-05-26 14:32:54 -07001082 struct node* old_parent_node;
1083 struct node* new_parent_node;
1084 struct node* child_node;
1085 char old_parent_path[PATH_MAX];
1086 char new_parent_path[PATH_MAX];
1087 char old_child_path[PATH_MAX];
1088 char new_child_path[PATH_MAX];
1089 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001090 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001091
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001092 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001093 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1094 old_parent_path, sizeof(old_parent_path));
1095 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1096 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001097 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001098 old_name, new_name,
1099 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1100 req->newdir, new_parent_node ? new_parent_node->name : "?");
1101 if (!old_parent_node || !new_parent_node) {
1102 res = -ENOENT;
1103 goto lookup_error;
1104 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001105 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001106 res = -EACCES;
1107 goto lookup_error;
1108 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001109 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001110 res = -EACCES;
1111 goto lookup_error;
1112 }
Jeff Brown6249b902012-05-26 14:32:54 -07001113 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1114 if (!child_node || get_node_path_locked(child_node,
1115 old_child_path, sizeof(old_child_path)) < 0) {
1116 res = -ENOENT;
1117 goto lookup_error;
1118 }
1119 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001120 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001121
1122 /* Special case for renaming a file where destination is same path
1123 * differing only by case. In this case we don't want to look for a case
1124 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1125 */
1126 int search = old_parent_node != new_parent_node
1127 || strcasecmp(old_name, new_name);
1128 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1129 new_child_path, sizeof(new_child_path), search))) {
1130 res = -ENOENT;
1131 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001132 }
1133
Jeff Brown6249b902012-05-26 14:32:54 -07001134 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1135 res = rename(old_child_path, new_child_path);
1136 if (res < 0) {
1137 res = -errno;
1138 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001139 }
1140
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001141 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001142 res = rename_node_locked(child_node, new_name, new_actual_name);
1143 if (!res) {
1144 remove_node_from_parent_locked(child_node);
1145 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001146 }
Jeff Brown6249b902012-05-26 14:32:54 -07001147 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001148
Jeff Brown6249b902012-05-26 14:32:54 -07001149io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001150 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001151done:
1152 release_node_locked(child_node);
1153lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001154 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001155 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156}
1157
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001158static int open_flags_to_access_mode(int open_flags) {
1159 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1160 return R_OK;
1161 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1162 return W_OK;
1163 } else {
1164 /* Probably O_RDRW, but treat as default to be safe */
1165 return R_OK | W_OK;
1166 }
1167}
1168
Jeff Brown6249b902012-05-26 14:32:54 -07001169static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001170 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1171{
Jeff Brown6249b902012-05-26 14:32:54 -07001172 struct node* node;
1173 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001174 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001175 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001176
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001177 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001178 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001179 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001180 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001181 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001182
1183 if (!node) {
1184 return -ENOENT;
1185 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001186 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001187 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001188 return -EACCES;
1189 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 h = malloc(sizeof(*h));
1191 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001192 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 }
Jeff Brown6249b902012-05-26 14:32:54 -07001194 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195 h->fd = open(path, req->flags);
1196 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001197 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001198 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199 }
1200 out.fh = ptr_to_id(h);
1201 out.open_flags = 0;
1202 out.padding = 0;
1203 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001204 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205}
1206
Jeff Brown6249b902012-05-26 14:32:54 -07001207static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001208 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1209{
1210 struct handle *h = id_to_ptr(req->fh);
1211 __u64 unique = hdr->unique;
1212 __u32 size = req->size;
1213 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001214 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001215 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001216
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1218 * overlaps the request buffer and will clobber data in the request. This
1219 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001220
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001221 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1222 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001223 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001224 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001226 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001228 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001229 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001230 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001231 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001232}
1233
Jeff Brown6249b902012-05-26 14:32:54 -07001234static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001235 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1236 const void* buffer)
1237{
1238 struct fuse_write_out out;
1239 struct handle *h = id_to_ptr(req->fh);
1240 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001241 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001242
Arpad Horvath49e93442014-02-18 10:18:25 +01001243 if (req->flags & O_DIRECT) {
1244 memcpy(aligned_buffer, buffer, req->size);
1245 buffer = (const __u8*) aligned_buffer;
1246 }
Jeff Brown6249b902012-05-26 14:32:54 -07001247
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001248 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001249 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001250 res = pwrite64(h->fd, buffer, req->size, req->offset);
1251 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001252 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001253 }
1254 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001255 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001256 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001257 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001258}
1259
Jeff Brown6249b902012-05-26 14:32:54 -07001260static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001261 const struct fuse_in_header* hdr)
1262{
Jeff Brown6249b902012-05-26 14:32:54 -07001263 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001264 struct statfs stat;
1265 struct fuse_statfs_out out;
1266 int res;
1267
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001268 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001269 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001270 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001271 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001272 if (res < 0) {
1273 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001275 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001276 return -errno;
1277 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001278 memset(&out, 0, sizeof(out));
1279 out.st.blocks = stat.f_blocks;
1280 out.st.bfree = stat.f_bfree;
1281 out.st.bavail = stat.f_bavail;
1282 out.st.files = stat.f_files;
1283 out.st.ffree = stat.f_ffree;
1284 out.st.bsize = stat.f_bsize;
1285 out.st.namelen = stat.f_namelen;
1286 out.st.frsize = stat.f_frsize;
1287 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001288 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001289}
1290
Jeff Brown6249b902012-05-26 14:32:54 -07001291static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001292 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1293{
1294 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001295
1296 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 close(h->fd);
1298 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001299 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001300}
1301
Jeff Brown6249b902012-05-26 14:32:54 -07001302static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001303 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1304{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001305 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1306 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001307
Elliott Hughesf6d67372014-07-08 14:38:26 -07001308 int fd = -1;
1309 if (is_dir) {
1310 struct dirhandle *dh = id_to_ptr(req->fh);
1311 fd = dirfd(dh->d);
1312 } else {
1313 struct handle *h = id_to_ptr(req->fh);
1314 fd = h->fd;
1315 }
1316
1317 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1318 is_dir ? "FSYNCDIR" : "FSYNC",
1319 id_to_ptr(req->fh), fd, is_data_sync);
1320 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1321 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001322 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001323 }
Jeff Brown6249b902012-05-26 14:32:54 -07001324 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001325}
1326
Jeff Brown6249b902012-05-26 14:32:54 -07001327static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001328 const struct fuse_in_header* hdr)
1329{
Jeff Brown6249b902012-05-26 14:32:54 -07001330 TRACE("[%d] FLUSH\n", handler->token);
1331 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001332}
1333
Jeff Brown6249b902012-05-26 14:32:54 -07001334static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001335 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1336{
Jeff Brown6249b902012-05-26 14:32:54 -07001337 struct node* node;
1338 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001339 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001340 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001342 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001343 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001344 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001345 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001346 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001347
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001349 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001350 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001351 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001352 return -EACCES;
1353 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 h = malloc(sizeof(*h));
1355 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001356 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001357 }
Jeff Brown6249b902012-05-26 14:32:54 -07001358 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001360 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001361 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001362 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001363 }
1364 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001365 out.open_flags = 0;
1366 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001368 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369}
1370
Jeff Brown6249b902012-05-26 14:32:54 -07001371static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001372 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1373{
1374 char buffer[8192];
1375 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1376 struct dirent *de;
1377 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001378
1379 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 if (req->offset == 0) {
1381 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001382 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001383 rewinddir(h->d);
1384 }
1385 de = readdir(h->d);
1386 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001387 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388 }
1389 fde->ino = FUSE_UNKNOWN_INO;
1390 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1391 fde->off = req->offset + 1;
1392 fde->type = de->d_type;
1393 fde->namelen = strlen(de->d_name);
1394 memcpy(fde->name, de->d_name, fde->namelen + 1);
1395 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001396 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1397 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001398}
1399
Jeff Brown6249b902012-05-26 14:32:54 -07001400static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001401 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1402{
1403 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001404
1405 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001406 closedir(h->d);
1407 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001408 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001409}
1410
Jeff Brown6249b902012-05-26 14:32:54 -07001411static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001412 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1413{
1414 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001415 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001416
Jeff Brown6249b902012-05-26 14:32:54 -07001417 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1418 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001419
1420 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1421 * defined (fuse version 7.6). The structure is the same from 7.6 through
1422 * 7.22. Beginning with 7.23, the structure increased in size and added
1423 * new parameters.
1424 */
1425 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1426 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1427 req->major, req->minor, FUSE_KERNEL_VERSION);
1428 return -1;
1429 }
1430
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001431 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1432 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001433 fuse_struct_size = sizeof(out);
1434#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1435 /* FUSE_KERNEL_VERSION >= 23. */
1436
1437 /* If the kernel only works on minor revs older than or equal to 22,
1438 * then use the older structure size since this code only uses the 7.22
1439 * version of the structure. */
1440 if (req->minor <= 22) {
1441 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1442 }
1443#endif
1444
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001445 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001446 out.max_readahead = req->max_readahead;
1447 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1448 out.max_background = 32;
1449 out.congestion_threshold = 32;
1450 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001451 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001452 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001453}
1454
Jeff Brown6249b902012-05-26 14:32:54 -07001455static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001456 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1457{
Brian Swetland03ee9472010-08-12 18:01:08 -07001458 switch (hdr->opcode) {
1459 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001460 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001461 return handle_lookup(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_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001465 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001466 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001467 }
Jeff Brown84715842012-05-25 14:07:47 -07001468
Brian Swetland03ee9472010-08-12 18:01:08 -07001469 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001470 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001471 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001472 }
Jeff Brown84715842012-05-25 14:07:47 -07001473
Brian Swetland03ee9472010-08-12 18:01:08 -07001474 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001475 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001476 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001477 }
Jeff Brown84715842012-05-25 14:07:47 -07001478
Brian Swetland03ee9472010-08-12 18:01:08 -07001479// case FUSE_READLINK:
1480// case FUSE_SYMLINK:
1481 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001482 const struct fuse_mknod_in *req = data;
1483 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001484 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001485 }
Jeff Brown84715842012-05-25 14:07:47 -07001486
Brian Swetland03ee9472010-08-12 18:01:08 -07001487 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001488 const struct fuse_mkdir_in *req = data;
1489 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001490 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001491 }
Jeff Brown84715842012-05-25 14:07:47 -07001492
Brian Swetland03ee9472010-08-12 18:01:08 -07001493 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001494 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001495 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001496 }
Jeff Brown84715842012-05-25 14:07:47 -07001497
Brian Swetland03ee9472010-08-12 18:01:08 -07001498 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001499 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001500 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001501 }
Jeff Brown84715842012-05-25 14:07:47 -07001502
Brian Swetland03ee9472010-08-12 18:01:08 -07001503 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001504 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001505 const char *old_name = ((const char*) data) + sizeof(*req);
1506 const char *new_name = old_name + strlen(old_name) + 1;
1507 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001508 }
Jeff Brown84715842012-05-25 14:07:47 -07001509
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001510// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001511 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001512 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001513 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001514 }
Jeff Brown84715842012-05-25 14:07:47 -07001515
Brian Swetland03ee9472010-08-12 18:01:08 -07001516 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001517 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001518 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001519 }
Jeff Brown84715842012-05-25 14:07:47 -07001520
Brian Swetland03ee9472010-08-12 18:01:08 -07001521 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001522 const struct fuse_write_in *req = data;
1523 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001524 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001525 }
Jeff Brown84715842012-05-25 14:07:47 -07001526
Mike Lockwood4553b082010-08-16 14:14:44 -04001527 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001528 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001529 }
Jeff Brown84715842012-05-25 14:07:47 -07001530
Brian Swetland03ee9472010-08-12 18:01:08 -07001531 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001532 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001533 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001534 }
Jeff Brown84715842012-05-25 14:07:47 -07001535
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001536 case FUSE_FSYNC:
1537 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001538 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001539 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001540 }
1541
Brian Swetland03ee9472010-08-12 18:01:08 -07001542// case FUSE_SETXATTR:
1543// case FUSE_GETXATTR:
1544// case FUSE_LISTXATTR:
1545// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001546 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001547 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001548 }
1549
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001551 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001552 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 }
Jeff Brown84715842012-05-25 14:07:47 -07001554
Brian Swetland03ee9472010-08-12 18:01:08 -07001555 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001556 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001557 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001558 }
Jeff Brown84715842012-05-25 14:07:47 -07001559
Brian Swetland03ee9472010-08-12 18:01:08 -07001560 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001561 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001562 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001563 }
Jeff Brown84715842012-05-25 14:07:47 -07001564
Brian Swetland03ee9472010-08-12 18:01:08 -07001565 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001566 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001567 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001568 }
Jeff Brown84715842012-05-25 14:07:47 -07001569
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001571 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001572 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1573 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001574 }
Jeff Brown84715842012-05-25 14:07:47 -07001575 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001576}
1577
Jeff Brown6249b902012-05-26 14:32:54 -07001578static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001579{
Jeff Brown6249b902012-05-26 14:32:54 -07001580 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001581 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001582 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1583 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001584 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001585 if (errno == ENODEV) {
1586 ERROR("[%d] someone stole our marbles!\n", handler->token);
1587 exit(2);
1588 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001589 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001590 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001591 }
Jeff Brown84715842012-05-25 14:07:47 -07001592
1593 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001594 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1595 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001596 }
1597
Jeff Brown7729d242012-05-25 15:35:28 -07001598 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001599 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001600 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1601 handler->token, (size_t)len, hdr->len);
1602 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001603 }
1604
Jeff Brown7729d242012-05-25 15:35:28 -07001605 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001606 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001607 __u64 unique = hdr->unique;
1608 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001609
1610 /* We do not access the request again after this point because the underlying
1611 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001612
1613 if (res != NO_STATUS) {
1614 if (res) {
1615 TRACE("[%d] ERROR %d\n", handler->token, res);
1616 }
1617 fuse_status(fuse, unique, res);
1618 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001619 }
1620}
1621
Jeff Brown6249b902012-05-26 14:32:54 -07001622static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001623{
Jeff Brown6249b902012-05-26 14:32:54 -07001624 struct fuse_handler* handler = data;
1625 handle_fuse_requests(handler);
1626 return NULL;
1627}
1628
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001629static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001630 Hashmap* map = context;
1631 hashmapRemove(map, key);
1632 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001633 return true;
1634}
1635
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001636static int read_package_list(struct fuse_global* global) {
1637 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001638
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001639 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001640
1641 FILE* file = fopen(kPackagesListFile, "r");
1642 if (!file) {
1643 ERROR("failed to open package list: %s\n", strerror(errno));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001644 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001645 return -1;
1646 }
1647
1648 char buf[512];
1649 while (fgets(buf, sizeof(buf), file) != NULL) {
1650 char package_name[512];
1651 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001652 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001653
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001654 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1655 char* package_name_dup = strdup(package_name);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001656 hashmapPut(global->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001657 }
1658 }
1659
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001660 TRACE("read_package_list: found %zu packages\n",
1661 hashmapSize(global->package_to_appid));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001662 fclose(file);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001663 pthread_mutex_unlock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001664 return 0;
1665}
1666
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001667static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001668 struct inotify_event *event;
1669 char event_buf[512];
1670
1671 int nfd = inotify_init();
1672 if (nfd < 0) {
1673 ERROR("inotify_init failed: %s\n", strerror(errno));
1674 return;
1675 }
1676
1677 bool active = false;
1678 while (1) {
1679 if (!active) {
1680 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1681 if (res == -1) {
1682 if (errno == ENOENT || errno == EACCES) {
1683 /* Framework may not have created yet, sleep and retry */
1684 ERROR("missing packages.list; retrying\n");
1685 sleep(3);
1686 continue;
1687 } else {
1688 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1689 return;
1690 }
1691 }
1692
1693 /* Watch above will tell us about any future changes, so
1694 * read the current state. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001695 if (read_package_list(global) == -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001696 ERROR("read_package_list failed: %s\n", strerror(errno));
1697 return;
1698 }
1699 active = true;
1700 }
1701
1702 int event_pos = 0;
1703 int res = read(nfd, event_buf, sizeof(event_buf));
1704 if (res < (int) sizeof(*event)) {
1705 if (errno == EINTR)
1706 continue;
1707 ERROR("failed to read inotify event: %s\n", strerror(errno));
1708 return;
1709 }
1710
1711 while (res >= (int) sizeof(*event)) {
1712 int event_size;
1713 event = (struct inotify_event *) (event_buf + event_pos);
1714
1715 TRACE("inotify event: %08x\n", event->mask);
1716 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1717 /* Previously watched file was deleted, probably due to move
1718 * that swapped in new data; re-arm the watch and read. */
1719 active = false;
1720 }
1721
1722 event_size = sizeof(*event) + event->len;
1723 res -= event_size;
1724 event_pos += event_size;
1725 }
1726 }
1727}
1728
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001729static int usage() {
1730 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001731 " -u: specify UID to run as\n"
1732 " -g: specify GID to run as\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001733 " -m: source_path is multi-user\n"
1734 " -w: runtime_write mount has full write access\n"
1735 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001736 return 1;
1737}
1738
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001739static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001740 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001741
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001742 fuse->fd = open("/dev/fuse", O_RDWR);
1743 if (fuse->fd == -1) {
1744 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001745 return -1;
1746 }
1747
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001748 umount2(fuse->dest_path, MNT_DETACH);
1749
Jeff Brown26567352012-05-25 13:27:43 -07001750 snprintf(opts, sizeof(opts),
1751 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001752 fuse->fd, fuse->global->uid, fuse->global->gid);
1753 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1754 MS_NOATIME, opts) != 0) {
1755 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1756 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001757 }
1758
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001759 fuse->gid = gid;
1760 fuse->mask = mask;
1761
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001762 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001763}
1764
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001765static void run(const char* source_path, const char* label, uid_t uid,
1766 gid_t gid, bool multi_user, bool full_write) {
1767 struct fuse_global global;
1768 struct fuse fuse_default;
1769 struct fuse fuse_read;
1770 struct fuse fuse_write;
1771 struct fuse_handler handler_default;
1772 struct fuse_handler handler_read;
1773 struct fuse_handler handler_write;
1774 pthread_t thread_default;
1775 pthread_t thread_read;
1776 pthread_t thread_write;
1777
1778 memset(&global, 0, sizeof(global));
1779 memset(&fuse_default, 0, sizeof(fuse_default));
1780 memset(&fuse_read, 0, sizeof(fuse_read));
1781 memset(&fuse_write, 0, sizeof(fuse_write));
1782 memset(&handler_default, 0, sizeof(handler_default));
1783 memset(&handler_read, 0, sizeof(handler_read));
1784 memset(&handler_write, 0, sizeof(handler_write));
1785
1786 pthread_mutex_init(&global.lock, NULL);
1787 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1788 global.uid = uid;
1789 global.gid = gid;
1790 global.multi_user = multi_user;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001791 global.next_generation = 0;
1792 global.inode_ctr = 1;
1793
1794 memset(&global.root, 0, sizeof(global.root));
1795 global.root.nid = FUSE_ROOT_ID; /* 1 */
1796 global.root.refcount = 2;
1797 global.root.namelen = strlen(source_path);
1798 global.root.name = strdup(source_path);
1799 global.root.userid = 0;
1800 global.root.uid = AID_ROOT;
1801
1802 strcpy(global.source_path, source_path);
1803
1804 if (multi_user) {
1805 global.root.perm = PERM_PRE_ROOT;
1806 global.root.mode = 0711;
1807 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
1808 } else {
1809 global.root.perm = PERM_ROOT;
1810 global.root.mode = 0771;
1811 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
1812 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001813
1814 fuse_default.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001815 fuse_read.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001816 fuse_write.global = &global;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001817
1818 global.fuse_default = &fuse_default;
1819 global.fuse_read = &fuse_read;
1820 global.fuse_write = &fuse_write;
1821
1822 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime_default/%s", label);
1823 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime_read/%s", label);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001824 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime_write/%s", label);
1825
1826 handler_default.fuse = &fuse_default;
1827 handler_read.fuse = &fuse_read;
1828 handler_write.fuse = &fuse_write;
1829
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001830 handler_default.token = 0;
1831 handler_read.token = 1;
1832 handler_write.token = 2;
1833
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001834 umask(0);
1835
1836 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1837 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1838 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1839 ERROR("failed to fuse_setup\n");
1840 exit(1);
1841 }
1842
1843 /* Drop privs */
1844 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1845 ERROR("cannot setgroups: %s\n", strerror(errno));
1846 exit(1);
1847 }
1848 if (setgid(gid) < 0) {
1849 ERROR("cannot setgid: %s\n", strerror(errno));
1850 exit(1);
1851 }
1852 if (setuid(uid) < 0) {
1853 ERROR("cannot setuid: %s\n", strerror(errno));
1854 exit(1);
1855 }
1856
1857 if (multi_user) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001858 fs_prepare_dir(global.obb_path, 0775, uid, gid);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001859 }
1860
1861 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1862 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1863 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1864 ERROR("failed to pthread_create\n");
1865 exit(1);
1866 }
1867
1868 watch_package_list(&global);
1869 ERROR("terminated prematurely\n");
1870 exit(1);
1871}
1872
1873int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001874 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001875 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001876 uid_t uid = 0;
1877 gid_t gid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001878 bool multi_user = false;
1879 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001880 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001881 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001882 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001883
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001884 int opt;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001885 while ((opt = getopt(argc, argv, "u:g:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001886 switch (opt) {
1887 case 'u':
1888 uid = strtoul(optarg, NULL, 10);
1889 break;
1890 case 'g':
1891 gid = strtoul(optarg, NULL, 10);
1892 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001893 case 'm':
1894 multi_user = true;
1895 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001896 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001897 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001898 break;
1899 case '?':
1900 default:
1901 return usage();
1902 }
1903 }
1904
1905 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001906 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001907 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001908 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001909 } else if (!label) {
1910 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001911 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001912 ERROR("too many arguments\n");
1913 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001914 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001915 }
1916
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001917 if (!source_path) {
1918 ERROR("no source path specified\n");
1919 return usage();
1920 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001921 if (!label) {
1922 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001923 return usage();
1924 }
Jeff Brown26567352012-05-25 13:27:43 -07001925 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001926 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001927 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001928 }
1929
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001930 rlim.rlim_cur = 8192;
1931 rlim.rlim_max = 8192;
1932 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1933 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1934 }
1935
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001936 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1937 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1938 sleep(1);
1939 }
1940
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001941 run(source_path, label, uid, gid, multi_user, full_write);
1942 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07001943}