aoeblk.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoeblk.c
  4. * block device routines
  5. */
  6. #include <linux/hdreg.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/backing-dev.h>
  9. #include <linux/fs.h>
  10. #include <linux/ioctl.h>
  11. #include <linux/slab.h>
  12. #include <linux/genhd.h>
  13. #include <linux/netdevice.h>
  14. #include "aoe.h"
  15. static struct kmem_cache *buf_pool_cache;
  16. static ssize_t aoedisk_show_state(struct device *dev,
  17. struct device_attribute *attr, char *page)
  18. {
  19. struct gendisk *disk = dev_to_disk(dev);
  20. struct aoedev *d = disk->private_data;
  21. return snprintf(page, PAGE_SIZE,
  22. "%s%s\n",
  23. (d->flags & DEVFL_UP) ? "up" : "down",
  24. (d->flags & DEVFL_KICKME) ? ",kickme" :
  25. (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
  26. /* I'd rather see nopen exported so we can ditch closewait */
  27. }
  28. static ssize_t aoedisk_show_mac(struct device *dev,
  29. struct device_attribute *attr, char *page)
  30. {
  31. struct gendisk *disk = dev_to_disk(dev);
  32. struct aoedev *d = disk->private_data;
  33. struct aoetgt *t = d->targets[0];
  34. if (t == NULL)
  35. return snprintf(page, PAGE_SIZE, "none\n");
  36. return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
  37. }
  38. static ssize_t aoedisk_show_netif(struct device *dev,
  39. struct device_attribute *attr, char *page)
  40. {
  41. struct gendisk *disk = dev_to_disk(dev);
  42. struct aoedev *d = disk->private_data;
  43. struct net_device *nds[8], **nd, **nnd, **ne;
  44. struct aoetgt **t, **te;
  45. struct aoeif *ifp, *e;
  46. char *p;
  47. memset(nds, 0, sizeof nds);
  48. nd = nds;
  49. ne = nd + ARRAY_SIZE(nds);
  50. t = d->targets;
  51. te = t + NTARGETS;
  52. for (; t < te && *t; t++) {
  53. ifp = (*t)->ifs;
  54. e = ifp + NAOEIFS;
  55. for (; ifp < e && ifp->nd; ifp++) {
  56. for (nnd = nds; nnd < nd; nnd++)
  57. if (*nnd == ifp->nd)
  58. break;
  59. if (nnd == nd && nd != ne)
  60. *nd++ = ifp->nd;
  61. }
  62. }
  63. ne = nd;
  64. nd = nds;
  65. if (*nd == NULL)
  66. return snprintf(page, PAGE_SIZE, "none\n");
  67. for (p = page; nd < ne; nd++)
  68. p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
  69. p == page ? "" : ",", (*nd)->name);
  70. p += snprintf(p, PAGE_SIZE - (p-page), "\n");
  71. return p-page;
  72. }
  73. /* firmware version */
  74. static ssize_t aoedisk_show_fwver(struct device *dev,
  75. struct device_attribute *attr, char *page)
  76. {
  77. struct gendisk *disk = dev_to_disk(dev);
  78. struct aoedev *d = disk->private_data;
  79. return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
  80. }
  81. static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
  82. static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
  83. static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
  84. static struct device_attribute dev_attr_firmware_version = {
  85. .attr = { .name = "firmware-version", .mode = S_IRUGO },
  86. .show = aoedisk_show_fwver,
  87. };
  88. static struct attribute *aoe_attrs[] = {
  89. &dev_attr_state.attr,
  90. &dev_attr_mac.attr,
  91. &dev_attr_netif.attr,
  92. &dev_attr_firmware_version.attr,
  93. NULL,
  94. };
  95. static const struct attribute_group attr_group = {
  96. .attrs = aoe_attrs,
  97. };
  98. static int
  99. aoedisk_add_sysfs(struct aoedev *d)
  100. {
  101. return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  102. }
  103. void
  104. aoedisk_rm_sysfs(struct aoedev *d)
  105. {
  106. sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  107. }
  108. static int
  109. aoeblk_open(struct block_device *bdev, fmode_t mode)
  110. {
  111. struct aoedev *d = bdev->bd_disk->private_data;
  112. ulong flags;
  113. spin_lock_irqsave(&d->lock, flags);
  114. if (d->flags & DEVFL_UP) {
  115. d->nopen++;
  116. spin_unlock_irqrestore(&d->lock, flags);
  117. return 0;
  118. }
  119. spin_unlock_irqrestore(&d->lock, flags);
  120. return -ENODEV;
  121. }
  122. static int
  123. aoeblk_release(struct gendisk *disk, fmode_t mode)
  124. {
  125. struct aoedev *d = disk->private_data;
  126. ulong flags;
  127. spin_lock_irqsave(&d->lock, flags);
  128. if (--d->nopen == 0) {
  129. spin_unlock_irqrestore(&d->lock, flags);
  130. aoecmd_cfg(d->aoemajor, d->aoeminor);
  131. return 0;
  132. }
  133. spin_unlock_irqrestore(&d->lock, flags);
  134. return 0;
  135. }
  136. static int
  137. aoeblk_make_request(struct request_queue *q, struct bio *bio)
  138. {
  139. struct sk_buff_head queue;
  140. struct aoedev *d;
  141. struct buf *buf;
  142. ulong flags;
  143. blk_queue_bounce(q, &bio);
  144. if (bio == NULL) {
  145. printk(KERN_ERR "aoe: bio is NULL\n");
  146. BUG();
  147. return 0;
  148. }
  149. d = bio->bi_bdev->bd_disk->private_data;
  150. if (d == NULL) {
  151. printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
  152. BUG();
  153. bio_endio(bio, -ENXIO);
  154. return 0;
  155. } else if (bio->bi_rw & REQ_HARDBARRIER) {
  156. bio_endio(bio, -EOPNOTSUPP);
  157. return 0;
  158. } else if (bio->bi_io_vec == NULL) {
  159. printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
  160. BUG();
  161. bio_endio(bio, -ENXIO);
  162. return 0;
  163. }
  164. buf = mempool_alloc(d->bufpool, GFP_NOIO);
  165. if (buf == NULL) {
  166. printk(KERN_INFO "aoe: buf allocation failure\n");
  167. bio_endio(bio, -ENOMEM);
  168. return 0;
  169. }
  170. memset(buf, 0, sizeof(*buf));
  171. INIT_LIST_HEAD(&buf->bufs);
  172. buf->stime = jiffies;
  173. buf->bio = bio;
  174. buf->resid = bio->bi_size;
  175. buf->sector = bio->bi_sector;
  176. buf->bv = &bio->bi_io_vec[bio->bi_idx];
  177. buf->bv_resid = buf->bv->bv_len;
  178. WARN_ON(buf->bv_resid == 0);
  179. buf->bv_off = buf->bv->bv_offset;
  180. spin_lock_irqsave(&d->lock, flags);
  181. if ((d->flags & DEVFL_UP) == 0) {
  182. printk(KERN_INFO "aoe: device %ld.%d is not up\n",
  183. d->aoemajor, d->aoeminor);
  184. spin_unlock_irqrestore(&d->lock, flags);
  185. mempool_free(buf, d->bufpool);
  186. bio_endio(bio, -ENXIO);
  187. return 0;
  188. }
  189. list_add_tail(&buf->bufs, &d->bufq);
  190. aoecmd_work(d);
  191. __skb_queue_head_init(&queue);
  192. skb_queue_splice_init(&d->sendq, &queue);
  193. spin_unlock_irqrestore(&d->lock, flags);
  194. aoenet_xmit(&queue);
  195. return 0;
  196. }
  197. static int
  198. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  199. {
  200. struct aoedev *d = bdev->bd_disk->private_data;
  201. if ((d->flags & DEVFL_UP) == 0) {
  202. printk(KERN_ERR "aoe: disk not up\n");
  203. return -ENODEV;
  204. }
  205. geo->cylinders = d->geo.cylinders;
  206. geo->heads = d->geo.heads;
  207. geo->sectors = d->geo.sectors;
  208. return 0;
  209. }
  210. static const struct block_device_operations aoe_bdops = {
  211. .open = aoeblk_open,
  212. .release = aoeblk_release,
  213. .getgeo = aoeblk_getgeo,
  214. .owner = THIS_MODULE,
  215. };
  216. /* alloc_disk and add_disk can sleep */
  217. void
  218. aoeblk_gdalloc(void *vp)
  219. {
  220. struct aoedev *d = vp;
  221. struct gendisk *gd;
  222. ulong flags;
  223. gd = alloc_disk(AOE_PARTITIONS);
  224. if (gd == NULL) {
  225. printk(KERN_ERR
  226. "aoe: cannot allocate disk structure for %ld.%d\n",
  227. d->aoemajor, d->aoeminor);
  228. goto err;
  229. }
  230. d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
  231. if (d->bufpool == NULL) {
  232. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
  233. d->aoemajor, d->aoeminor);
  234. goto err_disk;
  235. }
  236. d->blkq = blk_alloc_queue(GFP_KERNEL);
  237. if (!d->blkq)
  238. goto err_mempool;
  239. blk_queue_make_request(d->blkq, aoeblk_make_request);
  240. d->blkq->backing_dev_info.name = "aoe";
  241. if (bdi_init(&d->blkq->backing_dev_info))
  242. goto err_blkq;
  243. spin_lock_irqsave(&d->lock, flags);
  244. gd->major = AOE_MAJOR;
  245. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  246. gd->fops = &aoe_bdops;
  247. gd->private_data = d;
  248. set_capacity(gd, d->ssize);
  249. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
  250. d->aoemajor, d->aoeminor);
  251. gd->queue = d->blkq;
  252. d->gd = gd;
  253. d->flags &= ~DEVFL_GDALLOC;
  254. d->flags |= DEVFL_UP;
  255. spin_unlock_irqrestore(&d->lock, flags);
  256. add_disk(gd);
  257. aoedisk_add_sysfs(d);
  258. return;
  259. err_blkq:
  260. blk_cleanup_queue(d->blkq);
  261. d->blkq = NULL;
  262. err_mempool:
  263. mempool_destroy(d->bufpool);
  264. err_disk:
  265. put_disk(gd);
  266. err:
  267. spin_lock_irqsave(&d->lock, flags);
  268. d->flags &= ~DEVFL_GDALLOC;
  269. spin_unlock_irqrestore(&d->lock, flags);
  270. }
  271. void
  272. aoeblk_exit(void)
  273. {
  274. kmem_cache_destroy(buf_pool_cache);
  275. }
  276. int __init
  277. aoeblk_init(void)
  278. {
  279. buf_pool_cache = kmem_cache_create("aoe_bufs",
  280. sizeof(struct buf),
  281. 0, 0, NULL);
  282. if (buf_pool_cache == NULL)
  283. return -ENOMEM;
  284. return 0;
  285. }