hmcdrv_ftp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * HMC Drive FTP Services
  3. *
  4. * Copyright IBM Corp. 2013
  5. * Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
  6. */
  7. #define KMSG_COMPONENT "hmcdrv"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/export.h>
  13. #include <linux/ctype.h>
  14. #include <linux/crc16.h>
  15. #include "hmcdrv_ftp.h"
  16. #include "hmcdrv_cache.h"
  17. #include "sclp_ftp.h"
  18. #include "diag_ftp.h"
  19. /**
  20. * struct hmcdrv_ftp_ops - HMC drive FTP operations
  21. * @startup: startup function
  22. * @shutdown: shutdown function
  23. * @cmd: FTP transfer function
  24. */
  25. struct hmcdrv_ftp_ops {
  26. int (*startup)(void);
  27. void (*shutdown)(void);
  28. ssize_t (*transfer)(const struct hmcdrv_ftp_cmdspec *ftp,
  29. size_t *fsize);
  30. };
  31. static enum hmcdrv_ftp_cmdid hmcdrv_ftp_cmd_getid(const char *cmd, int len);
  32. static int hmcdrv_ftp_parse(char *cmd, struct hmcdrv_ftp_cmdspec *ftp);
  33. static struct hmcdrv_ftp_ops *hmcdrv_ftp_funcs; /* current operations */
  34. static DEFINE_MUTEX(hmcdrv_ftp_mutex); /* mutex for hmcdrv_ftp_funcs */
  35. static unsigned hmcdrv_ftp_refcnt; /* start/shutdown reference counter */
  36. /**
  37. * hmcdrv_ftp_cmd_getid() - determine FTP command ID from a command string
  38. * @cmd: FTP command string (NOT zero-terminated)
  39. * @len: length of FTP command string in @cmd
  40. */
  41. static enum hmcdrv_ftp_cmdid hmcdrv_ftp_cmd_getid(const char *cmd, int len)
  42. {
  43. /* HMC FTP command descriptor */
  44. struct hmcdrv_ftp_cmd_desc {
  45. const char *str; /* command string */
  46. enum hmcdrv_ftp_cmdid cmd; /* associated command as enum */
  47. };
  48. /* Description of all HMC drive FTP commands
  49. *
  50. * Notes:
  51. * 1. Array size should be a prime number.
  52. * 2. Do not change the order of commands in table (because the
  53. * index is determined by CRC % ARRAY_SIZE).
  54. * 3. Original command 'nlist' was renamed, else the CRC would
  55. * collide with 'append' (see point 2).
  56. */
  57. static const struct hmcdrv_ftp_cmd_desc ftpcmds[7] = {
  58. {.str = "get", /* [0] get (CRC = 0x68eb) */
  59. .cmd = HMCDRV_FTP_GET},
  60. {.str = "dir", /* [1] dir (CRC = 0x6a9e) */
  61. .cmd = HMCDRV_FTP_DIR},
  62. {.str = "delete", /* [2] delete (CRC = 0x53ae) */
  63. .cmd = HMCDRV_FTP_DELETE},
  64. {.str = "nls", /* [3] nls (CRC = 0xf87c) */
  65. .cmd = HMCDRV_FTP_NLIST},
  66. {.str = "put", /* [4] put (CRC = 0xac56) */
  67. .cmd = HMCDRV_FTP_PUT},
  68. {.str = "append", /* [5] append (CRC = 0xf56e) */
  69. .cmd = HMCDRV_FTP_APPEND},
  70. {.str = NULL} /* [6] unused */
  71. };
  72. const struct hmcdrv_ftp_cmd_desc *pdesc;
  73. u16 crc = 0xffffU;
  74. if (len == 0)
  75. return HMCDRV_FTP_NOOP; /* error indiactor */
  76. crc = crc16(crc, cmd, len);
  77. pdesc = ftpcmds + (crc % ARRAY_SIZE(ftpcmds));
  78. pr_debug("FTP command '%s' has CRC 0x%04x, at table pos. %lu\n",
  79. cmd, crc, (crc % ARRAY_SIZE(ftpcmds)));
  80. if (!pdesc->str || strncmp(pdesc->str, cmd, len))
  81. return HMCDRV_FTP_NOOP;
  82. pr_debug("FTP command '%s' found, with ID %d\n",
  83. pdesc->str, pdesc->cmd);
  84. return pdesc->cmd;
  85. }
  86. /**
  87. * hmcdrv_ftp_parse() - HMC drive FTP command parser
  88. * @cmd: FTP command string "<cmd> <filename>"
  89. * @ftp: Pointer to FTP command specification buffer (output)
  90. *
  91. * Return: 0 on success, else a (negative) error code
  92. */
  93. static int hmcdrv_ftp_parse(char *cmd, struct hmcdrv_ftp_cmdspec *ftp)
  94. {
  95. char *start;
  96. int argc = 0;
  97. ftp->id = HMCDRV_FTP_NOOP;
  98. ftp->fname = NULL;
  99. while (*cmd != '\0') {
  100. while (isspace(*cmd))
  101. ++cmd;
  102. if (*cmd == '\0')
  103. break;
  104. start = cmd;
  105. switch (argc) {
  106. case 0: /* 1st argument (FTP command) */
  107. while ((*cmd != '\0') && !isspace(*cmd))
  108. ++cmd;
  109. ftp->id = hmcdrv_ftp_cmd_getid(start, cmd - start);
  110. break;
  111. case 1: /* 2nd / last argument (rest of line) */
  112. while ((*cmd != '\0') && !iscntrl(*cmd))
  113. ++cmd;
  114. ftp->fname = start;
  115. /* fall through */
  116. default:
  117. *cmd = '\0';
  118. break;
  119. } /* switch */
  120. ++argc;
  121. } /* while */
  122. if (!ftp->fname || (ftp->id == HMCDRV_FTP_NOOP))
  123. return -EINVAL;
  124. return 0;
  125. }
  126. /**
  127. * hmcdrv_ftp_do() - perform a HMC drive FTP, with data from kernel-space
  128. * @ftp: pointer to FTP command specification
  129. *
  130. * Return: number of bytes read/written or a negative error code
  131. */
  132. ssize_t hmcdrv_ftp_do(const struct hmcdrv_ftp_cmdspec *ftp)
  133. {
  134. ssize_t len;
  135. mutex_lock(&hmcdrv_ftp_mutex);
  136. if (hmcdrv_ftp_funcs && hmcdrv_ftp_refcnt) {
  137. pr_debug("starting transfer, cmd %d for '%s' at %lld with %zd bytes\n",
  138. ftp->id, ftp->fname, (long long) ftp->ofs, ftp->len);
  139. len = hmcdrv_cache_cmd(ftp, hmcdrv_ftp_funcs->transfer);
  140. } else {
  141. len = -ENXIO;
  142. }
  143. mutex_unlock(&hmcdrv_ftp_mutex);
  144. return len;
  145. }
  146. EXPORT_SYMBOL(hmcdrv_ftp_do);
  147. /**
  148. * hmcdrv_ftp_probe() - probe for the HMC drive FTP service
  149. *
  150. * Return: 0 if service is available, else an (negative) error code
  151. */
  152. int hmcdrv_ftp_probe(void)
  153. {
  154. int rc;
  155. struct hmcdrv_ftp_cmdspec ftp = {
  156. .id = HMCDRV_FTP_NOOP,
  157. .ofs = 0,
  158. .fname = "",
  159. .len = PAGE_SIZE
  160. };
  161. ftp.buf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  162. if (!ftp.buf)
  163. return -ENOMEM;
  164. rc = hmcdrv_ftp_startup();
  165. if (rc)
  166. goto out;
  167. rc = hmcdrv_ftp_do(&ftp);
  168. hmcdrv_ftp_shutdown();
  169. switch (rc) {
  170. case -ENOENT: /* no such file/media or currently busy, */
  171. case -EBUSY: /* but service seems to be available */
  172. rc = 0;
  173. break;
  174. default: /* leave 'rc' as it is for [0, -EPERM, -E...] */
  175. if (rc > 0)
  176. rc = 0; /* clear length (success) */
  177. break;
  178. } /* switch */
  179. out:
  180. free_page((unsigned long) ftp.buf);
  181. return rc;
  182. }
  183. EXPORT_SYMBOL(hmcdrv_ftp_probe);
  184. /**
  185. * hmcdrv_ftp_cmd() - Perform a HMC drive FTP, with data from user-space
  186. *
  187. * @cmd: FTP command string "<cmd> <filename>"
  188. * @offset: file position to read/write
  189. * @buf: user-space buffer for read/written directory/file
  190. * @len: size of @buf (read/dir) or number of bytes to write
  191. *
  192. * This function must not be called before hmcdrv_ftp_startup() was called.
  193. *
  194. * Return: number of bytes read/written or a negative error code
  195. */
  196. ssize_t hmcdrv_ftp_cmd(char __kernel *cmd, loff_t offset,
  197. char __user *buf, size_t len)
  198. {
  199. int order;
  200. struct hmcdrv_ftp_cmdspec ftp = {.len = len, .ofs = offset};
  201. ssize_t retlen = hmcdrv_ftp_parse(cmd, &ftp);
  202. if (retlen)
  203. return retlen;
  204. order = get_order(ftp.len);
  205. ftp.buf = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, order);
  206. if (!ftp.buf)
  207. return -ENOMEM;
  208. switch (ftp.id) {
  209. case HMCDRV_FTP_DIR:
  210. case HMCDRV_FTP_NLIST:
  211. case HMCDRV_FTP_GET:
  212. retlen = hmcdrv_ftp_do(&ftp);
  213. if ((retlen >= 0) &&
  214. copy_to_user(buf, ftp.buf, retlen))
  215. retlen = -EFAULT;
  216. break;
  217. case HMCDRV_FTP_PUT:
  218. case HMCDRV_FTP_APPEND:
  219. if (!copy_from_user(ftp.buf, buf, ftp.len))
  220. retlen = hmcdrv_ftp_do(&ftp);
  221. else
  222. retlen = -EFAULT;
  223. break;
  224. case HMCDRV_FTP_DELETE:
  225. retlen = hmcdrv_ftp_do(&ftp);
  226. break;
  227. default:
  228. retlen = -EOPNOTSUPP;
  229. break;
  230. }
  231. free_pages((unsigned long) ftp.buf, order);
  232. return retlen;
  233. }
  234. /**
  235. * hmcdrv_ftp_startup() - startup of HMC drive FTP functionality for a
  236. * dedicated (owner) instance
  237. *
  238. * Return: 0 on success, else an (negative) error code
  239. */
  240. int hmcdrv_ftp_startup(void)
  241. {
  242. static struct hmcdrv_ftp_ops hmcdrv_ftp_zvm = {
  243. .startup = diag_ftp_startup,
  244. .shutdown = diag_ftp_shutdown,
  245. .transfer = diag_ftp_cmd
  246. };
  247. static struct hmcdrv_ftp_ops hmcdrv_ftp_lpar = {
  248. .startup = sclp_ftp_startup,
  249. .shutdown = sclp_ftp_shutdown,
  250. .transfer = sclp_ftp_cmd
  251. };
  252. int rc = 0;
  253. mutex_lock(&hmcdrv_ftp_mutex); /* block transfers while start-up */
  254. if (hmcdrv_ftp_refcnt == 0) {
  255. if (MACHINE_IS_VM)
  256. hmcdrv_ftp_funcs = &hmcdrv_ftp_zvm;
  257. else if (MACHINE_IS_LPAR || MACHINE_IS_KVM)
  258. hmcdrv_ftp_funcs = &hmcdrv_ftp_lpar;
  259. else
  260. rc = -EOPNOTSUPP;
  261. if (hmcdrv_ftp_funcs)
  262. rc = hmcdrv_ftp_funcs->startup();
  263. }
  264. if (!rc)
  265. ++hmcdrv_ftp_refcnt;
  266. mutex_unlock(&hmcdrv_ftp_mutex);
  267. return rc;
  268. }
  269. EXPORT_SYMBOL(hmcdrv_ftp_startup);
  270. /**
  271. * hmcdrv_ftp_shutdown() - shutdown of HMC drive FTP functionality for a
  272. * dedicated (owner) instance
  273. */
  274. void hmcdrv_ftp_shutdown(void)
  275. {
  276. mutex_lock(&hmcdrv_ftp_mutex);
  277. --hmcdrv_ftp_refcnt;
  278. if ((hmcdrv_ftp_refcnt == 0) && hmcdrv_ftp_funcs)
  279. hmcdrv_ftp_funcs->shutdown();
  280. mutex_unlock(&hmcdrv_ftp_mutex);
  281. }
  282. EXPORT_SYMBOL(hmcdrv_ftp_shutdown);