devtmpfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. #define pr_fmt(fmt) "devtmpfs: " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/mount.h>
  19. #include <linux/device.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/namei.h>
  22. #include <linux/fs.h>
  23. #include <linux/shmem_fs.h>
  24. #include <linux/ramfs.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/kthread.h>
  28. #include <linux/init_syscalls.h>
  29. #include <uapi/linux/mount.h>
  30. #include "base.h"
  31. #ifdef CONFIG_DEVTMPFS_SAFE
  32. #define DEVTMPFS_MFLAGS (MS_SILENT | MS_NOEXEC | MS_NOSUID)
  33. #else
  34. #define DEVTMPFS_MFLAGS (MS_SILENT)
  35. #endif
  36. static struct task_struct *thread;
  37. static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
  38. static DEFINE_SPINLOCK(req_lock);
  39. static struct req {
  40. struct req *next;
  41. struct completion done;
  42. int err;
  43. const char *name;
  44. umode_t mode; /* 0 => delete */
  45. kuid_t uid;
  46. kgid_t gid;
  47. struct device *dev;
  48. } *requests;
  49. static int __init mount_param(char *str)
  50. {
  51. return kstrtoint(str, 0, &mount_dev) == 0;
  52. }
  53. __setup("devtmpfs.mount=", mount_param);
  54. static struct vfsmount *mnt;
  55. static struct file_system_type internal_fs_type = {
  56. .name = "devtmpfs",
  57. #ifdef CONFIG_TMPFS
  58. .init_fs_context = shmem_init_fs_context,
  59. #else
  60. .init_fs_context = ramfs_init_fs_context,
  61. #endif
  62. .kill_sb = kill_anon_super,
  63. };
  64. /* Simply take a ref on the existing mount */
  65. static int devtmpfs_get_tree(struct fs_context *fc)
  66. {
  67. struct super_block *sb = mnt->mnt_sb;
  68. atomic_inc(&sb->s_active);
  69. down_write(&sb->s_umount);
  70. fc->root = dget(sb->s_root);
  71. return 0;
  72. }
  73. /* Ops are filled in during init depending on underlying shmem or ramfs type */
  74. static struct fs_context_operations devtmpfs_context_ops = {};
  75. /* Call the underlying initialization and set to our ops */
  76. static int devtmpfs_init_fs_context(struct fs_context *fc)
  77. {
  78. int ret;
  79. #ifdef CONFIG_TMPFS
  80. ret = shmem_init_fs_context(fc);
  81. #else
  82. ret = ramfs_init_fs_context(fc);
  83. #endif
  84. if (ret < 0)
  85. return ret;
  86. fc->ops = &devtmpfs_context_ops;
  87. return 0;
  88. }
  89. static struct file_system_type dev_fs_type = {
  90. .name = "devtmpfs",
  91. .init_fs_context = devtmpfs_init_fs_context,
  92. };
  93. static int devtmpfs_submit_req(struct req *req, const char *tmp)
  94. {
  95. init_completion(&req->done);
  96. spin_lock(&req_lock);
  97. req->next = requests;
  98. requests = req;
  99. spin_unlock(&req_lock);
  100. wake_up_process(thread);
  101. wait_for_completion(&req->done);
  102. kfree(tmp);
  103. return req->err;
  104. }
  105. int devtmpfs_create_node(struct device *dev)
  106. {
  107. const char *tmp = NULL;
  108. struct req req;
  109. if (!thread)
  110. return 0;
  111. req.mode = 0;
  112. req.uid = GLOBAL_ROOT_UID;
  113. req.gid = GLOBAL_ROOT_GID;
  114. req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
  115. if (!req.name)
  116. return -ENOMEM;
  117. if (req.mode == 0)
  118. req.mode = 0600;
  119. if (is_blockdev(dev))
  120. req.mode |= S_IFBLK;
  121. else
  122. req.mode |= S_IFCHR;
  123. req.dev = dev;
  124. return devtmpfs_submit_req(&req, tmp);
  125. }
  126. int devtmpfs_delete_node(struct device *dev)
  127. {
  128. const char *tmp = NULL;
  129. struct req req;
  130. if (!thread)
  131. return 0;
  132. req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
  133. if (!req.name)
  134. return -ENOMEM;
  135. req.mode = 0;
  136. req.dev = dev;
  137. return devtmpfs_submit_req(&req, tmp);
  138. }
  139. static int dev_mkdir(const char *name, umode_t mode)
  140. {
  141. struct dentry *dentry;
  142. struct path path;
  143. dentry = start_creating_path(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
  144. if (IS_ERR(dentry))
  145. return PTR_ERR(dentry);
  146. dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode, NULL);
  147. if (!IS_ERR(dentry))
  148. /* mark as kernel-created inode */
  149. d_inode(dentry)->i_private = &thread;
  150. end_creating_path(&path, dentry);
  151. return PTR_ERR_OR_ZERO(dentry);
  152. }
  153. static int create_path(const char *nodepath)
  154. {
  155. char *path;
  156. char *s;
  157. int err = 0;
  158. /* parent directories do not exist, create them */
  159. path = kstrdup(nodepath, GFP_KERNEL);
  160. if (!path)
  161. return -ENOMEM;
  162. s = path;
  163. for (;;) {
  164. s = strchr(s, '/');
  165. if (!s)
  166. break;
  167. s[0] = '\0';
  168. err = dev_mkdir(path, 0755);
  169. if (err && err != -EEXIST)
  170. break;
  171. s[0] = '/';
  172. s++;
  173. }
  174. kfree(path);
  175. return err;
  176. }
  177. static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
  178. kgid_t gid, struct device *dev)
  179. {
  180. struct dentry *dentry;
  181. struct path path;
  182. int err;
  183. dentry = start_creating_path(AT_FDCWD, nodename, &path, 0);
  184. if (dentry == ERR_PTR(-ENOENT)) {
  185. create_path(nodename);
  186. dentry = start_creating_path(AT_FDCWD, nodename, &path, 0);
  187. }
  188. if (IS_ERR(dentry))
  189. return PTR_ERR(dentry);
  190. err = vfs_mknod(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode,
  191. dev->devt, NULL);
  192. if (!err) {
  193. struct iattr newattrs;
  194. newattrs.ia_mode = mode;
  195. newattrs.ia_uid = uid;
  196. newattrs.ia_gid = gid;
  197. newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
  198. inode_lock(d_inode(dentry));
  199. notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
  200. inode_unlock(d_inode(dentry));
  201. /* mark as kernel-created inode */
  202. d_inode(dentry)->i_private = &thread;
  203. }
  204. end_creating_path(&path, dentry);
  205. return err;
  206. }
  207. static int dev_rmdir(const char *name)
  208. {
  209. struct path parent;
  210. struct dentry *dentry;
  211. int err;
  212. dentry = start_removing_path(name, &parent);
  213. if (IS_ERR(dentry))
  214. return PTR_ERR(dentry);
  215. if (d_inode(dentry)->i_private == &thread)
  216. err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
  217. dentry, NULL);
  218. else
  219. err = -EPERM;
  220. end_removing_path(&parent, dentry);
  221. return err;
  222. }
  223. static int delete_path(const char *nodepath)
  224. {
  225. char *path;
  226. int err = 0;
  227. path = kstrdup(nodepath, GFP_KERNEL);
  228. if (!path)
  229. return -ENOMEM;
  230. for (;;) {
  231. char *base;
  232. base = strrchr(path, '/');
  233. if (!base)
  234. break;
  235. base[0] = '\0';
  236. err = dev_rmdir(path);
  237. if (err)
  238. break;
  239. }
  240. kfree(path);
  241. return err;
  242. }
  243. static int dev_mynode(struct device *dev, struct inode *inode)
  244. {
  245. /* did we create it */
  246. if (inode->i_private != &thread)
  247. return 0;
  248. /* does the dev_t match */
  249. if (is_blockdev(dev)) {
  250. if (!S_ISBLK(inode->i_mode))
  251. return 0;
  252. } else {
  253. if (!S_ISCHR(inode->i_mode))
  254. return 0;
  255. }
  256. if (inode->i_rdev != dev->devt)
  257. return 0;
  258. /* ours */
  259. return 1;
  260. }
  261. static int handle_remove(const char *nodename, struct device *dev)
  262. {
  263. struct path parent;
  264. struct dentry *dentry;
  265. struct inode *inode;
  266. int deleted = 0;
  267. int err = 0;
  268. dentry = start_removing_path(nodename, &parent);
  269. if (IS_ERR(dentry))
  270. return PTR_ERR(dentry);
  271. inode = d_inode(dentry);
  272. if (dev_mynode(dev, inode)) {
  273. struct iattr newattrs;
  274. /*
  275. * before unlinking this node, reset permissions
  276. * of possible references like hardlinks
  277. */
  278. newattrs.ia_uid = GLOBAL_ROOT_UID;
  279. newattrs.ia_gid = GLOBAL_ROOT_GID;
  280. newattrs.ia_mode = inode->i_mode & ~0777;
  281. newattrs.ia_valid =
  282. ATTR_UID|ATTR_GID|ATTR_MODE;
  283. inode_lock(d_inode(dentry));
  284. notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
  285. inode_unlock(d_inode(dentry));
  286. err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
  287. dentry, NULL);
  288. if (!err || err == -ENOENT)
  289. deleted = 1;
  290. }
  291. end_removing_path(&parent, dentry);
  292. if (deleted && strchr(nodename, '/'))
  293. delete_path(nodename);
  294. return err;
  295. }
  296. /*
  297. * If configured, or requested by the commandline, devtmpfs will be
  298. * auto-mounted after the kernel mounted the root filesystem.
  299. */
  300. int __init devtmpfs_mount(void)
  301. {
  302. int err;
  303. if (!mount_dev)
  304. return 0;
  305. if (!thread)
  306. return 0;
  307. err = init_mount("devtmpfs", "dev", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
  308. if (err)
  309. pr_info("error mounting %d\n", err);
  310. else
  311. pr_info("mounted\n");
  312. return err;
  313. }
  314. static __initdata DECLARE_COMPLETION(setup_done);
  315. static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
  316. struct device *dev)
  317. {
  318. if (mode)
  319. return handle_create(name, mode, uid, gid, dev);
  320. else
  321. return handle_remove(name, dev);
  322. }
  323. static void __noreturn devtmpfs_work_loop(void)
  324. {
  325. while (1) {
  326. spin_lock(&req_lock);
  327. while (requests) {
  328. struct req *req = requests;
  329. requests = NULL;
  330. spin_unlock(&req_lock);
  331. while (req) {
  332. struct req *next = req->next;
  333. req->err = handle(req->name, req->mode,
  334. req->uid, req->gid, req->dev);
  335. complete(&req->done);
  336. req = next;
  337. }
  338. spin_lock(&req_lock);
  339. }
  340. __set_current_state(TASK_INTERRUPTIBLE);
  341. spin_unlock(&req_lock);
  342. schedule();
  343. }
  344. }
  345. static noinline int __init devtmpfs_setup(void *p)
  346. {
  347. int err;
  348. err = ksys_unshare(CLONE_NEWNS);
  349. if (err)
  350. goto out;
  351. err = init_mount("devtmpfs", "/", "devtmpfs", DEVTMPFS_MFLAGS, NULL);
  352. if (err)
  353. goto out;
  354. init_chdir("/.."); /* will traverse into overmounted root */
  355. init_chroot(".");
  356. out:
  357. *(int *)p = err;
  358. return err;
  359. }
  360. /*
  361. * The __ref is because devtmpfs_setup needs to be __init for the routines it
  362. * calls. That call is done while devtmpfs_init, which is marked __init,
  363. * synchronously waits for it to complete.
  364. */
  365. static int __ref devtmpfsd(void *p)
  366. {
  367. int err = devtmpfs_setup(p);
  368. complete(&setup_done);
  369. if (err)
  370. return err;
  371. devtmpfs_work_loop();
  372. return 0;
  373. }
  374. /*
  375. * Get the underlying (shmem/ramfs) context ops to build ours
  376. */
  377. static int devtmpfs_configure_context(void)
  378. {
  379. struct fs_context *fc;
  380. fc = fs_context_for_reconfigure(mnt->mnt_root, mnt->mnt_sb->s_flags,
  381. MS_RMT_MASK);
  382. if (IS_ERR(fc))
  383. return PTR_ERR(fc);
  384. /* Set up devtmpfs_context_ops based on underlying type */
  385. devtmpfs_context_ops.free = fc->ops->free;
  386. devtmpfs_context_ops.dup = fc->ops->dup;
  387. devtmpfs_context_ops.parse_param = fc->ops->parse_param;
  388. devtmpfs_context_ops.parse_monolithic = fc->ops->parse_monolithic;
  389. devtmpfs_context_ops.get_tree = &devtmpfs_get_tree;
  390. devtmpfs_context_ops.reconfigure = fc->ops->reconfigure;
  391. put_fs_context(fc);
  392. return 0;
  393. }
  394. /*
  395. * Create devtmpfs instance, driver-core devices will add their device
  396. * nodes here.
  397. */
  398. int __init devtmpfs_init(void)
  399. {
  400. char opts[] = "mode=0755";
  401. int err;
  402. mnt = vfs_kern_mount(&internal_fs_type, 0, "devtmpfs", opts);
  403. if (IS_ERR(mnt)) {
  404. pr_err("unable to create devtmpfs %ld\n", PTR_ERR(mnt));
  405. return PTR_ERR(mnt);
  406. }
  407. err = devtmpfs_configure_context();
  408. if (err) {
  409. pr_err("unable to configure devtmpfs type %d\n", err);
  410. return err;
  411. }
  412. err = register_filesystem(&dev_fs_type);
  413. if (err) {
  414. pr_err("unable to register devtmpfs type %d\n", err);
  415. return err;
  416. }
  417. thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
  418. if (!IS_ERR(thread)) {
  419. wait_for_completion(&setup_done);
  420. } else {
  421. err = PTR_ERR(thread);
  422. thread = NULL;
  423. }
  424. if (err) {
  425. pr_err("unable to create devtmpfs %d\n", err);
  426. unregister_filesystem(&dev_fs_type);
  427. thread = NULL;
  428. return err;
  429. }
  430. pr_info("initialized\n");
  431. return 0;
  432. }