Driver core: devtmpfs: prevent concurrent subdirectory creation and removal
[linux-2.6.git] / drivers / base / devtmpfs.c
1 /*
2  * devtmpfs - kernel-maintained tmpfs-based /dev
3  *
4  * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
13  */
14
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
27 static struct vfsmount *dev_mnt;
28
29 #if defined CONFIG_DEVTMPFS_MOUNT
30 static int dev_mount = 1;
31 #else
32 static int dev_mount;
33 #endif
34
35 static rwlock_t dirlock;
36
37 static int __init mount_param(char *str)
38 {
39         dev_mount = simple_strtoul(str, NULL, 0);
40         return 1;
41 }
42 __setup("devtmpfs.mount=", mount_param);
43
44 static int dev_get_sb(struct file_system_type *fs_type, int flags,
45                       const char *dev_name, void *data, struct vfsmount *mnt)
46 {
47         return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
48 }
49
50 static struct file_system_type dev_fs_type = {
51         .name = "devtmpfs",
52         .get_sb = dev_get_sb,
53         .kill_sb = kill_litter_super,
54 };
55
56 #ifdef CONFIG_BLOCK
57 static inline int is_blockdev(struct device *dev)
58 {
59         return dev->class == &block_class;
60 }
61 #else
62 static inline int is_blockdev(struct device *dev) { return 0; }
63 #endif
64
65 static int dev_mkdir(const char *name, mode_t mode)
66 {
67         struct nameidata nd;
68         struct dentry *dentry;
69         int err;
70
71         err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
72                               name, LOOKUP_PARENT, &nd);
73         if (err)
74                 return err;
75
76         dentry = lookup_create(&nd, 1);
77         if (!IS_ERR(dentry)) {
78                 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
79                 dput(dentry);
80         } else {
81                 err = PTR_ERR(dentry);
82         }
83         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
84
85         path_put(&nd.path);
86         return err;
87 }
88
89 static int create_path(const char *nodepath)
90 {
91         struct nameidata nd;
92         int err = 0;
93
94         read_lock(&dirlock);
95         err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
96                               nodepath, LOOKUP_PARENT, &nd);
97         if (err == 0) {
98                 struct dentry *dentry;
99
100                 /* create directory right away */
101                 dentry = lookup_create(&nd, 1);
102                 if (!IS_ERR(dentry)) {
103                         err = vfs_mkdir(nd.path.dentry->d_inode,
104                                         dentry, 0755);
105                         dput(dentry);
106                 }
107                 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
108                 path_put(&nd.path);
109         } else if (err == -ENOENT) {
110                 char *path;
111                 char *s;
112
113                 /* parent directories do not exist, create them */
114                 path = kstrdup(nodepath, GFP_KERNEL);
115                 if (!path)
116                         return -ENOMEM;
117                 s = path;
118                 for (;;) {
119                         s = strchr(s, '/');
120                         if (!s)
121                                 break;
122                         s[0] = '\0';
123                         err = dev_mkdir(path, 0755);
124                         if (err && err != -EEXIST)
125                                 break;
126                         s[0] = '/';
127                         s++;
128                 }
129                 kfree(path);
130         }
131         read_unlock(&dirlock);
132
133         return err;
134 }
135
136 int devtmpfs_create_node(struct device *dev)
137 {
138         const char *tmp = NULL;
139         const char *nodename;
140         const struct cred *curr_cred;
141         mode_t mode = 0;
142         struct nameidata nd;
143         struct dentry *dentry;
144         int err;
145
146         if (!dev_mnt)
147                 return 0;
148
149         nodename = device_get_devnode(dev, &mode, &tmp);
150         if (!nodename)
151                 return -ENOMEM;
152
153         if (mode == 0)
154                 mode = 0600;
155         if (is_blockdev(dev))
156                 mode |= S_IFBLK;
157         else
158                 mode |= S_IFCHR;
159
160         curr_cred = override_creds(&init_cred);
161
162         err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
163                               nodename, LOOKUP_PARENT, &nd);
164         if (err == -ENOENT) {
165                 create_path(nodename);
166                 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
167                                       nodename, LOOKUP_PARENT, &nd);
168         }
169         if (err)
170                 goto out;
171
172         dentry = lookup_create(&nd, 0);
173         if (!IS_ERR(dentry)) {
174                 err = vfs_mknod(nd.path.dentry->d_inode,
175                                 dentry, mode, dev->devt);
176                 if (!err) {
177                         struct iattr newattrs;
178
179                         /* fixup possibly umasked mode */
180                         newattrs.ia_mode = mode;
181                         newattrs.ia_valid = ATTR_MODE;
182                         mutex_lock(&dentry->d_inode->i_mutex);
183                         notify_change(dentry, &newattrs);
184                         mutex_unlock(&dentry->d_inode->i_mutex);
185
186                         /* mark as kernel-created inode */
187                         dentry->d_inode->i_private = &dev_mnt;
188                 }
189                 dput(dentry);
190         } else {
191                 err = PTR_ERR(dentry);
192         }
193
194         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
195         path_put(&nd.path);
196 out:
197         kfree(tmp);
198         revert_creds(curr_cred);
199         return err;
200 }
201
202 static int dev_rmdir(const char *name)
203 {
204         struct nameidata nd;
205         struct dentry *dentry;
206         int err;
207
208         err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
209                               name, LOOKUP_PARENT, &nd);
210         if (err)
211                 return err;
212
213         mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
214         dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
215         if (!IS_ERR(dentry)) {
216                 if (dentry->d_inode)
217                         err = vfs_rmdir(nd.path.dentry->d_inode, dentry);
218                 else
219                         err = -ENOENT;
220                 dput(dentry);
221         } else {
222                 err = PTR_ERR(dentry);
223         }
224         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
225
226         path_put(&nd.path);
227         return err;
228 }
229
230 static int delete_path(const char *nodepath)
231 {
232         const char *path;
233         int err = 0;
234
235         path = kstrdup(nodepath, GFP_KERNEL);
236         if (!path)
237                 return -ENOMEM;
238
239         write_lock(&dirlock);
240         for (;;) {
241                 char *base;
242
243                 base = strrchr(path, '/');
244                 if (!base)
245                         break;
246                 base[0] = '\0';
247                 err = dev_rmdir(path);
248                 if (err)
249                         break;
250         }
251         write_unlock(&dirlock);
252
253         kfree(path);
254         return err;
255 }
256
257 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
258 {
259         /* did we create it */
260         if (inode->i_private != &dev_mnt)
261                 return 0;
262
263         /* does the dev_t match */
264         if (is_blockdev(dev)) {
265                 if (!S_ISBLK(stat->mode))
266                         return 0;
267         } else {
268                 if (!S_ISCHR(stat->mode))
269                         return 0;
270         }
271         if (stat->rdev != dev->devt)
272                 return 0;
273
274         /* ours */
275         return 1;
276 }
277
278 int devtmpfs_delete_node(struct device *dev)
279 {
280         const char *tmp = NULL;
281         const char *nodename;
282         const struct cred *curr_cred;
283         struct nameidata nd;
284         struct dentry *dentry;
285         struct kstat stat;
286         int deleted = 1;
287         int err;
288
289         if (!dev_mnt)
290                 return 0;
291
292         nodename = device_get_devnode(dev, NULL, &tmp);
293         if (!nodename)
294                 return -ENOMEM;
295
296         curr_cred = override_creds(&init_cred);
297         err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
298                               nodename, LOOKUP_PARENT, &nd);
299         if (err)
300                 goto out;
301
302         mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
303         dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
304         if (!IS_ERR(dentry)) {
305                 if (dentry->d_inode) {
306                         err = vfs_getattr(nd.path.mnt, dentry, &stat);
307                         if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
308                                 err = vfs_unlink(nd.path.dentry->d_inode,
309                                                  dentry);
310                                 if (!err || err == -ENOENT)
311                                         deleted = 1;
312                         }
313                 } else {
314                         err = -ENOENT;
315                 }
316                 dput(dentry);
317         } else {
318                 err = PTR_ERR(dentry);
319         }
320         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
321
322         path_put(&nd.path);
323         if (deleted && strchr(nodename, '/'))
324                 delete_path(nodename);
325 out:
326         kfree(tmp);
327         revert_creds(curr_cred);
328         return err;
329 }
330
331 /*
332  * If configured, or requested by the commandline, devtmpfs will be
333  * auto-mounted after the kernel mounted the root filesystem.
334  */
335 int devtmpfs_mount(const char *mountpoint)
336 {
337         struct path path;
338         int err;
339
340         if (!dev_mount)
341                 return 0;
342
343         if (!dev_mnt)
344                 return 0;
345
346         err = kern_path(mountpoint, LOOKUP_FOLLOW, &path);
347         if (err)
348                 return err;
349         err = do_add_mount(dev_mnt, &path, 0, NULL);
350         if (err)
351                 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
352         else
353                 printk(KERN_INFO "devtmpfs: mounted\n");
354         path_put(&path);
355         return err;
356 }
357
358 /*
359  * Create devtmpfs instance, driver-core devices will add their device
360  * nodes here.
361  */
362 int __init devtmpfs_init(void)
363 {
364         int err;
365         struct vfsmount *mnt;
366
367         rwlock_init(&dirlock);
368
369         err = register_filesystem(&dev_fs_type);
370         if (err) {
371                 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
372                        "type %i\n", err);
373                 return err;
374         }
375
376         mnt = kern_mount(&dev_fs_type);
377         if (IS_ERR(mnt)) {
378                 err = PTR_ERR(mnt);
379                 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
380                 unregister_filesystem(&dev_fs_type);
381                 return err;
382         }
383         dev_mnt = mnt;
384
385         printk(KERN_INFO "devtmpfs: initialized\n");
386         return 0;
387 }