Daniel Campello | 593d662 | 2015-07-20 16:23:50 -0700 | [diff] [blame^] | 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 = 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;\ |
| 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 | struct nameidata *nd); |
| 163 | extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb, |
| 164 | struct path *lower_path); |
| 165 | |
| 166 | /* file private data */ |
| 167 | struct sdcardfs_file_info { |
| 168 | struct file *lower_file; |
| 169 | const struct vm_operations_struct *lower_vm_ops; |
| 170 | }; |
| 171 | |
| 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 |
| 177 | */ |
| 178 | perm_t perm; |
| 179 | userid_t userid; |
| 180 | uid_t d_uid; |
| 181 | gid_t d_gid; |
| 182 | mode_t d_mode; |
| 183 | |
| 184 | struct inode vfs_inode; |
| 185 | }; |
| 186 | |
| 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; |
| 192 | }; |
| 193 | |
| 194 | struct sdcardfs_mount_options { |
| 195 | uid_t fs_low_uid; |
| 196 | gid_t fs_low_gid; |
| 197 | gid_t write_gid; |
| 198 | int split_perms; |
| 199 | derive_t derive; |
| 200 | lower_fs_t lower_fs; |
| 201 | unsigned int reserved_mb; |
| 202 | }; |
| 203 | |
| 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 */ |
| 211 | char *obbpath_s; |
| 212 | struct path obbpath; |
| 213 | void *pkgl_id; |
| 214 | }; |
| 215 | |
| 216 | /* |
| 217 | * inode to private data |
| 218 | * |
| 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. |
| 222 | */ |
| 223 | static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode) |
| 224 | { |
| 225 | return container_of(inode, struct sdcardfs_inode_info, vfs_inode); |
| 226 | } |
| 227 | |
| 228 | /* dentry to private data */ |
| 229 | #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata) |
| 230 | |
| 231 | /* superblock to private data */ |
| 232 | #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info) |
| 233 | |
| 234 | /* file to private Data */ |
| 235 | #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data)) |
| 236 | |
| 237 | /* file to lower file */ |
| 238 | static inline struct file *sdcardfs_lower_file(const struct file *f) |
| 239 | { |
| 240 | return SDCARDFS_F(f)->lower_file; |
| 241 | } |
| 242 | |
| 243 | static inline void sdcardfs_set_lower_file(struct file *f, struct file *val) |
| 244 | { |
| 245 | SDCARDFS_F(f)->lower_file = val; |
| 246 | } |
| 247 | |
| 248 | /* inode to lower inode. */ |
| 249 | static inline struct inode *sdcardfs_lower_inode(const struct inode *i) |
| 250 | { |
| 251 | return SDCARDFS_I(i)->lower_inode; |
| 252 | } |
| 253 | |
| 254 | static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val) |
| 255 | { |
| 256 | SDCARDFS_I(i)->lower_inode = val; |
| 257 | } |
| 258 | |
| 259 | /* superblock to lower superblock */ |
| 260 | static inline struct super_block *sdcardfs_lower_super( |
| 261 | const struct super_block *sb) |
| 262 | { |
| 263 | return SDCARDFS_SB(sb)->lower_sb; |
| 264 | } |
| 265 | |
| 266 | static inline void sdcardfs_set_lower_super(struct super_block *sb, |
| 267 | struct super_block *val) |
| 268 | { |
| 269 | SDCARDFS_SB(sb)->lower_sb = val; |
| 270 | } |
| 271 | |
| 272 | /* path based (dentry/mnt) macros */ |
| 273 | static inline void pathcpy(struct path *dst, const struct path *src) |
| 274 | { |
| 275 | dst->dentry = src->dentry; |
| 276 | dst->mnt = src->mnt; |
| 277 | } |
| 278 | |
| 279 | /* sdcardfs_get_pname functions calls path_get() |
| 280 | * therefore, the caller must call "proper" path_put functions |
| 281 | */ |
| 282 | #define SDCARDFS_DENT_FUNC(pname) \ |
| 283 | static inline void sdcardfs_get_##pname(const struct dentry *dent, \ |
| 284 | struct path *pname) \ |
| 285 | { \ |
| 286 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 287 | pathcpy(pname, &SDCARDFS_D(dent)->pname); \ |
| 288 | path_get(pname); \ |
| 289 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 290 | return; \ |
| 291 | } \ |
| 292 | static inline void sdcardfs_put_##pname(const struct dentry *dent, \ |
| 293 | struct path *pname) \ |
| 294 | { \ |
| 295 | path_put(pname); \ |
| 296 | return; \ |
| 297 | } \ |
| 298 | static inline void sdcardfs_set_##pname(const struct dentry *dent, \ |
| 299 | struct path *pname) \ |
| 300 | { \ |
| 301 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 302 | pathcpy(&SDCARDFS_D(dent)->pname, pname); \ |
| 303 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 304 | return; \ |
| 305 | } \ |
| 306 | static inline void sdcardfs_reset_##pname(const struct dentry *dent) \ |
| 307 | { \ |
| 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); \ |
| 312 | return; \ |
| 313 | } \ |
| 314 | static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \ |
| 315 | { \ |
| 316 | struct path pname; \ |
| 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); \ |
| 323 | path_put(&pname); \ |
| 324 | } else \ |
| 325 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 326 | return; \ |
| 327 | } |
| 328 | |
| 329 | SDCARDFS_DENT_FUNC(lower_path) |
| 330 | SDCARDFS_DENT_FUNC(orig_path) |
| 331 | |
| 332 | static inline int has_graft_path(const struct dentry *dent) |
| 333 | { |
| 334 | int ret = 0; |
| 335 | |
| 336 | spin_lock(&SDCARDFS_D(dent)->lock); |
| 337 | if (SDCARDFS_D(dent)->orig_path.dentry != NULL) |
| 338 | ret = 1; |
| 339 | spin_unlock(&SDCARDFS_D(dent)->lock); |
| 340 | |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | static inline void sdcardfs_get_real_lower(const struct dentry *dent, |
| 345 | struct path *real_lower) |
| 346 | { |
| 347 | /* in case of a local obb dentry |
| 348 | * the orig_path should be returned |
| 349 | */ |
| 350 | if(has_graft_path(dent)) |
| 351 | sdcardfs_get_orig_path(dent, real_lower); |
| 352 | else |
| 353 | sdcardfs_get_lower_path(dent, real_lower); |
| 354 | } |
| 355 | |
| 356 | static inline void sdcardfs_put_real_lower(const struct dentry *dent, |
| 357 | struct path *real_lower) |
| 358 | { |
| 359 | if(has_graft_path(dent)) |
| 360 | sdcardfs_put_orig_path(dent, real_lower); |
| 361 | else |
| 362 | sdcardfs_put_lower_path(dent, real_lower); |
| 363 | } |
| 364 | |
| 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); |
| 375 | |
| 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); |
| 385 | |
| 386 | /* locking helpers */ |
| 387 | static inline struct dentry *lock_parent(struct dentry *dentry) |
| 388 | { |
| 389 | struct dentry *dir = dget_parent(dentry); |
| 390 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
| 391 | return dir; |
| 392 | } |
| 393 | |
| 394 | static inline void unlock_dir(struct dentry *dir) |
| 395 | { |
| 396 | mutex_unlock(&dir->d_inode->i_mutex); |
| 397 | dput(dir); |
| 398 | } |
| 399 | |
| 400 | static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode) |
| 401 | { |
| 402 | int err; |
| 403 | struct dentry *dent; |
| 404 | struct iattr attrs; |
| 405 | struct nameidata nd; |
| 406 | |
| 407 | err = kern_path_parent(path_s, &nd); |
| 408 | if (err) { |
| 409 | if (err == -EEXIST) |
| 410 | err = 0; |
| 411 | goto out; |
| 412 | } |
| 413 | |
| 414 | dent = lookup_create(&nd, 1); |
| 415 | if (IS_ERR(dent)) { |
| 416 | err = PTR_ERR(dent); |
| 417 | if (err == -EEXIST) |
| 418 | err = 0; |
| 419 | goto out_unlock; |
| 420 | } |
| 421 | |
| 422 | err = vfs_mkdir(nd.path.dentry->d_inode, dent, mode); |
| 423 | if (err) { |
| 424 | if (err == -EEXIST) |
| 425 | err = 0; |
| 426 | goto out_dput; |
| 427 | } |
| 428 | |
| 429 | attrs.ia_uid = uid; |
| 430 | attrs.ia_gid = gid; |
| 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); |
| 435 | |
| 436 | out_dput: |
| 437 | dput(dent); |
| 438 | |
| 439 | out_unlock: |
| 440 | /* parent dentry locked by lookup_create */ |
| 441 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 442 | path_put(&nd.path); |
| 443 | |
| 444 | out: |
| 445 | return err; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Return 1, if a disk has enough free space, otherwise 0. |
| 450 | * We assume that any files can not be overwritten. |
| 451 | */ |
| 452 | static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir) |
| 453 | { |
| 454 | int err; |
| 455 | struct path lower_path; |
| 456 | struct kstatfs statfs; |
| 457 | u64 avail; |
| 458 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
| 459 | |
| 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); |
| 465 | |
| 466 | if (unlikely(err)) |
| 467 | return 0; |
| 468 | |
| 469 | /* Invalid statfs informations. */ |
| 470 | if (unlikely(statfs.f_bsize == 0)) |
| 471 | return 0; |
| 472 | |
| 473 | /* if you are checking directory, set size to f_bsize. */ |
| 474 | if (unlikely(dir)) |
| 475 | size = statfs.f_bsize; |
| 476 | |
| 477 | /* available size */ |
| 478 | avail = statfs.f_bavail * statfs.f_bsize; |
| 479 | |
| 480 | /* not enough space */ |
| 481 | if ((u64)size > avail) |
| 482 | return 0; |
| 483 | |
| 484 | /* enough space */ |
| 485 | if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024)) |
| 486 | return 1; |
| 487 | |
| 488 | return 0; |
| 489 | } else |
| 490 | return 1; |
| 491 | } |
| 492 | |
| 493 | #endif /* not _SDCARDFS_H_ */ |