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