Root/
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 | #include <linux/slab.h> |
27 | |
28 | static struct vfsmount *dev_mnt; |
29 | |
30 | #if defined CONFIG_DEVTMPFS_MOUNT |
31 | static int dev_mount = 1; |
32 | #else |
33 | static int dev_mount; |
34 | #endif |
35 | |
36 | static DEFINE_MUTEX(dirlock); |
37 | |
38 | static int __init mount_param(char *str) |
39 | { |
40 | dev_mount = simple_strtoul(str, NULL, 0); |
41 | return 1; |
42 | } |
43 | __setup("devtmpfs.mount=", mount_param); |
44 | |
45 | static int dev_get_sb(struct file_system_type *fs_type, int flags, |
46 | const char *dev_name, void *data, struct vfsmount *mnt) |
47 | { |
48 | return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt); |
49 | } |
50 | |
51 | static struct file_system_type dev_fs_type = { |
52 | .name = "devtmpfs", |
53 | .get_sb = dev_get_sb, |
54 | .kill_sb = kill_litter_super, |
55 | }; |
56 | |
57 | #ifdef CONFIG_BLOCK |
58 | static inline int is_blockdev(struct device *dev) |
59 | { |
60 | return dev->class == &block_class; |
61 | } |
62 | #else |
63 | static inline int is_blockdev(struct device *dev) { return 0; } |
64 | #endif |
65 | |
66 | static int dev_mkdir(const char *name, mode_t mode) |
67 | { |
68 | struct nameidata nd; |
69 | struct dentry *dentry; |
70 | int err; |
71 | |
72 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
73 | name, LOOKUP_PARENT, &nd); |
74 | if (err) |
75 | return err; |
76 | |
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); |
86 | } |
87 | |
88 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
89 | path_put(&nd.path); |
90 | return err; |
91 | } |
92 | |
93 | static int create_path(const char *nodepath) |
94 | { |
95 | int err; |
96 | |
97 | mutex_lock(&dirlock); |
98 | err = dev_mkdir(nodepath, 0755); |
99 | if (err == -ENOENT) { |
100 | char *path; |
101 | char *s; |
102 | |
103 | /* parent directories do not exist, create them */ |
104 | path = kstrdup(nodepath, GFP_KERNEL); |
105 | if (!path) { |
106 | err = -ENOMEM; |
107 | goto out; |
108 | } |
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++; |
120 | } |
121 | kfree(path); |
122 | } |
123 | out: |
124 | mutex_unlock(&dirlock); |
125 | return err; |
126 | } |
127 | |
128 | int devtmpfs_create_node(struct device *dev) |
129 | { |
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; |
137 | |
138 | if (!dev_mnt) |
139 | return 0; |
140 | |
141 | nodename = device_get_devnode(dev, &mode, &tmp); |
142 | if (!nodename) |
143 | return -ENOMEM; |
144 | |
145 | if (mode == 0) |
146 | mode = 0600; |
147 | if (is_blockdev(dev)) |
148 | mode |= S_IFBLK; |
149 | else |
150 | mode |= S_IFCHR; |
151 | |
152 | curr_cred = override_creds(&init_cred); |
153 | |
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); |
160 | } |
161 | if (err) |
162 | goto out; |
163 | |
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; |
170 | |
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); |
177 | |
178 | /* mark as kernel-created inode */ |
179 | dentry->d_inode->i_private = &dev_mnt; |
180 | } |
181 | dput(dentry); |
182 | } else { |
183 | err = PTR_ERR(dentry); |
184 | } |
185 | |
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; |
192 | } |
193 | |
194 | static int dev_rmdir(const char *name) |
195 | { |
196 | struct nameidata nd; |
197 | struct dentry *dentry; |
198 | int err; |
199 | |
200 | err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt, |
201 | name, LOOKUP_PARENT, &nd); |
202 | if (err) |
203 | return err; |
204 | |
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; |
216 | } |
217 | dput(dentry); |
218 | } else { |
219 | err = PTR_ERR(dentry); |
220 | } |
221 | |
222 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
223 | path_put(&nd.path); |
224 | return err; |
225 | } |
226 | |
227 | static int delete_path(const char *nodepath) |
228 | { |
229 | const char *path; |
230 | int err = 0; |
231 | |
232 | path = kstrdup(nodepath, GFP_KERNEL); |
233 | if (!path) |
234 | return -ENOMEM; |
235 | |
236 | mutex_lock(&dirlock); |
237 | for (;;) { |
238 | char *base; |
239 | |
240 | base = strrchr(path, '/'); |
241 | if (!base) |
242 | break; |
243 | base[0] = '\0'; |
244 | err = dev_rmdir(path); |
245 | if (err) |
246 | break; |
247 | } |
248 | mutex_unlock(&dirlock); |
249 | |
250 | kfree(path); |
251 | return err; |
252 | } |
253 | |
254 | static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat) |
255 | { |
256 | /* did we create it */ |
257 | if (inode->i_private != &dev_mnt) |
258 | return 0; |
259 | |
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; |
267 | } |
268 | if (stat->rdev != dev->devt) |
269 | return 0; |
270 | |
271 | /* ours */ |
272 | return 1; |
273 | } |
274 | |
275 | int devtmpfs_delete_node(struct device *dev) |
276 | { |
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; |
285 | |
286 | if (!dev_mnt) |
287 | return 0; |
288 | |
289 | nodename = device_get_devnode(dev, NULL, &tmp); |
290 | if (!nodename) |
291 | return -ENOMEM; |
292 | |
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; |
298 | |
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; |
306 | /* |
307 | * before unlinking this node, reset permissions |
308 | * of possible references like hardlinks |
309 | */ |
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; |
322 | } |
323 | } else { |
324 | err = -ENOENT; |
325 | } |
326 | dput(dentry); |
327 | } else { |
328 | err = PTR_ERR(dentry); |
329 | } |
330 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
331 | |
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; |
339 | } |
340 | |
341 | /* |
342 | * If configured, or requested by the commandline, devtmpfs will be |
343 | * auto-mounted after the kernel mounted the root filesystem. |
344 | */ |
345 | int devtmpfs_mount(const char *mntdir) |
346 | { |
347 | int err; |
348 | |
349 | if (!dev_mount) |
350 | return 0; |
351 | |
352 | if (!dev_mnt) |
353 | return 0; |
354 | |
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; |
361 | } |
362 | |
363 | /* |
364 | * Create devtmpfs instance, driver-core devices will add their device |
365 | * nodes here. |
366 | */ |
367 | int __init devtmpfs_init(void) |
368 | { |
369 | int err; |
370 | struct vfsmount *mnt; |
371 | char options[] = "mode=0755"; |
372 | |
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; |
378 | } |
379 | |
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; |
386 | } |
387 | dev_mnt = mnt; |
388 | |
389 | printk(KERN_INFO "devtmpfs: initialized\n"); |
390 | return 0; |
391 | } |
392 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9