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