mirror of
https://github.com/FEX-Emu/linux.git
synced 2024-12-22 01:10:28 +00:00
6038f373a3
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>
300 lines
6.4 KiB
C
300 lines
6.4 KiB
C
/**
|
|
* @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
|
|
|
|
DEFINE_SPINLOCK(oprofilefs_lock);
|
|
|
|
static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
|
|
{
|
|
struct inode *inode = new_inode(sb);
|
|
|
|
if (inode) {
|
|
inode->i_mode = mode;
|
|
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
|
}
|
|
return inode;
|
|
}
|
|
|
|
|
|
static const struct super_operations s_ops = {
|
|
.statfs = simple_statfs,
|
|
.drop_inode = generic_delete_inode,
|
|
};
|
|
|
|
|
|
ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
|
|
{
|
|
return simple_read_from_buffer(buf, count, offset, str, strlen(str));
|
|
}
|
|
|
|
|
|
#define TMPBUFSIZE 50
|
|
|
|
ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
|
|
{
|
|
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);
|
|
}
|
|
|
|
|
|
int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
|
|
{
|
|
char tmpbuf[TMPBUFSIZE];
|
|
unsigned long flags;
|
|
|
|
if (!count)
|
|
return 0;
|
|
|
|
if (count > TMPBUFSIZE - 1)
|
|
return -EINVAL;
|
|
|
|
memset(tmpbuf, 0x0, TMPBUFSIZE);
|
|
|
|
if (copy_from_user(tmpbuf, buf, count))
|
|
return -EFAULT;
|
|
|
|
spin_lock_irqsave(&oprofilefs_lock, flags);
|
|
*val = simple_strtoul(tmpbuf, NULL, 0);
|
|
spin_unlock_irqrestore(&oprofilefs_lock, flags);
|
|
return 0;
|
|
}
|
|
|
|
|
|
static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
|
|
{
|
|
unsigned long *val = file->private_data;
|
|
return oprofilefs_ulong_to_user(*val, buf, count, offset);
|
|
}
|
|
|
|
|
|
static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
|
|
{
|
|
unsigned long *value = file->private_data;
|
|
int retval;
|
|
|
|
if (*offset)
|
|
return -EINVAL;
|
|
|
|
retval = oprofilefs_ulong_from_user(value, buf, count);
|
|
|
|
if (retval)
|
|
return retval;
|
|
return count;
|
|
}
|
|
|
|
|
|
static int default_open(struct inode *inode, struct file *filp)
|
|
{
|
|
if (inode->i_private)
|
|
filp->private_data = inode->i_private;
|
|
return 0;
|
|
}
|
|
|
|
|
|
static const struct file_operations ulong_fops = {
|
|
.read = ulong_read_file,
|
|
.write = ulong_write_file,
|
|
.open = default_open,
|
|
.llseek = default_llseek,
|
|
};
|
|
|
|
|
|
static const struct file_operations ulong_ro_fops = {
|
|
.read = ulong_read_file,
|
|
.open = default_open,
|
|
.llseek = default_llseek,
|
|
};
|
|
|
|
|
|
static struct dentry *__oprofilefs_create_file(struct super_block *sb,
|
|
struct dentry *root, char const *name, const struct file_operations *fops,
|
|
int perm)
|
|
{
|
|
struct dentry *dentry;
|
|
struct inode *inode;
|
|
|
|
dentry = d_alloc_name(root, name);
|
|
if (!dentry)
|
|
return NULL;
|
|
inode = oprofilefs_get_inode(sb, S_IFREG | perm);
|
|
if (!inode) {
|
|
dput(dentry);
|
|
return NULL;
|
|
}
|
|
inode->i_fop = fops;
|
|
d_add(dentry, inode);
|
|
return dentry;
|
|
}
|
|
|
|
|
|
int oprofilefs_create_ulong(struct super_block *sb, struct dentry *root,
|
|
char const *name, unsigned long *val)
|
|
{
|
|
struct dentry *d = __oprofilefs_create_file(sb, root, name,
|
|
&ulong_fops, 0644);
|
|
if (!d)
|
|
return -EFAULT;
|
|
|
|
d->d_inode->i_private = val;
|
|
return 0;
|
|
}
|
|
|
|
|
|
int oprofilefs_create_ro_ulong(struct super_block *sb, struct dentry *root,
|
|
char const *name, unsigned long *val)
|
|
{
|
|
struct dentry *d = __oprofilefs_create_file(sb, root, name,
|
|
&ulong_ro_fops, 0444);
|
|
if (!d)
|
|
return -EFAULT;
|
|
|
|
d->d_inode->i_private = val;
|
|
return 0;
|
|
}
|
|
|
|
|
|
static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
|
|
{
|
|
atomic_t *val = file->private_data;
|
|
return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
|
|
}
|
|
|
|
|
|
static const struct file_operations atomic_ro_fops = {
|
|
.read = atomic_read_file,
|
|
.open = default_open,
|
|
.llseek = default_llseek,
|
|
};
|
|
|
|
|
|
int oprofilefs_create_ro_atomic(struct super_block *sb, struct dentry *root,
|
|
char const *name, atomic_t *val)
|
|
{
|
|
struct dentry *d = __oprofilefs_create_file(sb, root, name,
|
|
&atomic_ro_fops, 0444);
|
|
if (!d)
|
|
return -EFAULT;
|
|
|
|
d->d_inode->i_private = val;
|
|
return 0;
|
|
}
|
|
|
|
|
|
int oprofilefs_create_file(struct super_block *sb, struct dentry *root,
|
|
char const *name, const struct file_operations *fops)
|
|
{
|
|
if (!__oprofilefs_create_file(sb, root, name, fops, 0644))
|
|
return -EFAULT;
|
|
return 0;
|
|
}
|
|
|
|
|
|
int oprofilefs_create_file_perm(struct super_block *sb, struct dentry *root,
|
|
char const *name, const struct file_operations *fops, int perm)
|
|
{
|
|
if (!__oprofilefs_create_file(sb, root, name, fops, perm))
|
|
return -EFAULT;
|
|
return 0;
|
|
}
|
|
|
|
|
|
struct dentry *oprofilefs_mkdir(struct super_block *sb,
|
|
struct dentry *root, char const *name)
|
|
{
|
|
struct dentry *dentry;
|
|
struct inode *inode;
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
|
|
{
|
|
struct inode *root_inode;
|
|
struct dentry *root_dentry;
|
|
|
|
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;
|
|
root_dentry = d_alloc_root(root_inode);
|
|
if (!root_dentry) {
|
|
iput(root_inode);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
sb->s_root = root_dentry;
|
|
|
|
oprofile_create_files(sb, root_dentry);
|
|
|
|
// FIXME: verify kill_litter_super removes our dentries
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int oprofilefs_get_sb(struct file_system_type *fs_type,
|
|
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
|
|
{
|
|
return get_sb_single(fs_type, flags, data, oprofilefs_fill_super, mnt);
|
|
}
|
|
|
|
|
|
static struct file_system_type oprofilefs_type = {
|
|
.owner = THIS_MODULE,
|
|
.name = "oprofilefs",
|
|
.get_sb = oprofilefs_get_sb,
|
|
.kill_sb = kill_litter_super,
|
|
};
|
|
|
|
|
|
int __init oprofilefs_register(void)
|
|
{
|
|
return register_filesystem(&oprofilefs_type);
|
|
}
|
|
|
|
|
|
void __exit oprofilefs_unregister(void)
|
|
{
|
|
unregister_filesystem(&oprofilefs_type);
|
|
}
|