aoeblk.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (c) 2006 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/fs.h>
  9. #include <linux/ioctl.h>
  10. #include <linux/genhd.h>
  11. #include <linux/netdevice.h>
  12. #include "aoe.h"
  13. static kmem_cache_t *buf_pool_cache;
  14. static ssize_t aoedisk_show_state(struct gendisk * disk, char *page)
  15. {
  16. struct aoedev *d = disk->private_data;
  17. return snprintf(page, PAGE_SIZE,
  18. "%s%s\n",
  19. (d->flags & DEVFL_UP) ? "up" : "down",
  20. (d->flags & DEVFL_PAUSE) ? ",paused" :
  21. (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
  22. /* I'd rather see nopen exported so we can ditch closewait */
  23. }
  24. static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page)
  25. {
  26. struct aoedev *d = disk->private_data;
  27. return snprintf(page, PAGE_SIZE, "%012llx\n",
  28. (unsigned long long)mac_addr(d->addr));
  29. }
  30. static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page)
  31. {
  32. struct aoedev *d = disk->private_data;
  33. return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
  34. }
  35. /* firmware version */
  36. static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page)
  37. {
  38. struct aoedev *d = disk->private_data;
  39. return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
  40. }
  41. static struct disk_attribute disk_attr_state = {
  42. .attr = {.name = "state", .mode = S_IRUGO },
  43. .show = aoedisk_show_state
  44. };
  45. static struct disk_attribute disk_attr_mac = {
  46. .attr = {.name = "mac", .mode = S_IRUGO },
  47. .show = aoedisk_show_mac
  48. };
  49. static struct disk_attribute disk_attr_netif = {
  50. .attr = {.name = "netif", .mode = S_IRUGO },
  51. .show = aoedisk_show_netif
  52. };
  53. static struct disk_attribute disk_attr_fwver = {
  54. .attr = {.name = "firmware-version", .mode = S_IRUGO },
  55. .show = aoedisk_show_fwver
  56. };
  57. static void
  58. aoedisk_add_sysfs(struct aoedev *d)
  59. {
  60. sysfs_create_file(&d->gd->kobj, &disk_attr_state.attr);
  61. sysfs_create_file(&d->gd->kobj, &disk_attr_mac.attr);
  62. sysfs_create_file(&d->gd->kobj, &disk_attr_netif.attr);
  63. sysfs_create_file(&d->gd->kobj, &disk_attr_fwver.attr);
  64. }
  65. void
  66. aoedisk_rm_sysfs(struct aoedev *d)
  67. {
  68. sysfs_remove_link(&d->gd->kobj, "state");
  69. sysfs_remove_link(&d->gd->kobj, "mac");
  70. sysfs_remove_link(&d->gd->kobj, "netif");
  71. sysfs_remove_link(&d->gd->kobj, "firmware-version");
  72. }
  73. static int
  74. aoeblk_open(struct inode *inode, struct file *filp)
  75. {
  76. struct aoedev *d;
  77. ulong flags;
  78. d = inode->i_bdev->bd_disk->private_data;
  79. spin_lock_irqsave(&d->lock, flags);
  80. if (d->flags & DEVFL_UP) {
  81. d->nopen++;
  82. spin_unlock_irqrestore(&d->lock, flags);
  83. return 0;
  84. }
  85. spin_unlock_irqrestore(&d->lock, flags);
  86. return -ENODEV;
  87. }
  88. static int
  89. aoeblk_release(struct inode *inode, struct file *filp)
  90. {
  91. struct aoedev *d;
  92. ulong flags;
  93. d = inode->i_bdev->bd_disk->private_data;
  94. spin_lock_irqsave(&d->lock, flags);
  95. if (--d->nopen == 0) {
  96. spin_unlock_irqrestore(&d->lock, flags);
  97. aoecmd_cfg(d->aoemajor, d->aoeminor);
  98. return 0;
  99. }
  100. spin_unlock_irqrestore(&d->lock, flags);
  101. return 0;
  102. }
  103. static int
  104. aoeblk_make_request(request_queue_t *q, struct bio *bio)
  105. {
  106. struct aoedev *d;
  107. struct buf *buf;
  108. struct sk_buff *sl;
  109. ulong flags;
  110. blk_queue_bounce(q, &bio);
  111. d = bio->bi_bdev->bd_disk->private_data;
  112. buf = mempool_alloc(d->bufpool, GFP_NOIO);
  113. if (buf == NULL) {
  114. iprintk("buf allocation failure\n");
  115. bio_endio(bio, bio->bi_size, -ENOMEM);
  116. return 0;
  117. }
  118. memset(buf, 0, sizeof(*buf));
  119. INIT_LIST_HEAD(&buf->bufs);
  120. buf->start_time = jiffies;
  121. buf->bio = bio;
  122. buf->resid = bio->bi_size;
  123. buf->sector = bio->bi_sector;
  124. buf->bv = &bio->bi_io_vec[bio->bi_idx];
  125. WARN_ON(buf->bv->bv_len == 0);
  126. buf->bv_resid = buf->bv->bv_len;
  127. buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
  128. spin_lock_irqsave(&d->lock, flags);
  129. if ((d->flags & DEVFL_UP) == 0) {
  130. iprintk("device %ld.%ld is not up\n", d->aoemajor, d->aoeminor);
  131. spin_unlock_irqrestore(&d->lock, flags);
  132. mempool_free(buf, d->bufpool);
  133. bio_endio(bio, bio->bi_size, -ENXIO);
  134. return 0;
  135. }
  136. list_add_tail(&buf->bufs, &d->bufq);
  137. aoecmd_work(d);
  138. sl = d->sendq_hd;
  139. d->sendq_hd = d->sendq_tl = NULL;
  140. spin_unlock_irqrestore(&d->lock, flags);
  141. aoenet_xmit(sl);
  142. return 0;
  143. }
  144. static int
  145. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  146. {
  147. struct aoedev *d = bdev->bd_disk->private_data;
  148. if ((d->flags & DEVFL_UP) == 0) {
  149. eprintk("disk not up\n");
  150. return -ENODEV;
  151. }
  152. geo->cylinders = d->geo.cylinders;
  153. geo->heads = d->geo.heads;
  154. geo->sectors = d->geo.sectors;
  155. return 0;
  156. }
  157. static struct block_device_operations aoe_bdops = {
  158. .open = aoeblk_open,
  159. .release = aoeblk_release,
  160. .getgeo = aoeblk_getgeo,
  161. .owner = THIS_MODULE,
  162. };
  163. /* alloc_disk and add_disk can sleep */
  164. void
  165. aoeblk_gdalloc(void *vp)
  166. {
  167. struct aoedev *d = vp;
  168. struct gendisk *gd;
  169. ulong flags;
  170. gd = alloc_disk(AOE_PARTITIONS);
  171. if (gd == NULL) {
  172. eprintk("cannot allocate disk structure for %ld.%ld\n",
  173. d->aoemajor, d->aoeminor);
  174. spin_lock_irqsave(&d->lock, flags);
  175. d->flags &= ~DEVFL_GDALLOC;
  176. spin_unlock_irqrestore(&d->lock, flags);
  177. return;
  178. }
  179. d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
  180. if (d->bufpool == NULL) {
  181. eprintk("cannot allocate bufpool for %ld.%ld\n",
  182. d->aoemajor, d->aoeminor);
  183. put_disk(gd);
  184. spin_lock_irqsave(&d->lock, flags);
  185. d->flags &= ~DEVFL_GDALLOC;
  186. spin_unlock_irqrestore(&d->lock, flags);
  187. return;
  188. }
  189. spin_lock_irqsave(&d->lock, flags);
  190. blk_queue_make_request(&d->blkq, aoeblk_make_request);
  191. gd->major = AOE_MAJOR;
  192. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  193. gd->fops = &aoe_bdops;
  194. gd->private_data = d;
  195. gd->capacity = d->ssize;
  196. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
  197. d->aoemajor, d->aoeminor);
  198. gd->queue = &d->blkq;
  199. d->gd = gd;
  200. d->flags &= ~DEVFL_GDALLOC;
  201. d->flags |= DEVFL_UP;
  202. spin_unlock_irqrestore(&d->lock, flags);
  203. add_disk(gd);
  204. aoedisk_add_sysfs(d);
  205. }
  206. void
  207. aoeblk_exit(void)
  208. {
  209. kmem_cache_destroy(buf_pool_cache);
  210. }
  211. int __init
  212. aoeblk_init(void)
  213. {
  214. buf_pool_cache = kmem_cache_create("aoe_bufs",
  215. sizeof(struct buf),
  216. 0, 0, NULL, NULL);
  217. if (buf_pool_cache == NULL)
  218. return -ENOMEM;
  219. return 0;
  220. }