sysfs/kernfs: make read requests on pre-alloc files use the buffer.

To match the previous patch which used the pre-alloc buffer for
writes, this patch causes reads to use the same buffer.
This is not strictly necessary as the current seq_read() will allocate
on first read, so user-space can trigger the required pre-alloc.  But
consistency is valuable.

The read function is somewhat simpler than seq_read() and, for example,
does not support reading from an offset into the file: reads must be
at the start of the file.

As seq_read() does not use the prealloc buffer, ->seq_show is
incompatible with ->prealloc and caused an EINVAL return from open().
sysfs code which calls into kernfs always chooses the correct function.

As the buffer is shared with writes and other reads, the mutex is
extended to cover the copy_to_user.

Signed-off-by: NeilBrown <neilb@suse.de>
Reviewed-by: Tejun Heo <tj@kernel.org>
md: mark some attributes as pre-alloc
block_dev/DIO: Optionally allocate single 'struct dio' per file.

To be able to support RAID metadata operations in user-space, mdmon
(part of mdadm) sometimes needs to update the metadata on an array
before any future writes to the array are permitted.  This is
particularly needed for recording a device failure.

If that array is being used for swap (and even to some extent when
just used for a filesystem) then any memory allocation performed by
mdmon can cause a deadlock if the allocation waits for data to be
written out to the array.

mdmon uses mlockall(MCL_FUTURE|MCL_CURRENT) and is careful not to
allocate any memory at the wrong time.  However the kernel sometimes
allocates memory on its behalf and this can deadlock.

Updating the metadata requires an O_DIRECT write to each of a number
of files (which were previously opened).  Each write requires
allocating a 'struct dio'.

To avoid this deadlock risk, this patch caches the 'struct dio' the
first time it is allocated so that future writes on the file do not
require the allocation.  It is cached in '->private_data' for the
struct file.  Only a single struct is cached so only sequential
accesses are allocation-free.

The caching is only performed if mlockall(MCL_FUTURE) is in effect,
thus limiting the change to only those cases where it will bring a
benefit.

Effectively, the memory allocated for O_DIRECT access is 'locked' in
place for future use.


Signed-off-by: NeilBrown <neilb@suse.de>
block_dev/DIO - cache one bio allocation when caching a DIO.

When performing an O_DIRECT write to a block device, a 'struct bio' is
allocated from a mempool.
There is only one mempool for all block devices so is a single block
device blocked indefinitely, the mempool could in theory be exhausted
and other block devices would be affected.

When mdmon needs to update RAID metadata (see previous patch) it needs
to perform an O_DIRECT write to some block devices while another block
device (the array) is frozen.  This could conceivably lead to a
deadlock.

Rather than allocate one mempool per block device (which would be an
effective solution), this patch effects a single-bio pool for each
'struct dio' that is being used by an mlockall(MCL_FUTURE) process.

'cache_bio' is added to 'struct dio' and placed at the end of that it
isn't zeroed out regularly.
When an allocation is needed, the bio is used if it is present and
large enough.  When a bio if freed, it is placed here if appropriate.

Naturally it is freed when the file is closed.

All other allocations to serve O_DIRECT writes are further down the
stack and use mempools that cannot be exhausted by a frozen md array.

Signed-off-by: NeilBrown <neilb@suse.de>