]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - fs/devpts/inode.c
Per-mount allocated_ptys
[linux-2.6.git] / fs / devpts / inode.c
index 638db9b769ac628316a8f147a8d55120d89125f0..49d879d911b1f359daf418e436b7815738c20a60 100644 (file)
 #define DEVPTS_SUPER_MAGIC 0x1cd1
 
 #define DEVPTS_DEFAULT_MODE 0600
+#define PTMX_MINOR     2
 
 extern int pty_limit;                  /* Config limit on Unix98 ptys */
-static DEFINE_IDA(allocated_ptys);
 static DEFINE_MUTEX(allocated_ptys_lock);
 
 static struct vfsmount *devpts_mnt;
-static struct dentry *devpts_root;
 
 static struct {
        int setuid;
@@ -48,13 +47,30 @@ enum {
        Opt_err
 };
 
-static match_table_t tokens = {
+static const match_table_t tokens = {
        {Opt_uid, "uid=%u"},
        {Opt_gid, "gid=%u"},
        {Opt_mode, "mode=%o"},
        {Opt_err, NULL}
 };
 
+struct pts_fs_info {
+       struct ida allocated_ptys;
+};
+
+static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb)
+{
+       return sb->s_fs_info;
+}
+
+static inline struct super_block *pts_sb_from_inode(struct inode *inode)
+{
+       if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
+               return inode->i_sb;
+
+       return devpts_mnt->mnt_sb;
+}
+
 static int devpts_remount(struct super_block *sb, int *flags, char *data)
 {
        char *p;
@@ -118,6 +134,19 @@ static const struct super_operations devpts_sops = {
        .show_options   = devpts_show_options,
 };
 
+static void *new_pts_fs_info(void)
+{
+       struct pts_fs_info *fsi;
+
+       fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL);
+       if (!fsi)
+               return NULL;
+
+       ida_init(&fsi->allocated_ptys);
+
+       return fsi;
+}
+
 static int
 devpts_fill_super(struct super_block *s, void *data, int silent)
 {
@@ -129,9 +158,13 @@ devpts_fill_super(struct super_block *s, void *data, int silent)
        s->s_op = &devpts_sops;
        s->s_time_gran = 1;
 
+       s->s_fs_info = new_pts_fs_info();
+       if (!s->s_fs_info)
+               goto fail;
+
        inode = new_inode(s);
        if (!inode)
-               goto fail;
+               goto free_fsi;
        inode->i_ino = 1;
        inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
        inode->i_blocks = 0;
@@ -141,12 +174,15 @@ devpts_fill_super(struct super_block *s, void *data, int silent)
        inode->i_fop = &simple_dir_operations;
        inode->i_nlink = 2;
 
-       devpts_root = s->s_root = d_alloc_root(inode);
+       s->s_root = d_alloc_root(inode);
        if (s->s_root)
                return 0;
        
        printk("devpts: get root dentry failed\n");
        iput(inode);
+
+free_fsi:
+       kfree(s->s_fs_info);
 fail:
        return -ENOMEM;
 }
@@ -157,11 +193,19 @@ static int devpts_get_sb(struct file_system_type *fs_type,
        return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
 }
 
+static void devpts_kill_sb(struct super_block *sb)
+{
+       struct pts_fs_info *fsi = DEVPTS_SB(sb);
+
+       kfree(fsi);
+       kill_anon_super(sb);
+}
+
 static struct file_system_type devpts_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "devpts",
        .get_sb         = devpts_get_sb,
-       .kill_sb        = kill_anon_super,
+       .kill_sb        = devpts_kill_sb,
 };
 
 /*
@@ -169,26 +213,20 @@ static struct file_system_type devpts_fs_type = {
  * to the System V naming convention
  */
 
