devtmpfs.c 9.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * devtmpfs - kernel-maintained tmpfs-based /dev
 *
 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
 *
 * During bootup, before any driver core device is registered,
 * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
 * device which requests a device node, will add a node in this
9
 * filesystem.
P
Peter Korsgaard 已提交
10 11 12
 * By default, all devices are named after the name of the device,
 * owned by root and have a default mode of 0600. Subsystems can
 * overwrite the default setting if needed.
13 14 15 16 17 18 19 20 21 22
 */

#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/mount.h>
#include <linux/device.h>
#include <linux/genhd.h>
#include <linux/namei.h>
#include <linux/fs.h>
#include <linux/shmem_fs.h>
23
#include <linux/ramfs.h>
24
#include <linux/sched.h>
25
#include <linux/slab.h>
26
#include <linux/kthread.h>
27

28
static struct task_struct *thread;
29 30

#if defined CONFIG_DEVTMPFS_MOUNT
A
Al Viro 已提交
31
static int mount_dev = 1;
32
#else
A
Al Viro 已提交
33
static int mount_dev;
34 35
#endif

36 37 38 39 40 41 42
static DEFINE_SPINLOCK(req_lock);

static struct req {
	struct req *next;
	struct completion done;
	int err;
	const char *name;
43
	umode_t mode;	/* 0 => delete */
44 45
	struct device *dev;
} *requests;
46

47 48
static int __init mount_param(char *str)
{
A
Al Viro 已提交
49
	mount_dev = simple_strtoul(str, NULL, 0);
50 51 52 53
	return 1;
}
__setup("devtmpfs.mount=", mount_param);

A
Al Viro 已提交
54 55
static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
		      const char *dev_name, void *data)
56
{
57
#ifdef CONFIG_TMPFS
A
Al Viro 已提交
58
	return mount_single(fs_type, flags, data, shmem_fill_super);
59
#else
A
Al Viro 已提交
60
	return mount_single(fs_type, flags, data, ramfs_fill_super);
61
#endif
62 63 64 65
}

static struct file_system_type dev_fs_type = {
	.name = "devtmpfs",
A
Al Viro 已提交
66
	.mount = dev_mount,
67 68 69 70 71 72 73 74 75 76 77 78
	.kill_sb = kill_litter_super,
};

#ifdef CONFIG_BLOCK
static inline int is_blockdev(struct device *dev)
{
	return dev->class == &block_class;
}
#else
static inline int is_blockdev(struct device *dev) { return 0; }
#endif

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
int devtmpfs_create_node(struct device *dev)
{
	const char *tmp = NULL;
	struct req req;

	if (!thread)
		return 0;

	req.mode = 0;
	req.name = device_get_devnode(dev, &req.mode, &tmp);
	if (!req.name)
		return -ENOMEM;

	if (req.mode == 0)
		req.mode = 0600;
	if (is_blockdev(dev))
		req.mode |= S_IFBLK;
	else
		req.mode |= S_IFCHR;

	req.dev = dev;

	init_completion(&req.done);

	spin_lock(&req_lock);
	req.next = requests;
	requests = &req;
	spin_unlock(&req_lock);

	wake_up_process(thread);
	wait_for_completion(&req.done);

	kfree(tmp);

	return req.err;
}

int devtmpfs_delete_node(struct device *dev)
{
	const char *tmp = NULL;
	struct req req;

	if (!thread)
		return 0;

	req.name = device_get_devnode(dev, NULL, &tmp);
	if (!req.name)
		return -ENOMEM;

	req.mode = 0;
	req.dev = dev;

	init_completion(&req.done);

	spin_lock(&req_lock);
	req.next = requests;
	requests = &req;
	spin_unlock(&req_lock);

	wake_up_process(thread);
	wait_for_completion(&req.done);

	kfree(tmp);
	return req.err;
}

