aoechr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoechr.c
  4. * AoE character device driver
  5. */
  6. #include <linux/hdreg.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/completion.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/mutex.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/export.h>
  14. #include "aoe.h"
  15. enum {
  16. //MINOR_STAT = 1, (moved to sysfs)
  17. MINOR_ERR = 2,
  18. MINOR_DISCOVER,
  19. MINOR_INTERFACES,
  20. MINOR_REVALIDATE,
  21. MINOR_FLUSH,
  22. MSGSZ = 2048,
  23. NMSG = 100, /* message backlog to retain */
  24. };
  25. struct aoe_chardev {
  26. ulong minor;
  27. char name[32];
  28. };
  29. enum { EMFL_VALID = 1 };
  30. struct ErrMsg {
  31. short flags;
  32. short len;
  33. char *msg;
  34. };
  35. static DEFINE_MUTEX(aoechr_mutex);
  36. /* A ring buffer of error messages, to be read through
  37. * "/dev/etherd/err". When no messages are present,
  38. * readers will block waiting for messages to appear.
  39. */
  40. static struct ErrMsg emsgs[NMSG];
  41. static int emsgs_head_idx, emsgs_tail_idx;
  42. static struct completion emsgs_comp;
  43. static spinlock_t emsgs_lock;
  44. static int nblocked_emsgs_readers;
  45. static struct class *aoe_class;
  46. static struct aoe_chardev chardevs[] = {
  47. { MINOR_ERR, "err" },
  48. { MINOR_DISCOVER, "discover" },
  49. { MINOR_INTERFACES, "interfaces" },
  50. { MINOR_REVALIDATE, "revalidate" },
  51. { MINOR_FLUSH, "flush" },
  52. };
  53. static int
  54. discover(void)
  55. {
  56. aoecmd_cfg(0xffff, 0xff);
  57. return 0;
  58. }
  59. static int
  60. interfaces(const char __user *str, size_t size)
  61. {
  62. if (set_aoe_iflist(str, size)) {
  63. printk(KERN_ERR
  64. "aoe: could not set interface list: too many interfaces\n");
  65. return -EINVAL;
  66. }
  67. return 0;
  68. }
  69. static int
  70. revalidate(const char __user *str, size_t size)
  71. {
  72. int major, minor, n;
  73. ulong flags;
  74. struct aoedev *d;
  75. struct sk_buff *skb;
  76. char buf[16];
  77. if (size >= sizeof buf)
  78. return -EINVAL;
  79. buf[sizeof buf - 1] = '\0';
  80. if (copy_from_user(buf, str, size))
  81. return -EFAULT;
  82. n = sscanf(buf, "e%d.%d", &major, &minor);
  83. if (n != 2) {
  84. pr_err("aoe: invalid device specification %s\n", buf);
  85. return -EINVAL;
  86. }
  87. d = aoedev_by_aoeaddr(major, minor, 0);
  88. if (!d)
  89. return -EINVAL;
  90. spin_lock_irqsave(&d->lock, flags);
  91. aoecmd_cleanslate(d);
  92. aoecmd_cfg(major, minor);
  93. loop:
  94. skb = aoecmd_ata_id(d);
  95. spin_unlock_irqrestore(&d->lock, flags);
  96. /* try again if we are able to sleep a bit,
  97. * otherwise give up this revalidation
  98. */
  99. if (!skb && !msleep_interruptible(250)) {
  100. spin_lock_irqsave(&d->lock, flags);
  101. goto loop;
  102. }
  103. aoedev_put(d);
  104. if (skb) {
  105. struct sk_buff_head queue;
  106. __skb_queue_head_init(&queue);
  107. __skb_queue_tail(&queue, skb);
  108. aoenet_xmit(&queue);
  109. }
  110. return 0;
  111. }
  112. void
  113. aoechr_error(char *msg)
  114. {
  115. struct ErrMsg *em;
  116. char *mp;
  117. ulong flags, n;
  118. n = strlen(msg);
  119. spin_lock_irqsave(&emsgs_lock, flags);
  120. em = emsgs + emsgs_tail_idx;
  121. if ((em->flags & EMFL_VALID)) {
  122. bail: spin_unlock_irqrestore(&emsgs_lock, flags);
  123. return;
  124. }
  125. mp = kmemdup(msg, n, GFP_ATOMIC);
  126. if (mp == NULL) {
  127. printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
  128. goto bail;
  129. }
  130. em->msg = mp;
  131. em->flags |= EMFL_VALID;
  132. em->len = n;
  133. emsgs_tail_idx++;
  134. emsgs_tail_idx %= ARRAY_SIZE(emsgs);
  135. spin_unlock_irqrestore(&emsgs_lock, flags);
  136. if (nblocked_emsgs_readers)
  137. complete(&emsgs_comp);
  138. }
  139. static ssize_t
  140. aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
  141. {
  142. int ret = -EINVAL;
  143. switch ((unsigned long) filp->private_data) {
  144. default:
  145. printk(KERN_INFO "aoe: can't write to that file.\n");
  146. break;
  147. case MINOR_DISCOVER:
  148. ret = discover();
  149. break;
  150. case MINOR_INTERFACES:
  151. ret = interfaces(buf, cnt);
  152. break;
  153. case MINOR_REVALIDATE:
  154. ret = revalidate(buf, cnt);
  155. break;
  156. case MINOR_FLUSH:
  157. ret = aoedev_flush(buf, cnt);
  158. break;
  159. }
  160. if (ret == 0)
  161. ret = cnt;
  162. return ret;
  163. }
  164. static int
  165. aoechr_open(struct inode *inode, struct file *filp)
  166. {
  167. int n, i;
  168. mutex_lock(&aoechr_mutex);
  169. n = iminor(inode);
  170. filp->private_data = (void *) (unsigned long) n;
  171. for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
  172. if (chardevs[i].minor == n) {
  173. mutex_unlock(&aoechr_mutex);
  174. return 0;
  175. }
  176. mutex_unlock(&aoechr_mutex);
  177. return -EINVAL;
  178. }
  179. static int
  180. aoechr_rel(struct inode *inode, struct file *filp)
  181. {
  182. return 0;
  183. }
  184. static ssize_t
  185. aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
  186. {
  187. unsigned long n;
  188. char *mp;
  189. struct ErrMsg *em;
  190. ssize_t len;
  191. ulong flags;
  192. n = (unsigned long) filp->private_data;
  193. if (n != MINOR_ERR)
  194. return -EFAULT;
  195. spin_lock_irqsave(&emsgs_lock, flags);
  196. for (;;) {
  197. em = emsgs + emsgs_head_idx;
  198. if ((em->flags & EMFL_VALID) != 0)
  199. break;
  200. if (filp->f_flags & O_NDELAY) {
  201. spin_unlock_irqrestore(&emsgs_lock, flags);
  202. return -EAGAIN;
  203. }
  204. nblocked_emsgs_readers++;
  205. spin_unlock_irqrestore(&emsgs_lock, flags);
  206. n = wait_for_completion_interruptible(&emsgs_comp);
  207. spin_lock_irqsave(&emsgs_lock, flags);
  208. nblocked_emsgs_readers--;
  209. if (n) {
  210. spin_unlock_irqrestore(&emsgs_lock, flags);
  211. return -ERESTARTSYS;
  212. }
  213. }
  214. if (em->len > cnt) {
  215. spin_unlock_irqrestore(&emsgs_lock, flags);
  216. return -EAGAIN;
  217. }
  218. mp = em->msg;
  219. len = em->len;
  220. em->msg = NULL;
  221. em->flags &= ~EMFL_VALID;
  222. emsgs_head_idx++;
  223. emsgs_head_idx %= ARRAY_SIZE(emsgs);
  224. spin_unlock_irqrestore(&emsgs_lock, flags);
  225. n = copy_to_user(buf, mp, len);
  226. kfree(mp);
  227. return n == 0 ? len : -EFAULT;
  228. }
  229. static const struct file_operations aoe_fops = {
  230. .write = aoechr_write,
  231. .read = aoechr_read,
  232. .open = aoechr_open,
  233. .release = aoechr_rel,
  234. .owner = THIS_MODULE,
  235. .llseek = noop_llseek,
  236. };
  237. static char *aoe_devnode(struct device *dev, umode_t *mode)
  238. {
  239. return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
  240. }
  241. int __init
  242. aoechr_init(void)
  243. {
  244. int n, i;
  245. n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
  246. if (n < 0) {
  247. printk(KERN_ERR "aoe: can't register char device\n");
  248. return n;
  249. }
  250. init_completion(&emsgs_comp);
  251. spin_lock_init(&emsgs_lock);
  252. aoe_class = class_create(THIS_MODULE, "aoe");
  253. if (IS_ERR(aoe_class)) {
  254. unregister_chrdev(AOE_MAJOR, "aoechr");
  255. return PTR_ERR(aoe_class);
  256. }
  257. aoe_class->devnode = aoe_devnode;
  258. for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
  259. device_create(aoe_class, NULL,
  260. MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
  261. chardevs[i].name);
  262. return 0;
  263. }
  264. void
  265. aoechr_exit(void)
  266. {
  267. int i;
  268. for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
  269. device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
  270. class_destroy(aoe_class);
  271. unregister_chrdev(AOE_MAJOR, "aoechr");
  272. }