ANDROID: sdcardfs: Add option to not link obb
[GitHub/LineageOS/android_kernel_samsung_universal7580.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/aio.h>
33 #include <linux/kref.h>
34 #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>
47 #include <linux/list.h>
48 #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 */
57 #define UDBG pr_default("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
58
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 */
70 #define AID_MEDIA_OBB 1059 /* obb files */
71
72 #define AID_SDCARD_IMAGE 1057
73
74 #define AID_PACKAGE_INFO 1027
75
76
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) \
84 do { \
85 (x)->i_uid = SDCARDFS_I(x)->data->d_uid; \
86 (x)->i_gid = AID_SDCARD_RW; \
87 (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\
88 } while (0)
89
90 /* Android 5.0 support */
91
92 /* Permission mode for a specific node. Controls how file permissions
93 * are derived for children nodes.
94 */
95 typedef enum {
96 /* 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,
116 } perm_t;
117
118 struct sdcardfs_sb_info;
119 struct sdcardfs_mount_options;
120 struct sdcardfs_inode_info;
121 struct sdcardfs_inode_data;
122
123 /* Do not directly use this function. Use OVERRIDE_CRED() instead. */
124 const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
125 struct sdcardfs_inode_data *data);
126 /* Do not directly use this function, use REVERT_CRED() instead. */
127 void revert_fsids(const struct cred *old_cred);
128
129 /* operations vectors defined in specific files */
130 extern const struct file_operations sdcardfs_main_fops;
131 extern const struct file_operations sdcardfs_dir_fops;
132 extern const struct inode_operations sdcardfs_main_iops;
133 extern const struct inode_operations sdcardfs_dir_iops;
134 extern const struct inode_operations sdcardfs_symlink_iops;
135 extern const struct super_operations sdcardfs_sops;
136 extern const struct dentry_operations sdcardfs_ci_dops;
137 extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
138 extern const struct vm_operations_struct sdcardfs_vm_ops;
139
140 extern int sdcardfs_init_inode_cache(void);
141 extern void sdcardfs_destroy_inode_cache(void);
142 extern int sdcardfs_init_dentry_cache(void);
143 extern void sdcardfs_destroy_dentry_cache(void);
144 extern int new_dentry_private_data(struct dentry *dentry);
145 extern void free_dentry_private_data(struct dentry *dentry);
146 extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
147 unsigned int flags);
148 extern struct inode *sdcardfs_iget(struct super_block *sb,
149 struct inode *lower_inode, userid_t id);
150 extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
151 struct path *lower_path, userid_t id);
152
153 /* file private data */
154 struct sdcardfs_file_info {
155 struct file *lower_file;
156 const struct vm_operations_struct *lower_vm_ops;
157 };
158
159 struct sdcardfs_inode_data {
160 struct kref refcount;
161 bool abandoned;
162
163 perm_t perm;
164 userid_t userid;
165 uid_t d_uid;
166 bool under_android;
167 bool under_cache;
168 bool under_obb;
169 };
170
171 /* sdcardfs inode data in memory */
172 struct sdcardfs_inode_info {
173 struct inode *lower_inode;
174 /* state derived based on current position in hierarchy */
175 struct sdcardfs_inode_data *data;
176
177 /* top folder for ownership */
178 spinlock_t top_lock;
179 struct sdcardfs_inode_data *top_data;
180
181 struct inode vfs_inode;
182 };
183
184
185 /* sdcardfs dentry data in memory */
186 struct sdcardfs_dentry_info {
187 spinlock_t lock; /* protects lower_path */
188 struct path lower_path;
189 struct path orig_path;
190 };
191
192 struct sdcardfs_mount_options {
193 uid_t fs_low_uid;
194 gid_t fs_low_gid;
195 userid_t fs_user_id;
196 bool multiuser;
197 bool gid_derivation;
198 bool default_normal;
199 bool unshared_obb;
200 unsigned int reserved_mb;
201 bool nocache;
202 };
203
204 struct sdcardfs_vfsmount_options {
205 gid_t gid;
206 mode_t mask;
207 };
208
209 extern int parse_options_remount(struct super_block *sb, char *options, int silent,
210 struct sdcardfs_vfsmount_options *vfsopts);
211
212 /* sdcardfs super-block data in memory */
213 struct sdcardfs_sb_info {
214 struct super_block *sb;
215 struct super_block *lower_sb;
216 /* derived perm policy : some of options have been added
217 * to sdcardfs_mount_options (Android 4.4 support)
218 */
219 struct sdcardfs_mount_options options;
220 spinlock_t lock; /* protects obbpath */
221 char *obbpath_s;
222 struct path obbpath;
223 void *pkgl_id;
224 struct list_head list;
225 };
226
227 /*
228 * inode to private data
229 *
230 * Since we use containers and the struct inode is _inside_ the
231 * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
232 * inode pointer), return a valid non-NULL pointer.
233 */
234 static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
235 {
236 return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
237 }
238
239 /* dentry to private data */
240 #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
241
242 /* superblock to private data */
243 #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
244
245 /* file to private Data */
246 #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
247
248 /* file to lower file */
249 static inline struct file *sdcardfs_lower_file(const struct file *f)
250 {
251 return SDCARDFS_F(f)->lower_file;
252 }
253
254 static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
255 {
256 SDCARDFS_F(f)->lower_file = val;
257 }
258
259 /* inode to lower inode. */
260 static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
261 {
262 return SDCARDFS_I(i)->lower_inode;
263 }
264
265 static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
266 {
267 SDCARDFS_I(i)->lower_inode = val;
268 }
269
270 /* superblock to lower superblock */
271 static inline struct super_block *sdcardfs_lower_super(
272 const struct super_block *sb)
273 {
274 return SDCARDFS_SB(sb)->lower_sb;
275 }
276
277 static inline void sdcardfs_set_lower_super(struct super_block *sb,
278 struct super_block *val)
279 {
280 SDCARDFS_SB(sb)->lower_sb = val;
281 }
282
283 /* path based (dentry/mnt) macros */
284 static inline void pathcpy(struct path *dst, const struct path *src)
285 {
286 dst->dentry = src->dentry;
287 dst->mnt = src->mnt;
288 }
289
290 /* sdcardfs_get_pname functions calls path_get()
291 * therefore, the caller must call "proper" path_put functions
292 */
293 #define SDCARDFS_DENT_FUNC(pname) \
294 static inline void sdcardfs_get_##pname(const struct dentry *dent, \
295 struct path *pname) \
296 { \
297 spin_lock(&SDCARDFS_D(dent)->lock); \
298 pathcpy(pname, &SDCARDFS_D(dent)->pname); \
299 path_get(pname); \
300 spin_unlock(&SDCARDFS_D(dent)->lock); \
301 return; \
302 } \
303 static inline void sdcardfs_put_##pname(const struct dentry *dent, \
304 struct path *pname) \
305 { \
306 path_put(pname); \
307 return; \
308 } \
309 static inline void sdcardfs_set_##pname(const struct dentry *dent, \
310 struct path *pname) \
311 { \
312 spin_lock(&SDCARDFS_D(dent)->lock); \
313 pathcpy(&SDCARDFS_D(dent)->pname, pname); \
314 spin_unlock(&SDCARDFS_D(dent)->lock); \
315 return; \
316 } \
317 static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
318 { \
319 spin_lock(&SDCARDFS_D(dent)->lock); \
320 SDCARDFS_D(dent)->pname.dentry = NULL; \
321 SDCARDFS_D(dent)->pname.mnt = NULL; \
322 spin_unlock(&SDCARDFS_D(dent)->lock); \
323 return; \
324 } \
325 static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
326 { \
327 struct path pname; \
328 spin_lock(&SDCARDFS_D(dent)->lock); \
329 if (SDCARDFS_D(dent)->pname.dentry) { \
330 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
331 SDCARDFS_D(dent)->pname.dentry = NULL; \
332 SDCARDFS_D(dent)->pname.mnt = NULL; \
333 spin_unlock(&SDCARDFS_D(dent)->lock); \
334 path_put(&pname); \
335 } else \
336 spin_unlock(&SDCARDFS_D(dent)->lock); \
337 return; \
338 }
339
340 SDCARDFS_DENT_FUNC(lower_path)
341 SDCARDFS_DENT_FUNC(orig_path)
342
343 static inline bool sbinfo_has_sdcard_magic(struct sdcardfs_sb_info *sbinfo)
344 {
345 return sbinfo && sbinfo->sb
346 && sbinfo->sb->s_magic == SDCARDFS_SUPER_MAGIC;
347 }
348
349 static inline struct sdcardfs_inode_data *data_get(
350 struct sdcardfs_inode_data *data)
351 {
352 if (data)
353 kref_get(&data->refcount);
354 return data;
355 }
356
357 static inline struct sdcardfs_inode_data *top_data_get(
358 struct sdcardfs_inode_info *info)
359 {
360 struct sdcardfs_inode_data *top_data;
361
362 spin_lock(&info->top_lock);
363 top_data = data_get(info->top_data);
364 spin_unlock(&info->top_lock);
365 return top_data;
366 }
367
368 extern void data_release(struct kref *ref);
369
370 static inline void data_put(struct sdcardfs_inode_data *data)
371 {
372 kref_put(&data->refcount, data_release);
373 }
374
375 static inline void release_own_data(struct sdcardfs_inode_info *info)
376 {
377 /*
378 * This happens exactly once per inode. At this point, the inode that
379 * originally held this data is about to be freed, and all references
380 * to it are held as a top value, and will likely be released soon.
381 */
382 info->data->abandoned = true;
383 data_put(info->data);
384 }
385
386 static inline void set_top(struct sdcardfs_inode_info *info,
387 struct sdcardfs_inode_info *top_owner)
388 {
389 struct sdcardfs_inode_data *old_top;
390 struct sdcardfs_inode_data *new_top = NULL;
391
392 if (top_owner)
393 new_top = top_data_get(top_owner);
394
395 spin_lock(&info->top_lock);
396 old_top = info->top_data;
397 info->top_data = new_top;
398 if (old_top)
399 data_put(old_top);
400 spin_unlock(&info->top_lock);
401 }
402
403 static inline int get_gid(struct vfsmount *mnt,
404 struct super_block *sb,
405 struct sdcardfs_inode_data *data)
406 {
407 struct sdcardfs_vfsmount_options *vfsopts = mnt->data;
408 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(sb);
409
410 if (vfsopts->gid == AID_SDCARD_RW && !sbi->options.default_normal)
411 /* As an optimization, certain trusted system components only run
412 * as owner but operate across all users. Since we're now handing
413 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
414 * the user boundary enforcement for the default view. The UIDs
415 * assigned to app directories are still multiuser aware.
416 */
417 return AID_SDCARD_RW;
418 else
419 return multiuser_get_uid(data->userid, vfsopts->gid);
420 }
421
422 static inline int get_mode(struct vfsmount *mnt,
423 struct sdcardfs_inode_info *info,
424 struct sdcardfs_inode_data *data)
425 {
426 int owner_mode;
427 int filtered_mode;
428 struct sdcardfs_vfsmount_options *opts = mnt->data;
429 int visible_mode = 0775 & ~opts->mask;
430
431
432 if (data->perm == PERM_PRE_ROOT) {
433 /* Top of multi-user view should always be visible to ensure
434 * secondary users can traverse inside.
435 */
436 visible_mode = 0711;
437 } else if (data->under_android) {
438 /* Block "other" access to Android directories, since only apps
439 * belonging to a specific user should be in there; we still
440 * leave +x open for the default view.
441 */
442 if (opts->gid == AID_SDCARD_RW)
443 visible_mode = visible_mode & ~0006;
444 else
445 visible_mode = visible_mode & ~0007;
446 }
447 owner_mode = info->lower_inode->i_mode & 0700;
448 filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
449 return filtered_mode;
450 }
451
452 static inline int has_graft_path(const struct dentry *dent)
453 {
454 int ret = 0;
455
456 spin_lock(&SDCARDFS_D(dent)->lock);
457 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
458 ret = 1;
459 spin_unlock(&SDCARDFS_D(dent)->lock);
460
461 return ret;
462 }
463
464 static inline void sdcardfs_get_real_lower(const struct dentry *dent,
465 struct path *real_lower)
466 {
467 /* in case of a local obb dentry
468 * the orig_path should be returned
469 */
470 if (has_graft_path(dent))
471 sdcardfs_get_orig_path(dent, real_lower);
472 else
473 sdcardfs_get_lower_path(dent, real_lower);
474 }
475
476 static inline void sdcardfs_put_real_lower(const struct dentry *dent,
477 struct path *real_lower)
478 {
479 if (has_graft_path(dent))
480 sdcardfs_put_orig_path(dent, real_lower);
481 else
482 sdcardfs_put_lower_path(dent, real_lower);
483 }
484
485 extern struct mutex sdcardfs_super_list_lock;
486 extern struct list_head sdcardfs_super_list;
487
488 /* for packagelist.c */
489 extern appid_t get_appid(const char *app_name);
490 extern appid_t get_ext_gid(const char *app_name);
491 extern appid_t is_excluded(const char *app_name, userid_t userid);
492 extern int check_caller_access_to_name(struct inode *parent_node, const struct qstr *name);
493 extern int packagelist_init(void);
494 extern void packagelist_exit(void);
495
496 /* for derived_perm.c */
497 #define BY_NAME (1 << 0)
498 #define BY_USERID (1 << 1)
499 struct limit_search {
500 unsigned int flags;
501 struct qstr name;
502 userid_t userid;
503 };
504
505 extern void setup_derived_state(struct inode *inode, perm_t perm,
506 userid_t userid, uid_t uid);
507 extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
508 extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name);
509 extern void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit);
510
511 extern void update_derived_permission_lock(struct dentry *dentry);
512 void fixup_lower_ownership(struct dentry *dentry, const char *name);
513 extern int need_graft_path(struct dentry *dentry);
514 extern int is_base_obbpath(struct dentry *dentry);
515 extern int is_obbpath_invalid(struct dentry *dentry);
516 extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
517
518 /* locking helpers */
519 static inline struct dentry *lock_parent(struct dentry *dentry)
520 {
521 struct dentry *dir = dget_parent(dentry);
522
523 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
524 return dir;
525 }
526
527 static inline void unlock_dir(struct dentry *dir)
528 {
529 mutex_unlock(&dir->d_inode->i_mutex);
530 dput(dir);
531 }
532
533 static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
534 {
535 int err;
536 struct dentry *dent;
537 struct iattr attrs;
538 struct path parent;
539
540 dent = kern_path_locked(path_s, &parent);
541 if (IS_ERR(dent)) {
542 err = PTR_ERR(dent);
543 if (err == -EEXIST)
544 err = 0;
545 goto out_unlock;
546 }
547
548 err = vfs_mkdir2(parent.mnt, parent.dentry->d_inode, dent, mode);
549 if (err) {
550 if (err == -EEXIST)
551 err = 0;
552 goto out_dput;
553 }
554
555 attrs.ia_uid = uid;
556 attrs.ia_gid = gid;
557 attrs.ia_valid = ATTR_UID | ATTR_GID;
558 mutex_lock(&dent->d_inode->i_mutex);
559 notify_change2(parent.mnt, dent, &attrs);
560 mutex_unlock(&dent->d_inode->i_mutex);
561
562 out_dput:
563 dput(dent);
564
565 out_unlock:
566 /* parent dentry locked by lookup_create */
567 mutex_unlock(&parent.dentry->d_inode->i_mutex);
568 path_put(&parent);
569 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 */
576 static 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
617 /*
618 * Copies attrs and maintains sdcardfs managed attrs
619 * Since our permission check handles all special permissions, set those to be open
620 */
621 static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
622 {
623
624 dest->i_mode = (src->i_mode & S_IFMT) | S_IRWXU | S_IRWXG |
625 S_IROTH | S_IXOTH; /* 0775 */
626 dest->i_uid = SDCARDFS_I(dest)->data->d_uid;
627 dest->i_gid = AID_SDCARD_RW;
628 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 }
636
637 static inline bool str_case_eq(const char *s1, const char *s2)
638 {
639 return !strcasecmp(s1, s2);
640 }
641
642 static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len)
643 {
644 return !strncasecmp(s1, s2, len);
645 }
646
647 static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2)
648 {
649 return q1->len == q2->len && str_n_case_eq(q1->name, q2->name, q2->len);
650 }
651
652 #define QSTR_LITERAL(string) QSTR_INIT(string, sizeof(string)-1)
653
654 #endif /* not _SDCARDFS_H_ */