aboutsummaryrefslogtreecommitdiffstats
path: root/xenolinux-2.4.21-pre4-sparse/arch/xeno/drivers/block/xl_segment.c
blob: 6ef1838a2b87b669d4012008df0fd08a86d7d2de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/******************************************************************************
 * xl_segment.c
 * 
 * Xenolinux virtual block-device driver (vhd).
 * 
 */

#include "xl_block.h"

#define MAJOR_NR XLVIRT_MAJOR
#include <linux/blk.h>

/* Copied from linux/ide.h */
typedef unsigned char byte; 

/* We support up to 16 devices of up to 16 partitions each. */
#define XLVIRT_MAX        256
#define XLVIRT_MAJOR_NAME "xvd"
#define VIRT_PARTN_BITS   4
static int xlseg_blksize_size[XLVIRT_MAX];
static int xlseg_hardsect_size[XLVIRT_MAX];
static int xlseg_max_sectors[XLVIRT_MAX];

struct gendisk *xlsegment_gendisk = NULL;

static xen_disk_info_t xlseg_disk_info;

static struct block_device_operations xlsegment_block_fops = 
{
    open:               xenolinux_block_open,
    release:            xenolinux_block_release,
    ioctl:              xenolinux_block_ioctl,
    check_media_change: xenolinux_block_check,
    revalidate:         xenolinux_block_revalidate,
};


int xlsegment_hwsect(int minor) 
{
    return xlseg_hardsect_size[minor]; 
} 


int __init xlseg_init(void)
{
    int i, result, units, minors, disk;
    xen_disk_info_t *xdi = &xlseg_disk_info;
    struct gendisk *gd;

    SET_MODULE_OWNER(&xlsegment_block_fops);

    /* Probe for disk information. */
    memset(xdi, 0, sizeof(*xdi));
    xenolinux_control_msg(XEN_BLOCK_PROBE_SEG, (char *)xdi, sizeof(*xdi));

    DPRINTK("vhd block device probe:\n");
    for ( i = 0; i < xdi->count; i++ )
    { 
	DPRINTK("  %2d: type: %d, capacity: %ld\n",
		i, xdi->disks[i].type, xdi->disks[i].capacity);
    }

    result = register_blkdev(XLVIRT_MAJOR, XLVIRT_MAJOR_NAME,
                             &xlsegment_block_fops);
    if ( result < 0 )
    {
	printk(KERN_ALERT "XL Segment: can't get major %d\n", XLVIRT_MAJOR);
	return result;
    }

    /* Initialize global arrays. */
    for (i = 0; i < XLVIRT_MAX; i++) 
    {
        xlseg_blksize_size[i]  = 512;
        xlseg_hardsect_size[i] = 512;
        xlseg_max_sectors[i]   = 128;
    }

    blk_size[XLVIRT_MAJOR]      = NULL;
    blksize_size[XLVIRT_MAJOR]  = xlseg_blksize_size;
    hardsect_size[XLVIRT_MAJOR] = xlseg_hardsect_size;
    max_sectors[XLVIRT_MAJOR]   = xlseg_max_sectors;
    read_ahead[XLVIRT_MAJOR]    = 8;

    blk_init_queue(BLK_DEFAULT_QUEUE(XLVIRT_MAJOR), do_xlblk_request);

    /*
     * Turn off barking 'headactive' mode. We dequeue buffer heads as
     * soon as we pass them down to Xen.
     */
    blk_queue_headactive(BLK_DEFAULT_QUEUE(XLVIRT_MAJOR), 0);

    /*
     * We may register up to 16 devices in a sparse identifier space.
     * Unlike with IDE and SCSI, we always register a gendisk, as new
     * virtual devices may get allocate dto us later on.
     */
    units = 16;

    /* Construct an appropriate gendisk structure. */
    minors    = units * (1<<VIRT_PARTN_BITS);
    gd        = kmalloc(sizeof(struct gendisk), GFP_KERNEL);
    gd->sizes = kmalloc(minors * sizeof(int), GFP_KERNEL);
    gd->part  = kmalloc(minors * sizeof(struct hd_struct), GFP_KERNEL);
    gd->major        = XLVIRT_MAJOR;
    gd->major_name   = XLVIRT_MAJOR_NAME;
    gd->minor_shift  = VIRT_PARTN_BITS; 
    gd->max_p	     = 1<<VIRT_PARTN_BITS;
    gd->nr_real	     = units;           
    gd->real_devices = kmalloc(units * sizeof(xl_disk_t), GFP_KERNEL);
    gd->next	     = NULL;            
    gd->fops         = &xlsegment_block_fops;
    gd->de_arr       = kmalloc(sizeof(*gd->de_arr) * units, GFP_KERNEL);
    gd->flags	     = kmalloc(sizeof(*gd->flags) * units, GFP_KERNEL);
    memset(gd->sizes, 0, minors * sizeof(int));
    memset(gd->part,  0, minors * sizeof(struct hd_struct));
    memset(gd->de_arr, 0, sizeof(*gd->de_arr) * units);
    memset(gd->flags, 0, sizeof(*gd->flags) * units);
    memset(gd->real_devices, 0, sizeof(xl_disk_t) * units);
    xlsegment_gendisk = gd;
    add_gendisk(gd);

    /* Now register each disk in turn. */
    for ( i = 0; i < xdi->count; i++ )
    {
        disk = xdi->disks[i].device & XENDEV_IDX_MASK;

        /* We can use the first 16 IDE devices. */
        if ( !IS_VIRTUAL_XENDEV(xdi->disks[i].device) || (disk >= 16) )
            continue;

        ((xl_disk_t *)gd->real_devices)[disk].capacity =
            xdi->disks[i].capacity;
        register_disk(gd, 
                      MKDEV(XLVIRT_MAJOR, disk<<VIRT_PARTN_BITS), 
                      1<<VIRT_PARTN_BITS, 
                      &xlsegment_block_fops, 
                      xdi->disks[i].capacity);
    }

    printk(KERN_ALERT 
	   "XenoLinux Virtual Segment Device Driver installed [device: %d]\n",
	   XLVIRT_MAJOR);

    return 0;
}


static void __exit xlseg_cleanup(void)
{
    if ( xlsegment_gendisk == NULL ) return;

    blk_cleanup_queue(BLK_DEFAULT_QUEUE(XLVIRT_MAJOR));

    xlsegment_gendisk = NULL;

    read_ahead[XLVIRT_MAJOR] = 0;

    if ( blksize_size[XLVIRT_MAJOR] != NULL )
    { 
	kfree(blksize_size[XLVIRT_MAJOR]);
        blksize_size[XLVIRT_MAJOR] = NULL;
    }

    if ( hardsect_size[XLVIRT_MAJOR] != NULL )
    { 
	kfree(hardsect_size[XLVIRT_MAJOR]);
        hardsect_size[XLVIRT_MAJOR] = NULL;
    }
    
    if ( max_sectors[XLVIRT_MAJOR] != NULL )
    { 
	kfree(max_sectors[XLVIRT_MAJOR]);
        max_sectors[XLVIRT_MAJOR] = NULL;
    }
    
    if ( unregister_blkdev(XLVIRT_MAJOR, XLVIRT_MAJOR_NAME) != 0 )
    {
	printk(KERN_ALERT
	       "XenoLinux Virtual Segment Device Driver"
               " uninstalled w/ errs\n");
    }
}


#ifdef MODULE
module_init(xlseg_init);
module_exit(xlseg_cleanup);
#endif