blob: a6522286d7314b57e2269e31ab31ee4ec5bcd5dc [file] [log] [blame]
Daniel Campello9fbe8662015-07-20 16:23:50 -07001/*
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 {
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080027 Opt_fsuid,
28 Opt_fsgid,
Daniel Campello9fbe8662015-07-20 16:23:50 -070029 Opt_gid,
Daniel Campello9fbe8662015-07-20 16:23:50 -070030 Opt_debug,
Daniel Campello9fbe8662015-07-20 16:23:50 -070031 Opt_lower_fs,
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080032 Opt_mask,
33 Opt_multiuser, // May need?
34 Opt_userid,
Daniel Campello9fbe8662015-07-20 16:23:50 -070035 Opt_reserved_mb,
36 Opt_err,
37};
38
39static const match_table_t sdcardfs_tokens = {
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080040 {Opt_fsuid, "fsuid=%u"},
41 {Opt_fsgid, "fsgid=%u"},
Daniel Campello9fbe8662015-07-20 16:23:50 -070042 {Opt_gid, "gid=%u"},
Daniel Campello9fbe8662015-07-20 16:23:50 -070043 {Opt_debug, "debug"},
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080044 {Opt_mask, "mask=%u"},
45 {Opt_userid, "userid=%d"},
46 {Opt_multiuser, "multiuser"},
Daniel Campello9fbe8662015-07-20 16:23:50 -070047 {Opt_reserved_mb, "reserved_mb=%u"},
48 {Opt_err, NULL}
49};
50
51static int parse_options(struct super_block *sb, char *options, int silent,
52 int *debug, struct sdcardfs_mount_options *opts)
53{
54 char *p;
55 substring_t args[MAX_OPT_ARGS];
56 int option;
Daniel Campello9fbe8662015-07-20 16:23:50 -070057
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;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080061 opts->mask = 0;
62 opts->multiuser = false;
63 opts->fs_user_id = 0;
64 opts->gid = 0;
Daniel Campello9fbe8662015-07-20 16:23:50 -070065 /* 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;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080084 case Opt_fsuid:
Daniel Campello9fbe8662015-07-20 16:23:50 -070085 if (match_int(&args[0], &option))
86 return 0;
87 opts->fs_low_uid = option;
88 break;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080089 case Opt_fsgid:
Daniel Campello9fbe8662015-07-20 16:23:50 -070090 if (match_int(&args[0], &option))
91 return 0;
92 opts->fs_low_gid = option;
93 break;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080094 case Opt_gid:
Daniel Campello9fbe8662015-07-20 16:23:50 -070095 if (match_int(&args[0], &option))
96 return 0;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080097 opts->gid = option;
Daniel Campello9fbe8662015-07-20 16:23:50 -070098 break;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080099 case Opt_userid:
100 if (match_int(&args[0], &option))
101 return 0;
102 opts->fs_user_id = option;
Daniel Campello9fbe8662015-07-20 16:23:50 -0700103 break;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800104 case Opt_mask:
105 if (match_int(&args[0], &option))
106 return 0;
107 opts->mask = option;
108 break;
109 case Opt_multiuser:
110 opts->multiuser = true;
Daniel Campello9fbe8662015-07-20 16:23:50 -0700111 break;
Daniel Campello9fbe8662015-07-20 16:23:50 -0700112 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:
Daniel Campello9fbe8662015-07-20 16:23:50 -0700119 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
Daniel Campelloe84c5252015-07-20 16:27:37 -0700138#if 0
Daniel Campello9fbe8662015-07-20 16:23:50 -0700139/*
140 * our custom d_alloc_root work-alike
141 *
142 * we can't use d_alloc_root if we want to use our own interpose function
143 * unchanged, so we simply call our own "fake" d_alloc_root
144 */
145static struct dentry *sdcardfs_d_alloc_root(struct super_block *sb)
146{
147 struct dentry *ret = NULL;
148
149 if (sb) {
150 static const struct qstr name = {
151 .name = "/",
152 .len = 1
153 };
154
155 ret = d_alloc(NULL, &name);
156 if (ret) {
157 d_set_d_op(ret, &sdcardfs_ci_dops);
158 ret->d_sb = sb;
159 ret->d_parent = ret;
160 }
161 }
162 return ret;
163}
Daniel Campelloe84c5252015-07-20 16:27:37 -0700164#endif
Daniel Campello9fbe8662015-07-20 16:23:50 -0700165
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800166DEFINE_MUTEX(sdcardfs_super_list_lock);
167LIST_HEAD(sdcardfs_super_list);
168EXPORT_SYMBOL_GPL(sdcardfs_super_list_lock);
169EXPORT_SYMBOL_GPL(sdcardfs_super_list);
170
Daniel Campello9fbe8662015-07-20 16:23:50 -0700171/*
172 * There is no need to lock the sdcardfs_super_info's rwsem as there is no
173 * way anyone can have a reference to the superblock at this point in time.
174 */
175static int sdcardfs_read_super(struct super_block *sb, const char *dev_name,
176 void *raw_data, int silent)
177{
178 int err = 0;
179 int debug;
180 struct super_block *lower_sb;
181 struct path lower_path;
182 struct sdcardfs_sb_info *sb_info;
Daniel Campelloe84c5252015-07-20 16:27:37 -0700183 struct inode *inode;
Daniel Campello9fbe8662015-07-20 16:23:50 -0700184
185 printk(KERN_INFO "sdcardfs version 2.0\n");
186
187 if (!dev_name) {
188 printk(KERN_ERR
189 "sdcardfs: read_super: missing dev_name argument\n");
190 err = -EINVAL;
191 goto out;
192 }
193
194 printk(KERN_INFO "sdcardfs: dev_name -> %s\n", dev_name);
195 printk(KERN_INFO "sdcardfs: options -> %s\n", (char *)raw_data);
196
197 /* parse lower path */
198 err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
199 &lower_path);
200 if (err) {
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800201 printk(KERN_ERR "sdcardfs: error accessing lower directory '%s'\n", dev_name);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700202 goto out;
203 }
204
205 /* allocate superblock private data */
206 sb->s_fs_info = kzalloc(sizeof(struct sdcardfs_sb_info), GFP_KERNEL);
207 if (!SDCARDFS_SB(sb)) {
208 printk(KERN_CRIT "sdcardfs: read_super: out of memory\n");
209 err = -ENOMEM;
210 goto out_free;
211 }
212
213 sb_info = sb->s_fs_info;
Daniel Campello9fbe8662015-07-20 16:23:50 -0700214 /* parse options */
215 err = parse_options(sb, raw_data, silent, &debug, &sb_info->options);
216 if (err) {
217 printk(KERN_ERR "sdcardfs: invalid options\n");
218 goto out_freesbi;
219 }
220
Daniel Campello9fbe8662015-07-20 16:23:50 -0700221 /* set the lower superblock field of upper superblock */
222 lower_sb = lower_path.dentry->d_sb;
223 atomic_inc(&lower_sb->s_active);
224 sdcardfs_set_lower_super(sb, lower_sb);
225
226 /* inherit maxbytes from lower file system */
227 sb->s_maxbytes = lower_sb->s_maxbytes;
228
229 /*
230 * Our c/m/atime granularity is 1 ns because we may stack on file
231 * systems whose granularity is as good.
232 */
233 sb->s_time_gran = 1;
234
235 sb->s_magic = SDCARDFS_SUPER_MAGIC;
236 sb->s_op = &sdcardfs_sops;
237
Daniel Campelloe84c5252015-07-20 16:27:37 -0700238 /* get a new inode and allocate our root dentry */
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800239 inode = sdcardfs_iget(sb, lower_path.dentry->d_inode, 0);
Daniel Campelloe84c5252015-07-20 16:27:37 -0700240 if (IS_ERR(inode)) {
241 err = PTR_ERR(inode);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700242 goto out_sput;
243 }
Daniel Campelloe84c5252015-07-20 16:27:37 -0700244 sb->s_root = d_make_root(inode);
245 if (!sb->s_root) {
246 err = -ENOMEM;
247 goto out_iput;
248 }
249 d_set_d_op(sb->s_root, &sdcardfs_ci_dops);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700250
251 /* link the upper and lower dentries */
252 sb->s_root->d_fsdata = NULL;
253 err = new_dentry_private_data(sb->s_root);
254 if (err)
255 goto out_freeroot;
256
257 /* set the lower dentries for s_root */
258 sdcardfs_set_lower_path(sb->s_root, &lower_path);
259
Daniel Campelloe84c5252015-07-20 16:27:37 -0700260 /*
261 * No need to call interpose because we already have a positive
262 * dentry, which was instantiated by d_make_root. Just need to
263 * d_rehash it.
264 */
265 d_rehash(sb->s_root);
266
267 /* setup permission policy */
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800268 sb_info->obbpath_s = kzalloc(PATH_MAX, GFP_KERNEL);
269 mutex_lock(&sdcardfs_super_list_lock);
270 if(sb_info->options.multiuser) {
271 setup_derived_state(sb->s_root->d_inode, PERM_PRE_ROOT, sb_info->options.fs_user_id, AID_ROOT, false);
272 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/obb", dev_name);
273 /*err = prepare_dir(sb_info->obbpath_s,
Daniel Campelloe84c5252015-07-20 16:27:37 -0700274 sb_info->options.fs_low_uid,
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800275 sb_info->options.fs_low_gid, 00755);*/
276 } else {
277 setup_derived_state(sb->s_root->d_inode, PERM_ROOT, sb_info->options.fs_low_uid, AID_ROOT, false);
278 snprintf(sb_info->obbpath_s, PATH_MAX, "%s/Android/obb", dev_name);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700279 }
Daniel Campelloe84c5252015-07-20 16:27:37 -0700280 fix_derived_permission(sb->s_root->d_inode);
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800281 sb_info->sb = sb;
282 list_add(&sb_info->list, &sdcardfs_super_list);
283 mutex_unlock(&sdcardfs_super_list_lock);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700284
Daniel Campelloe84c5252015-07-20 16:27:37 -0700285 if (!silent)
286 printk(KERN_INFO "sdcardfs: mounted on top of %s type %s\n",
287 dev_name, lower_sb->s_type->name);
288 goto out; /* all is well */
289
290 /* no longer needed: free_dentry_private_data(sb->s_root); */
Daniel Campello9fbe8662015-07-20 16:23:50 -0700291out_freeroot:
292 dput(sb->s_root);
Daniel Campelloe84c5252015-07-20 16:27:37 -0700293out_iput:
294 iput(inode);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700295out_sput:
296 /* drop refs we took earlier */
297 atomic_dec(&lower_sb->s_active);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700298out_freesbi:
299 kfree(SDCARDFS_SB(sb));
300 sb->s_fs_info = NULL;
301out_free:
302 path_put(&lower_path);
303
304out:
305 return err;
306}
307
308/* A feature which supports mount_nodev() with options */
309static struct dentry *mount_nodev_with_options(struct file_system_type *fs_type,
310 int flags, const char *dev_name, void *data,
311 int (*fill_super)(struct super_block *, const char *, void *, int))
312
313{
314 int error;
Daniel Campelloe84c5252015-07-20 16:27:37 -0700315 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700316
317 if (IS_ERR(s))
318 return ERR_CAST(s);
319
320 s->s_flags = flags;
321
322 error = fill_super(s, dev_name, data, flags & MS_SILENT ? 1 : 0);
323 if (error) {
324 deactivate_locked_super(s);
325 return ERR_PTR(error);
326 }
327 s->s_flags |= MS_ACTIVE;
328 return dget(s->s_root);
329}
330
331struct dentry *sdcardfs_mount(struct file_system_type *fs_type, int flags,
332 const char *dev_name, void *raw_data)
333{
334 /*
335 * dev_name is a lower_path_name,
336 * raw_data is a option string.
337 */
338 return mount_nodev_with_options(fs_type, flags, dev_name,
339 raw_data, sdcardfs_read_super);
340}
341
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800342void sdcardfs_kill_sb(struct super_block *sb) {
343 struct sdcardfs_sb_info *sbi;
344 if (sb->s_magic == SDCARDFS_SUPER_MAGIC) {
345 sbi = SDCARDFS_SB(sb);
346 mutex_lock(&sdcardfs_super_list_lock);
347 list_del(&sbi->list);
348 mutex_unlock(&sdcardfs_super_list_lock);
349 }
350 generic_shutdown_super(sb);
351}
352
Daniel Campello9fbe8662015-07-20 16:23:50 -0700353static struct file_system_type sdcardfs_fs_type = {
354 .owner = THIS_MODULE,
355 .name = SDCARDFS_NAME,
356 .mount = sdcardfs_mount,
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800357 .kill_sb = sdcardfs_kill_sb,
Daniel Campelloe84c5252015-07-20 16:27:37 -0700358 .fs_flags = 0,
Daniel Campello9fbe8662015-07-20 16:23:50 -0700359};
360
361static int __init init_sdcardfs_fs(void)
362{
363 int err;
364
365 pr_info("Registering sdcardfs " SDCARDFS_VERSION "\n");
366
367 err = sdcardfs_init_inode_cache();
368 if (err)
369 goto out;
370 err = sdcardfs_init_dentry_cache();
371 if (err)
372 goto out;
373 err = packagelist_init();
374 if (err)
375 goto out;
376 err = register_filesystem(&sdcardfs_fs_type);
377out:
378 if (err) {
379 sdcardfs_destroy_inode_cache();
380 sdcardfs_destroy_dentry_cache();
381 packagelist_exit();
382 }
383 return err;
384}
385
386static void __exit exit_sdcardfs_fs(void)
387{
388 sdcardfs_destroy_inode_cache();
389 sdcardfs_destroy_dentry_cache();
390 packagelist_exit();
391 unregister_filesystem(&sdcardfs_fs_type);
392 pr_info("Completed sdcardfs module unload\n");
393}
394
395MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
396 " (http://www.fsl.cs.sunysb.edu/)");
397MODULE_DESCRIPTION("Wrapfs " SDCARDFS_VERSION
398 " (http://wrapfs.filesystems.org/)");
399MODULE_LICENSE("GPL");
400
401module_init(init_sdcardfs_fs);
402module_exit(exit_sdcardfs_fs);