devtmpfs.c 9.0 KB

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