1 /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */ 2 /* 3 * aoeblk.c 4 * block device routines 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/hdreg.h> 9 #include <linux/blk-mq.h> 10 #include <linux/backing-dev.h> 11 #include <linux/fs.h> 12 #include <linux/ioctl.h> 13 #include <linux/slab.h> 14 #include <linux/ratelimit.h> 15 #include <linux/netdevice.h> 16 #include <linux/mutex.h> 17 #include <linux/export.h> 18 #include <linux/moduleparam.h> 19 #include <linux/debugfs.h> 20 #include <scsi/sg.h> 21 #include "aoe.h" 22 23 static DEFINE_MUTEX(aoeblk_mutex); 24 static struct kmem_cache *buf_pool_cache; 25 static struct dentry *aoe_debugfs_dir; 26 27 /* GPFS needs a larger value than the default. */ 28 static int aoe_maxsectors; 29 module_param(aoe_maxsectors, int, 0644); 30 MODULE_PARM_DESC(aoe_maxsectors, 31 "When nonzero, set the maximum number of sectors per I/O request"); 32 33 static ssize_t aoedisk_show_state(struct device *dev, 34 struct device_attribute *attr, char *page) 35 { 36 struct gendisk *disk = dev_to_disk(dev); 37 struct aoedev *d = disk->private_data; 38 39 return sysfs_emit(page, "%s%s\n", 40 (d->flags & DEVFL_UP) ? "up" : "down", 41 (d->flags & DEVFL_KICKME) ? ",kickme" : 42 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : ""); 43 /* I'd rather see nopen exported so we can ditch closewait */ 44 } 45 static ssize_t aoedisk_show_mac(struct device *dev, 46 struct device_attribute *attr, char *page) 47 { 48 struct gendisk *disk = dev_to_disk(dev); 49 struct aoedev *d = disk->private_data; 50 struct aoetgt *t = d->targets[0]; 51 52 if (t == NULL) 53 return sysfs_emit(page, "none\n"); 54 return sysfs_emit(page, "%pm\n", t->addr); 55 } 56 static ssize_t aoedisk_show_netif(struct device *dev, 57 struct device_attribute *attr, char *page) 58 { 59 struct gendisk *disk = dev_to_disk(dev); 60 struct aoedev *d = disk->private_data; 61 struct net_device *nds[8], **nd, **nnd, **ne; 62 struct aoetgt **t, **te; 63 struct aoeif *ifp, *e; 64 char *p; 65 66 memset(nds, 0, sizeof nds); 67 nd = nds; 68 ne = nd + ARRAY_SIZE(nds); 69 t = d->targets; 70 te = t + d->ntargets; 71 for (; t < te && *t; t++) { 72 ifp = (*t)->ifs; 73 e = ifp + NAOEIFS; 74 for (; ifp < e && ifp->nd; ifp++) { 75 for (nnd = nds; nnd < nd; nnd++) 76 if (*nnd == ifp->nd) 77 break; 78 if (nnd == nd && nd != ne) 79 *nd++ = ifp->nd; 80 } 81 } 82 83 ne = nd; 84 nd = nds; 85 if (*nd == NULL) 86 return sysfs_emit(page, "none\n"); 87 for (p = page; nd < ne; nd++) 88 p += scnprintf(p, PAGE_SIZE - (p-page), "%s%s", 89 p == page ? "" : ",", (*nd)->name); 90 p += scnprintf(p, PAGE_SIZE - (p-page), "\n"); 91 return p-page; 92 } 93 /* firmware version */ 94 static ssize_t aoedisk_show_fwver(struct device *dev, 95 struct device_attribute *attr, char *page) 96 { 97 struct gendisk *disk = dev_to_disk(dev); 98 struct aoedev *d = disk->private_data; 99 100 return sysfs_emit(page, "0x%04x\n", (unsigned int) d->fw_ver); 101 } 102 static ssize_t aoedisk_show_payload(struct device *dev, 103 struct device_attribute *attr, char *page) 104 { 105 struct gendisk *disk = dev_to_disk(dev); 106 struct aoedev *d = disk->private_data; 107 108 return sysfs_emit(page, "%lu\n", d->maxbcnt); 109 } 110 111 static int aoe_debugfs_show(struct seq_file *s, void *ignored) 112 { 113 struct aoedev *d; 114 struct aoetgt **t, **te; 115 struct aoeif *ifp, *ife; 116 unsigned long flags; 117 char c; 118 119 d = s->private; 120 seq_printf(s, "rttavg: %d rttdev: %d\n", 121 d->rttavg >> RTTSCALE, 122 d->rttdev >> RTTDSCALE); 123 seq_printf(s, "nskbpool: %d\n", skb_queue_len(&d->skbpool)); 124 seq_printf(s, "kicked: %ld\n", d->kicked); 125 seq_printf(s, "maxbcnt: %ld\n", d->maxbcnt); 126 seq_printf(s, "ref: %ld\n", d->ref); 127 128 spin_lock_irqsave(&d->lock, flags); 129 t = d->targets; 130 te = t + d->ntargets; 131 for (; t < te && *t; t++) { 132 c = '\t'; 133 seq_printf(s, "falloc: %ld\n", (*t)->falloc); 134 seq_printf(s, "ffree: %p\n", 135 list_empty(&(*t)->ffree) ? NULL : (*t)->ffree.next); 136 seq_printf(s, "%pm:%d:%d:%d\n", (*t)->addr, (*t)->nout, 137 (*t)->maxout, (*t)->nframes); 138 seq_printf(s, "\tssthresh:%d\n", (*t)->ssthresh); 139 seq_printf(s, "\ttaint:%d\n", (*t)->taint); 140 seq_printf(s, "\tr:%d\n", (*t)->rpkts); 141 seq_printf(s, "\tw:%d\n", (*t)->wpkts); 142 ifp = (*t)->ifs; 143 ife = ifp + ARRAY_SIZE((*t)->ifs); 144 for (; ifp->nd && ifp < ife; ifp++) { 145 seq_printf(s, "%c%s", c, ifp->nd->name); 146 c = ','; 147 } 148 seq_puts(s, "\n"); 149 } 150 spin_unlock_irqrestore(&d->lock, flags); 151 152 return 0; 153 } 154 DEFINE_SHOW_ATTRIBUTE(aoe_debugfs); 155 156 static DEVICE_ATTR(state, 0444, aoedisk_show_state, NULL); 157 static DEVICE_ATTR(mac, 0444, aoedisk_show_mac, NULL); 158 static DEVICE_ATTR(netif, 0444, aoedisk_show_netif, NULL); 159 static struct device_attribute dev_attr_firmware_version = { 160 .attr = { .name = "firmware-version", .mode = 0444 }, 161 .show = aoedisk_show_fwver, 162 }; 163 static DEVICE_ATTR(payload, 0444, aoedisk_show_payload, NULL); 164 165 static struct attribute *aoe_attrs[] = { 166 &dev_attr_state.attr, 167 &dev_attr_mac.attr, 168 &dev_attr_netif.attr, 169 &dev_attr_firmware_version.attr, 170 &dev_attr_payload.attr, 171 NULL, 172 }; 173 174 static const struct attribute_group aoe_attr_group = { 175 .attrs = aoe_attrs, 176 }; 177 178 static const struct attribute_group *aoe_attr_groups[] = { 179 &aoe_attr_group, 180 NULL, 181 }; 182 183 static void 184 aoedisk_add_debugfs(struct aoedev *d) 185 { 186 char *p; 187 188 if (aoe_debugfs_dir == NULL) 189 return; 190 p = strchr(d->gd->disk_name, '/'); 191 if (p == NULL) 192 p = d->gd->disk_name; 193 else 194 p++; 195 BUG_ON(*p == '\0'); 196 d->debugfs = debugfs_create_file(p, 0444, aoe_debugfs_dir, d, 197 &aoe_debugfs_fops); 198 } 199 void 200 aoedisk_rm_debugfs(struct aoedev *d) 201 { 202 debugfs_remove(d->debugfs); 203 d->debugfs = NULL; 204 } 205 206 static int 207 aoeblk_open(struct block_device *bdev, fmode_t mode) 208 { 209 struct aoedev *d = bdev->bd_disk->private_data; 210 ulong flags; 211 212 if (!virt_addr_valid(d)) { 213 pr_crit("aoe: invalid device pointer in %s\n", 214 __func__); 215 WARN_ON(1); 216 return -ENODEV; 217 } 218 if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL) 219 return -ENODEV; 220 221 mutex_lock(&aoeblk_mutex); 222 spin_lock_irqsave(&d->lock, flags); 223 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) { 224 d->nopen++; 225 spin_unlock_irqrestore(&d->lock, flags); 226 mutex_unlock(&aoeblk_mutex); 227 return 0; 228 } 229 spin_unlock_irqrestore(&d->lock, flags); 230 mutex_unlock(&aoeblk_mutex); 231 return -ENODEV; 232 } 233 234 static void 235 aoeblk_release(struct gendisk *disk, fmode_t mode) 236 { 237 struct aoedev *d = disk->private_data; 238 ulong flags; 239 240 spin_lock_irqsave(&d->lock, flags); 241 242 if (--d->nopen == 0) { 243 spin_unlock_irqrestore(&d->lock, flags); 244 aoecmd_cfg(d->aoemajor, d->aoeminor); 245 return; 246 } 247 spin_unlock_irqrestore(&d->lock, flags); 248 } 249 250 static blk_status_t aoeblk_queue_rq(struct blk_mq_hw_ctx *hctx, 251 const struct blk_mq_queue_data *bd) 252 { 253 struct aoedev *d = hctx->queue->queuedata; 254 255 spin_lock_irq(&d->lock); 256 257 if ((d->flags & DEVFL_UP) == 0) { 258 pr_info_ratelimited("aoe: device %ld.%d is not up\n", 259 d->aoemajor, d->aoeminor); 260 spin_unlock_irq(&d->lock); 261 blk_mq_start_request(bd->rq); 262 return BLK_STS_IOERR; 263 } 264 265 list_add_tail(&bd->rq->queuelist, &d->rq_list); 266 aoecmd_work(d); 267 spin_unlock_irq(&d->lock); 268 return BLK_STS_OK; 269 } 270 271 static int 272 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 273 { 274 struct aoedev *d = bdev->bd_disk->private_data; 275 276 if ((d->flags & DEVFL_UP) == 0) { 277 printk(KERN_ERR "aoe: disk not up\n"); 278 return -ENODEV; 279 } 280 281 geo->cylinders = d->geo.cylinders; 282 geo->heads = d->geo.heads; 283 geo->sectors = d->geo.sectors; 284 return 0; 285 } 286 287 static int 288 aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg) 289 { 290 struct aoedev *d; 291 292 if (!arg) 293 return -EINVAL; 294 295 d = bdev->bd_disk->private_data; 296 if ((d->flags & DEVFL_UP) == 0) { 297 pr_err("aoe: disk not up\n"); 298 return -ENODEV; 299 } 300 301 if (cmd == HDIO_GET_IDENTITY) { 302 if (!copy_to_user((void __user *) arg, &d->ident, 303 sizeof(d->ident))) 304 return 0; 305 return -EFAULT; 306 } 307 308 /* udev calls scsi_id, which uses SG_IO, resulting in noise */ 309 if (cmd != SG_IO) 310 pr_info("aoe: unknown ioctl 0x%x\n", cmd); 311 312 return -ENOTTY; 313 } 314 315 static const struct block_device_operations aoe_bdops = { 316 .open = aoeblk_open, 317 .release = aoeblk_release, 318 .ioctl = aoeblk_ioctl, 319 .compat_ioctl = blkdev_compat_ptr_ioctl, 320 .getgeo = aoeblk_getgeo, 321 .owner = THIS_MODULE, 322 }; 323 324 static const struct blk_mq_ops aoeblk_mq_ops = { 325 .queue_rq = aoeblk_queue_rq, 326 }; 327 328 /* blk_mq_alloc_disk and add_disk can sleep */ 329 void 330 aoeblk_gdalloc(void *vp) 331 { 332 struct aoedev *d = vp; 333 struct gendisk *gd; 334 mempool_t *mp; 335 struct blk_mq_tag_set *set; 336 ulong flags; 337 int late = 0; 338 int err; 339 340 spin_lock_irqsave(&d->lock, flags); 341 if (d->flags & DEVFL_GDALLOC 342 && !(d->flags & DEVFL_TKILL) 343 && !(d->flags & DEVFL_GD_NOW)) 344 d->flags |= DEVFL_GD_NOW; 345 else 346 late = 1; 347 spin_unlock_irqrestore(&d->lock, flags); 348 if (late) 349 return; 350 351 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab, 352 buf_pool_cache); 353 if (mp == NULL) { 354 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n", 355 d->aoemajor, d->aoeminor); 356 goto err; 357 } 358 359 set = &d->tag_set; 360 set->ops = &aoeblk_mq_ops; 361 set->cmd_size = sizeof(struct aoe_req); 362 set->nr_hw_queues = 1; 363 set->queue_depth = 128; 364 set->numa_node = NUMA_NO_NODE; 365 set->flags = BLK_MQ_F_SHOULD_MERGE; 366 err = blk_mq_alloc_tag_set(set); 367 if (err) { 368 pr_err("aoe: cannot allocate tag set for %ld.%d\n", 369 d->aoemajor, d->aoeminor); 370 goto err_mempool; 371 } 372 373 gd = blk_mq_alloc_disk(set, d); 374 if (IS_ERR(gd)) { 375 pr_err("aoe: cannot allocate block queue for %ld.%d\n", 376 d->aoemajor, d->aoeminor); 377 goto err_tagset; 378 } 379 380 spin_lock_irqsave(&d->lock, flags); 381 WARN_ON(!(d->flags & DEVFL_GD_NOW)); 382 WARN_ON(!(d->flags & DEVFL_GDALLOC)); 383 WARN_ON(d->flags & DEVFL_TKILL); 384 WARN_ON(d->gd); 385 WARN_ON(d->flags & DEVFL_UP); 386 blk_queue_max_hw_sectors(gd->queue, BLK_DEF_MAX_SECTORS); 387 blk_queue_io_opt(gd->queue, SZ_2M); 388 d->bufpool = mp; 389 d->blkq = gd->queue; 390 d->gd = gd; 391 if (aoe_maxsectors) 392 blk_queue_max_hw_sectors(gd->queue, aoe_maxsectors); 393 gd->major = AOE_MAJOR; 394 gd->first_minor = d->sysminor; 395 gd->minors = AOE_PARTITIONS; 396 gd->fops = &aoe_bdops; 397 gd->private_data = d; 398 set_capacity(gd, d->ssize); 399 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d", 400 d->aoemajor, d->aoeminor); 401 402 d->flags &= ~DEVFL_GDALLOC; 403 d->flags |= DEVFL_UP; 404 405 spin_unlock_irqrestore(&d->lock, flags); 406 407 err = device_add_disk(NULL, gd, aoe_attr_groups); 408 if (err) 409 goto out_disk_cleanup; 410 aoedisk_add_debugfs(d); 411 412 spin_lock_irqsave(&d->lock, flags); 413 WARN_ON(!(d->flags & DEVFL_GD_NOW)); 414 d->flags &= ~DEVFL_GD_NOW; 415 spin_unlock_irqrestore(&d->lock, flags); 416 return; 417 418 out_disk_cleanup: 419 put_disk(gd); 420 err_tagset: 421 blk_mq_free_tag_set(set); 422 err_mempool: 423 mempool_destroy(mp); 424 err: 425 spin_lock_irqsave(&d->lock, flags); 426 d->flags &= ~DEVFL_GD_NOW; 427 queue_work(aoe_wq, &d->work); 428 spin_unlock_irqrestore(&d->lock, flags); 429 } 430 431 void 432 aoeblk_exit(void) 433 { 434 debugfs_remove_recursive(aoe_debugfs_dir); 435 aoe_debugfs_dir = NULL; 436 kmem_cache_destroy(buf_pool_cache); 437 } 438 439 int __init 440 aoeblk_init(void) 441 { 442 buf_pool_cache = kmem_cache_create("aoe_bufs", 443 sizeof(struct buf), 444 0, 0, NULL); 445 if (buf_pool_cache == NULL) 446 return -ENOMEM; 447 aoe_debugfs_dir = debugfs_create_dir("aoe", NULL); 448 return 0; 449 } 450 451