aoeblk.c 7.6 KB

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