xref: /kvmtool/virtio/9p.c (revision 5529bcd7ad9ccda2d66d8b7cfe1e8bba0e8b2a0f)
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"
81c7850f9SSasha Levin 
9bfc15268SAneesh Kumar K.V #include <stdio.h>
10bfc15268SAneesh Kumar K.V #include <stdlib.h>
111c7850f9SSasha Levin #include <fcntl.h>
121c7850f9SSasha Levin #include <sys/stat.h>
13bfc15268SAneesh Kumar K.V #include <unistd.h>
14bfc15268SAneesh Kumar K.V #include <string.h>
15bfc15268SAneesh Kumar K.V #include <errno.h>
16c797b6c6SAneesh Kumar K.V #include <sys/vfs.h>
171c7850f9SSasha Levin 
182daa28d4SAneesh Kumar K.V #include <linux/virtio_ring.h>
192daa28d4SAneesh Kumar K.V #include <linux/virtio_9p.h>
202daa28d4SAneesh Kumar K.V #include <net/9p/9p.h>
212daa28d4SAneesh Kumar K.V 
22c7838fbdSSasha Levin static LIST_HEAD(devs);
23312c62d1SSasha Levin static int compat_id = -1;
24c7838fbdSSasha Levin 
251c7850f9SSasha Levin /* Warning: Immediately use value returned from this function */
26b4422bf3SAneesh Kumar K.V static const char *rel_to_abs(struct p9_dev *p9dev,
27b4422bf3SAneesh Kumar K.V 			      const char *path, char *abs_path)
281c7850f9SSasha Levin {
29b4422bf3SAneesh Kumar K.V 	sprintf(abs_path, "%s/%s", p9dev->root_dir, path);
301c7850f9SSasha Levin 
311c7850f9SSasha Levin 	return abs_path;
321c7850f9SSasha Levin }
331c7850f9SSasha Levin 
34c797b6c6SAneesh Kumar K.V static void stat2qid(struct stat *st, struct p9_qid *qid)
351c7850f9SSasha Levin {
361c7850f9SSasha Levin 	*qid = (struct p9_qid) {
371c7850f9SSasha Levin 		.path		= st->st_ino,
381c7850f9SSasha Levin 		.version	= st->st_mtime,
391c7850f9SSasha Levin 	};
401c7850f9SSasha Levin 
411c7850f9SSasha Levin 	if (S_ISDIR(st->st_mode))
421c7850f9SSasha Levin 		qid->type	|= P9_QTDIR;
431c7850f9SSasha Levin }
441c7850f9SSasha Levin 
45b4422bf3SAneesh Kumar K.V static void close_fid(struct p9_dev *p9dev, u32 fid)
461c7850f9SSasha Levin {
47b4422bf3SAneesh Kumar K.V 	if (p9dev->fids[fid].fd > 0) {
48b4422bf3SAneesh Kumar K.V 		close(p9dev->fids[fid].fd);
49b4422bf3SAneesh Kumar K.V 		p9dev->fids[fid].fd = -1;
501c7850f9SSasha Levin 	}
51b4422bf3SAneesh Kumar K.V 	if (p9dev->fids[fid].dir) {
52b4422bf3SAneesh Kumar K.V 		closedir(p9dev->fids[fid].dir);
53b4422bf3SAneesh Kumar K.V 		p9dev->fids[fid].dir = NULL;
541c7850f9SSasha Levin 	}
55c797b6c6SAneesh Kumar K.V 	p9dev->fids[fid].fid = P9_NOFID;
561c7850f9SSasha Levin }
571c7850f9SSasha Levin 
58bfc15268SAneesh Kumar K.V static void virtio_p9_set_reply_header(struct p9_pdu *pdu, u32 size)
591c7850f9SSasha Levin {
60bfc15268SAneesh Kumar K.V 	u8 cmd;
61bfc15268SAneesh Kumar K.V 	u16 tag;
62bfc15268SAneesh Kumar K.V 
63bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32);
64bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "bw", &cmd, &tag);
65bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
66bfc15268SAneesh Kumar K.V 	/* cmd + 1 is the reply message */
67bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", size, cmd + 1, tag);
681c7850f9SSasha Levin }
691c7850f9SSasha Levin 
706b163a87SAneesh Kumar K.V static u16 virtio_p9_update_iov_cnt(struct iovec iov[], u32 count, int iov_cnt)
716b163a87SAneesh Kumar K.V {
726b163a87SAneesh Kumar K.V 	int i;
736b163a87SAneesh Kumar K.V 	u32 total = 0;
746b163a87SAneesh Kumar K.V 	for (i = 0; (i < iov_cnt) && (total < count); i++) {
756b163a87SAneesh Kumar K.V 		if (total + iov[i].iov_len > count) {
766b163a87SAneesh Kumar K.V 			/* we don't need this iov fully */
776b163a87SAneesh Kumar K.V 			iov[i].iov_len -= ((total + iov[i].iov_len) - count);
786b163a87SAneesh Kumar K.V 			i++;
796b163a87SAneesh Kumar K.V 			break;
806b163a87SAneesh Kumar K.V 		}
816b163a87SAneesh Kumar K.V 		total += iov[i].iov_len;
826b163a87SAneesh Kumar K.V 	}
836b163a87SAneesh Kumar K.V 	return i;
846b163a87SAneesh Kumar K.V }
856b163a87SAneesh Kumar K.V 
86eee1ba8eSAneesh Kumar K.V static void virtio_p9_error_reply(struct p9_dev *p9dev,
87eee1ba8eSAneesh Kumar K.V 				  struct p9_pdu *pdu, int err, u32 *outlen)
88eee1ba8eSAneesh Kumar K.V {
89bfc15268SAneesh Kumar K.V 	u16 tag;
90eee1ba8eSAneesh Kumar K.V 
91*5529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
92c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", err);
93bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
94eee1ba8eSAneesh Kumar K.V 
95c797b6c6SAneesh Kumar K.V 	/* read the tag from input */
96bfc15268SAneesh Kumar K.V 	pdu->read_offset = sizeof(u32) + sizeof(u8);
97bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "w", &tag);
98bfc15268SAneesh Kumar K.V 
99c797b6c6SAneesh Kumar K.V 	/* Update the header */
100bfc15268SAneesh Kumar K.V 	pdu->write_offset = 0;
101c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "dbw", *outlen, P9_RLERROR, tag);
102eee1ba8eSAneesh Kumar K.V }
103eee1ba8eSAneesh Kumar K.V 
104ead43b01SAneesh Kumar K.V static void virtio_p9_version(struct p9_dev *p9dev,
105af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
1061c7850f9SSasha Levin {
107c797b6c6SAneesh Kumar K.V 	u32 msize;
108c797b6c6SAneesh Kumar K.V 	char *version;
109c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ds", &msize, &version);
110c797b6c6SAneesh Kumar K.V 	/*
111c797b6c6SAneesh Kumar K.V 	 * reply with the same msize the client sent us
112c797b6c6SAneesh Kumar K.V 	 * Error out if the request is not for 9P2000.L
113c797b6c6SAneesh Kumar K.V 	 */
114*5529bcd7SAsias He 	if (!strcmp(version, VIRTIO_9P_VERSION_DOTL))
115c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, version);
116c797b6c6SAneesh Kumar K.V 	else
117c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "ds", msize, "unknown");
1181c7850f9SSasha Levin 
119bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
120bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
121c797b6c6SAneesh Kumar K.V 	free(version);
122ead43b01SAneesh Kumar K.V 	return;
1231c7850f9SSasha Levin }
1241c7850f9SSasha Levin 
125ead43b01SAneesh Kumar K.V static void virtio_p9_clunk(struct p9_dev *p9dev,
126af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
1271c7850f9SSasha Levin {
128bfc15268SAneesh Kumar K.V 	u32 fid;
1291c7850f9SSasha Levin 
130bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid);
131bfc15268SAneesh Kumar K.V 	close_fid(p9dev, fid);
1321c7850f9SSasha Levin 
133bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
134bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
135ead43b01SAneesh Kumar K.V 	return;
1361c7850f9SSasha Levin }
1371c7850f9SSasha Levin 
138c797b6c6SAneesh Kumar K.V /*
139c797b6c6SAneesh Kumar K.V  * FIXME!! Need to map to protocol independent value. Upstream
140c797b6c6SAneesh Kumar K.V  * 9p also have the same BUG
141c797b6c6SAneesh Kumar K.V  */
142c797b6c6SAneesh Kumar K.V static int virtio_p9_openflags(int flags)
143c797b6c6SAneesh Kumar K.V {
144c797b6c6SAneesh Kumar K.V 	flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT | O_DIRECT);
145c797b6c6SAneesh Kumar K.V 	flags |= O_NOFOLLOW;
146c797b6c6SAneesh Kumar K.V 	return flags;
147c797b6c6SAneesh Kumar K.V }
148c797b6c6SAneesh Kumar K.V 
149ead43b01SAneesh Kumar K.V static void virtio_p9_open(struct p9_dev *p9dev,
150af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
1511c7850f9SSasha Levin {
152c797b6c6SAneesh Kumar K.V 	u32 fid, flags;
1531c7850f9SSasha Levin 	struct stat st;
154bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
155bfc15268SAneesh Kumar K.V 	struct p9_fid *new_fid;
156bfc15268SAneesh Kumar K.V 
157c797b6c6SAneesh Kumar K.V 
158c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid, &flags);
159bfc15268SAneesh Kumar K.V 	new_fid = &p9dev->fids[fid];
1601c7850f9SSasha Levin 
16130204a77SAneesh Kumar K.V 	if (lstat(new_fid->abs_path, &st) < 0)
162eee1ba8eSAneesh Kumar K.V 		goto err_out;
1631c7850f9SSasha Levin 
164c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
1651c7850f9SSasha Levin 
166eee1ba8eSAneesh Kumar K.V 	if (new_fid->is_dir) {
1671c7850f9SSasha Levin 		new_fid->dir = opendir(new_fid->abs_path);
168eee1ba8eSAneesh Kumar K.V 		if (!new_fid->dir)
169eee1ba8eSAneesh Kumar K.V 			goto err_out;
170eee1ba8eSAneesh Kumar K.V 	} else {
171eee1ba8eSAneesh Kumar K.V 		new_fid->fd  = open(new_fid->abs_path,
172c797b6c6SAneesh Kumar K.V 				    virtio_p9_openflags(flags));
173eee1ba8eSAneesh Kumar K.V 		if (new_fid->fd < 0)
174eee1ba8eSAneesh Kumar K.V 			goto err_out;
175eee1ba8eSAneesh Kumar K.V 	}
176c797b6c6SAneesh Kumar K.V 	/* FIXME!! need ot send proper iounit  */
177bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
178bfc15268SAneesh Kumar K.V 
179bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
180bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
181ead43b01SAneesh Kumar K.V 	return;
182eee1ba8eSAneesh Kumar K.V err_out:
183eee1ba8eSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
184ead43b01SAneesh Kumar K.V 	return;
1851c7850f9SSasha Levin }
1861c7850f9SSasha Levin 
187ead43b01SAneesh Kumar K.V static void virtio_p9_create(struct p9_dev *p9dev,
188af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
1891c7850f9SSasha Levin {
190c797b6c6SAneesh Kumar K.V 	int fd, ret;
191bfc15268SAneesh Kumar K.V 	char *name;
192af045e53SAneesh Kumar K.V 	struct stat st;
193bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
194c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
1954bc9734aSAneesh Kumar K.V 	char full_path[PATH_MAX];
196c797b6c6SAneesh Kumar K.V 	u32 dfid_val, flags, mode, gid;
197af045e53SAneesh Kumar K.V 
198c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsddd", &dfid_val,
199c797b6c6SAneesh Kumar K.V 			    &name, &flags, &mode, &gid);
200c797b6c6SAneesh Kumar K.V 	dfid = &p9dev->fids[dfid_val];
2011c7850f9SSasha Levin 
202c797b6c6SAneesh Kumar K.V 	flags = virtio_p9_openflags(flags);
2035f900f6dSSasha Levin 
204c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
205c797b6c6SAneesh Kumar K.V 	fd = open(full_path, flags | O_CREAT, mode);
2064bc9734aSAneesh Kumar K.V 	if (fd < 0)
2074bc9734aSAneesh Kumar K.V 		goto err_out;
208c797b6c6SAneesh Kumar K.V 	close_fid(p9dev, dfid_val);
209c797b6c6SAneesh Kumar K.V 	dfid->fd = fd;
210c797b6c6SAneesh Kumar K.V 
2114bc9734aSAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
2126c8ca053SAneesh Kumar K.V 		goto err_out;
2131c7850f9SSasha Levin 
214c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
215c797b6c6SAneesh Kumar K.V 	if (ret < 0)
216c797b6c6SAneesh Kumar K.V 		goto err_out;
217c797b6c6SAneesh Kumar K.V 
218c797b6c6SAneesh Kumar K.V 	ret = lchown(full_path, dfid->uid, gid);
219c797b6c6SAneesh Kumar K.V 	if (ret < 0)
220c797b6c6SAneesh Kumar K.V 		goto err_out;
221c797b6c6SAneesh Kumar K.V 
222c797b6c6SAneesh Kumar K.V 	sprintf(dfid->path, "%s/%s", dfid->path, name);
223c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
224bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
225bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
226bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
2275f900f6dSSasha Levin 	free(name);
2286c8ca053SAneesh Kumar K.V 	return;
2296c8ca053SAneesh Kumar K.V err_out:
2305f900f6dSSasha Levin 	free(name);
231c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
232c797b6c6SAneesh Kumar K.V 	return;
233c797b6c6SAneesh Kumar K.V }
234c797b6c6SAneesh Kumar K.V 
235c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev,
236c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
237c797b6c6SAneesh Kumar K.V {
238c797b6c6SAneesh Kumar K.V 	int ret;
239c797b6c6SAneesh Kumar K.V 	char *name;
240c797b6c6SAneesh Kumar K.V 	struct stat st;
241c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
242c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
243c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
244c797b6c6SAneesh Kumar K.V 	u32 dfid_val, mode, gid;
245c797b6c6SAneesh Kumar K.V 
246c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val,
247c797b6c6SAneesh Kumar K.V 			    &name, &mode, &gid);
248c797b6c6SAneesh Kumar K.V 	dfid = &p9dev->fids[dfid_val];
249c797b6c6SAneesh Kumar K.V 
250c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
251c797b6c6SAneesh Kumar K.V 	ret = mkdir(full_path, mode);
252c797b6c6SAneesh Kumar K.V 	if (ret < 0)
253c797b6c6SAneesh Kumar K.V 		goto err_out;
254c797b6c6SAneesh Kumar K.V 
255c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
256c797b6c6SAneesh Kumar K.V 		goto err_out;
257c797b6c6SAneesh Kumar K.V 
258c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
259c797b6c6SAneesh Kumar K.V 	if (ret < 0)
260c797b6c6SAneesh Kumar K.V 		goto err_out;
261c797b6c6SAneesh Kumar K.V 
262c797b6c6SAneesh Kumar K.V 	ret = lchown(full_path, dfid->uid, gid);
263c797b6c6SAneesh Kumar K.V 	if (ret < 0)
264c797b6c6SAneesh Kumar K.V 		goto err_out;
265c797b6c6SAneesh Kumar K.V 
266c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
267c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Qd", &qid, 0);
268c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
269c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
270c797b6c6SAneesh Kumar K.V 	free(name);
271c797b6c6SAneesh Kumar K.V 	return;
272c797b6c6SAneesh Kumar K.V err_out:
273c797b6c6SAneesh Kumar K.V 	free(name);
274c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
275ead43b01SAneesh Kumar K.V 	return;
2761c7850f9SSasha Levin }
2771c7850f9SSasha Levin 
278ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev,
279af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
2801c7850f9SSasha Levin {
281af045e53SAneesh Kumar K.V 	u8 i;
282bfc15268SAneesh Kumar K.V 	u16 nwqid;
283bfc15268SAneesh Kumar K.V 	u16 nwname;
284bfc15268SAneesh Kumar K.V 	struct p9_qid wqid;
285bfc15268SAneesh Kumar K.V 	struct p9_fid *new_fid;
286c797b6c6SAneesh Kumar K.V 	u32 fid_val, newfid_val;
287c797b6c6SAneesh Kumar K.V 
2881c7850f9SSasha Levin 
289bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname);
290bfc15268SAneesh Kumar K.V 	new_fid	= &p9dev->fids[newfid_val];
2911c7850f9SSasha Levin 
292bfc15268SAneesh Kumar K.V 	nwqid = 0;
293bfc15268SAneesh Kumar K.V 	if (nwname) {
294bfc15268SAneesh Kumar K.V 		struct p9_fid *fid = &p9dev->fids[fid_val];
295bfc15268SAneesh Kumar K.V 
296baac79a5SAneesh Kumar K.V 		strcpy(new_fid->path, fid->path);
297bfc15268SAneesh Kumar K.V 		/* skip the space for count */
298bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
299bfc15268SAneesh Kumar K.V 		for (i = 0; i < nwname; i++) {
300bfc15268SAneesh Kumar K.V 			struct stat st;
3011c7850f9SSasha Levin 			char tmp[PATH_MAX] = {0};
3021c7850f9SSasha Levin 			char full_path[PATH_MAX];
303e55ed135SPekka Enberg 			char *str;
304bfc15268SAneesh Kumar K.V 
305bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_readf(pdu, "s", &str);
3061c7850f9SSasha Levin 
3071c7850f9SSasha Levin 			/* Format the new path we're 'walk'ing into */
308baac79a5SAneesh Kumar K.V 			sprintf(tmp, "%s/%s", new_fid->path, str);
309e55ed135SPekka Enberg 
310e55ed135SPekka Enberg 			free(str);
311e55ed135SPekka Enberg 
312c797b6c6SAneesh Kumar K.V 			if (lstat(rel_to_abs(p9dev, tmp, full_path), &st) < 0)
3136c8ca053SAneesh Kumar K.V 				goto err_out;
3141c7850f9SSasha Levin 
315c797b6c6SAneesh Kumar K.V 			stat2qid(&st, &wqid);
3161c7850f9SSasha Levin 			new_fid->is_dir = S_ISDIR(st.st_mode);
3171c7850f9SSasha Levin 			strcpy(new_fid->path, tmp);
318bfc15268SAneesh Kumar K.V 			new_fid->fid = newfid_val;
319c797b6c6SAneesh Kumar K.V 			new_fid->uid = fid->uid;
320bfc15268SAneesh Kumar K.V 			nwqid++;
321bfc15268SAneesh Kumar K.V 			virtio_p9_pdu_writef(pdu, "Q", &wqid);
3221c7850f9SSasha Levin 		}
3231c7850f9SSasha Levin 	} else {
324bfc15268SAneesh Kumar K.V 		/*
325bfc15268SAneesh Kumar K.V 		 * update write_offset so our outlen get correct value
326bfc15268SAneesh Kumar K.V 		 */
327bfc15268SAneesh Kumar K.V 		pdu->write_offset += sizeof(u16);
328bfc15268SAneesh Kumar K.V 		new_fid->is_dir = p9dev->fids[fid_val].is_dir;
329bfc15268SAneesh Kumar K.V 		strcpy(new_fid->path, p9dev->fids[fid_val].path);
330bfc15268SAneesh Kumar K.V 		new_fid->fid	= newfid_val;
331c797b6c6SAneesh Kumar K.V 		new_fid->uid    = p9dev->fids[fid_val].uid;
3321c7850f9SSasha Levin 	}
333bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
334*5529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
335bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", nwqid);
336bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
3376c8ca053SAneesh Kumar K.V 	return;
3386c8ca053SAneesh Kumar K.V err_out:
3396c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
340ead43b01SAneesh Kumar K.V 	return;
3411c7850f9SSasha Levin }
3421c7850f9SSasha Levin 
343ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev,
344af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
3451c7850f9SSasha Levin {
346c797b6c6SAneesh Kumar K.V 	int i;
347bfc15268SAneesh Kumar K.V 	char *uname;
348bfc15268SAneesh Kumar K.V 	char *aname;
3491c7850f9SSasha Levin 	struct stat st;
350bfc15268SAneesh Kumar K.V 	struct p9_qid qid;
3511c7850f9SSasha Levin 	struct p9_fid *fid;
352c797b6c6SAneesh Kumar K.V 	u32 fid_val, afid, uid;
353bfc15268SAneesh Kumar K.V 
354c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid,
355c797b6c6SAneesh Kumar K.V 			    &uname, &aname, &uid);
3561c7850f9SSasha Levin 
35739257180SPekka Enberg 	free(uname);
35839257180SPekka Enberg 	free(aname);
35939257180SPekka Enberg 
3601c7850f9SSasha Levin 	/* Reset everything */
361*5529bcd7SAsias He 	for (i = 0; i < VIRTIO_9P_MAX_FID; i++)
362b4422bf3SAneesh Kumar K.V 		p9dev->fids[i].fid = P9_NOFID;
3631c7850f9SSasha Levin 
36430204a77SAneesh Kumar K.V 	if (lstat(p9dev->root_dir, &st) < 0)
3656c8ca053SAneesh Kumar K.V 		goto err_out;
3661c7850f9SSasha Levin 
367c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
3681c7850f9SSasha Levin 
369bfc15268SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
370bfc15268SAneesh Kumar K.V 	fid->fid = fid_val;
371c797b6c6SAneesh Kumar K.V 	fid->uid = uid;
3721c7850f9SSasha Levin 	fid->is_dir = 1;
3731c7850f9SSasha Levin 	strcpy(fid->path, "/");
3741c7850f9SSasha Levin 
375bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
376bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
377bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
3786c8ca053SAneesh Kumar K.V 	return;
3796c8ca053SAneesh Kumar K.V err_out:
3806c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
381ead43b01SAneesh Kumar K.V 	return;
3821c7850f9SSasha Levin }
3831c7850f9SSasha Levin 
384c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev,
385c797b6c6SAneesh Kumar K.V 				struct stat *st, struct p9_stat_dotl *statl)
3865f900f6dSSasha Levin {
387c797b6c6SAneesh Kumar K.V 	memset(statl, 0, sizeof(*statl));
388c797b6c6SAneesh Kumar K.V 	statl->st_mode = st->st_mode;
389c797b6c6SAneesh Kumar K.V 	statl->st_nlink = st->st_nlink;
390c797b6c6SAneesh Kumar K.V 	statl->st_uid = st->st_uid;
391c797b6c6SAneesh Kumar K.V 	statl->st_gid = st->st_gid;
392c797b6c6SAneesh Kumar K.V 	statl->st_rdev = st->st_rdev;
393c797b6c6SAneesh Kumar K.V 	statl->st_size = st->st_size;
394c797b6c6SAneesh Kumar K.V 	statl->st_blksize = st->st_blksize;
395c797b6c6SAneesh Kumar K.V 	statl->st_blocks = st->st_blocks;
396c797b6c6SAneesh Kumar K.V 	statl->st_atime_sec = st->st_atime;
397c797b6c6SAneesh Kumar K.V 	statl->st_atime_nsec = st->st_atim.tv_nsec;
398c797b6c6SAneesh Kumar K.V 	statl->st_mtime_sec = st->st_mtime;
399c797b6c6SAneesh Kumar K.V 	statl->st_mtime_nsec = st->st_mtim.tv_nsec;
400c797b6c6SAneesh Kumar K.V 	statl->st_ctime_sec = st->st_ctime;
401c797b6c6SAneesh Kumar K.V 	statl->st_ctime_nsec = st->st_ctim.tv_nsec;
402c797b6c6SAneesh Kumar K.V 	/* Currently we only support BASIC fields in stat */
403c797b6c6SAneesh Kumar K.V 	statl->st_result_mask = P9_STATS_BASIC;
404c797b6c6SAneesh Kumar K.V 	stat2qid(st, &statl->qid);
4051c7850f9SSasha Levin }
4061c7850f9SSasha Levin 
407ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev,
408af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
4091c7850f9SSasha Levin {
410bfc15268SAneesh Kumar K.V 	u64 offset;
411bfc15268SAneesh Kumar K.V 	u32 fid_val;
412c797b6c6SAneesh Kumar K.V 	u16 iov_cnt;
413c797b6c6SAneesh Kumar K.V 	void *iov_base;
414c797b6c6SAneesh Kumar K.V 	size_t iov_len;
415bfc15268SAneesh Kumar K.V 	u32 count, rcount;
416bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
417c797b6c6SAneesh Kumar K.V 
4181c7850f9SSasha Levin 
419bfc15268SAneesh Kumar K.V 	rcount = 0;
420bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
421bfc15268SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
42250c479e0SAneesh Kumar K.V 
42350c479e0SAneesh Kumar K.V 	iov_base = pdu->in_iov[0].iov_base;
42450c479e0SAneesh Kumar K.V 	iov_len  = pdu->in_iov[0].iov_len;
42550c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->in_iov_cnt;
426*5529bcd7SAsias He 	pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32);
427*5529bcd7SAsias He 	pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32);
4286b163a87SAneesh Kumar K.V 	pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov,
429bfc15268SAneesh Kumar K.V 						   count,
4306b163a87SAneesh Kumar K.V 						   pdu->in_iov_cnt);
431bfc15268SAneesh Kumar K.V 	rcount = preadv(fid->fd, pdu->in_iov,
432bfc15268SAneesh Kumar K.V 			pdu->in_iov_cnt, offset);
433bfc15268SAneesh Kumar K.V 	if (rcount > count)
434bfc15268SAneesh Kumar K.V 		rcount = count;
435bfc15268SAneesh Kumar K.V 	/*
436bfc15268SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
437bfc15268SAneesh Kumar K.V 	 * pdu_writef works correctly.
438bfc15268SAneesh Kumar K.V 	 */
43950c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_base = iov_base;
44050c479e0SAneesh Kumar K.V 	pdu->in_iov[0].iov_len  = iov_len;
44150c479e0SAneesh Kumar K.V 	pdu->in_iov_cnt         = iov_cnt;
442c797b6c6SAneesh Kumar K.V 
443*5529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
444bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
445bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
446bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
447ead43b01SAneesh Kumar K.V 	return;
4481c7850f9SSasha Levin }
4491c7850f9SSasha Levin 
450c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent)
451c797b6c6SAneesh Kumar K.V {
452c797b6c6SAneesh Kumar K.V 	/*
453c797b6c6SAneesh Kumar K.V 	 * Size of each dirent:
454c797b6c6SAneesh Kumar K.V 	 * qid(13) + offset(8) + type(1) + name_len(2) + name
455c797b6c6SAneesh Kumar K.V 	 */
456c797b6c6SAneesh Kumar K.V 	return 24 + strlen(dent->d_name);
457c797b6c6SAneesh Kumar K.V }
458c797b6c6SAneesh Kumar K.V 
459c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev,
460c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
461c797b6c6SAneesh Kumar K.V {
462c797b6c6SAneesh Kumar K.V 	u32 fid_val;
463c797b6c6SAneesh Kumar K.V 	u32 count, rcount;
464c797b6c6SAneesh Kumar K.V 	struct stat st;
465c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
466c797b6c6SAneesh Kumar K.V 	struct dirent *dent;
467c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
468c797b6c6SAneesh Kumar K.V 	u64 offset, old_offset;
469c797b6c6SAneesh Kumar K.V 
470c797b6c6SAneesh Kumar K.V 	rcount = 0;
471c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
472c797b6c6SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
473c797b6c6SAneesh Kumar K.V 
474c797b6c6SAneesh Kumar K.V 	if (!fid->is_dir) {
475c797b6c6SAneesh Kumar K.V 		errno = -EINVAL;
476c797b6c6SAneesh Kumar K.V 		goto err_out;
477c797b6c6SAneesh Kumar K.V 	}
478c797b6c6SAneesh Kumar K.V 
479c797b6c6SAneesh Kumar K.V 	/* Move the offset specified */
480c797b6c6SAneesh Kumar K.V 	seekdir(fid->dir, offset);
481c797b6c6SAneesh Kumar K.V 
482c797b6c6SAneesh Kumar K.V 	old_offset = offset;
483c797b6c6SAneesh Kumar K.V 	/* If reading a dir, fill the buffer with p9_stat entries */
484c797b6c6SAneesh Kumar K.V 	dent = readdir(fid->dir);
485c797b6c6SAneesh Kumar K.V 
486c797b6c6SAneesh Kumar K.V 	/* Skip the space for writing count */
487c797b6c6SAneesh Kumar K.V 	pdu->write_offset += sizeof(u32);
488c797b6c6SAneesh Kumar K.V 	while (dent) {
489c797b6c6SAneesh Kumar K.V 		u32 read;
490c797b6c6SAneesh Kumar K.V 		struct p9_qid qid;
491c797b6c6SAneesh Kumar K.V 
492c797b6c6SAneesh Kumar K.V 		if ((rcount + virtio_p9_dentry_size(dent)) > count) {
493c797b6c6SAneesh Kumar K.V 			/* seek to the previous offset and return */
494c797b6c6SAneesh Kumar K.V 			seekdir(fid->dir, old_offset);
495c797b6c6SAneesh Kumar K.V 			break;
496c797b6c6SAneesh Kumar K.V 		}
497c797b6c6SAneesh Kumar K.V 		old_offset = dent->d_off;
498c797b6c6SAneesh Kumar K.V 		lstat(rel_to_abs(p9dev, dent->d_name, full_path), &st);
499c797b6c6SAneesh Kumar K.V 		stat2qid(&st, &qid);
500c797b6c6SAneesh Kumar K.V 		read = pdu->write_offset;
501c797b6c6SAneesh Kumar K.V 		virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off,
502c797b6c6SAneesh Kumar K.V 				     dent->d_type, dent->d_name);
503c797b6c6SAneesh Kumar K.V 		rcount += pdu->write_offset - read;
504c797b6c6SAneesh Kumar K.V 		dent = readdir(fid->dir);
505c797b6c6SAneesh Kumar K.V 	}
506c797b6c6SAneesh Kumar K.V 
507*5529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
508c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", rcount);
509c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset + rcount;
510c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
511c797b6c6SAneesh Kumar K.V 	return;
512c797b6c6SAneesh Kumar K.V err_out:
513c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
514c797b6c6SAneesh Kumar K.V 	return;
515c797b6c6SAneesh Kumar K.V }
516c797b6c6SAneesh Kumar K.V 
517c797b6c6SAneesh Kumar K.V 
518c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev,
519af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
5201c7850f9SSasha Levin {
521bfc15268SAneesh Kumar K.V 	u32 fid_val;
522af045e53SAneesh Kumar K.V 	struct stat st;
523c797b6c6SAneesh Kumar K.V 	u64 request_mask;
524bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
525c797b6c6SAneesh Kumar K.V 	struct p9_stat_dotl statl;
5261c7850f9SSasha Levin 
527c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask);
528bfc15268SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
52930204a77SAneesh Kumar K.V 	if (lstat(fid->abs_path, &st) < 0)
5306c8ca053SAneesh Kumar K.V 		goto err_out;
5311c7850f9SSasha Levin 
532c797b6c6SAneesh Kumar K.V 	virtio_p9_fill_stat(p9dev, &st, &statl);
533c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "A", &statl);
534bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
535bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
536ead43b01SAneesh Kumar K.V 	return;
5376c8ca053SAneesh Kumar K.V err_out:
5386c8ca053SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
5396c8ca053SAneesh Kumar K.V 	return;
5401c7850f9SSasha Levin }
5411c7850f9SSasha Levin 
542c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */
543c797b6c6SAneesh Kumar K.V /*
544c797b6c6SAneesh Kumar K.V  * Attribute flags.  These should be or-ed together to figure out what
545c797b6c6SAneesh Kumar K.V  * has been changed!
546c797b6c6SAneesh Kumar K.V  */
547c797b6c6SAneesh Kumar K.V #define ATTR_MODE	(1 << 0)
548c797b6c6SAneesh Kumar K.V #define ATTR_UID	(1 << 1)
549c797b6c6SAneesh Kumar K.V #define ATTR_GID	(1 << 2)
550c797b6c6SAneesh Kumar K.V #define ATTR_SIZE	(1 << 3)
551c797b6c6SAneesh Kumar K.V #define ATTR_ATIME	(1 << 4)
552c797b6c6SAneesh Kumar K.V #define ATTR_MTIME	(1 << 5)
553c797b6c6SAneesh Kumar K.V #define ATTR_CTIME	(1 << 6)
554c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET	(1 << 7)
555c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET	(1 << 8)
556c797b6c6SAneesh Kumar K.V #define ATTR_FORCE	(1 << 9) /* Not a change, but a change it */
557c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG	(1 << 10)
558c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID	(1 << 11)
559c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID	(1 << 12)
560c797b6c6SAneesh Kumar K.V #define ATTR_FILE	(1 << 13)
561c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV	(1 << 14)
562c797b6c6SAneesh Kumar K.V #define ATTR_OPEN	(1 << 15) /* Truncating from open(O_TRUNC) */
563c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET	(1 << 16)
564c797b6c6SAneesh Kumar K.V 
565c797b6c6SAneesh Kumar K.V #define ATTR_MASK    127
566c797b6c6SAneesh Kumar K.V 
567c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev,
568af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
5691c7850f9SSasha Levin {
570c797b6c6SAneesh Kumar K.V 	int ret = 0;
571bfc15268SAneesh Kumar K.V 	u32 fid_val;
572bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
573c797b6c6SAneesh Kumar K.V 	struct p9_iattr_dotl p9attr;
5741c7850f9SSasha Levin 
575c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr);
576bfc15268SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
5771c7850f9SSasha Levin 
578c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & ATTR_MODE) {
579c797b6c6SAneesh Kumar K.V 		ret = chmod(fid->abs_path, p9attr.mode);
580c797b6c6SAneesh Kumar K.V 		if (ret < 0)
581c797b6c6SAneesh Kumar K.V 			goto err_out;
582c797b6c6SAneesh Kumar K.V 	}
583c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) {
584c797b6c6SAneesh Kumar K.V 		struct timespec times[2];
585c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_ATIME) {
586c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_ATIME_SET) {
587c797b6c6SAneesh Kumar K.V 				times[0].tv_sec = p9attr.atime_sec;
588c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = p9attr.atime_nsec;
589c797b6c6SAneesh Kumar K.V 			} else {
590c797b6c6SAneesh Kumar K.V 				times[0].tv_nsec = UTIME_NOW;
591c797b6c6SAneesh Kumar K.V 			}
592c797b6c6SAneesh Kumar K.V 		} else {
593c797b6c6SAneesh Kumar K.V 			times[0].tv_nsec = UTIME_OMIT;
594c797b6c6SAneesh Kumar K.V 		}
595c797b6c6SAneesh Kumar K.V 		if (p9attr.valid & ATTR_MTIME) {
596c797b6c6SAneesh Kumar K.V 			if (p9attr.valid & ATTR_MTIME_SET) {
597c797b6c6SAneesh Kumar K.V 				times[1].tv_sec = p9attr.mtime_sec;
598c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = p9attr.mtime_nsec;
599c797b6c6SAneesh Kumar K.V 			} else {
600c797b6c6SAneesh Kumar K.V 				times[1].tv_nsec = UTIME_NOW;
601c797b6c6SAneesh Kumar K.V 			}
602c797b6c6SAneesh Kumar K.V 		} else
603c797b6c6SAneesh Kumar K.V 			times[1].tv_nsec = UTIME_OMIT;
604c797b6c6SAneesh Kumar K.V 
605c797b6c6SAneesh Kumar K.V 		ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW);
606c797b6c6SAneesh Kumar K.V 		if (ret < 0)
607c797b6c6SAneesh Kumar K.V 			goto err_out;
608c797b6c6SAneesh Kumar K.V 	}
609c797b6c6SAneesh Kumar K.V 	/*
610c797b6c6SAneesh Kumar K.V 	 * If the only valid entry in iattr is ctime we can call
611c797b6c6SAneesh Kumar K.V 	 * chown(-1,-1) to update the ctime of the file
612c797b6c6SAneesh Kumar K.V 	 */
613c797b6c6SAneesh Kumar K.V 	if ((p9attr.valid & (ATTR_UID | ATTR_GID)) ||
614c797b6c6SAneesh Kumar K.V 	    ((p9attr.valid & ATTR_CTIME)
615c797b6c6SAneesh Kumar K.V 	     && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
616c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_UID))
617c797b6c6SAneesh Kumar K.V 			p9attr.uid = -1;
618c797b6c6SAneesh Kumar K.V 
619c797b6c6SAneesh Kumar K.V 		if (!(p9attr.valid & ATTR_GID))
620c797b6c6SAneesh Kumar K.V 			p9attr.gid = -1;
621c797b6c6SAneesh Kumar K.V 
622c797b6c6SAneesh Kumar K.V 		ret = lchown(fid->abs_path, p9attr.uid, p9attr.gid);
623c797b6c6SAneesh Kumar K.V 		if (ret < 0)
624c797b6c6SAneesh Kumar K.V 			goto err_out;
625c797b6c6SAneesh Kumar K.V 	}
626c797b6c6SAneesh Kumar K.V 	if (p9attr.valid & (ATTR_SIZE)) {
627c797b6c6SAneesh Kumar K.V 		ret = truncate(fid->abs_path, p9attr.size);
628c797b6c6SAneesh Kumar K.V 		if (ret < 0)
629c797b6c6SAneesh Kumar K.V 			goto err_out;
630c797b6c6SAneesh Kumar K.V 	}
631*5529bcd7SAsias He 	*outlen = VIRTIO_9P_HDR_LEN;
632bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
633ead43b01SAneesh Kumar K.V 	return;
6344bc9734aSAneesh Kumar K.V err_out:
6354bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
6364bc9734aSAneesh Kumar K.V 	return;
6371c7850f9SSasha Levin }
6381c7850f9SSasha Levin 
639ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev,
640af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
6411c7850f9SSasha Levin {
6424bc9734aSAneesh Kumar K.V 
643bfc15268SAneesh Kumar K.V 	u64 offset;
644bfc15268SAneesh Kumar K.V 	u32 fid_val;
6454bc9734aSAneesh Kumar K.V 	u32 count;
6464bc9734aSAneesh Kumar K.V 	ssize_t res;
64750c479e0SAneesh Kumar K.V 	u16 iov_cnt;
64850c479e0SAneesh Kumar K.V 	void *iov_base;
64950c479e0SAneesh Kumar K.V 	size_t iov_len;
650bfc15268SAneesh Kumar K.V 	struct p9_fid *fid;
651b064b05aSAneesh Kumar K.V 	/* u32 fid + u64 offset + u32 count */
652b064b05aSAneesh Kumar K.V 	int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32);
6531c7850f9SSasha Levin 
654bfc15268SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count);
655bfc15268SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
656af045e53SAneesh Kumar K.V 
65750c479e0SAneesh Kumar K.V 	iov_base = pdu->out_iov[0].iov_base;
65850c479e0SAneesh Kumar K.V 	iov_len  = pdu->out_iov[0].iov_len;
65950c479e0SAneesh Kumar K.V 	iov_cnt  = pdu->out_iov_cnt;
66050c479e0SAneesh Kumar K.V 
661bfc15268SAneesh Kumar K.V 	/* Adjust the iovec to skip the header and meta data */
662b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size);
663b064b05aSAneesh Kumar K.V 	pdu->out_iov[0].iov_len -=  (sizeof(struct p9_msg) + twrite_size);
664bfc15268SAneesh Kumar K.V 	pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count,
6656b163a87SAneesh Kumar K.V 						    pdu->out_iov_cnt);
6664bc9734aSAneesh Kumar K.V 	res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset);
66750c479e0SAneesh Kumar K.V 	/*
66850c479e0SAneesh Kumar K.V 	 * Update the iov_base back, so that rest of
66950c479e0SAneesh Kumar K.V 	 * pdu_readf works correctly.
67050c479e0SAneesh Kumar K.V 	 */
67150c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_base = iov_base;
67250c479e0SAneesh Kumar K.V 	pdu->out_iov[0].iov_len  = iov_len;
67350c479e0SAneesh Kumar K.V 	pdu->out_iov_cnt         = iov_cnt;
674c797b6c6SAneesh Kumar K.V 
6754bc9734aSAneesh Kumar K.V 	if (res < 0)
6764bc9734aSAneesh Kumar K.V 		goto err_out;
6774bc9734aSAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", res);
678bfc15268SAneesh Kumar K.V 	*outlen = pdu->write_offset;
679bfc15268SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
680ead43b01SAneesh Kumar K.V 	return;
6814bc9734aSAneesh Kumar K.V err_out:
6824bc9734aSAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
6834bc9734aSAneesh Kumar K.V 	return;
6841c7850f9SSasha Levin }
6851c7850f9SSasha Levin 
6866fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev,
6876fc5cd9bSSasha Levin 			       struct p9_pdu *pdu, u32 *outlen)
6886fc5cd9bSSasha Levin {
6896fc5cd9bSSasha Levin 	int ret;
6906fc5cd9bSSasha Levin 	u32 fid_val;
6916fc5cd9bSSasha Levin 	struct p9_fid *fid;
6926fc5cd9bSSasha Levin 
6936fc5cd9bSSasha Levin 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
6946fc5cd9bSSasha Levin 	fid = &p9dev->fids[fid_val];
6956fc5cd9bSSasha Levin 
6969b604a9cSSasha Levin 	ret = remove(fid->abs_path);
6976fc5cd9bSSasha Levin 	if (ret < 0)
6986fc5cd9bSSasha Levin 		goto err_out;
6996fc5cd9bSSasha Levin 	*outlen = pdu->write_offset;
7006fc5cd9bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
7016fc5cd9bSSasha Levin 	return;
7026fc5cd9bSSasha Levin 
7036fc5cd9bSSasha Levin err_out:
7046fc5cd9bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
7056fc5cd9bSSasha Levin 	return;
7066fc5cd9bSSasha Levin }
7076fc5cd9bSSasha Levin 
708f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev,
709f161f28bSSasha Levin 			       struct p9_pdu *pdu, u32 *outlen)
710f161f28bSSasha Levin {
711f161f28bSSasha Levin 	int ret;
712f161f28bSSasha Levin 	u32 fid_val, new_fid_val;
713f161f28bSSasha Levin 	struct p9_fid *fid, *new_fid;
714f161f28bSSasha Levin 	char full_path[PATH_MAX], *new_name;
715f161f28bSSasha Levin 
716f161f28bSSasha Levin 	virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name);
717f161f28bSSasha Levin 	fid = &p9dev->fids[fid_val];
718f161f28bSSasha Levin 	new_fid = &p9dev->fids[new_fid_val];
719f161f28bSSasha Levin 
720f161f28bSSasha Levin 	sprintf(full_path, "%s/%s", new_fid->abs_path, new_name);
721f161f28bSSasha Levin 	ret = rename(fid->abs_path, full_path);
722f161f28bSSasha Levin 	if (ret < 0)
723f161f28bSSasha Levin 		goto err_out;
724f161f28bSSasha Levin 	close_fid(p9dev, fid_val);
725f161f28bSSasha Levin 	*outlen = pdu->write_offset;
726f161f28bSSasha Levin 	virtio_p9_set_reply_header(pdu, *outlen);
727f161f28bSSasha Levin 	return;
728f161f28bSSasha Levin 
729f161f28bSSasha Levin err_out:
730f161f28bSSasha Levin 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
731f161f28bSSasha Levin 	return;
732f161f28bSSasha Levin }
733f161f28bSSasha Levin 
734c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev,
735c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
736c797b6c6SAneesh Kumar K.V {
737c797b6c6SAneesh Kumar K.V 	int ret;
738c797b6c6SAneesh Kumar K.V 	u32 fid_val;
739c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
740c797b6c6SAneesh Kumar K.V 	char target_path[PATH_MAX];
741c797b6c6SAneesh Kumar K.V 
742c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
743c797b6c6SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
744c797b6c6SAneesh Kumar K.V 
745c797b6c6SAneesh Kumar K.V 	memset(target_path, 0, PATH_MAX);
746c797b6c6SAneesh Kumar K.V 	ret = readlink(fid->abs_path, target_path, PATH_MAX - 1);
747c797b6c6SAneesh Kumar K.V 	if (ret < 0)
748c797b6c6SAneesh Kumar K.V 		goto err_out;
749c797b6c6SAneesh Kumar K.V 
750c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "s", target_path);
751c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
752c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
753c797b6c6SAneesh Kumar K.V 	return;
754c797b6c6SAneesh Kumar K.V err_out:
755c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
756c797b6c6SAneesh Kumar K.V 	return;
757c797b6c6SAneesh Kumar K.V }
758c797b6c6SAneesh Kumar K.V 
759c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev,
760c797b6c6SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
761c797b6c6SAneesh Kumar K.V {
762c797b6c6SAneesh Kumar K.V 	int ret;
763c797b6c6SAneesh Kumar K.V 	u64 fsid;
764c797b6c6SAneesh Kumar K.V 	u32 fid_val;
765c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
766c797b6c6SAneesh Kumar K.V 	struct statfs stat_buf;
767c797b6c6SAneesh Kumar K.V 
768c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "d", &fid_val);
769c797b6c6SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
770c797b6c6SAneesh Kumar K.V 
771c797b6c6SAneesh Kumar K.V 	ret = statfs(fid->abs_path, &stat_buf);
772c797b6c6SAneesh Kumar K.V 	if (ret < 0)
773c797b6c6SAneesh Kumar K.V 		goto err_out;
774c797b6c6SAneesh Kumar K.V 	/* FIXME!! f_blocks needs update based on client msize */
775c797b6c6SAneesh Kumar K.V 	fsid = (unsigned int) stat_buf.f_fsid.__val[0] |
776c797b6c6SAneesh Kumar K.V 		(unsigned long long)stat_buf.f_fsid.__val[1] << 32;
777c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type,
778c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bsize, stat_buf.f_blocks,
779c797b6c6SAneesh Kumar K.V 			     stat_buf.f_bfree, stat_buf.f_bavail,
780c797b6c6SAneesh Kumar K.V 			     stat_buf.f_files, stat_buf.f_ffree,
781c797b6c6SAneesh Kumar K.V 			     fsid, stat_buf.f_namelen);
782c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
783c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
784c797b6c6SAneesh Kumar K.V 	return;
785c797b6c6SAneesh Kumar K.V err_out:
786c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
787c797b6c6SAneesh Kumar K.V 	return;
788c797b6c6SAneesh Kumar K.V }
789c797b6c6SAneesh Kumar K.V 
790c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev,
791c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
792c797b6c6SAneesh Kumar K.V {
793c797b6c6SAneesh Kumar K.V 	int ret;
794c797b6c6SAneesh Kumar K.V 	char *name;
795c797b6c6SAneesh Kumar K.V 	struct stat st;
796c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
797c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
798c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
799c797b6c6SAneesh Kumar K.V 	u32 fid_val, mode, major, minor, gid;
800c797b6c6SAneesh Kumar K.V 
801c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode,
802c797b6c6SAneesh Kumar K.V 			    &major, &minor, &gid);
803c797b6c6SAneesh Kumar K.V 
804c797b6c6SAneesh Kumar K.V 	dfid = &p9dev->fids[fid_val];
805c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
806c797b6c6SAneesh Kumar K.V 	ret = mknod(full_path, mode, makedev(major, minor));
807c797b6c6SAneesh Kumar K.V 	if (ret < 0)
808c797b6c6SAneesh Kumar K.V 		goto err_out;
809c797b6c6SAneesh Kumar K.V 
810c797b6c6SAneesh Kumar K.V 	if (lstat(full_path, &st) < 0)
811c797b6c6SAneesh Kumar K.V 		goto err_out;
812c797b6c6SAneesh Kumar K.V 
813c797b6c6SAneesh Kumar K.V 	ret = chmod(full_path, mode & 0777);
814c797b6c6SAneesh Kumar K.V 	if (ret < 0)
815c797b6c6SAneesh Kumar K.V 		goto err_out;
816c797b6c6SAneesh Kumar K.V 
817c797b6c6SAneesh Kumar K.V 	ret = lchown(full_path, dfid->uid, gid);
818c797b6c6SAneesh Kumar K.V 	if (ret < 0)
819c797b6c6SAneesh Kumar K.V 		goto err_out;
820c797b6c6SAneesh Kumar K.V 
821c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
822c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
823c797b6c6SAneesh Kumar K.V 	free(name);
824c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
825c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
826c797b6c6SAneesh Kumar K.V 	return;
827c797b6c6SAneesh Kumar K.V err_out:
828c797b6c6SAneesh Kumar K.V 	free(name);
829c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
830c797b6c6SAneesh Kumar K.V 	return;
831c797b6c6SAneesh Kumar K.V }
832c797b6c6SAneesh Kumar K.V 
833c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev,
834c797b6c6SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
835c797b6c6SAneesh Kumar K.V {
836c797b6c6SAneesh Kumar K.V 	int ret;
837c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
838c797b6c6SAneesh Kumar K.V 	u32 fid_val, datasync;
839c797b6c6SAneesh Kumar K.V 
840c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync);
841c797b6c6SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
842c797b6c6SAneesh Kumar K.V 
843c797b6c6SAneesh Kumar K.V 	if (datasync)
844c797b6c6SAneesh Kumar K.V 		ret = fdatasync(fid->fd);
845c797b6c6SAneesh Kumar K.V 	else
846c797b6c6SAneesh Kumar K.V 		ret = fsync(fid->fd);
847c797b6c6SAneesh Kumar K.V 	if (ret < 0)
848c797b6c6SAneesh Kumar K.V 		goto err_out;
849c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
850c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
851c797b6c6SAneesh Kumar K.V 	return;
852c797b6c6SAneesh Kumar K.V err_out:
853c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
854c797b6c6SAneesh Kumar K.V 	return;
855c797b6c6SAneesh Kumar K.V }
856c797b6c6SAneesh Kumar K.V 
857c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev,
858c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
859c797b6c6SAneesh Kumar K.V {
860c797b6c6SAneesh Kumar K.V 	int ret;
861c797b6c6SAneesh Kumar K.V 	struct stat st;
862c797b6c6SAneesh Kumar K.V 	u32 fid_val, gid;
863c797b6c6SAneesh Kumar K.V 	struct p9_qid qid;
864c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid;
865c797b6c6SAneesh Kumar K.V 	char new_name[PATH_MAX];
866c797b6c6SAneesh Kumar K.V 	char *old_path, *name;
867c797b6c6SAneesh Kumar K.V 
868c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid);
869c797b6c6SAneesh Kumar K.V 
870c797b6c6SAneesh Kumar K.V 	dfid = &p9dev->fids[fid_val];
871c797b6c6SAneesh Kumar K.V 	sprintf(new_name, "%s/%s", dfid->abs_path, name);
872c797b6c6SAneesh Kumar K.V 	ret = symlink(old_path, new_name);
873c797b6c6SAneesh Kumar K.V 	if (ret < 0)
874c797b6c6SAneesh Kumar K.V 		goto err_out;
875c797b6c6SAneesh Kumar K.V 
876c797b6c6SAneesh Kumar K.V 	if (lstat(new_name, &st) < 0)
877c797b6c6SAneesh Kumar K.V 		goto err_out;
878c797b6c6SAneesh Kumar K.V 
879c797b6c6SAneesh Kumar K.V 	ret = lchown(new_name, dfid->uid, gid);
880c797b6c6SAneesh Kumar K.V 	if (ret < 0)
881c797b6c6SAneesh Kumar K.V 		goto err_out;
882c797b6c6SAneesh Kumar K.V 
883c797b6c6SAneesh Kumar K.V 	stat2qid(&st, &qid);
884c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "Q", &qid);
885c797b6c6SAneesh Kumar K.V 	free(name);
886c797b6c6SAneesh Kumar K.V 	free(old_path);
887c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
888c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
889c797b6c6SAneesh Kumar K.V 	return;
890c797b6c6SAneesh Kumar K.V err_out:
891c797b6c6SAneesh Kumar K.V 	free(name);
892c797b6c6SAneesh Kumar K.V 	free(old_path);
893c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
894c797b6c6SAneesh Kumar K.V 	return;
895c797b6c6SAneesh Kumar K.V }
896c797b6c6SAneesh Kumar K.V 
897c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev,
898c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
899c797b6c6SAneesh Kumar K.V {
900c797b6c6SAneesh Kumar K.V 	int ret;
901c797b6c6SAneesh Kumar K.V 	char *name;
902c797b6c6SAneesh Kumar K.V 	u32 fid_val, dfid_val;
903c797b6c6SAneesh Kumar K.V 	struct p9_fid *dfid, *fid;
904c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
905c797b6c6SAneesh Kumar K.V 
906c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name);
907c797b6c6SAneesh Kumar K.V 
908c797b6c6SAneesh Kumar K.V 	dfid = &p9dev->fids[dfid_val];
909c797b6c6SAneesh Kumar K.V 	fid =  &p9dev->fids[fid_val];
910c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", dfid->abs_path, name);
911c797b6c6SAneesh Kumar K.V 	ret = link(fid->abs_path, full_path);
912c797b6c6SAneesh Kumar K.V 	if (ret < 0)
913c797b6c6SAneesh Kumar K.V 		goto err_out;
914c797b6c6SAneesh Kumar K.V 	free(name);
915c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
916c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
917c797b6c6SAneesh Kumar K.V 	return;
918c797b6c6SAneesh Kumar K.V err_out:
919c797b6c6SAneesh Kumar K.V 	free(name);
920c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
921c797b6c6SAneesh Kumar K.V 	return;
922c797b6c6SAneesh Kumar K.V 
923c797b6c6SAneesh Kumar K.V }
924c797b6c6SAneesh Kumar K.V 
925c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev,
926c797b6c6SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
927c797b6c6SAneesh Kumar K.V {
928c797b6c6SAneesh Kumar K.V 	u8 ret;
929c797b6c6SAneesh Kumar K.V 	u32 fid_val;
930c797b6c6SAneesh Kumar K.V 	struct p9_flock flock;
931c797b6c6SAneesh Kumar K.V 
932c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type,
933c797b6c6SAneesh Kumar K.V 			    &flock.flags, &flock.start, &flock.length,
934c797b6c6SAneesh Kumar K.V 			    &flock.proc_id, &flock.client_id);
935c797b6c6SAneesh Kumar K.V 
936c797b6c6SAneesh Kumar K.V 	/* Just return success */
937c797b6c6SAneesh Kumar K.V 	ret = P9_LOCK_SUCCESS;
938c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "d", ret);
939c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
940c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
941c797b6c6SAneesh Kumar K.V 	free(flock.client_id);
942c797b6c6SAneesh Kumar K.V 	return;
943c797b6c6SAneesh Kumar K.V }
944c797b6c6SAneesh Kumar K.V 
945c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev,
946c797b6c6SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
947c797b6c6SAneesh Kumar K.V {
948c797b6c6SAneesh Kumar K.V 	u32 fid_val;
949c797b6c6SAneesh Kumar K.V 	struct p9_getlock glock;
950c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type,
951c797b6c6SAneesh Kumar K.V 			    &glock.start, &glock.length, &glock.proc_id,
952c797b6c6SAneesh Kumar K.V 			    &glock.client_id);
953c797b6c6SAneesh Kumar K.V 
954c797b6c6SAneesh Kumar K.V 	/* Just return success */
955c797b6c6SAneesh Kumar K.V 	glock.type = F_UNLCK;
956c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_writef(pdu, "bqqds", glock.type,
957c797b6c6SAneesh Kumar K.V 			     glock.start, glock.length, glock.proc_id,
958c797b6c6SAneesh Kumar K.V 			     glock.client_id);
959c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
960c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
961c797b6c6SAneesh Kumar K.V 	free(glock.client_id);
962c797b6c6SAneesh Kumar K.V 	return;
963c797b6c6SAneesh Kumar K.V }
964c797b6c6SAneesh Kumar K.V 
965c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor)
966c797b6c6SAneesh Kumar K.V {
967c797b6c6SAneesh Kumar K.V 	int size = strlen(ancestor);
968c797b6c6SAneesh Kumar K.V 	if (!strncmp(path, ancestor, size)) {
969c797b6c6SAneesh Kumar K.V 		/*
970c797b6c6SAneesh Kumar K.V 		 * Now check whether ancestor is a full name or
971c797b6c6SAneesh Kumar K.V 		 * or directory component and not just part
972c797b6c6SAneesh Kumar K.V 		 * of a name.
973c797b6c6SAneesh Kumar K.V 		 */
974c797b6c6SAneesh Kumar K.V 		if (path[size] == '\0' || path[size] == '/')
975c797b6c6SAneesh Kumar K.V 			return 1;
976c797b6c6SAneesh Kumar K.V 	}
977c797b6c6SAneesh Kumar K.V 	return 0;
978c797b6c6SAneesh Kumar K.V }
979c797b6c6SAneesh Kumar K.V 
980c797b6c6SAneesh Kumar K.V static void virtio_p9_fix_path(char *fid_path, char *old_name, char *new_name)
981c797b6c6SAneesh Kumar K.V {
982c797b6c6SAneesh Kumar K.V 	char tmp_name[PATH_MAX];
983c797b6c6SAneesh Kumar K.V 	size_t rp_sz = strlen(old_name);
984c797b6c6SAneesh Kumar K.V 
985c797b6c6SAneesh Kumar K.V 	if (rp_sz == strlen(fid_path)) {
986c797b6c6SAneesh Kumar K.V 		/* replace the full name */
987c797b6c6SAneesh Kumar K.V 		strcpy(fid_path, new_name);
988c797b6c6SAneesh Kumar K.V 		return;
989c797b6c6SAneesh Kumar K.V 	}
990c797b6c6SAneesh Kumar K.V 	/* save the trailing path details */
991c797b6c6SAneesh Kumar K.V 	strcpy(tmp_name, fid_path + rp_sz);
992c797b6c6SAneesh Kumar K.V 	sprintf(fid_path, "%s%s", new_name, tmp_name);
993c797b6c6SAneesh Kumar K.V 	return;
994c797b6c6SAneesh Kumar K.V }
995c797b6c6SAneesh Kumar K.V 
996c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev,
997c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
998c797b6c6SAneesh Kumar K.V {
999c797b6c6SAneesh Kumar K.V 	int i, ret;
1000c797b6c6SAneesh Kumar K.V 	char *old_name, *new_name;
1001c797b6c6SAneesh Kumar K.V 	u32 old_dfid_val, new_dfid_val;
1002c797b6c6SAneesh Kumar K.V 	struct p9_fid *old_dfid, *new_dfid;
1003c797b6c6SAneesh Kumar K.V 	char old_full_path[PATH_MAX], new_full_path[PATH_MAX];
1004c797b6c6SAneesh Kumar K.V 
1005c797b6c6SAneesh Kumar K.V 
1006c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name,
1007c797b6c6SAneesh Kumar K.V 			    &new_dfid_val, &new_name);
1008c797b6c6SAneesh Kumar K.V 
1009c797b6c6SAneesh Kumar K.V 	old_dfid = &p9dev->fids[old_dfid_val];
1010c797b6c6SAneesh Kumar K.V 	new_dfid = &p9dev->fids[new_dfid_val];
1011c797b6c6SAneesh Kumar K.V 
1012c797b6c6SAneesh Kumar K.V 	sprintf(old_full_path, "%s/%s", old_dfid->abs_path, old_name);
1013c797b6c6SAneesh Kumar K.V 	sprintf(new_full_path, "%s/%s", new_dfid->abs_path, new_name);
1014c797b6c6SAneesh Kumar K.V 	ret = rename(old_full_path, new_full_path);
1015c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1016c797b6c6SAneesh Kumar K.V 		goto err_out;
1017c797b6c6SAneesh Kumar K.V 	/*
1018c797b6c6SAneesh Kumar K.V 	 * Now fix path in other fids, if the renamed path is part of
1019c797b6c6SAneesh Kumar K.V 	 * that.
1020c797b6c6SAneesh Kumar K.V 	 */
1021*5529bcd7SAsias He 	for (i = 0; i < VIRTIO_9P_MAX_FID; i++) {
1022c797b6c6SAneesh Kumar K.V 		if (p9dev->fids[i].fid != P9_NOFID &&
1023c797b6c6SAneesh Kumar K.V 		    virtio_p9_ancestor(p9dev->fids[i].path, old_name)) {
1024c797b6c6SAneesh Kumar K.V 			virtio_p9_fix_path(p9dev->fids[i].path, old_name,
1025c797b6c6SAneesh Kumar K.V 					   new_name);
1026c797b6c6SAneesh Kumar K.V 		}
1027c797b6c6SAneesh Kumar K.V 	}
1028c797b6c6SAneesh Kumar K.V 	free(old_name);
1029c797b6c6SAneesh Kumar K.V 	free(new_name);
1030c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1031c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1032c797b6c6SAneesh Kumar K.V 	return;
1033c797b6c6SAneesh Kumar K.V err_out:
1034c797b6c6SAneesh Kumar K.V 	free(old_name);
1035c797b6c6SAneesh Kumar K.V 	free(new_name);
1036c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1037c797b6c6SAneesh Kumar K.V 	return;
1038c797b6c6SAneesh Kumar K.V }
1039c797b6c6SAneesh Kumar K.V 
1040c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev,
1041c797b6c6SAneesh Kumar K.V 			       struct p9_pdu *pdu, u32 *outlen)
1042c797b6c6SAneesh Kumar K.V {
1043c797b6c6SAneesh Kumar K.V 	int ret;
1044c797b6c6SAneesh Kumar K.V 	char *name;
1045c797b6c6SAneesh Kumar K.V 	u32 fid_val, flags;
1046c797b6c6SAneesh Kumar K.V 	struct p9_fid *fid;
1047c797b6c6SAneesh Kumar K.V 	char full_path[PATH_MAX];
1048c797b6c6SAneesh Kumar K.V 
1049c797b6c6SAneesh Kumar K.V 	virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags);
1050c797b6c6SAneesh Kumar K.V 	fid = &p9dev->fids[fid_val];
1051c797b6c6SAneesh Kumar K.V 
1052c797b6c6SAneesh Kumar K.V 	sprintf(full_path, "%s/%s", fid->abs_path, name);
1053c797b6c6SAneesh Kumar K.V 	ret = remove(full_path);
1054c797b6c6SAneesh Kumar K.V 	if (ret < 0)
1055c797b6c6SAneesh Kumar K.V 		goto err_out;
1056c797b6c6SAneesh Kumar K.V 	free(name);
1057c797b6c6SAneesh Kumar K.V 	*outlen = pdu->write_offset;
1058c797b6c6SAneesh Kumar K.V 	virtio_p9_set_reply_header(pdu, *outlen);
1059c797b6c6SAneesh Kumar K.V 	return;
1060c797b6c6SAneesh Kumar K.V err_out:
1061c797b6c6SAneesh Kumar K.V 	free(name);
1062c797b6c6SAneesh Kumar K.V 	virtio_p9_error_reply(p9dev, pdu, errno, outlen);
1063c797b6c6SAneesh Kumar K.V 	return;
1064c797b6c6SAneesh Kumar K.V }
1065c797b6c6SAneesh Kumar K.V 
1066c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev,
1067c797b6c6SAneesh Kumar K.V 				 struct p9_pdu *pdu, u32 *outlen)
1068c797b6c6SAneesh Kumar K.V {
1069c797b6c6SAneesh Kumar K.V 	return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen);
1070c797b6c6SAneesh Kumar K.V }
1071c797b6c6SAneesh Kumar K.V 
1072ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev,
1073af045e53SAneesh Kumar K.V 			struct p9_pdu *pdu, u32 *outlen);
1074b4422bf3SAneesh Kumar K.V 
1075c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */
1076c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74
1077c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76
1078c797b6c6SAneesh Kumar K.V 
1079c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = {
1080c797b6c6SAneesh Kumar K.V 	[P9_TREADDIR]     = virtio_p9_readdir,
1081c797b6c6SAneesh Kumar K.V 	[P9_TSTATFS]      = virtio_p9_statfs,
1082c797b6c6SAneesh Kumar K.V 	[P9_TGETATTR]     = virtio_p9_getattr,
1083c797b6c6SAneesh Kumar K.V 	[P9_TSETATTR]     = virtio_p9_setattr,
1084c797b6c6SAneesh Kumar K.V 	[P9_TXATTRWALK]   = virtio_p9_eopnotsupp,
1085c797b6c6SAneesh Kumar K.V 	[P9_TXATTRCREATE] = virtio_p9_eopnotsupp,
1086c797b6c6SAneesh Kumar K.V 	[P9_TMKNOD]       = virtio_p9_mknod,
1087c797b6c6SAneesh Kumar K.V 	[P9_TLOCK]        = virtio_p9_lock,
1088c797b6c6SAneesh Kumar K.V 	[P9_TGETLOCK]     = virtio_p9_getlock,
1089c797b6c6SAneesh Kumar K.V 	[P9_TRENAMEAT]    = virtio_p9_renameat,
1090c797b6c6SAneesh Kumar K.V 	[P9_TREADLINK]    = virtio_p9_readlink,
1091c797b6c6SAneesh Kumar K.V 	[P9_TUNLINKAT]    = virtio_p9_unlinkat,
1092c797b6c6SAneesh Kumar K.V 	[P9_TMKDIR]       = virtio_p9_mkdir,
1093b4422bf3SAneesh Kumar K.V 	[P9_TVERSION]     = virtio_p9_version,
1094c797b6c6SAneesh Kumar K.V 	[P9_TLOPEN]       = virtio_p9_open,
1095b4422bf3SAneesh Kumar K.V 	[P9_TATTACH]      = virtio_p9_attach,
1096b4422bf3SAneesh Kumar K.V 	[P9_TWALK]        = virtio_p9_walk,
1097c797b6c6SAneesh Kumar K.V 	[P9_TCLUNK]       = virtio_p9_clunk,
1098c797b6c6SAneesh Kumar K.V 	[P9_TFSYNC]       = virtio_p9_fsync,
1099b4422bf3SAneesh Kumar K.V 	[P9_TREAD]        = virtio_p9_read,
1100c797b6c6SAneesh Kumar K.V 	[P9_TFLUSH]       = virtio_p9_eopnotsupp,
1101c797b6c6SAneesh Kumar K.V 	[P9_TLINK]        = virtio_p9_link,
1102c797b6c6SAneesh Kumar K.V 	[P9_TSYMLINK]     = virtio_p9_symlink,
1103c797b6c6SAneesh Kumar K.V 	[P9_TLCREATE]     = virtio_p9_create,
1104b4422bf3SAneesh Kumar K.V 	[P9_TWRITE]       = virtio_p9_write,
11056fc5cd9bSSasha Levin 	[P9_TREMOVE]      = virtio_p9_remove,
1106f161f28bSSasha Levin 	[P9_TRENAME]      = virtio_p9_rename,
1107b4422bf3SAneesh Kumar K.V };
1108b4422bf3SAneesh Kumar K.V 
1109af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq)
1110af045e53SAneesh Kumar K.V {
1111af045e53SAneesh Kumar K.V 	struct p9_pdu *pdu = calloc(1, sizeof(*pdu));
1112af045e53SAneesh Kumar K.V 	if (!pdu)
1113af045e53SAneesh Kumar K.V 		return NULL;
1114af045e53SAneesh Kumar K.V 
1115bfc15268SAneesh Kumar K.V 	/* skip the pdu header p9_msg */
1116*5529bcd7SAsias He 	pdu->read_offset  = VIRTIO_9P_HDR_LEN;
1117*5529bcd7SAsias He 	pdu->write_offset = VIRTIO_9P_HDR_LEN;
1118af045e53SAneesh Kumar K.V 	pdu->queue_head  = virt_queue__get_inout_iov(kvm, vq, pdu->in_iov,
1119af045e53SAneesh Kumar K.V 						     pdu->out_iov,
1120af045e53SAneesh Kumar K.V 						     &pdu->in_iov_cnt,
1121af045e53SAneesh Kumar K.V 						     &pdu->out_iov_cnt);
1122af045e53SAneesh Kumar K.V 	return pdu;
1123af045e53SAneesh Kumar K.V }
1124af045e53SAneesh Kumar K.V 
1125af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu)
1126af045e53SAneesh Kumar K.V {
1127af045e53SAneesh Kumar K.V 	struct p9_msg *msg;
1128af045e53SAneesh Kumar K.V 	/*
1129af045e53SAneesh Kumar K.V 	 * we can peek directly into pdu for a u8
1130af045e53SAneesh Kumar K.V 	 * value. The host endianess won't be an issue
1131af045e53SAneesh Kumar K.V 	 */
1132af045e53SAneesh Kumar K.V 	msg = pdu->out_iov[0].iov_base;
1133af045e53SAneesh Kumar K.V 	return msg->cmd;
1134af045e53SAneesh Kumar K.V }
1135af045e53SAneesh Kumar K.V 
1136b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job)
11371c7850f9SSasha Levin {
1138af045e53SAneesh Kumar K.V 	u8 cmd;
1139b4422bf3SAneesh Kumar K.V 	u32 len = 0;
1140b4422bf3SAneesh Kumar K.V 	p9_handler *handler;
1141b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev;
1142af045e53SAneesh Kumar K.V 	struct virt_queue *vq;
1143af045e53SAneesh Kumar K.V 	struct p9_pdu *p9pdu;
11441c7850f9SSasha Levin 
1145b4422bf3SAneesh Kumar K.V 	vq = job->vq;
1146b4422bf3SAneesh Kumar K.V 	p9dev = job->p9dev;
11471c7850f9SSasha Levin 
1148af045e53SAneesh Kumar K.V 	p9pdu = virtio_p9_pdu_init(kvm, vq);
1149af045e53SAneesh Kumar K.V 	cmd = virtio_p9_get_cmd(p9pdu);
1150af045e53SAneesh Kumar K.V 
1151c797b6c6SAneesh Kumar K.V 	if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) ||
1152c797b6c6SAneesh Kumar K.V 	    !virtio_9p_dotl_handler[cmd])
115397b408afSAneesh Kumar K.V 		handler = virtio_p9_eopnotsupp;
1154dd78d9eaSAneesh Kumar K.V 	else
1155c797b6c6SAneesh Kumar K.V 		handler = virtio_9p_dotl_handler[cmd];
1156c797b6c6SAneesh Kumar K.V 
1157af045e53SAneesh Kumar K.V 	handler(p9dev, p9pdu, &len);
1158af045e53SAneesh Kumar K.V 	virt_queue__set_used_elem(vq, p9pdu->queue_head, len);
1159af045e53SAneesh Kumar K.V 	free(p9pdu);
11601c7850f9SSasha Levin 	return true;
11611c7850f9SSasha Levin }
11621c7850f9SSasha Levin 
11631c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param)
11641c7850f9SSasha Levin {
1165b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job = (struct p9_dev_job *)param;
1166b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev   = job->p9dev;
1167b4422bf3SAneesh Kumar K.V 	struct virt_queue *vq  = job->vq;
11681c7850f9SSasha Levin 
11691c7850f9SSasha Levin 	while (virt_queue__available(vq)) {
1170b4422bf3SAneesh Kumar K.V 		virtio_p9_do_io_request(kvm, job);
117102eca50cSAsias He 		p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs);
11721c7850f9SSasha Levin 	}
11731c7850f9SSasha Levin }
11741c7850f9SSasha Levin 
1175c7838fbdSSasha Levin static void set_config(struct kvm *kvm, void *dev, u8 data, u32 offset)
11761c7850f9SSasha Levin {
1177c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
11781c7850f9SSasha Levin 
1179c7838fbdSSasha Levin 	((u8 *)(p9dev->config))[offset] = data;
1180c7838fbdSSasha Levin }
11811c7850f9SSasha Levin 
1182c7838fbdSSasha Levin static u8 get_config(struct kvm *kvm, void *dev, u32 offset)
1183c7838fbdSSasha Levin {
1184c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1185c7838fbdSSasha Levin 
1186c7838fbdSSasha Levin 	return ((u8 *)(p9dev->config))[offset];
1187c7838fbdSSasha Levin }
1188c7838fbdSSasha Levin 
1189c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev)
1190c7838fbdSSasha Levin {
1191c7838fbdSSasha Levin 	return 1 << VIRTIO_9P_MOUNT_TAG;
1192c7838fbdSSasha Levin }
1193c7838fbdSSasha Levin 
1194c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features)
1195c7838fbdSSasha Levin {
1196c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1197c7838fbdSSasha Levin 
1198c7838fbdSSasha Levin 	p9dev->features = features;
1199c7838fbdSSasha Levin }
1200c7838fbdSSasha Levin 
1201c7838fbdSSasha Levin static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn)
1202c7838fbdSSasha Levin {
1203c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1204b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job;
1205b4422bf3SAneesh Kumar K.V 	struct virt_queue *queue;
1206c7838fbdSSasha Levin 	void *p;
12071c7850f9SSasha Levin 
1208312c62d1SSasha Levin 	compat__remove_message(compat_id);
1209e59662b3SSasha Levin 
1210c7838fbdSSasha Levin 	queue			= &p9dev->vqs[vq];
1211c7838fbdSSasha Levin 	queue->pfn		= pfn;
12121c7850f9SSasha Levin 	p			= guest_pfn_to_host(kvm, queue->pfn);
1213c7838fbdSSasha Levin 	job			= &p9dev->jobs[vq];
12141c7850f9SSasha Levin 
1215c7838fbdSSasha Levin 	vring_init(&queue->vring, VIRTQUEUE_NUM, p, VIRTIO_PCI_VRING_ALIGN);
12161c7850f9SSasha Levin 
1217b4422bf3SAneesh Kumar K.V 	*job			= (struct p9_dev_job) {
1218b4422bf3SAneesh Kumar K.V 		.vq			= queue,
1219b4422bf3SAneesh Kumar K.V 		.p9dev			= p9dev,
1220b4422bf3SAneesh Kumar K.V 	};
1221df0c7f57SSasha Levin 	thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job);
122260eb42d5SSasha Levin 
1223c7838fbdSSasha Levin 	return 0;
12241c7850f9SSasha Levin }
12251c7850f9SSasha Levin 
1226c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq)
1227c7838fbdSSasha Levin {
1228c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
12291c7850f9SSasha Levin 
1230c7838fbdSSasha Levin 	thread_pool__do_job(&p9dev->jobs[vq].job_id);
1231c7838fbdSSasha Levin 
1232c7838fbdSSasha Levin 	return 0;
1233c7838fbdSSasha Levin }
1234c7838fbdSSasha Levin 
1235c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq)
1236c7838fbdSSasha Levin {
1237c7838fbdSSasha Levin 	struct p9_dev *p9dev = dev;
1238c7838fbdSSasha Levin 
1239c7838fbdSSasha Levin 	return p9dev->vqs[vq].pfn;
1240c7838fbdSSasha Levin }
1241c7838fbdSSasha Levin 
1242c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq)
1243c7838fbdSSasha Levin {
1244c7838fbdSSasha Levin 	return VIRTQUEUE_NUM;
1245c7838fbdSSasha Levin }
1246c7838fbdSSasha Levin 
12471c47ce69SSasha Levin struct virtio_ops p9_dev_virtio_ops = (struct virtio_ops) {
1248c7838fbdSSasha Levin 	.set_config		= set_config,
1249c7838fbdSSasha Levin 	.get_config		= get_config,
1250c7838fbdSSasha Levin 	.get_host_features	= get_host_features,
1251c7838fbdSSasha Levin 	.set_guest_features	= set_guest_features,
1252c7838fbdSSasha Levin 	.init_vq		= init_vq,
1253c7838fbdSSasha Levin 	.notify_vq		= notify_vq,
1254c7838fbdSSasha Levin 	.get_pfn_vq		= get_pfn_vq,
1255c7838fbdSSasha Levin 	.get_size_vq		= get_size_vq,
1256c7838fbdSSasha Levin };
12571c47ce69SSasha Levin 
12581c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm)
12591c47ce69SSasha Levin {
12601c47ce69SSasha Levin 	struct p9_dev *p9dev;
12611c47ce69SSasha Levin 
12621c47ce69SSasha Levin 	list_for_each_entry(p9dev, &devs, list) {
126302eca50cSAsias He 		virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops,
1264*5529bcd7SAsias He 			    VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_9P, VIRTIO_ID_9P, PCI_CLASS_9P);
1265c7838fbdSSasha Levin 	}
1266c7838fbdSSasha Levin 
1267c7838fbdSSasha Levin 	return 0;
1268c7838fbdSSasha Levin }
1269c7838fbdSSasha Levin 
1270c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name)
1271c7838fbdSSasha Levin {
1272c7838fbdSSasha Levin 	struct p9_dev *p9dev;
127354f6802dSPekka Enberg 	u32 i, root_len;
127454f6802dSPekka Enberg 	int err = 0;
12751c7850f9SSasha Levin 
1276b4422bf3SAneesh Kumar K.V 	p9dev = calloc(1, sizeof(*p9dev));
1277b4422bf3SAneesh Kumar K.V 	if (!p9dev)
127854f6802dSPekka Enberg 		return -ENOMEM;
127954f6802dSPekka Enberg 
1280b4422bf3SAneesh Kumar K.V 	if (!tag_name)
1281*5529bcd7SAsias He 		tag_name = VIRTIO_9P_DEFAULT_TAG;
128254f6802dSPekka Enberg 
1283b4422bf3SAneesh Kumar K.V 	p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1);
128454f6802dSPekka Enberg 	if (p9dev->config == NULL) {
128554f6802dSPekka Enberg 		err = -ENOMEM;
1286b4422bf3SAneesh Kumar K.V 		goto free_p9dev;
128754f6802dSPekka Enberg 	}
12881c7850f9SSasha Levin 
1289b4422bf3SAneesh Kumar K.V 	strcpy(p9dev->root_dir, root);
12901c7850f9SSasha Levin 	root_len = strlen(root);
12911c7850f9SSasha Levin 	/*
12921c7850f9SSasha Levin 	 * We prefix the full path in all fids, This allows us to get the
12931c7850f9SSasha Levin 	 * absolute path of an fid without playing with strings.
12941c7850f9SSasha Levin 	 */
1295*5529bcd7SAsias He 	for (i = 0; i < VIRTIO_9P_MAX_FID; i++) {
1296b4422bf3SAneesh Kumar K.V 		strcpy(p9dev->fids[i].abs_path, root);
1297b4422bf3SAneesh Kumar K.V 		p9dev->fids[i].path = p9dev->fids[i].abs_path + root_len;
12981c7850f9SSasha Levin 	}
1299b4422bf3SAneesh Kumar K.V 	p9dev->config->tag_len = strlen(tag_name);
130054f6802dSPekka Enberg 	if (p9dev->config->tag_len > MAX_TAG_LEN) {
130154f6802dSPekka Enberg 		err = -EINVAL;
1302b4422bf3SAneesh Kumar K.V 		goto free_p9dev_config;
130354f6802dSPekka Enberg 	}
13041c7850f9SSasha Levin 
1305c7838fbdSSasha Levin 	memcpy(&p9dev->config->tag, tag_name, strlen(tag_name));
13061c7850f9SSasha Levin 
1307c7838fbdSSasha Levin 	list_add(&p9dev->list, &devs);
1308b4422bf3SAneesh Kumar K.V 
1309312c62d1SSasha Levin 	if (compat_id != -1)
1310312c62d1SSasha Levin 		compat_id = compat__add_message("virtio-9p device was not detected",
1311e59662b3SSasha Levin 						"While you have requested a virtio-9p device, "
1312fc835ab3SSasha Levin 						"the guest kernel did not initialize it.\n"
1313fc835ab3SSasha Levin 						"Please make sure that the guest kernel was "
1314fc835ab3SSasha Levin 						"compiled with CONFIG_NET_9P_VIRTIO=y enabled "
1315fc835ab3SSasha Levin 						"in its .config");
1316e59662b3SSasha Levin 
131754f6802dSPekka Enberg 	return err;
131854f6802dSPekka Enberg 
1319b4422bf3SAneesh Kumar K.V free_p9dev_config:
1320b4422bf3SAneesh Kumar K.V 	free(p9dev->config);
1321b4422bf3SAneesh Kumar K.V free_p9dev:
1322b4422bf3SAneesh Kumar K.V 	free(p9dev);
132354f6802dSPekka Enberg 	return err;
13241c7850f9SSasha Levin }
1325