aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/s390/char/hmcdrv_ftp.c
blob: 8cb7d8fbadd62983dbf56cebc25fb493a31a9860 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 *    HMC Drive FTP Services
 *
 *    Copyright IBM Corp. 2013
 *    Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
 */

#define KMSG_COMPONENT "hmcdrv"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/export.h>

#include <linux/ctype.h>
#include <linux/crc16.h>

#include "hmcdrv_ftp.h"
#include "hmcdrv_cache.h"
#include "sclp_ftp.h"
#include "diag_ftp.h"

/**
 * struct hmcdrv_ftp_ops - HMC drive FTP operations
 * @startup: startup function
 * @shutdown: shutdown function
 * @cmd: FTP transfer function
 */
struct hmcdrv_ftp_ops {
	int (*startup)(void);
	void (*shutdown)(void);
	ssize_t (*transfer)(const struct hmcdrv_ftp_cmdspec *ftp,
			    size_t *fsize);
};

static enum hmcdrv_ftp_cmdid hmcdrv_ftp_cmd_getid(const char *cmd, int len);
static int hmcdrv_ftp_parse(char *cmd, struct hmcdrv_ftp_cmdspec *ftp);

static const struct hmcdrv_ftp_ops *hmcdrv_ftp_funcs; /* current operations */
static DEFINE_MUTEX(hmcdrv_ftp_mutex); /* mutex for hmcdrv_ftp_funcs */
static unsigned hmcdrv_ftp_refcnt; /* start/shutdown reference counter */

/**
 * hmcdrv_ftp_cmd_getid() - determine FTP command ID from a command string
 * @cmd: FTP command string (NOT zero-terminated)
 * @len: length of FTP command string in @cmd
 */
static enum hmcdrv_ftp_cmdid hmcdrv_ftp_cmd_getid(const char *cmd, int len)
{
	/* HMC FTP command descriptor */
	struct hmcdrv_ftp_cmd_desc {
		const char *str;	   /* command string */
		enum hmcdrv_ftp_cmdid cmd; /* associated command as enum */
	};

	/* Description of all HMC drive FTP commands
	 *
	 * Notes:
	 * 1. Array size should be a prime number.
	 * 2. Do not change the order of commands in table (because the
	 *    index is determined by CRC % ARRAY_SIZE).
	 * 3. Original command 'nlist' was renamed, else the CRC would
	 *    collide with 'append' (see point 2).
	 */
	static const struct hmcdrv_ftp_cmd_desc ftpcmds[7] = {

		{.str = "get", /* [0] get (CRC = 0x68eb) */
		 .cmd = HMCDRV_FTP_GET},
		{.str = "dir", /* [1] dir (CRC = 0x6a9e) */
		 .cmd = HMCDRV_FTP_DIR},
		{.str = "delete", /* [2] delete (CRC = 0x53ae) */
		 .cmd = HMCDRV_FTP_DELETE},
		{.str = "nls", /* [3] nls (CRC = 0xf87c) */
		 .cmd = HMCDRV_FTP_NLIST},
		{.str = "put", /* [4] put (CRC = 0xac56) */
		 .cmd = HMCDRV_FTP_PUT},
		{.str = "append", /* [5] append (CRC = 0xf56e) */
		 .cmd = HMCDRV_FTP_APPEND},
		{.str = NULL} /* [6] unused */
	};

	const struct hmcdrv_ftp_cmd_desc *pdesc;

	u16 crc = 0xffffU;

	if (len == 0)
		return HMCDRV_FTP_NOOP; /* error indiactor */

	crc = crc16(crc, cmd, len);
	pdesc = ftpcmds + (crc % ARRAY_SIZE(ftpcmds));
	pr_debug("FTP command '%s' has CRC 0x%04x, at table pos. %lu\n",
		 cmd, crc, (crc % ARRAY_SIZE(ftpcmds)));

