Fix typos in licence headers
[archipelago] / xseg / sys / kernel / segdev.c
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
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.
15  *
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.
28  *
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.
33  */
34
35 /*
36  *
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/init.h>
43 #include <linux/mm.h>
44 #include <linux/fs.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>
58 #include <sys/util.h>
59 #include "segdev.h"
60
61 static struct segdev segdev;
62
63 int segdev_create_segment(struct segdev *dev, u64 segsize, char reserved)
64 {
65         void *segment;
66         int ret = mutex_lock_interruptible(&dev->mutex);
67         if (ret)
68                 goto out;
69
70         ret = -EBUSY;
71         if (dev->segment)
72                 goto out_unlock;
73
74         /* vmalloc can handle large sizes */
75         ret = -ENOMEM;
76         XSEGLOG("creating segment of size %llu\n", segsize);
77         segment = vmalloc(segsize);
78         if (!segment)
79                 goto out_unlock;
80
81         dev->segsize = segsize;
82         dev->segment = segment;
83         memset(dev->segment, 0, segsize);
84         set_bit(SEGDEV_READY, &dev->flags);
85         if (reserved)
86                 set_bit(SEGDEV_RESERVED, &dev->flags);
87         ret = 0;
88
89 out_unlock:
90         mutex_unlock(&dev->mutex);
91 out:
92         return ret;
93 }
94
95 EXPORT_SYMBOL(segdev_create_segment);
96
97 int segdev_destroy_segment(struct segdev *dev)
98 {
99         int ret = mutex_lock_interruptible(&dev->mutex);
100         if (ret)
101                 goto out;
102
103         /* VERIFY:
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.
109         */
110
111         ret = -EINVAL;
112         if (!dev->segment)
113                 goto out_unlock;
114
115         ret = -EBUSY;
116         if (test_bit(SEGDEV_RESERVED, &dev->flags))
117                 goto out_unlock;
118
119         clear_bit(SEGDEV_READY, &dev->flags);
120         ret = wait_event_interruptible(dev->wq, atomic_read(&dev->usercount) <= 1);
121         if (ret)
122                 goto out_unlock;
123
124         vfree(dev->segment);
125         dev->segment = NULL;
126         dev->segsize = 0;
127         ret = 0;
128
129 out_unlock:
130         mutex_unlock(&dev->mutex);
131         set_bit(SEGDEV_READY, &dev->flags);
132 out:
133         return ret;
134 }
135
136 EXPORT_SYMBOL(segdev_destroy_segment);
137
138 struct segdev *segdev_get(int minor)
139 {
140         struct segdev *dev = ERR_PTR(-ENODEV);
141         if (minor)
142                 goto out;
143
144         dev = &segdev;
145         atomic_inc(&dev->usercount);
146         if (!test_bit(SEGDEV_READY, &dev->flags))
147                 goto fail_busy;
148 out:
149         return dev;
150
151 fail_busy:
152         segdev_put(dev);
153         dev = ERR_PTR(-EBUSY);
154         goto out;
155 }
156
157 EXPORT_SYMBOL(segdev_get);
158
159 void segdev_put(struct segdev *dev)
160 {
161         atomic_dec(&dev->usercount);
162         wake_up(&dev->wq);
163         /* ain't all this too heavy ? */
164 }
165
166 EXPORT_SYMBOL(segdev_put);
167
168 /* ********************* */
169 /* ** File Operations ** */
170 /* ********************* */
171
172 struct segdev_file {
173         int minor;
174 };
175
176 static int segdev_open(struct inode *inode, struct file *file)
177 {
178         struct segdev_file *vf = kmalloc(sizeof(struct segdev_file), GFP_KERNEL);
179         if (!vf)
180                 return -ENOMEM;
181         vf->minor = 0;
182         file->private_data = vf;
183         return 0;
184 }
185
186 static int segdev_release(struct inode *inode, struct file *file)
187 {
188         struct segdev_file *vf = file->private_data;
189         kfree(vf);
190         return 0;
191 }
192
193 static long segdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
194 {
195         struct segdev *dev;
196         char *seg;
197         long size;
198         int ret = -EINVAL;
199
200         switch (cmd) {
201
202         case SEGDEV_IOC_CREATESEG:
203                 dev = segdev_get(0);
204                 ret = IS_ERR(dev) ? PTR_ERR(dev) : 0;
205                 if (ret)
206                         goto out;
207
208                 ret = segdev_create_segment(dev, (u64)arg, 0);
209                 segdev_put(dev);
210                 goto out;
211
212         case SEGDEV_IOC_DESTROYSEG:
213                 dev = segdev_get(0);
214                 ret = segdev_destroy_segment(&segdev);
215                 segdev_put(dev);
216                 goto out;
217
218         case SEGDEV_IOC_SEGSIZE:
219                 dev = segdev_get(0);
220
221                 ret = IS_ERR(dev) ? PTR_ERR(dev) : 0;
222                 if (ret)
223                         goto out;
224
225                 size = dev->segsize;
226                 seg = dev->segment;
227                 segdev_put(dev);
228
229                 ret = -ENODEV;
230                 if (!seg)
231                         goto out;
232
233                 return size;
234         }
235
236 out:
237         return ret;
238 }
239
240 static ssize_t segdev_read(struct file *file, char __user *buf,
241                            size_t count, loff_t *f_pos)
242 {
243         return 0;
244 }
245
246 static ssize_t segdev_write(struct file *file, const char __user *buf,
247                             size_t count, loff_t *f_pos)
248 {
249         struct segdev_file *vf = file->private_data;
250         struct segdev *dev = segdev_get(vf->minor);
251         uint32_t portno;
252         int ret = -ENODEV;
253         if (!dev)
254                 goto out;
255
256         if (count != sizeof(uint32_t))
257                 goto out_put;
258
259         ret = copy_from_user(&portno, buf, sizeof(uint32_t));
260         if (ret < 0)
261                 goto out_put;
262
263         if((count - ret) != sizeof(uint32_t))
264                 goto out_put;
265
266         ret = 0;
267         if (dev->callback)
268                 dev->callback(dev, portno);
269         else
270                 ret = -ENOSYS;
271
272         dev->buffer_index = 0;
273 out_put:
274         segdev_put(dev);
275 out:
276         return ret;
277 }
278
279 static int segdev_mmap(struct file *file, struct vm_area_struct *vma)
280 {
281         struct segdev_file *vf = file->private_data;
282         struct segdev *dev;
283         size_t size = vma->vm_end - vma->vm_start;
284         unsigned long start = vma->vm_start, end = start + size;
285         char *ptr;
286         int ret = -ENODEV;
287
288         dev = segdev_get(vf->minor);
289         if (IS_ERR(dev))
290                 goto out;
291
292         ptr = dev->segment;
293         if (!ptr)
294                 goto out_put;
295
296         ret = -EINVAL;
297
298         /* do not allow offset mappings, for now */
299         if (vma->vm_pgoff || size > dev->segsize)
300                 goto out_put;
301
302         /* allow only shared, read-write mappings */
303         if (!(vma->vm_flags & VM_SHARED))
304                 goto out_put;
305
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);
311                 if (ret)
312                         goto out_put; /* mmap syscall should clean up, right? */
313         }
314
315         ret = 0;
316
317 out_put:
318         segdev_put(dev);
319 out:
320         return ret;
321 }
322
323 static struct file_operations segdev_ops = 
324 {
325         .owner          = THIS_MODULE,
326         .open           = segdev_open,
327         .release        = segdev_release,
328         .read           = segdev_read,
329         .write          = segdev_write,
330         .mmap           = segdev_mmap,
331         .unlocked_ioctl = segdev_ioctl,
332 };
333
334
335 /* *************************** */
336 /* ** Module Initialization ** */
337 /* *************************** */
338
339 static void segdev_init(struct segdev *dev, int minor)
340 {
341         dev->minor = 0;
342         dev->segment = NULL;
343         dev->segsize = 0;
344         dev->flags = 0;
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);
352 }
353
354 int __init segdev_mod_init(void)
355 {
356         int ret;
357         dev_t dev_no = MKDEV(SEGDEV_MAJOR, 0);
358         ret = register_chrdev_region(dev_no, 1, "segdev");
359         if (ret < 0)
360                 goto out;
361
362         segdev_init(&segdev, 0);
363         ret = cdev_add(&segdev.cdev, dev_no, 1);
364         if (ret < 0)
365                 goto out_unregister;
366
367         return ret;
368
369 out_unregister:
370         unregister_chrdev_region(dev_no, 1);
371 out:
372         return ret;
373 }
374
375 void __exit segdev_mod_exit(void)
376 {
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);
381 }
382
383 module_init(segdev_mod_init);
384 module_exit(segdev_mod_exit);
385
386 MODULE_DESCRIPTION("segdev");
387 MODULE_AUTHOR("XSEG");
388 MODULE_LICENSE("BSD");
389