devtmpfs.c 7.6 KB

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