80aa355d801e4e5c609c0a94e23a404f120147e9
[firefly-linux-kernel-4.4.55.git] / fs / sdcardfs / main.c
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/module.h>
23 #include <linux/types.h>
24 #include <linux/parser.h>
25
26 enum {
27         Opt_fsuid,
28         Opt_fsgid,
29         Opt_gid,
30         Opt_debug,
31         Opt_lower_fs,
32         Opt_mask,
33         Opt_multiuser, // May need?
34         Opt_userid,
35         Opt_reserved_mb,
36         Opt_err,
37 };
38
39 static const match_table_t sdcardfs_tokens = {
40         {Opt_fsuid, "fsuid=%u"},
41         {Opt_fsgid, "fsgid=%u"},
42         {Opt_gid, "gid=%u"},
43         {Opt_debug, "debug"},
44         {Opt_lower_fs, "lower_fs=%s"},
45         {Opt_mask, "mask=%u"},
46         {Opt_userid, "userid=%d"},
47         {Opt_multiuser, "multiuser"},
48         {Opt_reserved_mb, "reserved_mb=%u"},
49         {Opt_err, NULL}
50 };
51
52 static int parse_options(struct super_block *sb, char *options, int silent,
53                                 int *debug, struct sdcardfs_mount_options *opts)
54 {
55         char *p;
56         substring_t args[MAX_OPT_ARGS];
57         int option;
58         char *string_option;
59
60         /* by default, we use AID_MEDIA_RW as uid, gid */
61         opts->fs_low_uid = AID_MEDIA_RW;
62         opts->fs_low_gid = AID_MEDIA_RW;
63         opts->mask = 0;
64         opts->multiuser = false;
65         opts->fs_user_id = 0;
66         opts->gid = 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;
71
72         *debug = 0;
73
74         if (!options)
75                 return 0;
76
77         while ((p = strsep(&options, ",")) != NULL) {
78                 int token;
79                 if (!*p)
80                         continue;
81
82                 token = match_token(p, sdcardfs_tokens, args);
83
84                 switch (token) {
85                 case Opt_debug:
86                         *debug = 1;
87                         break;
88                 case Opt_fsuid:
89                         if (match_int(&args[0], &option))
90                                 return 0;
91                         opts->fs_low_uid = option;
92                         break;
93                 case Opt_fsgid:
94                         if (match_int(&args[0], &option))
95                                 return 0;
96                         opts->fs_low_gid = option;
97                         break;
98                 case Opt_gid:
99                         if (match_int(&args[0], &option))
100                                 return 0;
101                         opts->gid = option;
102                         break;
103                 case Opt_userid:
104                         if (match_int(&args[0], &option))
105                                 return 0;
106                         opts->fs_user_id = option;
107                         break;
108                 case Opt_mask:
109                         if (match_int(&args[0], &option))
110                                 return 0;
111                         opts->mask = option;
112                         break;
113                 case Opt_multiuser:
114                         opts->multiuser = true;
115                         break;
116                 case Opt_lower_fs:
117                         string_option = match_strdup(&args[0]);
118                         if (!strcmp("ext4", string_option)) {
119                                 opts->lower_fs = LOWER_FS_EXT4;
120                         } else if (!strcmp("fat", string_option)) {
121                                 opts->lower_fs = LOWER_FS_FAT;
122                         } else {
123                                 kfree(string_option);
124                                 goto invalid_option;
125                         }
126                         kfree(string_option);
127                         break;
128                 case Opt_reserved_mb:
129                         if (match_int(&args[0], &option))
130                                 return 0;
131                         opts->reserved_mb = option;
132                         break;
133                 /* unknown option */
134                 default:
135 invalid_option:
136                         if (!silent) {
137                                 printk( KERN_ERR "Unrecognized mount option \"%s\" "
138                                                 "or missing value", p);
139                         }
140                         return -EINVAL;
141                 }
142         }
143
144         if (*debug) {
145                 printk( KERN_INFO "sdcardfs : options - debug:%d\n", *debug);
146                 printk( KERN_INFO "sdcardfs : options - uid:%d\n",
147                                                         opts->fs_low_uid);
148                 printk( KERN_INFO "sdcardfs : options - gid:%d\n",
149                                                         opts->fs_low_gid);
150         }
151
152         return 0;
153 }
154
155 #if 0
156 /*
157  * our custom d_alloc_root work-alike
158  *
159  * we can't use d_alloc_root if we want to use our own interpose function
160  * unchanged, so we simply call our own "fake" d_alloc_root
161  */
162 static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
163 {
164         struct dentry *ret = NULL;
165
166         if (sb) {
167                 static const struct qstr name = {
168                         .name = "/",
169                         .len = 1
170                 };
171
172                 ret = d_alloc(NULL, &name);
173                 if (ret) {
174                         d_set_d_op(ret, &sdcardfs_ci_dops);
175                         ret->d_sb = sb;
176                         ret->d_parent = ret;
177                 }
178         }
179         return ret;
180 }
181 #endif
182
183 DEFINE_MUTEX(sdcardfs_super_list_lock);
184 LIST_HEAD(sdcardfs_super_list);
185 EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock);
186 EXPORT_SYMBOL_GPL(sdcardfs_super_list);
187
188 /*
189  * There is no need to lock the sdcardfs_super_info's rwsem as there is no
190  * way anyone can have a reference to the superblock at this point in time.
191  */
192 static int sdcardfs_read_super(struct super_block *sb, const char *dev_name,
193                                                 void *raw_data, int silent)
194 {
195         int err = 0;
196         int debug;
197         struct super_block *lower_sb;
198         struct path lower_path;
199         struct sdcardfs_sb_info *sb_info;
200         struct inode *inode;
201
202         printk(KERN_INFO "sdcardfs version 2.0\n");
203
204         if (!dev_name) {
205                 printk(KERN_ERR
206                        "sdcardfs: read_super: missing dev_name argument\n");
207                 err = -EINVAL;
208                 goto out;
209         }
210
211         printk(KERN_INFO "sdcardfs: dev_name -> %s\n", dev_name);
212         printk(KERN_INFO "sdcardfs: options -> %s\n", (char *)raw_data);
213
214         /* parse lower path */
215         err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
216                         &lower_path);
217         if (err) {
218                 printk(KERN_ERR "sdcardfs: error accessing lower directory '%s'\n", dev_name);
219                 goto out;
220         }
221
222         /* allocate superblock private data */
223         sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL);
224         if (!SDCARDFS_SB(sb)) {
225                 printk(KERN_CRIT "sdcardfs: read_super: out of memory\n");
226                 err = -ENOMEM;
227                 goto out_free;
228         }
229
230         sb_info = sb->s_fs_info;
231         /* parse options */
232         err = parse_options(sb, raw_data, silent, &debug, &sb_info->options);
233         if (err) {
234                 printk(KERN_ERR "sdcardfs: invalid options\n");
235                 goto out_freesbi;
236         }
237
238         /* set the lower superblock field of upper superblock */
239         lower_sb = lower_path.dentry->d_sb;
240         atomic_inc(&lower_sb->s_active);
241         sdcardfs_set_lower_super(sb, lower_sb);
242
243         /* inherit maxbytes from lower file system */
244         sb->s_maxbytes = lower_sb->s_maxbytes;
245
246         /*
247          * Our c/m/atime granularity is 1 ns because we may stack on file
248          * systems whose granularity is as good.
249          */
250         sb->s_time_gran = 1;
251
252         sb->s_magic = SDCARDFS_SUPER_MAGIC;
253         sb->s_op = &sdcardfs_sops;
254
255         /* get a new inode and allocate our root dentry */
256         inode = sdcardfs_iget(sb, lower_path.dentry->d_inode, 0);
257         if (IS_ERR(inode)) {
258                 err = PTR_ERR(inode);
259                 goto out_sput;
260         }
261         sb->s_root = d_make_root(inode);
262         if (!sb->s_root) {
263                 err = -ENOMEM;
264                 goto out_iput;
265         }
266         d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
267
268         /* link the upper and lower dentries */
269         sb->s_root->d_fsdata = NULL;
270         err = new_dentry_private_data(sb->s_root);
271         if (err)
272                 goto out_freeroot;
273
274         /* set the lower dentries for s_root */
275         sdcardfs_set_lower_path(sb->s_root, &lower_path);
276
277         /*
278          * No need to call interpose because we already have a positive
279          * dentry, which was instantiated by d_make_root.  Just need to
280          * d_rehash it.
281          */
282         d_rehash(sb->s_root);
283
284         /* setup permission policy */
285         sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
286         mutex_lock(&sdcardfs_super_list_lock);
287         if(sb_info->options.multiuser) {
288                 setup_derived_state(sb->s_root->d_inode, PERM_PRE_ROOT, sb_info->options.fs_user_id, AID_ROOT, false);
289                 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
290                 /*err =  prepare_dir(sb_info->obbpath_s,
291                                         sb_info->options.fs_low_uid,
292                                         sb_info->options.fs_low_gid, 00755);*/
293         } else {
294                 setup_derived_state(sb->s_root->d_inode, PERM_ROOT, sb_info->options.fs_low_uid, AID_ROOT, false);
295                 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
296         }
297         fix_derived_permission(sb->s_root->d_inode);
298         sb_info->sb = sb;
299         list_add(&sb_info->list, &sdcardfs_super_list);
300         mutex_unlock(&sdcardfs_super_list_lock);
301
302         if (!silent)
303                 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
304                                 dev_name, lower_sb->s_type->name);
305         goto out; /* all is well */
306
307         /* no longer needed: free_dentry_private_data(sb->s_root); */
308 out_freeroot:
309         dput(sb->s_root);
310 out_iput:
311         iput(inode);
312 out_sput:
313         /* drop refs we took earlier */
314         atomic_dec(&lower_sb->s_active);
315 out_freesbi:
316         kfree(SDCARDFS_SB(sb));
317         sb->s_fs_info = NULL;
318 out_free:
319         path_put(&lower_path);
320
321 out:
322         return err;
323 }
324
325 /* A feature which supports mount_nodev() with options */
326 static struct dentry *mount_nodev_with_options(struct file_system_type *fs_type,
327         int flags, const char *dev_name, void *data,
328         int (*fill_super)(struct super_block *, const char *, void *, int))
329
330 {
331         int error;
332         struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
333
334         if (IS_ERR(s))
335                 return ERR_CAST(s);
336
337         s->s_flags = flags;
338
339         error = fill_super(s, dev_name, data, flags & MS_SILENT ? 1 : 0);
340         if (error) {
341                 deactivate_locked_super(s);
342                 return ERR_PTR(error);
343         }
344         s->s_flags |= MS_ACTIVE;
345         return dget(s->s_root);
346 }
347
348 struct dentry *sdcardfs_mount(struct file_system_type *fs_type, int flags,
349                             const char *dev_name, void *raw_data)
350 {
351         /*
352          * dev_name is a lower_path_name,
353          * raw_data is a option string.
354          */
355         return mount_nodev_with_options(fs_type, flags, dev_name,
356                                         raw_data, sdcardfs_read_super);
357 }
358
359 void sdcardfs_kill_sb(struct super_block *sb) {
360         struct sdcardfs_sb_info *sbi;
361         if (sb->s_magic == SDCARDFS_SUPER_MAGIC) {
362                 sbi = SDCARDFS_SB(sb);
363                 mutex_lock(&sdcardfs_super_list_lock);
364                 list_del(&sbi->list);
365                 mutex_unlock(&sdcardfs_super_list_lock);
366         }
367         generic_shutdown_super(sb);
368 }
369
370 static struct file_system_type sdcardfs_fs_type = {
371         .owner          = THIS_MODULE,
372         .name           = SDCARDFS_NAME,
373         .mount          = sdcardfs_mount,
374         .kill_sb        = sdcardfs_kill_sb,
375         .fs_flags       = 0,
376 };
377
378 static int __init init_sdcardfs_fs(void)
379 {
380         int err;
381
382         pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
383
384         err = sdcardfs_init_inode_cache();
385         if (err)
386                 goto out;
387         err = sdcardfs_init_dentry_cache();
388         if (err)
389                 goto out;
390         err = packagelist_init();
391         if (err)
392                 goto out;
393         err = register_filesystem(&sdcardfs_fs_type);
394 out:
395         if (err) {
396                 sdcardfs_destroy_inode_cache();
397                 sdcardfs_destroy_dentry_cache();
398                 packagelist_exit();
399         }
400         return err;
401 }
402
403 static void __exit exit_sdcardfs_fs(void)
404 {
405         sdcardfs_destroy_inode_cache();
406         sdcardfs_destroy_dentry_cache();
407         packagelist_exit();
408         unregister_filesystem(&sdcardfs_fs_type);
409         pr_info("Completed sdcardfs module unload\n");
410 }
411
412 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
413               " (http://www.fsl.cs.sunysb.edu/)");
414 MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
415                    " (http://wrapfs.filesystems.org/)");
416 MODULE_LICENSE("GPL");
417
418 module_init(init_sdcardfs_fs);
419 module_exit(exit_sdcardfs_fs);