aoeblk.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoeblk.c
  4. * block device routines
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/hdreg.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/backing-dev.h>
  10. #include <linux/fs.h>
  11. #include <linux/ioctl.h>
  12. #include <linux/slab.h>
  13. #include <linux/ratelimit.h>
  14. #include <linux/genhd.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/mutex.h>
  17. #include <linux/export.h>
  18. #include "aoe.h"
  19. static DEFINE_MUTEX(aoeblk_mutex);
  20. static struct kmem_cache *buf_pool_cache;
  21. static ssize_t aoedisk_show_state(struct device *dev,
  22. struct device_attribute *attr, char *page)
  23. {
  24. struct gendisk *disk = dev_to_disk(dev);
  25. struct aoedev *d = disk->private_data;
  26. return snprintf(page, PAGE_SIZE,
  27. "%s%s\n",
  28. (d->flags & DEVFL_UP) ? "up" : "down",
  29. (d->flags & DEVFL_KICKME) ? ",kickme" :
  30. (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
  31. /* I'd rather see nopen exported so we can ditch closewait */
  32. }
  33. static ssize_t aoedisk_show_mac(struct device *dev,
  34. struct device_attribute *attr, char *page)
  35. {
  36. struct gendisk *disk = dev_to_disk(dev);
  37. struct aoedev *d = disk->private_data;
  38. struct aoetgt *t = d->targets[0];
  39. if (t == NULL)
  40. return snprintf(page, PAGE_SIZE, "none\n");
  41. return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
  42. }
  43. static ssize_t aoedisk_show_netif(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. struct net_device *nds[8], **nd, **nnd, **ne;
  49. struct aoetgt **t, **te;
  50. struct aoeif *ifp, *e;
  51. char *p;
  52. memset(nds, 0, sizeof nds);
  53. nd = nds;
  54. ne = nd + ARRAY_SIZE(nds);
  55. t = d->targets;
  56. te = t + NTARGETS;
  57. for (; t < te && *t; t++) {
  58. ifp = (*t)->ifs;
  59. e = ifp + NAOEIFS;
  60. for (; ifp < e && ifp->nd; ifp++) {
  61. for (nnd = nds; nnd < nd; nnd++)
  62. if (*nnd == ifp->nd)
  63. break;
  64. if (nnd == nd && nd != ne)
  65. *nd++ = ifp->nd;
  66. }
  67. }
  68. ne = nd;
  69. nd = nds;
  70. if (*nd == NULL)
  71. return snprintf(page, PAGE_SIZE, "none\n");
  72. for (p = page; nd < ne; nd++)
  73. p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
  74. p == page ? "" : ",", (*nd)->name);
  75. p += snprintf(p, PAGE_SIZE - (p-page), "\n");
  76. return p-page;
  77. }
  78. /* firmware version */
  79. static ssize_t aoedisk_show_fwver(struct device *dev,
  80. struct device_attribute *attr, char *page)
  81. {
  82. struct gendisk *disk = dev_to_disk(dev);
  83. struct aoedev *d = disk->private_data;
  84. return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
  85. }
  86. static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
  87. static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
  88. static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
  89. static struct device_attribute dev_attr_firmware_version = {
  90. .attr = { .name = "firmware-version", .mode = S_IRUGO },
  91. .show = aoedisk_show_fwver,
  92. };
  93. static struct attribute *aoe_attrs[] = {
  94. &dev_attr_state.attr,
  95. &dev_attr_mac.attr,
  96. &dev_attr_netif.attr,
  97. &dev_attr_firmware_version.attr,
  98. NULL,
  99. };
  100. static const struct attribute_group attr_group = {
  101. .attrs = aoe_attrs,
  102. };
  103. static int
  104. aoedisk_add_sysfs(struct aoedev *d)
  105. {
  106. return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  107. }
  108. void
  109. aoedisk_rm_sysfs(struct aoedev *d)
  110. {
  111. sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  112. }
  113. static int
  114. aoeblk_open(struct block_device *bdev, fmode_t mode)
  115. {
  116. struct aoedev *d = bdev->bd_disk->private_data;
  117. ulong flags;
  118. mutex_lock(&aoeblk_mutex);
  119. spin_lock_irqsave(&d->lock, flags);
  120. if (d->flags & DEVFL_UP) {
  121. d->nopen++;
  122. spin_unlock_irqrestore(&d->lock, flags);
  123. mutex_unlock(&aoeblk_mutex);
  124. return 0;
  125. }
  126. spin_unlock_irqrestore(&d->lock, flags);
  127. mutex_unlock(&aoeblk_mutex);
  128. return -ENODEV;
  129. }
  130. static int
  131. aoeblk_release(struct gendisk *disk, fmode_t mode)
  132. {
  133. struct aoedev *d = disk->private_data;
  134. ulong flags;
  135. spin_lock_irqsave(&d->lock, flags);
  136. if (--d->nopen == 0) {
  137. spin_unlock_irqrestore(&d->lock, flags);
  138. aoecmd_cfg(d->aoemajor, d->aoeminor);
  139. return 0;
  140. }
  141. spin_unlock_irqrestore(&d->lock, flags);
  142. return 0;
  143. }
  144. static void
  145. aoeblk_request(struct request_queue *q)
  146. {
  147. struct aoedev *d;
  148. struct request *rq;
  149. d = q->queuedata;
  150. if ((d->flags & DEVFL_UP) == 0) {
  151. pr_info_ratelimited("aoe: device %ld.%d is not up\n",
  152. d->aoemajor, d->aoeminor);
  153. while ((rq = blk_peek_request(q))) {
  154. blk_start_request(rq);
  155. aoe_end_request(d, rq, 1);
  156. }
  157. return;
  158. }
  159. aoecmd_work(d);
  160. }
  161. static int
  162. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  163. {
  164. struct aoedev *d = bdev->bd_disk->private_data;
  165. if ((d->flags & DEVFL_UP) == 0) {
  166. printk(KERN_ERR "aoe: disk not up\n");
  167. return -ENODEV;
  168. }
  169. geo->cylinders = d->geo.cylinders;
  170. geo->heads = d->geo.heads;
  171. geo->sectors = d->geo.sectors;
  172. return 0;
  173. }
  174. static const struct block_device_operations aoe_bdops = {
  175. .open = aoeblk_open,
  176. .release = aoeblk_release,
  177. .getgeo = aoeblk_getgeo,
  178. .owner = THIS_MODULE,
  179. };
  180. /* alloc_disk and add_disk can sleep */
  181. void
  182. aoeblk_gdalloc(void *vp)
  183. {
  184. struct aoedev *d = vp;
  185. struct gendisk *gd;
  186. mempool_t *mp;
  187. struct request_queue *q;
  188. enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
  189. ulong flags;
  190. gd = alloc_disk(AOE_PARTITIONS);
  191. if (gd == NULL) {
  192. pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
  193. d->aoemajor, d->aoeminor);
  194. goto err;
  195. }
  196. mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
  197. buf_pool_cache);
  198. if (mp == NULL) {
  199. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
  200. d->aoemajor, d->aoeminor);
  201. goto err_disk;
  202. }
  203. q = blk_init_queue(aoeblk_request, &d->lock);
  204. if (q == NULL) {
  205. pr_err("aoe: cannot allocate block queue for %ld.%d\n",
  206. d->aoemajor, d->aoeminor);
  207. mempool_destroy(mp);
  208. goto err_disk;
  209. }
  210. d->blkq = blk_alloc_queue(GFP_KERNEL);
  211. if (!d->blkq)
  212. goto err_mempool;
  213. d->blkq->backing_dev_info.name = "aoe";
  214. if (bdi_init(&d->blkq->backing_dev_info))
  215. goto err_blkq;
  216. spin_lock_irqsave(&d->lock, flags);
  217. blk_queue_max_hw_sectors(d->blkq, BLK_DEF_MAX_SECTORS);
  218. q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
  219. d->bufpool = mp;
  220. d->blkq = gd->queue = q;
  221. q->queuedata = d;
  222. d->gd = gd;
  223. gd->major = AOE_MAJOR;
  224. gd->first_minor = d->sysminor * AOE_PARTITIONS;
  225. gd->fops = &aoe_bdops;
  226. gd->private_data = d;
  227. set_capacity(gd, d->ssize);
  228. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
  229. d->aoemajor, d->aoeminor);
  230. d->flags &= ~DEVFL_GDALLOC;
  231. d->flags |= DEVFL_UP;
  232. spin_unlock_irqrestore(&d->lock, flags);
  233. add_disk(gd);
  234. aoedisk_add_sysfs(d);
  235. return;
  236. err_blkq:
  237. blk_cleanup_queue(d->blkq);
  238. d->blkq = NULL;
  239. err_mempool:
  240. mempool_destroy(d->bufpool);
  241. err_disk:
  242. put_disk(gd);
  243. err:
  244. spin_lock_irqsave(&d->lock, flags);
  245. d->flags &= ~DEVFL_GDALLOC;
  246. spin_unlock_irqrestore(&d->lock, flags);
  247. }
  248. void
  249. aoeblk_exit(void)
  250. {
  251. kmem_cache_destroy(buf_pool_cache);
  252. }
  253. int __init
  254. aoeblk_init(void)
  255. {
  256. buf_pool_cache = kmem_cache_create("aoe_bufs",
  257. sizeof(struct buf),
  258. 0, 0, NULL);
  259. if (buf_pool_cache == NULL)
  260. return -ENOMEM;
  261. return 0;
  262. }