1 /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */ 2 /* 3 * aoeblk.c 4 * block device routines 5 */ 6 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/slab.h> 13 #include <linux/genhd.h> 14 #include <linux/netdevice.h> 15 #include "aoe.h" 16 17 static struct kmem_cache *buf_pool_cache; 18 19 static ssize_t aoedisk_show_state(struct device *dev, 20 struct device_attribute *attr, char *page) 21 { 22 struct gendisk *disk = dev_to_disk(dev); 23 struct aoedev *d = disk->private_data; 24 25 return snprintf(page, PAGE_SIZE, 26 "%s%s\n", 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 */ 31 } 32 static ssize_t aoedisk_show_mac(struct device *dev, 33 struct device_attribute *attr, char *page) 34 { 35 struct gendisk *disk = dev_to_disk(dev); 36 struct aoedev *d = disk->private_data; 37 struct aoetgt *t = d->targets[0]; 38 39 if (t == NULL) 40 return snprintf(page, PAGE_SIZE, "none\n"); 41 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr); 42 } 43 static ssize_t aoedisk_show_netif(struct device *dev, 44 struct device_attribute *attr, char *page) 45 { 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; 51 char *p; 52 53 memset(nds, 0, sizeof nds); 54 nd = nds; 55 ne = nd + ARRAY_SIZE(nds); 56 t = d->targets; 57 te = t + NTARGETS; 58 for (; t < te && *t; t++) { 59 ifp = (*t)->ifs; 60 e = ifp + NAOEIFS; 61 for (; ifp < e && ifp->nd; ifp++) { 62 for (nnd = nds; nnd < nd; nnd++) 63 if (*nnd == ifp->nd) 64 break; 65 if (nnd == nd && nd != ne) 66 *nd++ = ifp->nd; 67 } 68 } 69 70 ne = nd; 71 nd = nds; 72 if (*nd == NULL) 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"); 78 return p-page; 79 } 80 /* firmware version */ 81 static ssize_t aoedisk_show_fwver(struct device *dev, 82 struct device_attribute *attr, char *page) 83 { 84 struct gendisk *disk = dev_to_disk(dev); 85 struct aoedev *d = disk->private_data; 86 87 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); 88 } 89 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, 96 }; 97 98 static struct attribute *aoe_attrs[] = { 99 &dev_attr_state.attr, 100 &dev_attr_mac.attr, 101 &dev_attr_netif.attr, 102 &dev_attr_firmware_version.attr, 103 NULL, 104 }; 105 106 static const struct attribute_group attr_group = { 107 .attrs = aoe_attrs, 108 }; 109 110 static int 111 aoedisk_add_sysfs(struct aoedev *d) 112 { 113 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group); 114 } 115 void 116 aoedisk_rm_sysfs(struct aoedev *d) 117 { 118 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group); 119 } 120 121 static int 122 aoeblk_open(struct block_device *bdev, fmode_t mode) 123 { 124 struct aoedev *d = bdev->bd_disk->private_data; 125 ulong flags; 126 127 spin_lock_irqsave(&d->lock, flags); 128 if (d->flags & DEVFL_UP) { 129 d->nopen++; 130 spin_unlock_irqrestore(&d->lock, flags); 131 return 0; 132 } 133 spin_unlock_irqrestore(&d->lock, flags); 134 return -ENODEV; 135 } 136 137 static int 138 aoeblk_release(struct gendisk *disk, fmode_t mode) 139 { 140 struct aoedev *d = disk->private_data; 141 ulong flags; 142 143 spin_lock_irqsave(&d->lock, flags); 144 145 if (--d->nopen == 0) { 146 spin_unlock_irqrestore(&d->lock, flags); 147 aoecmd_cfg(d->aoemajor, d->aoeminor); 148 return 0; 149 } 150 spin_unlock_irqrestore(&d->lock, flags); 151 152 return 0; 153 } 154 155 static int 156 aoeblk_make_request(struct request_queue *q, struct bio *bio) 157 { 158 struct sk_buff_head queue; 159 struct aoedev *d; 160 struct buf *buf; 161 ulong flags; 162 163 blk_queue_bounce(q, &bio); 164 165 if (bio == NULL) { 166 printk(KERN_ERR "aoe: bio is NULL\n"); 167 BUG(); 168 return 0; 169 } 170 d = bio->bi_bdev->bd_disk->private_data; 171 if (d == NULL) { 172 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n"); 173 BUG(); 174 bio_endio(bio, -ENXIO); 175 return 0; 176 } else if (bio_rw_flagged(bio, BIO_RW_BARRIER)) { 177 bio_endio(bio, -EOPNOTSUPP); 178 return 0; 179 } else if (bio->bi_io_vec == NULL) { 180 printk(KERN_ERR "aoe: bi_io_vec is NULL\n"); 181 BUG(); 182 bio_endio(bio, -ENXIO); 183 return 0; 184 } 185 buf = mempool_alloc(d->bufpool, GFP_NOIO); 186 if (buf == NULL) { 187 printk(KERN_INFO "aoe: buf allocation failure\n"); 188 bio_endio(bio, -ENOMEM); 189 return 0; 190 } 191 memset(buf, 0, sizeof(*buf)); 192 INIT_LIST_HEAD(&buf->bufs); 193 buf->stime = jiffies; 194 buf->bio = bio; 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; 201 202 spin_lock_irqsave(&d->lock, flags); 203 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); 210 return 0; 211 } 212 213 list_add_tail(&buf->bufs, &d->bufq); 214 215 aoecmd_work(d); 216 __skb_queue_head_init(&queue); 217 skb_queue_splice_init(&d->sendq, &queue); 218 219 spin_unlock_irqrestore(&d->lock, flags); 220 aoenet_xmit(&queue); 221 222 return 0; 223 } 224 225 static int 226 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 227 { 228 struct aoedev *d = bdev->bd_disk->private_data; 229 230 if ((d->flags & DEVFL_UP) == 0) { 231 printk(KERN_ERR "aoe: disk not up\n"); 232 return -ENODEV; 233 } 234 235 geo->cylinders = d->geo.cylinders; 236 geo->heads = d->geo.heads; 237 geo->sectors = d->geo.sectors; 238 return 0; 239 } 240 241 static const struct block_device_operations aoe_bdops = { 242 .open = aoeblk_open, 243 .release = aoeblk_release, 244 .getgeo = aoeblk_getgeo, 245 .owner = THIS_MODULE, 246 }; 247 248 /* alloc_disk and add_disk can sleep */ 249 void 250 aoeblk_gdalloc(void *vp) 251 { 252 struct aoedev *d = vp; 253 struct gendisk *gd; 254 ulong flags; 255 256 gd = alloc_disk(AOE_PARTITIONS); 257 if (gd == NULL) { 258 printk(KERN_ERR 259 "aoe: cannot allocate disk structure for %ld.%d\n", 260 d->aoemajor, d->aoeminor); 261 goto err; 262 } 263 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); 268 goto err_disk; 269 } 270 271 d->blkq = blk_alloc_queue(GFP_KERNEL); 272 if (!d->blkq) 273 goto err_mempool; 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)) 277 goto err_blkq; 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); 286 287 gd->queue = d->blkq; 288 d->gd = gd; 289 d->flags &= ~DEVFL_GDALLOC; 290 d->flags |= DEVFL_UP; 291 292 spin_unlock_irqrestore(&d->lock, flags); 293 294 add_disk(gd); 295 aoedisk_add_sysfs(d); 296 return; 297 298 err_blkq: 299 blk_cleanup_queue(d->blkq); 300 d->blkq = NULL; 301 err_mempool: 302 mempool_destroy(d->bufpool); 303 err_disk: 304 put_disk(gd); 305 err: 306 spin_lock_irqsave(&d->lock, flags); 307 d->flags &= ~DEVFL_GDALLOC; 308 spin_unlock_irqrestore(&d->lock, flags); 309 } 310 311 void 312 aoeblk_exit(void) 313 { 314 kmem_cache_destroy(buf_pool_cache); 315 } 316 317 int __init 318 aoeblk_init(void) 319 { 320 buf_pool_cache = kmem_cache_create("aoe_bufs", 321 sizeof(struct buf), 322 0, 0, NULL); 323 if (buf_pool_cache == NULL) 324 return -ENOMEM; 325 326 return 0; 327 } 328 329