Simplify devpts_pty_new()
[safe/jmp/linux-2.6] / fs / devpts / inode.c
index 285b64a..50e885f 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_IDR(allocated_ptys);
+static DEFINE_IDA(allocated_ptys);
 static DEFINE_MUTEX(allocated_ptys_lock);
 
 static struct vfsmount *devpts_mnt;
@@ -177,27 +178,27 @@ static struct dentry *get_node(int num)
        return lookup_one_len(s, root, sprintf(s, "%d", num));
 }
 
-int devpts_new_index(void)
+int devpts_new_index(struct inode *ptmx_inode)
 {
        int index;
-       int idr_ret;
+       int ida_ret;
 
 retry:
-       if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
+       if (!ida_pre_get(&allocated_ptys, GFP_KERNEL)) {
                return -ENOMEM;
        }
 
        mutex_lock(&allocated_ptys_lock);
-       idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
-       if (idr_ret < 0) {
+       ida_ret = ida_get_new(&allocated_ptys, &index);
+       if (ida_ret < 0) {
                mutex_unlock(&allocated_ptys_lock);
-               if (idr_ret == -EAGAIN)
+               if (ida_ret == -EAGAIN)
                        goto retry;
                return -EIO;
        }
 
        if (index >= pty_limit) {
-               idr_remove(&allocated_ptys, index);
+               ida_remove(&allocated_ptys, index);
                mutex_unlock(&allocated_ptys_lock);
                return -EIO;
        }
@@ -205,20 +206,21 @@ retry:
        return index;
 }
 
-void devpts_kill_index(int idx)
+void devpts_kill_index(struct inode *ptmx_inode, int idx)
 {
        mutex_lock(&allocated_ptys_lock);
-       idr_remove(&allocated_ptys, idx);
+       ida_remove(&allocated_ptys, idx);
        mutex_unlock(&allocated_ptys_lock);
 }
 
-int devpts_pty_new(struct tty_struct *tty)
+int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
 {
        int number = tty->index; /* tty layer puts index from devpts_new_index() in here */
        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);
+       char s[12];
 
        /* We're supposed to be given the slave end of a pty */
        BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
@@ -234,9 +236,13 @@ int devpts_pty_new(struct tty_struct *tty)
        init_special_inode(inode, S_IFCHR|config.mode, device);
        inode->i_private = tty;
 
-       dentry = get_node(number);
-       if (!IS_ERR(dentry) && !dentry->d_inode) {
-               d_instantiate(dentry, inode);
+       sprintf(s, "%d", number);
+
+       mutex_lock(&devpts_root->d_inode->i_mutex);
+
+       dentry = d_alloc_name(devpts_root, s);
+       if (!IS_ERR(dentry)) {
+               d_add(dentry, inode);
                fsnotify_create(devpts_root->d_inode, dentry);
        }
 
@@ -245,25 +251,18 @@ int devpts_pty_new(struct tty_struct *tty)
        return 0;
 }
 
-struct tty_struct *devpts_get_tty(int number)
+struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
 {
-       struct dentry *dentry = get_node(number);
-       struct tty_struct *tty;
-
-       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);
+       BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
 
-       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(int number)
+void devpts_pty_kill(struct tty_struct *tty)
 {
+       int number = tty->index;
        struct dentry *dentry = get_node(number);
 
        if (!IS_ERR(dentry)) {