aoeblk.c 10 KB

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