Port of sdcardfs to 4.4
[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 #if 0
160 /*
161  * our custom d_alloc_root work-alike
162  *
163  * we can't use d_alloc_root if we want to use our own interpose function
164  * unchanged, so we simply call our own "fake" d_alloc_root
165  */
166 static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
167 {
168         struct dentry *ret = NULL;
169
170         if (sb) {
171                 static const struct qstr name = {
172                         .name = "/",
173                         .len = 1
174                 };
175
176                 ret = d_alloc(NULL, &name);
177                 if (ret) {
178                         d_set_d_op(ret, &sdcardfs_ci_dops);
179                         ret->d_sb = sb;
180                         ret->d_parent = ret;
181                 }
182         }
183         return ret;
184 }
185 #endif
186
187 /*
188  * There is no need to lock the sdcardfs_super_info's rwsem as there is no
189  * way anyone can have a reference to the superblock at this point in time.
190  */
191 static int sdcardfs_read_super(struct super_block *sb, const char *dev_name,
192                                                 void *raw_data, int silent)
193 {
194         int err = 0;
195         int debug;
196         struct super_block *lower_sb;
197         struct path lower_path;
198         struct sdcardfs_sb_info *sb_info;
199         void *pkgl_id;
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 "
219                        "lower directory '%s'\n", dev_name);
220                 goto out;
221         }
222
223         /* allocate superblock private data */
224         sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL);
225         if (!SDCARDFS_SB(sb)) {
226                 printk(KERN_CRIT "sdcardfs: read_super: out of memory\n");
227                 err = -ENOMEM;
228                 goto out_free;
229         }
230
231         sb_info = sb->s_fs_info;
232
233         /* parse options */
234         err = parse_options(sb, raw_data, silent, &debug, &sb_info->options);
235         if (err) {
236                 printk(KERN_ERR "sdcardfs: invalid options\n");
237                 goto out_freesbi;
238         }
239
240         if (sb_info->options.derive != DERIVE_NONE) {
241                 pkgl_id = packagelist_create(sb_info->options.write_gid);
242                 if(IS_ERR(pkgl_id))
243                         goto out_freesbi;
244                 else
245                         sb_info->pkgl_id = pkgl_id;
246         }
247
248         /* set the lower superblock field of upper superblock */
249         lower_sb = lower_path.dentry->d_sb;
250         atomic_inc(&lower_sb->s_active);
251         sdcardfs_set_lower_super(sb, lower_sb);
252
253         /* inherit maxbytes from lower file system */
254         sb->s_maxbytes = lower_sb->s_maxbytes;
255
256         /*
257          * Our c/m/atime granularity is 1 ns because we may stack on file
258          * systems whose granularity is as good.
259          */
260         sb->s_time_gran = 1;
261
262         sb->s_magic = SDCARDFS_SUPER_MAGIC;
263         sb->s_op = &sdcardfs_sops;
264
265         /* get a new inode and allocate our root dentry */
266         inode = sdcardfs_iget(sb, lower_path.dentry->d_inode);
267         if (IS_ERR(inode)) {
268                 err = PTR_ERR(inode);
269                 goto out_sput;
270         }
271         sb->s_root = d_make_root(inode);
272         if (!sb->s_root) {
273                 err = -ENOMEM;
274                 goto out_iput;
275         }
276         d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
277
278         /* link the upper and lower dentries */
279         sb->s_root->d_fsdata = NULL;
280         err = new_dentry_private_data(sb->s_root);
281         if (err)
282                 goto out_freeroot;
283
284         /* set the lower dentries for s_root */
285         sdcardfs_set_lower_path(sb->s_root, &lower_path);
286
287         /*
288          * No need to call interpose because we already have a positive
289          * dentry, which was instantiated by d_make_root.  Just need to
290          * d_rehash it.
291          */
292         d_rehash(sb->s_root);
293
294         /* setup permission policy */
295         switch(sb_info->options.derive) {
296                 case DERIVE_NONE:
297                         setup_derived_state(sb->s_root->d_inode,
298                                         PERM_ROOT, 0, AID_ROOT, AID_SDCARD_RW, 00775);
299                         sb_info->obbpath_s = NULL;
300                         break;
301                 case DERIVE_LEGACY:
302                         /* Legacy behavior used to support internal multiuser layout which
303                          * places user_id at the top directory level, with the actual roots
304                          * just below that. Shared OBB path is also at top level. */
305                         setup_derived_state(sb->s_root->d_inode,
306                                         PERM_LEGACY_PRE_ROOT, 0, AID_ROOT, AID_SDCARD_R, 00771);
307                         /* initialize the obbpath string and lookup the path
308                          * sb_info->obb_path will be deactivated by path_put
309                          * on sdcardfs_put_super */
310                         sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
311                         snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
312                         err =  prepare_dir(sb_info->obbpath_s,
313                                         sb_info->options.fs_low_uid,
314                                         sb_info->options.fs_low_gid, 00755);
315                         if(err)
316                                 printk(KERN_ERR "sdcardfs: %s: %d, error on creating %s\n",
317                                                 __func__,__LINE__, sb_info->obbpath_s);
318                         break;
319                 case DERIVE_UNIFIED:
320                         /* Unified multiuser layout which places secondary user_id under
321                          * /Android/user and shared OBB path under /Android/obb. */
322                         setup_derived_state(sb->s_root->d_inode,
323                                         PERM_ROOT, 0, AID_ROOT, AID_SDCARD_R, 00771);
324
325                         sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
326                         snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
327                         break;
328         }
329         fix_derived_permission(sb->s_root->d_inode);
330
331         if (!silent)
332                 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
333                                 dev_name, lower_sb->s_type->name);
334         goto out; /* all is well */
335
336         /* no longer needed: free_dentry_private_data(sb->s_root); */
337 out_freeroot:
338         dput(sb->s_root);
339 out_iput:
340         iput(inode);
341 out_sput:
342         /* drop refs we took earlier */
343         atomic_dec(&lower_sb->s_active);
344         packagelist_destroy(sb_info->pkgl_id);
345 out_freesbi:
346         kfree(SDCARDFS_SB(sb));
347         sb->s_fs_info = NULL;
348 out_free:
349         path_put(&lower_path);
350
351 out:
352         return err;
353 }
354
355 /* A feature which supports mount_nodev() with options */
356 static struct dentry *mount_nodev_with_options(struct file_system_type *fs_type,
357         int flags, const char *dev_name, void *data,
358         int (*fill_super)(struct super_block *, const char *, void *, int))
359
360 {
361         int error;
362         struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
363
364         if (IS_ERR(s))
365                 return ERR_CAST(s);
366
367         s->s_flags = flags;
368
369         error = fill_super(s, dev_name, data, flags & MS_SILENT ? 1 : 0);
370         if (error) {
371                 deactivate_locked_super(s);
372                 return ERR_PTR(error);
373         }
374         s->s_flags |= MS_ACTIVE;
375         return dget(s->s_root);
376 }
377
378 struct dentry *sdcardfs_mount(struct file_system_type *fs_type, int flags,
379                             const char *dev_name, void *raw_data)
380 {
381         /*
382          * dev_name is a lower_path_name,
383          * raw_data is a option string.
384          */
385         return mount_nodev_with_options(fs_type, flags, dev_name,
386                                         raw_data, sdcardfs_read_super);
387 }
388
389 static struct file_system_type sdcardfs_fs_type = {
390         .owner          = THIS_MODULE,
391         .name           = SDCARDFS_NAME,
392         .mount          = sdcardfs_mount,
393         .kill_sb        = generic_shutdown_super,
394         .fs_flags       = 0,
395 };
396
397 static int __init init_sdcardfs_fs(void)
398 {
399         int err;
400
401         pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
402
403         err = sdcardfs_init_inode_cache();
404         if (err)
405                 goto out;
406         err = sdcardfs_init_dentry_cache();
407         if (err)
408                 goto out;
409         err = packagelist_init();
410         if (err)
411                 goto out;
412         err = register_filesystem(&sdcardfs_fs_type);
413 out:
414         if (err) {
415                 sdcardfs_destroy_inode_cache();
416                 sdcardfs_destroy_dentry_cache();
417                 packagelist_exit();
418         }
419         return err;
420 }
421
422 static void __exit exit_sdcardfs_fs(void)
423 {
424         sdcardfs_destroy_inode_cache();
425         sdcardfs_destroy_dentry_cache();
426         packagelist_exit();
427         unregister_filesystem(&sdcardfs_fs_type);
428         pr_info("Completed sdcardfs module unload\n");
429 }
430
431 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
432               " (http://www.fsl.cs.sunysb.edu/)");
433 MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
434                    " (http://wrapfs.filesystems.org/)");
435 MODULE_LICENSE("GPL");
436
437 module_init(init_sdcardfs_fs);
438 module_exit(exit_sdcardfs_fs);