aoeblk.c 6.0 KB

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