	if (!pdesc->str || strncmp(pdesc->str, cmd, len))
		return HMCDRV_FTP_NOOP;

	pr_debug("FTP command '%s' found, with ID %d\n",
		 pdesc->str, pdesc->cmd);

	return pdesc->cmd;
}

/**
 * hmcdrv_ftp_parse() - HMC drive FTP command parser
 * @cmd: FTP command string "<cmd> <filename>"
 * @ftp: Pointer to FTP command specification buffer (output)
 *
 * Return: 0 on success, else a (negative) error code
 */
static int hmcdrv_ftp_parse(char *cmd, struct hmcdrv_ftp_cmdspec *ftp)
{
	char *start;
	int argc = 0;

	ftp->id = HMCDRV_FTP_NOOP;
	ftp->fname = NULL;

	while (*cmd != '\0') {

		while (isspace(*cmd))
			++cmd;

		if (*cmd == '\0')
			break;

		start = cmd;

		switch (argc) {
		case 0: /* 1st argument (FTP command) */
			while ((*cmd != '\0') && !isspace(*cmd))
				++cmd;
			ftp->id = hmcdrv_ftp_cmd_getid(start, cmd - start);
			break;
		case 1: /* 2nd / last argument (rest of line) */
			while ((*cmd != '\0') && !iscntrl(*cmd))
				++cmd;
			ftp->fname = start;
			/* fall through */
		default:
			*cmd = '\0';
			break;
		} /* switch */

		++argc;
	} /* while */

	if (!ftp->fname || (ftp->id == HMCDRV_FTP_NOOP))
		return -EINVAL;

	return 0;
}

/**
 * hmcdrv_ftp_do() - perform a HMC drive FTP, with data from kernel-space
 * @ftp: pointer to FTP command specification
 *
 * Return: number of bytes read/written or a negative error code
 */
ssize_t hmcdrv_ftp_do(const struct hmcdrv_ftp_cmdspec *ftp)
{
	ssize_t len;

	mutex_lock(&hmcdrv_ftp_mutex);

	if (hmcdrv_ftp_funcs && hmcdrv_ftp_refcnt) {
		pr_debug("starting transfer, cmd %d for '%s' at %lld with %zd bytes\n",
			 ftp->id, ftp->fname, (long long) ftp->ofs, ftp->len);
		len = hmcdrv_cache_cmd(ftp, hmcdrv_ftp_funcs->transfer);
	} else {
		len = -ENXIO;
	}

	mutex_unlock(&hmcdrv_ftp_mutex);
	return len;
}
EXPORT_SYMBOL(hmcdrv_ftp_do);

/**
 * hmcdrv_ftp_probe() - probe for the HMC drive FTP service
 *
 * Return: 0 if service is available, else an (negative) error code
 */
int hmcdrv_ftp_probe(void)
{
	int rc;

	struct hmcdrv_ftp_cmdspec ftp = {
		.id = HMCDRV_FTP_NOOP,
		.ofs = 0,
		.fname = "",
		.len = PAGE_SIZE
	};

	ftp.buf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);

	if (!ftp.buf)
		return -ENOMEM;

	rc = hmcdrv_ftp_startup();

	if (rc)
		goto out;

	rc = hmcdrv_ftp_do(&ftp);
	hmcdrv_ftp_shutdown();

	switch (rc) {
	case -ENOENT: /* no such file/media or currently busy, */
	case -EBUSY:  /* but service seems to be available */
		rc = 0;
		break;
	default: /* leave 'rc' as it is for [0, -EPERM, -E...] */
		if (rc > 0)
			rc = 0; /* clear length (success) */
		break;
	} /* switch */
out:
	free_page((unsigned long) ftp.buf);
	return rc;
}
EXPORT_SYMBOL(hmcdrv_ftp_probe);

