2005-04-17 05:20:36 +07:00
|
|
|
/**
|
|
|
|
* @file oprofilefs.c
|
|
|
|
*
|
|
|
|
* @remark Copyright 2002 OProfile authors
|
|
|
|
* @remark Read the file COPYING
|
|
|
|
*
|
|
|
|
* @author John Levon
|
|
|
|
*
|
|
|
|
* A simple filesystem for configuration and
|
|
|
|
* access of oprofile.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/oprofile.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
|
|
|
#include "oprof.h"
|
|
|
|
|
|
|
|
#define OPROFILEFS_MAGIC 0x6f70726f
|
|
|
|
|
2009-07-25 21:18:34 +07:00
|
|
|
DEFINE_RAW_SPINLOCK(oprofilefs_lock);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-05 22:12:36 +07:00
|
|
|
struct inode *inode = new_inode(sb);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (inode) {
|
2010-10-23 22:19:54 +07:00
|
|
|
inode->i_ino = get_next_ino();
|
2005-04-17 05:20:36 +07:00
|
|
|
inode->i_mode = mode;
|
|
|
|
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
|
|
|
}
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-22 07:01:09 +07:00
|
|
|
static const struct super_operations s_ops = {
|
2005-04-17 05:20:36 +07:00
|
|
|
.statfs = simple_statfs,
|
|
|
|
.drop_inode = generic_delete_inode,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
return simple_read_from_buffer(buf, count, offset, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define TMPBUFSIZE 50
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
char tmpbuf[TMPBUFSIZE];
|
|
|
|
size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
|
|
|
|
if (maxlen > TMPBUFSIZE)
|
|
|
|
maxlen = TMPBUFSIZE;
|
|
|
|
return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-19 22:38:30 +07:00
|
|
|
/*
|
|
|
|
* Note: If oprofilefs_ulong_from_user() returns 0, then *val remains
|
|
|
|
* unchanged and might be uninitialized. This follows write syscall
|
|
|
|
* implementation when count is zero: "If count is zero ... [and if]
|
|
|
|
* no errors are detected, 0 will be returned without causing any
|
|
|
|
* other effect." (man 2 write)
|
|
|
|
*/
|
2008-09-05 22:12:36 +07:00
|
|
|
int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
char tmpbuf[TMPBUFSIZE];
|
2007-03-28 23:12:34 +07:00
|
|
|
unsigned long flags;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (count > TMPBUFSIZE - 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memset(tmpbuf, 0x0, TMPBUFSIZE);
|
|
|
|
|
|
|
|
if (copy_from_user(tmpbuf, buf, count))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2009-07-25 21:18:34 +07:00
|
|
|
raw_spin_lock_irqsave(&oprofilefs_lock, flags);
|
2005-04-17 05:20:36 +07:00
|
|
|
*val = simple_strtoul(tmpbuf, NULL, 0);
|
2009-07-25 21:18:34 +07:00
|
|
|
raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
|
2011-12-19 22:38:30 +07:00
|
|
|
return count;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-05 22:12:36 +07:00
|
|
|
unsigned long *val = file->private_data;
|
2005-04-17 05:20:36 +07:00
|
|
|
return oprofilefs_ulong_to_user(*val, buf, count, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2010-10-05 02:09:36 +07:00
|
|
|
unsigned long value;
|
2005-04-17 05:20:36 +07:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (*offset)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-10-05 02:09:36 +07:00
|
|
|
retval = oprofilefs_ulong_from_user(&value, buf, count);
|
2011-12-19 22:38:30 +07:00
|
|
|
if (retval <= 0)
|
2010-10-05 02:09:36 +07:00
|
|
|
return retval;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2010-10-05 02:09:36 +07:00
|
|
|
retval = oprofile_set_ulong(file->private_data, value);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
2010-10-05 02:09:36 +07:00
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
static int default_open(struct inode *inode, struct file *filp)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2006-09-27 15:50:46 +07:00
|
|
|
if (inode->i_private)
|
|
|
|
filp->private_data = inode->i_private;
|
2005-04-17 05:20:36 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-12 15:55:34 +07:00
|
|
|
static const struct file_operations ulong_fops = {
|
2005-04-17 05:20:36 +07:00
|
|
|
.read = ulong_read_file,
|
|
|
|
.write = ulong_write_file,
|
|
|
|
.open = default_open,
|
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-15 23:52:59 +07:00
|
|
|
.llseek = default_llseek,
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-02-12 15:55:34 +07:00
|
|
|
static const struct file_operations ulong_ro_fops = {
|
2005-04-17 05:20:36 +07:00
|
|
|
.read = ulong_read_file,
|
|
|
|
.open = default_open,
|
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-15 23:52:59 +07:00
|
|
|
.llseek = default_llseek,
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-08-19 15:50:26 +07:00
|
|
|
static int __oprofilefs_create_file(struct super_block *sb,
|
2008-09-05 22:12:36 +07:00
|
|
|
struct dentry *root, char const *name, const struct file_operations *fops,
|
2010-08-19 15:50:26 +07:00
|
|
|
int perm, void *priv)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-05 22:12:36 +07:00
|
|
|
struct dentry *dentry;
|
|
|
|
struct inode *inode;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
dentry = d_alloc_name(root, name);
|
|
|
|
if (!dentry)
|
2010-08-19 15:50:26 +07:00
|
|
|
return -ENOMEM;
|
2005-04-17 05:20:36 +07:00
|
|
|
inode = oprofilefs_get_inode(sb, S_IFREG | perm);
|
|
|
|
if (!inode) {
|
|
|
|
dput(dentry);
|
2010-08-19 15:50:26 +07:00
|
|
|
return -ENOMEM;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
inode->i_fop = fops;
|
|
|
|
d_add(dentry, inode);
|
2010-08-19 15:50:26 +07:00
|
|
|
dentry->d_inode->i_private = priv;
|
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
int oprofilefs_create_ulong(struct super_block *sb, struct dentry *root,
|
|
|
|
char const *name, unsigned long *val)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2010-08-19 15:50:26 +07:00
|
|
|
return __oprofilefs_create_file(sb, root, name,
|
|
|
|
&ulong_fops, 0644, val);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
int oprofilefs_create_ro_ulong(struct super_block *sb, struct dentry *root,
|
|
|
|
char const *name, unsigned long *val)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2010-08-19 15:50:26 +07:00
|
|
|
return __oprofilefs_create_file(sb, root, name,
|
|
|
|
&ulong_ro_fops, 0444, val);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-05 22:12:36 +07:00
|
|
|
atomic_t *val = file->private_data;
|
2005-04-17 05:20:36 +07:00
|
|
|
return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
|
|
|
|
}
|
2008-10-16 20:01:40 +07:00
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2007-02-12 15:55:34 +07:00
|
|
|
static const struct file_operations atomic_ro_fops = {
|
2005-04-17 05:20:36 +07:00
|
|
|
.read = atomic_read_file,
|
|
|
|
.open = default_open,
|
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-15 23:52:59 +07:00
|
|
|
.llseek = default_llseek,
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
2008-10-16 20:01:40 +07:00
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
int oprofilefs_create_ro_atomic(struct super_block *sb, struct dentry *root,
|
|
|
|
char const *name, atomic_t *val)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2010-08-19 15:50:26 +07:00
|
|
|
return __oprofilefs_create_file(sb, root, name,
|
|
|
|
&atomic_ro_fops, 0444, val);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2008-10-16 20:01:40 +07:00
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
int oprofilefs_create_file(struct super_block *sb, struct dentry *root,
|
|
|
|
char const *name, const struct file_operations *fops)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2010-08-19 15:50:26 +07:00
|
|
|
return __oprofilefs_create_file(sb, root, name, fops, 0644, NULL);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
int oprofilefs_create_file_perm(struct super_block *sb, struct dentry *root,
|
|
|
|
char const *name, const struct file_operations *fops, int perm)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2010-08-19 15:50:26 +07:00
|
|
|
return __oprofilefs_create_file(sb, root, name, fops, perm, NULL);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
struct dentry *oprofilefs_mkdir(struct super_block *sb,
|
|
|
|
struct dentry *root, char const *name)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-05 22:12:36 +07:00
|
|
|
struct dentry *dentry;
|
|
|
|
struct inode *inode;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
dentry = d_alloc_name(root, name);
|
|
|
|
if (!dentry)
|
|
|
|
return NULL;
|
|
|
|
inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
|
|
|
|
if (!inode) {
|
|
|
|
dput(dentry);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
inode->i_op = &simple_dir_inode_operations;
|
|
|
|
inode->i_fop = &simple_dir_operations;
|
|
|
|
d_add(dentry, inode);
|
|
|
|
return dentry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 22:12:36 +07:00
|
|
|
static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-09-05 22:12:36 +07:00
|
|
|
struct inode *root_inode;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
sb->s_blocksize = PAGE_CACHE_SIZE;
|
|
|
|
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
|
|
|
|
sb->s_magic = OPROFILEFS_MAGIC;
|
|
|
|
sb->s_op = &s_ops;
|
|
|
|
sb->s_time_gran = 1;
|
|
|
|
|
|
|
|
root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
|
|
|
|
if (!root_inode)
|
|
|
|
return -ENOMEM;
|
|
|
|
root_inode->i_op = &simple_dir_inode_operations;
|
|
|
|
root_inode->i_fop = &simple_dir_operations;
|
2012-02-13 10:08:01 +07:00
|
|
|
sb->s_root = d_make_root(root_inode);
|
|
|
|
if (!sb->s_root)
|
2005-04-17 05:20:36 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-02-13 10:08:01 +07:00
|
|
|
oprofile_create_files(sb, sb->s_root);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
// FIXME: verify kill_litter_super removes our dentries
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-25 04:48:30 +07:00
|
|
|
static struct dentry *oprofilefs_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *data)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2010-07-25 04:48:30 +07:00
|
|
|
return mount_single(fs_type, flags, data, oprofilefs_fill_super);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct file_system_type oprofilefs_type = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "oprofilefs",
|
2010-07-25 04:48:30 +07:00
|
|
|
.mount = oprofilefs_mount,
|
2005-04-17 05:20:36 +07:00
|
|
|
.kill_sb = kill_litter_super,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int __init oprofilefs_register(void)
|
|
|
|
{
|
|
|
|
return register_filesystem(&oprofilefs_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void __exit oprofilefs_unregister(void)
|
|
|
|
{
|
|
|
|
unregister_filesystem(&oprofilefs_type);
|
|
|
|
}
|