hmcdrv_ftp.c 8.0 KB

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