Commit 80e96b51 authored by Alexander Viro's avatar Alexander Viro Committed by Linus Torvalds

[PATCH] open_by_devnum()

	New helper - open_by_devnum().  Opens block_device by device number;
for use in situations when we really have nothing better than dev_t (i.e.
had received it from stupid userland API).
parent 259f529f
......@@ -345,26 +345,21 @@ static struct dm_dev *find_device(struct list_head *l, dev_t dev)
static int open_dev(struct dm_dev *d, dev_t dev)
{
static char *_claim_ptr = "I belong to device-mapper";
struct block_device *bdev;
int r;
if (d->bdev)
BUG();
d->bdev = bdget(dev);
if (!d->bdev)
return -ENOMEM;
r = blkdev_get(d->bdev, d->mode, 0, BDEV_RAW);
bdev = open_by_devnum(dev, d->mode, BDEV_RAW);
if (IS_ERR(bdev))
return PTR_ERR(bdev);
r = bd_claim(bdev, _claim_ptr);
if (r)
return r;
r = bd_claim(d->bdev, _claim_ptr);
if (r) {
blkdev_put(d->bdev, BDEV_RAW);
d->bdev = NULL;
}
blkdev_put(bdev, BDEV_RAW);
else
d->bdev = bdev;
return r;
}
......
......@@ -1046,12 +1046,9 @@ static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
int err = 0;
struct block_device *bdev;
bdev = bdget(dev);
if (!bdev)
return -ENOMEM;
err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_RAW);
if (err)
return err;
bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE, BDEV_RAW);
if (IS_ERR(bdev))
return PTR_ERR(bdev);
err = bd_claim(bdev, rdev);
if (err) {
blkdev_put(bdev, BDEV_RAW);
......
......@@ -1128,9 +1128,8 @@ static int __init init_blkmtd(void)
return 1;
}
/* get the block device */
bdev = bdget(rdev);
err = blkdev_get(bdev, mode, 0, BDEV_RAW);
if (err)
bdev = open_by_devnum(rdev, FMODE_READ, BDEV_RAW);
if (IS_ERR(bdev))
return 1;
DEBUG(1, "blkmtd: devname = %s\n", bdevname(bdev, b));
......
......@@ -436,6 +436,23 @@ void bd_release(struct block_device *bdev)
spin_unlock(&bdev_lock);
}
/*
* Tries to open block device by device number. Use it ONLY if you
* really do not have anything better - i.e. when you are behind a
* truly sucky interface and all you are given is a device number. _Never_
* to be used for internal purposes. If you ever need it - reconsider
* your API.
*/
struct block_device *open_by_devnum(dev_t dev, unsigned mode, int kind)
{
struct block_device *bdev = bdget(dev);
int err = -ENOMEM;
int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
if (bdev)
err = blkdev_get(bdev, mode, flags, kind);
return err ? ERR_PTR(err) : bdev;
}
/*
* This routine checks whether a removable media has been changed,
* and invalidates all buffer-cache-entries in that case. This
......
......@@ -384,20 +384,16 @@ void ext3_update_dynamic_rev(struct super_block *sb)
static struct block_device *ext3_blkdev_get(dev_t dev)
{
struct block_device *bdev;
int err = -ENODEV;
char b[BDEVNAME_SIZE];
bdev = bdget(dev);
if (bdev == NULL)
goto fail;
err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FS);
if (err < 0)
bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE, BDEV_FS);
if (IS_ERR(bdev))
goto fail;
return bdev;
fail:
printk(KERN_ERR "EXT3: failed to open journal device %s: %d\n",
__bdevname(dev, b), err);
printk(KERN_ERR "EXT3: failed to open journal device %s: %ld\n",
__bdevname(dev, b), PTR_ERR(bdev));
return NULL;
}
......
......@@ -1105,13 +1105,10 @@ int lmLogOpen(struct super_block *sb, struct jfs_log ** logptr)
*/
externalLog:
if (!(bdev = bdget(JFS_SBI(sb)->logdev))) {
rc = ENODEV;
goto free;
}
if ((rc = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FS))) {
rc = -rc;
bdev = open_by_devnum(JFS_SBI(sb)->logdev,
FMODE_READ|FMODE_WRITE, BDEV_FS);
if (IS_ERR(bdev)) {
rc = -PTR_ERR(bdev);
goto free;
}
......
......@@ -1911,21 +1911,16 @@ static int journal_init_dev( struct super_block *super,
/* there is no "jdev" option and journal is on separate device */
if( ( !jdev_name || !jdev_name[ 0 ] ) ) {
journal -> j_dev_bd = bdget(jdev);
if( journal -> j_dev_bd )
result = blkdev_get( journal -> j_dev_bd,
blkdev_mode, 0,
BDEV_FS );
else
result = -ENOMEM;
if( result != 0 )
journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode, BDEV_FS);
if (IS_ERR(journal->j_dev_bd)) {
result = PTR_ERR(journal->j_dev_bd);
journal->j_dev_bd = NULL;
printk( "sh-458: journal_init_dev: cannot init journal device\n '%s': %i",
bdevname(journal->j_dev_bd, b), result );
else if (jdev != super->s_dev) {
set_blocksize(journal->j_dev_bd, super->s_blocksize);
}
__bdevname(jdev, b), result );
return result;
} else if (jdev != super->s_dev)
set_blocksize(journal->j_dev_bd, super->s_blocksize);
return 0;
}
journal -> j_dev_file = filp_open( jdev_name, 0, 0 );
......
......@@ -1041,6 +1041,7 @@ extern void bd_forget(struct inode *inode);
extern void bdput(struct block_device *);
extern int blkdev_open(struct inode *, struct file *);
extern int blkdev_close(struct inode *, struct file *);
extern struct block_device *open_by_devnum(dev_t, unsigned, int);
extern struct file_operations def_blk_fops;
extern struct address_space_operations def_blk_aops;
extern struct file_operations def_chr_fops;
......
......@@ -209,6 +209,7 @@ EXPORT_SYMBOL(bd_claim);
EXPORT_SYMBOL(bd_release);
EXPORT_SYMBOL(open_bdev_excl);
EXPORT_SYMBOL(close_bdev_excl);
EXPORT_SYMBOL(open_by_devnum);
EXPORT_SYMBOL(__brelse);
EXPORT_SYMBOL(__bforget);
EXPORT_SYMBOL(ll_rw_block);
......
......@@ -1161,15 +1161,14 @@ static int read_suspend_image(const char * specialfile, int noresume)
struct block_device *bdev;
printk("Resuming from device %s\n",
__bdevname(resume_device, b));
bdev = bdget(resume_device);
if (!bdev) {
printk("No such block device ?!\n");
BUG();
}
blkdev_get(bdev, FMODE_READ, O_RDONLY, BDEV_RAW);
bdev = open_by_devnum(resume_device, FMODE_READ, BDEV_RAW);
if (IS_ERR(bdev)) {
error = PTR_ERR(bdev);
} else {
set_blocksize(bdev, PAGE_SIZE);
error = __read_suspend_image(bdev, cur, noresume);
blkdev_put(bdev, BDEV_RAW);
}
} else error = -ENOMEM;
if (scratch_page)
......
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