1fdceffec72ca9b65229d1af55612fb9d74f0137
[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_uid,
28         Opt_gid,
29         Opt_wgid,
30         Opt_debug,
31         Opt_split,
32         Opt_derive,
33         Opt_lower_fs,
34         Opt_reserved_mb,
35         Opt_err,
36 };
37
38 static const match_table_t sdcardfs_tokens = {
39         {Opt_uid, "uid=%u"},
40         {Opt_gid, "gid=%u"},
41         {Opt_wgid, "wgid=%u"},
42         {Opt_debug, "debug"},
43         {Opt_split, "split"},
44         {Opt_derive, "derive=%s"},
45         {Opt_lower_fs, "lower_fs=%s"},
46         {Opt_reserved_mb, "reserved_mb=%u"},
47         {Opt_err, NULL}
48 };
49
50 static int parse_options(struct super_block *sb, char *options, int silent,
51                                 int *debug, struct sdcardfs_mount_options *opts)
52 {
53         char *p;
54         substring_t args[MAX_OPT_ARGS];
55         int option;
56         char *string_option;
57
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;
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_uid:
89                         if (match_int(&args[0], &option))
90                                 return 0;
91                         opts->fs_low_uid = option;
92                         break;
93                 case Opt_gid:
94                         if (match_int(&args[0], &option))
95                                 return 0;
96                         opts->fs_low_gid = option;
97                         break;
98                 case Opt_wgid:
99                         if (match_int(&args[0], &option))
100                                 return 0;
101                         opts->write_gid = option;
102                         break;
103                 case Opt_split:
104                         opts->split_perms=1;
105                         break;
106                 case Opt_derive:
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;
114                         } else {
115                                 kfree(string_option);
116                                 goto invalid_option;
117                         }
118                         kfree(string_option);
119                         break;
120                 case Opt_lower_fs:
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;
126                         } else {
127                                 kfree(string_option);
128                                 goto invalid_option;
129                         }
130                         kfree(string_option);
131                         break;
132                 case Opt_reserved_mb:
133                         if (match_int(&args[0], &option))
134                                 return 0;
135                         opts->reserved_mb = option;
136                         break;
137                 /* unknown option */
138                 default:
139 invalid_option:
140                         if (!silent) {
141                                 printk( KERN_ERR "Unrecognized mount option \"%s\" "
142                                                 "or missing value", p);
143                         }
144                         return -EINVAL;
145                 }
146         }
147
148         if (*debug) {
149                 printk( KERN_INFO "sdcardfs : options - debug:%d\n", *debug);
150                 printk( KERN_INFO "sdcardfs : options - uid:%d\n",
151                                                         opts->fs_low_uid);
152                 printk( KERN_INFO "sdcardfs : options - gid:%d\n",
153                                                         opts->fs_low_gid);
154         }
155
156         return 0;
157 }
158
159 /*
160  * our custom d_alloc_root work-alike
161  *
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
164  */
165 static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
166 {
167         struct dentry *ret = NULL;
168
169         if (sb) {
170                 static const struct qstr name = {
171                         .name = "/",
172                         .len = 1
173                 };
174
175                 ret = d_alloc(NULL, &name);
176                 if (ret) {
177                         d_set_d_op(ret, &sdcardfs_ci_dops);
178                         ret->d_sb = sb;
179                         ret->d_parent = ret;
180                 }
181         }
182         return ret;
183 }
184
185 /*
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.
188  */
189 static int sdcardfs_read_super(struct super_block *sb, const char *dev_name,
190                                                 void *raw_data, int silent)
191 {
192         int err = 0;
193         int debug;
194         struct super_block *lower_sb;
195         struct path lower_path;
196         struct sdcardfs_sb_info *sb_info;
197         void *pkgl_id;
198
199         printk(KERN_INFO "sdcardfs version 2.0\n");
200
201         if (!dev_name) {
202                 printk(KERN_ERR
203                        "sdcardfs: read_super: missing dev_name argument\n");
204                 err = -EINVAL;
205                 goto out;
206         }
207
208         printk(KERN_INFO "sdcardfs: dev_name -> %s\n", dev_name);
209         printk(KERN_INFO "sdcardfs: options -> %s\n", (char *)raw_data);
210
211         /* parse lower path */
212         err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
213                         &lower_path);
214         if (err) {
215                 printk(KERN_ERR "sdcardfs: error accessing "
216                        "lower directory '%s'\n", dev_name);
217                 goto out;
218         }
219
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");
224                 err = -ENOMEM;
225                 goto out_free;
226         }
227
228         sb_info = sb->s_fs_info;
229
230         /* parse options */
231         err = parse_options(sb, raw_data, silent, &debug, &sb_info->options);
232         if (err) {
233                 printk(KERN_ERR "sdcardfs: invalid options\n");
234                 goto out_freesbi;
235         }
236
237         if (sb_info->options.derive != DERIVE_NONE) {
238                 pkgl_id = packagelist_create(sb_info->options.write_gid);
239                 if(IS_ERR(pkgl_id))
240                         goto out_freesbi;
241                 else
242                         sb_info->pkgl_id = pkgl_id;
243         }
244
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);
249
250         /* inherit maxbytes from lower file system */
251         sb->s_maxbytes = lower_sb->s_maxbytes;
252
253         /*
254          * Our c/m/atime granularity is 1 ns because we may stack on file
255          * systems whose granularity is as good.
256          */
257         sb->s_time_gran = 1;
258
259         sb->s_magic = SDCARDFS_SUPER_MAGIC;
260         sb->s_op = &sdcardfs_sops;
261
262         /* see comment next to the definition of sdcardfs_d_alloc_root */
263         sb->s_root = sdcardfs_d_alloc_root(sb);
264         if (!sb->s_root) {
265                 err = -ENOMEM;
266                 goto out_sput;
267         }
268
269         /* link the upper and lower dentries */
270         sb->s_root->d_fsdata = NULL;
271         err = new_dentry_private_data(sb->s_root);
272         if (err)
273                 goto out_freeroot;
274
275         /* set the lower dentries for s_root */
276         sdcardfs_set_lower_path(sb->s_root, &lower_path);
277
278         /* call interpose to create the upper level inode */
279         err = sdcardfs_interpose(sb->s_root, sb, &lower_path);
280         if (!err) {
281                 /* setup permission policy */
282                 switch(sb_info->options.derive) {
283                         case DERIVE_NONE:
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;
287                                 break;
288                         case DERIVE_LEGACY:
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);
302                                 if(err)
303                                         printk(KERN_ERR "sdcardfs: %s: %d, error on creating %s\n",
304                                                         __func__,__LINE__, sb_info->obbpath_s);
305                                 break;
306                         case DERIVE_UNIFIED:
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);
311
312                                 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
313                                 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
314                                 break;
315                 }
316                 fix_derived_permission(sb->s_root->d_inode);
317
318                 if (!silent)
319                         printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
320                                                 dev_name, lower_sb->s_type->name);
321                 goto out;
322         }
323         /* else error: fall through */
324
325         free_dentry_private_data(sb->s_root);
326 out_freeroot:
327         dput(sb->s_root);
328 out_sput:
329         /* drop refs we took earlier */
330         atomic_dec(&lower_sb->s_active);
331         packagelist_destroy(sb_info->pkgl_id);
332 out_freesbi:
333         kfree(SDCARDFS_SB(sb));
334         sb->s_fs_info = NULL;
335 out_free:
336         path_put(&lower_path);
337
338 out:
339         return err;
340 }
341
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))
346
347 {
348         int error;
349         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
350
351         if (IS_ERR(s))
352                 return ERR_CAST(s);
353
354         s->s_flags = flags;
355
356         error = fill_super(s, dev_name, data, flags & MS_SILENT ? 1 : 0);
357         if (error) {
358                 deactivate_locked_super(s);
359                 return ERR_PTR(error);
360         }
361         s->s_flags |= MS_ACTIVE;
362         return dget(s->s_root);
363 }
364
365 struct dentry *sdcardfs_mount(struct file_system_type *fs_type, int flags,
366                             const char *dev_name, void *raw_data)
367 {
368         /*
369          * dev_name is a lower_path_name,
370          * raw_data is a option string.
371          */
372         return mount_nodev_with_options(fs_type, flags, dev_name,
373                                         raw_data, sdcardfs_read_super);
374 }
375
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,
382 };
383
384 static int __init init_sdcardfs_fs(void)
385 {
386         int err;
387
388         pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
389
390         err = sdcardfs_init_inode_cache();
391         if (err)
392                 goto out;
393         err = sdcardfs_init_dentry_cache();
394         if (err)
395                 goto out;
396         err = packagelist_init();
397         if (err)
398                 goto out;
399         err = register_filesystem(&sdcardfs_fs_type);
400 out:
401         if (err) {
402                 sdcardfs_destroy_inode_cache();
403                 sdcardfs_destroy_dentry_cache();
404                 packagelist_exit();
405         }
406         return err;
407 }
408
409 static void __exit exit_sdcardfs_fs(void)
410 {
411         sdcardfs_destroy_inode_cache();
412         sdcardfs_destroy_dentry_cache();
413         packagelist_exit();
414         unregister_filesystem(&sdcardfs_fs_type);
415         pr_info("Completed sdcardfs module unload\n");
416 }
417
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");
423
424 module_init(init_sdcardfs_fs);
425 module_exit(exit_sdcardfs_fs);