2005-04-17 02:20:36 +04: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>
|
2016-12-24 22:46:01 +03:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
#include "oprof.h"
|
|
|
|
|
|
|
|
#define OPROFILEFS_MAGIC 0x6f70726f
|
|
|
|
|
2009-07-25 18:18:34 +04:00
|
|
|
DEFINE_RAW_SPINLOCK(oprofilefs_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-09-05 19:12:36 +04:00
|
|
|
static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-09-05 19:12:36 +04:00
|
|
|
struct inode *inode = new_inode(sb);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (inode) {
|
2010-10-23 19:19:54 +04:00
|
|
|
inode->i_ino = get_next_ino();
|
2005-04-17 02:20:36 +04:00
|
|
|
inode->i_mode = mode;
|
2016-09-14 17:48:04 +03:00
|
|
|
inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-22 04:01:09 +04:00
|
|
|
static const struct super_operations s_ops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.statfs = simple_statfs,
|
|
|
|
.drop_inode = generic_delete_inode,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-09-05 19:12:36 +04:00
|
|
|
ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return simple_read_from_buffer(buf, count, offset, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define TMPBUFSIZE 50
|
|
|
|
|
2008-09-05 19:12:36 +04:00
|
|
|
ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 02:20:36 +04: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 19:38:30 +04: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 19:12:36 +04:00
|
|
|
int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
char tmpbuf[TMPBUFSIZE];
|
2007-03-28 20:12:34 +04:00
|
|
|
unsigned long flags;
|
2005-04-17 02:20:36 +04: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 18:18:34 +04:00
|
|
|
raw_spin_lock_irqsave(&oprofilefs_lock, flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
*val = simple_strtoul(tmpbuf, NULL, 0);
|
2009-07-25 18:18:34 +04:00
|
|
|
raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
|
2011-12-19 19:38:30 +04:00
|
|
|
return count;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 19:12:36 +04:00
|
|
|
static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-09-05 19:12:36 +04:00
|
|
|
unsigned long *val = file->private_data;
|
2005-04-17 02:20:36 +04:00
|
|
|
return oprofilefs_ulong_to_user(*val, buf, count, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 19:12:36 +04:00
|
|
|
static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2010-10-04 23:09:36 +04:00
|
|
|
unsigned long value;
|
2005-04-17 02:20:36 +04:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (*offset)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-10-04 23:09:36 +04:00
|
|
|
retval = oprofilefs_ulong_from_user(&value, buf, count);
|
2011-12-19 19:38:30 +04:00
|
|
|
if (retval <= 0)
|
2010-10-04 23:09:36 +04:00
|
|
|
return retval;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-10-04 23:09:36 +04:00
|
|
|
retval = oprofile_set_ulong(file->private_data, value);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
2010-10-04 23:09:36 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-12 11:55:34 +03:00
|
|
|
static const struct file_operations ulong_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.read = ulong_read_file,
|
|
|
|
.write = ulong_write_file,
|
2012-04-06 01:25:11 +04:00
|
|
|
.open = simple_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 20:52:59 +04:00
|
|
|
.llseek = default_llseek,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-02-12 11:55:34 +03:00
|
|
|
static const struct file_operations ulong_ro_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.read = ulong_read_file,
|
2012-04-06 01:25:11 +04:00
|
|
|
.open = simple_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 20:52:59 +04:00
|
|
|
.llseek = default_llseek,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-19 16:10:36 +04:00
|
|
|
static int __oprofilefs_create_file(struct dentry *root, char const *name,
|
|
|
|
const struct file_operations *fops, int perm, void *priv)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-09-05 19:12:36 +04:00
|
|
|
struct dentry *dentry;
|
|
|
|
struct inode *inode;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2018-03-11 00:40:33 +03:00
|
|
|
if (!root)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_lock(d_inode(root));
|
2005-04-17 02:20:36 +04:00
|
|
|
dentry = d_alloc_name(root, name);
|
2013-01-28 23:42:42 +04:00
|
|
|
if (!dentry) {
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_unlock(d_inode(root));
|
2010-08-19 12:50:26 +04:00
|
|
|
return -ENOMEM;
|
2013-01-28 23:42:42 +04:00
|
|
|
}
|
2013-07-19 16:10:36 +04:00
|
|
|
inode = oprofilefs_get_inode(root->d_sb, S_IFREG | perm);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (!inode) {
|
|
|
|
dput(dentry);
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_unlock(d_inode(root));
|
2010-08-19 12:50:26 +04:00
|
|
|
return -ENOMEM;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
inode->i_fop = fops;
|
2013-01-28 23:42:42 +04:00
|
|
|
inode->i_private = priv;
|
2005-04-17 02:20:36 +04:00
|
|
|
d_add(dentry, inode);
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_unlock(d_inode(root));
|
2010-08-19 12:50:26 +04:00
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 16:10:36 +04:00
|
|
|
int oprofilefs_create_ulong(struct dentry *root,
|
2008-09-05 19:12:36 +04:00
|
|
|
char const *name, unsigned long *val)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-07-19 16:10:36 +04:00
|
|
|
return __oprofilefs_create_file(root, name,
|
2010-08-19 12:50:26 +04:00
|
|
|
&ulong_fops, 0644, val);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 16:10:36 +04:00
|
|
|
int oprofilefs_create_ro_ulong(struct dentry *root,
|
2008-09-05 19:12:36 +04:00
|
|
|
char const *name, unsigned long *val)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-07-19 16:10:36 +04:00
|
|
|
return __oprofilefs_create_file(root, name,
|
2010-08-19 12:50:26 +04:00
|
|
|
&ulong_ro_fops, 0444, val);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 19:12:36 +04:00
|
|
|
static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-09-05 19:12:36 +04:00
|
|
|
atomic_t *val = file->private_data;
|
2005-04-17 02:20:36 +04:00
|
|
|
return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
|
|
|
|
}
|
2008-10-16 17:01:40 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2007-02-12 11:55:34 +03:00
|
|
|
static const struct file_operations atomic_ro_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.read = atomic_read_file,
|
2012-04-06 01:25:11 +04:00
|
|
|
.open = simple_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 20:52:59 +04:00
|
|
|
.llseek = default_llseek,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
2008-10-16 17:01:40 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-07-19 16:10:36 +04:00
|
|
|
int oprofilefs_create_ro_atomic(struct dentry *root,
|
2008-09-05 19:12:36 +04:00
|
|
|
char const *name, atomic_t *val)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-07-19 16:10:36 +04:00
|
|
|
return __oprofilefs_create_file(root, name,
|
2010-08-19 12:50:26 +04:00
|
|
|
&atomic_ro_fops, 0444, val);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2008-10-16 17:01:40 +04:00
|
|
|
|
2013-07-19 16:10:36 +04:00
|
|
|
int oprofilefs_create_file(struct dentry *root,
|
2008-09-05 19:12:36 +04:00
|
|
|
char const *name, const struct file_operations *fops)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-07-19 16:10:36 +04:00
|
|
|
return __oprofilefs_create_file(root, name, fops, 0644, NULL);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 16:10:36 +04:00
|
|
|
int oprofilefs_create_file_perm(struct dentry *root,
|
2008-09-05 19:12:36 +04:00
|
|
|
char const *name, const struct file_operations *fops, int perm)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-07-19 16:10:36 +04:00
|
|
|
return __oprofilefs_create_file(root, name, fops, perm, NULL);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 15:58:27 +04:00
|
|
|
struct dentry *oprofilefs_mkdir(struct dentry *parent, char const *name)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-09-05 19:12:36 +04:00
|
|
|
struct dentry *dentry;
|
|
|
|
struct inode *inode;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_lock(d_inode(parent));
|
2013-07-19 15:58:27 +04:00
|
|
|
dentry = d_alloc_name(parent, name);
|
2013-01-28 23:42:42 +04:00
|
|
|
if (!dentry) {
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_unlock(d_inode(parent));
|
2005-04-17 02:20:36 +04:00
|
|
|
return NULL;
|
2013-01-28 23:42:42 +04:00
|
|
|
}
|
2013-07-19 15:58:27 +04:00
|
|
|
inode = oprofilefs_get_inode(parent->d_sb, S_IFDIR | 0755);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (!inode) {
|
|
|
|
dput(dentry);
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_unlock(d_inode(parent));
|
2005-04-17 02:20:36 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
inode->i_op = &simple_dir_inode_operations;
|
|
|
|
inode->i_fop = &simple_dir_operations;
|
|
|
|
d_add(dentry, inode);
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_unlock(d_inode(parent));
|
2005-04-17 02:20:36 +04:00
|
|
|
return dentry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-05 19:12:36 +04:00
|
|
|
static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-09-05 19:12:36 +04:00
|
|
|
struct inode *root_inode;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
mm, fs: get rid of PAGE_CACHE_* and page_cache_{get,release} macros
PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} macros were introduced *long* time
ago with promise that one day it will be possible to implement page
cache with bigger chunks than PAGE_SIZE.
This promise never materialized. And unlikely will.
We have many places where PAGE_CACHE_SIZE assumed to be equal to
PAGE_SIZE. And it's constant source of confusion on whether
PAGE_CACHE_* or PAGE_* constant should be used in a particular case,
especially on the border between fs and mm.
Global switching to PAGE_CACHE_SIZE != PAGE_SIZE would cause to much
breakage to be doable.
Let's stop pretending that pages in page cache are special. They are
not.
The changes are pretty straight-forward:
- <foo> << (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- <foo> >> (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} -> PAGE_{SIZE,SHIFT,MASK,ALIGN};
- page_cache_get() -> get_page();
- page_cache_release() -> put_page();
This patch contains automated changes generated with coccinelle using
script below. For some reason, coccinelle doesn't patch header files.
I've called spatch for them manually.
The only adjustment after coccinelle is revert of changes to
PAGE_CAHCE_ALIGN definition: we are going to drop it later.
There are few places in the code where coccinelle didn't reach. I'll
fix them manually in a separate patch. Comments and documentation also
will be addressed with the separate patch.
virtual patch
@@
expression E;
@@
- E << (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
expression E;
@@
- E >> (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
@@
- PAGE_CACHE_SHIFT
+ PAGE_SHIFT
@@
@@
- PAGE_CACHE_SIZE
+ PAGE_SIZE
@@
@@
- PAGE_CACHE_MASK
+ PAGE_MASK
@@
expression E;
@@
- PAGE_CACHE_ALIGN(E)
+ PAGE_ALIGN(E)
@@
expression E;
@@
- page_cache_get(E)
+ get_page(E)
@@
expression E;
@@
- page_cache_release(E)
+ put_page(E)
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-04-01 15:29:47 +03:00
|
|
|
sb->s_blocksize = PAGE_SIZE;
|
|
|
|
sb->s_blocksize_bits = PAGE_SHIFT;
|
2005-04-17 02:20:36 +04:00
|
|
|
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 07:08:01 +04:00
|
|
|
sb->s_root = d_make_root(root_inode);
|
|
|
|
if (!sb->s_root)
|
2005-04-17 02:20:36 +04:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-07-19 15:47:29 +04:00
|
|
|
oprofile_create_files(sb->s_root);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
// FIXME: verify kill_litter_super removes our dentries
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-25 01:48:30 +04:00
|
|
|
static struct dentry *oprofilefs_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *data)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2010-07-25 01:48:30 +04:00
|
|
|
return mount_single(fs_type, flags, data, oprofilefs_fill_super);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct file_system_type oprofilefs_type = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "oprofilefs",
|
2010-07-25 01:48:30 +04:00
|
|
|
.mount = oprofilefs_mount,
|
2005-04-17 02:20:36 +04:00
|
|
|
.kill_sb = kill_litter_super,
|
|
|
|
};
|
2013-03-03 07:39:14 +04:00
|
|
|
MODULE_ALIAS_FS("oprofilefs");
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
int __init oprofilefs_register(void)
|
|
|
|
{
|
|
|
|
return register_filesystem(&oprofilefs_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void __exit oprofilefs_unregister(void)
|
|
|
|
{
|
|
|
|
unregister_filesystem(&oprofilefs_type);
|
|
|
|
}
|