2006-06-25 20:48:51 +08:00
|
|
|
/*
|
|
|
|
FUSE: Filesystem in Userspace
|
2008-11-26 19:03:54 +08:00
|
|
|
Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
|
2006-06-25 20:48:51 +08:00
|
|
|
|
|
|
|
This program can be distributed under the terms of the GNU GPL.
|
|
|
|
See the file COPYING.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fuse_i.h"
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#define FUSE_CTL_SUPER_MAGIC 0x65735543
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is non-NULL when the single instance of the control filesystem
|
|
|
|
* exists. Protected by fuse_mutex
|
|
|
|
*/
|
|
|
|
static struct super_block *fuse_control_sb;
|
|
|
|
|
|
|
|
static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
|
|
|
|
{
|
|
|
|
struct fuse_conn *fc;
|
|
|
|
mutex_lock(&fuse_mutex);
|
2013-01-24 06:07:38 +08:00
|
|
|
fc = file_inode(file)->i_private;
|
2006-06-25 20:48:51 +08:00
|
|
|
if (fc)
|
|
|
|
fc = fuse_conn_get(fc);
|
|
|
|
mutex_unlock(&fuse_mutex);
|
|
|
|
return fc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
|
|
|
|
if (fc) {
|
|
|
|
fuse_abort_conn(fc);
|
|
|
|
fuse_conn_put(fc);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
|
|
|
|
size_t len, loff_t *ppos)
|
|
|
|
{
|
|
|
|
char tmp[32];
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (!*ppos) {
|
2008-11-26 19:03:54 +08:00
|
|
|
long value;
|
2006-06-25 20:48:51 +08:00
|
|
|
struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
|
|
|
|
if (!fc)
|
|
|
|
return 0;
|
|
|
|
|
2008-11-26 19:03:54 +08:00
|
|
|
value = atomic_read(&fc->num_waiting);
|
|
|
|
file->private_data = (void *)value;
|
2006-06-25 20:48:51 +08:00
|
|
|
fuse_conn_put(fc);
|
|
|
|
}
|
|
|
|
size = sprintf(tmp, "%ld\n", (long)file->private_data);
|
|
|
|
return simple_read_from_buffer(buf, len, ppos, tmp, size);
|
|
|
|
}
|
|
|
|
|
2009-08-27 01:18:24 +08:00
|
|
|
static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
|
|
|
|
size_t len, loff_t *ppos, unsigned val)
|
|
|
|
{
|
|
|
|
char tmp[32];
|
|
|
|
size_t size = sprintf(tmp, "%u\n", val);
|
|
|
|
|
|
|
|
return simple_read_from_buffer(buf, len, ppos, tmp, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fuse_conn_limit_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos, unsigned *val,
|
|
|
|
unsigned global_limit)
|
|
|
|
{
|
|
|
|
unsigned long t;
|
|
|
|
unsigned limit = (1 << 16) - 1;
|
|
|
|
int err;
|
|
|
|
|
2012-04-15 08:20:27 +08:00
|
|
|
if (*ppos)
|
2009-08-27 01:18:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2012-04-15 08:20:27 +08:00
|
|
|
err = kstrtoul_from_user(buf, count, 0, &t);
|
2009-08-27 01:18:24 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
limit = min(limit, global_limit);
|
|
|
|
|
|
|
|
if (t > limit)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*val = t;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fuse_conn_max_background_read(struct file *file,
|
|
|
|
char __user *buf, size_t len,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct fuse_conn *fc;
|
|
|
|
unsigned val;
|
|
|
|
|
|
|
|
fc = fuse_ctl_file_conn_get(file);
|
|
|
|
if (!fc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
val = fc->max_background;
|
|
|
|
fuse_conn_put(fc);
|
|
|
|
|
|
|
|
return fuse_conn_limit_read(file, buf, len, ppos, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fuse_conn_max_background_write(struct file *file,
|
|
|
|
const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2012-08-28 16:38:03 +08:00
|
|
|
unsigned uninitialized_var(val);
|
2009-08-27 01:18:24 +08:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
|
|
|
|
max_user_bgreq);
|
|
|
|
if (ret > 0) {
|
|
|
|
struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
|
|
|
|
if (fc) {
|
|
|
|
fc->max_background = val;
|
|
|
|
fuse_conn_put(fc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
|
|
|
|
char __user *buf, size_t len,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct fuse_conn *fc;
|
|
|
|
unsigned val;
|
|
|
|
|
|
|
|
fc = fuse_ctl_file_conn_get(file);
|
|
|
|
if (!fc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
val = fc->congestion_threshold;
|
|
|
|
fuse_conn_put(fc);
|
|
|
|
|
|
|
|
return fuse_conn_limit_read(file, buf, len, ppos, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
|
|
|
|
const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2012-08-28 16:38:03 +08:00
|
|
|
unsigned uninitialized_var(val);
|
2009-08-27 01:18:24 +08:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
|
|
|
|
max_user_congthresh);
|
|
|
|
if (ret > 0) {
|
|
|
|
struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
|
|
|
|
if (fc) {
|
|
|
|
fc->congestion_threshold = val;
|
|
|
|
fuse_conn_put(fc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-06-25 20:48:51 +08:00
|
|
|
static const struct file_operations fuse_ctl_abort_ops = {
|
|
|
|
.open = nonseekable_open,
|
|
|
|
.write = fuse_conn_abort_write,
|
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,
|
2006-06-25 20:48:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations fuse_ctl_waiting_ops = {
|
|
|
|
.open = nonseekable_open,
|
|
|
|
.read = fuse_conn_waiting_read,
|
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,
|
2006-06-25 20:48:51 +08:00
|
|
|
};
|
|
|
|
|
2009-08-27 01:18:24 +08:00
|
|
|
static const struct file_operations fuse_conn_max_background_ops = {
|
|
|
|
.open = nonseekable_open,
|
|
|
|
.read = fuse_conn_max_background_read,
|
|
|
|
.write = fuse_conn_max_background_write,
|
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-08-27 01:18:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations fuse_conn_congestion_threshold_ops = {
|
|
|
|
.open = nonseekable_open,
|
|
|
|
.read = fuse_conn_congestion_threshold_read,
|
|
|
|
.write = fuse_conn_congestion_threshold_write,
|
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-08-27 01:18:24 +08:00
|
|
|
};
|
|
|
|
|
2006-06-25 20:48:51 +08:00
|
|
|
static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
|
|
|
|
struct fuse_conn *fc,
|
|
|
|
const char *name,
|
|
|
|
int mode, int nlink,
|
2007-02-12 16:55:38 +08:00
|
|
|
const struct inode_operations *iop,
|
2006-06-25 20:48:51 +08:00
|
|
|
const struct file_operations *fop)
|
|
|
|
{
|
|
|
|
struct dentry *dentry;
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
|
|
|
|
dentry = d_alloc_name(parent, name);
|
|
|
|
if (!dentry)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fc->ctl_dentry[fc->ctl_ndents++] = dentry;
|
|
|
|
inode = new_inode(fuse_control_sb);
|
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-23 23:19:54 +08:00
|
|
|
inode->i_ino = get_next_ino();
|
2006-06-25 20:48:51 +08:00
|
|
|
inode->i_mode = mode;
|
|
|
|
inode->i_uid = fc->user_id;
|
|
|
|
inode->i_gid = fc->group_id;
|
|
|
|
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
|
|
|
/* setting ->i_op to NULL is not allowed */
|
|
|
|
if (iop)
|
|
|
|
inode->i_op = iop;
|
|
|
|
inode->i_fop = fop;
|
2011-10-28 20:13:29 +08:00
|
|
|
set_nlink(inode, nlink);
|
2006-09-27 16:50:46 +08:00
|
|
|
inode->i_private = fc;
|
2006-06-25 20:48:51 +08:00
|
|
|
d_add(dentry, inode);
|
|
|
|
return dentry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a connection to the control filesystem (if it exists). Caller
|
2006-07-30 18:04:10 +08:00
|
|
|
* must hold fuse_mutex
|
2006-06-25 20:48:51 +08:00
|
|
|
*/
|
|
|
|
int fuse_ctl_add_conn(struct fuse_conn *fc)
|
|
|
|
{
|
|
|
|
struct dentry *parent;
|
|
|
|
char name[32];
|
|
|
|
|
|
|
|
if (!fuse_control_sb)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
parent = fuse_control_sb->s_root;
|
2006-10-01 14:29:04 +08:00
|
|
|
inc_nlink(parent->d_inode);
|
2008-04-30 15:54:34 +08:00
|
|
|
sprintf(name, "%u", fc->dev);
|
2006-06-25 20:48:51 +08:00
|
|
|
parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
|
|
|
|
&simple_dir_inode_operations,
|
|
|
|
&simple_dir_operations);
|
|
|
|
if (!parent)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
|
2009-08-27 01:18:24 +08:00
|
|
|
NULL, &fuse_ctl_waiting_ops) ||
|
2006-06-25 20:48:51 +08:00
|
|
|
!fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
|
2009-08-27 01:18:24 +08:00
|
|
|
NULL, &fuse_ctl_abort_ops) ||
|
|
|
|
!fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
|
|
|
|
1, NULL, &fuse_conn_max_background_ops) ||
|
|
|
|
!fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
|
|
|
|
S_IFREG | 0600, 1, NULL,
|
|
|
|
&fuse_conn_congestion_threshold_ops))
|
2006-06-25 20:48:51 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
fuse_ctl_remove_conn(fc);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a connection from the control filesystem (if it exists).
|
2006-07-30 18:04:10 +08:00
|
|
|
* Caller must hold fuse_mutex
|
2006-06-25 20:48:51 +08:00
|
|
|
*/
|
|
|
|
void fuse_ctl_remove_conn(struct fuse_conn *fc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!fuse_control_sb)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = fc->ctl_ndents - 1; i >= 0; i--) {
|
|
|
|
struct dentry *dentry = fc->ctl_dentry[i];
|
2006-09-27 16:50:46 +08:00
|
|
|
dentry->d_inode->i_private = NULL;
|
2006-06-25 20:48:51 +08:00
|
|
|
d_drop(dentry);
|
|
|
|
dput(dentry);
|
|
|
|
}
|
2009-08-24 12:14:07 +08:00
|
|
|
drop_nlink(fuse_control_sb->s_root->d_inode);
|
2006-06-25 20:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
|
|
|
|
{
|
|
|
|
struct tree_descr empty_descr = {""};
|
|
|
|
struct fuse_conn *fc;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
mutex_lock(&fuse_mutex);
|
|
|
|
BUG_ON(fuse_control_sb);
|
|
|
|
fuse_control_sb = sb;
|
|
|
|
list_for_each_entry(fc, &fuse_conn_list, entry) {
|
|
|
|
err = fuse_ctl_add_conn(fc);
|
|
|
|
if (err) {
|
|
|
|
fuse_control_sb = NULL;
|
|
|
|
mutex_unlock(&fuse_mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_unlock(&fuse_mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-25 05:48:30 +08:00
|
|
|
static struct dentry *fuse_ctl_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *raw_data)
|
2006-06-25 20:48:51 +08:00
|
|
|
{
|
2010-07-25 05:48:30 +08:00
|
|
|
return mount_single(fs_type, flags, raw_data, fuse_ctl_fill_super);
|
2006-06-25 20:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fuse_ctl_kill_sb(struct super_block *sb)
|
|
|
|
{
|
2007-01-30 05:19:54 +08:00
|
|
|
struct fuse_conn *fc;
|
|
|
|
|
2006-06-25 20:48:51 +08:00
|
|
|
mutex_lock(&fuse_mutex);
|
|
|
|
fuse_control_sb = NULL;
|
2007-01-30 05:19:54 +08:00
|
|
|
list_for_each_entry(fc, &fuse_conn_list, entry)
|
|
|
|
fc->ctl_ndents = 0;
|
2006-06-25 20:48:51 +08:00
|
|
|
mutex_unlock(&fuse_mutex);
|
|
|
|
|
|
|
|
kill_litter_super(sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_system_type fuse_ctl_fs_type = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "fusectl",
|
2010-07-25 05:48:30 +08:00
|
|
|
.mount = fuse_ctl_mount,
|
2006-06-25 20:48:51 +08:00
|
|
|
.kill_sb = fuse_ctl_kill_sb,
|
|
|
|
};
|
2013-03-03 11:39:14 +08:00
|
|
|
MODULE_ALIAS_FS("fusectl");
|
2006-06-25 20:48:51 +08:00
|
|
|
|
|
|
|
int __init fuse_ctl_init(void)
|
|
|
|
{
|
|
|
|
return register_filesystem(&fuse_ctl_fs_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fuse_ctl_cleanup(void)
|
|
|
|
{
|
|
|
|
unregister_filesystem(&fuse_ctl_fs_type);
|
|
|
|
}
|