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 = SDCARDFS_I(x)->d_uid; \
73 (x)->i_gid = 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,
162 struct nameidata *nd);
163 extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
164 struct path *lower_path);
166 /* file private data */
167 struct sdcardfs_file_info {
168 struct file *lower_file;
169 const struct vm_operations_struct *lower_vm_ops;
172 /* sdcardfs inode data in memory */
173 struct sdcardfs_inode_info {
174 struct inode *lower_inode;
175 /* state derived based on current position in hierachy
176 * caution: d_mode does not include file types
184 struct inode vfs_inode;
187 /* sdcardfs dentry data in memory */
188 struct sdcardfs_dentry_info {
189 spinlock_t lock; /* protects lower_path */
190 struct path lower_path;
191 struct path orig_path;
194 struct sdcardfs_mount_options {
201 unsigned int reserved_mb;
204 /* sdcardfs super-block data in memory */
205 struct sdcardfs_sb_info {
206 struct super_block *lower_sb;
207 /* derived perm policy : some of options have been added
208 * to sdcardfs_mount_options (Android 4.4 support) */
209 struct sdcardfs_mount_options options;
210 spinlock_t lock; /* protects obbpath */
217 * inode to private data
219 * Since we use containers and the struct inode is _inside_ the
220 * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
221 * inode pointer), return a valid non-NULL pointer.
223 static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
225 return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
228 /* dentry to private data */
229 #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
231 /* superblock to private data */
232 #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
234 /* file to private Data */
235 #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
237 /* file to lower file */
238 static inline struct file *sdcardfs_lower_file(const struct file *f)
240 return SDCARDFS_F(f)->lower_file;
243 static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
245 SDCARDFS_F(f)->lower_file = val;
248 /* inode to lower inode. */
249 static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
251 return SDCARDFS_I(i)->lower_inode;
254 static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
256 SDCARDFS_I(i)->lower_inode = val;
259 /* superblock to lower superblock */
260 static inline struct super_block *sdcardfs_lower_super(
261 const struct super_block *sb)
263 return SDCARDFS_SB(sb)->lower_sb;
266 static inline void sdcardfs_set_lower_super(struct super_block *sb,
267 struct super_block *val)
269 SDCARDFS_SB(sb)->lower_sb = val;
272 /* path based (dentry/mnt) macros */
273 static inline void pathcpy(struct path *dst, const struct path *src)
275 dst->dentry = src->dentry;
279 /* sdcardfs_get_pname functions calls path_get()
280 * therefore, the caller must call "proper" path_put functions
282 #define SDCARDFS_DENT_FUNC(pname) \
283 static inline void sdcardfs_get_##pname(const struct dentry *dent, \
284 struct path *pname) \
286 spin_lock(&SDCARDFS_D(dent)->lock); \
287 pathcpy(pname, &SDCARDFS_D(dent)->pname); \
289 spin_unlock(&SDCARDFS_D(dent)->lock); \
292 static inline void sdcardfs_put_##pname(const struct dentry *dent, \
293 struct path *pname) \
298 static inline void sdcardfs_set_##pname(const struct dentry *dent, \
299 struct path *pname) \
301 spin_lock(&SDCARDFS_D(dent)->lock); \
302 pathcpy(&SDCARDFS_D(dent)->pname, pname); \
303 spin_unlock(&SDCARDFS_D(dent)->lock); \
306 static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
308 spin_lock(&SDCARDFS_D(dent)->lock); \
309 SDCARDFS_D(dent)->pname.dentry = NULL; \
310 SDCARDFS_D(dent)->pname.mnt = NULL; \
311 spin_unlock(&SDCARDFS_D(dent)->lock); \
314 static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
317 spin_lock(&SDCARDFS_D(dent)->lock); \
318 if(SDCARDFS_D(dent)->pname.dentry) { \
319 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
320 SDCARDFS_D(dent)->pname.dentry = NULL; \
321 SDCARDFS_D(dent)->pname.mnt = NULL; \
322 spin_unlock(&SDCARDFS_D(dent)->lock); \
325 spin_unlock(&SDCARDFS_D(dent)->lock); \
329 SDCARDFS_DENT_FUNC(lower_path)
330 SDCARDFS_DENT_FUNC(orig_path)
332 static inline int has_graft_path(const struct dentry *dent)
336 spin_lock(&SDCARDFS_D(dent)->lock);
337 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
339 spin_unlock(&SDCARDFS_D(dent)->lock);
344 static inline void sdcardfs_get_real_lower(const struct dentry *dent,
345 struct path *real_lower)
347 /* in case of a local obb dentry
348 * the orig_path should be returned
350 if(has_graft_path(dent))
351 sdcardfs_get_orig_path(dent, real_lower);
353 sdcardfs_get_lower_path(dent, real_lower);
356 static inline void sdcardfs_put_real_lower(const struct dentry *dent,
357 struct path *real_lower)
359 if(has_graft_path(dent))
360 sdcardfs_put_orig_path(dent, real_lower);
362 sdcardfs_put_lower_path(dent, real_lower);
365 /* for packagelist.c */
366 extern int get_caller_has_rw_locked(void *pkgl_id, derive_t derive);
367 extern appid_t get_appid(void *pkgl_id, const char *app_name);
368 extern int check_caller_access_to_name(struct inode *parent_node, const char* name,
369 derive_t derive, int w_ok, int has_rw);
370 extern int open_flags_to_access_mode(int open_flags);
371 extern void * packagelist_create(gid_t write_gid);
372 extern void packagelist_destroy(void *pkgl_id);
373 extern int packagelist_init(void);
374 extern void packagelist_exit(void);
376 /* for derived_perm.c */
377 extern void setup_derived_state(struct inode *inode, perm_t perm,
378 userid_t userid, uid_t uid, gid_t gid, mode_t mode);
379 extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
380 extern void update_derived_permission(struct dentry *dentry);
381 extern int need_graft_path(struct dentry *dentry);
382 extern int is_base_obbpath(struct dentry *dentry);
383 extern int is_obbpath_invalid(struct dentry *dentry);
384 extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
386 /* locking helpers */
387 static inline struct dentry *lock_parent(struct dentry *dentry)
389 struct dentry *dir = dget_parent(dentry);
390 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
394 static inline void unlock_dir(struct dentry *dir)
396 mutex_unlock(&dir->d_inode->i_mutex);
400 static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
407 err = kern_path_parent(path_s, &nd);
414 dent = lookup_create(&nd, 1);
422 err = vfs_mkdir(nd.path.dentry->d_inode, dent, mode);
431 attrs.ia_valid = ATTR_UID | ATTR_GID;
432 mutex_lock(&dent->d_inode->i_mutex);
433 notify_change(dent, &attrs);
434 mutex_unlock(&dent->d_inode->i_mutex);
440 /* parent dentry locked by lookup_create */
441 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
449 * Return 1, if a disk has enough free space, otherwise 0.
450 * We assume that any files can not be overwritten.
452 static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
455 struct path lower_path;
456 struct kstatfs statfs;
458 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
460 if (sbi->options.reserved_mb) {
461 /* Get fs stat of lower filesystem. */
462 sdcardfs_get_lower_path(dentry, &lower_path);
463 err = vfs_statfs(&lower_path, &statfs);
464 sdcardfs_put_lower_path(dentry, &lower_path);
469 /* Invalid statfs informations. */
470 if (unlikely(statfs.f_bsize == 0))
473 /* if you are checking directory, set size to f_bsize. */
475 size = statfs.f_bsize;
478 avail = statfs.f_bavail * statfs.f_bsize;
480 /* not enough space */
481 if ((u64)size > avail)
485 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
493 #endif /* not _SDCARDFS_H_ */