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