blob: 9db1c0984f4f34bae3e9b8acc472c412a9f3a0a5 [file] [log] [blame]
Daniel Campello3a703812015-07-20 16:23:50 -07001/*
2 * fs/sdcardfs/sdcardfs.h
3 *
4 * The sdcardfs v2.0
5 * This file system replaces the sdcard daemon on Android
6 * On version 2.0, some of the daemon functions have been ported
7 * to support the multi-user concepts of Android 4.4
8 *
9 * Copyright (c) 2013 Samsung Electronics Co. Ltd
10 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
11 * Sunghwan Yun, Sungjong Seo
12 *
13 * This program has been developed as a stackable file system based on
14 * the WrapFS which written by
15 *
16 * Copyright (c) 1998-2011 Erez Zadok
17 * Copyright (c) 2009 Shrikar Archak
18 * Copyright (c) 2003-2011 Stony Brook University
19 * Copyright (c) 2003-2011 The Research Foundation of SUNY
20 *
21 * This file is dual licensed. It may be redistributed and/or modified
22 * under the terms of the Apache 2.0 License OR version 2 of the GNU
23 * General Public License.
24 */
25
26#ifndef _SDCARDFS_H_
27#define _SDCARDFS_H_
28
29#include <linux/dcache.h>
30#include <linux/file.h>
31#include <linux/fs.h>
Daniel Rosenberg685618b2017-02-24 15:49:45 -080032#include <linux/aio.h>
Dmitry Shmidtd81b8fb2017-05-17 13:05:20 -070033#include <linux/kref.h>
Daniel Campello3a703812015-07-20 16:23:50 -070034#include <linux/mm.h>
35#include <linux/mount.h>
36#include <linux/namei.h>
37#include <linux/seq_file.h>
38#include <linux/statfs.h>
39#include <linux/fs_stack.h>
40#include <linux/magic.h>
41#include <linux/uaccess.h>
42#include <linux/slab.h>
43#include <linux/sched.h>
44#include <linux/types.h>
45#include <linux/security.h>
46#include <linux/string.h>
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -080047#include <linux/list.h>
Daniel Campello3a703812015-07-20 16:23:50 -070048#include "multiuser.h"
49
50/* the file system name */
51#define SDCARDFS_NAME "sdcardfs"
52
53/* sdcardfs root inode number */
54#define SDCARDFS_ROOT_INO 1
55
56/* useful for tracking code reachability */
Daniel Rosenberg720f8572017-03-16 17:46:13 -070057#define UDBG pr_default("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
Daniel Campello3a703812015-07-20 16:23:50 -070058
59#define SDCARDFS_DIRENT_SIZE 256
60
61/* temporary static uid settings for development */
62#define AID_ROOT 0 /* uid for accessing /mnt/sdcard & extSdcard */
63#define AID_MEDIA_RW 1023 /* internal media storage write access */
64
65#define AID_SDCARD_RW 1015 /* external storage write access */
66#define AID_SDCARD_R 1028 /* external storage read access */
67#define AID_SDCARD_PICS 1033 /* external storage photos access */
68#define AID_SDCARD_AV 1034 /* external storage audio/video access */
69#define AID_SDCARD_ALL 1035 /* access all users external storage */
Daniel Rosenbergb7c13d82017-01-25 13:48:45 -080070#define AID_MEDIA_OBB 1059 /* obb files */
71
72#define AID_SDCARD_IMAGE 1057
Daniel Campello3a703812015-07-20 16:23:50 -070073
74#define AID_PACKAGE_INFO 1027
75
Daniel Rosenberga95870e2016-10-26 20:27:20 -070076
77/*
78 * Permissions are handled by our permission function.
79 * We don't want anyone who happens to look at our inode value to prematurely
80 * block access, so store more permissive values. These are probably never
81 * used.
82 */
83#define fixup_tmp_permissions(x) \
Daniel Campello3a703812015-07-20 16:23:50 -070084 do { \
Daniel Rosenberged4e2532017-05-15 14:03:15 -070085 (x)->i_uid = SDCARDFS_I(x)->data->d_uid; \
Daniel Rosenberga95870e2016-10-26 20:27:20 -070086 (x)->i_gid = AID_SDCARD_RW; \
87 (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\
Daniel Campello3a703812015-07-20 16:23:50 -070088 } while (0)
89
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -080090/* Android 5.0 support */
Daniel Campello3a703812015-07-20 16:23:50 -070091
92/* Permission mode for a specific node. Controls how file permissions
Daniel Rosenbergbeb00302017-03-16 17:42:58 -070093 * are derived for children nodes.
94 */
Daniel Campello3a703812015-07-20 16:23:50 -070095typedef enum {
Daniel Rosenbergbeb00302017-03-16 17:42:58 -070096 /* Nothing special; this node should just inherit from its parent. */
97 PERM_INHERIT,
98 /* This node is one level above a normal root; used for legacy layouts
99 * which use the first level to represent user_id.
100 */
101 PERM_PRE_ROOT,
102 /* This node is "/" */
103 PERM_ROOT,
104 /* This node is "/Android" */
105 PERM_ANDROID,
106 /* This node is "/Android/data" */
107 PERM_ANDROID_DATA,
108 /* This node is "/Android/obb" */
109 PERM_ANDROID_OBB,
110 /* This node is "/Android/media" */
111 PERM_ANDROID_MEDIA,
112 /* This node is "/Android/[data|media|obb]/[package]" */
113 PERM_ANDROID_PACKAGE,
114 /* This node is "/Android/[data|media|obb]/[package]/cache" */
115 PERM_ANDROID_PACKAGE_CACHE,
Daniel Campello3a703812015-07-20 16:23:50 -0700116} perm_t;
117
Daniel Campello3a703812015-07-20 16:23:50 -0700118struct sdcardfs_sb_info;
119struct sdcardfs_mount_options;
Daniel Rosenbergb7c13d82017-01-25 13:48:45 -0800120struct sdcardfs_inode_info;
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700121struct sdcardfs_inode_data;
Daniel Campello3a703812015-07-20 16:23:50 -0700122
123/* Do not directly use this function. Use OVERRIDE_CRED() instead. */
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700124const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
125 struct sdcardfs_inode_data *data);
Daniel Campello3a703812015-07-20 16:23:50 -0700126/* Do not directly use this function, use REVERT_CRED() instead. */
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700127void revert_fsids(const struct cred *old_cred);
Daniel Campello3a703812015-07-20 16:23:50 -0700128
129/* operations vectors defined in specific files */
130extern const struct file_operations sdcardfs_main_fops;
131extern const struct file_operations sdcardfs_dir_fops;
132extern const struct inode_operations sdcardfs_main_iops;
133extern const struct inode_operations sdcardfs_dir_iops;
134extern const struct inode_operations sdcardfs_symlink_iops;
135extern const struct super_operations sdcardfs_sops;
136extern const struct dentry_operations sdcardfs_ci_dops;
137extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
138extern const struct vm_operations_struct sdcardfs_vm_ops;
139
140extern int sdcardfs_init_inode_cache(void);
141extern void sdcardfs_destroy_inode_cache(void);
142extern int sdcardfs_init_dentry_cache(void);
143extern void sdcardfs_destroy_dentry_cache(void);
144extern int new_dentry_private_data(struct dentry *dentry);
145extern void free_dentry_private_data(struct dentry *dentry);
146extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro986122f2012-06-10 17:13:09 -0400147 unsigned int flags);
fluxidd0f23e2016-07-29 18:52:13 +0200148extern struct inode *sdcardfs_iget(struct super_block *sb,
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800149 struct inode *lower_inode, userid_t id);
Daniel Campello3a703812015-07-20 16:23:50 -0700150extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800151 struct path *lower_path, userid_t id);
Daniel Campello3a703812015-07-20 16:23:50 -0700152
153/* file private data */
154struct sdcardfs_file_info {
155 struct file *lower_file;
156 const struct vm_operations_struct *lower_vm_ops;
157};
158
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700159struct sdcardfs_inode_data {
160 struct kref refcount;
161 bool abandoned;
162
Daniel Campello3a703812015-07-20 16:23:50 -0700163 perm_t perm;
164 userid_t userid;
165 uid_t d_uid;
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800166 bool under_android;
Daniel Rosenbergb7c13d82017-01-25 13:48:45 -0800167 bool under_cache;
168 bool under_obb;
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700169};
170
171/* sdcardfs inode data in memory */
172struct sdcardfs_inode_info {
173 struct inode *lower_inode;
174 /* state derived based on current position in hierarchy */
175 struct sdcardfs_inode_data *data;
176
Daniel Rosenberg3286c8a2016-05-18 16:57:10 -0700177 /* top folder for ownership */
Daniel Rosenbergdc2e3812018-02-01 16:52:22 -0800178 spinlock_t top_lock;
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700179 struct sdcardfs_inode_data *top_data;
Daniel Campello3a703812015-07-20 16:23:50 -0700180
181 struct inode vfs_inode;
182};
183
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800184
Daniel Campello3a703812015-07-20 16:23:50 -0700185/* sdcardfs dentry data in memory */
186struct sdcardfs_dentry_info {
187 spinlock_t lock; /* protects lower_path */
188 struct path lower_path;
189 struct path orig_path;
190};
191
192struct sdcardfs_mount_options {
193 uid_t fs_low_uid;
194 gid_t fs_low_gid;
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800195 userid_t fs_user_id;
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800196 bool multiuser;
Daniel Rosenberg36ae01f2017-07-19 17:25:07 -0700197 bool gid_derivation;
Daniel Rosenbergc99c82d2018-01-18 16:17:16 -0800198 bool default_normal;
Daniel Rosenbergb315cf12018-10-25 16:25:15 -0700199 bool unshared_obb;
Daniel Campello3a703812015-07-20 16:23:50 -0700200 unsigned int reserved_mb;
201};
202
Daniel Rosenbergc7bae0d2016-10-26 17:36:05 -0700203struct sdcardfs_vfsmount_options {
204 gid_t gid;
205 mode_t mask;
206};
207
208extern int parse_options_remount(struct super_block *sb, char *options, int silent,
209 struct sdcardfs_vfsmount_options *vfsopts);
210
Daniel Campello3a703812015-07-20 16:23:50 -0700211/* sdcardfs super-block data in memory */
212struct sdcardfs_sb_info {
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800213 struct super_block *sb;
Daniel Campello3a703812015-07-20 16:23:50 -0700214 struct super_block *lower_sb;
215 /* derived perm policy : some of options have been added
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700216 * to sdcardfs_mount_options (Android 4.4 support)
217 */
Daniel Campello3a703812015-07-20 16:23:50 -0700218 struct sdcardfs_mount_options options;
219 spinlock_t lock; /* protects obbpath */
220 char *obbpath_s;
221 struct path obbpath;
222 void *pkgl_id;
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800223 struct list_head list;
Daniel Campello3a703812015-07-20 16:23:50 -0700224};
225
226/*
227 * inode to private data
228 *
229 * Since we use containers and the struct inode is _inside_ the
230 * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
231 * inode pointer), return a valid non-NULL pointer.
232 */
233static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
234{
235 return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
236}
237
238/* dentry to private data */
239#define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
240
241/* superblock to private data */
242#define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
243
244/* file to private Data */
245#define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
246
247/* file to lower file */
248static inline struct file *sdcardfs_lower_file(const struct file *f)
249{
250 return SDCARDFS_F(f)->lower_file;
251}
252
253static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
254{
255 SDCARDFS_F(f)->lower_file = val;
256}
257
258/* inode to lower inode. */
259static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
260{
261 return SDCARDFS_I(i)->lower_inode;
262}
263
264static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
265{
266 SDCARDFS_I(i)->lower_inode = val;
267}
268
269/* superblock to lower superblock */
270static inline struct super_block *sdcardfs_lower_super(
271 const struct super_block *sb)
272{
273 return SDCARDFS_SB(sb)->lower_sb;
274}
275
276static inline void sdcardfs_set_lower_super(struct super_block *sb,
277 struct super_block *val)
278{
279 SDCARDFS_SB(sb)->lower_sb = val;
280}
281
282/* path based (dentry/mnt) macros */
283static inline void pathcpy(struct path *dst, const struct path *src)
284{
285 dst->dentry = src->dentry;
286 dst->mnt = src->mnt;
287}
288
289/* sdcardfs_get_pname functions calls path_get()
290 * therefore, the caller must call "proper" path_put functions
291 */
292#define SDCARDFS_DENT_FUNC(pname) \
293static inline void sdcardfs_get_##pname(const struct dentry *dent, \
294 struct path *pname) \
295{ \
296 spin_lock(&SDCARDFS_D(dent)->lock); \
297 pathcpy(pname, &SDCARDFS_D(dent)->pname); \
298 path_get(pname); \
299 spin_unlock(&SDCARDFS_D(dent)->lock); \
300 return; \
301} \
302static inline void sdcardfs_put_##pname(const struct dentry *dent, \
303 struct path *pname) \
304{ \
305 path_put(pname); \
306 return; \
307} \
308static inline void sdcardfs_set_##pname(const struct dentry *dent, \
309 struct path *pname) \
310{ \
311 spin_lock(&SDCARDFS_D(dent)->lock); \
312 pathcpy(&SDCARDFS_D(dent)->pname, pname); \
313 spin_unlock(&SDCARDFS_D(dent)->lock); \
314 return; \
315} \
316static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
317{ \
318 spin_lock(&SDCARDFS_D(dent)->lock); \
319 SDCARDFS_D(dent)->pname.dentry = NULL; \
320 SDCARDFS_D(dent)->pname.mnt = NULL; \
321 spin_unlock(&SDCARDFS_D(dent)->lock); \
322 return; \
323} \
324static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
325{ \
326 struct path pname; \
327 spin_lock(&SDCARDFS_D(dent)->lock); \
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700328 if (SDCARDFS_D(dent)->pname.dentry) { \
Daniel Campello3a703812015-07-20 16:23:50 -0700329 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
330 SDCARDFS_D(dent)->pname.dentry = NULL; \
331 SDCARDFS_D(dent)->pname.mnt = NULL; \
332 spin_unlock(&SDCARDFS_D(dent)->lock); \
333 path_put(&pname); \
334 } else \
335 spin_unlock(&SDCARDFS_D(dent)->lock); \
336 return; \
337}
338
339SDCARDFS_DENT_FUNC(lower_path)
340SDCARDFS_DENT_FUNC(orig_path)
341
Daniel Rosenberg6a7ea012017-01-22 15:32:49 -0800342static inline bool sbinfo_has_sdcard_magic(struct sdcardfs_sb_info *sbinfo)
343{
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700344 return sbinfo && sbinfo->sb
345 && sbinfo->sb->s_magic == SDCARDFS_SUPER_MAGIC;
Daniel Rosenberg6a7ea012017-01-22 15:32:49 -0800346}
347
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700348static inline struct sdcardfs_inode_data *data_get(
349 struct sdcardfs_inode_data *data)
Daniel Rosenberg3286c8a2016-05-18 16:57:10 -0700350{
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700351 if (data)
352 kref_get(&data->refcount);
353 return data;
Daniel Rosenberg3286c8a2016-05-18 16:57:10 -0700354}
355
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700356static inline struct sdcardfs_inode_data *top_data_get(
357 struct sdcardfs_inode_info *info)
Daniel Rosenberg3286c8a2016-05-18 16:57:10 -0700358{
Daniel Rosenbergdc2e3812018-02-01 16:52:22 -0800359 struct sdcardfs_inode_data *top_data;
360
361 spin_lock(&info->top_lock);
362 top_data = data_get(info->top_data);
363 spin_unlock(&info->top_lock);
364 return top_data;
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700365}
366
367extern void data_release(struct kref *ref);
368
369static inline void data_put(struct sdcardfs_inode_data *data)
370{
371 kref_put(&data->refcount, data_release);
372}
373
374static inline void release_own_data(struct sdcardfs_inode_info *info)
375{
376 /*
377 * This happens exactly once per inode. At this point, the inode that
378 * originally held this data is about to be freed, and all references
379 * to it are held as a top value, and will likely be released soon.
380 */
381 info->data->abandoned = true;
382 data_put(info->data);
383}
384
385static inline void set_top(struct sdcardfs_inode_info *info,
Daniel Rosenbergdc2e3812018-02-01 16:52:22 -0800386 struct sdcardfs_inode_info *top_owner)
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700387{
Daniel Rosenbergdc2e3812018-02-01 16:52:22 -0800388 struct sdcardfs_inode_data *old_top;
389 struct sdcardfs_inode_data *new_top = NULL;
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700390
Daniel Rosenbergdc2e3812018-02-01 16:52:22 -0800391 if (top_owner)
392 new_top = top_data_get(top_owner);
393
394 spin_lock(&info->top_lock);
395 old_top = info->top_data;
396 info->top_data = new_top;
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700397 if (old_top)
398 data_put(old_top);
Daniel Rosenbergdc2e3812018-02-01 16:52:22 -0800399 spin_unlock(&info->top_lock);
Daniel Rosenberg3286c8a2016-05-18 16:57:10 -0700400}
401
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700402static inline int get_gid(struct vfsmount *mnt,
Daniel Rosenbergc99c82d2018-01-18 16:17:16 -0800403 struct super_block *sb,
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700404 struct sdcardfs_inode_data *data)
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700405{
Daniel Rosenbergc99c82d2018-01-18 16:17:16 -0800406 struct sdcardfs_vfsmount_options *vfsopts = mnt->data;
407 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(sb);
Daniel Rosenberga95870e2016-10-26 20:27:20 -0700408
Daniel Rosenbergc99c82d2018-01-18 16:17:16 -0800409 if (vfsopts->gid == AID_SDCARD_RW && !sbi->options.default_normal)
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800410 /* As an optimization, certain trusted system components only run
411 * as owner but operate across all users. Since we're now handing
412 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
413 * the user boundary enforcement for the default view. The UIDs
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700414 * assigned to app directories are still multiuser aware.
415 */
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800416 return AID_SDCARD_RW;
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700417 else
Daniel Rosenbergc99c82d2018-01-18 16:17:16 -0800418 return multiuser_get_uid(data->userid, vfsopts->gid);
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800419}
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700420
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700421static inline int get_mode(struct vfsmount *mnt,
422 struct sdcardfs_inode_info *info,
423 struct sdcardfs_inode_data *data)
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700424{
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800425 int owner_mode;
426 int filtered_mode;
Daniel Rosenberga95870e2016-10-26 20:27:20 -0700427 struct sdcardfs_vfsmount_options *opts = mnt->data;
428 int visible_mode = 0775 & ~opts->mask;
429
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800430
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700431 if (data->perm == PERM_PRE_ROOT) {
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800432 /* Top of multi-user view should always be visible to ensure
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700433 * secondary users can traverse inside.
434 */
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800435 visible_mode = 0711;
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700436 } else if (data->under_android) {
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800437 /* Block "other" access to Android directories, since only apps
438 * belonging to a specific user should be in there; we still
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700439 * leave +x open for the default view.
440 */
441 if (opts->gid == AID_SDCARD_RW)
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800442 visible_mode = visible_mode & ~0006;
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700443 else
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800444 visible_mode = visible_mode & ~0007;
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800445 }
446 owner_mode = info->lower_inode->i_mode & 0700;
447 filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
448 return filtered_mode;
449}
450
Daniel Campello3a703812015-07-20 16:23:50 -0700451static inline int has_graft_path(const struct dentry *dent)
452{
453 int ret = 0;
454
455 spin_lock(&SDCARDFS_D(dent)->lock);
456 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
457 ret = 1;
458 spin_unlock(&SDCARDFS_D(dent)->lock);
459
460 return ret;
461}
462
463static inline void sdcardfs_get_real_lower(const struct dentry *dent,
464 struct path *real_lower)
465{
466 /* in case of a local obb dentry
467 * the orig_path should be returned
468 */
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700469 if (has_graft_path(dent))
Daniel Campello3a703812015-07-20 16:23:50 -0700470 sdcardfs_get_orig_path(dent, real_lower);
471 else
472 sdcardfs_get_lower_path(dent, real_lower);
473}
474
475static inline void sdcardfs_put_real_lower(const struct dentry *dent,
476 struct path *real_lower)
477{
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700478 if (has_graft_path(dent))
Daniel Campello3a703812015-07-20 16:23:50 -0700479 sdcardfs_put_orig_path(dent, real_lower);
480 else
481 sdcardfs_put_lower_path(dent, real_lower);
482}
483
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800484extern struct mutex sdcardfs_super_list_lock;
485extern struct list_head sdcardfs_super_list;
486
Daniel Campello3a703812015-07-20 16:23:50 -0700487/* for packagelist.c */
Daniel Rosenberga3306942016-05-10 13:42:43 -0700488extern appid_t get_appid(const char *app_name);
Daniel Rosenbergb7c13d82017-01-25 13:48:45 -0800489extern appid_t get_ext_gid(const char *app_name);
Daniel Rosenberg6a7ea012017-01-22 15:32:49 -0800490extern appid_t is_excluded(const char *app_name, userid_t userid);
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700491extern int check_caller_access_to_name(struct inode *parent_node, const struct qstr *name);
Daniel Campello3a703812015-07-20 16:23:50 -0700492extern int packagelist_init(void);
493extern void packagelist_exit(void);
494
495/* for derived_perm.c */
Daniel Rosenberg6a7ea012017-01-22 15:32:49 -0800496#define BY_NAME (1 << 0)
497#define BY_USERID (1 << 1)
498struct limit_search {
499 unsigned int flags;
Daniel Rosenberg47b534f2017-03-08 17:20:02 -0800500 struct qstr name;
Daniel Rosenberg6a7ea012017-01-22 15:32:49 -0800501 userid_t userid;
502};
503
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700504extern void setup_derived_state(struct inode *inode, perm_t perm,
Daniel Rosenbergdc2e3812018-02-01 16:52:22 -0800505 userid_t userid, uid_t uid);
Daniel Campello3a703812015-07-20 16:23:50 -0700506extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
Daniel Rosenberg2d336b62017-01-31 20:07:51 -0800507extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name);
Daniel Rosenberg6a7ea012017-01-22 15:32:49 -0800508extern void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit);
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800509
510extern void update_derived_permission_lock(struct dentry *dentry);
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700511void fixup_lower_ownership(struct dentry *dentry, const char *name);
Daniel Campello3a703812015-07-20 16:23:50 -0700512extern int need_graft_path(struct dentry *dentry);
513extern int is_base_obbpath(struct dentry *dentry);
514extern int is_obbpath_invalid(struct dentry *dentry);
515extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
516
517/* locking helpers */
518static inline struct dentry *lock_parent(struct dentry *dentry)
519{
520 struct dentry *dir = dget_parent(dentry);
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700521
Daniel Campello3a703812015-07-20 16:23:50 -0700522 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
523 return dir;
524}
525
526static inline void unlock_dir(struct dentry *dir)
527{
528 mutex_unlock(&dir->d_inode->i_mutex);
529 dput(dir);
530}
531
532static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
533{
534 int err;
535 struct dentry *dent;
536 struct iattr attrs;
fluxidd0f23e2016-07-29 18:52:13 +0200537 struct path parent;
Daniel Campello3a703812015-07-20 16:23:50 -0700538
fluxidd0f23e2016-07-29 18:52:13 +0200539 dent = kern_path_locked(path_s, &parent);
Daniel Campello3a703812015-07-20 16:23:50 -0700540 if (IS_ERR(dent)) {
541 err = PTR_ERR(dent);
542 if (err == -EEXIST)
543 err = 0;
544 goto out_unlock;
545 }
546
Daniel Rosenberge78bbb32016-10-26 16:48:45 -0700547 err = vfs_mkdir2(parent.mnt, parent.dentry->d_inode, dent, mode);
Daniel Campello3a703812015-07-20 16:23:50 -0700548 if (err) {
549 if (err == -EEXIST)
550 err = 0;
551 goto out_dput;
552 }
553
554 attrs.ia_uid = uid;
555 attrs.ia_gid = gid;
556 attrs.ia_valid = ATTR_UID | ATTR_GID;
557 mutex_lock(&dent->d_inode->i_mutex);
Daniel Rosenberge78bbb32016-10-26 16:48:45 -0700558 notify_change2(parent.mnt, dent, &attrs);
Daniel Campello3a703812015-07-20 16:23:50 -0700559 mutex_unlock(&dent->d_inode->i_mutex);
560
561out_dput:
562 dput(dent);
563
564out_unlock:
565 /* parent dentry locked by lookup_create */
fluxidd0f23e2016-07-29 18:52:13 +0200566 mutex_unlock(&parent.dentry->d_inode->i_mutex);
567 path_put(&parent);
Daniel Campello3a703812015-07-20 16:23:50 -0700568
Daniel Campello3a703812015-07-20 16:23:50 -0700569 return err;
570}
571
572/*
573 * Return 1, if a disk has enough free space, otherwise 0.
574 * We assume that any files can not be overwritten.
575 */
576static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
577{
578 int err;
579 struct path lower_path;
580 struct kstatfs statfs;
581 u64 avail;
582 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
583
584 if (sbi->options.reserved_mb) {
585 /* Get fs stat of lower filesystem. */
586 sdcardfs_get_lower_path(dentry, &lower_path);
587 err = vfs_statfs(&lower_path, &statfs);
588 sdcardfs_put_lower_path(dentry, &lower_path);
589
590 if (unlikely(err))
591 return 0;
592
593 /* Invalid statfs informations. */
594 if (unlikely(statfs.f_bsize == 0))
595 return 0;
596
597 /* if you are checking directory, set size to f_bsize. */
598 if (unlikely(dir))
599 size = statfs.f_bsize;
600
601 /* available size */
602 avail = statfs.f_bavail * statfs.f_bsize;
603
604 /* not enough space */
605 if ((u64)size > avail)
606 return 0;
607
608 /* enough space */
609 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
610 return 1;
611
612 return 0;
613 } else
614 return 1;
615}
616
Daniel Rosenberga95870e2016-10-26 20:27:20 -0700617/*
618 * Copies attrs and maintains sdcardfs managed attrs
619 * Since our permission check handles all special permissions, set those to be open
620 */
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800621static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
622{
Daniel Rosenberga95870e2016-10-26 20:27:20 -0700623
624 dest->i_mode = (src->i_mode & S_IFMT) | S_IRWXU | S_IRWXG |
625 S_IROTH | S_IXOTH; /* 0775 */
Daniel Rosenberged4e2532017-05-15 14:03:15 -0700626 dest->i_uid = SDCARDFS_I(dest)->data->d_uid;
Daniel Rosenberga95870e2016-10-26 20:27:20 -0700627 dest->i_gid = AID_SDCARD_RW;
Daniel Rosenberg5c09ef42016-02-03 21:08:21 -0800628 dest->i_rdev = src->i_rdev;
629 dest->i_atime = src->i_atime;
630 dest->i_mtime = src->i_mtime;
631 dest->i_ctime = src->i_ctime;
632 dest->i_blkbits = src->i_blkbits;
633 dest->i_flags = src->i_flags;
634 set_nlink(dest, src->i_nlink);
635}
Daniel Rosenberg2d336b62017-01-31 20:07:51 -0800636
637static inline bool str_case_eq(const char *s1, const char *s2)
638{
639 return !strcasecmp(s1, s2);
640}
641
Daniel Rosenbergbeb00302017-03-16 17:42:58 -0700642static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len)
643{
Daniel Rosenberg47b534f2017-03-08 17:20:02 -0800644 return !strncasecmp(s1, s2, len);
645}
646
Daniel Rosenberg2d336b62017-01-31 20:07:51 -0800647static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2)
648{
Ritesh Harjanidbd58bd2018-03-19 15:49:54 +0530649 return q1->len == q2->len && str_n_case_eq(q1->name, q2->name, q2->len);
Daniel Rosenberg2d336b62017-01-31 20:07:51 -0800650}
651
Daniel Rosenberg2d336b62017-01-31 20:07:51 -0800652#define QSTR_LITERAL(string) QSTR_INIT(string, sizeof(string)-1)
653
Daniel Campello3a703812015-07-20 16:23:50 -0700654#endif /* not _SDCARDFS_H_ */