aoeblk.c 7.1 KB

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