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>
33 Opt_multiuser, // May need?
39 static const match_table_t sdcardfs_tokens = {
40 {Opt_fsuid, "fsuid=%u"},
41 {Opt_fsgid, "fsgid=%u"},
44 {Opt_mask, "mask=%u"},
45 {Opt_userid, "userid=%d"},
46 {Opt_multiuser, "multiuser"},
47 {Opt_reserved_mb, "reserved_mb=%u"},
51 static int parse_options(struct super_block *sb, char *options, int silent,
52 int *debug, struct sdcardfs_vfsmount_options *vfsopts,
53 struct sdcardfs_mount_options *opts)
56 substring_t args[MAX_OPT_ARGS];
59 /* by default, we use AID_MEDIA_RW as uid, gid */
60 opts->fs_low_uid = AID_MEDIA_RW;
61 opts->fs_low_gid = AID_MEDIA_RW;
64 opts->multiuser = false;
68 /* by default, 0MB is reserved */
69 opts->reserved_mb = 0;
76 while ((p = strsep(&options, ",")) != NULL) {
81 token = match_token(p, sdcardfs_tokens, args);
88 if (match_int(&args[0], &option))
90 opts->fs_low_uid = option;
93 if (match_int(&args[0], &option))
95 opts->fs_low_gid = option;
98 if (match_int(&args[0], &option))
101 vfsopts->gid = option;
104 if (match_int(&args[0], &option))
106 opts->fs_user_id = option;
109 if (match_int(&args[0], &option))
112 vfsopts->mask = option;
115 opts->multiuser = true;
117 case Opt_reserved_mb:
118 if (match_int(&args[0], &option))
120 opts->reserved_mb = option;
125 printk( KERN_ERR "Unrecognized mount option \"%s\" "
126 "or missing value", p);
133 printk( KERN_INFO "sdcardfs : options - debug:%d\n", *debug);
134 printk( KERN_INFO "sdcardfs : options - uid:%d\n",
136 printk( KERN_INFO "sdcardfs : options - gid:%d\n",
143 int parse_options_remount(struct super_block *sb, char *options, int silent,
144 struct sdcardfs_vfsmount_options *vfsopts)
147 substring_t args[MAX_OPT_ARGS];
154 while ((p = strsep(&options, ",")) != NULL) {
159 token = match_token(p, sdcardfs_tokens, args);
166 if (match_int(&args[0], &option))
168 vfsopts->gid = option;
172 if (match_int(&args[0], &option))
174 vfsopts->mask = option;
180 case Opt_reserved_mb:
181 printk( KERN_WARNING "Option \"%s\" can't be changed during remount\n", p);
186 printk( KERN_ERR "Unrecognized mount option \"%s\" "
187 "or missing value", p);
194 printk( KERN_INFO "sdcardfs : options - debug:%d\n", debug);
195 printk( KERN_INFO "sdcardfs : options - gid:%d\n", vfsopts->gid);
196 printk( KERN_INFO "sdcardfs : options - mask:%d\n", vfsopts->mask);
204 * our custom d_alloc_root work-alike
206 * we can't use d_alloc_root if we want to use our own interpose function
207 * unchanged, so we simply call our own "fake" d_alloc_root
209 static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
211 struct dentry *ret = NULL;
214 static const struct qstr name = {
219 ret = d_alloc(NULL, &name);
221 d_set_d_op(ret, &sdcardfs_ci_dops);
230 DEFINE_MUTEX(sdcardfs_super_list_lock);
231 LIST_HEAD(sdcardfs_super_list);
232 EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock);
233 EXPORT_SYMBOL_GPL(sdcardfs_super_list);
236 * There is no need to lock the sdcardfs_super_info's rwsem as there is no
237 * way anyone can have a reference to the superblock at this point in time.
239 static int sdcardfs_read_super(struct vfsmount *mnt, struct super_block *sb,
240 const char *dev_name, void *raw_data, int silent)
244 struct super_block *lower_sb;
245 struct path lower_path;
246 struct sdcardfs_sb_info *sb_info;
247 struct sdcardfs_vfsmount_options *mnt_opt = mnt->data;
250 printk(KERN_INFO "sdcardfs version 2.0\n");
254 "sdcardfs: read_super: missing dev_name argument\n");
259 printk(KERN_INFO "sdcardfs: dev_name -> %s\n", dev_name);
260 printk(KERN_INFO "sdcardfs: options -> %s\n", (char *)raw_data);
262 /* parse lower path */
263 err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
266 printk(KERN_ERR "sdcardfs: error accessing lower directory '%s'\n", dev_name);
270 /* allocate superblock private data */
271 sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL);
272 if (!SDCARDFS_SB(sb)) {
273 printk(KERN_CRIT "sdcardfs: read_super: out of memory\n");
278 sb_info = sb->s_fs_info;
280 err = parse_options(sb, raw_data, silent, &debug, mnt_opt, &sb_info->options);
282 printk(KERN_ERR "sdcardfs: invalid options\n");
286 /* set the lower superblock field of upper superblock */
287 lower_sb = lower_path.dentry->d_sb;
288 atomic_inc(&lower_sb->s_active);
289 sdcardfs_set_lower_super(sb, lower_sb);
291 /* inherit maxbytes from lower file system */
292 sb->s_maxbytes = lower_sb->s_maxbytes;
295 * Our c/m/atime granularity is 1 ns because we may stack on file
296 * systems whose granularity is as good.
300 sb->s_magic = SDCARDFS_SUPER_MAGIC;
301 sb->s_op = &sdcardfs_sops;
303 /* get a new inode and allocate our root dentry */
304 inode = sdcardfs_iget(sb, lower_path.dentry->d_inode, 0);
306 err = PTR_ERR(inode);
309 sb->s_root = d_make_root(inode);
314 d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
316 /* link the upper and lower dentries */
317 sb->s_root->d_fsdata = NULL;
318 err = new_dentry_private_data(sb->s_root);
322 /* set the lower dentries for s_root */
323 sdcardfs_set_lower_path(sb->s_root, &lower_path);
326 * No need to call interpose because we already have a positive
327 * dentry, which was instantiated by d_make_root. Just need to
330 d_rehash(sb->s_root);
332 /* setup permission policy */
333 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
334 mutex_lock(&sdcardfs_super_list_lock);
335 if(sb_info->options.multiuser) {
336 setup_derived_state(d_inode(sb->s_root), PERM_PRE_ROOT, sb_info->options.fs_user_id, AID_ROOT, false, d_inode(sb->s_root));
337 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
338 /*err = prepare_dir(sb_info->obbpath_s,
339 sb_info->options.fs_low_uid,
340 sb_info->options.fs_low_gid, 00755);*/
342 setup_derived_state(d_inode(sb->s_root), PERM_ROOT, sb_info->options.fs_user_id, AID_ROOT, false, d_inode(sb->s_root));
343 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
345 fix_derived_permission(sb->s_root->d_inode);
347 list_add(&sb_info->list, &sdcardfs_super_list);
348 mutex_unlock(&sdcardfs_super_list_lock);
351 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
352 dev_name, lower_sb->s_type->name);
353 goto out; /* all is well */
355 /* no longer needed: free_dentry_private_data(sb->s_root); */
361 /* drop refs we took earlier */
362 atomic_dec(&lower_sb->s_active);
364 kfree(SDCARDFS_SB(sb));
365 sb->s_fs_info = NULL;
367 path_put(&lower_path);
373 /* A feature which supports mount_nodev() with options */
374 static struct dentry *mount_nodev_with_options(struct vfsmount *mnt,
375 struct file_system_type *fs_type, int flags, const char *dev_name, void *data,
376 int (*fill_super)(struct vfsmount *, struct super_block *, const char *, void *, int))
380 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
387 error = fill_super(mnt, s, dev_name, data, flags & MS_SILENT ? 1 : 0);
389 deactivate_locked_super(s);
390 return ERR_PTR(error);
392 s->s_flags |= MS_ACTIVE;
393 return dget(s->s_root);
396 static struct dentry *sdcardfs_mount(struct vfsmount *mnt,
397 struct file_system_type *fs_type, int flags,
398 const char *dev_name, void *raw_data)
401 * dev_name is a lower_path_name,
402 * raw_data is a option string.
404 return mount_nodev_with_options(mnt, fs_type, flags, dev_name,
405 raw_data, sdcardfs_read_super);
408 static struct dentry *sdcardfs_mount_wrn(struct file_system_type *fs_type, int flags,
409 const char *dev_name, void *raw_data)
411 WARN(1, "sdcardfs does not support mount. Use mount2.\n");
412 return ERR_PTR(-EINVAL);
415 void *sdcardfs_alloc_mnt_data(void) {
416 return kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL);
419 void sdcardfs_kill_sb(struct super_block *sb) {
420 struct sdcardfs_sb_info *sbi;
421 if (sb->s_magic == SDCARDFS_SUPER_MAGIC) {
422 sbi = SDCARDFS_SB(sb);
423 mutex_lock(&sdcardfs_super_list_lock);
424 list_del(&sbi->list);
425 mutex_unlock(&sdcardfs_super_list_lock);
427 generic_shutdown_super(sb);
430 static struct file_system_type sdcardfs_fs_type = {
431 .owner = THIS_MODULE,
432 .name = SDCARDFS_NAME,
433 .mount = sdcardfs_mount_wrn,
434 .mount2 = sdcardfs_mount,
435 .alloc_mnt_data = sdcardfs_alloc_mnt_data,
436 .kill_sb = sdcardfs_kill_sb,
440 static int __init init_sdcardfs_fs(void)
444 pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
446 err = sdcardfs_init_inode_cache();
449 err = sdcardfs_init_dentry_cache();
452 err = packagelist_init();
455 err = register_filesystem(&sdcardfs_fs_type);
458 sdcardfs_destroy_inode_cache();
459 sdcardfs_destroy_dentry_cache();
465 static void __exit exit_sdcardfs_fs(void)
467 sdcardfs_destroy_inode_cache();
468 sdcardfs_destroy_dentry_cache();
470 unregister_filesystem(&sdcardfs_fs_type);
471 pr_info("Completed sdcardfs module unload\n");
474 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
475 " (http://www.fsl.cs.sunysb.edu/)");
476 MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
477 " (http://wrapfs.filesystems.org/)");
478 MODULE_LICENSE("GPL");
480 module_init(init_sdcardfs_fs);
481 module_exit(exit_sdcardfs_fs);