devtmpfs.c 7.9 KB

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