blob: 380982b4a567de7bed8ba08abafedd8fd8b5f3b0 [file] [log] [blame]
Daniel Campello35c9e242015-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 Rosenberg85626b42017-02-24 15:49:45 -080032#include <linux/aio.h>
Daniel Campello35c9e242015-07-20 16:23:50 -070033#include <linux/mm.h>
34#include <linux/mount.h>
35#include <linux/namei.h>
36#include <linux/seq_file.h>
37#include <linux/statfs.h>
38#include <linux/fs_stack.h>
39#include <linux/magic.h>
40#include <linux/uaccess.h>
41#include <linux/slab.h>
42#include <linux/sched.h>
43#include <linux/types.h>
44#include <linux/security.h>
45#include <linux/string.h>
Daniel Rosenberg497ac902016-02-03 21:08:21 -080046#include <linux/list.h>
Daniel Campello35c9e242015-07-20 16:23:50 -070047#include "multiuser.h"
48
49/* the file system name */
50#define SDCARDFS_NAME "sdcardfs"
51
52/* sdcardfs root inode number */
53#define SDCARDFS_ROOT_INO 1
54
55/* useful for tracking code reachability */
Daniel Rosenberg77ecf212017-03-16 17:46:13 -070056#define UDBG pr_default("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
Daniel Campello35c9e242015-07-20 16:23:50 -070057
58#define SDCARDFS_DIRENT_SIZE 256
59
60/* temporary static uid settings for development */
61#define AID_ROOT 0 /* uid for accessing /mnt/sdcard & extSdcard */
62#define AID_MEDIA_RW 1023 /* internal media storage write access */
63
64#define AID_SDCARD_RW 1015 /* external storage write access */
65#define AID_SDCARD_R 1028 /* external storage read access */
66#define AID_SDCARD_PICS 1033 /* external storage photos access */
67#define AID_SDCARD_AV 1034 /* external storage audio/video access */
68#define AID_SDCARD_ALL 1035 /* access all users external storage */
Daniel Rosenbergad905252017-01-25 13:48:45 -080069#define AID_MEDIA_OBB 1059 /* obb files */
70
71#define AID_SDCARD_IMAGE 1057
Daniel Campello35c9e242015-07-20 16:23:50 -070072
73#define AID_PACKAGE_INFO 1027
74
Daniel Rosenberg90219272016-10-26 20:27:20 -070075
76/*
77 * Permissions are handled by our permission function.
78 * We don't want anyone who happens to look at our inode value to prematurely
79 * block access, so store more permissive values. These are probably never
80 * used.
81 */
82#define fixup_tmp_permissions(x) \
Daniel Campello35c9e242015-07-20 16:23:50 -070083 do { \
Daniel Campellod1d080c2015-07-20 16:27:37 -070084 (x)->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(x)->d_uid); \
Daniel Rosenberg90219272016-10-26 20:27:20 -070085 (x)->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW); \
86 (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\
Daniel Campello35c9e242015-07-20 16:23:50 -070087 } while (0)
88
89/* OVERRIDE_CRED() and REVERT_CRED()
Daniel Rosenberg5e024f62017-03-16 17:42:58 -070090 * OVERRIDE_CRED()
91 * backup original task->cred
92 * and modifies task->cred->fsuid/fsgid to specified value.
Daniel Campello35c9e242015-07-20 16:23:50 -070093 * REVERT_CRED()
Daniel Rosenberg5e024f62017-03-16 17:42:58 -070094 * restore original task->cred->fsuid/fsgid.
Daniel Campello35c9e242015-07-20 16:23:50 -070095 * These two macro should be used in pair, and OVERRIDE_CRED() should be
96 * placed at the beginning of a function, right after variable declaration.
97 */
Daniel Rosenbergad905252017-01-25 13:48:45 -080098#define OVERRIDE_CRED(sdcardfs_sbi, saved_cred, info) \
99 do { \
100 saved_cred = override_fsids(sdcardfs_sbi, info); \
101 if (!saved_cred) \
102 return -ENOMEM; \
103 } while (0)
Daniel Campello35c9e242015-07-20 16:23:50 -0700104
Daniel Rosenbergad905252017-01-25 13:48:45 -0800105#define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred, info) \
106 do { \
107 saved_cred = override_fsids(sdcardfs_sbi, info); \
108 if (!saved_cred) \
109 return ERR_PTR(-ENOMEM); \
110 } while (0)
Daniel Campello35c9e242015-07-20 16:23:50 -0700111
112#define REVERT_CRED(saved_cred) revert_fsids(saved_cred)
113
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800114/* Android 5.0 support */
Daniel Campello35c9e242015-07-20 16:23:50 -0700115
116/* Permission mode for a specific node. Controls how file permissions
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700117 * are derived for children nodes.
118 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700119typedef enum {
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700120 /* Nothing special; this node should just inherit from its parent. */
121 PERM_INHERIT,
122 /* This node is one level above a normal root; used for legacy layouts
123 * which use the first level to represent user_id.
124 */
125 PERM_PRE_ROOT,
126 /* This node is "/" */
127 PERM_ROOT,
128 /* This node is "/Android" */
129 PERM_ANDROID,
130 /* This node is "/Android/data" */
131 PERM_ANDROID_DATA,
132 /* This node is "/Android/obb" */
133 PERM_ANDROID_OBB,
134 /* This node is "/Android/media" */
135 PERM_ANDROID_MEDIA,
136 /* This node is "/Android/[data|media|obb]/[package]" */
137 PERM_ANDROID_PACKAGE,
138 /* This node is "/Android/[data|media|obb]/[package]/cache" */
139 PERM_ANDROID_PACKAGE_CACHE,
Daniel Campello35c9e242015-07-20 16:23:50 -0700140} perm_t;
141
Daniel Campello35c9e242015-07-20 16:23:50 -0700142struct sdcardfs_sb_info;
143struct sdcardfs_mount_options;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800144struct sdcardfs_inode_info;
Daniel Campello35c9e242015-07-20 16:23:50 -0700145
146/* Do not directly use this function. Use OVERRIDE_CRED() instead. */
Daniel Rosenbergad905252017-01-25 13:48:45 -0800147const struct cred *override_fsids(struct sdcardfs_sb_info *sbi, struct sdcardfs_inode_info *info);
Daniel Campello35c9e242015-07-20 16:23:50 -0700148/* Do not directly use this function, use REVERT_CRED() instead. */
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700149void revert_fsids(const struct cred *old_cred);
Daniel Campello35c9e242015-07-20 16:23:50 -0700150
151/* operations vectors defined in specific files */
152extern const struct file_operations sdcardfs_main_fops;
153extern const struct file_operations sdcardfs_dir_fops;
154extern const struct inode_operations sdcardfs_main_iops;
155extern const struct inode_operations sdcardfs_dir_iops;
156extern const struct inode_operations sdcardfs_symlink_iops;
157extern const struct super_operations sdcardfs_sops;
158extern const struct dentry_operations sdcardfs_ci_dops;
159extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
160extern const struct vm_operations_struct sdcardfs_vm_ops;
161
162extern int sdcardfs_init_inode_cache(void);
163extern void sdcardfs_destroy_inode_cache(void);
164extern int sdcardfs_init_dentry_cache(void);
165extern void sdcardfs_destroy_dentry_cache(void);
166extern int new_dentry_private_data(struct dentry *dentry);
167extern void free_dentry_private_data(struct dentry *dentry);
168extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
Daniel Campellod1d080c2015-07-20 16:27:37 -0700169 unsigned int flags);
170extern struct inode *sdcardfs_iget(struct super_block *sb,
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800171 struct inode *lower_inode, userid_t id);
Daniel Campello35c9e242015-07-20 16:23:50 -0700172extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800173 struct path *lower_path, userid_t id);
Daniel Campello35c9e242015-07-20 16:23:50 -0700174
175/* file private data */
176struct sdcardfs_file_info {
177 struct file *lower_file;
178 const struct vm_operations_struct *lower_vm_ops;
179};
180
181/* sdcardfs inode data in memory */
182struct sdcardfs_inode_info {
183 struct inode *lower_inode;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800184 /* state derived based on current position in hierachy */
Daniel Campello35c9e242015-07-20 16:23:50 -0700185 perm_t perm;
186 userid_t userid;
187 uid_t d_uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800188 bool under_android;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800189 bool under_cache;
190 bool under_obb;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700191 /* top folder for ownership */
192 struct inode *top;
Daniel Campello35c9e242015-07-20 16:23:50 -0700193
194 struct inode vfs_inode;
195};
196
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800197
Daniel Campello35c9e242015-07-20 16:23:50 -0700198/* sdcardfs dentry data in memory */
199struct sdcardfs_dentry_info {
200 spinlock_t lock; /* protects lower_path */
201 struct path lower_path;
202 struct path orig_path;
203};
204
205struct sdcardfs_mount_options {
206 uid_t fs_low_uid;
207 gid_t fs_low_gid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800208 userid_t fs_user_id;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800209 bool multiuser;
Daniel Campello35c9e242015-07-20 16:23:50 -0700210 unsigned int reserved_mb;
211};
212
Daniel Rosenberg317e7702016-10-26 17:36:05 -0700213struct sdcardfs_vfsmount_options {
214 gid_t gid;
215 mode_t mask;
216};
217
218extern int parse_options_remount(struct super_block *sb, char *options, int silent,
219 struct sdcardfs_vfsmount_options *vfsopts);
220
Daniel Campello35c9e242015-07-20 16:23:50 -0700221/* sdcardfs super-block data in memory */
222struct sdcardfs_sb_info {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800223 struct super_block *sb;
Daniel Campello35c9e242015-07-20 16:23:50 -0700224 struct super_block *lower_sb;
225 /* derived perm policy : some of options have been added
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700226 * to sdcardfs_mount_options (Android 4.4 support)
227 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700228 struct sdcardfs_mount_options options;
229 spinlock_t lock; /* protects obbpath */
230 char *obbpath_s;
231 struct path obbpath;
232 void *pkgl_id;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800233 struct list_head list;
Daniel Campello35c9e242015-07-20 16:23:50 -0700234};
235
236/*
237 * inode to private data
238 *
239 * Since we use containers and the struct inode is _inside_ the
240 * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
241 * inode pointer), return a valid non-NULL pointer.
242 */
243static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
244{
245 return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
246}
247
248/* dentry to private data */
249#define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
250
251/* superblock to private data */
252#define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
253
254/* file to private Data */
255#define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
256
257/* file to lower file */
258static inline struct file *sdcardfs_lower_file(const struct file *f)
259{
260 return SDCARDFS_F(f)->lower_file;
261}
262
263static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
264{
265 SDCARDFS_F(f)->lower_file = val;
266}
267
268/* inode to lower inode. */
269static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
270{
271 return SDCARDFS_I(i)->lower_inode;
272}
273
274static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
275{
276 SDCARDFS_I(i)->lower_inode = val;
277}
278
279/* superblock to lower superblock */
280static inline struct super_block *sdcardfs_lower_super(
281 const struct super_block *sb)
282{
283 return SDCARDFS_SB(sb)->lower_sb;
284}
285
286static inline void sdcardfs_set_lower_super(struct super_block *sb,
287 struct super_block *val)
288{
289 SDCARDFS_SB(sb)->lower_sb = val;
290}
291
292/* path based (dentry/mnt) macros */
293static inline void pathcpy(struct path *dst, const struct path *src)
294{
295 dst->dentry = src->dentry;
296 dst->mnt = src->mnt;
297}
298
299/* sdcardfs_get_pname functions calls path_get()
300 * therefore, the caller must call "proper" path_put functions
301 */
302#define SDCARDFS_DENT_FUNC(pname) \
303static inline void sdcardfs_get_##pname(const struct dentry *dent, \
304 struct path *pname) \
305{ \
306 spin_lock(&SDCARDFS_D(dent)->lock); \
307 pathcpy(pname, &SDCARDFS_D(dent)->pname); \
308 path_get(pname); \
309 spin_unlock(&SDCARDFS_D(dent)->lock); \
310 return; \
311} \
312static inline void sdcardfs_put_##pname(const struct dentry *dent, \
313 struct path *pname) \
314{ \
315 path_put(pname); \
316 return; \
317} \
318static inline void sdcardfs_set_##pname(const struct dentry *dent, \
319 struct path *pname) \
320{ \
321 spin_lock(&SDCARDFS_D(dent)->lock); \
322 pathcpy(&SDCARDFS_D(dent)->pname, pname); \
323 spin_unlock(&SDCARDFS_D(dent)->lock); \
324 return; \
325} \
326static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
327{ \
328 spin_lock(&SDCARDFS_D(dent)->lock); \
329 SDCARDFS_D(dent)->pname.dentry = NULL; \
330 SDCARDFS_D(dent)->pname.mnt = NULL; \
331 spin_unlock(&SDCARDFS_D(dent)->lock); \
332 return; \
333} \
334static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
335{ \
336 struct path pname; \
337 spin_lock(&SDCARDFS_D(dent)->lock); \
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700338 if (SDCARDFS_D(dent)->pname.dentry) { \
Daniel Campello35c9e242015-07-20 16:23:50 -0700339 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
340 SDCARDFS_D(dent)->pname.dentry = NULL; \
341 SDCARDFS_D(dent)->pname.mnt = NULL; \
342 spin_unlock(&SDCARDFS_D(dent)->lock); \
343 path_put(&pname); \
344 } else \
345 spin_unlock(&SDCARDFS_D(dent)->lock); \
346 return; \
347}
348
349SDCARDFS_DENT_FUNC(lower_path)
350SDCARDFS_DENT_FUNC(orig_path)
351
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800352static inline bool sbinfo_has_sdcard_magic(struct sdcardfs_sb_info *sbinfo)
353{
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700354 return sbinfo && sbinfo->sb && sbinfo->sb->s_magic == SDCARDFS_SUPER_MAGIC;
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800355}
356
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700357/* grab a refererence if we aren't linking to ourself */
358static inline void set_top(struct sdcardfs_inode_info *info, struct inode *top)
359{
360 struct inode *old_top = NULL;
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700361
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700362 BUG_ON(IS_ERR_OR_NULL(top));
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700363 if (info->top && info->top != &info->vfs_inode)
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700364 old_top = info->top;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700365 if (top != &info->vfs_inode)
366 igrab(top);
367 info->top = top;
368 iput(old_top);
369}
370
371static inline struct inode *grab_top(struct sdcardfs_inode_info *info)
372{
373 struct inode *top = info->top;
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700374
375 if (top)
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700376 return igrab(top);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700377 else
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700378 return NULL;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700379}
380
381static inline void release_top(struct sdcardfs_inode_info *info)
382{
383 iput(info->top);
384}
385
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700386static inline int get_gid(struct vfsmount *mnt, struct sdcardfs_inode_info *info)
387{
Daniel Rosenberg90219272016-10-26 20:27:20 -0700388 struct sdcardfs_vfsmount_options *opts = mnt->data;
389
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700390 if (opts->gid == AID_SDCARD_RW)
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800391 /* As an optimization, certain trusted system components only run
392 * as owner but operate across all users. Since we're now handing
393 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
394 * the user boundary enforcement for the default view. The UIDs
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700395 * assigned to app directories are still multiuser aware.
396 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800397 return AID_SDCARD_RW;
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700398 else
Daniel Rosenberg90219272016-10-26 20:27:20 -0700399 return multiuser_get_uid(info->userid, opts->gid);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800400}
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700401
402static inline int get_mode(struct vfsmount *mnt, struct sdcardfs_inode_info *info)
403{
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800404 int owner_mode;
405 int filtered_mode;
Daniel Rosenberg90219272016-10-26 20:27:20 -0700406 struct sdcardfs_vfsmount_options *opts = mnt->data;
407 int visible_mode = 0775 & ~opts->mask;
408
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800409
410 if (info->perm == PERM_PRE_ROOT) {
411 /* Top of multi-user view should always be visible to ensure
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700412 * secondary users can traverse inside.
413 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800414 visible_mode = 0711;
415 } else if (info->under_android) {
416 /* Block "other" access to Android directories, since only apps
417 * belonging to a specific user should be in there; we still
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700418 * leave +x open for the default view.
419 */
420 if (opts->gid == AID_SDCARD_RW)
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800421 visible_mode = visible_mode & ~0006;
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700422 else
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800423 visible_mode = visible_mode & ~0007;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800424 }
425 owner_mode = info->lower_inode->i_mode & 0700;
426 filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
427 return filtered_mode;
428}
429
Daniel Campello35c9e242015-07-20 16:23:50 -0700430static inline int has_graft_path(const struct dentry *dent)
431{
432 int ret = 0;
433
434 spin_lock(&SDCARDFS_D(dent)->lock);
435 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
436 ret = 1;
437 spin_unlock(&SDCARDFS_D(dent)->lock);
438
439 return ret;
440}
441
442static inline void sdcardfs_get_real_lower(const struct dentry *dent,
443 struct path *real_lower)
444{
445 /* in case of a local obb dentry
446 * the orig_path should be returned
447 */
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700448 if (has_graft_path(dent))
Daniel Campello35c9e242015-07-20 16:23:50 -0700449 sdcardfs_get_orig_path(dent, real_lower);
450 else
451 sdcardfs_get_lower_path(dent, real_lower);
452}
453
454static inline void sdcardfs_put_real_lower(const struct dentry *dent,
455 struct path *real_lower)
456{
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700457 if (has_graft_path(dent))
Daniel Campello35c9e242015-07-20 16:23:50 -0700458 sdcardfs_put_orig_path(dent, real_lower);
459 else
460 sdcardfs_put_lower_path(dent, real_lower);
461}
462
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800463extern struct mutex sdcardfs_super_list_lock;
464extern struct list_head sdcardfs_super_list;
465
Daniel Campello35c9e242015-07-20 16:23:50 -0700466/* for packagelist.c */
Daniel Rosenbergfbd34b62016-05-10 13:42:43 -0700467extern appid_t get_appid(const char *app_name);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800468extern appid_t get_ext_gid(const char *app_name);
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800469extern appid_t is_excluded(const char *app_name, userid_t userid);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800470extern int check_caller_access_to_name(struct inode *parent_node, const struct qstr *name);
Daniel Campello35c9e242015-07-20 16:23:50 -0700471extern int open_flags_to_access_mode(int open_flags);
Daniel Campello35c9e242015-07-20 16:23:50 -0700472extern int packagelist_init(void);
473extern void packagelist_exit(void);
474
475/* for derived_perm.c */
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800476#define BY_NAME (1 << 0)
477#define BY_USERID (1 << 1)
478struct limit_search {
479 unsigned int flags;
Daniel Rosenberg721274a2017-03-08 17:20:02 -0800480 struct qstr name;
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800481 userid_t userid;
482};
483
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700484extern void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
485 uid_t uid, bool under_android, struct inode *top);
Daniel Campello35c9e242015-07-20 16:23:50 -0700486extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800487extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name);
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800488extern void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800489
490extern void update_derived_permission_lock(struct dentry *dentry);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800491void fixup_lower_ownership(struct dentry *dentry, const char *name);
Daniel Campello35c9e242015-07-20 16:23:50 -0700492extern int need_graft_path(struct dentry *dentry);
493extern int is_base_obbpath(struct dentry *dentry);
494extern int is_obbpath_invalid(struct dentry *dentry);
495extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
496
497/* locking helpers */
498static inline struct dentry *lock_parent(struct dentry *dentry)
499{
500 struct dentry *dir = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700501
Amit Pundirb47e1102016-06-01 21:53:20 +0530502 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
Daniel Campello35c9e242015-07-20 16:23:50 -0700503 return dir;
504}
505
506static inline void unlock_dir(struct dentry *dir)
507{
Amit Pundirb47e1102016-06-01 21:53:20 +0530508 inode_unlock(d_inode(dir));
Daniel Campello35c9e242015-07-20 16:23:50 -0700509 dput(dir);
510}
511
512static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
513{
514 int err;
515 struct dentry *dent;
516 struct iattr attrs;
Daniel Campellod1d080c2015-07-20 16:27:37 -0700517 struct path parent;
Daniel Campello35c9e242015-07-20 16:23:50 -0700518
Daniel Campellod1d080c2015-07-20 16:27:37 -0700519 dent = kern_path_locked(path_s, &parent);
Daniel Campello35c9e242015-07-20 16:23:50 -0700520 if (IS_ERR(dent)) {
521 err = PTR_ERR(dent);
522 if (err == -EEXIST)
523 err = 0;
524 goto out_unlock;
525 }
526
Daniel Rosenberg1844d9e2016-10-26 16:48:45 -0700527 err = vfs_mkdir2(parent.mnt, d_inode(parent.dentry), dent, mode);
Daniel Campello35c9e242015-07-20 16:23:50 -0700528 if (err) {
529 if (err == -EEXIST)
530 err = 0;
531 goto out_dput;
532 }
533
Daniel Campellod1d080c2015-07-20 16:27:37 -0700534 attrs.ia_uid = make_kuid(&init_user_ns, uid);
535 attrs.ia_gid = make_kgid(&init_user_ns, gid);
Daniel Campello35c9e242015-07-20 16:23:50 -0700536 attrs.ia_valid = ATTR_UID | ATTR_GID;
Amit Pundirb47e1102016-06-01 21:53:20 +0530537 inode_lock(d_inode(dent));
Daniel Rosenberg1844d9e2016-10-26 16:48:45 -0700538 notify_change2(parent.mnt, dent, &attrs, NULL);
Amit Pundirb47e1102016-06-01 21:53:20 +0530539 inode_unlock(d_inode(dent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700540
541out_dput:
542 dput(dent);
543
544out_unlock:
545 /* parent dentry locked by lookup_create */
Amit Pundirb47e1102016-06-01 21:53:20 +0530546 inode_unlock(d_inode(parent.dentry));
Daniel Campellod1d080c2015-07-20 16:27:37 -0700547 path_put(&parent);
Daniel Campello35c9e242015-07-20 16:23:50 -0700548 return err;
549}
550
551/*
552 * Return 1, if a disk has enough free space, otherwise 0.
553 * We assume that any files can not be overwritten.
554 */
555static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
556{
557 int err;
558 struct path lower_path;
559 struct kstatfs statfs;
560 u64 avail;
561 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
562
563 if (sbi->options.reserved_mb) {
564 /* Get fs stat of lower filesystem. */
565 sdcardfs_get_lower_path(dentry, &lower_path);
566 err = vfs_statfs(&lower_path, &statfs);
567 sdcardfs_put_lower_path(dentry, &lower_path);
568
569 if (unlikely(err))
570 return 0;
571
572 /* Invalid statfs informations. */
573 if (unlikely(statfs.f_bsize == 0))
574 return 0;
575
576 /* if you are checking directory, set size to f_bsize. */
577 if (unlikely(dir))
578 size = statfs.f_bsize;
579
580 /* available size */
581 avail = statfs.f_bavail * statfs.f_bsize;
582
583 /* not enough space */
584 if ((u64)size > avail)
585 return 0;
586
587 /* enough space */
588 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
589 return 1;
590
591 return 0;
592 } else
593 return 1;
594}
595
Daniel Rosenberg90219272016-10-26 20:27:20 -0700596/*
597 * Copies attrs and maintains sdcardfs managed attrs
598 * Since our permission check handles all special permissions, set those to be open
599 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800600static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
601{
Daniel Rosenberg90219272016-10-26 20:27:20 -0700602 dest->i_mode = (src->i_mode & S_IFMT) | S_IRWXU | S_IRWXG |
603 S_IROTH | S_IXOTH; /* 0775 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800604 dest->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(dest)->d_uid);
Daniel Rosenberg90219272016-10-26 20:27:20 -0700605 dest->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800606 dest->i_rdev = src->i_rdev;
607 dest->i_atime = src->i_atime;
608 dest->i_mtime = src->i_mtime;
609 dest->i_ctime = src->i_ctime;
610 dest->i_blkbits = src->i_blkbits;
611 dest->i_flags = src->i_flags;
612 set_nlink(dest, src->i_nlink);
613}
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800614
615static inline bool str_case_eq(const char *s1, const char *s2)
616{
617 return !strcasecmp(s1, s2);
618}
619
Daniel Rosenberg721274a2017-03-08 17:20:02 -0800620static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len)
621{
622 return !strncasecmp(s1, s2, len);
623}
624
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800625static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2)
626{
627 return q1->len == q2->len && str_case_eq(q1->name, q2->name);
628}
629
630#define QSTR_LITERAL(string) QSTR_INIT(string, sizeof(string)-1)
631
Daniel Campello35c9e242015-07-20 16:23:50 -0700632#endif /* not _SDCARDFS_H_ */