Commit fe178e80 authored by Alexander Viro's avatar Alexander Viro Committed by Linus Torvalds

[PATCH] dev_t handling cleanups (9/12)

struct block_device made the private part of bdevfs inodes; bd_count
is gone, we use ->i_count of inode now; separate hash is also gone and we
are using iget5_locked()/igrab()/iput() instead.
parent 3dd5fcf6
...@@ -245,6 +245,7 @@ static int rd_make_request(request_queue_t * q, struct bio *bio) ...@@ -245,6 +245,7 @@ static int rd_make_request(request_queue_t * q, struct bio *bio)
static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{ {
int error; int error;
struct block_device *bdev = inode->i_bdev;
if (cmd != BLKFLSBUF) if (cmd != BLKFLSBUF)
return -EINVAL; return -EINVAL;
...@@ -253,12 +254,12 @@ static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un ...@@ -253,12 +254,12 @@ static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
it's not like with the other blockdevices where it's not like with the other blockdevices where
this ioctl only flushes away the buffer cache. */ this ioctl only flushes away the buffer cache. */
error = -EBUSY; error = -EBUSY;
down(&inode->i_bdev->bd_sem); down(&bdev->bd_sem);
if (inode->i_bdev->bd_openers <= 2) { if (bdev->bd_openers <= 2) {
truncate_inode_pages(inode->i_mapping, 0); truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
error = 0; error = 0;
} }
up(&inode->i_bdev->bd_sem); up(&bdev->bd_sem);
return error; return error;
} }
...@@ -276,11 +277,11 @@ static int rd_open(struct inode * inode, struct file * filp) ...@@ -276,11 +277,11 @@ static int rd_open(struct inode * inode, struct file * filp)
*/ */
if (rd_bdev[unit] == NULL) { if (rd_bdev[unit] == NULL) {
struct block_device *bdev = inode->i_bdev; struct block_device *bdev = inode->i_bdev;
atomic_inc(&bdev->bd_count); inode = igrab(bdev->bd_inode);
rd_bdev[unit] = bdev; rd_bdev[unit] = bdev;
bdev->bd_openers++; bdev->bd_openers++;
bdev->bd_block_size = rd_blocksize; bdev->bd_block_size = rd_blocksize;
bdev->bd_inode->i_size = get_capacity(rd_disks[unit])<<9; inode->i_size = get_capacity(rd_disks[unit])<<9;
inode->i_mapping->a_ops = &ramdisk_aops; inode->i_mapping->a_ops = &ramdisk_aops;
inode->i_mapping->backing_dev_info = &rd_backing_dev_info; inode->i_mapping->backing_dev_info = &rd_backing_dev_info;
} }
......
...@@ -63,7 +63,9 @@ static int raw_open(struct inode *inode, struct file *filp) ...@@ -63,7 +63,9 @@ static int raw_open(struct inode *inode, struct file *filp)
err = bd_claim(bdev, raw_open); err = bd_claim(bdev, raw_open);
if (err) if (err)
goto out; goto out;
atomic_inc(&bdev->bd_count); err = -ENODEV;
if (!igrab(bdev->bd_inode))
goto out;
err = blkdev_get(bdev, filp->f_mode, 0, BDEV_RAW); err = blkdev_get(bdev, filp->f_mode, 0, BDEV_RAW);
if (err) { if (err) {
bd_release(bdev); bd_release(bdev);
......
...@@ -197,40 +197,36 @@ static int block_fsync(struct file *filp, struct dentry *dentry, int datasync) ...@@ -197,40 +197,36 @@ static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
* pseudo-fs * pseudo-fs
*/ */
static struct super_block *bd_get_sb(struct file_system_type *fs_type, static spinlock_t bdev_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
int flags, const char *dev_name, void *data) static kmem_cache_t * bdev_cachep;
{
return get_sb_pseudo(fs_type, "bdev:", NULL, 0x62646576);
}
static struct file_system_type bd_type = { struct bdev_inode {
.name = "bdev", struct block_device bdev;
.get_sb = bd_get_sb, struct inode vfs_inode;
.kill_sb = kill_anon_super,
}; };
static struct vfsmount *bd_mnt; static inline struct bdev_inode *BDEV_I(struct inode *inode)
struct super_block *blockdev_superblock; {
return container_of(inode, struct bdev_inode, vfs_inode);
/* }
* bdev cache handling - shamelessly stolen from inode.c
* We use smaller hashtable, though.
*/
#define HASH_BITS 6 static struct inode *bdev_alloc_inode(struct super_block *sb)
#define HASH_SIZE (1UL << HASH_BITS) {
#define HASH_MASK (HASH_SIZE-1) struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
static struct list_head bdev_hashtable[HASH_SIZE]; if (!ei)
static spinlock_t bdev_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED; return NULL;
static kmem_cache_t * bdev_cachep; return &ei->vfs_inode;
}
#define alloc_bdev() \ static void bdev_destroy_inode(struct inode *inode)
((struct block_device *) kmem_cache_alloc(bdev_cachep, SLAB_KERNEL)) {
#define destroy_bdev(bdev) kmem_cache_free(bdev_cachep, (bdev)) kmem_cache_free(bdev_cachep, BDEV_I(inode));
}
static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
{ {
struct block_device * bdev = (struct block_device *) foo; struct bdev_inode *ei = (struct bdev_inode *) foo;
struct block_device *bdev = &ei->bdev;
if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
SLAB_CTOR_CONSTRUCTOR) SLAB_CTOR_CONSTRUCTOR)
...@@ -238,24 +234,61 @@ static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) ...@@ -238,24 +234,61 @@ static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
memset(bdev, 0, sizeof(*bdev)); memset(bdev, 0, sizeof(*bdev));
sema_init(&bdev->bd_sem, 1); sema_init(&bdev->bd_sem, 1);
INIT_LIST_HEAD(&bdev->bd_inodes); INIT_LIST_HEAD(&bdev->bd_inodes);
INIT_LIST_HEAD(&bdev->bd_list);
inode_init_once(&ei->vfs_inode);
} }
} }
void __init bdev_cache_init(void) static inline void __bd_forget(struct inode *inode)
{ {
int i, err; list_del_init(&inode->i_devices);
struct list_head *head = bdev_hashtable; inode->i_bdev = NULL;
inode->i_mapping = &inode->i_data;
}
static void bdev_clear_inode(struct inode *inode)
{
struct block_device *bdev = &BDEV_I(inode)->bdev;
struct list_head *p;
spin_lock(&bdev_lock);
while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
__bd_forget(list_entry(p, struct inode, i_devices));
}
list_del_init(&bdev->bd_list);
spin_unlock(&bdev_lock);
}
static struct super_operations bdev_sops = {
.statfs = simple_statfs,
.alloc_inode = bdev_alloc_inode,
.destroy_inode = bdev_destroy_inode,
.drop_inode = generic_delete_inode,
.clear_inode = bdev_clear_inode,
};
static struct super_block *bd_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576);
}
static struct file_system_type bd_type = {
.name = "bdev",
.get_sb = bd_get_sb,
.kill_sb = kill_anon_super,
};
i = HASH_SIZE; static struct vfsmount *bd_mnt;
do { struct super_block *blockdev_superblock;
INIT_LIST_HEAD(head);
head++;
i--;
} while (i);
void __init bdev_cache_init(void)
{
int err;
bdev_cachep = kmem_cache_create("bdev_cache", bdev_cachep = kmem_cache_create("bdev_cache",
sizeof(struct block_device), sizeof(struct bdev_inode),
0, SLAB_HWCACHE_ALIGN, init_once, 0,
SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
init_once,
NULL); NULL);
if (!bdev_cachep) if (!bdev_cachep)
panic("Cannot create bdev_cache SLAB cache"); panic("Cannot create bdev_cache SLAB cache");
...@@ -272,123 +305,83 @@ void __init bdev_cache_init(void) ...@@ -272,123 +305,83 @@ void __init bdev_cache_init(void)
/* /*
* Most likely _very_ bad one - but then it's hardly critical for small * Most likely _very_ bad one - but then it's hardly critical for small
* /dev and can be fixed when somebody will need really large one. * /dev and can be fixed when somebody will need really large one.
* Keep in mind that it will be fed through icache hash function too.
*/ */
static inline unsigned long hash(dev_t dev) static inline unsigned long hash(dev_t dev)
{ {
unsigned long tmp = dev; return MAJOR(dev)+MINOR(dev);
tmp = tmp + (tmp >> HASH_BITS) + (tmp >> HASH_BITS*2);
return tmp & HASH_MASK;
} }
static struct block_device *bdfind(dev_t dev, struct list_head *head) static int bdev_test(struct inode *inode, void *data)
{ {
struct list_head *p; return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
struct block_device *bdev; }
list_for_each(p, head) {
bdev = list_entry(p, struct block_device, bd_hash); static int bdev_set(struct inode *inode, void *data)
if (bdev->bd_dev != dev) {
continue; BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
atomic_inc(&bdev->bd_count); return 0;
return bdev;
}
return NULL;
} }
static LIST_HEAD(all_bdevs);
struct block_device *bdget(dev_t dev) struct block_device *bdget(dev_t dev)
{ {
struct list_head * head = bdev_hashtable + hash(dev); struct block_device *bdev;
struct block_device *bdev, *new_bdev; struct inode *inode;
spin_lock(&bdev_lock);
bdev = bdfind(dev, head); inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
spin_unlock(&bdev_lock); bdev_test, bdev_set, &dev);
if (bdev)
return bdev; if (!inode)
new_bdev = alloc_bdev(); return NULL;
if (new_bdev) {
struct inode *inode = new_inode(bd_mnt->mnt_sb); bdev = &BDEV_I(inode)->bdev;
if (inode) {
kdev_t kdev = to_kdev_t(dev); if (inode->i_state & I_NEW) {
bdev->bd_contains = NULL;
atomic_set(&new_bdev->bd_count,1); bdev->bd_inode = inode;
new_bdev->bd_dev = dev; bdev->bd_block_size = (1 << inode->i_blkbits);
new_bdev->bd_contains = NULL; bdev->bd_part_count = 0;
new_bdev->bd_inode = inode; bdev->bd_invalidated = 0;
new_bdev->bd_block_size = (1 << inode->i_blkbits);
new_bdev->bd_part_count = 0;
new_bdev->bd_invalidated = 0;
inode->i_mode = S_IFBLK; inode->i_mode = S_IFBLK;
inode->i_rdev = kdev; inode->i_rdev = to_kdev_t(dev);
inode->i_bdev = new_bdev; inode->i_bdev = bdev;
inode->i_data.a_ops = &def_blk_aops; inode->i_data.a_ops = &def_blk_aops;
mapping_set_gfp_mask(&inode->i_data, GFP_USER); mapping_set_gfp_mask(&inode->i_data, GFP_USER);
inode->i_data.backing_dev_info = &default_backing_dev_info; inode->i_data.backing_dev_info = &default_backing_dev_info;
spin_lock(&bdev_lock); spin_lock(&bdev_lock);
bdev = bdfind(dev, head); list_add(&bdev->bd_list, &all_bdevs);
if (!bdev) {
list_add(&new_bdev->bd_hash, head);
spin_unlock(&bdev_lock);
return new_bdev;
}
spin_unlock(&bdev_lock); spin_unlock(&bdev_lock);
iput(new_bdev->bd_inode); unlock_new_inode(inode);
}
destroy_bdev(new_bdev);
} }
return bdev; return bdev;
} }
long nr_blockdev_pages(void) long nr_blockdev_pages(void)
{ {
struct list_head *p;
long ret = 0; long ret = 0;
int i;
spin_lock(&bdev_lock); spin_lock(&bdev_lock);
for (i = 0; i < ARRAY_SIZE(bdev_hashtable); i++) { list_for_each(p, &all_bdevs) {
struct list_head *head = &bdev_hashtable[i];
struct list_head *lh;
if (head == NULL)
continue;
list_for_each(lh, head) {
struct block_device *bdev; struct block_device *bdev;
bdev = list_entry(p, struct block_device, bd_list);
bdev = list_entry(lh, struct block_device, bd_hash);
ret += bdev->bd_inode->i_mapping->nrpages; ret += bdev->bd_inode->i_mapping->nrpages;
} }
}
spin_unlock(&bdev_lock); spin_unlock(&bdev_lock);
return ret; return ret;
} }
static inline void __bd_forget(struct inode *inode)
{
list_del_init(&inode->i_devices);
inode->i_bdev = NULL;
inode->i_mapping = &inode->i_data;
}
void bdput(struct block_device *bdev) void bdput(struct block_device *bdev)
{ {
if (atomic_dec_and_lock(&bdev->bd_count, &bdev_lock)) {
struct list_head *p;
if (bdev->bd_openers)
BUG();
list_del(&bdev->bd_hash);
while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
__bd_forget(list_entry(p, struct inode, i_devices));
}
spin_unlock(&bdev_lock);
iput(bdev->bd_inode); iput(bdev->bd_inode);
destroy_bdev(bdev);
}
} }
int bd_acquire(struct inode *inode) int bd_acquire(struct inode *inode)
{ {
struct block_device *bdev; struct block_device *bdev;
spin_lock(&bdev_lock); spin_lock(&bdev_lock);
if (inode->i_bdev) { if (inode->i_bdev && igrab(inode->i_bdev->bd_inode)) {
atomic_inc(&inode->i_bdev->bd_count);
spin_unlock(&bdev_lock); spin_unlock(&bdev_lock);
return 0; return 0;
} }
...@@ -397,12 +390,11 @@ int bd_acquire(struct inode *inode) ...@@ -397,12 +390,11 @@ int bd_acquire(struct inode *inode)
if (!bdev) if (!bdev)
return -ENOMEM; return -ENOMEM;
spin_lock(&bdev_lock); spin_lock(&bdev_lock);
if (!inode->i_bdev) { if (inode->i_bdev)
__bd_forget(inode);
inode->i_bdev = bdev; inode->i_bdev = bdev;
inode->i_mapping = bdev->bd_inode->i_mapping; inode->i_mapping = bdev->bd_inode->i_mapping;
list_add(&inode->i_devices, &bdev->bd_inodes); list_add(&inode->i_devices, &bdev->bd_inodes);
} else if (inode->i_bdev != bdev)
BUG();
spin_unlock(&bdev_lock); spin_unlock(&bdev_lock);
return 0; return 0;
} }
......
...@@ -336,10 +336,8 @@ struct address_space { ...@@ -336,10 +336,8 @@ struct address_space {
}; };
struct block_device { struct block_device {
struct list_head bd_hash;
atomic_t bd_count;
struct inode * bd_inode;
dev_t bd_dev; /* not a kdev_t - it's a search key */ dev_t bd_dev; /* not a kdev_t - it's a search key */
struct inode * bd_inode; /* will die */
int bd_openers; int bd_openers;
struct semaphore bd_sem; /* open/close mutex */ struct semaphore bd_sem; /* open/close mutex */
struct list_head bd_inodes; struct list_head bd_inodes;
...@@ -351,6 +349,7 @@ struct block_device { ...@@ -351,6 +349,7 @@ struct block_device {
unsigned bd_part_count; unsigned bd_part_count;
int bd_invalidated; int bd_invalidated;
struct gendisk * bd_disk; struct gendisk * bd_disk;
struct list_head bd_list;
}; };
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment