xref: /kvmtool/virtio/9p.c (revision cac9e8fd0152dbcccd2ba7edeef4fda79778b445)
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"
8*cac9e8fdSSasha 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 	ret = lchown(full_path, dfid->uid, gid);
290c797b6c6SAneesh Kumar K.V 	if (ret < 0)
291c797b6c6SAneesh Kumar K.V 		goto err_out;
292c797b6c6SAneesh Kumar K.V 
293c797b6c6SAneesh Kumar K.V 	sprintf(dfid->path, "%s/%s", dfid->path, name);
294c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
295bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
296bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
297bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
2985f900f6dSSasha Levin 	free(name);
2996c8ca053SAneesh Kumar K.V 	return;
3006c8ca053SAneesh Kumar K.V err_out:
3015f900f6dSSasha Levin 	free(name);
302c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
303c797b6c6SAneesh Kumar K.V 	return;
304c797b6c6SAneesh Kumar K.V }
305c797b6c6SAneesh Kumar K.V 
306c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev,
307c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
308c797b6c6SAneesh Kumar K.V {
309c797b6c6SAneesh Kumar K.V 	int ret;
310c797b6c6SAneesh Kumar K.V 	char *name;
311c797b6c6SAneesh Kumar K.V 	struct stat st;
312c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
313c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
314c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
315c797b6c6SAneesh Kumar K.V 	u32 dfid_val, mode, gid;
316c797b6c6SAneesh Kumar K.V 
317c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val,
318c797b6c6SAneesh Kumar K.V 			    &name, &mode, &gid);
31931a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
320c797b6c6SAneesh Kumar K.V 
321c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
322c797b6c6SAneesh Kumar K.V 	ret = mkdir(full_path, mode);
323c797b6c6SAneesh Kumar K.V 	if (ret < 0)
324c797b6c6SAneesh Kumar K.V 		goto err_out;
325c797b6c6SAneesh Kumar K.V 
326c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
327c797b6c6SAneesh Kumar K.V 		goto err_out;
328c797b6c6SAneesh Kumar K.V 
329c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
330c797b6c6SAneesh Kumar K.V 	if (ret < 0)
331c797b6c6SAneesh Kumar K.V 		goto err_out;
332c797b6c6SAneesh Kumar K.V 
333c797b6c6SAneesh Kumar K.V 	ret = lchown(full_path, dfid->uid, gid);
334c797b6c6SAneesh Kumar K.V 	if (ret < 0)
335c797b6c6SAneesh Kumar K.V 		goto err_out;
336c797b6c6SAneesh Kumar K.V 
337c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
338c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
339c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
340c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
341c797b6c6SAneesh Kumar K.V 	free(name);
342c797b6c6SAneesh Kumar K.V 	return;
343c797b6c6SAneesh Kumar K.V err_out:
344c797b6c6SAneesh Kumar K.V 	free(name);
345c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
346ead43b01SAneesh Kumar K.V 	return;
3471c7850f9SSasha Levin }
3481c7850f9SSasha Levin 
349ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev,
350af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
3511c7850f9SSasha Levin {
352af045e53SAneesh Kumar K.V 	u8 i;
353bfc15268SAneesh Kumar K.V 	u16 nwqid;
354bfc15268SAneesh Kumar K.V 	u16 nwname;
355bfc15268SAneesh Kumar K.V 	struct p9_qid wqid;
356e2341580SSasha Levin 	struct p9_fid *new_fid, *old_fid;
357c797b6c6SAneesh Kumar K.V 	u32 fid_val, newfid_val;
358c797b6c6SAneesh Kumar K.V 
3591c7850f9SSasha Levin 
360bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname);
36131a6fb8dSSasha Levin 	new_fid	= get_fid(p9dev, newfid_val);
3621c7850f9SSasha Levin 
363bfc15268SAneesh Kumar K.V 	nwqid = 0;
364bfc15268SAneesh Kumar K.V 	if (nwname) {
36531a6fb8dSSasha Levin 		struct p9_fid *fid = get_fid(p9dev, fid_val);
366bfc15268SAneesh Kumar K.V 
367baac79a5SAneesh Kumar K.V 		strcpy(new_fid->path, fid->path);
368bfc15268SAneesh Kumar K.V 		/* skip the space for count */
369bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
370bfc15268SAneesh Kumar K.V 		for (i = 0; i < nwname; i++) {
371bfc15268SAneesh Kumar K.V 			struct stat st;
3721c7850f9SSasha Levin 			char tmp[PATH_MAX] = {0};
3731c7850f9SSasha Levin 			char full_path[PATH_MAX];
374e55ed135SPekka Enberg 			char *str;
375bfc15268SAneesh Kumar K.V 
376bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_readf(pdu, "s", &str);
3771c7850f9SSasha Levin 
3781c7850f9SSasha Levin 			/* Format the new path we're 'walk'ing into */
379baac79a5SAneesh Kumar K.V 			sprintf(tmp, "%s/%s", new_fid->path, str);
380e55ed135SPekka Enberg 
381e55ed135SPekka Enberg 			free(str);
382e55ed135SPekka Enberg 
383c797b6c6SAneesh Kumar K.V 			if (lstat(rel_to_abs(p9dev, tmp, full_path), &st) < 0)
3846c8ca053SAneesh Kumar K.V 				goto err_out;
3851c7850f9SSasha Levin 
386c797b6c6SAneesh Kumar K.V 			stat2qid(&st, &wqid);
3871c7850f9SSasha Levin 			strcpy(new_fid->path, tmp);
388c797b6c6SAneesh Kumar K.V 			new_fid->uid = fid->uid;
389bfc15268SAneesh Kumar K.V 			nwqid++;
390bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_writef(pdu, "Q", &wqid);
3911c7850f9SSasha Levin 		}
3921c7850f9SSasha Levin 	} else {
393bfc15268SAneesh Kumar K.V 		/*
394bfc15268SAneesh Kumar K.V 		 * update write_offset so our outlen get correct value
395bfc15268SAneesh Kumar K.V 		 */
396bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
397e2341580SSasha Levin 		old_fid = get_fid(p9dev, fid_val);
398e2341580SSasha Levin 		strcpy(new_fid->path, old_fid->path);
399e2341580SSasha Levin 		new_fid->uid    = old_fid->uid;
4001c7850f9SSasha Levin 	}
401bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
4025529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
403bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", nwqid);
404bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
4056c8ca053SAneesh Kumar K.V 	return;
4066c8ca053SAneesh Kumar K.V err_out:
4076c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
408ead43b01SAneesh Kumar K.V 	return;
4091c7850f9SSasha Levin }
4101c7850f9SSasha Levin 
411ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev,
412af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
4131c7850f9SSasha Levin {
414bfc15268SAneesh Kumar K.V 	char *uname;
415bfc15268SAneesh Kumar K.V 	char *aname;
4161c7850f9SSasha Levin 	struct stat st;
417bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
4181c7850f9SSasha Levin 	struct p9_fid *fid;
419c797b6c6SAneesh Kumar K.V 	u32 fid_val, afid, uid;
420bfc15268SAneesh Kumar K.V 
421c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid,
422c797b6c6SAneesh Kumar K.V 			    &uname, &aname, &uid);
4231c7850f9SSasha Levin 
42439257180SPekka Enberg 	free(uname);
42539257180SPekka Enberg 	free(aname);
42639257180SPekka Enberg 
42730204a77SAneesh Kumar K.V 	if (lstat(p9dev->root_dir, &st) < 0)
4286c8ca053SAneesh Kumar K.V 		goto err_out;
4291c7850f9SSasha Levin 
430c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
4311c7850f9SSasha Levin 
43231a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
433c797b6c6SAneesh Kumar K.V 	fid->uid = uid;
4341c7850f9SSasha Levin 	strcpy(fid->path, "/");
4351c7850f9SSasha Levin 
436bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
437bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
438bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
4396c8ca053SAneesh Kumar K.V 	return;
4406c8ca053SAneesh Kumar K.V err_out:
4416c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
442ead43b01SAneesh Kumar K.V 	return;
4431c7850f9SSasha Levin }
4441c7850f9SSasha Levin 
445c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev,
446c797b6c6SAneesh Kumar K.V 				struct stat *st, struct p9_stat_dotl *statl)
4475f900f6dSSasha Levin {
448c797b6c6SAneesh Kumar K.V 	memset(statl, 0, sizeof(*statl));
449c797b6c6SAneesh Kumar K.V 	statl->st_mode		= st->st_mode;
450c797b6c6SAneesh Kumar K.V 	statl->st_nlink		= st->st_nlink;
451c797b6c6SAneesh Kumar K.V 	statl->st_uid		= st->st_uid;
452c797b6c6SAneesh Kumar K.V 	statl->st_gid		= st->st_gid;
453c797b6c6SAneesh Kumar K.V 	statl->st_rdev		= st->st_rdev;
454c797b6c6SAneesh Kumar K.V 	statl->st_size		= st->st_size;
455c797b6c6SAneesh Kumar K.V 	statl->st_blksize	= st->st_blksize;
456c797b6c6SAneesh Kumar K.V 	statl->st_blocks	= st->st_blocks;
457c797b6c6SAneesh Kumar K.V 	statl->st_atime_sec	= st->st_atime;
458c797b6c6SAneesh Kumar K.V 	statl->st_atime_nsec	= st->st_atim.tv_nsec;
459c797b6c6SAneesh Kumar K.V 	statl->st_mtime_sec	= st->st_mtime;
460c797b6c6SAneesh Kumar K.V 	statl->st_mtime_nsec	= st->st_mtim.tv_nsec;
461c797b6c6SAneesh Kumar K.V 	statl->st_ctime_sec	= st->st_ctime;
462c797b6c6SAneesh Kumar K.V 	statl->st_ctime_nsec	= st->st_ctim.tv_nsec;
463c797b6c6SAneesh Kumar K.V 	/* Currently we only support BASIC fields in stat */
464c797b6c6SAneesh Kumar K.V 	statl->st_result_mask	= P9_STATS_BASIC;
465c797b6c6SAneesh Kumar K.V 	stat2qid(st, &statl->qid);
4661c7850f9SSasha Levin }
4671c7850f9SSasha Levin 
468ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev,
469af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
4701c7850f9SSasha Levin {
471bfc15268SAneesh Kumar K.V 	u64 offset;
472bfc15268SAneesh Kumar K.V 	u32 fid_val;
473c797b6c6SAneesh Kumar K.V 	u16 iov_cnt;
474c797b6c6SAneesh Kumar K.V 	void *iov_base;
475c797b6c6SAneesh Kumar K.V 	size_t iov_len;
476bfc15268SAneesh Kumar K.V 	u32 count, rcount;
477bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
478c797b6c6SAneesh Kumar K.V 
4791c7850f9SSasha Levin 
480bfc15268SAneesh Kumar K.V 	rcount = 0;
481bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
48231a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
48350c479e0SAneesh Kumar K.V 
48450c479e0SAneesh Kumar K.V 	iov_base = pdu->in_iov[0].iov_base;
48550c479e0SAneesh Kumar K.V 	iov_len  = pdu->in_iov[0].iov_len;
48650c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->in_iov_cnt;
4875529bcd7SAsias He 	pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32);
4885529bcd7SAsias He 	pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32);
4896b163a87SAneesh Kumar K.V 	pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov,
490bfc15268SAneesh Kumar K.V 						   count,
4916b163a87SAneesh Kumar K.V 						   pdu->in_iov_cnt);
492bfc15268SAneesh Kumar K.V 	rcount = preadv(fid->fd, pdu->in_iov,
493bfc15268SAneesh Kumar K.V 			pdu->in_iov_cnt, offset);
494bfc15268SAneesh Kumar K.V 	if (rcount > count)
495bfc15268SAneesh Kumar K.V 		rcount = count;
496bfc15268SAneesh Kumar K.V 	/*
497bfc15268SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
498bfc15268SAneesh Kumar K.V 	 * pdu_writef works correctly.
499bfc15268SAneesh Kumar K.V 	 */
50050c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_base = iov_base;
50150c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_len  = iov_len;
50250c479e0SAneesh Kumar K.V 	pdu->in_iov_cnt         = iov_cnt;
503c797b6c6SAneesh Kumar K.V 
5045529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
505bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
506bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
507bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
508ead43b01SAneesh Kumar K.V 	return;
5091c7850f9SSasha Levin }
5101c7850f9SSasha Levin 
511c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent)
512c797b6c6SAneesh Kumar K.V {
513c797b6c6SAneesh Kumar K.V 	/*
514c797b6c6SAneesh Kumar K.V 	 * Size of each dirent:
515c797b6c6SAneesh Kumar K.V 	 * qid(13) + offset(8) + type(1) + name_len(2) + name
516c797b6c6SAneesh Kumar K.V 	 */
517c797b6c6SAneesh Kumar K.V 	return 24 + strlen(dent->d_name);
518c797b6c6SAneesh Kumar K.V }
519c797b6c6SAneesh Kumar K.V 
520c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev,
521c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
522c797b6c6SAneesh Kumar K.V {
523c797b6c6SAneesh Kumar K.V 	u32 fid_val;
524c797b6c6SAneesh Kumar K.V 	u32 count, rcount;
525c797b6c6SAneesh Kumar K.V 	struct stat st;
526c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
527c797b6c6SAneesh Kumar K.V 	struct dirent *dent;
528c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
529c797b6c6SAneesh Kumar K.V 	u64 offset, old_offset;
530c797b6c6SAneesh Kumar K.V 
531c797b6c6SAneesh Kumar K.V 	rcount = 0;
532c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
53331a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
534c797b6c6SAneesh Kumar K.V 
53532585666SSasha Levin 	if (!is_dir(fid)) {
53669bb4278SSasha Levin 		errno = EINVAL;
537c797b6c6SAneesh Kumar K.V 		goto err_out;
538c797b6c6SAneesh Kumar K.V 	}
539c797b6c6SAneesh Kumar K.V 
540c797b6c6SAneesh Kumar K.V 	/* Move the offset specified */
541c797b6c6SAneesh Kumar K.V 	seekdir(fid->dir, offset);
542c797b6c6SAneesh Kumar K.V 
543c797b6c6SAneesh Kumar K.V 	old_offset = offset;
544c797b6c6SAneesh Kumar K.V 	/* If reading a dir, fill the buffer with p9_stat entries */
545c797b6c6SAneesh Kumar K.V 	dent = readdir(fid->dir);
546c797b6c6SAneesh Kumar K.V 
547c797b6c6SAneesh Kumar K.V 	/* Skip the space for writing count */
548c797b6c6SAneesh Kumar K.V 	pdu->write_offset += sizeof(u32);
549c797b6c6SAneesh Kumar K.V 	while (dent) {
550c797b6c6SAneesh Kumar K.V 		u32 read;
551c797b6c6SAneesh Kumar K.V 		struct p9_qid qid;
552c797b6c6SAneesh Kumar K.V 
553c797b6c6SAneesh Kumar K.V 		if ((rcount + virtio_p9_dentry_size(dent)) > count) {
554c797b6c6SAneesh Kumar K.V 			/* seek to the previous offset and return */
555c797b6c6SAneesh Kumar K.V 			seekdir(fid->dir, old_offset);
556c797b6c6SAneesh Kumar K.V 			break;
557c797b6c6SAneesh Kumar K.V 		}
558c797b6c6SAneesh Kumar K.V 		old_offset = dent->d_off;
559c797b6c6SAneesh Kumar K.V 		lstat(rel_to_abs(p9dev, dent->d_name, full_path), &st);
560c797b6c6SAneesh Kumar K.V 		stat2qid(&st, &qid);
561c797b6c6SAneesh Kumar K.V 		read = pdu->write_offset;
562c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off,
563c797b6c6SAneesh Kumar K.V 				     dent->d_type, dent->d_name);
564c797b6c6SAneesh Kumar K.V 		rcount += pdu->write_offset - read;
565c797b6c6SAneesh Kumar K.V 		dent = readdir(fid->dir);
566c797b6c6SAneesh Kumar K.V 	}
567c797b6c6SAneesh Kumar K.V 
5685529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
569c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
570c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
571c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
572c797b6c6SAneesh Kumar K.V 	return;
573c797b6c6SAneesh Kumar K.V err_out:
574c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
575c797b6c6SAneesh Kumar K.V 	return;
576c797b6c6SAneesh Kumar K.V }
577c797b6c6SAneesh Kumar K.V 
578c797b6c6SAneesh Kumar K.V 
579c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev,
580af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
5811c7850f9SSasha Levin {
582bfc15268SAneesh Kumar K.V 	u32 fid_val;
583af045e53SAneesh Kumar K.V 	struct stat st;
584c797b6c6SAneesh Kumar K.V 	u64 request_mask;
585bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
586c797b6c6SAneesh Kumar K.V 	struct p9_stat_dotl statl;
5871c7850f9SSasha Levin 
588c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask);
58931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
59030204a77SAneesh Kumar K.V 	if (lstat(fid->abs_path, &st) < 0)
5916c8ca053SAneesh Kumar K.V 		goto err_out;
5921c7850f9SSasha Levin 
593c797b6c6SAneesh Kumar K.V 	virtio_p9_fill_stat(p9dev, &st, &statl);
594c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "A", &statl);
595bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
596bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
597ead43b01SAneesh Kumar K.V 	return;
5986c8ca053SAneesh Kumar K.V err_out:
5996c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
6006c8ca053SAneesh Kumar K.V 	return;
6011c7850f9SSasha Levin }
6021c7850f9SSasha Levin 
603c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */
604c797b6c6SAneesh Kumar K.V /*
605c797b6c6SAneesh Kumar K.V  * Attribute flags.  These should be or-ed together to figure out what
606c797b6c6SAneesh Kumar K.V  * has been changed!
607c797b6c6SAneesh Kumar K.V  */
608c797b6c6SAneesh Kumar K.V #define ATTR_MODE	(1 << 0)
609c797b6c6SAneesh Kumar K.V #define ATTR_UID	(1 << 1)
610c797b6c6SAneesh Kumar K.V #define ATTR_GID	(1 << 2)
611c797b6c6SAneesh Kumar K.V #define ATTR_SIZE	(1 << 3)
612c797b6c6SAneesh Kumar K.V #define ATTR_ATIME	(1 << 4)
613c797b6c6SAneesh Kumar K.V #define ATTR_MTIME	(1 << 5)
614c797b6c6SAneesh Kumar K.V #define ATTR_CTIME	(1 << 6)
615c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET	(1 << 7)
616c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET	(1 << 8)
617c797b6c6SAneesh Kumar K.V #define ATTR_FORCE	(1 << 9) /* Not a change, but a change it */
618c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG	(1 << 10)
619c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID	(1 << 11)
620c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID	(1 << 12)
621c797b6c6SAneesh Kumar K.V #define ATTR_FILE	(1 << 13)
622c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV	(1 << 14)
623c797b6c6SAneesh Kumar K.V #define ATTR_OPEN	(1 << 15) /* Truncating from open(O_TRUNC) */
624c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET	(1 << 16)
625c797b6c6SAneesh Kumar K.V 
626c797b6c6SAneesh Kumar K.V #define ATTR_MASK    127
627c797b6c6SAneesh Kumar K.V 
628c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev,
629af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
6301c7850f9SSasha Levin {
631c797b6c6SAneesh Kumar K.V 	int ret = 0;
632bfc15268SAneesh Kumar K.V 	u32 fid_val;
633bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
634c797b6c6SAneesh Kumar K.V 	struct p9_iattr_dotl p9attr;
6351c7850f9SSasha Levin 
636c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr);
63731a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
6381c7850f9SSasha Levin 
639c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & ATTR_MODE) {
640c797b6c6SAneesh Kumar K.V 		ret = chmod(fid->abs_path, p9attr.mode);
641c797b6c6SAneesh Kumar K.V 		if (ret < 0)
642c797b6c6SAneesh Kumar K.V 			goto err_out;
643c797b6c6SAneesh Kumar K.V 	}
644c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) {
645c797b6c6SAneesh Kumar K.V 		struct timespec times[2];
646c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_ATIME) {
647c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_ATIME_SET) {
648c797b6c6SAneesh Kumar K.V 				times[0].tv_sec = p9attr.atime_sec;
649c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = p9attr.atime_nsec;
650c797b6c6SAneesh Kumar K.V 			} else {
651c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = UTIME_NOW;
652c797b6c6SAneesh Kumar K.V 			}
653c797b6c6SAneesh Kumar K.V 		} else {
654c797b6c6SAneesh Kumar K.V 			times[0].tv_nsec = UTIME_OMIT;
655c797b6c6SAneesh Kumar K.V 		}
656c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_MTIME) {
657c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_MTIME_SET) {
658c797b6c6SAneesh Kumar K.V 				times[1].tv_sec = p9attr.mtime_sec;
659c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = p9attr.mtime_nsec;
660c797b6c6SAneesh Kumar K.V 			} else {
661c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = UTIME_NOW;
662c797b6c6SAneesh Kumar K.V 			}
663c797b6c6SAneesh Kumar K.V 		} else
664c797b6c6SAneesh Kumar K.V 			times[1].tv_nsec = UTIME_OMIT;
665c797b6c6SAneesh Kumar K.V 
666c797b6c6SAneesh Kumar K.V 		ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW);
667c797b6c6SAneesh Kumar K.V 		if (ret < 0)
668c797b6c6SAneesh Kumar K.V 			goto err_out;
669c797b6c6SAneesh Kumar K.V 	}
670c797b6c6SAneesh Kumar K.V 	/*
671c797b6c6SAneesh Kumar K.V 	 * If the only valid entry in iattr is ctime we can call
672c797b6c6SAneesh Kumar K.V 	 * chown(-1,-1) to update the ctime of the file
673c797b6c6SAneesh Kumar K.V 	 */
674c797b6c6SAneesh Kumar K.V 	if ((p9attr.valid & (ATTR_UID | ATTR_GID)) ||
675c797b6c6SAneesh Kumar K.V 	    ((p9attr.valid & ATTR_CTIME)
676c797b6c6SAneesh Kumar K.V 	     && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
677c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_UID))
678c797b6c6SAneesh Kumar K.V 			p9attr.uid = -1;
679c797b6c6SAneesh Kumar K.V 
680c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_GID))
681c797b6c6SAneesh Kumar K.V 			p9attr.gid = -1;
682c797b6c6SAneesh Kumar K.V 
683c797b6c6SAneesh Kumar K.V 		ret = lchown(fid->abs_path, p9attr.uid, p9attr.gid);
684c797b6c6SAneesh Kumar K.V 		if (ret < 0)
685c797b6c6SAneesh Kumar K.V 			goto err_out;
686c797b6c6SAneesh Kumar K.V 	}
687c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_SIZE)) {
688c797b6c6SAneesh Kumar K.V 		ret = truncate(fid->abs_path, p9attr.size);
689c797b6c6SAneesh Kumar K.V 		if (ret < 0)
690c797b6c6SAneesh Kumar K.V 			goto err_out;
691c797b6c6SAneesh Kumar K.V 	}
6925529bcd7SAsias He 	*outlen = VIRTIO_9P_HDR_LEN;
693bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
694ead43b01SAneesh Kumar K.V 	return;
6954bc9734aSAneesh Kumar K.V err_out:
6964bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
6974bc9734aSAneesh Kumar K.V 	return;
6981c7850f9SSasha Levin }
6991c7850f9SSasha Levin 
700ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev,
701af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
7021c7850f9SSasha Levin {
7034bc9734aSAneesh Kumar K.V 
704bfc15268SAneesh Kumar K.V 	u64 offset;
705bfc15268SAneesh Kumar K.V 	u32 fid_val;
7064bc9734aSAneesh Kumar K.V 	u32 count;
7074bc9734aSAneesh Kumar K.V 	ssize_t res;
70850c479e0SAneesh Kumar K.V 	u16 iov_cnt;
70950c479e0SAneesh Kumar K.V 	void *iov_base;
71050c479e0SAneesh Kumar K.V 	size_t iov_len;
711bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
712b064b05aSAneesh Kumar K.V 	/* u32 fid + u64 offset + u32 count */
713b064b05aSAneesh Kumar K.V 	int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32);
7141c7850f9SSasha Levin 
715bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
71631a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
717af045e53SAneesh Kumar K.V 
71850c479e0SAneesh Kumar K.V 	iov_base = pdu->out_iov[0].iov_base;
71950c479e0SAneesh Kumar K.V 	iov_len  = pdu->out_iov[0].iov_len;
72050c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->out_iov_cnt;
72150c479e0SAneesh Kumar K.V 
722bfc15268SAneesh Kumar K.V 	/* Adjust the iovec to skip the header and meta data */
723b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size);
724b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_len -=  (sizeof(struct p9_msg) + twrite_size);
725bfc15268SAneesh Kumar K.V 	pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count,
7266b163a87SAneesh Kumar K.V 						    pdu->out_iov_cnt);
7274bc9734aSAneesh Kumar K.V 	res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset);
72850c479e0SAneesh Kumar K.V 	/*
72950c479e0SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
73050c479e0SAneesh Kumar K.V 	 * pdu_readf works correctly.
73150c479e0SAneesh Kumar K.V 	 */
73250c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_base = iov_base;
73350c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_len  = iov_len;
73450c479e0SAneesh Kumar K.V 	pdu->out_iov_cnt         = iov_cnt;
735c797b6c6SAneesh Kumar K.V 
7364bc9734aSAneesh Kumar K.V 	if (res < 0)
7374bc9734aSAneesh Kumar K.V 		goto err_out;
7384bc9734aSAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", res);
739bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
740bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
741ead43b01SAneesh Kumar K.V 	return;
7424bc9734aSAneesh Kumar K.V err_out:
7434bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
7444bc9734aSAneesh Kumar K.V 	return;
7451c7850f9SSasha Levin }
7461c7850f9SSasha Levin 
7476fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev,
7486fc5cd9bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
7496fc5cd9bSSasha Levin {
7506fc5cd9bSSasha Levin 	int ret;
7516fc5cd9bSSasha Levin 	u32 fid_val;
7526fc5cd9bSSasha Levin 	struct p9_fid *fid;
7536fc5cd9bSSasha Levin 
7546fc5cd9bSSasha Levin 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
75531a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
7566fc5cd9bSSasha Levin 
7579b604a9cSSasha Levin 	ret = remove(fid->abs_path);
7586fc5cd9bSSasha Levin 	if (ret < 0)
7596fc5cd9bSSasha Levin 		goto err_out;
7606fc5cd9bSSasha Levin 	*outlen = pdu->write_offset;
7616fc5cd9bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
7626fc5cd9bSSasha Levin 	return;
7636fc5cd9bSSasha Levin 
7646fc5cd9bSSasha Levin err_out:
7656fc5cd9bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
7666fc5cd9bSSasha Levin 	return;
7676fc5cd9bSSasha Levin }
7686fc5cd9bSSasha Levin 
769f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev,
770f161f28bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
771f161f28bSSasha Levin {
772f161f28bSSasha Levin 	int ret;
773f161f28bSSasha Levin 	u32 fid_val, new_fid_val;
774f161f28bSSasha Levin 	struct p9_fid *fid, *new_fid;
775f161f28bSSasha Levin 	char full_path[PATH_MAX], *new_name;
776f161f28bSSasha Levin 
777f161f28bSSasha Levin 	virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name);
77831a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
77931a6fb8dSSasha Levin 	new_fid = get_fid(p9dev, new_fid_val);
780f161f28bSSasha Levin 
781f161f28bSSasha Levin 	sprintf(full_path, "%s/%s", new_fid->abs_path, new_name);
782f161f28bSSasha Levin 	ret = rename(fid->abs_path, full_path);
783f161f28bSSasha Levin 	if (ret < 0)
784f161f28bSSasha Levin 		goto err_out;
785f161f28bSSasha Levin 	*outlen = pdu->write_offset;
786f161f28bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
787f161f28bSSasha Levin 	return;
788f161f28bSSasha Levin 
789f161f28bSSasha Levin err_out:
790f161f28bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
791f161f28bSSasha Levin 	return;
792f161f28bSSasha Levin }
793f161f28bSSasha Levin 
794c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev,
795c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
796c797b6c6SAneesh Kumar K.V {
797c797b6c6SAneesh Kumar K.V 	int ret;
798c797b6c6SAneesh Kumar K.V 	u32 fid_val;
799c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
800c797b6c6SAneesh Kumar K.V 	char target_path[PATH_MAX];
801c797b6c6SAneesh Kumar K.V 
802c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
80331a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
804c797b6c6SAneesh Kumar K.V 
805c797b6c6SAneesh Kumar K.V 	memset(target_path, 0, PATH_MAX);
806c797b6c6SAneesh Kumar K.V 	ret = readlink(fid->abs_path, target_path, PATH_MAX - 1);
807c797b6c6SAneesh Kumar K.V 	if (ret < 0)
808c797b6c6SAneesh Kumar K.V 		goto err_out;
809c797b6c6SAneesh Kumar K.V 
810c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "s", target_path);
811c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
812c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
813c797b6c6SAneesh Kumar K.V 	return;
814c797b6c6SAneesh Kumar K.V err_out:
815c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
816c797b6c6SAneesh Kumar K.V 	return;
817c797b6c6SAneesh Kumar K.V }
818c797b6c6SAneesh Kumar K.V 
819c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev,
820c797b6c6SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
821c797b6c6SAneesh Kumar K.V {
822c797b6c6SAneesh Kumar K.V 	int ret;
823c797b6c6SAneesh Kumar K.V 	u64 fsid;
824c797b6c6SAneesh Kumar K.V 	u32 fid_val;
825c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
826c797b6c6SAneesh Kumar K.V 	struct statfs stat_buf;
827c797b6c6SAneesh Kumar K.V 
828c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
82931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
830c797b6c6SAneesh Kumar K.V 
831c797b6c6SAneesh Kumar K.V 	ret = statfs(fid->abs_path, &stat_buf);
832c797b6c6SAneesh Kumar K.V 	if (ret < 0)
833c797b6c6SAneesh Kumar K.V 		goto err_out;
834c797b6c6SAneesh Kumar K.V 	/* FIXME!! f_blocks needs update based on client msize */
835c797b6c6SAneesh Kumar K.V 	fsid = (unsigned int) stat_buf.f_fsid.__val[0] |
836c797b6c6SAneesh Kumar K.V 		(unsigned long long)stat_buf.f_fsid.__val[1] << 32;
837c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type,
838c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bsize, stat_buf.f_blocks,
839c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bfree, stat_buf.f_bavail,
840c797b6c6SAneesh Kumar K.V 			     stat_buf.f_files, stat_buf.f_ffree,
841c797b6c6SAneesh Kumar K.V 			     fsid, stat_buf.f_namelen);
842c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
843c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
844c797b6c6SAneesh Kumar K.V 	return;
845c797b6c6SAneesh Kumar K.V err_out:
846c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
847c797b6c6SAneesh Kumar K.V 	return;
848c797b6c6SAneesh Kumar K.V }
849c797b6c6SAneesh Kumar K.V 
850c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev,
851c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
852c797b6c6SAneesh Kumar K.V {
853c797b6c6SAneesh Kumar K.V 	int ret;
854c797b6c6SAneesh Kumar K.V 	char *name;
855c797b6c6SAneesh Kumar K.V 	struct stat st;
856c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
857c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
858c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
859c797b6c6SAneesh Kumar K.V 	u32 fid_val, mode, major, minor, gid;
860c797b6c6SAneesh Kumar K.V 
861c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode,
862c797b6c6SAneesh Kumar K.V 			    &major, &minor, &gid);
863c797b6c6SAneesh Kumar K.V 
86431a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
865c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
866c797b6c6SAneesh Kumar K.V 	ret = mknod(full_path, mode, makedev(major, minor));
867c797b6c6SAneesh Kumar K.V 	if (ret < 0)
868c797b6c6SAneesh Kumar K.V 		goto err_out;
869c797b6c6SAneesh Kumar K.V 
870c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
871c797b6c6SAneesh Kumar K.V 		goto err_out;
872c797b6c6SAneesh Kumar K.V 
873c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
874c797b6c6SAneesh Kumar K.V 	if (ret < 0)
875c797b6c6SAneesh Kumar K.V 		goto err_out;
876c797b6c6SAneesh Kumar K.V 
877c797b6c6SAneesh Kumar K.V 	ret = lchown(full_path, dfid->uid, gid);
878c797b6c6SAneesh Kumar K.V 	if (ret < 0)
879c797b6c6SAneesh Kumar K.V 		goto err_out;
880c797b6c6SAneesh Kumar K.V 
881c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
882c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
883c797b6c6SAneesh Kumar K.V 	free(name);
884c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
885c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
886c797b6c6SAneesh Kumar K.V 	return;
887c797b6c6SAneesh Kumar K.V err_out:
888c797b6c6SAneesh Kumar K.V 	free(name);
889c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
890c797b6c6SAneesh Kumar K.V 	return;
891c797b6c6SAneesh Kumar K.V }
892c797b6c6SAneesh Kumar K.V 
893c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev,
894c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
895c797b6c6SAneesh Kumar K.V {
896c797b6c6SAneesh Kumar K.V 	int ret;
897c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
898c797b6c6SAneesh Kumar K.V 	u32 fid_val, datasync;
899c797b6c6SAneesh Kumar K.V 
900c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync);
90131a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
902c797b6c6SAneesh Kumar K.V 
903c797b6c6SAneesh Kumar K.V 	if (datasync)
904c797b6c6SAneesh Kumar K.V 		ret = fdatasync(fid->fd);
905c797b6c6SAneesh Kumar K.V 	else
906c797b6c6SAneesh Kumar K.V 		ret = fsync(fid->fd);
907c797b6c6SAneesh Kumar K.V 	if (ret < 0)
908c797b6c6SAneesh Kumar K.V 		goto err_out;
909c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
910c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
911c797b6c6SAneesh Kumar K.V 	return;
912c797b6c6SAneesh Kumar K.V err_out:
913c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
914c797b6c6SAneesh Kumar K.V 	return;
915c797b6c6SAneesh Kumar K.V }
916c797b6c6SAneesh Kumar K.V 
917c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev,
918c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
919c797b6c6SAneesh Kumar K.V {
920c797b6c6SAneesh Kumar K.V 	int ret;
921c797b6c6SAneesh Kumar K.V 	struct stat st;
922c797b6c6SAneesh Kumar K.V 	u32 fid_val, gid;
923c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
924c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
925c797b6c6SAneesh Kumar K.V 	char new_name[PATH_MAX];
926c797b6c6SAneesh Kumar K.V 	char *old_path, *name;
927c797b6c6SAneesh Kumar K.V 
928c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid);
929c797b6c6SAneesh Kumar K.V 
93031a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
931c797b6c6SAneesh Kumar K.V 	sprintf(new_name, "%s/%s", dfid->abs_path, name);
932c797b6c6SAneesh Kumar K.V 	ret = symlink(old_path, new_name);
933c797b6c6SAneesh Kumar K.V 	if (ret < 0)
934c797b6c6SAneesh Kumar K.V 		goto err_out;
935c797b6c6SAneesh Kumar K.V 
936c797b6c6SAneesh Kumar K.V 	if (lstat(new_name, &st) < 0)
937c797b6c6SAneesh Kumar K.V 		goto err_out;
938c797b6c6SAneesh Kumar K.V 
939c797b6c6SAneesh Kumar K.V 	ret = lchown(new_name, dfid->uid, gid);
940c797b6c6SAneesh Kumar K.V 	if (ret < 0)
941c797b6c6SAneesh Kumar K.V 		goto err_out;
942c797b6c6SAneesh Kumar K.V 
943c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
944c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
945c797b6c6SAneesh Kumar K.V 	free(name);
946c797b6c6SAneesh Kumar K.V 	free(old_path);
947c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
948c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
949c797b6c6SAneesh Kumar K.V 	return;
950c797b6c6SAneesh Kumar K.V err_out:
951c797b6c6SAneesh Kumar K.V 	free(name);
952c797b6c6SAneesh Kumar K.V 	free(old_path);
953c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
954c797b6c6SAneesh Kumar K.V 	return;
955c797b6c6SAneesh Kumar K.V }
956c797b6c6SAneesh Kumar K.V 
957c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev,
958c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
959c797b6c6SAneesh Kumar K.V {
960c797b6c6SAneesh Kumar K.V 	int ret;
961c797b6c6SAneesh Kumar K.V 	char *name;
962c797b6c6SAneesh Kumar K.V 	u32 fid_val, dfid_val;
963c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid, *fid;
964c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
965c797b6c6SAneesh Kumar K.V 
966c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name);
967c797b6c6SAneesh Kumar K.V 
96831a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
96931a6fb8dSSasha Levin 	fid =  get_fid(p9dev, fid_val);
970c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
971c797b6c6SAneesh Kumar K.V 	ret = link(fid->abs_path, full_path);
972c797b6c6SAneesh Kumar K.V 	if (ret < 0)
973c797b6c6SAneesh Kumar K.V 		goto err_out;
974c797b6c6SAneesh Kumar K.V 	free(name);
975c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
976c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
977c797b6c6SAneesh Kumar K.V 	return;
978c797b6c6SAneesh Kumar K.V err_out:
979c797b6c6SAneesh Kumar K.V 	free(name);
980c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
981c797b6c6SAneesh Kumar K.V 	return;
982c797b6c6SAneesh Kumar K.V 
983c797b6c6SAneesh Kumar K.V }
984c797b6c6SAneesh Kumar K.V 
985c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev,
986c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
987c797b6c6SAneesh Kumar K.V {
988c797b6c6SAneesh Kumar K.V 	u8 ret;
989c797b6c6SAneesh Kumar K.V 	u32 fid_val;
990c797b6c6SAneesh Kumar K.V 	struct p9_flock flock;
991c797b6c6SAneesh Kumar K.V 
992c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type,
993c797b6c6SAneesh Kumar K.V 			    &flock.flags, &flock.start, &flock.length,
994c797b6c6SAneesh Kumar K.V 			    &flock.proc_id, &flock.client_id);
995c797b6c6SAneesh Kumar K.V 
996c797b6c6SAneesh Kumar K.V 	/* Just return success */
997c797b6c6SAneesh Kumar K.V 	ret = P9_LOCK_SUCCESS;
998c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", ret);
999c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1000c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1001c797b6c6SAneesh Kumar K.V 	free(flock.client_id);
1002c797b6c6SAneesh Kumar K.V 	return;
1003c797b6c6SAneesh Kumar K.V }
1004c797b6c6SAneesh Kumar K.V 
1005c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev,
1006c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1007c797b6c6SAneesh Kumar K.V {
1008c797b6c6SAneesh Kumar K.V 	u32 fid_val;
1009c797b6c6SAneesh Kumar K.V 	struct p9_getlock glock;
1010c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type,
1011c797b6c6SAneesh Kumar K.V 			    &glock.start, &glock.length, &glock.proc_id,
1012c797b6c6SAneesh Kumar K.V 			    &glock.client_id);
1013c797b6c6SAneesh Kumar K.V 
1014c797b6c6SAneesh Kumar K.V 	/* Just return success */
1015c797b6c6SAneesh Kumar K.V 	glock.type = F_UNLCK;
1016c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "bqqds", glock.type,
1017c797b6c6SAneesh Kumar K.V 			     glock.start, glock.length, glock.proc_id,
1018c797b6c6SAneesh Kumar K.V 			     glock.client_id);
1019c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1020c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1021c797b6c6SAneesh Kumar K.V 	free(glock.client_id);
1022c797b6c6SAneesh Kumar K.V 	return;
1023c797b6c6SAneesh Kumar K.V }
1024c797b6c6SAneesh Kumar K.V 
1025c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor)
1026c797b6c6SAneesh Kumar K.V {
1027c797b6c6SAneesh Kumar K.V 	int size = strlen(ancestor);
1028c797b6c6SAneesh Kumar K.V 	if (!strncmp(path, ancestor, size)) {
1029c797b6c6SAneesh Kumar K.V 		/*
1030c797b6c6SAneesh Kumar K.V 		 * Now check whether ancestor is a full name or
1031c797b6c6SAneesh Kumar K.V 		 * or directory component and not just part
1032c797b6c6SAneesh Kumar K.V 		 * of a name.
1033c797b6c6SAneesh Kumar K.V 		 */
1034c797b6c6SAneesh Kumar K.V 		if (path[size] == '\0' || path[size] == '/')
1035c797b6c6SAneesh Kumar K.V 			return 1;
1036c797b6c6SAneesh Kumar K.V 	}
1037c797b6c6SAneesh Kumar K.V 	return 0;
1038c797b6c6SAneesh Kumar K.V }
1039c797b6c6SAneesh Kumar K.V 
1040c797b6c6SAneesh Kumar K.V static void virtio_p9_fix_path(char *fid_path, char *old_name, char *new_name)
1041c797b6c6SAneesh Kumar K.V {
1042c797b6c6SAneesh Kumar K.V 	char tmp_name[PATH_MAX];
1043c797b6c6SAneesh Kumar K.V 	size_t rp_sz = strlen(old_name);
1044c797b6c6SAneesh Kumar K.V 
1045c797b6c6SAneesh Kumar K.V 	if (rp_sz == strlen(fid_path)) {
1046c797b6c6SAneesh Kumar K.V 		/* replace the full name */
1047c797b6c6SAneesh Kumar K.V 		strcpy(fid_path, new_name);
1048c797b6c6SAneesh Kumar K.V 		return;
1049c797b6c6SAneesh Kumar K.V 	}
1050c797b6c6SAneesh Kumar K.V 	/* save the trailing path details */
1051c797b6c6SAneesh Kumar K.V 	strcpy(tmp_name, fid_path + rp_sz);
1052c797b6c6SAneesh Kumar K.V 	sprintf(fid_path, "%s%s", new_name, tmp_name);
1053c797b6c6SAneesh Kumar K.V 	return;
1054c797b6c6SAneesh Kumar K.V }
1055c797b6c6SAneesh Kumar K.V 
1056e2341580SSasha Levin static void rename_fids(struct p9_dev *p9dev, char *old_name, char *new_name)
1057e2341580SSasha Levin {
1058e2341580SSasha Levin 	struct rb_node *node = rb_first(&p9dev->fids);
1059e2341580SSasha Levin 
1060e2341580SSasha Levin 	while (node) {
1061e2341580SSasha Levin 		struct p9_fid *fid = rb_entry(node, struct p9_fid, node);
1062e2341580SSasha Levin 
1063e2341580SSasha Levin 		if (fid->fid != P9_NOFID && virtio_p9_ancestor(fid->path, old_name)) {
1064e2341580SSasha Levin 				virtio_p9_fix_path(fid->path, old_name, new_name);
1065e2341580SSasha Levin 		}
1066e2341580SSasha Levin 		node = rb_next(node);
1067e2341580SSasha Levin 	}
1068e2341580SSasha Levin }
1069e2341580SSasha Levin 
1070c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev,
1071c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1072c797b6c6SAneesh Kumar K.V {
1073e2341580SSasha Levin 	int ret;
1074c797b6c6SAneesh Kumar K.V 	char *old_name, *new_name;
1075c797b6c6SAneesh Kumar K.V 	u32 old_dfid_val, new_dfid_val;
1076c797b6c6SAneesh Kumar K.V 	struct p9_fid *old_dfid, *new_dfid;
1077c797b6c6SAneesh Kumar K.V 	char old_full_path[PATH_MAX], new_full_path[PATH_MAX];
1078c797b6c6SAneesh Kumar K.V 
1079c797b6c6SAneesh Kumar K.V 
1080c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name,
1081c797b6c6SAneesh Kumar K.V 			    &new_dfid_val, &new_name);
1082c797b6c6SAneesh Kumar K.V 
108331a6fb8dSSasha Levin 	old_dfid = get_fid(p9dev, old_dfid_val);
108431a6fb8dSSasha Levin 	new_dfid = get_fid(p9dev, new_dfid_val);
1085c797b6c6SAneesh Kumar K.V 
1086c797b6c6SAneesh Kumar K.V 	sprintf(old_full_path, "%s/%s", old_dfid->abs_path, old_name);
1087c797b6c6SAneesh Kumar K.V 	sprintf(new_full_path, "%s/%s", new_dfid->abs_path, new_name);
1088c797b6c6SAneesh Kumar K.V 	ret = rename(old_full_path, new_full_path);
1089c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1090c797b6c6SAneesh Kumar K.V 		goto err_out;
1091c797b6c6SAneesh Kumar K.V 	/*
1092c797b6c6SAneesh Kumar K.V 	 * Now fix path in other fids, if the renamed path is part of
1093c797b6c6SAneesh Kumar K.V 	 * that.
1094c797b6c6SAneesh Kumar K.V 	 */
1095e2341580SSasha Levin 	rename_fids(p9dev, old_name, new_name);
1096c797b6c6SAneesh Kumar K.V 	free(old_name);
1097c797b6c6SAneesh Kumar K.V 	free(new_name);
1098c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1099c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1100c797b6c6SAneesh Kumar K.V 	return;
1101c797b6c6SAneesh Kumar K.V err_out:
1102c797b6c6SAneesh Kumar K.V 	free(old_name);
1103c797b6c6SAneesh Kumar K.V 	free(new_name);
1104c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1105c797b6c6SAneesh Kumar K.V 	return;
1106c797b6c6SAneesh Kumar K.V }
1107c797b6c6SAneesh Kumar K.V 
1108c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev,
1109c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1110c797b6c6SAneesh Kumar K.V {
1111c797b6c6SAneesh Kumar K.V 	int ret;
1112c797b6c6SAneesh Kumar K.V 	char *name;
1113c797b6c6SAneesh Kumar K.V 	u32 fid_val, flags;
1114c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
1115c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
1116c797b6c6SAneesh Kumar K.V 
1117c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags);
111831a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
1119c797b6c6SAneesh Kumar K.V 
1120c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", fid->abs_path, name);
1121c797b6c6SAneesh Kumar K.V 	ret = remove(full_path);
1122c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1123c797b6c6SAneesh Kumar K.V 		goto err_out;
1124c797b6c6SAneesh Kumar K.V 	free(name);
1125c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1126c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1127c797b6c6SAneesh Kumar K.V 	return;
1128c797b6c6SAneesh Kumar K.V err_out:
1129c797b6c6SAneesh Kumar K.V 	free(name);
1130c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1131c797b6c6SAneesh Kumar K.V 	return;
1132c797b6c6SAneesh Kumar K.V }
1133c797b6c6SAneesh Kumar K.V 
11345cc808aaSSasha Levin static void virtio_p9_flush(struct p9_dev *p9dev,
11355cc808aaSSasha Levin 				struct p9_pdu *pdu, u32 *outlen)
11365cc808aaSSasha Levin {
11375cc808aaSSasha Levin 	u16 tag, oldtag;
11385cc808aaSSasha Levin 
11395cc808aaSSasha Levin 	virtio_p9_pdu_readf(pdu, "ww", &tag, &oldtag);
11405cc808aaSSasha Levin 	virtio_p9_pdu_writef(pdu, "w", tag);
11415cc808aaSSasha Levin 	*outlen = pdu->write_offset;
11425cc808aaSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
11435cc808aaSSasha Levin 
11445cc808aaSSasha Levin 	return;
11455cc808aaSSasha Levin }
11465cc808aaSSasha Levin 
1147c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev,
1148c797b6c6SAneesh Kumar K.V 				 struct p9_pdu *pdu, u32 *outlen)
1149c797b6c6SAneesh Kumar K.V {
1150c797b6c6SAneesh Kumar K.V 	return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen);
1151c797b6c6SAneesh Kumar K.V }
1152c797b6c6SAneesh Kumar K.V 
1153ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev,
1154af045e53SAneesh Kumar K.V 			struct p9_pdu *pdu, u32 *outlen);
1155b4422bf3SAneesh Kumar K.V 
1156c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */
1157c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74
1158c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76
1159c797b6c6SAneesh Kumar K.V 
1160c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = {
1161c797b6c6SAneesh Kumar K.V 	[P9_TREADDIR]     = virtio_p9_readdir,
1162c797b6c6SAneesh Kumar K.V 	[P9_TSTATFS]      = virtio_p9_statfs,
1163c797b6c6SAneesh Kumar K.V 	[P9_TGETATTR]     = virtio_p9_getattr,
1164c797b6c6SAneesh Kumar K.V 	[P9_TSETATTR]     = virtio_p9_setattr,
1165c797b6c6SAneesh Kumar K.V 	[P9_TXATTRWALK]   = virtio_p9_eopnotsupp,
1166c797b6c6SAneesh Kumar K.V 	[P9_TXATTRCREATE] = virtio_p9_eopnotsupp,
1167c797b6c6SAneesh Kumar K.V 	[P9_TMKNOD]       = virtio_p9_mknod,
1168c797b6c6SAneesh Kumar K.V 	[P9_TLOCK]        = virtio_p9_lock,
1169c797b6c6SAneesh Kumar K.V 	[P9_TGETLOCK]     = virtio_p9_getlock,
1170c797b6c6SAneesh Kumar K.V 	[P9_TRENAMEAT]    = virtio_p9_renameat,
1171c797b6c6SAneesh Kumar K.V 	[P9_TREADLINK]    = virtio_p9_readlink,
1172c797b6c6SAneesh Kumar K.V 	[P9_TUNLINKAT]    = virtio_p9_unlinkat,
1173c797b6c6SAneesh Kumar K.V 	[P9_TMKDIR]       = virtio_p9_mkdir,
1174b4422bf3SAneesh Kumar K.V 	[P9_TVERSION]     = virtio_p9_version,
1175c797b6c6SAneesh Kumar K.V 	[P9_TLOPEN]       = virtio_p9_open,
1176b4422bf3SAneesh Kumar K.V 	[P9_TATTACH]      = virtio_p9_attach,
1177b4422bf3SAneesh Kumar K.V 	[P9_TWALK]        = virtio_p9_walk,
1178c797b6c6SAneesh Kumar K.V 	[P9_TCLUNK]       = virtio_p9_clunk,
1179c797b6c6SAneesh Kumar K.V 	[P9_TFSYNC]       = virtio_p9_fsync,
1180b4422bf3SAneesh Kumar K.V 	[P9_TREAD]        = virtio_p9_read,
11815cc808aaSSasha Levin 	[P9_TFLUSH]       = virtio_p9_flush,
1182c797b6c6SAneesh Kumar K.V 	[P9_TLINK]        = virtio_p9_link,
1183c797b6c6SAneesh Kumar K.V 	[P9_TSYMLINK]     = virtio_p9_symlink,
1184c797b6c6SAneesh Kumar K.V 	[P9_TLCREATE]     = virtio_p9_create,
1185b4422bf3SAneesh Kumar K.V 	[P9_TWRITE]       = virtio_p9_write,
11866fc5cd9bSSasha Levin 	[P9_TREMOVE]      = virtio_p9_remove,
1187f161f28bSSasha Levin 	[P9_TRENAME]      = virtio_p9_rename,
1188b4422bf3SAneesh Kumar K.V };
1189b4422bf3SAneesh Kumar K.V 
1190af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq)
1191af045e53SAneesh Kumar K.V {
1192af045e53SAneesh Kumar K.V 	struct p9_pdu *pdu = calloc(1, sizeof(*pdu));
1193af045e53SAneesh Kumar K.V 	if (!pdu)
1194af045e53SAneesh Kumar K.V 		return NULL;
1195af045e53SAneesh Kumar K.V 
1196bfc15268SAneesh Kumar K.V 	/* skip the pdu header p9_msg */
11975529bcd7SAsias He 	pdu->read_offset	= VIRTIO_9P_HDR_LEN;
11985529bcd7SAsias He 	pdu->write_offset	= VIRTIO_9P_HDR_LEN;
1199af045e53SAneesh Kumar K.V 	pdu->queue_head		= virt_queue__get_inout_iov(kvm, vq, pdu->in_iov,
1200a8a44649SAsias He 					pdu->out_iov, &pdu->in_iov_cnt, &pdu->out_iov_cnt);
1201af045e53SAneesh Kumar K.V 	return pdu;
1202af045e53SAneesh Kumar K.V }
1203af045e53SAneesh Kumar K.V 
1204af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu)
1205af045e53SAneesh Kumar K.V {
1206af045e53SAneesh Kumar K.V 	struct p9_msg *msg;
1207af045e53SAneesh Kumar K.V 	/*
1208af045e53SAneesh Kumar K.V 	 * we can peek directly into pdu for a u8
1209af045e53SAneesh Kumar K.V 	 * value. The host endianess won't be an issue
1210af045e53SAneesh Kumar K.V 	 */
1211af045e53SAneesh Kumar K.V 	msg = pdu->out_iov[0].iov_base;
1212af045e53SAneesh Kumar K.V 	return msg->cmd;
1213af045e53SAneesh Kumar K.V }
1214af045e53SAneesh Kumar K.V 
1215b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job)
12161c7850f9SSasha Levin {
1217af045e53SAneesh Kumar K.V 	u8 cmd;
1218b4422bf3SAneesh Kumar K.V 	u32 len = 0;
1219b4422bf3SAneesh Kumar K.V 	p9_handler *handler;
1220b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev;
1221af045e53SAneesh Kumar K.V 	struct virt_queue *vq;
1222af045e53SAneesh Kumar K.V 	struct p9_pdu *p9pdu;
12231c7850f9SSasha Levin 
1224b4422bf3SAneesh Kumar K.V 	vq = job->vq;
1225b4422bf3SAneesh Kumar K.V 	p9dev = job->p9dev;
12261c7850f9SSasha Levin 
1227af045e53SAneesh Kumar K.V 	p9pdu = virtio_p9_pdu_init(kvm, vq);
1228af045e53SAneesh Kumar K.V 	cmd = virtio_p9_get_cmd(p9pdu);
1229af045e53SAneesh Kumar K.V 
1230c797b6c6SAneesh Kumar K.V 	if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) ||
1231c797b6c6SAneesh Kumar K.V 	    !virtio_9p_dotl_handler[cmd])
123297b408afSAneesh Kumar K.V 		handler = virtio_p9_eopnotsupp;
1233dd78d9eaSAneesh Kumar K.V 	else
1234c797b6c6SAneesh Kumar K.V 		handler = virtio_9p_dotl_handler[cmd];
1235c797b6c6SAneesh Kumar K.V 
1236af045e53SAneesh Kumar K.V 	handler(p9dev, p9pdu, &len);
1237af045e53SAneesh Kumar K.V 	virt_queue__set_used_elem(vq, p9pdu->queue_head, len);
1238af045e53SAneesh Kumar K.V 	free(p9pdu);
12391c7850f9SSasha Levin 	return true;
12401c7850f9SSasha Levin }
12411c7850f9SSasha Levin 
12421c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param)
12431c7850f9SSasha Levin {
1244b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job = (struct p9_dev_job *)param;
1245b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev   = job->p9dev;
1246b4422bf3SAneesh Kumar K.V 	struct virt_queue *vq  = job->vq;
12471c7850f9SSasha Levin 
12481c7850f9SSasha Levin 	while (virt_queue__available(vq)) {
1249b4422bf3SAneesh Kumar K.V 		virtio_p9_do_io_request(kvm, job);
125002eca50cSAsias He 		p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs);
12511c7850f9SSasha Levin 	}
12521c7850f9SSasha Levin }
12531c7850f9SSasha Levin 
1254c5ae742bSSasha Levin static u8 *get_config(struct kvm *kvm, void *dev)
12551c7850f9SSasha Levin {
1256c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
12571c7850f9SSasha Levin 
1258c5ae742bSSasha Levin 	return ((u8 *)(p9dev->config));
1259c7838fbdSSasha Levin }
1260c7838fbdSSasha Levin 
1261c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev)
1262c7838fbdSSasha Levin {
1263c7838fbdSSasha Levin 	return 1 << VIRTIO_9P_MOUNT_TAG;
1264c7838fbdSSasha Levin }
1265c7838fbdSSasha Levin 
1266c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
1267c7838fbdSSasha Levin {
1268c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1269c7838fbdSSasha Levin 
1270c7838fbdSSasha Levin 	p9dev->features = features;
1271c7838fbdSSasha Levin }
1272c7838fbdSSasha Levin 
1273c7838fbdSSasha Levin static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn)
1274c7838fbdSSasha Levin {
1275c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1276b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job;
1277b4422bf3SAneesh Kumar K.V 	struct virt_queue *queue;
1278c7838fbdSSasha Levin 	void *p;
12791c7850f9SSasha Levin 
1280312c62d1SSasha Levin 	compat__remove_message(compat_id);
1281e59662b3SSasha Levin 
1282c7838fbdSSasha Levin 	queue		= &p9dev->vqs[vq];
1283c7838fbdSSasha Levin 	queue->pfn	= pfn;
12841c7850f9SSasha Levin 	p		= guest_pfn_to_host(kvm, queue->pfn);
1285c7838fbdSSasha Levin 	job		= &p9dev->jobs[vq];
12861c7850f9SSasha Levin 
1287c7838fbdSSasha Levin 	vring_init(&queue->vring, VIRTQUEUE_NUM, p, VIRTIO_PCI_VRING_ALIGN);
12881c7850f9SSasha Levin 
1289b4422bf3SAneesh Kumar K.V 	*job		= (struct p9_dev_job) {
1290b4422bf3SAneesh Kumar K.V 		.vq		= queue,
1291b4422bf3SAneesh Kumar K.V 		.p9dev		= p9dev,
1292b4422bf3SAneesh Kumar K.V 	};
1293df0c7f57SSasha Levin 	thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job);
129460eb42d5SSasha Levin 
1295c7838fbdSSasha Levin 	return 0;
12961c7850f9SSasha Levin }
12971c7850f9SSasha Levin 
1298c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq)
1299c7838fbdSSasha Levin {
1300c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
13011c7850f9SSasha Levin 
1302c7838fbdSSasha Levin 	thread_pool__do_job(&p9dev->jobs[vq].job_id);
1303c7838fbdSSasha Levin 
1304c7838fbdSSasha Levin 	return 0;
1305c7838fbdSSasha Levin }
1306c7838fbdSSasha Levin 
1307c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq)
1308c7838fbdSSasha Levin {
1309c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1310c7838fbdSSasha Levin 
1311c7838fbdSSasha Levin 	return p9dev->vqs[vq].pfn;
1312c7838fbdSSasha Levin }
1313c7838fbdSSasha Levin 
1314c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
1315c7838fbdSSasha Levin {
1316c7838fbdSSasha Levin 	return VIRTQUEUE_NUM;
1317c7838fbdSSasha Levin }
1318c7838fbdSSasha Levin 
13191c47ce69SSasha Levin struct virtio_ops p9_dev_virtio_ops = (struct virtio_ops) {
1320c7838fbdSSasha Levin 	.get_config		= get_config,
1321c7838fbdSSasha Levin 	.get_host_features	= get_host_features,
1322c7838fbdSSasha Levin 	.set_guest_features	= set_guest_features,
1323c7838fbdSSasha Levin 	.init_vq		= init_vq,
1324c7838fbdSSasha Levin 	.notify_vq		= notify_vq,
1325c7838fbdSSasha Levin 	.get_pfn_vq		= get_pfn_vq,
1326c7838fbdSSasha Levin 	.get_size_vq		= get_size_vq,
1327c7838fbdSSasha Levin };
13281c47ce69SSasha Levin 
1329*cac9e8fdSSasha Levin int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
1330*cac9e8fdSSasha Levin {
1331*cac9e8fdSSasha Levin 	char *tag_name;
1332*cac9e8fdSSasha Levin 	char tmp[PATH_MAX];
1333*cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1334*cac9e8fdSSasha Levin 
1335*cac9e8fdSSasha Levin 	/*
1336*cac9e8fdSSasha Levin 	 * 9p dir can be of the form dirname,tag_name or
1337*cac9e8fdSSasha Levin 	 * just dirname. In the later case we use the
1338*cac9e8fdSSasha Levin 	 * default tag name
1339*cac9e8fdSSasha Levin 	 */
1340*cac9e8fdSSasha Levin 	tag_name = strstr(arg, ",");
1341*cac9e8fdSSasha Levin 	if (tag_name) {
1342*cac9e8fdSSasha Levin 		*tag_name = '\0';
1343*cac9e8fdSSasha Levin 		tag_name++;
1344*cac9e8fdSSasha Levin 	}
1345*cac9e8fdSSasha Levin 	if (realpath(arg, tmp)) {
1346*cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, tmp, tag_name) < 0)
1347*cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1348*cac9e8fdSSasha Levin 	} else
1349*cac9e8fdSSasha Levin 		die("Failed resolving 9p path");
1350*cac9e8fdSSasha Levin 	return 0;
1351*cac9e8fdSSasha Levin }
1352*cac9e8fdSSasha Levin 
1353*cac9e8fdSSasha Levin int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset)
1354*cac9e8fdSSasha Levin {
1355*cac9e8fdSSasha Levin 	char path[PATH_MAX];
1356*cac9e8fdSSasha Levin 	struct stat st;
1357*cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1358*cac9e8fdSSasha Levin 
1359*cac9e8fdSSasha Levin 	if (stat(arg, &st) == 0 &&
1360*cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1361*cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1362*cac9e8fdSSasha Levin 
1363*cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1364*cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1365*cac9e8fdSSasha Levin 
1366*cac9e8fdSSasha Levin 		if (realpath(arg, tmp) == 0 ||
1367*cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1368*cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1369*cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = 1;
1370*cac9e8fdSSasha Levin 		return 0;
1371*cac9e8fdSSasha Levin 	}
1372*cac9e8fdSSasha Levin 
1373*cac9e8fdSSasha Levin 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
1374*cac9e8fdSSasha Levin 
1375*cac9e8fdSSasha Levin 	if (stat(path, &st) == 0 &&
1376*cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1377*cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1378*cac9e8fdSSasha Levin 
1379*cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1380*cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1381*cac9e8fdSSasha Levin 
1382*cac9e8fdSSasha Levin 		if (realpath(path, tmp) == 0 ||
1383*cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1384*cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1385*cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
1386*cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1387*cac9e8fdSSasha Levin 		kvm_setup_resolv(arg);
1388*cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
1389*cac9e8fdSSasha Levin 		kvm->cfg.custom_rootfs_name = arg;
1390*cac9e8fdSSasha Levin 		return 0;
1391*cac9e8fdSSasha Levin 	}
1392*cac9e8fdSSasha Levin 
1393*cac9e8fdSSasha Levin 	return -1;
1394*cac9e8fdSSasha Levin }
1395*cac9e8fdSSasha Levin 
13961c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm)
13971c47ce69SSasha Levin {
13981c47ce69SSasha Levin 	struct p9_dev *p9dev;
13991c47ce69SSasha Levin 
14001c47ce69SSasha Levin 	list_for_each_entry(p9dev, &devs, list) {
140102eca50cSAsias He 		virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops,
14025529bcd7SAsias He 			    VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_9P, VIRTIO_ID_9P, PCI_CLASS_9P);
1403c7838fbdSSasha Levin 	}
1404c7838fbdSSasha Levin 
1405c7838fbdSSasha Levin 	return 0;
1406c7838fbdSSasha Levin }
1407c7838fbdSSasha Levin 
1408c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name)
1409c7838fbdSSasha Levin {
1410c7838fbdSSasha Levin 	struct p9_dev *p9dev;
141154f6802dSPekka Enberg 	int err = 0;
14121c7850f9SSasha Levin 
1413b4422bf3SAneesh Kumar K.V 	p9dev = calloc(1, sizeof(*p9dev));
1414b4422bf3SAneesh Kumar K.V 	if (!p9dev)
141554f6802dSPekka Enberg 		return -ENOMEM;
141654f6802dSPekka Enberg 
1417b4422bf3SAneesh Kumar K.V 	if (!tag_name)
14185529bcd7SAsias He 		tag_name = VIRTIO_9P_DEFAULT_TAG;
141954f6802dSPekka Enberg 
1420b4422bf3SAneesh Kumar K.V 	p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1);
142154f6802dSPekka Enberg 	if (p9dev->config == NULL) {
142254f6802dSPekka Enberg 		err = -ENOMEM;
1423b4422bf3SAneesh Kumar K.V 		goto free_p9dev;
142454f6802dSPekka Enberg 	}
14251c7850f9SSasha Levin 
1426b4422bf3SAneesh Kumar K.V 	strcpy(p9dev->root_dir, root);
1427b4422bf3SAneesh Kumar K.V 	p9dev->config->tag_len = strlen(tag_name);
142854f6802dSPekka Enberg 	if (p9dev->config->tag_len > MAX_TAG_LEN) {
142954f6802dSPekka Enberg 		err = -EINVAL;
1430b4422bf3SAneesh Kumar K.V 		goto free_p9dev_config;
143154f6802dSPekka Enberg 	}
14321c7850f9SSasha Levin 
1433c7838fbdSSasha Levin 	memcpy(&p9dev->config->tag, tag_name, strlen(tag_name));
14341c7850f9SSasha Levin 
1435c7838fbdSSasha Levin 	list_add(&p9dev->list, &devs);
1436b4422bf3SAneesh Kumar K.V 
1437d278197dSAsias He 	if (compat_id == -1)
143852f34d2cSAsias He 		compat_id = virtio_compat_add_message("virtio-9p", "CONFIG_NET_9P_VIRTIO");
1439e59662b3SSasha Levin 
144054f6802dSPekka Enberg 	return err;
144154f6802dSPekka Enberg 
1442b4422bf3SAneesh Kumar K.V free_p9dev_config:
1443b4422bf3SAneesh Kumar K.V 	free(p9dev->config);
1444b4422bf3SAneesh Kumar K.V free_p9dev:
1445b4422bf3SAneesh Kumar K.V 	free(p9dev);
144654f6802dSPekka Enberg 	return err;
14471c7850f9SSasha Levin }
1448