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