4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
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
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.
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/parser.h>
38 static const match_table_t sdcardfs_tokens = {
41 {Opt_wgid, "wgid=%u"},
44 {Opt_derive, "derive=%s"},
45 {Opt_lower_fs, "lower_fs=%s"},
46 {Opt_reserved_mb, "reserved_mb=%u"},
50 static int parse_options(struct super_block *sb, char *options, int silent,
51 int *debug, struct sdcardfs_mount_options *opts)
54 substring_t args[MAX_OPT_ARGS];
58 /* by default, we use AID_MEDIA_RW as uid, gid */
59 opts->fs_low_uid = AID_MEDIA_RW;
60 opts->fs_low_gid = AID_MEDIA_RW;
61 /* by default, we use AID_SDCARD_RW as write_gid */
62 opts->write_gid = AID_SDCARD_RW;
63 /* default permission policy
64 * (DERIVE_NONE | DERIVE_LEGACY | DERIVE_UNIFIED) */
65 opts->derive = DERIVE_NONE;
66 opts->split_perms = 0;
67 /* by default, we use LOWER_FS_EXT4 as lower fs type */
68 opts->lower_fs = LOWER_FS_EXT4;
69 /* by default, 0MB is reserved */
70 opts->reserved_mb = 0;
77 while ((p = strsep(&options, ",")) != NULL) {
82 token = match_token(p, sdcardfs_tokens, args);
89 if (match_int(&args[0], &option))
91 opts->fs_low_uid = option;
94 if (match_int(&args[0], &option))
96 opts->fs_low_gid = option;
99 if (match_int(&args[0], &option))
101 opts->write_gid = option;
107 string_option = match_strdup(&args[0]);
108 if (!strcmp("none", string_option)) {
109 opts->derive = DERIVE_NONE;
110 } else if (!strcmp("legacy", string_option)) {
111 opts->derive = DERIVE_LEGACY;
112 } else if (!strcmp("unified", string_option)) {
113 opts->derive = DERIVE_UNIFIED;
115 kfree(string_option);
118 kfree(string_option);
121 string_option = match_strdup(&args[0]);
122 if (!strcmp("ext4", string_option)) {
123 opts->lower_fs = LOWER_FS_EXT4;
124 } else if (!strcmp("fat", string_option)) {
125 opts->lower_fs = LOWER_FS_FAT;
127 kfree(string_option);
130 kfree(string_option);
132 case Opt_reserved_mb:
133 if (match_int(&args[0], &option))
135 opts->reserved_mb = option;
141 printk( KERN_ERR "Unrecognized mount option \"%s\" "
142 "or missing value", p);
149 printk( KERN_INFO "sdcardfs : options - debug:%d\n", *debug);
150 printk( KERN_INFO "sdcardfs : options - uid:%d\n",
152 printk( KERN_INFO "sdcardfs : options - gid:%d\n",
161 * our custom d_alloc_root work-alike
163 * we can't use d_alloc_root if we want to use our own interpose function
164 * unchanged, so we simply call our own "fake" d_alloc_root
166 static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
168 struct dentry *ret = NULL;
171 static const struct qstr name = {
176 ret = d_alloc(NULL, &name);
178 d_set_d_op(ret, &sdcardfs_ci_dops);
188 * There is no need to lock the sdcardfs_super_info's rwsem as there is no
189 * way anyone can have a reference to the superblock at this point in time.
191 static int sdcardfs_read_super(struct super_block *sb, const char *dev_name,
192 void *raw_data, int silent)
196 struct super_block *lower_sb;
197 struct path lower_path;
198 struct sdcardfs_sb_info *sb_info;
202 printk(KERN_INFO "sdcardfs version 2.0\n");
206 "sdcardfs: read_super: missing dev_name argument\n");
211 printk(KERN_INFO "sdcardfs: dev_name -> %s\n", dev_name);
212 printk(KERN_INFO "sdcardfs: options -> %s\n", (char *)raw_data);
214 /* parse lower path */
215 err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
218 printk(KERN_ERR "sdcardfs: error accessing "
219 "lower directory '%s'\n", dev_name);
223 /* allocate superblock private data */
224 sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL);
225 if (!SDCARDFS_SB(sb)) {
226 printk(KERN_CRIT "sdcardfs: read_super: out of memory\n");
231 sb_info = sb->s_fs_info;
234 err = parse_options(sb, raw_data, silent, &debug, &sb_info->options);
236 printk(KERN_ERR "sdcardfs: invalid options\n");
240 if (sb_info->options.derive != DERIVE_NONE) {
241 pkgl_id = packagelist_create(sb_info->options.write_gid);
245 sb_info->pkgl_id = pkgl_id;
248 /* set the lower superblock field of upper superblock */
249 lower_sb = lower_path.dentry->d_sb;
250 atomic_inc(&lower_sb->s_active);
251 sdcardfs_set_lower_super(sb, lower_sb);
253 /* inherit maxbytes from lower file system */
254 sb->s_maxbytes = lower_sb->s_maxbytes;
257 * Our c/m/atime granularity is 1 ns because we may stack on file
258 * systems whose granularity is as good.
262 sb->s_magic = SDCARDFS_SUPER_MAGIC;
263 sb->s_op = &sdcardfs_sops;
265 /* get a new inode and allocate our root dentry */
266 inode = sdcardfs_iget(sb, lower_path.dentry->d_inode);
268 err = PTR_ERR(inode);
271 sb->s_root = d_make_root(inode);
276 d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
278 /* link the upper and lower dentries */
279 sb->s_root->d_fsdata = NULL;
280 err = new_dentry_private_data(sb->s_root);
284 /* set the lower dentries for s_root */
285 sdcardfs_set_lower_path(sb->s_root, &lower_path);
288 * No need to call interpose because we already have a positive
289 * dentry, which was instantiated by d_make_root. Just need to
292 d_rehash(sb->s_root);
294 /* setup permission policy */
295 switch(sb_info->options.derive) {
297 setup_derived_state(sb->s_root->d_inode,
298 PERM_ROOT, 0, AID_ROOT, AID_SDCARD_RW, 00775);
299 sb_info->obbpath_s = NULL;
302 /* Legacy behavior used to support internal multiuser layout which
303 * places user_id at the top directory level, with the actual roots
304 * just below that. Shared OBB path is also at top level. */
305 setup_derived_state(sb->s_root->d_inode,
306 PERM_LEGACY_PRE_ROOT, 0, AID_ROOT, AID_SDCARD_R, 00771);
307 /* initialize the obbpath string and lookup the path
308 * sb_info->obb_path will be deactivated by path_put
309 * on sdcardfs_put_super */
310 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
311 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
312 err = prepare_dir(sb_info->obbpath_s,
313 sb_info->options.fs_low_uid,
314 sb_info->options.fs_low_gid, 00755);
316 printk(KERN_ERR "sdcardfs: %s: %d, error on creating %s\n",
317 __func__,__LINE__, sb_info->obbpath_s);
320 /* Unified multiuser layout which places secondary user_id under
321 * /Android/user and shared OBB path under /Android/obb. */
322 setup_derived_state(sb->s_root->d_inode,
323 PERM_ROOT, 0, AID_ROOT, AID_SDCARD_R, 00771);
325 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
326 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
329 fix_derived_permission(sb->s_root->d_inode);
332 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
333 dev_name, lower_sb->s_type->name);
334 goto out; /* all is well */
336 /* no longer needed: free_dentry_private_data(sb->s_root); */
342 /* drop refs we took earlier */
343 atomic_dec(&lower_sb->s_active);
344 packagelist_destroy(sb_info->pkgl_id);
346 kfree(SDCARDFS_SB(sb));
347 sb->s_fs_info = NULL;
349 path_put(&lower_path);
355 /* A feature which supports mount_nodev() with options */
356 static struct dentry *mount_nodev_with_options(struct file_system_type *fs_type,
357 int flags, const char *dev_name, void *data,
358 int (*fill_super)(struct super_block *, const char *, void *, int))
362 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
369 error = fill_super(s, dev_name, data, flags & MS_SILENT ? 1 : 0);
371 deactivate_locked_super(s);
372 return ERR_PTR(error);
374 s->s_flags |= MS_ACTIVE;
375 return dget(s->s_root);
378 struct dentry *sdcardfs_mount(struct file_system_type *fs_type, int flags,
379 const char *dev_name, void *raw_data)
382 * dev_name is a lower_path_name,
383 * raw_data is a option string.
385 return mount_nodev_with_options(fs_type, flags, dev_name,
386 raw_data, sdcardfs_read_super);
389 static struct file_system_type sdcardfs_fs_type = {
390 .owner = THIS_MODULE,
391 .name = SDCARDFS_NAME,
392 .mount = sdcardfs_mount,
393 .kill_sb = generic_shutdown_super,
397 static int __init init_sdcardfs_fs(void)
401 pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
403 err = sdcardfs_init_inode_cache();
406 err = sdcardfs_init_dentry_cache();
409 err = packagelist_init();
412 err = register_filesystem(&sdcardfs_fs_type);
415 sdcardfs_destroy_inode_cache();
416 sdcardfs_destroy_dentry_cache();
422 static void __exit exit_sdcardfs_fs(void)
424 sdcardfs_destroy_inode_cache();
425 sdcardfs_destroy_dentry_cache();
427 unregister_filesystem(&sdcardfs_fs_type);
428 pr_info("Completed sdcardfs module unload\n");
431 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
432 " (http://www.fsl.cs.sunysb.edu/)");
433 MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
434 " (http://wrapfs.filesystems.org/)");
435 MODULE_LICENSE("GPL");
437 module_init(init_sdcardfs_fs);
438 module_exit(exit_sdcardfs_fs);