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