ANDROID: sdcardfs: Use per mount permissions
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / sdcardfs / main.c
CommitLineData
84a1b7d3
DC
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
26enum {
1e2d3bbc
DR
27 Opt_fsuid,
28 Opt_fsgid,
84a1b7d3 29 Opt_gid,
84a1b7d3 30 Opt_debug,
1e2d3bbc
DR
31 Opt_mask,
32 Opt_multiuser, // May need?
33 Opt_userid,
84a1b7d3
DC
34 Opt_reserved_mb,
35 Opt_err,
36};
37
38static const match_table_t sdcardfs_tokens = {
1e2d3bbc
DR
39 {Opt_fsuid, "fsuid=%u"},
40 {Opt_fsgid, "fsgid=%u"},
84a1b7d3 41 {Opt_gid, "gid=%u"},
84a1b7d3 42 {Opt_debug, "debug"},
1e2d3bbc
DR
43 {Opt_mask, "mask=%u"},
44 {Opt_userid, "userid=%d"},
45 {Opt_multiuser, "multiuser"},
84a1b7d3
DC
46 {Opt_reserved_mb, "reserved_mb=%u"},
47 {Opt_err, NULL}
48};
49
50static int parse_options(struct super_block *sb, char *options, int silent,
6b6e896b
DR
51 int *debug, struct sdcardfs_vfsmount_options *vfsopts,
52 struct sdcardfs_mount_options *opts)
84a1b7d3
DC
53{
54 char *p;
55 substring_t args[MAX_OPT_ARGS];
56 int option;
84a1b7d3
DC
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;
6b6e896b 61 vfsopts->mask = 0;
1e2d3bbc
DR
62 opts->multiuser = false;
63 opts->fs_user_id = 0;
6b6e896b 64 vfsopts->gid = 0;
84a1b7d3
DC
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;
1e2d3bbc 84 case Opt_fsuid:
84a1b7d3
DC
85 if (match_int(&args[0], &option))
86 return 0;
87 opts->fs_low_uid = option;
88 break;
1e2d3bbc 89 case Opt_fsgid:
84a1b7d3
DC
90 if (match_int(&args[0], &option))
91 return 0;
92 opts->fs_low_gid = option;
93 break;
1e2d3bbc 94 case Opt_gid:
84a1b7d3
DC
95 if (match_int(&args[0], &option))
96 return 0;
6b6e896b 97 vfsopts->gid = option;
84a1b7d3 98 break;
1e2d3bbc
DR
99 case Opt_userid:
100 if (match_int(&args[0], &option))
101 return 0;
102 opts->fs_user_id = option;
84a1b7d3 103 break;
1e2d3bbc
DR
104 case Opt_mask:
105 if (match_int(&args[0], &option))
106 return 0;
6b6e896b 107 vfsopts->mask = option;
1e2d3bbc
DR
108 break;
109 case Opt_multiuser:
110 opts->multiuser = true;
84a1b7d3 111 break;
84a1b7d3
DC
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:
84a1b7d3
DC
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
6b6e896b
DR
138int 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
e76fbcd4 197#if 0
84a1b7d3
DC
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 */
204static 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}
e76fbcd4 223#endif
84a1b7d3 224
1e2d3bbc
DR
225DEFINE_MUTEX(sdcardfs_super_list_lock);
226LIST_HEAD(sdcardfs_super_list);
227EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock);
228EXPORT_SYMBOL_GPL(sdcardfs_super_list);
229
84a1b7d3
DC
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 */
6b6e896b
DR
234static int sdcardfs_read_super(struct vfsmount *mnt, struct super_block *sb,
235 const char *dev_name, void *raw_data, int silent)
84a1b7d3
DC
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;
6b6e896b 242 struct sdcardfs_vfsmount_options *mnt_opt = mnt->data;
e76fbcd4 243 struct inode *inode;
84a1b7d3
DC
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);
4d70f731 256 printk(KERN_INFO "sdcardfs: mnt -> %p\n", mnt);
84a1b7d3
DC
257
258 /* parse lower path */
259 err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
260 &lower_path);
261 if (err) {
1e2d3bbc 262 printk(KERN_ERR "sdcardfs: error accessing lower directory '%s'\n", dev_name);
84a1b7d3
DC
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;
84a1b7d3 275 /* parse options */
6b6e896b 276 err = parse_options(sb, raw_data, silent, &debug, mnt_opt, &sb_info->options);
84a1b7d3
DC
277 if (err) {
278 printk(KERN_ERR "sdcardfs: invalid options\n");
279 goto out_freesbi;
280 }
281
84a1b7d3
DC
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
e76fbcd4 299 /* get a new inode and allocate our root dentry */
1e2d3bbc 300 inode = sdcardfs_iget(sb, lower_path.dentry->d_inode, 0);
e76fbcd4
DC
301 if (IS_ERR(inode)) {
302 err = PTR_ERR(inode);
303 goto out_sput;
304 }
305 sb->s_root = d_make_root(inode);
84a1b7d3
DC
306 if (!sb->s_root) {
307 err = -ENOMEM;
e76fbcd4 308 goto out_iput;
84a1b7d3 309 }
e76fbcd4 310 d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
84a1b7d3
DC
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
e76fbcd4
DC
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);
84a1b7d3 327
e76fbcd4 328 /* setup permission policy */
1e2d3bbc
DR
329 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
330 mutex_lock(&sdcardfs_super_list_lock);
331 if(sb_info->options.multiuser) {
640e5265 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));
1e2d3bbc
DR
333 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
334 /*err = prepare_dir(sb_info->obbpath_s,
e76fbcd4 335 sb_info->options.fs_low_uid,
1e2d3bbc
DR
336 sb_info->options.fs_low_gid, 00755);*/
337 } else {
cae2e7f2 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));
1e2d3bbc 339 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
84a1b7d3 340 }
4d70f731 341 fixup_tmp_permissions(d_inode(sb->s_root));
1e2d3bbc
DR
342 sb_info->sb = sb;
343 list_add(&sb_info->list, &sdcardfs_super_list);
344 mutex_unlock(&sdcardfs_super_list_lock);
e76fbcd4
DC
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 */
84a1b7d3 350
e76fbcd4 351 /* no longer needed: free_dentry_private_data(sb->s_root); */
84a1b7d3
DC
352out_freeroot:
353 dput(sb->s_root);
e76fbcd4
DC
354out_iput:
355 iput(inode);
84a1b7d3
DC
356out_sput:
357 /* drop refs we took earlier */
358 atomic_dec(&lower_sb->s_active);
84a1b7d3
DC
359out_freesbi:
360 kfree(SDCARDFS_SB(sb));
361 sb->s_fs_info = NULL;
362out_free:
363 path_put(&lower_path);
364
365out:
366 return err;
367}
368
369/* A feature which supports mount_nodev() with options */
6b6e896b
DR
370static 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))
84a1b7d3
DC
373
374{
375 int error;
e76fbcd4 376 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
84a1b7d3
DC
377
378 if (IS_ERR(s))
379 return ERR_CAST(s);
380
381 s->s_flags = flags;
382
6b6e896b 383 error = fill_super(mnt, s, dev_name, data, flags & MS_SILENT ? 1 : 0);
84a1b7d3
DC
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
6b6e896b
DR
392static struct dentry *sdcardfs_mount(struct vfsmount *mnt,
393 struct file_system_type *fs_type, int flags,
84a1b7d3
DC
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 */
6b6e896b
DR
400 return mount_nodev_with_options(mnt, fs_type, flags, dev_name,
401 raw_data, sdcardfs_read_super);
402}
403
404static 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
411void *sdcardfs_alloc_mnt_data(void) {
412 return kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL);
84a1b7d3
DC
413}
414
1e2d3bbc
DR
415void 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
84a1b7d3
DC
426static struct file_system_type sdcardfs_fs_type = {
427 .owner = THIS_MODULE,
428 .name = SDCARDFS_NAME,
6b6e896b
DR
429 .mount = sdcardfs_mount_wrn,
430 .mount2 = sdcardfs_mount,
431 .alloc_mnt_data = sdcardfs_alloc_mnt_data,
1e2d3bbc 432 .kill_sb = sdcardfs_kill_sb,
e76fbcd4 433 .fs_flags = 0,
84a1b7d3
DC
434};
435
436static 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);
452out:
453 if (err) {
454 sdcardfs_destroy_inode_cache();
455 sdcardfs_destroy_dentry_cache();
456 packagelist_exit();
457 }
458 return err;
459}
460
461static 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
470MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
471 " (http://www.fsl.cs.sunysb.edu/)");
472MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
473 " (http://wrapfs.filesystems.org/)");
474MODULE_LICENSE("GPL");
475
476module_init(init_sdcardfs_fs);
477module_exit(exit_sdcardfs_fs);