xref: /kvmtool/virtio/9p.c (revision ae06ce7165bbb08080fcbf9bb026b16de1b3b685)
11c7850f9SSasha Levin #include "kvm/virtio-pci-dev.h"
21c7850f9SSasha Levin #include "kvm/ioport.h"
31c7850f9SSasha Levin #include "kvm/util.h"
41c7850f9SSasha Levin #include "kvm/threadpool.h"
5bfc15268SAneesh Kumar K.V #include "kvm/irq.h"
6bfc15268SAneesh Kumar K.V #include "kvm/virtio-9p.h"
7e59662b3SSasha Levin #include "kvm/guest_compat.h"
8cac9e8fdSSasha Levin #include "kvm/builtin-setup.h"
91c7850f9SSasha Levin 
10bfc15268SAneesh Kumar K.V #include <stdio.h>
11bfc15268SAneesh Kumar K.V #include <stdlib.h>
121c7850f9SSasha Levin #include <fcntl.h>
131c7850f9SSasha Levin #include <sys/stat.h>
14bfc15268SAneesh Kumar K.V #include <unistd.h>
15bfc15268SAneesh Kumar K.V #include <string.h>
16bfc15268SAneesh Kumar K.V #include <errno.h>
17c797b6c6SAneesh Kumar K.V #include <sys/vfs.h>
181c7850f9SSasha Levin 
192daa28d4SAneesh Kumar K.V #include <linux/virtio_ring.h>
202daa28d4SAneesh Kumar K.V #include <linux/virtio_9p.h>
212daa28d4SAneesh Kumar K.V #include <net/9p/9p.h>
222daa28d4SAneesh Kumar K.V 
23c7838fbdSSasha Levin static LIST_HEAD(devs);
24312c62d1SSasha Levin static int compat_id = -1;
25c7838fbdSSasha Levin 
26e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid);
27e2341580SSasha Levin static struct p9_fid *find_or_create_fid(struct p9_dev *dev, u32 fid)
28e2341580SSasha Levin {
29e2341580SSasha Levin 	struct rb_node *node = dev->fids.rb_node;
30e2341580SSasha Levin 	struct p9_fid *pfid = NULL;
31e2341580SSasha Levin 
32e2341580SSasha Levin 	while (node) {
33e2341580SSasha Levin 		struct p9_fid *cur = rb_entry(node, struct p9_fid, node);
34e2341580SSasha Levin 
35e2341580SSasha Levin 		if (fid < cur->fid) {
36e2341580SSasha Levin 			node = node->rb_left;
37e2341580SSasha Levin 		} else if (fid > cur->fid) {
38e2341580SSasha Levin 			node = node->rb_right;
39e2341580SSasha Levin 		} else {
40e2341580SSasha Levin 			return cur;
41e2341580SSasha Levin 		}
42e2341580SSasha Levin 	}
43e2341580SSasha Levin 
44e2341580SSasha Levin 	pfid = calloc(sizeof(*pfid), 1);
45e2341580SSasha Levin 	if (!pfid)
46e2341580SSasha Levin 		return NULL;
47e2341580SSasha Levin 
48e2341580SSasha Levin 	pfid->fid = fid;
49e2341580SSasha Levin 	strcpy(pfid->abs_path, dev->root_dir);
50e2341580SSasha Levin 	pfid->path = pfid->abs_path + strlen(dev->root_dir);
51e2341580SSasha Levin 
52e2341580SSasha Levin 	insert_new_fid(dev, pfid);
53e2341580SSasha Levin 
54e2341580SSasha Levin 	return pfid;
55e2341580SSasha Levin }
56e2341580SSasha Levin 
57e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid)
58e2341580SSasha Levin {
59e2341580SSasha Levin 	struct rb_node **node = &(dev->fids.rb_node), *parent = NULL;
60e2341580SSasha Levin 
61e2341580SSasha Levin 	while (*node) {
62e2341580SSasha Levin 		int result = fid->fid - rb_entry(*node, struct p9_fid, node)->fid;
63e2341580SSasha Levin 
64e2341580SSasha Levin 		parent = *node;
65e2341580SSasha Levin 		if (result < 0)
66e2341580SSasha Levin 			node    = &((*node)->rb_left);
67e2341580SSasha Levin 		else if (result > 0)
68e2341580SSasha Levin 			node    = &((*node)->rb_right);
69e2341580SSasha Levin 		else
70e2341580SSasha Levin 			return -EEXIST;
71e2341580SSasha Levin 	}
72e2341580SSasha Levin 
73e2341580SSasha Levin 	rb_link_node(&fid->node, parent, node);
74e2341580SSasha Levin 	rb_insert_color(&fid->node, &dev->fids);
75e2341580SSasha Levin 	return 0;
76e2341580SSasha Levin }
77e2341580SSasha Levin 
7831a6fb8dSSasha Levin static struct p9_fid *get_fid(struct p9_dev *p9dev, int fid)
7931a6fb8dSSasha Levin {
80e2341580SSasha Levin 	struct p9_fid *new;
8131a6fb8dSSasha Levin 
82e2341580SSasha Levin 	new = find_or_create_fid(p9dev, fid);
83e2341580SSasha Levin 
84e2341580SSasha Levin 	return new;
8531a6fb8dSSasha Levin }
8631a6fb8dSSasha Levin 
871c7850f9SSasha Levin /* Warning: Immediately use value returned from this function */
88b4422bf3SAneesh Kumar K.V static const char *rel_to_abs(struct p9_dev *p9dev,
89b4422bf3SAneesh Kumar K.V 			      const char *path, char *abs_path)
901c7850f9SSasha Levin {
91b4422bf3SAneesh Kumar K.V 	sprintf(abs_path, "%s/%s", p9dev->root_dir, path);
921c7850f9SSasha Levin 
931c7850f9SSasha Levin 	return abs_path;
941c7850f9SSasha Levin }
951c7850f9SSasha Levin 
96c797b6c6SAneesh Kumar K.V static void stat2qid(struct stat *st, struct p9_qid *qid)
971c7850f9SSasha Levin {
981c7850f9SSasha Levin 	*qid = (struct p9_qid) {
991c7850f9SSasha Levin 		.path		= st->st_ino,
1001c7850f9SSasha Levin 		.version	= st->st_mtime,
1011c7850f9SSasha Levin 	};
1021c7850f9SSasha Levin 
1031c7850f9SSasha Levin 	if (S_ISDIR(st->st_mode))
1041c7850f9SSasha Levin 		qid->type	|= P9_QTDIR;
1051c7850f9SSasha Levin }
1061c7850f9SSasha Levin 
107b4422bf3SAneesh Kumar K.V static void close_fid(struct p9_dev *p9dev, u32 fid)
1081c7850f9SSasha Levin {
109e2341580SSasha Levin 	struct p9_fid *pfid = get_fid(p9dev, fid);
110e2341580SSasha Levin 
111e2341580SSasha Levin 	if (pfid->fd > 0)
112e2341580SSasha Levin 		close(pfid->fd);
113e2341580SSasha Levin 
114e2341580SSasha Levin 	if (pfid->dir)
115e2341580SSasha Levin 		closedir(pfid->dir);
116e2341580SSasha Levin 
117e2341580SSasha Levin 	rb_erase(&pfid->node, &p9dev->fids);
118e2341580SSasha Levin 	free(pfid);
1191c7850f9SSasha Levin }
120e2341580SSasha Levin 
121bfc15268SAneesh Kumar K.V static void virtio_p9_set_reply_header(struct p9_pdu *pdu, u32 size)
1221c7850f9SSasha Levin {
123bfc15268SAneesh Kumar K.V 	u8 cmd;
124bfc15268SAneesh Kumar K.V 	u16 tag;
125bfc15268SAneesh Kumar K.V 
126bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32);
127bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "bw", &cmd, &tag);
128bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
129bfc15268SAneesh Kumar K.V 	/* cmd + 1 is the reply message */
130bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", size, cmd + 1, tag);
1311c7850f9SSasha Levin }
1321c7850f9SSasha Levin 
1336b163a87SAneesh Kumar K.V static u16 virtio_p9_update_iov_cnt(struct iovec iov[], u32 count, int iov_cnt)
1346b163a87SAneesh Kumar K.V {
1356b163a87SAneesh Kumar K.V 	int i;
1366b163a87SAneesh Kumar K.V 	u32 total = 0;
1376b163a87SAneesh Kumar K.V 	for (i = 0; (i < iov_cnt) && (total < count); i++) {
1386b163a87SAneesh Kumar K.V 		if (total + iov[i].iov_len > count) {
1396b163a87SAneesh Kumar K.V 			/* we don't need this iov fully */
1406b163a87SAneesh Kumar K.V 			iov[i].iov_len -= ((total + iov[i].iov_len) - count);
1416b163a87SAneesh Kumar K.V 			i++;
1426b163a87SAneesh Kumar K.V 			break;
1436b163a87SAneesh Kumar K.V 		}
1446b163a87SAneesh Kumar K.V 		total += iov[i].iov_len;
1456b163a87SAneesh Kumar K.V 	}
1466b163a87SAneesh Kumar K.V 	return i;
1476b163a87SAneesh Kumar K.V }
1486b163a87SAneesh Kumar K.V 
149eee1ba8eSAneesh Kumar K.V static void virtio_p9_error_reply(struct p9_dev *p9dev,
150eee1ba8eSAneesh Kumar K.V 				  struct p9_pdu *pdu, int err, u32 *outlen)
151eee1ba8eSAneesh Kumar K.V {
152bfc15268SAneesh Kumar K.V 	u16 tag;
153eee1ba8eSAneesh Kumar K.V 
1545529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
155c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", err);
156bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
157eee1ba8eSAneesh Kumar K.V 
158c797b6c6SAneesh Kumar K.V 	/* read the tag from input */
159bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32) + sizeof(u8);
160bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "w", &tag);
161bfc15268SAneesh Kumar K.V 
162c797b6c6SAneesh Kumar K.V 	/* Update the header */
163bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
164c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", *outlen, P9_RLERROR, tag);
165eee1ba8eSAneesh Kumar K.V }
166eee1ba8eSAneesh Kumar K.V 
167ead43b01SAneesh Kumar K.V static void virtio_p9_version(struct p9_dev *p9dev,
168af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1691c7850f9SSasha Levin {
170c797b6c6SAneesh Kumar K.V 	u32 msize;
171c797b6c6SAneesh Kumar K.V 	char *version;
172c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ds", &msize, &version);
173c797b6c6SAneesh Kumar K.V 	/*
174c797b6c6SAneesh Kumar K.V 	 * reply with the same msize the client sent us
175c797b6c6SAneesh Kumar K.V 	 * Error out if the request is not for 9P2000.L
176c797b6c6SAneesh Kumar K.V 	 */
1775529bcd7SAsias He 	if (!strcmp(version, VIRTIO_9P_VERSION_DOTL))
178c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, version);
179c797b6c6SAneesh Kumar K.V 	else
180c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, "unknown");
1811c7850f9SSasha Levin 
182bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
183bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
184c797b6c6SAneesh Kumar K.V 	free(version);
185ead43b01SAneesh Kumar K.V 	return;
1861c7850f9SSasha Levin }
1871c7850f9SSasha Levin 
188ead43b01SAneesh Kumar K.V static void virtio_p9_clunk(struct p9_dev *p9dev,
189af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
1901c7850f9SSasha Levin {
191bfc15268SAneesh Kumar K.V 	u32 fid;
1921c7850f9SSasha Levin 
193bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid);
194bfc15268SAneesh Kumar K.V 	close_fid(p9dev, fid);
1951c7850f9SSasha Levin 
196bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
197bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
198ead43b01SAneesh Kumar K.V 	return;
1991c7850f9SSasha Levin }
2001c7850f9SSasha Levin 
201c797b6c6SAneesh Kumar K.V /*
202c797b6c6SAneesh Kumar K.V  * FIXME!! Need to map to protocol independent value. Upstream
203c797b6c6SAneesh Kumar K.V  * 9p also have the same BUG
204c797b6c6SAneesh Kumar K.V  */
205c797b6c6SAneesh Kumar K.V static int virtio_p9_openflags(int flags)
206c797b6c6SAneesh Kumar K.V {
207c797b6c6SAneesh Kumar K.V 	flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT | O_DIRECT);
208c797b6c6SAneesh Kumar K.V 	flags |= O_NOFOLLOW;
209c797b6c6SAneesh Kumar K.V 	return flags;
210c797b6c6SAneesh Kumar K.V }
211c797b6c6SAneesh Kumar K.V 
21232585666SSasha Levin static bool is_dir(struct p9_fid *fid)
21332585666SSasha Levin {
21432585666SSasha Levin 	struct stat st;
21532585666SSasha Levin 
21632585666SSasha Levin 	stat(fid->abs_path, &st);
21732585666SSasha Levin 
21832585666SSasha Levin 	return S_ISDIR(st.st_mode);
21932585666SSasha Levin }
22032585666SSasha Levin 
221ead43b01SAneesh Kumar K.V static void virtio_p9_open(struct p9_dev *p9dev,
222af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
2231c7850f9SSasha Levin {
224c797b6c6SAneesh Kumar K.V 	u32 fid, flags;
2251c7850f9SSasha Levin 	struct stat st;
226bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
227bfc15268SAneesh Kumar K.V 	struct p9_fid *new_fid;
228bfc15268SAneesh Kumar K.V 
229c797b6c6SAneesh Kumar K.V 
230c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid, &flags);
23131a6fb8dSSasha Levin 	new_fid = get_fid(p9dev, fid);
2321c7850f9SSasha Levin 
23330204a77SAneesh Kumar K.V 	if (lstat(new_fid->abs_path, &st) < 0)
234eee1ba8eSAneesh Kumar K.V 		goto err_out;
2351c7850f9SSasha Levin 
236c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
2371c7850f9SSasha Levin 
23832585666SSasha Levin 	if (is_dir(new_fid)) {
2391c7850f9SSasha Levin 		new_fid->dir = opendir(new_fid->abs_path);
240eee1ba8eSAneesh Kumar K.V 		if (!new_fid->dir)
241eee1ba8eSAneesh Kumar K.V 			goto err_out;
242eee1ba8eSAneesh Kumar K.V 	} else {
243eee1ba8eSAneesh Kumar K.V 		new_fid->fd  = open(new_fid->abs_path,
244c797b6c6SAneesh Kumar K.V 				    virtio_p9_openflags(flags));
245eee1ba8eSAneesh Kumar K.V 		if (new_fid->fd < 0)
246eee1ba8eSAneesh Kumar K.V 			goto err_out;
247eee1ba8eSAneesh Kumar K.V 	}
248c797b6c6SAneesh Kumar K.V 	/* FIXME!! need ot send proper iounit  */
249bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
250bfc15268SAneesh Kumar K.V 
251bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
252bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
253ead43b01SAneesh Kumar K.V 	return;
254eee1ba8eSAneesh Kumar K.V err_out:
255eee1ba8eSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
256ead43b01SAneesh Kumar K.V 	return;
2571c7850f9SSasha Levin }
2581c7850f9SSasha Levin 
259ead43b01SAneesh Kumar K.V static void virtio_p9_create(struct p9_dev *p9dev,
260af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
2611c7850f9SSasha Levin {
262c797b6c6SAneesh Kumar K.V 	int fd, ret;
263bfc15268SAneesh Kumar K.V 	char *name;
264af045e53SAneesh Kumar K.V 	struct stat st;
265bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
266c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
2674bc9734aSAneesh Kumar K.V 	char full_path[PATH_MAX];
268c797b6c6SAneesh Kumar K.V 	u32 dfid_val, flags, mode, gid;
269af045e53SAneesh Kumar K.V 
270c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsddd", &dfid_val,
271c797b6c6SAneesh Kumar K.V 			    &name, &flags, &mode, &gid);
27231a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
2731c7850f9SSasha Levin 
274c797b6c6SAneesh Kumar K.V 	flags = virtio_p9_openflags(flags);
2755f900f6dSSasha Levin 
276c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
277c797b6c6SAneesh Kumar K.V 	fd = open(full_path, flags | O_CREAT, mode);
2784bc9734aSAneesh Kumar K.V 	if (fd < 0)
2794bc9734aSAneesh Kumar K.V 		goto err_out;
280c797b6c6SAneesh Kumar K.V 	dfid->fd = fd;
281c797b6c6SAneesh Kumar K.V 
2824bc9734aSAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
2836c8ca053SAneesh Kumar K.V 		goto err_out;
2841c7850f9SSasha Levin 
285c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
286c797b6c6SAneesh Kumar K.V 	if (ret < 0)
287c797b6c6SAneesh Kumar K.V 		goto err_out;
288c797b6c6SAneesh Kumar K.V 
289c797b6c6SAneesh Kumar K.V 	sprintf(dfid->path, "%s/%s", dfid->path, name);
290c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
291bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
292bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
293bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
2945f900f6dSSasha Levin 	free(name);
2956c8ca053SAneesh Kumar K.V 	return;
2966c8ca053SAneesh Kumar K.V err_out:
2975f900f6dSSasha Levin 	free(name);
298c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
299c797b6c6SAneesh Kumar K.V 	return;
300c797b6c6SAneesh Kumar K.V }
301c797b6c6SAneesh Kumar K.V 
302c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev,
303c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
304c797b6c6SAneesh Kumar K.V {
305c797b6c6SAneesh Kumar K.V 	int ret;
306c797b6c6SAneesh Kumar K.V 	char *name;
307c797b6c6SAneesh Kumar K.V 	struct stat st;
308c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
309c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
310c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
311c797b6c6SAneesh Kumar K.V 	u32 dfid_val, mode, gid;
312c797b6c6SAneesh Kumar K.V 
313c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val,
314c797b6c6SAneesh Kumar K.V 			    &name, &mode, &gid);
31531a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
316c797b6c6SAneesh Kumar K.V 
317c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
318c797b6c6SAneesh Kumar K.V 	ret = mkdir(full_path, mode);
319c797b6c6SAneesh Kumar K.V 	if (ret < 0)
320c797b6c6SAneesh Kumar K.V 		goto err_out;
321c797b6c6SAneesh Kumar K.V 
322c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
323c797b6c6SAneesh Kumar K.V 		goto err_out;
324c797b6c6SAneesh Kumar K.V 
325c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
326c797b6c6SAneesh Kumar K.V 	if (ret < 0)
327c797b6c6SAneesh Kumar K.V 		goto err_out;
328c797b6c6SAneesh Kumar K.V 
329c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
330c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
331c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
332c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
333c797b6c6SAneesh Kumar K.V 	free(name);
334c797b6c6SAneesh Kumar K.V 	return;
335c797b6c6SAneesh Kumar K.V err_out:
336c797b6c6SAneesh Kumar K.V 	free(name);
337c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
338ead43b01SAneesh Kumar K.V 	return;
3391c7850f9SSasha Levin }
3401c7850f9SSasha Levin 
341ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev,
342af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
3431c7850f9SSasha Levin {
344af045e53SAneesh Kumar K.V 	u8 i;
345bfc15268SAneesh Kumar K.V 	u16 nwqid;
346bfc15268SAneesh Kumar K.V 	u16 nwname;
347bfc15268SAneesh Kumar K.V 	struct p9_qid wqid;
348e2341580SSasha Levin 	struct p9_fid *new_fid, *old_fid;
349c797b6c6SAneesh Kumar K.V 	u32 fid_val, newfid_val;
350c797b6c6SAneesh Kumar K.V 
3511c7850f9SSasha Levin 
352bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname);
35331a6fb8dSSasha Levin 	new_fid	= get_fid(p9dev, newfid_val);
3541c7850f9SSasha Levin 
355bfc15268SAneesh Kumar K.V 	nwqid = 0;
356bfc15268SAneesh Kumar K.V 	if (nwname) {
35731a6fb8dSSasha Levin 		struct p9_fid *fid = get_fid(p9dev, fid_val);
358bfc15268SAneesh Kumar K.V 
359baac79a5SAneesh Kumar K.V 		strcpy(new_fid->path, fid->path);
360bfc15268SAneesh Kumar K.V 		/* skip the space for count */
361bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
362bfc15268SAneesh Kumar K.V 		for (i = 0; i < nwname; i++) {
363bfc15268SAneesh Kumar K.V 			struct stat st;
3641c7850f9SSasha Levin 			char tmp[PATH_MAX] = {0};
3651c7850f9SSasha Levin 			char full_path[PATH_MAX];
366e55ed135SPekka Enberg 			char *str;
367bfc15268SAneesh Kumar K.V 
368bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_readf(pdu, "s", &str);
3691c7850f9SSasha Levin 
3701c7850f9SSasha Levin 			/* Format the new path we're 'walk'ing into */
371baac79a5SAneesh Kumar K.V 			sprintf(tmp, "%s/%s", new_fid->path, str);
372e55ed135SPekka Enberg 
373e55ed135SPekka Enberg 			free(str);
374e55ed135SPekka Enberg 
375c797b6c6SAneesh Kumar K.V 			if (lstat(rel_to_abs(p9dev, tmp, full_path), &st) < 0)
3766c8ca053SAneesh Kumar K.V 				goto err_out;
3771c7850f9SSasha Levin 
378c797b6c6SAneesh Kumar K.V 			stat2qid(&st, &wqid);
3791c7850f9SSasha Levin 			strcpy(new_fid->path, tmp);
380c797b6c6SAneesh Kumar K.V 			new_fid->uid = fid->uid;
381bfc15268SAneesh Kumar K.V 			nwqid++;
382bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_writef(pdu, "Q", &wqid);
3831c7850f9SSasha Levin 		}
3841c7850f9SSasha Levin 	} else {
385bfc15268SAneesh Kumar K.V 		/*
386bfc15268SAneesh Kumar K.V 		 * update write_offset so our outlen get correct value
387bfc15268SAneesh Kumar K.V 		 */
388bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
389e2341580SSasha Levin 		old_fid = get_fid(p9dev, fid_val);
390e2341580SSasha Levin 		strcpy(new_fid->path, old_fid->path);
391e2341580SSasha Levin 		new_fid->uid    = old_fid->uid;
3921c7850f9SSasha Levin 	}
393bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
3945529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
395bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", nwqid);
396bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
3976c8ca053SAneesh Kumar K.V 	return;
3986c8ca053SAneesh Kumar K.V err_out:
3996c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
400ead43b01SAneesh Kumar K.V 	return;
4011c7850f9SSasha Levin }
4021c7850f9SSasha Levin 
403ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev,
404af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
4051c7850f9SSasha Levin {
406bfc15268SAneesh Kumar K.V 	char *uname;
407bfc15268SAneesh Kumar K.V 	char *aname;
4081c7850f9SSasha Levin 	struct stat st;
409bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
4101c7850f9SSasha Levin 	struct p9_fid *fid;
411c797b6c6SAneesh Kumar K.V 	u32 fid_val, afid, uid;
412bfc15268SAneesh Kumar K.V 
413c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid,
414c797b6c6SAneesh Kumar K.V 			    &uname, &aname, &uid);
4151c7850f9SSasha Levin 
41639257180SPekka Enberg 	free(uname);
41739257180SPekka Enberg 	free(aname);
41839257180SPekka Enberg 
41930204a77SAneesh Kumar K.V 	if (lstat(p9dev->root_dir, &st) < 0)
4206c8ca053SAneesh Kumar K.V 		goto err_out;
4211c7850f9SSasha Levin 
422c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
4231c7850f9SSasha Levin 
42431a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
425c797b6c6SAneesh Kumar K.V 	fid->uid = uid;
4261c7850f9SSasha Levin 	strcpy(fid->path, "/");
4271c7850f9SSasha Levin 
428bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
429bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
430bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
4316c8ca053SAneesh Kumar K.V 	return;
4326c8ca053SAneesh Kumar K.V err_out:
4336c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
434ead43b01SAneesh Kumar K.V 	return;
4351c7850f9SSasha Levin }
4361c7850f9SSasha Levin 
437c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev,
438c797b6c6SAneesh Kumar K.V 				struct stat *st, struct p9_stat_dotl *statl)
4395f900f6dSSasha Levin {
440c797b6c6SAneesh Kumar K.V 	memset(statl, 0, sizeof(*statl));
441c797b6c6SAneesh Kumar K.V 	statl->st_mode		= st->st_mode;
442c797b6c6SAneesh Kumar K.V 	statl->st_nlink		= st->st_nlink;
443c797b6c6SAneesh Kumar K.V 	statl->st_uid		= st->st_uid;
444c797b6c6SAneesh Kumar K.V 	statl->st_gid		= st->st_gid;
445c797b6c6SAneesh Kumar K.V 	statl->st_rdev		= st->st_rdev;
446c797b6c6SAneesh Kumar K.V 	statl->st_size		= st->st_size;
447c797b6c6SAneesh Kumar K.V 	statl->st_blksize	= st->st_blksize;
448c797b6c6SAneesh Kumar K.V 	statl->st_blocks	= st->st_blocks;
449c797b6c6SAneesh Kumar K.V 	statl->st_atime_sec	= st->st_atime;
450c797b6c6SAneesh Kumar K.V 	statl->st_atime_nsec	= st->st_atim.tv_nsec;
451c797b6c6SAneesh Kumar K.V 	statl->st_mtime_sec	= st->st_mtime;
452c797b6c6SAneesh Kumar K.V 	statl->st_mtime_nsec	= st->st_mtim.tv_nsec;
453c797b6c6SAneesh Kumar K.V 	statl->st_ctime_sec	= st->st_ctime;
454c797b6c6SAneesh Kumar K.V 	statl->st_ctime_nsec	= st->st_ctim.tv_nsec;
455c797b6c6SAneesh Kumar K.V 	/* Currently we only support BASIC fields in stat */
456c797b6c6SAneesh Kumar K.V 	statl->st_result_mask	= P9_STATS_BASIC;
457c797b6c6SAneesh Kumar K.V 	stat2qid(st, &statl->qid);
4581c7850f9SSasha Levin }
4591c7850f9SSasha Levin 
460ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev,
461af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
4621c7850f9SSasha Levin {
463bfc15268SAneesh Kumar K.V 	u64 offset;
464bfc15268SAneesh Kumar K.V 	u32 fid_val;
465c797b6c6SAneesh Kumar K.V 	u16 iov_cnt;
466c797b6c6SAneesh Kumar K.V 	void *iov_base;
467c797b6c6SAneesh Kumar K.V 	size_t iov_len;
468bfc15268SAneesh Kumar K.V 	u32 count, rcount;
469bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
470c797b6c6SAneesh Kumar K.V 
4711c7850f9SSasha Levin 
472bfc15268SAneesh Kumar K.V 	rcount = 0;
473bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
47431a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
47550c479e0SAneesh Kumar K.V 
47650c479e0SAneesh Kumar K.V 	iov_base = pdu->in_iov[0].iov_base;
47750c479e0SAneesh Kumar K.V 	iov_len  = pdu->in_iov[0].iov_len;
47850c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->in_iov_cnt;
4795529bcd7SAsias He 	pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32);
4805529bcd7SAsias He 	pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32);
4816b163a87SAneesh Kumar K.V 	pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov,
482bfc15268SAneesh Kumar K.V 						   count,
4836b163a87SAneesh Kumar K.V 						   pdu->in_iov_cnt);
484bfc15268SAneesh Kumar K.V 	rcount = preadv(fid->fd, pdu->in_iov,
485bfc15268SAneesh Kumar K.V 			pdu->in_iov_cnt, offset);
486bfc15268SAneesh Kumar K.V 	if (rcount > count)
487bfc15268SAneesh Kumar K.V 		rcount = count;
488bfc15268SAneesh Kumar K.V 	/*
489bfc15268SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
490bfc15268SAneesh Kumar K.V 	 * pdu_writef works correctly.
491bfc15268SAneesh Kumar K.V 	 */
49250c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_base = iov_base;
49350c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_len  = iov_len;
49450c479e0SAneesh Kumar K.V 	pdu->in_iov_cnt         = iov_cnt;
495c797b6c6SAneesh Kumar K.V 
4965529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
497bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
498bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
499bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
500ead43b01SAneesh Kumar K.V 	return;
5011c7850f9SSasha Levin }
5021c7850f9SSasha Levin 
503c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent)
504c797b6c6SAneesh Kumar K.V {
505c797b6c6SAneesh Kumar K.V 	/*
506c797b6c6SAneesh Kumar K.V 	 * Size of each dirent:
507c797b6c6SAneesh Kumar K.V 	 * qid(13) + offset(8) + type(1) + name_len(2) + name
508c797b6c6SAneesh Kumar K.V 	 */
509c797b6c6SAneesh Kumar K.V 	return 24 + strlen(dent->d_name);
510c797b6c6SAneesh Kumar K.V }
511c797b6c6SAneesh Kumar K.V 
512c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev,
513c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
514c797b6c6SAneesh Kumar K.V {
515c797b6c6SAneesh Kumar K.V 	u32 fid_val;
516c797b6c6SAneesh Kumar K.V 	u32 count, rcount;
517c797b6c6SAneesh Kumar K.V 	struct stat st;
518c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
519c797b6c6SAneesh Kumar K.V 	struct dirent *dent;
520c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
521c797b6c6SAneesh Kumar K.V 	u64 offset, old_offset;
522c797b6c6SAneesh Kumar K.V 
523c797b6c6SAneesh Kumar K.V 	rcount = 0;
524c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
52531a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
526c797b6c6SAneesh Kumar K.V 
52732585666SSasha Levin 	if (!is_dir(fid)) {
52869bb4278SSasha Levin 		errno = EINVAL;
529c797b6c6SAneesh Kumar K.V 		goto err_out;
530c797b6c6SAneesh Kumar K.V 	}
531c797b6c6SAneesh Kumar K.V 
532c797b6c6SAneesh Kumar K.V 	/* Move the offset specified */
533c797b6c6SAneesh Kumar K.V 	seekdir(fid->dir, offset);
534c797b6c6SAneesh Kumar K.V 
535c797b6c6SAneesh Kumar K.V 	old_offset = offset;
536c797b6c6SAneesh Kumar K.V 	/* If reading a dir, fill the buffer with p9_stat entries */
537c797b6c6SAneesh Kumar K.V 	dent = readdir(fid->dir);
538c797b6c6SAneesh Kumar K.V 
539c797b6c6SAneesh Kumar K.V 	/* Skip the space for writing count */
540c797b6c6SAneesh Kumar K.V 	pdu->write_offset += sizeof(u32);
541c797b6c6SAneesh Kumar K.V 	while (dent) {
542c797b6c6SAneesh Kumar K.V 		u32 read;
543c797b6c6SAneesh Kumar K.V 		struct p9_qid qid;
544c797b6c6SAneesh Kumar K.V 
545c797b6c6SAneesh Kumar K.V 		if ((rcount + virtio_p9_dentry_size(dent)) > count) {
546c797b6c6SAneesh Kumar K.V 			/* seek to the previous offset and return */
547c797b6c6SAneesh Kumar K.V 			seekdir(fid->dir, old_offset);
548c797b6c6SAneesh Kumar K.V 			break;
549c797b6c6SAneesh Kumar K.V 		}
550c797b6c6SAneesh Kumar K.V 		old_offset = dent->d_off;
551c797b6c6SAneesh Kumar K.V 		lstat(rel_to_abs(p9dev, dent->d_name, full_path), &st);
552c797b6c6SAneesh Kumar K.V 		stat2qid(&st, &qid);
553c797b6c6SAneesh Kumar K.V 		read = pdu->write_offset;
554c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off,
555c797b6c6SAneesh Kumar K.V 				     dent->d_type, dent->d_name);
556c797b6c6SAneesh Kumar K.V 		rcount += pdu->write_offset - read;
557c797b6c6SAneesh Kumar K.V 		dent = readdir(fid->dir);
558c797b6c6SAneesh Kumar K.V 	}
559c797b6c6SAneesh Kumar K.V 
5605529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
561c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
562c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
563c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
564c797b6c6SAneesh Kumar K.V 	return;
565c797b6c6SAneesh Kumar K.V err_out:
566c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
567c797b6c6SAneesh Kumar K.V 	return;
568c797b6c6SAneesh Kumar K.V }
569c797b6c6SAneesh Kumar K.V 
570c797b6c6SAneesh Kumar K.V 
571c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev,
572af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
5731c7850f9SSasha Levin {
574bfc15268SAneesh Kumar K.V 	u32 fid_val;
575af045e53SAneesh Kumar K.V 	struct stat st;
576c797b6c6SAneesh Kumar K.V 	u64 request_mask;
577bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
578c797b6c6SAneesh Kumar K.V 	struct p9_stat_dotl statl;
5791c7850f9SSasha Levin 
580c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask);
58131a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
58230204a77SAneesh Kumar K.V 	if (lstat(fid->abs_path, &st) < 0)
5836c8ca053SAneesh Kumar K.V 		goto err_out;
5841c7850f9SSasha Levin 
585c797b6c6SAneesh Kumar K.V 	virtio_p9_fill_stat(p9dev, &st, &statl);
586c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "A", &statl);
587bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
588bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
589ead43b01SAneesh Kumar K.V 	return;
5906c8ca053SAneesh Kumar K.V err_out:
5916c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
5926c8ca053SAneesh Kumar K.V 	return;
5931c7850f9SSasha Levin }
5941c7850f9SSasha Levin 
595c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */
596c797b6c6SAneesh Kumar K.V /*
597c797b6c6SAneesh Kumar K.V  * Attribute flags.  These should be or-ed together to figure out what
598c797b6c6SAneesh Kumar K.V  * has been changed!
599c797b6c6SAneesh Kumar K.V  */
600c797b6c6SAneesh Kumar K.V #define ATTR_MODE	(1 << 0)
601c797b6c6SAneesh Kumar K.V #define ATTR_UID	(1 << 1)
602c797b6c6SAneesh Kumar K.V #define ATTR_GID	(1 << 2)
603c797b6c6SAneesh Kumar K.V #define ATTR_SIZE	(1 << 3)
604c797b6c6SAneesh Kumar K.V #define ATTR_ATIME	(1 << 4)
605c797b6c6SAneesh Kumar K.V #define ATTR_MTIME	(1 << 5)
606c797b6c6SAneesh Kumar K.V #define ATTR_CTIME	(1 << 6)
607c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET	(1 << 7)
608c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET	(1 << 8)
609c797b6c6SAneesh Kumar K.V #define ATTR_FORCE	(1 << 9) /* Not a change, but a change it */
610c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG	(1 << 10)
611c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID	(1 << 11)
612c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID	(1 << 12)
613c797b6c6SAneesh Kumar K.V #define ATTR_FILE	(1 << 13)
614c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV	(1 << 14)
615c797b6c6SAneesh Kumar K.V #define ATTR_OPEN	(1 << 15) /* Truncating from open(O_TRUNC) */
616c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET	(1 << 16)
617c797b6c6SAneesh Kumar K.V 
618c797b6c6SAneesh Kumar K.V #define ATTR_MASK    127
619c797b6c6SAneesh Kumar K.V 
620c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev,
621af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
6221c7850f9SSasha Levin {
623c797b6c6SAneesh Kumar K.V 	int ret = 0;
624bfc15268SAneesh Kumar K.V 	u32 fid_val;
625bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
626c797b6c6SAneesh Kumar K.V 	struct p9_iattr_dotl p9attr;
6271c7850f9SSasha Levin 
628c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr);
62931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
6301c7850f9SSasha Levin 
631c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & ATTR_MODE) {
632c797b6c6SAneesh Kumar K.V 		ret = chmod(fid->abs_path, p9attr.mode);
633c797b6c6SAneesh Kumar K.V 		if (ret < 0)
634c797b6c6SAneesh Kumar K.V 			goto err_out;
635c797b6c6SAneesh Kumar K.V 	}
636c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) {
637c797b6c6SAneesh Kumar K.V 		struct timespec times[2];
638c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_ATIME) {
639c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_ATIME_SET) {
640c797b6c6SAneesh Kumar K.V 				times[0].tv_sec = p9attr.atime_sec;
641c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = p9attr.atime_nsec;
642c797b6c6SAneesh Kumar K.V 			} else {
643c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = UTIME_NOW;
644c797b6c6SAneesh Kumar K.V 			}
645c797b6c6SAneesh Kumar K.V 		} else {
646c797b6c6SAneesh Kumar K.V 			times[0].tv_nsec = UTIME_OMIT;
647c797b6c6SAneesh Kumar K.V 		}
648c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_MTIME) {
649c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_MTIME_SET) {
650c797b6c6SAneesh Kumar K.V 				times[1].tv_sec = p9attr.mtime_sec;
651c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = p9attr.mtime_nsec;
652c797b6c6SAneesh Kumar K.V 			} else {
653c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = UTIME_NOW;
654c797b6c6SAneesh Kumar K.V 			}
655c797b6c6SAneesh Kumar K.V 		} else
656c797b6c6SAneesh Kumar K.V 			times[1].tv_nsec = UTIME_OMIT;
657c797b6c6SAneesh Kumar K.V 
658c797b6c6SAneesh Kumar K.V 		ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW);
659c797b6c6SAneesh Kumar K.V 		if (ret < 0)
660c797b6c6SAneesh Kumar K.V 			goto err_out;
661c797b6c6SAneesh Kumar K.V 	}
662c797b6c6SAneesh Kumar K.V 	/*
663c797b6c6SAneesh Kumar K.V 	 * If the only valid entry in iattr is ctime we can call
664c797b6c6SAneesh Kumar K.V 	 * chown(-1,-1) to update the ctime of the file
665c797b6c6SAneesh Kumar K.V 	 */
666c797b6c6SAneesh Kumar K.V 	if ((p9attr.valid & (ATTR_UID | ATTR_GID)) ||
667c797b6c6SAneesh Kumar K.V 	    ((p9attr.valid & ATTR_CTIME)
668c797b6c6SAneesh Kumar K.V 	     && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
669c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_UID))
670c797b6c6SAneesh Kumar K.V 			p9attr.uid = -1;
671c797b6c6SAneesh Kumar K.V 
672c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_GID))
673c797b6c6SAneesh Kumar K.V 			p9attr.gid = -1;
674c797b6c6SAneesh Kumar K.V 
675c797b6c6SAneesh Kumar K.V 		ret = lchown(fid->abs_path, p9attr.uid, p9attr.gid);
676c797b6c6SAneesh Kumar K.V 		if (ret < 0)
677c797b6c6SAneesh Kumar K.V 			goto err_out;
678c797b6c6SAneesh Kumar K.V 	}
679c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_SIZE)) {
680c797b6c6SAneesh Kumar K.V 		ret = truncate(fid->abs_path, p9attr.size);
681c797b6c6SAneesh Kumar K.V 		if (ret < 0)
682c797b6c6SAneesh Kumar K.V 			goto err_out;
683c797b6c6SAneesh Kumar K.V 	}
6845529bcd7SAsias He 	*outlen = VIRTIO_9P_HDR_LEN;
685bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
686ead43b01SAneesh Kumar K.V 	return;
6874bc9734aSAneesh Kumar K.V err_out:
6884bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
6894bc9734aSAneesh Kumar K.V 	return;
6901c7850f9SSasha Levin }
6911c7850f9SSasha Levin 
692ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev,
693af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
6941c7850f9SSasha Levin {
6954bc9734aSAneesh Kumar K.V 
696bfc15268SAneesh Kumar K.V 	u64 offset;
697bfc15268SAneesh Kumar K.V 	u32 fid_val;
6984bc9734aSAneesh Kumar K.V 	u32 count;
6994bc9734aSAneesh Kumar K.V 	ssize_t res;
70050c479e0SAneesh Kumar K.V 	u16 iov_cnt;
70150c479e0SAneesh Kumar K.V 	void *iov_base;
70250c479e0SAneesh Kumar K.V 	size_t iov_len;
703bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
704b064b05aSAneesh Kumar K.V 	/* u32 fid + u64 offset + u32 count */
705b064b05aSAneesh Kumar K.V 	int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32);
7061c7850f9SSasha Levin 
707bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
70831a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
709af045e53SAneesh Kumar K.V 
71050c479e0SAneesh Kumar K.V 	iov_base = pdu->out_iov[0].iov_base;
71150c479e0SAneesh Kumar K.V 	iov_len  = pdu->out_iov[0].iov_len;
71250c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->out_iov_cnt;
71350c479e0SAneesh Kumar K.V 
714bfc15268SAneesh Kumar K.V 	/* Adjust the iovec to skip the header and meta data */
715b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size);
716b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_len -=  (sizeof(struct p9_msg) + twrite_size);
717bfc15268SAneesh Kumar K.V 	pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count,
7186b163a87SAneesh Kumar K.V 						    pdu->out_iov_cnt);
7194bc9734aSAneesh Kumar K.V 	res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset);
72050c479e0SAneesh Kumar K.V 	/*
72150c479e0SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
72250c479e0SAneesh Kumar K.V 	 * pdu_readf works correctly.
72350c479e0SAneesh Kumar K.V 	 */
72450c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_base = iov_base;
72550c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_len  = iov_len;
72650c479e0SAneesh Kumar K.V 	pdu->out_iov_cnt         = iov_cnt;
727c797b6c6SAneesh Kumar K.V 
7284bc9734aSAneesh Kumar K.V 	if (res < 0)
7294bc9734aSAneesh Kumar K.V 		goto err_out;
7304bc9734aSAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", res);
731bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
732bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
733ead43b01SAneesh Kumar K.V 	return;
7344bc9734aSAneesh Kumar K.V err_out:
7354bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
7364bc9734aSAneesh Kumar K.V 	return;
7371c7850f9SSasha Levin }
7381c7850f9SSasha Levin 
7396fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev,
7406fc5cd9bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
7416fc5cd9bSSasha Levin {
7426fc5cd9bSSasha Levin 	int ret;
7436fc5cd9bSSasha Levin 	u32 fid_val;
7446fc5cd9bSSasha Levin 	struct p9_fid *fid;
7456fc5cd9bSSasha Levin 
7466fc5cd9bSSasha Levin 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
74731a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
7486fc5cd9bSSasha Levin 
7499b604a9cSSasha Levin 	ret = remove(fid->abs_path);
7506fc5cd9bSSasha Levin 	if (ret < 0)
7516fc5cd9bSSasha Levin 		goto err_out;
7526fc5cd9bSSasha Levin 	*outlen = pdu->write_offset;
7536fc5cd9bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
7546fc5cd9bSSasha Levin 	return;
7556fc5cd9bSSasha Levin 
7566fc5cd9bSSasha Levin err_out:
7576fc5cd9bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
7586fc5cd9bSSasha Levin 	return;
7596fc5cd9bSSasha Levin }
7606fc5cd9bSSasha Levin 
761f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev,
762f161f28bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
763f161f28bSSasha Levin {
764f161f28bSSasha Levin 	int ret;
765f161f28bSSasha Levin 	u32 fid_val, new_fid_val;
766f161f28bSSasha Levin 	struct p9_fid *fid, *new_fid;
767f161f28bSSasha Levin 	char full_path[PATH_MAX], *new_name;
768f161f28bSSasha Levin 
769f161f28bSSasha Levin 	virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name);
77031a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
77131a6fb8dSSasha Levin 	new_fid = get_fid(p9dev, new_fid_val);
772f161f28bSSasha Levin 
773f161f28bSSasha Levin 	sprintf(full_path, "%s/%s", new_fid->abs_path, new_name);
774f161f28bSSasha Levin 	ret = rename(fid->abs_path, full_path);
775f161f28bSSasha Levin 	if (ret < 0)
776f161f28bSSasha Levin 		goto err_out;
777f161f28bSSasha Levin 	*outlen = pdu->write_offset;
778f161f28bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
779f161f28bSSasha Levin 	return;
780f161f28bSSasha Levin 
781f161f28bSSasha Levin err_out:
782f161f28bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
783f161f28bSSasha Levin 	return;
784f161f28bSSasha Levin }
785f161f28bSSasha Levin 
786c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev,
787c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
788c797b6c6SAneesh Kumar K.V {
789c797b6c6SAneesh Kumar K.V 	int ret;
790c797b6c6SAneesh Kumar K.V 	u32 fid_val;
791c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
792c797b6c6SAneesh Kumar K.V 	char target_path[PATH_MAX];
793c797b6c6SAneesh Kumar K.V 
794c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
79531a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
796c797b6c6SAneesh Kumar K.V 
797c797b6c6SAneesh Kumar K.V 	memset(target_path, 0, PATH_MAX);
798c797b6c6SAneesh Kumar K.V 	ret = readlink(fid->abs_path, target_path, PATH_MAX - 1);
799c797b6c6SAneesh Kumar K.V 	if (ret < 0)
800c797b6c6SAneesh Kumar K.V 		goto err_out;
801c797b6c6SAneesh Kumar K.V 
802c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "s", target_path);
803c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
804c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
805c797b6c6SAneesh Kumar K.V 	return;
806c797b6c6SAneesh Kumar K.V err_out:
807c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
808c797b6c6SAneesh Kumar K.V 	return;
809c797b6c6SAneesh Kumar K.V }
810c797b6c6SAneesh Kumar K.V 
811c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev,
812c797b6c6SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
813c797b6c6SAneesh Kumar K.V {
814c797b6c6SAneesh Kumar K.V 	int ret;
815c797b6c6SAneesh Kumar K.V 	u64 fsid;
816c797b6c6SAneesh Kumar K.V 	u32 fid_val;
817c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
818c797b6c6SAneesh Kumar K.V 	struct statfs stat_buf;
819c797b6c6SAneesh Kumar K.V 
820c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
82131a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
822c797b6c6SAneesh Kumar K.V 
823c797b6c6SAneesh Kumar K.V 	ret = statfs(fid->abs_path, &stat_buf);
824c797b6c6SAneesh Kumar K.V 	if (ret < 0)
825c797b6c6SAneesh Kumar K.V 		goto err_out;
826c797b6c6SAneesh Kumar K.V 	/* FIXME!! f_blocks needs update based on client msize */
827c797b6c6SAneesh Kumar K.V 	fsid = (unsigned int) stat_buf.f_fsid.__val[0] |
828c797b6c6SAneesh Kumar K.V 		(unsigned long long)stat_buf.f_fsid.__val[1] << 32;
829c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type,
830c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bsize, stat_buf.f_blocks,
831c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bfree, stat_buf.f_bavail,
832c797b6c6SAneesh Kumar K.V 			     stat_buf.f_files, stat_buf.f_ffree,
833c797b6c6SAneesh Kumar K.V 			     fsid, stat_buf.f_namelen);
834c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
835c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
836c797b6c6SAneesh Kumar K.V 	return;
837c797b6c6SAneesh Kumar K.V err_out:
838c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
839c797b6c6SAneesh Kumar K.V 	return;
840c797b6c6SAneesh Kumar K.V }
841c797b6c6SAneesh Kumar K.V 
842c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev,
843c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
844c797b6c6SAneesh Kumar K.V {
845c797b6c6SAneesh Kumar K.V 	int ret;
846c797b6c6SAneesh Kumar K.V 	char *name;
847c797b6c6SAneesh Kumar K.V 	struct stat st;
848c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
849c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
850c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
851c797b6c6SAneesh Kumar K.V 	u32 fid_val, mode, major, minor, gid;
852c797b6c6SAneesh Kumar K.V 
853c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode,
854c797b6c6SAneesh Kumar K.V 			    &major, &minor, &gid);
855c797b6c6SAneesh Kumar K.V 
85631a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
857c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
858c797b6c6SAneesh Kumar K.V 	ret = mknod(full_path, mode, makedev(major, minor));
859c797b6c6SAneesh Kumar K.V 	if (ret < 0)
860c797b6c6SAneesh Kumar K.V 		goto err_out;
861c797b6c6SAneesh Kumar K.V 
862c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
863c797b6c6SAneesh Kumar K.V 		goto err_out;
864c797b6c6SAneesh Kumar K.V 
865c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
866c797b6c6SAneesh Kumar K.V 	if (ret < 0)
867c797b6c6SAneesh Kumar K.V 		goto err_out;
868c797b6c6SAneesh Kumar K.V 
869c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
870c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
871c797b6c6SAneesh Kumar K.V 	free(name);
872c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
873c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
874c797b6c6SAneesh Kumar K.V 	return;
875c797b6c6SAneesh Kumar K.V err_out:
876c797b6c6SAneesh Kumar K.V 	free(name);
877c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
878c797b6c6SAneesh Kumar K.V 	return;
879c797b6c6SAneesh Kumar K.V }
880c797b6c6SAneesh Kumar K.V 
881c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev,
882c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
883c797b6c6SAneesh Kumar K.V {
884c797b6c6SAneesh Kumar K.V 	int ret;
885c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
886c797b6c6SAneesh Kumar K.V 	u32 fid_val, datasync;
887c797b6c6SAneesh Kumar K.V 
888c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync);
88931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
890c797b6c6SAneesh Kumar K.V 
891c797b6c6SAneesh Kumar K.V 	if (datasync)
892c797b6c6SAneesh Kumar K.V 		ret = fdatasync(fid->fd);
893c797b6c6SAneesh Kumar K.V 	else
894c797b6c6SAneesh Kumar K.V 		ret = fsync(fid->fd);
895c797b6c6SAneesh Kumar K.V 	if (ret < 0)
896c797b6c6SAneesh Kumar K.V 		goto err_out;
897c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
898c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
899c797b6c6SAneesh Kumar K.V 	return;
900c797b6c6SAneesh Kumar K.V err_out:
901c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
902c797b6c6SAneesh Kumar K.V 	return;
903c797b6c6SAneesh Kumar K.V }
904c797b6c6SAneesh Kumar K.V 
905c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev,
906c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
907c797b6c6SAneesh Kumar K.V {
908c797b6c6SAneesh Kumar K.V 	int ret;
909c797b6c6SAneesh Kumar K.V 	struct stat st;
910c797b6c6SAneesh Kumar K.V 	u32 fid_val, gid;
911c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
912c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
913c797b6c6SAneesh Kumar K.V 	char new_name[PATH_MAX];
914c797b6c6SAneesh Kumar K.V 	char *old_path, *name;
915c797b6c6SAneesh Kumar K.V 
916c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid);
917c797b6c6SAneesh Kumar K.V 
91831a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
919c797b6c6SAneesh Kumar K.V 	sprintf(new_name, "%s/%s", dfid->abs_path, name);
920c797b6c6SAneesh Kumar K.V 	ret = symlink(old_path, new_name);
921c797b6c6SAneesh Kumar K.V 	if (ret < 0)
922c797b6c6SAneesh Kumar K.V 		goto err_out;
923c797b6c6SAneesh Kumar K.V 
924c797b6c6SAneesh Kumar K.V 	if (lstat(new_name, &st) < 0)
925c797b6c6SAneesh Kumar K.V 		goto err_out;
926c797b6c6SAneesh Kumar K.V 
927c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
928c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
929c797b6c6SAneesh Kumar K.V 	free(name);
930c797b6c6SAneesh Kumar K.V 	free(old_path);
931c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
932c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
933c797b6c6SAneesh Kumar K.V 	return;
934c797b6c6SAneesh Kumar K.V err_out:
935c797b6c6SAneesh Kumar K.V 	free(name);
936c797b6c6SAneesh Kumar K.V 	free(old_path);
937c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
938c797b6c6SAneesh Kumar K.V 	return;
939c797b6c6SAneesh Kumar K.V }
940c797b6c6SAneesh Kumar K.V 
941c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev,
942c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
943c797b6c6SAneesh Kumar K.V {
944c797b6c6SAneesh Kumar K.V 	int ret;
945c797b6c6SAneesh Kumar K.V 	char *name;
946c797b6c6SAneesh Kumar K.V 	u32 fid_val, dfid_val;
947c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid, *fid;
948c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
949c797b6c6SAneesh Kumar K.V 
950c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name);
951c797b6c6SAneesh Kumar K.V 
95231a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
95331a6fb8dSSasha Levin 	fid =  get_fid(p9dev, fid_val);
954c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
955c797b6c6SAneesh Kumar K.V 	ret = link(fid->abs_path, full_path);
956c797b6c6SAneesh Kumar K.V 	if (ret < 0)
957c797b6c6SAneesh Kumar K.V 		goto err_out;
958c797b6c6SAneesh Kumar K.V 	free(name);
959c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
960c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
961c797b6c6SAneesh Kumar K.V 	return;
962c797b6c6SAneesh Kumar K.V err_out:
963c797b6c6SAneesh Kumar K.V 	free(name);
964c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
965c797b6c6SAneesh Kumar K.V 	return;
966c797b6c6SAneesh Kumar K.V 
967c797b6c6SAneesh Kumar K.V }
968c797b6c6SAneesh Kumar K.V 
969c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev,
970c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
971c797b6c6SAneesh Kumar K.V {
972c797b6c6SAneesh Kumar K.V 	u8 ret;
973c797b6c6SAneesh Kumar K.V 	u32 fid_val;
974c797b6c6SAneesh Kumar K.V 	struct p9_flock flock;
975c797b6c6SAneesh Kumar K.V 
976c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type,
977c797b6c6SAneesh Kumar K.V 			    &flock.flags, &flock.start, &flock.length,
978c797b6c6SAneesh Kumar K.V 			    &flock.proc_id, &flock.client_id);
979c797b6c6SAneesh Kumar K.V 
980c797b6c6SAneesh Kumar K.V 	/* Just return success */
981c797b6c6SAneesh Kumar K.V 	ret = P9_LOCK_SUCCESS;
982c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", ret);
983c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
984c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
985c797b6c6SAneesh Kumar K.V 	free(flock.client_id);
986c797b6c6SAneesh Kumar K.V 	return;
987c797b6c6SAneesh Kumar K.V }
988c797b6c6SAneesh Kumar K.V 
989c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev,
990c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
991c797b6c6SAneesh Kumar K.V {
992c797b6c6SAneesh Kumar K.V 	u32 fid_val;
993c797b6c6SAneesh Kumar K.V 	struct p9_getlock glock;
994c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type,
995c797b6c6SAneesh Kumar K.V 			    &glock.start, &glock.length, &glock.proc_id,
996c797b6c6SAneesh Kumar K.V 			    &glock.client_id);
997c797b6c6SAneesh Kumar K.V 
998c797b6c6SAneesh Kumar K.V 	/* Just return success */
999c797b6c6SAneesh Kumar K.V 	glock.type = F_UNLCK;
1000c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "bqqds", glock.type,
1001c797b6c6SAneesh Kumar K.V 			     glock.start, glock.length, glock.proc_id,
1002c797b6c6SAneesh Kumar K.V 			     glock.client_id);
1003c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1004c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1005c797b6c6SAneesh Kumar K.V 	free(glock.client_id);
1006c797b6c6SAneesh Kumar K.V 	return;
1007c797b6c6SAneesh Kumar K.V }
1008c797b6c6SAneesh Kumar K.V 
1009c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor)
1010c797b6c6SAneesh Kumar K.V {
1011c797b6c6SAneesh Kumar K.V 	int size = strlen(ancestor);
1012c797b6c6SAneesh Kumar K.V 	if (!strncmp(path, ancestor, size)) {
1013c797b6c6SAneesh Kumar K.V 		/*
1014c797b6c6SAneesh Kumar K.V 		 * Now check whether ancestor is a full name or
1015c797b6c6SAneesh Kumar K.V 		 * or directory component and not just part
1016c797b6c6SAneesh Kumar K.V 		 * of a name.
1017c797b6c6SAneesh Kumar K.V 		 */
1018c797b6c6SAneesh Kumar K.V 		if (path[size] == '\0' || path[size] == '/')
1019c797b6c6SAneesh Kumar K.V 			return 1;
1020c797b6c6SAneesh Kumar K.V 	}
1021c797b6c6SAneesh Kumar K.V 	return 0;
1022c797b6c6SAneesh Kumar K.V }
1023c797b6c6SAneesh Kumar K.V 
1024c797b6c6SAneesh Kumar K.V static void virtio_p9_fix_path(char *fid_path, char *old_name, char *new_name)
1025c797b6c6SAneesh Kumar K.V {
1026c797b6c6SAneesh Kumar K.V 	char tmp_name[PATH_MAX];
1027c797b6c6SAneesh Kumar K.V 	size_t rp_sz = strlen(old_name);
1028c797b6c6SAneesh Kumar K.V 
1029c797b6c6SAneesh Kumar K.V 	if (rp_sz == strlen(fid_path)) {
1030c797b6c6SAneesh Kumar K.V 		/* replace the full name */
1031c797b6c6SAneesh Kumar K.V 		strcpy(fid_path, new_name);
1032c797b6c6SAneesh Kumar K.V 		return;
1033c797b6c6SAneesh Kumar K.V 	}
1034c797b6c6SAneesh Kumar K.V 	/* save the trailing path details */
1035c797b6c6SAneesh Kumar K.V 	strcpy(tmp_name, fid_path + rp_sz);
1036c797b6c6SAneesh Kumar K.V 	sprintf(fid_path, "%s%s", new_name, tmp_name);
1037c797b6c6SAneesh Kumar K.V 	return;
1038c797b6c6SAneesh Kumar K.V }
1039c797b6c6SAneesh Kumar K.V 
1040e2341580SSasha Levin static void rename_fids(struct p9_dev *p9dev, char *old_name, char *new_name)
1041e2341580SSasha Levin {
1042e2341580SSasha Levin 	struct rb_node *node = rb_first(&p9dev->fids);
1043e2341580SSasha Levin 
1044e2341580SSasha Levin 	while (node) {
1045e2341580SSasha Levin 		struct p9_fid *fid = rb_entry(node, struct p9_fid, node);
1046e2341580SSasha Levin 
1047e2341580SSasha Levin 		if (fid->fid != P9_NOFID && virtio_p9_ancestor(fid->path, old_name)) {
1048e2341580SSasha Levin 				virtio_p9_fix_path(fid->path, old_name, new_name);
1049e2341580SSasha Levin 		}
1050e2341580SSasha Levin 		node = rb_next(node);
1051e2341580SSasha Levin 	}
1052e2341580SSasha Levin }
1053e2341580SSasha Levin 
1054c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev,
1055c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1056c797b6c6SAneesh Kumar K.V {
1057e2341580SSasha Levin 	int ret;
1058c797b6c6SAneesh Kumar K.V 	char *old_name, *new_name;
1059c797b6c6SAneesh Kumar K.V 	u32 old_dfid_val, new_dfid_val;
1060c797b6c6SAneesh Kumar K.V 	struct p9_fid *old_dfid, *new_dfid;
1061c797b6c6SAneesh Kumar K.V 	char old_full_path[PATH_MAX], new_full_path[PATH_MAX];
1062c797b6c6SAneesh Kumar K.V 
1063c797b6c6SAneesh Kumar K.V 
1064c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name,
1065c797b6c6SAneesh Kumar K.V 			    &new_dfid_val, &new_name);
1066c797b6c6SAneesh Kumar K.V 
106731a6fb8dSSasha Levin 	old_dfid = get_fid(p9dev, old_dfid_val);
106831a6fb8dSSasha Levin 	new_dfid = get_fid(p9dev, new_dfid_val);
1069c797b6c6SAneesh Kumar K.V 
1070c797b6c6SAneesh Kumar K.V 	sprintf(old_full_path, "%s/%s", old_dfid->abs_path, old_name);
1071c797b6c6SAneesh Kumar K.V 	sprintf(new_full_path, "%s/%s", new_dfid->abs_path, new_name);
1072c797b6c6SAneesh Kumar K.V 	ret = rename(old_full_path, new_full_path);
1073c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1074c797b6c6SAneesh Kumar K.V 		goto err_out;
1075c797b6c6SAneesh Kumar K.V 	/*
1076c797b6c6SAneesh Kumar K.V 	 * Now fix path in other fids, if the renamed path is part of
1077c797b6c6SAneesh Kumar K.V 	 * that.
1078c797b6c6SAneesh Kumar K.V 	 */
1079e2341580SSasha Levin 	rename_fids(p9dev, old_name, new_name);
1080c797b6c6SAneesh Kumar K.V 	free(old_name);
1081c797b6c6SAneesh Kumar K.V 	free(new_name);
1082c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1083c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1084c797b6c6SAneesh Kumar K.V 	return;
1085c797b6c6SAneesh Kumar K.V err_out:
1086c797b6c6SAneesh Kumar K.V 	free(old_name);
1087c797b6c6SAneesh Kumar K.V 	free(new_name);
1088c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1089c797b6c6SAneesh Kumar K.V 	return;
1090c797b6c6SAneesh Kumar K.V }
1091c797b6c6SAneesh Kumar K.V 
1092c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev,
1093c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1094c797b6c6SAneesh Kumar K.V {
1095c797b6c6SAneesh Kumar K.V 	int ret;
1096c797b6c6SAneesh Kumar K.V 	char *name;
1097c797b6c6SAneesh Kumar K.V 	u32 fid_val, flags;
1098c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
1099c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
1100c797b6c6SAneesh Kumar K.V 
1101c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags);
110231a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
1103c797b6c6SAneesh Kumar K.V 
1104c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", fid->abs_path, name);
1105c797b6c6SAneesh Kumar K.V 	ret = remove(full_path);
1106c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1107c797b6c6SAneesh Kumar K.V 		goto err_out;
1108c797b6c6SAneesh Kumar K.V 	free(name);
1109c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1110c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1111c797b6c6SAneesh Kumar K.V 	return;
1112c797b6c6SAneesh Kumar K.V err_out:
1113c797b6c6SAneesh Kumar K.V 	free(name);
1114c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1115c797b6c6SAneesh Kumar K.V 	return;
1116c797b6c6SAneesh Kumar K.V }
1117c797b6c6SAneesh Kumar K.V 
11185cc808aaSSasha Levin static void virtio_p9_flush(struct p9_dev *p9dev,
11195cc808aaSSasha Levin 				struct p9_pdu *pdu, u32 *outlen)
11205cc808aaSSasha Levin {
11215cc808aaSSasha Levin 	u16 tag, oldtag;
11225cc808aaSSasha Levin 
11235cc808aaSSasha Levin 	virtio_p9_pdu_readf(pdu, "ww", &tag, &oldtag);
11245cc808aaSSasha Levin 	virtio_p9_pdu_writef(pdu, "w", tag);
11255cc808aaSSasha Levin 	*outlen = pdu->write_offset;
11265cc808aaSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
11275cc808aaSSasha Levin 
11285cc808aaSSasha Levin 	return;
11295cc808aaSSasha Levin }
11305cc808aaSSasha Levin 
1131c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev,
1132c797b6c6SAneesh Kumar K.V 				 struct p9_pdu *pdu, u32 *outlen)
1133c797b6c6SAneesh Kumar K.V {
1134c797b6c6SAneesh Kumar K.V 	return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen);
1135c797b6c6SAneesh Kumar K.V }
1136c797b6c6SAneesh Kumar K.V 
1137ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev,
1138af045e53SAneesh Kumar K.V 			struct p9_pdu *pdu, u32 *outlen);
1139b4422bf3SAneesh Kumar K.V 
1140c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */
1141c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74
1142c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76
1143c797b6c6SAneesh Kumar K.V 
1144c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = {
1145c797b6c6SAneesh Kumar K.V 	[P9_TREADDIR]     = virtio_p9_readdir,
1146c797b6c6SAneesh Kumar K.V 	[P9_TSTATFS]      = virtio_p9_statfs,
1147c797b6c6SAneesh Kumar K.V 	[P9_TGETATTR]     = virtio_p9_getattr,
1148c797b6c6SAneesh Kumar K.V 	[P9_TSETATTR]     = virtio_p9_setattr,
1149c797b6c6SAneesh Kumar K.V 	[P9_TXATTRWALK]   = virtio_p9_eopnotsupp,
1150c797b6c6SAneesh Kumar K.V 	[P9_TXATTRCREATE] = virtio_p9_eopnotsupp,
1151c797b6c6SAneesh Kumar K.V 	[P9_TMKNOD]       = virtio_p9_mknod,
1152c797b6c6SAneesh Kumar K.V 	[P9_TLOCK]        = virtio_p9_lock,
1153c797b6c6SAneesh Kumar K.V 	[P9_TGETLOCK]     = virtio_p9_getlock,
1154c797b6c6SAneesh Kumar K.V 	[P9_TRENAMEAT]    = virtio_p9_renameat,
1155c797b6c6SAneesh Kumar K.V 	[P9_TREADLINK]    = virtio_p9_readlink,
1156c797b6c6SAneesh Kumar K.V 	[P9_TUNLINKAT]    = virtio_p9_unlinkat,
1157c797b6c6SAneesh Kumar K.V 	[P9_TMKDIR]       = virtio_p9_mkdir,
1158b4422bf3SAneesh Kumar K.V 	[P9_TVERSION]     = virtio_p9_version,
1159c797b6c6SAneesh Kumar K.V 	[P9_TLOPEN]       = virtio_p9_open,
1160b4422bf3SAneesh Kumar K.V 	[P9_TATTACH]      = virtio_p9_attach,
1161b4422bf3SAneesh Kumar K.V 	[P9_TWALK]        = virtio_p9_walk,
1162c797b6c6SAneesh Kumar K.V 	[P9_TCLUNK]       = virtio_p9_clunk,
1163c797b6c6SAneesh Kumar K.V 	[P9_TFSYNC]       = virtio_p9_fsync,
1164b4422bf3SAneesh Kumar K.V 	[P9_TREAD]        = virtio_p9_read,
11655cc808aaSSasha Levin 	[P9_TFLUSH]       = virtio_p9_flush,
1166c797b6c6SAneesh Kumar K.V 	[P9_TLINK]        = virtio_p9_link,
1167c797b6c6SAneesh Kumar K.V 	[P9_TSYMLINK]     = virtio_p9_symlink,
1168c797b6c6SAneesh Kumar K.V 	[P9_TLCREATE]     = virtio_p9_create,
1169b4422bf3SAneesh Kumar K.V 	[P9_TWRITE]       = virtio_p9_write,
11706fc5cd9bSSasha Levin 	[P9_TREMOVE]      = virtio_p9_remove,
1171f161f28bSSasha Levin 	[P9_TRENAME]      = virtio_p9_rename,
1172b4422bf3SAneesh Kumar K.V };
1173b4422bf3SAneesh Kumar K.V 
1174af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq)
1175af045e53SAneesh Kumar K.V {
1176af045e53SAneesh Kumar K.V 	struct p9_pdu *pdu = calloc(1, sizeof(*pdu));
1177af045e53SAneesh Kumar K.V 	if (!pdu)
1178af045e53SAneesh Kumar K.V 		return NULL;
1179af045e53SAneesh Kumar K.V 
1180bfc15268SAneesh Kumar K.V 	/* skip the pdu header p9_msg */
11815529bcd7SAsias He 	pdu->read_offset	= VIRTIO_9P_HDR_LEN;
11825529bcd7SAsias He 	pdu->write_offset	= VIRTIO_9P_HDR_LEN;
1183af045e53SAneesh Kumar K.V 	pdu->queue_head		= virt_queue__get_inout_iov(kvm, vq, pdu->in_iov,
1184a8a44649SAsias He 					pdu->out_iov, &pdu->in_iov_cnt, &pdu->out_iov_cnt);
1185af045e53SAneesh Kumar K.V 	return pdu;
1186af045e53SAneesh Kumar K.V }
1187af045e53SAneesh Kumar K.V 
1188af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu)
1189af045e53SAneesh Kumar K.V {
1190af045e53SAneesh Kumar K.V 	struct p9_msg *msg;
1191af045e53SAneesh Kumar K.V 	/*
1192af045e53SAneesh Kumar K.V 	 * we can peek directly into pdu for a u8
1193af045e53SAneesh Kumar K.V 	 * value. The host endianess won't be an issue
1194af045e53SAneesh Kumar K.V 	 */
1195af045e53SAneesh Kumar K.V 	msg = pdu->out_iov[0].iov_base;
1196af045e53SAneesh Kumar K.V 	return msg->cmd;
1197af045e53SAneesh Kumar K.V }
1198af045e53SAneesh Kumar K.V 
1199b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job)
12001c7850f9SSasha Levin {
1201af045e53SAneesh Kumar K.V 	u8 cmd;
1202b4422bf3SAneesh Kumar K.V 	u32 len = 0;
1203b4422bf3SAneesh Kumar K.V 	p9_handler *handler;
1204b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev;
1205af045e53SAneesh Kumar K.V 	struct virt_queue *vq;
1206af045e53SAneesh Kumar K.V 	struct p9_pdu *p9pdu;
12071c7850f9SSasha Levin 
1208b4422bf3SAneesh Kumar K.V 	vq = job->vq;
1209b4422bf3SAneesh Kumar K.V 	p9dev = job->p9dev;
12101c7850f9SSasha Levin 
1211af045e53SAneesh Kumar K.V 	p9pdu = virtio_p9_pdu_init(kvm, vq);
1212af045e53SAneesh Kumar K.V 	cmd = virtio_p9_get_cmd(p9pdu);
1213af045e53SAneesh Kumar K.V 
1214c797b6c6SAneesh Kumar K.V 	if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) ||
1215c797b6c6SAneesh Kumar K.V 	    !virtio_9p_dotl_handler[cmd])
121697b408afSAneesh Kumar K.V 		handler = virtio_p9_eopnotsupp;
1217dd78d9eaSAneesh Kumar K.V 	else
1218c797b6c6SAneesh Kumar K.V 		handler = virtio_9p_dotl_handler[cmd];
1219c797b6c6SAneesh Kumar K.V 
1220af045e53SAneesh Kumar K.V 	handler(p9dev, p9pdu, &len);
1221af045e53SAneesh Kumar K.V 	virt_queue__set_used_elem(vq, p9pdu->queue_head, len);
1222af045e53SAneesh Kumar K.V 	free(p9pdu);
12231c7850f9SSasha Levin 	return true;
12241c7850f9SSasha Levin }
12251c7850f9SSasha Levin 
12261c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param)
12271c7850f9SSasha Levin {
1228b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job = (struct p9_dev_job *)param;
1229b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev   = job->p9dev;
1230b4422bf3SAneesh Kumar K.V 	struct virt_queue *vq  = job->vq;
12311c7850f9SSasha Levin 
12321c7850f9SSasha Levin 	while (virt_queue__available(vq)) {
1233b4422bf3SAneesh Kumar K.V 		virtio_p9_do_io_request(kvm, job);
123402eca50cSAsias He 		p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs);
12351c7850f9SSasha Levin 	}
12361c7850f9SSasha Levin }
12371c7850f9SSasha Levin 
1238c5ae742bSSasha Levin static u8 *get_config(struct kvm *kvm, void *dev)
12391c7850f9SSasha Levin {
1240c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
12411c7850f9SSasha Levin 
1242c5ae742bSSasha Levin 	return ((u8 *)(p9dev->config));
1243c7838fbdSSasha Levin }
1244c7838fbdSSasha Levin 
1245c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev)
1246c7838fbdSSasha Levin {
1247c7838fbdSSasha Levin 	return 1 << VIRTIO_9P_MOUNT_TAG;
1248c7838fbdSSasha Levin }
1249c7838fbdSSasha Levin 
1250c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
1251c7838fbdSSasha Levin {
1252c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1253c7838fbdSSasha Levin 
1254c7838fbdSSasha Levin 	p9dev->features = features;
1255c7838fbdSSasha Levin }
1256c7838fbdSSasha Levin 
1257c7838fbdSSasha Levin static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn)
1258c7838fbdSSasha Levin {
1259c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1260b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job;
1261b4422bf3SAneesh Kumar K.V 	struct virt_queue *queue;
1262c7838fbdSSasha Levin 	void *p;
12631c7850f9SSasha Levin 
1264312c62d1SSasha Levin 	compat__remove_message(compat_id);
1265e59662b3SSasha Levin 
1266c7838fbdSSasha Levin 	queue		= &p9dev->vqs[vq];
1267c7838fbdSSasha Levin 	queue->pfn	= pfn;
12681c7850f9SSasha Levin 	p		= guest_pfn_to_host(kvm, queue->pfn);
1269c7838fbdSSasha Levin 	job		= &p9dev->jobs[vq];
12701c7850f9SSasha Levin 
1271c7838fbdSSasha Levin 	vring_init(&queue->vring, VIRTQUEUE_NUM, p, VIRTIO_PCI_VRING_ALIGN);
12721c7850f9SSasha Levin 
1273b4422bf3SAneesh Kumar K.V 	*job		= (struct p9_dev_job) {
1274b4422bf3SAneesh Kumar K.V 		.vq		= queue,
1275b4422bf3SAneesh Kumar K.V 		.p9dev		= p9dev,
1276b4422bf3SAneesh Kumar K.V 	};
1277df0c7f57SSasha Levin 	thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job);
127860eb42d5SSasha Levin 
1279c7838fbdSSasha Levin 	return 0;
12801c7850f9SSasha Levin }
12811c7850f9SSasha Levin 
1282c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq)
1283c7838fbdSSasha Levin {
1284c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
12851c7850f9SSasha Levin 
1286c7838fbdSSasha Levin 	thread_pool__do_job(&p9dev->jobs[vq].job_id);
1287c7838fbdSSasha Levin 
1288c7838fbdSSasha Levin 	return 0;
1289c7838fbdSSasha Levin }
1290c7838fbdSSasha Levin 
1291c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq)
1292c7838fbdSSasha Levin {
1293c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1294c7838fbdSSasha Levin 
1295c7838fbdSSasha Levin 	return p9dev->vqs[vq].pfn;
1296c7838fbdSSasha Levin }
1297c7838fbdSSasha Levin 
1298c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
1299c7838fbdSSasha Levin {
1300c7838fbdSSasha Levin 	return VIRTQUEUE_NUM;
1301c7838fbdSSasha Levin }
1302c7838fbdSSasha Levin 
13037aba29c1SWill Deacon static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size)
13047aba29c1SWill Deacon {
13057aba29c1SWill Deacon 	/* FIXME: dynamic */
13067aba29c1SWill Deacon 	return size;
13077aba29c1SWill Deacon }
13087aba29c1SWill Deacon 
13091c47ce69SSasha Levin struct virtio_ops p9_dev_virtio_ops = (struct virtio_ops) {
1310c7838fbdSSasha Levin 	.get_config		= get_config,
1311c7838fbdSSasha Levin 	.get_host_features	= get_host_features,
1312c7838fbdSSasha Levin 	.set_guest_features	= set_guest_features,
1313c7838fbdSSasha Levin 	.init_vq		= init_vq,
1314c7838fbdSSasha Levin 	.notify_vq		= notify_vq,
1315c7838fbdSSasha Levin 	.get_pfn_vq		= get_pfn_vq,
1316c7838fbdSSasha Levin 	.get_size_vq		= get_size_vq,
13177aba29c1SWill Deacon 	.set_size_vq		= set_size_vq,
1318c7838fbdSSasha Levin };
13191c47ce69SSasha Levin 
1320cac9e8fdSSasha Levin int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
1321cac9e8fdSSasha Levin {
1322cac9e8fdSSasha Levin 	char *tag_name;
1323cac9e8fdSSasha Levin 	char tmp[PATH_MAX];
1324cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1325cac9e8fdSSasha Levin 
1326cac9e8fdSSasha Levin 	/*
1327cac9e8fdSSasha Levin 	 * 9p dir can be of the form dirname,tag_name or
1328cac9e8fdSSasha Levin 	 * just dirname. In the later case we use the
1329cac9e8fdSSasha Levin 	 * default tag name
1330cac9e8fdSSasha Levin 	 */
1331cac9e8fdSSasha Levin 	tag_name = strstr(arg, ",");
1332cac9e8fdSSasha Levin 	if (tag_name) {
1333cac9e8fdSSasha Levin 		*tag_name = '\0';
1334cac9e8fdSSasha Levin 		tag_name++;
1335cac9e8fdSSasha Levin 	}
1336cac9e8fdSSasha Levin 	if (realpath(arg, tmp)) {
1337cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, tmp, tag_name) < 0)
1338cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1339cac9e8fdSSasha Levin 	} else
1340cac9e8fdSSasha Levin 		die("Failed resolving 9p path");
1341cac9e8fdSSasha Levin 	return 0;
1342cac9e8fdSSasha Levin }
1343cac9e8fdSSasha Levin 
1344cac9e8fdSSasha Levin int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset)
1345cac9e8fdSSasha Levin {
1346cac9e8fdSSasha Levin 	char path[PATH_MAX];
1347cac9e8fdSSasha Levin 	struct stat st;
1348cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1349cac9e8fdSSasha Levin 
1350cac9e8fdSSasha Levin 	if (stat(arg, &st) == 0 &&
1351cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1352cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1353cac9e8fdSSasha Levin 
1354cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1355cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1356cac9e8fdSSasha Levin 
1357cac9e8fdSSasha Levin 		if (realpath(arg, tmp) == 0 ||
1358cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1359cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1360cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = 1;
1361cac9e8fdSSasha Levin 		return 0;
1362cac9e8fdSSasha Levin 	}
1363cac9e8fdSSasha Levin 
1364cac9e8fdSSasha Levin 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
1365cac9e8fdSSasha Levin 
1366cac9e8fdSSasha Levin 	if (stat(path, &st) == 0 &&
1367cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1368cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1369cac9e8fdSSasha Levin 
1370cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1371cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1372cac9e8fdSSasha Levin 
1373cac9e8fdSSasha Levin 		if (realpath(path, tmp) == 0 ||
1374cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1375cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1376cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
1377cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1378cac9e8fdSSasha Levin 		kvm_setup_resolv(arg);
1379cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
1380cac9e8fdSSasha Levin 		kvm->cfg.custom_rootfs_name = arg;
1381cac9e8fdSSasha Levin 		return 0;
1382cac9e8fdSSasha Levin 	}
1383cac9e8fdSSasha Levin 
1384cac9e8fdSSasha Levin 	return -1;
1385cac9e8fdSSasha Levin }
1386cac9e8fdSSasha Levin 
13871c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm)
13881c47ce69SSasha Levin {
13891c47ce69SSasha Levin 	struct p9_dev *p9dev;
13901c47ce69SSasha Levin 
13911c47ce69SSasha Levin 	list_for_each_entry(p9dev, &devs, list) {
139202eca50cSAsias He 		virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops,
1393*ae06ce71SWill Deacon 			    VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_9P,
1394*ae06ce71SWill Deacon 			    VIRTIO_ID_9P, PCI_CLASS_9P);
1395c7838fbdSSasha Levin 	}
1396c7838fbdSSasha Levin 
1397c7838fbdSSasha Levin 	return 0;
1398c7838fbdSSasha Levin }
139949a8afd1SSasha Levin virtio_dev_init(virtio_9p__init);
1400c7838fbdSSasha Levin 
1401c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name)
1402c7838fbdSSasha Levin {
1403c7838fbdSSasha Levin 	struct p9_dev *p9dev;
140454f6802dSPekka Enberg 	int err = 0;
14051c7850f9SSasha Levin 
1406b4422bf3SAneesh Kumar K.V 	p9dev = calloc(1, sizeof(*p9dev));
1407b4422bf3SAneesh Kumar K.V 	if (!p9dev)
140854f6802dSPekka Enberg 		return -ENOMEM;
140954f6802dSPekka Enberg 
1410b4422bf3SAneesh Kumar K.V 	if (!tag_name)
14115529bcd7SAsias He 		tag_name = VIRTIO_9P_DEFAULT_TAG;
141254f6802dSPekka Enberg 
1413b4422bf3SAneesh Kumar K.V 	p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1);
141454f6802dSPekka Enberg 	if (p9dev->config == NULL) {
141554f6802dSPekka Enberg 		err = -ENOMEM;
1416b4422bf3SAneesh Kumar K.V 		goto free_p9dev;
141754f6802dSPekka Enberg 	}
14181c7850f9SSasha Levin 
1419b4422bf3SAneesh Kumar K.V 	strcpy(p9dev->root_dir, root);
1420b4422bf3SAneesh Kumar K.V 	p9dev->config->tag_len = strlen(tag_name);
142154f6802dSPekka Enberg 	if (p9dev->config->tag_len > MAX_TAG_LEN) {
142254f6802dSPekka Enberg 		err = -EINVAL;
1423b4422bf3SAneesh Kumar K.V 		goto free_p9dev_config;
142454f6802dSPekka Enberg 	}
14251c7850f9SSasha Levin 
1426c7838fbdSSasha Levin 	memcpy(&p9dev->config->tag, tag_name, strlen(tag_name));
14271c7850f9SSasha Levin 
1428c7838fbdSSasha Levin 	list_add(&p9dev->list, &devs);
1429b4422bf3SAneesh Kumar K.V 
1430d278197dSAsias He 	if (compat_id == -1)
143152f34d2cSAsias He 		compat_id = virtio_compat_add_message("virtio-9p", "CONFIG_NET_9P_VIRTIO");
1432e59662b3SSasha Levin 
143354f6802dSPekka Enberg 	return err;
143454f6802dSPekka Enberg 
1435b4422bf3SAneesh Kumar K.V free_p9dev_config:
1436b4422bf3SAneesh Kumar K.V 	free(p9dev->config);
1437b4422bf3SAneesh Kumar K.V free_p9dev:
1438b4422bf3SAneesh Kumar K.V 	free(p9dev);
143954f6802dSPekka Enberg 	return err;
14401c7850f9SSasha Levin }
1441