2019-05-19 20:08:20 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* linux/drivers/char/raw.c
|
|
|
|
*
|
|
|
|
* Front-end raw character devices. These can be bound to any block
|
|
|
|
* devices to provide genuine Unix raw character device semantics.
|
|
|
|
*
|
|
|
|
* We reserve minor number 0 for a control interface. ioctl()s on this
|
|
|
|
* device are used to bind the other minor numbers to block devices.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/major.h>
|
|
|
|
#include <linux/blkdev.h>
|
2015-05-23 05:13:32 +08:00
|
|
|
#include <linux/backing-dev.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/raw.h>
|
|
|
|
#include <linux/capability.h>
|
|
|
|
#include <linux/uio.h>
|
|
|
|
#include <linux/cdev.h>
|
|
|
|
#include <linux/device.h>
|
2006-03-23 19:00:21 +08:00
|
|
|
#include <linux/mutex.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/gfp.h>
|
2009-08-25 06:42:56 +08:00
|
|
|
#include <linux/compat.h>
|
2011-04-29 06:24:29 +08:00
|
|
|
#include <linux/vmalloc.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct raw_device_data {
|
|
|
|
struct block_device *binding;
|
|
|
|
int inuse;
|
|
|
|
};
|
|
|
|
|
2005-03-24 01:53:09 +08:00
|
|
|
static struct class *raw_class;
|
2011-04-29 06:24:29 +08:00
|
|
|
static struct raw_device_data *raw_devices;
|
2006-03-23 19:00:21 +08:00
|
|
|
static DEFINE_MUTEX(raw_mutex);
|
2006-07-03 15:24:21 +08:00
|
|
|
static const struct file_operations raw_ctl_fops; /* forward declaration */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-04-29 06:24:29 +08:00
|
|
|
static int max_raw_minors = MAX_RAW_MINORS;
|
|
|
|
|
|
|
|
module_param(max_raw_minors, int, 0);
|
|
|
|
MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Open/close code for raw IO.
|
|
|
|
*
|
|
|
|
* We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to
|
|
|
|
* point at the blockdev's address_space and set the file handle to use
|
|
|
|
* O_DIRECT.
|
|
|
|
*
|
|
|
|
* Set the device's soft blocksize to the minimum possible. This gives the
|
|
|
|
* finest possible alignment and has no adverse impact on performance.
|
|
|
|
*/
|
|
|
|
static int raw_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
const int minor = iminor(inode);
|
|
|
|
struct block_device *bdev;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (minor == 0) { /* It is the control device */
|
|
|
|
filp->f_op = &raw_ctl_fops;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-23 19:00:21 +08:00
|
|
|
mutex_lock(&raw_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All we need to do on open is check that the device is bound.
|
|
|
|
*/
|
|
|
|
bdev = raw_devices[minor].binding;
|
|
|
|
err = -ENODEV;
|
|
|
|
if (!bdev)
|
|
|
|
goto out;
|
2015-11-21 05:18:43 +08:00
|
|
|
bdgrab(bdev);
|
block: make blkdev_get/put() handle exclusive access
Over time, block layer has accumulated a set of APIs dealing with bdev
open, close, claim and release.
* blkdev_get/put() are the primary open and close functions.
* bd_claim/release() deal with exclusive open.
* open/close_bdev_exclusive() are combination of open and claim and
the other way around, respectively.
* bd_link/unlink_disk_holder() to create and remove holder/slave
symlinks.
* open_by_devnum() wraps bdget() + blkdev_get().
The interface is a bit confusing and the decoupling of open and claim
makes it impossible to properly guarantee exclusive access as
in-kernel open + claim sequence can disturb the existing exclusive
open even before the block layer knows the current open if for another
exclusive access. Reorganize the interface such that,
* blkdev_get() is extended to include exclusive access management.
@holder argument is added and, if is @FMODE_EXCL specified, it will
gain exclusive access atomically w.r.t. other exclusive accesses.
* blkdev_put() is similarly extended. It now takes @mode argument and
if @FMODE_EXCL is set, it releases an exclusive access. Also, when
the last exclusive claim is released, the holder/slave symlinks are
removed automatically.
* bd_claim/release() and close_bdev_exclusive() are no longer
necessary and either made static or removed.
* bd_link_disk_holder() remains the same but bd_unlink_disk_holder()
is no longer necessary and removed.
* open_bdev_exclusive() becomes a simple wrapper around lookup_bdev()
and blkdev_get(). It also has an unexpected extra bdev_read_only()
test which probably should be moved into blkdev_get().
* open_by_devnum() is modified to take @holder argument and pass it to
blkdev_get().
Most of bdev open/close operations are unified into blkdev_get/put()
and most exclusive accesses are tested atomically at the open time (as
it should). This cleans up code and removes some, both valid and
invalid, but unnecessary all the same, corner cases.
open_bdev_exclusive() and open_by_devnum() can use further cleanup -
rename to blkdev_get_by_path() and blkdev_get_by_devt() and drop
special features. Well, let's leave them for another day.
Most conversions are straight-forward. drbd conversion is a bit more
involved as there was some reordering, but the logic should stay the
same.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Neil Brown <neilb@suse.de>
Acked-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Acked-by: Philipp Reisner <philipp.reisner@linbit.com>
Cc: Peter Osterlund <petero2@telia.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Cc: Alex Elder <aelder@sgi.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: dm-devel@redhat.com
Cc: drbd-dev@lists.linbit.com
Cc: Leo Chen <leochen@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: Chris Mason <chris.mason@oracle.com>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
Cc: Joern Engel <joern@logfs.org>
Cc: reiserfs-devel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
2010-11-13 18:55:17 +08:00
|
|
|
err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2009-05-23 05:17:49 +08:00
|
|
|
err = set_blocksize(bdev, bdev_logical_block_size(bdev));
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
block: make blkdev_get/put() handle exclusive access
Over time, block layer has accumulated a set of APIs dealing with bdev
open, close, claim and release.
* blkdev_get/put() are the primary open and close functions.
* bd_claim/release() deal with exclusive open.
* open/close_bdev_exclusive() are combination of open and claim and
the other way around, respectively.
* bd_link/unlink_disk_holder() to create and remove holder/slave
symlinks.
* open_by_devnum() wraps bdget() + blkdev_get().
The interface is a bit confusing and the decoupling of open and claim
makes it impossible to properly guarantee exclusive access as
in-kernel open + claim sequence can disturb the existing exclusive
open even before the block layer knows the current open if for another
exclusive access. Reorganize the interface such that,
* blkdev_get() is extended to include exclusive access management.
@holder argument is added and, if is @FMODE_EXCL specified, it will
gain exclusive access atomically w.r.t. other exclusive accesses.
* blkdev_put() is similarly extended. It now takes @mode argument and
if @FMODE_EXCL is set, it releases an exclusive access. Also, when
the last exclusive claim is released, the holder/slave symlinks are
removed automatically.
* bd_claim/release() and close_bdev_exclusive() are no longer
necessary and either made static or removed.
* bd_link_disk_holder() remains the same but bd_unlink_disk_holder()
is no longer necessary and removed.
* open_bdev_exclusive() becomes a simple wrapper around lookup_bdev()
and blkdev_get(). It also has an unexpected extra bdev_read_only()
test which probably should be moved into blkdev_get().
* open_by_devnum() is modified to take @holder argument and pass it to
blkdev_get().
Most of bdev open/close operations are unified into blkdev_get/put()
and most exclusive accesses are tested atomically at the open time (as
it should). This cleans up code and removes some, both valid and
invalid, but unnecessary all the same, corner cases.
open_bdev_exclusive() and open_by_devnum() can use further cleanup -
rename to blkdev_get_by_path() and blkdev_get_by_devt() and drop
special features. Well, let's leave them for another day.
Most conversions are straight-forward. drbd conversion is a bit more
involved as there was some reordering, but the logic should stay the
same.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Neil Brown <neilb@suse.de>
Acked-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Acked-by: Philipp Reisner <philipp.reisner@linbit.com>
Cc: Peter Osterlund <petero2@telia.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Cc: Alex Elder <aelder@sgi.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: dm-devel@redhat.com
Cc: drbd-dev@lists.linbit.com
Cc: Leo Chen <leochen@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: Chris Mason <chris.mason@oracle.com>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
Cc: Joern Engel <joern@logfs.org>
Cc: reiserfs-devel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
2010-11-13 18:55:17 +08:00
|
|
|
goto out1;
|
2005-04-17 06:20:36 +08:00
|
|
|
filp->f_flags |= O_DIRECT;
|
|
|
|
filp->f_mapping = bdev->bd_inode->i_mapping;
|
|
|
|
if (++raw_devices[minor].inuse == 1)
|
2013-01-24 06:07:38 +08:00
|
|
|
file_inode(filp)->i_mapping =
|
2005-04-17 06:20:36 +08:00
|
|
|
bdev->bd_inode->i_mapping;
|
|
|
|
filp->private_data = bdev;
|
2006-03-23 19:00:21 +08:00
|
|
|
mutex_unlock(&raw_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out1:
|
block: make blkdev_get/put() handle exclusive access
Over time, block layer has accumulated a set of APIs dealing with bdev
open, close, claim and release.
* blkdev_get/put() are the primary open and close functions.
* bd_claim/release() deal with exclusive open.
* open/close_bdev_exclusive() are combination of open and claim and
the other way around, respectively.
* bd_link/unlink_disk_holder() to create and remove holder/slave
symlinks.
* open_by_devnum() wraps bdget() + blkdev_get().
The interface is a bit confusing and the decoupling of open and claim
makes it impossible to properly guarantee exclusive access as
in-kernel open + claim sequence can disturb the existing exclusive
open even before the block layer knows the current open if for another
exclusive access. Reorganize the interface such that,
* blkdev_get() is extended to include exclusive access management.
@holder argument is added and, if is @FMODE_EXCL specified, it will
gain exclusive access atomically w.r.t. other exclusive accesses.
* blkdev_put() is similarly extended. It now takes @mode argument and
if @FMODE_EXCL is set, it releases an exclusive access. Also, when
the last exclusive claim is released, the holder/slave symlinks are
removed automatically.
* bd_claim/release() and close_bdev_exclusive() are no longer
necessary and either made static or removed.
* bd_link_disk_holder() remains the same but bd_unlink_disk_holder()
is no longer necessary and removed.
* open_bdev_exclusive() becomes a simple wrapper around lookup_bdev()
and blkdev_get(). It also has an unexpected extra bdev_read_only()
test which probably should be moved into blkdev_get().
* open_by_devnum() is modified to take @holder argument and pass it to
blkdev_get().
Most of bdev open/close operations are unified into blkdev_get/put()
and most exclusive accesses are tested atomically at the open time (as
it should). This cleans up code and removes some, both valid and
invalid, but unnecessary all the same, corner cases.
open_bdev_exclusive() and open_by_devnum() can use further cleanup -
rename to blkdev_get_by_path() and blkdev_get_by_devt() and drop
special features. Well, let's leave them for another day.
Most conversions are straight-forward. drbd conversion is a bit more
involved as there was some reordering, but the logic should stay the
same.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Neil Brown <neilb@suse.de>
Acked-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Acked-by: Philipp Reisner <philipp.reisner@linbit.com>
Cc: Peter Osterlund <petero2@telia.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Cc: Alex Elder <aelder@sgi.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: dm-devel@redhat.com
Cc: drbd-dev@lists.linbit.com
Cc: Leo Chen <leochen@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: Chris Mason <chris.mason@oracle.com>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
Cc: Joern Engel <joern@logfs.org>
Cc: reiserfs-devel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
2010-11-13 18:55:17 +08:00
|
|
|
blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
|
2005-04-17 06:20:36 +08:00
|
|
|
out:
|
2006-03-23 19:00:21 +08:00
|
|
|
mutex_unlock(&raw_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When the final fd which refers to this character-special node is closed, we
|
|
|
|
* make its ->mapping point back at its own i_data.
|
|
|
|
*/
|
|
|
|
static int raw_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
const int minor= iminor(inode);
|
|
|
|
struct block_device *bdev;
|
|
|
|
|
2006-03-23 19:00:21 +08:00
|
|
|
mutex_lock(&raw_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
bdev = raw_devices[minor].binding;
|
2015-01-14 17:42:37 +08:00
|
|
|
if (--raw_devices[minor].inuse == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Here inode->i_mapping == bdev->bd_inode->i_mapping */
|
|
|
|
inode->i_mapping = &inode->i_data;
|
2006-03-23 19:00:21 +08:00
|
|
|
mutex_unlock(&raw_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
block: make blkdev_get/put() handle exclusive access
Over time, block layer has accumulated a set of APIs dealing with bdev
open, close, claim and release.
* blkdev_get/put() are the primary open and close functions.
* bd_claim/release() deal with exclusive open.
* open/close_bdev_exclusive() are combination of open and claim and
the other way around, respectively.
* bd_link/unlink_disk_holder() to create and remove holder/slave
symlinks.
* open_by_devnum() wraps bdget() + blkdev_get().
The interface is a bit confusing and the decoupling of open and claim
makes it impossible to properly guarantee exclusive access as
in-kernel open + claim sequence can disturb the existing exclusive
open even before the block layer knows the current open if for another
exclusive access. Reorganize the interface such that,
* blkdev_get() is extended to include exclusive access management.
@holder argument is added and, if is @FMODE_EXCL specified, it will
gain exclusive access atomically w.r.t. other exclusive accesses.
* blkdev_put() is similarly extended. It now takes @mode argument and
if @FMODE_EXCL is set, it releases an exclusive access. Also, when
the last exclusive claim is released, the holder/slave symlinks are
removed automatically.
* bd_claim/release() and close_bdev_exclusive() are no longer
necessary and either made static or removed.
* bd_link_disk_holder() remains the same but bd_unlink_disk_holder()
is no longer necessary and removed.
* open_bdev_exclusive() becomes a simple wrapper around lookup_bdev()
and blkdev_get(). It also has an unexpected extra bdev_read_only()
test which probably should be moved into blkdev_get().
* open_by_devnum() is modified to take @holder argument and pass it to
blkdev_get().
Most of bdev open/close operations are unified into blkdev_get/put()
and most exclusive accesses are tested atomically at the open time (as
it should). This cleans up code and removes some, both valid and
invalid, but unnecessary all the same, corner cases.
open_bdev_exclusive() and open_by_devnum() can use further cleanup -
rename to blkdev_get_by_path() and blkdev_get_by_devt() and drop
special features. Well, let's leave them for another day.
Most conversions are straight-forward. drbd conversion is a bit more
involved as there was some reordering, but the logic should stay the
same.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Neil Brown <neilb@suse.de>
Acked-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Acked-by: Philipp Reisner <philipp.reisner@linbit.com>
Cc: Peter Osterlund <petero2@telia.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Cc: Alex Elder <aelder@sgi.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: dm-devel@redhat.com
Cc: drbd-dev@lists.linbit.com
Cc: Leo Chen <leochen@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: Chris Mason <chris.mason@oracle.com>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
Cc: Joern Engel <joern@logfs.org>
Cc: reiserfs-devel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
2010-11-13 18:55:17 +08:00
|
|
|
blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forward ioctls to the underlying block device.
|
|
|
|
*/
|
2010-04-27 06:24:05 +08:00
|
|
|
static long
|
|
|
|
raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct block_device *bdev = filp->private_data;
|
2009-08-25 06:42:56 +08:00
|
|
|
return blkdev_ioctl(bdev, 0, command, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bind_set(int number, u64 major, u64 minor)
|
|
|
|
{
|
|
|
|
dev_t dev = MKDEV(major, minor);
|
|
|
|
struct raw_device_data *rawdev;
|
|
|
|
int err = 0;
|
|
|
|
|
2011-04-29 06:24:29 +08:00
|
|
|
if (number <= 0 || number >= max_raw_minors)
|
2009-08-25 06:42:56 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (MAJOR(dev) != major || MINOR(dev) != minor)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
rawdev = &raw_devices[number];
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-25 06:42:56 +08:00
|
|
|
/*
|
|
|
|
* This is like making block devices, so demand the
|
|
|
|
* same capability
|
|
|
|
*/
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For now, we don't need to check that the underlying
|
|
|
|
* block device is present or not: we can do that when
|
|
|
|
* the raw device is opened. Just check that the
|
|
|
|
* major/minor numbers make sense.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (MAJOR(dev) == 0 && dev != 0)
|
|
|
|
return -EINVAL;
|
2010-04-27 06:24:05 +08:00
|
|
|
|
2009-08-25 06:42:56 +08:00
|
|
|
mutex_lock(&raw_mutex);
|
|
|
|
if (rawdev->inuse) {
|
|
|
|
mutex_unlock(&raw_mutex);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
if (rawdev->binding) {
|
|
|
|
bdput(rawdev->binding);
|
|
|
|
module_put(THIS_MODULE);
|
|
|
|
}
|
|
|
|
if (!dev) {
|
|
|
|
/* unbind */
|
|
|
|
rawdev->binding = NULL;
|
|
|
|
device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
|
|
|
|
} else {
|
|
|
|
rawdev->binding = bdget(dev);
|
|
|
|
if (rawdev->binding == NULL) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
} else {
|
|
|
|
dev_t raw = MKDEV(RAW_MAJOR, number);
|
|
|
|
__module_get(THIS_MODULE);
|
|
|
|
device_destroy(raw_class, raw);
|
|
|
|
device_create(raw_class, NULL, raw, NULL,
|
|
|
|
"raw%d", number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_unlock(&raw_mutex);
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-08-25 06:42:56 +08:00
|
|
|
static int bind_get(int number, dev_t *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2009-08-25 06:42:56 +08:00
|
|
|
struct raw_device_data *rawdev;
|
|
|
|
struct block_device *bdev;
|
|
|
|
|
2014-02-05 06:23:12 +08:00
|
|
|
if (number <= 0 || number >= max_raw_minors)
|
2009-08-25 06:42:56 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
rawdev = &raw_devices[number];
|
|
|
|
|
|
|
|
mutex_lock(&raw_mutex);
|
|
|
|
bdev = rawdev->binding;
|
|
|
|
*dev = bdev ? bdev->bd_dev : 0;
|
|
|
|
mutex_unlock(&raw_mutex);
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with ioctls against the raw-device control interface, to bind
|
|
|
|
* and unbind other raw devices.
|
|
|
|
*/
|
2010-04-27 06:24:05 +08:00
|
|
|
static long raw_ctl_ioctl(struct file *filp, unsigned int command,
|
|
|
|
unsigned long arg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct raw_config_request rq;
|
2009-08-25 06:42:56 +08:00
|
|
|
dev_t dev;
|
|
|
|
int err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
case RAW_SETBIND:
|
2009-08-25 06:42:56 +08:00
|
|
|
if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case RAW_GETBIND:
|
2009-08-25 06:42:56 +08:00
|
|
|
if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
|
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-25 06:42:56 +08:00
|
|
|
err = bind_get(rq.raw_minor, &dev);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-25 06:42:56 +08:00
|
|
|
rq.block_major = MAJOR(dev);
|
|
|
|
rq.block_minor = MINOR(dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-25 06:42:56 +08:00
|
|
|
if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2009-08-25 06:42:56 +08:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
struct raw32_config_request {
|
|
|
|
compat_int_t raw_minor;
|
|
|
|
compat_u64 block_major;
|
|
|
|
compat_u64 block_minor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
struct raw32_config_request __user *user_req = compat_ptr(arg);
|
|
|
|
struct raw32_config_request rq;
|
|
|
|
dev_t dev;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case RAW_SETBIND:
|
|
|
|
if (copy_from_user(&rq, user_req, sizeof(rq)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
|
|
|
|
|
|
|
|
case RAW_GETBIND:
|
|
|
|
if (copy_from_user(&rq, user_req, sizeof(rq)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
err = bind_get(rq.raw_minor, &dev);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
rq.block_major = MAJOR(dev);
|
|
|
|
rq.block_minor = MINOR(dev);
|
|
|
|
|
|
|
|
if (copy_to_user(user_req, &rq, sizeof(rq)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2009-08-25 06:42:56 +08:00
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-03 15:24:21 +08:00
|
|
|
static const struct file_operations raw_fops = {
|
2014-09-29 22:21:10 +08:00
|
|
|
.read_iter = blkdev_read_iter,
|
2014-04-03 15:21:50 +08:00
|
|
|
.write_iter = blkdev_write_iter,
|
2010-04-27 06:24:05 +08:00
|
|
|
.fsync = blkdev_fsync,
|
|
|
|
.open = raw_open,
|
|
|
|
.release = raw_release,
|
|
|
|
.unlocked_ioctl = raw_ioctl,
|
2010-07-07 05:03:55 +08:00
|
|
|
.llseek = default_llseek,
|
2010-04-27 06:24:05 +08:00
|
|
|
.owner = THIS_MODULE,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2006-07-03 15:24:21 +08:00
|
|
|
static const struct file_operations raw_ctl_fops = {
|
2010-04-27 06:24:05 +08:00
|
|
|
.unlocked_ioctl = raw_ctl_ioctl,
|
2009-08-25 06:42:56 +08:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = raw_ctl_compat_ioctl,
|
|
|
|
#endif
|
2010-04-27 06:24:05 +08:00
|
|
|
.open = raw_open,
|
|
|
|
.owner = THIS_MODULE,
|
2010-07-07 05:03:55 +08:00
|
|
|
.llseek = noop_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2007-09-13 06:06:57 +08:00
|
|
|
static struct cdev raw_cdev;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-07-24 08:24:48 +08:00
|
|
|
static char *raw_devnode(struct device *dev, umode_t *mode)
|
2009-04-30 21:23:42 +08:00
|
|
|
{
|
|
|
|
return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev));
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int __init raw_init(void)
|
|
|
|
{
|
|
|
|
dev_t dev = MKDEV(RAW_MAJOR, 0);
|
2006-09-29 17:00:46 +08:00
|
|
|
int ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-04-29 06:24:29 +08:00
|
|
|
if (max_raw_minors < 1 || max_raw_minors > 65536) {
|
|
|
|
printk(KERN_WARNING "raw: invalid max_raw_minors (must be"
|
|
|
|
" between 1 and 65536), using %d\n", MAX_RAW_MINORS);
|
|
|
|
max_raw_minors = MAX_RAW_MINORS;
|
|
|
|
}
|
|
|
|
|
treewide: Use array_size() in vzalloc()
The vzalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vzalloc(a * b)
with:
vzalloc(array_size(a, b))
as well as handling cases of:
vzalloc(a * b * c)
with:
vzalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vzalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vzalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vzalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vzalloc(C1 * C2 * C3, ...)
|
vzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vzalloc(C1 * C2, ...)
|
vzalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:27:37 +08:00
|
|
|
raw_devices = vzalloc(array_size(max_raw_minors,
|
|
|
|
sizeof(struct raw_device_data)));
|
2011-04-29 06:24:29 +08:00
|
|
|
if (!raw_devices) {
|
|
|
|
printk(KERN_ERR "Not enough memory for raw device structures\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = register_chrdev_region(dev, max_raw_minors, "raw");
|
2006-09-29 17:00:46 +08:00
|
|
|
if (ret)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
cdev_init(&raw_cdev, &raw_fops);
|
2011-04-29 06:24:29 +08:00
|
|
|
ret = cdev_add(&raw_cdev, dev, max_raw_minors);
|
2015-12-22 02:41:11 +08:00
|
|
|
if (ret)
|
2006-09-29 17:00:46 +08:00
|
|
|
goto error_region;
|
2005-03-24 01:53:09 +08:00
|
|
|
raw_class = class_create(THIS_MODULE, "raw");
|
2005-04-17 06:20:36 +08:00
|
|
|
if (IS_ERR(raw_class)) {
|
|
|
|
printk(KERN_ERR "Error creating raw class.\n");
|
|
|
|
cdev_del(&raw_cdev);
|
2006-09-29 17:00:46 +08:00
|
|
|
ret = PTR_ERR(raw_class);
|
|
|
|
goto error_region;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2009-09-19 05:01:12 +08:00
|
|
|
raw_class->devnode = raw_devnode;
|
2008-07-22 11:03:34 +08:00
|
|
|
device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl");
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2006-09-29 17:00:46 +08:00
|
|
|
error_region:
|
2011-04-29 06:24:29 +08:00
|
|
|
unregister_chrdev_region(dev, max_raw_minors);
|
2005-04-17 06:20:36 +08:00
|
|
|
error:
|
2011-04-29 06:24:29 +08:00
|
|
|
vfree(raw_devices);
|
2006-09-29 17:00:46 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit raw_exit(void)
|
|
|
|
{
|
2006-08-08 13:19:37 +08:00
|
|
|
device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
|
2005-03-24 01:53:09 +08:00
|
|
|
class_destroy(raw_class);
|
2005-04-17 06:20:36 +08:00
|
|
|
cdev_del(&raw_cdev);
|
2011-04-29 06:24:29 +08:00
|
|
|
unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(raw_init);
|
|
|
|
module_exit(raw_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|