2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/init.h>
45 #include <linux/init.h>
46 #include <linux/list.h>
47 #include <linux/cdev.h>
48 #include <linux/poll.h>
49 #include <linux/slab.h>
50 #include <linux/sched.h>
51 #include <linux/ioctl.h>
52 #include <linux/types.h>
53 #include <linux/module.h>
54 #include <linux/mmzone.h>
55 #include <linux/vmalloc.h>
56 #include <linux/spinlock.h>
57 #include <linux/wait.h>
61 static struct segdev segdev;
63 int segdev_create_segment(struct segdev *dev, u64 segsize, char reserved)
66 int ret = mutex_lock_interruptible(&dev->mutex);
74 /* vmalloc can handle large sizes */
76 XSEGLOG("creating segment of size %llu\n", segsize);
77 segment = vmalloc(segsize);
81 dev->segsize = segsize;
82 dev->segment = segment;
83 memset(dev->segment, 0, segsize);
84 set_bit(SEGDEV_READY, &dev->flags);
86 set_bit(SEGDEV_RESERVED, &dev->flags);
90 mutex_unlock(&dev->mutex);
95 EXPORT_SYMBOL(segdev_create_segment);
97 int segdev_destroy_segment(struct segdev *dev)
99 int ret = mutex_lock_interruptible(&dev->mutex);
104 * The segment trully dies when everyone in userspace has unmapped it.
105 * However, the kernel mapping is immediately destroyed.
106 * Kernel users are notified to abort via switching of SEGDEV_READY.
107 * The mapping deallocation is performed when all kernel users
108 * have stopped using the segment as reported by usercount.
116 if (test_bit(SEGDEV_RESERVED, &dev->flags))
119 clear_bit(SEGDEV_READY, &dev->flags);
120 ret = wait_event_interruptible(dev->wq, atomic_read(&dev->usercount) <= 1);
130 mutex_unlock(&dev->mutex);
131 set_bit(SEGDEV_READY, &dev->flags);
136 EXPORT_SYMBOL(segdev_destroy_segment);
138 struct segdev *segdev_get(int minor)
140 struct segdev *dev = ERR_PTR(-ENODEV);
145 atomic_inc(&dev->usercount);
146 if (!test_bit(SEGDEV_READY, &dev->flags))
153 dev = ERR_PTR(-EBUSY);
157 EXPORT_SYMBOL(segdev_get);
159 void segdev_put(struct segdev *dev)
161 atomic_dec(&dev->usercount);
163 /* ain't all this too heavy ? */
166 EXPORT_SYMBOL(segdev_put);
168 /* ********************* */
169 /* ** File Operations ** */
170 /* ********************* */
176 static int segdev_open(struct inode *inode, struct file *file)
178 struct segdev_file *vf = kmalloc(sizeof(struct segdev_file), GFP_KERNEL);
182 file->private_data = vf;
186 static int segdev_release(struct inode *inode, struct file *file)
188 struct segdev_file *vf = file->private_data;
193 static long segdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
202 case SEGDEV_IOC_CREATESEG:
204 ret = IS_ERR(dev) ? PTR_ERR(dev) : 0;
208 ret = segdev_create_segment(dev, (u64)arg, 0);
212 case SEGDEV_IOC_DESTROYSEG:
214 ret = segdev_destroy_segment(&segdev);
218 case SEGDEV_IOC_SEGSIZE:
221 ret = IS_ERR(dev) ? PTR_ERR(dev) : 0;
240 static ssize_t segdev_read(struct file *file, char __user *buf,
241 size_t count, loff_t *f_pos)
246 static ssize_t segdev_write(struct file *file, const char __user *buf,
247 size_t count, loff_t *f_pos)
249 struct segdev_file *vf = file->private_data;
250 struct segdev *dev = segdev_get(vf->minor);
256 if (count != sizeof(uint32_t))
259 ret = copy_from_user(&portno, buf, sizeof(uint32_t));
263 if((count - ret) != sizeof(uint32_t))
268 dev->callback(dev, portno);
272 dev->buffer_index = 0;
279 static int segdev_mmap(struct file *file, struct vm_area_struct *vma)
281 struct segdev_file *vf = file->private_data;
283 size_t size = vma->vm_end - vma->vm_start;
284 unsigned long start = vma->vm_start, end = start + size;
288 dev = segdev_get(vf->minor);
298 /* do not allow offset mappings, for now */
299 if (vma->vm_pgoff || size > dev->segsize)
302 /* allow only shared, read-write mappings */
303 if (!(vma->vm_flags & VM_SHARED))
306 /* the segment is vmalloc() so we have to iterate through
307 * all pages and laboriously map them one by one. */
308 for (; start < end; start += PAGE_SIZE, ptr += PAGE_SIZE) {
309 ret = remap_pfn_range(vma, start, vmalloc_to_pfn(ptr),
310 PAGE_SIZE, vma->vm_page_prot);
312 goto out_put; /* mmap syscall should clean up, right? */
323 static struct file_operations segdev_ops =
325 .owner = THIS_MODULE,
327 .release = segdev_release,
329 .write = segdev_write,
331 .unlocked_ioctl = segdev_ioctl,
335 /* *************************** */
336 /* ** Module Initialization ** */
337 /* *************************** */
339 static void segdev_init(struct segdev *dev, int minor)
345 atomic_set(&dev->usercount, 0);
346 init_waitqueue_head(&dev->wq);
347 cdev_init(&dev->cdev, &segdev_ops);
348 mutex_init(&dev->mutex);
349 spin_lock_init(&dev->lock);
350 dev->cdev.owner = THIS_MODULE;
351 set_bit(SEGDEV_READY, &dev->flags);
354 int __init segdev_mod_init(void)
357 dev_t dev_no = MKDEV(SEGDEV_MAJOR, 0);
358 ret = register_chrdev_region(dev_no, 1, "segdev");
362 segdev_init(&segdev, 0);
363 ret = cdev_add(&segdev.cdev, dev_no, 1);
370 unregister_chrdev_region(dev_no, 1);
375 void __exit segdev_mod_exit(void)
377 dev_t dev_no = MKDEV(SEGDEV_MAJOR, 0);
378 segdev_destroy_segment(&segdev);
379 cdev_del(&segdev.cdev);
380 unregister_chrdev_region(dev_no, 1);
383 module_init(segdev_mod_init);
384 module_exit(segdev_mod_exit);
386 MODULE_DESCRIPTION("segdev");
387 MODULE_AUTHOR("XSEG");
388 MODULE_LICENSE("BSD");