inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. err = parse_mount_options(data, PARSE_REMOUNT, opts);
  266. /*
  267. * parse_mount_options() restores options to default values
  268. * before parsing and may have changed ptmxmode. So, update the
  269. * mode in the inode too. Bogus options don't fail the remount,
  270. * so do this even on error return.
  271. */
  272. update_ptmx_mode(fsi);
  273. return err;
  274. }
  275. static int devpts_show_options(struct seq_file *seq, struct dentry *root)
  276. {
  277. struct pts_fs_info *fsi = DEVPTS_SB(root->d_sb);
  278. struct pts_mount_opts *opts = &fsi->mount_opts;
  279. if (opts->setuid)
  280. seq_printf(seq, ",uid=%u", opts->uid);
  281. if (opts->setgid)
  282. seq_printf(seq, ",gid=%u", opts->gid);
  283. seq_printf(seq, ",mode=%03o", opts->mode);
  284. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  285. seq_printf(seq, ",ptmxmode=%03o", opts->ptmxmode);
  286. if (opts->max < NR_UNIX98_PTY_MAX)
  287. seq_printf(seq, ",max=%d", opts->max);
  288. #endif
  289. return 0;
  290. }
  291. static const struct super_operations devpts_sops = {
  292. .statfs = simple_statfs,
  293. .remount_fs = devpts_remount,
  294. .show_options = devpts_show_options,
  295. };
  296. static void *new_pts_fs_info(void)
  297. {
  298. struct pts_fs_info *fsi;
  299. fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
  300. if (!fsi)
  301. return NULL;
  302. ida_init(&fsi->allocated_ptys);
  303. fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
  304. fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
  305. return fsi;
  306. }
  307. static int
  308. devpts_fill_super(struct super_block *s, void *data, int silent)
  309. {
  310. struct inode *inode;
  311. s->s_blocksize = 1024;
  312. s->s_blocksize_bits = 10;
  313. s->s_magic = DEVPTS_SUPER_MAGIC;
  314. s->s_op = &devpts_sops;
  315. s->s_time_gran = 1;
  316. s->s_fs_info = new_pts_fs_info();
  317. if (!s->s_fs_info)
  318. goto fail;
  319. inode = new_inode(s);
  320. if (!inode)
  321. goto fail;
  322. inode->i_ino = 1;
  323. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  324. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  325. inode->i_op = &simple_dir_inode_operations;
  326. inode->i_fop = &simple_dir_operations;
  327. set_nlink(inode, 2);
  328. s->s_root = d_make_root(inode);
  329. if (s->s_root)
  330. return 0;
  331. printk(KERN_ERR "devpts: get root dentry failed\n");
  332. fail:
  333. return -ENOMEM;
  334. }
  335. #ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
  336. static int compare_init_pts_sb(struct super_block *s, void *p)
  337. {
  338. if (devpts_mnt)
  339. return devpts_mnt->mnt_sb == s;
  340. return 0;
  341. }
  342. /*
  343. * devpts_mount()
  344. *
  345. * If the '-o newinstance' mount option was specified, mount a new
  346. * (private) instance of devpts. PTYs created in this instance are
  347. * independent of the PTYs in other devpts instances.
  348. *
  349. * If the '-o newinstance' option was not specified, mount/remount the
  350. * initial kernel mount of devpts. This type of mount gives the
  351. * legacy, single-instance semantics.
  352. *
  353. * The 'newinstance' option is needed to support multiple namespace
  354. * semantics in devpts while preserving backward compatibility of the
  355. * current 'single-namespace' semantics. i.e all mounts of devpts
  356. * without the 'newinstance' mount option should bind to the initial
  357. * kernel mount, like mount_single().
  358. *
  359. * Mounts with 'newinstance' option create a new, private namespace.
  360. *
  361. * NOTE:
  362. *
  363. * For single-mount semantics, devpts cannot use mount_single(),
  364. * because mount_single()/sget() find and use the super-block from
  365. * the most recent mount of devpts. But that recent mount may be a
  366. * 'newinstance' mount and mount_single() would pick the newinstance
  367. * super-block instead of the initial super-block.
  368. */
  369. static struct dentry *devpts_mount(struct file_system_type *fs_type,
  370. int flags, const char *dev_name, void *data)
  371. {
  372. int error;
  373. struct pts_mount_opts opts;
  374. struct super_block *s;
  375. error = parse_mount_options(data, PARSE_MOUNT, &opts);
  376. if (error)
  377. return ERR_PTR(error);
  378. if (opts.newinstance)
  379. s = sget(fs_type, NULL, set_anon_super, NULL);
  380. else
  381. s = sget(fs_type, compare_init_pts_sb, set_anon_super, NULL);
  382. if (IS_ERR(s))
  383. return ERR_CAST(s);
  384. if (!s->s_root) {
  385. s->s_flags = flags;
  386. error = devpts_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  387. if (error)
  388. goto out_undo_sget;
  389. s->s_flags |= MS_ACTIVE;
  390. }
  391. memcpy(&(DEVPTS_SB(s))->mount_opts, &opts, sizeof(opts));
  392. error = mknod_ptmx(s);
  393. if (error)
  394. goto out_undo_sget;
  395. return dget(s->s_root);
  396. out_undo_sget:
  397. deactivate_locked_super(s);
  398. return ERR_PTR(error);
  399. }
  400. #else
  401. /*
  402. * This supports only the legacy single-instance semantics (no
  403. * multiple-instance semantics)
  404. */
  405. static struct dentry *devpts_mount(struct file_system_type *fs_type, int flags,
  406. const char *dev_name, void *data)
  407. {
  408. return mount_single(fs_type, flags, data, devpts_fill_super);
  409. }
  410. #endif
  411. static void devpts_kill_sb(struct super_block *sb)
  412. {
  413. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  414. kfree(fsi);
  415. kill_litter_super(sb);
  416. }
  417. static struct file_system_type devpts_fs_type = {
  418. .name = "devpts",
  419. .mount = devpts_mount,
  420. .kill_sb = devpts_kill_sb,
  421. };
  422. /*
  423. * The normal naming convention is simply /dev/pts/<number>; this conforms
  424. * to the System V naming convention
  425. */
  426. int devpts_new_index(struct inode *ptmx_inode)
  427. {
  428. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  429. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  430. int index;
  431. int ida_ret;
  432. retry:
  433. if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL))
  434. return -ENOMEM;
  435. mutex_lock(&allocated_ptys_lock);
  436. if (pty_count >= pty_limit -
  437. (fsi->mount_opts.newinstance ? pty_reserve : 0)) {
  438. mutex_unlock(&allocated_ptys_lock);
  439. return -ENOSPC;
  440. }
  441. ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
  442. if (ida_ret < 0) {
  443. mutex_unlock(&allocated_ptys_lock);
  444. if (ida_ret == -EAGAIN)
  445. goto retry;
  446. return -EIO;
  447. }
  448. if (index >= fsi->mount_opts.max) {
  449. ida_remove(&fsi->allocated_ptys, index);
  450. mutex_unlock(&allocated_ptys_lock);
  451. return -ENOSPC;
  452. }
  453. pty_count++;
  454. mutex_unlock(&allocated_ptys_lock);
  455. return index;
  456. }
  457. void devpts_kill_index(struct inode *ptmx_inode, int idx)
  458. {
  459. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  460. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  461. mutex_lock(&allocated_ptys_lock);
  462. ida_remove(&fsi->allocated_ptys, idx);
  463. pty_count--;
  464. mutex_unlock(&allocated_ptys_lock);
  465. }
  466. int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
  467. {
  468. /* tty layer puts index from devpts_new_index() in here */
  469. int number = tty->index;
  470. struct tty_driver *driver = tty->driver;
  471. dev_t device = MKDEV(driver->major, driver->minor_start+number);
  472. struct dentry *dentry;
  473. struct super_block *sb = pts_sb_from_inode(ptmx_inode);
  474. struct inode *inode = new_inode(sb);
  475. struct dentry *root = sb->s_root;
  476. struct pts_fs_info *fsi = DEVPTS_SB(sb);
  477. struct pts_mount_opts *opts = &fsi->mount_opts;
  478. int ret = 0;
  479. char s[12];
  480. /* We're supposed to be given the slave end of a pty */
  481. BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
  482. BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
  483. if (!inode)
  484. return -ENOMEM;
  485. inode->i_ino = number + 3;
  486. inode->i_uid = opts->setuid ? opts->uid : current_fsuid();
  487. inode->i_gid = opts->setgid ? opts->gid : current_fsgid();
  488. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  489. init_special_inode(inode, S_IFCHR|opts->mode, device);
  490. inode->i_private = tty;
  491. tty->driver_data = inode;
  492. sprintf(s, "%d", number);
  493. mutex_lock(&root->d_inode->i_mutex);
  494. dentry = d_alloc_name(root, s);
  495. if (dentry) {
  496. d_add(dentry, inode);
  497. fsnotify_create(root->d_inode, dentry);
  498. } else {
  499. iput(inode);
  500. ret = -ENOMEM;
  501. }
  502. mutex_unlock(&root->d_inode->i_mutex);
  503. return ret;
  504. }
  505. struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
  506. {
  507. struct dentry *dentry;
  508. struct tty_struct *tty;
  509. BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  510. /* Ensure dentry has not been deleted by devpts_pty_kill() */
  511. dentry = d_find_alias(pts_inode);
  512. if (!dentry)
  513. return NULL;
  514. tty = NULL;
  515. if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  516. tty = (struct tty_struct *)pts_inode->i_private;
  517. dput(dentry);
  518. return tty;
  519. }
  520. void devpts_pty_kill(struct tty_struct *tty)
  521. {
  522. struct inode *inode = tty->driver_data;
  523. struct super_block *sb = pts_sb_from_inode(inode);
  524. struct dentry *root = sb->s_root;
  525. struct dentry *dentry;
  526. BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  527. mutex_lock(&root->d_inode->i_mutex);
  528. dentry = d_find_alias(inode);
  529. drop_nlink(inode);
  530. d_delete(dentry);
  531. dput(dentry); /* d_alloc_name() in devpts_pty_new() */
  532. dput(dentry); /* d_find_alias above */
  533. mutex_unlock(&root->d_inode->i_mutex);
  534. }
  535. static int __init init_devpts_fs(void)
  536. {
  537. int err = register_filesystem(&devpts_fs_type);
  538. struct ctl_table_header *table;
  539. if (!err) {
  540. table = register_sysctl_table(pty_root_table);
  541. devpts_mnt = kern_mount(&devpts_fs_type);
  542. if (IS_ERR(devpts_mnt)) {
  543. err = PTR_ERR(devpts_mnt);
  544. unregister_filesystem(&devpts_fs_type);
  545. unregister_sysctl_table(table);
  546. }
  547. }
  548. return err;
  549. }
  550. module_init(init_devpts_fs)