devtmpfs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/ramfs.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/kthread.h>
  26. static struct task_struct *thread;
  27. #if defined CONFIG_DEVTMPFS_MOUNT
  28. static int mount_dev = 1;
  29. #else
  30. static int mount_dev;
  31. #endif
  32. static DEFINE_SPINLOCK(req_lock);
  33. static struct req {
  34. struct req *next;
  35. struct completion done;
  36. int err;
  37. const char *name;
  38. mode_t mode; /* 0 => delete */
  39. struct device *dev;
  40. } *requests;
  41. static int __init mount_param(char *str)
  42. {
  43. mount_dev = simple_strtoul(str, NULL, 0);
  44. return 1;
  45. }
  46. __setup("devtmpfs.mount=", mount_param);
  47. static struct dentry *dev_mount(struct file_system_type *fs_type, int flags,
  48. const char *dev_name, void *data)
  49. {
  50. #ifdef CONFIG_TMPFS
  51. return mount_single(fs_type, flags, data, shmem_fill_super);
  52. #else
  53. return mount_single(fs_type, flags, data, ramfs_fill_super);
  54. #endif
  55. }
  56. static struct file_system_type dev_fs_type = {
  57. .name = "devtmpfs",
  58. .mount = dev_mount,
  59. .kill_sb = kill_litter_super,
  60. };
  61. #ifdef CONFIG_BLOCK
  62. static inline int is_blockdev(struct device *dev)
  63. {
  64. return dev->class == &block_class;
  65. }
  66. #else
  67. static inline int is_blockdev(struct device *dev) { return 0; }
  68. #endif
  69. int devtmpfs_create_node(struct device *dev)
  70. {
  71. const char *tmp = NULL;
  72. struct req req;
  73. if (!thread)
  74. return 0;
  75. req.mode = 0;
  76. req.name = device_get_devnode(dev, &req.mode, &tmp);
  77. if (!req.name)
  78. return -ENOMEM;
  79. if (req.mode == 0)
  80. req.mode = 0600;
  81. if (is_blockdev(dev))
  82. req.mode |= S_IFBLK;
  83. else
  84. req.mode |= S_IFCHR;
  85. req.dev = dev;
  86. init_completion(&req.done);
  87. spin_lock(&req_lock);
  88. req.next = requests;
  89. requests = &req;
  90. spin_unlock(&req_lock);
  91. wake_up_process(thread);
  92. wait_for_completion(&req.done);
  93. kfree(tmp);
  94. return req.err;
  95. }
  96. int devtmpfs_delete_node(struct device *dev)
  97. {
  98. const char *tmp = NULL;
  99. struct req req;
  100. if (!thread)
  101. return 0;
  102. req.name = device_get_devnode(dev, NULL, &tmp);
  103. if (!req.name)
  104. return -ENOMEM;
  105. req.mode = 0;
  106. req.dev = dev;
  107. init_completion(&req.done);
  108. spin_lock(&req_lock);
  109. req.next = requests;
  110. requests = &req;
  111. spin_unlock(&req_lock);
  112. wake_up_process(thread);
  113. wait_for_completion(&req.done);
  114. kfree(tmp);
  115. return req.err;
  116. }
  117. static int dev_mkdir(const char *name, mode_t mode)
  118. {
  119. struct dentry *dentry;
  120. struct path path;
  121. int err;
  122. dentry = kern_path_create(AT_FDCWD, name, &path, 1);
  123. if (IS_ERR(dentry))
  124. return PTR_ERR(dentry);
  125. err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  126. if (!err)
  127. /* mark as kernel-created inode */
  128. dentry->d_inode->i_private = &thread;
  129. dput(dentry);
  130. mutex_unlock(&path.dentry->d_inode->i_mutex);
  131. path_put(&path);
  132. return err;
  133. }
  134. static int create_path(const char *nodepath)
  135. {
  136. char *path;
  137. char *s;
  138. int err = 0;
  139. /* parent directories do not exist, create them */
  140. path = kstrdup(nodepath, GFP_KERNEL);
  141. if (!path)
  142. return -ENOMEM;
  143. s = path;
  144. for (;;) {
  145. s = strchr(s, '/');
  146. if (!s)
  147. break;
  148. s[0] = '\0';
  149. err = dev_mkdir(path, 0755);
  150. if (err && err != -EEXIST)
  151. break;
  152. s[0] = '/';
  153. s++;
  154. }
  155. kfree(path);
  156. return err;
  157. }
  158. static int handle_create(const char *nodename, mode_t mode, struct device *dev)
  159. {
  160. struct dentry *dentry;
  161. struct path path;
  162. int err;
  163. dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
  164. if (dentry == ERR_PTR(-ENOENT)) {
  165. create_path(nodename);
  166. dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
  167. }
  168. if (IS_ERR(dentry))
  169. return PTR_ERR(dentry);
  170. err = vfs_mknod(path.dentry->d_inode,
  171. dentry, mode, dev->devt);
  172. if (!err) {
  173. struct iattr newattrs;
  174. /* fixup possibly umasked mode */
  175. newattrs.ia_mode = mode;
  176. newattrs.ia_valid = ATTR_MODE;
  177. mutex_lock(&dentry->d_inode->i_mutex);
  178. notify_change(dentry, &newattrs);
  179. mutex_unlock(&dentry->d_inode->i_mutex);
  180. /* mark as kernel-created inode */
  181. dentry->d_inode->i_private = &thread;
  182. }
  183. dput(dentry);
  184. mutex_unlock(&path.dentry->d_inode->i_mutex);
  185. path_put(&path);
  186. return err;
  187. }
  188. static int dev_rmdir(const char *name)
  189. {
  190. struct nameidata nd;
  191. struct dentry *dentry;
  192. int err;
  193. err = kern_path_parent(name, &nd);
  194. if (err)
  195. return err;
  196. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  197. dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
  198. if (!IS_ERR(dentry)) {
  199. if (dentry->d_inode) {
  200. if (dentry->d_inode->i_private == &thread)
  201. err = vfs_rmdir(nd.path.dentry->d_inode,
  202. dentry);
  203. else
  204. err = -EPERM;
  205. } else {
  206. err = -ENOENT;
  207. }
  208. dput(dentry);
  209. } else {
  210. err = PTR_ERR(dentry);
  211. }
  212. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  213. path_put(&nd.path);
  214. return err;
  215. }
  216. static int delete_path(const char *nodepath)
  217. {
  218. const char *path;
  219. int err = 0;
  220. path = kstrdup(nodepath, GFP_KERNEL);
  221. if (!path)
  222. return -ENOMEM;
  223. for (;;) {
  224. char *base;
  225. base = strrchr(path, '/');
  226. if (!base)
  227. break;
  228. base[0] = '\0';
  229. err = dev_rmdir(path);
  230. if (err)
  231. break;
  232. }
  233. kfree(path);
  234. return err;
  235. }
  236. static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
  237. {
  238. /* did we create it */
  239. if (inode->i_private != &thread)
  240. return 0;
  241. /* does the dev_t match */
  242. if (is_blockdev(dev)) {
  243. if (!S_ISBLK(stat->mode))
  244. return 0;
  245. } else {
  246. if (!S_ISCHR(stat->mode))
  247. return 0;
  248. }
  249. if (stat->rdev != dev->devt)
  250. return 0;
  251. /* ours */
  252. return 1;
  253. }
  254. static int handle_remove(const char *nodename, struct device *dev)
  255. {
  256. struct nameidata nd;
  257. struct dentry *dentry;
  258. struct kstat stat;
  259. int deleted = 1;
  260. int err;
  261. err = kern_path_parent(nodename, &nd);
  262. if (err)
  263. return err;
  264. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  265. dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
  266. if (!IS_ERR(dentry)) {
  267. if (dentry->d_inode) {
  268. err = vfs_getattr(nd.path.mnt, dentry, &stat);
  269. if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
  270. struct iattr newattrs;
  271. /*
  272. * before unlinking this node, reset permissions
  273. * of possible references like hardlinks
  274. */
  275. newattrs.ia_uid = 0;
  276. newattrs.ia_gid = 0;
  277. newattrs.ia_mode = stat.mode & ~0777;
  278. newattrs.ia_valid =
  279. ATTR_UID|ATTR_GID|ATTR_MODE;
  280. mutex_lock(&dentry->d_inode->i_mutex);
  281. notify_change(dentry, &newattrs);
  282. mutex_unlock(&dentry->d_inode->i_mutex);
  283. err = vfs_unlink(nd.path.dentry->d_inode,
  284. dentry);
  285. if (!err || err == -ENOENT)
  286. deleted = 1;
  287. }
  288. } else {
  289. err = -ENOENT;
  290. }
  291. dput(dentry);
  292. } else {
  293. err = PTR_ERR(dentry);
  294. }
  295. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  296. path_put(&nd.path);
  297. if (deleted && strchr(nodename, '/'))
  298. delete_path(nodename);
  299. return err;
  300. }
  301. /*
  302. * If configured, or requested by the commandline, devtmpfs will be
  303. * auto-mounted after the kernel mounted the root filesystem.
  304. */
  305. int devtmpfs_mount(const char *mntdir)
  306. {
  307. int err;
  308. if (!mount_dev)
  309. return 0;
  310. if (!thread)
  311. return 0;
  312. err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
  313. if (err)
  314. printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
  315. else
  316. printk(KERN_INFO "devtmpfs: mounted\n");
  317. return err;
  318. }
  319. static __initdata DECLARE_COMPLETION(setup_done);
  320. static int handle(const char *name, mode_t mode, struct device *dev)
  321. {
  322. if (mode)
  323. return handle_create(name, mode, dev);
  324. else
  325. return handle_remove(name, dev);
  326. }
  327. static int devtmpfsd(void *p)
  328. {
  329. char options[] = "mode=0755";
  330. int *err = p;
  331. *err = sys_unshare(CLONE_NEWNS);
  332. if (*err)
  333. goto out;
  334. *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
  335. if (*err)
  336. goto out;
  337. sys_chdir("/.."); /* will traverse into overmounted root */
  338. sys_chroot(".");
  339. complete(&setup_done);
  340. while (1) {
  341. spin_lock(&req_lock);
  342. while (requests) {
  343. struct req *req = requests;
  344. requests = NULL;
  345. spin_unlock(&req_lock);
  346. while (req) {
  347. struct req *next = req->next;
  348. req->err = handle(req->name, req->mode, req->dev);
  349. complete(&req->done);
  350. req = next;
  351. }
  352. spin_lock(&req_lock);
  353. }
  354. set_current_state(TASK_INTERRUPTIBLE);
  355. spin_unlock(&req_lock);
  356. schedule();
  357. __set_current_state(TASK_RUNNING);
  358. }
  359. return 0;
  360. out:
  361. complete(&setup_done);
  362. return *err;
  363. }
  364. /*
  365. * Create devtmpfs instance, driver-core devices will add their device
  366. * nodes here.
  367. */
  368. int __init devtmpfs_init(void)
  369. {
  370. int err = register_filesystem(&dev_fs_type);
  371. if (err) {
  372. printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
  373. "type %i\n", err);
  374. return err;
  375. }
  376. thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
  377. if (!IS_ERR(thread)) {
  378. wait_for_completion(&setup_done);
  379. } else {
  380. err = PTR_ERR(thread);
  381. thread = NULL;
  382. }
  383. if (err) {
  384. printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
  385. unregister_filesystem(&dev_fs_type);
  386. return err;
  387. }
  388. printk(KERN_INFO "devtmpfs: initialized\n");
  389. return 0;
  390. }