A
Al Viro 已提交
145
static int dev_mkdir(const char *name, umode_t mode)
146 147
{
	struct dentry *dentry;
A
Al Viro 已提交
148
	struct path path;
149 150
	int err;

A
Al Viro 已提交
151 152 153 154 155 156 157 158 159 160 161
	dentry = kern_path_create(AT_FDCWD, name, &path, 1);
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);

	err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
	if (!err)
		/* mark as kernel-created inode */
		dentry->d_inode->i_private = &thread;
	dput(dentry);
	mutex_unlock(&path.dentry->d_inode->i_mutex);
	path_put(&path);
162 163 164 165 166
	return err;
}

static int create_path(const char *nodepath)
{
167 168
	char *path;
	char *s;
169
	int err = 0;
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	/* parent directories do not exist, create them */
	path = kstrdup(nodepath, GFP_KERNEL);
	if (!path)
		return -ENOMEM;

	s = path;
	for (;;) {
		s = strchr(s, '/');
		if (!s)
			break;
		s[0] = '\0';
		err = dev_mkdir(path, 0755);
		if (err && err != -EEXIST)
			break;
		s[0] = '/';
		s++;
187
	}
188
	kfree(path);
189 190 191
	return err;
}

A
Al Viro 已提交
192
static int handle_create(const char *nodename, umode_t mode, struct device *dev)
193 194
{
	struct dentry *dentry;
A
Al Viro 已提交
195
	struct path path;
196 197
	int err;

A
Al Viro 已提交
198 199
	dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
	if (dentry == ERR_PTR(-ENOENT)) {
200
		create_path(nodename);
A
Al Viro 已提交
201
		dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
202
	}
A
Al Viro 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	if (IS_ERR(dentry))
		return PTR_ERR(dentry);

	err = vfs_mknod(path.dentry->d_inode,
			dentry, mode, dev->devt);
	if (!err) {
		struct iattr newattrs;

		/* fixup possibly umasked mode */
		newattrs.ia_mode = mode;
		newattrs.ia_valid = ATTR_MODE;
		mutex_lock(&dentry->d_inode->i_mutex);
		notify_change(dentry, &newattrs);
		mutex_unlock(&dentry->d_inode->i_mutex);

		/* mark as kernel-created inode */
		dentry->d_inode->i_private = &thread;
220
	}
A
Al Viro 已提交
221
	dput(dentry);
222

A
Al Viro 已提交
223 224
	mutex_unlock(&path.dentry->d_inode->i_mutex);
	path_put(&path);
225 226 227 228 229 230 231 232 233
	return err;
}

static int dev_rmdir(const char *name)
{
	struct nameidata nd;
	struct dentry *dentry;
	int err;

234
	err = kern_path_parent(name, &nd);
235 236 237 238 239 240
	if (err)
		return err;

	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
	dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
	if (!IS_ERR(dentry)) {
241
		if (dentry->d_inode) {
242
			if (dentry->d_inode->i_private == &thread)
243 244 245 246 247
				err = vfs_rmdir(nd.path.dentry->d_inode,
						dentry);
			else
				err = -EPERM;
		} else {
248
			err = -ENOENT;
249
		}
250 251 252 253 254
		dput(dentry);
	} else {
		err = PTR_ERR(dentry);
	}

255
	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
256 257 258 259 260 261 262 263 264 265 266 267 268
	path_put(&nd.path);
	return err;
}

static int delete_path(const char *nodepath)
{
	const char *path;
	int err = 0;

	path = kstrdup(nodepath, GFP_KERNEL);
	if (!path)
		return -ENOMEM;

269
	for (;;) {
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
		char *base;

		base = strrchr(path, '/');
		if (!base)
			break;
		base[0] = '\0';
		err = dev_rmdir(path);
		if (err)
			break;
	}

	kfree(path);
	return err;
}

static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
{
	/* did we create it */
288
	if (inode->i_private != &thread)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		return 0;

	/* does the dev_t match */
	if (is_blockdev(dev)) {
		if (!S_ISBLK(stat->mode))
			return 0;
	} else {
		if (!S_ISCHR(stat->mode))
			return 0;
	}
	if (stat->rdev != dev->devt)
		return 0;

	/* ours */
	return 1;
}

