aoe: properly initialise the request_queue's backing_dev_info
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / block / aoe / aoeblk.c
blobad00b3d94711d09b6344ea36cd73ce21418932a1
1 /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
2 /*
3 * aoeblk.c
4 * block device routines
5 */
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/backing-dev.h>
10 #include <linux/fs.h>
11 #include <linux/ioctl.h>
12 #include <linux/genhd.h>
13 #include <linux/netdevice.h>
14 #include "aoe.h"
16 static struct kmem_cache *buf_pool_cache;
18 static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
20 struct aoedev *d = disk->private_data;
22 return snprintf(page, PAGE_SIZE,
23 "%s%s\n",
24 (d->flags & DEVFL_UP) ? "up" : "down",
25 (d->flags & DEVFL_PAUSE) ? ",paused" :
26 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
27 /* I'd rather see nopen exported so we can ditch closewait */
29 static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page)
31 struct aoedev *d = disk->private_data;
33 return snprintf(page, PAGE_SIZE, "%012llx\n",
34 (unsigned long long)mac_addr(d->addr));
36 static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page)
38 struct aoedev *d = disk->private_data;
40 return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
42 /* firmware version */
43 static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page)
45 struct aoedev *d = disk->private_data;
47 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
50 static struct disk_attribute disk_attr_state = {
51 .attr = {.name = "state", .mode = S_IRUGO },
52 .show = aoedisk_show_state
54 static struct disk_attribute disk_attr_mac = {
55 .attr = {.name = "mac", .mode = S_IRUGO },
56 .show = aoedisk_show_mac
58 static struct disk_attribute disk_attr_netif = {
59 .attr = {.name = "netif", .mode = S_IRUGO },
60 .show = aoedisk_show_netif
62 static struct disk_attribute disk_attr_fwver = {
63 .attr = {.name = "firmware-version", .mode = S_IRUGO },
64 .show = aoedisk_show_fwver
67 static struct attribute *aoe_attrs[] = {
68 &disk_attr_state.attr,
69 &disk_attr_mac.attr,
70 &disk_attr_netif.attr,
71 &disk_attr_fwver.attr,
72 NULL
75 static const struct attribute_group attr_group = {
76 .attrs = aoe_attrs,
79 static int
80 aoedisk_add_sysfs(struct aoedev *d)
82 return sysfs_create_group(&d->gd->kobj, &attr_group);
84 void
85 aoedisk_rm_sysfs(struct aoedev *d)
87 sysfs_remove_group(&d->gd->kobj, &attr_group);
90 static int
91 aoeblk_open(struct inode *inode, struct file *filp)
93 struct aoedev *d;
94 ulong flags;
96 d = inode->i_bdev->bd_disk->private_data;
98 spin_lock_irqsave(&d->lock, flags);
99 if (d->flags & DEVFL_UP) {
100 d->nopen++;
101 spin_unlock_irqrestore(&d->lock, flags);
102 return 0;
104 spin_unlock_irqrestore(&d->lock, flags);
105 return -ENODEV;
108 static int
109 aoeblk_release(struct inode *inode, struct file *filp)
111 struct aoedev *d;
112 ulong flags;
114 d = inode->i_bdev->bd_disk->private_data;
116 spin_lock_irqsave(&d->lock, flags);
118 if (--d->nopen == 0) {
119 spin_unlock_irqrestore(&d->lock, flags);
120 aoecmd_cfg(d->aoemajor, d->aoeminor);
121 return 0;
123 spin_unlock_irqrestore(&d->lock, flags);
125 return 0;
128 static int
129 aoeblk_make_request(struct request_queue *q, struct bio *bio)
131 struct aoedev *d;
132 struct buf *buf;
133 struct sk_buff *sl;
134 ulong flags;
136 blk_queue_bounce(q, &bio);
138 d = bio->bi_bdev->bd_disk->private_data;
139 buf = mempool_alloc(d->bufpool, GFP_NOIO);
140 if (buf == NULL) {
141 printk(KERN_INFO "aoe: buf allocation failure\n");
142 bio_endio(bio, -ENOMEM);
143 return 0;
145 memset(buf, 0, sizeof(*buf));
146 INIT_LIST_HEAD(&buf->bufs);
147 buf->start_time = jiffies;
148 buf->bio = bio;
149 buf->resid = bio->bi_size;
150 buf->sector = bio->bi_sector;
151 buf->bv = &bio->bi_io_vec[bio->bi_idx];
152 WARN_ON(buf->bv->bv_len == 0);
153 buf->bv_resid = buf->bv->bv_len;
154 buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
156 spin_lock_irqsave(&d->lock, flags);
158 if ((d->flags & DEVFL_UP) == 0) {
159 printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
160 d->aoemajor, d->aoeminor);
161 spin_unlock_irqrestore(&d->lock, flags);
162 mempool_free(buf, d->bufpool);
163 bio_endio(bio, -ENXIO);
164 return 0;
167 list_add_tail(&buf->bufs, &d->bufq);
169 aoecmd_work(d);
170 sl = d->sendq_hd;
171 d->sendq_hd = d->sendq_tl = NULL;
173 spin_unlock_irqrestore(&d->lock, flags);
174 aoenet_xmit(sl);
176 return 0;
179 static int
180 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
182 struct aoedev *d = bdev->bd_disk->private_data;
184 if ((d->flags & DEVFL_UP) == 0) {
185 printk(KERN_ERR "aoe: disk not up\n");
186 return -ENODEV;
189 geo->cylinders = d->geo.cylinders;
190 geo->heads = d->geo.heads;
191 geo->sectors = d->geo.sectors;
192 return 0;
195 static struct block_device_operations aoe_bdops = {
196 .open = aoeblk_open,
197 .release = aoeblk_release,
198 .getgeo = aoeblk_getgeo,
199 .owner = THIS_MODULE,
202 /* alloc_disk and add_disk can sleep */
203 void
204 aoeblk_gdalloc(void *vp)
206 struct aoedev *d = vp;
207 struct gendisk *gd;
208 ulong flags;
210 gd = alloc_disk(AOE_PARTITIONS);
211 if (gd == NULL) {
212 printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
213 d->aoemajor, d->aoeminor);
214 goto err;
217 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
218 if (d->bufpool == NULL) {
219 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
220 d->aoemajor, d->aoeminor);
221 goto err_disk;
224 blk_queue_make_request(&d->blkq, aoeblk_make_request);
225 if (bdi_init(&d->blkq.backing_dev_info))
226 goto err_mempool;
227 spin_lock_irqsave(&d->lock, flags);
228 gd->major = AOE_MAJOR;
229 gd->first_minor = d->sysminor * AOE_PARTITIONS;
230 gd->fops = &aoe_bdops;
231 gd->private_data = d;
232 gd->capacity = d->ssize;
233 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
234 d->aoemajor, d->aoeminor);
236 gd->queue = &d->blkq;
237 d->gd = gd;
238 d->flags &= ~DEVFL_GDALLOC;
239 d->flags |= DEVFL_UP;
241 spin_unlock_irqrestore(&d->lock, flags);
243 add_disk(gd);
244 aoedisk_add_sysfs(d);
245 return;
247 err_mempool:
248 mempool_destroy(d->bufpool);
249 err_disk:
250 put_disk(gd);
251 err:
252 spin_lock_irqsave(&d->lock, flags);
253 d->flags &= ~DEVFL_GDALLOC;
254 spin_unlock_irqrestore(&d->lock, flags);
257 void
258 aoeblk_exit(void)
260 kmem_cache_destroy(buf_pool_cache);
263 int __init
264 aoeblk_init(void)
266 buf_pool_cache = kmem_cache_create("aoe_bufs",
267 sizeof(struct buf),
268 0, 0, NULL);
269 if (buf_pool_cache == NULL)
270 return -ENOMEM;
272 return 0;