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