ANDROID: sdcardfs: Added top to sdcardfs_inode_info
[firefly-linux-kernel-4.4.55.git] / fs / sdcardfs / sdcardfs.h
1 /*
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>
32 #include <linux/mm.h>
33 #include <linux/mount.h>
34 #include <linux/namei.h>
35 #include <linux/seq_file.h>
36 #include <linux/statfs.h>
37 #include <linux/fs_stack.h>
38 #include <linux/magic.h>
39 #include <linux/uaccess.h>
40 #include <linux/slab.h>
41 #include <linux/sched.h>
42 #include <linux/types.h>
43 #include <linux/security.h>
44 #include <linux/string.h>
45 #include <linux/list.h>
46 #include "multiuser.h"
47
48 /* the file system name */
49 #define SDCARDFS_NAME "sdcardfs"
50
51 /* sdcardfs root inode number */
52 #define SDCARDFS_ROOT_INO     1
53
54 /* useful for tracking code reachability */
55 #define UDBG printk(KERN_DEFAULT "DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
56
57 #define SDCARDFS_DIRENT_SIZE 256
58
59 /* temporary static uid settings for development */
60 #define AID_ROOT             0  /* uid for accessing /mnt/sdcard & extSdcard */
61 #define AID_MEDIA_RW      1023  /* internal media storage write access */
62
63 #define AID_SDCARD_RW     1015  /* external storage write access */
64 #define AID_SDCARD_R      1028  /* external storage read access */
65 #define AID_SDCARD_PICS   1033  /* external storage photos access */
66 #define AID_SDCARD_AV     1034  /* external storage audio/video access */
67 #define AID_SDCARD_ALL    1035  /* access all users external storage */
68
69 #define AID_PACKAGE_INFO  1027
70
71 #define fix_derived_permission(x)       \
72         do {                                            \
73                 (x)->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(x)->d_uid);    \
74                 (x)->i_gid = make_kgid(&init_user_ns, get_gid(SDCARDFS_I(x)));  \
75                 (x)->i_mode = ((x)->i_mode & S_IFMT) | get_mode(SDCARDFS_I(x));\
76         } while (0)
77
78
79 /* OVERRIDE_CRED() and REVERT_CRED()
80  *      OVERRID_CRED()
81  *              backup original task->cred
82  *              and modifies task->cred->fsuid/fsgid to specified value.
83  *      REVERT_CRED()
84  *              restore original task->cred->fsuid/fsgid.
85  * These two macro should be used in pair, and OVERRIDE_CRED() should be
86  * placed at the beginning of a function, right after variable declaration.
87  */
88 #define OVERRIDE_CRED(sdcardfs_sbi, saved_cred)         \
89         saved_cred = override_fsids(sdcardfs_sbi);      \
90         if (!saved_cred) { return -ENOMEM; }
91
92 #define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred)     \
93         saved_cred = override_fsids(sdcardfs_sbi);      \
94         if (!saved_cred) { return ERR_PTR(-ENOMEM); }
95
96 #define REVERT_CRED(saved_cred) revert_fsids(saved_cred)
97
98 #define DEBUG_CRED()            \
99         printk("KAKJAGI: %s:%d fsuid %d fsgid %d\n",    \
100                 __FUNCTION__, __LINE__,                 \
101                 (int)current->cred->fsuid,              \
102                 (int)current->cred->fsgid);
103
104 /* Android 5.0 support */
105
106 /* Permission mode for a specific node. Controls how file permissions
107  * are derived for children nodes. */
108 typedef enum {
109     /* Nothing special; this node should just inherit from its parent. */
110     PERM_INHERIT,
111     /* This node is one level above a normal root; used for legacy layouts
112      * which use the first level to represent user_id. */
113     PERM_PRE_ROOT,
114     /* This node is "/" */
115     PERM_ROOT,
116     /* This node is "/Android" */
117     PERM_ANDROID,
118     /* This node is "/Android/data" */
119     PERM_ANDROID_DATA,
120     /* This node is "/Android/obb" */
121     PERM_ANDROID_OBB,
122     /* This node is "/Android/media" */
123     PERM_ANDROID_MEDIA,
124 } perm_t;
125
126 struct sdcardfs_sb_info;
127 struct sdcardfs_mount_options;
128
129 /* Do not directly use this function. Use OVERRIDE_CRED() instead. */
130 const struct cred * override_fsids(struct sdcardfs_sb_info* sbi);
131 /* Do not directly use this function, use REVERT_CRED() instead. */
132 void revert_fsids(const struct cred * old_cred);
133
134 /* operations vectors defined in specific files */
135 extern const struct file_operations sdcardfs_main_fops;
136 extern const struct file_operations sdcardfs_dir_fops;
137 extern const struct inode_operations sdcardfs_main_iops;
138 extern const struct inode_operations sdcardfs_dir_iops;
139 extern const struct inode_operations sdcardfs_symlink_iops;
140 extern const struct super_operations sdcardfs_sops;
141 extern const struct dentry_operations sdcardfs_ci_dops;
142 extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
143 extern const struct vm_operations_struct sdcardfs_vm_ops;
144
145 extern int sdcardfs_init_inode_cache(void);
146 extern void sdcardfs_destroy_inode_cache(void);
147 extern int sdcardfs_init_dentry_cache(void);
148 extern void sdcardfs_destroy_dentry_cache(void);
149 extern int new_dentry_private_data(struct dentry *dentry);
150 extern void free_dentry_private_data(struct dentry *dentry);
151 extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
152                                 unsigned int flags);
153 extern struct inode *sdcardfs_iget(struct super_block *sb,
154                                  struct inode *lower_inode, userid_t id);
155 extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
156                             struct path *lower_path, userid_t id);
157
158 /* file private data */
159 struct sdcardfs_file_info {
160         struct file *lower_file;
161         const struct vm_operations_struct *lower_vm_ops;
162 };
163
164 /* sdcardfs inode data in memory */
165 struct sdcardfs_inode_info {
166         struct inode *lower_inode;
167         /* state derived based on current position in hierachy */
168         perm_t perm;
169         userid_t userid;
170         uid_t d_uid;
171         bool under_android;
172         /* top folder for ownership */
173         struct inode *top;
174
175         struct inode vfs_inode;
176 };
177
178
179 /* sdcardfs dentry data in memory */
180 struct sdcardfs_dentry_info {
181         spinlock_t lock;        /* protects lower_path */
182         struct path lower_path;
183         struct path orig_path;
184 };
185
186 struct sdcardfs_mount_options {
187         uid_t fs_low_uid;
188         gid_t fs_low_gid;
189         userid_t fs_user_id;
190         gid_t gid;
191         mode_t mask;
192         bool multiuser;
193         unsigned int reserved_mb;
194 };
195
196 /* sdcardfs super-block data in memory */
197 struct sdcardfs_sb_info {
198         struct super_block *sb;
199         struct super_block *lower_sb;
200         /* derived perm policy : some of options have been added
201          * to sdcardfs_mount_options (Android 4.4 support) */
202         struct sdcardfs_mount_options options;
203         spinlock_t lock;        /* protects obbpath */
204         char *obbpath_s;
205         struct path obbpath;
206         void *pkgl_id;
207         struct list_head list;
208 };
209
210 /*
211  * inode to private data
212  *
213  * Since we use containers and the struct inode is _inside_ the
214  * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
215  * inode pointer), return a valid non-NULL pointer.
216  */
217 static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
218 {
219         return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
220 }
221
222 /* dentry to private data */
223 #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
224
225 /* superblock to private data */
226 #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
227
228 /* file to private Data */
229 #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
230
231 /* file to lower file */
232 static inline struct file *sdcardfs_lower_file(const struct file *f)
233 {
234         return SDCARDFS_F(f)->lower_file;
235 }
236
237 static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
238 {
239         SDCARDFS_F(f)->lower_file = val;
240 }
241
242 /* inode to lower inode. */
243 static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
244 {
245         return SDCARDFS_I(i)->lower_inode;
246 }
247
248 static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
249 {
250         SDCARDFS_I(i)->lower_inode = val;
251 }
252
253 /* superblock to lower superblock */
254 static inline struct super_block *sdcardfs_lower_super(
255         const struct super_block *sb)
256 {
257         return SDCARDFS_SB(sb)->lower_sb;
258 }
259
260 static inline void sdcardfs_set_lower_super(struct super_block *sb,
261                                           struct super_block *val)
262 {
263         SDCARDFS_SB(sb)->lower_sb = val;
264 }
265
266 /* path based (dentry/mnt) macros */
267 static inline void pathcpy(struct path *dst, const struct path *src)
268 {
269         dst->dentry = src->dentry;
270         dst->mnt = src->mnt;
271 }
272
273 /* sdcardfs_get_pname functions calls path_get()
274  * therefore, the caller must call "proper" path_put functions
275  */
276 #define SDCARDFS_DENT_FUNC(pname) \
277 static inline void sdcardfs_get_##pname(const struct dentry *dent, \
278                                         struct path *pname) \
279 { \
280         spin_lock(&SDCARDFS_D(dent)->lock); \
281         pathcpy(pname, &SDCARDFS_D(dent)->pname); \
282         path_get(pname); \
283         spin_unlock(&SDCARDFS_D(dent)->lock); \
284         return; \
285 } \
286 static inline void sdcardfs_put_##pname(const struct dentry *dent, \
287                                         struct path *pname) \
288 { \
289         path_put(pname); \
290         return; \
291 } \
292 static inline void sdcardfs_set_##pname(const struct dentry *dent, \
293                                         struct path *pname) \
294 { \
295         spin_lock(&SDCARDFS_D(dent)->lock); \
296         pathcpy(&SDCARDFS_D(dent)->pname, pname); \
297         spin_unlock(&SDCARDFS_D(dent)->lock); \
298         return; \
299 } \
300 static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
301 { \
302         spin_lock(&SDCARDFS_D(dent)->lock); \
303         SDCARDFS_D(dent)->pname.dentry = NULL; \
304         SDCARDFS_D(dent)->pname.mnt = NULL; \
305         spin_unlock(&SDCARDFS_D(dent)->lock); \
306         return; \
307 } \
308 static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
309 { \
310         struct path pname; \
311         spin_lock(&SDCARDFS_D(dent)->lock); \
312         if(SDCARDFS_D(dent)->pname.dentry) { \
313                 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
314                 SDCARDFS_D(dent)->pname.dentry = NULL; \
315                 SDCARDFS_D(dent)->pname.mnt = NULL; \
316                 spin_unlock(&SDCARDFS_D(dent)->lock); \
317                 path_put(&pname); \
318         } else \
319                 spin_unlock(&SDCARDFS_D(dent)->lock); \
320         return; \
321 }
322
323 SDCARDFS_DENT_FUNC(lower_path)
324 SDCARDFS_DENT_FUNC(orig_path)
325
326 /* grab a refererence if we aren't linking to ourself */
327 static inline void set_top(struct sdcardfs_inode_info *info, struct inode *top)
328 {
329         struct inode *old_top = NULL;
330         BUG_ON(IS_ERR_OR_NULL(top));
331         if (info->top && info->top != &info->vfs_inode) {
332                 old_top = info->top;
333         }
334         if (top != &info->vfs_inode)
335                 igrab(top);
336         info->top = top;
337         iput(old_top);
338 }
339
340 static inline struct inode *grab_top(struct sdcardfs_inode_info *info)
341 {
342         struct inode *top = info->top;
343         if (top) {
344                 return igrab(top);
345         } else {
346                 return NULL;
347         }
348 }
349
350 static inline void release_top(struct sdcardfs_inode_info *info)
351 {
352         iput(info->top);
353 }
354
355 static inline int get_gid(struct sdcardfs_inode_info *info) {
356         struct sdcardfs_sb_info *sb_info = SDCARDFS_SB(info->vfs_inode.i_sb);
357         if (sb_info->options.gid == AID_SDCARD_RW) {
358                 /* As an optimization, certain trusted system components only run
359                  * as owner but operate across all users. Since we're now handing
360                  * out the sdcard_rw GID only to trusted apps, we're okay relaxing
361                  * the user boundary enforcement for the default view. The UIDs
362                  * assigned to app directories are still multiuser aware. */
363                 return AID_SDCARD_RW;
364         } else {
365                 return multiuser_get_uid(info->userid, sb_info->options.gid);
366         }
367 }
368 static inline int get_mode(struct sdcardfs_inode_info *info) {
369         int owner_mode;
370         int filtered_mode;
371         struct sdcardfs_sb_info * sb_info = SDCARDFS_SB(info->vfs_inode.i_sb);
372         int visible_mode = 0775 & ~sb_info->options.mask;
373
374         if (info->perm == PERM_PRE_ROOT) {
375                 /* Top of multi-user view should always be visible to ensure
376                 * secondary users can traverse inside. */
377                 visible_mode = 0711;
378         } else if (info->under_android) {
379                 /* Block "other" access to Android directories, since only apps
380                 * belonging to a specific user should be in there; we still
381                 * leave +x open for the default view. */
382                 if (sb_info->options.gid == AID_SDCARD_RW) {
383                         visible_mode = visible_mode & ~0006;
384                 } else {
385                         visible_mode = visible_mode & ~0007;
386                 }
387         }
388         owner_mode = info->lower_inode->i_mode & 0700;
389         filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
390         return filtered_mode;
391 }
392
393 static inline int has_graft_path(const struct dentry *dent)
394 {
395         int ret = 0;
396
397         spin_lock(&SDCARDFS_D(dent)->lock);
398         if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
399                 ret = 1;
400         spin_unlock(&SDCARDFS_D(dent)->lock);
401
402         return ret;
403 }
404
405 static inline void sdcardfs_get_real_lower(const struct dentry *dent,
406                                                 struct path *real_lower)
407 {
408         /* in case of a local obb dentry
409          * the orig_path should be returned
410          */
411         if(has_graft_path(dent))
412                 sdcardfs_get_orig_path(dent, real_lower);
413         else
414                 sdcardfs_get_lower_path(dent, real_lower);
415 }
416
417 static inline void sdcardfs_put_real_lower(const struct dentry *dent,
418                                                 struct path *real_lower)
419 {
420         if(has_graft_path(dent))
421                 sdcardfs_put_orig_path(dent, real_lower);
422         else
423                 sdcardfs_put_lower_path(dent, real_lower);
424 }
425
426 extern struct mutex sdcardfs_super_list_lock;
427 extern struct list_head sdcardfs_super_list;
428
429 /* for packagelist.c */
430 extern appid_t get_appid(const char *app_name);
431 extern int check_caller_access_to_name(struct inode *parent_node, const char* name);
432 extern int open_flags_to_access_mode(int open_flags);
433 extern int packagelist_init(void);
434 extern void packagelist_exit(void);
435
436 /* for derived_perm.c */
437 extern void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
438                         uid_t uid, bool under_android, struct inode *top);
439 extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
440 extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry);
441 extern void fixup_top_recursive(struct dentry *parent);
442 extern void fixup_perms_recursive(struct dentry *dentry, const char *name, size_t len);
443
444 extern void update_derived_permission_lock(struct dentry *dentry);
445 extern int need_graft_path(struct dentry *dentry);
446 extern int is_base_obbpath(struct dentry *dentry);
447 extern int is_obbpath_invalid(struct dentry *dentry);
448 extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
449
450 /* locking helpers */
451 static inline struct dentry *lock_parent(struct dentry *dentry)
452 {
453         struct dentry *dir = dget_parent(dentry);
454         mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT);
455         return dir;
456 }
457
458 static inline void unlock_dir(struct dentry *dir)
459 {
460         mutex_unlock(&d_inode(dir)->i_mutex);
461         dput(dir);
462 }
463
464 static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
465 {
466         int err;
467         struct dentry *dent;
468         struct iattr attrs;
469         struct path parent;
470
471         dent = kern_path_locked(path_s, &parent);
472         if (IS_ERR(dent)) {
473                 err = PTR_ERR(dent);
474                 if (err == -EEXIST)
475                         err = 0;
476                 goto out_unlock;
477         }
478
479         err = vfs_mkdir(d_inode(parent.dentry), dent, mode);
480         if (err) {
481                 if (err == -EEXIST)
482                         err = 0;
483                 goto out_dput;
484         }
485
486         attrs.ia_uid = make_kuid(&init_user_ns, uid);
487         attrs.ia_gid = make_kgid(&init_user_ns, gid);
488         attrs.ia_valid = ATTR_UID | ATTR_GID;
489         mutex_lock(&d_inode(dent)->i_mutex);
490         notify_change(dent, &attrs, NULL);
491         mutex_unlock(&d_inode(dent)->i_mutex);
492
493 out_dput:
494         dput(dent);
495
496 out_unlock:
497         /* parent dentry locked by lookup_create */
498         mutex_unlock(&d_inode(parent.dentry)->i_mutex);
499         path_put(&parent);
500         return err;
501 }
502
503 /*
504  * Return 1, if a disk has enough free space, otherwise 0.
505  * We assume that any files can not be overwritten.
506  */
507 static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
508 {
509         int err;
510         struct path lower_path;
511         struct kstatfs statfs;
512         u64 avail;
513         struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
514
515         if (sbi->options.reserved_mb) {
516                 /* Get fs stat of lower filesystem. */
517                 sdcardfs_get_lower_path(dentry, &lower_path);
518                 err = vfs_statfs(&lower_path, &statfs);
519                 sdcardfs_put_lower_path(dentry, &lower_path);
520
521                 if (unlikely(err))
522                         return 0;
523
524                 /* Invalid statfs informations. */
525                 if (unlikely(statfs.f_bsize == 0))
526                         return 0;
527
528                 /* if you are checking directory, set size to f_bsize. */
529                 if (unlikely(dir))
530                         size = statfs.f_bsize;
531
532                 /* available size */
533                 avail = statfs.f_bavail * statfs.f_bsize;
534
535                 /* not enough space */
536                 if ((u64)size > avail)
537                         return 0;
538
539                 /* enough space */
540                 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
541                         return 1;
542
543                 return 0;
544         } else
545                 return 1;
546 }
547
548 /* Copies attrs and maintains sdcardfs managed attrs */
549 static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
550 {
551         dest->i_mode = (src->i_mode  & S_IFMT) | get_mode(SDCARDFS_I(dest));
552         dest->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(dest)->d_uid);
553         dest->i_gid = make_kgid(&init_user_ns, get_gid(SDCARDFS_I(dest)));
554         dest->i_rdev = src->i_rdev;
555         dest->i_atime = src->i_atime;
556         dest->i_mtime = src->i_mtime;
557         dest->i_ctime = src->i_ctime;
558         dest->i_blkbits = src->i_blkbits;
559         dest->i_flags = src->i_flags;
560         set_nlink(dest, src->i_nlink);
561 }
562 #endif  /* not _SDCARDFS_H_ */