aoeblk.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* Copyright (c) 2004 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. /* add attributes for our block devices in sysfs */
  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_CLOSEWAIT) ? ",closewait" : "");
  22. }
  23. static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page)
  24. {
  25. struct aoedev *d = disk->private_data;
  26. return snprintf(page, PAGE_SIZE, "%012llx\n",
  27. (unsigned long long)mac_addr(d->addr));
  28. }
  29. static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page)
  30. {
  31. struct aoedev *d = disk->private_data;
  32. return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name);
  33. }
  34. static struct disk_attribute disk_attr_state = {
  35. .attr = {.name = "state", .mode = S_IRUGO },
  36. .show = aoedisk_show_state
  37. };
  38. static struct disk_attribute disk_attr_mac = {
  39. .attr = {.name = "mac", .mode = S_IRUGO },
  40. .show = aoedisk_show_mac
  41. };
  42. static struct disk_attribute disk_attr_netif = {
  43. .attr = {.name = "netif", .mode = S_IRUGO },
  44. .show = aoedisk_show_netif
  45. };
  46. static void
  47. aoedisk_add_sysfs(struct aoedev *d)
  48. {
  49. sysfs_create_file(&d->gd->kobj, &disk_attr_state.attr);
  50. sysfs_create_file(&d->gd->kobj, &disk_attr_mac.attr);
  51. sysfs_create_file(&d->gd->kobj, &disk_attr_netif.attr);
  52. }
  53. void
  54. aoedisk_rm_sysfs(struct aoedev *d)
  55. {
  56. sysfs_remove_link(&d->gd->kobj, "state");
  57. sysfs_remove_link(&d->gd->kobj, "mac");
  58. sysfs_remove_link(&d->gd->kobj, "netif");
  59. }
  60. static int
  61. aoeblk_open(struct inode *inode, struct file *filp)
  62. {
  63. struct aoedev *d;
  64. ulong flags;
  65. d = inode->i_bdev->bd_disk->private_data;
  66. spin_lock_irqsave(&d->lock, flags);
  67. if (d->flags & DEVFL_UP) {
  68. d->nopen++;
  69. spin_unlock_irqrestore(&d->lock, flags);
  70. return 0;
  71. }
  72. spin_unlock_irqrestore(&d->lock, flags);
  73. return -ENODEV;
  74. }
  75. static int
  76. aoeblk_release(struct inode *inode, struct file *filp)
  77. {
  78. struct aoedev *d;
  79. ulong flags;
  80. d = inode->i_bdev->bd_disk->private_data;
  81. spin_lock_irqsave(&d->lock, flags);
  82. if (--d->nopen == 0 && (d->flags & DEVFL_CLOSEWAIT)) {
  83. d->flags &= ~DEVFL_CLOSEWAIT;
  84. spin_unlock_irqrestore(&d->lock, flags);
  85. aoecmd_cfg(d->aoemajor, d->aoeminor);
  86. return 0;
  87. }
  88. spin_unlock_irqrestore(&d->lock, flags);
  89. return 0;
  90. }
  91. static int
  92. aoeblk_make_request(request_queue_t *q, struct bio *bio)
  93. {
  94. struct aoedev *d;
  95. struct buf *buf;
  96. struct sk_buff *sl;
  97. ulong flags;
  98. blk_queue_bounce(q, &bio);
  99. d = bio->bi_bdev->bd_disk->private_data;
  100. buf = mempool_alloc(d->bufpool, GFP_NOIO);
  101. if (buf == NULL) {
  102. printk(KERN_INFO "aoe: aoeblk_make_request: buf allocation "
  103. "failure\n");
  104. bio_endio(bio, bio->bi_size, -ENOMEM);
  105. return 0;
  106. }
  107. memset(buf, 0, sizeof(*buf));
  108. INIT_LIST_HEAD(&buf->bufs);
  109. buf->start_time = jiffies;
  110. buf->bio = bio;
  111. buf->resid = bio->bi_size;
  112. buf->sector = bio->bi_sector;
  113. buf->bv = buf->bio->bi_io_vec;
  114. buf->bv_resid = buf->bv->bv_len;
  115. buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset;
  116. spin_lock_irqsave(&d->lock, flags);
  117. if ((d->flags & DEVFL_UP) == 0) {
  118. printk(KERN_INFO "aoe: aoeblk_make_request: device %ld.%ld is not up\n",
  119. d->aoemajor, d->aoeminor);
  120. spin_unlock_irqrestore(&d->lock, flags);
  121. mempool_free(buf, d->bufpool);
  122. bio_endio(bio, bio->bi_size, -ENXIO);
  123. return 0;
  124. }
  125. list_add_tail(&buf->bufs, &d->bufq);
  126. aoecmd_work(d);
  127. sl = d->sendq_hd;
  128. d->sendq_hd = d->sendq_tl = NULL;
  129. spin_unlock_irqrestore(&d->lock, flags);
  130. aoenet_xmit(sl);
  131. return 0;
  132. }
  133. /* This ioctl implementation expects userland to have the device node
  134. * permissions set so that only priviledged users can open an aoe
  135. * block device directly.
  136. */
  137. static int
  138. aoeblk_ioctl(struct inode *inode, struct file *filp, uint cmd, ulong arg)
  139. {
  140. struct aoedev *d;
  141. if (!arg)
  142. return -EINVAL;
  143. d = inode->i_bdev->bd_disk->private_data;
  144. if ((d->flags & DEVFL_UP) == 0) {
  145. printk(KERN_ERR "aoe: aoeblk_ioctl: disk not up\n");
  146. return -ENODEV;
  147. }
  148. if (cmd == HDIO_GETGEO) {
  149. d->geo.start = get_start_sect(inode->i_bdev);
  150. if (!copy_to_user((void __user *) arg, &d->geo, sizeof d->geo))
  151. return 0;
  152. return -EFAULT;
  153. }
  154. printk(KERN_INFO "aoe: aoeblk_ioctl: unknown ioctl %d\n", cmd);
  155. return -EINVAL;
  156. }
  157. static struct block_device_operations aoe_bdops = {
  158. .open = aoeblk_open,
  159. .release = aoeblk_release,
  160. .ioctl = aoeblk_ioctl,
  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. printk(KERN_ERR "aoe: aoeblk_gdalloc: cannot allocate disk "
  173. "structure for %ld.%ld\n", d->aoemajor, d->aoeminor);
  174. spin_lock_irqsave(&d->lock, flags);
  175. d->flags &= ~DEVFL_WORKON;
  176. spin_unlock_irqrestore(&d->lock, flags);
  177. return;
  178. }
  179. d->bufpool = mempool_create(MIN_BUFS,
  180. mempool_alloc_slab, mempool_free_slab,
  181. buf_pool_cache);
  182. if (d->bufpool == NULL) {
  183. printk(KERN_ERR "aoe: aoeblk_gdalloc: cannot allocate bufpool "
  184. "for %ld.%ld\n", d->aoemajor, d->aoeminor);
  185. put_disk(gd);
  186. spin_lock_irqsave(&d->lock, flags);
  187. d->flags &= ~DEVFL_WORKON;
  188. spin_unlock_irqrestore(&d->lock, flags);
  189. return;
  190. }
  191. spin_lock_irqsave(&d->lock, flags);
  192. blk_queue_make_request(&d->blkq, aoeblk_make_request);
  193. gd->major = AOE_MAJOR;
  194. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  195. gd->fops = &aoe_bdops;
  196. gd->private_data = d;
  197. gd->capacity = d->ssize;
  198. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld",
  199. d->aoemajor, d->aoeminor);
  200. gd->queue = &d->blkq;
  201. d->gd = gd;
  202. d->flags &= ~DEVFL_WORKON;
  203. d->flags |= DEVFL_UP;
  204. spin_unlock_irqrestore(&d->lock, flags);
  205. add_disk(gd);
  206. aoedisk_add_sysfs(d);
  207. printk(KERN_INFO "aoe: %012llx e%lu.%lu v%04x has %llu "
  208. "sectors\n", (unsigned long long)mac_addr(d->addr),
  209. d->aoemajor, d->aoeminor,
  210. d->fw_ver, (long long)d->ssize);
  211. }
  212. void
  213. aoeblk_exit(void)
  214. {
  215. kmem_cache_destroy(buf_pool_cache);
  216. }
  217. int __init
  218. aoeblk_init(void)
  219. {
  220. buf_pool_cache = kmem_cache_create("aoe_bufs",
  221. sizeof(struct buf),
  222. 0, 0, NULL, NULL);
  223. if (buf_pool_cache == NULL)
  224. return -ENOMEM;
  225. return 0;
  226. }