ia64/linux-2.6.18-xen.hg

view drivers/xen/blktap/blktap.c @ 761:5e1269aa5c29

blktap, gntdev: fix highpte handling

In case of highpte, virt_to_machine() can't be used. Introduce
ptep_to_machine() and use it, also to simplify xen_l1_entry_update().

Original patch from: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Dec 10 13:32:32 2008 +0000 (2008-12-10)
parents 5012c470f875
children 464a925d73f1
line source
1 /******************************************************************************
2 * drivers/xen/blktap/blktap.c
3 *
4 * Back-end driver for user level virtual block devices. This portion of the
5 * driver exports a 'unified' block-device interface that can be accessed
6 * by any operating system that implements a compatible front end. Requests
7 * are remapped to a user-space memory region.
8 *
9 * Based on the blkback driver code.
10 *
11 * Copyright (c) 2004-2005, Andrew Warfield and Julian Chesterfield
12 *
13 * Clean ups and fix ups:
14 * Copyright (c) 2006, Steven Rostedt - Red Hat, Inc.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation; or, when distributed
19 * separately from the Linux kernel or incorporated into other
20 * software packages, subject to the following license:
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this source file (the "Software"), to deal in the Software without
24 * restriction, including without limitation the rights to use, copy, modify,
25 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
26 * and to permit persons to whom the Software is furnished to do so, subject to
27 * the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
37 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
38 * IN THE SOFTWARE.
39 */
41 #include <linux/spinlock.h>
42 #include <linux/kthread.h>
43 #include <linux/list.h>
44 #include <asm/hypervisor.h>
45 #include "common.h"
46 #include <xen/balloon.h>
47 #include <xen/driver_util.h>
48 #include <linux/kernel.h>
49 #include <linux/fs.h>
50 #include <linux/mm.h>
51 #include <linux/errno.h>
52 #include <linux/major.h>
53 #include <linux/gfp.h>
54 #include <linux/poll.h>
55 #include <linux/delay.h>
56 #include <asm/tlbflush.h>
58 #define MAX_TAP_DEV 256 /*the maximum number of tapdisk ring devices */
59 #define MAX_DEV_NAME 100 /*the max tapdisk ring device name e.g. blktap0 */
61 /*
62 * The maximum number of requests that can be outstanding at any time
63 * is determined by
64 *
65 * [mmap_alloc * MAX_PENDING_REQS * BLKIF_MAX_SEGMENTS_PER_REQUEST]
66 *
67 * where mmap_alloc < MAX_DYNAMIC_MEM.
68 *
69 * TODO:
70 * mmap_alloc is initialised to 2 and should be adjustable on the fly via
71 * sysfs.
72 */
73 #define BLK_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
74 #define MAX_DYNAMIC_MEM BLK_RING_SIZE
75 #define MAX_PENDING_REQS BLK_RING_SIZE
76 #define MMAP_PAGES (MAX_PENDING_REQS * BLKIF_MAX_SEGMENTS_PER_REQUEST)
77 #define MMAP_VADDR(_start, _req,_seg) \
78 (_start + \
79 ((_req) * BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE) + \
80 ((_seg) * PAGE_SIZE))
81 static int blkif_reqs = MAX_PENDING_REQS;
82 static int mmap_pages = MMAP_PAGES;
84 #define RING_PAGES 1 /* BLKTAP - immediately before the mmap area, we
85 * have a bunch of pages reserved for shared
86 * memory rings.
87 */
89 /*Data struct handed back to userspace for tapdisk device to VBD mapping*/
90 typedef struct domid_translate {
91 unsigned short domid;
92 unsigned short busid;
93 } domid_translate_t ;
95 typedef struct domid_translate_ext {
96 unsigned short domid;
97 u32 busid;
98 } domid_translate_ext_t ;
100 /*Data struct associated with each of the tapdisk devices*/
101 typedef struct tap_blkif {
102 struct vm_area_struct *vma; /*Shared memory area */
103 unsigned long rings_vstart; /*Kernel memory mapping */
104 unsigned long user_vstart; /*User memory mapping */
105 unsigned long dev_inuse; /*One process opens device at a time. */
106 unsigned long dev_pending; /*In process of being opened */
107 unsigned long ring_ok; /*make this ring->state */
108 blkif_front_ring_t ufe_ring; /*Rings up to user space. */
109 wait_queue_head_t wait; /*for poll */
110 unsigned long mode; /*current switching mode */
111 int minor; /*Minor number for tapdisk device */
112 pid_t pid; /*tapdisk process id */
113 enum { RUNNING, CLEANSHUTDOWN } status; /*Detect a clean userspace
114 shutdown */
115 unsigned long *idx_map; /*Record the user ring id to kern
116 [req id, idx] tuple */
117 blkif_t *blkif; /*Associate blkif with tapdev */
118 struct domid_translate_ext trans; /*Translation from domid to bus. */
119 } tap_blkif_t;
121 static struct tap_blkif *tapfds[MAX_TAP_DEV];
122 static int blktap_next_minor;
124 module_param(blkif_reqs, int, 0);
125 /* Run-time switchable: /sys/module/blktap/parameters/ */
126 static unsigned int log_stats = 0;
127 static unsigned int debug_lvl = 0;
128 module_param(log_stats, int, 0644);
129 module_param(debug_lvl, int, 0644);
131 /*
132 * Each outstanding request that we've passed to the lower device layers has a
133 * 'pending_req' allocated to it. Each buffer_head that completes decrements
134 * the pendcnt towards zero. When it hits zero, the specified domain has a
135 * response queued for it, with the saved 'id' passed back.
136 */
137 typedef struct {
138 blkif_t *blkif;
139 u64 id;
140 unsigned short mem_idx;
141 int nr_pages;
142 atomic_t pendcnt;
143 unsigned short operation;
144 int status;
145 struct list_head free_list;
146 int inuse;
147 } pending_req_t;
149 static pending_req_t *pending_reqs[MAX_PENDING_REQS];
150 static struct list_head pending_free;
151 static DEFINE_SPINLOCK(pending_free_lock);
152 static DECLARE_WAIT_QUEUE_HEAD (pending_free_wq);
153 static int alloc_pending_reqs;
155 typedef unsigned int PEND_RING_IDX;
157 static inline int MASK_PEND_IDX(int i) {
158 return (i & (MAX_PENDING_REQS-1));
159 }
161 static inline unsigned int RTN_PEND_IDX(pending_req_t *req, int idx) {
162 return (req - pending_reqs[idx]);
163 }
165 #define NR_PENDING_REQS (MAX_PENDING_REQS - pending_prod + pending_cons)
167 #define BLKBACK_INVALID_HANDLE (~0)
169 static struct page **foreign_pages[MAX_DYNAMIC_MEM];
170 static inline unsigned long idx_to_kaddr(
171 unsigned int mmap_idx, unsigned int req_idx, unsigned int sg_idx)
172 {
173 unsigned int arr_idx = req_idx*BLKIF_MAX_SEGMENTS_PER_REQUEST + sg_idx;
174 unsigned long pfn = page_to_pfn(foreign_pages[mmap_idx][arr_idx]);
175 return (unsigned long)pfn_to_kaddr(pfn);
176 }
178 static unsigned short mmap_alloc = 0;
179 static unsigned short mmap_lock = 0;
180 static unsigned short mmap_inuse = 0;
182 /******************************************************************
183 * GRANT HANDLES
184 */
186 /* When using grant tables to map a frame for device access then the
187 * handle returned must be used to unmap the frame. This is needed to
188 * drop the ref count on the frame.
189 */
190 struct grant_handle_pair
191 {
192 grant_handle_t kernel;
193 grant_handle_t user;
194 };
195 #define INVALID_GRANT_HANDLE 0xFFFF
197 static struct grant_handle_pair
198 pending_grant_handles[MAX_DYNAMIC_MEM][MMAP_PAGES];
199 #define pending_handle(_id, _idx, _i) \
200 (pending_grant_handles[_id][((_idx) * BLKIF_MAX_SEGMENTS_PER_REQUEST) \
201 + (_i)])
204 static int blktap_read_ufe_ring(tap_blkif_t *info); /*local prototypes*/
206 #define BLKTAP_MINOR 0 /*/dev/xen/blktap has a dynamic major */
207 #define BLKTAP_DEV_DIR "/dev/xen"
209 static int blktap_major;
211 /* blktap IOCTLs: */
212 #define BLKTAP_IOCTL_KICK_FE 1
213 #define BLKTAP_IOCTL_KICK_BE 2 /* currently unused */
214 #define BLKTAP_IOCTL_SETMODE 3
215 #define BLKTAP_IOCTL_SENDPID 4
216 #define BLKTAP_IOCTL_NEWINTF 5
217 #define BLKTAP_IOCTL_MINOR 6
218 #define BLKTAP_IOCTL_MAJOR 7
219 #define BLKTAP_QUERY_ALLOC_REQS 8
220 #define BLKTAP_IOCTL_FREEINTF 9
221 #define BLKTAP_IOCTL_NEWINTF_EXT 50
222 #define BLKTAP_IOCTL_PRINT_IDXS 100
224 /* blktap switching modes: (Set with BLKTAP_IOCTL_SETMODE) */
225 #define BLKTAP_MODE_PASSTHROUGH 0x00000000 /* default */
226 #define BLKTAP_MODE_INTERCEPT_FE 0x00000001
227 #define BLKTAP_MODE_INTERCEPT_BE 0x00000002 /* unimp. */
229 #define BLKTAP_MODE_INTERPOSE \
230 (BLKTAP_MODE_INTERCEPT_FE | BLKTAP_MODE_INTERCEPT_BE)
233 static inline int BLKTAP_MODE_VALID(unsigned long arg)
234 {
235 return ((arg == BLKTAP_MODE_PASSTHROUGH ) ||
236 (arg == BLKTAP_MODE_INTERCEPT_FE) ||
237 (arg == BLKTAP_MODE_INTERPOSE ));
238 }
240 /* Requests passing through the tap to userspace are re-assigned an ID.
241 * We must record a mapping between the BE [IDX,ID] tuple and the userspace
242 * ring ID.
243 */
245 static inline unsigned long MAKE_ID(domid_t fe_dom, PEND_RING_IDX idx)
246 {
247 return ((fe_dom << 16) | MASK_PEND_IDX(idx));
248 }
250 extern inline PEND_RING_IDX ID_TO_IDX(unsigned long id)
251 {
252 return (PEND_RING_IDX)(id & 0x0000ffff);
253 }
255 extern inline int ID_TO_MIDX(unsigned long id)
256 {
257 return (int)(id >> 16);
258 }
260 #define INVALID_REQ 0xdead0000
262 /*TODO: Convert to a free list*/
263 static inline int GET_NEXT_REQ(unsigned long *idx_map)
264 {
265 int i;
266 for (i = 0; i < MAX_PENDING_REQS; i++)
267 if (idx_map[i] == INVALID_REQ)
268 return i;
270 return INVALID_REQ;
271 }
273 static inline int OFFSET_TO_USR_IDX(int offset)
274 {
275 return offset / BLKIF_MAX_SEGMENTS_PER_REQUEST;
276 }
278 static inline int OFFSET_TO_SEG(int offset)
279 {
280 return offset % BLKIF_MAX_SEGMENTS_PER_REQUEST;
281 }
284 #define BLKTAP_INVALID_HANDLE(_g) \
285 (((_g->kernel) == INVALID_GRANT_HANDLE) && \
286 ((_g->user) == INVALID_GRANT_HANDLE))
288 #define BLKTAP_INVALIDATE_HANDLE(_g) do { \
289 (_g)->kernel = INVALID_GRANT_HANDLE; (_g)->user = INVALID_GRANT_HANDLE; \
290 } while(0)
293 /******************************************************************
294 * BLKTAP VM OPS
295 */
297 static struct page *blktap_nopage(struct vm_area_struct *vma,
298 unsigned long address,
299 int *type)
300 {
301 /*
302 * if the page has not been mapped in by the driver then return
303 * NOPAGE_SIGBUS to the domain.
304 */
306 return NOPAGE_SIGBUS;
307 }
309 static pte_t blktap_clear_pte(struct vm_area_struct *vma,
310 unsigned long uvaddr,
311 pte_t *ptep, int is_fullmm)
312 {
313 pte_t copy;
314 tap_blkif_t *info;
315 int offset, seg, usr_idx, pending_idx, mmap_idx;
316 unsigned long uvstart = vma->vm_start + (RING_PAGES << PAGE_SHIFT);
317 unsigned long kvaddr;
318 struct page **map;
319 struct page *pg;
320 struct grant_handle_pair *khandle;
321 struct gnttab_unmap_grant_ref unmap[2];
322 int count = 0;
324 /*
325 * If the address is before the start of the grant mapped region or
326 * if vm_file is NULL (meaning mmap failed and we have nothing to do)
327 */
328 if (uvaddr < uvstart || vma->vm_file == NULL)
329 return ptep_get_and_clear_full(vma->vm_mm, uvaddr,
330 ptep, is_fullmm);
332 info = vma->vm_file->private_data;
333 map = vma->vm_private_data;
335 /* TODO Should these be changed to if statements? */
336 BUG_ON(!info);
337 BUG_ON(!info->idx_map);
338 BUG_ON(!map);
340 offset = (int) ((uvaddr - uvstart) >> PAGE_SHIFT);
341 usr_idx = OFFSET_TO_USR_IDX(offset);
342 seg = OFFSET_TO_SEG(offset);
344 pending_idx = MASK_PEND_IDX(ID_TO_IDX(info->idx_map[usr_idx]));
345 mmap_idx = ID_TO_MIDX(info->idx_map[usr_idx]);
347 kvaddr = idx_to_kaddr(mmap_idx, pending_idx, seg);
348 pg = pfn_to_page(__pa(kvaddr) >> PAGE_SHIFT);
349 ClearPageReserved(pg);
350 map[offset + RING_PAGES] = NULL;
352 khandle = &pending_handle(mmap_idx, pending_idx, seg);
354 if (khandle->kernel != INVALID_GRANT_HANDLE) {
355 gnttab_set_unmap_op(&unmap[count], kvaddr,
356 GNTMAP_host_map, khandle->kernel);
357 count++;
359 set_phys_to_machine(__pa(kvaddr) >> PAGE_SHIFT,
360 INVALID_P2M_ENTRY);
361 }
363 if (khandle->user != INVALID_GRANT_HANDLE) {
364 BUG_ON(xen_feature(XENFEAT_auto_translated_physmap));
366 copy = *ptep;
367 gnttab_set_unmap_op(&unmap[count], ptep_to_machine(ptep),
368 GNTMAP_host_map
369 | GNTMAP_application_map
370 | GNTMAP_contains_pte,
371 khandle->user);
372 count++;
373 } else {
374 BUG_ON(!xen_feature(XENFEAT_auto_translated_physmap));
376 /* USING SHADOW PAGE TABLES. */
377 copy = ptep_get_and_clear_full(vma->vm_mm, uvaddr, ptep,
378 is_fullmm);
379 }
381 if (count) {
382 BLKTAP_INVALIDATE_HANDLE(khandle);
383 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
384 unmap, count))
385 BUG();
386 }
388 return copy;
389 }
391 struct vm_operations_struct blktap_vm_ops = {
392 nopage: blktap_nopage,
393 zap_pte: blktap_clear_pte,
394 };
396 /******************************************************************
397 * BLKTAP FILE OPS
398 */
400 /*Function Declarations*/
401 static tap_blkif_t *get_next_free_dev(void);
402 static int blktap_open(struct inode *inode, struct file *filp);
403 static int blktap_release(struct inode *inode, struct file *filp);
404 static int blktap_mmap(struct file *filp, struct vm_area_struct *vma);
405 static int blktap_ioctl(struct inode *inode, struct file *filp,
406 unsigned int cmd, unsigned long arg);
407 static unsigned int blktap_poll(struct file *file, poll_table *wait);
409 static const struct file_operations blktap_fops = {
410 .owner = THIS_MODULE,
411 .poll = blktap_poll,
412 .ioctl = blktap_ioctl,
413 .open = blktap_open,
414 .release = blktap_release,
415 .mmap = blktap_mmap,
416 };
419 static tap_blkif_t *get_next_free_dev(void)
420 {
421 struct class *class;
422 tap_blkif_t *info;
423 int minor;
425 /*
426 * This is called only from the ioctl, which
427 * means we should always have interrupts enabled.
428 */
429 BUG_ON(irqs_disabled());
431 spin_lock_irq(&pending_free_lock);
433 /* tapfds[0] is always NULL */
435 for (minor = 1; minor < blktap_next_minor; minor++) {
436 info = tapfds[minor];
437 /* we could have failed a previous attempt. */
438 if (!info ||
439 ((info->dev_inuse == 0) &&
440 (info->dev_pending == 0)) ) {
441 info->dev_pending = 1;
442 goto found;
443 }
444 }
445 info = NULL;
446 minor = -1;
448 /*
449 * We didn't find free device. If we can still allocate
450 * more, then we grab the next device minor that is
451 * available. This is done while we are still under
452 * the protection of the pending_free_lock.
453 */
454 if (blktap_next_minor < MAX_TAP_DEV)
455 minor = blktap_next_minor++;
456 found:
457 spin_unlock_irq(&pending_free_lock);
459 if (!info && minor > 0) {
460 info = kzalloc(sizeof(*info), GFP_KERNEL);
461 if (unlikely(!info)) {
462 /*
463 * If we failed here, try to put back
464 * the next minor number. But if one
465 * was just taken, then we just lose this
466 * minor. We can try to allocate this
467 * minor again later.
468 */
469 spin_lock_irq(&pending_free_lock);
470 if (blktap_next_minor == minor+1)
471 blktap_next_minor--;
472 spin_unlock_irq(&pending_free_lock);
473 goto out;
474 }
476 info->minor = minor;
477 /*
478 * Make sure that we have a minor before others can
479 * see us.
480 */
481 wmb();
482 tapfds[minor] = info;
484 if ((class = get_xen_class()) != NULL)
485 class_device_create(class, NULL,
486 MKDEV(blktap_major, minor), NULL,
487 "blktap%d", minor);
488 }
490 out:
491 return info;
492 }
494 int dom_to_devid(domid_t domid, int xenbus_id, blkif_t *blkif)
495 {
496 tap_blkif_t *info;
497 int i;
499 for (i = 1; i < blktap_next_minor; i++) {
500 info = tapfds[i];
501 if ( info &&
502 (info->trans.domid == domid) &&
503 (info->trans.busid == xenbus_id) ) {
504 info->blkif = blkif;
505 info->status = RUNNING;
506 return i;
507 }
508 }
509 return -1;
510 }
512 void signal_tapdisk(int idx)
513 {
514 tap_blkif_t *info;
515 struct task_struct *ptask;
517 /*
518 * if the userland tools set things up wrong, this could be negative;
519 * just don't try to signal in this case
520 */
521 if (idx < 0)
522 return;
524 info = tapfds[idx];
525 if ((idx < 0) || (idx > MAX_TAP_DEV) || !info)
526 return;
528 if (info->pid > 0) {
529 ptask = find_task_by_pid(info->pid);
530 if (ptask)
531 info->status = CLEANSHUTDOWN;
532 }
533 info->blkif = NULL;
535 return;
536 }
538 static int blktap_open(struct inode *inode, struct file *filp)
539 {
540 blkif_sring_t *sring;
541 int idx = iminor(inode) - BLKTAP_MINOR;
542 tap_blkif_t *info;
543 int i;
545 /* ctrl device, treat differently */
546 if (!idx)
547 return 0;
549 info = tapfds[idx];
551 if ((idx < 0) || (idx > MAX_TAP_DEV) || !info) {
552 WPRINTK("Unable to open device /dev/xen/blktap%d\n",
553 idx);
554 return -ENODEV;
555 }
557 DPRINTK("Opening device /dev/xen/blktap%d\n",idx);
559 /*Only one process can access device at a time*/
560 if (test_and_set_bit(0, &info->dev_inuse))
561 return -EBUSY;
563 info->dev_pending = 0;
565 /* Allocate the fe ring. */
566 sring = (blkif_sring_t *)get_zeroed_page(GFP_KERNEL);
567 if (sring == NULL)
568 goto fail_nomem;
570 SetPageReserved(virt_to_page(sring));
572 SHARED_RING_INIT(sring);
573 FRONT_RING_INIT(&info->ufe_ring, sring, PAGE_SIZE);
575 filp->private_data = info;
576 info->vma = NULL;
578 info->idx_map = kmalloc(sizeof(unsigned long) * MAX_PENDING_REQS,
579 GFP_KERNEL);
581 if (info->idx_map == NULL)
582 goto fail_nomem;
584 if (idx > 0) {
585 init_waitqueue_head(&info->wait);
586 for (i = 0; i < MAX_PENDING_REQS; i++)
587 info->idx_map[i] = INVALID_REQ;
588 }
590 DPRINTK("Tap open: device /dev/xen/blktap%d\n",idx);
591 return 0;
593 fail_nomem:
594 return -ENOMEM;
595 }
597 static int blktap_release(struct inode *inode, struct file *filp)
598 {
599 tap_blkif_t *info = filp->private_data;
601 /* check for control device */
602 if (!info)
603 return 0;
605 info->dev_inuse = 0;
606 DPRINTK("Freeing device [/dev/xen/blktap%d]\n",info->minor);
608 /* Free the ring page. */
609 ClearPageReserved(virt_to_page(info->ufe_ring.sring));
610 free_page((unsigned long) info->ufe_ring.sring);
612 /* Clear any active mappings and free foreign map table */
613 if (info->vma) {
614 struct mm_struct *mm = info->vma->vm_mm;
616 down_write(&mm->mmap_sem);
617 zap_page_range(
618 info->vma, info->vma->vm_start,
619 info->vma->vm_end - info->vma->vm_start, NULL);
620 up_write(&mm->mmap_sem);
622 kfree(info->vma->vm_private_data);
624 info->vma = NULL;
625 }
627 if (info->idx_map) {
628 kfree(info->idx_map);
629 info->idx_map = NULL;
630 }
632 if ( (info->status != CLEANSHUTDOWN) && (info->blkif != NULL) ) {
633 if (info->blkif->xenblkd != NULL) {
634 kthread_stop(info->blkif->xenblkd);
635 info->blkif->xenblkd = NULL;
636 }
637 info->status = CLEANSHUTDOWN;
638 }
640 return 0;
641 }
644 /* Note on mmap:
645 * We need to map pages to user space in a way that will allow the block
646 * subsystem set up direct IO to them. This couldn't be done before, because
647 * there isn't really a sane way to translate a user virtual address down to a
648 * physical address when the page belongs to another domain.
649 *
650 * My first approach was to map the page in to kernel memory, add an entry
651 * for it in the physical frame list (using alloc_lomem_region as in blkback)
652 * and then attempt to map that page up to user space. This is disallowed
653 * by xen though, which realizes that we don't really own the machine frame
654 * underlying the physical page.
655 *
656 * The new approach is to provide explicit support for this in xen linux.
657 * The VMA now has a flag, VM_FOREIGN, to indicate that it contains pages
658 * mapped from other vms. vma->vm_private_data is set up as a mapping
659 * from pages to actual page structs. There is a new clause in get_user_pages
660 * that does the right thing for this sort of mapping.
661 */
662 static int blktap_mmap(struct file *filp, struct vm_area_struct *vma)
663 {
664 int size;
665 struct page **map;
666 int i;
667 tap_blkif_t *info = filp->private_data;
668 int ret;
670 if (info == NULL) {
671 WPRINTK("blktap: mmap, retrieving idx failed\n");
672 return -ENOMEM;
673 }
675 vma->vm_flags |= VM_RESERVED;
676 vma->vm_ops = &blktap_vm_ops;
678 size = vma->vm_end - vma->vm_start;
679 if (size != ((mmap_pages + RING_PAGES) << PAGE_SHIFT)) {
680 WPRINTK("you _must_ map exactly %d pages!\n",
681 mmap_pages + RING_PAGES);
682 return -EAGAIN;
683 }
685 size >>= PAGE_SHIFT;
686 info->rings_vstart = vma->vm_start;
687 info->user_vstart = info->rings_vstart + (RING_PAGES << PAGE_SHIFT);
689 /* Map the ring pages to the start of the region and reserve it. */
690 if (xen_feature(XENFEAT_auto_translated_physmap))
691 ret = vm_insert_page(vma, vma->vm_start,
692 virt_to_page(info->ufe_ring.sring));
693 else
694 ret = remap_pfn_range(vma, vma->vm_start,
695 __pa(info->ufe_ring.sring) >> PAGE_SHIFT,
696 PAGE_SIZE, vma->vm_page_prot);
697 if (ret) {
698 WPRINTK("Mapping user ring failed!\n");
699 goto fail;
700 }
702 /* Mark this VM as containing foreign pages, and set up mappings. */
703 map = kzalloc(((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)
704 * sizeof(struct page *),
705 GFP_KERNEL);
706 if (map == NULL) {
707 WPRINTK("Couldn't alloc VM_FOREIGN map.\n");
708 goto fail;
709 }
711 for (i = 0; i < ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT); i++)
712 map[i] = NULL;
714 vma->vm_private_data = map;
715 vma->vm_flags |= VM_FOREIGN;
716 vma->vm_flags |= VM_DONTCOPY;
718 #ifdef CONFIG_X86
719 vma->vm_mm->context.has_foreign_mappings = 1;
720 #endif
722 info->vma = vma;
723 info->ring_ok = 1;
724 return 0;
725 fail:
726 /* Clear any active mappings. */
727 zap_page_range(vma, vma->vm_start,
728 vma->vm_end - vma->vm_start, NULL);
730 return -ENOMEM;
731 }
734 static int blktap_ioctl(struct inode *inode, struct file *filp,
735 unsigned int cmd, unsigned long arg)
736 {
737 tap_blkif_t *info = filp->private_data;
739 switch(cmd) {
740 case BLKTAP_IOCTL_KICK_FE:
741 {
742 /* There are fe messages to process. */
743 return blktap_read_ufe_ring(info);
744 }
745 case BLKTAP_IOCTL_SETMODE:
746 {
747 if (info) {
748 if (BLKTAP_MODE_VALID(arg)) {
749 info->mode = arg;
750 /* XXX: may need to flush rings here. */
751 DPRINTK("blktap: set mode to %lx\n",
752 arg);
753 return 0;
754 }
755 }
756 return 0;
757 }
758 case BLKTAP_IOCTL_PRINT_IDXS:
759 {
760 if (info) {
761 printk("User Rings: \n-----------\n");
762 printk("UF: rsp_cons: %2d, req_prod_prv: %2d "
763 "| req_prod: %2d, rsp_prod: %2d\n",
764 info->ufe_ring.rsp_cons,
765 info->ufe_ring.req_prod_pvt,
766 info->ufe_ring.sring->req_prod,
767 info->ufe_ring.sring->rsp_prod);
768 }
769 return 0;
770 }
771 case BLKTAP_IOCTL_SENDPID:
772 {
773 if (info) {
774 info->pid = (pid_t)arg;
775 DPRINTK("blktap: pid received %d\n",
776 info->pid);
777 }
778 return 0;
779 }
780 case BLKTAP_IOCTL_NEWINTF:
781 {
782 uint64_t val = (uint64_t)arg;
783 domid_translate_t *tr = (domid_translate_t *)&val;
785 DPRINTK("NEWINTF Req for domid %d and bus id %d\n",
786 tr->domid, tr->busid);
787 info = get_next_free_dev();
788 if (!info) {
789 WPRINTK("Error initialising /dev/xen/blktap - "
790 "No more devices\n");
791 return -1;
792 }
793 info->trans.domid = tr->domid;
794 info->trans.busid = tr->busid;
795 return info->minor;
796 }
797 case BLKTAP_IOCTL_NEWINTF_EXT:
798 {
799 void __user *udata = (void __user *) arg;
800 domid_translate_ext_t tr;
802 if (copy_from_user(&tr, udata, sizeof(domid_translate_ext_t)))
803 return -EFAULT;
805 DPRINTK("NEWINTF_EXT Req for domid %d and bus id %d\n",
806 tr.domid, tr.busid);
807 info = get_next_free_dev();
808 if (!info) {
809 WPRINTK("Error initialising /dev/xen/blktap - "
810 "No more devices\n");
811 return -1;
812 }
813 info->trans.domid = tr.domid;
814 info->trans.busid = tr.busid;
815 return info->minor;
816 }
817 case BLKTAP_IOCTL_FREEINTF:
818 {
819 unsigned long dev = arg;
820 unsigned long flags;
822 info = tapfds[dev];
824 if ((dev > MAX_TAP_DEV) || !info)
825 return 0; /* should this be an error? */
827 spin_lock_irqsave(&pending_free_lock, flags);
828 if (info->dev_pending)
829 info->dev_pending = 0;
830 spin_unlock_irqrestore(&pending_free_lock, flags);
832 return 0;
833 }
834 case BLKTAP_IOCTL_MINOR:
835 {
836 unsigned long dev = arg;
838 info = tapfds[dev];
840 if ((dev > MAX_TAP_DEV) || !info)
841 return -EINVAL;
843 return info->minor;
844 }
845 case BLKTAP_IOCTL_MAJOR:
846 return blktap_major;
848 case BLKTAP_QUERY_ALLOC_REQS:
849 {
850 WPRINTK("BLKTAP_QUERY_ALLOC_REQS ioctl: %d/%d\n",
851 alloc_pending_reqs, blkif_reqs);
852 return (alloc_pending_reqs/blkif_reqs) * 100;
853 }
854 }
855 return -ENOIOCTLCMD;
856 }
858 static unsigned int blktap_poll(struct file *filp, poll_table *wait)
859 {
860 tap_blkif_t *info = filp->private_data;
862 /* do not work on the control device */
863 if (!info)
864 return 0;
866 poll_wait(filp, &info->wait, wait);
867 if (info->ufe_ring.req_prod_pvt != info->ufe_ring.sring->req_prod) {
868 RING_PUSH_REQUESTS(&info->ufe_ring);
869 return POLLIN | POLLRDNORM;
870 }
871 return 0;
872 }
874 void blktap_kick_user(int idx)
875 {
876 tap_blkif_t *info;
878 info = tapfds[idx];
880 if ((idx < 0) || (idx > MAX_TAP_DEV) || !info)
881 return;
883 wake_up_interruptible(&info->wait);
885 return;
886 }
888 static int do_block_io_op(blkif_t *blkif);
889 static void dispatch_rw_block_io(blkif_t *blkif,
890 blkif_request_t *req,
891 pending_req_t *pending_req);
892 static void make_response(blkif_t *blkif, u64 id,
893 unsigned short op, int st);
895 /******************************************************************
896 * misc small helpers
897 */
898 static int req_increase(void)
899 {
900 int i, j;
902 if (mmap_alloc >= MAX_PENDING_REQS || mmap_lock)
903 return -EINVAL;
905 pending_reqs[mmap_alloc] = kzalloc(sizeof(pending_req_t)
906 * blkif_reqs, GFP_KERNEL);
907 foreign_pages[mmap_alloc] = alloc_empty_pages_and_pagevec(mmap_pages);
909 if (!pending_reqs[mmap_alloc] || !foreign_pages[mmap_alloc])
910 goto out_of_memory;
912 DPRINTK("%s: reqs=%d, pages=%d\n",
913 __FUNCTION__, blkif_reqs, mmap_pages);
915 for (i = 0; i < MAX_PENDING_REQS; i++) {
916 list_add_tail(&pending_reqs[mmap_alloc][i].free_list,
917 &pending_free);
918 pending_reqs[mmap_alloc][i].mem_idx = mmap_alloc;
919 for (j = 0; j < BLKIF_MAX_SEGMENTS_PER_REQUEST; j++)
920 BLKTAP_INVALIDATE_HANDLE(&pending_handle(mmap_alloc,
921 i, j));
922 }
924 mmap_alloc++;
925 DPRINTK("# MMAPs increased to %d\n",mmap_alloc);
926 return 0;
928 out_of_memory:
929 free_empty_pages_and_pagevec(foreign_pages[mmap_alloc], mmap_pages);
930 kfree(pending_reqs[mmap_alloc]);
931 WPRINTK("%s: out of memory\n", __FUNCTION__);
932 return -ENOMEM;
933 }
935 static void mmap_req_del(int mmap)
936 {
937 BUG_ON(!spin_is_locked(&pending_free_lock));
939 kfree(pending_reqs[mmap]);
940 pending_reqs[mmap] = NULL;
942 free_empty_pages_and_pagevec(foreign_pages[mmap_alloc], mmap_pages);
943 foreign_pages[mmap] = NULL;
945 mmap_lock = 0;
946 DPRINTK("# MMAPs decreased to %d\n",mmap_alloc);
947 mmap_alloc--;
948 }
950 static pending_req_t* alloc_req(void)
951 {
952 pending_req_t *req = NULL;
953 unsigned long flags;
955 spin_lock_irqsave(&pending_free_lock, flags);
957 if (!list_empty(&pending_free)) {
958 req = list_entry(pending_free.next, pending_req_t, free_list);
959 list_del(&req->free_list);
960 }
962 if (req) {
963 req->inuse = 1;
964 alloc_pending_reqs++;
965 }
966 spin_unlock_irqrestore(&pending_free_lock, flags);
968 return req;
969 }
971 static void free_req(pending_req_t *req)
972 {
973 unsigned long flags;
974 int was_empty;
976 spin_lock_irqsave(&pending_free_lock, flags);
978 alloc_pending_reqs--;
979 req->inuse = 0;
980 if (mmap_lock && (req->mem_idx == mmap_alloc-1)) {
981 mmap_inuse--;
982 if (mmap_inuse == 0) mmap_req_del(mmap_alloc-1);
983 spin_unlock_irqrestore(&pending_free_lock, flags);
984 return;
985 }
986 was_empty = list_empty(&pending_free);
987 list_add(&req->free_list, &pending_free);
989 spin_unlock_irqrestore(&pending_free_lock, flags);
991 if (was_empty)
992 wake_up(&pending_free_wq);
993 }
995 static void fast_flush_area(pending_req_t *req, int k_idx, int u_idx,
996 int tapidx)
997 {
998 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST*2];
999 unsigned int i, invcount = 0, locked = 0;
1000 struct grant_handle_pair *khandle;
1001 uint64_t ptep;
1002 int ret, mmap_idx;
1003 unsigned long kvaddr, uvaddr;
1004 tap_blkif_t *info;
1005 struct mm_struct *mm;
1008 info = tapfds[tapidx];
1010 if ((tapidx < 0) || (tapidx > MAX_TAP_DEV) || !info) {
1011 WPRINTK("fast_flush: Couldn't get info!\n");
1012 return;
1015 mm = info->vma ? info->vma->vm_mm : NULL;
1017 if (info->vma != NULL &&
1018 xen_feature(XENFEAT_auto_translated_physmap)) {
1019 down_write(&mm->mmap_sem);
1020 zap_page_range(info->vma,
1021 MMAP_VADDR(info->user_vstart, u_idx, 0),
1022 req->nr_pages << PAGE_SHIFT, NULL);
1023 up_write(&mm->mmap_sem);
1024 return;
1027 mmap_idx = req->mem_idx;
1029 for (i = 0; i < req->nr_pages; i++) {
1030 kvaddr = idx_to_kaddr(mmap_idx, k_idx, i);
1031 uvaddr = MMAP_VADDR(info->user_vstart, u_idx, i);
1033 khandle = &pending_handle(mmap_idx, k_idx, i);
1035 if (khandle->kernel != INVALID_GRANT_HANDLE) {
1036 gnttab_set_unmap_op(&unmap[invcount],
1037 idx_to_kaddr(mmap_idx, k_idx, i),
1038 GNTMAP_host_map, khandle->kernel);
1039 invcount++;
1041 set_phys_to_machine(
1042 __pa(idx_to_kaddr(mmap_idx, k_idx, i))
1043 >> PAGE_SHIFT, INVALID_P2M_ENTRY);
1046 if (khandle->user != INVALID_GRANT_HANDLE) {
1047 BUG_ON(xen_feature(XENFEAT_auto_translated_physmap));
1048 if (!locked++)
1049 down_write(&mm->mmap_sem);
1050 if (create_lookup_pte_addr(
1051 mm,
1052 MMAP_VADDR(info->user_vstart, u_idx, i),
1053 &ptep) !=0) {
1054 up_write(&mm->mmap_sem);
1055 WPRINTK("Couldn't get a pte addr!\n");
1056 return;
1059 gnttab_set_unmap_op(&unmap[invcount], ptep,
1060 GNTMAP_host_map
1061 | GNTMAP_application_map
1062 | GNTMAP_contains_pte,
1063 khandle->user);
1064 invcount++;
1067 BLKTAP_INVALIDATE_HANDLE(khandle);
1069 ret = HYPERVISOR_grant_table_op(
1070 GNTTABOP_unmap_grant_ref, unmap, invcount);
1071 BUG_ON(ret);
1073 if (info->vma != NULL &&
1074 !xen_feature(XENFEAT_auto_translated_physmap)) {
1075 if (!locked++)
1076 down_write(&mm->mmap_sem);
1077 zap_page_range(info->vma,
1078 MMAP_VADDR(info->user_vstart, u_idx, 0),
1079 req->nr_pages << PAGE_SHIFT, NULL);
1082 if (locked)
1083 up_write(&mm->mmap_sem);
1086 /******************************************************************
1087 * SCHEDULER FUNCTIONS
1088 */
1090 static void print_stats(blkif_t *blkif)
1092 printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d\n",
1093 current->comm, blkif->st_oo_req,
1094 blkif->st_rd_req, blkif->st_wr_req);
1095 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
1096 blkif->st_rd_req = 0;
1097 blkif->st_wr_req = 0;
1098 blkif->st_oo_req = 0;
1101 int tap_blkif_schedule(void *arg)
1103 blkif_t *blkif = arg;
1105 blkif_get(blkif);
1107 if (debug_lvl)
1108 printk(KERN_DEBUG "%s: started\n", current->comm);
1110 while (!kthread_should_stop()) {
1111 if (try_to_freeze())
1112 continue;
1114 wait_event_interruptible(
1115 blkif->wq,
1116 blkif->waiting_reqs || kthread_should_stop());
1117 wait_event_interruptible(
1118 pending_free_wq,
1119 !list_empty(&pending_free) || kthread_should_stop());
1121 blkif->waiting_reqs = 0;
1122 smp_mb(); /* clear flag *before* checking for work */
1124 if (do_block_io_op(blkif))
1125 blkif->waiting_reqs = 1;
1127 if (log_stats && time_after(jiffies, blkif->st_print))
1128 print_stats(blkif);
1131 if (log_stats)
1132 print_stats(blkif);
1133 if (debug_lvl)
1134 printk(KERN_DEBUG "%s: exiting\n", current->comm);
1136 blkif->xenblkd = NULL;
1137 blkif_put(blkif);
1139 return 0;
1142 /******************************************************************
1143 * COMPLETION CALLBACK -- Called by user level ioctl()
1144 */
1146 static int blktap_read_ufe_ring(tap_blkif_t *info)
1148 /* This is called to read responses from the UFE ring. */
1149 RING_IDX i, j, rp;
1150 blkif_response_t *resp;
1151 blkif_t *blkif=NULL;
1152 int pending_idx, usr_idx, mmap_idx;
1153 pending_req_t *pending_req;
1155 if (!info)
1156 return 0;
1158 /* We currently only forward packets in INTERCEPT_FE mode. */
1159 if (!(info->mode & BLKTAP_MODE_INTERCEPT_FE))
1160 return 0;
1162 /* for each outstanding message on the UFEring */
1163 rp = info->ufe_ring.sring->rsp_prod;
1164 rmb();
1166 for (i = info->ufe_ring.rsp_cons; i != rp; i++) {
1167 blkif_response_t res;
1168 resp = RING_GET_RESPONSE(&info->ufe_ring, i);
1169 memcpy(&res, resp, sizeof(res));
1170 mb(); /* rsp_cons read by RING_FULL() in do_block_io_op(). */
1171 ++info->ufe_ring.rsp_cons;
1173 /*retrieve [usr_idx] to [mmap_idx,pending_idx] mapping*/
1174 usr_idx = (int)res.id;
1175 pending_idx = MASK_PEND_IDX(ID_TO_IDX(info->idx_map[usr_idx]));
1176 mmap_idx = ID_TO_MIDX(info->idx_map[usr_idx]);
1178 if ( (mmap_idx >= mmap_alloc) ||
1179 (ID_TO_IDX(info->idx_map[usr_idx]) >= MAX_PENDING_REQS) )
1180 WPRINTK("Incorrect req map"
1181 "[%d], internal map [%d,%d (%d)]\n",
1182 usr_idx, mmap_idx,
1183 ID_TO_IDX(info->idx_map[usr_idx]),
1184 MASK_PEND_IDX(
1185 ID_TO_IDX(info->idx_map[usr_idx])));
1187 pending_req = &pending_reqs[mmap_idx][pending_idx];
1188 blkif = pending_req->blkif;
1190 for (j = 0; j < pending_req->nr_pages; j++) {
1192 unsigned long kvaddr, uvaddr;
1193 struct page **map = info->vma->vm_private_data;
1194 struct page *pg;
1195 int offset;
1197 uvaddr = MMAP_VADDR(info->user_vstart, usr_idx, j);
1198 kvaddr = idx_to_kaddr(mmap_idx, pending_idx, j);
1200 pg = pfn_to_page(__pa(kvaddr) >> PAGE_SHIFT);
1201 ClearPageReserved(pg);
1202 offset = (uvaddr - info->vma->vm_start)
1203 >> PAGE_SHIFT;
1204 map[offset] = NULL;
1206 fast_flush_area(pending_req, pending_idx, usr_idx, info->minor);
1207 info->idx_map[usr_idx] = INVALID_REQ;
1208 make_response(blkif, pending_req->id, res.operation,
1209 res.status);
1210 blkif_put(pending_req->blkif);
1211 free_req(pending_req);
1214 return 0;
1218 /******************************************************************************
1219 * NOTIFICATION FROM GUEST OS.
1220 */
1222 static void blkif_notify_work(blkif_t *blkif)
1224 blkif->waiting_reqs = 1;
1225 wake_up(&blkif->wq);
1228 irqreturn_t tap_blkif_be_int(int irq, void *dev_id, struct pt_regs *regs)
1230 blkif_notify_work(dev_id);
1231 return IRQ_HANDLED;
1236 /******************************************************************
1237 * DOWNWARD CALLS -- These interface with the block-device layer proper.
1238 */
1239 static int print_dbug = 1;
1240 static int do_block_io_op(blkif_t *blkif)
1242 blkif_back_rings_t *blk_rings = &blkif->blk_rings;
1243 blkif_request_t req;
1244 pending_req_t *pending_req;
1245 RING_IDX rc, rp;
1246 int more_to_do = 0;
1247 tap_blkif_t *info;
1249 rc = blk_rings->common.req_cons;
1250 rp = blk_rings->common.sring->req_prod;
1251 rmb(); /* Ensure we see queued requests up to 'rp'. */
1253 /*Check blkif has corresponding UE ring*/
1254 if (blkif->dev_num < 0) {
1255 /*oops*/
1256 if (print_dbug) {
1257 WPRINTK("Corresponding UE "
1258 "ring does not exist!\n");
1259 print_dbug = 0; /*We only print this message once*/
1261 return 0;
1264 info = tapfds[blkif->dev_num];
1266 if (blkif->dev_num > MAX_TAP_DEV || !info || !info->dev_inuse) {
1267 if (print_dbug) {
1268 WPRINTK("Can't get UE info!\n");
1269 print_dbug = 0;
1271 return 0;
1274 while (rc != rp) {
1276 if (RING_FULL(&info->ufe_ring)) {
1277 WPRINTK("RING_FULL! More to do\n");
1278 more_to_do = 1;
1279 break;
1282 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc)) {
1283 WPRINTK("RING_REQUEST_CONS_OVERFLOW!"
1284 " More to do\n");
1285 more_to_do = 1;
1286 break;
1289 if (kthread_should_stop()) {
1290 more_to_do = 1;
1291 break;
1294 pending_req = alloc_req();
1295 if (NULL == pending_req) {
1296 blkif->st_oo_req++;
1297 more_to_do = 1;
1298 break;
1301 switch (blkif->blk_protocol) {
1302 case BLKIF_PROTOCOL_NATIVE:
1303 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc),
1304 sizeof(req));
1305 break;
1306 case BLKIF_PROTOCOL_X86_32:
1307 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
1308 break;
1309 case BLKIF_PROTOCOL_X86_64:
1310 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
1311 break;
1312 default:
1313 BUG();
1315 blk_rings->common.req_cons = ++rc; /* before make_response() */
1317 /* Apply all sanity checks to /private copy/ of request. */
1318 barrier();
1320 switch (req.operation) {
1321 case BLKIF_OP_READ:
1322 blkif->st_rd_req++;
1323 dispatch_rw_block_io(blkif, &req, pending_req);
1324 break;
1326 case BLKIF_OP_WRITE:
1327 blkif->st_wr_req++;
1328 dispatch_rw_block_io(blkif, &req, pending_req);
1329 break;
1331 default:
1332 /* A good sign something is wrong: sleep for a while to
1333 * avoid excessive CPU consumption by a bad guest. */
1334 msleep(1);
1335 WPRINTK("unknown operation [%d]\n",
1336 req.operation);
1337 make_response(blkif, req.id, req.operation,
1338 BLKIF_RSP_ERROR);
1339 free_req(pending_req);
1340 break;
1343 /* Yield point for this unbounded loop. */
1344 cond_resched();
1347 blktap_kick_user(blkif->dev_num);
1349 return more_to_do;
1352 static void dispatch_rw_block_io(blkif_t *blkif,
1353 blkif_request_t *req,
1354 pending_req_t *pending_req)
1356 extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]);
1357 int op, operation = (req->operation == BLKIF_OP_WRITE) ? WRITE : READ;
1358 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST*2];
1359 unsigned int nseg;
1360 int ret, i, nr_sects = 0;
1361 tap_blkif_t *info;
1362 blkif_request_t *target;
1363 int pending_idx = RTN_PEND_IDX(pending_req,pending_req->mem_idx);
1364 int usr_idx;
1365 uint16_t mmap_idx = pending_req->mem_idx;
1366 struct mm_struct *mm;
1368 if (blkif->dev_num < 0 || blkif->dev_num > MAX_TAP_DEV)
1369 goto fail_response;
1371 info = tapfds[blkif->dev_num];
1372 if (info == NULL)
1373 goto fail_response;
1375 /* Check we have space on user ring - should never fail. */
1376 usr_idx = GET_NEXT_REQ(info->idx_map);
1377 if (usr_idx == INVALID_REQ) {
1378 BUG();
1379 goto fail_response;
1382 /* Check that number of segments is sane. */
1383 nseg = req->nr_segments;
1384 if ( unlikely(nseg == 0) ||
1385 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) ) {
1386 WPRINTK("Bad number of segments in request (%d)\n", nseg);
1387 goto fail_response;
1390 /* Make sure userspace is ready. */
1391 if (!info->ring_ok) {
1392 WPRINTK("blktap: ring not ready for requests!\n");
1393 goto fail_response;
1396 if (RING_FULL(&info->ufe_ring)) {
1397 WPRINTK("blktap: fe_ring is full, can't add "
1398 "IO Request will be dropped. %d %d\n",
1399 RING_SIZE(&info->ufe_ring),
1400 RING_SIZE(&blkif->blk_rings.common));
1401 goto fail_response;
1404 pending_req->blkif = blkif;
1405 pending_req->id = req->id;
1406 pending_req->operation = operation;
1407 pending_req->status = BLKIF_RSP_OKAY;
1408 pending_req->nr_pages = nseg;
1409 op = 0;
1410 mm = info->vma->vm_mm;
1411 if (!xen_feature(XENFEAT_auto_translated_physmap))
1412 down_write(&mm->mmap_sem);
1413 for (i = 0; i < nseg; i++) {
1414 unsigned long uvaddr;
1415 unsigned long kvaddr;
1416 uint64_t ptep;
1417 uint32_t flags;
1419 uvaddr = MMAP_VADDR(info->user_vstart, usr_idx, i);
1420 kvaddr = idx_to_kaddr(mmap_idx, pending_idx, i);
1422 flags = GNTMAP_host_map;
1423 if (operation == WRITE)
1424 flags |= GNTMAP_readonly;
1425 gnttab_set_map_op(&map[op], kvaddr, flags,
1426 req->seg[i].gref, blkif->domid);
1427 op++;
1429 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1430 /* Now map it to user. */
1431 ret = create_lookup_pte_addr(mm, uvaddr, &ptep);
1432 if (ret) {
1433 up_write(&mm->mmap_sem);
1434 WPRINTK("Couldn't get a pte addr!\n");
1435 goto fail_flush;
1438 flags = GNTMAP_host_map | GNTMAP_application_map
1439 | GNTMAP_contains_pte;
1440 if (operation == WRITE)
1441 flags |= GNTMAP_readonly;
1442 gnttab_set_map_op(&map[op], ptep, flags,
1443 req->seg[i].gref, blkif->domid);
1444 op++;
1447 nr_sects += (req->seg[i].last_sect -
1448 req->seg[i].first_sect + 1);
1451 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, op);
1452 BUG_ON(ret);
1454 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1455 up_write(&mm->mmap_sem);
1457 for (i = 0; i < (nseg*2); i+=2) {
1458 unsigned long uvaddr;
1459 unsigned long kvaddr;
1460 unsigned long offset;
1461 struct page *pg;
1463 uvaddr = MMAP_VADDR(info->user_vstart, usr_idx, i/2);
1464 kvaddr = idx_to_kaddr(mmap_idx, pending_idx, i/2);
1466 if (unlikely(map[i].status != 0)) {
1467 WPRINTK("invalid kernel buffer -- "
1468 "could not remap it\n");
1469 ret |= 1;
1470 map[i].handle = INVALID_GRANT_HANDLE;
1473 if (unlikely(map[i+1].status != 0)) {
1474 WPRINTK("invalid user buffer -- "
1475 "could not remap it\n");
1476 ret |= 1;
1477 map[i+1].handle = INVALID_GRANT_HANDLE;
1480 pending_handle(mmap_idx, pending_idx, i/2).kernel
1481 = map[i].handle;
1482 pending_handle(mmap_idx, pending_idx, i/2).user
1483 = map[i+1].handle;
1485 if (ret)
1486 continue;
1488 set_phys_to_machine(__pa(kvaddr) >> PAGE_SHIFT,
1489 FOREIGN_FRAME(map[i].dev_bus_addr
1490 >> PAGE_SHIFT));
1491 offset = (uvaddr - info->vma->vm_start) >> PAGE_SHIFT;
1492 pg = pfn_to_page(__pa(kvaddr) >> PAGE_SHIFT);
1493 ((struct page **)info->vma->vm_private_data)[offset] =
1494 pg;
1496 } else {
1497 for (i = 0; i < nseg; i++) {
1498 unsigned long uvaddr;
1499 unsigned long kvaddr;
1500 unsigned long offset;
1501 struct page *pg;
1503 uvaddr = MMAP_VADDR(info->user_vstart, usr_idx, i);
1504 kvaddr = idx_to_kaddr(mmap_idx, pending_idx, i);
1506 if (unlikely(map[i].status != 0)) {
1507 WPRINTK("invalid kernel buffer -- "
1508 "could not remap it\n");
1509 ret |= 1;
1510 map[i].handle = INVALID_GRANT_HANDLE;
1513 pending_handle(mmap_idx, pending_idx, i).kernel
1514 = map[i].handle;
1516 if (ret)
1517 continue;
1519 offset = (uvaddr - info->vma->vm_start) >> PAGE_SHIFT;
1520 pg = pfn_to_page(__pa(kvaddr) >> PAGE_SHIFT);
1521 ((struct page **)info->vma->vm_private_data)[offset] =
1522 pg;
1526 if (ret)
1527 goto fail_flush;
1529 if (xen_feature(XENFEAT_auto_translated_physmap))
1530 down_write(&mm->mmap_sem);
1531 /* Mark mapped pages as reserved: */
1532 for (i = 0; i < req->nr_segments; i++) {
1533 unsigned long kvaddr;
1534 struct page *pg;
1536 kvaddr = idx_to_kaddr(mmap_idx, pending_idx, i);
1537 pg = pfn_to_page(__pa(kvaddr) >> PAGE_SHIFT);
1538 SetPageReserved(pg);
1539 if (xen_feature(XENFEAT_auto_translated_physmap)) {
1540 ret = vm_insert_page(info->vma,
1541 MMAP_VADDR(info->user_vstart,
1542 usr_idx, i), pg);
1543 if (ret) {
1544 up_write(&mm->mmap_sem);
1545 goto fail_flush;
1549 if (xen_feature(XENFEAT_auto_translated_physmap))
1550 up_write(&mm->mmap_sem);
1552 /*record [mmap_idx,pending_idx] to [usr_idx] mapping*/
1553 info->idx_map[usr_idx] = MAKE_ID(mmap_idx, pending_idx);
1555 blkif_get(blkif);
1556 /* Finally, write the request message to the user ring. */
1557 target = RING_GET_REQUEST(&info->ufe_ring,
1558 info->ufe_ring.req_prod_pvt);
1559 memcpy(target, req, sizeof(*req));
1560 target->id = usr_idx;
1561 wmb(); /* blktap_poll() reads req_prod_pvt asynchronously */
1562 info->ufe_ring.req_prod_pvt++;
1564 if (operation == READ)
1565 blkif->st_rd_sect += nr_sects;
1566 else if (operation == WRITE)
1567 blkif->st_wr_sect += nr_sects;
1569 return;
1571 fail_flush:
1572 WPRINTK("Reached Fail_flush\n");
1573 fast_flush_area(pending_req, pending_idx, usr_idx, blkif->dev_num);
1574 fail_response:
1575 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
1576 free_req(pending_req);
1577 msleep(1); /* back off a bit */
1582 /******************************************************************
1583 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
1584 */
1587 static void make_response(blkif_t *blkif, u64 id,
1588 unsigned short op, int st)
1590 blkif_response_t resp;
1591 unsigned long flags;
1592 blkif_back_rings_t *blk_rings = &blkif->blk_rings;
1593 int more_to_do = 0;
1594 int notify;
1596 resp.id = id;
1597 resp.operation = op;
1598 resp.status = st;
1600 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1601 /* Place on the response ring for the relevant domain. */
1602 switch (blkif->blk_protocol) {
1603 case BLKIF_PROTOCOL_NATIVE:
1604 memcpy(RING_GET_RESPONSE(&blk_rings->native,
1605 blk_rings->native.rsp_prod_pvt),
1606 &resp, sizeof(resp));
1607 break;
1608 case BLKIF_PROTOCOL_X86_32:
1609 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32,
1610 blk_rings->x86_32.rsp_prod_pvt),
1611 &resp, sizeof(resp));
1612 break;
1613 case BLKIF_PROTOCOL_X86_64:
1614 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64,
1615 blk_rings->x86_64.rsp_prod_pvt),
1616 &resp, sizeof(resp));
1617 break;
1618 default:
1619 BUG();
1621 blk_rings->common.rsp_prod_pvt++;
1622 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
1624 if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
1625 /*
1626 * Tail check for pending requests. Allows frontend to avoid
1627 * notifications if requests are already in flight (lower
1628 * overheads and promotes batching).
1629 */
1630 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
1631 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
1632 more_to_do = 1;
1635 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
1636 if (more_to_do)
1637 blkif_notify_work(blkif);
1638 if (notify)
1639 notify_remote_via_irq(blkif->irq);
1642 static int __init blkif_init(void)
1644 int i, ret;
1645 struct class *class;
1647 if (!is_running_on_xen())
1648 return -ENODEV;
1650 INIT_LIST_HEAD(&pending_free);
1651 for(i = 0; i < 2; i++) {
1652 ret = req_increase();
1653 if (ret)
1654 break;
1656 if (i == 0)
1657 return ret;
1659 tap_blkif_interface_init();
1661 alloc_pending_reqs = 0;
1663 tap_blkif_xenbus_init();
1665 /* Dynamically allocate a major for this device */
1666 ret = register_chrdev(0, "blktap", &blktap_fops);
1668 if (ret < 0) {
1669 WPRINTK("Couldn't register /dev/xen/blktap\n");
1670 return -ENOMEM;
1673 blktap_major = ret;
1675 /* tapfds[0] is always NULL */
1676 blktap_next_minor++;
1678 DPRINTK("Created misc_dev [/dev/xen/blktap%d]\n",i);
1680 /* Make sure the xen class exists */
1681 if ((class = get_xen_class()) != NULL) {
1682 /*
1683 * This will allow udev to create the blktap ctrl device.
1684 * We only want to create blktap0 first. We don't want
1685 * to flood the sysfs system with needless blktap devices.
1686 * We only create the device when a request of a new device is
1687 * made.
1688 */
1689 class_device_create(class, NULL,
1690 MKDEV(blktap_major, 0), NULL,
1691 "blktap0");
1692 } else {
1693 /* this is bad, but not fatal */
1694 WPRINTK("blktap: sysfs xen_class not created\n");
1697 DPRINTK("Blktap device successfully created\n");
1699 return 0;
1702 module_init(blkif_init);
1704 MODULE_LICENSE("Dual BSD/GPL");