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