11c7850f9SSasha Levin #include "kvm/virtio-pci-dev.h" 21c7850f9SSasha Levin #include "kvm/ioport.h" 31c7850f9SSasha Levin #include "kvm/util.h" 41c7850f9SSasha Levin #include "kvm/threadpool.h" 5bfc15268SAneesh Kumar K.V #include "kvm/irq.h" 6bfc15268SAneesh Kumar K.V #include "kvm/virtio-9p.h" 7e59662b3SSasha Levin #include "kvm/guest_compat.h" 8cac9e8fdSSasha Levin #include "kvm/builtin-setup.h" 91c7850f9SSasha Levin 10bfc15268SAneesh Kumar K.V #include <stdio.h> 11bfc15268SAneesh Kumar K.V #include <stdlib.h> 121c7850f9SSasha Levin #include <fcntl.h> 131c7850f9SSasha Levin #include <sys/stat.h> 14bfc15268SAneesh Kumar K.V #include <unistd.h> 15bfc15268SAneesh Kumar K.V #include <string.h> 16bfc15268SAneesh Kumar K.V #include <errno.h> 17c797b6c6SAneesh Kumar K.V #include <sys/vfs.h> 181c7850f9SSasha Levin 192daa28d4SAneesh Kumar K.V #include <linux/virtio_ring.h> 202daa28d4SAneesh Kumar K.V #include <linux/virtio_9p.h> 21c6cb7c75SAndre Przywara #include <linux/9p.h> 222daa28d4SAneesh Kumar K.V 23c7838fbdSSasha Levin static LIST_HEAD(devs); 24312c62d1SSasha Levin static int compat_id = -1; 25c7838fbdSSasha Levin 26e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid); 27e2341580SSasha Levin static struct p9_fid *find_or_create_fid(struct p9_dev *dev, u32 fid) 28e2341580SSasha Levin { 29e2341580SSasha Levin struct rb_node *node = dev->fids.rb_node; 30e2341580SSasha Levin struct p9_fid *pfid = NULL; 31e2341580SSasha Levin 32e2341580SSasha Levin while (node) { 33e2341580SSasha Levin struct p9_fid *cur = rb_entry(node, struct p9_fid, node); 34e2341580SSasha Levin 35e2341580SSasha Levin if (fid < cur->fid) { 36e2341580SSasha Levin node = node->rb_left; 37e2341580SSasha Levin } else if (fid > cur->fid) { 38e2341580SSasha Levin node = node->rb_right; 39e2341580SSasha Levin } else { 40e2341580SSasha Levin return cur; 41e2341580SSasha Levin } 42e2341580SSasha Levin } 43e2341580SSasha Levin 44e2341580SSasha Levin pfid = calloc(sizeof(*pfid), 1); 45e2341580SSasha Levin if (!pfid) 46e2341580SSasha Levin return NULL; 47e2341580SSasha Levin 48e2341580SSasha Levin pfid->fid = fid; 49e2341580SSasha Levin strcpy(pfid->abs_path, dev->root_dir); 50e2341580SSasha Levin pfid->path = pfid->abs_path + strlen(dev->root_dir); 51e2341580SSasha Levin 52e2341580SSasha Levin insert_new_fid(dev, pfid); 53e2341580SSasha Levin 54e2341580SSasha Levin return pfid; 55e2341580SSasha Levin } 56e2341580SSasha Levin 57e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid) 58e2341580SSasha Levin { 59e2341580SSasha Levin struct rb_node **node = &(dev->fids.rb_node), *parent = NULL; 60e2341580SSasha Levin 61e2341580SSasha Levin while (*node) { 62e2341580SSasha Levin int result = fid->fid - rb_entry(*node, struct p9_fid, node)->fid; 63e2341580SSasha Levin 64e2341580SSasha Levin parent = *node; 65e2341580SSasha Levin if (result < 0) 66e2341580SSasha Levin node = &((*node)->rb_left); 67e2341580SSasha Levin else if (result > 0) 68e2341580SSasha Levin node = &((*node)->rb_right); 69e2341580SSasha Levin else 70e2341580SSasha Levin return -EEXIST; 71e2341580SSasha Levin } 72e2341580SSasha Levin 73e2341580SSasha Levin rb_link_node(&fid->node, parent, node); 74e2341580SSasha Levin rb_insert_color(&fid->node, &dev->fids); 75e2341580SSasha Levin return 0; 76e2341580SSasha Levin } 77e2341580SSasha Levin 7831a6fb8dSSasha Levin static struct p9_fid *get_fid(struct p9_dev *p9dev, int fid) 7931a6fb8dSSasha Levin { 80e2341580SSasha Levin struct p9_fid *new; 8131a6fb8dSSasha Levin 82e2341580SSasha Levin new = find_or_create_fid(p9dev, fid); 83e2341580SSasha Levin 84e2341580SSasha Levin return new; 8531a6fb8dSSasha Levin } 8631a6fb8dSSasha Levin 871c7850f9SSasha Levin /* Warning: Immediately use value returned from this function */ 88b4422bf3SAneesh Kumar K.V static const char *rel_to_abs(struct p9_dev *p9dev, 89b4422bf3SAneesh Kumar K.V const char *path, char *abs_path) 901c7850f9SSasha Levin { 91b4422bf3SAneesh Kumar K.V sprintf(abs_path, "%s/%s", p9dev->root_dir, path); 921c7850f9SSasha Levin 931c7850f9SSasha Levin return abs_path; 941c7850f9SSasha Levin } 951c7850f9SSasha Levin 96c797b6c6SAneesh Kumar K.V static void stat2qid(struct stat *st, struct p9_qid *qid) 971c7850f9SSasha Levin { 981c7850f9SSasha Levin *qid = (struct p9_qid) { 991c7850f9SSasha Levin .path = st->st_ino, 1001c7850f9SSasha Levin .version = st->st_mtime, 1011c7850f9SSasha Levin }; 1021c7850f9SSasha Levin 1031c7850f9SSasha Levin if (S_ISDIR(st->st_mode)) 1041c7850f9SSasha Levin qid->type |= P9_QTDIR; 1051c7850f9SSasha Levin } 1061c7850f9SSasha Levin 107b4422bf3SAneesh Kumar K.V static void close_fid(struct p9_dev *p9dev, u32 fid) 1081c7850f9SSasha Levin { 109e2341580SSasha Levin struct p9_fid *pfid = get_fid(p9dev, fid); 110e2341580SSasha Levin 111e2341580SSasha Levin if (pfid->fd > 0) 112e2341580SSasha Levin close(pfid->fd); 113e2341580SSasha Levin 114e2341580SSasha Levin if (pfid->dir) 115e2341580SSasha Levin closedir(pfid->dir); 116e2341580SSasha Levin 117e2341580SSasha Levin rb_erase(&pfid->node, &p9dev->fids); 118e2341580SSasha Levin free(pfid); 1191c7850f9SSasha Levin } 120e2341580SSasha Levin 121bfc15268SAneesh Kumar K.V static void virtio_p9_set_reply_header(struct p9_pdu *pdu, u32 size) 1221c7850f9SSasha Levin { 123bfc15268SAneesh Kumar K.V u8 cmd; 124bfc15268SAneesh Kumar K.V u16 tag; 125bfc15268SAneesh Kumar K.V 126bfc15268SAneesh Kumar K.V pdu->read_offset = sizeof(u32); 127bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "bw", &cmd, &tag); 128bfc15268SAneesh Kumar K.V pdu->write_offset = 0; 129bfc15268SAneesh Kumar K.V /* cmd + 1 is the reply message */ 130bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "dbw", size, cmd + 1, tag); 1311c7850f9SSasha Levin } 1321c7850f9SSasha Levin 1336b163a87SAneesh Kumar K.V static u16 virtio_p9_update_iov_cnt(struct iovec iov[], u32 count, int iov_cnt) 1346b163a87SAneesh Kumar K.V { 1356b163a87SAneesh Kumar K.V int i; 1366b163a87SAneesh Kumar K.V u32 total = 0; 1376b163a87SAneesh Kumar K.V for (i = 0; (i < iov_cnt) && (total < count); i++) { 1386b163a87SAneesh Kumar K.V if (total + iov[i].iov_len > count) { 1396b163a87SAneesh Kumar K.V /* we don't need this iov fully */ 1406b163a87SAneesh Kumar K.V iov[i].iov_len -= ((total + iov[i].iov_len) - count); 1416b163a87SAneesh Kumar K.V i++; 1426b163a87SAneesh Kumar K.V break; 1436b163a87SAneesh Kumar K.V } 1446b163a87SAneesh Kumar K.V total += iov[i].iov_len; 1456b163a87SAneesh Kumar K.V } 1466b163a87SAneesh Kumar K.V return i; 1476b163a87SAneesh Kumar K.V } 1486b163a87SAneesh Kumar K.V 149eee1ba8eSAneesh Kumar K.V static void virtio_p9_error_reply(struct p9_dev *p9dev, 150eee1ba8eSAneesh Kumar K.V struct p9_pdu *pdu, int err, u32 *outlen) 151eee1ba8eSAneesh Kumar K.V { 152bfc15268SAneesh Kumar K.V u16 tag; 153eee1ba8eSAneesh Kumar K.V 1549c2e1d1aSSuzuki K. Poulose /* EMFILE at server implies ENFILE for the VM */ 1559c2e1d1aSSuzuki K. Poulose if (err == EMFILE) 1569c2e1d1aSSuzuki K. Poulose err = ENFILE; 1579c2e1d1aSSuzuki K. Poulose 1585529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 159c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", err); 160bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 161eee1ba8eSAneesh Kumar K.V 162c797b6c6SAneesh Kumar K.V /* read the tag from input */ 163bfc15268SAneesh Kumar K.V pdu->read_offset = sizeof(u32) + sizeof(u8); 164bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "w", &tag); 165bfc15268SAneesh Kumar K.V 166c797b6c6SAneesh Kumar K.V /* Update the header */ 167bfc15268SAneesh Kumar K.V pdu->write_offset = 0; 168c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "dbw", *outlen, P9_RLERROR, tag); 169eee1ba8eSAneesh Kumar K.V } 170eee1ba8eSAneesh Kumar K.V 171ead43b01SAneesh Kumar K.V static void virtio_p9_version(struct p9_dev *p9dev, 172af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1731c7850f9SSasha Levin { 174c797b6c6SAneesh Kumar K.V u32 msize; 175c797b6c6SAneesh Kumar K.V char *version; 176c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ds", &msize, &version); 177c797b6c6SAneesh Kumar K.V /* 178c797b6c6SAneesh Kumar K.V * reply with the same msize the client sent us 179c797b6c6SAneesh Kumar K.V * Error out if the request is not for 9P2000.L 180c797b6c6SAneesh Kumar K.V */ 1815529bcd7SAsias He if (!strcmp(version, VIRTIO_9P_VERSION_DOTL)) 182c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ds", msize, version); 183c797b6c6SAneesh Kumar K.V else 184c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ds", msize, "unknown"); 1851c7850f9SSasha Levin 186bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 187bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 188c797b6c6SAneesh Kumar K.V free(version); 189ead43b01SAneesh Kumar K.V return; 1901c7850f9SSasha Levin } 1911c7850f9SSasha Levin 192ead43b01SAneesh Kumar K.V static void virtio_p9_clunk(struct p9_dev *p9dev, 193af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1941c7850f9SSasha Levin { 195bfc15268SAneesh Kumar K.V u32 fid; 1961c7850f9SSasha Levin 197bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid); 198bfc15268SAneesh Kumar K.V close_fid(p9dev, fid); 1991c7850f9SSasha Levin 200bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 201bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 202ead43b01SAneesh Kumar K.V return; 2031c7850f9SSasha Levin } 2041c7850f9SSasha Levin 205c797b6c6SAneesh Kumar K.V /* 206c797b6c6SAneesh Kumar K.V * FIXME!! Need to map to protocol independent value. Upstream 207c797b6c6SAneesh Kumar K.V * 9p also have the same BUG 208c797b6c6SAneesh Kumar K.V */ 209c797b6c6SAneesh Kumar K.V static int virtio_p9_openflags(int flags) 210c797b6c6SAneesh Kumar K.V { 211c797b6c6SAneesh Kumar K.V flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT | O_DIRECT); 212c797b6c6SAneesh Kumar K.V flags |= O_NOFOLLOW; 213c797b6c6SAneesh Kumar K.V return flags; 214c797b6c6SAneesh Kumar K.V } 215c797b6c6SAneesh Kumar K.V 21632585666SSasha Levin static bool is_dir(struct p9_fid *fid) 21732585666SSasha Levin { 21832585666SSasha Levin struct stat st; 21932585666SSasha Levin 22032585666SSasha Levin stat(fid->abs_path, &st); 22132585666SSasha Levin 22232585666SSasha Levin return S_ISDIR(st.st_mode); 22332585666SSasha Levin } 22432585666SSasha Levin 225ead43b01SAneesh Kumar K.V static void virtio_p9_open(struct p9_dev *p9dev, 226af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 2271c7850f9SSasha Levin { 228c797b6c6SAneesh Kumar K.V u32 fid, flags; 2291c7850f9SSasha Levin struct stat st; 230bfc15268SAneesh Kumar K.V struct p9_qid qid; 231bfc15268SAneesh Kumar K.V struct p9_fid *new_fid; 232bfc15268SAneesh Kumar K.V 233c797b6c6SAneesh Kumar K.V 234c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dd", &fid, &flags); 23531a6fb8dSSasha Levin new_fid = get_fid(p9dev, fid); 2361c7850f9SSasha Levin 23730204a77SAneesh Kumar K.V if (lstat(new_fid->abs_path, &st) < 0) 238eee1ba8eSAneesh Kumar K.V goto err_out; 2391c7850f9SSasha Levin 240c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 2411c7850f9SSasha Levin 24232585666SSasha Levin if (is_dir(new_fid)) { 2431c7850f9SSasha Levin new_fid->dir = opendir(new_fid->abs_path); 244eee1ba8eSAneesh Kumar K.V if (!new_fid->dir) 245eee1ba8eSAneesh Kumar K.V goto err_out; 246eee1ba8eSAneesh Kumar K.V } else { 247eee1ba8eSAneesh Kumar K.V new_fid->fd = open(new_fid->abs_path, 248c797b6c6SAneesh Kumar K.V virtio_p9_openflags(flags)); 249eee1ba8eSAneesh Kumar K.V if (new_fid->fd < 0) 250eee1ba8eSAneesh Kumar K.V goto err_out; 251eee1ba8eSAneesh Kumar K.V } 252c797b6c6SAneesh Kumar K.V /* FIXME!! need ot send proper iounit */ 253bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 254bfc15268SAneesh Kumar K.V 255bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 256bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 257ead43b01SAneesh Kumar K.V return; 258eee1ba8eSAneesh Kumar K.V err_out: 259eee1ba8eSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 260ead43b01SAneesh Kumar K.V return; 2611c7850f9SSasha Levin } 2621c7850f9SSasha Levin 263ead43b01SAneesh Kumar K.V static void virtio_p9_create(struct p9_dev *p9dev, 264af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 2651c7850f9SSasha Levin { 266c797b6c6SAneesh Kumar K.V int fd, ret; 267bfc15268SAneesh Kumar K.V char *name; 268af045e53SAneesh Kumar K.V struct stat st; 269bfc15268SAneesh Kumar K.V struct p9_qid qid; 270c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 2714bc9734aSAneesh Kumar K.V char full_path[PATH_MAX]; 272c797b6c6SAneesh Kumar K.V u32 dfid_val, flags, mode, gid; 273af045e53SAneesh Kumar K.V 274c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsddd", &dfid_val, 275c797b6c6SAneesh Kumar K.V &name, &flags, &mode, &gid); 27631a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 2771c7850f9SSasha Levin 278c797b6c6SAneesh Kumar K.V flags = virtio_p9_openflags(flags); 2795f900f6dSSasha Levin 280c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 281c797b6c6SAneesh Kumar K.V fd = open(full_path, flags | O_CREAT, mode); 2824bc9734aSAneesh Kumar K.V if (fd < 0) 2834bc9734aSAneesh Kumar K.V goto err_out; 284c797b6c6SAneesh Kumar K.V dfid->fd = fd; 285c797b6c6SAneesh Kumar K.V 2864bc9734aSAneesh Kumar K.V if (lstat(full_path, &st) < 0) 2876c8ca053SAneesh Kumar K.V goto err_out; 2881c7850f9SSasha Levin 289c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 290c797b6c6SAneesh Kumar K.V if (ret < 0) 291c797b6c6SAneesh Kumar K.V goto err_out; 292c797b6c6SAneesh Kumar K.V 293c797b6c6SAneesh Kumar K.V sprintf(dfid->path, "%s/%s", dfid->path, name); 294c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 295bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 296bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 297bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 2985f900f6dSSasha Levin free(name); 2996c8ca053SAneesh Kumar K.V return; 3006c8ca053SAneesh Kumar K.V err_out: 3015f900f6dSSasha Levin free(name); 302c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 303c797b6c6SAneesh Kumar K.V return; 304c797b6c6SAneesh Kumar K.V } 305c797b6c6SAneesh Kumar K.V 306c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev, 307c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 308c797b6c6SAneesh Kumar K.V { 309c797b6c6SAneesh Kumar K.V int ret; 310c797b6c6SAneesh Kumar K.V char *name; 311c797b6c6SAneesh Kumar K.V struct stat st; 312c797b6c6SAneesh Kumar K.V struct p9_qid qid; 313c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 314c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 315c797b6c6SAneesh Kumar K.V u32 dfid_val, mode, gid; 316c797b6c6SAneesh Kumar K.V 317c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val, 318c797b6c6SAneesh Kumar K.V &name, &mode, &gid); 31931a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 320c797b6c6SAneesh Kumar K.V 321c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 322c797b6c6SAneesh Kumar K.V ret = mkdir(full_path, mode); 323c797b6c6SAneesh Kumar K.V if (ret < 0) 324c797b6c6SAneesh Kumar K.V goto err_out; 325c797b6c6SAneesh Kumar K.V 326c797b6c6SAneesh Kumar K.V if (lstat(full_path, &st) < 0) 327c797b6c6SAneesh Kumar K.V goto err_out; 328c797b6c6SAneesh Kumar K.V 329c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 330c797b6c6SAneesh Kumar K.V if (ret < 0) 331c797b6c6SAneesh Kumar K.V goto err_out; 332c797b6c6SAneesh Kumar K.V 333c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 334c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 335c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 336c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 337c797b6c6SAneesh Kumar K.V free(name); 338c797b6c6SAneesh Kumar K.V return; 339c797b6c6SAneesh Kumar K.V err_out: 340c797b6c6SAneesh Kumar K.V free(name); 341c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 342ead43b01SAneesh Kumar K.V return; 3431c7850f9SSasha Levin } 3441c7850f9SSasha Levin 345ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev, 346af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 3471c7850f9SSasha Levin { 348af045e53SAneesh Kumar K.V u8 i; 349bfc15268SAneesh Kumar K.V u16 nwqid; 350bfc15268SAneesh Kumar K.V u16 nwname; 351bfc15268SAneesh Kumar K.V struct p9_qid wqid; 352e2341580SSasha Levin struct p9_fid *new_fid, *old_fid; 353c797b6c6SAneesh Kumar K.V u32 fid_val, newfid_val; 354c797b6c6SAneesh Kumar K.V 3551c7850f9SSasha Levin 356bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname); 35731a6fb8dSSasha Levin new_fid = get_fid(p9dev, newfid_val); 3581c7850f9SSasha Levin 359bfc15268SAneesh Kumar K.V nwqid = 0; 360bfc15268SAneesh Kumar K.V if (nwname) { 36131a6fb8dSSasha Levin struct p9_fid *fid = get_fid(p9dev, fid_val); 362bfc15268SAneesh Kumar K.V 363baac79a5SAneesh Kumar K.V strcpy(new_fid->path, fid->path); 364bfc15268SAneesh Kumar K.V /* skip the space for count */ 365bfc15268SAneesh Kumar K.V pdu->write_offset += sizeof(u16); 366bfc15268SAneesh Kumar K.V for (i = 0; i < nwname; i++) { 367bfc15268SAneesh Kumar K.V struct stat st; 3681c7850f9SSasha Levin char tmp[PATH_MAX] = {0}; 3691c7850f9SSasha Levin char full_path[PATH_MAX]; 370e55ed135SPekka Enberg char *str; 371bfc15268SAneesh Kumar K.V 372bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "s", &str); 3731c7850f9SSasha Levin 3741c7850f9SSasha Levin /* Format the new path we're 'walk'ing into */ 375baac79a5SAneesh Kumar K.V sprintf(tmp, "%s/%s", new_fid->path, str); 376e55ed135SPekka Enberg 377e55ed135SPekka Enberg free(str); 378e55ed135SPekka Enberg 379c797b6c6SAneesh Kumar K.V if (lstat(rel_to_abs(p9dev, tmp, full_path), &st) < 0) 3806c8ca053SAneesh Kumar K.V goto err_out; 3811c7850f9SSasha Levin 382c797b6c6SAneesh Kumar K.V stat2qid(&st, &wqid); 3831c7850f9SSasha Levin strcpy(new_fid->path, tmp); 384c797b6c6SAneesh Kumar K.V new_fid->uid = fid->uid; 385bfc15268SAneesh Kumar K.V nwqid++; 386bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &wqid); 3871c7850f9SSasha Levin } 3881c7850f9SSasha Levin } else { 389bfc15268SAneesh Kumar K.V /* 390bfc15268SAneesh Kumar K.V * update write_offset so our outlen get correct value 391bfc15268SAneesh Kumar K.V */ 392bfc15268SAneesh Kumar K.V pdu->write_offset += sizeof(u16); 393e2341580SSasha Levin old_fid = get_fid(p9dev, fid_val); 394e2341580SSasha Levin strcpy(new_fid->path, old_fid->path); 395e2341580SSasha Levin new_fid->uid = old_fid->uid; 3961c7850f9SSasha Levin } 397bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 3985529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 399bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", nwqid); 400bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 4016c8ca053SAneesh Kumar K.V return; 4026c8ca053SAneesh Kumar K.V err_out: 4036c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 404ead43b01SAneesh Kumar K.V return; 4051c7850f9SSasha Levin } 4061c7850f9SSasha Levin 407ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev, 408af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 4091c7850f9SSasha Levin { 410bfc15268SAneesh Kumar K.V char *uname; 411bfc15268SAneesh Kumar K.V char *aname; 4121c7850f9SSasha Levin struct stat st; 413bfc15268SAneesh Kumar K.V struct p9_qid qid; 4141c7850f9SSasha Levin struct p9_fid *fid; 415c797b6c6SAneesh Kumar K.V u32 fid_val, afid, uid; 416bfc15268SAneesh Kumar K.V 417c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid, 418c797b6c6SAneesh Kumar K.V &uname, &aname, &uid); 4191c7850f9SSasha Levin 42039257180SPekka Enberg free(uname); 42139257180SPekka Enberg free(aname); 42239257180SPekka Enberg 42330204a77SAneesh Kumar K.V if (lstat(p9dev->root_dir, &st) < 0) 4246c8ca053SAneesh Kumar K.V goto err_out; 4251c7850f9SSasha Levin 426c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 4271c7850f9SSasha Levin 42831a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 429c797b6c6SAneesh Kumar K.V fid->uid = uid; 4301c7850f9SSasha Levin strcpy(fid->path, "/"); 4311c7850f9SSasha Levin 432bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 433bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 434bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 4356c8ca053SAneesh Kumar K.V return; 4366c8ca053SAneesh Kumar K.V err_out: 4376c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 438ead43b01SAneesh Kumar K.V return; 4391c7850f9SSasha Levin } 4401c7850f9SSasha Levin 441c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev, 442c797b6c6SAneesh Kumar K.V struct stat *st, struct p9_stat_dotl *statl) 4435f900f6dSSasha Levin { 444c797b6c6SAneesh Kumar K.V memset(statl, 0, sizeof(*statl)); 445c797b6c6SAneesh Kumar K.V statl->st_mode = st->st_mode; 446c797b6c6SAneesh Kumar K.V statl->st_nlink = st->st_nlink; 447506fd90bSSasha Levin statl->st_uid = KUIDT_INIT(st->st_uid); 448506fd90bSSasha Levin statl->st_gid = KGIDT_INIT(st->st_gid); 449c797b6c6SAneesh Kumar K.V statl->st_rdev = st->st_rdev; 450c797b6c6SAneesh Kumar K.V statl->st_size = st->st_size; 451c797b6c6SAneesh Kumar K.V statl->st_blksize = st->st_blksize; 452c797b6c6SAneesh Kumar K.V statl->st_blocks = st->st_blocks; 453c797b6c6SAneesh Kumar K.V statl->st_atime_sec = st->st_atime; 454c797b6c6SAneesh Kumar K.V statl->st_atime_nsec = st->st_atim.tv_nsec; 455c797b6c6SAneesh Kumar K.V statl->st_mtime_sec = st->st_mtime; 456c797b6c6SAneesh Kumar K.V statl->st_mtime_nsec = st->st_mtim.tv_nsec; 457c797b6c6SAneesh Kumar K.V statl->st_ctime_sec = st->st_ctime; 458c797b6c6SAneesh Kumar K.V statl->st_ctime_nsec = st->st_ctim.tv_nsec; 459c797b6c6SAneesh Kumar K.V /* Currently we only support BASIC fields in stat */ 460c797b6c6SAneesh Kumar K.V statl->st_result_mask = P9_STATS_BASIC; 461c797b6c6SAneesh Kumar K.V stat2qid(st, &statl->qid); 4621c7850f9SSasha Levin } 4631c7850f9SSasha Levin 464ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev, 465af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 4661c7850f9SSasha Levin { 467bfc15268SAneesh Kumar K.V u64 offset; 468bfc15268SAneesh Kumar K.V u32 fid_val; 469c797b6c6SAneesh Kumar K.V u16 iov_cnt; 470c797b6c6SAneesh Kumar K.V void *iov_base; 471c797b6c6SAneesh Kumar K.V size_t iov_len; 472bfc15268SAneesh Kumar K.V u32 count, rcount; 473bfc15268SAneesh Kumar K.V struct p9_fid *fid; 474c797b6c6SAneesh Kumar K.V 4751c7850f9SSasha Levin 476bfc15268SAneesh Kumar K.V rcount = 0; 477bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 47831a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 47950c479e0SAneesh Kumar K.V 48050c479e0SAneesh Kumar K.V iov_base = pdu->in_iov[0].iov_base; 48150c479e0SAneesh Kumar K.V iov_len = pdu->in_iov[0].iov_len; 48250c479e0SAneesh Kumar K.V iov_cnt = pdu->in_iov_cnt; 4835529bcd7SAsias He pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32); 4845529bcd7SAsias He pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32); 4856b163a87SAneesh Kumar K.V pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov, 486bfc15268SAneesh Kumar K.V count, 4876b163a87SAneesh Kumar K.V pdu->in_iov_cnt); 488bfc15268SAneesh Kumar K.V rcount = preadv(fid->fd, pdu->in_iov, 489bfc15268SAneesh Kumar K.V pdu->in_iov_cnt, offset); 490bfc15268SAneesh Kumar K.V if (rcount > count) 491bfc15268SAneesh Kumar K.V rcount = count; 492bfc15268SAneesh Kumar K.V /* 493bfc15268SAneesh Kumar K.V * Update the iov_base back, so that rest of 494bfc15268SAneesh Kumar K.V * pdu_writef works correctly. 495bfc15268SAneesh Kumar K.V */ 49650c479e0SAneesh Kumar K.V pdu->in_iov[0].iov_base = iov_base; 49750c479e0SAneesh Kumar K.V pdu->in_iov[0].iov_len = iov_len; 49850c479e0SAneesh Kumar K.V pdu->in_iov_cnt = iov_cnt; 499c797b6c6SAneesh Kumar K.V 5005529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 501bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", rcount); 502bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset + rcount; 503bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 504ead43b01SAneesh Kumar K.V return; 5051c7850f9SSasha Levin } 5061c7850f9SSasha Levin 507c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent) 508c797b6c6SAneesh Kumar K.V { 509c797b6c6SAneesh Kumar K.V /* 510c797b6c6SAneesh Kumar K.V * Size of each dirent: 511c797b6c6SAneesh Kumar K.V * qid(13) + offset(8) + type(1) + name_len(2) + name 512c797b6c6SAneesh Kumar K.V */ 513c797b6c6SAneesh Kumar K.V return 24 + strlen(dent->d_name); 514c797b6c6SAneesh Kumar K.V } 515c797b6c6SAneesh Kumar K.V 516c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev, 517c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 518c797b6c6SAneesh Kumar K.V { 519c797b6c6SAneesh Kumar K.V u32 fid_val; 520c797b6c6SAneesh Kumar K.V u32 count, rcount; 521c797b6c6SAneesh Kumar K.V struct stat st; 522c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 523c797b6c6SAneesh Kumar K.V struct dirent *dent; 524c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 525c797b6c6SAneesh Kumar K.V u64 offset, old_offset; 526c797b6c6SAneesh Kumar K.V 527c797b6c6SAneesh Kumar K.V rcount = 0; 528c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 52931a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 530c797b6c6SAneesh Kumar K.V 53132585666SSasha Levin if (!is_dir(fid)) { 53269bb4278SSasha Levin errno = EINVAL; 533c797b6c6SAneesh Kumar K.V goto err_out; 534c797b6c6SAneesh Kumar K.V } 535c797b6c6SAneesh Kumar K.V 536c797b6c6SAneesh Kumar K.V /* Move the offset specified */ 537c797b6c6SAneesh Kumar K.V seekdir(fid->dir, offset); 538c797b6c6SAneesh Kumar K.V 539c797b6c6SAneesh Kumar K.V old_offset = offset; 540c797b6c6SAneesh Kumar K.V /* If reading a dir, fill the buffer with p9_stat entries */ 541c797b6c6SAneesh Kumar K.V dent = readdir(fid->dir); 542c797b6c6SAneesh Kumar K.V 543c797b6c6SAneesh Kumar K.V /* Skip the space for writing count */ 544c797b6c6SAneesh Kumar K.V pdu->write_offset += sizeof(u32); 545c797b6c6SAneesh Kumar K.V while (dent) { 546c797b6c6SAneesh Kumar K.V u32 read; 547c797b6c6SAneesh Kumar K.V struct p9_qid qid; 548c797b6c6SAneesh Kumar K.V 549c797b6c6SAneesh Kumar K.V if ((rcount + virtio_p9_dentry_size(dent)) > count) { 550c797b6c6SAneesh Kumar K.V /* seek to the previous offset and return */ 551c797b6c6SAneesh Kumar K.V seekdir(fid->dir, old_offset); 552c797b6c6SAneesh Kumar K.V break; 553c797b6c6SAneesh Kumar K.V } 554c797b6c6SAneesh Kumar K.V old_offset = dent->d_off; 555c797b6c6SAneesh Kumar K.V lstat(rel_to_abs(p9dev, dent->d_name, full_path), &st); 556c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 557c797b6c6SAneesh Kumar K.V read = pdu->write_offset; 558c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off, 559c797b6c6SAneesh Kumar K.V dent->d_type, dent->d_name); 560c797b6c6SAneesh Kumar K.V rcount += pdu->write_offset - read; 561c797b6c6SAneesh Kumar K.V dent = readdir(fid->dir); 562c797b6c6SAneesh Kumar K.V } 563c797b6c6SAneesh Kumar K.V 5645529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 565c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", rcount); 566c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset + rcount; 567c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 568c797b6c6SAneesh Kumar K.V return; 569c797b6c6SAneesh Kumar K.V err_out: 570c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 571c797b6c6SAneesh Kumar K.V return; 572c797b6c6SAneesh Kumar K.V } 573c797b6c6SAneesh Kumar K.V 574c797b6c6SAneesh Kumar K.V 575c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev, 576af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 5771c7850f9SSasha Levin { 578bfc15268SAneesh Kumar K.V u32 fid_val; 579af045e53SAneesh Kumar K.V struct stat st; 580c797b6c6SAneesh Kumar K.V u64 request_mask; 581bfc15268SAneesh Kumar K.V struct p9_fid *fid; 582c797b6c6SAneesh Kumar K.V struct p9_stat_dotl statl; 5831c7850f9SSasha Levin 584c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask); 58531a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 58630204a77SAneesh Kumar K.V if (lstat(fid->abs_path, &st) < 0) 5876c8ca053SAneesh Kumar K.V goto err_out; 5881c7850f9SSasha Levin 589c797b6c6SAneesh Kumar K.V virtio_p9_fill_stat(p9dev, &st, &statl); 590c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "A", &statl); 591bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 592bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 593ead43b01SAneesh Kumar K.V return; 5946c8ca053SAneesh Kumar K.V err_out: 5956c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 5966c8ca053SAneesh Kumar K.V return; 5971c7850f9SSasha Levin } 5981c7850f9SSasha Levin 599c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */ 600c797b6c6SAneesh Kumar K.V /* 601c797b6c6SAneesh Kumar K.V * Attribute flags. These should be or-ed together to figure out what 602c797b6c6SAneesh Kumar K.V * has been changed! 603c797b6c6SAneesh Kumar K.V */ 604c797b6c6SAneesh Kumar K.V #define ATTR_MODE (1 << 0) 605c797b6c6SAneesh Kumar K.V #define ATTR_UID (1 << 1) 606c797b6c6SAneesh Kumar K.V #define ATTR_GID (1 << 2) 607c797b6c6SAneesh Kumar K.V #define ATTR_SIZE (1 << 3) 608c797b6c6SAneesh Kumar K.V #define ATTR_ATIME (1 << 4) 609c797b6c6SAneesh Kumar K.V #define ATTR_MTIME (1 << 5) 610c797b6c6SAneesh Kumar K.V #define ATTR_CTIME (1 << 6) 611c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET (1 << 7) 612c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET (1 << 8) 613c797b6c6SAneesh Kumar K.V #define ATTR_FORCE (1 << 9) /* Not a change, but a change it */ 614c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG (1 << 10) 615c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID (1 << 11) 616c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID (1 << 12) 617c797b6c6SAneesh Kumar K.V #define ATTR_FILE (1 << 13) 618c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV (1 << 14) 619c797b6c6SAneesh Kumar K.V #define ATTR_OPEN (1 << 15) /* Truncating from open(O_TRUNC) */ 620c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET (1 << 16) 621c797b6c6SAneesh Kumar K.V 622c797b6c6SAneesh Kumar K.V #define ATTR_MASK 127 623c797b6c6SAneesh Kumar K.V 624c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev, 625af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 6261c7850f9SSasha Levin { 627c797b6c6SAneesh Kumar K.V int ret = 0; 628bfc15268SAneesh Kumar K.V u32 fid_val; 629bfc15268SAneesh Kumar K.V struct p9_fid *fid; 630c797b6c6SAneesh Kumar K.V struct p9_iattr_dotl p9attr; 6311c7850f9SSasha Levin 632c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr); 63331a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 6341c7850f9SSasha Levin 635c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MODE) { 636c797b6c6SAneesh Kumar K.V ret = chmod(fid->abs_path, p9attr.mode); 637c797b6c6SAneesh Kumar K.V if (ret < 0) 638c797b6c6SAneesh Kumar K.V goto err_out; 639c797b6c6SAneesh Kumar K.V } 640c797b6c6SAneesh Kumar K.V if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) { 641c797b6c6SAneesh Kumar K.V struct timespec times[2]; 642c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_ATIME) { 643c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_ATIME_SET) { 644c797b6c6SAneesh Kumar K.V times[0].tv_sec = p9attr.atime_sec; 645c797b6c6SAneesh Kumar K.V times[0].tv_nsec = p9attr.atime_nsec; 646c797b6c6SAneesh Kumar K.V } else { 647c797b6c6SAneesh Kumar K.V times[0].tv_nsec = UTIME_NOW; 648c797b6c6SAneesh Kumar K.V } 649c797b6c6SAneesh Kumar K.V } else { 650c797b6c6SAneesh Kumar K.V times[0].tv_nsec = UTIME_OMIT; 651c797b6c6SAneesh Kumar K.V } 652c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MTIME) { 653c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MTIME_SET) { 654c797b6c6SAneesh Kumar K.V times[1].tv_sec = p9attr.mtime_sec; 655c797b6c6SAneesh Kumar K.V times[1].tv_nsec = p9attr.mtime_nsec; 656c797b6c6SAneesh Kumar K.V } else { 657c797b6c6SAneesh Kumar K.V times[1].tv_nsec = UTIME_NOW; 658c797b6c6SAneesh Kumar K.V } 659c797b6c6SAneesh Kumar K.V } else 660c797b6c6SAneesh Kumar K.V times[1].tv_nsec = UTIME_OMIT; 661c797b6c6SAneesh Kumar K.V 662c797b6c6SAneesh Kumar K.V ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW); 663c797b6c6SAneesh Kumar K.V if (ret < 0) 664c797b6c6SAneesh Kumar K.V goto err_out; 665c797b6c6SAneesh Kumar K.V } 666c797b6c6SAneesh Kumar K.V /* 667c797b6c6SAneesh Kumar K.V * If the only valid entry in iattr is ctime we can call 668c797b6c6SAneesh Kumar K.V * chown(-1,-1) to update the ctime of the file 669c797b6c6SAneesh Kumar K.V */ 670c797b6c6SAneesh Kumar K.V if ((p9attr.valid & (ATTR_UID | ATTR_GID)) || 671c797b6c6SAneesh Kumar K.V ((p9attr.valid & ATTR_CTIME) 672c797b6c6SAneesh Kumar K.V && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) { 673c797b6c6SAneesh Kumar K.V if (!(p9attr.valid & ATTR_UID)) 674506fd90bSSasha Levin p9attr.uid = KUIDT_INIT(-1); 675c797b6c6SAneesh Kumar K.V 676c797b6c6SAneesh Kumar K.V if (!(p9attr.valid & ATTR_GID)) 677506fd90bSSasha Levin p9attr.gid = KGIDT_INIT(-1); 678c797b6c6SAneesh Kumar K.V 679506fd90bSSasha Levin ret = lchown(fid->abs_path, __kuid_val(p9attr.uid), 680506fd90bSSasha Levin __kgid_val(p9attr.gid)); 681c797b6c6SAneesh Kumar K.V if (ret < 0) 682c797b6c6SAneesh Kumar K.V goto err_out; 683c797b6c6SAneesh Kumar K.V } 684c797b6c6SAneesh Kumar K.V if (p9attr.valid & (ATTR_SIZE)) { 685c797b6c6SAneesh Kumar K.V ret = truncate(fid->abs_path, p9attr.size); 686c797b6c6SAneesh Kumar K.V if (ret < 0) 687c797b6c6SAneesh Kumar K.V goto err_out; 688c797b6c6SAneesh Kumar K.V } 6895529bcd7SAsias He *outlen = VIRTIO_9P_HDR_LEN; 690bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 691ead43b01SAneesh Kumar K.V return; 6924bc9734aSAneesh Kumar K.V err_out: 6934bc9734aSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 6944bc9734aSAneesh Kumar K.V return; 6951c7850f9SSasha Levin } 6961c7850f9SSasha Levin 697ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev, 698af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 6991c7850f9SSasha Levin { 7004bc9734aSAneesh Kumar K.V 701bfc15268SAneesh Kumar K.V u64 offset; 702bfc15268SAneesh Kumar K.V u32 fid_val; 7034bc9734aSAneesh Kumar K.V u32 count; 7044bc9734aSAneesh Kumar K.V ssize_t res; 70550c479e0SAneesh Kumar K.V u16 iov_cnt; 70650c479e0SAneesh Kumar K.V void *iov_base; 70750c479e0SAneesh Kumar K.V size_t iov_len; 708bfc15268SAneesh Kumar K.V struct p9_fid *fid; 709b064b05aSAneesh Kumar K.V /* u32 fid + u64 offset + u32 count */ 710b064b05aSAneesh Kumar K.V int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32); 7111c7850f9SSasha Levin 712bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 71331a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 714af045e53SAneesh Kumar K.V 71550c479e0SAneesh Kumar K.V iov_base = pdu->out_iov[0].iov_base; 71650c479e0SAneesh Kumar K.V iov_len = pdu->out_iov[0].iov_len; 71750c479e0SAneesh Kumar K.V iov_cnt = pdu->out_iov_cnt; 71850c479e0SAneesh Kumar K.V 719bfc15268SAneesh Kumar K.V /* Adjust the iovec to skip the header and meta data */ 720b064b05aSAneesh Kumar K.V pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size); 721b064b05aSAneesh Kumar K.V pdu->out_iov[0].iov_len -= (sizeof(struct p9_msg) + twrite_size); 722bfc15268SAneesh Kumar K.V pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count, 7236b163a87SAneesh Kumar K.V pdu->out_iov_cnt); 7244bc9734aSAneesh Kumar K.V res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset); 72550c479e0SAneesh Kumar K.V /* 72650c479e0SAneesh Kumar K.V * Update the iov_base back, so that rest of 72750c479e0SAneesh Kumar K.V * pdu_readf works correctly. 72850c479e0SAneesh Kumar K.V */ 72950c479e0SAneesh Kumar K.V pdu->out_iov[0].iov_base = iov_base; 73050c479e0SAneesh Kumar K.V pdu->out_iov[0].iov_len = iov_len; 73150c479e0SAneesh Kumar K.V pdu->out_iov_cnt = iov_cnt; 732c797b6c6SAneesh Kumar K.V 7334bc9734aSAneesh Kumar K.V if (res < 0) 7344bc9734aSAneesh Kumar K.V goto err_out; 7354bc9734aSAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", res); 736bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 737bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 738ead43b01SAneesh Kumar K.V return; 7394bc9734aSAneesh Kumar K.V err_out: 7404bc9734aSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 7414bc9734aSAneesh Kumar K.V return; 7421c7850f9SSasha Levin } 7431c7850f9SSasha Levin 7446fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev, 7456fc5cd9bSSasha Levin struct p9_pdu *pdu, u32 *outlen) 7466fc5cd9bSSasha Levin { 7476fc5cd9bSSasha Levin int ret; 7486fc5cd9bSSasha Levin u32 fid_val; 7496fc5cd9bSSasha Levin struct p9_fid *fid; 7506fc5cd9bSSasha Levin 7516fc5cd9bSSasha Levin virtio_p9_pdu_readf(pdu, "d", &fid_val); 75231a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 7536fc5cd9bSSasha Levin 7549b604a9cSSasha Levin ret = remove(fid->abs_path); 7556fc5cd9bSSasha Levin if (ret < 0) 7566fc5cd9bSSasha Levin goto err_out; 7576fc5cd9bSSasha Levin *outlen = pdu->write_offset; 7586fc5cd9bSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 7596fc5cd9bSSasha Levin return; 7606fc5cd9bSSasha Levin 7616fc5cd9bSSasha Levin err_out: 7626fc5cd9bSSasha Levin virtio_p9_error_reply(p9dev, pdu, errno, outlen); 7636fc5cd9bSSasha Levin return; 7646fc5cd9bSSasha Levin } 7656fc5cd9bSSasha Levin 766f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev, 767f161f28bSSasha Levin struct p9_pdu *pdu, u32 *outlen) 768f161f28bSSasha Levin { 769f161f28bSSasha Levin int ret; 770f161f28bSSasha Levin u32 fid_val, new_fid_val; 771f161f28bSSasha Levin struct p9_fid *fid, *new_fid; 772f161f28bSSasha Levin char full_path[PATH_MAX], *new_name; 773f161f28bSSasha Levin 774f161f28bSSasha Levin virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name); 77531a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 77631a6fb8dSSasha Levin new_fid = get_fid(p9dev, new_fid_val); 777f161f28bSSasha Levin 778f161f28bSSasha Levin sprintf(full_path, "%s/%s", new_fid->abs_path, new_name); 779f161f28bSSasha Levin ret = rename(fid->abs_path, full_path); 780f161f28bSSasha Levin if (ret < 0) 781f161f28bSSasha Levin goto err_out; 782f161f28bSSasha Levin *outlen = pdu->write_offset; 783f161f28bSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 784f161f28bSSasha Levin return; 785f161f28bSSasha Levin 786f161f28bSSasha Levin err_out: 787f161f28bSSasha Levin virtio_p9_error_reply(p9dev, pdu, errno, outlen); 788f161f28bSSasha Levin return; 789f161f28bSSasha Levin } 790f161f28bSSasha Levin 791c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev, 792c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 793c797b6c6SAneesh Kumar K.V { 794c797b6c6SAneesh Kumar K.V int ret; 795c797b6c6SAneesh Kumar K.V u32 fid_val; 796c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 797c797b6c6SAneesh Kumar K.V char target_path[PATH_MAX]; 798c797b6c6SAneesh Kumar K.V 799c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid_val); 80031a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 801c797b6c6SAneesh Kumar K.V 802c797b6c6SAneesh Kumar K.V memset(target_path, 0, PATH_MAX); 803c797b6c6SAneesh Kumar K.V ret = readlink(fid->abs_path, target_path, PATH_MAX - 1); 804c797b6c6SAneesh Kumar K.V if (ret < 0) 805c797b6c6SAneesh Kumar K.V goto err_out; 806c797b6c6SAneesh Kumar K.V 807c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "s", target_path); 808c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 809c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 810c797b6c6SAneesh Kumar K.V return; 811c797b6c6SAneesh Kumar K.V err_out: 812c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 813c797b6c6SAneesh Kumar K.V return; 814c797b6c6SAneesh Kumar K.V } 815c797b6c6SAneesh Kumar K.V 816c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev, 817c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 818c797b6c6SAneesh Kumar K.V { 819c797b6c6SAneesh Kumar K.V int ret; 820c797b6c6SAneesh Kumar K.V u64 fsid; 821c797b6c6SAneesh Kumar K.V u32 fid_val; 822c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 823c797b6c6SAneesh Kumar K.V struct statfs stat_buf; 824c797b6c6SAneesh Kumar K.V 825c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid_val); 82631a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 827c797b6c6SAneesh Kumar K.V 828c797b6c6SAneesh Kumar K.V ret = statfs(fid->abs_path, &stat_buf); 829c797b6c6SAneesh Kumar K.V if (ret < 0) 830c797b6c6SAneesh Kumar K.V goto err_out; 831c797b6c6SAneesh Kumar K.V /* FIXME!! f_blocks needs update based on client msize */ 832c797b6c6SAneesh Kumar K.V fsid = (unsigned int) stat_buf.f_fsid.__val[0] | 833c797b6c6SAneesh Kumar K.V (unsigned long long)stat_buf.f_fsid.__val[1] << 32; 834c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type, 835c797b6c6SAneesh Kumar K.V stat_buf.f_bsize, stat_buf.f_blocks, 836c797b6c6SAneesh Kumar K.V stat_buf.f_bfree, stat_buf.f_bavail, 837c797b6c6SAneesh Kumar K.V stat_buf.f_files, stat_buf.f_ffree, 838c797b6c6SAneesh Kumar K.V fsid, stat_buf.f_namelen); 839c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 840c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 841c797b6c6SAneesh Kumar K.V return; 842c797b6c6SAneesh Kumar K.V err_out: 843c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 844c797b6c6SAneesh Kumar K.V return; 845c797b6c6SAneesh Kumar K.V } 846c797b6c6SAneesh Kumar K.V 847c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev, 848c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 849c797b6c6SAneesh Kumar K.V { 850c797b6c6SAneesh Kumar K.V int ret; 851c797b6c6SAneesh Kumar K.V char *name; 852c797b6c6SAneesh Kumar K.V struct stat st; 853c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 854c797b6c6SAneesh Kumar K.V struct p9_qid qid; 855c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 856c797b6c6SAneesh Kumar K.V u32 fid_val, mode, major, minor, gid; 857c797b6c6SAneesh Kumar K.V 858c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode, 859c797b6c6SAneesh Kumar K.V &major, &minor, &gid); 860c797b6c6SAneesh Kumar K.V 86131a6fb8dSSasha Levin dfid = get_fid(p9dev, fid_val); 862c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 863c797b6c6SAneesh Kumar K.V ret = mknod(full_path, mode, makedev(major, minor)); 864c797b6c6SAneesh Kumar K.V if (ret < 0) 865c797b6c6SAneesh Kumar K.V goto err_out; 866c797b6c6SAneesh Kumar K.V 867c797b6c6SAneesh Kumar K.V if (lstat(full_path, &st) < 0) 868c797b6c6SAneesh Kumar K.V goto err_out; 869c797b6c6SAneesh Kumar K.V 870c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 871c797b6c6SAneesh Kumar K.V if (ret < 0) 872c797b6c6SAneesh Kumar K.V goto err_out; 873c797b6c6SAneesh Kumar K.V 874c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 875c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 876c797b6c6SAneesh Kumar K.V free(name); 877c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 878c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 879c797b6c6SAneesh Kumar K.V return; 880c797b6c6SAneesh Kumar K.V err_out: 881c797b6c6SAneesh Kumar K.V free(name); 882c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 883c797b6c6SAneesh Kumar K.V return; 884c797b6c6SAneesh Kumar K.V } 885c797b6c6SAneesh Kumar K.V 886c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev, 887c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 888c797b6c6SAneesh Kumar K.V { 889*644140efSRussell King int ret, fd; 890c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 891c797b6c6SAneesh Kumar K.V u32 fid_val, datasync; 892c797b6c6SAneesh Kumar K.V 893c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync); 89431a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 895c797b6c6SAneesh Kumar K.V 896*644140efSRussell King if (fid->dir) 897*644140efSRussell King fd = dirfd(fid->dir); 898c797b6c6SAneesh Kumar K.V else 899*644140efSRussell King fd = fid->fd; 900*644140efSRussell King 901*644140efSRussell King if (datasync) 902*644140efSRussell King ret = fdatasync(fd); 903*644140efSRussell King else 904*644140efSRussell King ret = fsync(fd); 905c797b6c6SAneesh Kumar K.V if (ret < 0) 906c797b6c6SAneesh Kumar K.V goto err_out; 907c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 908c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 909c797b6c6SAneesh Kumar K.V return; 910c797b6c6SAneesh Kumar K.V err_out: 911c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 912c797b6c6SAneesh Kumar K.V return; 913c797b6c6SAneesh Kumar K.V } 914c797b6c6SAneesh Kumar K.V 915c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev, 916c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 917c797b6c6SAneesh Kumar K.V { 918c797b6c6SAneesh Kumar K.V int ret; 919c797b6c6SAneesh Kumar K.V struct stat st; 920c797b6c6SAneesh Kumar K.V u32 fid_val, gid; 921c797b6c6SAneesh Kumar K.V struct p9_qid qid; 922c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 923c797b6c6SAneesh Kumar K.V char new_name[PATH_MAX]; 924c797b6c6SAneesh Kumar K.V char *old_path, *name; 925c797b6c6SAneesh Kumar K.V 926c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid); 927c797b6c6SAneesh Kumar K.V 92831a6fb8dSSasha Levin dfid = get_fid(p9dev, fid_val); 929c797b6c6SAneesh Kumar K.V sprintf(new_name, "%s/%s", dfid->abs_path, name); 930c797b6c6SAneesh Kumar K.V ret = symlink(old_path, new_name); 931c797b6c6SAneesh Kumar K.V if (ret < 0) 932c797b6c6SAneesh Kumar K.V goto err_out; 933c797b6c6SAneesh Kumar K.V 934c797b6c6SAneesh Kumar K.V if (lstat(new_name, &st) < 0) 935c797b6c6SAneesh Kumar K.V goto err_out; 936c797b6c6SAneesh Kumar K.V 937c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 938c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 939c797b6c6SAneesh Kumar K.V free(name); 940c797b6c6SAneesh Kumar K.V free(old_path); 941c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 942c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 943c797b6c6SAneesh Kumar K.V return; 944c797b6c6SAneesh Kumar K.V err_out: 945c797b6c6SAneesh Kumar K.V free(name); 946c797b6c6SAneesh Kumar K.V free(old_path); 947c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 948c797b6c6SAneesh Kumar K.V return; 949c797b6c6SAneesh Kumar K.V } 950c797b6c6SAneesh Kumar K.V 951c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev, 952c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 953c797b6c6SAneesh Kumar K.V { 954c797b6c6SAneesh Kumar K.V int ret; 955c797b6c6SAneesh Kumar K.V char *name; 956c797b6c6SAneesh Kumar K.V u32 fid_val, dfid_val; 957c797b6c6SAneesh Kumar K.V struct p9_fid *dfid, *fid; 958c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 959c797b6c6SAneesh Kumar K.V 960c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name); 961c797b6c6SAneesh Kumar K.V 96231a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 96331a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 964c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 965c797b6c6SAneesh Kumar K.V ret = link(fid->abs_path, full_path); 966c797b6c6SAneesh Kumar K.V if (ret < 0) 967c797b6c6SAneesh Kumar K.V goto err_out; 968c797b6c6SAneesh Kumar K.V free(name); 969c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 970c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 971c797b6c6SAneesh Kumar K.V return; 972c797b6c6SAneesh Kumar K.V err_out: 973c797b6c6SAneesh Kumar K.V free(name); 974c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 975c797b6c6SAneesh Kumar K.V return; 976c797b6c6SAneesh Kumar K.V 977c797b6c6SAneesh Kumar K.V } 978c797b6c6SAneesh Kumar K.V 979c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev, 980c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 981c797b6c6SAneesh Kumar K.V { 982c797b6c6SAneesh Kumar K.V u8 ret; 983c797b6c6SAneesh Kumar K.V u32 fid_val; 984c797b6c6SAneesh Kumar K.V struct p9_flock flock; 985c797b6c6SAneesh Kumar K.V 986c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type, 987c797b6c6SAneesh Kumar K.V &flock.flags, &flock.start, &flock.length, 988c797b6c6SAneesh Kumar K.V &flock.proc_id, &flock.client_id); 989c797b6c6SAneesh Kumar K.V 990c797b6c6SAneesh Kumar K.V /* Just return success */ 991c797b6c6SAneesh Kumar K.V ret = P9_LOCK_SUCCESS; 992c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", ret); 993c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 994c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 995c797b6c6SAneesh Kumar K.V free(flock.client_id); 996c797b6c6SAneesh Kumar K.V return; 997c797b6c6SAneesh Kumar K.V } 998c797b6c6SAneesh Kumar K.V 999c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev, 1000c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1001c797b6c6SAneesh Kumar K.V { 1002c797b6c6SAneesh Kumar K.V u32 fid_val; 1003c797b6c6SAneesh Kumar K.V struct p9_getlock glock; 1004c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type, 1005c797b6c6SAneesh Kumar K.V &glock.start, &glock.length, &glock.proc_id, 1006c797b6c6SAneesh Kumar K.V &glock.client_id); 1007c797b6c6SAneesh Kumar K.V 1008c797b6c6SAneesh Kumar K.V /* Just return success */ 1009c797b6c6SAneesh Kumar K.V glock.type = F_UNLCK; 1010c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "bqqds", glock.type, 1011c797b6c6SAneesh Kumar K.V glock.start, glock.length, glock.proc_id, 1012c797b6c6SAneesh Kumar K.V glock.client_id); 1013c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1014c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1015c797b6c6SAneesh Kumar K.V free(glock.client_id); 1016c797b6c6SAneesh Kumar K.V return; 1017c797b6c6SAneesh Kumar K.V } 1018c797b6c6SAneesh Kumar K.V 1019c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor) 1020c797b6c6SAneesh Kumar K.V { 1021c797b6c6SAneesh Kumar K.V int size = strlen(ancestor); 1022c797b6c6SAneesh Kumar K.V if (!strncmp(path, ancestor, size)) { 1023c797b6c6SAneesh Kumar K.V /* 1024c797b6c6SAneesh Kumar K.V * Now check whether ancestor is a full name or 1025c797b6c6SAneesh Kumar K.V * or directory component and not just part 1026c797b6c6SAneesh Kumar K.V * of a name. 1027c797b6c6SAneesh Kumar K.V */ 1028c797b6c6SAneesh Kumar K.V if (path[size] == '\0' || path[size] == '/') 1029c797b6c6SAneesh Kumar K.V return 1; 1030c797b6c6SAneesh Kumar K.V } 1031c797b6c6SAneesh Kumar K.V return 0; 1032c797b6c6SAneesh Kumar K.V } 1033c797b6c6SAneesh Kumar K.V 1034c797b6c6SAneesh Kumar K.V static void virtio_p9_fix_path(char *fid_path, char *old_name, char *new_name) 1035c797b6c6SAneesh Kumar K.V { 1036c797b6c6SAneesh Kumar K.V char tmp_name[PATH_MAX]; 1037c797b6c6SAneesh Kumar K.V size_t rp_sz = strlen(old_name); 1038c797b6c6SAneesh Kumar K.V 1039c797b6c6SAneesh Kumar K.V if (rp_sz == strlen(fid_path)) { 1040c797b6c6SAneesh Kumar K.V /* replace the full name */ 1041c797b6c6SAneesh Kumar K.V strcpy(fid_path, new_name); 1042c797b6c6SAneesh Kumar K.V return; 1043c797b6c6SAneesh Kumar K.V } 1044c797b6c6SAneesh Kumar K.V /* save the trailing path details */ 1045c797b6c6SAneesh Kumar K.V strcpy(tmp_name, fid_path + rp_sz); 1046c797b6c6SAneesh Kumar K.V sprintf(fid_path, "%s%s", new_name, tmp_name); 1047c797b6c6SAneesh Kumar K.V return; 1048c797b6c6SAneesh Kumar K.V } 1049c797b6c6SAneesh Kumar K.V 1050e2341580SSasha Levin static void rename_fids(struct p9_dev *p9dev, char *old_name, char *new_name) 1051e2341580SSasha Levin { 1052e2341580SSasha Levin struct rb_node *node = rb_first(&p9dev->fids); 1053e2341580SSasha Levin 1054e2341580SSasha Levin while (node) { 1055e2341580SSasha Levin struct p9_fid *fid = rb_entry(node, struct p9_fid, node); 1056e2341580SSasha Levin 1057e2341580SSasha Levin if (fid->fid != P9_NOFID && virtio_p9_ancestor(fid->path, old_name)) { 1058e2341580SSasha Levin virtio_p9_fix_path(fid->path, old_name, new_name); 1059e2341580SSasha Levin } 1060e2341580SSasha Levin node = rb_next(node); 1061e2341580SSasha Levin } 1062e2341580SSasha Levin } 1063e2341580SSasha Levin 1064c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev, 1065c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1066c797b6c6SAneesh Kumar K.V { 1067e2341580SSasha Levin int ret; 1068c797b6c6SAneesh Kumar K.V char *old_name, *new_name; 1069c797b6c6SAneesh Kumar K.V u32 old_dfid_val, new_dfid_val; 1070c797b6c6SAneesh Kumar K.V struct p9_fid *old_dfid, *new_dfid; 1071c797b6c6SAneesh Kumar K.V char old_full_path[PATH_MAX], new_full_path[PATH_MAX]; 1072c797b6c6SAneesh Kumar K.V 1073c797b6c6SAneesh Kumar K.V 1074c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name, 1075c797b6c6SAneesh Kumar K.V &new_dfid_val, &new_name); 1076c797b6c6SAneesh Kumar K.V 107731a6fb8dSSasha Levin old_dfid = get_fid(p9dev, old_dfid_val); 107831a6fb8dSSasha Levin new_dfid = get_fid(p9dev, new_dfid_val); 1079c797b6c6SAneesh Kumar K.V 1080c797b6c6SAneesh Kumar K.V sprintf(old_full_path, "%s/%s", old_dfid->abs_path, old_name); 1081c797b6c6SAneesh Kumar K.V sprintf(new_full_path, "%s/%s", new_dfid->abs_path, new_name); 1082c797b6c6SAneesh Kumar K.V ret = rename(old_full_path, new_full_path); 1083c797b6c6SAneesh Kumar K.V if (ret < 0) 1084c797b6c6SAneesh Kumar K.V goto err_out; 1085c797b6c6SAneesh Kumar K.V /* 1086c797b6c6SAneesh Kumar K.V * Now fix path in other fids, if the renamed path is part of 1087c797b6c6SAneesh Kumar K.V * that. 1088c797b6c6SAneesh Kumar K.V */ 1089e2341580SSasha Levin rename_fids(p9dev, old_name, new_name); 1090c797b6c6SAneesh Kumar K.V free(old_name); 1091c797b6c6SAneesh Kumar K.V free(new_name); 1092c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1093c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1094c797b6c6SAneesh Kumar K.V return; 1095c797b6c6SAneesh Kumar K.V err_out: 1096c797b6c6SAneesh Kumar K.V free(old_name); 1097c797b6c6SAneesh Kumar K.V free(new_name); 1098c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1099c797b6c6SAneesh Kumar K.V return; 1100c797b6c6SAneesh Kumar K.V } 1101c797b6c6SAneesh Kumar K.V 1102c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev, 1103c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1104c797b6c6SAneesh Kumar K.V { 1105c797b6c6SAneesh Kumar K.V int ret; 1106c797b6c6SAneesh Kumar K.V char *name; 1107c797b6c6SAneesh Kumar K.V u32 fid_val, flags; 1108c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 1109c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 1110c797b6c6SAneesh Kumar K.V 1111c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags); 111231a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 1113c797b6c6SAneesh Kumar K.V 1114c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", fid->abs_path, name); 1115c797b6c6SAneesh Kumar K.V ret = remove(full_path); 1116c797b6c6SAneesh Kumar K.V if (ret < 0) 1117c797b6c6SAneesh Kumar K.V goto err_out; 1118c797b6c6SAneesh Kumar K.V free(name); 1119c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1120c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1121c797b6c6SAneesh Kumar K.V return; 1122c797b6c6SAneesh Kumar K.V err_out: 1123c797b6c6SAneesh Kumar K.V free(name); 1124c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1125c797b6c6SAneesh Kumar K.V return; 1126c797b6c6SAneesh Kumar K.V } 1127c797b6c6SAneesh Kumar K.V 11285cc808aaSSasha Levin static void virtio_p9_flush(struct p9_dev *p9dev, 11295cc808aaSSasha Levin struct p9_pdu *pdu, u32 *outlen) 11305cc808aaSSasha Levin { 11315cc808aaSSasha Levin u16 tag, oldtag; 11325cc808aaSSasha Levin 11335cc808aaSSasha Levin virtio_p9_pdu_readf(pdu, "ww", &tag, &oldtag); 11345cc808aaSSasha Levin virtio_p9_pdu_writef(pdu, "w", tag); 11355cc808aaSSasha Levin *outlen = pdu->write_offset; 11365cc808aaSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 11375cc808aaSSasha Levin 11385cc808aaSSasha Levin return; 11395cc808aaSSasha Levin } 11405cc808aaSSasha Levin 1141c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev, 1142c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1143c797b6c6SAneesh Kumar K.V { 1144c797b6c6SAneesh Kumar K.V return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen); 1145c797b6c6SAneesh Kumar K.V } 1146c797b6c6SAneesh Kumar K.V 1147ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev, 1148af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen); 1149b4422bf3SAneesh Kumar K.V 1150c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */ 1151c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74 1152c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76 1153c797b6c6SAneesh Kumar K.V 1154c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = { 1155c797b6c6SAneesh Kumar K.V [P9_TREADDIR] = virtio_p9_readdir, 1156c797b6c6SAneesh Kumar K.V [P9_TSTATFS] = virtio_p9_statfs, 1157c797b6c6SAneesh Kumar K.V [P9_TGETATTR] = virtio_p9_getattr, 1158c797b6c6SAneesh Kumar K.V [P9_TSETATTR] = virtio_p9_setattr, 1159c797b6c6SAneesh Kumar K.V [P9_TXATTRWALK] = virtio_p9_eopnotsupp, 1160c797b6c6SAneesh Kumar K.V [P9_TXATTRCREATE] = virtio_p9_eopnotsupp, 1161c797b6c6SAneesh Kumar K.V [P9_TMKNOD] = virtio_p9_mknod, 1162c797b6c6SAneesh Kumar K.V [P9_TLOCK] = virtio_p9_lock, 1163c797b6c6SAneesh Kumar K.V [P9_TGETLOCK] = virtio_p9_getlock, 1164c797b6c6SAneesh Kumar K.V [P9_TRENAMEAT] = virtio_p9_renameat, 1165c797b6c6SAneesh Kumar K.V [P9_TREADLINK] = virtio_p9_readlink, 1166c797b6c6SAneesh Kumar K.V [P9_TUNLINKAT] = virtio_p9_unlinkat, 1167c797b6c6SAneesh Kumar K.V [P9_TMKDIR] = virtio_p9_mkdir, 1168b4422bf3SAneesh Kumar K.V [P9_TVERSION] = virtio_p9_version, 1169c797b6c6SAneesh Kumar K.V [P9_TLOPEN] = virtio_p9_open, 1170b4422bf3SAneesh Kumar K.V [P9_TATTACH] = virtio_p9_attach, 1171b4422bf3SAneesh Kumar K.V [P9_TWALK] = virtio_p9_walk, 1172c797b6c6SAneesh Kumar K.V [P9_TCLUNK] = virtio_p9_clunk, 1173c797b6c6SAneesh Kumar K.V [P9_TFSYNC] = virtio_p9_fsync, 1174b4422bf3SAneesh Kumar K.V [P9_TREAD] = virtio_p9_read, 11755cc808aaSSasha Levin [P9_TFLUSH] = virtio_p9_flush, 1176c797b6c6SAneesh Kumar K.V [P9_TLINK] = virtio_p9_link, 1177c797b6c6SAneesh Kumar K.V [P9_TSYMLINK] = virtio_p9_symlink, 1178c797b6c6SAneesh Kumar K.V [P9_TLCREATE] = virtio_p9_create, 1179b4422bf3SAneesh Kumar K.V [P9_TWRITE] = virtio_p9_write, 11806fc5cd9bSSasha Levin [P9_TREMOVE] = virtio_p9_remove, 1181f161f28bSSasha Levin [P9_TRENAME] = virtio_p9_rename, 1182b4422bf3SAneesh Kumar K.V }; 1183b4422bf3SAneesh Kumar K.V 1184af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq) 1185af045e53SAneesh Kumar K.V { 1186af045e53SAneesh Kumar K.V struct p9_pdu *pdu = calloc(1, sizeof(*pdu)); 1187af045e53SAneesh Kumar K.V if (!pdu) 1188af045e53SAneesh Kumar K.V return NULL; 1189af045e53SAneesh Kumar K.V 1190bfc15268SAneesh Kumar K.V /* skip the pdu header p9_msg */ 11915529bcd7SAsias He pdu->read_offset = VIRTIO_9P_HDR_LEN; 11925529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 1193af045e53SAneesh Kumar K.V pdu->queue_head = virt_queue__get_inout_iov(kvm, vq, pdu->in_iov, 1194a8a44649SAsias He pdu->out_iov, &pdu->in_iov_cnt, &pdu->out_iov_cnt); 1195af045e53SAneesh Kumar K.V return pdu; 1196af045e53SAneesh Kumar K.V } 1197af045e53SAneesh Kumar K.V 1198af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu) 1199af045e53SAneesh Kumar K.V { 1200af045e53SAneesh Kumar K.V struct p9_msg *msg; 1201af045e53SAneesh Kumar K.V /* 1202af045e53SAneesh Kumar K.V * we can peek directly into pdu for a u8 1203af045e53SAneesh Kumar K.V * value. The host endianess won't be an issue 1204af045e53SAneesh Kumar K.V */ 1205af045e53SAneesh Kumar K.V msg = pdu->out_iov[0].iov_base; 1206af045e53SAneesh Kumar K.V return msg->cmd; 1207af045e53SAneesh Kumar K.V } 1208af045e53SAneesh Kumar K.V 1209b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job) 12101c7850f9SSasha Levin { 1211af045e53SAneesh Kumar K.V u8 cmd; 1212b4422bf3SAneesh Kumar K.V u32 len = 0; 1213b4422bf3SAneesh Kumar K.V p9_handler *handler; 1214b4422bf3SAneesh Kumar K.V struct p9_dev *p9dev; 1215af045e53SAneesh Kumar K.V struct virt_queue *vq; 1216af045e53SAneesh Kumar K.V struct p9_pdu *p9pdu; 12171c7850f9SSasha Levin 1218b4422bf3SAneesh Kumar K.V vq = job->vq; 1219b4422bf3SAneesh Kumar K.V p9dev = job->p9dev; 12201c7850f9SSasha Levin 1221af045e53SAneesh Kumar K.V p9pdu = virtio_p9_pdu_init(kvm, vq); 1222af045e53SAneesh Kumar K.V cmd = virtio_p9_get_cmd(p9pdu); 1223af045e53SAneesh Kumar K.V 1224c797b6c6SAneesh Kumar K.V if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) || 1225c797b6c6SAneesh Kumar K.V !virtio_9p_dotl_handler[cmd]) 122697b408afSAneesh Kumar K.V handler = virtio_p9_eopnotsupp; 1227dd78d9eaSAneesh Kumar K.V else 1228c797b6c6SAneesh Kumar K.V handler = virtio_9p_dotl_handler[cmd]; 1229c797b6c6SAneesh Kumar K.V 1230af045e53SAneesh Kumar K.V handler(p9dev, p9pdu, &len); 1231af045e53SAneesh Kumar K.V virt_queue__set_used_elem(vq, p9pdu->queue_head, len); 1232af045e53SAneesh Kumar K.V free(p9pdu); 12331c7850f9SSasha Levin return true; 12341c7850f9SSasha Levin } 12351c7850f9SSasha Levin 12361c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param) 12371c7850f9SSasha Levin { 1238b4422bf3SAneesh Kumar K.V struct p9_dev_job *job = (struct p9_dev_job *)param; 1239b4422bf3SAneesh Kumar K.V struct p9_dev *p9dev = job->p9dev; 1240b4422bf3SAneesh Kumar K.V struct virt_queue *vq = job->vq; 12411c7850f9SSasha Levin 12421c7850f9SSasha Levin while (virt_queue__available(vq)) { 1243b4422bf3SAneesh Kumar K.V virtio_p9_do_io_request(kvm, job); 124402eca50cSAsias He p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs); 12451c7850f9SSasha Levin } 12461c7850f9SSasha Levin } 12471c7850f9SSasha Levin 1248c5ae742bSSasha Levin static u8 *get_config(struct kvm *kvm, void *dev) 12491c7850f9SSasha Levin { 1250c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 12511c7850f9SSasha Levin 1252c5ae742bSSasha Levin return ((u8 *)(p9dev->config)); 1253c7838fbdSSasha Levin } 1254c7838fbdSSasha Levin 1255c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev) 1256c7838fbdSSasha Levin { 1257c7838fbdSSasha Levin return 1 << VIRTIO_9P_MOUNT_TAG; 1258c7838fbdSSasha Levin } 1259c7838fbdSSasha Levin 1260c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features) 1261c7838fbdSSasha Levin { 1262c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1263e0ea1859SMarc Zyngier struct virtio_9p_config *conf = p9dev->config; 1264c7838fbdSSasha Levin 1265c7838fbdSSasha Levin p9dev->features = features; 1266e0ea1859SMarc Zyngier conf->tag_len = virtio_host_to_guest_u16(&p9dev->vdev, conf->tag_len); 1267c7838fbdSSasha Levin } 1268c7838fbdSSasha Levin 1269c59ba304SWill Deacon static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align, 1270c59ba304SWill Deacon u32 pfn) 1271c7838fbdSSasha Levin { 1272c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1273b4422bf3SAneesh Kumar K.V struct p9_dev_job *job; 1274b4422bf3SAneesh Kumar K.V struct virt_queue *queue; 1275c7838fbdSSasha Levin void *p; 12761c7850f9SSasha Levin 1277312c62d1SSasha Levin compat__remove_message(compat_id); 1278e59662b3SSasha Levin 1279c7838fbdSSasha Levin queue = &p9dev->vqs[vq]; 1280c7838fbdSSasha Levin queue->pfn = pfn; 1281e7e2950aSSasha Levin p = virtio_get_vq(kvm, queue->pfn, page_size); 1282c7838fbdSSasha Levin job = &p9dev->jobs[vq]; 12831c7850f9SSasha Levin 1284c59ba304SWill Deacon vring_init(&queue->vring, VIRTQUEUE_NUM, p, align); 1285e0ea1859SMarc Zyngier virtio_init_device_vq(&p9dev->vdev, queue); 12861c7850f9SSasha Levin 1287b4422bf3SAneesh Kumar K.V *job = (struct p9_dev_job) { 1288b4422bf3SAneesh Kumar K.V .vq = queue, 1289b4422bf3SAneesh Kumar K.V .p9dev = p9dev, 1290b4422bf3SAneesh Kumar K.V }; 1291df0c7f57SSasha Levin thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job); 129260eb42d5SSasha Levin 1293c7838fbdSSasha Levin return 0; 12941c7850f9SSasha Levin } 12951c7850f9SSasha Levin 1296c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq) 1297c7838fbdSSasha Levin { 1298c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 12991c7850f9SSasha Levin 1300c7838fbdSSasha Levin thread_pool__do_job(&p9dev->jobs[vq].job_id); 1301c7838fbdSSasha Levin 1302c7838fbdSSasha Levin return 0; 1303c7838fbdSSasha Levin } 1304c7838fbdSSasha Levin 1305c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq) 1306c7838fbdSSasha Levin { 1307c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1308c7838fbdSSasha Levin 1309c7838fbdSSasha Levin return p9dev->vqs[vq].pfn; 1310c7838fbdSSasha Levin } 1311c7838fbdSSasha Levin 1312c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq) 1313c7838fbdSSasha Levin { 1314c7838fbdSSasha Levin return VIRTQUEUE_NUM; 1315c7838fbdSSasha Levin } 1316c7838fbdSSasha Levin 13177aba29c1SWill Deacon static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size) 13187aba29c1SWill Deacon { 13197aba29c1SWill Deacon /* FIXME: dynamic */ 13207aba29c1SWill Deacon return size; 13217aba29c1SWill Deacon } 13227aba29c1SWill Deacon 13231c47ce69SSasha Levin struct virtio_ops p9_dev_virtio_ops = (struct virtio_ops) { 1324c7838fbdSSasha Levin .get_config = get_config, 1325c7838fbdSSasha Levin .get_host_features = get_host_features, 1326c7838fbdSSasha Levin .set_guest_features = set_guest_features, 1327c7838fbdSSasha Levin .init_vq = init_vq, 1328c7838fbdSSasha Levin .notify_vq = notify_vq, 1329c7838fbdSSasha Levin .get_pfn_vq = get_pfn_vq, 1330c7838fbdSSasha Levin .get_size_vq = get_size_vq, 13317aba29c1SWill Deacon .set_size_vq = set_size_vq, 1332c7838fbdSSasha Levin }; 13331c47ce69SSasha Levin 1334cac9e8fdSSasha Levin int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) 1335cac9e8fdSSasha Levin { 1336cac9e8fdSSasha Levin char *tag_name; 1337cac9e8fdSSasha Levin char tmp[PATH_MAX]; 1338cac9e8fdSSasha Levin struct kvm *kvm = opt->ptr; 1339cac9e8fdSSasha Levin 1340cac9e8fdSSasha Levin /* 1341cac9e8fdSSasha Levin * 9p dir can be of the form dirname,tag_name or 1342cac9e8fdSSasha Levin * just dirname. In the later case we use the 1343cac9e8fdSSasha Levin * default tag name 1344cac9e8fdSSasha Levin */ 1345cac9e8fdSSasha Levin tag_name = strstr(arg, ","); 1346cac9e8fdSSasha Levin if (tag_name) { 1347cac9e8fdSSasha Levin *tag_name = '\0'; 1348cac9e8fdSSasha Levin tag_name++; 1349cac9e8fdSSasha Levin } 1350cac9e8fdSSasha Levin if (realpath(arg, tmp)) { 1351cac9e8fdSSasha Levin if (virtio_9p__register(kvm, tmp, tag_name) < 0) 1352cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1353cac9e8fdSSasha Levin } else 1354cac9e8fdSSasha Levin die("Failed resolving 9p path"); 1355cac9e8fdSSasha Levin return 0; 1356cac9e8fdSSasha Levin } 1357cac9e8fdSSasha Levin 1358cac9e8fdSSasha Levin int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset) 1359cac9e8fdSSasha Levin { 1360cac9e8fdSSasha Levin char path[PATH_MAX]; 1361cac9e8fdSSasha Levin struct stat st; 1362cac9e8fdSSasha Levin struct kvm *kvm = opt->ptr; 1363cac9e8fdSSasha Levin 1364cac9e8fdSSasha Levin if (stat(arg, &st) == 0 && 1365cac9e8fdSSasha Levin S_ISDIR(st.st_mode)) { 1366cac9e8fdSSasha Levin char tmp[PATH_MAX]; 1367cac9e8fdSSasha Levin 1368cac9e8fdSSasha Levin if (kvm->cfg.using_rootfs) 1369cac9e8fdSSasha Levin die("Please use only one rootfs directory atmost"); 1370cac9e8fdSSasha Levin 1371cac9e8fdSSasha Levin if (realpath(arg, tmp) == 0 || 1372cac9e8fdSSasha Levin virtio_9p__register(kvm, tmp, "/dev/root") < 0) 1373cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1374cac9e8fdSSasha Levin kvm->cfg.using_rootfs = 1; 1375cac9e8fdSSasha Levin return 0; 1376cac9e8fdSSasha Levin } 1377cac9e8fdSSasha Levin 1378cac9e8fdSSasha Levin snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg); 1379cac9e8fdSSasha Levin 1380cac9e8fdSSasha Levin if (stat(path, &st) == 0 && 1381cac9e8fdSSasha Levin S_ISDIR(st.st_mode)) { 1382cac9e8fdSSasha Levin char tmp[PATH_MAX]; 1383cac9e8fdSSasha Levin 1384cac9e8fdSSasha Levin if (kvm->cfg.using_rootfs) 1385cac9e8fdSSasha Levin die("Please use only one rootfs directory atmost"); 1386cac9e8fdSSasha Levin 1387cac9e8fdSSasha Levin if (realpath(path, tmp) == 0 || 1388cac9e8fdSSasha Levin virtio_9p__register(kvm, tmp, "/dev/root") < 0) 1389cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1390cac9e8fdSSasha Levin if (virtio_9p__register(kvm, "/", "hostfs") < 0) 1391cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1392cac9e8fdSSasha Levin kvm_setup_resolv(arg); 1393cac9e8fdSSasha Levin kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 1394cac9e8fdSSasha Levin kvm->cfg.custom_rootfs_name = arg; 1395cac9e8fdSSasha Levin return 0; 1396cac9e8fdSSasha Levin } 1397cac9e8fdSSasha Levin 1398cac9e8fdSSasha Levin return -1; 1399cac9e8fdSSasha Levin } 1400cac9e8fdSSasha Levin 14011c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm) 14021c47ce69SSasha Levin { 14031c47ce69SSasha Levin struct p9_dev *p9dev; 14041c47ce69SSasha Levin 14051c47ce69SSasha Levin list_for_each_entry(p9dev, &devs, list) { 140602eca50cSAsias He virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops, 1407d97dadecSWill Deacon VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_9P, 1408ae06ce71SWill Deacon VIRTIO_ID_9P, PCI_CLASS_9P); 1409c7838fbdSSasha Levin } 1410c7838fbdSSasha Levin 1411c7838fbdSSasha Levin return 0; 1412c7838fbdSSasha Levin } 141349a8afd1SSasha Levin virtio_dev_init(virtio_9p__init); 1414c7838fbdSSasha Levin 1415c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name) 1416c7838fbdSSasha Levin { 1417c7838fbdSSasha Levin struct p9_dev *p9dev; 141854f6802dSPekka Enberg int err = 0; 14191c7850f9SSasha Levin 1420b4422bf3SAneesh Kumar K.V p9dev = calloc(1, sizeof(*p9dev)); 1421b4422bf3SAneesh Kumar K.V if (!p9dev) 142254f6802dSPekka Enberg return -ENOMEM; 142354f6802dSPekka Enberg 1424b4422bf3SAneesh Kumar K.V if (!tag_name) 14255529bcd7SAsias He tag_name = VIRTIO_9P_DEFAULT_TAG; 142654f6802dSPekka Enberg 1427b4422bf3SAneesh Kumar K.V p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1); 142854f6802dSPekka Enberg if (p9dev->config == NULL) { 142954f6802dSPekka Enberg err = -ENOMEM; 1430b4422bf3SAneesh Kumar K.V goto free_p9dev; 143154f6802dSPekka Enberg } 14321c7850f9SSasha Levin 1433b4422bf3SAneesh Kumar K.V strcpy(p9dev->root_dir, root); 1434b4422bf3SAneesh Kumar K.V p9dev->config->tag_len = strlen(tag_name); 143554f6802dSPekka Enberg if (p9dev->config->tag_len > MAX_TAG_LEN) { 143654f6802dSPekka Enberg err = -EINVAL; 1437b4422bf3SAneesh Kumar K.V goto free_p9dev_config; 143854f6802dSPekka Enberg } 14391c7850f9SSasha Levin 1440c7838fbdSSasha Levin memcpy(&p9dev->config->tag, tag_name, strlen(tag_name)); 14411c7850f9SSasha Levin 1442c7838fbdSSasha Levin list_add(&p9dev->list, &devs); 1443b4422bf3SAneesh Kumar K.V 1444d278197dSAsias He if (compat_id == -1) 144552f34d2cSAsias He compat_id = virtio_compat_add_message("virtio-9p", "CONFIG_NET_9P_VIRTIO"); 1446e59662b3SSasha Levin 144754f6802dSPekka Enberg return err; 144854f6802dSPekka Enberg 1449b4422bf3SAneesh Kumar K.V free_p9dev_config: 1450b4422bf3SAneesh Kumar K.V free(p9dev->config); 1451b4422bf3SAneesh Kumar K.V free_p9dev: 1452b4422bf3SAneesh Kumar K.V free(p9dev); 145354f6802dSPekka Enberg return err; 14541c7850f9SSasha Levin } 1455