inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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/slab.h>
  18. #include <linux/mount.h>
  19. #include <linux/tty.h>
  20. #include <linux/mutex.h>
  21. #include <linux/magic.h>
  22. #include <linux/idr.h>
  23. #include <linux/devpts_fs.h>
  24. #include <linux/parser.h>
  25. #include <linux/fsnotify.h>
  26. #include <linux/seq_file.h>
  27. #define DEVPTS_DEFAULT_MODE 0600
  28. /*
  29. * ptmx is a new node in /dev/pts and will be unused in legacy (single-
  30. * instance) mode. To prevent surprises in user space, set permissions of
  31. * ptmx to 0. Use 'chmod' or remount with '-o ptmxmode' to set meaningful
  32. * permissions.
  33. */
  34. #define DEVPTS_DEFAULT_PTMX_MODE 0000
  35. #define PTMX_MINOR 2
  36. /*
  37. * sysctl support for setting limits on the number of Unix98 ptys allocated.
  38. * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
  39. */
  40. static int pty_limit = NR_UNIX98_PTY_DEFAULT;
  41. static int pty_reserve = NR_UNIX98_PTY_RESERVE;
  42. static int pty_limit_min;
  43. static int pty_limit_max = INT_MAX;
  44. static int pty_count;
  45. static struct ctl_table pty_table[] = {
  46. {
  47. .procname = "max",
  48. .maxlen = sizeof(int),
  49. .mode = 0644,
  50. .data = &pty_limit,
  51. .proc_handler = proc_dointvec_minmax,
  52. .extra1 = &pty_limit_min,
  53. .extra2 = &pty_limit_max,
  54. }, {
  55. .procname = "reserve",
  56. .maxlen = sizeof(int),
  57. .mode = 0644,
  58. .data = &pty_reserve,
  59. .proc_handler = proc_dointvec_minmax,
  60. .extra1 = &pty_limit_min,
  61. .extra2 = &pty_limit_max,
  62. }, {
  63. .procname = "nr",
  64. .maxlen = sizeof(int),
  65. .mode = 0444,
  66. .data = &pty_count,
  67. .proc_handler = proc_dointvec,
  68. },
  69. {}
  70. };
  71. static struct ctl_table pty_kern_table[] = {
  72. {
  73. .procname = "pty",
  74. .mode = 0555,
  75. .child = pty_table,
  76. },
  77. {}
  78. };
  79. static struct ctl_table pty_root_table[] = {
  80. {
  81. .procname = "kernel",
  82. .mode = 0555,
  83. .child = pty_kern_table,
  84. },
  85. {}
  86. };
  87. static DEFINE_MUTEX(allocated_ptys_lock);
  88. static struct vfsmount *devpts_mnt;
  89. struct pts_mount_opts {
  90. int setuid;
  91. int setgid;
  92. uid_t uid;
  93. gid_t gid;
  94. umode_t mode;
  95. umode_t ptmxmode;
  96. int newinstance;
  97. int max;
  98. };
  99. enum {
  100. Opt_uid, Opt_gid, Opt_mode, Opt_ptmxmode, Opt_newinstance, Opt_max,
  101. Opt_err
  102. };
  103. static const match_table_t tokens = {
  104. {Opt_uid, "uid=%u"},
  105. {Opt_gid, "gid=%u"},
  106. {Opt_mode, "mode=%o"},
  107. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  108. {Opt_ptmxmode, "ptmxmode=%o"},
  109. {Opt_newinstance, "newinstance"},
  110. {Opt_max, "max=%d"},
  111. #endif
  112. {Opt_err, NULL}
  113. };
  114. struct pts_fs_info {
  115. struct ida allocated_ptys;
  116. struct pts_mount_opts mount_opts;
  117. struct dentry *ptmx_dentry;
  118. };
  119. static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
  120. {
  121. return sb->s_fs_info;
  122. }
  123. static inline struct super_block *pts_sb_from_inode(struct inode *inode)
  124. {
  125. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  126. if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  127. return inode->i_sb;
  128. #endif
  129. return devpts_mnt->mnt_sb;
  130. }
  131. #define PARSE_MOUNT 0
  132. #define PARSE_REMOUNT 1
  133. /*
  134. * parse_mount_options():
  135. * Set @opts to mount options specified in @data. If an option is not
  136. * specified in @data, set it to its default value. The exception is
  137. * 'newinstance' option which can only be set/cleared on a mount (i.e.
  138. * cannot be changed during remount).
  139. *
  140. * Note: @data may be NULL (in which case all options are set to default).
  141. */
  142. static int parse_mount_options(char *data, int op, struct pts_mount_opts *opts)
  143. {
  144. char *p;
  145. opts->setuid = 0;
  146. opts->setgid = 0;
  147. opts->uid = 0;
  148. opts->gid = 0;
  149. opts->mode = DEVPTS_DEFAULT_MODE;
  150. opts->ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  151. opts->max = NR_UNIX98_PTY_MAX;
  152. /* newinstance makes sense only on initial mount */
  153. if (op == PARSE_MOUNT)
  154. opts->newinstance = 0;
  155. while ((p = strsep(&data, ",")) != NULL) {
  156. substring_t args[MAX_OPT_ARGS];
  157. int token;
  158. int option;
  159. if (!*p)
  160. continue;
  161. token = match_token(p, tokens, args);
  162. switch (token) {
  163. case Opt_uid:
  164. if (match_int(&args[0], &option))
  165. return -EINVAL;
  166. opts->uid = option;
  167. opts->setuid = 1;
  168. break;
  169. case Opt_gid:
  170. if (match_int(&args[0], &option))
  171. return -EINVAL;
  172. opts->gid = option;
  173. opts->setgid = 1;
  174. break;
  175. case Opt_mode:
  176. if (match_octal(&args[0], &option))
  177. return -EINVAL;
  178. opts->mode = option & S_IALLUGO;
  179. break;
  180. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  181. case Opt_ptmxmode:
  182. if (match_octal(&args[0], &option))
  183. return -EINVAL;
  184. opts->ptmxmode = option & S_IALLUGO;
  185. break;
  186. case Opt_newinstance:
  187. /* newinstance makes sense only on initial mount */
  188. if (op == PARSE_MOUNT)
  189. opts->newinstance = 1;
  190. break;
  191. case Opt_max:
  192. if (match_int(&args[0], &option) ||
  193. option < 0 || option > NR_UNIX98_PTY_MAX)
  194. return -EINVAL;
  195. opts->max = option;
  196. break;
  197. #endif
  198. default:
  199. printk(KERN_ERR "devpts: called with bogus options\n");
  200. return -EINVAL;
  201. }
  202. }
  203. return 0;
  204. }
  205. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  206. static int mknod_ptmx(struct super_block *sb)
  207. {
  208. int mode;
  209. int rc = -ENOMEM;
  210. struct dentry *dentry;
  211. struct inode *inode;
  212. struct dentry *root = sb->s_root;
  213. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  214. struct pts_mount_opts *opts = &fsi->mount_opts;
  215. mutex_lock(&root->d_inode->i_mutex);
  216. /* If we have already created ptmx node, return */
  217. if (fsi->ptmx_dentry) {
  218. rc = 0;
  219. goto out;
  220. }
  221. dentry = d_alloc_name(root, "ptmx");
  222. if (!dentry) {
  223. printk(KERN_NOTICE "Unable to alloc dentry for ptmx node\n");
  224. goto out;
  225. }
  226. /*
  227. * Create a new 'ptmx' node in this mount of devpts.
  228. */
  229. inode = new_inode(sb);
  230. if (!inode) {
  231. printk(KERN_ERR "Unable to alloc inode for ptmx node\n");
  232. dput(dentry);
  233. goto out;
  234. }
  235. inode->i_ino = 2;
  236. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  237. mode = S_IFCHR|opts->ptmxmode;
  238. init_special_inode(inode, mode, MKDEV(TTYAUX_MAJOR, 2));
  239. d_add(dentry, inode);
  240. fsi->ptmx_dentry = dentry;
  241. rc = 0;
  242. out:
  243. mutex_unlock(&root->d_inode->i_mutex);
  244. return rc;
  245. }
  246. static void update_ptmx_mode(struct pts_fs_info *fsi)
  247. {
  248. struct inode *inode;
  249. if (fsi->ptmx_dentry) {
  250. inode = fsi->ptmx_dentry->d_inode;
  251. inode->i_mode = S_IFCHR|fsi->mount_opts.ptmxmode;
  252. }
  253. }
  254. #else
  255. static inline void update_ptmx_mode(struct pts_fs_info *fsi)
  256. {
  257. return;
  258. }
  259. #endif
  260. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  261. {
  262. int err;
  263. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  264. struct pts_mount_opts *opts = &fsi->mount_opts;
  265. sync_filesystem(sb);
  266. err = parse_mount_options(data, PARSE_REMOUNT, opts);
  267. /*
  268. * parse_mount_options() restores options to default values
  269. * before parsing and may have changed ptmxmode. So, update the
  270. * mode in the inode too. Bogus options don't fail the remount,
  271. * so do this even on error return.
  272. */
  273. update_ptmx_mode(fsi);
  274. return err;
  275. }
  276. static int devpts_show_options(struct seq_file *seq, struct dentry *root)
  277. {
  278. struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
  279. struct pts_mount_opts *opts = &fsi->mount_opts;
  280. if (opts->setuid)
  281. seq_printf(seq, ",uid=%u", opts->uid);
  282. if (opts->setgid)
  283. seq_printf(seq, ",gid=%u", opts->gid);
  284. seq_printf(seq, ",mode=%03o", opts->mode);
  285. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  286. seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
  287. if (opts->max < NR_UNIX98_PTY_MAX)
  288. seq_printf(seq, ",max=%d", opts->max);
  289. #endif
  290. return 0;
  291. }
  292. static const struct super_operations devpts_sops = {
  293. .statfs = simple_statfs,
  294. .remount_fs = devpts_remount,
  295. .show_options = devpts_show_options,
  296. };
  297. static void *new_pts_fs_info(void)
  298. {
  299. struct pts_fs_info *fsi;
  300. fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
  301. if (!fsi)
  302. return NULL;
  303. ida_init(&fsi->allocated_ptys);
  304. fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
  305. fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  306. return fsi;
  307. }
  308. static int
  309. devpts_fill_super(struct super_block *s, void *data, int silent)
  310. {
  311. struct inode *inode;
  312. s->s_blocksize = 1024;
  313. s->s_blocksize_bits = 10;
  314. s->s_magic = DEVPTS_SUPER_MAGIC;
  315. s->s_op = &devpts_sops;
  316. s->s_time_gran = 1;
  317. s->s_fs_info = new_pts_fs_info();
  318. if (!s->s_fs_info)
  319. goto fail;
  320. inode = new_inode(s);
  321. if (!inode)
  322. goto fail;
  323. inode->i_ino = 1;
  324. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  325. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  326. inode->i_op = &simple_dir_inode_operations;
  327. inode->i_fop = &simple_dir_operations;
  328. set_nlink(inode, 2);
  329. s->s_root = d_make_root(inode);
  330. if (s->s_root)
  331. return 0;
  332. printk(KERN_ERR "devpts: get root dentry failed\n");
  333. fail:
  334. return -ENOMEM;
  335. }
  336. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  337. static int compare_init_pts_sb(struct super_block *s, void *p)
  338. {
  339. if (devpts_mnt)
  340. return devpts_mnt->mnt_sb == s;
  341. return 0;
  342. }
  343. /*
  344. * devpts_mount()
  345. *
  346. * If the '-o newinstance' mount option was specified, mount a new
  347. * (private) instance of devpts. PTYs created in this instance are
  348. * independent of the PTYs in other devpts instances.
  349. *
  350. * If the '-o newinstance' option was not specified, mount/remount the
  351. * initial kernel mount of devpts. This type of mount gives the
  352. * legacy, single-instance semantics.
  353. *
  354. * The 'newinstance' option is needed to support multiple namespace
  355. * semantics in devpts while preserving backward compatibility of the
  356. * current 'single-namespace' semantics. i.e all mounts of devpts
  357. * without the 'newinstance' mount option should bind to the initial
  358. * kernel mount, like mount_single().
  359. *
  360. * Mounts with 'newinstance' option create a new, private namespace.
  361. *
  362. * NOTE:
  363. *
  364. * For single-mount semantics, devpts cannot use mount_single(),
  365. * because mount_single()/sget() find and use the super-block from
  366. * the most recent mount of devpts. But that recent mount may be a
  367. * 'newinstance' mount and mount_single() would pick the newinstance
  368. * super-block instead of the initial super-block.
  369. */
  370. static struct dentry *devpts_mount(struct file_system_type *fs_type,
  371. int flags, const char *dev_name, void *data)
  372. {
  373. int error;
  374. struct pts_mount_opts opts;
  375. struct super_block *s;
  376. error = parse_mount_options(data, PARSE_MOUNT, &opts);
  377. if (error)
  378. return ERR_PTR(error);
  379. if (opts.newinstance)
  380. s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  381. else
  382. s = sget(fs_type, compare_init_pts_sb, set_anon_super, flags,
  383. NULL);
  384. if (IS_ERR(s))
  385. return ERR_CAST(s);
  386. if (!s->s_root) {
  387. error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  388. if (error)
  389. goto out_undo_sget;
  390. s->s_flags |= MS_ACTIVE;
  391. }
  392. memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts));
  393. error = mknod_ptmx(s);
  394. if (error)
  395. goto out_undo_sget;
  396. return dget(s->s_root);
  397. out_undo_sget:
  398. deactivate_locked_super(s);
  399. return ERR_PTR(error);
  400. }
  401. #else
  402. /*
  403. * This supports only the legacy single-instance semantics (no
  404. * multiple-instance semantics)
  405. */
  406. static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags,
  407. const char *dev_name, void *data)
  408. {
  409. return mount_single(fs_type, flags, data, devpts_fill_super);
  410. }
  411. #endif
  412. static void devpts_kill_sb(struct super_block *sb)
  413. {
  414. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  415. ida_destroy(&fsi->allocated_ptys);
  416. kfree(fsi);
  417. kill_litter_super(sb);
  418. }
  419. static struct file_system_type devpts_fs_type = {
  420. .name = "devpts",
  421. .mount = devpts_mount,
  422. .kill_sb = devpts_kill_sb,
  423. };
  424. MODULE_ALIAS_FS("devpts");
  425. /*
  426. * The normal naming convention is simply /dev/pts/<number>; this conforms
  427. * to the System V naming convention
  428. */
  429. int devpts_new_index(struct inode *ptmx_inode)
  430. {
  431. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  432. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  433. int index;
  434. int ida_ret;
  435. retry:
  436. if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL))
  437. return -ENOMEM;
  438. mutex_lock(&allocated_ptys_lock);
  439. if (pty_count >= pty_limit -
  440. (fsi->mount_opts.newinstance ? pty_reserve : 0)) {
  441. mutex_unlock(&allocated_ptys_lock);
  442. return -ENOSPC;
  443. }
  444. ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
  445. if (ida_ret < 0) {
  446. mutex_unlock(&allocated_ptys_lock);
  447. if (ida_ret == -EAGAIN)
  448. goto retry;
  449. return -EIO;
  450. }
  451. if (index >= fsi->mount_opts.max) {
  452. ida_remove(&fsi->allocated_ptys, index);
  453. mutex_unlock(&allocated_ptys_lock);
  454. return -ENOSPC;
  455. }
  456. pty_count++;
  457. mutex_unlock(&allocated_ptys_lock);
  458. return index;
  459. }
  460. void devpts_kill_index(struct inode *ptmx_inode, int idx)
  461. {
  462. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  463. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  464. mutex_lock(&allocated_ptys_lock);
  465. ida_remove(&fsi->allocated_ptys, idx);
  466. pty_count--;
  467. mutex_unlock(&allocated_ptys_lock);
  468. }
  469. int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
  470. {
  471. /* tty layer puts index from devpts_new_index() in here */
  472. int number = tty->index;
  473. struct tty_driver *driver = tty->driver;
  474. dev_t device = MKDEV(driver->major, driver->minor_start+number);
  475. struct dentry *dentry;
  476. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  477. struct inode *inode = new_inode(sb);
  478. struct dentry *root = sb->s_root;
  479. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  480. struct pts_mount_opts *opts = &fsi->mount_opts;
  481. int ret = 0;
  482. char s[12];
  483. /* We're supposed to be given the slave end of a pty */
  484. BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
  485. BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
  486. if (!inode)
  487. return -ENOMEM;
  488. inode->i_ino = number + 3;
  489. inode->i_uid = opts->setuid ? opts->uid : current_fsuid();
  490. inode->i_gid = opts->setgid ? opts->gid : current_fsgid();
  491. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  492. init_special_inode(inode, S_IFCHR|opts->mode, device);
  493. inode->i_private = tty;
  494. tty->driver_data = inode;
  495. sprintf(s, "%d", number);
  496. mutex_lock(&root->d_inode->i_mutex);
  497. dentry = d_alloc_name(root, s);
  498. if (dentry) {
  499. d_add(dentry, inode);
  500. fsnotify_create(root->d_inode, dentry);
  501. } else {
  502. iput(inode);
  503. ret = -ENOMEM;
  504. }
  505. mutex_unlock(&root->d_inode->i_mutex);
  506. return ret;
  507. }
  508. struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
  509. {
  510. struct dentry *dentry;
  511. struct tty_struct *tty;
  512. BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  513. /* Ensure dentry has not been deleted by devpts_pty_kill() */
  514. dentry = d_find_alias(pts_inode);
  515. if (!dentry)
  516. return NULL;
  517. tty = NULL;
  518. if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  519. tty = (struct tty_struct *)pts_inode->i_private;
  520. dput(dentry);
  521. return tty;
  522. }
  523. void devpts_pty_kill(struct tty_struct *tty)
  524. {
  525. struct inode *inode = tty->driver_data;
  526. struct super_block *sb = pts_sb_from_inode(inode);
  527. struct dentry *root = sb->s_root;
  528. struct dentry *dentry;
  529. BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  530. mutex_lock(&root->d_inode->i_mutex);
  531. dentry = d_find_alias(inode);
  532. drop_nlink(inode);
  533. d_delete(dentry);
  534. dput(dentry); /* d_alloc_name() in devpts_pty_new() */
  535. dput(dentry); /* d_find_alias above */
  536. mutex_unlock(&root->d_inode->i_mutex);
  537. }
  538. static int __init init_devpts_fs(void)
  539. {
  540. int err = register_filesystem(&devpts_fs_type);
  541. struct ctl_table_header *table;
  542. if (!err) {
  543. table = register_sysctl_table(pty_root_table);
  544. devpts_mnt = kern_mount(&devpts_fs_type);
  545. if (IS_ERR(devpts_mnt)) {
  546. err = PTR_ERR(devpts_mnt);
  547. unregister_filesystem(&devpts_fs_type);
  548. unregister_sysctl_table(table);
  549. }
  550. }
  551. return err;
  552. }
  553. module_init(init_devpts_fs)