devtmpfs.c 9.1 KB

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