| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * fs/sdcardfs/main.c |
| 3 | * |
| 4 | * Copyright (c) 2013 Samsung Electronics Co. Ltd |
| 5 | * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun, |
| 6 | * Sunghwan Yun, Sungjong Seo |
| 7 | * |
| 8 | * This program has been developed as a stackable file system based on |
| 9 | * the WrapFS which written by |
| 10 | * |
| 11 | * Copyright (c) 1998-2011 Erez Zadok |
| 12 | * Copyright (c) 2009 Shrikar Archak |
| 13 | * Copyright (c) 2003-2011 Stony Brook University |
| 14 | * Copyright (c) 2003-2011 The Research Foundation of SUNY |
| 15 | * |
| 16 | * This file is dual licensed. It may be redistributed and/or modified |
| 17 | * under the terms of the Apache 2.0 License OR version 2 of the GNU |
| 18 | * General Public License. |
| 19 | */ |
| 20 | |
| 21 | #include "sdcardfs.h" |
| 22 | #include <linux/fscrypt.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/parser.h> |
| 26 | |
| 27 | enum { |
| 28 | Opt_fsuid, |
| 29 | Opt_fsgid, |
| 30 | Opt_gid, |
| 31 | Opt_debug, |
| 32 | Opt_mask, |
| 33 | Opt_multiuser, |
| 34 | Opt_userid, |
| 35 | Opt_reserved_mb, |
| 36 | Opt_gid_derivation, |
| 37 | Opt_default_normal, |
| 38 | Opt_nocache, |
| 39 | Opt_unshared_obb, |
| 40 | Opt_err, |
| 41 | }; |
| 42 | |
| 43 | static const match_table_t sdcardfs_tokens = { |
| 44 | {Opt_fsuid, "fsuid=%u"}, |
| 45 | {Opt_fsgid, "fsgid=%u"}, |
| 46 | {Opt_gid, "gid=%u"}, |
| 47 | {Opt_debug, "debug"}, |
| 48 | {Opt_mask, "mask=%u"}, |
| 49 | {Opt_userid, "userid=%d"}, |
| 50 | {Opt_multiuser, "multiuser"}, |
| 51 | {Opt_gid_derivation, "derive_gid"}, |
| 52 | {Opt_default_normal, "default_normal"}, |
| 53 | {Opt_unshared_obb, "unshared_obb"}, |
| 54 | {Opt_reserved_mb, "reserved_mb=%u"}, |
| 55 | {Opt_nocache, "nocache"}, |
| 56 | {Opt_err, NULL} |
| 57 | }; |
| 58 | |
| 59 | static int parse_options(struct super_block *sb, char *options, int silent, |
| 60 | int *debug, struct sdcardfs_vfsmount_options *vfsopts, |
| 61 | struct sdcardfs_mount_options *opts) |
| 62 | { |
| 63 | char *p; |
| 64 | substring_t args[MAX_OPT_ARGS]; |
| 65 | int option; |
| 66 | |
| 67 | /* by default, we use AID_MEDIA_RW as uid, gid */ |
| 68 | opts->fs_low_uid = AID_MEDIA_RW; |
| 69 | opts->fs_low_gid = AID_MEDIA_RW; |
| 70 | vfsopts->mask = 0; |
| 71 | opts->multiuser = false; |
| 72 | opts->fs_user_id = 0; |
| 73 | vfsopts->gid = 0; |
| 74 | /* by default, 0MB is reserved */ |
| 75 | opts->reserved_mb = 0; |
| 76 | /* by default, gid derivation is off */ |
| 77 | opts->gid_derivation = false; |
| 78 | opts->default_normal = false; |
| 79 | opts->nocache = false; |
| 80 | |
| 81 | *debug = 0; |
| 82 | |
| 83 | if (!options) |
| 84 | return 0; |
| 85 | |
| 86 | while ((p = strsep(&options, ",")) != NULL) { |
| 87 | int token; |
| 88 | |
| 89 | if (!*p) |
| 90 | continue; |
| 91 | |
| 92 | token = match_token(p, sdcardfs_tokens, args); |
| 93 | |
| 94 | switch (token) { |
| 95 | case Opt_debug: |
| 96 | *debug = 1; |
| 97 | break; |
| 98 | case Opt_fsuid: |
| 99 | if (match_int(&args[0], &option)) |
| 100 | return 0; |
| 101 | opts->fs_low_uid = option; |
| 102 | break; |
| 103 | case Opt_fsgid: |
| 104 | if (match_int(&args[0], &option)) |
| 105 | return 0; |
| 106 | opts->fs_low_gid = option; |
| 107 | break; |
| 108 | case Opt_gid: |
| 109 | if (match_int(&args[0], &option)) |
| 110 | return 0; |
| 111 | vfsopts->gid = option; |
| 112 | break; |
| 113 | case Opt_userid: |
| 114 | if (match_int(&args[0], &option)) |
| 115 | return 0; |
| 116 | opts->fs_user_id = option; |
| 117 | break; |
| 118 | case Opt_mask: |
| 119 | if (match_int(&args[0], &option)) |
| 120 | return 0; |
| 121 | vfsopts->mask = option; |
| 122 | break; |
| 123 | case Opt_multiuser: |
| 124 | opts->multiuser = true; |
| 125 | break; |
| 126 | case Opt_reserved_mb: |
| 127 | if (match_int(&args[0], &option)) |
| 128 | return 0; |
| 129 | opts->reserved_mb = option; |
| 130 | break; |
| 131 | case Opt_gid_derivation: |
| 132 | opts->gid_derivation = true; |
| 133 | break; |
| 134 | case Opt_default_normal: |
| 135 | opts->default_normal = true; |
| 136 | break; |
| 137 | case Opt_nocache: |
| 138 | opts->nocache = true; |
| 139 | break; |
| 140 | case Opt_unshared_obb: |
| 141 | opts->unshared_obb = true; |
| 142 | break; |
| 143 | /* unknown option */ |
| 144 | default: |
| 145 | if (!silent) |
| 146 | pr_err("Unrecognized mount option \"%s\" or missing value", p); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (*debug) { |
| 152 | pr_info("sdcardfs : options - debug:%d\n", *debug); |
| 153 | pr_info("sdcardfs : options - uid:%d\n", |
| 154 | opts->fs_low_uid); |
| 155 | pr_info("sdcardfs : options - gid:%d\n", |
| 156 | opts->fs_low_gid); |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int parse_options_remount(struct super_block *sb, char *options, int silent, |
| 163 | struct sdcardfs_vfsmount_options *vfsopts) |
| 164 | { |
| 165 | char *p; |
| 166 | substring_t args[MAX_OPT_ARGS]; |
| 167 | int option; |
| 168 | int debug; |
| 169 | |
| 170 | if (!options) |
| 171 | return 0; |
| 172 | |
| 173 | while ((p = strsep(&options, ",")) != NULL) { |
| 174 | int token; |
| 175 | |
| 176 | if (!*p) |
| 177 | continue; |
| 178 | |
| 179 | token = match_token(p, sdcardfs_tokens, args); |
| 180 | |
| 181 | switch (token) { |
| 182 | case Opt_debug: |
| 183 | debug = 1; |
| 184 | break; |
| 185 | case Opt_gid: |
| 186 | if (match_int(&args[0], &option)) |
| 187 | return 0; |
| 188 | vfsopts->gid = option; |
| 189 | |
| 190 | break; |
| 191 | case Opt_mask: |
| 192 | if (match_int(&args[0], &option)) |
| 193 | return 0; |
| 194 | vfsopts->mask = option; |
| 195 | break; |
| 196 | case Opt_unshared_obb: |
| 197 | case Opt_default_normal: |
| 198 | case Opt_multiuser: |
| 199 | case Opt_userid: |
| 200 | case Opt_fsuid: |
| 201 | case Opt_fsgid: |
| 202 | case Opt_reserved_mb: |
| 203 | case Opt_gid_derivation: |
| 204 | if (!silent) |
| 205 | pr_warn("Option \"%s\" can't be changed during remount\n", p); |
| 206 | break; |
| 207 | /* unknown option */ |
| 208 | default: |
| 209 | if (!silent) |
| 210 | pr_err("Unrecognized mount option \"%s\" or missing value", p); |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (debug) { |
| 216 | pr_info("sdcardfs : options - debug:%d\n", debug); |
| 217 | pr_info("sdcardfs : options - gid:%d\n", vfsopts->gid); |
| 218 | pr_info("sdcardfs : options - mask:%d\n", vfsopts->mask); |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | #if 0 |
| 225 | /* |
| 226 | * our custom d_alloc_root work-alike |
| 227 | * |
| 228 | * we can't use d_alloc_root if we want to use our own interpose function |
| 229 | * unchanged, so we simply call our own "fake" d_alloc_root |
| 230 | */ |
| 231 | static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb) |
| 232 | { |
| 233 | struct dentry *ret = NULL; |
| 234 | |
| 235 | if (sb) { |
| 236 | static const struct qstr name = { |
| 237 | .name = "/", |
| 238 | .len = 1 |
| 239 | }; |
| 240 | |
| 241 | ret = d_alloc(NULL, &name); |
| 242 | if (ret) { |
| 243 | d_set_d_op(ret, &sdcardfs_ci_dops); |
| 244 | ret->d_sb = sb; |
| 245 | ret->d_parent = ret; |
| 246 | } |
| 247 | } |
| 248 | return ret; |
| 249 | } |
| 250 | #endif |
| 251 | |
| 252 | DEFINE_MUTEX(sdcardfs_super_list_lock); |
| 253 | EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock); |
| 254 | LIST_HEAD(sdcardfs_super_list); |
| 255 | EXPORT_SYMBOL_GPL(sdcardfs_super_list); |
| 256 | |
| 257 | /* |
| 258 | * There is no need to lock the sdcardfs_super_info's rwsem as there is no |
| 259 | * way anyone can have a reference to the superblock at this point in time. |
| 260 | */ |
| 261 | static int sdcardfs_read_super(struct vfsmount *mnt, struct super_block *sb, |
| 262 | const char *dev_name, void *raw_data, int silent) |
| 263 | { |
| 264 | int err = 0; |
| 265 | int debug; |
| 266 | struct super_block *lower_sb; |
| 267 | struct path lower_path; |
| 268 | struct sdcardfs_sb_info *sb_info; |
| 269 | struct sdcardfs_vfsmount_options *mnt_opt = mnt->data; |
| 270 | struct inode *inode; |
| 271 | |
| 272 | pr_info("sdcardfs version 2.0\n"); |
| 273 | |
| 274 | if (!dev_name) { |
| 275 | pr_err("sdcardfs: read_super: missing dev_name argument\n"); |
| 276 | err = -EINVAL; |
| 277 | goto out; |
| 278 | } |
| 279 | |
| 280 | pr_info("sdcardfs: dev_name -> %s\n", dev_name); |
| 281 | pr_info("sdcardfs: options -> %s\n", (char *)raw_data); |
| 282 | pr_info("sdcardfs: mnt -> %p\n", mnt); |
| 283 | |
| 284 | /* parse lower path */ |
| 285 | err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, |
| 286 | &lower_path); |
| 287 | if (err) { |
| 288 | pr_err("sdcardfs: error accessing lower directory '%s'\n", dev_name); |
| 289 | goto out; |
| 290 | } |
| 291 | |
| 292 | /* allocate superblock private data */ |
| 293 | sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL); |
| 294 | if (!SDCARDFS_SB(sb)) { |
| 295 | pr_crit("sdcardfs: read_super: out of memory\n"); |
| 296 | err = -ENOMEM; |
| 297 | goto out_free; |
| 298 | } |
| 299 | |
| 300 | sb_info = sb->s_fs_info; |
| 301 | /* parse options */ |
| 302 | err = parse_options(sb, raw_data, silent, &debug, mnt_opt, &sb_info->options); |
| 303 | if (err) { |
| 304 | pr_err("sdcardfs: invalid options\n"); |
| 305 | goto out_freesbi; |
| 306 | } |
| 307 | |
| 308 | /* set the lower superblock field of upper superblock */ |
| 309 | lower_sb = lower_path.dentry->d_sb; |
| 310 | atomic_inc(&lower_sb->s_active); |
| 311 | sdcardfs_set_lower_super(sb, lower_sb); |
| 312 | |
| 313 | sb->s_stack_depth = lower_sb->s_stack_depth + 1; |
| 314 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 315 | pr_err("sdcardfs: maximum fs stacking depth exceeded\n"); |
| 316 | err = -EINVAL; |
| 317 | goto out_sput; |
| 318 | } |
| 319 | |
| 320 | /* inherit maxbytes from lower file system */ |
| 321 | sb->s_maxbytes = lower_sb->s_maxbytes; |
| 322 | |
| 323 | /* |
| 324 | * Our c/m/atime granularity is 1 ns because we may stack on file |
| 325 | * systems whose granularity is as good. |
| 326 | */ |
| 327 | sb->s_time_gran = 1; |
| 328 | |
| 329 | sb->s_magic = SDCARDFS_SUPER_MAGIC; |
| 330 | sb->s_op = &sdcardfs_sops; |
| 331 | |
| 332 | /* get a new inode and allocate our root dentry */ |
| 333 | inode = sdcardfs_iget(sb, d_inode(lower_path.dentry), 0); |
| 334 | if (IS_ERR(inode)) { |
| 335 | err = PTR_ERR(inode); |
| 336 | goto out_sput; |
| 337 | } |
| 338 | sb->s_root = d_make_root(inode); |
| 339 | if (!sb->s_root) { |
| 340 | err = -ENOMEM; |
| 341 | goto out_sput; |
| 342 | } |
| 343 | d_set_d_op(sb->s_root, &sdcardfs_ci_dops); |
| 344 | |
| 345 | /* link the upper and lower dentries */ |
| 346 | sb->s_root->d_fsdata = NULL; |
| 347 | err = new_dentry_private_data(sb->s_root); |
| 348 | if (err) |
| 349 | goto out_freeroot; |
| 350 | |
| 351 | /* set the lower dentries for s_root */ |
| 352 | sdcardfs_set_lower_path(sb->s_root, &lower_path); |
| 353 | |
| 354 | /* |
| 355 | * No need to call interpose because we already have a positive |
| 356 | * dentry, which was instantiated by d_make_root. Just need to |
| 357 | * d_rehash it. |
| 358 | */ |
| 359 | d_rehash(sb->s_root); |
| 360 | |
| 361 | /* setup permission policy */ |
| 362 | sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL); |
| 363 | mutex_lock(&sdcardfs_super_list_lock); |
| 364 | if (sb_info->options.multiuser) { |
| 365 | setup_derived_state(d_inode(sb->s_root), PERM_PRE_ROOT, |
| 366 | sb_info->options.fs_user_id, AID_ROOT); |
| 367 | snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name); |
| 368 | } else { |
| 369 | setup_derived_state(d_inode(sb->s_root), PERM_ROOT, |
| 370 | sb_info->options.fs_user_id, AID_ROOT); |
| 371 | snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name); |
| 372 | } |
| 373 | fixup_tmp_permissions(d_inode(sb->s_root)); |
| 374 | sb_info->sb = sb; |
| 375 | list_add(&sb_info->list, &sdcardfs_super_list); |
| 376 | mutex_unlock(&sdcardfs_super_list_lock); |
| 377 | |
| 378 | sb_info->fscrypt_nb.notifier_call = sdcardfs_on_fscrypt_key_removed; |
| 379 | fscrypt_register_key_removal_notifier(&sb_info->fscrypt_nb); |
| 380 | |
| 381 | if (!silent) |
| 382 | pr_info("sdcardfs: mounted on top of %s type %s\n", |
| 383 | dev_name, lower_sb->s_type->name); |
| 384 | goto out; /* all is well */ |
| 385 | |
| 386 | /* no longer needed: free_dentry_private_data(sb->s_root); */ |
| 387 | out_freeroot: |
| 388 | dput(sb->s_root); |
| 389 | sb->s_root = NULL; |
| 390 | out_sput: |
| 391 | /* drop refs we took earlier */ |
| 392 | atomic_dec(&lower_sb->s_active); |
| 393 | out_freesbi: |
| 394 | kfree(SDCARDFS_SB(sb)); |
| 395 | sb->s_fs_info = NULL; |
| 396 | out_free: |
| 397 | path_put(&lower_path); |
| 398 | |
| 399 | out: |
| 400 | return err; |
| 401 | } |
| 402 | |
| 403 | struct sdcardfs_mount_private { |
| 404 | struct vfsmount *mnt; |
| 405 | const char *dev_name; |
| 406 | void *raw_data; |
| 407 | }; |
| 408 | |
| 409 | static int __sdcardfs_fill_super( |
| 410 | struct super_block *sb, |
| 411 | void *_priv, int silent) |
| 412 | { |
| 413 | struct sdcardfs_mount_private *priv = _priv; |
| 414 | |
| 415 | return sdcardfs_read_super(priv->mnt, |
| 416 | sb, priv->dev_name, priv->raw_data, silent); |
| 417 | } |
| 418 | |
| 419 | static struct dentry *sdcardfs_mount(struct vfsmount *mnt, |
| 420 | struct file_system_type *fs_type, int flags, |
| 421 | const char *dev_name, void *raw_data) |
| 422 | { |
| 423 | struct sdcardfs_mount_private priv = { |
| 424 | .mnt = mnt, |
| 425 | .dev_name = dev_name, |
| 426 | .raw_data = raw_data |
| 427 | }; |
| 428 | |
| 429 | return mount_nodev(fs_type, flags, |
| 430 | &priv, __sdcardfs_fill_super); |
| 431 | } |
| 432 | |
| 433 | static struct dentry *sdcardfs_mount_wrn(struct file_system_type *fs_type, |
| 434 | int flags, const char *dev_name, void *raw_data) |
| 435 | { |
| 436 | WARN(1, "sdcardfs does not support mount. Use mount2.\n"); |
| 437 | return ERR_PTR(-EINVAL); |
| 438 | } |
| 439 | |
| 440 | void *sdcardfs_alloc_mnt_data(void) |
| 441 | { |
| 442 | return kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL); |
| 443 | } |
| 444 | |
| 445 | void sdcardfs_kill_sb(struct super_block *sb) |
| 446 | { |
| 447 | struct sdcardfs_sb_info *sbi; |
| 448 | |
| 449 | if (sb->s_magic == SDCARDFS_SUPER_MAGIC && sb->s_fs_info) { |
| 450 | sbi = SDCARDFS_SB(sb); |
| 451 | |
| 452 | fscrypt_unregister_key_removal_notifier(&sbi->fscrypt_nb); |
| 453 | |
| 454 | mutex_lock(&sdcardfs_super_list_lock); |
| 455 | list_del(&sbi->list); |
| 456 | mutex_unlock(&sdcardfs_super_list_lock); |
| 457 | } |
| 458 | kill_anon_super(sb); |
| 459 | } |
| 460 | |
| 461 | static struct file_system_type sdcardfs_fs_type = { |
| 462 | .owner = THIS_MODULE, |
| 463 | .name = SDCARDFS_NAME, |
| 464 | .mount = sdcardfs_mount_wrn, |
| 465 | .mount2 = sdcardfs_mount, |
| 466 | .alloc_mnt_data = sdcardfs_alloc_mnt_data, |
| 467 | .kill_sb = sdcardfs_kill_sb, |
| 468 | .fs_flags = 0, |
| 469 | }; |
| 470 | MODULE_ALIAS_FS(SDCARDFS_NAME); |
| 471 | |
| 472 | static int __init init_sdcardfs_fs(void) |
| 473 | { |
| 474 | int err; |
| 475 | |
| 476 | pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n"); |
| 477 | |
| 478 | err = sdcardfs_init_inode_cache(); |
| 479 | if (err) |
| 480 | goto out; |
| 481 | err = sdcardfs_init_dentry_cache(); |
| 482 | if (err) |
| 483 | goto out; |
| 484 | err = packagelist_init(); |
| 485 | if (err) |
| 486 | goto out; |
| 487 | err = register_filesystem(&sdcardfs_fs_type); |
| 488 | out: |
| 489 | if (err) { |
| 490 | sdcardfs_destroy_inode_cache(); |
| 491 | sdcardfs_destroy_dentry_cache(); |
| 492 | packagelist_exit(); |
| 493 | } |
| 494 | return err; |
| 495 | } |
| 496 | |
| 497 | static void __exit exit_sdcardfs_fs(void) |
| 498 | { |
| 499 | sdcardfs_destroy_inode_cache(); |
| 500 | sdcardfs_destroy_dentry_cache(); |
| 501 | packagelist_exit(); |
| 502 | unregister_filesystem(&sdcardfs_fs_type); |
| 503 | pr_info("Completed sdcardfs module unload\n"); |
| 504 | } |
| 505 | |
| 506 | /* Original wrapfs authors */ |
| 507 | MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University (http://www.fsl.cs.sunysb.edu/)"); |
| 508 | |
| 509 | /* Original sdcardfs authors */ |
| 510 | MODULE_AUTHOR("Woojoong Lee, Daeho Jeong, Kitae Lee, Yeongjin Gil System Memory Lab., Samsung Electronics"); |
| 511 | |
| 512 | /* Current maintainer */ |
| 513 | MODULE_AUTHOR("Daniel Rosenberg, Google"); |
| 514 | MODULE_DESCRIPTION("Sdcardfs " SDCARDFS_VERSION); |
| 515 | MODULE_LICENSE("GPL"); |
| 516 | |
| 517 | module_init(init_sdcardfs_fs); |
| 518 | module_exit(exit_sdcardfs_fs); |