2009-01-08 10:07:11 +08:00
|
|
|
/*
|
|
|
|
* Driver giving user-space access to the kernel's xenbus connection
|
|
|
|
* to xenstore.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005, Christian Limpach
|
|
|
|
* Copyright (c) 2005, Rusty Russell, IBM Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation; or, when distributed
|
|
|
|
* separately from the Linux kernel or incorporated into other
|
|
|
|
* software packages, subject to the following license:
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this source file (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy, modify,
|
|
|
|
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
* 2008-10-07 Alex Zeffertt Replaced /proc/xen/xenbus with xenfs filesystem
|
|
|
|
* and /proc/xen compatibility mount point.
|
|
|
|
* Turned xenfs into a loadable module.
|
|
|
|
*/
|
|
|
|
|
2013-06-28 18:21:41 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2009-01-08 10:07:11 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/uio.h>
|
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/mutex.h>
|
2009-10-04 20:11:37 +08:00
|
|
|
#include <linux/sched.h>
|
2009-01-08 10:07:11 +08:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/string.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/slab.h>
|
2011-12-11 02:29:47 +08:00
|
|
|
#include <linux/miscdevice.h>
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
#include <xen/xenbus.h>
|
2011-12-20 04:08:15 +08:00
|
|
|
#include <xen/xen.h>
|
2009-01-08 10:07:11 +08:00
|
|
|
#include <asm/xen/hypervisor.h>
|
|
|
|
|
2017-02-09 21:39:56 +08:00
|
|
|
#include "xenbus.h"
|
|
|
|
|
2019-05-13 21:56:35 +08:00
|
|
|
unsigned int xb_dev_generation_id;
|
|
|
|
|
2009-01-08 10:07:11 +08:00
|
|
|
/*
|
|
|
|
* An element of a list of outstanding transactions, for which we're
|
|
|
|
* still waiting a reply.
|
|
|
|
*/
|
|
|
|
struct xenbus_transaction_holder {
|
|
|
|
struct list_head list;
|
|
|
|
struct xenbus_transaction handle;
|
2019-05-13 21:56:35 +08:00
|
|
|
unsigned int generation_id;
|
2009-01-08 10:07:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A buffer of data on the queue.
|
|
|
|
*/
|
|
|
|
struct read_buffer {
|
|
|
|
struct list_head list;
|
|
|
|
unsigned int cons;
|
|
|
|
unsigned int len;
|
|
|
|
char msg[];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct xenbus_file_priv {
|
|
|
|
/*
|
|
|
|
* msgbuffer_mutex is held while partial requests are built up
|
|
|
|
* and complete requests are acted on. It therefore protects
|
|
|
|
* the "transactions" and "watches" lists, and the partial
|
|
|
|
* request length and buffer.
|
|
|
|
*
|
|
|
|
* reply_mutex protects the reply being built up to return to
|
|
|
|
* usermode. It nests inside msgbuffer_mutex but may be held
|
|
|
|
* alone during a watch callback.
|
|
|
|
*/
|
|
|
|
struct mutex msgbuffer_mutex;
|
|
|
|
|
|
|
|
/* In-progress transactions */
|
|
|
|
struct list_head transactions;
|
|
|
|
|
|
|
|
/* Active watches. */
|
|
|
|
struct list_head watches;
|
|
|
|
|
|
|
|
/* Partial request. */
|
|
|
|
unsigned int len;
|
|
|
|
union {
|
|
|
|
struct xsd_sockmsg msg;
|
2012-01-04 19:39:51 +08:00
|
|
|
char buffer[XENSTORE_PAYLOAD_MAX];
|
2009-01-08 10:07:11 +08:00
|
|
|
} u;
|
|
|
|
|
|
|
|
/* Response queue. */
|
|
|
|
struct mutex reply_mutex;
|
|
|
|
struct list_head read_buffers;
|
|
|
|
wait_queue_head_t read_waitq;
|
|
|
|
|
2017-02-09 21:39:58 +08:00
|
|
|
struct kref kref;
|
2009-01-08 10:07:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Read out any raw xenbus messages queued up. */
|
|
|
|
static ssize_t xenbus_file_read(struct file *filp,
|
|
|
|
char __user *ubuf,
|
|
|
|
size_t len, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct xenbus_file_priv *u = filp->private_data;
|
|
|
|
struct read_buffer *rb;
|
|
|
|
unsigned i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&u->reply_mutex);
|
2010-09-09 06:10:42 +08:00
|
|
|
again:
|
2009-01-08 10:07:11 +08:00
|
|
|
while (list_empty(&u->read_buffers)) {
|
|
|
|
mutex_unlock(&u->reply_mutex);
|
2010-06-24 00:30:15 +08:00
|
|
|
if (filp->f_flags & O_NONBLOCK)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
2009-01-08 10:07:11 +08:00
|
|
|
ret = wait_event_interruptible(u->read_waitq,
|
|
|
|
!list_empty(&u->read_buffers));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
mutex_lock(&u->reply_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb = list_entry(u->read_buffers.next, struct read_buffer, list);
|
|
|
|
i = 0;
|
|
|
|
while (i < len) {
|
|
|
|
unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
|
|
|
|
|
|
|
|
ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
|
|
|
|
|
|
|
|
i += sz - ret;
|
|
|
|
rb->cons += sz - ret;
|
|
|
|
|
2010-08-26 03:19:53 +08:00
|
|
|
if (ret != 0) {
|
2009-01-08 10:07:11 +08:00
|
|
|
if (i == 0)
|
|
|
|
i = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear out buffer if it has been consumed */
|
|
|
|
if (rb->cons == rb->len) {
|
|
|
|
list_del(&rb->list);
|
|
|
|
kfree(rb);
|
|
|
|
if (list_empty(&u->read_buffers))
|
|
|
|
break;
|
|
|
|
rb = list_entry(u->read_buffers.next,
|
|
|
|
struct read_buffer, list);
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 06:10:42 +08:00
|
|
|
if (i == 0)
|
|
|
|
goto again;
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&u->reply_mutex);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a buffer to the queue. Caller must hold the appropriate lock
|
|
|
|
* if the queue is not local. (Commonly the caller will build up
|
|
|
|
* multiple queued buffers on a temporary local list, and then add it
|
|
|
|
* to the appropriate list under lock once all the buffers have een
|
|
|
|
* successfully allocated.)
|
|
|
|
*/
|
|
|
|
static int queue_reply(struct list_head *queue, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
struct read_buffer *rb;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
2016-01-19 00:54:43 +08:00
|
|
|
if (len > XENSTORE_PAYLOAD_MAX)
|
|
|
|
return -EINVAL;
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
|
|
|
|
if (rb == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
rb->cons = 0;
|
|
|
|
rb->len = len;
|
|
|
|
|
|
|
|
memcpy(rb->msg, data, len);
|
|
|
|
|
|
|
|
list_add_tail(&rb->list, queue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all the read_buffer s on a list.
|
|
|
|
* Caller must have sole reference to list.
|
|
|
|
*/
|
|
|
|
static void queue_cleanup(struct list_head *list)
|
|
|
|
{
|
|
|
|
struct read_buffer *rb;
|
|
|
|
|
|
|
|
while (!list_empty(list)) {
|
|
|
|
rb = list_entry(list->next, struct read_buffer, list);
|
|
|
|
list_del(list->next);
|
|
|
|
kfree(rb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct watch_adapter {
|
|
|
|
struct list_head list;
|
|
|
|
struct xenbus_watch watch;
|
|
|
|
struct xenbus_file_priv *dev_data;
|
|
|
|
char *token;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void free_watch_adapter(struct watch_adapter *watch)
|
|
|
|
{
|
|
|
|
kfree(watch->watch.node);
|
|
|
|
kfree(watch->token);
|
|
|
|
kfree(watch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct watch_adapter *alloc_watch_adapter(const char *path,
|
|
|
|
const char *token)
|
|
|
|
{
|
|
|
|
struct watch_adapter *watch;
|
|
|
|
|
|
|
|
watch = kzalloc(sizeof(*watch), GFP_KERNEL);
|
|
|
|
if (watch == NULL)
|
|
|
|
goto out_fail;
|
|
|
|
|
|
|
|
watch->watch.node = kstrdup(path, GFP_KERNEL);
|
|
|
|
if (watch->watch.node == NULL)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
watch->token = kstrdup(token, GFP_KERNEL);
|
|
|
|
if (watch->token == NULL)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
return watch;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
free_watch_adapter(watch);
|
|
|
|
|
|
|
|
out_fail:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void watch_fired(struct xenbus_watch *watch,
|
2017-02-09 21:39:57 +08:00
|
|
|
const char *path,
|
|
|
|
const char *token)
|
2009-01-08 10:07:11 +08:00
|
|
|
{
|
|
|
|
struct watch_adapter *adap;
|
|
|
|
struct xsd_sockmsg hdr;
|
2017-02-09 21:39:57 +08:00
|
|
|
const char *token_caller;
|
|
|
|
int path_len, tok_len, body_len;
|
2009-01-08 10:07:11 +08:00
|
|
|
int ret;
|
|
|
|
LIST_HEAD(staging_q);
|
|
|
|
|
|
|
|
adap = container_of(watch, struct watch_adapter, watch);
|
|
|
|
|
2017-02-09 21:39:57 +08:00
|
|
|
token_caller = adap->token;
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
path_len = strlen(path) + 1;
|
2017-02-09 21:39:57 +08:00
|
|
|
tok_len = strlen(token_caller) + 1;
|
|
|
|
body_len = path_len + tok_len;
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
hdr.type = XS_WATCH_EVENT;
|
|
|
|
hdr.len = body_len;
|
|
|
|
|
|
|
|
mutex_lock(&adap->dev_data->reply_mutex);
|
|
|
|
|
|
|
|
ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
|
|
|
|
if (!ret)
|
|
|
|
ret = queue_reply(&staging_q, path, path_len);
|
|
|
|
if (!ret)
|
2017-02-09 21:39:57 +08:00
|
|
|
ret = queue_reply(&staging_q, token_caller, tok_len);
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
/* success: pass reply list onto watcher */
|
|
|
|
list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
|
|
|
|
wake_up(&adap->dev_data->read_waitq);
|
|
|
|
} else
|
|
|
|
queue_cleanup(&staging_q);
|
|
|
|
|
|
|
|
mutex_unlock(&adap->dev_data->reply_mutex);
|
|
|
|
}
|
|
|
|
|
2017-02-09 21:39:58 +08:00
|
|
|
static void xenbus_file_free(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct xenbus_file_priv *u;
|
|
|
|
struct xenbus_transaction_holder *trans, *tmp;
|
|
|
|
struct watch_adapter *watch, *tmp_watch;
|
|
|
|
struct read_buffer *rb, *tmp_rb;
|
|
|
|
|
|
|
|
u = container_of(kref, struct xenbus_file_priv, kref);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No need for locking here because there are no other users,
|
|
|
|
* by definition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
|
|
|
|
xenbus_transaction_end(trans->handle, 1);
|
|
|
|
list_del(&trans->list);
|
|
|
|
kfree(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
|
|
|
|
unregister_xenbus_watch(&watch->watch);
|
|
|
|
list_del(&watch->list);
|
|
|
|
free_watch_adapter(watch);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
|
|
|
|
list_del(&rb->list);
|
|
|
|
kfree(rb);
|
|
|
|
}
|
|
|
|
kfree(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct xenbus_transaction_holder *xenbus_get_transaction(
|
|
|
|
struct xenbus_file_priv *u, uint32_t tx_id)
|
|
|
|
{
|
|
|
|
struct xenbus_transaction_holder *trans;
|
|
|
|
|
|
|
|
list_for_each_entry(trans, &u->transactions, list)
|
|
|
|
if (trans->handle.id == tx_id)
|
|
|
|
return trans;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xenbus_dev_queue_reply(struct xb_req_data *req)
|
|
|
|
{
|
|
|
|
struct xenbus_file_priv *u = req->par;
|
|
|
|
struct xenbus_transaction_holder *trans = NULL;
|
|
|
|
int rc;
|
|
|
|
LIST_HEAD(staging_q);
|
|
|
|
|
|
|
|
xs_request_exit(req);
|
|
|
|
|
|
|
|
mutex_lock(&u->msgbuffer_mutex);
|
|
|
|
|
|
|
|
if (req->type == XS_TRANSACTION_START) {
|
|
|
|
trans = xenbus_get_transaction(u, 0);
|
|
|
|
if (WARN_ON(!trans))
|
|
|
|
goto out;
|
|
|
|
if (req->msg.type == XS_ERROR) {
|
|
|
|
list_del(&trans->list);
|
|
|
|
kfree(trans);
|
|
|
|
} else {
|
|
|
|
rc = kstrtou32(req->body, 10, &trans->handle.id);
|
|
|
|
if (WARN_ON(rc))
|
|
|
|
goto out;
|
|
|
|
}
|
2018-03-15 10:43:20 +08:00
|
|
|
} else if (req->type == XS_TRANSACTION_END) {
|
2017-02-09 21:39:58 +08:00
|
|
|
trans = xenbus_get_transaction(u, req->msg.tx_id);
|
|
|
|
if (WARN_ON(!trans))
|
|
|
|
goto out;
|
|
|
|
list_del(&trans->list);
|
|
|
|
kfree(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&u->msgbuffer_mutex);
|
|
|
|
|
|
|
|
mutex_lock(&u->reply_mutex);
|
|
|
|
rc = queue_reply(&staging_q, &req->msg, sizeof(req->msg));
|
|
|
|
if (!rc)
|
|
|
|
rc = queue_reply(&staging_q, req->body, req->msg.len);
|
|
|
|
if (!rc) {
|
|
|
|
list_splice_tail(&staging_q, &u->read_buffers);
|
|
|
|
wake_up(&u->read_waitq);
|
|
|
|
} else {
|
|
|
|
queue_cleanup(&staging_q);
|
|
|
|
}
|
|
|
|
mutex_unlock(&u->reply_mutex);
|
|
|
|
|
|
|
|
kfree(req->body);
|
|
|
|
kfree(req);
|
|
|
|
|
|
|
|
kref_put(&u->kref, xenbus_file_free);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&u->msgbuffer_mutex);
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:19:47 +08:00
|
|
|
static int xenbus_command_reply(struct xenbus_file_priv *u,
|
|
|
|
unsigned int msg_type, const char *reply)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
struct xsd_sockmsg hdr;
|
2018-03-15 11:08:03 +08:00
|
|
|
char body[16];
|
2016-12-22 15:19:47 +08:00
|
|
|
} msg;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
msg.hdr = u->u.msg;
|
|
|
|
msg.hdr.type = msg_type;
|
|
|
|
msg.hdr.len = strlen(reply) + 1;
|
|
|
|
if (msg.hdr.len > sizeof(msg.body))
|
|
|
|
return -E2BIG;
|
2018-03-15 11:08:03 +08:00
|
|
|
memcpy(&msg.body, reply, msg.hdr.len);
|
2016-12-22 15:19:47 +08:00
|
|
|
|
|
|
|
mutex_lock(&u->reply_mutex);
|
|
|
|
rc = queue_reply(&u->read_buffers, &msg, sizeof(msg.hdr) + msg.hdr.len);
|
|
|
|
wake_up(&u->read_waitq);
|
|
|
|
mutex_unlock(&u->reply_mutex);
|
|
|
|
|
2017-02-09 21:39:58 +08:00
|
|
|
if (!rc)
|
|
|
|
kref_put(&u->kref, xenbus_file_free);
|
|
|
|
|
2016-12-22 15:19:47 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-01-08 10:07:11 +08:00
|
|
|
static int xenbus_write_transaction(unsigned msg_type,
|
|
|
|
struct xenbus_file_priv *u)
|
|
|
|
{
|
2009-01-24 16:22:47 +08:00
|
|
|
int rc;
|
2009-01-08 10:07:11 +08:00
|
|
|
struct xenbus_transaction_holder *trans = NULL;
|
2018-03-15 10:43:22 +08:00
|
|
|
struct {
|
|
|
|
struct xsd_sockmsg hdr;
|
|
|
|
char body[];
|
|
|
|
} *msg = (void *)u->u.buffer;
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
if (msg_type == XS_TRANSACTION_START) {
|
2017-02-09 21:39:58 +08:00
|
|
|
trans = kzalloc(sizeof(*trans), GFP_KERNEL);
|
2009-01-08 10:07:11 +08:00
|
|
|
if (!trans) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-05-13 21:56:35 +08:00
|
|
|
trans->generation_id = xb_dev_generation_id;
|
2017-02-09 21:39:58 +08:00
|
|
|
list_add(&trans->list, &u->transactions);
|
2018-03-15 10:43:22 +08:00
|
|
|
} else if (msg->hdr.tx_id != 0 &&
|
|
|
|
!xenbus_get_transaction(u, msg->hdr.tx_id))
|
2017-02-09 21:39:58 +08:00
|
|
|
return xenbus_command_reply(u, XS_ERROR, "ENOENT");
|
2018-03-15 10:43:22 +08:00
|
|
|
else if (msg_type == XS_TRANSACTION_END &&
|
|
|
|
!(msg->hdr.len == 2 &&
|
|
|
|
(!strcmp(msg->body, "T") || !strcmp(msg->body, "F"))))
|
|
|
|
return xenbus_command_reply(u, XS_ERROR, "EINVAL");
|
2019-05-13 21:56:35 +08:00
|
|
|
else if (msg_type == XS_TRANSACTION_END) {
|
|
|
|
trans = xenbus_get_transaction(u, msg->hdr.tx_id);
|
|
|
|
if (trans && trans->generation_id != xb_dev_generation_id) {
|
|
|
|
list_del(&trans->list);
|
|
|
|
kfree(trans);
|
|
|
|
if (!strcmp(msg->body, "T"))
|
|
|
|
return xenbus_command_reply(u, XS_ERROR,
|
|
|
|
"EAGAIN");
|
|
|
|
else
|
|
|
|
return xenbus_command_reply(u,
|
|
|
|
XS_TRANSACTION_END,
|
|
|
|
"OK");
|
|
|
|
}
|
|
|
|
}
|
2009-01-08 10:07:11 +08:00
|
|
|
|
2018-03-15 10:43:22 +08:00
|
|
|
rc = xenbus_dev_request_and_reply(&msg->hdr, u);
|
2017-04-04 20:27:22 +08:00
|
|
|
if (rc && trans) {
|
|
|
|
list_del(&trans->list);
|
2009-01-08 10:07:11 +08:00
|
|
|
kfree(trans);
|
2017-04-04 20:27:22 +08:00
|
|
|
}
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
|
|
|
|
{
|
2016-10-24 23:03:49 +08:00
|
|
|
struct watch_adapter *watch;
|
2009-01-08 10:07:11 +08:00
|
|
|
char *path, *token;
|
|
|
|
int err, rc;
|
|
|
|
|
|
|
|
path = u->u.buffer + sizeof(u->u.msg);
|
|
|
|
token = memchr(path, 0, u->u.msg.len);
|
|
|
|
if (token == NULL) {
|
2016-12-22 15:19:47 +08:00
|
|
|
rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
|
2009-01-08 10:07:11 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
token++;
|
2012-01-24 21:52:42 +08:00
|
|
|
if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
|
2016-12-22 15:19:47 +08:00
|
|
|
rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
|
2012-01-24 21:52:42 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
if (msg_type == XS_WATCH) {
|
|
|
|
watch = alloc_watch_adapter(path, token);
|
|
|
|
if (watch == NULL) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
watch->watch.callback = watch_fired;
|
|
|
|
watch->dev_data = u;
|
|
|
|
|
|
|
|
err = register_xenbus_watch(&watch->watch);
|
|
|
|
if (err) {
|
|
|
|
free_watch_adapter(watch);
|
|
|
|
rc = err;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
list_add(&watch->list, &u->watches);
|
|
|
|
} else {
|
2016-10-24 23:03:49 +08:00
|
|
|
list_for_each_entry(watch, &u->watches, list) {
|
2009-01-08 10:07:11 +08:00
|
|
|
if (!strcmp(watch->token, token) &&
|
|
|
|
!strcmp(watch->watch.node, path)) {
|
|
|
|
unregister_xenbus_watch(&watch->watch);
|
|
|
|
list_del(&watch->list);
|
|
|
|
free_watch_adapter(watch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success. Synthesize a reply to say all is OK. */
|
2016-12-22 15:19:47 +08:00
|
|
|
rc = xenbus_command_reply(u, msg_type, "OK");
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t xenbus_file_write(struct file *filp,
|
|
|
|
const char __user *ubuf,
|
|
|
|
size_t len, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct xenbus_file_priv *u = filp->private_data;
|
|
|
|
uint32_t msg_type;
|
|
|
|
int rc = len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're expecting usermode to be writing properly formed
|
|
|
|
* xenbus messages. If they write an incomplete message we
|
|
|
|
* buffer it up. Once it is complete, we act on it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure concurrent writers can't stomp all over each
|
|
|
|
* other's messages and make a mess of our partial message
|
|
|
|
* buffer. We don't make any attemppt to stop multiple
|
|
|
|
* writers from making a mess of each other's incomplete
|
|
|
|
* messages; we're just trying to guarantee our own internal
|
|
|
|
* consistency and make sure that single writes are handled
|
|
|
|
* atomically.
|
|
|
|
*/
|
|
|
|
mutex_lock(&u->msgbuffer_mutex);
|
|
|
|
|
|
|
|
/* Get this out of the way early to avoid confusion */
|
|
|
|
if (len == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Can't write a xenbus message larger we can buffer */
|
2012-10-18 01:14:09 +08:00
|
|
|
if (len > sizeof(u->u.buffer) - u->len) {
|
2009-01-08 10:07:11 +08:00
|
|
|
/* On error, dump existing buffer */
|
|
|
|
u->len = 0;
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
|
|
|
|
|
2010-08-26 03:19:53 +08:00
|
|
|
if (ret != 0) {
|
2009-01-08 10:07:11 +08:00
|
|
|
rc = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with a partial copy. */
|
|
|
|
len -= ret;
|
|
|
|
rc = len;
|
|
|
|
|
|
|
|
u->len += len;
|
|
|
|
|
|
|
|
/* Return if we haven't got a full message yet */
|
|
|
|
if (u->len < sizeof(u->u.msg))
|
|
|
|
goto out; /* not even the header yet */
|
|
|
|
|
|
|
|
/* If we're expecting a message that's larger than we can
|
|
|
|
possibly send, dump what we have and return an error. */
|
|
|
|
if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
|
|
|
|
rc = -E2BIG;
|
|
|
|
u->len = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
|
|
|
|
goto out; /* incomplete data portion */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OK, now we have a complete message. Do something with it.
|
|
|
|
*/
|
|
|
|
|
2017-02-09 21:39:58 +08:00
|
|
|
kref_get(&u->kref);
|
|
|
|
|
2009-01-08 10:07:11 +08:00
|
|
|
msg_type = u->u.msg.type;
|
|
|
|
|
|
|
|
switch (msg_type) {
|
|
|
|
case XS_WATCH:
|
|
|
|
case XS_UNWATCH:
|
|
|
|
/* (Un)Ask for some path to be watched for changes */
|
|
|
|
ret = xenbus_write_watch(msg_type, u);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-09-02 00:18:54 +08:00
|
|
|
/* Send out a transaction */
|
|
|
|
ret = xenbus_write_transaction(msg_type, u);
|
2009-01-08 10:07:11 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-02-09 21:39:58 +08:00
|
|
|
if (ret != 0) {
|
2009-01-08 10:07:11 +08:00
|
|
|
rc = ret;
|
2017-02-09 21:39:58 +08:00
|
|
|
kref_put(&u->kref, xenbus_file_free);
|
|
|
|
}
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
/* Buffered message consumed */
|
|
|
|
u->len = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&u->msgbuffer_mutex);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xenbus_file_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct xenbus_file_priv *u;
|
|
|
|
|
|
|
|
if (xen_store_evtchn == 0)
|
|
|
|
return -ENOENT;
|
|
|
|
|
fs: stream_open - opener for stream-like files so that read and write can run simultaneously without deadlock
Commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") added
locking for file.f_pos access and in particular made concurrent read and
write not possible - now both those functions take f_pos lock for the
whole run, and so if e.g. a read is blocked waiting for data, write will
deadlock waiting for that read to complete.
This caused regression for stream-like files where previously read and
write could run simultaneously, but after that patch could not do so
anymore. See e.g. commit 581d21a2d02a ("xenbus: fix deadlock on writes
to /proc/xen/xenbus") which fixes such regression for particular case of
/proc/xen/xenbus.
The patch that added f_pos lock in 2014 did so to guarantee POSIX thread
safety for read/write/lseek and added the locking to file descriptors of
all regular files. In 2014 that thread-safety problem was not new as it
was already discussed earlier in 2006.
However even though 2006'th version of Linus's patch was adding f_pos
locking "only for files that are marked seekable with FMODE_LSEEK (thus
avoiding the stream-like objects like pipes and sockets)", the 2014
version - the one that actually made it into the tree as 9c225f2655e3 -
is doing so irregardless of whether a file is seekable or not.
See
https://lore.kernel.org/lkml/53022DB1.4070805@gmail.com/
https://lwn.net/Articles/180387
https://lwn.net/Articles/180396
for historic context.
The reason that it did so is, probably, that there are many files that
are marked non-seekable, but e.g. their read implementation actually
depends on knowing current position to correctly handle the read. Some
examples:
kernel/power/user.c snapshot_read
fs/debugfs/file.c u32_array_read
fs/fuse/control.c fuse_conn_waiting_read + ...
drivers/hwmon/asus_atk0110.c atk_debugfs_ggrp_read
arch/s390/hypfs/inode.c hypfs_read_iter
...
Despite that, many nonseekable_open users implement read and write with
pure stream semantics - they don't depend on passed ppos at all. And for
those cases where read could wait for something inside, it creates a
situation similar to xenbus - the write could be never made to go until
read is done, and read is waiting for some, potentially external, event,
for potentially unbounded time -> deadlock.
Besides xenbus, there are 14 such places in the kernel that I've found
with semantic patch (see below):
drivers/xen/evtchn.c:667:8-24: ERROR: evtchn_fops: .read() can deadlock .write()
drivers/isdn/capi/capi.c:963:8-24: ERROR: capi_fops: .read() can deadlock .write()
drivers/input/evdev.c:527:1-17: ERROR: evdev_fops: .read() can deadlock .write()
drivers/char/pcmcia/cm4000_cs.c:1685:7-23: ERROR: cm4000_fops: .read() can deadlock .write()
net/rfkill/core.c:1146:8-24: ERROR: rfkill_fops: .read() can deadlock .write()
drivers/s390/char/fs3270.c:488:1-17: ERROR: fs3270_fops: .read() can deadlock .write()
drivers/usb/misc/ldusb.c:310:1-17: ERROR: ld_usb_fops: .read() can deadlock .write()
drivers/hid/uhid.c:635:1-17: ERROR: uhid_fops: .read() can deadlock .write()
net/batman-adv/icmp_socket.c:80:1-17: ERROR: batadv_fops: .read() can deadlock .write()
drivers/media/rc/lirc_dev.c:198:1-17: ERROR: lirc_fops: .read() can deadlock .write()
drivers/leds/uleds.c:77:1-17: ERROR: uleds_fops: .read() can deadlock .write()
drivers/input/misc/uinput.c:400:1-17: ERROR: uinput_fops: .read() can deadlock .write()
drivers/infiniband/core/user_mad.c:985:7-23: ERROR: umad_fops: .read() can deadlock .write()
drivers/gnss/core.c:45:1-17: ERROR: gnss_fops: .read() can deadlock .write()
In addition to the cases above another regression caused by f_pos
locking is that now FUSE filesystems that implement open with
FOPEN_NONSEEKABLE flag, can no longer implement bidirectional
stream-like files - for the same reason as above e.g. read can deadlock
write locking on file.f_pos in the kernel.
FUSE's FOPEN_NONSEEKABLE was added in 2008 in a7c1b990f715 ("fuse:
implement nonseekable open") to support OSSPD. OSSPD implements /dev/dsp
in userspace with FOPEN_NONSEEKABLE flag, with corresponding read and
write routines not depending on current position at all, and with both
read and write being potentially blocking operations:
See
https://github.com/libfuse/osspd
https://lwn.net/Articles/308445
https://github.com/libfuse/osspd/blob/14a9cff0/osspd.c#L1406
https://github.com/libfuse/osspd/blob/14a9cff0/osspd.c#L1438-L1477
https://github.com/libfuse/osspd/blob/14a9cff0/osspd.c#L1479-L1510
Corresponding libfuse example/test also describes FOPEN_NONSEEKABLE as
"somewhat pipe-like files ..." with read handler not using offset.
However that test implements only read without write and cannot exercise
the deadlock scenario:
https://github.com/libfuse/libfuse/blob/fuse-3.4.2-3-ga1bff7d/example/poll.c#L124-L131
https://github.com/libfuse/libfuse/blob/fuse-3.4.2-3-ga1bff7d/example/poll.c#L146-L163
https://github.com/libfuse/libfuse/blob/fuse-3.4.2-3-ga1bff7d/example/poll.c#L209-L216
I've actually hit the read vs write deadlock for real while implementing
my FUSE filesystem where there is /head/watch file, for which open
creates separate bidirectional socket-like stream in between filesystem
and its user with both read and write being later performed
simultaneously. And there it is semantically not easy to split the
stream into two separate read-only and write-only channels:
https://lab.nexedi.com/kirr/wendelin.core/blob/f13aa600/wcfs/wcfs.go#L88-169
Let's fix this regression. The plan is:
1. We can't change nonseekable_open to include &~FMODE_ATOMIC_POS -
doing so would break many in-kernel nonseekable_open users which
actually use ppos in read/write handlers.
2. Add stream_open() to kernel to open stream-like non-seekable file
descriptors. Read and write on such file descriptors would never use
nor change ppos. And with that property on stream-like files read and
write will be running without taking f_pos lock - i.e. read and write
could be running simultaneously.
3. With semantic patch search and convert to stream_open all in-kernel
nonseekable_open users for which read and write actually do not
depend on ppos and where there is no other methods in file_operations
which assume @offset access.
4. Add FOPEN_STREAM to fs/fuse/ and open in-kernel file-descriptors via
steam_open if that bit is present in filesystem open reply.
It was tempting to change fs/fuse/ open handler to use stream_open
instead of nonseekable_open on just FOPEN_NONSEEKABLE flags, but
grepping through Debian codesearch shows users of FOPEN_NONSEEKABLE,
and in particular GVFS which actually uses offset in its read and
write handlers
https://codesearch.debian.net/search?q=-%3Enonseekable+%3D
https://gitlab.gnome.org/GNOME/gvfs/blob/1.40.0-6-gcbc54396/client/gvfsfusedaemon.c#L1080
https://gitlab.gnome.org/GNOME/gvfs/blob/1.40.0-6-gcbc54396/client/gvfsfusedaemon.c#L1247-1346
https://gitlab.gnome.org/GNOME/gvfs/blob/1.40.0-6-gcbc54396/client/gvfsfusedaemon.c#L1399-1481
so if we would do such a change it will break a real user.
5. Add stream_open and FOPEN_STREAM handling to stable kernels starting
from v3.14+ (the kernel where 9c225f2655 first appeared).
This will allow to patch OSSPD and other FUSE filesystems that
provide stream-like files to return FOPEN_STREAM | FOPEN_NONSEEKABLE
in their open handler and this way avoid the deadlock on all kernel
versions. This should work because fs/fuse/ ignores unknown open
flags returned from a filesystem and so passing FOPEN_STREAM to a
kernel that is not aware of this flag cannot hurt. In turn the kernel
that is not aware of FOPEN_STREAM will be < v3.14 where just
FOPEN_NONSEEKABLE is sufficient to implement streams without read vs
write deadlock.
This patch adds stream_open, converts /proc/xen/xenbus to it and adds
semantic patch to automatically locate in-kernel places that are either
required to be converted due to read vs write deadlock, or that are just
safe to be converted because read and write do not use ppos and there
are no other funky methods in file_operations.
Regarding semantic patch I've verified each generated change manually -
that it is correct to convert - and each other nonseekable_open instance
left - that it is either not correct to convert there, or that it is not
converted due to current stream_open.cocci limitations.
The script also does not convert files that should be valid to convert,
but that currently have .llseek = noop_llseek or generic_file_llseek for
unknown reason despite file being opened with nonseekable_open (e.g.
drivers/input/mousedev.c)
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Yongzhi Pan <panyongzhi@gmail.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: David Vrabel <david.vrabel@citrix.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Miklos Szeredi <miklos@szeredi.hu>
Cc: Tejun Heo <tj@kernel.org>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Julia Lawall <Julia.Lawall@lip6.fr>
Cc: Nikolaus Rath <Nikolaus@rath.org>
Cc: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Kirill Smelkov <kirr@nexedi.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-03-27 06:20:43 +08:00
|
|
|
stream_open(inode, filp);
|
2016-12-09 22:41:13 +08:00
|
|
|
|
2009-01-08 10:07:11 +08:00
|
|
|
u = kzalloc(sizeof(*u), GFP_KERNEL);
|
|
|
|
if (u == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-02-09 21:39:58 +08:00
|
|
|
kref_init(&u->kref);
|
|
|
|
|
2009-01-08 10:07:11 +08:00
|
|
|
INIT_LIST_HEAD(&u->transactions);
|
|
|
|
INIT_LIST_HEAD(&u->watches);
|
|
|
|
INIT_LIST_HEAD(&u->read_buffers);
|
|
|
|
init_waitqueue_head(&u->read_waitq);
|
|
|
|
|
|
|
|
mutex_init(&u->reply_mutex);
|
|
|
|
mutex_init(&u->msgbuffer_mutex);
|
|
|
|
|
|
|
|
filp->private_data = u;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xenbus_file_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct xenbus_file_priv *u = filp->private_data;
|
|
|
|
|
2017-02-09 21:39:58 +08:00
|
|
|
kref_put(&u->kref, xenbus_file_free);
|
2009-01-08 10:07:11 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:39:46 +08:00
|
|
|
static __poll_t xenbus_file_poll(struct file *file, poll_table *wait)
|
2009-01-08 10:07:11 +08:00
|
|
|
{
|
|
|
|
struct xenbus_file_priv *u = file->private_data;
|
|
|
|
|
|
|
|
poll_wait(file, &u->read_waitq, wait);
|
|
|
|
if (!list_empty(&u->read_buffers))
|
2018-02-12 06:34:03 +08:00
|
|
|
return EPOLLIN | EPOLLRDNORM;
|
2009-01-08 10:07:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-11 02:29:47 +08:00
|
|
|
const struct file_operations xen_xenbus_fops = {
|
2009-01-08 10:07:11 +08:00
|
|
|
.read = xenbus_file_read,
|
|
|
|
.write = xenbus_file_write,
|
|
|
|
.open = xenbus_file_open,
|
|
|
|
.release = xenbus_file_release,
|
|
|
|
.poll = xenbus_file_poll,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = no_llseek,
|
2009-01-08 10:07:11 +08:00
|
|
|
};
|
2011-12-11 02:29:47 +08:00
|
|
|
EXPORT_SYMBOL_GPL(xen_xenbus_fops);
|
|
|
|
|
|
|
|
static struct miscdevice xenbus_dev = {
|
|
|
|
.minor = MISC_DYNAMIC_MINOR,
|
|
|
|
.name = "xen/xenbus",
|
|
|
|
.fops = &xen_xenbus_fops,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init xenbus_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!xen_domain())
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
err = misc_register(&xenbus_dev);
|
|
|
|
if (err)
|
2013-06-28 18:21:41 +08:00
|
|
|
pr_err("Could not register xenbus frontend device\n");
|
2011-12-11 02:29:47 +08:00
|
|
|
return err;
|
|
|
|
}
|
2016-02-22 08:06:06 +08:00
|
|
|
device_initcall(xenbus_init);
|