devtmpfs.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * devtmpfs - kernel-maintained tmpfs-based /dev
  4. *
  5. * Copyright (C) 2009, Kay Sievers <kay.sievers@vrfy.org>
  6. *
  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 name of the device,
  12. * owned by root and have a default mode of 0600. Subsystems can
  13. * overwrite the default setting if needed.
  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/ramfs.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/kthread.h>
  27. #include <linux/init_syscalls.h>
  28. #include <uapi/linux/mount.h>
  29. #include "base.h"
  30. static struct task_struct *thread;
  31. static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
  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. umode_t mode; /* 0 => delete */
  39. kuid_t uid;
  40. kgid_t gid;
  41. struct device *dev;
  42. } *requests;
  43. static int __init mount_param(char *str)
  44. {
  45. mount_dev = simple_strtoul(str, NULL, 0);
  46. return 1;
  47. }
  48. __setup("devtmpfs.mount=", mount_param);
  49. static struct vfsmount *mnt;
  50. static struct dentry *public_dev_mount(struct file_system_type *fs_type, int flags,
  51. const char *dev_name, void *data)
  52. {
  53. struct super_block *s = mnt->mnt_sb;
  54. int err;
  55. atomic_inc(&s->s_active);
  56. down_write(&s->s_umount);
  57. err = reconfigure_single(s, flags, data);
  58. if (err < 0) {
  59. deactivate_locked_super(s);
  60. return ERR_PTR(err);
  61. }
  62. return dget(s->s_root);
  63. }
  64. static struct file_system_type internal_fs_type = {
  65. .name = "devtmpfs",
  66. #ifdef CONFIG_TMPFS
  67. .init_fs_context = shmem_init_fs_context,
  68. .parameters = shmem_fs_parameters,
  69. #else
  70. .init_fs_context = ramfs_init_fs_context,
  71. .parameters = ramfs_fs_parameters,
  72. #endif
  73. .kill_sb = kill_litter_super,
  74. };
  75. static struct file_system_type dev_fs_type = {
  76. .name = "devtmpfs",
  77. .mount = public_dev_mount,
  78. };
  79. #ifdef CONFIG_BLOCK
  80. static inline int is_blockdev(struct device *dev)
  81. {
  82. return dev->class == &block_class;
  83. }
  84. #else
  85. static inline int is_blockdev(struct device *dev) { return 0; }
  86. #endif
  87. static int devtmpfs_submit_req(struct req *req, const char *tmp)
  88. {
  89. init_completion(&req->done);
  90. spin_lock(&req_lock);
  91. req->next = requests;
  92. requests = req;
  93. spin_unlock(&req_lock);
  94. wake_up_process(thread);
  95. wait_for_completion(&req->done);
  96. kfree(tmp);
  97. return req->err;
  98. }
  99. int devtmpfs_create_node(struct device *dev)
  100. {
  101. const char *tmp = NULL;
  102. struct req req;
  103. if (!thread)
  104. return 0;
  105. req.mode = 0;
  106. req.uid = GLOBAL_ROOT_UID;
  107. req.gid = GLOBAL_ROOT_GID;
  108. req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
  109. if (!req.name)
  110. return -ENOMEM;
  111. if (req.mode == 0)
  112. req.mode = 0600;
  113. if (is_blockdev(dev))
  114. req.mode |= S_IFBLK;
  115. else
  116. req.mode |= S_IFCHR;
  117. req.dev = dev;
  118. return devtmpfs_submit_req(&req, tmp);
  119. }
  120. int devtmpfs_delete_node(struct device *dev)
  121. {
  122. const char *tmp = NULL;
  123. struct req req;
  124. if (!thread)
  125. return 0;
  126. req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
  127. if (!req.name)
  128. return -ENOMEM;
  129. req.mode = 0;
  130. req.dev = dev;
  131. return devtmpfs_submit_req(&req, tmp);
  132. }
  133. static int dev_mkdir(const char *name, umode_t mode)
  134. {
  135. struct dentry *dentry;
  136. struct path path;
  137. int err;
  138. dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
  139. if (IS_ERR(dentry))
  140. return PTR_ERR(dentry);
  141. err = vfs_mkdir(d_inode(path.dentry), dentry, mode);
  142. if (!err)
  143. /* mark as kernel-created inode */
  144. d_inode(dentry)->i_private = &thread;
  145. done_path_create(&path, dentry);
  146. return err;
  147. }
  148. static int create_path(const char *nodepath)
  149. {
  150. char *path;
  151. char *s;
  152. int err = 0;
  153. /* parent directories do not exist, create them */
  154. path = kstrdup(nodepath, GFP_KERNEL);
  155. if (!path)
  156. return -ENOMEM;
  157. s = path;
  158. for (;;) {
  159. s = strchr(s, '/');
  160. if (!s)
  161. break;
  162. s[0] = '\0';
  163. err = dev_mkdir(path, 0755);
  164. if (err && err != -EEXIST)
  165. break;
  166. s[0] = '/';
  167. s++;
  168. }
  169. kfree(path);
  170. return err;
  171. }
  172. static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
  173. kgid_t gid, struct device *dev)
  174. {
  175. struct dentry *dentry;
  176. struct path path;
  177. int err;
  178. dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
  179. if (dentry == ERR_PTR(-ENOENT)) {
  180. create_path(nodename);
  181. dentry = kern_path_create(AT_FDCWD, nodename, &path, 0);
  182. }
  183. if (IS_ERR(dentry))
  184. return PTR_ERR(dentry);
  185. err = vfs_mknod(d_inode(path.dentry), dentry, mode, dev->devt);
  186. if (!err) {
  187. struct iattr newattrs;
  188. newattrs.ia_mode = mode;
  189. newattrs.ia_uid = uid;
  190. newattrs.ia_gid = gid;
  191. newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
  192. inode_lock(d_inode(dentry));
  193. notify_change(dentry, &newattrs, NULL);
  194. inode_unlock(d_inode(dentry));
  195. /* mark as kernel-created inode */
  196. d_inode(dentry)->i_private = &thread;
  197. }
  198. done_path_create(&path, dentry);
  199. return err;
  200. }
  201. static int dev_rmdir(const char *name)
  202. {
  203. struct path parent;
  204. struct dentry *dentry;
  205. int err;
  206. dentry = kern_path_locked(name, &parent);
  207. if (IS_ERR(dentry))
  208. return PTR_ERR(dentry);
  209. if (d_really_is_positive(dentry)) {
  210. if (d_inode(dentry)->i_private == &thread)
  211. err = vfs_rmdir(d_inode(parent.dentry), dentry);
  212. else
  213. err = -EPERM;
  214. } else {
  215. err = -ENOENT;
  216. }
  217. dput(dentry);
  218. inode_unlock(d_inode(parent.dentry));
  219. path_put(&parent);
  220. return err;
  221. }
  222. static int delete_path(const char *nodepath)
  223. {
  224. char *path;
  225. int err = 0;
  226. path = kstrdup(nodepath, GFP_KERNEL);
  227. if (!path)
  228. return -ENOMEM;
  229. for (;;) {
  230. char *base;
  231. base = strrchr(path, '/');
  232. if (!base)
  233. break;
  234. base[0] = '\0';
  235. err = dev_rmdir(path);
  236. if (err)
  237. break;
  238. }
  239. kfree(path);
  240. return err;
  241. }
  242. static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
  243. {
  244. /* did we create it */
  245. if (inode->i_private != &thread)
  246. return 0;
  247. /* does the dev_t match */
  248. if (is_blockdev(dev)) {
  249. if (!S_ISBLK(stat->mode))
  250. return 0;
  251. } else {
  252. if (!S_ISCHR(stat->mode))
  253. return 0;
  254. }
  255. if (stat->rdev != dev->devt)
  256. return 0;
  257. /* ours */
  258. return 1;
  259. }
  260. static int handle_remove(const char *nodename, struct device *dev)
  261. {
  262. struct path parent;
  263. struct dentry *dentry;
  264. int deleted = 0;
  265. int err;
  266. dentry = kern_path_locked(nodename, &parent);
  267. if (IS_ERR(dentry))
  268. return PTR_ERR(dentry);
  269. if (d_really_is_positive(dentry)) {
  270. struct kstat stat;
  271. struct path p = {.mnt = parent.mnt, .dentry = dentry};
  272. err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
  273. AT_STATX_SYNC_AS_STAT);
  274. if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
  275. struct iattr newattrs;
  276. /*
  277. * before unlinking this node, reset permissions
  278. * of possible references like hardlinks
  279. */
  280. newattrs.ia_uid = GLOBAL_ROOT_UID;
  281. newattrs.ia_gid = GLOBAL_ROOT_GID;
  282. newattrs.ia_mode = stat.mode & ~0777;
  283. newattrs.ia_valid =
  284. ATTR_UID|ATTR_GID|ATTR_MODE;
  285. inode_lock(d_inode(dentry));
  286. notify_change(dentry, &newattrs, NULL);
  287. inode_unlock(d_inode(dentry));
  288. err = vfs_unlink(d_inode(parent.dentry), dentry, NULL);
  289. if (!err || err == -ENOENT)
  290. deleted = 1;
  291. }
  292. } else {
  293. err = -ENOENT;
  294. }
  295. dput(dentry);
  296. inode_unlock(d_inode(parent.dentry));
  297. path_put(&parent);
  298. if (deleted && strchr(nodename, '/'))
  299. delete_path(nodename);
  300. return err;
  301. }
  302. /*
  303. * If configured, or requested by the commandline, devtmpfs will be
  304. * auto-mounted after the kernel mounted the root filesystem.
  305. */
  306. int __init devtmpfs_mount(void)
  307. {
  308. int err;
  309. if (!mount_dev)
  310. return 0;
  311. if (!thread)
  312. return 0;
  313. err = init_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL);
  314. if (err)
  315. printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
  316. else
  317. printk(KERN_INFO "devtmpfs: mounted\n");
  318. return err;
  319. }
  320. static DECLARE_COMPLETION(setup_done);
  321. static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
  322. struct device *dev)
  323. {
  324. if (mode)
  325. return handle_create(name, mode, uid, gid, dev);
  326. else
  327. return handle_remove(name, dev);
  328. }
  329. static void __noreturn devtmpfs_work_loop(void)
  330. {
  331. while (1) {
  332. spin_lock(&req_lock);
  333. while (requests) {
  334. struct req *req = requests;
  335. requests = NULL;
  336. spin_unlock(&req_lock);
  337. while (req) {
  338. struct req *next = req->next;
  339. req->err = handle(req->name, req->mode,
  340. req->uid, req->gid, req->dev);
  341. complete(&req->done);
  342. req = next;
  343. }
  344. spin_lock(&req_lock);
  345. }
  346. __set_current_state(TASK_INTERRUPTIBLE);
  347. spin_unlock(&req_lock);
  348. schedule();
  349. }
  350. }
  351. static int __init devtmpfs_setup(void *p)
  352. {
  353. int err;
  354. err = ksys_unshare(CLONE_NEWNS);
  355. if (err)
  356. goto out;
  357. err = init_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
  358. if (err)
  359. goto out;
  360. init_chdir("/.."); /* will traverse into overmounted root */
  361. init_chroot(".");
  362. out:
  363. *(int *)p = err;
  364. return err;
  365. }
  366. /*
  367. * The __ref is because devtmpfs_setup needs to be __init for the routines it
  368. * calls. That call is done while devtmpfs_init, which is marked __init,
  369. * synchronously waits for it to complete.
  370. */
  371. static int __ref devtmpfsd(void *p)
  372. {
  373. int err = devtmpfs_setup(p);
  374. complete(&setup_done);
  375. if (err)
  376. return err;
  377. devtmpfs_work_loop();
  378. return 0;
  379. }
  380. /*
  381. * Create devtmpfs instance, driver-core devices will add their device
  382. * nodes here.
  383. */
  384. int __init devtmpfs_init(void)
  385. {
  386. char opts[] = "mode=0755";
  387. int err;
  388. mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
  389. if (IS_ERR(mnt)) {
  390. printk(KERN_ERR "devtmpfs: unable to create devtmpfs %ld\n",
  391. PTR_ERR(mnt));
  392. return PTR_ERR(mnt);
  393. }
  394. err = register_filesystem(&dev_fs_type);
  395. if (err) {
  396. printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
  397. "type %i\n", err);
  398. return err;
  399. }
  400. thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
  401. if (!IS_ERR(thread)) {
  402. wait_for_completion(&setup_done);
  403. } else {
  404. err = PTR_ERR(thread);
  405. thread = NULL;
  406. }
  407. if (err) {
  408. printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
  409. unregister_filesystem(&dev_fs_type);
  410. return err;
  411. }
  412. printk(KERN_INFO "devtmpfs: initialized\n");
  413. return 0;
  414. }