]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/relayfs/inode.c
[PATCH] relayfs: use generic_ip for private data
[mv-sheeva.git] / fs / relayfs / inode.c
1 /*
2  * VFS-related code for RelayFS, a high-speed data relay filesystem.
3  *
4  * Copyright (C) 2003-2005 - Tom Zanussi <zanussi@us.ibm.com>, IBM Corp
5  * Copyright (C) 2003-2005 - Karim Yaghmour <karim@opersys.com>
6  *
7  * Based on ramfs, Copyright (C) 2002 - Linus Torvalds
8  *
9  * This file is released under the GPL.
10  */
11
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"
24
25 #define RELAYFS_MAGIC                   0xF0B4A981
26
27 static struct vfsmount *                relayfs_mount;
28 static int                              relayfs_mount_count;
29 static kmem_cache_t *                   relayfs_inode_cachep;
30
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,
34 };
35
36 static struct inode *relayfs_get_inode(struct super_block *sb,
37                                        int mode,
38                                        struct file_operations *fops,
39                                        void *data)
40 {
41         struct inode *inode;
42
43         inode = new_inode(sb);
44         if (!inode)
45                 return NULL;
46
47         inode->i_mode = mode;
48         inode->i_uid = 0;
49         inode->i_gid = 0;
50         inode->i_blksize = PAGE_CACHE_SIZE;
51         inode->i_blocks = 0;
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) {
55         case S_IFREG:
56                 inode->i_fop = fops;
57                 if (data)
58                         inode->u.generic_ip = data;
59                 break;
60         case S_IFDIR:
61                 inode->i_op = &simple_dir_inode_operations;
62                 inode->i_fop = &simple_dir_operations;
63
64                 /* directory inodes start off with i_nlink == 2 (for "." entry) */
65                 inode->i_nlink++;
66                 break;
67         default:
68                 break;
69         }
70
71         return inode;
72 }
73
74 /**
75  *      relayfs_create_entry - create a relayfs directory or file
76  *      @name: the name of the file to create
77  *      @parent: parent directory
78  *      @mode: mode
79  *      @fops: file operations to use for the file
80  *      @data: user-associated data for this file
81  *
82  *      Returns the new dentry, NULL on failure
83  *
84  *      Creates a file or directory with the specifed permissions.
85  */
86 static struct dentry *relayfs_create_entry(const char *name,
87                                            struct dentry *parent,
88                                            int mode,
89                                            struct file_operations *fops,
90                                            void *data)
91 {
92         struct dentry *d;
93         struct inode *inode;
94         int error = 0;
95
96         BUG_ON(!name || !(S_ISREG(mode) || S_ISDIR(mode)));
97
98         error = simple_pin_fs("relayfs", &relayfs_mount, &relayfs_mount_count);
99         if (error) {
100                 printk(KERN_ERR "Couldn't mount relayfs: errcode %d\n", error);
101                 return NULL;
102         }
103
104         if (!parent && relayfs_mount && relayfs_mount->mnt_sb)
105                 parent = relayfs_mount->mnt_sb->s_root;
106
107         if (!parent) {
108                 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
109                 return NULL;
110         }
111
112         parent = dget(parent);
113         down(&parent->d_inode->i_sem);
114         d = lookup_one_len(name, parent, strlen(name));
115         if (IS_ERR(d)) {
116                 d = NULL;
117                 goto release_mount;
118         }
119
120         if (d->d_inode) {
121                 d = NULL;
122                 goto release_mount;
123         }
124
125         inode = relayfs_get_inode(parent->d_inode->i_sb, mode, fops, data);
126         if (!inode) {
127                 d = NULL;
128                 goto release_mount;
129         }
130
131         d_instantiate(d, inode);
132         dget(d);        /* Extra count - pin the dentry in core */
133
134         if (S_ISDIR(mode))
135                 parent->d_inode->i_nlink++;
136
137         goto exit;
138
139 release_mount:
140         simple_release_fs(&relayfs_mount, &relayfs_mount_count);
141
142 exit:
143         up(&parent->d_inode->i_sem);
144         dput(parent);
145         return d;
146 }
147
148 /**
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
155  *
156  *      Returns file dentry if successful, NULL otherwise.
157  *
158  *      The file will be created user r on behalf of current user.
159  */
160 struct dentry *relayfs_create_file(const char *name,
161                                    struct dentry *parent,
162                                    int mode,
163                                    struct file_operations *fops,
164                                    void *data)
165 {
166         BUG_ON(!fops);
167
168         if (!mode)
169                 mode = S_IRUSR;
170         mode = (mode & S_IALLUGO) | S_IFREG;
171
172         return relayfs_create_entry(name, parent, mode, fops, data);
173 }
174
175 /**
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
179  *
180  *      Returns directory dentry if successful, NULL otherwise.
181  *
182  *      The directory will be created world rwx on behalf of current user.
183  */
184 struct dentry *relayfs_create_dir(const char *name, struct dentry *parent)
185 {
186         int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
187         return relayfs_create_entry(name, parent, mode, NULL, NULL);
188 }
189
190 /**
191  *      relayfs_remove - remove a file or directory in the relay filesystem
192  *      @dentry: file or directory dentry
193  *
194  *      Returns 0 if successful, negative otherwise.
195  */
196 int relayfs_remove(struct dentry *dentry)
197 {
198         struct dentry *parent;
199         int error = 0;
200
201         if (!dentry)
202                 return -EINVAL;
203         parent = dentry->d_parent;
204         if (!parent)
205                 return -EINVAL;
206
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);
212                 else
213                         error = simple_unlink(parent->d_inode, dentry);
214                 if (!error)
215                         d_delete(dentry);
216         }
217         if (!error)
218                 dput(dentry);
219         up(&parent->d_inode->i_sem);
220         dput(parent);
221
222         if (!error)
223                 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
224
225         return error;
226 }
227
228 /**
229  *      relayfs_remove_file - remove a file from relay filesystem
230  *      @dentry: directory dentry
231  *
232  *      Returns 0 if successful, negative otherwise.
233  */
234 int relayfs_remove_file(struct dentry *dentry)
235 {
236         return relayfs_remove(dentry);
237 }
238
239 /**
240  *      relayfs_remove_dir - remove a directory in the relay filesystem
241  *      @dentry: directory dentry
242  *
243  *      Returns 0 if successful, negative otherwise.
244  */
245 int relayfs_remove_dir(struct dentry *dentry)
246 {
247         return relayfs_remove(dentry);
248 }
249
250 /**
251  *      relayfs_open - open file op for relayfs files
252  *      @inode: the inode
253  *      @filp: the file
254  *
255  *      Increments the channel buffer refcount.
256  */
257 static int relayfs_open(struct inode *inode, struct file *filp)
258 {
259         struct rchan_buf *buf = inode->u.generic_ip;
260         kref_get(&buf->kref);
261         filp->private_data = buf;
262
263         return 0;
264 }
265
266 /**
267  *      relayfs_mmap - mmap file op for relayfs files
268  *      @filp: the file
269  *      @vma: the vma describing what to map
270  *
271  *      Calls upon relay_mmap_buf to map the file into user space.
272  */
273 static int relayfs_mmap(struct file *filp, struct vm_area_struct *vma)
274 {
275         struct rchan_buf *buf = filp->private_data;
276         return relay_mmap_buf(buf, vma);
277 }
278
279 /**
280  *      relayfs_poll - poll file op for relayfs files
281  *      @filp: the file
282  *      @wait: poll table
283  *
284  *      Poll implemention.
285  */
286 static unsigned int relayfs_poll(struct file *filp, poll_table *wait)
287 {
288         unsigned int mask = 0;
289         struct rchan_buf *buf = filp->private_data;
290
291         if (buf->finalized)
292                 return POLLERR;
293
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;
298         }
299
300         return mask;
301 }
302
303 /**
304  *      relayfs_release - release file op for relayfs files
305  *      @inode: the inode
306  *      @filp: the file
307  *
308  *      Decrements the channel refcount, as the filesystem is
309  *      no longer using it.
310  */
311 static int relayfs_release(struct inode *inode, struct file *filp)
312 {
313         struct rchan_buf *buf = filp->private_data;
314         kref_put(&buf->kref, relay_remove_buf);
315
316         return 0;
317 }
318
319 /**
320  *      relayfs_read_consume - update the consumed count for the buffer
321  */
322 static void relayfs_read_consume(struct rchan_buf *buf,
323                                  size_t read_pos,
324                                  size_t bytes_consumed)
325 {
326         size_t subbuf_size = buf->chan->subbuf_size;
327         size_t n_subbufs = buf->chan->n_subbufs;
328         size_t read_subbuf;
329
330         if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
331                 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
332                 buf->bytes_consumed = 0;
333         }
334
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))
340                         return;
341                 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
342                 buf->bytes_consumed = 0;
343         }
344 }
345
346 /**
347  *      relayfs_read_avail - boolean, are there unconsumed bytes available?
348  */
349 static int relayfs_read_avail(struct rchan_buf *buf, size_t read_pos)
350 {
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;
356
357         write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
358
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;
368                 }
369         }
370
371         if (buf->offset > subbuf_size)
372                 bytes_produced = (produced - 1) * subbuf_size + write_offset;
373         else
374                 bytes_produced = produced * subbuf_size + write_offset;
375         bytes_consumed = consumed * subbuf_size + buf->bytes_consumed;
376
377         if (bytes_produced == bytes_consumed)
378                 return 0;
379
380         relayfs_read_consume(buf, read_pos, 0);
381
382         return 1;
383 }
384
385 /**
386  *      relayfs_read_subbuf_avail - return bytes available in sub-buffer
387  */
388 static size_t relayfs_read_subbuf_avail(size_t read_pos,
389                                         struct rchan_buf *buf)
390 {
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;
394
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];
400
401         if (read_subbuf == write_subbuf) {
402                 if (read_offset + padding < write_offset)
403                         avail = write_offset - (read_offset + padding);
404         } else
405                 avail = (subbuf_size - padding) - read_offset;
406
407         return avail;
408 }
409
410 /**
411  *      relayfs_read_start_pos - find the first available byte to read
412  *
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.
416  */
417 static size_t relayfs_read_start_pos(size_t read_pos,
418                                      struct rchan_buf *buf)
419 {
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;
423
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;
431         }
432
433         return read_pos;
434 }
435
436 /**
437  *      relayfs_read_end_pos - return the new read position
438  */
439 static size_t relayfs_read_end_pos(struct rchan_buf *buf,
440                                    size_t read_pos,
441                                    size_t count)
442 {
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;
446
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;
451         else
452                 end_pos = read_pos + count;
453         if (end_pos >= subbuf_size * n_subbufs)
454                 end_pos = 0;
455
456         return end_pos;
457 }
458
459 /**
460  *      relayfs_read - read file op for relayfs files
461  *      @filp: the file
462  *      @buffer: the userspace buffer
463  *      @count: number of bytes to read
464  *      @ppos: position to read from
465  *
466  *      Reads count bytes or the number of bytes available in the
467  *      current sub-buffer being read, whichever is smaller.
468  */
469 static ssize_t relayfs_read(struct file *filp,
470                             char __user *buffer,
471                             size_t count,
472                             loff_t *ppos)
473 {
474         struct rchan_buf *buf = filp->private_data;
475         struct inode *inode = filp->f_dentry->d_inode;
476         size_t read_start, avail;
477         ssize_t ret = 0;
478         void *from;
479
480         down(&inode->i_sem);
481         if(!relayfs_read_avail(buf, *ppos))
482                 goto out;
483
484         read_start = relayfs_read_start_pos(*ppos, buf);
485         avail = relayfs_read_subbuf_avail(read_start, buf);
486         if (!avail)
487                 goto out;
488
489         from = buf->start + read_start;
490         ret = count = min(count, avail);
491         if (copy_to_user(buffer, from, count)) {
492                 ret = -EFAULT;
493                 goto out;
494         }
495         relayfs_read_consume(buf, read_start, count);
496         *ppos = relayfs_read_end_pos(buf, read_start, count);
497 out:
498         up(&inode->i_sem);
499         return ret;
500 }
501
502 /**
503  *      relayfs alloc_inode() implementation
504  */
505 static struct inode *relayfs_alloc_inode(struct super_block *sb)
506 {
507         struct relayfs_inode_info *p = kmem_cache_alloc(relayfs_inode_cachep, SLAB_KERNEL);
508         if (!p)
509                 return NULL;
510         p->data = NULL;
511
512         return &p->vfs_inode;
513 }
514
515 /**
516  *      relayfs destroy_inode() implementation
517  */
518 static void relayfs_destroy_inode(struct inode *inode)
519 {
520         kmem_cache_free(relayfs_inode_cachep, RELAYFS_I(inode));
521 }
522
523 static void init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
524 {
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);
528 }
529
530 struct file_operations relayfs_file_operations = {
531         .open           = relayfs_open,
532         .poll           = relayfs_poll,
533         .mmap           = relayfs_mmap,
534         .read           = relayfs_read,
535         .llseek         = no_llseek,
536         .release        = relayfs_release,
537 };
538
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,
544 };
545
546 static int relayfs_fill_super(struct super_block * sb, void * data, int silent)
547 {
548         struct inode *inode;
549         struct dentry *root;
550         int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
551
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);
557
558         if (!inode)
559                 return -ENOMEM;
560
561         root = d_alloc_root(inode);
562         if (!root) {
563                 iput(inode);
564                 return -ENOMEM;
565         }
566         sb->s_root = root;
567
568         return 0;
569 }
570
571 static struct super_block * relayfs_get_sb(struct file_system_type *fs_type,
572                                            int flags, const char *dev_name,
573                                            void *data)
574 {
575         return get_sb_single(fs_type, flags, data, relayfs_fill_super);
576 }
577
578 static struct file_system_type relayfs_fs_type = {
579         .owner          = THIS_MODULE,
580         .name           = "relayfs",
581         .get_sb         = relayfs_get_sb,
582         .kill_sb        = kill_litter_super,
583 };
584
585 static int __init init_relayfs_fs(void)
586 {
587         int err;
588
589         relayfs_inode_cachep = kmem_cache_create("relayfs_inode_cache",
590                                 sizeof(struct relayfs_inode_info), 0,
591                                 0, init_once, NULL);
592         if (!relayfs_inode_cachep)
593                 return -ENOMEM;
594
595         err = register_filesystem(&relayfs_fs_type);
596         if (err)
597                 kmem_cache_destroy(relayfs_inode_cachep);
598
599         return err;
600 }
601
602 static void __exit exit_relayfs_fs(void)
603 {
604         unregister_filesystem(&relayfs_fs_type);
605         kmem_cache_destroy(relayfs_inode_cachep);
606 }
607
608 module_init(init_relayfs_fs)
609 module_exit(exit_relayfs_fs)
610
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);
616
617 MODULE_AUTHOR("Tom Zanussi <zanussi@us.ibm.com> and Karim Yaghmour <karim@opersys.com>");
618 MODULE_DESCRIPTION("Relay Filesystem");
619 MODULE_LICENSE("GPL");
620