@@ -5,7 +5,7 @@
# Rewritten to use lists instead of if-statements.
#
-obj-y := open.o read_write.o file_table.o super.o \
+obj-y := open.o read_write.o file_table.o super.o dev.o\
char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \
ioctl.o readdir.o select.o dcache.o inode.o \
attr.o bad_inode.o file.o filesystems.o namespace.o \
@@ -665,7 +665,7 @@ void bdput(struct block_device *bdev)
EXPORT_SYMBOL(bdput);
-static struct block_device *bd_acquire(struct inode *inode)
+struct block_device *bd_acquire(struct inode *inode)
{
struct block_device *bdev;
@@ -1713,34 +1713,12 @@ EXPORT_SYMBOL(ioctl_by_bdev);
struct block_device *lookup_bdev(const char *pathname)
{
struct block_device *bdev;
- struct inode *inode;
- struct path path;
int error;
- if (!pathname || !*pathname)
- return ERR_PTR(-EINVAL);
-
- error = kern_path(pathname, LOOKUP_FOLLOW, &path);
+ error = __lookup_dev(pathname, NULL, &bdev);
if (error)
return ERR_PTR(error);
-
- inode = d_backing_inode(path.dentry);
- error = -ENOTBLK;
- if (!S_ISBLK(inode->i_mode))
- goto fail;
- error = -EACCES;
- if (path.mnt->mnt_flags & MNT_NODEV)
- goto fail;
- error = -ENOMEM;
- bdev = bd_acquire(inode);
- if (!bdev)
- goto fail;
-out:
- path_put(&path);
return bdev;
-fail:
- bdev = ERR_PTR(error);
- goto out;
}
EXPORT_SYMBOL(lookup_bdev);
new file mode 100644
@@ -0,0 +1,77 @@
+/*
+ * This file contains some functions shared by block_dev and char_dev
+ */
+#include <linux/fs.h>
+#include <linux/namei.h>
+#include <linux/mount.h>
+
+#include "internal.h"
+
+/**
+ * __lookup_dev - lookup a block_device or cdev by name
+ * @pathname: special file representing the device
+ * @cdevp: cdev would be returned by cdevp
+ * @bdevp: block_device would be returned by bdevp
+ *
+ * Get a reference to the block_deivce or cdev at @pathname in
+ * the current namespace if possible and return it.
+ */
+int __lookup_dev(const char *pathname, struct cdev **cdevp,
+ struct block_device **bdevp)
+{
+ struct inode *inode;
+ struct path path;
+ int error = 0;
+
+ if (!pathname || !*pathname)
+ return -EINVAL;
+
+ error = kern_path(pathname, LOOKUP_FOLLOW, &path);
+ if (error)
+ return error;
+
+ inode = d_backing_inode(path.dentry);
+
+ /**
+ * We need at least one of bdevp and cdevp to be NULL,
+ * but cdevp and bdevp can not be both NULL.
+ */
+ error = -EINVAL;
+ if (!(cdevp || bdevp) || (cdevp && bdevp))
+ goto out;
+
+ if (cdevp) {
+ if (!S_ISCHR(inode->i_mode)) {
+ error = -EINVAL;
+ goto out;
+ }
+ } else {
+ if (!S_ISBLK(inode->i_mode)) {
+ error = -ENOTBLK;
+ goto out;
+ }
+ }
+ error = -EACCES;
+ if (path.mnt->mnt_flags & MNT_NODEV)
+ goto out;
+ error = -ENXIO;
+ if (S_ISCHR(inode->i_mode)) {
+ struct cdev *cdev;
+
+ cdev = cd_acquire(inode);
+ if (!cdev)
+ goto out;
+ *cdevp = cdev;
+ } else {
+ struct block_device *bdev;
+
+ bdev = bd_acquire(inode);
+ if (!bdev)
+ goto out;
+ *bdevp = bdev;
+ }
+ error = 0;
+out:
+ path_put(&path);
+ return error;
+}
@@ -17,6 +17,12 @@ struct mount;
struct shrink_control;
/*
+ * dev.c
+ */
+extern int __lookup_dev(const char *pathname, struct cdev **cdevp,
+ struct block_device **bdevp);
+
+/*
* block_dev.c
*/
#ifdef CONFIG_BLOCK
@@ -24,6 +30,8 @@ extern void __init bdev_cache_init(void);
extern int __sync_blockdev(struct block_device *bdev, int wait);
+extern struct block_device *bd_acquire(struct inode *inode);
+
#else
static inline void bdev_cache_init(void)
{
@@ -33,6 +41,11 @@ static inline int __sync_blockdev(struct block_device *bdev, int wait)
{
return 0;
}
+
+static struct block_device *bd_acquire(struct inode *inode)
+{
+ return NULL;
+}
#endif
/*
@@ -44,6 +57,7 @@ extern void guard_bio_eod(int rw, struct bio *bio);
* char_dev.c
*/
extern void __init chrdev_init(void);
+extern struct cdev *cd_acquire(struct inode *inode);
/*
* namei.c