1 /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
4 * block device routines
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/backing-dev.h>
11 #include <linux/ioctl.h>
12 #include <linux/slab.h>
13 #include <linux/genhd.h>
14 #include <linux/netdevice.h>
17 static struct kmem_cache *buf_pool_cache;
19 static ssize_t aoedisk_show_state(struct device *dev,
20 struct device_attribute *attr, char *page)
22 struct gendisk *disk = dev_to_disk(dev);
23 struct aoedev *d = disk->private_data;
25 return snprintf(page, PAGE_SIZE,
27 (d->flags & DEVFL_UP) ? "up" : "down",
28 (d->flags & DEVFL_KICKME) ? ",kickme" :
29 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
30 /* I'd rather see nopen exported so we can ditch closewait */
32 static ssize_t aoedisk_show_mac(struct device *dev,
33 struct device_attribute *attr, char *page)
35 struct gendisk *disk = dev_to_disk(dev);
36 struct aoedev *d = disk->private_data;
37 struct aoetgt *t = d->targets[0];
40 return snprintf(page, PAGE_SIZE, "none\n");
41 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
43 static ssize_t aoedisk_show_netif(struct device *dev,
44 struct device_attribute *attr, char *page)
46 struct gendisk *disk = dev_to_disk(dev);
47 struct aoedev *d = disk->private_data;
48 struct net_device *nds[8], **nd, **nnd, **ne;
49 struct aoetgt **t, **te;
50 struct aoeif *ifp, *e;
53 memset(nds, 0, sizeof nds);
55 ne = nd + ARRAY_SIZE(nds);
58 for (; t < te && *t; t++) {
61 for (; ifp < e && ifp->nd; ifp++) {
62 for (nnd = nds; nnd < nd; nnd++)
65 if (nnd == nd && nd != ne)
73 return snprintf(page, PAGE_SIZE, "none\n");
74 for (p = page; nd < ne; nd++)
75 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
76 p == page ? "" : ",", (*nd)->name);
77 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
80 /* firmware version */
81 static ssize_t aoedisk_show_fwver(struct device *dev,
82 struct device_attribute *attr, char *page)
84 struct gendisk *disk = dev_to_disk(dev);
85 struct aoedev *d = disk->private_data;
87 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
90 static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
91 static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
92 static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
93 static struct device_attribute dev_attr_firmware_version = {
94 .attr = { .name = "firmware-version", .mode = S_IRUGO },
95 .show = aoedisk_show_fwver,
98 static struct attribute *aoe_attrs[] = {
101 &dev_attr_netif.attr,
102 &dev_attr_firmware_version.attr,
106 static const struct attribute_group attr_group = {
111 aoedisk_add_sysfs(struct aoedev *d)
113 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
116 aoedisk_rm_sysfs(struct aoedev *d)
118 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
122 aoeblk_open(struct block_device *bdev, fmode_t mode)
124 struct aoedev *d = bdev->bd_disk->private_data;
127 spin_lock_irqsave(&d->lock, flags);
128 if (d->flags & DEVFL_UP) {
130 spin_unlock_irqrestore(&d->lock, flags);
133 spin_unlock_irqrestore(&d->lock, flags);
138 aoeblk_release(struct gendisk *disk, fmode_t mode)
140 struct aoedev *d = disk->private_data;
143 spin_lock_irqsave(&d->lock, flags);
145 if (--d->nopen == 0) {
146 spin_unlock_irqrestore(&d->lock, flags);
147 aoecmd_cfg(d->aoemajor, d->aoeminor);
150 spin_unlock_irqrestore(&d->lock, flags);
156 aoeblk_make_request(struct request_queue *q, struct bio *bio)
158 struct sk_buff_head queue;
163 blk_queue_bounce(q, &bio);
166 printk(KERN_ERR "aoe: bio is NULL\n");
170 d = bio->bi_bdev->bd_disk->private_data;
172 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
174 bio_endio(bio, -ENXIO);
176 } else if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
177 bio_endio(bio, -EOPNOTSUPP);
179 } else if (bio->bi_io_vec == NULL) {
180 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
182 bio_endio(bio, -ENXIO);
185 buf = mempool_alloc(d->bufpool, GFP_NOIO);
187 printk(KERN_INFO "aoe: buf allocation failure\n");
188 bio_endio(bio, -ENOMEM);
191 memset(buf, 0, sizeof(*buf));
192 INIT_LIST_HEAD(&buf->bufs);
193 buf->stime = jiffies;
195 buf->resid = bio->bi_size;
196 buf->sector = bio->bi_sector;
197 buf->bv = &bio->bi_io_vec[bio->bi_idx];
198 buf->bv_resid = buf->bv->bv_len;
199 WARN_ON(buf->bv_resid == 0);
200 buf->bv_off = buf->bv->bv_offset;
202 spin_lock_irqsave(&d->lock, flags);
204 if ((d->flags & DEVFL_UP) == 0) {
205 printk(KERN_INFO "aoe: device %ld.%d is not up\n",
206 d->aoemajor, d->aoeminor);
207 spin_unlock_irqrestore(&d->lock, flags);
208 mempool_free(buf, d->bufpool);
209 bio_endio(bio, -ENXIO);
213 list_add_tail(&buf->bufs, &d->bufq);
216 __skb_queue_head_init(&queue);
217 skb_queue_splice_init(&d->sendq, &queue);
219 spin_unlock_irqrestore(&d->lock, flags);
226 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
228 struct aoedev *d = bdev->bd_disk->private_data;
230 if ((d->flags & DEVFL_UP) == 0) {
231 printk(KERN_ERR "aoe: disk not up\n");
235 geo->cylinders = d->geo.cylinders;
236 geo->heads = d->geo.heads;
237 geo->sectors = d->geo.sectors;
241 static const struct block_device_operations aoe_bdops = {
243 .release = aoeblk_release,
244 .getgeo = aoeblk_getgeo,
245 .owner = THIS_MODULE,
248 /* alloc_disk and add_disk can sleep */
250 aoeblk_gdalloc(void *vp)
252 struct aoedev *d = vp;
256 gd = alloc_disk(AOE_PARTITIONS);
259 "aoe: cannot allocate disk structure for %ld.%d\n",
260 d->aoemajor, d->aoeminor);
264 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
265 if (d->bufpool == NULL) {
266 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
267 d->aoemajor, d->aoeminor);
271 d->blkq = blk_alloc_queue(GFP_KERNEL);
274 blk_queue_make_request(d->blkq, aoeblk_make_request);
275 d->blkq->backing_dev_info.name = "aoe";
276 if (bdi_init(&d->blkq->backing_dev_info))
278 spin_lock_irqsave(&d->lock, flags);
279 gd->major = AOE_MAJOR;
280 gd->first_minor = d->sysminor * AOE_PARTITIONS;
281 gd->fops = &aoe_bdops;
282 gd->private_data = d;
283 set_capacity(gd, d->ssize);
284 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
285 d->aoemajor, d->aoeminor);
289 d->flags &= ~DEVFL_GDALLOC;
290 d->flags |= DEVFL_UP;
292 spin_unlock_irqrestore(&d->lock, flags);
295 aoedisk_add_sysfs(d);
299 blk_cleanup_queue(d->blkq);
302 mempool_destroy(d->bufpool);
306 spin_lock_irqsave(&d->lock, flags);
307 d->flags &= ~DEVFL_GDALLOC;
308 spin_unlock_irqrestore(&d->lock, flags);
314 kmem_cache_destroy(buf_pool_cache);
320 buf_pool_cache = kmem_cache_create("aoe_bufs",
323 if (buf_pool_cache == NULL)