devtmpfs.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. 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 dentry *dev_mount(struct file_system_type *fs_type, int flags,
  50. const char *dev_name, void *data)
  51. {
  52. #ifdef CONFIG_TMPFS
  53. return mount_single(fs_type, flags, data, shmem_fill_super);
  54. #else
  55. return mount_single(fs_type, flags, data, ramfs_fill_super);
  56. #endif
  57. }
  58. static struct file_system_type dev_fs_type = {
  59. .name = "devtmpfs",
  60. .mount = dev_mount,
  61. .kill_sb = kill_litter_super,
  62. };
  63. #ifdef CONFIG_BLOCK
  64. static inline int is_blockdev(struct device *dev)
  65. {
  66. return dev->class == &block_class;
  67. }
  68. #else
  69. static inline int is_blockdev(struct device *dev) { return 0; }
  70. #endif
  71. int devtmpfs_create_node(struct device *dev)
  72. {
  73. const char *tmp = NULL;
  74. struct req req;
  75. if (!thread)
  76. return 0;
  77. req.mode = 0;
  78. req.uid = GLOBAL_ROOT_UID;
  79. req.gid = GLOBAL_ROOT_GID;
  80. req.name = device_get_devnode(dev, &req.mode, &req.uid, &req.gid, &tmp);
  81. if (!req.name)
  82. return -ENOMEM;
  83. if (req.mode == 0)
  84. req.mode = 0600;
  85. if (is_blockdev(dev))
  86. req.mode |= S_IFBLK;
  87. else
  88. req.mode |= S_IFCHR;
  89. req.dev = dev;
  90. init_completion(&req.done);
  91. spin_lock(&req_lock);
  92. req.next = requests;
  93. requests = &req;
  94. spin_unlock(&req_lock);
  95. wake_up_process(thread);
  96. wait_for_completion(&req.done);
  97. kfree(tmp);
  98. return req.err;
  99. }
  100. int devtmpfs_delete_node(struct device *dev)
  101. {
  102. const char *tmp = NULL;
  103. struct req req;
  104. if (!thread)
  105. return 0;
  106. req.name = device_get_devnode(dev, NULL, NULL, NULL, &tmp);
  107. if (!req.name)
  108. return -ENOMEM;
  109. req.mode = 0;
  110. req.dev = dev;
  111. init_completion(&req.done);
  112. spin_lock(&req_lock);
  113. req.next = requests;
  114. requests = &req;
  115. spin_unlock(&req_lock);
  116. wake_up_process(thread);
  117. wait_for_completion(&req.done);
  118. kfree(tmp);
  119. return req.err;
  120. }
  121. static int dev_mkdir(const char *name, umode_t mode)
  122. {
  123. struct dentry *dentry;
  124. struct path path;
  125. int err;
  126. dentry = kern_path_create(AT_FDCWD, name, &path, 1);
  127. if (IS_ERR(dentry))
  128. return PTR_ERR(dentry);
  129. err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  130. if (!err)
  131. /* mark as kernel-created inode */
  132. dentry->d_inode->i_private = &thread;
  133. dput(dentry);
  134. mutex_unlock(&path.dentry->d_inode->i_mutex);
  135. path_put(&path);
  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(path.dentry->d_inode, 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. mutex_lock(&dentry->d_inode->i_mutex);
  183. notify_change(dentry, &newattrs);
  184. mutex_unlock(&dentry->d_inode->i_mutex);
  185. /* mark as kernel-created inode */
  186. dentry->d_inode->i_private = &thread;
  187. }
  188. dput(dentry);
  189. mutex_unlock(&path.dentry->d_inode->i_mutex);
  190. path_put(&path);
  191. return err;
  192. }
  193. static int dev_rmdir(const char *name)
  194. {
  195. struct path parent;
  196. struct dentry *dentry;
  197. int err;
  198. dentry = kern_path_locked(name, &parent);
  199. if (IS_ERR(dentry))
  200. return PTR_ERR(dentry);
  201. if (dentry->d_inode) {
  202. if (dentry->d_inode->i_private == &thread)
  203. err = vfs_rmdir(parent.dentry->d_inode, dentry);
  204. else
  205. err = -EPERM;
  206. } else {
  207. err = -ENOENT;
  208. }
  209. dput(dentry);
  210. mutex_unlock(&parent.dentry->d_inode->i_mutex);
  211. path_put(&parent);
  212. return err;
  213. }
  214. static int delete_path(const char *nodepath)
  215. {
  216. const char *path;
  217. int err = 0;
  218. path = kstrdup(nodepath, GFP_KERNEL);
  219. if (!path)
  220. return -ENOMEM;
  221. for (;;) {
  222. char *base;
  223. base = strrchr(path, '/');
  224. if (!base)
  225. break;
  226. base[0] = '\0';
  227. err = dev_rmdir(path);
  228. if (err)
  229. break;
  230. }
  231. kfree(path);
  232. return err;
  233. }
  234. static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *stat)
  235. {
  236. /* did we create it */
  237. if (inode->i_private != &thread)
  238. return 0;
  239. /* does the dev_t match */
  240. if (is_blockdev(dev)) {
  241. if (!S_ISBLK(stat->mode))
  242. return 0;
  243. } else {
  244. if (!S_ISCHR(stat->mode))
  245. return 0;
  246. }
  247. if (stat->rdev != dev->devt)
  248. return 0;
  249. /* ours */
  250. return 1;
  251. }
  252. static int handle_remove(const char *nodename, struct device *dev)
  253. {
  254. struct path parent;
  255. struct dentry *dentry;
  256. int deleted = 1;
  257. int err;
  258. dentry = kern_path_locked(nodename, &parent);
  259. if (IS_ERR(dentry))
  260. return PTR_ERR(dentry);
  261. if (dentry->d_inode) {
  262. struct kstat stat;
  263. err = vfs_getattr(parent.mnt, dentry, &stat);
  264. if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
  265. struct iattr newattrs;
  266. /*
  267. * before unlinking this node, reset permissions
  268. * of possible references like hardlinks
  269. */
  270. newattrs.ia_uid = 0;
  271. newattrs.ia_gid = 0;
  272. newattrs.ia_mode = stat.mode & ~0777;
  273. newattrs.ia_valid =
  274. ATTR_UID|ATTR_GID|ATTR_MODE;
  275. mutex_lock(&dentry->d_inode->i_mutex);
  276. notify_change(dentry, &newattrs);
  277. mutex_unlock(&dentry->d_inode->i_mutex);
  278. err = vfs_unlink(parent.dentry->d_inode, dentry);
  279. if (!err || err == -ENOENT)
  280. deleted = 1;
  281. }
  282. } else {
  283. err = -ENOENT;
  284. }
  285. dput(dentry);
  286. mutex_unlock(&parent.dentry->d_inode->i_mutex);
  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 = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
  304. if (err)
  305. printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
  306. else
  307. printk(KERN_INFO "devtmpfs: mounted\n");
  308. return err;
  309. }
  310. static DECLARE_COMPLETION(setup_done);
  311. static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
  312. struct device *dev)
  313. {
  314. if (mode)
  315. return handle_create(name, mode, uid, gid, dev);
  316. else
  317. return handle_remove(name, dev);
  318. }
  319. static int devtmpfsd(void *p)
  320. {
  321. char options[] = "mode=0755";
  322. int *err = p;
  323. *err = sys_unshare(CLONE_NEWNS);
  324. if (*err)
  325. goto out;
  326. *err = sys_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, options);
  327. if (*err)
  328. goto out;
  329. sys_chdir("/.."); /* will traverse into overmounted root */
  330. sys_chroot(".");
  331. complete(&setup_done);
  332. while (1) {
  333. spin_lock(&req_lock);
  334. while (requests) {
  335. struct req *req = requests;
  336. requests = NULL;
  337. spin_unlock(&req_lock);
  338. while (req) {
  339. struct req *next = req->next;
  340. req->err = handle(req->name, req->mode,
  341. req->uid, req->gid, req->dev);
  342. complete(&req->done);
  343. req = next;
  344. }
  345. spin_lock(&req_lock);
  346. }
  347. __set_current_state(TASK_INTERRUPTIBLE);
  348. spin_unlock(&req_lock);
  349. schedule();
  350. }
  351. return 0;
  352. out:
  353. complete(&setup_done);
  354. return *err;
  355. }
  356. /*
  357. * Create devtmpfs instance, driver-core devices will add their device
  358. * nodes here.
  359. */
  360. int __init devtmpfs_init(void)
  361. {
  362. int err = register_filesystem(&dev_fs_type);
  363. if (err) {
  364. printk(KERN_ERR "devtmpfs: unable to register devtmpfs "
  365. "type %i\n", err);
  366. return err;
  367. }
  368. thread = kthread_run(devtmpfsd, &err, "kdevtmpfs");
  369. if (!IS_ERR(thread)) {
  370. wait_for_completion(&setup_done);
  371. } else {
  372. err = PTR_ERR(thread);
  373. thread = NULL;
  374. }
  375. if (err) {
  376. printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);
  377. unregister_filesystem(&dev_fs_type);
  378. return err;
  379. }
  380. printk(KERN_INFO "devtmpfs: initialized\n");
  381. return 0;
  382. }