-static struct dentry *get_node(int num)
-{
-       char s[12];
-       struct dentry *root = devpts_root;
-       mutex_lock(&root->d_inode->i_mutex);
-       return lookup_one_len(s, root, sprintf(s, "%d", num));
-}
-
 int devpts_new_index(struct inode *ptmx_inode)
 {
+       struct super_block *sb = pts_sb_from_inode(ptmx_inode);
+       struct pts_fs_info *fsi = DEVPTS_SB(sb);
        int index;
        int ida_ret;
 
 retry:
-       if (!ida_pre_get(&allocated_ptys, GFP_KERNEL)) {
+       if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) {
                return -ENOMEM;
        }
 
        mutex_lock(&allocated_ptys_lock);
-       ida_ret = ida_get_new(&allocated_ptys, &index);
+       ida_ret = ida_get_new(&fsi->allocated_ptys, &index);
        if (ida_ret < 0) {
                mutex_unlock(&allocated_ptys_lock);
                if (ida_ret == -EAGAIN)
@@ -197,7 +235,7 @@ retry:
        }
 
        if (index >= pty_limit) {
-               ida_remove(&allocated_ptys, index);
+               ida_remove(&fsi->allocated_ptys, index);
                mutex_unlock(&allocated_ptys_lock);
                return -EIO;
        }
@@ -207,8 +245,11 @@ retry:
 
 void devpts_kill_index(struct inode *ptmx_inode, int idx)
 {
+       struct super_block *sb = pts_sb_from_inode(ptmx_inode);
+       struct pts_fs_info *fsi = DEVPTS_SB(sb);
+
        mutex_lock(&allocated_ptys_lock);
-       ida_remove(&allocated_ptys, idx);
+       ida_remove(&fsi->allocated_ptys, idx);
        mutex_unlock(&allocated_ptys_lock);
 }
 
@@ -218,7 +259,10 @@ int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
        struct tty_driver *driver = tty->driver;
        dev_t device = MKDEV(driver->major, driver->minor_start+number);
        struct dentry *dentry;
-       struct inode *inode = new_inode(devpts_mnt->mnt_sb);
+       struct super_block *sb = pts_sb_from_inode(ptmx_inode);
+       struct inode *inode = new_inode(sb);
+       struct dentry *root = sb->s_root;
+       char s[12];
 
        /* We're supposed to be given the slave end of a pty */
        BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
@@ -228,55 +272,56 @@ int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
                return -ENOMEM;
 
        inode->i_ino = number+2;
-       inode->i_uid = config.setuid ? config.uid : current->fsuid;
-       inode->i_gid = config.setgid ? config.gid : current->fsgid;
+       inode->i_uid = config.setuid ? config.uid : current_fsuid();
+       inode->i_gid = config.setgid ? config.gid : current_fsgid();
        inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
        init_special_inode(inode, S_IFCHR|config.mode, device);
        inode->i_private = tty;
+       tty->driver_data = inode;
+
+       sprintf(s, "%d", number);
+
+       mutex_lock(&root->d_inode->i_mutex);
 
-       dentry = get_node(number);
-       if (!IS_ERR(dentry) && !dentry->d_inode) {
-               d_instantiate(dentry, inode);
-               fsnotify_create(devpts_root->d_inode, dentry);
+       dentry = d_alloc_name(root, s);
+       if (!IS_ERR(dentry)) {
+               d_add(dentry, inode);
+               fsnotify_create(root->d_inode, dentry);
        }
 
-       mutex_unlock(&devpts_root->d_inode->i_mutex);
+       mutex_unlock(&root->d_inode->i_mutex);
 
        return 0;
 }
 
 struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
 {
-       struct dentry *dentry = get_node(number);
-       struct tty_struct *tty;
+       BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
 
-       tty = NULL;
-       if (!IS_ERR(dentry)) {
-               if (dentry->d_inode)
-                       tty = dentry->d_inode->i_private;
-               dput(dentry);
-       }
-
-       mutex_unlock(&devpts_root->d_inode->i_mutex);
-
-       return tty;
+       if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
+               return (struct tty_struct *)pts_inode->i_private;
+       return NULL;
 }
 
 void devpts_pty_kill(struct tty_struct *tty)
 {
-       int number = tty->index;
-       struct dentry *dentry = get_node(number);
+       struct inode *inode = tty->driver_data;
+       struct super_block *sb = pts_sb_from_inode(inode);
+       struct dentry *root = sb->s_root;
+       struct dentry *dentry;
 
-       if (!IS_ERR(dentry)) {
-               struct inode *inode = dentry->d_inode;
-               if (inode) {
-                       inode->i_nlink--;
-                       d_delete(dentry);
-                       dput(dentry);
-               }
+       BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
+
+       mutex_lock(&root->d_inode->i_mutex);
+
+       dentry = d_find_alias(inode);
+       if (dentry && !IS_ERR(dentry)) {
+               inode->i_nlink--;
+               d_delete(dentry);
                dput(dentry);
        }
-       mutex_unlock(&devpts_root->d_inode->i_mutex);
+
+       mutex_unlock(&root->d_inode->i_mutex);
 }
 
 static int __init init_devpts_fs(void)