blob: 9dafa79eade046c7093c65083659c5736f76bcbc [file] [log] [blame]
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef FUSE_H_
18#define FUSE_H_
19
20#include <dirent.h>
21#include <fcntl.h>
22#include <linux/fuse.h>
23#include <pthread.h>
24#include <stdbool.h>
25#include <stdlib.h>
26#include <sys/param.h>
27#include <sys/stat.h>
28#include <sys/statfs.h>
29#include <sys/types.h>
30#include <sys/uio.h>
31#include <unistd.h>
32
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040033#include <map>
34#include <string>
35
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -040036#include <android-base/logging.h>
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040037#include <cutils/fs.h>
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040038#include <cutils/log.h>
39#include <cutils/multiuser.h>
40#include <packagelistparser/packagelistparser.h>
41
42#include <private/android_filesystem_config.h>
43
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040044#define FUSE_TRACE 0
45
46#if FUSE_TRACE
47#define TRACE(x...) ALOGD(x)
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -040048static constexpr bool kEnableDLog = true;
49#else // FUSE_TRACE == 0
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040050#define TRACE(x...) do {} while (0)
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -040051static constexpr bool kEnableDLog = false;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040052#endif
53
Jorge Lucangeli Obes714ec9d2016-07-20 15:19:44 -040054// Use same strategy as DCHECK().
55#define DLOG(x) \
56 if (kEnableDLog) LOG(x)
57
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040058#define ERROR(x...) ALOGE(x)
59
60/* Maximum number of bytes to write in one request. */
61#define MAX_WRITE (256 * 1024)
62
63/* Maximum number of bytes to read in one request. */
64#define MAX_READ (128 * 1024)
65
66/* Largest possible request.
67 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
68 * the largest possible data payload. */
69#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
70
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040071namespace {
72struct CaseInsensitiveCompare {
73 bool operator()(const std::string& lhs, const std::string& rhs) const {
74 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
75 }
76};
77}
78
79using AppIdMap = std::map<std::string, appid_t, CaseInsensitiveCompare>;
80
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040081/* Permission mode for a specific node. Controls how file permissions
82 * are derived for children nodes. */
83typedef enum {
84 /* Nothing special; this node should just inherit from its parent. */
85 PERM_INHERIT,
86 /* This node is one level above a normal root; used for legacy layouts
87 * which use the first level to represent user_id. */
88 PERM_PRE_ROOT,
89 /* This node is "/" */
90 PERM_ROOT,
91 /* This node is "/Android" */
92 PERM_ANDROID,
93 /* This node is "/Android/data" */
94 PERM_ANDROID_DATA,
95 /* This node is "/Android/obb" */
96 PERM_ANDROID_OBB,
97 /* This node is "/Android/media" */
98 PERM_ANDROID_MEDIA,
99} perm_t;
100
101struct handle {
102 int fd;
103};
104
105struct dirhandle {
106 DIR *d;
107};
108
109struct node {
110 __u32 refcount;
111 __u64 nid;
112 __u64 gen;
113 /*
114 * The inode number for this FUSE node. Note that this isn't stable across
115 * multiple invocations of the FUSE daemon.
116 */
117 __u32 ino;
118
119 /* State derived based on current position in hierarchy. */
120 perm_t perm;
121 userid_t userid;
122 uid_t uid;
123 bool under_android;
124
125 struct node *next; /* per-dir sibling list */
126 struct node *child; /* first contained file by this dir */
127 struct node *parent; /* containing directory */
128
129 size_t namelen;
130 char *name;
131 /* If non-null, this is the real name of the file in the underlying storage.
132 * This may differ from the field "name" only by case.
133 * strlen(actual_name) will always equal strlen(name), so it is safe to use
134 * namelen for both fields.
135 */
136 char *actual_name;
137
138 /* If non-null, an exact underlying path that should be grafted into this
139 * position. Used to support things like OBB. */
140 char* graft_path;
141 size_t graft_pathlen;
142
143 bool deleted;
144};
145
146/* Global data for all FUSE mounts */
147struct fuse_global {
148 pthread_mutex_t lock;
149
150 uid_t uid;
151 gid_t gid;
152 bool multi_user;
153
154 char source_path[PATH_MAX];
155 char obb_path[PATH_MAX];
156
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400157 AppIdMap* package_to_appid;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400158
159 __u64 next_generation;
160 struct node root;
161
162 /* Used to allocate unique inode numbers for fuse nodes. We use
163 * a simple counter based scheme where inode numbers from deleted
164 * nodes aren't reused. Note that inode allocations are not stable
165 * across multiple invocation of the sdcard daemon, but that shouldn't
166 * be a huge problem in practice.
167 *
168 * Note that we restrict inodes to 32 bit unsigned integers to prevent
169 * truncation on 32 bit processes when unsigned long long stat.st_ino is
170 * assigned to an unsigned long ino_t type in an LP32 process.
171 *
172 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
173 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
174 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
175 * in fs/fuse/inode.c).
176 *
177 * Accesses must be guarded by |lock|.
178 */
179 __u32 inode_ctr;
180
181 struct fuse* fuse_default;
182 struct fuse* fuse_read;
183 struct fuse* fuse_write;
184};
185
186/* Single FUSE mount */
187struct fuse {
188 struct fuse_global* global;
189
190 char dest_path[PATH_MAX];
191
192 int fd;
193
194 gid_t gid;
195 mode_t mask;
196};
197
198/* Private data used by a single FUSE handler */
199struct fuse_handler {
200 struct fuse* fuse;
201 int token;
202
203 /* To save memory, we never use the contents of the request buffer and the read
204 * buffer at the same time. This allows us to share the underlying storage. */
205 union {
206 __u8 request_buffer[MAX_REQUEST_SIZE];
207 __u8 read_buffer[MAX_READ + PAGE_SIZE];
208 };
209};
210
211void handle_fuse_requests(struct fuse_handler* handler);
212void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent);
213
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400214#endif /* FUSE_H_ */