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>
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>
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,
38 struct file_operations *fops,
43 inode = new_inode(sb);
50 inode->i_blksize = PAGE_CACHE_SIZE;
52 inode->i_mapping->backing_dev_info = &relayfs_backing_dev_info;
53 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
54 switch (mode & S_IFMT) {
58 inode->u.generic_ip = data;
61 inode->i_op = &simple_dir_inode_operations;
62 inode->i_fop = &simple_dir_operations;
64 /* directory inodes start off with i_nlink == 2 (for "." entry) */
75 * relayfs_create_entry - create a relayfs directory or file
76 * @name: the name of the file to create
77 * @parent: parent directory
79 * @fops: file operations to use for the file
80 * @data: user-associated data for this file
82 * Returns the new dentry, NULL on failure
84 * Creates a file or directory with the specifed permissions.
86 static struct dentry *relayfs_create_entry(const char *name,
87 struct dentry *parent,
89 struct file_operations *fops,
96 BUG_ON(!name || !(S_ISREG(mode) || S_ISDIR(mode)));
98 error = simple_pin_fs("relayfs", &relayfs_mount, &relayfs_mount_count);
100 printk(KERN_ERR "Couldn't mount relayfs: errcode %d\n", error);
104 if (!parent && relayfs_mount && relayfs_mount->mnt_sb)
105 parent = relayfs_mount->mnt_sb->s_root;
108 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
112 parent = dget(parent);
113 down(&parent->d_inode->i_sem);
114 d = lookup_one_len(name, parent, strlen(name));
125 inode = relayfs_get_inode(parent->d_inode->i_sb, mode, fops, data);
131 d_instantiate(d, inode);
132 dget(d); /* Extra count - pin the dentry in core */
135 parent->d_inode->i_nlink++;
140 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
143 up(&parent->d_inode->i_sem);
149 * relayfs_create_file - create a file in the relay filesystem
150 * @name: the name of the file to create
151 * @parent: parent directory
152 * @mode: mode, if not specied the default perms are used
153 * @fops: file operations to use for the file
154 * @data: user-associated data for this file
156 * Returns file dentry if successful, NULL otherwise.
158 * The file will be created user r on behalf of current user.
160 struct dentry *relayfs_create_file(const char *name,
161 struct dentry *parent,
163 struct file_operations *fops,
170 mode = (mode & S_IALLUGO) | S_IFREG;
172 return relayfs_create_entry(name, parent, mode, fops, data);
176 * relayfs_create_dir - create a directory in the relay filesystem
177 * @name: the name of the directory to create
178 * @parent: parent directory, NULL if parent should be fs root
180 * Returns directory dentry if successful, NULL otherwise.
182 * The directory will be created world rwx on behalf of current user.
184 struct dentry *relayfs_create_dir(const char *name, struct dentry *parent)
186 int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
187 return relayfs_create_entry(name, parent, mode, NULL, NULL);
191 * relayfs_remove - remove a file or directory in the relay filesystem
192 * @dentry: file or directory dentry
194 * Returns 0 if successful, negative otherwise.
196 int relayfs_remove(struct dentry *dentry)
198 struct dentry *parent;
203 parent = dentry->d_parent;
207 parent = dget(parent);
208 down(&parent->d_inode->i_sem);
209 if (dentry->d_inode) {
210 if (S_ISDIR(dentry->d_inode->i_mode))
211 error = simple_rmdir(parent->d_inode, dentry);
213 error = simple_unlink(parent->d_inode, dentry);
219 up(&parent->d_inode->i_sem);
223 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
229 * relayfs_remove_file - remove a file from relay filesystem
230 * @dentry: directory dentry
232 * Returns 0 if successful, negative otherwise.
234 int relayfs_remove_file(struct dentry *dentry)
236 return relayfs_remove(dentry);
240 * relayfs_remove_dir - remove a directory in the relay filesystem
241 * @dentry: directory dentry
243 * Returns 0 if successful, negative otherwise.
245 int relayfs_remove_dir(struct dentry *dentry)
247 return relayfs_remove(dentry);
251 * relayfs_open - open file op for relayfs files
255 * Increments the channel buffer refcount.
257 static int relayfs_open(struct inode *inode, struct file *filp)
259 struct rchan_buf *buf = inode->u.generic_ip;
260 kref_get(&buf->kref);
261 filp->private_data = buf;
267 * relayfs_mmap - mmap file op for relayfs files
269 * @vma: the vma describing what to map
271 * Calls upon relay_mmap_buf to map the file into user space.
273 static int relayfs_mmap(struct file *filp, struct vm_area_struct *vma)
275 struct rchan_buf *buf = filp->private_data;
276 return relay_mmap_buf(buf, vma);
280 * relayfs_poll - poll file op for relayfs files
286 static unsigned int relayfs_poll(struct file *filp, poll_table *wait)
288 unsigned int mask = 0;
289 struct rchan_buf *buf = filp->private_data;
294 if (filp->f_mode & FMODE_READ) {
295 poll_wait(filp, &buf->read_wait, wait);
296 if (!relay_buf_empty(buf))
297 mask |= POLLIN | POLLRDNORM;
304 * relayfs_release - release file op for relayfs files
308 * Decrements the channel refcount, as the filesystem is
309 * no longer using it.
311 static int relayfs_release(struct inode *inode, struct file *filp)
313 struct rchan_buf *buf = filp->private_data;
314 kref_put(&buf->kref, relay_remove_buf);
320 * relayfs_read_consume - update the consumed count for the buffer
322 static void relayfs_read_consume(struct rchan_buf *buf,
324 size_t bytes_consumed)
326 size_t subbuf_size = buf->chan->subbuf_size;
327 size_t n_subbufs = buf->chan->n_subbufs;
330 if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
331 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
332 buf->bytes_consumed = 0;
335 buf->bytes_consumed += bytes_consumed;
336 read_subbuf = read_pos / buf->chan->subbuf_size;
337 if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
338 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
339 (buf->offset == subbuf_size))
341 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
342 buf->bytes_consumed = 0;
347 * relayfs_read_avail - boolean, are there unconsumed bytes available?
349 static int relayfs_read_avail(struct rchan_buf *buf, size_t read_pos)
351 size_t bytes_produced, bytes_consumed, write_offset;
352 size_t subbuf_size = buf->chan->subbuf_size;
353 size_t n_subbufs = buf->chan->n_subbufs;
354 size_t produced = buf->subbufs_produced % n_subbufs;
355 size_t consumed = buf->subbufs_consumed % n_subbufs;
357 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
359 if (consumed > produced) {
360 if ((produced > n_subbufs) &&
361 (produced + n_subbufs - consumed <= n_subbufs))
362 produced += n_subbufs;
363 } else if (consumed == produced) {
364 if (buf->offset > subbuf_size) {
365 produced += n_subbufs;
366 if (buf->subbufs_produced == buf->subbufs_consumed)
367 consumed += n_subbufs;
371 if (buf->offset > subbuf_size)
372 bytes_produced = (produced - 1) * subbuf_size + write_offset;
374 bytes_produced = produced * subbuf_size + write_offset;
375 bytes_consumed = consumed * subbuf_size + buf->bytes_consumed;
377 if (bytes_produced == bytes_consumed)
380 relayfs_read_consume(buf, read_pos, 0);
386 * relayfs_read_subbuf_avail - return bytes available in sub-buffer
388 static size_t relayfs_read_subbuf_avail(size_t read_pos,
389 struct rchan_buf *buf)
391 size_t padding, avail = 0;
392 size_t read_subbuf, read_offset, write_subbuf, write_offset;
393 size_t subbuf_size = buf->chan->subbuf_size;
395 write_subbuf = (buf->data - buf->start) / subbuf_size;
396 write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
397 read_subbuf = read_pos / subbuf_size;
398 read_offset = read_pos % subbuf_size;
399 padding = buf->padding[read_subbuf];
401 if (read_subbuf == write_subbuf) {
402 if (read_offset + padding < write_offset)
403 avail = write_offset - (read_offset + padding);
405 avail = (subbuf_size - padding) - read_offset;
411 * relayfs_read_start_pos - find the first available byte to read
413 * If the read_pos is in the middle of padding, return the
414 * position of the first actually available byte, otherwise
415 * return the original value.
417 static size_t relayfs_read_start_pos(size_t read_pos,
418 struct rchan_buf *buf)
420 size_t read_subbuf, padding, padding_start, padding_end;
421 size_t subbuf_size = buf->chan->subbuf_size;
422 size_t n_subbufs = buf->chan->n_subbufs;
424 read_subbuf = read_pos / subbuf_size;
425 padding = buf->padding[read_subbuf];
426 padding_start = (read_subbuf + 1) * subbuf_size - padding;
427 padding_end = (read_subbuf + 1) * subbuf_size;
428 if (read_pos >= padding_start && read_pos < padding_end) {
429 read_subbuf = (read_subbuf + 1) % n_subbufs;
430 read_pos = read_subbuf * subbuf_size;
437 * relayfs_read_end_pos - return the new read position
439 static size_t relayfs_read_end_pos(struct rchan_buf *buf,
443 size_t read_subbuf, padding, end_pos;
444 size_t subbuf_size = buf->chan->subbuf_size;
445 size_t n_subbufs = buf->chan->n_subbufs;
447 read_subbuf = read_pos / subbuf_size;
448 padding = buf->padding[read_subbuf];
449 if (read_pos % subbuf_size + count + padding == subbuf_size)
450 end_pos = (read_subbuf + 1) * subbuf_size;
452 end_pos = read_pos + count;
453 if (end_pos >= subbuf_size * n_subbufs)
460 * relayfs_read - read file op for relayfs files
462 * @buffer: the userspace buffer
463 * @count: number of bytes to read
464 * @ppos: position to read from
466 * Reads count bytes or the number of bytes available in the
467 * current sub-buffer being read, whichever is smaller.
469 static ssize_t relayfs_read(struct file *filp,
474 struct rchan_buf *buf = filp->private_data;
475 struct inode *inode = filp->f_dentry->d_inode;
476 size_t read_start, avail;
481 if(!relayfs_read_avail(buf, *ppos))
484 read_start = relayfs_read_start_pos(*ppos, buf);
485 avail = relayfs_read_subbuf_avail(read_start, buf);
489 from = buf->start + read_start;
490 ret = count = min(count, avail);
491 if (copy_to_user(buffer, from, count)) {
495 relayfs_read_consume(buf, read_start, count);
496 *ppos = relayfs_read_end_pos(buf, read_start, count);
503 * relayfs alloc_inode() implementation
505 static struct inode *relayfs_alloc_inode(struct super_block *sb)
507 struct relayfs_inode_info *p = kmem_cache_alloc(relayfs_inode_cachep, SLAB_KERNEL);
512 return &p->vfs_inode;
516 * relayfs destroy_inode() implementation
518 static void relayfs_destroy_inode(struct inode *inode)
520 kmem_cache_free(relayfs_inode_cachep, RELAYFS_I(inode));
523 static void init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
525 struct relayfs_inode_info *i = p;
526 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
527 inode_init_once(&i->vfs_inode);
530 struct file_operations relayfs_file_operations = {
531 .open = relayfs_open,
532 .poll = relayfs_poll,
533 .mmap = relayfs_mmap,
534 .read = relayfs_read,
536 .release = relayfs_release,
539 static struct super_operations relayfs_ops = {
540 .statfs = simple_statfs,
541 .drop_inode = generic_delete_inode,
542 .alloc_inode = relayfs_alloc_inode,
543 .destroy_inode = relayfs_destroy_inode,
546 static int relayfs_fill_super(struct super_block * sb, void * data, int silent)
550 int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
552 sb->s_blocksize = PAGE_CACHE_SIZE;
553 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
554 sb->s_magic = RELAYFS_MAGIC;
555 sb->s_op = &relayfs_ops;
556 inode = relayfs_get_inode(sb, mode, NULL, NULL);
561 root = d_alloc_root(inode);
571 static struct super_block * relayfs_get_sb(struct file_system_type *fs_type,
572 int flags, const char *dev_name,
575 return get_sb_single(fs_type, flags, data, relayfs_fill_super);
578 static struct file_system_type relayfs_fs_type = {
579 .owner = THIS_MODULE,
581 .get_sb = relayfs_get_sb,
582 .kill_sb = kill_litter_super,
585 static int __init init_relayfs_fs(void)
589 relayfs_inode_cachep = kmem_cache_create("relayfs_inode_cache",
590 sizeof(struct relayfs_inode_info), 0,
592 if (!relayfs_inode_cachep)
595 err = register_filesystem(&relayfs_fs_type);
597 kmem_cache_destroy(relayfs_inode_cachep);
602 static void __exit exit_relayfs_fs(void)
604 unregister_filesystem(&relayfs_fs_type);
605 kmem_cache_destroy(relayfs_inode_cachep);
608 module_init(init_relayfs_fs)
609 module_exit(exit_relayfs_fs)
611 EXPORT_SYMBOL_GPL(relayfs_file_operations);
612 EXPORT_SYMBOL_GPL(relayfs_create_dir);
613 EXPORT_SYMBOL_GPL(relayfs_remove_dir);
614 EXPORT_SYMBOL_GPL(relayfs_create_file);
615 EXPORT_SYMBOL_GPL(relayfs_remove_file);
617 MODULE_AUTHOR("Tom Zanussi <zanussi@us.ibm.com> and Karim Yaghmour <karim@opersys.com>");
618 MODULE_DESCRIPTION("Relay Filesystem");
619 MODULE_LICENSE("GPL");