xref: /kvmtool/virtio/9p.c (revision 6b163a872939ba2bda326f8eba57ec5fa77d4e77)
11c7850f9SSasha Levin #include "kvm/virtio-9p.h"
21c7850f9SSasha Levin #include "kvm/virtio-pci-dev.h"
31c7850f9SSasha Levin #include "kvm/virtio.h"
41c7850f9SSasha Levin #include "kvm/ioport.h"
51c7850f9SSasha Levin #include "kvm/mutex.h"
61c7850f9SSasha Levin #include "kvm/util.h"
71c7850f9SSasha Levin #include "kvm/kvm.h"
81c7850f9SSasha Levin #include "kvm/pci.h"
91c7850f9SSasha Levin #include "kvm/threadpool.h"
101c7850f9SSasha Levin #include "kvm/irq.h"
1160eb42d5SSasha Levin #include "kvm/ioeventfd.h"
121c7850f9SSasha Levin 
131c7850f9SSasha Levin #include <fcntl.h>
141c7850f9SSasha Levin #include <sys/types.h>
151c7850f9SSasha Levin #include <sys/stat.h>
161c7850f9SSasha Levin #include <pthread.h>
171c7850f9SSasha Levin #include <dirent.h>
181c7850f9SSasha Levin 
192daa28d4SAneesh Kumar K.V #include <linux/virtio_ring.h>
202daa28d4SAneesh Kumar K.V #include <linux/virtio_9p.h>
212daa28d4SAneesh Kumar K.V #include <net/9p/9p.h>
222daa28d4SAneesh Kumar K.V 
231c7850f9SSasha Levin #define NUM_VIRT_QUEUES		1
24612038c5SAneesh Kumar K.V #define VIRTQUEUE_NUM		128
25b4422bf3SAneesh Kumar K.V #define	VIRTIO_P9_DEFAULT_TAG	"kvm_9p"
261c7850f9SSasha Levin #define VIRTIO_P9_HDR_LEN	(sizeof(u32)+sizeof(u8)+sizeof(u16))
271c7850f9SSasha Levin #define VIRTIO_P9_MAX_FID	128
281c7850f9SSasha Levin #define VIRTIO_P9_VERSION	"9P2000"
29b4422bf3SAneesh Kumar K.V #define MAX_TAG_LEN		32
30b4422bf3SAneesh Kumar K.V 
311c7850f9SSasha Levin 
321c7850f9SSasha Levin struct p9_msg {
331c7850f9SSasha Levin 	u32			size;
341c7850f9SSasha Levin 	u8			cmd;
351c7850f9SSasha Levin 	u16			tag;
361c7850f9SSasha Levin 	u8			msg[0];
371c7850f9SSasha Levin } __attribute__((packed));
381c7850f9SSasha Levin 
391c7850f9SSasha Levin struct p9_fid {
401c7850f9SSasha Levin 	u32			fid;
411c7850f9SSasha Levin 	u8			is_dir;
421c7850f9SSasha Levin 	char			abs_path[PATH_MAX];
431c7850f9SSasha Levin 	char			*path;
441c7850f9SSasha Levin 	DIR			*dir;
451c7850f9SSasha Levin 	int			fd;
461c7850f9SSasha Levin };
471c7850f9SSasha Levin 
48b4422bf3SAneesh Kumar K.V struct p9_dev_job {
49b4422bf3SAneesh Kumar K.V 	struct virt_queue		*vq;
50b4422bf3SAneesh Kumar K.V 	struct p9_dev			*p9dev;
51b4422bf3SAneesh Kumar K.V 	void				*job_id;
521c7850f9SSasha Levin };
531c7850f9SSasha Levin 
541c7850f9SSasha Levin struct p9_dev {
551c7850f9SSasha Levin 	u8			status;
561c7850f9SSasha Levin 	u8			isr;
571c7850f9SSasha Levin 	u16			config_vector;
581c7850f9SSasha Levin 	u32			features;
591c7850f9SSasha Levin 	struct virtio_9p_config	*config;
60f884f920SSasha Levin 	u16			base_addr;
611c7850f9SSasha Levin 
621c7850f9SSasha Levin 	/* virtio queue */
631c7850f9SSasha Levin 	u16			queue_selector;
641c7850f9SSasha Levin 	struct virt_queue	vqs[NUM_VIRT_QUEUES];
65b4422bf3SAneesh Kumar K.V 	struct p9_dev_job	jobs[NUM_VIRT_QUEUES];
661c7850f9SSasha Levin 	struct p9_fid		fids[VIRTIO_P9_MAX_FID];
671c7850f9SSasha Levin 	char			root_dir[PATH_MAX];
68b4422bf3SAneesh Kumar K.V 	struct pci_device_header pci_hdr;
691c7850f9SSasha Levin };
701c7850f9SSasha Levin 
71af045e53SAneesh Kumar K.V struct p9_pdu {
72af045e53SAneesh Kumar K.V 	u32 queue_head;
73af045e53SAneesh Kumar K.V 	int offset;
74af045e53SAneesh Kumar K.V 	u16 out_iov_cnt;
75af045e53SAneesh Kumar K.V 	u16 in_iov_cnt;
76af045e53SAneesh Kumar K.V 	struct iovec in_iov[VIRTQUEUE_NUM];
77af045e53SAneesh Kumar K.V 	struct iovec out_iov[VIRTQUEUE_NUM];
78af045e53SAneesh Kumar K.V };
79af045e53SAneesh Kumar K.V 
801c7850f9SSasha Levin /* Warning: Immediately use value returned from this function */
81b4422bf3SAneesh Kumar K.V static const char *rel_to_abs(struct p9_dev *p9dev,
82b4422bf3SAneesh Kumar K.V 			      const char *path, char *abs_path)
831c7850f9SSasha Levin {
84b4422bf3SAneesh Kumar K.V 	sprintf(abs_path, "%s/%s", p9dev->root_dir, path);
851c7850f9SSasha Levin 
861c7850f9SSasha Levin 	return abs_path;
871c7850f9SSasha Levin }
881c7850f9SSasha Levin 
89b4422bf3SAneesh Kumar K.V static bool virtio_p9_dev_in(struct p9_dev *p9dev, void *data,
90b4422bf3SAneesh Kumar K.V 			     unsigned long offset,
91b4422bf3SAneesh Kumar K.V 			     int size, u32 count)
921c7850f9SSasha Levin {
93b4422bf3SAneesh Kumar K.V 	u8 *config_space = (u8 *) p9dev->config;
941c7850f9SSasha Levin 
951c7850f9SSasha Levin 	if (size != 1 || count != 1)
961c7850f9SSasha Levin 		return false;
971c7850f9SSasha Levin 
981c7850f9SSasha Levin 	ioport__write8(data, config_space[offset - VIRTIO_MSI_CONFIG_VECTOR]);
991c7850f9SSasha Levin 
1001c7850f9SSasha Levin 	return true;
1011c7850f9SSasha Levin }
1021c7850f9SSasha Levin 
103b4422bf3SAneesh Kumar K.V static bool virtio_p9_pci_io_in(struct ioport *ioport, struct kvm *kvm,
104b4422bf3SAneesh Kumar K.V 				u16 port, void *data, int size, u32 count)
1051c7850f9SSasha Levin {
1061c7850f9SSasha Levin 	bool ret = true;
107b4422bf3SAneesh Kumar K.V 	unsigned long offset;
108b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev = ioport->priv;
1091c7850f9SSasha Levin 
110b4422bf3SAneesh Kumar K.V 
111b4422bf3SAneesh Kumar K.V 	offset = port - p9dev->base_addr;
1121c7850f9SSasha Levin 
1131c7850f9SSasha Levin 	switch (offset) {
1141c7850f9SSasha Levin 	case VIRTIO_PCI_HOST_FEATURES:
115b4422bf3SAneesh Kumar K.V 		ioport__write32(data, p9dev->features);
1161c7850f9SSasha Levin 		ret = true;
1171c7850f9SSasha Levin 		break;
1181c7850f9SSasha Levin 	case VIRTIO_PCI_GUEST_FEATURES:
1191c7850f9SSasha Levin 	case VIRTIO_PCI_QUEUE_SEL:
1201c7850f9SSasha Levin 	case VIRTIO_PCI_QUEUE_NOTIFY:
1211c7850f9SSasha Levin 		ret = false;
1221c7850f9SSasha Levin 		break;
1231c7850f9SSasha Levin 	case VIRTIO_PCI_QUEUE_PFN:
124b4422bf3SAneesh Kumar K.V 		ioport__write32(data, p9dev->vqs[p9dev->queue_selector].pfn);
1251c7850f9SSasha Levin 		break;
1261c7850f9SSasha Levin 	case VIRTIO_PCI_QUEUE_NUM:
127612038c5SAneesh Kumar K.V 		ioport__write16(data, VIRTQUEUE_NUM);
1281c7850f9SSasha Levin 		break;
1291c7850f9SSasha Levin 	case VIRTIO_PCI_STATUS:
130b4422bf3SAneesh Kumar K.V 		ioport__write8(data, p9dev->status);
1311c7850f9SSasha Levin 		break;
1321c7850f9SSasha Levin 	case VIRTIO_PCI_ISR:
133b4422bf3SAneesh Kumar K.V 		ioport__write8(data, p9dev->isr);
134b4422bf3SAneesh Kumar K.V 		kvm__irq_line(kvm, p9dev->pci_hdr.irq_line, VIRTIO_IRQ_LOW);
135b4422bf3SAneesh Kumar K.V 		p9dev->isr = VIRTIO_IRQ_LOW;
1361c7850f9SSasha Levin 		break;
1371c7850f9SSasha Levin 	default:
138b4422bf3SAneesh Kumar K.V 		ret = virtio_p9_dev_in(p9dev, data, offset, size, count);
1391c7850f9SSasha Levin 		break;
1401c7850f9SSasha Levin 	};
1411c7850f9SSasha Levin 
1421c7850f9SSasha Levin 	return ret;
1431c7850f9SSasha Levin }
1441c7850f9SSasha Levin 
1451c7850f9SSasha Levin static int omode2uflags(u8 mode)
1461c7850f9SSasha Levin {
1471c7850f9SSasha Levin 	int ret = 0;
1481c7850f9SSasha Levin 
1491c7850f9SSasha Levin 	/* Basic open modes are same as uflags */
1501c7850f9SSasha Levin 	ret = mode & 3;
1511c7850f9SSasha Levin 
1521c7850f9SSasha Levin 	/* Everything else is different */
1531c7850f9SSasha Levin 	if (mode & P9_OTRUNC)
1541c7850f9SSasha Levin 		ret |= O_TRUNC;
1551c7850f9SSasha Levin 
1561c7850f9SSasha Levin 	if (mode & P9_OAPPEND)
1571c7850f9SSasha Levin 		ret |= O_APPEND;
1581c7850f9SSasha Levin 
1591c7850f9SSasha Levin 	if (mode & P9_OEXCL)
1601c7850f9SSasha Levin 		ret |= O_EXCL;
1611c7850f9SSasha Levin 
1621c7850f9SSasha Levin 	return ret;
1631c7850f9SSasha Levin }
1641c7850f9SSasha Levin 
1651c7850f9SSasha Levin static void st2qid(struct stat *st, struct p9_qid *qid)
1661c7850f9SSasha Levin {
1671c7850f9SSasha Levin 	*qid = (struct p9_qid) {
1681c7850f9SSasha Levin 		.path		= st->st_ino,
1691c7850f9SSasha Levin 		.version	= st->st_mtime,
1701c7850f9SSasha Levin 	};
1711c7850f9SSasha Levin 
1721c7850f9SSasha Levin 	if (S_ISDIR(st->st_mode))
1731c7850f9SSasha Levin 		qid->type	|= P9_QTDIR;
1741c7850f9SSasha Levin }
1751c7850f9SSasha Levin 
176b4422bf3SAneesh Kumar K.V static void close_fid(struct p9_dev *p9dev, u32 fid)
1771c7850f9SSasha Levin {
178b4422bf3SAneesh Kumar K.V 	if (p9dev->fids[fid].fd > 0) {
179b4422bf3SAneesh Kumar K.V 		close(p9dev->fids[fid].fd);
180b4422bf3SAneesh Kumar K.V 		p9dev->fids[fid].fd = -1;
1811c7850f9SSasha Levin 	}
182b4422bf3SAneesh Kumar K.V 	if (p9dev->fids[fid].dir) {
183b4422bf3SAneesh Kumar K.V 		closedir(p9dev->fids[fid].dir);
184b4422bf3SAneesh Kumar K.V 		p9dev->fids[fid].dir = NULL;
1851c7850f9SSasha Levin 	}
1861c7850f9SSasha Levin }
1871c7850f9SSasha Levin 
1881c7850f9SSasha Levin static void set_p9msg_hdr(struct p9_msg *msg, u32 size, u8 cmd, u16 tag)
1891c7850f9SSasha Levin {
1901c7850f9SSasha Levin 	*msg = (struct p9_msg) {
1911c7850f9SSasha Levin 		.size	= size,
1921c7850f9SSasha Levin 		.tag	= tag,
1931c7850f9SSasha Levin 		.cmd	= cmd,
1941c7850f9SSasha Levin 	};
1951c7850f9SSasha Levin }
1961c7850f9SSasha Levin 
197*6b163a87SAneesh Kumar K.V static u16 virtio_p9_update_iov_cnt(struct iovec iov[], u32 count, int iov_cnt)
198*6b163a87SAneesh Kumar K.V {
199*6b163a87SAneesh Kumar K.V 	int i;
200*6b163a87SAneesh Kumar K.V 	u32 total = 0;
201*6b163a87SAneesh Kumar K.V 	for (i = 0; (i < iov_cnt) && (total < count); i++) {
202*6b163a87SAneesh Kumar K.V 		if (total + iov[i].iov_len > count) {
203*6b163a87SAneesh Kumar K.V 			/* we don't need this iov fully */
204*6b163a87SAneesh Kumar K.V 			iov[i].iov_len -= ((total + iov[i].iov_len) - count);
205*6b163a87SAneesh Kumar K.V 			i++;
206*6b163a87SAneesh Kumar K.V 			break;
207*6b163a87SAneesh Kumar K.V 		}
208*6b163a87SAneesh Kumar K.V 		total += iov[i].iov_len;
209*6b163a87SAneesh Kumar K.V 	}
210*6b163a87SAneesh Kumar K.V 	return i;
211*6b163a87SAneesh Kumar K.V }
212*6b163a87SAneesh Kumar K.V 
213af045e53SAneesh Kumar K.V static bool virtio_p9_version(struct p9_dev *p9dev,
214af045e53SAneesh Kumar K.V 			      struct p9_pdu *pdu, u32 *outlen)
2151c7850f9SSasha Levin {
216af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
217af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
218af045e53SAneesh Kumar K.V 	struct p9_rversion *rversion = (struct p9_rversion *)inmsg->msg;
2191c7850f9SSasha Levin 
2201c7850f9SSasha Levin 	rversion->msize		= 4096;
2211c7850f9SSasha Levin 	rversion->version.len	= strlen(VIRTIO_P9_VERSION);
2221c7850f9SSasha Levin 	memcpy(&rversion->version.str, VIRTIO_P9_VERSION, rversion->version.len);
2231c7850f9SSasha Levin 
224b4422bf3SAneesh Kumar K.V 	*outlen = VIRTIO_P9_HDR_LEN +
225b4422bf3SAneesh Kumar K.V 		rversion->version.len + sizeof(u16) + sizeof(u32);
226af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RVERSION, outmsg->tag);
2271c7850f9SSasha Levin 
2281c7850f9SSasha Levin 	return true;
2291c7850f9SSasha Levin }
2301c7850f9SSasha Levin 
231af045e53SAneesh Kumar K.V static bool virtio_p9_clunk(struct p9_dev *p9dev,
232af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
2331c7850f9SSasha Levin {
234af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
235af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
236af045e53SAneesh Kumar K.V 	struct p9_tclunk *tclunk = (struct p9_tclunk *)outmsg->msg;
2371c7850f9SSasha Levin 
238b4422bf3SAneesh Kumar K.V 	close_fid(p9dev, tclunk->fid);
2391c7850f9SSasha Levin 
2401c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN;
241af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RCLUNK, outmsg->tag);
2421c7850f9SSasha Levin 
2431c7850f9SSasha Levin 	return true;
2441c7850f9SSasha Levin }
2451c7850f9SSasha Levin 
246af045e53SAneesh Kumar K.V static bool virtio_p9_open(struct p9_dev *p9dev,
247af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
2481c7850f9SSasha Levin {
2491c7850f9SSasha Levin 	struct stat st;
250af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
251af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
252af045e53SAneesh Kumar K.V 	struct p9_topen *topen	= (struct p9_topen *)outmsg->msg;
253af045e53SAneesh Kumar K.V 	struct p9_ropen *ropen	= (struct p9_ropen *)inmsg->msg;
254af045e53SAneesh Kumar K.V 	struct p9_fid *new_fid	= &p9dev->fids[topen->fid];
2551c7850f9SSasha Levin 
25630204a77SAneesh Kumar K.V 	if (lstat(new_fid->abs_path, &st) < 0)
2571c7850f9SSasha Levin 		return false;
2581c7850f9SSasha Levin 
2591c7850f9SSasha Levin 	st2qid(&st, &ropen->qid);
2601c7850f9SSasha Levin 	ropen->iounit = 0;
2611c7850f9SSasha Levin 
2621c7850f9SSasha Levin 	if (new_fid->is_dir)
2631c7850f9SSasha Levin 		new_fid->dir	= opendir(new_fid->abs_path);
2641c7850f9SSasha Levin 	else
2651c7850f9SSasha Levin 		new_fid->fd	= open(new_fid->abs_path, omode2uflags(topen->mode));
2661c7850f9SSasha Levin 
2671c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN + sizeof(*ropen);
268af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_ROPEN, outmsg->tag);
2691c7850f9SSasha Levin 
2701c7850f9SSasha Levin 	return true;
2711c7850f9SSasha Levin }
2721c7850f9SSasha Levin 
273af045e53SAneesh Kumar K.V static bool virtio_p9_create(struct p9_dev *p9dev,
274af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
2751c7850f9SSasha Levin {
2761c7850f9SSasha Levin 	u8 mode;
2771c7850f9SSasha Levin 	u32 perm;
278af045e53SAneesh Kumar K.V 	struct stat st;
279af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
280af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
281af045e53SAneesh Kumar K.V 	struct p9_tcreate *tcreate	= (struct p9_tcreate *)outmsg->msg;
282af045e53SAneesh Kumar K.V 	struct p9_rcreate *rcreate	= (struct p9_rcreate *)inmsg->msg;
283af045e53SAneesh Kumar K.V 	struct p9_fid *fid		= &p9dev->fids[tcreate->fid];
284af045e53SAneesh Kumar K.V 
2851c7850f9SSasha Levin 
2861c7850f9SSasha Levin 	rcreate->iounit = 0;
2871c7850f9SSasha Levin 
2881c7850f9SSasha Levin 	/* Get last byte of the variable length struct */
289af045e53SAneesh Kumar K.V 	mode = *((u8 *)outmsg + outmsg->size - 1);
290af045e53SAneesh Kumar K.V 	perm = *(u32 *)((u8 *)outmsg + outmsg->size - 5);
2911c7850f9SSasha Levin 
2921c7850f9SSasha Levin 	sprintf(fid->path, "%s/%.*s", fid->path, tcreate->name.len, (char *)&tcreate->name.str);
2931c7850f9SSasha Levin 
294b4422bf3SAneesh Kumar K.V 	close_fid(p9dev, tcreate->fid);
2951c7850f9SSasha Levin 
2961c7850f9SSasha Levin 	if (perm & P9_DMDIR) {
2971c7850f9SSasha Levin 		mkdir(fid->abs_path, perm & 0xFFFF);
2981c7850f9SSasha Levin 		fid->dir = opendir(fid->abs_path);
2991c7850f9SSasha Levin 		fid->is_dir = 1;
3001c7850f9SSasha Levin 	} else {
3011c7850f9SSasha Levin 		fid->fd = open(fid->abs_path, omode2uflags(mode) | O_CREAT, 0777);
3021c7850f9SSasha Levin 	}
3031c7850f9SSasha Levin 
30430204a77SAneesh Kumar K.V 	if (lstat(fid->abs_path, &st) < 0)
3051c7850f9SSasha Levin 		return false;
3061c7850f9SSasha Levin 
3071c7850f9SSasha Levin 	st2qid(&st, &rcreate->qid);
3081c7850f9SSasha Levin 
3091c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN + sizeof(*rcreate);
310af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RCREATE, outmsg->tag);
3111c7850f9SSasha Levin 
3121c7850f9SSasha Levin 	return true;
3131c7850f9SSasha Levin }
3141c7850f9SSasha Levin 
315af045e53SAneesh Kumar K.V static bool virtio_p9_walk(struct p9_dev *p9dev,
316af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
3171c7850f9SSasha Levin {
318af045e53SAneesh Kumar K.V 	u8 i;
319af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
320af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
321af045e53SAneesh Kumar K.V 	struct p9_twalk *twalk	= (struct p9_twalk *)outmsg->msg;
322af045e53SAneesh Kumar K.V 	struct p9_rwalk *rwalk	= (struct p9_rwalk *)inmsg->msg;
3231c7850f9SSasha Levin 	struct p9_str *str	= twalk->wnames;
324b4422bf3SAneesh Kumar K.V 	struct p9_fid *new_fid	= &p9dev->fids[twalk->newfid];
325af045e53SAneesh Kumar K.V 
3261c7850f9SSasha Levin 
3271c7850f9SSasha Levin 	rwalk->nwqid = 0;
3281c7850f9SSasha Levin 	if (twalk->nwname) {
329b4422bf3SAneesh Kumar K.V 		struct p9_fid *fid = &p9dev->fids[twalk->fid];
3301c7850f9SSasha Levin 
3311c7850f9SSasha Levin 		for (i = 0; i < twalk->nwname; i++) {
3321c7850f9SSasha Levin 			char tmp[PATH_MAX] = {0};
3331c7850f9SSasha Levin 			char full_path[PATH_MAX];
3341c7850f9SSasha Levin 			struct stat st;
3351c7850f9SSasha Levin 
3361c7850f9SSasha Levin 			/* Format the new path we're 'walk'ing into */
337af045e53SAneesh Kumar K.V 			sprintf(tmp, "%s/%.*s", fid->path,
338af045e53SAneesh Kumar K.V 				str->len, (char *)&str->str);
3391c7850f9SSasha Levin 
34030204a77SAneesh Kumar K.V 			if (lstat(rel_to_abs(p9dev, tmp, full_path), &st) < 0)
3411c7850f9SSasha Levin 				break;
3421c7850f9SSasha Levin 
3431c7850f9SSasha Levin 			st2qid(&st, &rwalk->wqids[i]);
3441c7850f9SSasha Levin 			new_fid->is_dir = S_ISDIR(st.st_mode);
3451c7850f9SSasha Levin 			strcpy(new_fid->path, tmp);
3461c7850f9SSasha Levin 			new_fid->fid = twalk->newfid;
3471c7850f9SSasha Levin 			rwalk->nwqid++;
3481c7850f9SSasha Levin 		}
3491c7850f9SSasha Levin 	} else {
350b4422bf3SAneesh Kumar K.V 		new_fid->is_dir = p9dev->fids[twalk->fid].is_dir;
351b4422bf3SAneesh Kumar K.V 		strcpy(new_fid->path, p9dev->fids[twalk->fid].path);
3521c7850f9SSasha Levin 		new_fid->fid	= twalk->newfid;
3531c7850f9SSasha Levin 	}
3541c7850f9SSasha Levin 
355af045e53SAneesh Kumar K.V 	*outlen = VIRTIO_P9_HDR_LEN + sizeof(u16) +
356af045e53SAneesh Kumar K.V 		sizeof(struct p9_qid)*rwalk->nwqid;
357af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RWALK, outmsg->tag);
3581c7850f9SSasha Levin 
3591c7850f9SSasha Levin 	return true;
3601c7850f9SSasha Levin }
3611c7850f9SSasha Levin 
362af045e53SAneesh Kumar K.V static bool virtio_p9_attach(struct p9_dev *p9dev,
363af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
3641c7850f9SSasha Levin {
365af045e53SAneesh Kumar K.V 	u32 i;
3661c7850f9SSasha Levin 	struct stat st;
3671c7850f9SSasha Levin 	struct p9_fid *fid;
368af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
369af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
370af045e53SAneesh Kumar K.V 	struct p9_rattach *rattach = (struct p9_rattach *)inmsg->msg;
371af045e53SAneesh Kumar K.V 	struct p9_tattach *tattach = (struct p9_tattach *)outmsg->msg;
3721c7850f9SSasha Levin 
3731c7850f9SSasha Levin 	/* Reset everything */
3741c7850f9SSasha Levin 	for (i = 0; i < VIRTIO_P9_MAX_FID; i++)
375b4422bf3SAneesh Kumar K.V 		p9dev->fids[i].fid = P9_NOFID;
3761c7850f9SSasha Levin 
37730204a77SAneesh Kumar K.V 	if (lstat(p9dev->root_dir, &st) < 0)
3781c7850f9SSasha Levin 		return false;
3791c7850f9SSasha Levin 
3801c7850f9SSasha Levin 	st2qid(&st, &rattach->qid);
3811c7850f9SSasha Levin 
382b4422bf3SAneesh Kumar K.V 	fid = &p9dev->fids[tattach->fid];
3831c7850f9SSasha Levin 	fid->fid = tattach->fid;
3841c7850f9SSasha Levin 	fid->is_dir = 1;
3851c7850f9SSasha Levin 	strcpy(fid->path, "/");
3861c7850f9SSasha Levin 
3871c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN + sizeof(*rattach);
388af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RATTACH, outmsg->tag);
3891c7850f9SSasha Levin 
3901c7850f9SSasha Levin 	return true;
3911c7850f9SSasha Levin }
3921c7850f9SSasha Levin 
393b4422bf3SAneesh Kumar K.V static u32 virtio_p9_fill_stat(struct p9_dev *p9dev, const char *name,
394b4422bf3SAneesh Kumar K.V 			       struct stat *st, struct p9_rstat *rstat)
3951c7850f9SSasha Levin {
3961c7850f9SSasha Levin 	struct p9_str *str;
3971c7850f9SSasha Levin 
3981c7850f9SSasha Levin 	rstat->stat.type = 0;
3991c7850f9SSasha Levin 	rstat->stat.dev = 0;
4001c7850f9SSasha Levin 	st2qid(st, &rstat->stat.qid);
4011c7850f9SSasha Levin 	rstat->stat.mode = st->st_mode;
4021c7850f9SSasha Levin 	rstat->stat.length = st->st_size;
4031c7850f9SSasha Levin 	if (S_ISDIR(st->st_mode)) {
4041c7850f9SSasha Levin 		rstat->stat.length = 0;
4051c7850f9SSasha Levin 		rstat->stat.mode |= P9_DMDIR;
4061c7850f9SSasha Levin 	}
4071c7850f9SSasha Levin 
4081c7850f9SSasha Levin 	rstat->stat.atime = st->st_atime;
4091c7850f9SSasha Levin 	rstat->stat.mtime = st->st_mtime;
4101c7850f9SSasha Levin 
4111c7850f9SSasha Levin 	str = (struct p9_str *)&rstat->stat.name;
4121c7850f9SSasha Levin 	str->len = strlen(name);
4131c7850f9SSasha Levin 	memcpy(&str->str, name, str->len);
4141c7850f9SSasha Levin 	str = (void *)str + str->len + sizeof(u16);
4151c7850f9SSasha Levin 
4161c7850f9SSasha Levin 	/* TODO: Pass usernames to the client */
4171c7850f9SSasha Levin 	str->len = 0;
4181c7850f9SSasha Levin 	str = (void *)str + sizeof(u16);
4191c7850f9SSasha Levin 	str->len = 0;
4201c7850f9SSasha Levin 	str = (void *)str + sizeof(u16);
4211c7850f9SSasha Levin 	str->len = 0;
4221c7850f9SSasha Levin 	str = (void *)str + sizeof(u16);
4231c7850f9SSasha Levin 
424b4422bf3SAneesh Kumar K.V 	/*
425b4422bf3SAneesh Kumar K.V 	 * We subtract a u16 here because rstat->size
426b4422bf3SAneesh Kumar K.V 	 * doesn't include rstat->size itself
427b4422bf3SAneesh Kumar K.V 	 */
4281c7850f9SSasha Levin 	rstat->stat.size = (void *)str - (void *)&rstat->stat - sizeof(u16);
4291c7850f9SSasha Levin 
4301c7850f9SSasha Levin 	return rstat->stat.size + sizeof(u16);
4311c7850f9SSasha Levin }
4321c7850f9SSasha Levin 
433af045e53SAneesh Kumar K.V static bool virtio_p9_read(struct p9_dev *p9dev,
434af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
4351c7850f9SSasha Levin {
436af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
437af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
438af045e53SAneesh Kumar K.V 	struct p9_tread *tread	= (struct p9_tread *)outmsg->msg;
439af045e53SAneesh Kumar K.V 	struct p9_rread *rread	= (struct p9_rread *)inmsg->msg;
440af045e53SAneesh Kumar K.V 	struct p9_rstat *rstat	= (struct p9_rstat *)pdu->in_iov[1].iov_base;
441b4422bf3SAneesh Kumar K.V 	struct p9_fid *fid	= &p9dev->fids[tread->fid];
4421c7850f9SSasha Levin 	struct stat st;
4431c7850f9SSasha Levin 
4441c7850f9SSasha Levin 	rread->count = 0;
4451c7850f9SSasha Levin 
4461c7850f9SSasha Levin 	if (fid->is_dir) {
4471c7850f9SSasha Levin 		/* If reading a dir, fill the buffer with p9_stat entries */
4481c7850f9SSasha Levin 		struct dirent *cur = readdir(fid->dir);
4491c7850f9SSasha Levin 		char full_path[PATH_MAX];
4501c7850f9SSasha Levin 
4511c7850f9SSasha Levin 		while (cur) {
4521c7850f9SSasha Levin 			u32 read;
4531c7850f9SSasha Levin 
45430204a77SAneesh Kumar K.V 			lstat(rel_to_abs(p9dev, cur->d_name, full_path), &st);
455b4422bf3SAneesh Kumar K.V 			read = virtio_p9_fill_stat(p9dev, cur->d_name,
456b4422bf3SAneesh Kumar K.V 						   &st, rstat);
4571c7850f9SSasha Levin 			rread->count += read;
4581c7850f9SSasha Levin 			rstat = (void *)rstat + read;
4591c7850f9SSasha Levin 			cur = readdir(fid->dir);
4601c7850f9SSasha Levin 		}
4611c7850f9SSasha Levin 	} else {
462af045e53SAneesh Kumar K.V 		pdu->in_iov[0].iov_base += VIRTIO_P9_HDR_LEN + sizeof(u32);
463af045e53SAneesh Kumar K.V 		pdu->in_iov[0].iov_len -= VIRTIO_P9_HDR_LEN + sizeof(u32);
464*6b163a87SAneesh Kumar K.V 		pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov,
465*6b163a87SAneesh Kumar K.V 							    tread->count,
466*6b163a87SAneesh Kumar K.V 							    pdu->in_iov_cnt);
467af045e53SAneesh Kumar K.V 		rread->count = preadv(fid->fd, pdu->in_iov,
468af045e53SAneesh Kumar K.V 				      pdu->in_iov_cnt, tread->offset);
4691c7850f9SSasha Levin 		if (rread->count > tread->count)
4701c7850f9SSasha Levin 			rread->count = tread->count;
4711c7850f9SSasha Levin 	}
4721c7850f9SSasha Levin 
4731c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN + sizeof(u32) + rread->count;
474af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RREAD, outmsg->tag);
4751c7850f9SSasha Levin 
4761c7850f9SSasha Levin 	return true;
4771c7850f9SSasha Levin }
4781c7850f9SSasha Levin 
479af045e53SAneesh Kumar K.V static bool virtio_p9_stat(struct p9_dev *p9dev,
480af045e53SAneesh Kumar K.V 			   struct p9_pdu *pdu, u32 *outlen)
4811c7850f9SSasha Levin {
4821c7850f9SSasha Levin 	u32 ret;
483af045e53SAneesh Kumar K.V 	struct stat st;
484af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
485af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
486af045e53SAneesh Kumar K.V 	struct p9_tstat *tstat = (struct p9_tstat *)outmsg->msg;
487af045e53SAneesh Kumar K.V 	struct p9_rstat *rstat = (struct p9_rstat *)(inmsg->msg + sizeof(u16));
488af045e53SAneesh Kumar K.V 	struct p9_fid *fid = &p9dev->fids[tstat->fid];
4891c7850f9SSasha Levin 
49030204a77SAneesh Kumar K.V 	if (lstat(fid->abs_path, &st) < 0)
4911c7850f9SSasha Levin 		return false;
4921c7850f9SSasha Levin 
493b4422bf3SAneesh Kumar K.V 	ret = virtio_p9_fill_stat(p9dev, fid->path, &st, rstat);
4941c7850f9SSasha Levin 
495c81fc2c7SAneesh Kumar K.V 	*outlen = VIRTIO_P9_HDR_LEN + ret + sizeof(u16);
496af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RSTAT, outmsg->tag);
4971c7850f9SSasha Levin 	return true;
4981c7850f9SSasha Levin }
4991c7850f9SSasha Levin 
500af045e53SAneesh Kumar K.V static bool virtio_p9_wstat(struct p9_dev *p9dev,
501af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
5021c7850f9SSasha Levin {
503aec426f0SCyrill Gorcunov 	int res = 0;
504af045e53SAneesh Kumar K.V 	struct p9_str *str;
505af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
506af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
507af045e53SAneesh Kumar K.V 	struct p9_twstat *twstat = (struct p9_twstat *)outmsg->msg;
508af045e53SAneesh Kumar K.V 	struct p9_fid *fid = &p9dev->fids[twstat->fid];
509af045e53SAneesh Kumar K.V 
5101c7850f9SSasha Levin 
5111c7850f9SSasha Levin 	if (twstat->stat.length != -1UL)
5121c7850f9SSasha Levin 		res = ftruncate(fid->fd, twstat->stat.length);
5131c7850f9SSasha Levin 
5141c7850f9SSasha Levin 	if (twstat->stat.mode != -1U)
5151c7850f9SSasha Levin 		chmod(fid->abs_path, twstat->stat.mode & 0xFFFF);
5161c7850f9SSasha Levin 
5171c7850f9SSasha Levin 	str = (void *)&twstat->stat.name + sizeof(u16);
5181c7850f9SSasha Levin 	if (str->len > 0) {
5191c7850f9SSasha Levin 		char new_name[PATH_MAX] = {0};
5201c7850f9SSasha Levin 		char full_path[PATH_MAX];
5211c7850f9SSasha Levin 		char *last_dir = strrchr(fid->path, '/');
5221c7850f9SSasha Levin 
5231c7850f9SSasha Levin 		/* We need to get the full file name out of twstat->name */
5241c7850f9SSasha Levin 		if (last_dir)
5251c7850f9SSasha Levin 			strncpy(new_name, fid->path, last_dir - fid->path + 1);
5261c7850f9SSasha Levin 
5271c7850f9SSasha Levin 		memcpy(new_name + strlen(new_name), &str->str, str->len);
5281c7850f9SSasha Levin 
5291c7850f9SSasha Levin 		/* fid is reused for the new file */
530b4422bf3SAneesh Kumar K.V 		rename(fid->abs_path, rel_to_abs(p9dev, new_name, full_path));
5311c7850f9SSasha Levin 		sprintf(fid->path, "%s", new_name);
5321c7850f9SSasha Levin 	}
5331c7850f9SSasha Levin 
5341c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN;
535af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RWSTAT, outmsg->tag);
536aec426f0SCyrill Gorcunov 
537aec426f0SCyrill Gorcunov 	return res == 0;
5381c7850f9SSasha Levin }
5391c7850f9SSasha Levin 
540af045e53SAneesh Kumar K.V static bool virtio_p9_remove(struct p9_dev *p9dev,
541af045e53SAneesh Kumar K.V 			     struct p9_pdu *pdu, u32 *outlen)
5421c7850f9SSasha Levin {
543af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
544af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
545af045e53SAneesh Kumar K.V 	struct p9_tremove *tremove = (struct p9_tremove *)outmsg->msg;
546b4422bf3SAneesh Kumar K.V 	struct p9_fid *fid = &p9dev->fids[tremove->fid];
5471c7850f9SSasha Levin 
548b4422bf3SAneesh Kumar K.V 	close_fid(p9dev, tremove->fid);
5491c7850f9SSasha Levin 	if (fid->is_dir)
5501c7850f9SSasha Levin 		rmdir(fid->abs_path);
5511c7850f9SSasha Levin 	else
5521c7850f9SSasha Levin 		unlink(fid->abs_path);
5531c7850f9SSasha Levin 
5541c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN;
555af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RREMOVE, outmsg->tag);
5561c7850f9SSasha Levin 	return true;
5571c7850f9SSasha Levin }
5581c7850f9SSasha Levin 
559af045e53SAneesh Kumar K.V static bool virtio_p9_write(struct p9_dev *p9dev,
560af045e53SAneesh Kumar K.V 			    struct p9_pdu *pdu, u32 *outlen)
5611c7850f9SSasha Levin {
562af045e53SAneesh Kumar K.V 	struct p9_msg *inmsg  = pdu->in_iov[0].iov_base;
563af045e53SAneesh Kumar K.V 	struct p9_msg *outmsg = pdu->out_iov[0].iov_base;
564af045e53SAneesh Kumar K.V 	struct p9_twrite *twrite = (struct p9_twrite *)outmsg->msg;
565af045e53SAneesh Kumar K.V 	struct p9_rwrite *rwrite = (struct p9_rwrite *)inmsg->msg;
566b4422bf3SAneesh Kumar K.V 	struct p9_fid *fid = &p9dev->fids[twrite->fid];
5671c7850f9SSasha Levin 
568af045e53SAneesh Kumar K.V 
569af045e53SAneesh Kumar K.V 	pdu->out_iov[0].iov_base += (sizeof(*outmsg) + sizeof(*twrite));
570af045e53SAneesh Kumar K.V 	pdu->out_iov[0].iov_len -= (sizeof(*outmsg) + sizeof(*twrite));
571*6b163a87SAneesh Kumar K.V 	pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, twrite->count,
572*6b163a87SAneesh Kumar K.V 						    pdu->out_iov_cnt);
573af045e53SAneesh Kumar K.V 	rwrite->count = pwritev(fid->fd, pdu->out_iov,
574af045e53SAneesh Kumar K.V 				pdu->out_iov_cnt, twrite->offset);
5751c7850f9SSasha Levin 	*outlen = VIRTIO_P9_HDR_LEN + sizeof(u32);
576af045e53SAneesh Kumar K.V 	set_p9msg_hdr(inmsg, *outlen, P9_RWRITE, outmsg->tag);
5771c7850f9SSasha Levin 
5781c7850f9SSasha Levin 	return true;
5791c7850f9SSasha Levin }
5801c7850f9SSasha Levin 
581af045e53SAneesh Kumar K.V typedef bool p9_handler(struct p9_dev *p9dev,
582af045e53SAneesh Kumar K.V 			struct p9_pdu *pdu, u32 *outlen);
583b4422bf3SAneesh Kumar K.V 
584b4422bf3SAneesh Kumar K.V static p9_handler *virtio_9p_handler [] = {
585b4422bf3SAneesh Kumar K.V 	[P9_TVERSION] = virtio_p9_version,
586b4422bf3SAneesh Kumar K.V 	[P9_TATTACH]  = virtio_p9_attach,
587b4422bf3SAneesh Kumar K.V 	[P9_TSTAT]    = virtio_p9_stat,
588b4422bf3SAneesh Kumar K.V 	[P9_TCLUNK]   =	virtio_p9_clunk,
589b4422bf3SAneesh Kumar K.V 	[P9_TWALK]    =	virtio_p9_walk,
590b4422bf3SAneesh Kumar K.V 	[P9_TOPEN]    =	virtio_p9_open,
591b4422bf3SAneesh Kumar K.V 	[P9_TREAD]    = virtio_p9_read,
592b4422bf3SAneesh Kumar K.V 	[P9_TCREATE]  =	virtio_p9_create,
593b4422bf3SAneesh Kumar K.V 	[P9_TWSTAT]   =	virtio_p9_wstat,
594b4422bf3SAneesh Kumar K.V 	[P9_TREMOVE]  =	virtio_p9_remove,
595b4422bf3SAneesh Kumar K.V 	[P9_TWRITE]   =	virtio_p9_write,
596b4422bf3SAneesh Kumar K.V };
597b4422bf3SAneesh Kumar K.V 
598af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq)
599af045e53SAneesh Kumar K.V {
600af045e53SAneesh Kumar K.V 	struct p9_pdu *pdu = calloc(1, sizeof(*pdu));
601af045e53SAneesh Kumar K.V 	if (!pdu)
602af045e53SAneesh Kumar K.V 		return NULL;
603af045e53SAneesh Kumar K.V 
604af045e53SAneesh Kumar K.V 	pdu->queue_head  = virt_queue__get_inout_iov(kvm, vq, pdu->in_iov,
605af045e53SAneesh Kumar K.V 						     pdu->out_iov,
606af045e53SAneesh Kumar K.V 						     &pdu->in_iov_cnt,
607af045e53SAneesh Kumar K.V 						     &pdu->out_iov_cnt);
608af045e53SAneesh Kumar K.V 	return pdu;
609af045e53SAneesh Kumar K.V }
610af045e53SAneesh Kumar K.V 
611af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu)
612af045e53SAneesh Kumar K.V {
613af045e53SAneesh Kumar K.V 	struct p9_msg *msg;
614af045e53SAneesh Kumar K.V 	/*
615af045e53SAneesh Kumar K.V 	 * we can peek directly into pdu for a u8
616af045e53SAneesh Kumar K.V 	 * value. The host endianess won't be an issue
617af045e53SAneesh Kumar K.V 	 */
618af045e53SAneesh Kumar K.V 	msg = pdu->out_iov[0].iov_base;
619af045e53SAneesh Kumar K.V 	return msg->cmd;
620af045e53SAneesh Kumar K.V }
621af045e53SAneesh Kumar K.V 
622b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job)
6231c7850f9SSasha Levin {
624af045e53SAneesh Kumar K.V 	u8 cmd;
625b4422bf3SAneesh Kumar K.V 	u32 len = 0;
626b4422bf3SAneesh Kumar K.V 	p9_handler *handler;
627b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev;
628af045e53SAneesh Kumar K.V 	struct virt_queue *vq;
629af045e53SAneesh Kumar K.V 	struct p9_pdu *p9pdu;
6301c7850f9SSasha Levin 
631b4422bf3SAneesh Kumar K.V 	vq = job->vq;
632b4422bf3SAneesh Kumar K.V 	p9dev = job->p9dev;
6331c7850f9SSasha Levin 
634af045e53SAneesh Kumar K.V 	p9pdu = virtio_p9_pdu_init(kvm, vq);
635af045e53SAneesh Kumar K.V 	cmd = virtio_p9_get_cmd(p9pdu);
636af045e53SAneesh Kumar K.V 
637af045e53SAneesh Kumar K.V 	if (cmd >= ARRAY_SIZE(virtio_9p_handler) ||
638af045e53SAneesh Kumar K.V 	    !virtio_9p_handler[cmd]) {
639af045e53SAneesh Kumar K.V 		printf("Unsupported P9 message type: %u\n", cmd);
6401c7850f9SSasha Levin 
641b4422bf3SAneesh Kumar K.V 	} else {
642af045e53SAneesh Kumar K.V 		handler = virtio_9p_handler[cmd];
643af045e53SAneesh Kumar K.V 		handler(p9dev, p9pdu, &len);
644b4422bf3SAneesh Kumar K.V 	}
645af045e53SAneesh Kumar K.V 	virt_queue__set_used_elem(vq, p9pdu->queue_head, len);
646af045e53SAneesh Kumar K.V 	free(p9pdu);
6471c7850f9SSasha Levin 	return true;
6481c7850f9SSasha Levin }
6491c7850f9SSasha Levin 
6501c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param)
6511c7850f9SSasha Levin {
652b4422bf3SAneesh Kumar K.V 	struct p9_dev_job *job = (struct p9_dev_job *)param;
653b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev   = job->p9dev;
654b4422bf3SAneesh Kumar K.V 	struct virt_queue *vq  = job->vq;
6551c7850f9SSasha Levin 
6561c7850f9SSasha Levin 	while (virt_queue__available(vq)) {
657b4422bf3SAneesh Kumar K.V 		virtio_p9_do_io_request(kvm, job);
658b4422bf3SAneesh Kumar K.V 		virt_queue__trigger_irq(vq, p9dev->pci_hdr.irq_line,
659b4422bf3SAneesh Kumar K.V 					&p9dev->isr, kvm);
6601c7850f9SSasha Levin 	}
6611c7850f9SSasha Levin }
6621c7850f9SSasha Levin 
66360eb42d5SSasha Levin static void ioevent_callback(struct kvm *kvm, void *param)
66460eb42d5SSasha Levin {
66560eb42d5SSasha Levin 	struct p9_dev_job *job = param;
66660eb42d5SSasha Levin 
66760eb42d5SSasha Levin 	thread_pool__do_job(job->job_id);
66860eb42d5SSasha Levin }
66960eb42d5SSasha Levin 
670b4422bf3SAneesh Kumar K.V static bool virtio_p9_pci_io_out(struct ioport *ioport, struct kvm *kvm,
671b4422bf3SAneesh Kumar K.V 				 u16 port, void *data, int size, u32 count)
6721c7850f9SSasha Levin {
6731c7850f9SSasha Levin 	unsigned long offset;
6741c7850f9SSasha Levin 	bool ret = true;
675b4422bf3SAneesh Kumar K.V 	struct p9_dev  *p9dev;
67660eb42d5SSasha Levin 	struct ioevent ioevent;
6771c7850f9SSasha Levin 
678b4422bf3SAneesh Kumar K.V 	p9dev = ioport->priv;
679b4422bf3SAneesh Kumar K.V 	offset = port - p9dev->base_addr;
6801c7850f9SSasha Levin 
6811c7850f9SSasha Levin 	switch (offset) {
6821c7850f9SSasha Levin 	case VIRTIO_MSI_QUEUE_VECTOR:
6831c7850f9SSasha Levin 	case VIRTIO_PCI_GUEST_FEATURES:
6841c7850f9SSasha Levin 		break;
6851c7850f9SSasha Levin 	case VIRTIO_PCI_QUEUE_PFN: {
6861c7850f9SSasha Levin 		void *p;
687b4422bf3SAneesh Kumar K.V 		struct p9_dev_job *job;
688b4422bf3SAneesh Kumar K.V 		struct virt_queue *queue;
6891c7850f9SSasha Levin 
690b4422bf3SAneesh Kumar K.V 		job			= &p9dev->jobs[p9dev->queue_selector];
691b4422bf3SAneesh Kumar K.V 		queue			= &p9dev->vqs[p9dev->queue_selector];
6921c7850f9SSasha Levin 		queue->pfn		= ioport__read32(data);
6931c7850f9SSasha Levin 		p			= guest_pfn_to_host(kvm, queue->pfn);
6941c7850f9SSasha Levin 
695612038c5SAneesh Kumar K.V 		vring_init(&queue->vring, VIRTQUEUE_NUM, p,
696b4422bf3SAneesh Kumar K.V 			   VIRTIO_PCI_VRING_ALIGN);
6971c7850f9SSasha Levin 
698b4422bf3SAneesh Kumar K.V 		*job			= (struct p9_dev_job) {
699b4422bf3SAneesh Kumar K.V 			.vq			= queue,
700b4422bf3SAneesh Kumar K.V 			.p9dev			= p9dev,
701b4422bf3SAneesh Kumar K.V 		};
702b4422bf3SAneesh Kumar K.V 		job->job_id = thread_pool__add_job(kvm, virtio_p9_do_io, job);
70360eb42d5SSasha Levin 
70460eb42d5SSasha Levin 		ioevent = (struct ioevent) {
70560eb42d5SSasha Levin 			.io_addr		= p9dev->base_addr + VIRTIO_PCI_QUEUE_NOTIFY,
70660eb42d5SSasha Levin 			.io_len			= sizeof(u16),
70760eb42d5SSasha Levin 			.fn			= ioevent_callback,
70860eb42d5SSasha Levin 			.datamatch		= p9dev->queue_selector,
70960eb42d5SSasha Levin 			.fn_ptr			= &p9dev->jobs[p9dev->queue_selector],
71060eb42d5SSasha Levin 			.fn_kvm			= kvm,
71160eb42d5SSasha Levin 			.fd			= eventfd(0, 0),
71260eb42d5SSasha Levin 		};
71360eb42d5SSasha Levin 
71460eb42d5SSasha Levin 		ioeventfd__add_event(&ioevent);
71560eb42d5SSasha Levin 
7161c7850f9SSasha Levin 		break;
7171c7850f9SSasha Levin 	}
7181c7850f9SSasha Levin 	case VIRTIO_PCI_QUEUE_SEL:
719b4422bf3SAneesh Kumar K.V 		p9dev->queue_selector	= ioport__read16(data);
7201c7850f9SSasha Levin 		break;
7211c7850f9SSasha Levin 	case VIRTIO_PCI_QUEUE_NOTIFY: {
7221c7850f9SSasha Levin 		u16 queue_index;
72360eb42d5SSasha Levin 
7241c7850f9SSasha Levin 		queue_index		= ioport__read16(data);
725b4422bf3SAneesh Kumar K.V 		thread_pool__do_job(p9dev->jobs[queue_index].job_id);
7261c7850f9SSasha Levin 		break;
7271c7850f9SSasha Levin 	}
7281c7850f9SSasha Levin 	case VIRTIO_PCI_STATUS:
729b4422bf3SAneesh Kumar K.V 		p9dev->status		= ioport__read8(data);
7301c7850f9SSasha Levin 		break;
7311c7850f9SSasha Levin 	case VIRTIO_MSI_CONFIG_VECTOR:
732b4422bf3SAneesh Kumar K.V 		p9dev->config_vector	= VIRTIO_MSI_NO_VECTOR;
7331c7850f9SSasha Levin 		break;
7341c7850f9SSasha Levin 	default:
7351c7850f9SSasha Levin 		ret			= false;
7361c7850f9SSasha Levin 		break;
7371c7850f9SSasha Levin 	};
7381c7850f9SSasha Levin 
7391c7850f9SSasha Levin 	return ret;
7401c7850f9SSasha Levin }
7411c7850f9SSasha Levin 
7421c7850f9SSasha Levin static struct ioport_operations virtio_p9_io_ops = {
7431c7850f9SSasha Levin 	.io_in				= virtio_p9_pci_io_in,
7441c7850f9SSasha Levin 	.io_out				= virtio_p9_pci_io_out,
7451c7850f9SSasha Levin };
7461c7850f9SSasha Levin 
747b4422bf3SAneesh Kumar K.V void virtio_9p__init(struct kvm *kvm, const char *root, const char *tag_name)
7481c7850f9SSasha Levin {
7491c7850f9SSasha Levin 	u8 pin, line, dev;
7501c7850f9SSasha Levin 	u32 i, root_len;
751f884f920SSasha Levin 	u16 p9_base_addr;
752b4422bf3SAneesh Kumar K.V 	struct p9_dev *p9dev;
7531c7850f9SSasha Levin 
754b4422bf3SAneesh Kumar K.V 	p9dev = calloc(1, sizeof(*p9dev));
755b4422bf3SAneesh Kumar K.V 	if (!p9dev)
7561c7850f9SSasha Levin 		return;
757b4422bf3SAneesh Kumar K.V 	if (!tag_name)
758b4422bf3SAneesh Kumar K.V 		tag_name = VIRTIO_P9_DEFAULT_TAG;
759b4422bf3SAneesh Kumar K.V 	p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1);
760b4422bf3SAneesh Kumar K.V 	if (p9dev->config == NULL)
761b4422bf3SAneesh Kumar K.V 		goto free_p9dev;
7621c7850f9SSasha Levin 
763b4422bf3SAneesh Kumar K.V 	strcpy(p9dev->root_dir, root);
7641c7850f9SSasha Levin 	root_len = strlen(root);
7651c7850f9SSasha Levin 	/*
7661c7850f9SSasha Levin 	 * We prefix the full path in all fids, This allows us to get the
7671c7850f9SSasha Levin 	 * absolute path of an fid without playing with strings.
7681c7850f9SSasha Levin 	 */
7691c7850f9SSasha Levin 	for (i = 0; i < VIRTIO_P9_MAX_FID; i++) {
770b4422bf3SAneesh Kumar K.V 		strcpy(p9dev->fids[i].abs_path, root);
771b4422bf3SAneesh Kumar K.V 		p9dev->fids[i].path = p9dev->fids[i].abs_path + root_len;
7721c7850f9SSasha Levin 	}
773b4422bf3SAneesh Kumar K.V 	p9dev->config->tag_len = strlen(tag_name);
774b4422bf3SAneesh Kumar K.V 	if (p9dev->config->tag_len > MAX_TAG_LEN)
775b4422bf3SAneesh Kumar K.V 		goto free_p9dev_config;
7761c7850f9SSasha Levin 
777b4422bf3SAneesh Kumar K.V 	memcpy(p9dev->config->tag, tag_name, strlen(tag_name));
778b4422bf3SAneesh Kumar K.V 	p9dev->features |= 1 << VIRTIO_9P_MOUNT_TAG;
7791c7850f9SSasha Levin 
7801c7850f9SSasha Levin 	if (irq__register_device(VIRTIO_ID_9P, &dev, &pin, &line) < 0)
781b4422bf3SAneesh Kumar K.V 		goto free_p9dev_config;
7821c7850f9SSasha Levin 
783b4422bf3SAneesh Kumar K.V 	p9_base_addr			= ioport__register(IOPORT_EMPTY,
784b4422bf3SAneesh Kumar K.V 							   &virtio_p9_io_ops,
785b4422bf3SAneesh Kumar K.V 							   IOPORT_SIZE, p9dev);
786b4422bf3SAneesh Kumar K.V 	p9dev->base_addr		    = p9_base_addr;
787b4422bf3SAneesh Kumar K.V 	p9dev->pci_hdr = (struct pci_device_header) {
788b4422bf3SAneesh Kumar K.V 		.vendor_id		= PCI_VENDOR_ID_REDHAT_QUMRANET,
789b4422bf3SAneesh Kumar K.V 		.device_id		= PCI_DEVICE_ID_VIRTIO_P9,
790b4422bf3SAneesh Kumar K.V 		.header_type		= PCI_HEADER_TYPE_NORMAL,
791b4422bf3SAneesh Kumar K.V 		.revision_id		= 0,
792b4422bf3SAneesh Kumar K.V 		.class			= 0x010000,
793b4422bf3SAneesh Kumar K.V 		.subsys_vendor_id	= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
794b4422bf3SAneesh Kumar K.V 		.subsys_id		= VIRTIO_ID_9P,
795b4422bf3SAneesh Kumar K.V 		.irq_pin		= pin,
796b4422bf3SAneesh Kumar K.V 		.irq_line		= line,
797b4422bf3SAneesh Kumar K.V 		.bar[0]			= p9_base_addr | PCI_BASE_ADDRESS_SPACE_IO,
798b4422bf3SAneesh Kumar K.V 	};
799b4422bf3SAneesh Kumar K.V 	pci__register(&p9dev->pci_hdr, dev);
800b4422bf3SAneesh Kumar K.V 
801b4422bf3SAneesh Kumar K.V 	return;
802b4422bf3SAneesh Kumar K.V free_p9dev_config:
803b4422bf3SAneesh Kumar K.V 	free(p9dev->config);
804b4422bf3SAneesh Kumar K.V free_p9dev:
805b4422bf3SAneesh Kumar K.V 	free(p9dev);
8061c7850f9SSasha Levin }
807