blob: 634fbf191db4827a41b547ff75bb655fb31663d3 [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 Obesc255f252016-07-12 15:13:05 -040036#include <cutils/fs.h>
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040037#include <cutils/log.h>
38#include <cutils/multiuser.h>
39#include <packagelistparser/packagelistparser.h>
40
41#include <private/android_filesystem_config.h>
42
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040043// TODO(b/30222003): Fix compilation with FUSE_TRACE == 1.
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040044#define FUSE_TRACE 0
45
46#if FUSE_TRACE
47#define TRACE(x...) ALOGD(x)
48#else
49#define TRACE(x...) do {} while (0)
50#endif
51
52#define ERROR(x...) ALOGE(x)
53
54/* Maximum number of bytes to write in one request. */
55#define MAX_WRITE (256 * 1024)
56
57/* Maximum number of bytes to read in one request. */
58#define MAX_READ (128 * 1024)
59
60/* Largest possible request.
61 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
62 * the largest possible data payload. */
63#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
64
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -040065namespace {
66struct CaseInsensitiveCompare {
67 bool operator()(const std::string& lhs, const std::string& rhs) const {
68 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
69 }
70};
71}
72
73using AppIdMap = std::map<std::string, appid_t, CaseInsensitiveCompare>;
74
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -040075/* Permission mode for a specific node. Controls how file permissions
76 * are derived for children nodes. */
77typedef enum {
78 /* Nothing special; this node should just inherit from its parent. */
79 PERM_INHERIT,
80 /* This node is one level above a normal root; used for legacy layouts
81 * which use the first level to represent user_id. */
82 PERM_PRE_ROOT,
83 /* This node is "/" */
84 PERM_ROOT,
85 /* This node is "/Android" */
86 PERM_ANDROID,
87 /* This node is "/Android/data" */
88 PERM_ANDROID_DATA,
89 /* This node is "/Android/obb" */
90 PERM_ANDROID_OBB,
91 /* This node is "/Android/media" */
92 PERM_ANDROID_MEDIA,
93} perm_t;
94
95struct handle {
96 int fd;
97};
98
99struct dirhandle {
100 DIR *d;
101};
102
103struct node {
104 __u32 refcount;
105 __u64 nid;
106 __u64 gen;
107 /*
108 * The inode number for this FUSE node. Note that this isn't stable across
109 * multiple invocations of the FUSE daemon.
110 */
111 __u32 ino;
112
113 /* State derived based on current position in hierarchy. */
114 perm_t perm;
115 userid_t userid;
116 uid_t uid;
117 bool under_android;
118
119 struct node *next; /* per-dir sibling list */
120 struct node *child; /* first contained file by this dir */
121 struct node *parent; /* containing directory */
122
123 size_t namelen;
124 char *name;
125 /* If non-null, this is the real name of the file in the underlying storage.
126 * This may differ from the field "name" only by case.
127 * strlen(actual_name) will always equal strlen(name), so it is safe to use
128 * namelen for both fields.
129 */
130 char *actual_name;
131
132 /* If non-null, an exact underlying path that should be grafted into this
133 * position. Used to support things like OBB. */
134 char* graft_path;
135 size_t graft_pathlen;
136
137 bool deleted;
138};
139
140/* Global data for all FUSE mounts */
141struct fuse_global {
142 pthread_mutex_t lock;
143
144 uid_t uid;
145 gid_t gid;
146 bool multi_user;
147
148 char source_path[PATH_MAX];
149 char obb_path[PATH_MAX];
150
Jorge Lucangeli Obesd6d8faa2016-07-19 12:10:26 -0400151 AppIdMap* package_to_appid;
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400152
153 __u64 next_generation;
154 struct node root;
155
156 /* Used to allocate unique inode numbers for fuse nodes. We use
157 * a simple counter based scheme where inode numbers from deleted
158 * nodes aren't reused. Note that inode allocations are not stable
159 * across multiple invocation of the sdcard daemon, but that shouldn't
160 * be a huge problem in practice.
161 *
162 * Note that we restrict inodes to 32 bit unsigned integers to prevent
163 * truncation on 32 bit processes when unsigned long long stat.st_ino is
164 * assigned to an unsigned long ino_t type in an LP32 process.
165 *
166 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
167 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
168 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
169 * in fs/fuse/inode.c).
170 *
171 * Accesses must be guarded by |lock|.
172 */
173 __u32 inode_ctr;
174
175 struct fuse* fuse_default;
176 struct fuse* fuse_read;
177 struct fuse* fuse_write;
178};
179
180/* Single FUSE mount */
181struct fuse {
182 struct fuse_global* global;
183
184 char dest_path[PATH_MAX];
185
186 int fd;
187
188 gid_t gid;
189 mode_t mask;
190};
191
192/* Private data used by a single FUSE handler */
193struct fuse_handler {
194 struct fuse* fuse;
195 int token;
196
197 /* To save memory, we never use the contents of the request buffer and the read
198 * buffer at the same time. This allows us to share the underlying storage. */
199 union {
200 __u8 request_buffer[MAX_REQUEST_SIZE];
201 __u8 read_buffer[MAX_READ + PAGE_SIZE];
202 };
203};
204
205void handle_fuse_requests(struct fuse_handler* handler);
206void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent);
207
Jorge Lucangeli Obesc255f252016-07-12 15:13:05 -0400208#endif /* FUSE_H_ */