[PATCH] relayfs: decouple buffer creation from inode creation
[linux-2.6/kmemtrace.git] / fs / relayfs / inode.c
blob379e07cd2b34c3583daa8c004416d60c08f13932
1 /*
2 * VFS-related code for RelayFS, a high-speed data relay filesystem.
4 * Copyright (C) 2003-2005 - Tom Zanussi <zanussi@us.ibm.com>, IBM Corp
5 * Copyright (C) 2003-2005 - Karim Yaghmour <karim@opersys.com>
7 * Based on ramfs, Copyright (C) 2002 - Linus Torvalds
9 * This file is released under the GPL.
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/mount.h>
15 #include <linux/pagemap.h>
16 #include <linux/init.h>
17 #include <linux/string.h>
18 #include <linux/backing-dev.h>
19 #include <linux/namei.h>
20 #include <linux/poll.h>
21 #include <linux/relayfs_fs.h>
22 #include "relay.h"
23 #include "buffers.h"
25 #define RELAYFS_MAGIC 0xF0B4A981
27 static struct vfsmount * relayfs_mount;
28 static int relayfs_mount_count;
29 static kmem_cache_t * relayfs_inode_cachep;
31 static struct backing_dev_info relayfs_backing_dev_info = {
32 .ra_pages = 0, /* No readahead */
33 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
36 static struct inode *relayfs_get_inode(struct super_block *sb, int mode,
37 void *data)
39 struct inode *inode;
41 inode = new_inode(sb);
42 if (!inode)
43 return NULL;
45 inode->i_mode = mode;
46 inode->i_uid = 0;
47 inode->i_gid = 0;
48 inode->i_blksize = PAGE_CACHE_SIZE;
49 inode->i_blocks = 0;
50 inode->i_mapping->backing_dev_info = &relayfs_backing_dev_info;
51 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
52 switch (mode & S_IFMT) {
53 case S_IFREG:
54 inode->i_fop = &relayfs_file_operations;
55 RELAYFS_I(inode)->buf = data;
56 break;
57 case S_IFDIR:
58 inode->i_op = &simple_dir_inode_operations;
59 inode->i_fop = &simple_dir_operations;
61 /* directory inodes start off with i_nlink == 2 (for "." entry) */
62 inode->i_nlink++;
63 break;
64 default:
65 break;
68 return inode;
71 /**
72 * relayfs_create_entry - create a relayfs directory or file
73 * @name: the name of the file to create
74 * @parent: parent directory
75 * @mode: mode
76 * @data: user-associated data for this file
78 * Returns the new dentry, NULL on failure
80 * Creates a file or directory with the specifed permissions.
82 static struct dentry *relayfs_create_entry(const char *name,
83 struct dentry *parent,
84 int mode,
85 void *data)
87 struct dentry *d;
88 struct inode *inode;
89 int error = 0;
91 BUG_ON(!name || !(S_ISREG(mode) || S_ISDIR(mode)));
93 error = simple_pin_fs("relayfs", &relayfs_mount, &relayfs_mount_count);
94 if (error) {
95 printk(KERN_ERR "Couldn't mount relayfs: errcode %d\n", error);
96 return NULL;
99 if (!parent && relayfs_mount && relayfs_mount->mnt_sb)
100 parent = relayfs_mount->mnt_sb->s_root;
102 if (!parent) {
103 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
104 return NULL;
107 parent = dget(parent);
108 down(&parent->d_inode->i_sem);
109 d = lookup_one_len(name, parent, strlen(name));
110 if (IS_ERR(d)) {
111 d = NULL;
112 goto release_mount;
115 if (d->d_inode) {
116 d = NULL;
117 goto release_mount;
120 inode = relayfs_get_inode(parent->d_inode->i_sb, mode, data);
121 if (!inode) {
122 d = NULL;
123 goto release_mount;
126 d_instantiate(d, inode);
127 dget(d); /* Extra count - pin the dentry in core */
129 if (S_ISDIR(mode))
130 parent->d_inode->i_nlink++;
132 goto exit;
134 release_mount:
135 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
137 exit:
138 up(&parent->d_inode->i_sem);
139 dput(parent);
140 return d;
144 * relayfs_create_file - create a file in the relay filesystem
145 * @name: the name of the file to create
146 * @parent: parent directory
147 * @mode: mode, if not specied the default perms are used
148 * @data: user-associated data for this file
150 * Returns file dentry if successful, NULL otherwise.
152 * The file will be created user r on behalf of current user.
154 struct dentry *relayfs_create_file(const char *name, struct dentry *parent,
155 int mode, void *data)
157 if (!mode)
158 mode = S_IRUSR;
159 mode = (mode & S_IALLUGO) | S_IFREG;
161 return relayfs_create_entry(name, parent, mode, data);
165 * relayfs_create_dir - create a directory in the relay filesystem
166 * @name: the name of the directory to create
167 * @parent: parent directory, NULL if parent should be fs root
169 * Returns directory dentry if successful, NULL otherwise.
171 * The directory will be created world rwx on behalf of current user.
173 struct dentry *relayfs_create_dir(const char *name, struct dentry *parent)
175 int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
176 return relayfs_create_entry(name, parent, mode, NULL);
180 * relayfs_remove - remove a file or directory in the relay filesystem
181 * @dentry: file or directory dentry
183 * Returns 0 if successful, negative otherwise.
185 int relayfs_remove(struct dentry *dentry)
187 struct dentry *parent;
188 int error = 0;
190 if (!dentry)
191 return -EINVAL;
192 parent = dentry->d_parent;
193 if (!parent)
194 return -EINVAL;
196 parent = dget(parent);
197 down(&parent->d_inode->i_sem);
198 if (dentry->d_inode) {
199 if (S_ISDIR(dentry->d_inode->i_mode))
200 error = simple_rmdir(parent->d_inode, dentry);
201 else
202 error = simple_unlink(parent->d_inode, dentry);
203 if (!error)
204 d_delete(dentry);
206 if (!error)
207 dput(dentry);
208 up(&parent->d_inode->i_sem);
209 dput(parent);
211 if (!error)
212 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
214 return error;
218 * relayfs_remove_dir - remove a directory in the relay filesystem
219 * @dentry: directory dentry
221 * Returns 0 if successful, negative otherwise.
223 int relayfs_remove_dir(struct dentry *dentry)
225 return relayfs_remove(dentry);
229 * relayfs_open - open file op for relayfs files
230 * @inode: the inode
231 * @filp: the file
233 * Increments the channel buffer refcount.
235 static int relayfs_open(struct inode *inode, struct file *filp)
237 struct rchan_buf *buf = RELAYFS_I(inode)->buf;
238 kref_get(&buf->kref);
240 return 0;
244 * relayfs_mmap - mmap file op for relayfs files
245 * @filp: the file
246 * @vma: the vma describing what to map
248 * Calls upon relay_mmap_buf to map the file into user space.
250 static int relayfs_mmap(struct file *filp, struct vm_area_struct *vma)
252 struct inode *inode = filp->f_dentry->d_inode;
253 return relay_mmap_buf(RELAYFS_I(inode)->buf, vma);
257 * relayfs_poll - poll file op for relayfs files
258 * @filp: the file
259 * @wait: poll table
261 * Poll implemention.
263 static unsigned int relayfs_poll(struct file *filp, poll_table *wait)
265 unsigned int mask = 0;
266 struct inode *inode = filp->f_dentry->d_inode;
267 struct rchan_buf *buf = RELAYFS_I(inode)->buf;
269 if (buf->finalized)
270 return POLLERR;
272 if (filp->f_mode & FMODE_READ) {
273 poll_wait(filp, &buf->read_wait, wait);
274 if (!relay_buf_empty(buf))
275 mask |= POLLIN | POLLRDNORM;
278 return mask;
282 * relayfs_release - release file op for relayfs files
283 * @inode: the inode
284 * @filp: the file
286 * Decrements the channel refcount, as the filesystem is
287 * no longer using it.
289 static int relayfs_release(struct inode *inode, struct file *filp)
291 struct rchan_buf *buf = RELAYFS_I(inode)->buf;
292 kref_put(&buf->kref, relay_remove_buf);
294 return 0;
298 * relayfs_read_consume - update the consumed count for the buffer
300 static void relayfs_read_consume(struct rchan_buf *buf,
301 size_t read_pos,
302 size_t bytes_consumed)
304 size_t subbuf_size = buf->chan->subbuf_size;
305 size_t n_subbufs = buf->chan->n_subbufs;
306 size_t read_subbuf;
308 if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
309 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
310 buf->bytes_consumed = 0;
313 buf->bytes_consumed += bytes_consumed;
314 read_subbuf = read_pos / buf->chan->subbuf_size;
315 if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
316 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
317 (buf->offset == subbuf_size))
318 return;
319 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
320 buf->bytes_consumed = 0;
325 * relayfs_read_avail - boolean, are there unconsumed bytes available?
327 static int relayfs_read_avail(struct rchan_buf *buf, size_t read_pos)
329 size_t bytes_produced, bytes_consumed, write_offset;
330 size_t subbuf_size = buf->chan->subbuf_size;
331 size_t n_subbufs = buf->chan->n_subbufs;
332 size_t produced = buf->subbufs_produced % n_subbufs;
333 size_t consumed = buf->subbufs_consumed % n_subbufs;
335 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
337 if (consumed > produced) {
338 if ((produced > n_subbufs) &&
339 (produced + n_subbufs - consumed <= n_subbufs))
340 produced += n_subbufs;
341 } else if (consumed == produced) {
342 if (buf->offset > subbuf_size) {
343 produced += n_subbufs;
344 if (buf->subbufs_produced == buf->subbufs_consumed)
345 consumed += n_subbufs;
349 if (buf->offset > subbuf_size)
350 bytes_produced = (produced - 1) * subbuf_size + write_offset;
351 else
352 bytes_produced = produced * subbuf_size + write_offset;
353 bytes_consumed = consumed * subbuf_size + buf->bytes_consumed;
355 if (bytes_produced == bytes_consumed)
356 return 0;
358 relayfs_read_consume(buf, read_pos, 0);
360 return 1;
364 * relayfs_read_subbuf_avail - return bytes available in sub-buffer
366 static size_t relayfs_read_subbuf_avail(size_t read_pos,
367 struct rchan_buf *buf)
369 size_t padding, avail = 0;
370 size_t read_subbuf, read_offset, write_subbuf, write_offset;
371 size_t subbuf_size = buf->chan->subbuf_size;
373 write_subbuf = (buf->data - buf->start) / subbuf_size;
374 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
375 read_subbuf = read_pos / subbuf_size;
376 read_offset = read_pos % subbuf_size;
377 padding = buf->padding[read_subbuf];
379 if (read_subbuf == write_subbuf) {
380 if (read_offset + padding < write_offset)
381 avail = write_offset - (read_offset + padding);
382 } else
383 avail = (subbuf_size - padding) - read_offset;
385 return avail;
389 * relayfs_read_start_pos - find the first available byte to read
391 * If the read_pos is in the middle of padding, return the
392 * position of the first actually available byte, otherwise
393 * return the original value.
395 static size_t relayfs_read_start_pos(size_t read_pos,
396 struct rchan_buf *buf)
398 size_t read_subbuf, padding, padding_start, padding_end;
399 size_t subbuf_size = buf->chan->subbuf_size;
400 size_t n_subbufs = buf->chan->n_subbufs;
402 read_subbuf = read_pos / subbuf_size;
403 padding = buf->padding[read_subbuf];
404 padding_start = (read_subbuf + 1) * subbuf_size - padding;
405 padding_end = (read_subbuf + 1) * subbuf_size;
406 if (read_pos >= padding_start && read_pos < padding_end) {
407 read_subbuf = (read_subbuf + 1) % n_subbufs;
408 read_pos = read_subbuf * subbuf_size;
411 return read_pos;
415 * relayfs_read_end_pos - return the new read position
417 static size_t relayfs_read_end_pos(struct rchan_buf *buf,
418 size_t read_pos,
419 size_t count)
421 size_t read_subbuf, padding, end_pos;
422 size_t subbuf_size = buf->chan->subbuf_size;
423 size_t n_subbufs = buf->chan->n_subbufs;
425 read_subbuf = read_pos / subbuf_size;
426 padding = buf->padding[read_subbuf];
427 if (read_pos % subbuf_size + count + padding == subbuf_size)
428 end_pos = (read_subbuf + 1) * subbuf_size;
429 else
430 end_pos = read_pos + count;
431 if (end_pos >= subbuf_size * n_subbufs)
432 end_pos = 0;
434 return end_pos;
438 * relayfs_read - read file op for relayfs files
439 * @filp: the file
440 * @buffer: the userspace buffer
441 * @count: number of bytes to read
442 * @ppos: position to read from
444 * Reads count bytes or the number of bytes available in the
445 * current sub-buffer being read, whichever is smaller.
447 static ssize_t relayfs_read(struct file *filp,
448 char __user *buffer,
449 size_t count,
450 loff_t *ppos)
452 struct inode *inode = filp->f_dentry->d_inode;
453 struct rchan_buf *buf = RELAYFS_I(inode)->buf;
454 size_t read_start, avail;
455 ssize_t ret = 0;
456 void *from;
458 down(&inode->i_sem);
459 if(!relayfs_read_avail(buf, *ppos))
460 goto out;
462 read_start = relayfs_read_start_pos(*ppos, buf);
463 avail = relayfs_read_subbuf_avail(read_start, buf);
464 if (!avail)
465 goto out;
467 from = buf->start + read_start;
468 ret = count = min(count, avail);
469 if (copy_to_user(buffer, from, count)) {
470 ret = -EFAULT;
471 goto out;
473 relayfs_read_consume(buf, read_start, count);
474 *ppos = relayfs_read_end_pos(buf, read_start, count);
475 out:
476 up(&inode->i_sem);
477 return ret;
481 * relayfs alloc_inode() implementation
483 static struct inode *relayfs_alloc_inode(struct super_block *sb)
485 struct relayfs_inode_info *p = kmem_cache_alloc(relayfs_inode_cachep, SLAB_KERNEL);
486 if (!p)
487 return NULL;
488 p->buf = NULL;
490 return &p->vfs_inode;
494 * relayfs destroy_inode() implementation
496 static void relayfs_destroy_inode(struct inode *inode)
498 kmem_cache_free(relayfs_inode_cachep, RELAYFS_I(inode));
501 static void init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
503 struct relayfs_inode_info *i = p;
504 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
505 inode_init_once(&i->vfs_inode);
508 struct file_operations relayfs_file_operations = {
509 .open = relayfs_open,
510 .poll = relayfs_poll,
511 .mmap = relayfs_mmap,
512 .read = relayfs_read,
513 .llseek = no_llseek,
514 .release = relayfs_release,
517 static struct super_operations relayfs_ops = {
518 .statfs = simple_statfs,
519 .drop_inode = generic_delete_inode,
520 .alloc_inode = relayfs_alloc_inode,
521 .destroy_inode = relayfs_destroy_inode,
524 static int relayfs_fill_super(struct super_block * sb, void * data, int silent)
526 struct inode *inode;
527 struct dentry *root;
528 int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
530 sb->s_blocksize = PAGE_CACHE_SIZE;
531 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
532 sb->s_magic = RELAYFS_MAGIC;
533 sb->s_op = &relayfs_ops;
534 inode = relayfs_get_inode(sb, mode, NULL);
536 if (!inode)
537 return -ENOMEM;
539 root = d_alloc_root(inode);
540 if (!root) {
541 iput(inode);
542 return -ENOMEM;
544 sb->s_root = root;
546 return 0;
549 static struct super_block * relayfs_get_sb(struct file_system_type *fs_type,
550 int flags, const char *dev_name,
551 void *data)
553 return get_sb_single(fs_type, flags, data, relayfs_fill_super);
556 static struct file_system_type relayfs_fs_type = {
557 .owner = THIS_MODULE,
558 .name = "relayfs",
559 .get_sb = relayfs_get_sb,
560 .kill_sb = kill_litter_super,
563 static int __init init_relayfs_fs(void)
565 int err;
567 relayfs_inode_cachep = kmem_cache_create("relayfs_inode_cache",
568 sizeof(struct relayfs_inode_info), 0,
569 0, init_once, NULL);
570 if (!relayfs_inode_cachep)
571 return -ENOMEM;
573 err = register_filesystem(&relayfs_fs_type);
574 if (err)
575 kmem_cache_destroy(relayfs_inode_cachep);
577 return err;
580 static void __exit exit_relayfs_fs(void)
582 unregister_filesystem(&relayfs_fs_type);
583 kmem_cache_destroy(relayfs_inode_cachep);
586 module_init(init_relayfs_fs)
587 module_exit(exit_relayfs_fs)
589 EXPORT_SYMBOL_GPL(relayfs_file_operations);
590 EXPORT_SYMBOL_GPL(relayfs_create_dir);
591 EXPORT_SYMBOL_GPL(relayfs_remove_dir);
593 MODULE_AUTHOR("Tom Zanussi <zanussi@us.ibm.com> and Karim Yaghmour <karim@opersys.com>");
594 MODULE_DESCRIPTION("Relay Filesystem");
595 MODULE_LICENSE("GPL");