lockd: Try to reconnect if statd has moved
[linux/fpc-iii.git] / drivers / base / devtmpfs.c
blob4d809667815e5d64ba72ef6b57146c8174d87170
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 int __init mount_param(char *str)
37 dev_mount = simple_strtoul(str, NULL, 0);
38 return 1;
40 __setup("devtmpfs.mount=", mount_param);
42 static int dev_get_sb(struct file_system_type *fs_type, int flags,
43 const char *dev_name, void *data, struct vfsmount *mnt)
45 return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
48 static struct file_system_type dev_fs_type = {
49 .name = "devtmpfs",
50 .get_sb = dev_get_sb,
51 .kill_sb = kill_litter_super,
54 #ifdef CONFIG_BLOCK
55 static inline int is_blockdev(struct device *dev)
57 return dev->class == &block_class;
59 #else
60 static inline int is_blockdev(struct device *dev) { return 0; }
61 #endif
63 static int dev_mkdir(const char *name, mode_t mode)
65 struct nameidata nd;
66 struct dentry *dentry;
67 int err;
69 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
70 name, LOOKUP_PARENT, &nd);
71 if (err)
72 return err;
74 dentry = lookup_create(&nd, 1);
75 if (!IS_ERR(dentry)) {
76 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
77 dput(dentry);
78 } else {
79 err = PTR_ERR(dentry);
81 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
83 path_put(&nd.path);
84 return err;
87 static int create_path(const char *nodepath)
89 char *path;
90 struct nameidata nd;
91 int err = 0;
93 path = kstrdup(nodepath, GFP_KERNEL);
94 if (!path)
95 return -ENOMEM;
97 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
98 path, LOOKUP_PARENT, &nd);
99 if (err == 0) {
100 struct dentry *dentry;
102 /* create directory right away */
103 dentry = lookup_create(&nd, 1);
104 if (!IS_ERR(dentry)) {
105 err = vfs_mkdir(nd.path.dentry->d_inode,
106 dentry, 0755);
107 dput(dentry);
109 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
111 path_put(&nd.path);
112 } else if (err == -ENOENT) {
113 char *s;
115 /* parent directories do not exist, create them */
116 s = path;
117 while (1) {
118 s = strchr(s, '/');
119 if (!s)
120 break;
121 s[0] = '\0';
122 err = dev_mkdir(path, 0755);
123 if (err && err != -EEXIST)
124 break;
125 s[0] = '/';
126 s++;
130 kfree(path);
131 return err;
134 int devtmpfs_create_node(struct device *dev)
136 const char *tmp = NULL;
137 const char *nodename;
138 const struct cred *curr_cred;
139 mode_t mode = 0;
140 struct nameidata nd;
141 struct dentry *dentry;
142 int err;
144 if (!dev_mnt)
145 return 0;
147 nodename = device_get_devnode(dev, &mode, &tmp);
148 if (!nodename)
149 return -ENOMEM;
151 if (mode == 0)
152 mode = 0600;
153 if (is_blockdev(dev))
154 mode |= S_IFBLK;
155 else
156 mode |= S_IFCHR;
158 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 missing parent directories */
163 create_path(nodename);
164 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
165 nodename, LOOKUP_PARENT, &nd);
166 if (err)
167 goto out;
170 dentry = lookup_create(&nd, 0);
171 if (!IS_ERR(dentry)) {
172 int umask;
174 umask = sys_umask(0000);
175 err = vfs_mknod(nd.path.dentry->d_inode,
176 dentry, mode, dev->devt);
177 sys_umask(umask);
178 /* mark as kernel created inode */
179 if (!err)
180 dentry->d_inode->i_private = &dev_mnt;
181 dput(dentry);
182 } else {
183 err = PTR_ERR(dentry);
185 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 err = vfs_rmdir(nd.path.dentry->d_inode, dentry);
210 else
211 err = -ENOENT;
212 dput(dentry);
213 } else {
214 err = PTR_ERR(dentry);
216 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
218 path_put(&nd.path);
219 return err;
222 static int delete_path(const char *nodepath)
224 const char *path;
225 int err = 0;
227 path = kstrdup(nodepath, GFP_KERNEL);
228 if (!path)
229 return -ENOMEM;
231 while (1) {
232 char *base;
234 base = strrchr(path, '/');
235 if (!base)
236 break;
237 base[0] = '\0';
238 err = dev_rmdir(path);
239 if (err)
240 break;
243 kfree(path);
244 return err;
247 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
249 /* did we create it */
250 if (inode->i_private != &dev_mnt)
251 return 0;
253 /* does the dev_t match */
254 if (is_blockdev(dev)) {
255 if (!S_ISBLK(stat->mode))
256 return 0;
257 } else {
258 if (!S_ISCHR(stat->mode))
259 return 0;
261 if (stat->rdev != dev->devt)
262 return 0;
264 /* ours */
265 return 1;
268 int devtmpfs_delete_node(struct device *dev)
270 const char *tmp = NULL;
271 const char *nodename;
272 const struct cred *curr_cred;
273 struct nameidata nd;
274 struct dentry *dentry;
275 struct kstat stat;
276 int deleted = 1;
277 int err;
279 if (!dev_mnt)
280 return 0;
282 nodename = device_get_devnode(dev, NULL, &tmp);
283 if (!nodename)
284 return -ENOMEM;
286 curr_cred = override_creds(&init_cred);
287 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
288 nodename, LOOKUP_PARENT, &nd);
289 if (err)
290 goto out;
292 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
293 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
294 if (!IS_ERR(dentry)) {
295 if (dentry->d_inode) {
296 err = vfs_getattr(nd.path.mnt, dentry, &stat);
297 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
298 struct iattr newattrs;
300 * before unlinking this node, reset permissions
301 * of possible references like hardlinks
303 newattrs.ia_uid = 0;
304 newattrs.ia_gid = 0;
305 newattrs.ia_mode = stat.mode & ~0777;
306 newattrs.ia_valid =
307 ATTR_UID|ATTR_GID|ATTR_MODE;
308 mutex_lock(&dentry->d_inode->i_mutex);
309 notify_change(dentry, &newattrs);
310 mutex_unlock(&dentry->d_inode->i_mutex);
311 err = vfs_unlink(nd.path.dentry->d_inode,
312 dentry);
313 if (!err || err == -ENOENT)
314 deleted = 1;
316 } else {
317 err = -ENOENT;
319 dput(dentry);
320 } else {
321 err = PTR_ERR(dentry);
323 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
325 path_put(&nd.path);
326 if (deleted && strchr(nodename, '/'))
327 delete_path(nodename);
328 out:
329 kfree(tmp);
330 revert_creds(curr_cred);
331 return err;
335 * If configured, or requested by the commandline, devtmpfs will be
336 * auto-mounted after the kernel mounted the root filesystem.
338 int devtmpfs_mount(const char *mountpoint)
340 struct path path;
341 int err;
343 if (!dev_mount)
344 return 0;
346 if (!dev_mnt)
347 return 0;
349 err = kern_path(mountpoint, LOOKUP_FOLLOW, &path);
350 if (err)
351 return err;
352 err = do_add_mount(dev_mnt, &path, 0, NULL);
353 if (err)
354 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
355 else
356 printk(KERN_INFO "devtmpfs: mounted\n");
357 path_put(&path);
358 return err;
362 * Create devtmpfs instance, driver-core devices will add their device
363 * nodes here.
365 int __init devtmpfs_init(void)
367 int err;
368 struct vfsmount *mnt;
369 char options[] = "mode=0755";
371 err = register_filesystem(&dev_fs_type);
372 if (err) {
373 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
374 "type %i\n", err);
375 return err;
378 mnt = kern_mount_data(&dev_fs_type, options);
379 if (IS_ERR(mnt)) {
380 err = PTR_ERR(mnt);
381 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
382 unregister_filesystem(&dev_fs_type);
383 return err;
385 dev_mnt = mnt;
387 printk(KERN_INFO "devtmpfs: initialized\n");
388 return 0;