aoeblk.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <linux/export.h>
  18. #include "aoe.h"
  19. static DEFINE_MUTEX(aoeblk_mutex);
  20. static struct kmem_cache *buf_pool_cache;
  21. static ssize_t aoedisk_show_state(struct device *dev,
  22. struct device_attribute *attr, char *page)
  23. {
  24. struct gendisk *disk = dev_to_disk(dev);
  25. struct aoedev *d = disk->private_data;
  26. return snprintf(page, PAGE_SIZE,
  27. "%s%s\n",
  28. (d->flags & DEVFL_UP) ? "up" : "down",
  29. (d->flags & DEVFL_KICKME) ? ",kickme" :
  30. (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
  31. /* I'd rather see nopen exported so we can ditch closewait */
  32. }
  33. static ssize_t aoedisk_show_mac(struct device *dev,
  34. struct device_attribute *attr, char *page)
  35. {
  36. struct gendisk *disk = dev_to_disk(dev);
  37. struct aoedev *d = disk->private_data;
  38. struct aoetgt *t = d->targets[0];
  39. if (t == NULL)
  40. return snprintf(page, PAGE_SIZE, "none\n");
  41. return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
  42. }
  43. static ssize_t aoedisk_show_netif(struct device *dev,
  44. struct device_attribute *attr, char *page)
  45. {
  46. struct gendisk *disk = dev_to_disk(dev);
  47. struct aoedev *d = disk->private_data;
  48. struct net_device *nds[8], **nd, **nnd, **ne;
  49. struct aoetgt **t, **te;
  50. struct aoeif *ifp, *e;
  51. char *p;
  52. memset(nds, 0, sizeof nds);
  53. nd = nds;
  54. ne = nd + ARRAY_SIZE(nds);
  55. t = d->targets;
  56. te = t + NTARGETS;
  57. for (; t < te && *t; t++) {
  58. ifp = (*t)->ifs;
  59. e = ifp + NAOEIFS;
  60. for (; ifp < e && ifp->nd; ifp++) {
  61. for (nnd = nds; nnd < nd; nnd++)
  62. if (*nnd == ifp->nd)
  63. break;
  64. if (nnd == nd && nd != ne)
  65. *nd++ = ifp->nd;
  66. }
  67. }
  68. ne = nd;
  69. nd = nds;
  70. if (*nd == NULL)
  71. return snprintf(page, PAGE_SIZE, "none\n");
  72. for (p = page; nd < ne; nd++)
  73. p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
  74. p == page ? "" : ",", (*nd)->name);
  75. p += snprintf(p, PAGE_SIZE - (p-page), "\n");
  76. return p-page;
  77. }
  78. /* firmware version */
  79. static ssize_t aoedisk_show_fwver(struct device *dev,
  80. struct device_attribute *attr, char *page)
  81. {
  82. struct gendisk *disk = dev_to_disk(dev);
  83. struct aoedev *d = disk->private_data;
  84. return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
  85. }
  86. static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
  87. static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
  88. static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
  89. static struct device_attribute dev_attr_firmware_version = {
  90. .attr = { .name = "firmware-version", .mode = S_IRUGO },
  91. .show = aoedisk_show_fwver,
  92. };
  93. static struct attribute *aoe_attrs[] = {
  94. &dev_attr_state.attr,
  95. &dev_attr_mac.attr,
  96. &dev_attr_netif.attr,
  97. &dev_attr_firmware_version.attr,
  98. NULL,
  99. };
  100. static const struct attribute_group attr_group = {
  101. .attrs = aoe_attrs,
  102. };
  103. static int
  104. aoedisk_add_sysfs(struct aoedev *d)
  105. {
  106. return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  107. }
  108. void
  109. aoedisk_rm_sysfs(struct aoedev *d)
  110. {
  111. sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  112. }
  113. static int
  114. aoeblk_open(struct block_device *bdev, fmode_t mode)
  115. {
  116. struct aoedev *d = bdev->bd_disk->private_data;
  117. ulong flags;
  118. mutex_lock(&aoeblk_mutex);
  119. spin_lock_irqsave(&d->lock, flags);
  120. if (d->flags & DEVFL_UP) {
  121. d->nopen++;
  122. spin_unlock_irqrestore(&d->lock, flags);
  123. mutex_unlock(&aoeblk_mutex);
  124. return 0;
  125. }
  126. spin_unlock_irqrestore(&d->lock, flags);
  127. mutex_unlock(&aoeblk_mutex);
  128. return -ENODEV;
  129. }
  130. static int
  131. aoeblk_release(struct gendisk *disk, fmode_t mode)
  132. {
  133. struct aoedev *d = disk->private_data;
  134. ulong flags;
  135. spin_lock_irqsave(&d->lock, flags);
  136. if (--d->nopen == 0) {
  137. spin_unlock_irqrestore(&d->lock, flags);
  138. aoecmd_cfg(d->aoemajor, d->aoeminor);
  139. return 0;
  140. }
  141. spin_unlock_irqrestore(&d->lock, flags);
  142. return 0;
  143. }
  144. static void
  145. aoeblk_make_request(struct request_queue *q, struct bio *bio)
  146. {
  147. struct sk_buff_head queue;
  148. struct aoedev *d;
  149. struct buf *buf;
  150. ulong flags;
  151. blk_queue_bounce(q, &bio);
  152. if (bio == NULL) {
  153. printk(KERN_ERR "aoe: bio is NULL\n");
  154. BUG();
  155. return;
  156. }
  157. d = bio->bi_bdev->bd_disk->private_data;
  158. if (d == NULL) {
  159. printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
  160. BUG();
  161. bio_endio(bio, -ENXIO);
  162. return;
  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;
  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;
  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. pr_info_ratelimited("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;
  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. }
  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. spin_lock_irqsave(&d->lock, flags);
  246. gd->major = AOE_MAJOR;
  247. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  248. gd->fops = &aoe_bdops;
  249. gd->private_data = d;
  250. set_capacity(gd, d->ssize);
  251. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
  252. d->aoemajor, d->aoeminor);
  253. gd->queue = d->blkq;
  254. d->gd = gd;
  255. d->flags &= ~DEVFL_GDALLOC;
  256. d->flags |= DEVFL_UP;
  257. spin_unlock_irqrestore(&d->lock, flags);
  258. add_disk(gd);
  259. aoedisk_add_sysfs(d);
  260. return;
  261. err_mempool:
  262. mempool_destroy(d->bufpool);
  263. err_disk:
  264. put_disk(gd);
  265. err:
  266. spin_lock_irqsave(&d->lock, flags);
  267. d->flags &= ~DEVFL_GDALLOC;
  268. spin_unlock_irqrestore(&d->lock, flags);
  269. }
  270. void
  271. aoeblk_exit(void)
  272. {
  273. kmem_cache_destroy(buf_pool_cache);
  274. }
  275. int __init
  276. aoeblk_init(void)
  277. {
  278. buf_pool_cache = kmem_cache_create("aoe_bufs",
  279. sizeof(struct buf),
  280. 0, 0, NULL);
  281. if (buf_pool_cache == NULL)
  282. return -ENOMEM;
  283. return 0;
  284. }