inode.c 14 KB

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