File: [Development] / linux-2.6-xfs / fs / relayfs / Attic / inode.c (download)
Revision 1.2, Wed Sep 7 15:13:53 2005 UTC (12 years, 1 month ago) by nathans.longdrop.melbourne.sgi.com
Branch: MAIN
Changes since 1.1: +19 -9
lines
Relayfs rmdir ENOTEMPTY fix, ACKed by Tom Zanussi.
Merge of 2.6.x-xfs-melb:linux:23730a by kenmcd.
|
/*
* VFS-related code for RelayFS, a high-speed data relay filesystem.
*
* Copyright (C) 2003-2005 - Tom Zanussi <zanussi@us.ibm.com>, IBM Corp
* Copyright (C) 2003-2005 - Karim Yaghmour <karim@opersys.com>
*
* Based on ramfs, Copyright (C) 2002 - Linus Torvalds
*
* This file is released under the GPL.
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/backing-dev.h>
#include <linux/namei.h>
#include <linux/poll.h>
#include <linux/relayfs_fs.h>
#include "relay.h"
#include "buffers.h"
#define RELAYFS_MAGIC 0xF0B4A981
static struct vfsmount * relayfs_mount;
static int relayfs_mount_count;
static kmem_cache_t * relayfs_inode_cachep;
static struct backing_dev_info relayfs_backing_dev_info = {
.ra_pages = 0, /* No readahead */
.capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
};
static struct inode *relayfs_get_inode(struct super_block *sb, int mode,
struct rchan *chan)
{
struct rchan_buf *buf = NULL;
struct inode *inode;
if (S_ISREG(mode)) {
BUG_ON(!chan);
buf = relay_create_buf(chan);
if (!buf)
return NULL;
}
inode = new_inode(sb);
if (!inode) {
relay_destroy_buf(buf);
return NULL;
}
inode->i_mode = mode;
inode->i_uid = 0;
inode->i_gid = 0;
inode->i_blksize = PAGE_CACHE_SIZE;
inode->i_blocks = 0;
inode->i_mapping->backing_dev_info = &relayfs_backing_dev_info;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
switch (mode & S_IFMT) {
case S_IFREG:
inode->i_fop = &relayfs_file_operations;
RELAYFS_I(inode)->buf = buf;
break;
case S_IFDIR:
inode->i_op = &simple_dir_inode_operations;
inode->i_fop = &simple_dir_operations;
/* directory inodes start off with i_nlink == 2 (for "." entry) */
inode->i_nlink++;
break;
default:
break;
}
return inode;
}
/**
* relayfs_create_entry - create a relayfs directory or file
* @name: the name of the file to create
* @parent: parent directory
* @mode: mode
* @chan: relay channel associated with the file
*
* Returns the new dentry, NULL on failure
*
* Creates a file or directory with the specifed permissions.
*/
static struct dentry *relayfs_create_entry(const char *name,
struct dentry *parent,
int mode,
struct rchan *chan)
{
struct dentry *d;
struct inode *inode;
int error = 0;
BUG_ON(!name || !(S_ISREG(mode) || S_ISDIR(mode)));
error = simple_pin_fs("relayfs", &relayfs_mount, &relayfs_mount_count);
if (error) {
printk(KERN_ERR "Couldn't mount relayfs: errcode %d\n", error);
return NULL;
}
if (!parent && relayfs_mount && relayfs_mount->mnt_sb)
parent = relayfs_mount->mnt_sb->s_root;
if (!parent) {
simple_release_fs(&relayfs_mount, &relayfs_mount_count);
return NULL;
}
parent = dget(parent);
down(&parent->d_inode->i_sem);
d = lookup_one_len(name, parent, strlen(name));
if (IS_ERR(d)) {
d = NULL;
goto release_mount;
}
if (d->d_inode) {
d = NULL;
goto release_mount;
}
inode = relayfs_get_inode(parent->d_inode->i_sb, mode, chan);
if (!inode) {
d = NULL;
goto release_mount;
}
d_instantiate(d, inode);
dget(d); /* Extra count - pin the dentry in core */
if (S_ISDIR(mode))
parent->d_inode->i_nlink++;
goto exit;
release_mount:
simple_release_fs(&relayfs_mount, &relayfs_mount_count);
exit:
up(&parent->d_inode->i_sem);
dput(parent);
return d;
}
/**
* relayfs_create_file - create a file in the relay filesystem
* @name: the name of the file to create
* @parent: parent directory
* @mode: mode, if not specied the default perms are used
* @chan: channel associated with the file
*
* Returns file dentry if successful, NULL otherwise.
*
* The file will be created user r on behalf of current user.
*/
struct dentry *relayfs_create_file(const char *name, struct dentry *parent,
int mode, struct rchan *chan)
{
if (!mode)
mode = S_IRUSR;
mode = (mode & S_IALLUGO) | S_IFREG;
return relayfs_create_entry(name, parent, mode, chan);
}
/**
* relayfs_create_dir - create a directory in the relay filesystem
* @name: the name of the directory to create
* @parent: parent directory, NULL if parent should be fs root
*
* Returns directory dentry if successful, NULL otherwise.
*
* The directory will be created world rwx on behalf of current user.
*/
struct dentry *relayfs_create_dir(const char *name, struct dentry *parent)
{
int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
return relayfs_create_entry(name, parent, mode, NULL);
}
/**
* relayfs_remove - remove a file or directory in the relay filesystem
* @dentry: file or directory dentry
*
* Returns 0 if successful, negative otherwise.
*/
int relayfs_remove(struct dentry *dentry)
{
struct dentry *parent;
int error = 0;
if (!dentry)
return -EINVAL;
parent = dentry->d_parent;
if (!parent)
return -EINVAL;
parent = dget(parent);
down(&parent->d_inode->i_sem);
if (dentry->d_inode) {
if (S_ISDIR(dentry->d_inode->i_mode))
error = simple_rmdir(parent->d_inode, dentry);
else
error = simple_unlink(parent->d_inode, dentry);
if (!error)
d_delete(dentry);
}
if (!error)
dput(dentry);
up(&parent->d_inode->i_sem);
dput(parent);
if (!error)
simple_release_fs(&relayfs_mount, &relayfs_mount_count);
return error;
}
/**
* relayfs_remove_dir - remove a directory in the relay filesystem
* @dentry: directory dentry
*
* Returns 0 if successful, negative otherwise.
*/
int relayfs_remove_dir(struct dentry *dentry)
{
return relayfs_remove(dentry);
}
/**
* relayfs_open - open file op for relayfs files
* @inode: the inode
* @filp: the file
*
* Increments the channel buffer refcount.
*/
static int relayfs_open(struct inode *inode, struct file *filp)
{
struct rchan_buf *buf = RELAYFS_I(inode)->buf;
kref_get(&buf->kref);
return 0;
}
/**
* relayfs_mmap - mmap file op for relayfs files
* @filp: the file
* @vma: the vma describing what to map
*
* Calls upon relay_mmap_buf to map the file into user space.
*/
static int relayfs_mmap(struct file *filp, struct vm_area_struct *vma)
{
struct inode *inode = filp->f_dentry->d_inode;
return relay_mmap_buf(RELAYFS_I(inode)->buf, vma);
}
/**
* relayfs_poll - poll file op for relayfs files
* @filp: the file
* @wait: poll table
*
* Poll implemention.
*/
static unsigned int relayfs_poll(struct file *filp, poll_table *wait)
{
unsigned int mask = 0;
struct inode *inode = filp->f_dentry->d_inode;
struct rchan_buf *buf = RELAYFS_I(inode)->buf;
if (buf->finalized)
return POLLERR;
if (filp->f_mode & FMODE_READ) {
poll_wait(filp, &buf->read_wait, wait);
if (!relay_buf_empty(buf))
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
/**
* relayfs_release - release file op for relayfs files
* @inode: the inode
* @filp: the file
*
* Decrements the channel refcount, as the filesystem is
* no longer using it.
*/
static int relayfs_release(struct inode *inode, struct file *filp)
{
struct rchan_buf *buf = RELAYFS_I(inode)->buf;
kref_put(&buf->kref, relay_remove_buf);
return 0;
}
/**
* relayfs_read_start - find the first available byte to read
*
* If the read_pos is in the middle of padding, return the
* position of the first actually available byte, otherwise
* return the original value.
*/
static inline size_t relayfs_read_start(size_t read_pos,
size_t avail,
size_t start_subbuf,
struct rchan_buf *buf)
{
size_t read_subbuf, adj_read_subbuf;
size_t padding, padding_start, padding_end;
size_t subbuf_size = buf->chan->subbuf_size;
size_t n_subbufs = buf->chan->n_subbufs;
read_subbuf = read_pos / subbuf_size;
adj_read_subbuf = (read_subbuf + start_subbuf) % n_subbufs;
if ((read_subbuf + 1) * subbuf_size <= avail) {
padding = buf->padding[adj_read_subbuf];
padding_start = (read_subbuf + 1) * subbuf_size - padding;
padding_end = (read_subbuf + 1) * subbuf_size;
if (read_pos >= padding_start && read_pos < padding_end) {
read_subbuf = (read_subbuf + 1) % n_subbufs;
read_pos = read_subbuf * subbuf_size;
}
}
return read_pos;
}
/**
* relayfs_read_end - return the end of available bytes to read
*
* If the read_pos is in the middle of a full sub-buffer, return
* the padding-adjusted end of that sub-buffer, otherwise return
* the position after the last byte written to the buffer. At
* most, 1 sub-buffer can be read at a time.
*
*/
static inline size_t relayfs_read_end(size_t read_pos,
size_t avail,
size_t start_subbuf,
struct rchan_buf *buf)
{
size_t padding, read_endpos, buf_offset;
size_t read_subbuf, adj_read_subbuf;
size_t subbuf_size = buf->chan->subbuf_size;
size_t n_subbufs = buf->chan->n_subbufs;
buf_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
read_subbuf = read_pos / subbuf_size;
adj_read_subbuf = (read_subbuf + start_subbuf) % n_subbufs;
if ((read_subbuf + 1) * subbuf_size <= avail) {
padding = buf->padding[adj_read_subbuf];
read_endpos = (read_subbuf + 1) * subbuf_size - padding;
} else
read_endpos = read_subbuf * subbuf_size + buf_offset;
return read_endpos;
}
/**
* relayfs_read_avail - return total available along with buffer start
*
* Because buffers are circular, the 'beginning' of the buffer
* depends on where the buffer was last written. If the writer
* has cycled around the buffer, the beginning is defined to be
* the beginning of the sub-buffer following the last sub-buffer
* written to, otherwise it's the beginning of sub-buffer 0.
*
*/
static inline size_t relayfs_read_avail(struct rchan_buf *buf,
size_t *start_subbuf)
{
size_t avail, complete_subbufs, cur_subbuf, buf_offset;
size_t subbuf_size = buf->chan->subbuf_size;
size_t n_subbufs = buf->chan->n_subbufs;
buf_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
if (buf->subbufs_produced >= n_subbufs) {
complete_subbufs = n_subbufs - 1;
cur_subbuf = (buf->data - buf->start) / subbuf_size;
*start_subbuf = (cur_subbuf + 1) % n_subbufs;
} else {
complete_subbufs = buf->subbufs_produced;
*start_subbuf = 0;
}
avail = complete_subbufs * subbuf_size + buf_offset;
return avail;
}
/**
* relayfs_read - read file op for relayfs files
* @filp: the file
* @buffer: the userspace buffer
* @count: number of bytes to read
* @ppos: position to read from
*
* Reads count bytes or the number of bytes available in the
* current sub-buffer being read, whichever is smaller.
*
* NOTE: The results of reading a relayfs file which is currently
* being written to are undefined. This is because the buffer is
* circular and an active writer in the kernel could be
* overwriting the data currently being read. Therefore read()
* is mainly useful for reading the contents of a buffer after
* logging has completed.
*/
static ssize_t relayfs_read(struct file *filp,
char __user *buffer,
size_t count,
loff_t *ppos)
{
struct inode *inode = filp->f_dentry->d_inode;
struct rchan_buf *buf = RELAYFS_I(inode)->buf;
size_t read_start, read_end, avail, start_subbuf;
size_t buf_size = buf->chan->subbuf_size * buf->chan->n_subbufs;
void *from;
avail = relayfs_read_avail(buf, &start_subbuf);
if (*ppos >= avail)
return 0;
read_start = relayfs_read_start(*ppos, avail, start_subbuf, buf);
if (read_start == 0 && *ppos)
return 0;
read_end = relayfs_read_end(read_start, avail, start_subbuf, buf);
if (read_end == read_start)
return 0;
from = buf->start + start_subbuf * buf->chan->subbuf_size + read_start;
if (from >= buf->start + buf_size)
from -= buf_size;
count = min(count, read_end - read_start);
if (copy_to_user(buffer, from, count))
return -EFAULT;
*ppos = read_start + count;
return count;
}
/**
* relayfs alloc_inode() implementation
*/
static struct inode *relayfs_alloc_inode(struct super_block *sb)
{
struct relayfs_inode_info *p = kmem_cache_alloc(relayfs_inode_cachep, SLAB_KERNEL);
if (!p)
return NULL;
p->buf = NULL;
return &p->vfs_inode;
}
/**
* relayfs destroy_inode() implementation
*/
static void relayfs_destroy_inode(struct inode *inode)
{
if (RELAYFS_I(inode)->buf)
relay_destroy_buf(RELAYFS_I(inode)->buf);
kmem_cache_free(relayfs_inode_cachep, RELAYFS_I(inode));
}
static void init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
{
struct relayfs_inode_info *i = p;
if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
inode_init_once(&i->vfs_inode);
}
struct file_operations relayfs_file_operations = {
.open = relayfs_open,
.poll = relayfs_poll,
.mmap = relayfs_mmap,
.read = relayfs_read,
.release = relayfs_release,
};
static struct super_operations relayfs_ops = {
.statfs = simple_statfs,
.drop_inode = generic_delete_inode,
.alloc_inode = relayfs_alloc_inode,
.destroy_inode = relayfs_destroy_inode,
};
static int relayfs_fill_super(struct super_block * sb, void * data, int silent)
{
struct inode *inode;
struct dentry *root;
int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
sb->s_blocksize = PAGE_CACHE_SIZE;
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
sb->s_magic = RELAYFS_MAGIC;
sb->s_op = &relayfs_ops;
inode = relayfs_get_inode(sb, mode, NULL);
if (!inode)
return -ENOMEM;
root = d_alloc_root(inode);
if (!root) {
iput(inode);
return -ENOMEM;
}
sb->s_root = root;
return 0;
}
static struct super_block * relayfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name,
void *data)
{
return get_sb_single(fs_type, flags, data, relayfs_fill_super);
}
static struct file_system_type relayfs_fs_type = {
.owner = THIS_MODULE,
.name = "relayfs",
.get_sb = relayfs_get_sb,
.kill_sb = kill_litter_super,
};
static int __init init_relayfs_fs(void)
{
int err;
relayfs_inode_cachep = kmem_cache_create("relayfs_inode_cache",
sizeof(struct relayfs_inode_info), 0,
0, init_once, NULL);
if (!relayfs_inode_cachep)
return -ENOMEM;
err = register_filesystem(&relayfs_fs_type);
if (err)
kmem_cache_destroy(relayfs_inode_cachep);
return err;
}
static void __exit exit_relayfs_fs(void)
{
unregister_filesystem(&relayfs_fs_type);
kmem_cache_destroy(relayfs_inode_cachep);
}
module_init(init_relayfs_fs)
module_exit(exit_relayfs_fs)
EXPORT_SYMBOL_GPL(relayfs_file_operations);
EXPORT_SYMBOL_GPL(relayfs_create_dir);
EXPORT_SYMBOL_GPL(relayfs_remove_dir);
MODULE_AUTHOR("Tom Zanussi <zanussi@us.ibm.com> and Karim Yaghmour <karim@opersys.com>");
MODULE_DESCRIPTION("Relay Filesystem");
MODULE_LICENSE("GPL");