Staging: netwave: delete the driver
[linux/fpc-iii.git] / drivers / base / devtmpfs.c
blob057cf11326bff9c84e3e18b24cee28250a49562a
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/cred.h>
24 #include <linux/sched.h>
25 #include <linux/init_task.h>
26 #include <linux/slab.h>
28 static struct vfsmount *dev_mnt;
30 #if defined CONFIG_DEVTMPFS_MOUNT
31 static int dev_mount = 1;
32 #else
33 static int dev_mount;
34 #endif
36 static DEFINE_MUTEX(dirlock);
38 static int __init mount_param(char *str)
40 dev_mount = simple_strtoul(str, NULL, 0);
41 return 1;
43 __setup("devtmpfs.mount=", mount_param);
45 static int dev_get_sb(struct file_system_type *fs_type, int flags,
46 const char *dev_name, void *data, struct vfsmount *mnt)
48 return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
51 static struct file_system_type dev_fs_type = {
52 .name = "devtmpfs",
53 .get_sb = dev_get_sb,
54 .kill_sb = kill_litter_super,
57 #ifdef CONFIG_BLOCK
58 static inline int is_blockdev(struct device *dev)
60 return dev->class == &block_class;
62 #else
63 static inline int is_blockdev(struct device *dev) { return 0; }
64 #endif
66 static int dev_mkdir(const char *name, mode_t mode)
68 struct nameidata nd;
69 struct dentry *dentry;
70 int err;
72 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
73 name, LOOKUP_PARENT, &nd);
74 if (err)
75 return err;
77 dentry = lookup_create(&nd, 1);
78 if (!IS_ERR(dentry)) {
79 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
80 if (!err)
81 /* mark as kernel-created inode */
82 dentry->d_inode->i_private = &dev_mnt;
83 dput(dentry);
84 } else {
85 err = PTR_ERR(dentry);
88 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
89 path_put(&nd.path);
90 return err;
93 static int create_path(const char *nodepath)
95 int err;
97 mutex_lock(&dirlock);
98 err = dev_mkdir(nodepath, 0755);
99 if (err == -ENOENT) {
100 char *path;
101 char *s;
103 /* parent directories do not exist, create them */
104 path = kstrdup(nodepath, GFP_KERNEL);
105 if (!path) {
106 err = -ENOMEM;
107 goto out;
109 s = path;
110 for (;;) {
111 s = strchr(s, '/');
112 if (!s)
113 break;
114 s[0] = '\0';
115 err = dev_mkdir(path, 0755);
116 if (err && err != -EEXIST)
117 break;
118 s[0] = '/';
119 s++;
121 kfree(path);
123 out:
124 mutex_unlock(&dirlock);
125 return err;
128 int devtmpfs_create_node(struct device *dev)
130 const char *tmp = NULL;
131 const char *nodename;
132 const struct cred *curr_cred;
133 mode_t mode = 0;
134 struct nameidata nd;
135 struct dentry *dentry;
136 int err;
138 if (!dev_mnt)
139 return 0;
141 nodename = device_get_devnode(dev, &mode, &tmp);
142 if (!nodename)
143 return -ENOMEM;
145 if (mode == 0)
146 mode = 0600;
147 if (is_blockdev(dev))
148 mode |= S_IFBLK;
149 else
150 mode |= S_IFCHR;
152 curr_cred = override_creds(&init_cred);
154 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
155 nodename, LOOKUP_PARENT, &nd);
156 if (err == -ENOENT) {
157 create_path(nodename);
158 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
159 nodename, LOOKUP_PARENT, &nd);
161 if (err)
162 goto out;
164 dentry = lookup_create(&nd, 0);
165 if (!IS_ERR(dentry)) {
166 err = vfs_mknod(nd.path.dentry->d_inode,
167 dentry, mode, dev->devt);
168 if (!err) {
169 struct iattr newattrs;
171 /* fixup possibly umasked mode */
172 newattrs.ia_mode = mode;
173 newattrs.ia_valid = ATTR_MODE;
174 mutex_lock(&dentry->d_inode->i_mutex);
175 notify_change(dentry, &newattrs);
176 mutex_unlock(&dentry->d_inode->i_mutex);
178 /* mark as kernel-created inode */
179 dentry->d_inode->i_private = &dev_mnt;
181 dput(dentry);
182 } else {
183 err = PTR_ERR(dentry);
186 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
187 path_put(&nd.path);
188 out:
189 kfree(tmp);
190 revert_creds(curr_cred);
191 return err;
194 static int dev_rmdir(const char *name)
196 struct nameidata nd;
197 struct dentry *dentry;
198 int err;
200 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
201 name, LOOKUP_PARENT, &nd);
202 if (err)
203 return err;
205 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
206 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
207 if (!IS_ERR(dentry)) {
208 if (dentry->d_inode) {
209 if (dentry->d_inode->i_private == &dev_mnt)
210 err = vfs_rmdir(nd.path.dentry->d_inode,
211 dentry);
212 else
213 err = -EPERM;
214 } else {
215 err = -ENOENT;
217 dput(dentry);
218 } else {
219 err = PTR_ERR(dentry);
222 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
223 path_put(&nd.path);
224 return err;
227 static int delete_path(const char *nodepath)
229 const char *path;
230 int err = 0;
232 path = kstrdup(nodepath, GFP_KERNEL);
233 if (!path)
234 return -ENOMEM;
236 mutex_lock(&dirlock);
237 for (;;) {
238 char *base;
240 base = strrchr(path, '/');
241 if (!base)
242 break;
243 base[0] = '\0';
244 err = dev_rmdir(path);
245 if (err)
246 break;
248 mutex_unlock(&dirlock);
250 kfree(path);
251 return err;
254 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
256 /* did we create it */
257 if (inode->i_private != &dev_mnt)
258 return 0;
260 /* does the dev_t match */
261 if (is_blockdev(dev)) {
262 if (!S_ISBLK(stat->mode))
263 return 0;
264 } else {
265 if (!S_ISCHR(stat->mode))
266 return 0;
268 if (stat->rdev != dev->devt)
269 return 0;
271 /* ours */
272 return 1;
275 int devtmpfs_delete_node(struct device *dev)
277 const char *tmp = NULL;
278 const char *nodename;
279 const struct cred *curr_cred;
280 struct nameidata nd;
281 struct dentry *dentry;
282 struct kstat stat;
283 int deleted = 1;
284 int err;
286 if (!dev_mnt)
287 return 0;
289 nodename = device_get_devnode(dev, NULL, &tmp);
290 if (!nodename)
291 return -ENOMEM;
293 curr_cred = override_creds(&init_cred);
294 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
295 nodename, LOOKUP_PARENT, &nd);
296 if (err)
297 goto out;
299 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
300 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
301 if (!IS_ERR(dentry)) {
302 if (dentry->d_inode) {
303 err = vfs_getattr(nd.path.mnt, dentry, &stat);
304 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
305 struct iattr newattrs;
307 * before unlinking this node, reset permissions
308 * of possible references like hardlinks
310 newattrs.ia_uid = 0;
311 newattrs.ia_gid = 0;
312 newattrs.ia_mode = stat.mode & ~0777;
313 newattrs.ia_valid =
314 ATTR_UID|ATTR_GID|ATTR_MODE;
315 mutex_lock(&dentry->d_inode->i_mutex);
316 notify_change(dentry, &newattrs);
317 mutex_unlock(&dentry->d_inode->i_mutex);
318 err = vfs_unlink(nd.path.dentry->d_inode,
319 dentry);
320 if (!err || err == -ENOENT)
321 deleted = 1;
323 } else {
324 err = -ENOENT;
326 dput(dentry);
327 } else {
328 err = PTR_ERR(dentry);
330 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
332 path_put(&nd.path);
333 if (deleted && strchr(nodename, '/'))
334 delete_path(nodename);
335 out:
336 kfree(tmp);
337 revert_creds(curr_cred);
338 return err;
342 * If configured, or requested by the commandline, devtmpfs will be
343 * auto-mounted after the kernel mounted the root filesystem.
345 int devtmpfs_mount(const char *mntdir)
347 int err;
349 if (!dev_mount)
350 return 0;
352 if (!dev_mnt)
353 return 0;
355 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
356 if (err)
357 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
358 else
359 printk(KERN_INFO "devtmpfs: mounted\n");
360 return err;
364 * Create devtmpfs instance, driver-core devices will add their device
365 * nodes here.
367 int __init devtmpfs_init(void)
369 int err;
370 struct vfsmount *mnt;
371 char options[] = "mode=0755";
373 err = register_filesystem(&dev_fs_type);
374 if (err) {
375 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
376 "type %i\n", err);
377 return err;
380 mnt = kern_mount_data(&dev_fs_type, options);
381 if (IS_ERR(mnt)) {
382 err = PTR_ERR(mnt);
383 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
384 unregister_filesystem(&dev_fs_type);
385 return err;
387 dev_mnt = mnt;
389 printk(KERN_INFO "devtmpfs: initialized\n");
390 return 0;