aoeblk.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 struct kmem_cache *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 struct attribute *aoe_attrs[] = {
  58. &disk_attr_state.attr,
  59. &disk_attr_mac.attr,
  60. &disk_attr_netif.attr,
  61. &disk_attr_fwver.attr,
  62. NULL
  63. };
  64. static const struct attribute_group attr_group = {
  65. .attrs = aoe_attrs,
  66. };
  67. static int
  68. aoedisk_add_sysfs(struct aoedev *d)
  69. {
  70. return sysfs_create_group(&d->gd->kobj, &attr_group);
  71. }
  72. void
  73. aoedisk_rm_sysfs(struct aoedev *d)
  74. {
  75. sysfs_remove_group(&d->gd->kobj, &attr_group);
  76. }
  77. static int
  78. aoeblk_open(struct inode *inode, struct file *filp)
  79. {
  80. struct aoedev *d;
  81. ulong flags;
  82. d = inode->i_bdev->bd_disk->private_data;
  83. spin_lock_irqsave(&d->lock, flags);
  84. if (d->flags & DEVFL_UP) {
  85. d->nopen++;
  86. spin_unlock_irqrestore(&d->lock, flags);
  87. return 0;
  88. }
  89. spin_unlock_irqrestore(&d->lock, flags);
  90. return -ENODEV;
  91. }
  92. static int
  93. aoeblk_release(struct inode *inode, struct file *filp)
  94. {
  95. struct aoedev *d;
  96. ulong flags;
  97. d = inode->i_bdev->bd_disk->private_data;
  98. spin_lock_irqsave(&d->lock, flags);
  99. if (--d->nopen == 0) {
  100. spin_unlock_irqrestore(&d->lock, flags);
  101. aoecmd_cfg(d->aoemajor, d->aoeminor);
  102. return 0;
  103. }
  104. spin_unlock_irqrestore(&d->lock, flags);
  105. return 0;
  106. }
  107. static int
  108. aoeblk_make_request(request_queue_t *q, struct bio *bio)
  109. {
  110. struct aoedev *d;
  111. struct buf *buf;
  112. struct sk_buff *sl;
  113. ulong flags;
  114. blk_queue_bounce(q, &bio);
  115. d = bio->bi_bdev->bd_disk->private_data;
  116. buf = mempool_alloc(d->bufpool, GFP_NOIO);
  117. if (buf == NULL) {
  118. printk(KERN_INFO "aoe: buf allocation failure\n");
  119. bio_endio(bio, bio->bi_size, -ENOMEM);
  120. return 0;
  121. }
  122. memset(buf, 0, sizeof(*buf));
  123. INIT_LIST_HEAD(&buf->bufs);
  124. buf->start_time = jiffies;
  125. buf->bio = bio;
  126. buf->resid = bio->bi_size;
  127. buf->sector = bio->bi_sector;
  128. buf->bv = &bio->bi_io_vec[bio->bi_idx];
  129. WARN_ON(buf->bv->bv_len == 0);
  130. buf->bv_resid = buf->bv->bv_len;
  131. buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
  132. spin_lock_irqsave(&d->lock, flags);
  133. if ((d->flags & DEVFL_UP) == 0) {
  134. printk(KERN_INFO "aoe: device %ld.%ld is not up\n",
  135. d->aoemajor, d->aoeminor);
  136. spin_unlock_irqrestore(&d->lock, flags);
  137. mempool_free(buf, d->bufpool);
  138. bio_endio(bio, bio->bi_size, -ENXIO);
  139. return 0;
  140. }
  141. list_add_tail(&buf->bufs, &d->bufq);
  142. aoecmd_work(d);
  143. sl = d->sendq_hd;
  144. d->sendq_hd = d->sendq_tl = NULL;
  145. spin_unlock_irqrestore(&d->lock, flags);
  146. aoenet_xmit(sl);
  147. return 0;
  148. }
  149. static int
  150. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  151. {
  152. struct aoedev *d = bdev->bd_disk->private_data;
  153. if ((d->flags & DEVFL_UP) == 0) {
  154. printk(KERN_ERR "aoe: disk not up\n");
  155. return -ENODEV;
  156. }
  157. geo->cylinders = d->geo.cylinders;
  158. geo->heads = d->geo.heads;
  159. geo->sectors = d->geo.sectors;
  160. return 0;
  161. }
  162. static struct block_device_operations aoe_bdops = {
  163. .open = aoeblk_open,
  164. .release = aoeblk_release,
  165. .getgeo = aoeblk_getgeo,
  166. .owner = THIS_MODULE,
  167. };
  168. /* alloc_disk and add_disk can sleep */
  169. void
  170. aoeblk_gdalloc(void *vp)
  171. {
  172. struct aoedev *d = vp;
  173. struct gendisk *gd;
  174. ulong flags;
  175. gd = alloc_disk(AOE_PARTITIONS);
  176. if (gd == NULL) {
  177. printk(KERN_ERR "aoe: cannot allocate disk structure for %ld.%ld\n",
  178. d->aoemajor, d->aoeminor);
  179. spin_lock_irqsave(&d->lock, flags);
  180. d->flags &= ~DEVFL_GDALLOC;
  181. spin_unlock_irqrestore(&d->lock, flags);
  182. return;
  183. }
  184. d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
  185. if (d->bufpool == NULL) {
  186. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
  187. d->aoemajor, d->aoeminor);
  188. put_disk(gd);
  189. spin_lock_irqsave(&d->lock, flags);
  190. d->flags &= ~DEVFL_GDALLOC;
  191. spin_unlock_irqrestore(&d->lock, flags);
  192. return;
  193. }
  194. spin_lock_irqsave(&d->lock, flags);
  195. blk_queue_make_request(&d->blkq, aoeblk_make_request);
  196. gd->major = AOE_MAJOR;
  197. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  198. gd->fops = &aoe_bdops;
  199. gd->private_data = d;
  200. gd->capacity = d->ssize;
  201. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
  202. d->aoemajor, d->aoeminor);
  203. gd->queue = &d->blkq;
  204. d->gd = gd;
  205. d->flags &= ~DEVFL_GDALLOC;
  206. d->flags |= DEVFL_UP;
  207. spin_unlock_irqrestore(&d->lock, flags);
  208. add_disk(gd);
  209. aoedisk_add_sysfs(d);
  210. }
  211. void
  212. aoeblk_exit(void)
  213. {
  214. kmem_cache_destroy(buf_pool_cache);
  215. }
  216. int __init
  217. aoeblk_init(void)
  218. {
  219. buf_pool_cache = kmem_cache_create("aoe_bufs",
  220. sizeof(struct buf),
  221. 0, 0, NULL, NULL);
  222. if (buf_pool_cache == NULL)
  223. return -ENOMEM;
  224. return 0;
  225. }