inode.c 15 KB

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