writeback: split writeback_inodes_wb
[linux-2.6/next.git] / drivers / staging / hv / osd.c
blob8c3eb278a81f35ead80a477dc101093b1143eac7
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/mm.h>
28 #include <linux/highmem.h>
29 #include <linux/vmalloc.h>
30 #include <linux/ioport.h>
31 #include <linux/irq.h>
32 #include <linux/interrupt.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/spinlock.h>
36 #include <linux/workqueue.h>
37 #include <linux/kernel.h>
38 #include <linux/jiffies.h>
39 #include <linux/delay.h>
40 #include <linux/time.h>
41 #include <linux/io.h>
42 #include <linux/bitops.h>
43 #include <linux/slab.h>
44 #include "osd.h"
46 struct osd_callback_struct {
47 struct work_struct work;
48 void (*callback)(void *);
49 void *data;
52 void *osd_VirtualAllocExec(unsigned int size)
54 #ifdef __x86_64__
55 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
56 #else
57 return __vmalloc(size, GFP_KERNEL,
58 __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
59 #endif
62 /**
63 * osd_PageAlloc() - Allocate pages
64 * @count: Total number of Kernel pages you want to allocate
66 * Tries to allocate @count number of consecutive free kernel pages.
67 * And if successful, it will set the pages to 0 before returning.
68 * If successfull it will return pointer to the @count pages.
69 * Mainly used by Hyper-V drivers.
71 void *osd_PageAlloc(unsigned int count)
73 void *p;
75 p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
76 if (p)
77 memset(p, 0, count * PAGE_SIZE);
78 return p;
80 /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
81 /* void *p; */
83 /* BUGBUG: We need to use kmap in case we are in HIMEM region */
84 /* p = page_address(page); */
85 /* if (p) memset(p, 0, PAGE_SIZE); */
86 /* return p; */
88 EXPORT_SYMBOL_GPL(osd_PageAlloc);
90 /**
91 * osd_PageFree() - Free pages
92 * @page: Pointer to the first page to be freed
93 * @count: Total number of Kernel pages you free
95 * Frees the pages allocated by osd_PageAlloc()
96 * Mainly used by Hyper-V drivers.
98 void osd_PageFree(void *page, unsigned int count)
100 free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
101 /*struct page* p = virt_to_page(page);
102 __free_page(p);*/
104 EXPORT_SYMBOL_GPL(osd_PageFree);
107 * osd_WaitEventCreate() - Create the event queue
109 * Allocates memory for a &struct osd_waitevent. And then calls
110 * init_waitqueue_head to set up the wait queue for the event.
111 * This structure is usually part of a another structure that contains
112 * the actual Hyper-V device driver structure.
114 * Returns pointer to &struct osd_waitevent
115 * Mainly used by Hyper-V drivers.
117 struct osd_waitevent *osd_WaitEventCreate(void)
119 struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent),
120 GFP_KERNEL);
121 if (!wait)
122 return NULL;
124 wait->condition = 0;
125 init_waitqueue_head(&wait->event);
126 return wait;
128 EXPORT_SYMBOL_GPL(osd_WaitEventCreate);
132 * osd_WaitEventSet() - Wake up the process
133 * @waitEvent: Structure to event to be woken up
135 * @waitevent is of type &struct osd_waitevent
137 * Wake up the sleeping process so it can do some work.
138 * And set condition indicator in &struct osd_waitevent to indicate
139 * the process is in a woken state.
141 * Only used by Network and Storage Hyper-V drivers.
143 void osd_WaitEventSet(struct osd_waitevent *waitEvent)
145 waitEvent->condition = 1;
146 wake_up_interruptible(&waitEvent->event);
148 EXPORT_SYMBOL_GPL(osd_WaitEventSet);
151 * osd_WaitEventWait() - Wait for event till condition is true
152 * @waitEvent: Structure to event to be put to sleep
154 * @waitevent is of type &struct osd_waitevent
156 * Set up the process to sleep until waitEvent->condition get true.
157 * And set condition indicator in &struct osd_waitevent to indicate
158 * the process is in a sleeping state.
160 * Returns the status of 'wait_event_interruptible()' system call
162 * Mainly used by Hyper-V drivers.
164 int osd_WaitEventWait(struct osd_waitevent *waitEvent)
166 int ret = 0;
168 ret = wait_event_interruptible(waitEvent->event,
169 waitEvent->condition);
170 waitEvent->condition = 0;
171 return ret;
173 EXPORT_SYMBOL_GPL(osd_WaitEventWait);
176 * osd_WaitEventWaitEx() - Wait for event or timeout for process wakeup
177 * @waitEvent: Structure to event to be put to sleep
178 * @TimeoutInMs: Total number of Milliseconds to wait before waking up
180 * @waitevent is of type &struct osd_waitevent
181 * Set up the process to sleep until @waitEvent->condition get true or
182 * @TimeoutInMs (Time out in Milliseconds) has been reached.
183 * And set condition indicator in &struct osd_waitevent to indicate
184 * the process is in a sleeping state.
186 * Returns the status of 'wait_event_interruptible_timeout()' system call
188 * Mainly used by Hyper-V drivers.
190 int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
192 int ret = 0;
194 ret = wait_event_interruptible_timeout(waitEvent->event,
195 waitEvent->condition,
196 msecs_to_jiffies(TimeoutInMs));
197 waitEvent->condition = 0;
198 return ret;
200 EXPORT_SYMBOL_GPL(osd_WaitEventWaitEx);
202 static void osd_callback_work(struct work_struct *work)
204 struct osd_callback_struct *cb = container_of(work,
205 struct osd_callback_struct,
206 work);
207 (cb->callback)(cb->data);
208 kfree(cb);
211 int osd_schedule_callback(struct workqueue_struct *wq,
212 void (*func)(void *),
213 void *data)
215 struct osd_callback_struct *cb;
217 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
218 if (!cb) {
219 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback\n");
220 return -1;
223 cb->callback = func;
224 cb->data = data;
225 INIT_WORK(&cb->work, osd_callback_work);
226 return queue_work(wq, &cb->work);