xref: /kvmtool/virtio/9p.c (revision b09224228296d9febf120f3aa956964cc01a14b5)
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 
94c797b6c6SAneesh Kumar K.V static void stat2qid(struct stat *st, struct p9_qid *qid)
951c7850f9SSasha Levin {
961c7850f9SSasha Levin 	*qid = (struct p9_qid) {
971c7850f9SSasha Levin 		.path		= st->st_ino,
981c7850f9SSasha Levin 		.version	= st->st_mtime,
991c7850f9SSasha Levin 	};
1001c7850f9SSasha Levin 
1011c7850f9SSasha Levin 	if (S_ISDIR(st->st_mode))
1021c7850f9SSasha Levin 		qid->type	|= P9_QTDIR;
1031c7850f9SSasha Levin }
1041c7850f9SSasha Levin 
105b4422bf3SAneesh Kumar K.V static void close_fid(struct p9_dev *p9dev, u32 fid)
1061c7850f9SSasha Levin {
107e2341580SSasha Levin 	struct p9_fid *pfid = get_fid(p9dev, fid);
108e2341580SSasha Levin 
109e2341580SSasha Levin 	if (pfid->fd > 0)
110e2341580SSasha Levin 		close(pfid->fd);
111e2341580SSasha Levin 
112e2341580SSasha Levin 	if (pfid->dir)
113e2341580SSasha Levin 		closedir(pfid->dir);
114e2341580SSasha Levin 
115e2341580SSasha Levin 	rb_erase(&pfid->node, &p9dev->fids);
116e2341580SSasha Levin 	free(pfid);
1171c7850f9SSasha Levin }
118e2341580SSasha Levin 
119bfc15268SAneesh Kumar K.V static void virtio_p9_set_reply_header(struct p9_pdu *pdu, u32 size)
1201c7850f9SSasha Levin {
121bfc15268SAneesh Kumar K.V 	u8 cmd;
122bfc15268SAneesh Kumar K.V 	u16 tag;
123bfc15268SAneesh Kumar K.V 
124bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32);
125bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "bw", &cmd, &tag);
126bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
127bfc15268SAneesh Kumar K.V 	/* cmd + 1 is the reply message */
128bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", size, cmd + 1, tag);
1291c7850f9SSasha Levin }
1301c7850f9SSasha Levin 
1316b163a87SAneesh Kumar K.V static u16 virtio_p9_update_iov_cnt(struct iovec iov[], u32 count, int iov_cnt)
1326b163a87SAneesh Kumar K.V {
1336b163a87SAneesh Kumar K.V 	int i;
1346b163a87SAneesh Kumar K.V 	u32 total = 0;
1356b163a87SAneesh Kumar K.V 	for (i = 0; (i < iov_cnt) && (total < count); i++) {
1366b163a87SAneesh Kumar K.V 		if (total + iov[i].iov_len > count) {
1376b163a87SAneesh Kumar K.V 			/* we don't need this iov fully */
1386b163a87SAneesh Kumar K.V 			iov[i].iov_len -= ((total + iov[i].iov_len) - count);
1396b163a87SAneesh Kumar K.V 			i++;
1406b163a87SAneesh Kumar K.V 			break;
1416b163a87SAneesh Kumar K.V 		}
1426b163a87SAneesh Kumar K.V 		total += iov[i].iov_len;
1436b163a87SAneesh Kumar K.V 	}
1446b163a87SAneesh Kumar K.V 	return i;
1456b163a87SAneesh Kumar K.V }
1466b163a87SAneesh Kumar K.V 
147eee1ba8eSAneesh Kumar K.V static void virtio_p9_error_reply(struct p9_dev *p9dev,
148eee1ba8eSAneesh Kumar K.V 				  struct p9_pdu *pdu, int err, u32 *outlen)
149eee1ba8eSAneesh Kumar K.V {
150bfc15268SAneesh Kumar K.V 	u16 tag;
151eee1ba8eSAneesh Kumar K.V 
1529c2e1d1aSSuzuki K. Poulose 	/* EMFILE at server implies ENFILE for the VM */
1539c2e1d1aSSuzuki K. Poulose 	if (err == EMFILE)
1549c2e1d1aSSuzuki K. Poulose 		err = ENFILE;
1559c2e1d1aSSuzuki K. Poulose 
1565529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
157c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", err);
158bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
159eee1ba8eSAneesh Kumar K.V 
160c797b6c6SAneesh Kumar K.V 	/* read the tag from input */
161bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32) + sizeof(u8);
162bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "w", &tag);
163bfc15268SAneesh Kumar K.V 
164c797b6c6SAneesh Kumar K.V 	/* Update the header */
165bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
166c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", *outlen, P9_RLERROR, tag);
167eee1ba8eSAneesh Kumar K.V }
168eee1ba8eSAneesh Kumar K.V 
169ead43b01SAneesh Kumar K.V static void virtio_p9_version(struct p9_dev *p9dev,
170af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1711c7850f9SSasha Levin {
172c797b6c6SAneesh Kumar K.V 	u32 msize;
173c797b6c6SAneesh Kumar K.V 	char *version;
174c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ds", &msize, &version);
175c797b6c6SAneesh Kumar K.V 	/*
176c797b6c6SAneesh Kumar K.V 	 * reply with the same msize the client sent us
177c797b6c6SAneesh Kumar K.V 	 * Error out if the request is not for 9P2000.L
178c797b6c6SAneesh Kumar K.V 	 */
1795529bcd7SAsias He 	if (!strcmp(version, VIRTIO_9P_VERSION_DOTL))
180c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, version);
181c797b6c6SAneesh Kumar K.V 	else
182c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, "unknown");
1831c7850f9SSasha Levin 
184bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
185bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
186c797b6c6SAneesh Kumar K.V 	free(version);
187ead43b01SAneesh Kumar K.V 	return;
1881c7850f9SSasha Levin }
1891c7850f9SSasha Levin 
190ead43b01SAneesh Kumar K.V static void virtio_p9_clunk(struct p9_dev *p9dev,
191af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
1921c7850f9SSasha Levin {
193bfc15268SAneesh Kumar K.V 	u32 fid;
1941c7850f9SSasha Levin 
195bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid);
196bfc15268SAneesh Kumar K.V 	close_fid(p9dev, fid);
1971c7850f9SSasha Levin 
198bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
199bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
200ead43b01SAneesh Kumar K.V 	return;
2011c7850f9SSasha Levin }
2021c7850f9SSasha Levin 
203c797b6c6SAneesh Kumar K.V /*
204c797b6c6SAneesh Kumar K.V  * FIXME!! Need to map to protocol independent value. Upstream
205c797b6c6SAneesh Kumar K.V  * 9p also have the same BUG
206c797b6c6SAneesh Kumar K.V  */
207c797b6c6SAneesh Kumar K.V static int virtio_p9_openflags(int flags)
208c797b6c6SAneesh Kumar K.V {
209c797b6c6SAneesh Kumar K.V 	flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT | O_DIRECT);
210c797b6c6SAneesh Kumar K.V 	flags |= O_NOFOLLOW;
211c797b6c6SAneesh Kumar K.V 	return flags;
212c797b6c6SAneesh Kumar K.V }
213c797b6c6SAneesh Kumar K.V 
21432585666SSasha Levin static bool is_dir(struct p9_fid *fid)
21532585666SSasha Levin {
21632585666SSasha Levin 	struct stat st;
21732585666SSasha Levin 
21832585666SSasha Levin 	stat(fid->abs_path, &st);
21932585666SSasha Levin 
22032585666SSasha Levin 	return S_ISDIR(st.st_mode);
22132585666SSasha Levin }
22232585666SSasha Levin 
2239bb99a82SG. Campana /* path is always absolute */
2249bb99a82SG. Campana static bool path_is_illegal(const char *path)
2259bb99a82SG. Campana {
2269bb99a82SG. Campana 	size_t len;
2279bb99a82SG. Campana 
2289bb99a82SG. Campana 	if (strstr(path, "/../") != NULL)
2299bb99a82SG. Campana 		return true;
2309bb99a82SG. Campana 
2319bb99a82SG. Campana 	len = strlen(path);
2329bb99a82SG. Campana 	if (len >= 3 && strcmp(path + len - 3, "/..") == 0)
2339bb99a82SG. Campana 		return true;
2349bb99a82SG. Campana 
2359bb99a82SG. Campana 	return false;
2369bb99a82SG. Campana }
2379bb99a82SG. Campana 
238d4727f2bSG. Campana static int get_full_path_helper(char *full_path, size_t size,
239d4727f2bSG. Campana 			 const char *dirname, const char *name)
240d4727f2bSG. Campana {
241d4727f2bSG. Campana 	int ret;
242d4727f2bSG. Campana 
243d4727f2bSG. Campana 	ret = snprintf(full_path, size, "%s/%s", dirname, name);
244716b2944SG. Campana 	if (ret >= (int)size) {
245d4727f2bSG. Campana 		errno = ENAMETOOLONG;
246d4727f2bSG. Campana 		return -1;
247d4727f2bSG. Campana 	}
248d4727f2bSG. Campana 
249d4727f2bSG. Campana 	if (path_is_illegal(full_path)) {
250d4727f2bSG. Campana 		errno = EACCES;
251d4727f2bSG. Campana 		return -1;
252d4727f2bSG. Campana 	}
253d4727f2bSG. Campana 
254d4727f2bSG. Campana 	return 0;
255d4727f2bSG. Campana }
256d4727f2bSG. Campana 
257d4727f2bSG. Campana static int get_full_path(char *full_path, size_t size, struct p9_fid *fid,
258d4727f2bSG. Campana 			 const char *name)
259d4727f2bSG. Campana {
260d4727f2bSG. Campana 	return get_full_path_helper(full_path, size, fid->abs_path, name);
261d4727f2bSG. Campana }
262d4727f2bSG. Campana 
263*b0922422SG. Campana static int stat_rel(struct p9_dev *p9dev, const char *path, struct stat *st)
264*b0922422SG. Campana {
265*b0922422SG. Campana 	char full_path[PATH_MAX];
266*b0922422SG. Campana 
267*b0922422SG. Campana 	if (get_full_path_helper(full_path, sizeof(full_path), p9dev->root_dir, path) != 0)
268*b0922422SG. Campana 		return -1;
269*b0922422SG. Campana 
270*b0922422SG. Campana 	if (lstat(full_path, st) != 0)
271*b0922422SG. Campana 		return -1;
272*b0922422SG. Campana 
273*b0922422SG. Campana 	return 0;
274*b0922422SG. Campana }
275*b0922422SG. Campana 
276ead43b01SAneesh Kumar K.V static void virtio_p9_open(struct p9_dev *p9dev,
277af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
2781c7850f9SSasha Levin {
279c797b6c6SAneesh Kumar K.V 	u32 fid, flags;
2801c7850f9SSasha Levin 	struct stat st;
281bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
282bfc15268SAneesh Kumar K.V 	struct p9_fid *new_fid;
283bfc15268SAneesh Kumar K.V 
284c797b6c6SAneesh Kumar K.V 
285c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid, &flags);
28631a6fb8dSSasha Levin 	new_fid = get_fid(p9dev, fid);
2871c7850f9SSasha Levin 
28830204a77SAneesh Kumar K.V 	if (lstat(new_fid->abs_path, &st) < 0)
289eee1ba8eSAneesh Kumar K.V 		goto err_out;
2901c7850f9SSasha Levin 
291c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
2921c7850f9SSasha Levin 
29332585666SSasha Levin 	if (is_dir(new_fid)) {
2941c7850f9SSasha Levin 		new_fid->dir = opendir(new_fid->abs_path);
295eee1ba8eSAneesh Kumar K.V 		if (!new_fid->dir)
296eee1ba8eSAneesh Kumar K.V 			goto err_out;
297eee1ba8eSAneesh Kumar K.V 	} else {
298eee1ba8eSAneesh Kumar K.V 		new_fid->fd  = open(new_fid->abs_path,
299c797b6c6SAneesh Kumar K.V 				    virtio_p9_openflags(flags));
300eee1ba8eSAneesh Kumar K.V 		if (new_fid->fd < 0)
301eee1ba8eSAneesh Kumar K.V 			goto err_out;
302eee1ba8eSAneesh Kumar K.V 	}
303c797b6c6SAneesh Kumar K.V 	/* FIXME!! need ot send proper iounit  */
304bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
305bfc15268SAneesh Kumar K.V 
306bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
307bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
308ead43b01SAneesh Kumar K.V 	return;
309eee1ba8eSAneesh Kumar K.V err_out:
310eee1ba8eSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
311ead43b01SAneesh Kumar K.V 	return;
3121c7850f9SSasha Levin }
3131c7850f9SSasha Levin 
314ead43b01SAneesh Kumar K.V static void virtio_p9_create(struct p9_dev *p9dev,
315af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
3161c7850f9SSasha Levin {
317c797b6c6SAneesh Kumar K.V 	int fd, ret;
318bfc15268SAneesh Kumar K.V 	char *name;
31932832dd1SG. Campana 	size_t size;
320af045e53SAneesh Kumar K.V 	struct stat st;
321bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
322c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
3234bc9734aSAneesh Kumar K.V 	char full_path[PATH_MAX];
324c797b6c6SAneesh Kumar K.V 	u32 dfid_val, flags, mode, gid;
325af045e53SAneesh Kumar K.V 
326c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsddd", &dfid_val,
327c797b6c6SAneesh Kumar K.V 			    &name, &flags, &mode, &gid);
32831a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
3291c7850f9SSasha Levin 
330d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
33132832dd1SG. Campana 		goto err_out;
3329bb99a82SG. Campana 
33332832dd1SG. Campana 	size = sizeof(dfid->abs_path) - (dfid->path - dfid->abs_path);
33432832dd1SG. Campana 	ret = snprintf(dfid->path, size, "%s/%s", dfid->path, name);
33532832dd1SG. Campana 	if (ret >= (int)size) {
33632832dd1SG. Campana 		errno = ENAMETOOLONG;
33732832dd1SG. Campana 		if (size > 0)
33832832dd1SG. Campana 			dfid->path[size] = '\x00';
33932832dd1SG. Campana 		goto err_out;
34032832dd1SG. Campana 	}
34132832dd1SG. Campana 
342d4727f2bSG. Campana 	flags = virtio_p9_openflags(flags);
343d4727f2bSG. Campana 
344c797b6c6SAneesh Kumar K.V 	fd = open(full_path, flags | O_CREAT, mode);
3454bc9734aSAneesh Kumar K.V 	if (fd < 0)
3464bc9734aSAneesh Kumar K.V 		goto err_out;
347c797b6c6SAneesh Kumar K.V 	dfid->fd = fd;
348c797b6c6SAneesh Kumar K.V 
3494bc9734aSAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
3506c8ca053SAneesh Kumar K.V 		goto err_out;
3511c7850f9SSasha Levin 
352c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
353c797b6c6SAneesh Kumar K.V 	if (ret < 0)
354c797b6c6SAneesh Kumar K.V 		goto err_out;
355c797b6c6SAneesh Kumar K.V 
356c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
357bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
358bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
359bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
3605f900f6dSSasha Levin 	free(name);
3616c8ca053SAneesh Kumar K.V 	return;
3626c8ca053SAneesh Kumar K.V err_out:
3635f900f6dSSasha Levin 	free(name);
364c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
365c797b6c6SAneesh Kumar K.V 	return;
366c797b6c6SAneesh Kumar K.V }
367c797b6c6SAneesh Kumar K.V 
368c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev,
369c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
370c797b6c6SAneesh Kumar K.V {
371c797b6c6SAneesh Kumar K.V 	int ret;
372c797b6c6SAneesh Kumar K.V 	char *name;
373c797b6c6SAneesh Kumar K.V 	struct stat st;
374c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
375c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
376c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
377c797b6c6SAneesh Kumar K.V 	u32 dfid_val, mode, gid;
378c797b6c6SAneesh Kumar K.V 
379c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val,
380c797b6c6SAneesh Kumar K.V 			    &name, &mode, &gid);
38131a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
382c797b6c6SAneesh Kumar K.V 
383d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
38432832dd1SG. Campana 		goto err_out;
3859bb99a82SG. Campana 
386c797b6c6SAneesh Kumar K.V 	ret = mkdir(full_path, mode);
387c797b6c6SAneesh Kumar K.V 	if (ret < 0)
388c797b6c6SAneesh Kumar K.V 		goto err_out;
389c797b6c6SAneesh Kumar K.V 
390c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
391c797b6c6SAneesh Kumar K.V 		goto err_out;
392c797b6c6SAneesh Kumar K.V 
393c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
394c797b6c6SAneesh Kumar K.V 	if (ret < 0)
395c797b6c6SAneesh Kumar K.V 		goto err_out;
396c797b6c6SAneesh Kumar K.V 
397c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
398c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
399c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
400c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
401c797b6c6SAneesh Kumar K.V 	free(name);
402c797b6c6SAneesh Kumar K.V 	return;
403c797b6c6SAneesh Kumar K.V err_out:
404c797b6c6SAneesh Kumar K.V 	free(name);
405c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
406ead43b01SAneesh Kumar K.V 	return;
4071c7850f9SSasha Levin }
4081c7850f9SSasha Levin 
409e277a1b4SG. Campana static int join_path(struct p9_fid *fid, const char *name)
410e277a1b4SG. Campana {
411e277a1b4SG. Campana 	size_t len, size;
412e277a1b4SG. Campana 
413e277a1b4SG. Campana 	size = sizeof(fid->abs_path) - (fid->path - fid->abs_path);
414e277a1b4SG. Campana 	len = strlen(name);
415e277a1b4SG. Campana 	if (len >= size)
416e277a1b4SG. Campana 		return -1;
417e277a1b4SG. Campana 
418e277a1b4SG. Campana 	strncpy(fid->path, name, size);
419e277a1b4SG. Campana 	return 0;
420e277a1b4SG. Campana }
421e277a1b4SG. Campana 
422ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev,
423af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
4241c7850f9SSasha Levin {
425af045e53SAneesh Kumar K.V 	u8 i;
426bfc15268SAneesh Kumar K.V 	u16 nwqid;
427bfc15268SAneesh Kumar K.V 	u16 nwname;
428bfc15268SAneesh Kumar K.V 	struct p9_qid wqid;
429e2341580SSasha Levin 	struct p9_fid *new_fid, *old_fid;
430c797b6c6SAneesh Kumar K.V 	u32 fid_val, newfid_val;
431c797b6c6SAneesh Kumar K.V 
4321c7850f9SSasha Levin 
433bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname);
43431a6fb8dSSasha Levin 	new_fid	= get_fid(p9dev, newfid_val);
4351c7850f9SSasha Levin 
436bfc15268SAneesh Kumar K.V 	nwqid = 0;
437bfc15268SAneesh Kumar K.V 	if (nwname) {
43831a6fb8dSSasha Levin 		struct p9_fid *fid = get_fid(p9dev, fid_val);
439bfc15268SAneesh Kumar K.V 
440e277a1b4SG. Campana 		if (join_path(new_fid, fid->path) != 0) {
441e277a1b4SG. Campana 			errno = ENAMETOOLONG;
442e277a1b4SG. Campana 			goto err_out;
443e277a1b4SG. Campana 		}
444e277a1b4SG. Campana 
445bfc15268SAneesh Kumar K.V 		/* skip the space for count */
446bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
447bfc15268SAneesh Kumar K.V 		for (i = 0; i < nwname; i++) {
448bfc15268SAneesh Kumar K.V 			struct stat st;
4491c7850f9SSasha Levin 			char tmp[PATH_MAX] = {0};
450e55ed135SPekka Enberg 			char *str;
45132832dd1SG. Campana 			int ret;
452bfc15268SAneesh Kumar K.V 
453bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_readf(pdu, "s", &str);
4541c7850f9SSasha Levin 
4551c7850f9SSasha Levin 			/* Format the new path we're 'walk'ing into */
45632832dd1SG. Campana 			ret = snprintf(tmp, sizeof(tmp), "%s/%s", new_fid->path, str);
45732832dd1SG. Campana 			if (ret >= (int)sizeof(tmp)) {
45832832dd1SG. Campana 				errno = ENAMETOOLONG;
45932832dd1SG. Campana 				goto err_out;
46032832dd1SG. Campana 			}
461e55ed135SPekka Enberg 
462e55ed135SPekka Enberg 			free(str);
463e55ed135SPekka Enberg 
464*b0922422SG. Campana 			if (stat_rel(p9dev, tmp, &st) != 0)
4656c8ca053SAneesh Kumar K.V 				goto err_out;
4661c7850f9SSasha Levin 
467c797b6c6SAneesh Kumar K.V 			stat2qid(&st, &wqid);
468e277a1b4SG. Campana 			if (join_path(new_fid, tmp) != 0) {
469e277a1b4SG. Campana 				errno = ENAMETOOLONG;
470e277a1b4SG. Campana 				goto err_out;
471e277a1b4SG. Campana 			}
472c797b6c6SAneesh Kumar K.V 			new_fid->uid = fid->uid;
473bfc15268SAneesh Kumar K.V 			nwqid++;
474bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_writef(pdu, "Q", &wqid);
4751c7850f9SSasha Levin 		}
4761c7850f9SSasha Levin 	} else {
477bfc15268SAneesh Kumar K.V 		/*
478bfc15268SAneesh Kumar K.V 		 * update write_offset so our outlen get correct value
479bfc15268SAneesh Kumar K.V 		 */
480bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
481e2341580SSasha Levin 		old_fid = get_fid(p9dev, fid_val);
482e277a1b4SG. Campana 		if (join_path(new_fid, old_fid->path) != 0) {
483e277a1b4SG. Campana 			errno = ENAMETOOLONG;
484e277a1b4SG. Campana 			goto err_out;
485e277a1b4SG. Campana 		}
486e2341580SSasha Levin 		new_fid->uid    = old_fid->uid;
4871c7850f9SSasha Levin 	}
488bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
4895529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
490bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", nwqid);
491bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
4926c8ca053SAneesh Kumar K.V 	return;
4936c8ca053SAneesh Kumar K.V err_out:
4946c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
495ead43b01SAneesh Kumar K.V 	return;
4961c7850f9SSasha Levin }
4971c7850f9SSasha Levin 
498ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev,
499af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
5001c7850f9SSasha Levin {
501bfc15268SAneesh Kumar K.V 	char *uname;
502bfc15268SAneesh Kumar K.V 	char *aname;
5031c7850f9SSasha Levin 	struct stat st;
504bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
5051c7850f9SSasha Levin 	struct p9_fid *fid;
506c797b6c6SAneesh Kumar K.V 	u32 fid_val, afid, uid;
507bfc15268SAneesh Kumar K.V 
508c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid,
509c797b6c6SAneesh Kumar K.V 			    &uname, &aname, &uid);
5101c7850f9SSasha Levin 
51139257180SPekka Enberg 	free(uname);
51239257180SPekka Enberg 	free(aname);
51339257180SPekka Enberg 
51430204a77SAneesh Kumar K.V 	if (lstat(p9dev->root_dir, &st) < 0)
5156c8ca053SAneesh Kumar K.V 		goto err_out;
5161c7850f9SSasha Levin 
517c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
5181c7850f9SSasha Levin 
51931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
520c797b6c6SAneesh Kumar K.V 	fid->uid = uid;
521e277a1b4SG. Campana 	if (join_path(fid, "/") != 0) {
522e277a1b4SG. Campana 		errno = ENAMETOOLONG;
523e277a1b4SG. Campana 		goto err_out;
524e277a1b4SG. Campana 	}
5251c7850f9SSasha Levin 
526bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
527bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
528bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
5296c8ca053SAneesh Kumar K.V 	return;
5306c8ca053SAneesh Kumar K.V err_out:
5316c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
532ead43b01SAneesh Kumar K.V 	return;
5331c7850f9SSasha Levin }
5341c7850f9SSasha Levin 
535c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev,
536c797b6c6SAneesh Kumar K.V 				struct stat *st, struct p9_stat_dotl *statl)
5375f900f6dSSasha Levin {
538c797b6c6SAneesh Kumar K.V 	memset(statl, 0, sizeof(*statl));
539c797b6c6SAneesh Kumar K.V 	statl->st_mode		= st->st_mode;
540c797b6c6SAneesh Kumar K.V 	statl->st_nlink		= st->st_nlink;
541506fd90bSSasha Levin 	statl->st_uid		= KUIDT_INIT(st->st_uid);
542506fd90bSSasha Levin 	statl->st_gid		= KGIDT_INIT(st->st_gid);
543c797b6c6SAneesh Kumar K.V 	statl->st_rdev		= st->st_rdev;
544c797b6c6SAneesh Kumar K.V 	statl->st_size		= st->st_size;
545c797b6c6SAneesh Kumar K.V 	statl->st_blksize	= st->st_blksize;
546c797b6c6SAneesh Kumar K.V 	statl->st_blocks	= st->st_blocks;
547c797b6c6SAneesh Kumar K.V 	statl->st_atime_sec	= st->st_atime;
548c797b6c6SAneesh Kumar K.V 	statl->st_atime_nsec	= st->st_atim.tv_nsec;
549c797b6c6SAneesh Kumar K.V 	statl->st_mtime_sec	= st->st_mtime;
550c797b6c6SAneesh Kumar K.V 	statl->st_mtime_nsec	= st->st_mtim.tv_nsec;
551c797b6c6SAneesh Kumar K.V 	statl->st_ctime_sec	= st->st_ctime;
552c797b6c6SAneesh Kumar K.V 	statl->st_ctime_nsec	= st->st_ctim.tv_nsec;
553c797b6c6SAneesh Kumar K.V 	/* Currently we only support BASIC fields in stat */
554c797b6c6SAneesh Kumar K.V 	statl->st_result_mask	= P9_STATS_BASIC;
555c797b6c6SAneesh Kumar K.V 	stat2qid(st, &statl->qid);
5561c7850f9SSasha Levin }
5571c7850f9SSasha Levin 
558ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev,
559af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
5601c7850f9SSasha Levin {
561bfc15268SAneesh Kumar K.V 	u64 offset;
562bfc15268SAneesh Kumar K.V 	u32 fid_val;
563c797b6c6SAneesh Kumar K.V 	u16 iov_cnt;
564c797b6c6SAneesh Kumar K.V 	void *iov_base;
565c797b6c6SAneesh Kumar K.V 	size_t iov_len;
566bfc15268SAneesh Kumar K.V 	u32 count, rcount;
567bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
568c797b6c6SAneesh Kumar K.V 
5691c7850f9SSasha Levin 
570bfc15268SAneesh Kumar K.V 	rcount = 0;
571bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
57231a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
57350c479e0SAneesh Kumar K.V 
57450c479e0SAneesh Kumar K.V 	iov_base = pdu->in_iov[0].iov_base;
57550c479e0SAneesh Kumar K.V 	iov_len  = pdu->in_iov[0].iov_len;
57650c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->in_iov_cnt;
5775529bcd7SAsias He 	pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32);
5785529bcd7SAsias He 	pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32);
5796b163a87SAneesh Kumar K.V 	pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov,
580bfc15268SAneesh Kumar K.V 						   count,
5816b163a87SAneesh Kumar K.V 						   pdu->in_iov_cnt);
582bfc15268SAneesh Kumar K.V 	rcount = preadv(fid->fd, pdu->in_iov,
583bfc15268SAneesh Kumar K.V 			pdu->in_iov_cnt, offset);
584bfc15268SAneesh Kumar K.V 	if (rcount > count)
585bfc15268SAneesh Kumar K.V 		rcount = count;
586bfc15268SAneesh Kumar K.V 	/*
587bfc15268SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
588bfc15268SAneesh Kumar K.V 	 * pdu_writef works correctly.
589bfc15268SAneesh Kumar K.V 	 */
59050c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_base = iov_base;
59150c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_len  = iov_len;
59250c479e0SAneesh Kumar K.V 	pdu->in_iov_cnt         = iov_cnt;
593c797b6c6SAneesh Kumar K.V 
5945529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
595bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
596bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
597bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
598ead43b01SAneesh Kumar K.V 	return;
5991c7850f9SSasha Levin }
6001c7850f9SSasha Levin 
601c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent)
602c797b6c6SAneesh Kumar K.V {
603c797b6c6SAneesh Kumar K.V 	/*
604c797b6c6SAneesh Kumar K.V 	 * Size of each dirent:
605c797b6c6SAneesh Kumar K.V 	 * qid(13) + offset(8) + type(1) + name_len(2) + name
606c797b6c6SAneesh Kumar K.V 	 */
607c797b6c6SAneesh Kumar K.V 	return 24 + strlen(dent->d_name);
608c797b6c6SAneesh Kumar K.V }
609c797b6c6SAneesh Kumar K.V 
610c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev,
611c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
612c797b6c6SAneesh Kumar K.V {
613c797b6c6SAneesh Kumar K.V 	u32 fid_val;
614c797b6c6SAneesh Kumar K.V 	u32 count, rcount;
615c797b6c6SAneesh Kumar K.V 	struct stat st;
616c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
617c797b6c6SAneesh Kumar K.V 	struct dirent *dent;
618c797b6c6SAneesh Kumar K.V 	u64 offset, old_offset;
619c797b6c6SAneesh Kumar K.V 
620c797b6c6SAneesh Kumar K.V 	rcount = 0;
621c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
62231a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
623c797b6c6SAneesh Kumar K.V 
62432585666SSasha Levin 	if (!is_dir(fid)) {
62569bb4278SSasha Levin 		errno = EINVAL;
626c797b6c6SAneesh Kumar K.V 		goto err_out;
627c797b6c6SAneesh Kumar K.V 	}
628c797b6c6SAneesh Kumar K.V 
629c797b6c6SAneesh Kumar K.V 	/* Move the offset specified */
630c797b6c6SAneesh Kumar K.V 	seekdir(fid->dir, offset);
631c797b6c6SAneesh Kumar K.V 
632c797b6c6SAneesh Kumar K.V 	old_offset = offset;
633c797b6c6SAneesh Kumar K.V 	/* If reading a dir, fill the buffer with p9_stat entries */
634c797b6c6SAneesh Kumar K.V 	dent = readdir(fid->dir);
635c797b6c6SAneesh Kumar K.V 
636c797b6c6SAneesh Kumar K.V 	/* Skip the space for writing count */
637c797b6c6SAneesh Kumar K.V 	pdu->write_offset += sizeof(u32);
638c797b6c6SAneesh Kumar K.V 	while (dent) {
639c797b6c6SAneesh Kumar K.V 		u32 read;
640c797b6c6SAneesh Kumar K.V 		struct p9_qid qid;
641c797b6c6SAneesh Kumar K.V 
642c797b6c6SAneesh Kumar K.V 		if ((rcount + virtio_p9_dentry_size(dent)) > count) {
643c797b6c6SAneesh Kumar K.V 			/* seek to the previous offset and return */
644c797b6c6SAneesh Kumar K.V 			seekdir(fid->dir, old_offset);
645c797b6c6SAneesh Kumar K.V 			break;
646c797b6c6SAneesh Kumar K.V 		}
647c797b6c6SAneesh Kumar K.V 		old_offset = dent->d_off;
648*b0922422SG. Campana 		if (stat_rel(p9dev, dent->d_name, &st) != 0)
649*b0922422SG. Campana 			memset(&st, -1, sizeof(st));
650c797b6c6SAneesh Kumar K.V 		stat2qid(&st, &qid);
651c797b6c6SAneesh Kumar K.V 		read = pdu->write_offset;
652c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off,
653c797b6c6SAneesh Kumar K.V 				     dent->d_type, dent->d_name);
654c797b6c6SAneesh Kumar K.V 		rcount += pdu->write_offset - read;
655c797b6c6SAneesh Kumar K.V 		dent = readdir(fid->dir);
656c797b6c6SAneesh Kumar K.V 	}
657c797b6c6SAneesh Kumar K.V 
6585529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
659c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
660c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
661c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
662c797b6c6SAneesh Kumar K.V 	return;
663c797b6c6SAneesh Kumar K.V err_out:
664c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
665c797b6c6SAneesh Kumar K.V 	return;
666c797b6c6SAneesh Kumar K.V }
667c797b6c6SAneesh Kumar K.V 
668c797b6c6SAneesh Kumar K.V 
669c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev,
670af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
6711c7850f9SSasha Levin {
672bfc15268SAneesh Kumar K.V 	u32 fid_val;
673af045e53SAneesh Kumar K.V 	struct stat st;
674c797b6c6SAneesh Kumar K.V 	u64 request_mask;
675bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
676c797b6c6SAneesh Kumar K.V 	struct p9_stat_dotl statl;
6771c7850f9SSasha Levin 
678c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask);
67931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
68030204a77SAneesh Kumar K.V 	if (lstat(fid->abs_path, &st) < 0)
6816c8ca053SAneesh Kumar K.V 		goto err_out;
6821c7850f9SSasha Levin 
683c797b6c6SAneesh Kumar K.V 	virtio_p9_fill_stat(p9dev, &st, &statl);
684c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "A", &statl);
685bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
686bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
687ead43b01SAneesh Kumar K.V 	return;
6886c8ca053SAneesh Kumar K.V err_out:
6896c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
6906c8ca053SAneesh Kumar K.V 	return;
6911c7850f9SSasha Levin }
6921c7850f9SSasha Levin 
693c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */
694c797b6c6SAneesh Kumar K.V /*
695c797b6c6SAneesh Kumar K.V  * Attribute flags.  These should be or-ed together to figure out what
696c797b6c6SAneesh Kumar K.V  * has been changed!
697c797b6c6SAneesh Kumar K.V  */
698c797b6c6SAneesh Kumar K.V #define ATTR_MODE	(1 << 0)
699c797b6c6SAneesh Kumar K.V #define ATTR_UID	(1 << 1)
700c797b6c6SAneesh Kumar K.V #define ATTR_GID	(1 << 2)
701c797b6c6SAneesh Kumar K.V #define ATTR_SIZE	(1 << 3)
702c797b6c6SAneesh Kumar K.V #define ATTR_ATIME	(1 << 4)
703c797b6c6SAneesh Kumar K.V #define ATTR_MTIME	(1 << 5)
704c797b6c6SAneesh Kumar K.V #define ATTR_CTIME	(1 << 6)
705c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET	(1 << 7)
706c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET	(1 << 8)
707c797b6c6SAneesh Kumar K.V #define ATTR_FORCE	(1 << 9) /* Not a change, but a change it */
708c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG	(1 << 10)
709c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID	(1 << 11)
710c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID	(1 << 12)
711c797b6c6SAneesh Kumar K.V #define ATTR_FILE	(1 << 13)
712c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV	(1 << 14)
713c797b6c6SAneesh Kumar K.V #define ATTR_OPEN	(1 << 15) /* Truncating from open(O_TRUNC) */
714c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET	(1 << 16)
715c797b6c6SAneesh Kumar K.V 
716c797b6c6SAneesh Kumar K.V #define ATTR_MASK    127
717c797b6c6SAneesh Kumar K.V 
718c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev,
719af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
7201c7850f9SSasha Levin {
721c797b6c6SAneesh Kumar K.V 	int ret = 0;
722bfc15268SAneesh Kumar K.V 	u32 fid_val;
723bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
724c797b6c6SAneesh Kumar K.V 	struct p9_iattr_dotl p9attr;
7251c7850f9SSasha Levin 
726c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr);
72731a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
7281c7850f9SSasha Levin 
729c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & ATTR_MODE) {
730c797b6c6SAneesh Kumar K.V 		ret = chmod(fid->abs_path, p9attr.mode);
731c797b6c6SAneesh Kumar K.V 		if (ret < 0)
732c797b6c6SAneesh Kumar K.V 			goto err_out;
733c797b6c6SAneesh Kumar K.V 	}
734c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) {
735c797b6c6SAneesh Kumar K.V 		struct timespec times[2];
736c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_ATIME) {
737c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_ATIME_SET) {
738c797b6c6SAneesh Kumar K.V 				times[0].tv_sec = p9attr.atime_sec;
739c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = p9attr.atime_nsec;
740c797b6c6SAneesh Kumar K.V 			} else {
741c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = UTIME_NOW;
742c797b6c6SAneesh Kumar K.V 			}
743c797b6c6SAneesh Kumar K.V 		} else {
744c797b6c6SAneesh Kumar K.V 			times[0].tv_nsec = UTIME_OMIT;
745c797b6c6SAneesh Kumar K.V 		}
746c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_MTIME) {
747c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_MTIME_SET) {
748c797b6c6SAneesh Kumar K.V 				times[1].tv_sec = p9attr.mtime_sec;
749c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = p9attr.mtime_nsec;
750c797b6c6SAneesh Kumar K.V 			} else {
751c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = UTIME_NOW;
752c797b6c6SAneesh Kumar K.V 			}
753c797b6c6SAneesh Kumar K.V 		} else
754c797b6c6SAneesh Kumar K.V 			times[1].tv_nsec = UTIME_OMIT;
755c797b6c6SAneesh Kumar K.V 
756c797b6c6SAneesh Kumar K.V 		ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW);
757c797b6c6SAneesh Kumar K.V 		if (ret < 0)
758c797b6c6SAneesh Kumar K.V 			goto err_out;
759c797b6c6SAneesh Kumar K.V 	}
760c797b6c6SAneesh Kumar K.V 	/*
761c797b6c6SAneesh Kumar K.V 	 * If the only valid entry in iattr is ctime we can call
762c797b6c6SAneesh Kumar K.V 	 * chown(-1,-1) to update the ctime of the file
763c797b6c6SAneesh Kumar K.V 	 */
764c797b6c6SAneesh Kumar K.V 	if ((p9attr.valid & (ATTR_UID | ATTR_GID)) ||
765c797b6c6SAneesh Kumar K.V 	    ((p9attr.valid & ATTR_CTIME)
766c797b6c6SAneesh Kumar K.V 	     && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
767c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_UID))
768506fd90bSSasha Levin 			p9attr.uid = KUIDT_INIT(-1);
769c797b6c6SAneesh Kumar K.V 
770c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_GID))
771506fd90bSSasha Levin 			p9attr.gid = KGIDT_INIT(-1);
772c797b6c6SAneesh Kumar K.V 
773506fd90bSSasha Levin 		ret = lchown(fid->abs_path, __kuid_val(p9attr.uid),
774506fd90bSSasha Levin 				__kgid_val(p9attr.gid));
775c797b6c6SAneesh Kumar K.V 		if (ret < 0)
776c797b6c6SAneesh Kumar K.V 			goto err_out;
777c797b6c6SAneesh Kumar K.V 	}
778c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_SIZE)) {
779c797b6c6SAneesh Kumar K.V 		ret = truncate(fid->abs_path, p9attr.size);
780c797b6c6SAneesh Kumar K.V 		if (ret < 0)
781c797b6c6SAneesh Kumar K.V 			goto err_out;
782c797b6c6SAneesh Kumar K.V 	}
7835529bcd7SAsias He 	*outlen = VIRTIO_9P_HDR_LEN;
784bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
785ead43b01SAneesh Kumar K.V 	return;
7864bc9734aSAneesh Kumar K.V err_out:
7874bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
7884bc9734aSAneesh Kumar K.V 	return;
7891c7850f9SSasha Levin }
7901c7850f9SSasha Levin 
791ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev,
792af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
7931c7850f9SSasha Levin {
7944bc9734aSAneesh Kumar K.V 
795bfc15268SAneesh Kumar K.V 	u64 offset;
796bfc15268SAneesh Kumar K.V 	u32 fid_val;
7974bc9734aSAneesh Kumar K.V 	u32 count;
7984bc9734aSAneesh Kumar K.V 	ssize_t res;
79950c479e0SAneesh Kumar K.V 	u16 iov_cnt;
80050c479e0SAneesh Kumar K.V 	void *iov_base;
80150c479e0SAneesh Kumar K.V 	size_t iov_len;
802bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
803b064b05aSAneesh Kumar K.V 	/* u32 fid + u64 offset + u32 count */
804b064b05aSAneesh Kumar K.V 	int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32);
8051c7850f9SSasha Levin 
806bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
80731a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
808af045e53SAneesh Kumar K.V 
80950c479e0SAneesh Kumar K.V 	iov_base = pdu->out_iov[0].iov_base;
81050c479e0SAneesh Kumar K.V 	iov_len  = pdu->out_iov[0].iov_len;
81150c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->out_iov_cnt;
81250c479e0SAneesh Kumar K.V 
813bfc15268SAneesh Kumar K.V 	/* Adjust the iovec to skip the header and meta data */
814b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size);
815b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_len -=  (sizeof(struct p9_msg) + twrite_size);
816bfc15268SAneesh Kumar K.V 	pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count,
8176b163a87SAneesh Kumar K.V 						    pdu->out_iov_cnt);
8184bc9734aSAneesh Kumar K.V 	res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset);
81950c479e0SAneesh Kumar K.V 	/*
82050c479e0SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
82150c479e0SAneesh Kumar K.V 	 * pdu_readf works correctly.
82250c479e0SAneesh Kumar K.V 	 */
82350c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_base = iov_base;
82450c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_len  = iov_len;
82550c479e0SAneesh Kumar K.V 	pdu->out_iov_cnt         = iov_cnt;
826c797b6c6SAneesh Kumar K.V 
8274bc9734aSAneesh Kumar K.V 	if (res < 0)
8284bc9734aSAneesh Kumar K.V 		goto err_out;
8294bc9734aSAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", res);
830bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
831bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
832ead43b01SAneesh Kumar K.V 	return;
8334bc9734aSAneesh Kumar K.V err_out:
8344bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
8354bc9734aSAneesh Kumar K.V 	return;
8361c7850f9SSasha Levin }
8371c7850f9SSasha Levin 
8386fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev,
8396fc5cd9bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
8406fc5cd9bSSasha Levin {
8416fc5cd9bSSasha Levin 	int ret;
8426fc5cd9bSSasha Levin 	u32 fid_val;
8436fc5cd9bSSasha Levin 	struct p9_fid *fid;
8446fc5cd9bSSasha Levin 
8456fc5cd9bSSasha Levin 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
84631a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
8476fc5cd9bSSasha Levin 
8489b604a9cSSasha Levin 	ret = remove(fid->abs_path);
8496fc5cd9bSSasha Levin 	if (ret < 0)
8506fc5cd9bSSasha Levin 		goto err_out;
8516fc5cd9bSSasha Levin 	*outlen = pdu->write_offset;
8526fc5cd9bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
8536fc5cd9bSSasha Levin 	return;
8546fc5cd9bSSasha Levin 
8556fc5cd9bSSasha Levin err_out:
8566fc5cd9bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
8576fc5cd9bSSasha Levin 	return;
8586fc5cd9bSSasha Levin }
8596fc5cd9bSSasha Levin 
860f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev,
861f161f28bSSasha Levin 			     struct p9_pdu *pdu, u32 *outlen)
862f161f28bSSasha Levin {
863f161f28bSSasha Levin 	int ret;
864f161f28bSSasha Levin 	u32 fid_val, new_fid_val;
865f161f28bSSasha Levin 	struct p9_fid *fid, *new_fid;
866f161f28bSSasha Levin 	char full_path[PATH_MAX], *new_name;
867f161f28bSSasha Levin 
868f161f28bSSasha Levin 	virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name);
86931a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
87031a6fb8dSSasha Levin 	new_fid = get_fid(p9dev, new_fid_val);
871f161f28bSSasha Levin 
872d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), new_fid, new_name) != 0)
87332832dd1SG. Campana 		goto err_out;
8749bb99a82SG. Campana 
875f161f28bSSasha Levin 	ret = rename(fid->abs_path, full_path);
876f161f28bSSasha Levin 	if (ret < 0)
877f161f28bSSasha Levin 		goto err_out;
878f161f28bSSasha Levin 	*outlen = pdu->write_offset;
879f161f28bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
880f161f28bSSasha Levin 	return;
881f161f28bSSasha Levin 
882f161f28bSSasha Levin err_out:
883f161f28bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
884f161f28bSSasha Levin 	return;
885f161f28bSSasha Levin }
886f161f28bSSasha Levin 
887c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev,
888c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
889c797b6c6SAneesh Kumar K.V {
890c797b6c6SAneesh Kumar K.V 	int ret;
891c797b6c6SAneesh Kumar K.V 	u32 fid_val;
892c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
893c797b6c6SAneesh Kumar K.V 	char target_path[PATH_MAX];
894c797b6c6SAneesh Kumar K.V 
895c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
89631a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
897c797b6c6SAneesh Kumar K.V 
898c797b6c6SAneesh Kumar K.V 	memset(target_path, 0, PATH_MAX);
899c797b6c6SAneesh Kumar K.V 	ret = readlink(fid->abs_path, target_path, PATH_MAX - 1);
900c797b6c6SAneesh Kumar K.V 	if (ret < 0)
901c797b6c6SAneesh Kumar K.V 		goto err_out;
902c797b6c6SAneesh Kumar K.V 
903c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "s", target_path);
904c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
905c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
906c797b6c6SAneesh Kumar K.V 	return;
907c797b6c6SAneesh Kumar K.V err_out:
908c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
909c797b6c6SAneesh Kumar K.V 	return;
910c797b6c6SAneesh Kumar K.V }
911c797b6c6SAneesh Kumar K.V 
912c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev,
913c797b6c6SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
914c797b6c6SAneesh Kumar K.V {
915c797b6c6SAneesh Kumar K.V 	int ret;
916c797b6c6SAneesh Kumar K.V 	u64 fsid;
917c797b6c6SAneesh Kumar K.V 	u32 fid_val;
918c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
919c797b6c6SAneesh Kumar K.V 	struct statfs stat_buf;
920c797b6c6SAneesh Kumar K.V 
921c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
92231a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
923c797b6c6SAneesh Kumar K.V 
924c797b6c6SAneesh Kumar K.V 	ret = statfs(fid->abs_path, &stat_buf);
925c797b6c6SAneesh Kumar K.V 	if (ret < 0)
926c797b6c6SAneesh Kumar K.V 		goto err_out;
927c797b6c6SAneesh Kumar K.V 	/* FIXME!! f_blocks needs update based on client msize */
928c797b6c6SAneesh Kumar K.V 	fsid = (unsigned int) stat_buf.f_fsid.__val[0] |
929c797b6c6SAneesh Kumar K.V 		(unsigned long long)stat_buf.f_fsid.__val[1] << 32;
930c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type,
931c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bsize, stat_buf.f_blocks,
932c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bfree, stat_buf.f_bavail,
933c797b6c6SAneesh Kumar K.V 			     stat_buf.f_files, stat_buf.f_ffree,
934c797b6c6SAneesh Kumar K.V 			     fsid, stat_buf.f_namelen);
935c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
936c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
937c797b6c6SAneesh Kumar K.V 	return;
938c797b6c6SAneesh Kumar K.V err_out:
939c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
940c797b6c6SAneesh Kumar K.V 	return;
941c797b6c6SAneesh Kumar K.V }
942c797b6c6SAneesh Kumar K.V 
943c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev,
944c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
945c797b6c6SAneesh Kumar K.V {
946c797b6c6SAneesh Kumar K.V 	int ret;
947c797b6c6SAneesh Kumar K.V 	char *name;
948c797b6c6SAneesh Kumar K.V 	struct stat st;
949c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
950c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
951c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
952c797b6c6SAneesh Kumar K.V 	u32 fid_val, mode, major, minor, gid;
953c797b6c6SAneesh Kumar K.V 
954c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode,
955c797b6c6SAneesh Kumar K.V 			    &major, &minor, &gid);
956c797b6c6SAneesh Kumar K.V 
95731a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
95832832dd1SG. Campana 
959d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
9609bb99a82SG. Campana 		goto err_out;
9619bb99a82SG. Campana 
962c797b6c6SAneesh Kumar K.V 	ret = mknod(full_path, mode, makedev(major, minor));
963c797b6c6SAneesh Kumar K.V 	if (ret < 0)
964c797b6c6SAneesh Kumar K.V 		goto err_out;
965c797b6c6SAneesh Kumar K.V 
966c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
967c797b6c6SAneesh Kumar K.V 		goto err_out;
968c797b6c6SAneesh Kumar K.V 
969c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
970c797b6c6SAneesh Kumar K.V 	if (ret < 0)
971c797b6c6SAneesh Kumar K.V 		goto err_out;
972c797b6c6SAneesh Kumar K.V 
973c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
974c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
975c797b6c6SAneesh Kumar K.V 	free(name);
976c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
977c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
978c797b6c6SAneesh Kumar K.V 	return;
979c797b6c6SAneesh Kumar K.V err_out:
980c797b6c6SAneesh Kumar K.V 	free(name);
981c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
982c797b6c6SAneesh Kumar K.V 	return;
983c797b6c6SAneesh Kumar K.V }
984c797b6c6SAneesh Kumar K.V 
985c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev,
986c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
987c797b6c6SAneesh Kumar K.V {
988644140efSRussell King 	int ret, fd;
989c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
990c797b6c6SAneesh Kumar K.V 	u32 fid_val, datasync;
991c797b6c6SAneesh Kumar K.V 
992c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync);
99331a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
994c797b6c6SAneesh Kumar K.V 
995644140efSRussell King 	if (fid->dir)
996644140efSRussell King 		fd = dirfd(fid->dir);
997c797b6c6SAneesh Kumar K.V 	else
998644140efSRussell King 		fd = fid->fd;
999644140efSRussell King 
1000644140efSRussell King 	if (datasync)
1001644140efSRussell King 		ret = fdatasync(fd);
1002644140efSRussell King 	else
1003644140efSRussell King 		ret = fsync(fd);
1004c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1005c797b6c6SAneesh Kumar K.V 		goto err_out;
1006c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1007c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1008c797b6c6SAneesh Kumar K.V 	return;
1009c797b6c6SAneesh Kumar K.V err_out:
1010c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1011c797b6c6SAneesh Kumar K.V 	return;
1012c797b6c6SAneesh Kumar K.V }
1013c797b6c6SAneesh Kumar K.V 
1014c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev,
1015c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1016c797b6c6SAneesh Kumar K.V {
1017c797b6c6SAneesh Kumar K.V 	int ret;
1018c797b6c6SAneesh Kumar K.V 	struct stat st;
1019c797b6c6SAneesh Kumar K.V 	u32 fid_val, gid;
1020c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
1021c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
1022c797b6c6SAneesh Kumar K.V 	char new_name[PATH_MAX];
1023c797b6c6SAneesh Kumar K.V 	char *old_path, *name;
1024c797b6c6SAneesh Kumar K.V 
1025c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid);
1026c797b6c6SAneesh Kumar K.V 
102731a6fb8dSSasha Levin 	dfid = get_fid(p9dev, fid_val);
102832832dd1SG. Campana 
1029d4727f2bSG. Campana 	if (get_full_path(new_name, sizeof(new_name), dfid, name) != 0)
10309bb99a82SG. Campana 		goto err_out;
10319bb99a82SG. Campana 
1032c797b6c6SAneesh Kumar K.V 	ret = symlink(old_path, new_name);
1033c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1034c797b6c6SAneesh Kumar K.V 		goto err_out;
1035c797b6c6SAneesh Kumar K.V 
1036c797b6c6SAneesh Kumar K.V 	if (lstat(new_name, &st) < 0)
1037c797b6c6SAneesh Kumar K.V 		goto err_out;
1038c797b6c6SAneesh Kumar K.V 
1039c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
1040c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
1041c797b6c6SAneesh Kumar K.V 	free(name);
1042c797b6c6SAneesh Kumar K.V 	free(old_path);
1043c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1044c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1045c797b6c6SAneesh Kumar K.V 	return;
1046c797b6c6SAneesh Kumar K.V err_out:
1047c797b6c6SAneesh Kumar K.V 	free(name);
1048c797b6c6SAneesh Kumar K.V 	free(old_path);
1049c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1050c797b6c6SAneesh Kumar K.V 	return;
1051c797b6c6SAneesh Kumar K.V }
1052c797b6c6SAneesh Kumar K.V 
1053c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev,
1054c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
1055c797b6c6SAneesh Kumar K.V {
1056c797b6c6SAneesh Kumar K.V 	int ret;
1057c797b6c6SAneesh Kumar K.V 	char *name;
1058c797b6c6SAneesh Kumar K.V 	u32 fid_val, dfid_val;
1059c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid, *fid;
1060c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
1061c797b6c6SAneesh Kumar K.V 
1062c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name);
1063c797b6c6SAneesh Kumar K.V 
106431a6fb8dSSasha Levin 	dfid = get_fid(p9dev, dfid_val);
106531a6fb8dSSasha Levin 	fid =  get_fid(p9dev, fid_val);
106632832dd1SG. Campana 
1067d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0)
10689bb99a82SG. Campana 		goto err_out;
10699bb99a82SG. Campana 
1070c797b6c6SAneesh Kumar K.V 	ret = link(fid->abs_path, full_path);
1071c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1072c797b6c6SAneesh Kumar K.V 		goto err_out;
1073c797b6c6SAneesh Kumar K.V 	free(name);
1074c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1075c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1076c797b6c6SAneesh Kumar K.V 	return;
1077c797b6c6SAneesh Kumar K.V err_out:
1078c797b6c6SAneesh Kumar K.V 	free(name);
1079c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1080c797b6c6SAneesh Kumar K.V 	return;
1081c797b6c6SAneesh Kumar K.V 
1082c797b6c6SAneesh Kumar K.V }
1083c797b6c6SAneesh Kumar K.V 
1084c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev,
1085c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
1086c797b6c6SAneesh Kumar K.V {
1087c797b6c6SAneesh Kumar K.V 	u8 ret;
1088c797b6c6SAneesh Kumar K.V 	u32 fid_val;
1089c797b6c6SAneesh Kumar K.V 	struct p9_flock flock;
1090c797b6c6SAneesh Kumar K.V 
1091c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type,
1092c797b6c6SAneesh Kumar K.V 			    &flock.flags, &flock.start, &flock.length,
1093c797b6c6SAneesh Kumar K.V 			    &flock.proc_id, &flock.client_id);
1094c797b6c6SAneesh Kumar K.V 
1095c797b6c6SAneesh Kumar K.V 	/* Just return success */
1096c797b6c6SAneesh Kumar K.V 	ret = P9_LOCK_SUCCESS;
1097c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", ret);
1098c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1099c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1100c797b6c6SAneesh Kumar K.V 	free(flock.client_id);
1101c797b6c6SAneesh Kumar K.V 	return;
1102c797b6c6SAneesh Kumar K.V }
1103c797b6c6SAneesh Kumar K.V 
1104c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev,
1105c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1106c797b6c6SAneesh Kumar K.V {
1107c797b6c6SAneesh Kumar K.V 	u32 fid_val;
1108c797b6c6SAneesh Kumar K.V 	struct p9_getlock glock;
1109c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type,
1110c797b6c6SAneesh Kumar K.V 			    &glock.start, &glock.length, &glock.proc_id,
1111c797b6c6SAneesh Kumar K.V 			    &glock.client_id);
1112c797b6c6SAneesh Kumar K.V 
1113c797b6c6SAneesh Kumar K.V 	/* Just return success */
1114c797b6c6SAneesh Kumar K.V 	glock.type = F_UNLCK;
1115c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "bqqds", glock.type,
1116c797b6c6SAneesh Kumar K.V 			     glock.start, glock.length, glock.proc_id,
1117c797b6c6SAneesh Kumar K.V 			     glock.client_id);
1118c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1119c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1120c797b6c6SAneesh Kumar K.V 	free(glock.client_id);
1121c797b6c6SAneesh Kumar K.V 	return;
1122c797b6c6SAneesh Kumar K.V }
1123c797b6c6SAneesh Kumar K.V 
1124c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor)
1125c797b6c6SAneesh Kumar K.V {
1126c797b6c6SAneesh Kumar K.V 	int size = strlen(ancestor);
1127c797b6c6SAneesh Kumar K.V 	if (!strncmp(path, ancestor, size)) {
1128c797b6c6SAneesh Kumar K.V 		/*
1129c797b6c6SAneesh Kumar K.V 		 * Now check whether ancestor is a full name or
1130c797b6c6SAneesh Kumar K.V 		 * or directory component and not just part
1131c797b6c6SAneesh Kumar K.V 		 * of a name.
1132c797b6c6SAneesh Kumar K.V 		 */
1133c797b6c6SAneesh Kumar K.V 		if (path[size] == '\0' || path[size] == '/')
1134c797b6c6SAneesh Kumar K.V 			return 1;
1135c797b6c6SAneesh Kumar K.V 	}
1136c797b6c6SAneesh Kumar K.V 	return 0;
1137c797b6c6SAneesh Kumar K.V }
1138c797b6c6SAneesh Kumar K.V 
1139e277a1b4SG. Campana static int virtio_p9_fix_path(struct p9_fid *fid, char *old_name, char *new_name)
1140c797b6c6SAneesh Kumar K.V {
1141e277a1b4SG. Campana 	int ret;
1142e277a1b4SG. Campana 	char *p, tmp_name[PATH_MAX];
1143c797b6c6SAneesh Kumar K.V 	size_t rp_sz = strlen(old_name);
1144c797b6c6SAneesh Kumar K.V 
1145e277a1b4SG. Campana 	if (rp_sz == strlen(fid->path)) {
1146c797b6c6SAneesh Kumar K.V 		/* replace the full name */
1147e277a1b4SG. Campana 		p = new_name;
1148e277a1b4SG. Campana 	} else {
1149c797b6c6SAneesh Kumar K.V 		/* save the trailing path details */
1150e277a1b4SG. Campana 		ret = snprintf(tmp_name, sizeof(tmp_name), "%s%s", new_name, fid->path + rp_sz);
1151e277a1b4SG. Campana 		if (ret >= (int)sizeof(tmp_name))
1152e277a1b4SG. Campana 			return -1;
1153e277a1b4SG. Campana 		p = tmp_name;
1154e277a1b4SG. Campana 	}
1155e277a1b4SG. Campana 
1156e277a1b4SG. Campana 	return join_path(fid, p);
1157c797b6c6SAneesh Kumar K.V }
1158c797b6c6SAneesh Kumar K.V 
1159e2341580SSasha Levin static void rename_fids(struct p9_dev *p9dev, char *old_name, char *new_name)
1160e2341580SSasha Levin {
1161e2341580SSasha Levin 	struct rb_node *node = rb_first(&p9dev->fids);
1162e2341580SSasha Levin 
1163e2341580SSasha Levin 	while (node) {
1164e2341580SSasha Levin 		struct p9_fid *fid = rb_entry(node, struct p9_fid, node);
1165e2341580SSasha Levin 
1166e2341580SSasha Levin 		if (fid->fid != P9_NOFID && virtio_p9_ancestor(fid->path, old_name)) {
1167e277a1b4SG. Campana 				virtio_p9_fix_path(fid, old_name, new_name);
1168e2341580SSasha Levin 		}
1169e2341580SSasha Levin 		node = rb_next(node);
1170e2341580SSasha Levin 	}
1171e2341580SSasha Levin }
1172e2341580SSasha Levin 
1173c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev,
1174c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1175c797b6c6SAneesh Kumar K.V {
1176e2341580SSasha Levin 	int ret;
1177c797b6c6SAneesh Kumar K.V 	char *old_name, *new_name;
1178c797b6c6SAneesh Kumar K.V 	u32 old_dfid_val, new_dfid_val;
1179c797b6c6SAneesh Kumar K.V 	struct p9_fid *old_dfid, *new_dfid;
1180c797b6c6SAneesh Kumar K.V 	char old_full_path[PATH_MAX], new_full_path[PATH_MAX];
1181c797b6c6SAneesh Kumar K.V 
1182c797b6c6SAneesh Kumar K.V 
1183c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name,
1184c797b6c6SAneesh Kumar K.V 			    &new_dfid_val, &new_name);
1185c797b6c6SAneesh Kumar K.V 
118631a6fb8dSSasha Levin 	old_dfid = get_fid(p9dev, old_dfid_val);
118731a6fb8dSSasha Levin 	new_dfid = get_fid(p9dev, new_dfid_val);
1188c797b6c6SAneesh Kumar K.V 
1189d4727f2bSG. Campana 	if (get_full_path(old_full_path, sizeof(old_full_path), old_dfid, old_name) != 0)
119032832dd1SG. Campana 		goto err_out;
119132832dd1SG. Campana 
1192d4727f2bSG. Campana 	if (get_full_path(new_full_path, sizeof(new_full_path), new_dfid, new_name) != 0)
119332832dd1SG. Campana 		goto err_out;
11949bb99a82SG. Campana 
1195c797b6c6SAneesh Kumar K.V 	ret = rename(old_full_path, new_full_path);
1196c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1197c797b6c6SAneesh Kumar K.V 		goto err_out;
1198c797b6c6SAneesh Kumar K.V 	/*
1199c797b6c6SAneesh Kumar K.V 	 * Now fix path in other fids, if the renamed path is part of
1200c797b6c6SAneesh Kumar K.V 	 * that.
1201c797b6c6SAneesh Kumar K.V 	 */
1202e2341580SSasha Levin 	rename_fids(p9dev, old_name, new_name);
1203c797b6c6SAneesh Kumar K.V 	free(old_name);
1204c797b6c6SAneesh Kumar K.V 	free(new_name);
1205c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1206c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1207c797b6c6SAneesh Kumar K.V 	return;
1208c797b6c6SAneesh Kumar K.V err_out:
1209c797b6c6SAneesh Kumar K.V 	free(old_name);
1210c797b6c6SAneesh Kumar K.V 	free(new_name);
1211c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1212c797b6c6SAneesh Kumar K.V 	return;
1213c797b6c6SAneesh Kumar K.V }
1214c797b6c6SAneesh Kumar K.V 
1215c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev,
1216c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1217c797b6c6SAneesh Kumar K.V {
1218c797b6c6SAneesh Kumar K.V 	int ret;
1219c797b6c6SAneesh Kumar K.V 	char *name;
1220c797b6c6SAneesh Kumar K.V 	u32 fid_val, flags;
1221c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
1222c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
1223c797b6c6SAneesh Kumar K.V 
1224c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags);
122531a6fb8dSSasha Levin 	fid = get_fid(p9dev, fid_val);
1226c797b6c6SAneesh Kumar K.V 
1227d4727f2bSG. Campana 	if (get_full_path(full_path, sizeof(full_path), fid, name) != 0)
122832832dd1SG. Campana 		goto err_out;
12299bb99a82SG. Campana 
1230c797b6c6SAneesh Kumar K.V 	ret = remove(full_path);
1231c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1232c797b6c6SAneesh Kumar K.V 		goto err_out;
1233c797b6c6SAneesh Kumar K.V 	free(name);
1234c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1235c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1236c797b6c6SAneesh Kumar K.V 	return;
1237c797b6c6SAneesh Kumar K.V err_out:
1238c797b6c6SAneesh Kumar K.V 	free(name);
1239c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1240c797b6c6SAneesh Kumar K.V 	return;
1241c797b6c6SAneesh Kumar K.V }
1242c797b6c6SAneesh Kumar K.V 
12435cc808aaSSasha Levin static void virtio_p9_flush(struct p9_dev *p9dev,
12445cc808aaSSasha Levin 				struct p9_pdu *pdu, u32 *outlen)
12455cc808aaSSasha Levin {
12465cc808aaSSasha Levin 	u16 tag, oldtag;
12475cc808aaSSasha Levin 
12485cc808aaSSasha Levin 	virtio_p9_pdu_readf(pdu, "ww", &tag, &oldtag);
12495cc808aaSSasha Levin 	virtio_p9_pdu_writef(pdu, "w", tag);
12505cc808aaSSasha Levin 	*outlen = pdu->write_offset;
12515cc808aaSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
12525cc808aaSSasha Levin 
12535cc808aaSSasha Levin 	return;
12545cc808aaSSasha Levin }
12555cc808aaSSasha Levin 
1256c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev,
1257c797b6c6SAneesh Kumar K.V 				 struct p9_pdu *pdu, u32 *outlen)
1258c797b6c6SAneesh Kumar K.V {
1259c797b6c6SAneesh Kumar K.V 	return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen);
1260c797b6c6SAneesh Kumar K.V }
1261c797b6c6SAneesh Kumar K.V 
1262ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev,
1263af045e53SAneesh Kumar K.V 			struct p9_pdu *pdu, u32 *outlen);
1264b4422bf3SAneesh Kumar K.V 
1265c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */
1266c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74
1267c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76
1268c797b6c6SAneesh Kumar K.V 
1269c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = {
1270c797b6c6SAneesh Kumar K.V 	[P9_TREADDIR]     = virtio_p9_readdir,
1271c797b6c6SAneesh Kumar K.V 	[P9_TSTATFS]      = virtio_p9_statfs,
1272c797b6c6SAneesh Kumar K.V 	[P9_TGETATTR]     = virtio_p9_getattr,
1273c797b6c6SAneesh Kumar K.V 	[P9_TSETATTR]     = virtio_p9_setattr,
1274c797b6c6SAneesh Kumar K.V 	[P9_TXATTRWALK]   = virtio_p9_eopnotsupp,
1275c797b6c6SAneesh Kumar K.V 	[P9_TXATTRCREATE] = virtio_p9_eopnotsupp,
1276c797b6c6SAneesh Kumar K.V 	[P9_TMKNOD]       = virtio_p9_mknod,
1277c797b6c6SAneesh Kumar K.V 	[P9_TLOCK]        = virtio_p9_lock,
1278c797b6c6SAneesh Kumar K.V 	[P9_TGETLOCK]     = virtio_p9_getlock,
1279c797b6c6SAneesh Kumar K.V 	[P9_TRENAMEAT]    = virtio_p9_renameat,
1280c797b6c6SAneesh Kumar K.V 	[P9_TREADLINK]    = virtio_p9_readlink,
1281c797b6c6SAneesh Kumar K.V 	[P9_TUNLINKAT]    = virtio_p9_unlinkat,
1282c797b6c6SAneesh Kumar K.V 	[P9_TMKDIR]       = virtio_p9_mkdir,
1283b4422bf3SAneesh Kumar K.V 	[P9_TVERSION]     = virtio_p9_version,
1284c797b6c6SAneesh Kumar K.V 	[P9_TLOPEN]       = virtio_p9_open,
1285b4422bf3SAneesh Kumar K.V 	[P9_TATTACH]      = virtio_p9_attach,
1286b4422bf3SAneesh Kumar K.V 	[P9_TWALK]        = virtio_p9_walk,
1287c797b6c6SAneesh Kumar K.V 	[P9_TCLUNK]       = virtio_p9_clunk,
1288c797b6c6SAneesh Kumar K.V 	[P9_TFSYNC]       = virtio_p9_fsync,
1289b4422bf3SAneesh Kumar K.V 	[P9_TREAD]        = virtio_p9_read,
12905cc808aaSSasha Levin 	[P9_TFLUSH]       = virtio_p9_flush,
1291c797b6c6SAneesh Kumar K.V 	[P9_TLINK]        = virtio_p9_link,
1292c797b6c6SAneesh Kumar K.V 	[P9_TSYMLINK]     = virtio_p9_symlink,
1293c797b6c6SAneesh Kumar K.V 	[P9_TLCREATE]     = virtio_p9_create,
1294b4422bf3SAneesh Kumar K.V 	[P9_TWRITE]       = virtio_p9_write,
12956fc5cd9bSSasha Levin 	[P9_TREMOVE]      = virtio_p9_remove,
1296f161f28bSSasha Levin 	[P9_TRENAME]      = virtio_p9_rename,
1297b4422bf3SAneesh Kumar K.V };
1298b4422bf3SAneesh Kumar K.V 
1299af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq)
1300af045e53SAneesh Kumar K.V {
1301af045e53SAneesh Kumar K.V 	struct p9_pdu *pdu = calloc(1, sizeof(*pdu));
1302af045e53SAneesh Kumar K.V 	if (!pdu)
1303af045e53SAneesh Kumar K.V 		return NULL;
1304af045e53SAneesh Kumar K.V 
1305bfc15268SAneesh Kumar K.V 	/* skip the pdu header p9_msg */
13065529bcd7SAsias He 	pdu->read_offset	= VIRTIO_9P_HDR_LEN;
13075529bcd7SAsias He 	pdu->write_offset	= VIRTIO_9P_HDR_LEN;
1308af045e53SAneesh Kumar K.V 	pdu->queue_head		= virt_queue__get_inout_iov(kvm, vq, pdu->in_iov,
1309a8a44649SAsias He 					pdu->out_iov, &pdu->in_iov_cnt, &pdu->out_iov_cnt);
1310af045e53SAneesh Kumar K.V 	return pdu;
1311af045e53SAneesh Kumar K.V }
1312af045e53SAneesh Kumar K.V 
1313af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu)
1314af045e53SAneesh Kumar K.V {
1315af045e53SAneesh Kumar K.V 	struct p9_msg *msg;
1316af045e53SAneesh Kumar K.V 	/*
1317af045e53SAneesh Kumar K.V 	 * we can peek directly into pdu for a u8
1318af045e53SAneesh Kumar K.V 	 * value. The host endianess won't be an issue
1319af045e53SAneesh Kumar K.V 	 */
1320af045e53SAneesh Kumar K.V 	msg = pdu->out_iov[0].iov_base;
1321af045e53SAneesh Kumar K.V 	return msg->cmd;
1322af045e53SAneesh Kumar K.V }
1323af045e53SAneesh Kumar K.V 
1324b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job)
13251c7850f9SSasha Levin {
1326af045e53SAneesh Kumar K.V 	u8 cmd;
1327b4422bf3SAneesh Kumar K.V 	u32 len = 0;
1328b4422bf3SAneesh Kumar K.V 	p9_handler *handler;
1329b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev;
1330af045e53SAneesh Kumar K.V 	struct virt_queue *vq;
1331af045e53SAneesh Kumar K.V 	struct p9_pdu *p9pdu;
13321c7850f9SSasha Levin 
1333b4422bf3SAneesh Kumar K.V 	vq = job->vq;
1334b4422bf3SAneesh Kumar K.V 	p9dev = job->p9dev;
13351c7850f9SSasha Levin 
1336af045e53SAneesh Kumar K.V 	p9pdu = virtio_p9_pdu_init(kvm, vq);
1337af045e53SAneesh Kumar K.V 	cmd = virtio_p9_get_cmd(p9pdu);
1338af045e53SAneesh Kumar K.V 
1339c797b6c6SAneesh Kumar K.V 	if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) ||
1340c797b6c6SAneesh Kumar K.V 	    !virtio_9p_dotl_handler[cmd])
134197b408afSAneesh Kumar K.V 		handler = virtio_p9_eopnotsupp;
1342dd78d9eaSAneesh Kumar K.V 	else
1343c797b6c6SAneesh Kumar K.V 		handler = virtio_9p_dotl_handler[cmd];
1344c797b6c6SAneesh Kumar K.V 
1345af045e53SAneesh Kumar K.V 	handler(p9dev, p9pdu, &len);
1346af045e53SAneesh Kumar K.V 	virt_queue__set_used_elem(vq, p9pdu->queue_head, len);
1347af045e53SAneesh Kumar K.V 	free(p9pdu);
13481c7850f9SSasha Levin 	return true;
13491c7850f9SSasha Levin }
13501c7850f9SSasha Levin 
13511c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param)
13521c7850f9SSasha Levin {
1353b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job = (struct p9_dev_job *)param;
1354b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev   = job->p9dev;
1355b4422bf3SAneesh Kumar K.V 	struct virt_queue *vq  = job->vq;
13561c7850f9SSasha Levin 
13571c7850f9SSasha Levin 	while (virt_queue__available(vq)) {
1358b4422bf3SAneesh Kumar K.V 		virtio_p9_do_io_request(kvm, job);
135902eca50cSAsias He 		p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs);
13601c7850f9SSasha Levin 	}
13611c7850f9SSasha Levin }
13621c7850f9SSasha Levin 
1363c5ae742bSSasha Levin static u8 *get_config(struct kvm *kvm, void *dev)
13641c7850f9SSasha Levin {
1365c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
13661c7850f9SSasha Levin 
1367c5ae742bSSasha Levin 	return ((u8 *)(p9dev->config));
1368c7838fbdSSasha Levin }
1369c7838fbdSSasha Levin 
1370c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev)
1371c7838fbdSSasha Levin {
1372c7838fbdSSasha Levin 	return 1 << VIRTIO_9P_MOUNT_TAG;
1373c7838fbdSSasha Levin }
1374c7838fbdSSasha Levin 
1375c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
1376c7838fbdSSasha Levin {
1377c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1378e0ea1859SMarc Zyngier 	struct virtio_9p_config *conf = p9dev->config;
1379c7838fbdSSasha Levin 
1380c7838fbdSSasha Levin 	p9dev->features = features;
1381e0ea1859SMarc Zyngier 	conf->tag_len = virtio_host_to_guest_u16(&p9dev->vdev, conf->tag_len);
1382c7838fbdSSasha Levin }
1383c7838fbdSSasha Levin 
1384c59ba304SWill Deacon static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align,
1385c59ba304SWill Deacon 		   u32 pfn)
1386c7838fbdSSasha Levin {
1387c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1388b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job;
1389b4422bf3SAneesh Kumar K.V 	struct virt_queue *queue;
1390c7838fbdSSasha Levin 	void *p;
13911c7850f9SSasha Levin 
1392312c62d1SSasha Levin 	compat__remove_message(compat_id);
1393e59662b3SSasha Levin 
1394c7838fbdSSasha Levin 	queue		= &p9dev->vqs[vq];
1395c7838fbdSSasha Levin 	queue->pfn	= pfn;
1396e7e2950aSSasha Levin 	p		= virtio_get_vq(kvm, queue->pfn, page_size);
1397c7838fbdSSasha Levin 	job		= &p9dev->jobs[vq];
13981c7850f9SSasha Levin 
1399c59ba304SWill Deacon 	vring_init(&queue->vring, VIRTQUEUE_NUM, p, align);
1400e0ea1859SMarc Zyngier 	virtio_init_device_vq(&p9dev->vdev, queue);
14011c7850f9SSasha Levin 
1402b4422bf3SAneesh Kumar K.V 	*job		= (struct p9_dev_job) {
1403b4422bf3SAneesh Kumar K.V 		.vq		= queue,
1404b4422bf3SAneesh Kumar K.V 		.p9dev		= p9dev,
1405b4422bf3SAneesh Kumar K.V 	};
1406df0c7f57SSasha Levin 	thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job);
140760eb42d5SSasha Levin 
1408c7838fbdSSasha Levin 	return 0;
14091c7850f9SSasha Levin }
14101c7850f9SSasha Levin 
1411c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq)
1412c7838fbdSSasha Levin {
1413c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
14141c7850f9SSasha Levin 
1415c7838fbdSSasha Levin 	thread_pool__do_job(&p9dev->jobs[vq].job_id);
1416c7838fbdSSasha Levin 
1417c7838fbdSSasha Levin 	return 0;
1418c7838fbdSSasha Levin }
1419c7838fbdSSasha Levin 
1420c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq)
1421c7838fbdSSasha Levin {
1422c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1423c7838fbdSSasha Levin 
1424c7838fbdSSasha Levin 	return p9dev->vqs[vq].pfn;
1425c7838fbdSSasha Levin }
1426c7838fbdSSasha Levin 
1427c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
1428c7838fbdSSasha Levin {
1429c7838fbdSSasha Levin 	return VIRTQUEUE_NUM;
1430c7838fbdSSasha Levin }
1431c7838fbdSSasha Levin 
14327aba29c1SWill Deacon static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size)
14337aba29c1SWill Deacon {
14347aba29c1SWill Deacon 	/* FIXME: dynamic */
14357aba29c1SWill Deacon 	return size;
14367aba29c1SWill Deacon }
14377aba29c1SWill Deacon 
143815542babSAndre Przywara struct virtio_ops p9_dev_virtio_ops = {
1439c7838fbdSSasha Levin 	.get_config		= get_config,
1440c7838fbdSSasha Levin 	.get_host_features	= get_host_features,
1441c7838fbdSSasha Levin 	.set_guest_features	= set_guest_features,
1442c7838fbdSSasha Levin 	.init_vq		= init_vq,
1443c7838fbdSSasha Levin 	.notify_vq		= notify_vq,
1444c7838fbdSSasha Levin 	.get_pfn_vq		= get_pfn_vq,
1445c7838fbdSSasha Levin 	.get_size_vq		= get_size_vq,
14467aba29c1SWill Deacon 	.set_size_vq		= set_size_vq,
1447c7838fbdSSasha Levin };
14481c47ce69SSasha Levin 
1449cac9e8fdSSasha Levin int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
1450cac9e8fdSSasha Levin {
1451cac9e8fdSSasha Levin 	char *tag_name;
1452cac9e8fdSSasha Levin 	char tmp[PATH_MAX];
1453cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1454cac9e8fdSSasha Levin 
1455cac9e8fdSSasha Levin 	/*
1456cac9e8fdSSasha Levin 	 * 9p dir can be of the form dirname,tag_name or
1457cac9e8fdSSasha Levin 	 * just dirname. In the later case we use the
1458cac9e8fdSSasha Levin 	 * default tag name
1459cac9e8fdSSasha Levin 	 */
1460cac9e8fdSSasha Levin 	tag_name = strstr(arg, ",");
1461cac9e8fdSSasha Levin 	if (tag_name) {
1462cac9e8fdSSasha Levin 		*tag_name = '\0';
1463cac9e8fdSSasha Levin 		tag_name++;
1464cac9e8fdSSasha Levin 	}
1465cac9e8fdSSasha Levin 	if (realpath(arg, tmp)) {
1466cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, tmp, tag_name) < 0)
1467cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1468cac9e8fdSSasha Levin 	} else
1469cac9e8fdSSasha Levin 		die("Failed resolving 9p path");
1470cac9e8fdSSasha Levin 	return 0;
1471cac9e8fdSSasha Levin }
1472cac9e8fdSSasha Levin 
1473cac9e8fdSSasha Levin int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset)
1474cac9e8fdSSasha Levin {
1475cac9e8fdSSasha Levin 	char path[PATH_MAX];
1476cac9e8fdSSasha Levin 	struct stat st;
1477cac9e8fdSSasha Levin 	struct kvm *kvm = opt->ptr;
1478cac9e8fdSSasha Levin 
1479cac9e8fdSSasha Levin 	if (stat(arg, &st) == 0 &&
1480cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1481cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1482cac9e8fdSSasha Levin 
1483cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1484cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1485cac9e8fdSSasha Levin 
1486cac9e8fdSSasha Levin 		if (realpath(arg, tmp) == 0 ||
1487cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1488cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1489cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = 1;
1490cac9e8fdSSasha Levin 		return 0;
1491cac9e8fdSSasha Levin 	}
1492cac9e8fdSSasha Levin 
1493cac9e8fdSSasha Levin 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
1494cac9e8fdSSasha Levin 
1495cac9e8fdSSasha Levin 	if (stat(path, &st) == 0 &&
1496cac9e8fdSSasha Levin 	    S_ISDIR(st.st_mode)) {
1497cac9e8fdSSasha Levin 		char tmp[PATH_MAX];
1498cac9e8fdSSasha Levin 
1499cac9e8fdSSasha Levin 		if (kvm->cfg.using_rootfs)
1500cac9e8fdSSasha Levin 			die("Please use only one rootfs directory atmost");
1501cac9e8fdSSasha Levin 
1502cac9e8fdSSasha Levin 		if (realpath(path, tmp) == 0 ||
1503cac9e8fdSSasha Levin 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1504cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1505cac9e8fdSSasha Levin 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
1506cac9e8fdSSasha Levin 			die("Unable to initialize virtio 9p");
1507cac9e8fdSSasha Levin 		kvm_setup_resolv(arg);
1508cac9e8fdSSasha Levin 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
1509cac9e8fdSSasha Levin 		kvm->cfg.custom_rootfs_name = arg;
1510cac9e8fdSSasha Levin 		return 0;
1511cac9e8fdSSasha Levin 	}
1512cac9e8fdSSasha Levin 
1513cac9e8fdSSasha Levin 	return -1;
1514cac9e8fdSSasha Levin }
1515cac9e8fdSSasha Levin 
15161c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm)
15171c47ce69SSasha Levin {
15181c47ce69SSasha Levin 	struct p9_dev *p9dev;
15191c47ce69SSasha Levin 
15201c47ce69SSasha Levin 	list_for_each_entry(p9dev, &devs, list) {
152102eca50cSAsias He 		virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops,
1522d97dadecSWill Deacon 			    VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_9P,
1523ae06ce71SWill Deacon 			    VIRTIO_ID_9P, PCI_CLASS_9P);
1524c7838fbdSSasha Levin 	}
1525c7838fbdSSasha Levin 
1526c7838fbdSSasha Levin 	return 0;
1527c7838fbdSSasha Levin }
152849a8afd1SSasha Levin virtio_dev_init(virtio_9p__init);
1529c7838fbdSSasha Levin 
1530c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name)
1531c7838fbdSSasha Levin {
1532c7838fbdSSasha Levin 	struct p9_dev *p9dev;
153354f6802dSPekka Enberg 	int err = 0;
15341c7850f9SSasha Levin 
1535b4422bf3SAneesh Kumar K.V 	p9dev = calloc(1, sizeof(*p9dev));
1536b4422bf3SAneesh Kumar K.V 	if (!p9dev)
153754f6802dSPekka Enberg 		return -ENOMEM;
153854f6802dSPekka Enberg 
1539b4422bf3SAneesh Kumar K.V 	if (!tag_name)
15405529bcd7SAsias He 		tag_name = VIRTIO_9P_DEFAULT_TAG;
154154f6802dSPekka Enberg 
1542b4422bf3SAneesh Kumar K.V 	p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1);
154354f6802dSPekka Enberg 	if (p9dev->config == NULL) {
154454f6802dSPekka Enberg 		err = -ENOMEM;
1545b4422bf3SAneesh Kumar K.V 		goto free_p9dev;
154654f6802dSPekka Enberg 	}
15471c7850f9SSasha Levin 
1548e277a1b4SG. Campana 	strncpy(p9dev->root_dir, root, sizeof(p9dev->root_dir));
1549e277a1b4SG. Campana 	p9dev->root_dir[sizeof(p9dev->root_dir)-1] = '\x00';
1550e277a1b4SG. Campana 
1551b4422bf3SAneesh Kumar K.V 	p9dev->config->tag_len = strlen(tag_name);
155254f6802dSPekka Enberg 	if (p9dev->config->tag_len > MAX_TAG_LEN) {
155354f6802dSPekka Enberg 		err = -EINVAL;
1554b4422bf3SAneesh Kumar K.V 		goto free_p9dev_config;
155554f6802dSPekka Enberg 	}
15561c7850f9SSasha Levin 
1557c7838fbdSSasha Levin 	memcpy(&p9dev->config->tag, tag_name, strlen(tag_name));
15581c7850f9SSasha Levin 
1559c7838fbdSSasha Levin 	list_add(&p9dev->list, &devs);
1560b4422bf3SAneesh Kumar K.V 
1561d278197dSAsias He 	if (compat_id == -1)
156252f34d2cSAsias He 		compat_id = virtio_compat_add_message("virtio-9p", "CONFIG_NET_9P_VIRTIO");
1563e59662b3SSasha Levin 
156454f6802dSPekka Enberg 	return err;
156554f6802dSPekka Enberg 
1566b4422bf3SAneesh Kumar K.V free_p9dev_config:
1567b4422bf3SAneesh Kumar K.V 	free(p9dev->config);
1568b4422bf3SAneesh Kumar K.V free_p9dev:
1569b4422bf3SAneesh Kumar K.V 	free(p9dev);
157054f6802dSPekka Enberg 	return err;
15711c7850f9SSasha Levin }
1572