]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/relayfs/inode.c
[PATCH] relayfs: export relayfs_create_file() with fileops param
[linux-2.6-omap-h63xx.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                 RELAYFS_I(inode)->data = data;
58                 break;
59         case S_IFDIR:
60                 inode->i_op = &simple_dir_inode_operations;
61                 inode->i_fop = &simple_dir_operations;
62
63                 /* directory inodes start off with i_nlink == 2 (for "." entry) */
64                 inode->i_nlink++;
65                 break;
66         default:
67                 break;
68         }
69
70         return inode;
71 }
72
73 /**
74  *      relayfs_create_entry - create a relayfs directory or file
75  *      @name: the name of the file to create
76  *      @parent: parent directory
77  *      @mode: mode
78  *      @fops: file operations to use for the file
79  *      @data: user-associated data for this file
80  *
81  *      Returns the new dentry, NULL on failure
82  *
83  *      Creates a file or directory with the specifed permissions.
84  */
85 static struct dentry *relayfs_create_entry(const char *name,
86                                            struct dentry *parent,
87                                            int mode,
88                                            struct file_operations *fops,
89                                            void *data)
90 {
91         struct dentry *d;
92         struct inode *inode;
93         int error = 0;
94
95         BUG_ON(!name || !(S_ISREG(mode) || S_ISDIR(mode)));
96
97         error = simple_pin_fs("relayfs", &relayfs_mount, &relayfs_mount_count);
98         if (error) {
99                 printk(KERN_ERR "Couldn't mount relayfs: errcode %d\n", error);
100                 return NULL;
101         }
102
103         if (!parent && relayfs_mount && relayfs_mount->mnt_sb)
104                 parent = relayfs_mount->mnt_sb->s_root;
105
106         if (!parent) {
107                 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
108                 return NULL;
109         }
110
111         parent = dget(parent);
112         down(&parent->d_inode->i_sem);
113         d = lookup_one_len(name, parent, strlen(name));
114         if (IS_ERR(d)) {
115                 d = NULL;
116                 goto release_mount;
117         }
118
119         if (d->d_inode) {
120                 d = NULL;
121                 goto release_mount;
122         }
123
124         inode = relayfs_get_inode(parent->d_inode->i_sb, mode, fops, data);
125         if (!inode) {
126                 d = NULL;
127                 goto release_mount;
128         }
129
130         d_instantiate(d, inode);
131         dget(d);        /* Extra count - pin the dentry in core */
132
133         if (S_ISDIR(mode))
134                 parent->d_inode->i_nlink++;
135
136         goto exit;
137
138 release_mount:
139         simple_release_fs(&relayfs_mount, &relayfs_mount_count);
140
141 exit:
142         up(&parent->d_inode->i_sem);
143         dput(parent);
144         return d;
145 }
146
147 /**
148  *      relayfs_create_file - create a file in the relay filesystem
149  *      @name: the name of the file to create
150  *      @parent: parent directory
151  *      @mode: mode, if not specied the default perms are used
152  *      @fops: file operations to use for the file
153  *      @data: user-associated data for this file
154  *
155  *      Returns file dentry if successful, NULL otherwise.
156  *
157  *      The file will be created user r on behalf of current user.
158  */
159 struct dentry *relayfs_create_file(const char *name,
160                                    struct dentry *parent,
161                                    int mode,
162                                    struct file_operations *fops,
163                                    void *data)
164 {
165         BUG_ON(!fops);
166
167         if (!mode)
168                 mode = S_IRUSR;
169         mode = (mode & S_IALLUGO) | S_IFREG;
170
171         return relayfs_create_entry(name, parent, mode, fops, data);
172 }
173
174 /**
175  *      relayfs_create_dir - create a directory in the relay filesystem
176  *      @name: the name of the directory to create
177  *      @parent: parent directory, NULL if parent should be fs root
178  *
179  *      Returns directory dentry if successful, NULL otherwise.
180  *
181  *      The directory will be created world rwx on behalf of current user.
182  */
183 struct dentry *relayfs_create_dir(const char *name, struct dentry *parent)
184 {
185         int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
186         return relayfs_create_entry(name, parent, mode, NULL, NULL);
187 }
188
189 /**
190  *      relayfs_remove - remove a file or directory in the relay filesystem
191  *      @dentry: file or directory dentry
192  *
193  *      Returns 0 if successful, negative otherwise.
194  */
195 int relayfs_remove(struct dentry *dentry)
196 {
197         struct dentry *parent;
198         int error = 0;
199
200         if (!dentry)
201                 return -EINVAL;
202         parent = dentry->d_parent;
203         if (!parent)
204                 return -EINVAL;
205
206         parent = dget(parent);
207         down(&parent->d_inode->i_sem);
208         if (dentry->d_inode) {
209                 if (S_ISDIR(dentry->d_inode->i_mode))
210                         error = simple_rmdir(parent->d_inode, dentry);
211                 else
212                         error = simple_unlink(parent->d_inode, dentry);
213                 if (!error)
214                         d_delete(dentry);
215         }
216         if (!error)
217                 dput(dentry);
218         up(&parent->d_inode->i_sem);
219         dput(parent);
220
221         if (!error)
222                 simple_release_fs(&relayfs_mount, &relayfs_mount_count);
223
224         return error;
225 }
226
227 /**
228  *      relayfs_remove_dir - remove a directory in the relay filesystem
229  *      @dentry: directory dentry
230  *
231  *      Returns 0 if successful, negative otherwise.
232  */
233 int relayfs_remove_dir(struct dentry *dentry)
234 {
235         return relayfs_remove(dentry);
236 }
237
238 /**
239  *      relayfs_open - open file op for relayfs files
240  *      @inode: the inode
241  *      @filp: the file
242  *
243  *      Increments the channel buffer refcount.
244  */
245 static int relayfs_open(struct inode *inode, struct file *filp)
246 {
247         struct rchan_buf *buf = RELAYFS_I(inode)->data;
248         kref_get(&buf->kref);
249
250         return 0;
251 }
252
253 /**
254  *      relayfs_mmap - mmap file op for relayfs files
255  *      @filp: the file
256  *      @vma: the vma describing what to map
257  *
258  *      Calls upon relay_mmap_buf to map the file into user space.
259  */
260 static int relayfs_mmap(struct file *filp, struct vm_area_struct *vma)
261 {
262         struct inode *inode = filp->f_dentry->d_inode;
263         return relay_mmap_buf(RELAYFS_I(inode)->data, vma);
264 }
265
266 /**
267  *      relayfs_poll - poll file op for relayfs files
268  *      @filp: the file
269  *      @wait: poll table
270  *
271  *      Poll implemention.
272  */
273 static unsigned int relayfs_poll(struct file *filp, poll_table *wait)
274 {
275         unsigned int mask = 0;
276         struct inode *inode = filp->f_dentry->d_inode;
277         struct rchan_buf *buf = RELAYFS_I(inode)->data;
278
279         if (buf->finalized)
280                 return POLLERR;
281
282         if (filp->f_mode & FMODE_READ) {
283                 poll_wait(filp, &buf->read_wait, wait);
284                 if (!relay_buf_empty(buf))
285                         mask |= POLLIN | POLLRDNORM;
286         }
287
288         return mask;
289 }
290
291 /**
292  *      relayfs_release - release file op for relayfs files
293  *      @inode: the inode
294  *      @filp: the file
295  *
296  *      Decrements the channel refcount, as the filesystem is
297  *      no longer using it.
298  */
299 static int relayfs_release(struct inode *inode, struct file *filp)
300 {
301         struct rchan_buf *buf = RELAYFS_I(inode)->data;
302         kref_put(&buf->kref, relay_remove_buf);
303
304         return 0;
305 }
306
307 /**
308  *      relayfs_read_consume - update the consumed count for the buffer
309  */
310 static void relayfs_read_consume(struct rchan_buf *buf,
311                                  size_t read_pos,
312                                  size_t bytes_consumed)
313 {
314         size_t subbuf_size = buf->chan->subbuf_size;
315         size_t n_subbufs = buf->chan->n_subbufs;
316         size_t read_subbuf;
317
318         if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
319                 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
320                 buf->bytes_consumed = 0;
321         }
322
323         buf->bytes_consumed += bytes_consumed;
324         read_subbuf = read_pos / buf->chan->subbuf_size;
325         if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
326                 if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
327                     (buf->offset == subbuf_size))
328                         return;
329                 relay_subbufs_consumed(buf->chan, buf->cpu, 1);
330                 buf->bytes_consumed = 0;
331         }
332 }
333
334 /**
335  *      relayfs_read_avail - boolean, are there unconsumed bytes available?
336  */
337 static int relayfs_read_avail(struct rchan_buf *buf, size_t read_pos)
338 {
339         size_t bytes_produced, bytes_consumed, write_offset;
340         size_t subbuf_size = buf->chan->subbuf_size;
341         size_t n_subbufs = buf->chan->n_subbufs;
342         size_t produced = buf->subbufs_produced % n_subbufs;
343         size_t consumed = buf->subbufs_consumed % n_subbufs;
344
345         write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
346
347         if (consumed > produced) {
348                 if ((produced > n_subbufs) &&
349                     (produced + n_subbufs - consumed <= n_subbufs))
350                         produced += n_subbufs;
351         } else if (consumed == produced) {
352                 if (buf->offset > subbuf_size) {
353                         produced += n_subbufs;
354                         if (buf->subbufs_produced == buf->subbufs_consumed)
355                                 consumed += n_subbufs;
356                 }
357         }
358
359         if (buf->offset > subbuf_size)
360                 bytes_produced = (produced - 1) * subbuf_size + write_offset;
361         else
362                 bytes_produced = produced * subbuf_size + write_offset;
363         bytes_consumed = consumed * subbuf_size + buf->bytes_consumed;
364
365         if (bytes_produced == bytes_consumed)
366                 return 0;
367
368         relayfs_read_consume(buf, read_pos, 0);
369
370         return 1;
371 }
372
373 /**
374  *      relayfs_read_subbuf_avail - return bytes available in sub-buffer
375  */
376 static size_t relayfs_read_subbuf_avail(size_t read_pos,
377                                         struct rchan_buf *buf)
378 {
379         size_t padding, avail = 0;
380         size_t read_subbuf, read_offset, write_subbuf, write_offset;
381         size_t subbuf_size = buf->chan->subbuf_size;
382
383         write_subbuf = (buf->data - buf->start) / subbuf_size;
384         write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
385         read_subbuf = read_pos / subbuf_size;
386         read_offset = read_pos % subbuf_size;
387         padding = buf->padding[read_subbuf];
388
389         if (read_subbuf == write_subbuf) {
390                 if (read_offset + padding < write_offset)
391                         avail = write_offset - (read_offset + padding);
392         } else
393                 avail = (subbuf_size - padding) - read_offset;
394
395         return avail;
396 }
397
398 /**
399  *      relayfs_read_start_pos - find the first available byte to read
400  *
401  *      If the read_pos is in the middle of padding, return the
402  *      position of the first actually available byte, otherwise
403  *      return the original value.
404  */
405 static size_t relayfs_read_start_pos(size_t read_pos,
406                                      struct rchan_buf *buf)
407 {
408         size_t read_subbuf, padding, padding_start, padding_end;
409         size_t subbuf_size = buf->chan->subbuf_size;
410         size_t n_subbufs = buf->chan->n_subbufs;
411
412         read_subbuf = read_pos / subbuf_size;
413         padding = buf->padding[read_subbuf];
414         padding_start = (read_subbuf + 1) * subbuf_size - padding;
415         padding_end = (read_subbuf + 1) * subbuf_size;
416         if (read_pos >= padding_start && read_pos < padding_end) {
417                 read_subbuf = (read_subbuf + 1) % n_subbufs;
418                 read_pos = read_subbuf * subbuf_size;
419         }
420
421         return read_pos;
422 }
423
424 /**
425  *      relayfs_read_end_pos - return the new read position
426  */
427 static size_t relayfs_read_end_pos(struct rchan_buf *buf,
428                                    size_t read_pos,
429                                    size_t count)
430 {
431         size_t read_subbuf, padding, end_pos;
432         size_t subbuf_size = buf->chan->subbuf_size;
433         size_t n_subbufs = buf->chan->n_subbufs;
434
435         read_subbuf = read_pos / subbuf_size;
436         padding = buf->padding[read_subbuf];
437         if (read_pos % subbuf_size + count + padding == subbuf_size)
438                 end_pos = (read_subbuf + 1) * subbuf_size;
439         else
440                 end_pos = read_pos + count;
441         if (end_pos >= subbuf_size * n_subbufs)
442                 end_pos = 0;
443
444         return end_pos;
445 }
446
447 /**
448  *      relayfs_read - read file op for relayfs files
449  *      @filp: the file
450  *      @buffer: the userspace buffer
451  *      @count: number of bytes to read
452  *      @ppos: position to read from
453  *
454  *      Reads count bytes or the number of bytes available in the
455  *      current sub-buffer being read, whichever is smaller.
456  */
457 static ssize_t relayfs_read(struct file *filp,
458                             char __user *buffer,
459                             size_t count,
460                             loff_t *ppos)
461 {
462         struct inode *inode = filp->f_dentry->d_inode;
463         struct rchan_buf *buf = RELAYFS_I(inode)->data;
464         size_t read_start, avail;
465         ssize_t ret = 0;
466         void *from;
467
468         down(&inode->i_sem);
469         if(!relayfs_read_avail(buf, *ppos))
470                 goto out;
471
472         read_start = relayfs_read_start_pos(*ppos, buf);
473         avail = relayfs_read_subbuf_avail(read_start, buf);
474         if (!avail)
475                 goto out;
476
477         from = buf->start + read_start;
478         ret = count = min(count, avail);
479         if (copy_to_user(buffer, from, count)) {
480                 ret = -EFAULT;
481                 goto out;
482         }
483         relayfs_read_consume(buf, read_start, count);
484         *ppos = relayfs_read_end_pos(buf, read_start, count);
485 out:
486         up(&inode->i_sem);
487         return ret;
488 }
489
490 /**
491  *      relayfs alloc_inode() implementation
492  */
493 static struct inode *relayfs_alloc_inode(struct super_block *sb)
494 {
495         struct relayfs_inode_info *p = kmem_cache_alloc(relayfs_inode_cachep, SLAB_KERNEL);
496         if (!p)
497                 return NULL;
498         p->data = NULL;
499
500         return &p->vfs_inode;
501 }
502
503 /**
504  *      relayfs destroy_inode() implementation
505  */
506 static void relayfs_destroy_inode(struct inode *inode)
507 {
508         kmem_cache_free(relayfs_inode_cachep, RELAYFS_I(inode));
509 }
510
511 static void init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
512 {
513         struct relayfs_inode_info *i = p;
514         if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
515                 inode_init_once(&i->vfs_inode);
516 }
517
518 struct file_operations relayfs_file_operations = {
519         .open           = relayfs_open,
520         .poll           = relayfs_poll,
521         .mmap           = relayfs_mmap,
522         .read           = relayfs_read,
523         .llseek         = no_llseek,
524         .release        = relayfs_release,
525 };
526
527 static struct super_operations relayfs_ops = {
528         .statfs         = simple_statfs,
529         .drop_inode     = generic_delete_inode,
530         .alloc_inode    = relayfs_alloc_inode,
531         .destroy_inode  = relayfs_destroy_inode,
532 };
533
534 static int relayfs_fill_super(struct super_block * sb, void * data, int silent)
535 {
536         struct inode *inode;
537         struct dentry *root;
538         int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
539
540         sb->s_blocksize = PAGE_CACHE_SIZE;
541         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
542         sb->s_magic = RELAYFS_MAGIC;
543         sb->s_op = &relayfs_ops;
544         inode = relayfs_get_inode(sb, mode, NULL, NULL);
545
546         if (!inode)
547                 return -ENOMEM;
548
549         root = d_alloc_root(inode);
550         if (!root) {
551                 iput(inode);
552                 return -ENOMEM;
553         }
554         sb->s_root = root;
555
556         return 0;
557 }
558
559 static struct super_block * relayfs_get_sb(struct file_system_type *fs_type,
560                                            int flags, const char *dev_name,
561                                            void *data)
562 {
563         return get_sb_single(fs_type, flags, data, relayfs_fill_super);
564 }
565
566 static struct file_system_type relayfs_fs_type = {
567         .owner          = THIS_MODULE,
568         .name           = "relayfs",
569         .get_sb         = relayfs_get_sb,
570         .kill_sb        = kill_litter_super,
571 };
572
573 static int __init init_relayfs_fs(void)
574 {
575         int err;
576
577         relayfs_inode_cachep = kmem_cache_create("relayfs_inode_cache",
578                                 sizeof(struct relayfs_inode_info), 0,
579                                 0, init_once, NULL);
580         if (!relayfs_inode_cachep)
581                 return -ENOMEM;
582
583         err = register_filesystem(&relayfs_fs_type);
584         if (err)
585                 kmem_cache_destroy(relayfs_inode_cachep);
586
587         return err;
588 }
589
590 static void __exit exit_relayfs_fs(void)
591 {
592         unregister_filesystem(&relayfs_fs_type);
593         kmem_cache_destroy(relayfs_inode_cachep);
594 }
595
596 module_init(init_relayfs_fs)
597 module_exit(exit_relayfs_fs)
598
599 EXPORT_SYMBOL_GPL(relayfs_file_operations);
600 EXPORT_SYMBOL_GPL(relayfs_create_dir);
601 EXPORT_SYMBOL_GPL(relayfs_remove_dir);
602 EXPORT_SYMBOL_GPL(relayfs_create_file);
603
604 MODULE_AUTHOR("Tom Zanussi <zanussi@us.ibm.com> and Karim Yaghmour <karim@opersys.com>");
605 MODULE_DESCRIPTION("Relay Filesystem");
606 MODULE_LICENSE("GPL");
607