aoeblk.c 6.0 KB

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