aoeblk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* Copyright (c) 2013 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. struct aoetgt **t, **te;
  106. struct aoeif *ifp, *ife;
  107. unsigned long flags;
  108. char c;
  109. d = s->private;
  110. seq_printf(s, "rttavg: %d rttdev: %d\n",
  111. d->rttavg >> RTTSCALE,
  112. d->rttdev >> RTTDSCALE);
  113. seq_printf(s, "nskbpool: %d\n", skb_queue_len(&d->skbpool));
  114. seq_printf(s, "kicked: %ld\n", d->kicked);
  115. seq_printf(s, "maxbcnt: %ld\n", d->maxbcnt);
  116. seq_printf(s, "ref: %ld\n", d->ref);
  117. spin_lock_irqsave(&d->lock, flags);
  118. t = d->targets;
  119. te = t + d->ntargets;
  120. for (; t < te && *t; t++) {
  121. c = '\t';
  122. seq_printf(s, "falloc: %ld\n", (*t)->falloc);
  123. seq_printf(s, "ffree: %p\n",
  124. list_empty(&(*t)->ffree) ? NULL : (*t)->ffree.next);
  125. seq_printf(s, "%pm:%d:%d:%d\n", (*t)->addr, (*t)->nout,
  126. (*t)->maxout, (*t)->nframes);
  127. seq_printf(s, "\tssthresh:%d\n", (*t)->ssthresh);
  128. seq_printf(s, "\ttaint:%d\n", (*t)->taint);
  129. seq_printf(s, "\tr:%d\n", (*t)->rpkts);
  130. seq_printf(s, "\tw:%d\n", (*t)->wpkts);
  131. ifp = (*t)->ifs;
  132. ife = ifp + ARRAY_SIZE((*t)->ifs);
  133. for (; ifp->nd && ifp < ife; ifp++) {
  134. seq_printf(s, "%c%s", c, ifp->nd->name);
  135. c = ',';
  136. }
  137. seq_puts(s, "\n");
  138. }
  139. spin_unlock_irqrestore(&d->lock, flags);
  140. return 0;
  141. }
  142. static int aoe_debugfs_open(struct inode *inode, struct file *file)
  143. {
  144. return single_open(file, aoedisk_debugfs_show, inode->i_private);
  145. }
  146. static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
  147. static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
  148. static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
  149. static struct device_attribute dev_attr_firmware_version = {
  150. .attr = { .name = "firmware-version", .mode = S_IRUGO },
  151. .show = aoedisk_show_fwver,
  152. };
  153. static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
  154. static struct attribute *aoe_attrs[] = {
  155. &dev_attr_state.attr,
  156. &dev_attr_mac.attr,
  157. &dev_attr_netif.attr,
  158. &dev_attr_firmware_version.attr,
  159. &dev_attr_payload.attr,
  160. NULL,
  161. };
  162. static const struct attribute_group attr_group = {
  163. .attrs = aoe_attrs,
  164. };
  165. static const struct file_operations aoe_debugfs_fops = {
  166. .open = aoe_debugfs_open,
  167. .read = seq_read,
  168. .llseek = seq_lseek,
  169. .release = single_release,
  170. };
  171. static void
  172. aoedisk_add_debugfs(struct aoedev *d)
  173. {
  174. struct dentry *entry;
  175. char *p;
  176. if (aoe_debugfs_dir == NULL)
  177. return;
  178. p = strchr(d->gd->disk_name, '/');
  179. if (p == NULL)
  180. p = d->gd->disk_name;
  181. else
  182. p++;
  183. BUG_ON(*p == '\0');
  184. entry = debugfs_create_file(p, 0444, aoe_debugfs_dir, d,
  185. &aoe_debugfs_fops);
  186. if (IS_ERR_OR_NULL(entry)) {
  187. pr_info("aoe: cannot create debugfs file for %s\n",
  188. d->gd->disk_name);
  189. return;
  190. }
  191. BUG_ON(d->debugfs);
  192. d->debugfs = entry;
  193. }
  194. void
  195. aoedisk_rm_debugfs(struct aoedev *d)
  196. {
  197. debugfs_remove(d->debugfs);
  198. d->debugfs = NULL;
  199. }
  200. static int
  201. aoedisk_add_sysfs(struct aoedev *d)
  202. {
  203. return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  204. }
  205. void
  206. aoedisk_rm_sysfs(struct aoedev *d)
  207. {
  208. sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
  209. }
  210. static int
  211. aoeblk_open(struct block_device *bdev, fmode_t mode)
  212. {
  213. struct aoedev *d = bdev->bd_disk->private_data;
  214. ulong flags;
  215. if (!virt_addr_valid(d)) {
  216. pr_crit("aoe: invalid device pointer in %s\n",
  217. __func__);
  218. WARN_ON(1);
  219. return -ENODEV;
  220. }
  221. if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
  222. return -ENODEV;
  223. mutex_lock(&aoeblk_mutex);
  224. spin_lock_irqsave(&d->lock, flags);
  225. if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
  226. d->nopen++;
  227. spin_unlock_irqrestore(&d->lock, flags);
  228. mutex_unlock(&aoeblk_mutex);
  229. return 0;
  230. }
  231. spin_unlock_irqrestore(&d->lock, flags);
  232. mutex_unlock(&aoeblk_mutex);
  233. return -ENODEV;
  234. }
  235. static void
  236. aoeblk_release(struct gendisk *disk, fmode_t mode)
  237. {
  238. struct aoedev *d = disk->private_data;
  239. ulong flags;
  240. spin_lock_irqsave(&d->lock, flags);
  241. if (--d->nopen == 0) {
  242. spin_unlock_irqrestore(&d->lock, flags);
  243. aoecmd_cfg(d->aoemajor, d->aoeminor);
  244. return;
  245. }
  246. spin_unlock_irqrestore(&d->lock, flags);
  247. }
  248. static void
  249. aoeblk_request(struct request_queue *q)
  250. {
  251. struct aoedev *d;
  252. struct request *rq;
  253. d = q->queuedata;
  254. if ((d->flags & DEVFL_UP) == 0) {
  255. pr_info_ratelimited("aoe: device %ld.%d is not up\n",
  256. d->aoemajor, d->aoeminor);
  257. while ((rq = blk_peek_request(q))) {
  258. blk_start_request(rq);
  259. aoe_end_request(d, rq, 1);
  260. }
  261. return;
  262. }
  263. aoecmd_work(d);
  264. }
  265. static int
  266. aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  267. {
  268. struct aoedev *d = bdev->bd_disk->private_data;
  269. if ((d->flags & DEVFL_UP) == 0) {
  270. printk(KERN_ERR "aoe: disk not up\n");
  271. return -ENODEV;
  272. }
  273. geo->cylinders = d->geo.cylinders;
  274. geo->heads = d->geo.heads;
  275. geo->sectors = d->geo.sectors;
  276. return 0;
  277. }
  278. static int
  279. aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
  280. {
  281. struct aoedev *d;
  282. if (!arg)
  283. return -EINVAL;
  284. d = bdev->bd_disk->private_data;
  285. if ((d->flags & DEVFL_UP) == 0) {
  286. pr_err("aoe: disk not up\n");
  287. return -ENODEV;
  288. }
  289. if (cmd == HDIO_GET_IDENTITY) {
  290. if (!copy_to_user((void __user *) arg, &d->ident,
  291. sizeof(d->ident)))
  292. return 0;
  293. return -EFAULT;
  294. }
  295. /* udev calls scsi_id, which uses SG_IO, resulting in noise */
  296. if (cmd != SG_IO)
  297. pr_info("aoe: unknown ioctl 0x%x\n", cmd);
  298. return -ENOTTY;
  299. }
  300. static const struct block_device_operations aoe_bdops = {
  301. .open = aoeblk_open,
  302. .release = aoeblk_release,
  303. .ioctl = aoeblk_ioctl,
  304. .getgeo = aoeblk_getgeo,
  305. .owner = THIS_MODULE,
  306. };
  307. /* alloc_disk and add_disk can sleep */
  308. void
  309. aoeblk_gdalloc(void *vp)
  310. {
  311. struct aoedev *d = vp;
  312. struct gendisk *gd;
  313. mempool_t *mp;
  314. struct request_queue *q;
  315. enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
  316. ulong flags;
  317. int late = 0;
  318. spin_lock_irqsave(&d->lock, flags);
  319. if (d->flags & DEVFL_GDALLOC
  320. && !(d->flags & DEVFL_TKILL)
  321. && !(d->flags & DEVFL_GD_NOW))
  322. d->flags |= DEVFL_GD_NOW;
  323. else
  324. late = 1;
  325. spin_unlock_irqrestore(&d->lock, flags);
  326. if (late)
  327. return;
  328. gd = alloc_disk(AOE_PARTITIONS);
  329. if (gd == NULL) {
  330. pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
  331. d->aoemajor, d->aoeminor);
  332. goto err;
  333. }
  334. mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
  335. buf_pool_cache);
  336. if (mp == NULL) {
  337. printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
  338. d->aoemajor, d->aoeminor);
  339. goto err_disk;
  340. }
  341. q = blk_init_queue(aoeblk_request, &d->lock);
  342. if (q == NULL) {
  343. pr_err("aoe: cannot allocate block queue for %ld.%d\n",
  344. d->aoemajor, d->aoeminor);
  345. goto err_mempool;
  346. }
  347. spin_lock_irqsave(&d->lock, flags);
  348. WARN_ON(!(d->flags & DEVFL_GD_NOW));
  349. WARN_ON(!(d->flags & DEVFL_GDALLOC));
  350. WARN_ON(d->flags & DEVFL_TKILL);
  351. WARN_ON(d->gd);
  352. WARN_ON(d->flags & DEVFL_UP);
  353. blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
  354. q->backing_dev_info.name = "aoe";
  355. q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
  356. d->bufpool = mp;
  357. d->blkq = gd->queue = q;
  358. q->queuedata = d;
  359. d->gd = gd;
  360. if (aoe_maxsectors)
  361. blk_queue_max_hw_sectors(q, aoe_maxsectors);
  362. gd->major = AOE_MAJOR;
  363. gd->first_minor = d->sysminor;
  364. gd->fops = &aoe_bdops;
  365. gd->private_data = d;
  366. set_capacity(gd, d->ssize);
  367. snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
  368. d->aoemajor, d->aoeminor);
  369. d->flags &= ~DEVFL_GDALLOC;
  370. d->flags |= DEVFL_UP;
  371. spin_unlock_irqrestore(&d->lock, flags);
  372. add_disk(gd);
  373. aoedisk_add_sysfs(d);
  374. aoedisk_add_debugfs(d);
  375. spin_lock_irqsave(&d->lock, flags);
  376. WARN_ON(!(d->flags & DEVFL_GD_NOW));
  377. d->flags &= ~DEVFL_GD_NOW;
  378. spin_unlock_irqrestore(&d->lock, flags);
  379. return;
  380. err_mempool:
  381. mempool_destroy(mp);
  382. err_disk:
  383. put_disk(gd);
  384. err:
  385. spin_lock_irqsave(&d->lock, flags);
  386. d->flags &= ~DEVFL_GD_NOW;
  387. schedule_work(&d->work);
  388. spin_unlock_irqrestore(&d->lock, flags);
  389. }
  390. void
  391. aoeblk_exit(void)
  392. {
  393. debugfs_remove_recursive(aoe_debugfs_dir);
  394. aoe_debugfs_dir = NULL;
  395. kmem_cache_destroy(buf_pool_cache);
  396. }
  397. int __init
  398. aoeblk_init(void)
  399. {
  400. buf_pool_cache = kmem_cache_create("aoe_bufs",
  401. sizeof(struct buf),
  402. 0, 0, NULL);
  403. if (buf_pool_cache == NULL)
  404. return -ENOMEM;
  405. aoe_debugfs_dir = debugfs_create_dir("aoe", NULL);
  406. if (IS_ERR_OR_NULL(aoe_debugfs_dir)) {
  407. pr_info("aoe: cannot create debugfs directory\n");
  408. aoe_debugfs_dir = NULL;
  409. }
  410. return 0;
  411. }