aoeblk.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 <linux/moduleparam.h>
  19. #include <scsi/sg.h>
  20. #include "aoe.h"
  21. static DEFINE_MUTEX(aoeblk_mutex);
  22. static struct kmem_cache *buf_pool_cache;
  23. /* GPFS needs a larger value than the default. */
  24. static int aoe_maxsectors;
  25. module_param(aoe_maxsectors, int, 0644);
  26. MODULE_PARM_DESC(aoe_maxsectors,
  27. "When nonzero, set the maximum number of sectors per I/O request");
  28. static ssize_t aoedisk_show_state(struct device *dev,
  29. struct device_attribute *attr, char *page)
  30. {
  31. struct gendisk *disk = dev_to_disk(dev);
  32. struct aoedev *d = disk->private_data;
  33. return snprintf(page, PAGE_SIZE,
  34. "%s%s\n",
  35. (d->flags & DEVFL_UP) ? "up" : "down",
  36. (d->flags & DEVFL_KICKME) ? ",kickme" :
  37. (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
  38. /* I'd rather see nopen exported so we can ditch closewait */
  39. }
  40. static ssize_t aoedisk_show_mac(struct device *dev,
  41. struct device_attribute *attr, char *page)
  42. {
  43. struct gendisk *disk = dev_to_disk(dev);
  44. struct aoedev *d = disk->private_data;
  45. struct aoetgt *t = d->targets[0];
  46. if (t == NULL)
  47. return snprintf(page, PAGE_SIZE, "none\n");
  48. return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
  49. }
  50. static ssize_t aoedisk_show_netif(struct device *dev,
  51. struct device_attribute *attr, char *page)
  52. {
  53. struct gendisk *disk = dev_to_disk(dev);
  54. struct aoedev *d = disk->private_data;
  55. struct net_device *nds[8], **nd, **nnd, **ne;
  56. struct aoetgt **t, **te;
  57. struct aoeif *ifp, *e;
  58. char *p;
  59. memset(nds, 0, sizeof nds);
  60. nd = nds;
  61. ne = nd + ARRAY_SIZE(nds);
  62. t = d->targets;
  63. te = t + NTARGETS;
  64. for (; t < te && *t; t++) {
  65. ifp = (*t)->ifs;
  66. e = ifp + NAOEIFS;
  67. for (; ifp < e && ifp->nd; ifp++) {
  68. for (nnd = nds; nnd < nd; nnd++)
  69. if (*nnd == ifp->nd)
  70. break;
  71. if (nnd == nd && nd != ne)
  72. *nd++ = ifp->nd;
  73. }
  74. }
  75. ne = nd;
  76. nd = nds;
  77. if (*nd == NULL)
  78. return snprintf(page, PAGE_SIZE, "none\n");
  79. for (p = page; nd < ne; nd++)
  80. p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
  81. p == page ? "" : ",", (*nd)->name);
  82. p += snprintf(p, PAGE_SIZE - (p-page), "\n");
  83. return p-page;
  84. }
  85. /* firmware version */
  86. static ssize_t aoedisk_show_fwver(struct device *dev,
  87. struct device_attribute *attr, char *page)
  88. {
  89. struct gendisk *disk = dev_to_disk(dev);
  90. struct aoedev *d = disk->private_data;
  91. return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
  92. }
  93. static ssize_t aoedisk_show_payload(struct device *dev,
  94. struct device_attribute *attr, char *page)
  95. {
  96. struct gendisk *disk = dev_to_disk(dev);
  97. struct aoedev *d = disk->private_data;
  98. return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
  99. }
  100. static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
  101. static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
  102. static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
  103. static struct device_attribute dev_attr_firmware_version = {
  104. .attr = { .name = "firmware-version", .mode = S_IRUGO },
  105. .show = aoedisk_show_fwver,
  106. };
  107. static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
  108. static struct attribute *aoe_attrs[] = {
  109. &dev_attr_state.attr,
  110. &dev_attr_mac.attr,
  111. &dev_attr_netif.attr,
  112. &dev_attr_firmware_version.attr,
  113. &dev_attr_payload.attr,
  114. NULL,
  115. };
  116. static const struct attribute_group attr_group = {
  117. .attrs = aoe_attrs,
  118. };
  119. static int
  120. aoedisk_add_sysfs(struct aoedev *d)
  121. {
  122. return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  123. }
  124. void
  125. aoedisk_rm_sysfs(struct aoedev *d)
  126. {
  127. sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  128. }
  129. static int
  130. aoeblk_open(struct block_device *bdev, fmode_t mode)
  131. {
  132. struct aoedev *d = bdev->bd_disk->private_data;
  133. ulong flags;
  134. if (!virt_addr_valid(d)) {
  135. pr_crit("aoe: invalid device pointer in %s\n",
  136. __func__);
  137. WARN_ON(1);
  138. return -ENODEV;
  139. }
  140. if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
  141. return -ENODEV;
  142. mutex_lock(&aoeblk_mutex);
  143. spin_lock_irqsave(&d->lock, flags);
  144. if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
  145. d->nopen++;
  146. spin_unlock_irqrestore(&d->lock, flags);
  147. mutex_unlock(&aoeblk_mutex);
  148. return 0;
  149. }
  150. spin_unlock_irqrestore(&d->lock, flags);
  151. mutex_unlock(&aoeblk_mutex);
  152. return -ENODEV;
  153. }
  154. static int
  155. aoeblk_release(struct gendisk *disk, fmode_t mode)
  156. {
  157. struct aoedev *d = disk->private_data;
  158. ulong flags;
  159. spin_lock_irqsave(&d->lock, flags);
  160. if (--d->nopen == 0) {
  161. spin_unlock_irqrestore(&d->lock, flags);
  162. aoecmd_cfg(d->aoemajor, d->aoeminor);
  163. return 0;
  164. }
  165. spin_unlock_irqrestore(&d->lock, flags);
  166. return 0;
  167. }
  168. static void
  169. aoeblk_request(struct request_queue *q)
  170. {
  171. struct aoedev *d;
  172. struct request *rq;
  173. d = q->queuedata;
  174. if ((d->flags & DEVFL_UP) == 0) {
  175. pr_info_ratelimited("aoe: device %ld.%d is not up\n",
  176. d->aoemajor, d->aoeminor);
  177. while ((rq = blk_peek_request(q))) {
  178. blk_start_request(rq);
  179. aoe_end_request(d, rq, 1);
  180. }
  181. return;
  182. }
  183. aoecmd_work(d);
  184. }
  185. static int
  186. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  187. {
  188. struct aoedev *d = bdev->bd_disk->private_data;
  189. if ((d->flags & DEVFL_UP) == 0) {
  190. printk(KERN_ERR "aoe: disk not up\n");
  191. return -ENODEV;
  192. }
  193. geo->cylinders = d->geo.cylinders;
  194. geo->heads = d->geo.heads;
  195. geo->sectors = d->geo.sectors;
  196. return 0;
  197. }
  198. static int
  199. aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
  200. {
  201. struct aoedev *d;
  202. if (!arg)
  203. return -EINVAL;
  204. d = bdev->bd_disk->private_data;
  205. if ((d->flags & DEVFL_UP) == 0) {
  206. pr_err("aoe: disk not up\n");
  207. return -ENODEV;
  208. }
  209. if (cmd == HDIO_GET_IDENTITY) {
  210. if (!copy_to_user((void __user *) arg, &d->ident,
  211. sizeof(d->ident)))
  212. return 0;
  213. return -EFAULT;
  214. }
  215. /* udev calls scsi_id, which uses SG_IO, resulting in noise */
  216. if (cmd != SG_IO)
  217. pr_info("aoe: unknown ioctl 0x%x\n", cmd);
  218. return -ENOTTY;
  219. }
  220. static const struct block_device_operations aoe_bdops = {
  221. .open = aoeblk_open,
  222. .release = aoeblk_release,
  223. .ioctl = aoeblk_ioctl,
  224. .getgeo = aoeblk_getgeo,
  225. .owner = THIS_MODULE,
  226. };
  227. /* alloc_disk and add_disk can sleep */
  228. void
  229. aoeblk_gdalloc(void *vp)
  230. {
  231. struct aoedev *d = vp;
  232. struct gendisk *gd;
  233. mempool_t *mp;
  234. struct request_queue *q;
  235. enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
  236. ulong flags;
  237. int late = 0;
  238. spin_lock_irqsave(&d->lock, flags);
  239. if (d->flags & DEVFL_GDALLOC
  240. && !(d->flags & DEVFL_TKILL)
  241. && !(d->flags & DEVFL_GD_NOW))
  242. d->flags |= DEVFL_GD_NOW;
  243. else
  244. late = 1;
  245. spin_unlock_irqrestore(&d->lock, flags);
  246. if (late)
  247. return;
  248. gd = alloc_disk(AOE_PARTITIONS);
  249. if (gd == NULL) {
  250. pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
  251. d->aoemajor, d->aoeminor);
  252. goto err;
  253. }
  254. mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
  255. buf_pool_cache);
  256. if (mp == NULL) {
  257. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
  258. d->aoemajor, d->aoeminor);
  259. goto err_disk;
  260. }
  261. q = blk_init_queue(aoeblk_request, &d->lock);
  262. if (q == NULL) {
  263. pr_err("aoe: cannot allocate block queue for %ld.%d\n",
  264. d->aoemajor, d->aoeminor);
  265. goto err_mempool;
  266. }
  267. spin_lock_irqsave(&d->lock, flags);
  268. WARN_ON(!(d->flags & DEVFL_GD_NOW));
  269. WARN_ON(!(d->flags & DEVFL_GDALLOC));
  270. WARN_ON(d->flags & DEVFL_TKILL);
  271. WARN_ON(d->gd);
  272. WARN_ON(d->flags & DEVFL_UP);
  273. blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
  274. q->backing_dev_info.name = "aoe";
  275. q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
  276. d->bufpool = mp;
  277. d->blkq = gd->queue = q;
  278. q->queuedata = d;
  279. d->gd = gd;
  280. if (aoe_maxsectors)
  281. blk_queue_max_hw_sectors(q, aoe_maxsectors);
  282. gd->major = AOE_MAJOR;
  283. gd->first_minor = d->sysminor;
  284. gd->fops = &aoe_bdops;
  285. gd->private_data = d;
  286. set_capacity(gd, d->ssize);
  287. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
  288. d->aoemajor, d->aoeminor);
  289. d->flags &= ~DEVFL_GDALLOC;
  290. d->flags |= DEVFL_UP;
  291. spin_unlock_irqrestore(&d->lock, flags);
  292. add_disk(gd);
  293. aoedisk_add_sysfs(d);
  294. spin_lock_irqsave(&d->lock, flags);
  295. WARN_ON(!(d->flags & DEVFL_GD_NOW));
  296. d->flags &= ~DEVFL_GD_NOW;
  297. spin_unlock_irqrestore(&d->lock, flags);
  298. return;
  299. err_mempool:
  300. mempool_destroy(mp);
  301. err_disk:
  302. put_disk(gd);
  303. err:
  304. spin_lock_irqsave(&d->lock, flags);
  305. d->flags &= ~DEVFL_GD_NOW;
  306. schedule_work(&d->work);
  307. spin_unlock_irqrestore(&d->lock, flags);
  308. }
  309. void
  310. aoeblk_exit(void)
  311. {
  312. kmem_cache_destroy(buf_pool_cache);
  313. }
  314. int __init
  315. aoeblk_init(void)
  316. {
  317. buf_pool_cache = kmem_cache_create("aoe_bufs",
  318. sizeof(struct buf),
  319. 0, 0, NULL);
  320. if (buf_pool_cache == NULL)
  321. return -ENOMEM;
  322. return 0;
  323. }