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) ...@@ -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 int open_dev(struct dm_dev *d, dev_t dev)
{ {
static char *_claim_ptr = "I belong to device-mapper"; static char *_claim_ptr = "I belong to device-mapper";
struct block_device *bdev;
int r; int r;
if (d->bdev) if (d->bdev)
BUG(); BUG();
d->bdev = bdget(dev); bdev = open_by_devnum(dev, d->mode, BDEV_RAW);
if (!d->bdev) if (IS_ERR(bdev))
return -ENOMEM; return PTR_ERR(bdev);
r = bd_claim(bdev, _claim_ptr);
r = blkdev_get(d->bdev, d->mode, 0, BDEV_RAW);
if (r) if (r)
return r; blkdev_put(bdev, BDEV_RAW);
else
r = bd_claim(d->bdev, _claim_ptr); d->bdev = bdev;
if (r) {
blkdev_put(d->bdev, BDEV_RAW);
d->bdev = NULL;
}
return r; return r;
} }
......
...@@ -1046,12 +1046,9 @@ static int lock_rdev(mdk_rdev_t *rdev, dev_t dev) ...@@ -1046,12 +1046,9 @@ static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
int err = 0; int err = 0;
struct block_device *bdev; struct block_device *bdev;
bdev = bdget(dev); bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE, BDEV_RAW);
if (!bdev) if (IS_ERR(bdev))
return -ENOMEM; return PTR_ERR(bdev);
err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_RAW);
if (err)
return err;
err = bd_claim(bdev, rdev); err = bd_claim(bdev, rdev);
if (err) { if (err) {
blkdev_put(bdev, BDEV_RAW); blkdev_put(bdev, BDEV_RAW);
......
...@@ -1128,9 +1128,8 @@ static int __init init_blkmtd(void) ...@@ -1128,9 +1128,8 @@ static int __init init_blkmtd(void)
return 1; return 1;
} }
/* get the block device */ /* get the block device */
bdev = bdget(rdev); bdev = open_by_devnum(rdev, FMODE_READ, BDEV_RAW);
err = blkdev_get(bdev, mode, 0, BDEV_RAW); if (IS_ERR(bdev))
if (err)
return 1; return 1;
DEBUG(1, "blkmtd: devname = %s\n", bdevname(bdev, b)); DEBUG(1, "blkmtd: devname = %s\n", bdevname(bdev, b));
......
...@@ -436,6 +436,23 @@ void bd_release(struct block_device *bdev) ...@@ -436,6 +436,23 @@ void bd_release(struct block_device *bdev)
spin_unlock(&bdev_lock); 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, * This routine checks whether a removable media has been changed,
* and invalidates all buffer-cache-entries in that case. This * and invalidates all buffer-cache-entries in that case. This
......
...@@ -384,20 +384,16 @@ void ext3_update_dynamic_rev(struct super_block *sb) ...@@ -384,20 +384,16 @@ void ext3_update_dynamic_rev(struct super_block *sb)
static struct block_device *ext3_blkdev_get(dev_t dev) static struct block_device *ext3_blkdev_get(dev_t dev)
{ {
struct block_device *bdev; struct block_device *bdev;
int err = -ENODEV;
char b[BDEVNAME_SIZE]; char b[BDEVNAME_SIZE];
bdev = bdget(dev); bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE, BDEV_FS);
if (bdev == NULL) if (IS_ERR(bdev))
goto fail;
err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FS);
if (err < 0)
goto fail; goto fail;
return bdev; return bdev;
fail: fail:
printk(KERN_ERR "EXT3: failed to open journal device %s: %d\n", printk(KERN_ERR "EXT3: failed to open journal device %s: %ld\n",
__bdevname(dev, b), err); __bdevname(dev, b), PTR_ERR(bdev));
return NULL; return NULL;
} }
......
...@@ -1105,13 +1105,10 @@ int lmLogOpen(struct super_block *sb, struct jfs_log ** logptr) ...@@ -1105,13 +1105,10 @@ int lmLogOpen(struct super_block *sb, struct jfs_log ** logptr)
*/ */
externalLog: externalLog:
if (!(bdev = bdget(JFS_SBI(sb)->logdev))) { bdev = open_by_devnum(JFS_SBI(sb)->logdev,
rc = ENODEV; FMODE_READ|FMODE_WRITE, BDEV_FS);
goto free; if (IS_ERR(bdev)) {
} rc = -PTR_ERR(bdev);
if ((rc = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FS))) {
rc = -rc;
goto free; goto free;
} }
......
...@@ -1911,21 +1911,16 @@ static int journal_init_dev( struct super_block *super, ...@@ -1911,21 +1911,16 @@ static int journal_init_dev( struct super_block *super,
/* there is no "jdev" option and journal is on separate device */ /* there is no "jdev" option and journal is on separate device */
if( ( !jdev_name || !jdev_name[ 0 ] ) ) { if( ( !jdev_name || !jdev_name[ 0 ] ) ) {
journal -> j_dev_bd = bdget(jdev); journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode, BDEV_FS);
if( journal -> j_dev_bd ) if (IS_ERR(journal->j_dev_bd)) {
result = blkdev_get( journal -> j_dev_bd, result = PTR_ERR(journal->j_dev_bd);
blkdev_mode, 0, journal->j_dev_bd = NULL;
BDEV_FS );
else
result = -ENOMEM;
if( result != 0 )
printk( "sh-458: journal_init_dev: cannot init journal device\n '%s': %i", printk( "sh-458: journal_init_dev: cannot init journal device\n '%s': %i",
bdevname(journal->j_dev_bd, b), result ); __bdevname(jdev, b), result );
else if (jdev != super->s_dev) {
set_blocksize(journal->j_dev_bd, super->s_blocksize);
}
return 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 ); journal -> j_dev_file = filp_open( jdev_name, 0, 0 );
......
...@@ -1041,6 +1041,7 @@ extern void bd_forget(struct inode *inode); ...@@ -1041,6 +1041,7 @@ extern void bd_forget(struct inode *inode);
extern void bdput(struct block_device *); extern void bdput(struct block_device *);
extern int blkdev_open(struct inode *, struct file *); extern int blkdev_open(struct inode *, struct file *);
extern int blkdev_close(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 file_operations def_blk_fops;
extern struct address_space_operations def_blk_aops; extern struct address_space_operations def_blk_aops;
extern struct file_operations def_chr_fops; extern struct file_operations def_chr_fops;
......
...@@ -209,6 +209,7 @@ EXPORT_SYMBOL(bd_claim); ...@@ -209,6 +209,7 @@ EXPORT_SYMBOL(bd_claim);
EXPORT_SYMBOL(bd_release); EXPORT_SYMBOL(bd_release);
EXPORT_SYMBOL(open_bdev_excl); EXPORT_SYMBOL(open_bdev_excl);
EXPORT_SYMBOL(close_bdev_excl); EXPORT_SYMBOL(close_bdev_excl);
EXPORT_SYMBOL(open_by_devnum);
EXPORT_SYMBOL(__brelse); EXPORT_SYMBOL(__brelse);
EXPORT_SYMBOL(__bforget); EXPORT_SYMBOL(__bforget);
EXPORT_SYMBOL(ll_rw_block); EXPORT_SYMBOL(ll_rw_block);
......
...@@ -1161,15 +1161,14 @@ static int read_suspend_image(const char * specialfile, int noresume) ...@@ -1161,15 +1161,14 @@ static int read_suspend_image(const char * specialfile, int noresume)
struct block_device *bdev; struct block_device *bdev;
printk("Resuming from device %s\n", printk("Resuming from device %s\n",
__bdevname(resume_device, b)); __bdevname(resume_device, b));
bdev = bdget(resume_device); bdev = open_by_devnum(resume_device, FMODE_READ, BDEV_RAW);
if (!bdev) { if (IS_ERR(bdev)) {
printk("No such block device ?!\n"); error = PTR_ERR(bdev);
BUG(); } else {
}
blkdev_get(bdev, FMODE_READ, O_RDONLY, BDEV_RAW);
set_blocksize(bdev, PAGE_SIZE); set_blocksize(bdev, PAGE_SIZE);
error = __read_suspend_image(bdev, cur, noresume); error = __read_suspend_image(bdev, cur, noresume);
blkdev_put(bdev, BDEV_RAW); blkdev_put(bdev, BDEV_RAW);
}
} else error = -ENOMEM; } else error = -ENOMEM;
if (scratch_page) 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