aoeblk.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, "%012llx\n", mac_addr(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, .owner = THIS_MODULE },
  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 inode *inode, struct file *filp)
  109. {
  110. struct aoedev *d;
  111. ulong flags;
  112. d = inode->i_bdev->bd_disk->private_data;
  113. spin_lock_irqsave(&d->lock, flags);
  114. if (d->flags & DEVFL_UP) {
  115. d->nopen++;
  116. spin_unlock_irqrestore(&d->lock, flags);
  117. return 0;
  118. }
  119. spin_unlock_irqrestore(&d->lock, flags);
  120. return -ENODEV;
  121. }
  122. static int
  123. aoeblk_release(struct inode *inode, struct file *filp)
  124. {
  125. struct aoedev *d;
  126. ulong flags;
  127. d = inode->i_bdev->bd_disk->private_data;
  128. spin_lock_irqsave(&d->lock, flags);
  129. if (--d->nopen == 0) {
  130. spin_unlock_irqrestore(&d->lock, flags);
  131. aoecmd_cfg(d->aoemajor, d->aoeminor);
  132. return 0;
  133. }
  134. spin_unlock_irqrestore(&d->lock, flags);
  135. return 0;
  136. }
  137. static int
  138. aoeblk_make_request(struct request_queue *q, struct bio *bio)
  139. {
  140. struct aoedev *d;
  141. struct buf *buf;
  142. struct sk_buff *sl;
  143. ulong flags;
  144. blk_queue_bounce(q, &bio);
  145. if (bio == NULL) {
  146. printk(KERN_ERR "aoe: bio is NULL\n");
  147. BUG();
  148. return 0;
  149. }
  150. d = bio->bi_bdev->bd_disk->private_data;
  151. if (d == NULL) {
  152. printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
  153. BUG();
  154. bio_endio(bio, -ENXIO);
  155. return 0;
  156. } else if (bio->bi_io_vec == NULL) {
  157. printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
  158. BUG();
  159. bio_endio(bio, -ENXIO);
  160. return 0;
  161. }
  162. buf = mempool_alloc(d->bufpool, GFP_NOIO);
  163. if (buf == NULL) {
  164. printk(KERN_INFO "aoe: buf allocation failure\n");
  165. bio_endio(bio, -ENOMEM);
  166. return 0;
  167. }
  168. memset(buf, 0, sizeof(*buf));
  169. INIT_LIST_HEAD(&buf->bufs);
  170. buf->stime = jiffies;
  171. buf->bio = bio;
  172. buf->resid = bio->bi_size;
  173. buf->sector = bio->bi_sector;
  174. buf->bv = &bio->bi_io_vec[bio->bi_idx];
  175. buf->bv_resid = buf->bv->bv_len;
  176. WARN_ON(buf->bv_resid == 0);
  177. buf->bv_off = buf->bv->bv_offset;
  178. spin_lock_irqsave(&d->lock, flags);
  179. if ((d->flags & DEVFL_UP) == 0) {
  180. printk(KERN_INFO "aoe: device %ld.%d is not up\n",
  181. d->aoemajor, d->aoeminor);
  182. spin_unlock_irqrestore(&d->lock, flags);
  183. mempool_free(buf, d->bufpool);
  184. bio_endio(bio, -ENXIO);
  185. return 0;
  186. }
  187. list_add_tail(&buf->bufs, &d->bufq);
  188. aoecmd_work(d);
  189. sl = d->sendq_hd;
  190. d->sendq_hd = d->sendq_tl = NULL;
  191. spin_unlock_irqrestore(&d->lock, flags);
  192. aoenet_xmit(sl);
  193. return 0;
  194. }
  195. static int
  196. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  197. {
  198. struct aoedev *d = bdev->bd_disk->private_data;
  199. if ((d->flags & DEVFL_UP) == 0) {
  200. printk(KERN_ERR "aoe: disk not up\n");
  201. return -ENODEV;
  202. }
  203. geo->cylinders = d->geo.cylinders;
  204. geo->heads = d->geo.heads;
  205. geo->sectors = d->geo.sectors;
  206. return 0;
  207. }
  208. static struct block_device_operations aoe_bdops = {
  209. .open = aoeblk_open,
  210. .release = aoeblk_release,
  211. .getgeo = aoeblk_getgeo,
  212. .owner = THIS_MODULE,
  213. };
  214. /* alloc_disk and add_disk can sleep */
  215. void
  216. aoeblk_gdalloc(void *vp)
  217. {
  218. struct aoedev *d = vp;
  219. struct gendisk *gd;
  220. ulong flags;
  221. gd = alloc_disk(AOE_PARTITIONS);
  222. if (gd == NULL) {
  223. printk(KERN_ERR
  224. "aoe: cannot allocate disk structure for %ld.%d\n",
  225. d->aoemajor, d->aoeminor);
  226. goto err;
  227. }
  228. d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
  229. if (d->bufpool == NULL) {
  230. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
  231. d->aoemajor, d->aoeminor);
  232. goto err_disk;
  233. }
  234. blk_queue_make_request(&d->blkq, aoeblk_make_request);
  235. if (bdi_init(&d->blkq.backing_dev_info))
  236. goto err_mempool;
  237. spin_lock_irqsave(&d->lock, flags);
  238. gd->major = AOE_MAJOR;
  239. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  240. gd->fops = &aoe_bdops;
  241. gd->private_data = d;
  242. gd->capacity = d->ssize;
  243. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
  244. d->aoemajor, d->aoeminor);
  245. gd->queue = &d->blkq;
  246. d->gd = gd;
  247. d->flags &= ~DEVFL_GDALLOC;
  248. d->flags |= DEVFL_UP;
  249. spin_unlock_irqrestore(&d->lock, flags);
  250. add_disk(gd);
  251. aoedisk_add_sysfs(d);
  252. return;
  253. err_mempool:
  254. mempool_destroy(d->bufpool);
  255. err_disk:
  256. put_disk(gd);
  257. err:
  258. spin_lock_irqsave(&d->lock, flags);
  259. d->flags &= ~DEVFL_GDALLOC;
  260. spin_unlock_irqrestore(&d->lock, flags);
  261. }
  262. void
  263. aoeblk_exit(void)
  264. {
  265. kmem_cache_destroy(buf_pool_cache);
  266. }
  267. int __init
  268. aoeblk_init(void)
  269. {
  270. buf_pool_cache = kmem_cache_create("aoe_bufs",
  271. sizeof(struct buf),
  272. 0, 0, NULL);
  273. if (buf_pool_cache == NULL)
  274. return -ENOMEM;
  275. return 0;
  276. }