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",
160 * our custom d_alloc_root work-alike
162 * we can't use d_alloc_root if we want to use our own interpose function
163 * unchanged, so we simply call our own "fake" d_alloc_root
165 static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
167 struct dentry *ret = NULL;
170 static const struct qstr name = {
175 ret = d_alloc(NULL, &name);
177 d_set_d_op(ret, &sdcardfs_ci_dops);
186 * There is no need to lock the sdcardfs_super_info's rwsem as there is no
187 * way anyone can have a reference to the superblock at this point in time.
189 static int sdcardfs_read_super(struct super_block *sb, const char *dev_name,
190 void *raw_data, int silent)
194 struct super_block *lower_sb;
195 struct path lower_path;
196 struct sdcardfs_sb_info *sb_info;
199 printk(KERN_INFO "sdcardfs version 2.0\n");
203 "sdcardfs: read_super: missing dev_name argument\n");
208 printk(KERN_INFO "sdcardfs: dev_name -> %s\n", dev_name);
209 printk(KERN_INFO "sdcardfs: options -> %s\n", (char *)raw_data);
211 /* parse lower path */
212 err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
215 printk(KERN_ERR "sdcardfs: error accessing "
216 "lower directory '%s'\n", dev_name);
220 /* allocate superblock private data */
221 sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL);
222 if (!SDCARDFS_SB(sb)) {
223 printk(KERN_CRIT "sdcardfs: read_super: out of memory\n");
228 sb_info = sb->s_fs_info;
231 err = parse_options(sb, raw_data, silent, &debug, &sb_info->options);
233 printk(KERN_ERR "sdcardfs: invalid options\n");
237 if (sb_info->options.derive != DERIVE_NONE) {
238 pkgl_id = packagelist_create(sb_info->options.write_gid);
242 sb_info->pkgl_id = pkgl_id;
245 /* set the lower superblock field of upper superblock */
246 lower_sb = lower_path.dentry->d_sb;
247 atomic_inc(&lower_sb->s_active);
248 sdcardfs_set_lower_super(sb, lower_sb);
250 /* inherit maxbytes from lower file system */
251 sb->s_maxbytes = lower_sb->s_maxbytes;
254 * Our c/m/atime granularity is 1 ns because we may stack on file
255 * systems whose granularity is as good.
259 sb->s_magic = SDCARDFS_SUPER_MAGIC;
260 sb->s_op = &sdcardfs_sops;
262 /* see comment next to the definition of sdcardfs_d_alloc_root */
263 sb->s_root = sdcardfs_d_alloc_root(sb);
269 /* link the upper and lower dentries */
270 sb->s_root->d_fsdata = NULL;
271 err = new_dentry_private_data(sb->s_root);
275 /* set the lower dentries for s_root */
276 sdcardfs_set_lower_path(sb->s_root, &lower_path);
278 /* call interpose to create the upper level inode */
279 err = sdcardfs_interpose(sb->s_root, sb, &lower_path);
281 /* setup permission policy */
282 switch(sb_info->options.derive) {
284 setup_derived_state(sb->s_root->d_inode,
285 PERM_ROOT, 0, AID_ROOT, AID_SDCARD_RW, 00775);
286 sb_info->obbpath_s = NULL;
289 /* Legacy behavior used to support internal multiuser layout which
290 * places user_id at the top directory level, with the actual roots
291 * just below that. Shared OBB path is also at top level. */
292 setup_derived_state(sb->s_root->d_inode,
293 PERM_LEGACY_PRE_ROOT, 0, AID_ROOT, AID_SDCARD_R, 00771);
294 /* initialize the obbpath string and lookup the path
295 * sb_info->obb_path will be deactivated by path_put
296 * on sdcardfs_put_super */
297 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
298 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
299 err = prepare_dir(sb_info->obbpath_s,
300 sb_info->options.fs_low_uid,
301 sb_info->options.fs_low_gid, 00755);
303 printk(KERN_ERR "sdcardfs: %s: %d, error on creating %s\n",
304 __func__,__LINE__, sb_info->obbpath_s);
307 /* Unified multiuser layout which places secondary user_id under
308 * /Android/user and shared OBB path under /Android/obb. */
309 setup_derived_state(sb->s_root->d_inode,
310 PERM_ROOT, 0, AID_ROOT, AID_SDCARD_R, 00771);
312 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
313 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
316 fix_derived_permission(sb->s_root->d_inode);
319 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
320 dev_name, lower_sb->s_type->name);
323 /* else error: fall through */
325 free_dentry_private_data(sb->s_root);
329 /* drop refs we took earlier */
330 atomic_dec(&lower_sb->s_active);
331 packagelist_destroy(sb_info->pkgl_id);
333 kfree(SDCARDFS_SB(sb));
334 sb->s_fs_info = NULL;
336 path_put(&lower_path);
342 /* A feature which supports mount_nodev() with options */
343 static struct dentry *mount_nodev_with_options(struct file_system_type *fs_type,
344 int flags, const char *dev_name, void *data,
345 int (*fill_super)(struct super_block *, const char *, void *, int))
349 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
356 error = fill_super(s, dev_name, data, flags & MS_SILENT ? 1 : 0);
358 deactivate_locked_super(s);
359 return ERR_PTR(error);
361 s->s_flags |= MS_ACTIVE;
362 return dget(s->s_root);
365 struct dentry *sdcardfs_mount(struct file_system_type *fs_type, int flags,
366 const char *dev_name, void *raw_data)
369 * dev_name is a lower_path_name,
370 * raw_data is a option string.
372 return mount_nodev_with_options(fs_type, flags, dev_name,
373 raw_data, sdcardfs_read_super);
376 static struct file_system_type sdcardfs_fs_type = {
377 .owner = THIS_MODULE,
378 .name = SDCARDFS_NAME,
379 .mount = sdcardfs_mount,
380 .kill_sb = generic_shutdown_super,
381 .fs_flags = FS_REVAL_DOT,
384 static int __init init_sdcardfs_fs(void)
388 pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
390 err = sdcardfs_init_inode_cache();
393 err = sdcardfs_init_dentry_cache();
396 err = packagelist_init();
399 err = register_filesystem(&sdcardfs_fs_type);
402 sdcardfs_destroy_inode_cache();
403 sdcardfs_destroy_dentry_cache();
409 static void __exit exit_sdcardfs_fs(void)
411 sdcardfs_destroy_inode_cache();
412 sdcardfs_destroy_dentry_cache();
414 unregister_filesystem(&sdcardfs_fs_type);
415 pr_info("Completed sdcardfs module unload\n");
418 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
419 " (http://www.fsl.cs.sunysb.edu/)");
420 MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
421 " (http://wrapfs.filesystems.org/)");
422 MODULE_LICENSE("GPL");
424 module_init(init_sdcardfs_fs);
425 module_exit(exit_sdcardfs_fs);