inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* -*- linux-c -*- --------------------------------------------------------- *
  2. *
  3. * linux/fs/devpts/inode.c
  4. *
  5. * Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
  6. *
  7. * This file is part of the Linux kernel and is made available under
  8. * the terms of the GNU General Public License, version 2, or at your
  9. * option, any later version, incorporated herein by reference.
  10. *
  11. * ------------------------------------------------------------------------- */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/fs.h>
  15. #include <linux/sched.h>
  16. #include <linux/namei.h>
  17. #include <linux/mount.h>
  18. #include <linux/tty.h>
  19. #include <linux/mutex.h>
  20. #include <linux/idr.h>
  21. #include <linux/devpts_fs.h>
  22. #include <linux/parser.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/seq_file.h>
  25. #define DEVPTS_SUPER_MAGIC 0x1cd1
  26. #define DEVPTS_DEFAULT_MODE 0600
  27. /*
  28. * ptmx is a new node in /dev/pts and will be unused in legacy (single-
  29. * instance) mode. To prevent surprises in user space, set permissions of
  30. * ptmx to 0. Use 'chmod' or remount with '-o ptmxmode' to set meaningful
  31. * permissions.
  32. */
  33. #define DEVPTS_DEFAULT_PTMX_MODE 0000
  34. #define PTMX_MINOR 2
  35. extern int pty_limit; /* Config limit on Unix98 ptys */
  36. static DEFINE_MUTEX(allocated_ptys_lock);
  37. static struct vfsmount *devpts_mnt;
  38. struct pts_mount_opts {
  39. int setuid;
  40. int setgid;
  41. uid_t uid;
  42. gid_t gid;
  43. umode_t mode;
  44. umode_t ptmxmode;
  45. };
  46. enum {
  47. Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode,
  48. Opt_err
  49. };
  50. static const match_table_t tokens = {
  51. {Opt_uid, "uid=%u"},
  52. {Opt_gid, "gid=%u"},
  53. {Opt_mode, "mode=%o"},
  54. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  55. {Opt_ptmxmode, "ptmxmode=%o"},
  56. #endif
  57. {Opt_err, NULL}
  58. };
  59. struct pts_fs_info {
  60. struct ida allocated_ptys;
  61. struct pts_mount_opts mount_opts;
  62. struct dentry *ptmx_dentry;
  63. };
  64. static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
  65. {
  66. return sb->s_fs_info;
  67. }
  68. static inline struct super_block *pts_sb_from_inode(struct inode *inode)
  69. {
  70. if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  71. return inode->i_sb;
  72. return devpts_mnt->mnt_sb;
  73. }
  74. static int parse_mount_options(char *data, struct pts_mount_opts *opts)
  75. {
  76. char *p;
  77. opts->setuid = 0;
  78. opts->setgid = 0;
  79. opts->uid = 0;
  80. opts->gid = 0;
  81. opts->mode = DEVPTS_DEFAULT_MODE;
  82. opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  83. while ((p = strsep(&data, ",")) != NULL) {
  84. substring_t args[MAX_OPT_ARGS];
  85. int token;
  86. int option;
  87. if (!*p)
  88. continue;
  89. token = match_token(p, tokens, args);
  90. switch (token) {
  91. case Opt_uid:
  92. if (match_int(&args[0], &option))
  93. return -EINVAL;
  94. opts->uid = option;
  95. opts->setuid = 1;
  96. break;
  97. case Opt_gid:
  98. if (match_int(&args[0], &option))
  99. return -EINVAL;
  100. opts->gid = option;
  101. opts->setgid = 1;
  102. break;
  103. case Opt_mode:
  104. if (match_octal(&args[0], &option))
  105. return -EINVAL;
  106. opts->mode = option & S_IALLUGO;
  107. break;
  108. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  109. case Opt_ptmxmode:
  110. if (match_octal(&args[0], &option))
  111. return -EINVAL;
  112. opts->ptmxmode = option & S_IALLUGO;
  113. break;
  114. #endif
  115. default:
  116. printk(KERN_ERR "devpts: called with bogus options\n");
  117. return -EINVAL;
  118. }
  119. }
  120. return 0;
  121. }
  122. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  123. static int mknod_ptmx(struct super_block *sb)
  124. {
  125. int mode;
  126. int rc = -ENOMEM;
  127. struct dentry *dentry;
  128. struct inode *inode;
  129. struct dentry *root = sb->s_root;
  130. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  131. struct pts_mount_opts *opts = &fsi->mount_opts;
  132. mutex_lock(&root->d_inode->i_mutex);
  133. /* If we have already created ptmx node, return */
  134. if (fsi->ptmx_dentry) {
  135. rc = 0;
  136. goto out;
  137. }
  138. dentry = d_alloc_name(root, "ptmx");
  139. if (!dentry) {
  140. printk(KERN_NOTICE "Unable to alloc dentry for ptmx node\n");
  141. goto out;
  142. }
  143. /*
  144. * Create a new 'ptmx' node in this mount of devpts.
  145. */
  146. inode = new_inode(sb);
  147. if (!inode) {
  148. printk(KERN_ERR "Unable to alloc inode for ptmx node\n");
  149. dput(dentry);
  150. goto out;
  151. }
  152. inode->i_ino = 2;
  153. inode->i_uid = inode->i_gid = 0;
  154. inode->i_blocks = 0;
  155. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  156. mode = S_IFCHR|opts->ptmxmode;
  157. init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
  158. d_add(dentry, inode);
  159. fsi->ptmx_dentry = dentry;
  160. rc = 0;
  161. printk(KERN_DEBUG "Created ptmx node in devpts ino %lu\n",
  162. inode->i_ino);
  163. out:
  164. mutex_unlock(&root->d_inode->i_mutex);
  165. return rc;
  166. }
  167. static void update_ptmx_mode(struct pts_fs_info *fsi)
  168. {
  169. struct inode *inode;
  170. if (fsi->ptmx_dentry) {
  171. inode = fsi->ptmx_dentry->d_inode;
  172. inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
  173. }
  174. }
  175. #else
  176. static inline void update_ptmx_mode(struct pts_fs_info *fsi)
  177. {
  178. return;
  179. }
  180. #endif
  181. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  182. {
  183. int err;
  184. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  185. struct pts_mount_opts *opts = &fsi->mount_opts;
  186. err = parse_mount_options(data, opts);
  187. /*
  188. * parse_mount_options() restores options to default values
  189. * before parsing and may have changed ptmxmode. So, update the
  190. * mode in the inode too. Bogus options don't fail the remount,
  191. * so do this even on error return.
  192. */
  193. update_ptmx_mode(fsi);
  194. return err;
  195. }
  196. static int devpts_show_options(struct seq_file *seq, struct vfsmount *vfs)
  197. {
  198. struct pts_fs_info *fsi = DEVPTS_SB(vfs->mnt_sb);
  199. struct pts_mount_opts *opts = &fsi->mount_opts;
  200. if (opts->setuid)
  201. seq_printf(seq, ",uid=%u", opts->uid);
  202. if (opts->setgid)
  203. seq_printf(seq, ",gid=%u", opts->gid);
  204. seq_printf(seq, ",mode=%03o", opts->mode);
  205. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  206. seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
  207. #endif
  208. return 0;
  209. }
  210. static const struct super_operations devpts_sops = {
  211. .statfs = simple_statfs,
  212. .remount_fs = devpts_remount,
  213. .show_options = devpts_show_options,
  214. };
  215. static void *new_pts_fs_info(void)
  216. {
  217. struct pts_fs_info *fsi;
  218. fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
  219. if (!fsi)
  220. return NULL;
  221. ida_init(&fsi->allocated_ptys);
  222. fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
  223. fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  224. return fsi;
  225. }
  226. static int
  227. devpts_fill_super(struct super_block *s, void *data, int silent)
  228. {
  229. struct inode *inode;
  230. s->s_blocksize = 1024;
  231. s->s_blocksize_bits = 10;
  232. s->s_magic = DEVPTS_SUPER_MAGIC;
  233. s->s_op = &devpts_sops;
  234. s->s_time_gran = 1;
  235. s->s_fs_info = new_pts_fs_info();
  236. if (!s->s_fs_info)
  237. goto fail;
  238. inode = new_inode(s);
  239. if (!inode)
  240. goto free_fsi;
  241. inode->i_ino = 1;
  242. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  243. inode->i_blocks = 0;
  244. inode->i_uid = inode->i_gid = 0;
  245. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  246. inode->i_op = &simple_dir_inode_operations;
  247. inode->i_fop = &simple_dir_operations;
  248. inode->i_nlink = 2;
  249. s->s_root = d_alloc_root(inode);
  250. if (s->s_root)
  251. return 0;
  252. printk("devpts: get root dentry failed\n");
  253. iput(inode);
  254. free_fsi:
  255. kfree(s->s_fs_info);
  256. fail:
  257. return -ENOMEM;
  258. }
  259. static int compare_init_pts_sb(struct super_block *s, void *p)
  260. {
  261. if (devpts_mnt)
  262. return devpts_mnt->mnt_sb == s;
  263. return 0;
  264. }
  265. /*
  266. * get_init_pts_sb()
  267. *
  268. * This interface is needed to support multiple namespace semantics in
  269. * devpts while preserving backward compatibility of the current 'single-
  270. * namespace' semantics. i.e all mounts of devpts without the 'newinstance'
  271. * mount option should bind to the initial kernel mount, like
  272. * get_sb_single().
  273. *
  274. * Mounts with 'newinstance' option create a new private namespace.
  275. *
  276. * But for single-mount semantics, devpts cannot use get_sb_single(),
  277. * because get_sb_single()/sget() find and use the super-block from
  278. * the most recent mount of devpts. But that recent mount may be a
  279. * 'newinstance' mount and get_sb_single() would pick the newinstance
  280. * super-block instead of the initial super-block.
  281. *
  282. * This interface is identical to get_sb_single() except that it
  283. * consistently selects the 'single-namespace' superblock even in the
  284. * presence of the private namespace (i.e 'newinstance') super-blocks.
  285. */
  286. static int get_init_pts_sb(struct file_system_type *fs_type, int flags,
  287. void *data, struct vfsmount *mnt)
  288. {
  289. struct super_block *s;
  290. int error;
  291. s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL);
  292. if (IS_ERR(s))
  293. return PTR_ERR(s);
  294. if (!s->s_root) {
  295. s->s_flags = flags;
  296. error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  297. if (error) {
  298. up_write(&s->s_umount);
  299. deactivate_super(s);
  300. return error;
  301. }
  302. s->s_flags |= MS_ACTIVE;
  303. }
  304. do_remount_sb(s, flags, data, 0);
  305. return simple_set_mnt(mnt, s);
  306. }
  307. static int devpts_get_sb(struct file_system_type *fs_type,
  308. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  309. {
  310. return get_init_pts_sb(fs_type, flags, data, mnt);
  311. }
  312. static void devpts_kill_sb(struct super_block *sb)
  313. {
  314. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  315. kfree(fsi);
  316. kill_litter_super(sb);
  317. }
  318. static struct file_system_type devpts_fs_type = {
  319. .owner = THIS_MODULE,
  320. .name = "devpts",
  321. .get_sb = devpts_get_sb,
  322. .kill_sb = devpts_kill_sb,
  323. };
  324. /*
  325. * The normal naming convention is simply /dev/pts/<number>; this conforms
  326. * to the System V naming convention
  327. */
  328. int devpts_new_index(struct inode *ptmx_inode)
  329. {
  330. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  331. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  332. int index;
  333. int ida_ret;
  334. retry:
  335. if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) {
  336. return -ENOMEM;
  337. }
  338. mutex_lock(&allocated_ptys_lock);
  339. ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
  340. if (ida_ret < 0) {
  341. mutex_unlock(&allocated_ptys_lock);
  342. if (ida_ret == -EAGAIN)
  343. goto retry;
  344. return -EIO;
  345. }
  346. if (index >= pty_limit) {
  347. ida_remove(&fsi->allocated_ptys, index);
  348. mutex_unlock(&allocated_ptys_lock);
  349. return -EIO;
  350. }
  351. mutex_unlock(&allocated_ptys_lock);
  352. return index;
  353. }
  354. void devpts_kill_index(struct inode *ptmx_inode, int idx)
  355. {
  356. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  357. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  358. mutex_lock(&allocated_ptys_lock);
  359. ida_remove(&fsi->allocated_ptys, idx);
  360. mutex_unlock(&allocated_ptys_lock);
  361. }
  362. int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
  363. {
  364. int number = tty->index; /* tty layer puts index from devpts_new_index() in here */
  365. struct tty_driver *driver = tty->driver;
  366. dev_t device = MKDEV(driver->major, driver->minor_start+number);
  367. struct dentry *dentry;
  368. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  369. struct inode *inode = new_inode(sb);
  370. struct dentry *root = sb->s_root;
  371. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  372. struct pts_mount_opts *opts = &fsi->mount_opts;
  373. char s[12];
  374. /* We're supposed to be given the slave end of a pty */
  375. BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
  376. BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
  377. if (!inode)
  378. return -ENOMEM;
  379. inode->i_ino = number+2;
  380. inode->i_uid = config.setuid ? config.uid : current_fsuid();
  381. inode->i_gid = config.setgid ? config.gid : current_fsgid();
  382. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  383. init_special_inode(inode, S_IFCHR|opts->mode, device);
  384. inode->i_private = tty;
  385. tty->driver_data = inode;
  386. sprintf(s, "%d", number);
  387. mutex_lock(&root->d_inode->i_mutex);
  388. dentry = d_alloc_name(root, s);
  389. if (!IS_ERR(dentry)) {
  390. d_add(dentry, inode);
  391. fsnotify_create(root->d_inode, dentry);
  392. }
  393. mutex_unlock(&root->d_inode->i_mutex);
  394. return 0;
  395. }
  396. struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
  397. {
  398. BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  399. if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  400. return (struct tty_struct *)pts_inode->i_private;
  401. return NULL;
  402. }
  403. void devpts_pty_kill(struct tty_struct *tty)
  404. {
  405. struct inode *inode = tty->driver_data;
  406. struct super_block *sb = pts_sb_from_inode(inode);
  407. struct dentry *root = sb->s_root;
  408. struct dentry *dentry;
  409. BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  410. mutex_lock(&root->d_inode->i_mutex);
  411. dentry = d_find_alias(inode);
  412. if (dentry && !IS_ERR(dentry)) {
  413. inode->i_nlink--;
  414. d_delete(dentry);
  415. dput(dentry);
  416. }
  417. mutex_unlock(&root->d_inode->i_mutex);
  418. }
  419. static int __init init_devpts_fs(void)
  420. {
  421. int err = register_filesystem(&devpts_fs_type);
  422. if (!err) {
  423. devpts_mnt = kern_mount(&devpts_fs_type);
  424. if (IS_ERR(devpts_mnt))
  425. err = PTR_ERR(devpts_mnt);
  426. }
  427. return err;
  428. }
  429. static void __exit exit_devpts_fs(void)
  430. {
  431. unregister_filesystem(&devpts_fs_type);
  432. mntput(devpts_mnt);
  433. }
  434. module_init(init_devpts_fs)
  435. module_exit(exit_devpts_fs)
  436. MODULE_LICENSE("GPL");