xref: /openbmc/linux/drivers/android/binderfs.c (revision eaf271ea)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/compiler_types.h>
4 #include <linux/errno.h>
5 #include <linux/fs.h>
6 #include <linux/fsnotify.h>
7 #include <linux/gfp.h>
8 #include <linux/idr.h>
9 #include <linux/init.h>
10 #include <linux/ipc_namespace.h>
11 #include <linux/kdev_t.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/namei.h>
15 #include <linux/magic.h>
16 #include <linux/major.h>
17 #include <linux/miscdevice.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/mount.h>
21 #include <linux/fs_parser.h>
22 #include <linux/radix-tree.h>
23 #include <linux/sched.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock_types.h>
27 #include <linux/stddef.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/uaccess.h>
31 #include <linux/user_namespace.h>
32 #include <linux/xarray.h>
33 #include <uapi/asm-generic/errno-base.h>
34 #include <uapi/linux/android/binder.h>
35 #include <uapi/linux/android/binderfs.h>
36 
37 #include "binder_internal.h"
38 
39 #define FIRST_INODE 1
40 #define SECOND_INODE 2
41 #define INODE_OFFSET 3
42 #define BINDERFS_MAX_MINOR (1U << MINORBITS)
43 /* Ensure that the initial ipc namespace always has devices available. */
44 #define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
45 
46 static dev_t binderfs_dev;
47 static DEFINE_MUTEX(binderfs_minors_mutex);
48 static DEFINE_IDA(binderfs_minors);
49 
50 enum binderfs_param {
51 	Opt_max,
52 	Opt_stats_mode,
53 };
54 
55 enum binderfs_stats_mode {
56 	binderfs_stats_mode_unset,
57 	binderfs_stats_mode_global,
58 };
59 
60 struct binder_features {
61 	bool oneway_spam_detection;
62 	bool extended_error;
63 };
64 
65 static const struct constant_table binderfs_param_stats[] = {
66 	{ "global", binderfs_stats_mode_global },
67 	{}
68 };
69 
70 static const struct fs_parameter_spec binderfs_fs_parameters[] = {
71 	fsparam_u32("max",	Opt_max),
72 	fsparam_enum("stats",	Opt_stats_mode, binderfs_param_stats),
73 	{}
74 };
75 
76 static struct binder_features binder_features = {
77 	.oneway_spam_detection = true,
78 	.extended_error = true,
79 };
80 
81 static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
82 {
83 	return sb->s_fs_info;
84 }
85 
86 bool is_binderfs_device(const struct inode *inode)
87 {
88 	if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
89 		return true;
90 
91 	return false;
92 }
93 
94 /**
95  * binderfs_binder_device_create - allocate inode from super block of a
96  *                                 binderfs mount
97  * @ref_inode: inode from wich the super block will be taken
98  * @userp:     buffer to copy information about new device for userspace to
99  * @req:       struct binderfs_device as copied from userspace
100  *
101  * This function allocates a new binder_device and reserves a new minor
102  * number for it.
103  * Minor numbers are limited and tracked globally in binderfs_minors. The
104  * function will stash a struct binder_device for the specific binder
105  * device in i_private of the inode.
106  * It will go on to allocate a new inode from the super block of the
107  * filesystem mount, stash a struct binder_device in its i_private field
108  * and attach a dentry to that inode.
109  *
110  * Return: 0 on success, negative errno on failure
111  */
112 static int binderfs_binder_device_create(struct inode *ref_inode,
113 					 struct binderfs_device __user *userp,
114 					 struct binderfs_device *req)
115 {
116 	int minor, ret;
117 	struct dentry *dentry, *root;
118 	struct binder_device *device;
119 	char *name = NULL;
120 	size_t name_len;
121 	struct inode *inode = NULL;
122 	struct super_block *sb = ref_inode->i_sb;
123 	struct binderfs_info *info = sb->s_fs_info;
124 #if defined(CONFIG_IPC_NS)
125 	bool use_reserve = (info->ipc_ns == &init_ipc_ns);
126 #else
127 	bool use_reserve = true;
128 #endif
129 
130 	/* Reserve new minor number for the new device. */
131 	mutex_lock(&binderfs_minors_mutex);
132 	if (++info->device_count <= info->mount_opts.max)
133 		minor = ida_alloc_max(&binderfs_minors,
134 				      use_reserve ? BINDERFS_MAX_MINOR :
135 						    BINDERFS_MAX_MINOR_CAPPED,
136 				      GFP_KERNEL);
137 	else
138 		minor = -ENOSPC;
139 	if (minor < 0) {
140 		--info->device_count;
141 		mutex_unlock(&binderfs_minors_mutex);
142 		return minor;
143 	}
144 	mutex_unlock(&binderfs_minors_mutex);
145 
146 	ret = -ENOMEM;
147 	device = kzalloc(sizeof(*device), GFP_KERNEL);
148 	if (!device)
149 		goto err;
150 
151 	inode = new_inode(sb);
152 	if (!inode)
153 		goto err;
154 
155 	inode->i_ino = minor + INODE_OFFSET;
156 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
157 	init_special_inode(inode, S_IFCHR | 0600,
158 			   MKDEV(MAJOR(binderfs_dev), minor));
159 	inode->i_fop = &binder_fops;
160 	inode->i_uid = info->root_uid;
161 	inode->i_gid = info->root_gid;
162 
163 	req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */
164 	name_len = strlen(req->name);
165 	/* Make sure to include terminating NUL byte */
166 	name = kmemdup(req->name, name_len + 1, GFP_KERNEL);
167 	if (!name)
168 		goto err;
169 
170 	refcount_set(&device->ref, 1);
171 	device->binderfs_inode = inode;
172 	device->context.binder_context_mgr_uid = INVALID_UID;
173 	device->context.name = name;
174 	device->miscdev.name = name;
175 	device->miscdev.minor = minor;
176 	mutex_init(&device->context.context_mgr_node_lock);
177 
178 	req->major = MAJOR(binderfs_dev);
179 	req->minor = minor;
180 
181 	if (userp && copy_to_user(userp, req, sizeof(*req))) {
182 		ret = -EFAULT;
183 		goto err;
184 	}
185 
186 	root = sb->s_root;
187 	inode_lock(d_inode(root));
188 
189 	/* look it up */
190 	dentry = lookup_one_len(name, root, name_len);
191 	if (IS_ERR(dentry)) {
192 		inode_unlock(d_inode(root));
193 		ret = PTR_ERR(dentry);
194 		goto err;
195 	}
196 
197 	if (d_really_is_positive(dentry)) {
198 		/* already exists */
199 		dput(dentry);
200 		inode_unlock(d_inode(root));
201 		ret = -EEXIST;
202 		goto err;
203 	}
204 
205 	inode->i_private = device;
206 	d_instantiate(dentry, inode);
207 	fsnotify_create(root->d_inode, dentry);
208 	inode_unlock(d_inode(root));
209 
210 	return 0;
211 
212 err:
213 	kfree(name);
214 	kfree(device);
215 	mutex_lock(&binderfs_minors_mutex);
216 	--info->device_count;
217 	ida_free(&binderfs_minors, minor);
218 	mutex_unlock(&binderfs_minors_mutex);
219 	iput(inode);
220 
221 	return ret;
222 }
223 
224 /**
225  * binderfs_ctl_ioctl - handle binder device node allocation requests
226  *
227  * The request handler for the binder-control device. All requests operate on
228  * the binderfs mount the binder-control device resides in:
229  * - BINDER_CTL_ADD
230  *   Allocate a new binder device.
231  *
232  * Return: 0 on success, negative errno on failure
233  */
234 static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
235 			     unsigned long arg)
236 {
237 	int ret = -EINVAL;
238 	struct inode *inode = file_inode(file);
239 	struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
240 	struct binderfs_device device_req;
241 
242 	switch (cmd) {
243 	case BINDER_CTL_ADD:
244 		ret = copy_from_user(&device_req, device, sizeof(device_req));
245 		if (ret) {
246 			ret = -EFAULT;
247 			break;
248 		}
249 
250 		ret = binderfs_binder_device_create(inode, device, &device_req);
251 		break;
252 	default:
253 		break;
254 	}
255 
256 	return ret;
257 }
258 
259 static void binderfs_evict_inode(struct inode *inode)
260 {
261 	struct binder_device *device = inode->i_private;
262 	struct binderfs_info *info = BINDERFS_SB(inode->i_sb);
263 
264 	clear_inode(inode);
265 
266 	if (!S_ISCHR(inode->i_mode) || !device)
267 		return;
268 
269 	mutex_lock(&binderfs_minors_mutex);
270 	--info->device_count;
271 	ida_free(&binderfs_minors, device->miscdev.minor);
272 	mutex_unlock(&binderfs_minors_mutex);
273 
274 	if (refcount_dec_and_test(&device->ref)) {
275 		kfree(device->context.name);
276 		kfree(device);
277 	}
278 }
279 
280 static int binderfs_fs_context_parse_param(struct fs_context *fc,
281 					   struct fs_parameter *param)
282 {
283 	int opt;
284 	struct binderfs_mount_opts *ctx = fc->fs_private;
285 	struct fs_parse_result result;
286 
287 	opt = fs_parse(fc, binderfs_fs_parameters, param, &result);
288 	if (opt < 0)
289 		return opt;
290 
291 	switch (opt) {
292 	case Opt_max:
293 		if (result.uint_32 > BINDERFS_MAX_MINOR)
294 			return invalfc(fc, "Bad value for '%s'", param->key);
295 
296 		ctx->max = result.uint_32;
297 		break;
298 	case Opt_stats_mode:
299 		if (!capable(CAP_SYS_ADMIN))
300 			return -EPERM;
301 
302 		ctx->stats_mode = result.uint_32;
303 		break;
304 	default:
305 		return invalfc(fc, "Unsupported parameter '%s'", param->key);
306 	}
307 
308 	return 0;
309 }
310 
311 static int binderfs_fs_context_reconfigure(struct fs_context *fc)
312 {
313 	struct binderfs_mount_opts *ctx = fc->fs_private;
314 	struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);
315 
316 	if (info->mount_opts.stats_mode != ctx->stats_mode)
317 		return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");
318 
319 	info->mount_opts.stats_mode = ctx->stats_mode;
320 	info->mount_opts.max = ctx->max;
321 	return 0;
322 }
323 
324 static int binderfs_show_options(struct seq_file *seq, struct dentry *root)
325 {
326 	struct binderfs_info *info = BINDERFS_SB(root->d_sb);
327 
328 	if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
329 		seq_printf(seq, ",max=%d", info->mount_opts.max);
330 
331 	switch (info->mount_opts.stats_mode) {
332 	case binderfs_stats_mode_unset:
333 		break;
334 	case binderfs_stats_mode_global:
335 		seq_printf(seq, ",stats=global");
336 		break;
337 	}
338 
339 	return 0;
340 }
341 
342 static void binderfs_put_super(struct super_block *sb)
343 {
344 	struct binderfs_info *info = sb->s_fs_info;
345 
346 	if (info && info->ipc_ns)
347 		put_ipc_ns(info->ipc_ns);
348 
349 	kfree(info);
350 	sb->s_fs_info = NULL;
351 }
352 
353 static const struct super_operations binderfs_super_ops = {
354 	.evict_inode    = binderfs_evict_inode,
355 	.show_options	= binderfs_show_options,
356 	.statfs         = simple_statfs,
357 	.put_super	= binderfs_put_super,
358 };
359 
360 static inline bool is_binderfs_control_device(const struct dentry *dentry)
361 {
362 	struct binderfs_info *info = dentry->d_sb->s_fs_info;
363 
364 	return info->control_dentry == dentry;
365 }
366 
367 static int binderfs_rename(struct user_namespace *mnt_userns,
368 			   struct inode *old_dir, struct dentry *old_dentry,
369 			   struct inode *new_dir, struct dentry *new_dentry,
370 			   unsigned int flags)
371 {
372 	if (is_binderfs_control_device(old_dentry) ||
373 	    is_binderfs_control_device(new_dentry))
374 		return -EPERM;
375 
376 	return simple_rename(&init_user_ns, old_dir, old_dentry, new_dir,
377 			     new_dentry, flags);
378 }
379 
380 static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
381 {
382 	if (is_binderfs_control_device(dentry))
383 		return -EPERM;
384 
385 	return simple_unlink(dir, dentry);
386 }
387 
388 static const struct file_operations binder_ctl_fops = {
389 	.owner		= THIS_MODULE,
390 	.open		= nonseekable_open,
391 	.unlocked_ioctl	= binder_ctl_ioctl,
392 	.compat_ioctl	= binder_ctl_ioctl,
393 	.llseek		= noop_llseek,
394 };
395 
396 /**
397  * binderfs_binder_ctl_create - create a new binder-control device
398  * @sb: super block of the binderfs mount
399  *
400  * This function creates a new binder-control device node in the binderfs mount
401  * referred to by @sb.
402  *
403  * Return: 0 on success, negative errno on failure
404  */
405 static int binderfs_binder_ctl_create(struct super_block *sb)
406 {
407 	int minor, ret;
408 	struct dentry *dentry;
409 	struct binder_device *device;
410 	struct inode *inode = NULL;
411 	struct dentry *root = sb->s_root;
412 	struct binderfs_info *info = sb->s_fs_info;
413 #if defined(CONFIG_IPC_NS)
414 	bool use_reserve = (info->ipc_ns == &init_ipc_ns);
415 #else
416 	bool use_reserve = true;
417 #endif
418 
419 	device = kzalloc(sizeof(*device), GFP_KERNEL);
420 	if (!device)
421 		return -ENOMEM;
422 
423 	/* If we have already created a binder-control node, return. */
424 	if (info->control_dentry) {
425 		ret = 0;
426 		goto out;
427 	}
428 
429 	ret = -ENOMEM;
430 	inode = new_inode(sb);
431 	if (!inode)
432 		goto out;
433 
434 	/* Reserve a new minor number for the new device. */
435 	mutex_lock(&binderfs_minors_mutex);
436 	minor = ida_alloc_max(&binderfs_minors,
437 			      use_reserve ? BINDERFS_MAX_MINOR :
438 					    BINDERFS_MAX_MINOR_CAPPED,
439 			      GFP_KERNEL);
440 	mutex_unlock(&binderfs_minors_mutex);
441 	if (minor < 0) {
442 		ret = minor;
443 		goto out;
444 	}
445 
446 	inode->i_ino = SECOND_INODE;
447 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
448 	init_special_inode(inode, S_IFCHR | 0600,
449 			   MKDEV(MAJOR(binderfs_dev), minor));
450 	inode->i_fop = &binder_ctl_fops;
451 	inode->i_uid = info->root_uid;
452 	inode->i_gid = info->root_gid;
453 
454 	refcount_set(&device->ref, 1);
455 	device->binderfs_inode = inode;
456 	device->miscdev.minor = minor;
457 
458 	dentry = d_alloc_name(root, "binder-control");
459 	if (!dentry)
460 		goto out;
461 
462 	inode->i_private = device;
463 	info->control_dentry = dentry;
464 	d_add(dentry, inode);
465 
466 	return 0;
467 
468 out:
469 	kfree(device);
470 	iput(inode);
471 
472 	return ret;
473 }
474 
475 static const struct inode_operations binderfs_dir_inode_operations = {
476 	.lookup = simple_lookup,
477 	.rename = binderfs_rename,
478 	.unlink = binderfs_unlink,
479 };
480 
481 static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
482 {
483 	struct inode *ret;
484 
485 	ret = new_inode(sb);
486 	if (ret) {
487 		ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
488 		ret->i_mode = mode;
489 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
490 	}
491 	return ret;
492 }
493 
494 static struct dentry *binderfs_create_dentry(struct dentry *parent,
495 					     const char *name)
496 {
497 	struct dentry *dentry;
498 
499 	dentry = lookup_one_len(name, parent, strlen(name));
500 	if (IS_ERR(dentry))
501 		return dentry;
502 
503 	/* Return error if the file/dir already exists. */
504 	if (d_really_is_positive(dentry)) {
505 		dput(dentry);
506 		return ERR_PTR(-EEXIST);
507 	}
508 
509 	return dentry;
510 }
511 
512 void binderfs_remove_file(struct dentry *dentry)
513 {
514 	struct inode *parent_inode;
515 
516 	parent_inode = d_inode(dentry->d_parent);
517 	inode_lock(parent_inode);
518 	if (simple_positive(dentry)) {
519 		dget(dentry);
520 		simple_unlink(parent_inode, dentry);
521 		d_delete(dentry);
522 		dput(dentry);
523 	}
524 	inode_unlock(parent_inode);
525 }
526 
527 struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
528 				    const struct file_operations *fops,
529 				    void *data)
530 {
531 	struct dentry *dentry;
532 	struct inode *new_inode, *parent_inode;
533 	struct super_block *sb;
534 
535 	parent_inode = d_inode(parent);
536 	inode_lock(parent_inode);
537 
538 	dentry = binderfs_create_dentry(parent, name);
539 	if (IS_ERR(dentry))
540 		goto out;
541 
542 	sb = parent_inode->i_sb;
543 	new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
544 	if (!new_inode) {
545 		dput(dentry);
546 		dentry = ERR_PTR(-ENOMEM);
547 		goto out;
548 	}
549 
550 	new_inode->i_fop = fops;
551 	new_inode->i_private = data;
552 	d_instantiate(dentry, new_inode);
553 	fsnotify_create(parent_inode, dentry);
554 
555 out:
556 	inode_unlock(parent_inode);
557 	return dentry;
558 }
559 
560 static struct dentry *binderfs_create_dir(struct dentry *parent,
561 					  const char *name)
562 {
563 	struct dentry *dentry;
564 	struct inode *new_inode, *parent_inode;
565 	struct super_block *sb;
566 
567 	parent_inode = d_inode(parent);
568 	inode_lock(parent_inode);
569 
570 	dentry = binderfs_create_dentry(parent, name);
571 	if (IS_ERR(dentry))
572 		goto out;
573 
574 	sb = parent_inode->i_sb;
575 	new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
576 	if (!new_inode) {
577 		dput(dentry);
578 		dentry = ERR_PTR(-ENOMEM);
579 		goto out;
580 	}
581 
582 	new_inode->i_fop = &simple_dir_operations;
583 	new_inode->i_op = &simple_dir_inode_operations;
584 
585 	set_nlink(new_inode, 2);
586 	d_instantiate(dentry, new_inode);
587 	inc_nlink(parent_inode);
588 	fsnotify_mkdir(parent_inode, dentry);
589 
590 out:
591 	inode_unlock(parent_inode);
592 	return dentry;
593 }
594 
595 static int binder_features_show(struct seq_file *m, void *unused)
596 {
597 	bool *feature = m->private;
598 
599 	seq_printf(m, "%d\n", *feature);
600 
601 	return 0;
602 }
603 DEFINE_SHOW_ATTRIBUTE(binder_features);
604 
605 static int init_binder_features(struct super_block *sb)
606 {
607 	struct dentry *dentry, *dir;
608 
609 	dir = binderfs_create_dir(sb->s_root, "features");
610 	if (IS_ERR(dir))
611 		return PTR_ERR(dir);
612 
613 	dentry = binderfs_create_file(dir, "oneway_spam_detection",
614 				      &binder_features_fops,
615 				      &binder_features.oneway_spam_detection);
616 	if (IS_ERR(dentry))
617 		return PTR_ERR(dentry);
618 
619 	dentry = binderfs_create_file(dir, "extended_error",
620 				      &binder_features_fops,
621 				      &binder_features.extended_error);
622 	if (IS_ERR(dentry))
623 		return PTR_ERR(dentry);
624 
625 	return 0;
626 }
627 
628 static int init_binder_logs(struct super_block *sb)
629 {
630 	struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
631 	const struct binder_debugfs_entry *db_entry;
632 	struct binderfs_info *info;
633 	int ret = 0;
634 
635 	binder_logs_root_dir = binderfs_create_dir(sb->s_root,
636 						   "binder_logs");
637 	if (IS_ERR(binder_logs_root_dir)) {
638 		ret = PTR_ERR(binder_logs_root_dir);
639 		goto out;
640 	}
641 
642 	binder_for_each_debugfs_entry(db_entry) {
643 		dentry = binderfs_create_file(binder_logs_root_dir,
644 					      db_entry->name,
645 					      db_entry->fops,
646 					      db_entry->data);
647 		if (IS_ERR(dentry)) {
648 			ret = PTR_ERR(dentry);
649 			goto out;
650 		}
651 	}
652 
653 	proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
654 	if (IS_ERR(proc_log_dir)) {
655 		ret = PTR_ERR(proc_log_dir);
656 		goto out;
657 	}
658 	info = sb->s_fs_info;
659 	info->proc_log_dir = proc_log_dir;
660 
661 out:
662 	return ret;
663 }
664 
665 static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
666 {
667 	int ret;
668 	struct binderfs_info *info;
669 	struct binderfs_mount_opts *ctx = fc->fs_private;
670 	struct inode *inode = NULL;
671 	struct binderfs_device device_info = {};
672 	const char *name;
673 	size_t len;
674 
675 	sb->s_blocksize = PAGE_SIZE;
676 	sb->s_blocksize_bits = PAGE_SHIFT;
677 
678 	/*
679 	 * The binderfs filesystem can be mounted by userns root in a
680 	 * non-initial userns. By default such mounts have the SB_I_NODEV flag
681 	 * set in s_iflags to prevent security issues where userns root can
682 	 * just create random device nodes via mknod() since it owns the
683 	 * filesystem mount. But binderfs does not allow to create any files
684 	 * including devices nodes. The only way to create binder devices nodes
685 	 * is through the binder-control device which userns root is explicitly
686 	 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
687 	 * necessary and safe.
688 	 */
689 	sb->s_iflags &= ~SB_I_NODEV;
690 	sb->s_iflags |= SB_I_NOEXEC;
691 	sb->s_magic = BINDERFS_SUPER_MAGIC;
692 	sb->s_op = &binderfs_super_ops;
693 	sb->s_time_gran = 1;
694 
695 	sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
696 	if (!sb->s_fs_info)
697 		return -ENOMEM;
698 	info = sb->s_fs_info;
699 
700 	info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
701 
702 	info->root_gid = make_kgid(sb->s_user_ns, 0);
703 	if (!gid_valid(info->root_gid))
704 		info->root_gid = GLOBAL_ROOT_GID;
705 	info->root_uid = make_kuid(sb->s_user_ns, 0);
706 	if (!uid_valid(info->root_uid))
707 		info->root_uid = GLOBAL_ROOT_UID;
708 	info->mount_opts.max = ctx->max;
709 	info->mount_opts.stats_mode = ctx->stats_mode;
710 
711 	inode = new_inode(sb);
712 	if (!inode)
713 		return -ENOMEM;
714 
715 	inode->i_ino = FIRST_INODE;
716 	inode->i_fop = &simple_dir_operations;
717 	inode->i_mode = S_IFDIR | 0755;
718 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
719 	inode->i_op = &binderfs_dir_inode_operations;
720 	set_nlink(inode, 2);
721 
722 	sb->s_root = d_make_root(inode);
723 	if (!sb->s_root)
724 		return -ENOMEM;
725 
726 	ret = binderfs_binder_ctl_create(sb);
727 	if (ret)
728 		return ret;
729 
730 	name = binder_devices_param;
731 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
732 		strscpy(device_info.name, name, len + 1);
733 		ret = binderfs_binder_device_create(inode, NULL, &device_info);
734 		if (ret)
735 			return ret;
736 		name += len;
737 		if (*name == ',')
738 			name++;
739 	}
740 
741 	ret = init_binder_features(sb);
742 	if (ret)
743 		return ret;
744 
745 	if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
746 		return init_binder_logs(sb);
747 
748 	return 0;
749 }
750 
751 static int binderfs_fs_context_get_tree(struct fs_context *fc)
752 {
753 	return get_tree_nodev(fc, binderfs_fill_super);
754 }
755 
756 static void binderfs_fs_context_free(struct fs_context *fc)
757 {
758 	struct binderfs_mount_opts *ctx = fc->fs_private;
759 
760 	kfree(ctx);
761 }
762 
763 static const struct fs_context_operations binderfs_fs_context_ops = {
764 	.free		= binderfs_fs_context_free,
765 	.get_tree	= binderfs_fs_context_get_tree,
766 	.parse_param	= binderfs_fs_context_parse_param,
767 	.reconfigure	= binderfs_fs_context_reconfigure,
768 };
769 
770 static int binderfs_init_fs_context(struct fs_context *fc)
771 {
772 	struct binderfs_mount_opts *ctx;
773 
774 	ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);
775 	if (!ctx)
776 		return -ENOMEM;
777 
778 	ctx->max = BINDERFS_MAX_MINOR;
779 	ctx->stats_mode = binderfs_stats_mode_unset;
780 
781 	fc->fs_private = ctx;
782 	fc->ops = &binderfs_fs_context_ops;
783 
784 	return 0;
785 }
786 
787 static struct file_system_type binder_fs_type = {
788 	.name			= "binder",
789 	.init_fs_context	= binderfs_init_fs_context,
790 	.parameters		= binderfs_fs_parameters,
791 	.kill_sb		= kill_litter_super,
792 	.fs_flags		= FS_USERNS_MOUNT,
793 };
794 
795 int __init init_binderfs(void)
796 {
797 	int ret;
798 	const char *name;
799 	size_t len;
800 
801 	/* Verify that the default binderfs device names are valid. */
802 	name = binder_devices_param;
803 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
804 		if (len > BINDERFS_MAX_NAME)
805 			return -E2BIG;
806 		name += len;
807 		if (*name == ',')
808 			name++;
809 	}
810 
811 	/* Allocate new major number for binderfs. */
812 	ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
813 				  "binder");
814 	if (ret)
815 		return ret;
816 
817 	ret = register_filesystem(&binder_fs_type);
818 	if (ret) {
819 		unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
820 		return ret;
821 	}
822 
823 	return ret;
824 }
825