xref: /kvmtool/virtio/9p.c (revision d4727f2bbefff9c6869c8f5dfb28d1aeda032ee9)
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>
21c6cb7c75SAndre Przywara #include <linux/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;
31e277a1b4SG. Campana 	size_t len;
32e2341580SSasha Levin 
33e2341580SSasha Levin 	while (node) {
34e2341580SSasha Levin 		struct p9_fid *cur = rb_entry(node, struct p9_fid, node);
35e2341580SSasha Levin 
36e2341580SSasha Levin 		if (fid < cur->fid) {
37e2341580SSasha Levin 			node = node->rb_left;
38e2341580SSasha Levin 		} else if (fid > cur->fid) {
39e2341580SSasha Levin 			node = node->rb_right;
40e2341580SSasha Levin 		} else {
41e2341580SSasha Levin 			return cur;
42e2341580SSasha Levin 		}
43e2341580SSasha Levin 	}
44e2341580SSasha Levin 
45e2341580SSasha Levin 	pfid = calloc(sizeof(*pfid), 1);
46e2341580SSasha Levin 	if (!pfid)
47e2341580SSasha Levin 		return NULL;
48e2341580SSasha Levin 
49e277a1b4SG. Campana 	len = strlen(dev->root_dir);
50e277a1b4SG. Campana 	if (len >= sizeof(pfid->abs_path)) {
51e277a1b4SG. Campana 		free(pfid);
52e277a1b4SG. Campana 		return NULL;
53e277a1b4SG. Campana 	}
54e277a1b4SG. Campana 
55e2341580SSasha Levin 	pfid->fid = fid;
56e2341580SSasha Levin 	strcpy(pfid->abs_path, dev->root_dir);
57e277a1b4SG. Campana 	pfid->path = pfid->abs_path + strlen(pfid->abs_path);
58e2341580SSasha Levin 
59e2341580SSasha Levin 	insert_new_fid(dev, pfid);
60e2341580SSasha Levin 
61e2341580SSasha Levin 	return pfid;
62e2341580SSasha Levin }
63e2341580SSasha Levin 
64e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid)
65e2341580SSasha Levin {
66e2341580SSasha Levin 	struct rb_node **node = &(dev->fids.rb_node), *parent = NULL;
67e2341580SSasha Levin 
68e2341580SSasha Levin 	while (*node) {
69e2341580SSasha Levin 		int result = fid->fid - rb_entry(*node, struct p9_fid, node)->fid;
70e2341580SSasha Levin 
71e2341580SSasha Levin 		parent = *node;
72e2341580SSasha Levin 		if (result < 0)
73e2341580SSasha Levin 			node    = &((*node)->rb_left);
74e2341580SSasha Levin 		else if (result > 0)
75e2341580SSasha Levin 			node    = &((*node)->rb_right);
76e2341580SSasha Levin 		else
77e2341580SSasha Levin 			return -EEXIST;
78e2341580SSasha Levin 	}
79e2341580SSasha Levin 
80e2341580SSasha Levin 	rb_link_node(&fid->node, parent, node);
81e2341580SSasha Levin 	rb_insert_color(&fid->node, &dev->fids);
82e2341580SSasha Levin 	return 0;
83e2341580SSasha Levin }
84e2341580SSasha Levin 
8531a6fb8dSSasha Levin static struct p9_fid *get_fid(struct p9_dev *p9dev, int fid)
8631a6fb8dSSasha Levin {
87e2341580SSasha Levin 	struct p9_fid *new;
8831a6fb8dSSasha Levin 
89e2341580SSasha Levin 	new = find_or_create_fid(p9dev, fid);
90e2341580SSasha Levin 
91e2341580SSasha Levin 	return new;
9231a6fb8dSSasha Levin }
9331a6fb8dSSasha Levin 
941c7850f9SSasha Levin /* Warning: Immediately use value returned from this function */
95b4422bf3SAneesh Kumar K.V static const char *rel_to_abs(struct p9_dev *p9dev,
96b4422bf3SAneesh Kumar K.V 			      const char *path, char *abs_path)
971c7850f9SSasha Levin {
98b4422bf3SAneesh Kumar K.V 	sprintf(abs_path, "%s/%s", p9dev->root_dir, path);
991c7850f9SSasha Levin 
1001c7850f9SSasha Levin 	return abs_path;
1011c7850f9SSasha Levin }
1021c7850f9SSasha Levin 
103c797b6c6SAneesh Kumar K.V static void stat2qid(struct stat *st, struct p9_qid *qid)
1041c7850f9SSasha Levin {
1051c7850f9SSasha Levin 	*qid = (struct p9_qid) {
1061c7850f9SSasha Levin 		.path		= st->st_ino,
1071c7850f9SSasha Levin 		.version	= st->st_mtime,
1081c7850f9SSasha Levin 	};
1091c7850f9SSasha Levin 
1101c7850f9SSasha Levin 	if (S_ISDIR(st->st_mode))
1111c7850f9SSasha Levin 		qid->type	|= P9_QTDIR;
1121c7850f9SSasha Levin }
1131c7850f9SSasha Levin 
114b4422bf3SAneesh Kumar K.V static void close_fid(struct p9_dev *p9dev, u32 fid)
1151c7850f9SSasha Levin {
116e2341580SSasha Levin 	struct p9_fid *pfid = get_fid(p9dev, fid);
117e2341580SSasha Levin 
118e2341580SSasha Levin 	if (pfid->fd > 0)
119e2341580SSasha Levin 		close(pfid->fd);
120e2341580SSasha Levin 
121e2341580SSasha Levin 	if (pfid->dir)
122e2341580SSasha Levin 		closedir(pfid->dir);
123e2341580SSasha Levin 
124e2341580SSasha Levin 	rb_erase(&pfid->node, &p9dev->fids);
125e2341580SSasha Levin 	free(pfid);
1261c7850f9SSasha Levin }
127e2341580SSasha Levin 
128bfc15268SAneesh Kumar K.V static void virtio_p9_set_reply_header(struct p9_pdu *pdu, u32 size)
1291c7850f9SSasha Levin {
130bfc15268SAneesh Kumar K.V 	u8 cmd;
131bfc15268SAneesh Kumar K.V 	u16 tag;
132bfc15268SAneesh Kumar K.V 
133bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32);
134bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "bw", &cmd, &tag);
135bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
136bfc15268SAneesh Kumar K.V 	/* cmd + 1 is the reply message */
137bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", size, cmd + 1, tag);
1381c7850f9SSasha Levin }
1391c7850f9SSasha Levin 
1406b163a87SAneesh Kumar K.V static u16 virtio_p9_update_iov_cnt(struct iovec iov[], u32 count, int iov_cnt)
1416b163a87SAneesh Kumar K.V {
1426b163a87SAneesh Kumar K.V 	int i;
1436b163a87SAneesh Kumar K.V 	u32 total = 0;
1446b163a87SAneesh Kumar K.V 	for (i = 0; (i < iov_cnt) && (total < count); i++) {
1456b163a87SAneesh Kumar K.V 		if (total + iov[i].iov_len > count) {
1466b163a87SAneesh Kumar K.V 			/* we don't need this iov fully */
1476b163a87SAneesh Kumar K.V 			iov[i].iov_len -= ((total + iov[i].iov_len) - count);
1486b163a87SAneesh Kumar K.V 			i++;
1496b163a87SAneesh Kumar K.V 			break;
1506b163a87SAneesh Kumar K.V 		}
1516b163a87SAneesh Kumar K.V 		total += iov[i].iov_len;
1526b163a87SAneesh Kumar K.V 	}
1536b163a87SAneesh Kumar K.V 	return i;
1546b163a87SAneesh Kumar K.V }
1556b163a87SAneesh Kumar K.V 
156eee1ba8eSAneesh Kumar K.V static void virtio_p9_error_reply(struct p9_dev *p9dev,
157eee1ba8eSAneesh Kumar K.V 				  struct p9_pdu *pdu, int err, u32 *outlen)
158eee1ba8eSAneesh Kumar K.V {
159bfc15268SAneesh Kumar K.V 	u16 tag;
160eee1ba8eSAneesh Kumar K.V 
1619c2e1d1aSSuzuki K. Poulose 	/* EMFILE at server implies ENFILE for the VM */
1629c2e1d1aSSuzuki K. Poulose 	if (err == EMFILE)
1639c2e1d1aSSuzuki K. Poulose 		err = ENFILE;
1649c2e1d1aSSuzuki K. Poulose 
1655529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
166c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", err);
167bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
168eee1ba8eSAneesh Kumar K.V 
169c797b6c6SAneesh Kumar K.V 	/* read the tag from input */
170bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32) + sizeof(u8);
171bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "w", &tag);
172bfc15268SAneesh Kumar K.V 
173c797b6c6SAneesh Kumar K.V 	/* Update the header */
174bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
175c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", *outlen, P9_RLERROR, tag);
176eee1ba8eSAneesh Kumar K.V }
177eee1ba8eSAneesh Kumar K.V 
178ead43b01SAneesh Kumar K.V static void virtio_p9_version(struct p9_dev *p9dev,
179af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1801c7850f9SSasha Levin {
181c797b6c6SAneesh Kumar K.V 	u32 msize;
182c797b6c6SAneesh Kumar K.V 	char *version;
183c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ds", &msize, &version);
184c797b6c6SAneesh Kumar K.V 	/*
185c797b6c6SAneesh Kumar K.V 	 * reply with the same msize the client sent us
186c797b6c6SAneesh Kumar K.V 	 * Error out if the request is not for 9P2000.L
187c797b6c6SAneesh Kumar K.V 	 */
1885529bcd7SAsias He 	if (!strcmp(version, VIRTIO_9P_VERSION_DOTL))
189c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, version);
190c797b6c6SAneesh Kumar K.V 	else
191c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, "unknown");
1921c7850f9SSasha Levin 
193bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
194bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
195c797b6c6SAneesh Kumar K.V 	free(version);
196ead43b01SAneesh Kumar K.V 	return;
1971c7850f9SSasha Levin }
1981c7850f9SSasha Levin 
199ead43b01SAneesh Kumar K.V static void virtio_p9_clunk(struct p9_dev *p9dev,
200af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
2011c7850f9SSasha Levin {
202bfc15268SAneesh Kumar K.V 	u32 fid;
2031c7850f9SSasha Levin 
204bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid);
205bfc15268SAneesh Kumar K.V 	close_fid(p9dev, fid);
2061c7850f9SSasha Levin 
207bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
208bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
209ead43b01SAneesh Kumar K.V 	return;
2101c7850f9SSasha Levin }
2111c7850f9SSasha Levin 
212c797b6c6SAneesh Kumar K.V /*
213c797b6c6SAneesh Kumar K.V  * FIXME!! Need to map to protocol independent value. Upstream
214c797b6c6SAneesh Kumar K.V  * 9p also have the same BUG
215c797b6c6SAneesh Kumar K.V  */
216c797b6c6SAneesh Kumar K.V static int virtio_p9_openflags(int flags)
217c797b6c6SAneesh Kumar K.V {
218c797b6c6SAneesh Kumar K.V 	flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT | O_DIRECT);
219c797b6c6SAneesh Kumar K.V 	flags |= O_NOFOLLOW;
220c797b6c6SAneesh Kumar K.V 	return flags;
221c797b6c6SAneesh Kumar K.V }
222c797b6c6SAneesh Kumar K.V 
22332585666SSasha Levin static bool is_dir(struct p9_fid *fid)
22432585666SSasha Levin {
22532585666SSasha Levin 	struct stat st;
22632585666SSasha Levin 
22732585666SSasha Levin 	stat(fid->abs_path, &st);
22832585666SSasha Levin 
22932585666SSasha Levin 	return S_ISDIR(st.st_mode);
23032585666SSasha Levin }
23132585666SSasha Levin 
2329bb99a82SG. Campana /* path is always absolute */
2339bb99a82SG. Campana static bool path_is_illegal(const char *path)
2349bb99a82SG. Campana {
2359bb99a82SG. Campana 	size_t len;
2369bb99a82SG. Campana 
2379bb99a82SG. Campana 	if (strstr(path, "/../") != NULL)
2389bb99a82SG. Campana 		return true;
2399bb99a82SG. Campana 
2409bb99a82SG. Campana 	len = strlen(path);
2419bb99a82SG. Campana 	if (len >= 3 && strcmp(path + len - 3, "/..") == 0)
2429bb99a82SG. Campana 		return true;
2439bb99a82SG. Campana 
2449bb99a82SG. Campana 	return false;
2459bb99a82SG. Campana }
2469bb99a82SG. Campana 
247*d4727f2bSG. Campana static int get_full_path_helper(char *full_path, size_t size,
248*d4727f2bSG. Campana 			 const char *dirname, const char *name)
249*d4727f2bSG. Campana {
250*d4727f2bSG. Campana 	int ret;
251*d4727f2bSG. Campana 
252*d4727f2bSG. Campana 	ret = snprintf(full_path, size, "%s/%s", dirname, name);
253*d4727f2bSG. Campana 	if (ret >= (int)sizeof(full_path)) {
254*d4727f2bSG. Campana 		errno = ENAMETOOLONG;
255*d4727f2bSG. Campana 		return -1;
256*d4727f2bSG. Campana 	}
257*d4727f2bSG. Campana 
258*d4727f2bSG. Campana 	if (path_is_illegal(full_path)) {
259*d4727f2bSG. Campana 		errno = EACCES;
260*d4727f2bSG. Campana 		return -1;
261*d4727f2bSG. Campana 	}
262*d4727f2bSG. Campana 
263*d4727f2bSG. Campana 	return 0;
264*d4727f2bSG. Campana }
265*d4727f2bSG. Campana 
266*d4727f2bSG. Campana static int get_full_path(char *full_path, size_t size, struct p9_fid *fid,
267*d4727f2bSG. Campana 			 const char *name)
268*d4727f2bSG. Campana {
269*d4727f2bSG. Campana 	return get_full_path_helper(full_path, size, fid->abs_path, name);
270*d4727f2bSG. Campana }
271*d4727f2bSG. Campana 
272ead43b01SAneesh Kumar K.V static void virtio_p9_open(struct p9_dev *p9dev,
273af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
2741c7850f9SSasha Levin {
275c797b6c6SAneesh Kumar K.V 	u32 fid, flags;
2761c7850f9SSasha Levin 	struct stat st;
277bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
278bfc15268SAneesh Kumar K.V 	struct p9_fid *new_fid;
279bfc15268SAneesh Kumar K.V 
280c797b6c6SAneesh Kumar K.V 
281c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid, &flags);
28231a6fb8dSSasha Levin 	new_fid = get_fid(p9dev, fid);
2831c7850f9SSasha Levin 
28430204a77SAneesh Kumar K.V 	if (lstat(new_fid->abs_path, &st) < 0)
285eee1ba8eSAneesh Kumar K.V 		goto err_out;
2861c7850f9SSasha Levin 
287c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
2881c7850f9SSasha Levin 
28932585666SSasha Levin 	if (is_dir(new_fid)) {
2901c7850f9SSasha Levin 		new_fid->dir = opendir(new_fid->abs_path);
291eee1ba8eSAneesh Kumar K.V 		if (!new_fid->dir)
292eee1ba8eSAneesh Kumar K.V 			goto err_out;
293eee1ba8eSAneesh Kumar K.V 	} else {
294eee1ba8eSAneesh Kumar K.V 		new_fid->fd  = open(new_fid->abs_path,
295c797b6c6SAneesh Kumar K.V 				    virtio_p9_openflags(flags));
296eee1ba8eSAneesh Kumar K.V 		if (new_fid->fd < 0)
297eee1ba8eSAneesh Kumar K.V 			goto err_out;
298eee1ba8eSAneesh Kumar K.V 	}
299c797b6c6SAneesh Kumar K.V 	/* FIXME!! need ot send proper iounit  */
300bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
301bfc15268SAneesh Kumar K.V 
302bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
303bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
304ead43b01SAneesh Kumar K.V 	return;
305eee1ba8eSAneesh Kumar K.V err_out:
306eee1ba8eSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
307ead43b01SAneesh Kumar K.V 	return;
3081c7850f9SSasha Levin }
3091c7850f9SSasha Levin 
310ead43b01SAneesh Kumar K.V static void virtio_p9_create(struct p9_dev *p9dev,
311af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
3121c7850f9SSasha Levin {
313c797b6c6SAneesh Kumar K.V 	int fd, ret;
314bfc15268SAneesh Kumar K.V 	char *name;
31532832dd1SG. Campana 	size_t size;
316af045e53SAneesh Kumar K.V 	struct stat st;
317bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
318c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
3194bc9734aSAneesh Kumar K.V 	char full_path[PATH_MAX];
320c797b6c6SAneesh Kumar K.V 	u32 dfid_val, flags, mode, gid;
321af045e53SAneesh Kumar K.V 
322c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsddd", &dfid_val,
323c797b6c6SAneesh Kumar K.V 			    &name, &flags, &mode, &gid);
32431a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
3251c7850f9SSasha Levin 
326*d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
32732832dd1SG. Campana 		goto err_out;
3289bb99a82SG. Campana 
32932832dd1SG. Campana 	size = sizeof(dfid->abs_path) - (dfid->path - dfid->abs_path);
33032832dd1SG. Campana 	ret = snprintf(dfid->path, size, "%s/%s", dfid->path, name);
33132832dd1SG. Campana 	if (ret >= (int)size) {
33232832dd1SG. Campana 		errno = ENAMETOOLONG;
33332832dd1SG. Campana 		if (size > 0)
33432832dd1SG. Campana 			dfid->path[size] = '\x00';
33532832dd1SG. Campana 		goto err_out;
33632832dd1SG. Campana 	}
33732832dd1SG. Campana 
338*d4727f2bSG. Campana 	flags = virtio_p9_openflags(flags);
339*d4727f2bSG. Campana 
340c797b6c6SAneesh Kumar K.V 	fd = open(full_path, flags | O_CREAT, mode);
3414bc9734aSAneesh Kumar K.V 	if (fd < 0)
3424bc9734aSAneesh Kumar K.V 		goto err_out;
343c797b6c6SAneesh Kumar K.V 	dfid->fd = fd;
344c797b6c6SAneesh Kumar K.V 
3454bc9734aSAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
3466c8ca053SAneesh Kumar K.V 		goto err_out;
3471c7850f9SSasha Levin 
348c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
349c797b6c6SAneesh Kumar K.V 	if (ret < 0)
350c797b6c6SAneesh Kumar K.V 		goto err_out;
351c797b6c6SAneesh Kumar K.V 
352c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
353bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
354bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
355bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
3565f900f6dSSasha Levin 	free(name);
3576c8ca053SAneesh Kumar K.V 	return;
3586c8ca053SAneesh Kumar K.V err_out:
3595f900f6dSSasha Levin 	free(name);
360c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
361c797b6c6SAneesh Kumar K.V 	return;
362c797b6c6SAneesh Kumar K.V }
363c797b6c6SAneesh Kumar K.V 
364c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev,
365c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
366c797b6c6SAneesh Kumar K.V {
367c797b6c6SAneesh Kumar K.V 	int ret;
368c797b6c6SAneesh Kumar K.V 	char *name;
369c797b6c6SAneesh Kumar K.V 	struct stat st;
370c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
371c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
372c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
373c797b6c6SAneesh Kumar K.V 	u32 dfid_val, mode, gid;
374c797b6c6SAneesh Kumar K.V 
375c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val,
376c797b6c6SAneesh Kumar K.V 			    &name, &mode, &gid);
37731a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
378c797b6c6SAneesh Kumar K.V 
379*d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
38032832dd1SG. Campana 		goto err_out;
3819bb99a82SG. Campana 
382c797b6c6SAneesh Kumar K.V 	ret = mkdir(full_path, mode);
383c797b6c6SAneesh Kumar K.V 	if (ret < 0)
384c797b6c6SAneesh Kumar K.V 		goto err_out;
385c797b6c6SAneesh Kumar K.V 
386c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
387c797b6c6SAneesh Kumar K.V 		goto err_out;
388c797b6c6SAneesh Kumar K.V 
389c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
390c797b6c6SAneesh Kumar K.V 	if (ret < 0)
391c797b6c6SAneesh Kumar K.V 		goto err_out;
392c797b6c6SAneesh Kumar K.V 
393c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
394c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
395c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
396c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
397c797b6c6SAneesh Kumar K.V 	free(name);
398c797b6c6SAneesh Kumar K.V 	return;
399c797b6c6SAneesh Kumar K.V err_out:
400c797b6c6SAneesh Kumar K.V 	free(name);
401c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
402ead43b01SAneesh Kumar K.V 	return;
4031c7850f9SSasha Levin }
4041c7850f9SSasha Levin 
405e277a1b4SG. Campana static int join_path(struct p9_fid *fid, const char *name)
406e277a1b4SG. Campana {
407e277a1b4SG. Campana 	size_t len, size;
408e277a1b4SG. Campana 
409e277a1b4SG. Campana 	size = sizeof(fid->abs_path) - (fid->path - fid->abs_path);
410e277a1b4SG. Campana 	len = strlen(name);
411e277a1b4SG. Campana 	if (len >= size)
412e277a1b4SG. Campana 		return -1;
413e277a1b4SG. Campana 
414e277a1b4SG. Campana 	strncpy(fid->path, name, size);
415e277a1b4SG. Campana 	return 0;
416e277a1b4SG. Campana }
417e277a1b4SG. Campana 
418ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev,
419af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
4201c7850f9SSasha Levin {
421af045e53SAneesh Kumar K.V 	u8 i;
422bfc15268SAneesh Kumar K.V 	u16 nwqid;
423bfc15268SAneesh Kumar K.V 	u16 nwname;
424bfc15268SAneesh Kumar K.V 	struct p9_qid wqid;
425e2341580SSasha Levin 	struct p9_fid *new_fid, *old_fid;
426c797b6c6SAneesh Kumar K.V 	u32 fid_val, newfid_val;
427c797b6c6SAneesh Kumar K.V 
4281c7850f9SSasha Levin 
429bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname);
43031a6fb8dSSasha Levin 	new_fid	= get_fid(p9dev, newfid_val);
4311c7850f9SSasha Levin 
432bfc15268SAneesh Kumar K.V 	nwqid = 0;
433bfc15268SAneesh Kumar K.V 	if (nwname) {
43431a6fb8dSSasha Levin 		struct p9_fid *fid = get_fid(p9dev, fid_val);
435bfc15268SAneesh Kumar K.V 
436e277a1b4SG. Campana 		if (join_path(new_fid, fid->path) != 0) {
437e277a1b4SG. Campana 			errno = ENAMETOOLONG;
438e277a1b4SG. Campana 			goto err_out;
439e277a1b4SG. Campana 		}
440e277a1b4SG. Campana 
441bfc15268SAneesh Kumar K.V 		/* skip the space for count */
442bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
443bfc15268SAneesh Kumar K.V 		for (i = 0; i < nwname; i++) {
444bfc15268SAneesh Kumar K.V 			struct stat st;
4451c7850f9SSasha Levin 			char tmp[PATH_MAX] = {0};
4461c7850f9SSasha Levin 			char full_path[PATH_MAX];
447e55ed135SPekka Enberg 			char *str;
44832832dd1SG. Campana 			int ret;
449bfc15268SAneesh Kumar K.V 
450bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_readf(pdu, "s", &str);
4511c7850f9SSasha Levin 
4521c7850f9SSasha Levin 			/* Format the new path we're 'walk'ing into */
45332832dd1SG. Campana 			ret = snprintf(tmp, sizeof(tmp), "%s/%s", new_fid->path, str);
45432832dd1SG. Campana 			if (ret >= (int)sizeof(tmp)) {
45532832dd1SG. Campana 				errno = ENAMETOOLONG;
45632832dd1SG. Campana 				goto err_out;
45732832dd1SG. Campana 			}
458e55ed135SPekka Enberg 
459e55ed135SPekka Enberg 			free(str);
460e55ed135SPekka Enberg 
461c797b6c6SAneesh Kumar K.V 			if (lstat(rel_to_abs(p9dev, tmp, full_path), &st) < 0)
4626c8ca053SAneesh Kumar K.V 				goto err_out;
4631c7850f9SSasha Levin 
464c797b6c6SAneesh Kumar K.V 			stat2qid(&st, &wqid);
465e277a1b4SG. Campana 			if (join_path(new_fid, tmp) != 0) {
466e277a1b4SG. Campana 				errno = ENAMETOOLONG;
467e277a1b4SG. Campana 				goto err_out;
468e277a1b4SG. Campana 			}
469c797b6c6SAneesh Kumar K.V 			new_fid->uid = fid->uid;
470bfc15268SAneesh Kumar K.V 			nwqid++;
471bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_writef(pdu, "Q", &wqid);
4721c7850f9SSasha Levin 		}
4731c7850f9SSasha Levin 	} else {
474bfc15268SAneesh Kumar K.V 		/*
475bfc15268SAneesh Kumar K.V 		 * update write_offset so our outlen get correct value
476bfc15268SAneesh Kumar K.V 		 */
477bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
478e2341580SSasha Levin 		old_fid = get_fid(p9dev, fid_val);
479e277a1b4SG. Campana 		if (join_path(new_fid, old_fid->path) != 0) {
480e277a1b4SG. Campana 			errno = ENAMETOOLONG;
481e277a1b4SG. Campana 			goto err_out;
482e277a1b4SG. Campana 		}
483e2341580SSasha Levin 		new_fid->uid    = old_fid->uid;
4841c7850f9SSasha Levin 	}
485bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
4865529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
487bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", nwqid);
488bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
4896c8ca053SAneesh Kumar K.V 	return;
4906c8ca053SAneesh Kumar K.V err_out:
4916c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
492ead43b01SAneesh Kumar K.V 	return;
4931c7850f9SSasha Levin }
4941c7850f9SSasha Levin 
495ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev,
496af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
4971c7850f9SSasha Levin {
498bfc15268SAneesh Kumar K.V 	char *uname;
499bfc15268SAneesh Kumar K.V 	char *aname;
5001c7850f9SSasha Levin 	struct stat st;
501bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
5021c7850f9SSasha Levin 	struct p9_fid *fid;
503c797b6c6SAneesh Kumar K.V 	u32 fid_val, afid, uid;
504bfc15268SAneesh Kumar K.V 
505c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid,
506c797b6c6SAneesh Kumar K.V 			    &uname, &aname, &uid);
5071c7850f9SSasha Levin 
50839257180SPekka Enberg 	free(uname);
50939257180SPekka Enberg 	free(aname);
51039257180SPekka Enberg 
51130204a77SAneesh Kumar K.V 	if (lstat(p9dev->root_dir, &st) < 0)
5126c8ca053SAneesh Kumar K.V 		goto err_out;
5131c7850f9SSasha Levin 
514c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
5151c7850f9SSasha Levin 
51631a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
517c797b6c6SAneesh Kumar K.V 	fid->uid = uid;
518e277a1b4SG. Campana 	if (join_path(fid, "/") != 0) {
519e277a1b4SG. Campana 		errno = ENAMETOOLONG;
520e277a1b4SG. Campana 		goto err_out;
521e277a1b4SG. Campana 	}
5221c7850f9SSasha Levin 
523bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
524bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
525bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
5266c8ca053SAneesh Kumar K.V 	return;
5276c8ca053SAneesh Kumar K.V err_out:
5286c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
529ead43b01SAneesh Kumar K.V 	return;
5301c7850f9SSasha Levin }
5311c7850f9SSasha Levin 
532c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev,
533c797b6c6SAneesh Kumar K.V 				struct stat *st, struct p9_stat_dotl *statl)
5345f900f6dSSasha Levin {
535c797b6c6SAneesh Kumar K.V 	memset(statl, 0, sizeof(*statl));
536c797b6c6SAneesh Kumar K.V 	statl->st_mode		= st->st_mode;
537c797b6c6SAneesh Kumar K.V 	statl->st_nlink		= st->st_nlink;
538506fd90bSSasha Levin 	statl->st_uid		= KUIDT_INIT(st->st_uid);
539506fd90bSSasha Levin 	statl->st_gid		= KGIDT_INIT(st->st_gid);
540c797b6c6SAneesh Kumar K.V 	statl->st_rdev		= st->st_rdev;
541c797b6c6SAneesh Kumar K.V 	statl->st_size		= st->st_size;
542c797b6c6SAneesh Kumar K.V 	statl->st_blksize	= st->st_blksize;
543c797b6c6SAneesh Kumar K.V 	statl->st_blocks	= st->st_blocks;
544c797b6c6SAneesh Kumar K.V 	statl->st_atime_sec	= st->st_atime;
545c797b6c6SAneesh Kumar K.V 	statl->st_atime_nsec	= st->st_atim.tv_nsec;
546c797b6c6SAneesh Kumar K.V 	statl->st_mtime_sec	= st->st_mtime;
547c797b6c6SAneesh Kumar K.V 	statl->st_mtime_nsec	= st->st_mtim.tv_nsec;
548c797b6c6SAneesh Kumar K.V 	statl->st_ctime_sec	= st->st_ctime;
549c797b6c6SAneesh Kumar K.V 	statl->st_ctime_nsec	= st->st_ctim.tv_nsec;
550c797b6c6SAneesh Kumar K.V 	/* Currently we only support BASIC fields in stat */
551c797b6c6SAneesh Kumar K.V 	statl->st_result_mask	= P9_STATS_BASIC;
552c797b6c6SAneesh Kumar K.V 	stat2qid(st, &statl->qid);
5531c7850f9SSasha Levin }
5541c7850f9SSasha Levin 
555ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev,
556af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
5571c7850f9SSasha Levin {
558bfc15268SAneesh Kumar K.V 	u64 offset;
559bfc15268SAneesh Kumar K.V 	u32 fid_val;
560c797b6c6SAneesh Kumar K.V 	u16 iov_cnt;
561c797b6c6SAneesh Kumar K.V 	void *iov_base;
562c797b6c6SAneesh Kumar K.V 	size_t iov_len;
563bfc15268SAneesh Kumar K.V 	u32 count, rcount;
564bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
565c797b6c6SAneesh Kumar K.V 
5661c7850f9SSasha Levin 
567bfc15268SAneesh Kumar K.V 	rcount = 0;
568bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
56931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
57050c479e0SAneesh Kumar K.V 
57150c479e0SAneesh Kumar K.V 	iov_base = pdu->in_iov[0].iov_base;
57250c479e0SAneesh Kumar K.V 	iov_len  = pdu->in_iov[0].iov_len;
57350c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->in_iov_cnt;
5745529bcd7SAsias He 	pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32);
5755529bcd7SAsias He 	pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32);
5766b163a87SAneesh Kumar K.V 	pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov,
577bfc15268SAneesh Kumar K.V 						   count,
5786b163a87SAneesh Kumar K.V 						   pdu->in_iov_cnt);
579bfc15268SAneesh Kumar K.V 	rcount = preadv(fid->fd, pdu->in_iov,
580bfc15268SAneesh Kumar K.V 			pdu->in_iov_cnt, offset);
581bfc15268SAneesh Kumar K.V 	if (rcount > count)
582bfc15268SAneesh Kumar K.V 		rcount = count;
583bfc15268SAneesh Kumar K.V 	/*
584bfc15268SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
585bfc15268SAneesh Kumar K.V 	 * pdu_writef works correctly.
586bfc15268SAneesh Kumar K.V 	 */
58750c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_base = iov_base;
58850c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_len  = iov_len;
58950c479e0SAneesh Kumar K.V 	pdu->in_iov_cnt         = iov_cnt;
590c797b6c6SAneesh Kumar K.V 
5915529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
592bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
593bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
594bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
595ead43b01SAneesh Kumar K.V 	return;
5961c7850f9SSasha Levin }
5971c7850f9SSasha Levin 
598c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent)
599c797b6c6SAneesh Kumar K.V {
600c797b6c6SAneesh Kumar K.V 	/*
601c797b6c6SAneesh Kumar K.V 	 * Size of each dirent:
602c797b6c6SAneesh Kumar K.V 	 * qid(13) + offset(8) + type(1) + name_len(2) + name
603c797b6c6SAneesh Kumar K.V 	 */
604c797b6c6SAneesh Kumar K.V 	return 24 + strlen(dent->d_name);
605c797b6c6SAneesh Kumar K.V }
606c797b6c6SAneesh Kumar K.V 
607c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev,
608c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
609c797b6c6SAneesh Kumar K.V {
610c797b6c6SAneesh Kumar K.V 	u32 fid_val;
611c797b6c6SAneesh Kumar K.V 	u32 count, rcount;
612c797b6c6SAneesh Kumar K.V 	struct stat st;
613c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
614c797b6c6SAneesh Kumar K.V 	struct dirent *dent;
615c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
616c797b6c6SAneesh Kumar K.V 	u64 offset, old_offset;
617c797b6c6SAneesh Kumar K.V 
618c797b6c6SAneesh Kumar K.V 	rcount = 0;
619c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
62031a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
621c797b6c6SAneesh Kumar K.V 
62232585666SSasha Levin 	if (!is_dir(fid)) {
62369bb4278SSasha Levin 		errno = EINVAL;
624c797b6c6SAneesh Kumar K.V 		goto err_out;
625c797b6c6SAneesh Kumar K.V 	}
626c797b6c6SAneesh Kumar K.V 
627c797b6c6SAneesh Kumar K.V 	/* Move the offset specified */
628c797b6c6SAneesh Kumar K.V 	seekdir(fid->dir, offset);
629c797b6c6SAneesh Kumar K.V 
630c797b6c6SAneesh Kumar K.V 	old_offset = offset;
631c797b6c6SAneesh Kumar K.V 	/* If reading a dir, fill the buffer with p9_stat entries */
632c797b6c6SAneesh Kumar K.V 	dent = readdir(fid->dir);
633c797b6c6SAneesh Kumar K.V 
634c797b6c6SAneesh Kumar K.V 	/* Skip the space for writing count */
635c797b6c6SAneesh Kumar K.V 	pdu->write_offset += sizeof(u32);
636c797b6c6SAneesh Kumar K.V 	while (dent) {
637c797b6c6SAneesh Kumar K.V 		u32 read;
638c797b6c6SAneesh Kumar K.V 		struct p9_qid qid;
639c797b6c6SAneesh Kumar K.V 
640c797b6c6SAneesh Kumar K.V 		if ((rcount + virtio_p9_dentry_size(dent)) > count) {
641c797b6c6SAneesh Kumar K.V 			/* seek to the previous offset and return */
642c797b6c6SAneesh Kumar K.V 			seekdir(fid->dir, old_offset);
643c797b6c6SAneesh Kumar K.V 			break;
644c797b6c6SAneesh Kumar K.V 		}
645c797b6c6SAneesh Kumar K.V 		old_offset = dent->d_off;
646c797b6c6SAneesh Kumar K.V 		lstat(rel_to_abs(p9dev, dent->d_name, full_path), &st);
647c797b6c6SAneesh Kumar K.V 		stat2qid(&st, &qid);
648c797b6c6SAneesh Kumar K.V 		read = pdu->write_offset;
649c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off,
650c797b6c6SAneesh Kumar K.V 				     dent->d_type, dent->d_name);
651c797b6c6SAneesh Kumar K.V 		rcount += pdu->write_offset - read;
652c797b6c6SAneesh Kumar K.V 		dent = readdir(fid->dir);
653c797b6c6SAneesh Kumar K.V 	}
654c797b6c6SAneesh Kumar K.V 
6555529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
656c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
657c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
658c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
659c797b6c6SAneesh Kumar K.V 	return;
660c797b6c6SAneesh Kumar K.V err_out:
661c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
662c797b6c6SAneesh Kumar K.V 	return;
663c797b6c6SAneesh Kumar K.V }
664c797b6c6SAneesh Kumar K.V 
665c797b6c6SAneesh Kumar K.V 
666c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev,
667af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
6681c7850f9SSasha Levin {
669bfc15268SAneesh Kumar K.V 	u32 fid_val;
670af045e53SAneesh Kumar K.V 	struct stat st;
671c797b6c6SAneesh Kumar K.V 	u64 request_mask;
672bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
673c797b6c6SAneesh Kumar K.V 	struct p9_stat_dotl statl;
6741c7850f9SSasha Levin 
675c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask);
67631a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
67730204a77SAneesh Kumar K.V 	if (lstat(fid->abs_path, &st) < 0)
6786c8ca053SAneesh Kumar K.V 		goto err_out;
6791c7850f9SSasha Levin 
680c797b6c6SAneesh Kumar K.V 	virtio_p9_fill_stat(p9dev, &st, &statl);
681c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "A", &statl);
682bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
683bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
684ead43b01SAneesh Kumar K.V 	return;
6856c8ca053SAneesh Kumar K.V err_out:
6866c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
6876c8ca053SAneesh Kumar K.V 	return;
6881c7850f9SSasha Levin }
6891c7850f9SSasha Levin 
690c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */
691c797b6c6SAneesh Kumar K.V /*
692c797b6c6SAneesh Kumar K.V  * Attribute flags.  These should be or-ed together to figure out what
693c797b6c6SAneesh Kumar K.V  * has been changed!
694c797b6c6SAneesh Kumar K.V  */
695c797b6c6SAneesh Kumar K.V #define ATTR_MODE	(1 << 0)
696c797b6c6SAneesh Kumar K.V #define ATTR_UID	(1 << 1)
697c797b6c6SAneesh Kumar K.V #define ATTR_GID	(1 << 2)
698c797b6c6SAneesh Kumar K.V #define ATTR_SIZE	(1 << 3)
699c797b6c6SAneesh Kumar K.V #define ATTR_ATIME	(1 << 4)
700c797b6c6SAneesh Kumar K.V #define ATTR_MTIME	(1 << 5)
701c797b6c6SAneesh Kumar K.V #define ATTR_CTIME	(1 << 6)
702c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET	(1 << 7)
703c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET	(1 << 8)
704c797b6c6SAneesh Kumar K.V #define ATTR_FORCE	(1 << 9) /* Not a change, but a change it */
705c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG	(1 << 10)
706c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID	(1 << 11)
707c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID	(1 << 12)
708c797b6c6SAneesh Kumar K.V #define ATTR_FILE	(1 << 13)
709c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV	(1 << 14)
710c797b6c6SAneesh Kumar K.V #define ATTR_OPEN	(1 << 15) /* Truncating from open(O_TRUNC) */
711c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET	(1 << 16)
712c797b6c6SAneesh Kumar K.V 
713c797b6c6SAneesh Kumar K.V #define ATTR_MASK    127
714c797b6c6SAneesh Kumar K.V 
715c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev,
716af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
7171c7850f9SSasha Levin {
718c797b6c6SAneesh Kumar K.V 	int ret = 0;
719bfc15268SAneesh Kumar K.V 	u32 fid_val;
720bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
721c797b6c6SAneesh Kumar K.V 	struct p9_iattr_dotl p9attr;
7221c7850f9SSasha Levin 
723c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr);
72431a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
7251c7850f9SSasha Levin 
726c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & ATTR_MODE) {
727c797b6c6SAneesh Kumar K.V 		ret = chmod(fid->abs_path, p9attr.mode);
728c797b6c6SAneesh Kumar K.V 		if (ret < 0)
729c797b6c6SAneesh Kumar K.V 			goto err_out;
730c797b6c6SAneesh Kumar K.V 	}
731c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) {
732c797b6c6SAneesh Kumar K.V 		struct timespec times[2];
733c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_ATIME) {
734c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_ATIME_SET) {
735c797b6c6SAneesh Kumar K.V 				times[0].tv_sec = p9attr.atime_sec;
736c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = p9attr.atime_nsec;
737c797b6c6SAneesh Kumar K.V 			} else {
738c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = UTIME_NOW;
739c797b6c6SAneesh Kumar K.V 			}
740c797b6c6SAneesh Kumar K.V 		} else {
741c797b6c6SAneesh Kumar K.V 			times[0].tv_nsec = UTIME_OMIT;
742c797b6c6SAneesh Kumar K.V 		}
743c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_MTIME) {
744c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_MTIME_SET) {
745c797b6c6SAneesh Kumar K.V 				times[1].tv_sec = p9attr.mtime_sec;
746c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = p9attr.mtime_nsec;
747c797b6c6SAneesh Kumar K.V 			} else {
748c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = UTIME_NOW;
749c797b6c6SAneesh Kumar K.V 			}
750c797b6c6SAneesh Kumar K.V 		} else
751c797b6c6SAneesh Kumar K.V 			times[1].tv_nsec = UTIME_OMIT;
752c797b6c6SAneesh Kumar K.V 
753c797b6c6SAneesh Kumar K.V 		ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW);
754c797b6c6SAneesh Kumar K.V 		if (ret < 0)
755c797b6c6SAneesh Kumar K.V 			goto err_out;
756c797b6c6SAneesh Kumar K.V 	}
757c797b6c6SAneesh Kumar K.V 	/*
758c797b6c6SAneesh Kumar K.V 	 * If the only valid entry in iattr is ctime we can call
759c797b6c6SAneesh Kumar K.V 	 * chown(-1,-1) to update the ctime of the file
760c797b6c6SAneesh Kumar K.V 	 */
761c797b6c6SAneesh Kumar K.V 	if ((p9attr.valid & (ATTR_UID | ATTR_GID)) ||
762c797b6c6SAneesh Kumar K.V 	    ((p9attr.valid & ATTR_CTIME)
763c797b6c6SAneesh Kumar K.V 	     && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
764c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_UID))
765506fd90bSSasha Levin 			p9attr.uid = KUIDT_INIT(-1);
766c797b6c6SAneesh Kumar K.V 
767c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_GID))
768506fd90bSSasha Levin 			p9attr.gid = KGIDT_INIT(-1);
769c797b6c6SAneesh Kumar K.V 
770506fd90bSSasha Levin 		ret = lchown(fid->abs_path, __kuid_val(p9attr.uid),
771506fd90bSSasha Levin 				__kgid_val(p9attr.gid));
772c797b6c6SAneesh Kumar K.V 		if (ret < 0)
773c797b6c6SAneesh Kumar K.V 			goto err_out;
774c797b6c6SAneesh Kumar K.V 	}
775c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_SIZE)) {
776c797b6c6SAneesh Kumar K.V 		ret = truncate(fid->abs_path, p9attr.size);
777c797b6c6SAneesh Kumar K.V 		if (ret < 0)
778c797b6c6SAneesh Kumar K.V 			goto err_out;
779c797b6c6SAneesh Kumar K.V 	}
7805529bcd7SAsias He 	*outlen = VIRTIO_9P_HDR_LEN;
781bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
782ead43b01SAneesh Kumar K.V 	return;
7834bc9734aSAneesh Kumar K.V err_out:
7844bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
7854bc9734aSAneesh Kumar K.V 	return;
7861c7850f9SSasha Levin }
7871c7850f9SSasha Levin 
788ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev,
789af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
7901c7850f9SSasha Levin {
7914bc9734aSAneesh Kumar K.V 
792bfc15268SAneesh Kumar K.V 	u64 offset;
793bfc15268SAneesh Kumar K.V 	u32 fid_val;
7944bc9734aSAneesh Kumar K.V 	u32 count;
7954bc9734aSAneesh Kumar K.V 	ssize_t res;
79650c479e0SAneesh Kumar K.V 	u16 iov_cnt;
79750c479e0SAneesh Kumar K.V 	void *iov_base;
79850c479e0SAneesh Kumar K.V 	size_t iov_len;
799bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
800b064b05aSAneesh Kumar K.V 	/* u32 fid + u64 offset + u32 count */
801b064b05aSAneesh Kumar K.V 	int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32);
8021c7850f9SSasha Levin 
803bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
80431a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
805af045e53SAneesh Kumar K.V 
80650c479e0SAneesh Kumar K.V 	iov_base = pdu->out_iov[0].iov_base;
80750c479e0SAneesh Kumar K.V 	iov_len  = pdu->out_iov[0].iov_len;
80850c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->out_iov_cnt;
80950c479e0SAneesh Kumar K.V 
810bfc15268SAneesh Kumar K.V 	/* Adjust the iovec to skip the header and meta data */
811b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size);
812b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_len -=  (sizeof(struct p9_msg) + twrite_size);
813bfc15268SAneesh Kumar K.V 	pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count,
8146b163a87SAneesh Kumar K.V 						    pdu->out_iov_cnt);
8154bc9734aSAneesh Kumar K.V 	res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset);
81650c479e0SAneesh Kumar K.V 	/*
81750c479e0SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
81850c479e0SAneesh Kumar K.V 	 * pdu_readf works correctly.
81950c479e0SAneesh Kumar K.V 	 */
82050c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_base = iov_base;
82150c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_len  = iov_len;
82250c479e0SAneesh Kumar K.V 	pdu->out_iov_cnt         = iov_cnt;
823c797b6c6SAneesh Kumar K.V 
8244bc9734aSAneesh Kumar K.V 	if (res < 0)
8254bc9734aSAneesh Kumar K.V 		goto err_out;
8264bc9734aSAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", res);
827bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
828bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
829ead43b01SAneesh Kumar K.V 	return;
8304bc9734aSAneesh Kumar K.V err_out:
8314bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
8324bc9734aSAneesh Kumar K.V 	return;
8331c7850f9SSasha Levin }
8341c7850f9SSasha Levin 
8356fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev,
8366fc5cd9bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
8376fc5cd9bSSasha Levin {
8386fc5cd9bSSasha Levin 	int ret;
8396fc5cd9bSSasha Levin 	u32 fid_val;
8406fc5cd9bSSasha Levin 	struct p9_fid *fid;
8416fc5cd9bSSasha Levin 
8426fc5cd9bSSasha Levin 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
84331a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
8446fc5cd9bSSasha Levin 
8459b604a9cSSasha Levin 	ret = remove(fid->abs_path);
8466fc5cd9bSSasha Levin 	if (ret < 0)
8476fc5cd9bSSasha Levin 		goto err_out;
8486fc5cd9bSSasha Levin 	*outlen = pdu->write_offset;
8496fc5cd9bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
8506fc5cd9bSSasha Levin 	return;
8516fc5cd9bSSasha Levin 
8526fc5cd9bSSasha Levin err_out:
8536fc5cd9bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
8546fc5cd9bSSasha Levin 	return;
8556fc5cd9bSSasha Levin }
8566fc5cd9bSSasha Levin 
857f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev,
858f161f28bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
859f161f28bSSasha Levin {
860f161f28bSSasha Levin 	int ret;
861f161f28bSSasha Levin 	u32 fid_val, new_fid_val;
862f161f28bSSasha Levin 	struct p9_fid *fid, *new_fid;
863f161f28bSSasha Levin 	char full_path[PATH_MAX], *new_name;
864f161f28bSSasha Levin 
865f161f28bSSasha Levin 	virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name);
86631a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
86731a6fb8dSSasha Levin 	new_fid = get_fid(p9dev, new_fid_val);
868f161f28bSSasha Levin 
869*d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), new_fid, new_name) != 0)
87032832dd1SG. Campana 		goto err_out;
8719bb99a82SG. Campana 
872f161f28bSSasha Levin 	ret = rename(fid->abs_path, full_path);
873f161f28bSSasha Levin 	if (ret < 0)
874f161f28bSSasha Levin 		goto err_out;
875f161f28bSSasha Levin 	*outlen = pdu->write_offset;
876f161f28bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
877f161f28bSSasha Levin 	return;
878f161f28bSSasha Levin 
879f161f28bSSasha Levin err_out:
880f161f28bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
881f161f28bSSasha Levin 	return;
882f161f28bSSasha Levin }
883f161f28bSSasha Levin 
884c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev,
885c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
886c797b6c6SAneesh Kumar K.V {
887c797b6c6SAneesh Kumar K.V 	int ret;
888c797b6c6SAneesh Kumar K.V 	u32 fid_val;
889c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
890c797b6c6SAneesh Kumar K.V 	char target_path[PATH_MAX];
891c797b6c6SAneesh Kumar K.V 
892c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
89331a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
894c797b6c6SAneesh Kumar K.V 
895c797b6c6SAneesh Kumar K.V 	memset(target_path, 0, PATH_MAX);
896c797b6c6SAneesh Kumar K.V 	ret = readlink(fid->abs_path, target_path, PATH_MAX - 1);
897c797b6c6SAneesh Kumar K.V 	if (ret < 0)
898c797b6c6SAneesh Kumar K.V 		goto err_out;
899c797b6c6SAneesh Kumar K.V 
900c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "s", target_path);
901c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
902c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
903c797b6c6SAneesh Kumar K.V 	return;
904c797b6c6SAneesh Kumar K.V err_out:
905c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
906c797b6c6SAneesh Kumar K.V 	return;
907c797b6c6SAneesh Kumar K.V }
908c797b6c6SAneesh Kumar K.V 
909c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev,
910c797b6c6SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
911c797b6c6SAneesh Kumar K.V {
912c797b6c6SAneesh Kumar K.V 	int ret;
913c797b6c6SAneesh Kumar K.V 	u64 fsid;
914c797b6c6SAneesh Kumar K.V 	u32 fid_val;
915c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
916c797b6c6SAneesh Kumar K.V 	struct statfs stat_buf;
917c797b6c6SAneesh Kumar K.V 
918c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
91931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
920c797b6c6SAneesh Kumar K.V 
921c797b6c6SAneesh Kumar K.V 	ret = statfs(fid->abs_path, &stat_buf);
922c797b6c6SAneesh Kumar K.V 	if (ret < 0)
923c797b6c6SAneesh Kumar K.V 		goto err_out;
924c797b6c6SAneesh Kumar K.V 	/* FIXME!! f_blocks needs update based on client msize */
925c797b6c6SAneesh Kumar K.V 	fsid = (unsigned int) stat_buf.f_fsid.__val[0] |
926c797b6c6SAneesh Kumar K.V 		(unsigned long long)stat_buf.f_fsid.__val[1] << 32;
927c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type,
928c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bsize, stat_buf.f_blocks,
929c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bfree, stat_buf.f_bavail,
930c797b6c6SAneesh Kumar K.V 			     stat_buf.f_files, stat_buf.f_ffree,
931c797b6c6SAneesh Kumar K.V 			     fsid, stat_buf.f_namelen);
932c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
933c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
934c797b6c6SAneesh Kumar K.V 	return;
935c797b6c6SAneesh Kumar K.V err_out:
936c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
937c797b6c6SAneesh Kumar K.V 	return;
938c797b6c6SAneesh Kumar K.V }
939c797b6c6SAneesh Kumar K.V 
940c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev,
941c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
942c797b6c6SAneesh Kumar K.V {
943c797b6c6SAneesh Kumar K.V 	int ret;
944c797b6c6SAneesh Kumar K.V 	char *name;
945c797b6c6SAneesh Kumar K.V 	struct stat st;
946c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
947c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
948c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
949c797b6c6SAneesh Kumar K.V 	u32 fid_val, mode, major, minor, gid;
950c797b6c6SAneesh Kumar K.V 
951c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode,
952c797b6c6SAneesh Kumar K.V 			    &major, &minor, &gid);
953c797b6c6SAneesh Kumar K.V 
95431a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
95532832dd1SG. Campana 
956*d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
9579bb99a82SG. Campana 		goto err_out;
9589bb99a82SG. Campana 
959c797b6c6SAneesh Kumar K.V 	ret = mknod(full_path, mode, makedev(major, minor));
960c797b6c6SAneesh Kumar K.V 	if (ret < 0)
961c797b6c6SAneesh Kumar K.V 		goto err_out;
962c797b6c6SAneesh Kumar K.V 
963c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
964c797b6c6SAneesh Kumar K.V 		goto err_out;
965c797b6c6SAneesh Kumar K.V 
966c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
967c797b6c6SAneesh Kumar K.V 	if (ret < 0)
968c797b6c6SAneesh Kumar K.V 		goto err_out;
969c797b6c6SAneesh Kumar K.V 
970c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
971c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
972c797b6c6SAneesh Kumar K.V 	free(name);
973c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
974c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
975c797b6c6SAneesh Kumar K.V 	return;
976c797b6c6SAneesh Kumar K.V err_out:
977c797b6c6SAneesh Kumar K.V 	free(name);
978c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
979c797b6c6SAneesh Kumar K.V 	return;
980c797b6c6SAneesh Kumar K.V }
981c797b6c6SAneesh Kumar K.V 
982c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev,
983c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
984c797b6c6SAneesh Kumar K.V {
985644140efSRussell King 	int ret, fd;
986c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
987c797b6c6SAneesh Kumar K.V 	u32 fid_val, datasync;
988c797b6c6SAneesh Kumar K.V 
989c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync);
99031a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
991c797b6c6SAneesh Kumar K.V 
992644140efSRussell King 	if (fid->dir)
993644140efSRussell King 		fd = dirfd(fid->dir);
994c797b6c6SAneesh Kumar K.V 	else
995644140efSRussell King 		fd = fid->fd;
996644140efSRussell King 
997644140efSRussell King 	if (datasync)
998644140efSRussell King 		ret = fdatasync(fd);
999644140efSRussell King 	else
1000644140efSRussell King 		ret = fsync(fd);
1001c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1002c797b6c6SAneesh Kumar K.V 		goto err_out;
1003c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1004c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1005c797b6c6SAneesh Kumar K.V 	return;
1006c797b6c6SAneesh Kumar K.V err_out:
1007c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1008c797b6c6SAneesh Kumar K.V 	return;
1009c797b6c6SAneesh Kumar K.V }
1010c797b6c6SAneesh Kumar K.V 
1011c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev,
1012c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1013c797b6c6SAneesh Kumar K.V {
1014c797b6c6SAneesh Kumar K.V 	int ret;
1015c797b6c6SAneesh Kumar K.V 	struct stat st;
1016c797b6c6SAneesh Kumar K.V 	u32 fid_val, gid;
1017c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
1018c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
1019c797b6c6SAneesh Kumar K.V 	char new_name[PATH_MAX];
1020c797b6c6SAneesh Kumar K.V 	char *old_path, *name;
1021c797b6c6SAneesh Kumar K.V 
1022c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid);
1023c797b6c6SAneesh Kumar K.V 
102431a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
102532832dd1SG. Campana 
1026*d4727f2bSG. Campana 	if (get_full_path(new_name, sizeof(new_name), dfid, name) != 0)
10279bb99a82SG. Campana 		goto err_out;
10289bb99a82SG. Campana 
1029c797b6c6SAneesh Kumar K.V 	ret = symlink(old_path, new_name);
1030c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1031c797b6c6SAneesh Kumar K.V 		goto err_out;
1032c797b6c6SAneesh Kumar K.V 
1033c797b6c6SAneesh Kumar K.V 	if (lstat(new_name, &st) < 0)
1034c797b6c6SAneesh Kumar K.V 		goto err_out;
1035c797b6c6SAneesh Kumar K.V 
1036c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
1037c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
1038c797b6c6SAneesh Kumar K.V 	free(name);
1039c797b6c6SAneesh Kumar K.V 	free(old_path);
1040c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1041c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1042c797b6c6SAneesh Kumar K.V 	return;
1043c797b6c6SAneesh Kumar K.V err_out:
1044c797b6c6SAneesh Kumar K.V 	free(name);
1045c797b6c6SAneesh Kumar K.V 	free(old_path);
1046c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1047c797b6c6SAneesh Kumar K.V 	return;
1048c797b6c6SAneesh Kumar K.V }
1049c797b6c6SAneesh Kumar K.V 
1050c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev,
1051c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
1052c797b6c6SAneesh Kumar K.V {
1053c797b6c6SAneesh Kumar K.V 	int ret;
1054c797b6c6SAneesh Kumar K.V 	char *name;
1055c797b6c6SAneesh Kumar K.V 	u32 fid_val, dfid_val;
1056c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid, *fid;
1057c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
1058c797b6c6SAneesh Kumar K.V 
1059c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name);
1060c797b6c6SAneesh Kumar K.V 
106131a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
106231a6fb8dSSasha Levin 	fid =  get_fid(p9dev, fid_val);
106332832dd1SG. Campana 
1064*d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
10659bb99a82SG. Campana 		goto err_out;
10669bb99a82SG. Campana 
1067c797b6c6SAneesh Kumar K.V 	ret = link(fid->abs_path, full_path);
1068c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1069c797b6c6SAneesh Kumar K.V 		goto err_out;
1070c797b6c6SAneesh Kumar K.V 	free(name);
1071c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1072c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1073c797b6c6SAneesh Kumar K.V 	return;
1074c797b6c6SAneesh Kumar K.V err_out:
1075c797b6c6SAneesh Kumar K.V 	free(name);
1076c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1077c797b6c6SAneesh Kumar K.V 	return;
1078c797b6c6SAneesh Kumar K.V 
1079c797b6c6SAneesh Kumar K.V }
1080c797b6c6SAneesh Kumar K.V 
1081c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev,
1082c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
1083c797b6c6SAneesh Kumar K.V {
1084c797b6c6SAneesh Kumar K.V 	u8 ret;
1085c797b6c6SAneesh Kumar K.V 	u32 fid_val;
1086c797b6c6SAneesh Kumar K.V 	struct p9_flock flock;
1087c797b6c6SAneesh Kumar K.V 
1088c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type,
1089c797b6c6SAneesh Kumar K.V 			    &flock.flags, &flock.start, &flock.length,
1090c797b6c6SAneesh Kumar K.V 			    &flock.proc_id, &flock.client_id);
1091c797b6c6SAneesh Kumar K.V 
1092c797b6c6SAneesh Kumar K.V 	/* Just return success */
1093c797b6c6SAneesh Kumar K.V 	ret = P9_LOCK_SUCCESS;
1094c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", ret);
1095c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1096c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1097c797b6c6SAneesh Kumar K.V 	free(flock.client_id);
1098c797b6c6SAneesh Kumar K.V 	return;
1099c797b6c6SAneesh Kumar K.V }
1100c797b6c6SAneesh Kumar K.V 
1101c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev,
1102c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1103c797b6c6SAneesh Kumar K.V {
1104c797b6c6SAneesh Kumar K.V 	u32 fid_val;
1105c797b6c6SAneesh Kumar K.V 	struct p9_getlock glock;
1106c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type,
1107c797b6c6SAneesh Kumar K.V 			    &glock.start, &glock.length, &glock.proc_id,
1108c797b6c6SAneesh Kumar K.V 			    &glock.client_id);
1109c797b6c6SAneesh Kumar K.V 
1110c797b6c6SAneesh Kumar K.V 	/* Just return success */
1111c797b6c6SAneesh Kumar K.V 	glock.type = F_UNLCK;
1112c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "bqqds", glock.type,
1113c797b6c6SAneesh Kumar K.V 			     glock.start, glock.length, glock.proc_id,
1114c797b6c6SAneesh Kumar K.V 			     glock.client_id);
1115c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1116c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1117c797b6c6SAneesh Kumar K.V 	free(glock.client_id);
1118c797b6c6SAneesh Kumar K.V 	return;
1119c797b6c6SAneesh Kumar K.V }
1120c797b6c6SAneesh Kumar K.V 
1121c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor)
1122c797b6c6SAneesh Kumar K.V {
1123c797b6c6SAneesh Kumar K.V 	int size = strlen(ancestor);
1124c797b6c6SAneesh Kumar K.V 	if (!strncmp(path, ancestor, size)) {
1125c797b6c6SAneesh Kumar K.V 		/*
1126c797b6c6SAneesh Kumar K.V 		 * Now check whether ancestor is a full name or
1127c797b6c6SAneesh Kumar K.V 		 * or directory component and not just part
1128c797b6c6SAneesh Kumar K.V 		 * of a name.
1129c797b6c6SAneesh Kumar K.V 		 */
1130c797b6c6SAneesh Kumar K.V 		if (path[size] == '\0' || path[size] == '/')
1131c797b6c6SAneesh Kumar K.V 			return 1;
1132c797b6c6SAneesh Kumar K.V 	}
1133c797b6c6SAneesh Kumar K.V 	return 0;
1134c797b6c6SAneesh Kumar K.V }
1135c797b6c6SAneesh Kumar K.V 
1136e277a1b4SG. Campana static int virtio_p9_fix_path(struct p9_fid *fid, char *old_name, char *new_name)
1137c797b6c6SAneesh Kumar K.V {
1138e277a1b4SG. Campana 	int ret;
1139e277a1b4SG. Campana 	char *p, tmp_name[PATH_MAX];
1140c797b6c6SAneesh Kumar K.V 	size_t rp_sz = strlen(old_name);
1141c797b6c6SAneesh Kumar K.V 
1142e277a1b4SG. Campana 	if (rp_sz == strlen(fid->path)) {
1143c797b6c6SAneesh Kumar K.V 		/* replace the full name */
1144e277a1b4SG. Campana 		p = new_name;
1145e277a1b4SG. Campana 	} else {
1146c797b6c6SAneesh Kumar K.V 		/* save the trailing path details */
1147e277a1b4SG. Campana 		ret = snprintf(tmp_name, sizeof(tmp_name), "%s%s", new_name, fid->path + rp_sz);
1148e277a1b4SG. Campana 		if (ret >= (int)sizeof(tmp_name))
1149e277a1b4SG. Campana 			return -1;
1150e277a1b4SG. Campana 		p = tmp_name;
1151e277a1b4SG. Campana 	}
1152e277a1b4SG. Campana 
1153e277a1b4SG. Campana 	return join_path(fid, p);
1154c797b6c6SAneesh Kumar K.V }
1155c797b6c6SAneesh Kumar K.V 
1156e2341580SSasha Levin static void rename_fids(struct p9_dev *p9dev, char *old_name, char *new_name)
1157e2341580SSasha Levin {
1158e2341580SSasha Levin 	struct rb_node *node = rb_first(&p9dev->fids);
1159e2341580SSasha Levin 
1160e2341580SSasha Levin 	while (node) {
1161e2341580SSasha Levin 		struct p9_fid *fid = rb_entry(node, struct p9_fid, node);
1162e2341580SSasha Levin 
1163e2341580SSasha Levin 		if (fid->fid != P9_NOFID && virtio_p9_ancestor(fid->path, old_name)) {
1164e277a1b4SG. Campana 				virtio_p9_fix_path(fid, old_name, new_name);
1165e2341580SSasha Levin 		}
1166e2341580SSasha Levin 		node = rb_next(node);
1167e2341580SSasha Levin 	}
1168e2341580SSasha Levin }
1169e2341580SSasha Levin 
1170c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev,
1171c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1172c797b6c6SAneesh Kumar K.V {
1173e2341580SSasha Levin 	int ret;
1174c797b6c6SAneesh Kumar K.V 	char *old_name, *new_name;
1175c797b6c6SAneesh Kumar K.V 	u32 old_dfid_val, new_dfid_val;
1176c797b6c6SAneesh Kumar K.V 	struct p9_fid *old_dfid, *new_dfid;
1177c797b6c6SAneesh Kumar K.V 	char old_full_path[PATH_MAX], new_full_path[PATH_MAX];
1178c797b6c6SAneesh Kumar K.V 
1179c797b6c6SAneesh Kumar K.V 
1180c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name,
1181c797b6c6SAneesh Kumar K.V 			    &new_dfid_val, &new_name);
1182c797b6c6SAneesh Kumar K.V 
118331a6fb8dSSasha Levin 	old_dfid = get_fid(p9dev, old_dfid_val);
118431a6fb8dSSasha Levin 	new_dfid = get_fid(p9dev, new_dfid_val);
1185c797b6c6SAneesh Kumar K.V 
1186*d4727f2bSG. Campana 	if (get_full_path(old_full_path, sizeof(old_full_path), old_dfid, old_name) != 0)
118732832dd1SG. Campana 		goto err_out;
118832832dd1SG. Campana 
1189*d4727f2bSG. Campana 	if (get_full_path(new_full_path, sizeof(new_full_path), new_dfid, new_name) != 0)
119032832dd1SG. Campana 		goto err_out;
11919bb99a82SG. Campana 
1192c797b6c6SAneesh Kumar K.V 	ret = rename(old_full_path, new_full_path);
1193c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1194c797b6c6SAneesh Kumar K.V 		goto err_out;
1195c797b6c6SAneesh Kumar K.V 	/*
1196c797b6c6SAneesh Kumar K.V 	 * Now fix path in other fids, if the renamed path is part of
1197c797b6c6SAneesh Kumar K.V 	 * that.
1198c797b6c6SAneesh Kumar K.V 	 */
1199e2341580SSasha Levin 	rename_fids(p9dev, old_name, new_name);
1200c797b6c6SAneesh Kumar K.V 	free(old_name);
1201c797b6c6SAneesh Kumar K.V 	free(new_name);
1202c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1203c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1204c797b6c6SAneesh Kumar K.V 	return;
1205c797b6c6SAneesh Kumar K.V err_out:
1206c797b6c6SAneesh Kumar K.V 	free(old_name);
1207c797b6c6SAneesh Kumar K.V 	free(new_name);
1208c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1209c797b6c6SAneesh Kumar K.V 	return;
1210c797b6c6SAneesh Kumar K.V }
1211c797b6c6SAneesh Kumar K.V 
1212c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev,
1213c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1214c797b6c6SAneesh Kumar K.V {
1215c797b6c6SAneesh Kumar K.V 	int ret;
1216c797b6c6SAneesh Kumar K.V 	char *name;
1217c797b6c6SAneesh Kumar K.V 	u32 fid_val, flags;
1218c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
1219c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
1220c797b6c6SAneesh Kumar K.V 
1221c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags);
122231a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
1223c797b6c6SAneesh Kumar K.V 
1224*d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), fid, name) != 0)
122532832dd1SG. Campana 		goto err_out;
12269bb99a82SG. Campana 
1227c797b6c6SAneesh Kumar K.V 	ret = remove(full_path);
1228c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1229c797b6c6SAneesh Kumar K.V 		goto err_out;
1230c797b6c6SAneesh Kumar K.V 	free(name);
1231c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1232c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1233c797b6c6SAneesh Kumar K.V 	return;
1234c797b6c6SAneesh Kumar K.V err_out:
1235c797b6c6SAneesh Kumar K.V 	free(name);
1236c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1237c797b6c6SAneesh Kumar K.V 	return;
1238c797b6c6SAneesh Kumar K.V }
1239c797b6c6SAneesh Kumar K.V 
12405cc808aaSSasha Levin static void virtio_p9_flush(struct p9_dev *p9dev,
12415cc808aaSSasha Levin 				struct p9_pdu *pdu, u32 *outlen)
12425cc808aaSSasha Levin {
12435cc808aaSSasha Levin 	u16 tag, oldtag;
12445cc808aaSSasha Levin 
12455cc808aaSSasha Levin 	virtio_p9_pdu_readf(pdu, "ww", &tag, &oldtag);
12465cc808aaSSasha Levin 	virtio_p9_pdu_writef(pdu, "w", tag);
12475cc808aaSSasha Levin 	*outlen = pdu->write_offset;
12485cc808aaSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
12495cc808aaSSasha Levin 
12505cc808aaSSasha Levin 	return;
12515cc808aaSSasha Levin }
12525cc808aaSSasha Levin 
1253c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev,
1254c797b6c6SAneesh Kumar K.V 				 struct p9_pdu *pdu, u32 *outlen)
1255c797b6c6SAneesh Kumar K.V {
1256c797b6c6SAneesh Kumar K.V 	return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen);
1257c797b6c6SAneesh Kumar K.V }
1258c797b6c6SAneesh Kumar K.V 
1259ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev,
1260af045e53SAneesh Kumar K.V 			struct p9_pdu *pdu, u32 *outlen);
1261b4422bf3SAneesh Kumar K.V 
1262c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */
1263c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74
1264c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76
1265c797b6c6SAneesh Kumar K.V 
1266c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = {
1267c797b6c6SAneesh Kumar K.V 	[P9_TREADDIR]     = virtio_p9_readdir,
1268c797b6c6SAneesh Kumar K.V 	[P9_TSTATFS]      = virtio_p9_statfs,
1269c797b6c6SAneesh Kumar K.V 	[P9_TGETATTR]     = virtio_p9_getattr,
1270c797b6c6SAneesh Kumar K.V 	[P9_TSETATTR]     = virtio_p9_setattr,
1271c797b6c6SAneesh Kumar K.V 	[P9_TXATTRWALK]   = virtio_p9_eopnotsupp,
1272c797b6c6SAneesh Kumar K.V 	[P9_TXATTRCREATE] = virtio_p9_eopnotsupp,
1273c797b6c6SAneesh Kumar K.V 	[P9_TMKNOD]       = virtio_p9_mknod,
1274c797b6c6SAneesh Kumar K.V 	[P9_TLOCK]        = virtio_p9_lock,
1275c797b6c6SAneesh Kumar K.V 	[P9_TGETLOCK]     = virtio_p9_getlock,
1276c797b6c6SAneesh Kumar K.V 	[P9_TRENAMEAT]    = virtio_p9_renameat,
1277c797b6c6SAneesh Kumar K.V 	[P9_TREADLINK]    = virtio_p9_readlink,
1278c797b6c6SAneesh Kumar K.V 	[P9_TUNLINKAT]    = virtio_p9_unlinkat,
1279c797b6c6SAneesh Kumar K.V 	[P9_TMKDIR]       = virtio_p9_mkdir,
1280b4422bf3SAneesh Kumar K.V 	[P9_TVERSION]     = virtio_p9_version,
1281c797b6c6SAneesh Kumar K.V 	[P9_TLOPEN]       = virtio_p9_open,
1282b4422bf3SAneesh Kumar K.V 	[P9_TATTACH]      = virtio_p9_attach,
1283b4422bf3SAneesh Kumar K.V 	[P9_TWALK]        = virtio_p9_walk,
1284c797b6c6SAneesh Kumar K.V 	[P9_TCLUNK]       = virtio_p9_clunk,
1285c797b6c6SAneesh Kumar K.V 	[P9_TFSYNC]       = virtio_p9_fsync,
1286b4422bf3SAneesh Kumar K.V 	[P9_TREAD]        = virtio_p9_read,
12875cc808aaSSasha Levin 	[P9_TFLUSH]       = virtio_p9_flush,
1288c797b6c6SAneesh Kumar K.V 	[P9_TLINK]        = virtio_p9_link,
1289c797b6c6SAneesh Kumar K.V 	[P9_TSYMLINK]     = virtio_p9_symlink,
1290c797b6c6SAneesh Kumar K.V 	[P9_TLCREATE]     = virtio_p9_create,
1291b4422bf3SAneesh Kumar K.V 	[P9_TWRITE]       = virtio_p9_write,
12926fc5cd9bSSasha Levin 	[P9_TREMOVE]      = virtio_p9_remove,
1293f161f28bSSasha Levin 	[P9_TRENAME]      = virtio_p9_rename,
1294b4422bf3SAneesh Kumar K.V };
1295b4422bf3SAneesh Kumar K.V 
1296af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq)
1297af045e53SAneesh Kumar K.V {
1298af045e53SAneesh Kumar K.V 	struct p9_pdu *pdu = calloc(1, sizeof(*pdu));
1299af045e53SAneesh Kumar K.V 	if (!pdu)
1300af045e53SAneesh Kumar K.V 		return NULL;
1301af045e53SAneesh Kumar K.V 
1302bfc15268SAneesh Kumar K.V 	/* skip the pdu header p9_msg */
13035529bcd7SAsias He 	pdu->read_offset	= VIRTIO_9P_HDR_LEN;
13045529bcd7SAsias He 	pdu->write_offset	= VIRTIO_9P_HDR_LEN;
1305af045e53SAneesh Kumar K.V 	pdu->queue_head		= virt_queue__get_inout_iov(kvm, vq, pdu->in_iov,
1306a8a44649SAsias He 					pdu->out_iov, &pdu->in_iov_cnt, &pdu->out_iov_cnt);
1307af045e53SAneesh Kumar K.V 	return pdu;
1308af045e53SAneesh Kumar K.V }
1309af045e53SAneesh Kumar K.V 
1310af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu)
1311af045e53SAneesh Kumar K.V {
1312af045e53SAneesh Kumar K.V 	struct p9_msg *msg;
1313af045e53SAneesh Kumar K.V 	/*
1314af045e53SAneesh Kumar K.V 	 * we can peek directly into pdu for a u8
1315af045e53SAneesh Kumar K.V 	 * value. The host endianess won't be an issue
1316af045e53SAneesh Kumar K.V 	 */
1317af045e53SAneesh Kumar K.V 	msg = pdu->out_iov[0].iov_base;
1318af045e53SAneesh Kumar K.V 	return msg->cmd;
1319af045e53SAneesh Kumar K.V }
1320af045e53SAneesh Kumar K.V 
1321b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job)
13221c7850f9SSasha Levin {
1323af045e53SAneesh Kumar K.V 	u8 cmd;
1324b4422bf3SAneesh Kumar K.V 	u32 len = 0;
1325b4422bf3SAneesh Kumar K.V 	p9_handler *handler;
1326b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev;
1327af045e53SAneesh Kumar K.V 	struct virt_queue *vq;
1328af045e53SAneesh Kumar K.V 	struct p9_pdu *p9pdu;
13291c7850f9SSasha Levin 
1330b4422bf3SAneesh Kumar K.V 	vq = job->vq;
1331b4422bf3SAneesh Kumar K.V 	p9dev = job->p9dev;
13321c7850f9SSasha Levin 
1333af045e53SAneesh Kumar K.V 	p9pdu = virtio_p9_pdu_init(kvm, vq);
1334af045e53SAneesh Kumar K.V 	cmd = virtio_p9_get_cmd(p9pdu);
1335af045e53SAneesh Kumar K.V 
1336c797b6c6SAneesh Kumar K.V 	if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) ||
1337c797b6c6SAneesh Kumar K.V 	    !virtio_9p_dotl_handler[cmd])
133897b408afSAneesh Kumar K.V 		handler = virtio_p9_eopnotsupp;
1339dd78d9eaSAneesh Kumar K.V 	else
1340c797b6c6SAneesh Kumar K.V 		handler = virtio_9p_dotl_handler[cmd];
1341c797b6c6SAneesh Kumar K.V 
1342af045e53SAneesh Kumar K.V 	handler(p9dev, p9pdu, &len);
1343af045e53SAneesh Kumar K.V 	virt_queue__set_used_elem(vq, p9pdu->queue_head, len);
1344af045e53SAneesh Kumar K.V 	free(p9pdu);
13451c7850f9SSasha Levin 	return true;
13461c7850f9SSasha Levin }
13471c7850f9SSasha Levin 
13481c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param)
13491c7850f9SSasha Levin {
1350b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job = (struct p9_dev_job *)param;
1351b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev   = job->p9dev;
1352b4422bf3SAneesh Kumar K.V 	struct virt_queue *vq  = job->vq;
13531c7850f9SSasha Levin 
13541c7850f9SSasha Levin 	while (virt_queue__available(vq)) {
1355b4422bf3SAneesh Kumar K.V 		virtio_p9_do_io_request(kvm, job);
135602eca50cSAsias He 		p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs);
13571c7850f9SSasha Levin 	}
13581c7850f9SSasha Levin }
13591c7850f9SSasha Levin 
1360c5ae742bSSasha Levin static u8 *get_config(struct kvm *kvm, void *dev)
13611c7850f9SSasha Levin {
1362c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
13631c7850f9SSasha Levin 
1364c5ae742bSSasha Levin 	return ((u8 *)(p9dev->config));
1365c7838fbdSSasha Levin }
1366c7838fbdSSasha Levin 
1367c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev)
1368c7838fbdSSasha Levin {
1369c7838fbdSSasha Levin 	return 1 << VIRTIO_9P_MOUNT_TAG;
1370c7838fbdSSasha Levin }
1371c7838fbdSSasha Levin 
1372c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
1373c7838fbdSSasha Levin {
1374c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1375e0ea1859SMarc Zyngier 	struct virtio_9p_config *conf = p9dev->config;
1376c7838fbdSSasha Levin 
1377c7838fbdSSasha Levin 	p9dev->features = features;
1378e0ea1859SMarc Zyngier 	conf->tag_len = virtio_host_to_guest_u16(&p9dev->vdev, conf->tag_len);
1379c7838fbdSSasha Levin }
1380c7838fbdSSasha Levin 
1381c59ba304SWill Deacon static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align,
1382c59ba304SWill Deacon 		   u32 pfn)
1383c7838fbdSSasha Levin {
1384c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1385b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job;
1386b4422bf3SAneesh Kumar K.V 	struct virt_queue *queue;
1387c7838fbdSSasha Levin 	void *p;
13881c7850f9SSasha Levin 
1389312c62d1SSasha Levin 	compat__remove_message(compat_id);
1390e59662b3SSasha Levin 
1391c7838fbdSSasha Levin 	queue		= &p9dev->vqs[vq];
1392c7838fbdSSasha Levin 	queue->pfn	= pfn;
1393e7e2950aSSasha Levin 	p		= virtio_get_vq(kvm, queue->pfn, page_size);
1394c7838fbdSSasha Levin 	job		= &p9dev->jobs[vq];
13951c7850f9SSasha Levin 
1396c59ba304SWill Deacon 	vring_init(&queue->vring, VIRTQUEUE_NUM, p, align);
1397e0ea1859SMarc Zyngier 	virtio_init_device_vq(&p9dev->vdev, queue);
13981c7850f9SSasha Levin 
1399b4422bf3SAneesh Kumar K.V 	*job		= (struct p9_dev_job) {
1400b4422bf3SAneesh Kumar K.V 		.vq		= queue,
1401b4422bf3SAneesh Kumar K.V 		.p9dev		= p9dev,
1402b4422bf3SAneesh Kumar K.V 	};
1403df0c7f57SSasha Levin 	thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job);
140460eb42d5SSasha Levin 
1405c7838fbdSSasha Levin 	return 0;
14061c7850f9SSasha Levin }
14071c7850f9SSasha Levin 
1408c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq)
1409c7838fbdSSasha Levin {
1410c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
14111c7850f9SSasha Levin 
1412c7838fbdSSasha Levin 	thread_pool__do_job(&p9dev->jobs[vq].job_id);
1413c7838fbdSSasha Levin 
1414c7838fbdSSasha Levin 	return 0;
1415c7838fbdSSasha Levin }
1416c7838fbdSSasha Levin 
1417c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq)
1418c7838fbdSSasha Levin {
1419c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1420c7838fbdSSasha Levin 
1421c7838fbdSSasha Levin 	return p9dev->vqs[vq].pfn;
1422c7838fbdSSasha Levin }
1423c7838fbdSSasha Levin 
1424c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
1425c7838fbdSSasha Levin {
1426c7838fbdSSasha Levin 	return VIRTQUEUE_NUM;
1427c7838fbdSSasha Levin }
1428c7838fbdSSasha Levin 
14297aba29c1SWill Deacon static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size)
14307aba29c1SWill Deacon {
14317aba29c1SWill Deacon 	/* FIXME: dynamic */
14327aba29c1SWill Deacon 	return size;
14337aba29c1SWill Deacon }
14347aba29c1SWill Deacon 
143515542babSAndre Przywara struct virtio_ops p9_dev_virtio_ops = {
1436c7838fbdSSasha Levin 	.get_config		= get_config,
1437c7838fbdSSasha Levin 	.get_host_features	= get_host_features,
1438c7838fbdSSasha Levin 	.set_guest_features	= set_guest_features,
1439c7838fbdSSasha Levin 	.init_vq		= init_vq,
1440c7838fbdSSasha Levin 	.notify_vq		= notify_vq,
1441c7838fbdSSasha Levin 	.get_pfn_vq		= get_pfn_vq,
1442c7838fbdSSasha Levin 	.get_size_vq		= get_size_vq,
14437aba29c1SWill Deacon 	.set_size_vq		= set_size_vq,
1444c7838fbdSSasha Levin };
14451c47ce69SSasha Levin 
1446cac9e8fdSSasha Levin int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
1447cac9e8fdSSasha Levin {
1448cac9e8fdSSasha Levin 	char *tag_name;
1449cac9e8fdSSasha Levin 	char tmp[PATH_MAX];
1450cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1451cac9e8fdSSasha Levin 
1452cac9e8fdSSasha Levin 	/*
1453cac9e8fdSSasha Levin 	 * 9p dir can be of the form dirname,tag_name or
1454cac9e8fdSSasha Levin 	 * just dirname. In the later case we use the
1455cac9e8fdSSasha Levin 	 * default tag name
1456cac9e8fdSSasha Levin 	 */
1457cac9e8fdSSasha Levin 	tag_name = strstr(arg, ",");
1458cac9e8fdSSasha Levin 	if (tag_name) {
1459cac9e8fdSSasha Levin 		*tag_name = '\0';
1460cac9e8fdSSasha Levin 		tag_name++;
1461cac9e8fdSSasha Levin 	}
1462cac9e8fdSSasha Levin 	if (realpath(arg, tmp)) {
1463cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, tmp, tag_name) < 0)
1464cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1465cac9e8fdSSasha Levin 	} else
1466cac9e8fdSSasha Levin 		die("Failed resolving 9p path");
1467cac9e8fdSSasha Levin 	return 0;
1468cac9e8fdSSasha Levin }
1469cac9e8fdSSasha Levin 
1470cac9e8fdSSasha Levin int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset)
1471cac9e8fdSSasha Levin {
1472cac9e8fdSSasha Levin 	char path[PATH_MAX];
1473cac9e8fdSSasha Levin 	struct stat st;
1474cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1475cac9e8fdSSasha Levin 
1476cac9e8fdSSasha Levin 	if (stat(arg, &st) == 0 &&
1477cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1478cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1479cac9e8fdSSasha Levin 
1480cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1481cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1482cac9e8fdSSasha Levin 
1483cac9e8fdSSasha Levin 		if (realpath(arg, tmp) == 0 ||
1484cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1485cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1486cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = 1;
1487cac9e8fdSSasha Levin 		return 0;
1488cac9e8fdSSasha Levin 	}
1489cac9e8fdSSasha Levin 
1490cac9e8fdSSasha Levin 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
1491cac9e8fdSSasha Levin 
1492cac9e8fdSSasha Levin 	if (stat(path, &st) == 0 &&
1493cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1494cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1495cac9e8fdSSasha Levin 
1496cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1497cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1498cac9e8fdSSasha Levin 
1499cac9e8fdSSasha Levin 		if (realpath(path, tmp) == 0 ||
1500cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1501cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1502cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
1503cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1504cac9e8fdSSasha Levin 		kvm_setup_resolv(arg);
1505cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
1506cac9e8fdSSasha Levin 		kvm->cfg.custom_rootfs_name = arg;
1507cac9e8fdSSasha Levin 		return 0;
1508cac9e8fdSSasha Levin 	}
1509cac9e8fdSSasha Levin 
1510cac9e8fdSSasha Levin 	return -1;
1511cac9e8fdSSasha Levin }
1512cac9e8fdSSasha Levin 
15131c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm)
15141c47ce69SSasha Levin {
15151c47ce69SSasha Levin 	struct p9_dev *p9dev;
15161c47ce69SSasha Levin 
15171c47ce69SSasha Levin 	list_for_each_entry(p9dev, &devs, list) {
151802eca50cSAsias He 		virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops,
1519d97dadecSWill Deacon 			    VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_9P,
1520ae06ce71SWill Deacon 			    VIRTIO_ID_9P, PCI_CLASS_9P);
1521c7838fbdSSasha Levin 	}
1522c7838fbdSSasha Levin 
1523c7838fbdSSasha Levin 	return 0;
1524c7838fbdSSasha Levin }
152549a8afd1SSasha Levin virtio_dev_init(virtio_9p__init);
1526c7838fbdSSasha Levin 
1527c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name)
1528c7838fbdSSasha Levin {
1529c7838fbdSSasha Levin 	struct p9_dev *p9dev;
153054f6802dSPekka Enberg 	int err = 0;
15311c7850f9SSasha Levin 
1532b4422bf3SAneesh Kumar K.V 	p9dev = calloc(1, sizeof(*p9dev));
1533b4422bf3SAneesh Kumar K.V 	if (!p9dev)
153454f6802dSPekka Enberg 		return -ENOMEM;
153554f6802dSPekka Enberg 
1536b4422bf3SAneesh Kumar K.V 	if (!tag_name)
15375529bcd7SAsias He 		tag_name = VIRTIO_9P_DEFAULT_TAG;
153854f6802dSPekka Enberg 
1539b4422bf3SAneesh Kumar K.V 	p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1);
154054f6802dSPekka Enberg 	if (p9dev->config == NULL) {
154154f6802dSPekka Enberg 		err = -ENOMEM;
1542b4422bf3SAneesh Kumar K.V 		goto free_p9dev;
154354f6802dSPekka Enberg 	}
15441c7850f9SSasha Levin 
1545e277a1b4SG. Campana 	strncpy(p9dev->root_dir, root, sizeof(p9dev->root_dir));
1546e277a1b4SG. Campana 	p9dev->root_dir[sizeof(p9dev->root_dir)-1] = '\x00';
1547e277a1b4SG. Campana 
1548b4422bf3SAneesh Kumar K.V 	p9dev->config->tag_len = strlen(tag_name);
154954f6802dSPekka Enberg 	if (p9dev->config->tag_len > MAX_TAG_LEN) {
155054f6802dSPekka Enberg 		err = -EINVAL;
1551b4422bf3SAneesh Kumar K.V 		goto free_p9dev_config;
155254f6802dSPekka Enberg 	}
15531c7850f9SSasha Levin 
1554c7838fbdSSasha Levin 	memcpy(&p9dev->config->tag, tag_name, strlen(tag_name));
15551c7850f9SSasha Levin 
1556c7838fbdSSasha Levin 	list_add(&p9dev->list, &devs);
1557b4422bf3SAneesh Kumar K.V 
1558d278197dSAsias He 	if (compat_id == -1)
155952f34d2cSAsias He 		compat_id = virtio_compat_add_message("virtio-9p", "CONFIG_NET_9P_VIRTIO");
1560e59662b3SSasha Levin 
156154f6802dSPekka Enberg 	return err;
156254f6802dSPekka Enberg 
1563b4422bf3SAneesh Kumar K.V free_p9dev_config:
1564b4422bf3SAneesh Kumar K.V 	free(p9dev->config);
1565b4422bf3SAneesh Kumar K.V free_p9dev:
1566b4422bf3SAneesh Kumar K.V 	free(p9dev);
156754f6802dSPekka Enberg 	return err;
15681c7850f9SSasha Levin }
1569