Merge branches 'sh-fixes-for-linus' and 'rmobile-fixes-for-linus' of git://git.kernel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / devtmpfs.c
blob82bbb5967aa99ab94e8c0a64b97ae8a7cf4cd9b9
1 /*
2 * devtmpfs - kernel-maintained tmpfs-based /dev
4 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
6 * During bootup, before any driver core device is registered,
7 * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
8 * device which requests a device node, will add a node in this
9 * filesystem.
10 * By default, all devices are named after the the name of the
11 * device, owned by root and have a default mode of 0600. Subsystems
12 * can overwrite the default setting if needed.
15 #include <linux/kernel.h>
16 #include <linux/syscalls.h>
17 #include <linux/mount.h>
18 #include <linux/device.h>
19 #include <linux/genhd.h>
20 #include <linux/namei.h>
21 #include <linux/fs.h>
22 #include <linux/shmem_fs.h>
23 #include <linux/ramfs.h>
24 #include <linux/cred.h>
25 #include <linux/sched.h>
26 #include <linux/init_task.h>
27 #include <linux/slab.h>
29 static struct vfsmount *dev_mnt;
31 #if defined CONFIG_DEVTMPFS_MOUNT
32 static int mount_dev = 1;
33 #else
34 static int mount_dev;
35 #endif
37 static DEFINE_MUTEX(dirlock);
39 static int __init mount_param(char *str)
41 mount_dev = simple_strtoul(str, NULL, 0);
42 return 1;
44 __setup("devtmpfs.mount=", mount_param);
46 static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
47 const char *dev_name, void *data)
49 #ifdef CONFIG_TMPFS
50 return mount_single(fs_type, flags, data, shmem_fill_super);
51 #else
52 return mount_single(fs_type, flags, data, ramfs_fill_super);
53 #endif
56 static struct file_system_type dev_fs_type = {
57 .name = "devtmpfs",
58 .mount = dev_mount,
59 .kill_sb = kill_litter_super,
62 #ifdef CONFIG_BLOCK
63 static inline int is_blockdev(struct device *dev)
65 return dev->class == &block_class;
67 #else
68 static inline int is_blockdev(struct device *dev) { return 0; }
69 #endif
71 static int dev_mkdir(const char *name, mode_t mode)
73 struct nameidata nd;
74 struct dentry *dentry;
75 int err;
77 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
78 name, LOOKUP_PARENT, &nd);
79 if (err)
80 return err;
82 dentry = lookup_create(&nd, 1);
83 if (!IS_ERR(dentry)) {
84 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
85 if (!err)
86 /* mark as kernel-created inode */
87 dentry->d_inode->i_private = &dev_mnt;
88 dput(dentry);
89 } else {
90 err = PTR_ERR(dentry);
93 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
94 path_put(&nd.path);
95 return err;
98 static int create_path(const char *nodepath)
100 int err;
102 mutex_lock(&dirlock);
103 err = dev_mkdir(nodepath, 0755);
104 if (err == -ENOENT) {
105 char *path;
106 char *s;
108 /* parent directories do not exist, create them */
109 path = kstrdup(nodepath, GFP_KERNEL);
110 if (!path) {
111 err = -ENOMEM;
112 goto out;
114 s = path;
115 for (;;) {
116 s = strchr(s, '/');
117 if (!s)
118 break;
119 s[0] = '\0';
120 err = dev_mkdir(path, 0755);
121 if (err && err != -EEXIST)
122 break;
123 s[0] = '/';
124 s++;
126 kfree(path);
128 out:
129 mutex_unlock(&dirlock);
130 return err;
133 int devtmpfs_create_node(struct device *dev)
135 const char *tmp = NULL;
136 const char *nodename;
137 const struct cred *curr_cred;
138 mode_t mode = 0;
139 struct nameidata nd;
140 struct dentry *dentry;
141 int err;
143 if (!dev_mnt)
144 return 0;
146 nodename = device_get_devnode(dev, &mode, &tmp);
147 if (!nodename)
148 return -ENOMEM;
150 if (mode == 0)
151 mode = 0600;
152 if (is_blockdev(dev))
153 mode |= S_IFBLK;
154 else
155 mode |= S_IFCHR;
157 curr_cred = override_creds(&init_cred);
159 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
160 nodename, LOOKUP_PARENT, &nd);
161 if (err == -ENOENT) {
162 create_path(nodename);
163 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
164 nodename, LOOKUP_PARENT, &nd);
166 if (err)
167 goto out;
169 dentry = lookup_create(&nd, 0);
170 if (!IS_ERR(dentry)) {
171 err = vfs_mknod(nd.path.dentry->d_inode,
172 dentry, mode, dev->devt);
173 if (!err) {
174 struct iattr newattrs;
176 /* fixup possibly umasked mode */
177 newattrs.ia_mode = mode;
178 newattrs.ia_valid = ATTR_MODE;
179 mutex_lock(&dentry->d_inode->i_mutex);
180 notify_change(dentry, &newattrs);
181 mutex_unlock(&dentry->d_inode->i_mutex);
183 /* mark as kernel-created inode */
184 dentry->d_inode->i_private = &dev_mnt;
186 dput(dentry);
187 } else {
188 err = PTR_ERR(dentry);
191 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
192 path_put(&nd.path);
193 out:
194 kfree(tmp);
195 revert_creds(curr_cred);
196 return err;
199 static int dev_rmdir(const char *name)
201 struct nameidata nd;
202 struct dentry *dentry;
203 int err;
205 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
206 name, LOOKUP_PARENT, &nd);
207 if (err)
208 return err;
210 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
211 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
212 if (!IS_ERR(dentry)) {
213 if (dentry->d_inode) {
214 if (dentry->d_inode->i_private == &dev_mnt)
215 err = vfs_rmdir(nd.path.dentry->d_inode,
216 dentry);
217 else
218 err = -EPERM;
219 } else {
220 err = -ENOENT;
222 dput(dentry);
223 } else {
224 err = PTR_ERR(dentry);
227 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
228 path_put(&nd.path);
229 return err;
232 static int delete_path(const char *nodepath)
234 const char *path;
235 int err = 0;
237 path = kstrdup(nodepath, GFP_KERNEL);
238 if (!path)
239 return -ENOMEM;
241 mutex_lock(&dirlock);
242 for (;;) {
243 char *base;
245 base = strrchr(path, '/');
246 if (!base)
247 break;
248 base[0] = '\0';
249 err = dev_rmdir(path);
250 if (err)
251 break;
253 mutex_unlock(&dirlock);
255 kfree(path);
256 return err;
259 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
261 /* did we create it */
262 if (inode->i_private != &dev_mnt)
263 return 0;
265 /* does the dev_t match */
266 if (is_blockdev(dev)) {
267 if (!S_ISBLK(stat->mode))
268 return 0;
269 } else {
270 if (!S_ISCHR(stat->mode))
271 return 0;
273 if (stat->rdev != dev->devt)
274 return 0;
276 /* ours */
277 return 1;
280 int devtmpfs_delete_node(struct device *dev)
282 const char *tmp = NULL;
283 const char *nodename;
284 const struct cred *curr_cred;
285 struct nameidata nd;
286 struct dentry *dentry;
287 struct kstat stat;
288 int deleted = 1;
289 int err;
291 if (!dev_mnt)
292 return 0;
294 nodename = device_get_devnode(dev, NULL, &tmp);
295 if (!nodename)
296 return -ENOMEM;
298 curr_cred = override_creds(&init_cred);
299 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
300 nodename, LOOKUP_PARENT, &nd);
301 if (err)
302 goto out;
304 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
305 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
306 if (!IS_ERR(dentry)) {
307 if (dentry->d_inode) {
308 err = vfs_getattr(nd.path.mnt, dentry, &stat);
309 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
310 struct iattr newattrs;
312 * before unlinking this node, reset permissions
313 * of possible references like hardlinks
315 newattrs.ia_uid = 0;
316 newattrs.ia_gid = 0;
317 newattrs.ia_mode = stat.mode & ~0777;
318 newattrs.ia_valid =
319 ATTR_UID|ATTR_GID|ATTR_MODE;
320 mutex_lock(&dentry->d_inode->i_mutex);
321 notify_change(dentry, &newattrs);
322 mutex_unlock(&dentry->d_inode->i_mutex);
323 err = vfs_unlink(nd.path.dentry->d_inode,
324 dentry);
325 if (!err || err == -ENOENT)
326 deleted = 1;
328 } else {
329 err = -ENOENT;
331 dput(dentry);
332 } else {
333 err = PTR_ERR(dentry);
335 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
337 path_put(&nd.path);
338 if (deleted && strchr(nodename, '/'))
339 delete_path(nodename);
340 out:
341 kfree(tmp);
342 revert_creds(curr_cred);
343 return err;
347 * If configured, or requested by the commandline, devtmpfs will be
348 * auto-mounted after the kernel mounted the root filesystem.
350 int devtmpfs_mount(const char *mntdir)
352 int err;
354 if (!mount_dev)
355 return 0;
357 if (!dev_mnt)
358 return 0;
360 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
361 if (err)
362 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
363 else
364 printk(KERN_INFO "devtmpfs: mounted\n");
365 return err;
369 * Create devtmpfs instance, driver-core devices will add their device
370 * nodes here.
372 int __init devtmpfs_init(void)
374 int err;
375 struct vfsmount *mnt;
376 char options[] = "mode=0755";
378 err = register_filesystem(&dev_fs_type);
379 if (err) {
380 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
381 "type %i\n", err);
382 return err;
385 mnt = kern_mount_data(&dev_fs_type, options);
386 if (IS_ERR(mnt)) {
387 err = PTR_ERR(mnt);
388 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
389 unregister_filesystem(&dev_fs_type);
390 return err;
392 dev_mnt = mnt;
394 printk(KERN_INFO "devtmpfs: initialized\n");
395 return 0;