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/kernel.h>
  7. #include <linux/hdreg.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/backing-dev.h>
  10. #include <linux/fs.h>
  11. #include <linux/ioctl.h>
  12. #include <linux/slab.h>
  13. #include <linux/ratelimit.h>
  14. #include <linux/genhd.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/mutex.h>
  17. #include "aoe.h"
  18. static DEFINE_MUTEX(aoeblk_mutex);
  19. static struct kmem_cache *buf_pool_cache;
  20. static ssize_t aoedisk_show_state(struct device *dev,
  21. struct device_attribute *attr, char *page)
  22. {
  23. struct gendisk *disk = dev_to_disk(dev);
  24. struct aoedev *d = disk->private_data;
  25. return snprintf(page, PAGE_SIZE,
  26. "%s%s\n",
  27. (d->flags & DEVFL_UP) ? "up" : "down",
  28. (d->flags & DEVFL_KICKME) ? ",kickme" :
  29. (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
  30. /* I'd rather see nopen exported so we can ditch closewait */
  31. }
  32. static ssize_t aoedisk_show_mac(struct device *dev,
  33. struct device_attribute *attr, char *page)
  34. {
  35. struct gendisk *disk = dev_to_disk(dev);
  36. struct aoedev *d = disk->private_data;
  37. struct aoetgt *t = d->targets[0];
  38. if (t == NULL)
  39. return snprintf(page, PAGE_SIZE, "none\n");
  40. return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
  41. }
  42. static ssize_t aoedisk_show_netif(struct device *dev,
  43. struct device_attribute *attr, char *page)
  44. {
  45. struct gendisk *disk = dev_to_disk(dev);
  46. struct aoedev *d = disk->private_data;
  47. struct net_device *nds[8], **nd, **nnd, **ne;
  48. struct aoetgt **t, **te;
  49. struct aoeif *ifp, *e;
  50. char *p;
  51. memset(nds, 0, sizeof nds);
  52. nd = nds;
  53. ne = nd + ARRAY_SIZE(nds);
  54. t = d->targets;
  55. te = t + NTARGETS;
  56. for (; t < te && *t; t++) {
  57. ifp = (*t)->ifs;
  58. e = ifp + NAOEIFS;
  59. for (; ifp < e && ifp->nd; ifp++) {
  60. for (nnd = nds; nnd < nd; nnd++)
  61. if (*nnd == ifp->nd)
  62. break;
  63. if (nnd == nd && nd != ne)
  64. *nd++ = ifp->nd;
  65. }
  66. }
  67. ne = nd;
  68. nd = nds;
  69. if (*nd == NULL)
  70. return snprintf(page, PAGE_SIZE, "none\n");
  71. for (p = page; nd < ne; nd++)
  72. p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
  73. p == page ? "" : ",", (*nd)->name);
  74. p += snprintf(p, PAGE_SIZE - (p-page), "\n");
  75. return p-page;
  76. }
  77. /* firmware version */
  78. static ssize_t aoedisk_show_fwver(struct device *dev,
  79. struct device_attribute *attr, char *page)
  80. {
  81. struct gendisk *disk = dev_to_disk(dev);
  82. struct aoedev *d = disk->private_data;
  83. return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
  84. }
  85. static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
  86. static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
  87. static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
  88. static struct device_attribute dev_attr_firmware_version = {
  89. .attr = { .name = "firmware-version", .mode = S_IRUGO },
  90. .show = aoedisk_show_fwver,
  91. };
  92. static struct attribute *aoe_attrs[] = {
  93. &dev_attr_state.attr,
  94. &dev_attr_mac.attr,
  95. &dev_attr_netif.attr,
  96. &dev_attr_firmware_version.attr,
  97. NULL,
  98. };
  99. static const struct attribute_group attr_group = {
  100. .attrs = aoe_attrs,
  101. };
  102. static int
  103. aoedisk_add_sysfs(struct aoedev *d)
  104. {
  105. return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  106. }
  107. void
  108. aoedisk_rm_sysfs(struct aoedev *d)
  109. {
  110. sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  111. }
  112. static int
  113. aoeblk_open(struct block_device *bdev, fmode_t mode)
  114. {
  115. struct aoedev *d = bdev->bd_disk->private_data;
  116. ulong flags;
  117. mutex_lock(&aoeblk_mutex);
  118. spin_lock_irqsave(&d->lock, flags);
  119. if (d->flags & DEVFL_UP) {
  120. d->nopen++;
  121. spin_unlock_irqrestore(&d->lock, flags);
  122. mutex_unlock(&aoeblk_mutex);
  123. return 0;
  124. }
  125. spin_unlock_irqrestore(&d->lock, flags);
  126. mutex_unlock(&aoeblk_mutex);
  127. return -ENODEV;
  128. }
  129. static int
  130. aoeblk_release(struct gendisk *disk, fmode_t mode)
  131. {
  132. struct aoedev *d = disk->private_data;
  133. ulong flags;
  134. spin_lock_irqsave(&d->lock, flags);
  135. if (--d->nopen == 0) {
  136. spin_unlock_irqrestore(&d->lock, flags);
  137. aoecmd_cfg(d->aoemajor, d->aoeminor);
  138. return 0;
  139. }
  140. spin_unlock_irqrestore(&d->lock, flags);
  141. return 0;
  142. }
  143. static int
  144. aoeblk_make_request(struct request_queue *q, struct bio *bio)
  145. {
  146. struct sk_buff_head queue;
  147. struct aoedev *d;
  148. struct buf *buf;
  149. ulong flags;
  150. blk_queue_bounce(q, &bio);
  151. if (bio == NULL) {
  152. printk(KERN_ERR "aoe: bio is NULL\n");
  153. BUG();
  154. return 0;
  155. }
  156. d = bio->bi_bdev->bd_disk->private_data;
  157. if (d == NULL) {
  158. printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
  159. BUG();
  160. bio_endio(bio, -ENXIO);
  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. pr_info_ratelimited("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. }