/**
 * hmcdrv_ftp_cmd() - Perform a HMC drive FTP, with data from user-space
 *
 * @cmd: FTP command string "<cmd> <filename>"
 * @offset: file position to read/write
 * @buf: user-space buffer for read/written directory/file
 * @len: size of @buf (read/dir) or number of bytes to write
 *
 * This function must not be called before hmcdrv_ftp_startup() was called.
 *
 * Return: number of bytes read/written or a negative error code
 */
ssize_t hmcdrv_ftp_cmd(char __kernel *cmd, loff_t offset,
		       char __user *buf, size_t len)
{
	int order;

	struct hmcdrv_ftp_cmdspec ftp = {.len = len, .ofs = offset};
	ssize_t retlen = hmcdrv_ftp_parse(cmd, &ftp);

	if (retlen)
		return retlen;

	order = get_order(ftp.len);
	ftp.buf = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, order);

	if (!ftp.buf)
		return -ENOMEM;

	switch (ftp.id) {
	case HMCDRV_FTP_DIR:
	case HMCDRV_FTP_NLIST:
	case HMCDRV_FTP_GET:
		retlen = hmcdrv_ftp_do(&ftp);

		if ((retlen >= 0) &&
		    copy_to_user(buf, ftp.buf, retlen))
			retlen = -EFAULT;
		break;

	case HMCDRV_FTP_PUT:
	case HMCDRV_FTP_APPEND:
		if (!copy_from_user(ftp.buf, buf, ftp.len))
			retlen = hmcdrv_ftp_do(&ftp);
		else
			retlen = -EFAULT;
		break;

	case HMCDRV_FTP_DELETE:
		retlen = hmcdrv_ftp_do(&ftp);
		break;

	default:
		retlen = -EOPNOTSUPP;
		break;
	}

	free_pages((unsigned long) ftp.buf, order);
	return retlen;
}

/**
 * hmcdrv_ftp_startup() - startup of HMC drive FTP functionality for a
 * dedicated (owner) instance
 *
 * Return: 0 on success, else an (negative) error code
 */
int hmcdrv_ftp_startup(void)
{
	static const struct hmcdrv_ftp_ops hmcdrv_ftp_zvm = {
		.startup = diag_ftp_startup,
		.shutdown = diag_ftp_shutdown,
		.transfer = diag_ftp_cmd
	};

	static const struct hmcdrv_ftp_ops hmcdrv_ftp_lpar = {
		.startup = sclp_ftp_startup,
		.shutdown = sclp_ftp_shutdown,
		.transfer = sclp_ftp_cmd
	};

	int rc = 0;

	mutex_lock(&hmcdrv_ftp_mutex); /* block transfers while start-up */

	if (hmcdrv_ftp_refcnt == 0) {
		if (MACHINE_IS_VM)
			hmcdrv_ftp_funcs = &hmcdrv_ftp_zvm;
		else if (MACHINE_IS_LPAR || MACHINE_IS_KVM)
			hmcdrv_ftp_funcs = &hmcdrv_ftp_lpar;
		else
			rc = -EOPNOTSUPP;

		if (hmcdrv_ftp_funcs)
			rc = hmcdrv_ftp_funcs->startup();
	}

	if (!rc)
		++hmcdrv_ftp_refcnt;

	mutex_unlock(&hmcdrv_ftp_mutex);
	return rc;
}
EXPORT_SYMBOL(hmcdrv_ftp_startup);

/**
 * hmcdrv_ftp_shutdown() - shutdown of HMC drive FTP functionality for a
 * dedicated (owner) instance
 */
void hmcdrv_ftp_shutdown(void)
{
	mutex_lock(&hmcdrv_ftp_mutex);
	--hmcdrv_ftp_refcnt;

	if ((hmcdrv_ftp_refcnt == 0) && hmcdrv_ftp_funcs)
		hmcdrv_ftp_funcs->shutdown();

	mutex_unlock(&hmcdrv_ftp_mutex);
}
EXPORT_SYMBOL(hmcdrv_ftp_shutdown);