inode.c 13 KB

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