inode.c 16 KB

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