Port of sdcardfs to 4.4
[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 "multiuser.h"
46
47 /* the file system name */
48 #define SDCARDFS_NAME "sdcardfs"
49
50 /* sdcardfs root inode number */
51 #define SDCARDFS_ROOT_INO     1
52
53 /* useful for tracking code reachability */
54 #define UDBG printk(KERN_DEFAULT "DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
55
56 #define SDCARDFS_DIRENT_SIZE 256
57
58 /* temporary static uid settings for development */
59 #define AID_ROOT             0  /* uid for accessing /mnt/sdcard & extSdcard */
60 #define AID_MEDIA_RW      1023  /* internal media storage write access */
61
62 #define AID_SDCARD_RW     1015  /* external storage write access */
63 #define AID_SDCARD_R      1028  /* external storage read access */
64 #define AID_SDCARD_PICS   1033  /* external storage photos access */
65 #define AID_SDCARD_AV     1034  /* external storage audio/video access */
66 #define AID_SDCARD_ALL    1035  /* access all users external storage */
67
68 #define AID_PACKAGE_INFO  1027
69
70 #define fix_derived_permission(x)       \
71         do {                                            \
72                 (x)->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(x)->d_uid);    \
73                 (x)->i_gid = make_kgid(&init_user_ns, SDCARDFS_I(x)->d_gid);    \
74                 (x)->i_mode = ((x)->i_mode & S_IFMT) | SDCARDFS_I(x)->d_mode;\
75         } while (0)
76
77 /* OVERRIDE_CRED() and REVERT_CRED()
78  *      OVERRID_CRED()
79  *              backup original task->cred
80  *              and modifies task->cred->fsuid/fsgid to specified value.
81  *      REVERT_CRED()
82  *              restore original task->cred->fsuid/fsgid.
83  * These two macro should be used in pair, and OVERRIDE_CRED() should be
84  * placed at the beginning of a function, right after variable declaration.
85  */
86 #define OVERRIDE_CRED(sdcardfs_sbi, saved_cred)         \
87         saved_cred = override_fsids(sdcardfs_sbi);      \
88         if (!saved_cred) { return -ENOMEM; }
89
90 #define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred)     \
91         saved_cred = override_fsids(sdcardfs_sbi);      \
92         if (!saved_cred) { return ERR_PTR(-ENOMEM); }
93
94 #define REVERT_CRED(saved_cred) revert_fsids(saved_cred)
95
96 #define DEBUG_CRED()            \
97         printk("KAKJAGI: %s:%d fsuid %d fsgid %d\n",    \
98                 __FUNCTION__, __LINE__,                 \
99                 (int)current->cred->fsuid,              \
100                 (int)current->cred->fsgid);
101
102 /* Android 4.4 support */
103
104 /* Permission mode for a specific node. Controls how file permissions
105  * are derived for children nodes. */
106 typedef enum {
107         /* Nothing special; this node should just inherit from its parent. */
108         PERM_INHERIT,
109         /* This node is one level above a normal root; used for legacy layouts
110          * which use the first level to represent user_id. */
111         PERM_LEGACY_PRE_ROOT,
112         /* This node is "/" */
113         PERM_ROOT,
114         /* This node is "/Android" */
115         PERM_ANDROID,
116         /* This node is "/Android/data" */
117         PERM_ANDROID_DATA,
118         /* This node is "/Android/obb" */
119         PERM_ANDROID_OBB,
120         /* This node is "/Android/user" */
121         PERM_ANDROID_USER,
122 } perm_t;
123
124 /* Permissions structure to derive */
125 typedef enum {
126         DERIVE_NONE,
127         DERIVE_LEGACY,
128         DERIVE_UNIFIED,
129 } derive_t;
130
131 typedef enum {
132         LOWER_FS_EXT4,
133         LOWER_FS_FAT,
134 } lower_fs_t;
135
136 struct sdcardfs_sb_info;
137 struct sdcardfs_mount_options;
138
139 /* Do not directly use this function. Use OVERRIDE_CRED() instead. */
140 const struct cred * override_fsids(struct sdcardfs_sb_info* sbi);
141 /* Do not directly use this function, use REVERT_CRED() instead. */
142 void revert_fsids(const struct cred * old_cred);
143
144 /* operations vectors defined in specific files */
145 extern const struct file_operations sdcardfs_main_fops;
146 extern const struct file_operations sdcardfs_dir_fops;
147 extern const struct inode_operations sdcardfs_main_iops;
148 extern const struct inode_operations sdcardfs_dir_iops;
149 extern const struct inode_operations sdcardfs_symlink_iops;
150 extern const struct super_operations sdcardfs_sops;
151 extern const struct dentry_operations sdcardfs_ci_dops;
152 extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
153 extern const struct vm_operations_struct sdcardfs_vm_ops;
154
155 extern int sdcardfs_init_inode_cache(void);
156 extern void sdcardfs_destroy_inode_cache(void);
157 extern int sdcardfs_init_dentry_cache(void);
158 extern void sdcardfs_destroy_dentry_cache(void);
159 extern int new_dentry_private_data(struct dentry *dentry);
160 extern void free_dentry_private_data(struct dentry *dentry);
161 extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
162                                 unsigned int flags);
163 extern struct inode *sdcardfs_iget(struct super_block *sb,
164                                  struct inode *lower_inode);
165 extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
166                             struct path *lower_path);
167
168 /* file private data */
169 struct sdcardfs_file_info {
170         struct file *lower_file;
171         const struct vm_operations_struct *lower_vm_ops;
172 };
173
174 /* sdcardfs inode data in memory */
175 struct sdcardfs_inode_info {
176         struct inode *lower_inode;
177         /* state derived based on current position in hierachy
178          * caution: d_mode does not include file types
179          */
180         perm_t perm;
181         userid_t userid;
182         uid_t d_uid;
183         gid_t d_gid;
184         mode_t d_mode;
185
186         struct inode vfs_inode;
187 };
188
189 /* sdcardfs dentry data in memory */
190 struct sdcardfs_dentry_info {
191         spinlock_t lock;        /* protects lower_path */
192         struct path lower_path;
193         struct path orig_path;
194 };
195
196 struct sdcardfs_mount_options {
197         uid_t fs_low_uid;
198         gid_t fs_low_gid;
199         gid_t write_gid;
200         int split_perms;
201         derive_t derive;
202         lower_fs_t lower_fs;
203         unsigned int reserved_mb;
204 };
205
206 /* sdcardfs super-block data in memory */
207 struct sdcardfs_sb_info {
208         struct super_block *lower_sb;
209         /* derived perm policy : some of options have been added
210          * to sdcardfs_mount_options (Android 4.4 support) */
211         struct sdcardfs_mount_options options;
212         spinlock_t lock;        /* protects obbpath */
213         char *obbpath_s;
214         struct path obbpath;
215         void *pkgl_id;
216 };
217
218 /*
219  * inode to private data
220  *
221  * Since we use containers and the struct inode is _inside_ the
222  * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
223  * inode pointer), return a valid non-NULL pointer.
224  */
225 static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
226 {
227         return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
228 }
229
230 /* dentry to private data */
231 #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
232
233 /* superblock to private data */
234 #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
235
236 /* file to private Data */
237 #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
238
239 /* file to lower file */
240 static inline struct file *sdcardfs_lower_file(const struct file *f)
241 {
242         return SDCARDFS_F(f)->lower_file;
243 }
244
245 static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
246 {
247         SDCARDFS_F(f)->lower_file = val;
248 }
249
250 /* inode to lower inode. */
251 static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
252 {
253         return SDCARDFS_I(i)->lower_inode;
254 }
255
256 static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
257 {
258         SDCARDFS_I(i)->lower_inode = val;
259 }
260
261 /* superblock to lower superblock */
262 static inline struct super_block *sdcardfs_lower_super(
263         const struct super_block *sb)
264 {
265         return SDCARDFS_SB(sb)->lower_sb;
266 }
267
268 static inline void sdcardfs_set_lower_super(struct super_block *sb,
269                                           struct super_block *val)
270 {
271         SDCARDFS_SB(sb)->lower_sb = val;
272 }
273
274 /* path based (dentry/mnt) macros */
275 static inline void pathcpy(struct path *dst, const struct path *src)
276 {
277         dst->dentry = src->dentry;
278         dst->mnt = src->mnt;
279 }
280
281 /* sdcardfs_get_pname functions calls path_get()
282  * therefore, the caller must call "proper" path_put functions
283  */
284 #define SDCARDFS_DENT_FUNC(pname) \
285 static inline void sdcardfs_get_##pname(const struct dentry *dent, \
286                                         struct path *pname) \
287 { \
288         spin_lock(&SDCARDFS_D(dent)->lock); \
289         pathcpy(pname, &SDCARDFS_D(dent)->pname); \
290         path_get(pname); \
291         spin_unlock(&SDCARDFS_D(dent)->lock); \
292         return; \
293 } \
294 static inline void sdcardfs_put_##pname(const struct dentry *dent, \
295                                         struct path *pname) \
296 { \
297         path_put(pname); \
298         return; \
299 } \
300 static inline void sdcardfs_set_##pname(const struct dentry *dent, \
301                                         struct path *pname) \
302 { \
303         spin_lock(&SDCARDFS_D(dent)->lock); \
304         pathcpy(&SDCARDFS_D(dent)->pname, pname); \
305         spin_unlock(&SDCARDFS_D(dent)->lock); \
306         return; \
307 } \
308 static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
309 { \
310         spin_lock(&SDCARDFS_D(dent)->lock); \
311         SDCARDFS_D(dent)->pname.dentry = NULL; \
312         SDCARDFS_D(dent)->pname.mnt = NULL; \
313         spin_unlock(&SDCARDFS_D(dent)->lock); \
314         return; \
315 } \
316 static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
317 { \
318         struct path pname; \
319         spin_lock(&SDCARDFS_D(dent)->lock); \
320         if(SDCARDFS_D(dent)->pname.dentry) { \
321                 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
322                 SDCARDFS_D(dent)->pname.dentry = NULL; \
323                 SDCARDFS_D(dent)->pname.mnt = NULL; \
324                 spin_unlock(&SDCARDFS_D(dent)->lock); \
325                 path_put(&pname); \
326         } else \
327                 spin_unlock(&SDCARDFS_D(dent)->lock); \
328         return; \
329 }
330
331 SDCARDFS_DENT_FUNC(lower_path)
332 SDCARDFS_DENT_FUNC(orig_path)
333
334 static inline int has_graft_path(const struct dentry *dent)
335 {
336         int ret = 0;
337
338         spin_lock(&SDCARDFS_D(dent)->lock);
339         if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
340                 ret = 1;
341         spin_unlock(&SDCARDFS_D(dent)->lock);
342
343         return ret;
344 }
345
346 static inline void sdcardfs_get_real_lower(const struct dentry *dent,
347                                                 struct path *real_lower)
348 {
349         /* in case of a local obb dentry
350          * the orig_path should be returned
351          */
352         if(has_graft_path(dent))
353                 sdcardfs_get_orig_path(dent, real_lower);
354         else
355                 sdcardfs_get_lower_path(dent, real_lower);
356 }
357
358 static inline void sdcardfs_put_real_lower(const struct dentry *dent,
359                                                 struct path *real_lower)
360 {
361         if(has_graft_path(dent))
362                 sdcardfs_put_orig_path(dent, real_lower);
363         else
364                 sdcardfs_put_lower_path(dent, real_lower);
365 }
366
367 /* for packagelist.c */
368 extern int get_caller_has_rw_locked(void *pkgl_id, derive_t derive);
369 extern appid_t get_appid(void *pkgl_id, const char *app_name);
370 extern int check_caller_access_to_name(struct inode *parent_node, const char* name,
371                                         derive_t derive, int w_ok, int has_rw);
372 extern int open_flags_to_access_mode(int open_flags);
373 extern void * packagelist_create(gid_t write_gid);
374 extern void packagelist_destroy(void *pkgl_id);
375 extern int packagelist_init(void);
376 extern void packagelist_exit(void);
377
378 /* for derived_perm.c */
379 extern void setup_derived_state(struct inode *inode, perm_t perm,
380                         userid_t userid, uid_t uid, gid_t gid, mode_t mode);
381 extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
382 extern void update_derived_permission(struct dentry *dentry);
383 extern int need_graft_path(struct dentry *dentry);
384 extern int is_base_obbpath(struct dentry *dentry);
385 extern int is_obbpath_invalid(struct dentry *dentry);
386 extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
387
388 /* locking helpers */
389 static inline struct dentry *lock_parent(struct dentry *dentry)
390 {
391         struct dentry *dir = dget_parent(dentry);
392         mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT);
393         return dir;
394 }
395
396 static inline void unlock_dir(struct dentry *dir)
397 {
398         mutex_unlock(&d_inode(dir)->i_mutex);
399         dput(dir);
400 }
401
402 static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
403 {
404         int err;
405         struct dentry *dent;
406         struct iattr attrs;
407         struct path parent;
408
409         dent = kern_path_locked(path_s, &parent);
410         if (IS_ERR(dent)) {
411                 err = PTR_ERR(dent);
412                 if (err == -EEXIST)
413                         err = 0;
414                 goto out_unlock;
415         }
416
417         err = vfs_mkdir(d_inode(parent.dentry), dent, mode);
418         if (err) {
419                 if (err == -EEXIST)
420                         err = 0;
421                 goto out_dput;
422         }
423
424         attrs.ia_uid = make_kuid(&init_user_ns, uid);
425         attrs.ia_gid = make_kgid(&init_user_ns, gid);
426         attrs.ia_valid = ATTR_UID | ATTR_GID;
427         mutex_lock(&d_inode(dent)->i_mutex);
428         notify_change(dent, &attrs, NULL);
429         mutex_unlock(&d_inode(dent)->i_mutex);
430
431 out_dput:
432         dput(dent);
433
434 out_unlock:
435         /* parent dentry locked by lookup_create */
436         mutex_unlock(&d_inode(parent.dentry)->i_mutex);
437         path_put(&parent);
438         return err;
439 }
440
441 /*
442  * Return 1, if a disk has enough free space, otherwise 0.
443  * We assume that any files can not be overwritten.
444  */
445 static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
446 {
447         int err;
448         struct path lower_path;
449         struct kstatfs statfs;
450         u64 avail;
451         struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
452
453         if (sbi->options.reserved_mb) {
454                 /* Get fs stat of lower filesystem. */
455                 sdcardfs_get_lower_path(dentry, &lower_path);
456                 err = vfs_statfs(&lower_path, &statfs);
457                 sdcardfs_put_lower_path(dentry, &lower_path);
458
459                 if (unlikely(err))
460                         return 0;
461
462                 /* Invalid statfs informations. */
463                 if (unlikely(statfs.f_bsize == 0))
464                         return 0;
465
466                 /* if you are checking directory, set size to f_bsize. */
467                 if (unlikely(dir))
468                         size = statfs.f_bsize;
469
470                 /* available size */
471                 avail = statfs.f_bavail * statfs.f_bsize;
472
473                 /* not enough space */
474                 if ((u64)size > avail)
475                         return 0;
476
477                 /* enough space */
478                 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
479                         return 1;
480
481                 return 0;
482         } else
483                 return 1;
484 }
485
486 #endif  /* not _SDCARDFS_H_ */