aoeblk.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_rw_flagged(bio, BIO_RW_BARRIER)) {
  155. bio_endio(bio, -EOPNOTSUPP);
  156. return 0;
  157. } else if (bio->bi_io_vec == NULL) {
  158. printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
  159. BUG();
  160. bio_endio(bio, -ENXIO);
  161. return 0;
  162. }
  163. buf = mempool_alloc(d->bufpool, GFP_NOIO);
  164. if (buf == NULL) {
  165. printk(KERN_INFO "aoe: buf allocation failure\n");
  166. bio_endio(bio, -ENOMEM);
  167. return 0;
  168. }
  169. memset(buf, 0, sizeof(*buf));
  170. INIT_LIST_HEAD(&buf->bufs);
  171. buf->stime = jiffies;
  172. buf->bio = bio;
  173. buf->resid = bio->bi_size;
  174. buf->sector = bio->bi_sector;
  175. buf->bv = &bio->bi_io_vec[bio->bi_idx];
  176. buf->bv_resid = buf->bv->bv_len;
  177. WARN_ON(buf->bv_resid == 0);
  178. buf->bv_off = buf->bv->bv_offset;
  179. spin_lock_irqsave(&d->lock, flags);
  180. if ((d->flags & DEVFL_UP) == 0) {
  181. printk(KERN_INFO "aoe: device %ld.%d is not up\n",
  182. d->aoemajor, d->aoeminor);
  183. spin_unlock_irqrestore(&d->lock, flags);
  184. mempool_free(buf, d->bufpool);
  185. bio_endio(bio, -ENXIO);
  186. return 0;
  187. }
  188. list_add_tail(&buf->bufs, &d->bufq);
  189. aoecmd_work(d);
  190. __skb_queue_head_init(&queue);
  191. skb_queue_splice_init(&d->sendq, &queue);
  192. spin_unlock_irqrestore(&d->lock, flags);
  193. aoenet_xmit(&queue);
  194. return 0;
  195. }
  196. static int
  197. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  198. {
  199. struct aoedev *d = bdev->bd_disk->private_data;
  200. if ((d->flags & DEVFL_UP) == 0) {
  201. printk(KERN_ERR "aoe: disk not up\n");
  202. return -ENODEV;
  203. }
  204. geo->cylinders = d->geo.cylinders;
  205. geo->heads = d->geo.heads;
  206. geo->sectors = d->geo.sectors;
  207. return 0;
  208. }
  209. static struct block_device_operations aoe_bdops = {
  210. .open = aoeblk_open,
  211. .release = aoeblk_release,
  212. .getgeo = aoeblk_getgeo,
  213. .owner = THIS_MODULE,
  214. };
  215. /* alloc_disk and add_disk can sleep */
  216. void
  217. aoeblk_gdalloc(void *vp)
  218. {
  219. struct aoedev *d = vp;
  220. struct gendisk *gd;
  221. ulong flags;
  222. gd = alloc_disk(AOE_PARTITIONS);
  223. if (gd == NULL) {
  224. printk(KERN_ERR
  225. "aoe: cannot allocate disk structure for %ld.%d\n",
  226. d->aoemajor, d->aoeminor);
  227. goto err;
  228. }
  229. d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
  230. if (d->bufpool == NULL) {
  231. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
  232. d->aoemajor, d->aoeminor);
  233. goto err_disk;
  234. }
  235. d->blkq = blk_alloc_queue(GFP_KERNEL);
  236. if (!d->blkq)
  237. goto err_mempool;
  238. blk_queue_make_request(d->blkq, aoeblk_make_request);
  239. if (bdi_init(&d->blkq->backing_dev_info))
  240. goto err_blkq;
  241. spin_lock_irqsave(&d->lock, flags);
  242. gd->major = AOE_MAJOR;
  243. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  244. gd->fops = &aoe_bdops;
  245. gd->private_data = d;
  246. set_capacity(gd, d->ssize);
  247. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
  248. d->aoemajor, d->aoeminor);
  249. gd->queue = d->blkq;
  250. d->gd = gd;
  251. d->flags &= ~DEVFL_GDALLOC;
  252. d->flags |= DEVFL_UP;
  253. spin_unlock_irqrestore(&d->lock, flags);
  254. add_disk(gd);
  255. aoedisk_add_sysfs(d);
  256. return;
  257. err_blkq:
  258. blk_cleanup_queue(d->blkq);
  259. d->blkq = NULL;
  260. err_mempool:
  261. mempool_destroy(d->bufpool);
  262. err_disk:
  263. put_disk(gd);
  264. err:
  265. spin_lock_irqsave(&d->lock, flags);
  266. d->flags &= ~DEVFL_GDALLOC;
  267. spin_unlock_irqrestore(&d->lock, flags);
  268. }
  269. void
  270. aoeblk_exit(void)
  271. {
  272. kmem_cache_destroy(buf_pool_cache);
  273. }
  274. int __init
  275. aoeblk_init(void)
  276. {
  277. buf_pool_cache = kmem_cache_create("aoe_bufs",
  278. sizeof(struct buf),
  279. 0, 0, NULL);
  280. if (buf_pool_cache == NULL)
  281. return -ENOMEM;
  282. return 0;
  283. }