2 * fs/sdcardfs/sdcardfs.h
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
9 * Copyright (c) 2013 Samsung Electronics Co. Ltd
10 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
11 * Sunghwan Yun, Sungjong Seo
13 * This program has been developed as a stackable file system based on
14 * the WrapFS which written by
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
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.
29 #include <linux/dcache.h>
30 #include <linux/file.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"
47 /* the file system name */
48 #define SDCARDFS_NAME "sdcardfs"
50 /* sdcardfs root inode number */
51 #define SDCARDFS_ROOT_INO 1
53 /* useful for tracking code reachability */
54 #define UDBG printk(KERN_DEFAULT "DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
56 #define SDCARDFS_DIRENT_SIZE 256
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 */
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 */
68 #define AID_PACKAGE_INFO 1027
70 #define fix_derived_permission(x) \
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;\
77 /* OVERRIDE_CRED() and REVERT_CRED()
79 * backup original task->cred
80 * and modifies task->cred->fsuid/fsgid to specified value.
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.
86 #define OVERRIDE_CRED(sdcardfs_sbi, saved_cred) \
87 saved_cred = override_fsids(sdcardfs_sbi); \
88 if (!saved_cred) { return -ENOMEM; }
90 #define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred) \
91 saved_cred = override_fsids(sdcardfs_sbi); \
92 if (!saved_cred) { return ERR_PTR(-ENOMEM); }
94 #define REVERT_CRED(saved_cred) revert_fsids(saved_cred)
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);
102 /* Android 4.4 support */
104 /* Permission mode for a specific node. Controls how file permissions
105 * are derived for children nodes. */
107 /* Nothing special; this node should just inherit from its parent. */
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 "/" */
114 /* This node is "/Android" */
116 /* This node is "/Android/data" */
118 /* This node is "/Android/obb" */
120 /* This node is "/Android/user" */
124 /* Permissions structure to derive */
136 struct sdcardfs_sb_info;
137 struct sdcardfs_mount_options;
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);
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;
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,
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);
168 /* file private data */
169 struct sdcardfs_file_info {
170 struct file *lower_file;
171 const struct vm_operations_struct *lower_vm_ops;
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
186 struct inode vfs_inode;
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;
196 struct sdcardfs_mount_options {
203 unsigned int reserved_mb;
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 */
219 * inode to private data
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.
225 static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
227 return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
230 /* dentry to private data */
231 #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
233 /* superblock to private data */
234 #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
236 /* file to private Data */
237 #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
239 /* file to lower file */
240 static inline struct file *sdcardfs_lower_file(const struct file *f)
242 return SDCARDFS_F(f)->lower_file;
245 static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
247 SDCARDFS_F(f)->lower_file = val;
250 /* inode to lower inode. */
251 static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
253 return SDCARDFS_I(i)->lower_inode;
256 static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
258 SDCARDFS_I(i)->lower_inode = val;
261 /* superblock to lower superblock */
262 static inline struct super_block *sdcardfs_lower_super(
263 const struct super_block *sb)
265 return SDCARDFS_SB(sb)->lower_sb;
268 static inline void sdcardfs_set_lower_super(struct super_block *sb,
269 struct super_block *val)
271 SDCARDFS_SB(sb)->lower_sb = val;
274 /* path based (dentry/mnt) macros */
275 static inline void pathcpy(struct path *dst, const struct path *src)
277 dst->dentry = src->dentry;
281 /* sdcardfs_get_pname functions calls path_get()
282 * therefore, the caller must call "proper" path_put functions
284 #define SDCARDFS_DENT_FUNC(pname) \
285 static inline void sdcardfs_get_##pname(const struct dentry *dent, \
286 struct path *pname) \
288 spin_lock(&SDCARDFS_D(dent)->lock); \
289 pathcpy(pname, &SDCARDFS_D(dent)->pname); \
291 spin_unlock(&SDCARDFS_D(dent)->lock); \
294 static inline void sdcardfs_put_##pname(const struct dentry *dent, \
295 struct path *pname) \
300 static inline void sdcardfs_set_##pname(const struct dentry *dent, \
301 struct path *pname) \
303 spin_lock(&SDCARDFS_D(dent)->lock); \
304 pathcpy(&SDCARDFS_D(dent)->pname, pname); \
305 spin_unlock(&SDCARDFS_D(dent)->lock); \
308 static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
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); \
316 static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
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); \
327 spin_unlock(&SDCARDFS_D(dent)->lock); \
331 SDCARDFS_DENT_FUNC(lower_path)
332 SDCARDFS_DENT_FUNC(orig_path)
334 static inline int has_graft_path(const struct dentry *dent)
338 spin_lock(&SDCARDFS_D(dent)->lock);
339 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
341 spin_unlock(&SDCARDFS_D(dent)->lock);
346 static inline void sdcardfs_get_real_lower(const struct dentry *dent,
347 struct path *real_lower)
349 /* in case of a local obb dentry
350 * the orig_path should be returned
352 if(has_graft_path(dent))
353 sdcardfs_get_orig_path(dent, real_lower);
355 sdcardfs_get_lower_path(dent, real_lower);
358 static inline void sdcardfs_put_real_lower(const struct dentry *dent,
359 struct path *real_lower)
361 if(has_graft_path(dent))
362 sdcardfs_put_orig_path(dent, real_lower);
364 sdcardfs_put_lower_path(dent, real_lower);
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);
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);
388 /* locking helpers */
389 static inline struct dentry *lock_parent(struct dentry *dentry)
391 struct dentry *dir = dget_parent(dentry);
392 mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT);
396 static inline void unlock_dir(struct dentry *dir)
398 mutex_unlock(&d_inode(dir)->i_mutex);
402 static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
409 dent = kern_path_locked(path_s, &parent);
417 err = vfs_mkdir(d_inode(parent.dentry), dent, mode);
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);
435 /* parent dentry locked by lookup_create */
436 mutex_unlock(&d_inode(parent.dentry)->i_mutex);
442 * Return 1, if a disk has enough free space, otherwise 0.
443 * We assume that any files can not be overwritten.
445 static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
448 struct path lower_path;
449 struct kstatfs statfs;
451 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
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);
462 /* Invalid statfs informations. */
463 if (unlikely(statfs.f_bsize == 0))
466 /* if you are checking directory, set size to f_bsize. */
468 size = statfs.f_bsize;
471 avail = statfs.f_bavail * statfs.f_bsize;
473 /* not enough space */
474 if ((u64)size > avail)
478 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
486 #endif /* not _SDCARDFS_H_ */