devtmpfs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. umode_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, umode_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, umode_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 path parent;
  191. struct dentry *dentry;
  192. int err;
  193. dentry = kern_path_locked(name, &parent);
  194. if (IS_ERR(dentry))
  195. return PTR_ERR(dentry);
  196. if (dentry->d_inode) {
  197. if (dentry->d_inode->i_private == &thread)
  198. err = vfs_rmdir(parent.dentry->d_inode, dentry);
  199. else
  200. err = -EPERM;
  201. } else {
  202. err = -ENOENT;
  203. }
  204. dput(dentry);
  205. mutex_unlock(&parent.dentry->d_inode->i_mutex);
  206. path_put(&parent);
  207. return err;
  208. }
  209. static int delete_path(const char *nodepath)
  210. {
  211. const char *path;
  212. int err = 0;
  213. path = kstrdup(nodepath, GFP_KERNEL);
  214. if (!path)
  215. return -ENOMEM;
  216. for (;;) {
  217. char *base;
  218. base = strrchr(path, '/');
  219. if (!base)
  220. break;
  221. base[0] = '\0';
  222. err = dev_rmdir(path);
  223. if (err)
  224. break;
  225. }
  226. kfree(path);
  227. return err;
  228. }
  229. static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
  230. {
  231. /* did we create it */
  232. if (inode->i_private != &thread)
  233. return 0;
  234. /* does the dev_t match */
  235. if (is_blockdev(dev)) {
  236. if (!S_ISBLK(stat->mode))
  237. return 0;
  238. } else {
  239. if (!S_ISCHR(stat->mode))
  240. return 0;
  241. }
  242. if (stat->rdev != dev->devt)
  243. return 0;
  244. /* ours */
  245. return 1;
  246. }
  247. static int handle_remove(const char *nodename, struct device *dev)
  248. {
  249. struct path parent;
  250. struct dentry *dentry;
  251. int deleted = 1;
  252. int err;
  253. dentry = kern_path_locked(nodename, &parent);
  254. if (IS_ERR(dentry))
  255. return PTR_ERR(dentry);
  256. if (dentry->d_inode) {
  257. struct kstat stat;
  258. err = vfs_getattr(parent.mnt, dentry, &stat);
  259. if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
  260. struct iattr newattrs;
  261. /*
  262. * before unlinking this node, reset permissions
  263. * of possible references like hardlinks
  264. */
  265. newattrs.ia_uid = 0;
  266. newattrs.ia_gid = 0;
  267. newattrs.ia_mode = stat.mode & ~0777;
  268. newattrs.ia_valid =
  269. ATTR_UID|ATTR_GID|ATTR_MODE;
  270. mutex_lock(&dentry->d_inode->i_mutex);
  271. notify_change(dentry, &newattrs);
  272. mutex_unlock(&dentry->d_inode->i_mutex);
  273. err = vfs_unlink(parent.dentry->d_inode, dentry);
  274. if (!err || err == -ENOENT)
  275. deleted = 1;
  276. }
  277. } else {
  278. err = -ENOENT;
  279. }
  280. dput(dentry);
  281. mutex_unlock(&parent.dentry->d_inode->i_mutex);
  282. path_put(&parent);
  283. if (deleted && strchr(nodename, '/'))
  284. delete_path(nodename);
  285. return err;
  286. }
  287. /*
  288. * If configured, or requested by the commandline, devtmpfs will be
  289. * auto-mounted after the kernel mounted the root filesystem.
  290. */
  291. int devtmpfs_mount(const char *mntdir)
  292. {
  293. int err;
  294. if (!mount_dev)
  295. return 0;
  296. if (!thread)
  297. return 0;
  298. err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
  299. if (err)
  300. printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
  301. else
  302. printk(KERN_INFO "devtmpfs: mounted\n");
  303. return err;
  304. }
  305. static DECLARE_COMPLETION(setup_done);
  306. static int handle(const char *name, umode_t mode, struct device *dev)
  307. {
  308. if (mode)
  309. return handle_create(name, mode, dev);
  310. else
  311. return handle_remove(name, dev);
  312. }
  313. static int devtmpfsd(void *p)
  314. {
  315. char options[] = "mode=0755";
  316. int *err = p;
  317. *err = sys_unshare(CLONE_NEWNS);
  318. if (*err)
  319. goto out;
  320. *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
  321. if (*err)
  322. goto out;
  323. sys_chdir("/.."); /* will traverse into overmounted root */
  324. sys_chroot(".");
  325. complete(&setup_done);
  326. while (1) {
  327. spin_lock(&req_lock);
  328. while (requests) {
  329. struct req *req = requests;
  330. requests = NULL;
  331. spin_unlock(&req_lock);
  332. while (req) {
  333. struct req *next = req->next;
  334. req->err = handle(req->name, req->mode, 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. return 0;
  345. out:
  346. complete(&setup_done);
  347. return *err;
  348. }
  349. /*
  350. * Create devtmpfs instance, driver-core devices will add their device
  351. * nodes here.
  352. */
  353. int __init devtmpfs_init(void)
  354. {
  355. int err = register_filesystem(&dev_fs_type);
  356. if (err) {
  357. printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
  358. "type %i\n", err);
  359. return err;
  360. }
  361. thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
  362. if (!IS_ERR(thread)) {
  363. wait_for_completion(&setup_done);
  364. } else {
  365. err = PTR_ERR(thread);
  366. thread = NULL;
  367. }
  368. if (err) {
  369. printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
  370. unregister_filesystem(&dev_fs_type);
  371. return err;
  372. }
  373. printk(KERN_INFO "devtmpfs: initialized\n");
  374. return 0;
  375. }