inode.c 17 KB

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