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_mount_options *opts)
55 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;
62 opts->multiuser = false;
65 /* by default, 0MB is reserved */
66 opts->reserved_mb = 0;
73 while ((p = strsep(&options, ",")) != NULL) {
78 token = match_token(p, sdcardfs_tokens, args);
85 if (match_int(&args[0], &option))
87 opts->fs_low_uid = option;
90 if (match_int(&args[0], &option))
92 opts->fs_low_gid = option;
95 if (match_int(&args[0], &option))
100 if (match_int(&args[0], &option))
102 opts->fs_user_id = option;
105 if (match_int(&args[0], &option))
110 opts->multiuser = true;
112 case Opt_reserved_mb:
113 if (match_int(&args[0], &option))
115 opts->reserved_mb = option;
120 printk( KERN_ERR "Unrecognized mount option \"%s\" "
121 "or missing value", p);
128 printk( KERN_INFO "sdcardfs : options - debug:%d\n", *debug);
129 printk( KERN_INFO "sdcardfs : options - uid:%d\n",
131 printk( KERN_INFO "sdcardfs : options - gid:%d\n",
140 * our custom d_alloc_root work-alike
142 * we can't use d_alloc_root if we want to use our own interpose function
143 * unchanged, so we simply call our own "fake" d_alloc_root
145 static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
147 struct dentry *ret = NULL;
150 static const struct qstr name = {
155 ret = d_alloc(NULL, &name);
157 d_set_d_op(ret, &sdcardfs_ci_dops);
166 DEFINE_MUTEX(sdcardfs_super_list_lock);
167 LIST_HEAD(sdcardfs_super_list);
168 EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock);
169 EXPORT_SYMBOL_GPL(sdcardfs_super_list);
172 * There is no need to lock the sdcardfs_super_info's rwsem as there is no
173 * way anyone can have a reference to the superblock at this point in time.
175 static int sdcardfs_read_super(struct super_block *sb, const char *dev_name,
176 void *raw_data, int silent)
180 struct super_block *lower_sb;
181 struct path lower_path;
182 struct sdcardfs_sb_info *sb_info;
185 printk(KERN_INFO "sdcardfs version 2.0\n");
189 "sdcardfs: read_super: missing dev_name argument\n");
194 printk(KERN_INFO "sdcardfs: dev_name -> %s\n", dev_name);
195 printk(KERN_INFO "sdcardfs: options -> %s\n", (char *)raw_data);
197 /* parse lower path */
198 err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
201 printk(KERN_ERR "sdcardfs: error accessing lower directory '%s'\n", dev_name);
205 /* allocate superblock private data */
206 sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL);
207 if (!SDCARDFS_SB(sb)) {
208 printk(KERN_CRIT "sdcardfs: read_super: out of memory\n");
213 sb_info = sb->s_fs_info;
215 err = parse_options(sb, raw_data, silent, &debug, &sb_info->options);
217 printk(KERN_ERR "sdcardfs: invalid options\n");
221 /* set the lower superblock field of upper superblock */
222 lower_sb = lower_path.dentry->d_sb;
223 atomic_inc(&lower_sb->s_active);
224 sdcardfs_set_lower_super(sb, lower_sb);
226 /* inherit maxbytes from lower file system */
227 sb->s_maxbytes = lower_sb->s_maxbytes;
230 * Our c/m/atime granularity is 1 ns because we may stack on file
231 * systems whose granularity is as good.
235 sb->s_magic = SDCARDFS_SUPER_MAGIC;
236 sb->s_op = &sdcardfs_sops;
238 /* get a new inode and allocate our root dentry */
239 inode = sdcardfs_iget(sb, lower_path.dentry->d_inode, 0);
241 err = PTR_ERR(inode);
244 sb->s_root = d_make_root(inode);
249 d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
251 /* link the upper and lower dentries */
252 sb->s_root->d_fsdata = NULL;
253 err = new_dentry_private_data(sb->s_root);
257 /* set the lower dentries for s_root */
258 sdcardfs_set_lower_path(sb->s_root, &lower_path);
261 * No need to call interpose because we already have a positive
262 * dentry, which was instantiated by d_make_root. Just need to
265 d_rehash(sb->s_root);
267 /* setup permission policy */
268 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
269 mutex_lock(&sdcardfs_super_list_lock);
270 if(sb_info->options.multiuser) {
271 setup_derived_state(sb->s_root->d_inode, PERM_PRE_ROOT, sb_info->options.fs_user_id, AID_ROOT, false);
272 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
273 /*err = prepare_dir(sb_info->obbpath_s,
274 sb_info->options.fs_low_uid,
275 sb_info->options.fs_low_gid, 00755);*/
277 setup_derived_state(sb->s_root->d_inode, PERM_ROOT, sb_info->options.fs_low_uid, AID_ROOT, false);
278 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
280 fix_derived_permission(sb->s_root->d_inode);
282 list_add(&sb_info->list, &sdcardfs_super_list);
283 mutex_unlock(&sdcardfs_super_list_lock);
286 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
287 dev_name, lower_sb->s_type->name);
288 goto out; /* all is well */
290 /* no longer needed: free_dentry_private_data(sb->s_root); */
296 /* drop refs we took earlier */
297 atomic_dec(&lower_sb->s_active);
299 kfree(SDCARDFS_SB(sb));
300 sb->s_fs_info = NULL;
302 path_put(&lower_path);
308 /* A feature which supports mount_nodev() with options */
309 static struct dentry *mount_nodev_with_options(struct file_system_type *fs_type,
310 int flags, const char *dev_name, void *data,
311 int (*fill_super)(struct super_block *, const char *, void *, int))
315 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
322 error = fill_super(s, dev_name, data, flags & MS_SILENT ? 1 : 0);
324 deactivate_locked_super(s);
325 return ERR_PTR(error);
327 s->s_flags |= MS_ACTIVE;
328 return dget(s->s_root);
331 struct dentry *sdcardfs_mount(struct file_system_type *fs_type, int flags,
332 const char *dev_name, void *raw_data)
335 * dev_name is a lower_path_name,
336 * raw_data is a option string.
338 return mount_nodev_with_options(fs_type, flags, dev_name,
339 raw_data, sdcardfs_read_super);
342 void sdcardfs_kill_sb(struct super_block *sb) {
343 struct sdcardfs_sb_info *sbi;
344 if (sb->s_magic == SDCARDFS_SUPER_MAGIC) {
345 sbi = SDCARDFS_SB(sb);
346 mutex_lock(&sdcardfs_super_list_lock);
347 list_del(&sbi->list);
348 mutex_unlock(&sdcardfs_super_list_lock);
350 generic_shutdown_super(sb);
353 static struct file_system_type sdcardfs_fs_type = {
354 .owner = THIS_MODULE,
355 .name = SDCARDFS_NAME,
356 .mount = sdcardfs_mount,
357 .kill_sb = sdcardfs_kill_sb,
361 static int __init init_sdcardfs_fs(void)
365 pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
367 err = sdcardfs_init_inode_cache();
370 err = sdcardfs_init_dentry_cache();
373 err = packagelist_init();
376 err = register_filesystem(&sdcardfs_fs_type);
379 sdcardfs_destroy_inode_cache();
380 sdcardfs_destroy_dentry_cache();
386 static void __exit exit_sdcardfs_fs(void)
388 sdcardfs_destroy_inode_cache();
389 sdcardfs_destroy_dentry_cache();
391 unregister_filesystem(&sdcardfs_fs_type);
392 pr_info("Completed sdcardfs module unload\n");
395 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
396 " (http://www.fsl.cs.sunysb.edu/)");
397 MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
398 " (http://wrapfs.filesystems.org/)");
399 MODULE_LICENSE("GPL");
401 module_init(init_sdcardfs_fs);
402 module_exit(exit_sdcardfs_fs);