GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / base / devtmpfs.c
blob60316100bad610755facad0725bb66921fa0c207
1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
2 /*
3 * devtmpfs - kernel-maintained tmpfs-based /dev
5 * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
7 * During bootup, before any driver core device is registered,
8 * devtmpfs, a tmpfs-based filesystem is created. Every driver-core
9 * device which requests a device node, will add a node in this
10 * filesystem.
11 * By default, all devices are named after the the name of the
12 * device, owned by root and have a default mode of 0600. Subsystems
13 * can overwrite the default setting if needed.
16 #include <linux/kernel.h>
17 #include <linux/syscalls.h>
18 #include <linux/mount.h>
19 #include <linux/device.h>
20 #include <linux/genhd.h>
21 #include <linux/namei.h>
22 #include <linux/fs.h>
23 #include <linux/shmem_fs.h>
24 #include <linux/ramfs.h>
25 #include <linux/cred.h>
26 #include <linux/sched.h>
27 #include <linux/init_task.h>
28 #include <linux/slab.h>
30 static struct vfsmount *dev_mnt;
32 #if defined CONFIG_DEVTMPFS_MOUNT
33 static int dev_mount = 1;
34 #else
35 static int dev_mount;
36 #endif
38 static DEFINE_MUTEX(dirlock);
40 static int __init mount_param(char *str)
42 dev_mount = simple_strtoul(str, NULL, 0);
43 return 1;
45 __setup("devtmpfs.mount=", mount_param);
47 static int dev_get_sb(struct file_system_type *fs_type, int flags,
48 const char *dev_name, void *data, struct vfsmount *mnt)
50 #ifdef CONFIG_TMPFS
51 return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
52 #else
53 return get_sb_single(fs_type, flags, data, ramfs_fill_super, mnt);
54 #endif
57 static struct file_system_type dev_fs_type = {
58 .name = "devtmpfs",
59 .get_sb = dev_get_sb,
60 .kill_sb = kill_litter_super,
63 #ifdef CONFIG_BLOCK
64 static inline int is_blockdev(struct device *dev)
66 return dev->class == &block_class;
68 #else
69 static inline int is_blockdev(struct device *dev) { return 0; }
70 #endif
72 static int dev_mkdir(const char *name, mode_t mode)
74 struct nameidata nd;
75 struct dentry *dentry;
76 int err;
78 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
79 name, LOOKUP_PARENT, &nd);
80 if (err)
81 return err;
83 dentry = lookup_create(&nd, 1);
84 if (!IS_ERR(dentry)) {
85 err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
86 if (!err)
87 /* mark as kernel-created inode */
88 dentry->d_inode->i_private = &dev_mnt;
89 dput(dentry);
90 } else {
91 err = PTR_ERR(dentry);
94 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
95 path_put(&nd.path);
96 return err;
99 static int create_path(const char *nodepath)
101 int err;
103 mutex_lock(&dirlock);
104 err = dev_mkdir(nodepath, 0755);
105 if (err == -ENOENT) {
106 char *path;
107 char *s;
109 /* parent directories do not exist, create them */
110 path = kstrdup(nodepath, GFP_KERNEL);
111 if (!path) {
112 err = -ENOMEM;
113 goto out;
115 s = path;
116 for (;;) {
117 s = strchr(s, '/');
118 if (!s)
119 break;
120 s[0] = '\0';
121 err = dev_mkdir(path, 0755);
122 if (err && err != -EEXIST)
123 break;
124 s[0] = '/';
125 s++;
127 kfree(path);
129 out:
130 mutex_unlock(&dirlock);
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);
160 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
161 nodename, LOOKUP_PARENT, &nd);
162 if (err == -ENOENT) {
163 create_path(nodename);
164 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
165 nodename, LOOKUP_PARENT, &nd);
167 if (err)
168 goto out;
170 dentry = lookup_create(&nd, 0);
171 if (!IS_ERR(dentry)) {
172 err = vfs_mknod(nd.path.dentry->d_inode,
173 dentry, mode, dev->devt);
174 if (!err) {
175 struct iattr newattrs;
177 /* fixup possibly umasked mode */
178 newattrs.ia_mode = mode;
179 newattrs.ia_valid = ATTR_MODE;
180 mutex_lock(&dentry->d_inode->i_mutex);
181 notify_change(dentry, &newattrs);
182 mutex_unlock(&dentry->d_inode->i_mutex);
184 /* mark as kernel-created inode */
185 dentry->d_inode->i_private = &dev_mnt;
187 dput(dentry);
188 } else {
189 err = PTR_ERR(dentry);
192 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
193 path_put(&nd.path);
194 out:
195 kfree(tmp);
196 revert_creds(curr_cred);
197 return err;
200 static int dev_rmdir(const char *name)
202 struct nameidata nd;
203 struct dentry *dentry;
204 int err;
206 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
207 name, LOOKUP_PARENT, &nd);
208 if (err)
209 return err;
211 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
212 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
213 if (!IS_ERR(dentry)) {
214 if (dentry->d_inode) {
215 if (dentry->d_inode->i_private == &dev_mnt)
216 err = vfs_rmdir(nd.path.dentry->d_inode,
217 dentry);
218 else
219 err = -EPERM;
220 } else {
221 err = -ENOENT;
223 dput(dentry);
224 } else {
225 err = PTR_ERR(dentry);
228 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
229 path_put(&nd.path);
230 return err;
233 static int delete_path(const char *nodepath)
235 const char *path;
236 int err = 0;
238 path = kstrdup(nodepath, GFP_KERNEL);
239 if (!path)
240 return -ENOMEM;
242 mutex_lock(&dirlock);
243 for (;;) {
244 char *base;
246 base = strrchr(path, '/');
247 if (!base)
248 break;
249 base[0] = '\0';
250 err = dev_rmdir(path);
251 if (err)
252 break;
254 mutex_unlock(&dirlock);
256 kfree(path);
257 return err;
260 static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
262 /* did we create it */
263 if (inode->i_private != &dev_mnt)
264 return 0;
266 /* does the dev_t match */
267 if (is_blockdev(dev)) {
268 if (!S_ISBLK(stat->mode))
269 return 0;
270 } else {
271 if (!S_ISCHR(stat->mode))
272 return 0;
274 if (stat->rdev != dev->devt)
275 return 0;
277 /* ours */
278 return 1;
281 int devtmpfs_delete_node(struct device *dev)
283 const char *tmp = NULL;
284 const char *nodename;
285 const struct cred *curr_cred;
286 struct nameidata nd;
287 struct dentry *dentry;
288 struct kstat stat;
289 int deleted = 1;
290 int err;
292 if (!dev_mnt)
293 return 0;
295 nodename = device_get_devnode(dev, NULL, &tmp);
296 if (!nodename)
297 return -ENOMEM;
299 curr_cred = override_creds(&init_cred);
300 err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
301 nodename, LOOKUP_PARENT, &nd);
302 if (err)
303 goto out;
305 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
306 dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
307 if (!IS_ERR(dentry)) {
308 if (dentry->d_inode) {
309 err = vfs_getattr(nd.path.mnt, dentry, &stat);
310 if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
311 struct iattr newattrs;
313 * before unlinking this node, reset permissions
314 * of possible references like hardlinks
316 newattrs.ia_uid = 0;
317 newattrs.ia_gid = 0;
318 newattrs.ia_mode = stat.mode & ~0777;
319 newattrs.ia_valid =
320 ATTR_UID|ATTR_GID|ATTR_MODE;
321 mutex_lock(&dentry->d_inode->i_mutex);
322 notify_change(dentry, &newattrs);
323 mutex_unlock(&dentry->d_inode->i_mutex);
324 err = vfs_unlink(nd.path.dentry->d_inode,
325 dentry);
326 if (!err || err == -ENOENT)
327 deleted = 1;
329 } else {
330 err = -ENOENT;
332 dput(dentry);
333 } else {
334 err = PTR_ERR(dentry);
336 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
338 path_put(&nd.path);
339 if (deleted && strchr(nodename, '/'))
340 delete_path(nodename);
341 out:
342 kfree(tmp);
343 revert_creds(curr_cred);
344 return err;
348 * If configured, or requested by the commandline, devtmpfs will be
349 * auto-mounted after the kernel mounted the root filesystem.
351 int devtmpfs_mount(const char *mntdir)
353 int err;
355 if (!dev_mount)
356 return 0;
358 if (!dev_mnt)
359 return 0;
361 err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
362 if (err)
363 printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
364 else
365 printk(KERN_INFO "devtmpfs: mounted\n");
366 return err;
370 * Create devtmpfs instance, driver-core devices will add their device
371 * nodes here.
373 int __init devtmpfs_init(void)
375 int err;
376 struct vfsmount *mnt;
377 char *options = "mode=0755";
378 /* LR: char options[] = "..."; caused misaligned access, gcc issue */
380 err = register_filesystem(&dev_fs_type);
381 if (err) {
382 printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
383 "type %i\n", err);
384 return err;
387 mnt = kern_mount_data(&dev_fs_type, options);
388 if (IS_ERR(mnt)) {
389 err = PTR_ERR(mnt);
390 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
391 unregister_filesystem(&dev_fs_type);
392 return err;
394 dev_mnt = mnt;
396 printk(KERN_INFO "devtmpfs: initialized\n");
397 return 0;