• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/fs/9p/trans_xen
3  *
4  * Xen transport layer.
5  *
6  * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32 
33 #include <xen/events.h>
34 #include <xen/grant_table.h>
35 #include <xen/xen.h>
36 #include <xen/xenbus.h>
37 #include <xen/interface/io/9pfs.h>
38 
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <net/9p/9p.h>
42 #include <net/9p/client.h>
43 #include <net/9p/transport.h>
44 
45 #define XEN_9PFS_NUM_RINGS 2
46 #define XEN_9PFS_RING_ORDER 9
47 #define XEN_9PFS_RING_SIZE(ring)  XEN_FLEX_RING_SIZE(ring->intf->ring_order)
48 
49 struct xen_9pfs_header {
50 	uint32_t size;
51 	uint8_t id;
52 	uint16_t tag;
53 
54 	/* uint8_t sdata[]; */
55 } __attribute__((packed));
56 
57 /* One per ring, more than one per 9pfs share */
58 struct xen_9pfs_dataring {
59 	struct xen_9pfs_front_priv *priv;
60 
61 	struct xen_9pfs_data_intf *intf;
62 	grant_ref_t ref;
63 	int evtchn;
64 	int irq;
65 	/* protect a ring from concurrent accesses */
66 	spinlock_t lock;
67 
68 	struct xen_9pfs_data data;
69 	wait_queue_head_t wq;
70 	struct work_struct work;
71 };
72 
73 /* One per 9pfs share */
74 struct xen_9pfs_front_priv {
75 	struct list_head list;
76 	struct xenbus_device *dev;
77 	char *tag;
78 	struct p9_client *client;
79 
80 	int num_rings;
81 	struct xen_9pfs_dataring *rings;
82 };
83 
84 static LIST_HEAD(xen_9pfs_devs);
85 static DEFINE_RWLOCK(xen_9pfs_lock);
86 
87 /* We don't currently allow canceling of requests */
p9_xen_cancel(struct p9_client * client,struct p9_req_t * req)88 static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
89 {
90 	return 1;
91 }
92 
p9_xen_create(struct p9_client * client,const char * addr,char * args)93 static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
94 {
95 	struct xen_9pfs_front_priv *priv;
96 
97 	if (addr == NULL)
98 		return -EINVAL;
99 
100 	read_lock(&xen_9pfs_lock);
101 	list_for_each_entry(priv, &xen_9pfs_devs, list) {
102 		if (!strcmp(priv->tag, addr)) {
103 			priv->client = client;
104 			read_unlock(&xen_9pfs_lock);
105 			return 0;
106 		}
107 	}
108 	read_unlock(&xen_9pfs_lock);
109 	return -EINVAL;
110 }
111 
p9_xen_close(struct p9_client * client)112 static void p9_xen_close(struct p9_client *client)
113 {
114 	struct xen_9pfs_front_priv *priv;
115 
116 	read_lock(&xen_9pfs_lock);
117 	list_for_each_entry(priv, &xen_9pfs_devs, list) {
118 		if (priv->client == client) {
119 			priv->client = NULL;
120 			read_unlock(&xen_9pfs_lock);
121 			return;
122 		}
123 	}
124 	read_unlock(&xen_9pfs_lock);
125 }
126 
p9_xen_write_todo(struct xen_9pfs_dataring * ring,RING_IDX size)127 static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
128 {
129 	RING_IDX cons, prod;
130 
131 	cons = ring->intf->out_cons;
132 	prod = ring->intf->out_prod;
133 	virt_mb();
134 
135 	return XEN_9PFS_RING_SIZE(ring) -
136 		xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) >= size;
137 }
138 
p9_xen_request(struct p9_client * client,struct p9_req_t * p9_req)139 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
140 {
141 	struct xen_9pfs_front_priv *priv;
142 	RING_IDX cons, prod, masked_cons, masked_prod;
143 	unsigned long flags;
144 	u32 size = p9_req->tc.size;
145 	struct xen_9pfs_dataring *ring;
146 	int num;
147 
148 	read_lock(&xen_9pfs_lock);
149 	list_for_each_entry(priv, &xen_9pfs_devs, list) {
150 		if (priv->client == client)
151 			break;
152 	}
153 	read_unlock(&xen_9pfs_lock);
154 	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
155 		return -EINVAL;
156 
157 	num = p9_req->tc.tag % priv->num_rings;
158 	ring = &priv->rings[num];
159 
160 again:
161 	while (wait_event_killable(ring->wq,
162 				   p9_xen_write_todo(ring, size)) != 0)
163 		;
164 
165 	spin_lock_irqsave(&ring->lock, flags);
166 	cons = ring->intf->out_cons;
167 	prod = ring->intf->out_prod;
168 	virt_mb();
169 
170 	if (XEN_9PFS_RING_SIZE(ring) -
171 	    xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < size) {
172 		spin_unlock_irqrestore(&ring->lock, flags);
173 		goto again;
174 	}
175 
176 	masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
177 	masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
178 
179 	xen_9pfs_write_packet(ring->data.out, p9_req->tc.sdata, size,
180 			      &masked_prod, masked_cons,
181 			      XEN_9PFS_RING_SIZE(ring));
182 
183 	p9_req->status = REQ_STATUS_SENT;
184 	virt_wmb();			/* write ring before updating pointer */
185 	prod += size;
186 	ring->intf->out_prod = prod;
187 	spin_unlock_irqrestore(&ring->lock, flags);
188 	notify_remote_via_irq(ring->irq);
189 	p9_req_put(p9_req);
190 
191 	return 0;
192 }
193 
p9_xen_response(struct work_struct * work)194 static void p9_xen_response(struct work_struct *work)
195 {
196 	struct xen_9pfs_front_priv *priv;
197 	struct xen_9pfs_dataring *ring;
198 	RING_IDX cons, prod, masked_cons, masked_prod;
199 	struct xen_9pfs_header h;
200 	struct p9_req_t *req;
201 	int status;
202 
203 	ring = container_of(work, struct xen_9pfs_dataring, work);
204 	priv = ring->priv;
205 
206 	while (1) {
207 		cons = ring->intf->in_cons;
208 		prod = ring->intf->in_prod;
209 		virt_rmb();
210 
211 		if (xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) <
212 		    sizeof(h)) {
213 			notify_remote_via_irq(ring->irq);
214 			return;
215 		}
216 
217 		masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
218 		masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
219 
220 		/* First, read just the header */
221 		xen_9pfs_read_packet(&h, ring->data.in, sizeof(h),
222 				     masked_prod, &masked_cons,
223 				     XEN_9PFS_RING_SIZE(ring));
224 
225 		req = p9_tag_lookup(priv->client, h.tag);
226 		if (!req || req->status != REQ_STATUS_SENT) {
227 			dev_warn(&priv->dev->dev, "Wrong req tag=%x\n", h.tag);
228 			cons += h.size;
229 			virt_mb();
230 			ring->intf->in_cons = cons;
231 			continue;
232 		}
233 
234 		if (h.size > req->rc.capacity) {
235 			dev_warn(&priv->dev->dev,
236 				 "requested packet size too big: %d for tag %d with capacity %zd\n",
237 				 h.size, h.tag, req->rc.capacity);
238 			req->status = REQ_STATUS_ERROR;
239 			goto recv_error;
240 		}
241 
242 		memcpy(&req->rc, &h, sizeof(h));
243 		req->rc.offset = 0;
244 
245 		masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
246 		/* Then, read the whole packet (including the header) */
247 		xen_9pfs_read_packet(req->rc.sdata, ring->data.in, h.size,
248 				     masked_prod, &masked_cons,
249 				     XEN_9PFS_RING_SIZE(ring));
250 
251 recv_error:
252 		virt_mb();
253 		cons += h.size;
254 		ring->intf->in_cons = cons;
255 
256 		status = (req->status != REQ_STATUS_ERROR) ?
257 			REQ_STATUS_RCVD : REQ_STATUS_ERROR;
258 
259 		p9_client_cb(priv->client, req, status);
260 	}
261 }
262 
xen_9pfs_front_event_handler(int irq,void * r)263 static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r)
264 {
265 	struct xen_9pfs_dataring *ring = r;
266 
267 	if (!ring || !ring->priv->client) {
268 		/* ignore spurious interrupt */
269 		return IRQ_HANDLED;
270 	}
271 
272 	wake_up_interruptible(&ring->wq);
273 	schedule_work(&ring->work);
274 
275 	return IRQ_HANDLED;
276 }
277 
278 static struct p9_trans_module p9_xen_trans = {
279 	.name = "xen",
280 	.maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2),
281 	.def = 1,
282 	.create = p9_xen_create,
283 	.close = p9_xen_close,
284 	.request = p9_xen_request,
285 	.cancel = p9_xen_cancel,
286 	.owner = THIS_MODULE,
287 };
288 
289 static const struct xenbus_device_id xen_9pfs_front_ids[] = {
290 	{ "9pfs" },
291 	{ "" }
292 };
293 
xen_9pfs_front_free(struct xen_9pfs_front_priv * priv)294 static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv)
295 {
296 	int i, j;
297 
298 	write_lock(&xen_9pfs_lock);
299 	list_del(&priv->list);
300 	write_unlock(&xen_9pfs_lock);
301 
302 	for (i = 0; i < priv->num_rings; i++) {
303 		struct xen_9pfs_dataring *ring = &priv->rings[i];
304 
305 		cancel_work_sync(&ring->work);
306 
307 		if (!priv->rings[i].intf)
308 			break;
309 		if (priv->rings[i].irq > 0)
310 			unbind_from_irqhandler(priv->rings[i].irq, priv->dev);
311 		if (priv->rings[i].data.in) {
312 			for (j = 0;
313 			     j < (1 << priv->rings[i].intf->ring_order);
314 			     j++) {
315 				grant_ref_t ref;
316 
317 				ref = priv->rings[i].intf->ref[j];
318 				gnttab_end_foreign_access(ref, 0, 0);
319 			}
320 			free_pages_exact(priv->rings[i].data.in,
321 				   1UL << (priv->rings[i].intf->ring_order +
322 					   XEN_PAGE_SHIFT));
323 		}
324 		gnttab_end_foreign_access(priv->rings[i].ref, 0, 0);
325 		free_page((unsigned long)priv->rings[i].intf);
326 	}
327 	kfree(priv->rings);
328 	kfree(priv->tag);
329 	kfree(priv);
330 }
331 
xen_9pfs_front_remove(struct xenbus_device * dev)332 static int xen_9pfs_front_remove(struct xenbus_device *dev)
333 {
334 	struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
335 
336 	dev_set_drvdata(&dev->dev, NULL);
337 	xen_9pfs_front_free(priv);
338 	return 0;
339 }
340 
xen_9pfs_front_alloc_dataring(struct xenbus_device * dev,struct xen_9pfs_dataring * ring,unsigned int order)341 static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
342 					 struct xen_9pfs_dataring *ring,
343 					 unsigned int order)
344 {
345 	int i = 0;
346 	int ret = -ENOMEM;
347 	void *bytes = NULL;
348 
349 	init_waitqueue_head(&ring->wq);
350 	spin_lock_init(&ring->lock);
351 	INIT_WORK(&ring->work, p9_xen_response);
352 
353 	ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL);
354 	if (!ring->intf)
355 		return ret;
356 	ret = gnttab_grant_foreign_access(dev->otherend_id,
357 					  virt_to_gfn(ring->intf), 0);
358 	if (ret < 0)
359 		goto out;
360 	ring->ref = ret;
361 	bytes = alloc_pages_exact(1UL << (order + XEN_PAGE_SHIFT),
362 				  GFP_KERNEL | __GFP_ZERO);
363 	if (!bytes) {
364 		ret = -ENOMEM;
365 		goto out;
366 	}
367 	for (; i < (1 << order); i++) {
368 		ret = gnttab_grant_foreign_access(
369 				dev->otherend_id, virt_to_gfn(bytes) + i, 0);
370 		if (ret < 0)
371 			goto out;
372 		ring->intf->ref[i] = ret;
373 	}
374 	ring->intf->ring_order = order;
375 	ring->data.in = bytes;
376 	ring->data.out = bytes + XEN_FLEX_RING_SIZE(order);
377 
378 	ret = xenbus_alloc_evtchn(dev, &ring->evtchn);
379 	if (ret)
380 		goto out;
381 	ring->irq = bind_evtchn_to_irqhandler(ring->evtchn,
382 					      xen_9pfs_front_event_handler,
383 					      0, "xen_9pfs-frontend", ring);
384 	if (ring->irq >= 0)
385 		return 0;
386 
387 	xenbus_free_evtchn(dev, ring->evtchn);
388 	ret = ring->irq;
389 out:
390 	if (bytes) {
391 		for (i--; i >= 0; i--)
392 			gnttab_end_foreign_access(ring->intf->ref[i], 0, 0);
393 		free_pages_exact(bytes, 1UL << (order + XEN_PAGE_SHIFT));
394 	}
395 	gnttab_end_foreign_access(ring->ref, 0, 0);
396 	free_page((unsigned long)ring->intf);
397 	return ret;
398 }
399 
xen_9pfs_front_probe(struct xenbus_device * dev,const struct xenbus_device_id * id)400 static int xen_9pfs_front_probe(struct xenbus_device *dev,
401 				const struct xenbus_device_id *id)
402 {
403 	int ret, i;
404 	struct xenbus_transaction xbt;
405 	struct xen_9pfs_front_priv *priv = NULL;
406 	char *versions;
407 	unsigned int max_rings, max_ring_order, len = 0;
408 
409 	versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
410 	if (IS_ERR(versions))
411 		return PTR_ERR(versions);
412 	if (strcmp(versions, "1")) {
413 		kfree(versions);
414 		return -EINVAL;
415 	}
416 	kfree(versions);
417 	max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0);
418 	if (max_rings < XEN_9PFS_NUM_RINGS)
419 		return -EINVAL;
420 	max_ring_order = xenbus_read_unsigned(dev->otherend,
421 					      "max-ring-page-order", 0);
422 	if (max_ring_order > XEN_9PFS_RING_ORDER)
423 		max_ring_order = XEN_9PFS_RING_ORDER;
424 	if (p9_xen_trans.maxsize > XEN_FLEX_RING_SIZE(max_ring_order))
425 		p9_xen_trans.maxsize = XEN_FLEX_RING_SIZE(max_ring_order) / 2;
426 
427 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
428 	if (!priv)
429 		return -ENOMEM;
430 
431 	priv->dev = dev;
432 	priv->num_rings = XEN_9PFS_NUM_RINGS;
433 	priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings),
434 			      GFP_KERNEL);
435 	if (!priv->rings) {
436 		kfree(priv);
437 		return -ENOMEM;
438 	}
439 
440 	for (i = 0; i < priv->num_rings; i++) {
441 		priv->rings[i].priv = priv;
442 		ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i],
443 						    max_ring_order);
444 		if (ret < 0)
445 			goto error;
446 	}
447 
448  again:
449 	ret = xenbus_transaction_start(&xbt);
450 	if (ret) {
451 		xenbus_dev_fatal(dev, ret, "starting transaction");
452 		goto error;
453 	}
454 	ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
455 	if (ret)
456 		goto error_xenbus;
457 	ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u",
458 			    priv->num_rings);
459 	if (ret)
460 		goto error_xenbus;
461 	for (i = 0; i < priv->num_rings; i++) {
462 		char str[16];
463 
464 		BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
465 		sprintf(str, "ring-ref%d", i);
466 		ret = xenbus_printf(xbt, dev->nodename, str, "%d",
467 				    priv->rings[i].ref);
468 		if (ret)
469 			goto error_xenbus;
470 
471 		sprintf(str, "event-channel-%d", i);
472 		ret = xenbus_printf(xbt, dev->nodename, str, "%u",
473 				    priv->rings[i].evtchn);
474 		if (ret)
475 			goto error_xenbus;
476 	}
477 	priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL);
478 	if (IS_ERR(priv->tag)) {
479 		ret = PTR_ERR(priv->tag);
480 		goto error_xenbus;
481 	}
482 	ret = xenbus_transaction_end(xbt, 0);
483 	if (ret) {
484 		if (ret == -EAGAIN)
485 			goto again;
486 		xenbus_dev_fatal(dev, ret, "completing transaction");
487 		goto error;
488 	}
489 
490 	write_lock(&xen_9pfs_lock);
491 	list_add_tail(&priv->list, &xen_9pfs_devs);
492 	write_unlock(&xen_9pfs_lock);
493 	dev_set_drvdata(&dev->dev, priv);
494 	xenbus_switch_state(dev, XenbusStateInitialised);
495 
496 	return 0;
497 
498  error_xenbus:
499 	xenbus_transaction_end(xbt, 1);
500 	xenbus_dev_fatal(dev, ret, "writing xenstore");
501  error:
502 	dev_set_drvdata(&dev->dev, NULL);
503 	xen_9pfs_front_free(priv);
504 	return ret;
505 }
506 
xen_9pfs_front_resume(struct xenbus_device * dev)507 static int xen_9pfs_front_resume(struct xenbus_device *dev)
508 {
509 	dev_warn(&dev->dev, "suspend/resume unsupported\n");
510 	return 0;
511 }
512 
xen_9pfs_front_changed(struct xenbus_device * dev,enum xenbus_state backend_state)513 static void xen_9pfs_front_changed(struct xenbus_device *dev,
514 				   enum xenbus_state backend_state)
515 {
516 	switch (backend_state) {
517 	case XenbusStateReconfiguring:
518 	case XenbusStateReconfigured:
519 	case XenbusStateInitialising:
520 	case XenbusStateInitialised:
521 	case XenbusStateUnknown:
522 		break;
523 
524 	case XenbusStateInitWait:
525 		break;
526 
527 	case XenbusStateConnected:
528 		xenbus_switch_state(dev, XenbusStateConnected);
529 		break;
530 
531 	case XenbusStateClosed:
532 		if (dev->state == XenbusStateClosed)
533 			break;
534 		fallthrough;	/* Missed the backend's CLOSING state */
535 	case XenbusStateClosing:
536 		xenbus_frontend_closed(dev);
537 		break;
538 	}
539 }
540 
541 static struct xenbus_driver xen_9pfs_front_driver = {
542 	.ids = xen_9pfs_front_ids,
543 	.probe = xen_9pfs_front_probe,
544 	.remove = xen_9pfs_front_remove,
545 	.resume = xen_9pfs_front_resume,
546 	.otherend_changed = xen_9pfs_front_changed,
547 };
548 
p9_trans_xen_init(void)549 static int p9_trans_xen_init(void)
550 {
551 	int rc;
552 
553 	if (!xen_domain())
554 		return -ENODEV;
555 
556 	pr_info("Initialising Xen transport for 9pfs\n");
557 
558 	v9fs_register_trans(&p9_xen_trans);
559 	rc = xenbus_register_frontend(&xen_9pfs_front_driver);
560 	if (rc)
561 		v9fs_unregister_trans(&p9_xen_trans);
562 
563 	return rc;
564 }
565 module_init(p9_trans_xen_init);
566 
p9_trans_xen_exit(void)567 static void p9_trans_xen_exit(void)
568 {
569 	v9fs_unregister_trans(&p9_xen_trans);
570 	return xenbus_unregister_driver(&xen_9pfs_front_driver);
571 }
572 module_exit(p9_trans_xen_exit);
573 
574 MODULE_AUTHOR("Stefano Stabellini <stefano@aporeto.com>");
575 MODULE_DESCRIPTION("Xen Transport for 9P");
576 MODULE_LICENSE("GPL");
577