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