aoeblk.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/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_PAUSE) ? ",paused" :
  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. return snprintf(page, PAGE_SIZE, "%012llx\n",
  33. (unsigned long long)mac_addr(d->addr));
  34. }
  35. static ssize_t aoedisk_show_netif(struct device *dev,
  36. struct device_attribute *attr, char *page)
  37. {
  38. struct gendisk *disk = dev_to_disk(dev);
  39. struct aoedev *d = disk->private_data;
  40. return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
  41. }
  42. /* firmware version */
  43. static ssize_t aoedisk_show_fwver(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. return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
  49. }
  50. static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
  51. static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
  52. static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
  53. static struct device_attribute dev_attr_firmware_version = {
  54. .attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE },
  55. .show = aoedisk_show_fwver,
  56. };
  57. static struct attribute *aoe_attrs[] = {
  58. &dev_attr_state.attr,
  59. &dev_attr_mac.attr,
  60. &dev_attr_netif.attr,
  61. &dev_attr_firmware_version.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->dev.kobj, &attr_group);
  71. }
  72. void
  73. aoedisk_rm_sysfs(struct aoedev *d)
  74. {
  75. sysfs_remove_group(&d->gd->dev.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(struct request_queue *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, -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, -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. goto err;
  180. }
  181. d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
  182. if (d->bufpool == NULL) {
  183. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%ld\n",
  184. d->aoemajor, d->aoeminor);
  185. goto err_disk;
  186. }
  187. blk_queue_make_request(&d->blkq, aoeblk_make_request);
  188. if (bdi_init(&d->blkq.backing_dev_info))
  189. goto err_mempool;
  190. spin_lock_irqsave(&d->lock, flags);
  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. return;
  206. err_mempool:
  207. mempool_destroy(d->bufpool);
  208. err_disk:
  209. put_disk(gd);
  210. err:
  211. spin_lock_irqsave(&d->lock, flags);
  212. d->flags &= ~DEVFL_GDALLOC;
  213. spin_unlock_irqrestore(&d->lock, flags);
  214. }
  215. void
  216. aoeblk_exit(void)
  217. {
  218. kmem_cache_destroy(buf_pool_cache);
  219. }
  220. int __init
  221. aoeblk_init(void)
  222. {
  223. buf_pool_cache = kmem_cache_create("aoe_bufs",
  224. sizeof(struct buf),
  225. 0, 0, NULL);
  226. if (buf_pool_cache == NULL)
  227. return -ENOMEM;
  228. return 0;
  229. }