sdcardfs: limit stacking depth
[GitHub/LineageOS/android_kernel_samsung_universal7580.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 sb->s_stack_depth = lower_sb->s_stack_depth + 1;
288 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
289 pr_err("sdcardfs: maximum fs stacking depth exceeded\n");
290 err = -EINVAL;
291 goto out_sput;
292 }
293
294 /* inherit maxbytes from lower file system */
295 sb->s_maxbytes = lower_sb->s_maxbytes;
296
297 /*
298 * Our c/m/atime granularity is 1 ns because we may stack on file
299 * systems whose granularity is as good.
300 */
301 sb->s_time_gran = 1;
302
303 sb->s_magic = SDCARDFS_SUPER_MAGIC;
304 sb->s_op = &sdcardfs_sops;
305
306 /* get a new inode and allocate our root dentry */
307 inode = sdcardfs_iget(sb, lower_path.dentry->d_inode, 0);
308 if (IS_ERR(inode)) {
309 err = PTR_ERR(inode);
310 goto out_sput;
311 }
312 sb->s_root = d_make_root(inode);
313 if (!sb->s_root) {
314 err = -ENOMEM;
315 goto out_iput;
316 }
317 d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
318
319 /* link the upper and lower dentries */
320 sb->s_root->d_fsdata = NULL;
321 err = new_dentry_private_data(sb->s_root);
322 if (err)
323 goto out_freeroot;
324
325 /* set the lower dentries for s_root */
326 sdcardfs_set_lower_path(sb->s_root, &lower_path);
327
328 /*
329 * No need to call interpose because we already have a positive
330 * dentry, which was instantiated by d_make_root. Just need to
331 * d_rehash it.
332 */
333 d_rehash(sb->s_root);
334
335 /* setup permission policy */
336 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
337 mutex_lock(&sdcardfs_super_list_lock);
338 if(sb_info->options.multiuser) {
339 setup_derived_state(sb->s_root->d_inode, PERM_PRE_ROOT, sb_info->options.fs_user_id, AID_ROOT, false, sb->s_root->d_inode);
340 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
341 /*err = prepare_dir(sb_info->obbpath_s,
342 sb_info->options.fs_low_uid,
343 sb_info->options.fs_low_gid, 00755);*/
344 } else {
345 setup_derived_state(sb->s_root->d_inode, PERM_ROOT, sb_info->options.fs_user_id, AID_ROOT, false, sb->s_root->d_inode);
346 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
347 }
348 fixup_tmp_permissions(sb->s_root->d_inode);
349 sb_info->sb = sb;
350 list_add(&sb_info->list, &sdcardfs_super_list);
351 mutex_unlock(&sdcardfs_super_list_lock);
352
353 if (!silent)
354 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
355 dev_name, lower_sb->s_type->name);
356 goto out; /* all is well */
357
358 /* no longer needed: free_dentry_private_data(sb->s_root); */
359 out_freeroot:
360 dput(sb->s_root);
361 out_iput:
362 iput(inode);
363 out_sput:
364 /* drop refs we took earlier */
365 atomic_dec(&lower_sb->s_active);
366 out_freesbi:
367 kfree(SDCARDFS_SB(sb));
368 sb->s_fs_info = NULL;
369 out_free:
370 path_put(&lower_path);
371
372 out:
373 return err;
374 }
375
376 /* A feature which supports mount_nodev() with options */
377 static struct dentry *mount_nodev_with_options(struct vfsmount *mnt,
378 struct file_system_type *fs_type, int flags, const char *dev_name, void *data,
379 int (*fill_super)(struct vfsmount *, struct super_block *, const char *, void *, int))
380
381 {
382 int error;
383 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
384
385 if (IS_ERR(s))
386 return ERR_CAST(s);
387
388 s->s_flags = flags;
389
390 error = fill_super(mnt, s, dev_name, data, flags & MS_SILENT ? 1 : 0);
391 if (error) {
392 deactivate_locked_super(s);
393 return ERR_PTR(error);
394 }
395 s->s_flags |= MS_ACTIVE;
396 return dget(s->s_root);
397 }
398
399 static struct dentry *sdcardfs_mount(struct vfsmount *mnt,
400 struct file_system_type *fs_type, int flags,
401 const char *dev_name, void *raw_data)
402 {
403 /*
404 * dev_name is a lower_path_name,
405 * raw_data is a option string.
406 */
407 return mount_nodev_with_options(mnt, fs_type, flags, dev_name,
408 raw_data, sdcardfs_read_super);
409 }
410
411 static struct dentry *sdcardfs_mount_wrn(struct file_system_type *fs_type, int flags,
412 const char *dev_name, void *raw_data)
413 {
414 WARN(1, "sdcardfs does not support mount. Use mount2.\n");
415 return ERR_PTR(-EINVAL);
416 }
417
418 void *sdcardfs_alloc_mnt_data(void) {
419 return kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL);
420 }
421
422 void sdcardfs_kill_sb(struct super_block *sb) {
423 struct sdcardfs_sb_info *sbi;
424 if (sb->s_magic == SDCARDFS_SUPER_MAGIC) {
425 sbi = SDCARDFS_SB(sb);
426 mutex_lock(&sdcardfs_super_list_lock);
427 list_del(&sbi->list);
428 mutex_unlock(&sdcardfs_super_list_lock);
429 }
430 generic_shutdown_super(sb);
431 }
432
433 static struct file_system_type sdcardfs_fs_type = {
434 .owner = THIS_MODULE,
435 .name = SDCARDFS_NAME,
436 .mount = sdcardfs_mount_wrn,
437 .mount2 = sdcardfs_mount,
438 .alloc_mnt_data = sdcardfs_alloc_mnt_data,
439 .kill_sb = sdcardfs_kill_sb,
440 .fs_flags = 0,
441 };
442
443 static int __init init_sdcardfs_fs(void)
444 {
445 int err;
446
447 pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
448
449 err = sdcardfs_init_inode_cache();
450 if (err)
451 goto out;
452 err = sdcardfs_init_dentry_cache();
453 if (err)
454 goto out;
455 err = packagelist_init();
456 if (err)
457 goto out;
458 err = register_filesystem(&sdcardfs_fs_type);
459 out:
460 if (err) {
461 sdcardfs_destroy_inode_cache();
462 sdcardfs_destroy_dentry_cache();
463 packagelist_exit();
464 }
465 return err;
466 }
467
468 static void __exit exit_sdcardfs_fs(void)
469 {
470 sdcardfs_destroy_inode_cache();
471 sdcardfs_destroy_dentry_cache();
472 packagelist_exit();
473 unregister_filesystem(&sdcardfs_fs_type);
474 pr_info("Completed sdcardfs module unload\n");
475 }
476
477 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
478 " (http://www.fsl.cs.sunysb.edu/)");
479 MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
480 " (http://wrapfs.filesystems.org/)");
481 MODULE_LICENSE("GPL");
482
483 module_init(init_sdcardfs_fs);
484 module_exit(exit_sdcardfs_fs);