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