--- linux-2.5.74-smp/fs/devfs/base.c.deadlock 2003-06-23 19:46:44.000000000 +0400 +++ linux-2.5.74-smp/fs/devfs/base.c 2003-07-07 22:38:35.000000000 +0400 @@ -2208,8 +2208,38 @@ struct devfs_lookup_struct { devfs_handle_t de; wait_queue_head_t wait_queue; + atomic_t count; }; +static struct devfs_lookup_struct *new_devfs_lookup_struct (void) +{ + struct devfs_lookup_struct *info = kmalloc (sizeof (*info), GFP_KERNEL); + + if (info == NULL) { + PRINTK ("(): cannot allocate memory\n"); + return NULL; + } + + init_waitqueue_head (&info->wait_queue); + atomic_set (&info->count, 1); + DPRINTK (DEBUG_I_LOOKUP, "(%p): allocated\n", info); + return info; +} + +static inline void get_devfs_lookup_struct (struct devfs_lookup_struct *info) +{ + atomic_inc (&info->count); +} + +static inline void put_devfs_lookup_struct (struct devfs_lookup_struct *info) +{ + if (!atomic_dec_and_test (&info->count)) + return; + + DPRINTK (DEBUG_I_LOOKUP, "(%p): freed\n", info); + kfree (info); +} + /* XXX: this doesn't handle the case where we got a negative dentry but a devfs entry has been registered in the meanwhile */ static int devfs_d_revalidate_wait (struct dentry *dentry, int flags) @@ -2252,11 +2282,13 @@ static int devfs_d_revalidate_wait (stru read_lock (&parent->u.dir.lock); if (dentry->d_fsdata) { + get_devfs_lookup_struct (lookup_info); set_current_state (TASK_UNINTERRUPTIBLE); add_wait_queue (&lookup_info->wait_queue, &wait); read_unlock (&parent->u.dir.lock); schedule (); remove_wait_queue (&lookup_info->wait_queue, &wait); + put_devfs_lookup_struct (lookup_info); } else read_unlock (&parent->u.dir.lock); return 1; @@ -2268,7 +2300,7 @@ static int devfs_d_revalidate_wait (stru static struct dentry *devfs_lookup (struct inode *dir, struct dentry *dentry) { struct devfs_entry tmp; /* Must stay in scope until devfsd idle again */ - struct devfs_lookup_struct lookup_info; + struct devfs_lookup_struct *lookup_info; struct fs_info *fs_info = dir->i_sb->s_fs_info; struct devfs_entry *parent, *de; struct inode *inode; @@ -2285,9 +2317,10 @@ static struct dentry *devfs_lookup (stru read_lock (&parent->u.dir.lock); de = _devfs_search_dir (parent, dentry->d_name.name, dentry->d_name.len); read_unlock (&parent->u.dir.lock); - lookup_info.de = de; - init_waitqueue_head (&lookup_info.wait_queue); - dentry->d_fsdata = &lookup_info; + lookup_info = new_devfs_lookup_struct (); + if (lookup_info == NULL) return ERR_PTR (-ENOMEM); + lookup_info->de = de; + dentry->d_fsdata = lookup_info; if (de == NULL) { /* Try with devfsd. For any kind of failure, leave a negative dentry so someone else can deal with it (in the case where the sysadmin @@ -2297,6 +2330,7 @@ static struct dentry *devfs_lookup (stru if (try_modload (parent, fs_info, dentry->d_name.name, dentry->d_name.len, &tmp) < 0) { /* Lookup event was not queued to devfsd */ + put_devfs_lookup_struct (lookup_info); d_add (dentry, NULL); return NULL; } @@ -2308,8 +2342,7 @@ static struct dentry *devfs_lookup (stru revalidation */ up (&dir->i_sem); wait_for_devfsd_finished (fs_info); /* If I'm not devfsd, must wait */ - down (&dir->i_sem); /* Grab it again because them's the rules */ - de = lookup_info.de; + de = lookup_info->de; /* If someone else has been so kind as to make the inode, we go home early */ if (dentry->d_inode) goto out; @@ -2333,11 +2366,13 @@ static struct dentry *devfs_lookup (stru de->name, de->inode.ino, inode, de, current->comm); d_instantiate (dentry, inode); out: + write_lock (&parent->u.dir.lock); dentry->d_op = &devfs_dops; dentry->d_fsdata = NULL; - write_lock (&parent->u.dir.lock); - wake_up (&lookup_info.wait_queue); + wake_up (&lookup_info->wait_queue); + put_devfs_lookup_struct (lookup_info); write_unlock (&parent->u.dir.lock); + down (&dir->i_sem); /* Grab it again because them's the rules */ devfs_put (de); return retval; } /* End Function devfs_lookup */