aoeblk.c 7.5 KB

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