ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/blkback/vbd.c @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
line source
1 /******************************************************************************
2 * blkback/vbd.c
3 *
4 * Routines for managing virtual block devices (VBDs).
5 *
6 * Copyright (c) 2003-2005, Keir Fraser & Steve Hand
7 */
9 #include "common.h"
10 #include <asm-xen/xenbus.h>
12 static inline dev_t vbd_map_devnum(blkif_pdev_t cookie)
13 {
14 return MKDEV(BLKIF_MAJOR(cookie), BLKIF_MINOR(cookie));
15 }
16 #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
17 (_v)->bdev->bd_part->nr_sects : (_v)->bdev->bd_disk->capacity)
18 #define bdev_put(_b) blkdev_put(_b)
20 unsigned long vbd_size(struct vbd *vbd)
21 {
22 return vbd_sz(vbd);
23 }
25 unsigned int vbd_info(struct vbd *vbd)
26 {
27 return vbd->type | (vbd->readonly?VDISK_READONLY:0);
28 }
30 unsigned long vbd_secsize(struct vbd *vbd)
31 {
32 return bdev_hardsect_size(vbd->bdev);
33 }
35 int vbd_create(blkif_t *blkif, blkif_vdev_t handle,
36 blkif_pdev_t pdevice, int readonly)
37 {
38 struct vbd *vbd;
40 vbd = &blkif->vbd;
41 vbd->handle = handle;
42 vbd->readonly = readonly;
43 vbd->type = 0;
45 vbd->pdevice = pdevice;
47 vbd->bdev = open_by_devnum(
48 vbd_map_devnum(vbd->pdevice),
49 vbd->readonly ? FMODE_READ : FMODE_WRITE);
50 if ( IS_ERR(vbd->bdev) )
51 {
52 DPRINTK("vbd_creat: device %08x doesn't exist.\n", vbd->pdevice);
53 return -ENOENT;
54 }
56 if ( (vbd->bdev->bd_disk == NULL) )
57 {
58 DPRINTK("vbd_creat: device %08x doesn't exist.\n", vbd->pdevice);
59 vbd_free(vbd);
60 return -ENOENT;
61 }
63 if ( vbd->bdev->bd_disk->flags & GENHD_FL_CD )
64 vbd->type |= VDISK_CDROM;
65 if ( vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE )
66 vbd->type |= VDISK_REMOVABLE;
68 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
69 handle, blkif->domid);
70 return 0;
71 }
73 void vbd_free(struct vbd *vbd)
74 {
75 if (vbd->bdev)
76 bdev_put(vbd->bdev);
77 vbd->bdev = NULL;
78 }
80 int vbd_translate(struct phys_req *req, blkif_t *blkif, int operation)
81 {
82 struct vbd *vbd = &blkif->vbd;
83 int rc = -EACCES;
85 if ((operation == WRITE) && vbd->readonly)
86 goto out;
88 if (unlikely((req->sector_number + req->nr_sects) > vbd_sz(vbd)))
89 goto out;
91 req->dev = vbd->pdevice;
92 req->bdev = vbd->bdev;
93 rc = 0;
95 out:
96 return rc;
97 }