netfilter: xtables: remove old comments about reentrancy
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / devtmpfs.c
blobdac478c6e46089c5bf8070dfa4127659b44fb6ec
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>
27 static struct vfsmount *dev_mnt;
29 #if defined CONFIG_DEVTMPFS_MOUNT
30 static int dev_mount = 1;
31 #else
32 static int dev_mount;
33 #endif
35 static DEFINE_MUTEX(dirlock);
37 static int __init mount_param(char *str)
39 dev_mount = simple_strtoul(str, NULL, 0);
40 return 1;
42 __setup("devtmpfs.mount=", mount_param);
44 static int dev_get_sb(struct file_system_type *fs_type, int flags,
45 const char *dev_name, void *data, struct vfsmount *mnt)
47 return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
50 static struct file_system_type dev_fs_type = {
51 .name = "devtmpfs",
52 .get_sb = dev_get_sb,
53 .kill_sb = kill_litter_super,
56 #ifdef CONFIG_BLOCK
57 static inline int is_blockdev(struct device *dev)
59 return dev->class == &block_class;
61 #else
62 static inline int is_blockdev(struct device *dev) { return 0; }
63 #endif
65 static int dev_mkdir(const char *name, mode_t mode)
67 struct nameidata nd;
68 struct dentry *dentry;
69 int err;
71 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
72 name, LOOKUP_PARENT, &nd);
73 if (err)
74 return err;
76 dentry = lookup_create(&nd, 1);
77 if (!IS_ERR(dentry)) {
78 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
79 if (!err)
80 /* mark as kernel-created inode */
81 dentry->d_inode->i_private = &dev_mnt;
82 dput(dentry);
83 } else {
84 err = PTR_ERR(dentry);
87 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
88 path_put(&nd.path);
89 return err;
92 static int create_path(const char *nodepath)
94 int err;
96 mutex_lock(&dirlock);
97 err = dev_mkdir(nodepath, 0755);
98 if (err == -ENOENT) {
99 char *path;
100 char *s;
102 /* parent directories do not exist, create them */
103 path = kstrdup(nodepath, GFP_KERNEL);
104 if (!path) {
105 err = -ENOMEM;
106 goto out;
108 s = path;
109 for (;;) {
110 s = strchr(s, '/');
111 if (!s)
112 break;
113 s[0] = '\0';
114 err = dev_mkdir(path, 0755);
115 if (err && err != -EEXIST)
116 break;
117 s[0] = '/';
118 s++;
120 kfree(path);
122 out:
123 mutex_unlock(&dirlock);
124 return err;
127 int devtmpfs_create_node(struct device *dev)
129 const char *tmp = NULL;
130 const char *nodename;
131 const struct cred *curr_cred;
132 mode_t mode = 0;
133 struct nameidata nd;
134 struct dentry *dentry;
135 int err;
137 if (!dev_mnt)
138 return 0;
140 nodename = device_get_devnode(dev, &mode, &tmp);
141 if (!nodename)
142 return -ENOMEM;
144 if (mode == 0)
145 mode = 0600;
146 if (is_blockdev(dev))
147 mode |= S_IFBLK;
148 else
149 mode |= S_IFCHR;
151 curr_cred = override_creds(&init_cred);
153 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
154 nodename, LOOKUP_PARENT, &nd);
155 if (err == -ENOENT) {
156 create_path(nodename);
157 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
158 nodename, LOOKUP_PARENT, &nd);
160 if (err)
161 goto out;
163 dentry = lookup_create(&nd, 0);
164 if (!IS_ERR(dentry)) {
165 err = vfs_mknod(nd.path.dentry->d_inode,
166 dentry, mode, dev->devt);
167 if (!err) {
168 struct iattr newattrs;
170 /* fixup possibly umasked mode */
171 newattrs.ia_mode = mode;
172 newattrs.ia_valid = ATTR_MODE;
173 mutex_lock(&dentry->d_inode->i_mutex);
174 notify_change(dentry, &newattrs);
175 mutex_unlock(&dentry->d_inode->i_mutex);
177 /* mark as kernel-created inode */
178 dentry->d_inode->i_private = &dev_mnt;
180 dput(dentry);
181 } else {
182 err = PTR_ERR(dentry);
185 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
186 path_put(&nd.path);
187 out:
188 kfree(tmp);
189 revert_creds(curr_cred);
190 return err;
193 static int dev_rmdir(const char *name)
195 struct nameidata nd;
196 struct dentry *dentry;
197 int err;
199 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
200 name, LOOKUP_PARENT, &nd);
201 if (err)
202 return err;
204 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
205 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
206 if (!IS_ERR(dentry)) {
207 if (dentry->d_inode) {
208 if (dentry->d_inode->i_private == &dev_mnt)
209 err = vfs_rmdir(nd.path.dentry->d_inode,
210 dentry);
211 else
212 err = -EPERM;
213 } else {
214 err = -ENOENT;
216 dput(dentry);
217 } else {
218 err = PTR_ERR(dentry);
221 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
222 path_put(&nd.path);
223 return err;
226 static int delete_path(const char *nodepath)
228 const char *path;
229 int err = 0;
231 path = kstrdup(nodepath, GFP_KERNEL);
232 if (!path)
233 return -ENOMEM;
235 mutex_lock(&dirlock);
236 for (;;) {
237 char *base;
239 base = strrchr(path, '/');
240 if (!base)
241 break;
242 base[0] = '\0';
243 err = dev_rmdir(path);
244 if (err)
245 break;
247 mutex_unlock(&dirlock);
249 kfree(path);
250 return err;
253 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
255 /* did we create it */
256 if (inode->i_private != &dev_mnt)
257 return 0;
259 /* does the dev_t match */
260 if (is_blockdev(dev)) {
261 if (!S_ISBLK(stat->mode))
262 return 0;
263 } else {
264 if (!S_ISCHR(stat->mode))
265 return 0;
267 if (stat->rdev != dev->devt)
268 return 0;
270 /* ours */
271 return 1;
274 int devtmpfs_delete_node(struct device *dev)
276 const char *tmp = NULL;
277 const char *nodename;
278 const struct cred *curr_cred;
279 struct nameidata nd;
280 struct dentry *dentry;
281 struct kstat stat;
282 int deleted = 1;
283 int err;
285 if (!dev_mnt)
286 return 0;
288 nodename = device_get_devnode(dev, NULL, &tmp);
289 if (!nodename)
290 return -ENOMEM;
292 curr_cred = override_creds(&init_cred);
293 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
294 nodename, LOOKUP_PARENT, &nd);
295 if (err)
296 goto out;
298 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
299 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
300 if (!IS_ERR(dentry)) {
301 if (dentry->d_inode) {
302 err = vfs_getattr(nd.path.mnt, dentry, &stat);
303 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
304 struct iattr newattrs;
306 * before unlinking this node, reset permissions
307 * of possible references like hardlinks
309 newattrs.ia_uid = 0;
310 newattrs.ia_gid = 0;
311 newattrs.ia_mode = stat.mode & ~0777;
312 newattrs.ia_valid =
313 ATTR_UID|ATTR_GID|ATTR_MODE;
314 mutex_lock(&dentry->d_inode->i_mutex);
315 notify_change(dentry, &newattrs);
316 mutex_unlock(&dentry->d_inode->i_mutex);
317 err = vfs_unlink(nd.path.dentry->d_inode,
318 dentry);
319 if (!err || err == -ENOENT)
320 deleted = 1;
322 } else {
323 err = -ENOENT;
325 dput(dentry);
326 } else {
327 err = PTR_ERR(dentry);
329 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
331 path_put(&nd.path);
332 if (deleted && strchr(nodename, '/'))
333 delete_path(nodename);
334 out:
335 kfree(tmp);
336 revert_creds(curr_cred);
337 return err;
341 * If configured, or requested by the commandline, devtmpfs will be
342 * auto-mounted after the kernel mounted the root filesystem.
344 int devtmpfs_mount(const char *mntdir)
346 int err;
348 if (!dev_mount)
349 return 0;
351 if (!dev_mnt)
352 return 0;
354 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
355 if (err)
356 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
357 else
358 printk(KERN_INFO "devtmpfs: mounted\n");
359 return err;
363 * Create devtmpfs instance, driver-core devices will add their device
364 * nodes here.
366 int __init devtmpfs_init(void)
368 int err;
369 struct vfsmount *mnt;
370 char options[] = "mode=0755";
372 err = register_filesystem(&dev_fs_type);
373 if (err) {
374 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
375 "type %i\n", err);
376 return err;
379 mnt = kern_mount_data(&dev_fs_type, options);
380 if (IS_ERR(mnt)) {
381 err = PTR_ERR(mnt);
382 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
383 unregister_filesystem(&dev_fs_type);
384 return err;
386 dev_mnt = mnt;
388 printk(KERN_INFO "devtmpfs: initialized\n");
389 return 0;