blob: b24a4fb0f0a23af710c8d29eea2664eab2e68cdd [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05002/*
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -05003 * The Virtio 9p transport driver
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05004 *
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06005 * This is a block based transport driver based on the lguest block driver
6 * code.
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05007 *
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -05008 * Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -05009 *
10 * Based on virtio console driver
11 * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050012 */
13
Joe Perches5d385152011-11-28 10:40:46 -080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050016#include <linux/in.h>
17#include <linux/module.h>
18#include <linux/net.h>
19#include <linux/ipv6.h>
20#include <linux/errno.h>
21#include <linux/kernel.h>
22#include <linux/un.h>
23#include <linux/uaccess.h>
24#include <linux/inet.h>
25#include <linux/idr.h>
26#include <linux/file.h>
Will Deaconb9cdc882012-10-19 14:03:32 +010027#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050029#include <net/9p/9p.h>
30#include <linux/parser.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050031#include <net/9p/client.h>
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050032#include <net/9p/transport.h>
33#include <linux/scatterlist.h>
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -070034#include <linux/swap.h>
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050035#include <linux/virtio.h>
36#include <linux/virtio_9p.h>
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -080037#include "trans_common.h"
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050038
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060039#define VIRTQUEUE_NUM 128
40
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050041/* a single mutex to manage channel initialization and attachment */
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -060042static DEFINE_MUTEX(virtio_9p_lock);
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -070043static DECLARE_WAIT_QUEUE_HEAD(vp_wq);
44static atomic_t vp_pinned = ATOMIC_INIT(0);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050045
Eric Van Hensbergenee443992008-03-05 07:08:09 -060046/**
47 * struct virtio_chan - per-instance transport information
Eric Van Hensbergenee443992008-03-05 07:08:09 -060048 * @inuse: whether the channel is in use
49 * @lock: protects multiple elements within this structure
Abhishek Kulkarni0e155972009-07-19 13:41:55 -060050 * @client: client instance
Eric Van Hensbergenee443992008-03-05 07:08:09 -060051 * @vdev: virtio dev associated with this channel
52 * @vq: virtio queue associated with this channel
Andrew Lunn760b3d62020-10-31 19:26:55 +010053 * @ring_bufs_avail: flag to indicate there is some available in the ring buf
54 * @vc_wq: wait queue for waiting for thing to be added to ring buf
55 * @p9_max_pages: maximum number of pinned pages
Eric Van Hensbergenee443992008-03-05 07:08:09 -060056 * @sg: scatter gather list which is used to pack a request (protected?)
Andrew Lunn760b3d62020-10-31 19:26:55 +010057 * @chan_list: linked list of channels
Eric Van Hensbergenee443992008-03-05 07:08:09 -060058 *
59 * We keep all per-channel information in a structure.
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050060 * This structure is allocated within the devices dev->mem space.
61 * A pointer to the structure will get put in the transport private.
Eric Van Hensbergenee443992008-03-05 07:08:09 -060062 *
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050063 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -060064
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +000065struct virtio_chan {
Eric Van Hensbergenee443992008-03-05 07:08:09 -060066 bool inuse;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050067
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060068 spinlock_t lock;
69
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -050070 struct p9_client *client;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050071 struct virtio_device *vdev;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060072 struct virtqueue *vq;
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -070073 int ring_bufs_avail;
74 wait_queue_head_t *vc_wq;
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -070075 /* This is global limit. Since we don't have a global structure,
76 * will be placing it in each channel.
77 */
Zhang Yanfei7293bfb2013-02-22 16:35:49 -080078 unsigned long p9_max_pages;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -060079 /* Scatterlist: can be too big for stack. */
80 struct scatterlist sg[VIRTQUEUE_NUM];
Andrew Lunn760b3d62020-10-31 19:26:55 +010081 /**
82 * @tag: name to identify a mount null terminated
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +000083 */
84 char *tag;
85
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +000086 struct list_head chan_list;
87};
88
89static struct list_head virtio_chan_list;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050090
91/* How many bytes left in this page. */
92static unsigned int rest_of_page(void *data)
93{
Al Viro222e4ad2016-01-02 13:31:21 -050094 return PAGE_SIZE - offset_in_page(data);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -050095}
96
Eric Van Hensbergenee443992008-03-05 07:08:09 -060097/**
98 * p9_virtio_close - reclaim resources of a channel
Abhishek Kulkarni0e155972009-07-19 13:41:55 -060099 * @client: client instance
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600100 *
101 * This reclaims a channel by freeing its resources and
Zheng Yongjun8ab17842021-06-02 14:54:42 +0800102 * resetting its inuse flag.
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600103 *
104 */
105
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500106static void p9_virtio_close(struct p9_client *client)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500107{
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500108 struct virtio_chan *chan = client->trans;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500109
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600110 mutex_lock(&virtio_9p_lock);
Aneesh Kumar K.Vfb786102010-02-08 11:50:32 +0000111 if (chan)
112 chan->inuse = false;
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600113 mutex_unlock(&virtio_9p_lock);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500114}
115
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600116/**
117 * req_done - callback which signals activity from the server
118 * @vq: virtio queue activity was received on
119 *
120 * This notifies us that the server has triggered some activity
121 * on the virtio channel - most likely a response to request we
122 * sent. Figure out which requests now have responses and wake up
123 * those threads.
124 *
125 * Bugs: could do with some additional sanity checking, but appears to work.
126 *
127 */
128
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600129static void req_done(struct virtqueue *vq)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500130{
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600131 struct virtio_chan *chan = vq->vdev->priv;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600132 unsigned int len;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600133 struct p9_req_t *req;
jiangyiwen31934da2018-07-19 15:17:00 +0800134 bool need_wakeup = false;
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700135 unsigned long flags;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500136
Joe Perches5d385152011-11-28 10:40:46 -0800137 p9_debug(P9_DEBUG_TRANS, ": request done\n");
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500138
jiangyiwen31934da2018-07-19 15:17:00 +0800139 spin_lock_irqsave(&chan->lock, flags);
140 while ((req = virtqueue_get_buf(chan->vq, &len)) != NULL) {
141 if (!chan->ring_bufs_avail) {
142 chan->ring_bufs_avail = 1;
143 need_wakeup = true;
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700144 }
jiangyiwen31934da2018-07-19 15:17:00 +0800145
Tomas Bortolif9845792018-07-23 17:44:04 +0200146 if (len) {
Dominique Martinet523adb62018-07-30 05:55:19 +0000147 req->rc.size = len;
Greg Kurz26d99832018-01-22 22:02:05 +0100148 p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
Tomas Bortolif9845792018-07-23 17:44:04 +0200149 }
Venkateswararao Jujjuri (JV)a01a9842011-03-14 14:12:49 -0700150 }
jiangyiwen31934da2018-07-19 15:17:00 +0800151 spin_unlock_irqrestore(&chan->lock, flags);
152 /* Wakeup if anyone waiting for VirtIO ring space. */
153 if (need_wakeup)
154 wake_up(chan->vc_wq);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500155}
156
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600157/**
158 * pack_sg_list - pack a scatter gather list from a linear buffer
159 * @sg: scatter/gather list to pack into
160 * @start: which segment of the sg_list to start at
161 * @limit: maximum segment to pack data to
162 * @data: data to pack into scatter/gather list
163 * @count: amount of data to pack into the scatter/gather list
164 *
165 * sg_lists have multiple segments of various sizes. This will pack
166 * arbitrary data into an existing scatter gather list, segmenting the
167 * data as necessary within constraints.
168 *
169 */
170
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530171static int pack_sg_list(struct scatterlist *sg, int start,
172 int limit, char *data, int count)
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600173{
174 int s;
175 int index = start;
176
177 while (count) {
178 s = rest_of_page(data);
179 if (s > count)
180 s = count;
jiangyiwen23cba9c2018-08-03 12:11:34 +0800181 BUG_ON(index >= limit);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030182 /* Make sure we don't terminate early. */
183 sg_unmark_end(&sg[index]);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600184 sg_set_buf(&sg[index++], data, s);
185 count -= s;
186 data += s;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600187 }
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030188 if (index-start)
189 sg_mark_end(&sg[index - 1]);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600190 return index-start;
191}
192
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500193/* We don't currently allow canceling of virtio requests */
194static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
195{
196 return 1;
197}
198
Tomas Bortoli728356d2018-08-14 19:43:42 +0200199/* Reply won't come, so drop req ref */
200static int p9_virtio_cancelled(struct p9_client *client, struct p9_req_t *req)
201{
202 p9_req_put(req);
203 return 0;
204}
205
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600206/**
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800207 * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
208 * this takes a list of pages.
209 * @sg: scatter/gather list to pack into
210 * @start: which segment of the sg_list to start at
Andrew Lunn760b3d62020-10-31 19:26:55 +0100211 * @limit: maximum number of pages in sg list.
Ben Hutchings2c530402012-07-10 10:55:09 +0000212 * @pdata: a list of pages to add into sg.
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530213 * @nr_pages: number of pages to pack into the scatter/gather list
Al Viro4f3b35c2015-04-01 19:57:53 -0400214 * @offs: amount of data in the beginning of first page _not_ to pack
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800215 * @count: amount of data to pack into the scatter/gather list
216 */
217static int
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530218pack_sg_list_p(struct scatterlist *sg, int start, int limit,
Al Viro4f3b35c2015-04-01 19:57:53 -0400219 struct page **pdata, int nr_pages, size_t offs, int count)
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800220{
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530221 int i = 0, s;
Al Viro4f3b35c2015-04-01 19:57:53 -0400222 int data_off = offs;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800223 int index = start;
224
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530225 BUG_ON(nr_pages > (limit - start));
226 /*
227 * if the first page doesn't start at
228 * page boundary find the offset
229 */
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530230 while (nr_pages) {
Al Viro4f3b35c2015-04-01 19:57:53 -0400231 s = PAGE_SIZE - data_off;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530232 if (s > count)
233 s = count;
jiangyiwen23cba9c2018-08-03 12:11:34 +0800234 BUG_ON(index >= limit);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030235 /* Make sure we don't terminate early. */
236 sg_unmark_end(&sg[index]);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530237 sg_set_page(&sg[index++], pdata[i++], s, data_off);
238 data_off = 0;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800239 count -= s;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530240 nr_pages--;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800241 }
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030242
243 if (index-start)
244 sg_mark_end(&sg[index - 1]);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530245 return index - start;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800246}
247
248/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500249 * p9_virtio_request - issue a request
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600250 * @client: client instance issuing the request
251 * @req: request to be issued
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600252 *
253 */
254
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600255static int
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500256p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600257{
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530258 int err;
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030259 int in, out, out_sgs, in_sgs;
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700260 unsigned long flags;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530261 struct virtio_chan *chan = client->trans;
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030262 struct scatterlist *sgs[2];
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600263
Joe Perches5d385152011-11-28 10:40:46 -0800264 p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600265
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700266 req->status = REQ_STATUS_SENT;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530267req_retry:
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700268 spin_lock_irqsave(&chan->lock, flags);
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800269
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030270 out_sgs = in_sgs = 0;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800271 /* Handle out VirtIO ring buffers */
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530272 out = pack_sg_list(chan->sg, 0,
Dominique Martinet523adb62018-07-30 05:55:19 +0000273 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030274 if (out)
275 sgs[out_sgs++] = chan->sg;
Venkateswararao Jujjuri (JV)40388662011-01-28 15:22:36 -0800276
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530277 in = pack_sg_list(chan->sg, out,
Dominique Martinet523adb62018-07-30 05:55:19 +0000278 VIRTQUEUE_NUM, req->rc.sdata, req->rc.capacity);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030279 if (in)
280 sgs[out_sgs + in_sgs++] = chan->sg + out;
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600281
Al Viro474fe9f2015-07-12 21:06:44 -0400282 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
Rusty Russellf96fde42012-01-12 15:44:42 +1030283 GFP_ATOMIC);
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700284 if (err < 0) {
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700285 if (err == -ENOSPC) {
286 chan->ring_bufs_avail = 0;
287 spin_unlock_irqrestore(&chan->lock, flags);
Tuomas Tynkkynen9523fea2017-09-06 17:59:08 +0300288 err = wait_event_killable(*chan->vc_wq,
289 chan->ring_bufs_avail);
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700290 if (err == -ERESTARTSYS)
291 return err;
292
Joe Perches5d385152011-11-28 10:40:46 -0800293 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530294 goto req_retry;
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700295 } else {
296 spin_unlock_irqrestore(&chan->lock, flags);
Joe Perches5d385152011-11-28 10:40:46 -0800297 p9_debug(P9_DEBUG_TRANS,
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030298 "virtio rpc add_sgs returned failure\n");
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700299 return -EIO;
300 }
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600301 }
Michael S. Tsirkindc3f5e62010-04-13 16:11:50 +0300302 virtqueue_kick(chan->vq);
Venkateswararao Jujjuri (JV)419b3952010-09-29 18:06:54 -0700303 spin_unlock_irqrestore(&chan->lock, flags);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600304
Joe Perches5d385152011-11-28 10:40:46 -0800305 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600306 return 0;
307}
308
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530309static int p9_get_mapped_pages(struct virtio_chan *chan,
Al Viro4f3b35c2015-04-01 19:57:53 -0400310 struct page ***pages,
311 struct iov_iter *data,
312 int count,
313 size_t *offs,
314 int *need_drop)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530315{
Al Viro4f3b35c2015-04-01 19:57:53 -0400316 int nr_pages;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530317 int err;
Al Viro4f3b35c2015-04-01 19:57:53 -0400318
319 if (!iov_iter_count(data))
320 return 0;
321
Marc Zyngier2cbfdf42018-11-02 17:16:51 +0000322 if (!iov_iter_is_kvec(data)) {
Al Viro4f3b35c2015-04-01 19:57:53 -0400323 int n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530324 /*
325 * We allow only p9_max_pages pinned. We wait for the
326 * Other zc request to finish here
327 */
328 if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
Tuomas Tynkkynen9523fea2017-09-06 17:59:08 +0300329 err = wait_event_killable(vp_wq,
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530330 (atomic_read(&vp_pinned) < chan->p9_max_pages));
331 if (err == -ERESTARTSYS)
332 return err;
333 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400334 n = iov_iter_get_pages_alloc(data, pages, count, offs);
335 if (n < 0)
336 return n;
337 *need_drop = 1;
338 nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530339 atomic_add(nr_pages, &vp_pinned);
Al Viro4f3b35c2015-04-01 19:57:53 -0400340 return n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530341 } else {
342 /* kernel buffer, no need to pin pages */
Al Viro4f3b35c2015-04-01 19:57:53 -0400343 int index;
344 size_t len;
345 void *p;
346
347 /* we'd already checked that it's non-empty */
348 while (1) {
349 len = iov_iter_single_seg_count(data);
350 if (likely(len)) {
351 p = data->kvec->iov_base + data->iov_offset;
352 break;
353 }
354 iov_iter_advance(data, 0);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530355 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400356 if (len > count)
357 len = count;
358
359 nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
360 (unsigned long)p / PAGE_SIZE;
361
Kees Cook6da2ec52018-06-12 13:55:00 -0700362 *pages = kmalloc_array(nr_pages, sizeof(struct page *),
363 GFP_NOFS);
Al Viro4f3b35c2015-04-01 19:57:53 -0400364 if (!*pages)
365 return -ENOMEM;
366
367 *need_drop = 0;
Al Viro222e4ad2016-01-02 13:31:21 -0500368 p -= (*offs = offset_in_page(p));
Al Viro4f3b35c2015-04-01 19:57:53 -0400369 for (index = 0; index < nr_pages; index++) {
370 if (is_vmalloc_addr(p))
371 (*pages)[index] = vmalloc_to_page(p);
372 else
373 (*pages)[index] = kmap_to_page(p);
374 p += PAGE_SIZE;
375 }
376 return len;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530377 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530378}
379
380/**
381 * p9_virtio_zc_request - issue a zero copy request
382 * @client: client instance issuing the request
383 * @req: request to be issued
piaojunc7ebbae2018-07-18 11:45:29 +0800384 * @uidata: user buffer that should be used for zero copy read
385 * @uodata: user buffer that should be used for zero copy write
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530386 * @inlen: read buffer size
Sun Lianwen4a026da2018-05-08 09:49:38 +0800387 * @outlen: write buffer size
388 * @in_hdr_len: reader header size, This is the size of response protocol data
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530389 *
390 */
391static int
392p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
Al Viro4f3b35c2015-04-01 19:57:53 -0400393 struct iov_iter *uidata, struct iov_iter *uodata,
394 int inlen, int outlen, int in_hdr_len)
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530395{
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030396 int in, out, err, out_sgs, in_sgs;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530397 unsigned long flags;
398 int in_nr_pages = 0, out_nr_pages = 0;
399 struct page **in_pages = NULL, **out_pages = NULL;
400 struct virtio_chan *chan = client->trans;
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030401 struct scatterlist *sgs[4];
Al Viro4f3b35c2015-04-01 19:57:53 -0400402 size_t offs;
403 int need_drop = 0;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200404 int kicked = 0;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530405
Joe Perches5d385152011-11-28 10:40:46 -0800406 p9_debug(P9_DEBUG_TRANS, "virtio request\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530407
408 if (uodata) {
Chirantan Ekboted28c7562018-07-16 17:35:29 -0700409 __le32 sz;
Al Viro4f3b35c2015-04-01 19:57:53 -0400410 int n = p9_get_mapped_pages(chan, &out_pages, uodata,
411 outlen, &offs, &need_drop);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200412 if (n < 0) {
413 err = n;
414 goto err_out;
415 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400416 out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
417 if (n != outlen) {
418 __le32 v = cpu_to_le32(n);
Dominique Martinet523adb62018-07-30 05:55:19 +0000419 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
Al Viro4f3b35c2015-04-01 19:57:53 -0400420 outlen = n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530421 }
Chirantan Ekboted28c7562018-07-16 17:35:29 -0700422 /* The size field of the message must include the length of the
423 * header and the length of the data. We didn't actually know
424 * the length of the data until this point so add it in now.
425 */
Dominique Martinet523adb62018-07-30 05:55:19 +0000426 sz = cpu_to_le32(req->tc.size + outlen);
427 memcpy(&req->tc.sdata[0], &sz, sizeof(sz));
Al Viro4f3b35c2015-04-01 19:57:53 -0400428 } else if (uidata) {
429 int n = p9_get_mapped_pages(chan, &in_pages, uidata,
430 inlen, &offs, &need_drop);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200431 if (n < 0) {
432 err = n;
433 goto err_out;
434 }
Al Viro4f3b35c2015-04-01 19:57:53 -0400435 in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
436 if (n != inlen) {
437 __le32 v = cpu_to_le32(n);
Dominique Martinet523adb62018-07-30 05:55:19 +0000438 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
Al Viro4f3b35c2015-04-01 19:57:53 -0400439 inlen = n;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530440 }
441 }
442 req->status = REQ_STATUS_SENT;
443req_retry_pinned:
444 spin_lock_irqsave(&chan->lock, flags);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030445
446 out_sgs = in_sgs = 0;
447
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530448 /* out data */
449 out = pack_sg_list(chan->sg, 0,
Dominique Martinet523adb62018-07-30 05:55:19 +0000450 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530451
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030452 if (out)
453 sgs[out_sgs++] = chan->sg;
454
455 if (out_pages) {
456 sgs[out_sgs++] = chan->sg + out;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530457 out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
Al Viro4f3b35c2015-04-01 19:57:53 -0400458 out_pages, out_nr_pages, offs, outlen);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030459 }
Stephen Hemmingerc69f297d2018-07-24 12:29:10 -0700460
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530461 /*
462 * Take care of in data
463 * For example TREAD have 11.
464 * 11 is the read/write header = PDU Header(7) + IO Size (4).
465 * Arrange in such a way that server places header in the
Zheng Yongjun8ab17842021-06-02 14:54:42 +0800466 * allocated memory and payload onto the user buffer.
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530467 */
468 in = pack_sg_list(chan->sg, out,
Dominique Martinet523adb62018-07-30 05:55:19 +0000469 VIRTQUEUE_NUM, req->rc.sdata, in_hdr_len);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030470 if (in)
471 sgs[out_sgs + in_sgs++] = chan->sg + out;
472
473 if (in_pages) {
474 sgs[out_sgs + in_sgs++] = chan->sg + out + in;
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530475 in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
Al Viro4f3b35c2015-04-01 19:57:53 -0400476 in_pages, in_nr_pages, offs, inlen);
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030477 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530478
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030479 BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
Al Viro474fe9f2015-07-12 21:06:44 -0400480 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
Rusty Russellf96fde42012-01-12 15:44:42 +1030481 GFP_ATOMIC);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530482 if (err < 0) {
483 if (err == -ENOSPC) {
484 chan->ring_bufs_avail = 0;
485 spin_unlock_irqrestore(&chan->lock, flags);
Tuomas Tynkkynen9523fea2017-09-06 17:59:08 +0300486 err = wait_event_killable(*chan->vc_wq,
487 chan->ring_bufs_avail);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530488 if (err == -ERESTARTSYS)
489 goto err_out;
490
Joe Perches5d385152011-11-28 10:40:46 -0800491 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530492 goto req_retry_pinned;
493 } else {
494 spin_unlock_irqrestore(&chan->lock, flags);
Joe Perches5d385152011-11-28 10:40:46 -0800495 p9_debug(P9_DEBUG_TRANS,
Rusty Russell0b36f1a2013-03-20 15:44:30 +1030496 "virtio rpc add_sgs returned failure\n");
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530497 err = -EIO;
498 goto err_out;
499 }
500 }
501 virtqueue_kick(chan->vq);
502 spin_unlock_irqrestore(&chan->lock, flags);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200503 kicked = 1;
Joe Perches5d385152011-11-28 10:40:46 -0800504 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
Matthew Wilcox2557d0c2018-07-11 14:02:23 -0700505 err = wait_event_killable(req->wq, req->status >= REQ_STATUS_RCVD);
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530506 /*
507 * Non kernel buffers are pinned, unpin them
508 */
509err_out:
Al Viro4f3b35c2015-04-01 19:57:53 -0400510 if (need_drop) {
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530511 if (in_pages) {
512 p9_release_pages(in_pages, in_nr_pages);
513 atomic_sub(in_nr_pages, &vp_pinned);
514 }
515 if (out_pages) {
516 p9_release_pages(out_pages, out_nr_pages);
517 atomic_sub(out_nr_pages, &vp_pinned);
518 }
519 /* wakeup anybody waiting for slots to pin pages */
520 wake_up(&vp_wq);
521 }
Vegard Nossum1b8553c2016-08-03 19:59:47 +0300522 kvfree(in_pages);
523 kvfree(out_pages);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200524 if (!kicked) {
525 /* reply won't come */
526 p9_req_put(req);
527 }
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +0530528 return err;
529}
530
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000531static ssize_t p9_mount_tag_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
534 struct virtio_chan *chan;
535 struct virtio_device *vdev;
piaojunedcd9d92018-08-03 17:22:20 +0800536 int tag_len;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000537
538 vdev = dev_to_virtio(dev);
539 chan = vdev->priv;
piaojunedcd9d92018-08-03 17:22:20 +0800540 tag_len = strlen(chan->tag);
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000541
piaojunedcd9d92018-08-03 17:22:20 +0800542 memcpy(buf, chan->tag, tag_len + 1);
Andrey Ryabinin179a5bc2015-01-27 16:00:19 +0300543
piaojunedcd9d92018-08-03 17:22:20 +0800544 return tag_len + 1;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000545}
546
547static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
548
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600549/**
550 * p9_virtio_probe - probe for existence of 9P virtio channels
551 * @vdev: virtio device to probe
552 *
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000553 * This probes for existing virtio channels.
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600554 *
555 */
556
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600557static int p9_virtio_probe(struct virtio_device *vdev)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500558{
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000559 __u16 tag_len;
560 char *tag;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500561 int err;
562 struct virtio_chan *chan;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500563
Michael S. Tsirkin7754f532015-01-12 16:23:37 +0200564 if (!vdev->config->get) {
565 dev_err(&vdev->dev, "%s failure: config access disabled\n",
566 __func__);
567 return -EINVAL;
568 }
569
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000570 chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
571 if (!chan) {
Joe Perches5d385152011-11-28 10:40:46 -0800572 pr_err("Failed to allocate virtio 9P channel\n");
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500573 err = -ENOMEM;
574 goto fail;
575 }
576
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600577 chan->vdev = vdev;
578
579 /* We expect one virtqueue, for requests. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600580 chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600581 if (IS_ERR(chan->vq)) {
582 err = PTR_ERR(chan->vq);
Jean-Philippe Brucker92aef462018-07-17 19:14:45 -0700583 goto out_free_chan;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500584 }
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600585 chan->vq->vdev->priv = chan;
586 spin_lock_init(&chan->lock);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500587
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600588 sg_init_table(chan->sg, VIRTQUEUE_NUM);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500589
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500590 chan->inuse = false;
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000591 if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
Rusty Russell855e0c52013-10-14 18:11:51 +1030592 virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000593 } else {
594 err = -EINVAL;
595 goto out_free_vq;
596 }
piaojunedcd9d92018-08-03 17:22:20 +0800597 tag = kzalloc(tag_len + 1, GFP_KERNEL);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000598 if (!tag) {
599 err = -ENOMEM;
600 goto out_free_vq;
601 }
Rusty Russell855e0c52013-10-14 18:11:51 +1030602
603 virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
604 tag, tag_len);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000605 chan->tag = tag;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000606 err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
607 if (err) {
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700608 goto out_free_tag;
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000609 }
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700610 chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
611 if (!chan->vc_wq) {
612 err = -ENOMEM;
Xie Yongjif997ea32021-05-17 16:35:57 +0800613 goto out_remove_file;
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700614 }
615 init_waitqueue_head(chan->vc_wq);
616 chan->ring_bufs_avail = 1;
Venkateswararao Jujjuri (JV)68da9ba2011-03-18 15:49:48 -0700617 /* Ceiling limit to avoid denial of service attacks */
618 chan->p9_max_pages = nr_free_buffer_pages()/4;
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700619
Michael S. Tsirkin64b4cc32014-10-15 10:22:31 +1030620 virtio_device_ready(vdev);
621
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000622 mutex_lock(&virtio_9p_lock);
623 list_add_tail(&chan->chan_list, &virtio_chan_list);
624 mutex_unlock(&virtio_9p_lock);
Michael Marineaue0d6cb92013-08-11 00:53:45 -0400625
626 /* Let udev rules use the new mount_tag attribute. */
627 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
628
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500629 return 0;
630
Xie Yongjif997ea32021-05-17 16:35:57 +0800631out_remove_file:
632 sysfs_remove_file(&vdev->dev.kobj, &dev_attr_mount_tag.attr);
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700633out_free_tag:
634 kfree(tag);
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600635out_free_vq:
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600636 vdev->config->del_vqs(vdev);
Jean-Philippe Brucker92aef462018-07-17 19:14:45 -0700637out_free_chan:
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000638 kfree(chan);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500639fail:
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500640 return err;
641}
642
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600643
644/**
645 * p9_virtio_create - allocate a new virtio channel
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500646 * @client: client instance invoking this transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600647 * @devname: string identifying the channel to connect to (unused)
648 * @args: args passed from sys_mount() for per-transport options (unused)
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600649 *
650 * This sets up a transport channel for 9p communication. Right now
zhuxinranf27456692021-12-16 14:14:39 +0800651 * we only match the first available channel, but eventually we could look up
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500652 * alternate channels by matching devname versus a virtio_config entry.
653 * We use a simple reference count mechanism to ensure that only a single
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600654 * mount has a channel open at a time.
655 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600656 */
657
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500658static int
659p9_virtio_create(struct p9_client *client, const char *devname, char *args)
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500660{
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000661 struct virtio_chan *chan;
Aneesh Kumar K.Vc1a7c222010-02-15 17:27:02 +0000662 int ret = -ENOENT;
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000663 int found = 0;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500664
Tomas Bortoli10aa1452018-07-27 13:05:58 +0200665 if (devname == NULL)
666 return -EINVAL;
667
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600668 mutex_lock(&virtio_9p_lock);
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000669 list_for_each_entry(chan, &virtio_chan_list, chan_list) {
piaojunedcd9d92018-08-03 17:22:20 +0800670 if (!strcmp(devname, chan->tag)) {
Aneesh Kumar K.Vf75580c2010-02-15 17:27:00 +0000671 if (!chan->inuse) {
672 chan->inuse = true;
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000673 found = 1;
Aneesh Kumar K.Vf75580c2010-02-15 17:27:00 +0000674 break;
675 }
Aneesh Kumar K.Vc1a7c222010-02-15 17:27:02 +0000676 ret = -EBUSY;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500677 }
678 }
Josef 'Jeff' Sipekc1549492008-03-07 11:39:13 -0600679 mutex_unlock(&virtio_9p_lock);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500680
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000681 if (!found) {
Aneesh Kumar K.Vc7c72c52015-09-03 13:36:51 +0530682 pr_err("no channels available for device %s\n", devname);
Aneesh Kumar K.Vc1a7c222010-02-15 17:27:02 +0000683 return ret;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500684 }
685
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500686 client->trans = (void *)chan;
Eric Van Hensbergen562ada62010-01-15 18:54:03 -0600687 client->status = Connected;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500688 chan->client = client;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500689
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500690 return 0;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500691}
692
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600693/**
694 * p9_virtio_remove - clean up resources associated with a virtio device
695 * @vdev: virtio device to remove
696 *
697 */
698
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600699static void p9_virtio_remove(struct virtio_device *vdev)
700{
701 struct virtio_chan *chan = vdev->priv;
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030702 unsigned long warning_time;
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600703
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000704 mutex_lock(&virtio_9p_lock);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030705
706 /* Remove self from list so we don't get new users. */
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000707 list_del(&chan->chan_list);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030708 warning_time = jiffies;
709
710 /* Wait for existing users to close. */
711 while (chan->inuse) {
712 mutex_unlock(&virtio_9p_lock);
713 msleep(250);
714 if (time_after(jiffies, warning_time + 10 * HZ)) {
715 dev_emerg(&vdev->dev,
716 "p9_virtio_remove: waiting for device in use.\n");
717 warning_time = jiffies;
718 }
719 mutex_lock(&virtio_9p_lock);
720 }
721
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000722 mutex_unlock(&virtio_9p_lock);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030723
Michael S. Tsirkind9679d02021-10-13 06:55:44 -0400724 virtio_reset_device(vdev);
Michael S. Tsirkin8051a2a2015-03-12 11:53:41 +1030725 vdev->config->del_vqs(vdev);
726
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000727 sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
Michael Marineaue0d6cb92013-08-11 00:53:45 -0400728 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000729 kfree(chan->tag);
Venkateswararao Jujjuri (JV)52f44e02010-09-29 18:33:41 -0700730 kfree(chan->vc_wq);
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000731 kfree(chan);
732
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600733}
734
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500735static struct virtio_device_id id_table[] = {
736 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
737 { 0 },
738};
739
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000740static unsigned int features[] = {
741 VIRTIO_9P_MOUNT_TAG,
742};
743
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500744/* The standard "struct lguest_driver": */
745static struct virtio_driver p9_virtio_drv = {
Aneesh Kumar K.V97ee9b02010-03-06 04:44:14 +0000746 .feature_table = features,
747 .feature_table_size = ARRAY_SIZE(features),
748 .driver.name = KBUILD_MODNAME,
749 .driver.owner = THIS_MODULE,
750 .id_table = id_table,
751 .probe = p9_virtio_probe,
752 .remove = p9_virtio_remove,
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500753};
754
755static struct p9_trans_module p9_virtio_trans = {
756 .name = "virtio",
757 .create = p9_virtio_create,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500758 .close = p9_virtio_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500759 .request = p9_virtio_request,
Arnd Bergmanndc893e12013-03-08 12:43:31 -0800760 .zc_request = p9_virtio_zc_request,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500761 .cancel = p9_virtio_cancel,
Tomas Bortoli728356d2018-08-14 19:43:42 +0200762 .cancelled = p9_virtio_cancelled,
Aneesh Kumar K.Vb49d8b52011-08-17 16:56:04 +0000763 /*
764 * We leave one entry for input and one entry for response
Zheng Yongjun8ab17842021-06-02 14:54:42 +0800765 * headers. We also skip one more entry to accommodate, address
Aneesh Kumar K.Vb49d8b52011-08-17 16:56:04 +0000766 * that are not at page boundary, that can result in an extra
767 * page in zero copy.
768 */
769 .maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
Eric Van Hensbergenf94741f2013-11-12 10:20:03 -0600770 .def = 1,
Tejun Heo72029fe2008-09-24 16:22:23 -0500771 .owner = THIS_MODULE,
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500772};
773
774/* The standard init function */
775static int __init p9_virtio_init(void)
776{
YueHaibingd4548542019-04-30 19:59:42 +0800777 int rc;
778
Aneesh Kumar K.V37c1209d42010-02-15 17:27:01 +0000779 INIT_LIST_HEAD(&virtio_chan_list);
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500780
781 v9fs_register_trans(&p9_virtio_trans);
YueHaibingd4548542019-04-30 19:59:42 +0800782 rc = register_virtio_driver(&p9_virtio_drv);
783 if (rc)
784 v9fs_unregister_trans(&p9_virtio_trans);
785
786 return rc;
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500787}
788
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600789static void __exit p9_virtio_cleanup(void)
790{
791 unregister_virtio_driver(&p9_virtio_drv);
Tejun Heo72029fe2008-09-24 16:22:23 -0500792 v9fs_unregister_trans(&p9_virtio_trans);
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600793}
794
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500795module_init(p9_virtio_init);
Eric Van Hensbergenf3933542008-02-06 19:25:04 -0600796module_exit(p9_virtio_cleanup);
Thomas Weißschuh4cd82a52021-10-17 15:46:11 +0200797MODULE_ALIAS_9P("virtio");
Eric Van Hensbergenb530cc72007-10-23 13:47:31 -0500798
799MODULE_DEVICE_TABLE(virtio, id_table);
800MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
801MODULE_DESCRIPTION("Virtio 9p Transport");
802MODULE_LICENSE("GPL");