306
static int handle_remove(const char *nodename, struct device *dev)
307 308 309 310 311 312 313
{
	struct nameidata nd;
	struct dentry *dentry;
	struct kstat stat;
	int deleted = 1;
	int err;

314
	err = kern_path_parent(nodename, &nd);
315
	if (err)
316
		return err;
317 318 319 320 321 322 323

	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
	dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
	if (!IS_ERR(dentry)) {
		if (dentry->d_inode) {
			err = vfs_getattr(nd.path.mnt, dentry, &stat);
			if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
324 325 326 327 328 329 330 331 332 333 334 335 336
				struct iattr newattrs;
				/*
				 * before unlinking this node, reset permissions
				 * of possible references like hardlinks
				 */
				newattrs.ia_uid = 0;
				newattrs.ia_gid = 0;
				newattrs.ia_mode = stat.mode & ~0777;
				newattrs.ia_valid =
					ATTR_UID|ATTR_GID|ATTR_MODE;
				mutex_lock(&dentry->d_inode->i_mutex);
				notify_change(dentry, &newattrs);
				mutex_unlock(&dentry->d_inode->i_mutex);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
				err = vfs_unlink(nd.path.dentry->d_inode,
						 dentry);
				if (!err || err == -ENOENT)
					deleted = 1;
			}
		} else {
			err = -ENOENT;
		}
		dput(dentry);
	} else {
		err = PTR_ERR(dentry);
	}
	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);

	path_put(&nd.path);
	if (deleted && strchr(nodename, '/'))
		delete_path(nodename);
	return err;
}

/*
 * If configured, or requested by the commandline, devtmpfs will be
 * auto-mounted after the kernel mounted the root filesystem.
 */
361
int devtmpfs_mount(const char *mntdir)
362 363 364
{
	int err;

A
Al Viro 已提交
365
	if (!mount_dev)
366 367
		return 0;

368
	if (!thread)
369 370
		return 0;

371
	err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
372 373 374 375 376 377 378
	if (err)
		printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
	else
		printk(KERN_INFO "devtmpfs: mounted\n");
	return err;
}

379
static DECLARE_COMPLETION(setup_done);
380

A
Al Viro 已提交
381
static int handle(const char *name, umode_t mode, struct device *dev)
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
{
	if (mode)
		return handle_create(name, mode, dev);
	else
		return handle_remove(name, dev);
}

static int devtmpfsd(void *p)
{
	char options[] = "mode=0755";
	int *err = p;
	*err = sys_unshare(CLONE_NEWNS);
	if (*err)
		goto out;
	*err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
	if (*err)
		goto out;
	sys_chdir("/.."); /* will traverse into overmounted root */
	sys_chroot(".");
	complete(&setup_done);
	while (1) {
		spin_lock(&req_lock);
		while (requests) {
			struct req *req = requests;
			requests = NULL;
			spin_unlock(&req_lock);
			while (req) {
A
Al Viro 已提交
409
				struct req *next = req->next;
410 411
				req->err = handle(req->name, req->mode, req->dev);
				complete(&req->done);
A
Al Viro 已提交
412
				req = next;
413 414 415
			}
			spin_lock(&req_lock);
		}
K
Kautuk Consul 已提交
416
		__set_current_state(TASK_INTERRUPTIBLE);
417 418 419 420 421 422 423 424 425
		spin_unlock(&req_lock);
		schedule();
	}
	return 0;
out:
	complete(&setup_done);
	return *err;
}

426 427 428 429 430 431
/*
 * Create devtmpfs instance, driver-core devices will add their device
 * nodes here.
 */
int __init devtmpfs_init(void)
{
432
	int err = register_filesystem(&dev_fs_type);
433 434 435 436 437 438
	if (err) {
		printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
		       "type %i\n", err);
		return err;
	}

439 440 441 442 443 444 445 446 447
	thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
	if (!IS_ERR(thread)) {
		wait_for_completion(&setup_done);
	} else {
		err = PTR_ERR(thread);
		thread = NULL;
	}

	if (err) {
448 449 450 451 452 453 454 455
		printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
		unregister_filesystem(&dev_fs_type);
		return err;
	}

	printk(KERN_INFO "devtmpfs: initialized\n");
	return 0;
}