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_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_gid_derivation, "derive_gid"},
48 {Opt_reserved_mb, "reserved_mb=%u"},
49 {Opt_err, NULL}
50 };
51
parse_options(struct super_block * sb,char * options,int silent,int * debug,struct sdcardfs_vfsmount_options * vfsopts,struct sdcardfs_mount_options * opts)52 static int parse_options(struct super_block *sb, char *options, int silent,
53 int *debug, struct sdcardfs_vfsmount_options *vfsopts,
54 struct sdcardfs_mount_options *opts)
55 {
56 char *p;
57 substring_t args[MAX_OPT_ARGS];
58 int option;
59
60 /* by default, we use AID_MEDIA_RW as uid, gid */
61 opts->fs_low_uid = AID_MEDIA_RW;
62 opts->fs_low_gid = AID_MEDIA_RW;
63 vfsopts->mask = 0;
64 opts->multiuser = false;
65 opts->fs_user_id = 0;
66 vfsopts->gid = 0;
67 /* by default, 0MB is reserved */
68 opts->reserved_mb = 0;
69 /* by default, gid derivation is off */
70 opts->gid_derivation = false;
71
72 *debug = 0;
73
74 if (!options)
75 return 0;
76
77 while ((p = strsep(&options, ",")) != NULL) {
78 int token;
79
80 if (!*p)
81 continue;
82
83 token = match_token(p, sdcardfs_tokens, args);
84
85 switch (token) {
86 case Opt_debug:
87 *debug = 1;
88 break;
89 case Opt_fsuid:
90 if (match_int(&args[0], &option))
91 return 0;
92 opts->fs_low_uid = option;
93 break;
94 case Opt_fsgid:
95 if (match_int(&args[0], &option))
96 return 0;
97 opts->fs_low_gid = option;
98 break;
99 case Opt_gid:
100 if (match_int(&args[0], &option))
101 return 0;
102 vfsopts->gid = option;
103 break;
104 case Opt_userid:
105 if (match_int(&args[0], &option))
106 return 0;
107 opts->fs_user_id = option;
108 break;
109 case Opt_mask:
110 if (match_int(&args[0], &option))
111 return 0;
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 case Opt_gid_derivation:
123 opts->gid_derivation = true;
124 break;
125 /* unknown option */
126 default:
127 if (!silent)
128 pr_err("Unrecognized mount option \"%s\" or missing value", p);
129 return -EINVAL;
130 }
131 }
132
133 if (*debug) {
134 pr_info("sdcardfs : options - debug:%d\n", *debug);
135 pr_info("sdcardfs : options - uid:%d\n",
136 opts->fs_low_uid);
137 pr_info("sdcardfs : options - gid:%d\n",
138 opts->fs_low_gid);
139 }
140
141 return 0;
142 }
143
parse_options_remount(struct super_block * sb,char * options,int silent,struct sdcardfs_vfsmount_options * vfsopts)144 int parse_options_remount(struct super_block *sb, char *options, int silent,
145 struct sdcardfs_vfsmount_options *vfsopts)
146 {
147 char *p;
148 substring_t args[MAX_OPT_ARGS];
149 int option;
150 int debug;
151
152 if (!options)
153 return 0;
154
155 while ((p = strsep(&options, ",")) != NULL) {
156 int token;
157
158 if (!*p)
159 continue;
160
161 token = match_token(p, sdcardfs_tokens, args);
162
163 switch (token) {
164 case Opt_debug:
165 debug = 1;
166 break;
167 case Opt_gid:
168 if (match_int(&args[0], &option))
169 return 0;
170 vfsopts->gid = option;
171
172 break;
173 case Opt_mask:
174 if (match_int(&args[0], &option))
175 return 0;
176 vfsopts->mask = option;
177 break;
178 case Opt_multiuser:
179 case Opt_userid:
180 case Opt_fsuid:
181 case Opt_fsgid:
182 case Opt_reserved_mb:
183 pr_warn("Option \"%s\" can't be changed during remount\n", p);
184 break;
185 /* unknown option */
186 default:
187 if (!silent)
188 pr_err("Unrecognized mount option \"%s\" or missing value", p);
189 return -EINVAL;
190 }
191 }
192
193 if (debug) {
194 pr_info("sdcardfs : options - debug:%d\n", debug);
195 pr_info("sdcardfs : options - gid:%d\n", vfsopts->gid);
196 pr_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 EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock);
232 LIST_HEAD(sdcardfs_super_list);
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 */
sdcardfs_read_super(struct vfsmount * mnt,struct super_block * sb,const char * dev_name,void * raw_data,int silent)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 pr_info("sdcardfs version 2.0\n");
251
252 if (!dev_name) {
253 pr_err("sdcardfs: read_super: missing dev_name argument\n");
254 err = -EINVAL;
255 goto out;
256 }
257
258 pr_info("sdcardfs: dev_name -> %s\n", dev_name);
259 pr_info("sdcardfs: options -> %s\n", (char *)raw_data);
260 pr_info("sdcardfs: mnt -> %p\n", mnt);
261
262 /* parse lower path */
263 err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
264 &lower_path);
265 if (err) {
266 pr_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 pr_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 pr_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(sb->s_root->d_inode, PERM_PRE_ROOT,
337 sb_info->options.fs_user_id, AID_ROOT,
338 false, SDCARDFS_I(sb->s_root->d_inode)->data);
339 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
340 } else {
341 setup_derived_state(sb->s_root->d_inode, PERM_ROOT,
342 sb_info->options.fs_user_id, AID_ROOT,
343 false, SDCARDFS_I(sb->s_root->d_inode)->data);
344 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
345 }
346 fixup_tmp_permissions(sb->s_root->d_inode);
347 sb_info->sb = sb;
348 list_add(&sb_info->list, &sdcardfs_super_list);
349 mutex_unlock(&sdcardfs_super_list_lock);
350
351 if (!silent)
352 pr_info("sdcardfs: mounted on top of %s type %s\n",
353 dev_name, lower_sb->s_type->name);
354 goto out; /* all is well */
355
356 /* no longer needed: free_dentry_private_data(sb->s_root); */
357 out_freeroot:
358 dput(sb->s_root);
359 out_iput:
360 iput(inode);
361 out_sput:
362 /* drop refs we took earlier */
363 atomic_dec(&lower_sb->s_active);
364 out_freesbi:
365 kfree(SDCARDFS_SB(sb));
366 sb->s_fs_info = NULL;
367 out_free:
368 path_put(&lower_path);
369
370 out:
371 return err;
372 }
373
374 struct sdcardfs_mount_private {
375 struct vfsmount *mnt;
376 const char *dev_name;
377 void *raw_data;
378 };
379
__sdcardfs_fill_super(struct super_block * sb,void * _priv,int silent)380 static int __sdcardfs_fill_super(
381 struct super_block *sb,
382 void *_priv, int silent)
383 {
384 struct sdcardfs_mount_private *priv = _priv;
385
386 return sdcardfs_read_super(priv->mnt,
387 sb, priv->dev_name, priv->raw_data, silent);
388 }
389
sdcardfs_mount(struct vfsmount * mnt,struct file_system_type * fs_type,int flags,const char * dev_name,void * raw_data)390 static struct dentry *sdcardfs_mount(struct vfsmount *mnt,
391 struct file_system_type *fs_type, int flags,
392 const char *dev_name, void *raw_data)
393 {
394 struct sdcardfs_mount_private priv = {
395 .mnt = mnt,
396 .dev_name = dev_name,
397 .raw_data = raw_data
398 };
399
400 return mount_nodev(fs_type, flags,
401 &priv, __sdcardfs_fill_super);
402 }
403
sdcardfs_mount_wrn(struct file_system_type * fs_type,int flags,const char * dev_name,void * raw_data)404 static struct dentry *sdcardfs_mount_wrn(struct file_system_type *fs_type,
405 int flags, 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
sdcardfs_alloc_mnt_data(void)411 void *sdcardfs_alloc_mnt_data(void)
412 {
413 return kmalloc(sizeof(struct sdcardfs_vfsmount_options), GFP_KERNEL);
414 }
415
sdcardfs_kill_sb(struct super_block * sb)416 void sdcardfs_kill_sb(struct super_block *sb)
417 {
418 struct sdcardfs_sb_info *sbi;
419
420 if (sb->s_magic == SDCARDFS_SUPER_MAGIC) {
421 sbi = SDCARDFS_SB(sb);
422 mutex_lock(&sdcardfs_super_list_lock);
423 list_del(&sbi->list);
424 mutex_unlock(&sdcardfs_super_list_lock);
425 }
426 kill_anon_super(sb);
427 }
428
429 static struct file_system_type sdcardfs_fs_type = {
430 .owner = THIS_MODULE,
431 .name = SDCARDFS_NAME,
432 .mount = sdcardfs_mount_wrn,
433 .mount2 = sdcardfs_mount,
434 .alloc_mnt_data = sdcardfs_alloc_mnt_data,
435 .kill_sb = sdcardfs_kill_sb,
436 .fs_flags = 0,
437 };
438 MODULE_ALIAS_FS(SDCARDFS_NAME);
439
init_sdcardfs_fs(void)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
exit_sdcardfs_fs(void)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 /* Original wrapfs authors */
475 MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University (http://www.fsl.cs.sunysb.edu/)");
476
477 /* Original sdcardfs authors */
478 MODULE_AUTHOR("Woojoong Lee, Daeho Jeong, Kitae Lee, Yeongjin Gil System Memory Lab., Samsung Electronics");
479
480 /* Current maintainer */
481 MODULE_AUTHOR("Daniel Rosenberg, Google");
482 MODULE_DESCRIPTION("Sdcardfs " SDCARDFS_VERSION);
483 MODULE_LICENSE("GPL");
484
485 module_init(init_sdcardfs_fs);
486 module_exit(exit_sdcardfs_fs);
487