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> 14*c0a98553SJeremy Linton #include <sys/sysmacros.h> 15bfc15268SAneesh Kumar K.V #include <unistd.h> 16bfc15268SAneesh Kumar K.V #include <string.h> 17bfc15268SAneesh Kumar K.V #include <errno.h> 18c797b6c6SAneesh Kumar K.V #include <sys/vfs.h> 191c7850f9SSasha Levin 202daa28d4SAneesh Kumar K.V #include <linux/virtio_ring.h> 212daa28d4SAneesh Kumar K.V #include <linux/virtio_9p.h> 22c6cb7c75SAndre Przywara #include <linux/9p.h> 232daa28d4SAneesh Kumar K.V 24c7838fbdSSasha Levin static LIST_HEAD(devs); 25312c62d1SSasha Levin static int compat_id = -1; 26c7838fbdSSasha Levin 27e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid); 28e2341580SSasha Levin static struct p9_fid *find_or_create_fid(struct p9_dev *dev, u32 fid) 29e2341580SSasha Levin { 30e2341580SSasha Levin struct rb_node *node = dev->fids.rb_node; 31e2341580SSasha Levin struct p9_fid *pfid = NULL; 32e277a1b4SG. Campana size_t len; 33e2341580SSasha Levin 34e2341580SSasha Levin while (node) { 35e2341580SSasha Levin struct p9_fid *cur = rb_entry(node, struct p9_fid, node); 36e2341580SSasha Levin 37e2341580SSasha Levin if (fid < cur->fid) { 38e2341580SSasha Levin node = node->rb_left; 39e2341580SSasha Levin } else if (fid > cur->fid) { 40e2341580SSasha Levin node = node->rb_right; 41e2341580SSasha Levin } else { 42e2341580SSasha Levin return cur; 43e2341580SSasha Levin } 44e2341580SSasha Levin } 45e2341580SSasha Levin 46e2341580SSasha Levin pfid = calloc(sizeof(*pfid), 1); 47e2341580SSasha Levin if (!pfid) 48e2341580SSasha Levin return NULL; 49e2341580SSasha Levin 50e277a1b4SG. Campana len = strlen(dev->root_dir); 51e277a1b4SG. Campana if (len >= sizeof(pfid->abs_path)) { 52e277a1b4SG. Campana free(pfid); 53e277a1b4SG. Campana return NULL; 54e277a1b4SG. Campana } 55e277a1b4SG. Campana 56e2341580SSasha Levin pfid->fid = fid; 57e2341580SSasha Levin strcpy(pfid->abs_path, dev->root_dir); 58e277a1b4SG. Campana pfid->path = pfid->abs_path + strlen(pfid->abs_path); 59e2341580SSasha Levin 60e2341580SSasha Levin insert_new_fid(dev, pfid); 61e2341580SSasha Levin 62e2341580SSasha Levin return pfid; 63e2341580SSasha Levin } 64e2341580SSasha Levin 65e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid) 66e2341580SSasha Levin { 67e2341580SSasha Levin struct rb_node **node = &(dev->fids.rb_node), *parent = NULL; 68e2341580SSasha Levin 69e2341580SSasha Levin while (*node) { 70e2341580SSasha Levin int result = fid->fid - rb_entry(*node, struct p9_fid, node)->fid; 71e2341580SSasha Levin 72e2341580SSasha Levin parent = *node; 73e2341580SSasha Levin if (result < 0) 74e2341580SSasha Levin node = &((*node)->rb_left); 75e2341580SSasha Levin else if (result > 0) 76e2341580SSasha Levin node = &((*node)->rb_right); 77e2341580SSasha Levin else 78e2341580SSasha Levin return -EEXIST; 79e2341580SSasha Levin } 80e2341580SSasha Levin 81e2341580SSasha Levin rb_link_node(&fid->node, parent, node); 82e2341580SSasha Levin rb_insert_color(&fid->node, &dev->fids); 83e2341580SSasha Levin return 0; 84e2341580SSasha Levin } 85e2341580SSasha Levin 8631a6fb8dSSasha Levin static struct p9_fid *get_fid(struct p9_dev *p9dev, int fid) 8731a6fb8dSSasha Levin { 88e2341580SSasha Levin struct p9_fid *new; 8931a6fb8dSSasha Levin 90e2341580SSasha Levin new = find_or_create_fid(p9dev, fid); 91e2341580SSasha Levin 92e2341580SSasha Levin return new; 9331a6fb8dSSasha Levin } 9431a6fb8dSSasha Levin 95c797b6c6SAneesh Kumar K.V static void stat2qid(struct stat *st, struct p9_qid *qid) 961c7850f9SSasha Levin { 971c7850f9SSasha Levin *qid = (struct p9_qid) { 981c7850f9SSasha Levin .path = st->st_ino, 991c7850f9SSasha Levin .version = st->st_mtime, 1001c7850f9SSasha Levin }; 1011c7850f9SSasha Levin 1021c7850f9SSasha Levin if (S_ISDIR(st->st_mode)) 1031c7850f9SSasha Levin qid->type |= P9_QTDIR; 1041c7850f9SSasha Levin } 1051c7850f9SSasha Levin 106b4422bf3SAneesh Kumar K.V static void close_fid(struct p9_dev *p9dev, u32 fid) 1071c7850f9SSasha Levin { 108e2341580SSasha Levin struct p9_fid *pfid = get_fid(p9dev, fid); 109e2341580SSasha Levin 110e2341580SSasha Levin if (pfid->fd > 0) 111e2341580SSasha Levin close(pfid->fd); 112e2341580SSasha Levin 113e2341580SSasha Levin if (pfid->dir) 114e2341580SSasha Levin closedir(pfid->dir); 115e2341580SSasha Levin 116e2341580SSasha Levin rb_erase(&pfid->node, &p9dev->fids); 117e2341580SSasha Levin free(pfid); 1181c7850f9SSasha Levin } 119e2341580SSasha Levin 120bfc15268SAneesh Kumar K.V static void virtio_p9_set_reply_header(struct p9_pdu *pdu, u32 size) 1211c7850f9SSasha Levin { 122bfc15268SAneesh Kumar K.V u8 cmd; 123bfc15268SAneesh Kumar K.V u16 tag; 124bfc15268SAneesh Kumar K.V 125bfc15268SAneesh Kumar K.V pdu->read_offset = sizeof(u32); 126bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "bw", &cmd, &tag); 127bfc15268SAneesh Kumar K.V pdu->write_offset = 0; 128bfc15268SAneesh Kumar K.V /* cmd + 1 is the reply message */ 129bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "dbw", size, cmd + 1, tag); 1301c7850f9SSasha Levin } 1311c7850f9SSasha Levin 1326b163a87SAneesh Kumar K.V static u16 virtio_p9_update_iov_cnt(struct iovec iov[], u32 count, int iov_cnt) 1336b163a87SAneesh Kumar K.V { 1346b163a87SAneesh Kumar K.V int i; 1356b163a87SAneesh Kumar K.V u32 total = 0; 1366b163a87SAneesh Kumar K.V for (i = 0; (i < iov_cnt) && (total < count); i++) { 1376b163a87SAneesh Kumar K.V if (total + iov[i].iov_len > count) { 1386b163a87SAneesh Kumar K.V /* we don't need this iov fully */ 1396b163a87SAneesh Kumar K.V iov[i].iov_len -= ((total + iov[i].iov_len) - count); 1406b163a87SAneesh Kumar K.V i++; 1416b163a87SAneesh Kumar K.V break; 1426b163a87SAneesh Kumar K.V } 1436b163a87SAneesh Kumar K.V total += iov[i].iov_len; 1446b163a87SAneesh Kumar K.V } 1456b163a87SAneesh Kumar K.V return i; 1466b163a87SAneesh Kumar K.V } 1476b163a87SAneesh Kumar K.V 148eee1ba8eSAneesh Kumar K.V static void virtio_p9_error_reply(struct p9_dev *p9dev, 149eee1ba8eSAneesh Kumar K.V struct p9_pdu *pdu, int err, u32 *outlen) 150eee1ba8eSAneesh Kumar K.V { 151bfc15268SAneesh Kumar K.V u16 tag; 152eee1ba8eSAneesh Kumar K.V 1539c2e1d1aSSuzuki K. Poulose /* EMFILE at server implies ENFILE for the VM */ 1549c2e1d1aSSuzuki K. Poulose if (err == EMFILE) 1559c2e1d1aSSuzuki K. Poulose err = ENFILE; 1569c2e1d1aSSuzuki K. Poulose 1575529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 158c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", err); 159bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 160eee1ba8eSAneesh Kumar K.V 161c797b6c6SAneesh Kumar K.V /* read the tag from input */ 162bfc15268SAneesh Kumar K.V pdu->read_offset = sizeof(u32) + sizeof(u8); 163bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "w", &tag); 164bfc15268SAneesh Kumar K.V 165c797b6c6SAneesh Kumar K.V /* Update the header */ 166bfc15268SAneesh Kumar K.V pdu->write_offset = 0; 167c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "dbw", *outlen, P9_RLERROR, tag); 168eee1ba8eSAneesh Kumar K.V } 169eee1ba8eSAneesh Kumar K.V 170ead43b01SAneesh Kumar K.V static void virtio_p9_version(struct p9_dev *p9dev, 171af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1721c7850f9SSasha Levin { 173c797b6c6SAneesh Kumar K.V u32 msize; 174c797b6c6SAneesh Kumar K.V char *version; 175c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ds", &msize, &version); 176c797b6c6SAneesh Kumar K.V /* 177c797b6c6SAneesh Kumar K.V * reply with the same msize the client sent us 178c797b6c6SAneesh Kumar K.V * Error out if the request is not for 9P2000.L 179c797b6c6SAneesh Kumar K.V */ 1805529bcd7SAsias He if (!strcmp(version, VIRTIO_9P_VERSION_DOTL)) 181c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ds", msize, version); 182c797b6c6SAneesh Kumar K.V else 183c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ds", msize, "unknown"); 1841c7850f9SSasha Levin 185bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 186bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 187c797b6c6SAneesh Kumar K.V free(version); 188ead43b01SAneesh Kumar K.V return; 1891c7850f9SSasha Levin } 1901c7850f9SSasha Levin 191ead43b01SAneesh Kumar K.V static void virtio_p9_clunk(struct p9_dev *p9dev, 192af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1931c7850f9SSasha Levin { 194bfc15268SAneesh Kumar K.V u32 fid; 1951c7850f9SSasha Levin 196bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid); 197bfc15268SAneesh Kumar K.V close_fid(p9dev, fid); 1981c7850f9SSasha Levin 199bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 200bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 201ead43b01SAneesh Kumar K.V return; 2021c7850f9SSasha Levin } 2031c7850f9SSasha Levin 204c797b6c6SAneesh Kumar K.V /* 205c797b6c6SAneesh Kumar K.V * FIXME!! Need to map to protocol independent value. Upstream 206c797b6c6SAneesh Kumar K.V * 9p also have the same BUG 207c797b6c6SAneesh Kumar K.V */ 208c797b6c6SAneesh Kumar K.V static int virtio_p9_openflags(int flags) 209c797b6c6SAneesh Kumar K.V { 210c797b6c6SAneesh Kumar K.V flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT | O_DIRECT); 211c797b6c6SAneesh Kumar K.V flags |= O_NOFOLLOW; 212c797b6c6SAneesh Kumar K.V return flags; 213c797b6c6SAneesh Kumar K.V } 214c797b6c6SAneesh Kumar K.V 21532585666SSasha Levin static bool is_dir(struct p9_fid *fid) 21632585666SSasha Levin { 21732585666SSasha Levin struct stat st; 21832585666SSasha Levin 21932585666SSasha Levin stat(fid->abs_path, &st); 22032585666SSasha Levin 22132585666SSasha Levin return S_ISDIR(st.st_mode); 22232585666SSasha Levin } 22332585666SSasha Levin 2249bb99a82SG. Campana /* path is always absolute */ 2259bb99a82SG. Campana static bool path_is_illegal(const char *path) 2269bb99a82SG. Campana { 2279bb99a82SG. Campana size_t len; 2289bb99a82SG. Campana 2299bb99a82SG. Campana if (strstr(path, "/../") != NULL) 2309bb99a82SG. Campana return true; 2319bb99a82SG. Campana 2329bb99a82SG. Campana len = strlen(path); 2339bb99a82SG. Campana if (len >= 3 && strcmp(path + len - 3, "/..") == 0) 2349bb99a82SG. Campana return true; 2359bb99a82SG. Campana 2369bb99a82SG. Campana return false; 2379bb99a82SG. Campana } 2389bb99a82SG. Campana 239d4727f2bSG. Campana static int get_full_path_helper(char *full_path, size_t size, 240d4727f2bSG. Campana const char *dirname, const char *name) 241d4727f2bSG. Campana { 242d4727f2bSG. Campana int ret; 243d4727f2bSG. Campana 244d4727f2bSG. Campana ret = snprintf(full_path, size, "%s/%s", dirname, name); 245716b2944SG. Campana if (ret >= (int)size) { 246d4727f2bSG. Campana errno = ENAMETOOLONG; 247d4727f2bSG. Campana return -1; 248d4727f2bSG. Campana } 249d4727f2bSG. Campana 250d4727f2bSG. Campana if (path_is_illegal(full_path)) { 251d4727f2bSG. Campana errno = EACCES; 252d4727f2bSG. Campana return -1; 253d4727f2bSG. Campana } 254d4727f2bSG. Campana 255d4727f2bSG. Campana return 0; 256d4727f2bSG. Campana } 257d4727f2bSG. Campana 258d4727f2bSG. Campana static int get_full_path(char *full_path, size_t size, struct p9_fid *fid, 259d4727f2bSG. Campana const char *name) 260d4727f2bSG. Campana { 261d4727f2bSG. Campana return get_full_path_helper(full_path, size, fid->abs_path, name); 262d4727f2bSG. Campana } 263d4727f2bSG. Campana 264b0922422SG. Campana static int stat_rel(struct p9_dev *p9dev, const char *path, struct stat *st) 265b0922422SG. Campana { 266b0922422SG. Campana char full_path[PATH_MAX]; 267b0922422SG. Campana 268b0922422SG. Campana if (get_full_path_helper(full_path, sizeof(full_path), p9dev->root_dir, path) != 0) 269b0922422SG. Campana return -1; 270b0922422SG. Campana 271b0922422SG. Campana if (lstat(full_path, st) != 0) 272b0922422SG. Campana return -1; 273b0922422SG. Campana 274b0922422SG. Campana return 0; 275b0922422SG. Campana } 276b0922422SG. Campana 277ead43b01SAneesh Kumar K.V static void virtio_p9_open(struct p9_dev *p9dev, 278af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 2791c7850f9SSasha Levin { 280c797b6c6SAneesh Kumar K.V u32 fid, flags; 2811c7850f9SSasha Levin struct stat st; 282bfc15268SAneesh Kumar K.V struct p9_qid qid; 283bfc15268SAneesh Kumar K.V struct p9_fid *new_fid; 284bfc15268SAneesh Kumar K.V 285c797b6c6SAneesh Kumar K.V 286c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dd", &fid, &flags); 28731a6fb8dSSasha Levin new_fid = get_fid(p9dev, fid); 2881c7850f9SSasha Levin 28930204a77SAneesh Kumar K.V if (lstat(new_fid->abs_path, &st) < 0) 290eee1ba8eSAneesh Kumar K.V goto err_out; 2911c7850f9SSasha Levin 292c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 2931c7850f9SSasha Levin 29432585666SSasha Levin if (is_dir(new_fid)) { 2951c7850f9SSasha Levin new_fid->dir = opendir(new_fid->abs_path); 296eee1ba8eSAneesh Kumar K.V if (!new_fid->dir) 297eee1ba8eSAneesh Kumar K.V goto err_out; 298eee1ba8eSAneesh Kumar K.V } else { 299eee1ba8eSAneesh Kumar K.V new_fid->fd = open(new_fid->abs_path, 300c797b6c6SAneesh Kumar K.V virtio_p9_openflags(flags)); 301eee1ba8eSAneesh Kumar K.V if (new_fid->fd < 0) 302eee1ba8eSAneesh Kumar K.V goto err_out; 303eee1ba8eSAneesh Kumar K.V } 304c797b6c6SAneesh Kumar K.V /* FIXME!! need ot send proper iounit */ 305bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 306bfc15268SAneesh Kumar K.V 307bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 308bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 309ead43b01SAneesh Kumar K.V return; 310eee1ba8eSAneesh Kumar K.V err_out: 311eee1ba8eSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 312ead43b01SAneesh Kumar K.V return; 3131c7850f9SSasha Levin } 3141c7850f9SSasha Levin 315ead43b01SAneesh Kumar K.V static void virtio_p9_create(struct p9_dev *p9dev, 316af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 3171c7850f9SSasha Levin { 318c797b6c6SAneesh Kumar K.V int fd, ret; 319bfc15268SAneesh Kumar K.V char *name; 32032832dd1SG. Campana size_t size; 321af045e53SAneesh Kumar K.V struct stat st; 322bfc15268SAneesh Kumar K.V struct p9_qid qid; 323c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 3244bc9734aSAneesh Kumar K.V char full_path[PATH_MAX]; 325c797b6c6SAneesh Kumar K.V u32 dfid_val, flags, mode, gid; 326af045e53SAneesh Kumar K.V 327c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsddd", &dfid_val, 328c797b6c6SAneesh Kumar K.V &name, &flags, &mode, &gid); 32931a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 3301c7850f9SSasha Levin 331d4727f2bSG. Campana if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0) 33232832dd1SG. Campana goto err_out; 3339bb99a82SG. Campana 33432832dd1SG. Campana size = sizeof(dfid->abs_path) - (dfid->path - dfid->abs_path); 33532832dd1SG. Campana ret = snprintf(dfid->path, size, "%s/%s", dfid->path, name); 33632832dd1SG. Campana if (ret >= (int)size) { 33732832dd1SG. Campana errno = ENAMETOOLONG; 33832832dd1SG. Campana if (size > 0) 33932832dd1SG. Campana dfid->path[size] = '\x00'; 34032832dd1SG. Campana goto err_out; 34132832dd1SG. Campana } 34232832dd1SG. Campana 343d4727f2bSG. Campana flags = virtio_p9_openflags(flags); 344d4727f2bSG. Campana 345c797b6c6SAneesh Kumar K.V fd = open(full_path, flags | O_CREAT, mode); 3464bc9734aSAneesh Kumar K.V if (fd < 0) 3474bc9734aSAneesh Kumar K.V goto err_out; 348c797b6c6SAneesh Kumar K.V dfid->fd = fd; 349c797b6c6SAneesh Kumar K.V 3504bc9734aSAneesh Kumar K.V if (lstat(full_path, &st) < 0) 3516c8ca053SAneesh Kumar K.V goto err_out; 3521c7850f9SSasha Levin 353c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 354c797b6c6SAneesh Kumar K.V if (ret < 0) 355c797b6c6SAneesh Kumar K.V goto err_out; 356c797b6c6SAneesh Kumar K.V 357c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 358bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 359bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 360bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 3615f900f6dSSasha Levin free(name); 3626c8ca053SAneesh Kumar K.V return; 3636c8ca053SAneesh Kumar K.V err_out: 3645f900f6dSSasha Levin free(name); 365c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 366c797b6c6SAneesh Kumar K.V return; 367c797b6c6SAneesh Kumar K.V } 368c797b6c6SAneesh Kumar K.V 369c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev, 370c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 371c797b6c6SAneesh Kumar K.V { 372c797b6c6SAneesh Kumar K.V int ret; 373c797b6c6SAneesh Kumar K.V char *name; 374c797b6c6SAneesh Kumar K.V struct stat st; 375c797b6c6SAneesh Kumar K.V struct p9_qid qid; 376c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 377c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 378c797b6c6SAneesh Kumar K.V u32 dfid_val, mode, gid; 379c797b6c6SAneesh Kumar K.V 380c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val, 381c797b6c6SAneesh Kumar K.V &name, &mode, &gid); 38231a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 383c797b6c6SAneesh Kumar K.V 384d4727f2bSG. Campana if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0) 38532832dd1SG. Campana goto err_out; 3869bb99a82SG. Campana 387c797b6c6SAneesh Kumar K.V ret = mkdir(full_path, mode); 388c797b6c6SAneesh Kumar K.V if (ret < 0) 389c797b6c6SAneesh Kumar K.V goto err_out; 390c797b6c6SAneesh Kumar K.V 391c797b6c6SAneesh Kumar K.V if (lstat(full_path, &st) < 0) 392c797b6c6SAneesh Kumar K.V goto err_out; 393c797b6c6SAneesh Kumar K.V 394c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 395c797b6c6SAneesh Kumar K.V if (ret < 0) 396c797b6c6SAneesh Kumar K.V goto err_out; 397c797b6c6SAneesh Kumar K.V 398c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 399c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 400c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 401c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 402c797b6c6SAneesh Kumar K.V free(name); 403c797b6c6SAneesh Kumar K.V return; 404c797b6c6SAneesh Kumar K.V err_out: 405c797b6c6SAneesh Kumar K.V free(name); 406c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 407ead43b01SAneesh Kumar K.V return; 4081c7850f9SSasha Levin } 4091c7850f9SSasha Levin 410e277a1b4SG. Campana static int join_path(struct p9_fid *fid, const char *name) 411e277a1b4SG. Campana { 412e277a1b4SG. Campana size_t len, size; 413e277a1b4SG. Campana 414e277a1b4SG. Campana size = sizeof(fid->abs_path) - (fid->path - fid->abs_path); 415e277a1b4SG. Campana len = strlen(name); 416e277a1b4SG. Campana if (len >= size) 417e277a1b4SG. Campana return -1; 418e277a1b4SG. Campana 419e277a1b4SG. Campana strncpy(fid->path, name, size); 420e277a1b4SG. Campana return 0; 421e277a1b4SG. Campana } 422e277a1b4SG. Campana 423ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev, 424af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 4251c7850f9SSasha Levin { 426af045e53SAneesh Kumar K.V u8 i; 427bfc15268SAneesh Kumar K.V u16 nwqid; 428bfc15268SAneesh Kumar K.V u16 nwname; 429bfc15268SAneesh Kumar K.V struct p9_qid wqid; 430e2341580SSasha Levin struct p9_fid *new_fid, *old_fid; 431c797b6c6SAneesh Kumar K.V u32 fid_val, newfid_val; 432c797b6c6SAneesh Kumar K.V 4331c7850f9SSasha Levin 434bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname); 43531a6fb8dSSasha Levin new_fid = get_fid(p9dev, newfid_val); 4361c7850f9SSasha Levin 437bfc15268SAneesh Kumar K.V nwqid = 0; 438bfc15268SAneesh Kumar K.V if (nwname) { 43931a6fb8dSSasha Levin struct p9_fid *fid = get_fid(p9dev, fid_val); 440bfc15268SAneesh Kumar K.V 441e277a1b4SG. Campana if (join_path(new_fid, fid->path) != 0) { 442e277a1b4SG. Campana errno = ENAMETOOLONG; 443e277a1b4SG. Campana goto err_out; 444e277a1b4SG. Campana } 445e277a1b4SG. Campana 446bfc15268SAneesh Kumar K.V /* skip the space for count */ 447bfc15268SAneesh Kumar K.V pdu->write_offset += sizeof(u16); 448bfc15268SAneesh Kumar K.V for (i = 0; i < nwname; i++) { 449bfc15268SAneesh Kumar K.V struct stat st; 4501c7850f9SSasha Levin char tmp[PATH_MAX] = {0}; 451e55ed135SPekka Enberg char *str; 45232832dd1SG. Campana int ret; 453bfc15268SAneesh Kumar K.V 454bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "s", &str); 4551c7850f9SSasha Levin 4561c7850f9SSasha Levin /* Format the new path we're 'walk'ing into */ 45732832dd1SG. Campana ret = snprintf(tmp, sizeof(tmp), "%s/%s", new_fid->path, str); 45832832dd1SG. Campana if (ret >= (int)sizeof(tmp)) { 45932832dd1SG. Campana errno = ENAMETOOLONG; 46032832dd1SG. Campana goto err_out; 46132832dd1SG. Campana } 462e55ed135SPekka Enberg 463e55ed135SPekka Enberg free(str); 464e55ed135SPekka Enberg 465b0922422SG. Campana if (stat_rel(p9dev, tmp, &st) != 0) 4666c8ca053SAneesh Kumar K.V goto err_out; 4671c7850f9SSasha Levin 468c797b6c6SAneesh Kumar K.V stat2qid(&st, &wqid); 469e277a1b4SG. Campana if (join_path(new_fid, tmp) != 0) { 470e277a1b4SG. Campana errno = ENAMETOOLONG; 471e277a1b4SG. Campana goto err_out; 472e277a1b4SG. Campana } 473c797b6c6SAneesh Kumar K.V new_fid->uid = fid->uid; 474bfc15268SAneesh Kumar K.V nwqid++; 475bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &wqid); 4761c7850f9SSasha Levin } 4771c7850f9SSasha Levin } else { 478bfc15268SAneesh Kumar K.V /* 479bfc15268SAneesh Kumar K.V * update write_offset so our outlen get correct value 480bfc15268SAneesh Kumar K.V */ 481bfc15268SAneesh Kumar K.V pdu->write_offset += sizeof(u16); 482e2341580SSasha Levin old_fid = get_fid(p9dev, fid_val); 483e277a1b4SG. Campana if (join_path(new_fid, old_fid->path) != 0) { 484e277a1b4SG. Campana errno = ENAMETOOLONG; 485e277a1b4SG. Campana goto err_out; 486e277a1b4SG. Campana } 487e2341580SSasha Levin new_fid->uid = old_fid->uid; 4881c7850f9SSasha Levin } 489bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 4905529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 491bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", nwqid); 492bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 4936c8ca053SAneesh Kumar K.V return; 4946c8ca053SAneesh Kumar K.V err_out: 4956c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 496ead43b01SAneesh Kumar K.V return; 4971c7850f9SSasha Levin } 4981c7850f9SSasha Levin 499ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev, 500af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 5011c7850f9SSasha Levin { 502bfc15268SAneesh Kumar K.V char *uname; 503bfc15268SAneesh Kumar K.V char *aname; 5041c7850f9SSasha Levin struct stat st; 505bfc15268SAneesh Kumar K.V struct p9_qid qid; 5061c7850f9SSasha Levin struct p9_fid *fid; 507c797b6c6SAneesh Kumar K.V u32 fid_val, afid, uid; 508bfc15268SAneesh Kumar K.V 509c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid, 510c797b6c6SAneesh Kumar K.V &uname, &aname, &uid); 5111c7850f9SSasha Levin 51239257180SPekka Enberg free(uname); 51339257180SPekka Enberg free(aname); 51439257180SPekka Enberg 51530204a77SAneesh Kumar K.V if (lstat(p9dev->root_dir, &st) < 0) 5166c8ca053SAneesh Kumar K.V goto err_out; 5171c7850f9SSasha Levin 518c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 5191c7850f9SSasha Levin 52031a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 521c797b6c6SAneesh Kumar K.V fid->uid = uid; 522e277a1b4SG. Campana if (join_path(fid, "/") != 0) { 523e277a1b4SG. Campana errno = ENAMETOOLONG; 524e277a1b4SG. Campana goto err_out; 525e277a1b4SG. Campana } 5261c7850f9SSasha Levin 527bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 528bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 529bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 5306c8ca053SAneesh Kumar K.V return; 5316c8ca053SAneesh Kumar K.V err_out: 5326c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 533ead43b01SAneesh Kumar K.V return; 5341c7850f9SSasha Levin } 5351c7850f9SSasha Levin 536c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev, 537c797b6c6SAneesh Kumar K.V struct stat *st, struct p9_stat_dotl *statl) 5385f900f6dSSasha Levin { 539c797b6c6SAneesh Kumar K.V memset(statl, 0, sizeof(*statl)); 540c797b6c6SAneesh Kumar K.V statl->st_mode = st->st_mode; 541c797b6c6SAneesh Kumar K.V statl->st_nlink = st->st_nlink; 542506fd90bSSasha Levin statl->st_uid = KUIDT_INIT(st->st_uid); 543506fd90bSSasha Levin statl->st_gid = KGIDT_INIT(st->st_gid); 544c797b6c6SAneesh Kumar K.V statl->st_rdev = st->st_rdev; 545c797b6c6SAneesh Kumar K.V statl->st_size = st->st_size; 546c797b6c6SAneesh Kumar K.V statl->st_blksize = st->st_blksize; 547c797b6c6SAneesh Kumar K.V statl->st_blocks = st->st_blocks; 548c797b6c6SAneesh Kumar K.V statl->st_atime_sec = st->st_atime; 549c797b6c6SAneesh Kumar K.V statl->st_atime_nsec = st->st_atim.tv_nsec; 550c797b6c6SAneesh Kumar K.V statl->st_mtime_sec = st->st_mtime; 551c797b6c6SAneesh Kumar K.V statl->st_mtime_nsec = st->st_mtim.tv_nsec; 552c797b6c6SAneesh Kumar K.V statl->st_ctime_sec = st->st_ctime; 553c797b6c6SAneesh Kumar K.V statl->st_ctime_nsec = st->st_ctim.tv_nsec; 554c797b6c6SAneesh Kumar K.V /* Currently we only support BASIC fields in stat */ 555c797b6c6SAneesh Kumar K.V statl->st_result_mask = P9_STATS_BASIC; 556c797b6c6SAneesh Kumar K.V stat2qid(st, &statl->qid); 5571c7850f9SSasha Levin } 5581c7850f9SSasha Levin 559ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev, 560af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 5611c7850f9SSasha Levin { 562bfc15268SAneesh Kumar K.V u64 offset; 563bfc15268SAneesh Kumar K.V u32 fid_val; 564c797b6c6SAneesh Kumar K.V u16 iov_cnt; 565c797b6c6SAneesh Kumar K.V void *iov_base; 566c797b6c6SAneesh Kumar K.V size_t iov_len; 567bfc15268SAneesh Kumar K.V u32 count, rcount; 568bfc15268SAneesh Kumar K.V struct p9_fid *fid; 569c797b6c6SAneesh Kumar K.V 5701c7850f9SSasha Levin 571bfc15268SAneesh Kumar K.V rcount = 0; 572bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 57331a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 57450c479e0SAneesh Kumar K.V 57550c479e0SAneesh Kumar K.V iov_base = pdu->in_iov[0].iov_base; 57650c479e0SAneesh Kumar K.V iov_len = pdu->in_iov[0].iov_len; 57750c479e0SAneesh Kumar K.V iov_cnt = pdu->in_iov_cnt; 5785529bcd7SAsias He pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32); 5795529bcd7SAsias He pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32); 5806b163a87SAneesh Kumar K.V pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov, 581bfc15268SAneesh Kumar K.V count, 5826b163a87SAneesh Kumar K.V pdu->in_iov_cnt); 583bfc15268SAneesh Kumar K.V rcount = preadv(fid->fd, pdu->in_iov, 584bfc15268SAneesh Kumar K.V pdu->in_iov_cnt, offset); 585bfc15268SAneesh Kumar K.V if (rcount > count) 586bfc15268SAneesh Kumar K.V rcount = count; 587bfc15268SAneesh Kumar K.V /* 588bfc15268SAneesh Kumar K.V * Update the iov_base back, so that rest of 589bfc15268SAneesh Kumar K.V * pdu_writef works correctly. 590bfc15268SAneesh Kumar K.V */ 59150c479e0SAneesh Kumar K.V pdu->in_iov[0].iov_base = iov_base; 59250c479e0SAneesh Kumar K.V pdu->in_iov[0].iov_len = iov_len; 59350c479e0SAneesh Kumar K.V pdu->in_iov_cnt = iov_cnt; 594c797b6c6SAneesh Kumar K.V 5955529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 596bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", rcount); 597bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset + rcount; 598bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 599ead43b01SAneesh Kumar K.V return; 6001c7850f9SSasha Levin } 6011c7850f9SSasha Levin 602c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent) 603c797b6c6SAneesh Kumar K.V { 604c797b6c6SAneesh Kumar K.V /* 605c797b6c6SAneesh Kumar K.V * Size of each dirent: 606c797b6c6SAneesh Kumar K.V * qid(13) + offset(8) + type(1) + name_len(2) + name 607c797b6c6SAneesh Kumar K.V */ 608c797b6c6SAneesh Kumar K.V return 24 + strlen(dent->d_name); 609c797b6c6SAneesh Kumar K.V } 610c797b6c6SAneesh Kumar K.V 611c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev, 612c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 613c797b6c6SAneesh Kumar K.V { 614c797b6c6SAneesh Kumar K.V u32 fid_val; 615c797b6c6SAneesh Kumar K.V u32 count, rcount; 616c797b6c6SAneesh Kumar K.V struct stat st; 617c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 618c797b6c6SAneesh Kumar K.V struct dirent *dent; 619c797b6c6SAneesh Kumar K.V u64 offset, old_offset; 620c797b6c6SAneesh Kumar K.V 621c797b6c6SAneesh Kumar K.V rcount = 0; 622c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 62331a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 624c797b6c6SAneesh Kumar K.V 62532585666SSasha Levin if (!is_dir(fid)) { 62669bb4278SSasha Levin errno = EINVAL; 627c797b6c6SAneesh Kumar K.V goto err_out; 628c797b6c6SAneesh Kumar K.V } 629c797b6c6SAneesh Kumar K.V 630c797b6c6SAneesh Kumar K.V /* Move the offset specified */ 631c797b6c6SAneesh Kumar K.V seekdir(fid->dir, offset); 632c797b6c6SAneesh Kumar K.V 633c797b6c6SAneesh Kumar K.V old_offset = offset; 634c797b6c6SAneesh Kumar K.V /* If reading a dir, fill the buffer with p9_stat entries */ 635c797b6c6SAneesh Kumar K.V dent = readdir(fid->dir); 636c797b6c6SAneesh Kumar K.V 637c797b6c6SAneesh Kumar K.V /* Skip the space for writing count */ 638c797b6c6SAneesh Kumar K.V pdu->write_offset += sizeof(u32); 639c797b6c6SAneesh Kumar K.V while (dent) { 640c797b6c6SAneesh Kumar K.V u32 read; 641c797b6c6SAneesh Kumar K.V struct p9_qid qid; 642c797b6c6SAneesh Kumar K.V 643c797b6c6SAneesh Kumar K.V if ((rcount + virtio_p9_dentry_size(dent)) > count) { 644c797b6c6SAneesh Kumar K.V /* seek to the previous offset and return */ 645c797b6c6SAneesh Kumar K.V seekdir(fid->dir, old_offset); 646c797b6c6SAneesh Kumar K.V break; 647c797b6c6SAneesh Kumar K.V } 648c797b6c6SAneesh Kumar K.V old_offset = dent->d_off; 649b0922422SG. Campana if (stat_rel(p9dev, dent->d_name, &st) != 0) 650b0922422SG. Campana memset(&st, -1, sizeof(st)); 651c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 652c797b6c6SAneesh Kumar K.V read = pdu->write_offset; 653c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off, 654c797b6c6SAneesh Kumar K.V dent->d_type, dent->d_name); 655c797b6c6SAneesh Kumar K.V rcount += pdu->write_offset - read; 656c797b6c6SAneesh Kumar K.V dent = readdir(fid->dir); 657c797b6c6SAneesh Kumar K.V } 658c797b6c6SAneesh Kumar K.V 6595529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 660c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", rcount); 661c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset + rcount; 662c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 663c797b6c6SAneesh Kumar K.V return; 664c797b6c6SAneesh Kumar K.V err_out: 665c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 666c797b6c6SAneesh Kumar K.V return; 667c797b6c6SAneesh Kumar K.V } 668c797b6c6SAneesh Kumar K.V 669c797b6c6SAneesh Kumar K.V 670c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev, 671af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 6721c7850f9SSasha Levin { 673bfc15268SAneesh Kumar K.V u32 fid_val; 674af045e53SAneesh Kumar K.V struct stat st; 675c797b6c6SAneesh Kumar K.V u64 request_mask; 676bfc15268SAneesh Kumar K.V struct p9_fid *fid; 677c797b6c6SAneesh Kumar K.V struct p9_stat_dotl statl; 6781c7850f9SSasha Levin 679c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask); 68031a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 68130204a77SAneesh Kumar K.V if (lstat(fid->abs_path, &st) < 0) 6826c8ca053SAneesh Kumar K.V goto err_out; 6831c7850f9SSasha Levin 684c797b6c6SAneesh Kumar K.V virtio_p9_fill_stat(p9dev, &st, &statl); 685c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "A", &statl); 686bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 687bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 688ead43b01SAneesh Kumar K.V return; 6896c8ca053SAneesh Kumar K.V err_out: 6906c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 6916c8ca053SAneesh Kumar K.V return; 6921c7850f9SSasha Levin } 6931c7850f9SSasha Levin 694c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */ 695c797b6c6SAneesh Kumar K.V /* 696c797b6c6SAneesh Kumar K.V * Attribute flags. These should be or-ed together to figure out what 697c797b6c6SAneesh Kumar K.V * has been changed! 698c797b6c6SAneesh Kumar K.V */ 699c797b6c6SAneesh Kumar K.V #define ATTR_MODE (1 << 0) 700c797b6c6SAneesh Kumar K.V #define ATTR_UID (1 << 1) 701c797b6c6SAneesh Kumar K.V #define ATTR_GID (1 << 2) 702c797b6c6SAneesh Kumar K.V #define ATTR_SIZE (1 << 3) 703c797b6c6SAneesh Kumar K.V #define ATTR_ATIME (1 << 4) 704c797b6c6SAneesh Kumar K.V #define ATTR_MTIME (1 << 5) 705c797b6c6SAneesh Kumar K.V #define ATTR_CTIME (1 << 6) 706c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET (1 << 7) 707c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET (1 << 8) 708c797b6c6SAneesh Kumar K.V #define ATTR_FORCE (1 << 9) /* Not a change, but a change it */ 709c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG (1 << 10) 710c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID (1 << 11) 711c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID (1 << 12) 712c797b6c6SAneesh Kumar K.V #define ATTR_FILE (1 << 13) 713c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV (1 << 14) 714c797b6c6SAneesh Kumar K.V #define ATTR_OPEN (1 << 15) /* Truncating from open(O_TRUNC) */ 715c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET (1 << 16) 716c797b6c6SAneesh Kumar K.V 717c797b6c6SAneesh Kumar K.V #define ATTR_MASK 127 718c797b6c6SAneesh Kumar K.V 719c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev, 720af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 7211c7850f9SSasha Levin { 722c797b6c6SAneesh Kumar K.V int ret = 0; 723bfc15268SAneesh Kumar K.V u32 fid_val; 724bfc15268SAneesh Kumar K.V struct p9_fid *fid; 725c797b6c6SAneesh Kumar K.V struct p9_iattr_dotl p9attr; 7261c7850f9SSasha Levin 727c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr); 72831a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 7291c7850f9SSasha Levin 730c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MODE) { 731c797b6c6SAneesh Kumar K.V ret = chmod(fid->abs_path, p9attr.mode); 732c797b6c6SAneesh Kumar K.V if (ret < 0) 733c797b6c6SAneesh Kumar K.V goto err_out; 734c797b6c6SAneesh Kumar K.V } 735c797b6c6SAneesh Kumar K.V if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) { 736c797b6c6SAneesh Kumar K.V struct timespec times[2]; 737c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_ATIME) { 738c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_ATIME_SET) { 739c797b6c6SAneesh Kumar K.V times[0].tv_sec = p9attr.atime_sec; 740c797b6c6SAneesh Kumar K.V times[0].tv_nsec = p9attr.atime_nsec; 741c797b6c6SAneesh Kumar K.V } else { 742c797b6c6SAneesh Kumar K.V times[0].tv_nsec = UTIME_NOW; 743c797b6c6SAneesh Kumar K.V } 744c797b6c6SAneesh Kumar K.V } else { 745c797b6c6SAneesh Kumar K.V times[0].tv_nsec = UTIME_OMIT; 746c797b6c6SAneesh Kumar K.V } 747c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MTIME) { 748c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MTIME_SET) { 749c797b6c6SAneesh Kumar K.V times[1].tv_sec = p9attr.mtime_sec; 750c797b6c6SAneesh Kumar K.V times[1].tv_nsec = p9attr.mtime_nsec; 751c797b6c6SAneesh Kumar K.V } else { 752c797b6c6SAneesh Kumar K.V times[1].tv_nsec = UTIME_NOW; 753c797b6c6SAneesh Kumar K.V } 754c797b6c6SAneesh Kumar K.V } else 755c797b6c6SAneesh Kumar K.V times[1].tv_nsec = UTIME_OMIT; 756c797b6c6SAneesh Kumar K.V 757c797b6c6SAneesh Kumar K.V ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW); 758c797b6c6SAneesh Kumar K.V if (ret < 0) 759c797b6c6SAneesh Kumar K.V goto err_out; 760c797b6c6SAneesh Kumar K.V } 761c797b6c6SAneesh Kumar K.V /* 762c797b6c6SAneesh Kumar K.V * If the only valid entry in iattr is ctime we can call 763c797b6c6SAneesh Kumar K.V * chown(-1,-1) to update the ctime of the file 764c797b6c6SAneesh Kumar K.V */ 765c797b6c6SAneesh Kumar K.V if ((p9attr.valid & (ATTR_UID | ATTR_GID)) || 766c797b6c6SAneesh Kumar K.V ((p9attr.valid & ATTR_CTIME) 767c797b6c6SAneesh Kumar K.V && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) { 768c797b6c6SAneesh Kumar K.V if (!(p9attr.valid & ATTR_UID)) 769506fd90bSSasha Levin p9attr.uid = KUIDT_INIT(-1); 770c797b6c6SAneesh Kumar K.V 771c797b6c6SAneesh Kumar K.V if (!(p9attr.valid & ATTR_GID)) 772506fd90bSSasha Levin p9attr.gid = KGIDT_INIT(-1); 773c797b6c6SAneesh Kumar K.V 774506fd90bSSasha Levin ret = lchown(fid->abs_path, __kuid_val(p9attr.uid), 775506fd90bSSasha Levin __kgid_val(p9attr.gid)); 776c797b6c6SAneesh Kumar K.V if (ret < 0) 777c797b6c6SAneesh Kumar K.V goto err_out; 778c797b6c6SAneesh Kumar K.V } 779c797b6c6SAneesh Kumar K.V if (p9attr.valid & (ATTR_SIZE)) { 780c797b6c6SAneesh Kumar K.V ret = truncate(fid->abs_path, p9attr.size); 781c797b6c6SAneesh Kumar K.V if (ret < 0) 782c797b6c6SAneesh Kumar K.V goto err_out; 783c797b6c6SAneesh Kumar K.V } 7845529bcd7SAsias He *outlen = VIRTIO_9P_HDR_LEN; 785bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 786ead43b01SAneesh Kumar K.V return; 7874bc9734aSAneesh Kumar K.V err_out: 7884bc9734aSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 7894bc9734aSAneesh Kumar K.V return; 7901c7850f9SSasha Levin } 7911c7850f9SSasha Levin 792ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev, 793af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 7941c7850f9SSasha Levin { 7954bc9734aSAneesh Kumar K.V 796bfc15268SAneesh Kumar K.V u64 offset; 797bfc15268SAneesh Kumar K.V u32 fid_val; 7984bc9734aSAneesh Kumar K.V u32 count; 7994bc9734aSAneesh Kumar K.V ssize_t res; 80050c479e0SAneesh Kumar K.V u16 iov_cnt; 80150c479e0SAneesh Kumar K.V void *iov_base; 80250c479e0SAneesh Kumar K.V size_t iov_len; 803bfc15268SAneesh Kumar K.V struct p9_fid *fid; 804b064b05aSAneesh Kumar K.V /* u32 fid + u64 offset + u32 count */ 805b064b05aSAneesh Kumar K.V int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32); 8061c7850f9SSasha Levin 807bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 80831a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 809af045e53SAneesh Kumar K.V 81050c479e0SAneesh Kumar K.V iov_base = pdu->out_iov[0].iov_base; 81150c479e0SAneesh Kumar K.V iov_len = pdu->out_iov[0].iov_len; 81250c479e0SAneesh Kumar K.V iov_cnt = pdu->out_iov_cnt; 81350c479e0SAneesh Kumar K.V 814bfc15268SAneesh Kumar K.V /* Adjust the iovec to skip the header and meta data */ 815b064b05aSAneesh Kumar K.V pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size); 816b064b05aSAneesh Kumar K.V pdu->out_iov[0].iov_len -= (sizeof(struct p9_msg) + twrite_size); 817bfc15268SAneesh Kumar K.V pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count, 8186b163a87SAneesh Kumar K.V pdu->out_iov_cnt); 8194bc9734aSAneesh Kumar K.V res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset); 82050c479e0SAneesh Kumar K.V /* 82150c479e0SAneesh Kumar K.V * Update the iov_base back, so that rest of 82250c479e0SAneesh Kumar K.V * pdu_readf works correctly. 82350c479e0SAneesh Kumar K.V */ 82450c479e0SAneesh Kumar K.V pdu->out_iov[0].iov_base = iov_base; 82550c479e0SAneesh Kumar K.V pdu->out_iov[0].iov_len = iov_len; 82650c479e0SAneesh Kumar K.V pdu->out_iov_cnt = iov_cnt; 827c797b6c6SAneesh Kumar K.V 8284bc9734aSAneesh Kumar K.V if (res < 0) 8294bc9734aSAneesh Kumar K.V goto err_out; 8304bc9734aSAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", res); 831bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 832bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 833ead43b01SAneesh Kumar K.V return; 8344bc9734aSAneesh Kumar K.V err_out: 8354bc9734aSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 8364bc9734aSAneesh Kumar K.V return; 8371c7850f9SSasha Levin } 8381c7850f9SSasha Levin 8396fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev, 8406fc5cd9bSSasha Levin struct p9_pdu *pdu, u32 *outlen) 8416fc5cd9bSSasha Levin { 8426fc5cd9bSSasha Levin int ret; 8436fc5cd9bSSasha Levin u32 fid_val; 8446fc5cd9bSSasha Levin struct p9_fid *fid; 8456fc5cd9bSSasha Levin 8466fc5cd9bSSasha Levin virtio_p9_pdu_readf(pdu, "d", &fid_val); 84731a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 8486fc5cd9bSSasha Levin 8499b604a9cSSasha Levin ret = remove(fid->abs_path); 8506fc5cd9bSSasha Levin if (ret < 0) 8516fc5cd9bSSasha Levin goto err_out; 8526fc5cd9bSSasha Levin *outlen = pdu->write_offset; 8536fc5cd9bSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 8546fc5cd9bSSasha Levin return; 8556fc5cd9bSSasha Levin 8566fc5cd9bSSasha Levin err_out: 8576fc5cd9bSSasha Levin virtio_p9_error_reply(p9dev, pdu, errno, outlen); 8586fc5cd9bSSasha Levin return; 8596fc5cd9bSSasha Levin } 8606fc5cd9bSSasha Levin 861f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev, 862f161f28bSSasha Levin struct p9_pdu *pdu, u32 *outlen) 863f161f28bSSasha Levin { 864f161f28bSSasha Levin int ret; 865f161f28bSSasha Levin u32 fid_val, new_fid_val; 866f161f28bSSasha Levin struct p9_fid *fid, *new_fid; 867f161f28bSSasha Levin char full_path[PATH_MAX], *new_name; 868f161f28bSSasha Levin 869f161f28bSSasha Levin virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name); 87031a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 87131a6fb8dSSasha Levin new_fid = get_fid(p9dev, new_fid_val); 872f161f28bSSasha Levin 873d4727f2bSG. Campana if (get_full_path(full_path, sizeof(full_path), new_fid, new_name) != 0) 87432832dd1SG. Campana goto err_out; 8759bb99a82SG. Campana 876f161f28bSSasha Levin ret = rename(fid->abs_path, full_path); 877f161f28bSSasha Levin if (ret < 0) 878f161f28bSSasha Levin goto err_out; 879f161f28bSSasha Levin *outlen = pdu->write_offset; 880f161f28bSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 881f161f28bSSasha Levin return; 882f161f28bSSasha Levin 883f161f28bSSasha Levin err_out: 884f161f28bSSasha Levin virtio_p9_error_reply(p9dev, pdu, errno, outlen); 885f161f28bSSasha Levin return; 886f161f28bSSasha Levin } 887f161f28bSSasha Levin 888c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev, 889c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 890c797b6c6SAneesh Kumar K.V { 891c797b6c6SAneesh Kumar K.V int ret; 892c797b6c6SAneesh Kumar K.V u32 fid_val; 893c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 894c797b6c6SAneesh Kumar K.V char target_path[PATH_MAX]; 895c797b6c6SAneesh Kumar K.V 896c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid_val); 89731a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 898c797b6c6SAneesh Kumar K.V 899c797b6c6SAneesh Kumar K.V memset(target_path, 0, PATH_MAX); 900c797b6c6SAneesh Kumar K.V ret = readlink(fid->abs_path, target_path, PATH_MAX - 1); 901c797b6c6SAneesh Kumar K.V if (ret < 0) 902c797b6c6SAneesh Kumar K.V goto err_out; 903c797b6c6SAneesh Kumar K.V 904c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "s", target_path); 905c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 906c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 907c797b6c6SAneesh Kumar K.V return; 908c797b6c6SAneesh Kumar K.V err_out: 909c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 910c797b6c6SAneesh Kumar K.V return; 911c797b6c6SAneesh Kumar K.V } 912c797b6c6SAneesh Kumar K.V 913c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev, 914c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 915c797b6c6SAneesh Kumar K.V { 916c797b6c6SAneesh Kumar K.V int ret; 917c797b6c6SAneesh Kumar K.V u64 fsid; 918c797b6c6SAneesh Kumar K.V u32 fid_val; 919c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 920c797b6c6SAneesh Kumar K.V struct statfs stat_buf; 921c797b6c6SAneesh Kumar K.V 922c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid_val); 92331a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 924c797b6c6SAneesh Kumar K.V 925c797b6c6SAneesh Kumar K.V ret = statfs(fid->abs_path, &stat_buf); 926c797b6c6SAneesh Kumar K.V if (ret < 0) 927c797b6c6SAneesh Kumar K.V goto err_out; 928c797b6c6SAneesh Kumar K.V /* FIXME!! f_blocks needs update based on client msize */ 929c797b6c6SAneesh Kumar K.V fsid = (unsigned int) stat_buf.f_fsid.__val[0] | 930c797b6c6SAneesh Kumar K.V (unsigned long long)stat_buf.f_fsid.__val[1] << 32; 931c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type, 932c797b6c6SAneesh Kumar K.V stat_buf.f_bsize, stat_buf.f_blocks, 933c797b6c6SAneesh Kumar K.V stat_buf.f_bfree, stat_buf.f_bavail, 934c797b6c6SAneesh Kumar K.V stat_buf.f_files, stat_buf.f_ffree, 935c797b6c6SAneesh Kumar K.V fsid, stat_buf.f_namelen); 936c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 937c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 938c797b6c6SAneesh Kumar K.V return; 939c797b6c6SAneesh Kumar K.V err_out: 940c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 941c797b6c6SAneesh Kumar K.V return; 942c797b6c6SAneesh Kumar K.V } 943c797b6c6SAneesh Kumar K.V 944c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev, 945c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 946c797b6c6SAneesh Kumar K.V { 947c797b6c6SAneesh Kumar K.V int ret; 948c797b6c6SAneesh Kumar K.V char *name; 949c797b6c6SAneesh Kumar K.V struct stat st; 950c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 951c797b6c6SAneesh Kumar K.V struct p9_qid qid; 952c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 953c797b6c6SAneesh Kumar K.V u32 fid_val, mode, major, minor, gid; 954c797b6c6SAneesh Kumar K.V 955c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode, 956c797b6c6SAneesh Kumar K.V &major, &minor, &gid); 957c797b6c6SAneesh Kumar K.V 95831a6fb8dSSasha Levin dfid = get_fid(p9dev, fid_val); 95932832dd1SG. Campana 960d4727f2bSG. Campana if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0) 9619bb99a82SG. Campana goto err_out; 9629bb99a82SG. Campana 963c797b6c6SAneesh Kumar K.V ret = mknod(full_path, mode, makedev(major, minor)); 964c797b6c6SAneesh Kumar K.V if (ret < 0) 965c797b6c6SAneesh Kumar K.V goto err_out; 966c797b6c6SAneesh Kumar K.V 967c797b6c6SAneesh Kumar K.V if (lstat(full_path, &st) < 0) 968c797b6c6SAneesh Kumar K.V goto err_out; 969c797b6c6SAneesh Kumar K.V 970c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 971c797b6c6SAneesh Kumar K.V if (ret < 0) 972c797b6c6SAneesh Kumar K.V goto err_out; 973c797b6c6SAneesh Kumar K.V 974c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 975c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 976c797b6c6SAneesh Kumar K.V free(name); 977c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 978c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 979c797b6c6SAneesh Kumar K.V return; 980c797b6c6SAneesh Kumar K.V err_out: 981c797b6c6SAneesh Kumar K.V free(name); 982c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 983c797b6c6SAneesh Kumar K.V return; 984c797b6c6SAneesh Kumar K.V } 985c797b6c6SAneesh Kumar K.V 986c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev, 987c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 988c797b6c6SAneesh Kumar K.V { 989644140efSRussell King int ret, fd; 990c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 991c797b6c6SAneesh Kumar K.V u32 fid_val, datasync; 992c797b6c6SAneesh Kumar K.V 993c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync); 99431a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 995c797b6c6SAneesh Kumar K.V 996644140efSRussell King if (fid->dir) 997644140efSRussell King fd = dirfd(fid->dir); 998c797b6c6SAneesh Kumar K.V else 999644140efSRussell King fd = fid->fd; 1000644140efSRussell King 1001644140efSRussell King if (datasync) 1002644140efSRussell King ret = fdatasync(fd); 1003644140efSRussell King else 1004644140efSRussell King ret = fsync(fd); 1005c797b6c6SAneesh Kumar K.V if (ret < 0) 1006c797b6c6SAneesh Kumar K.V goto err_out; 1007c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1008c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1009c797b6c6SAneesh Kumar K.V return; 1010c797b6c6SAneesh Kumar K.V err_out: 1011c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1012c797b6c6SAneesh Kumar K.V return; 1013c797b6c6SAneesh Kumar K.V } 1014c797b6c6SAneesh Kumar K.V 1015c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev, 1016c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1017c797b6c6SAneesh Kumar K.V { 1018c797b6c6SAneesh Kumar K.V int ret; 1019c797b6c6SAneesh Kumar K.V struct stat st; 1020c797b6c6SAneesh Kumar K.V u32 fid_val, gid; 1021c797b6c6SAneesh Kumar K.V struct p9_qid qid; 1022c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 1023c797b6c6SAneesh Kumar K.V char new_name[PATH_MAX]; 1024c797b6c6SAneesh Kumar K.V char *old_path, *name; 1025c797b6c6SAneesh Kumar K.V 1026c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid); 1027c797b6c6SAneesh Kumar K.V 102831a6fb8dSSasha Levin dfid = get_fid(p9dev, fid_val); 102932832dd1SG. Campana 1030d4727f2bSG. Campana if (get_full_path(new_name, sizeof(new_name), dfid, name) != 0) 10319bb99a82SG. Campana goto err_out; 10329bb99a82SG. Campana 1033c797b6c6SAneesh Kumar K.V ret = symlink(old_path, new_name); 1034c797b6c6SAneesh Kumar K.V if (ret < 0) 1035c797b6c6SAneesh Kumar K.V goto err_out; 1036c797b6c6SAneesh Kumar K.V 1037c797b6c6SAneesh Kumar K.V if (lstat(new_name, &st) < 0) 1038c797b6c6SAneesh Kumar K.V goto err_out; 1039c797b6c6SAneesh Kumar K.V 1040c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 1041c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 1042c797b6c6SAneesh Kumar K.V free(name); 1043c797b6c6SAneesh Kumar K.V free(old_path); 1044c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1045c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1046c797b6c6SAneesh Kumar K.V return; 1047c797b6c6SAneesh Kumar K.V err_out: 1048c797b6c6SAneesh Kumar K.V free(name); 1049c797b6c6SAneesh Kumar K.V free(old_path); 1050c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1051c797b6c6SAneesh Kumar K.V return; 1052c797b6c6SAneesh Kumar K.V } 1053c797b6c6SAneesh Kumar K.V 1054c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev, 1055c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1056c797b6c6SAneesh Kumar K.V { 1057c797b6c6SAneesh Kumar K.V int ret; 1058c797b6c6SAneesh Kumar K.V char *name; 1059c797b6c6SAneesh Kumar K.V u32 fid_val, dfid_val; 1060c797b6c6SAneesh Kumar K.V struct p9_fid *dfid, *fid; 1061c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 1062c797b6c6SAneesh Kumar K.V 1063c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name); 1064c797b6c6SAneesh Kumar K.V 106531a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 106631a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 106732832dd1SG. Campana 1068d4727f2bSG. Campana if (get_full_path(full_path, sizeof(full_path), dfid, name) != 0) 10699bb99a82SG. Campana goto err_out; 10709bb99a82SG. Campana 1071c797b6c6SAneesh Kumar K.V ret = link(fid->abs_path, full_path); 1072c797b6c6SAneesh Kumar K.V if (ret < 0) 1073c797b6c6SAneesh Kumar K.V goto err_out; 1074c797b6c6SAneesh Kumar K.V free(name); 1075c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1076c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1077c797b6c6SAneesh Kumar K.V return; 1078c797b6c6SAneesh Kumar K.V err_out: 1079c797b6c6SAneesh Kumar K.V free(name); 1080c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1081c797b6c6SAneesh Kumar K.V return; 1082c797b6c6SAneesh Kumar K.V 1083c797b6c6SAneesh Kumar K.V } 1084c797b6c6SAneesh Kumar K.V 1085c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev, 1086c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1087c797b6c6SAneesh Kumar K.V { 1088c797b6c6SAneesh Kumar K.V u8 ret; 1089c797b6c6SAneesh Kumar K.V u32 fid_val; 1090c797b6c6SAneesh Kumar K.V struct p9_flock flock; 1091c797b6c6SAneesh Kumar K.V 1092c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type, 1093c797b6c6SAneesh Kumar K.V &flock.flags, &flock.start, &flock.length, 1094c797b6c6SAneesh Kumar K.V &flock.proc_id, &flock.client_id); 1095c797b6c6SAneesh Kumar K.V 1096c797b6c6SAneesh Kumar K.V /* Just return success */ 1097c797b6c6SAneesh Kumar K.V ret = P9_LOCK_SUCCESS; 1098c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", ret); 1099c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1100c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1101c797b6c6SAneesh Kumar K.V free(flock.client_id); 1102c797b6c6SAneesh Kumar K.V return; 1103c797b6c6SAneesh Kumar K.V } 1104c797b6c6SAneesh Kumar K.V 1105c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev, 1106c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1107c797b6c6SAneesh Kumar K.V { 1108c797b6c6SAneesh Kumar K.V u32 fid_val; 1109c797b6c6SAneesh Kumar K.V struct p9_getlock glock; 1110c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type, 1111c797b6c6SAneesh Kumar K.V &glock.start, &glock.length, &glock.proc_id, 1112c797b6c6SAneesh Kumar K.V &glock.client_id); 1113c797b6c6SAneesh Kumar K.V 1114c797b6c6SAneesh Kumar K.V /* Just return success */ 1115c797b6c6SAneesh Kumar K.V glock.type = F_UNLCK; 1116c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "bqqds", glock.type, 1117c797b6c6SAneesh Kumar K.V glock.start, glock.length, glock.proc_id, 1118c797b6c6SAneesh Kumar K.V glock.client_id); 1119c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1120c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1121c797b6c6SAneesh Kumar K.V free(glock.client_id); 1122c797b6c6SAneesh Kumar K.V return; 1123c797b6c6SAneesh Kumar K.V } 1124c797b6c6SAneesh Kumar K.V 1125c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor) 1126c797b6c6SAneesh Kumar K.V { 1127c797b6c6SAneesh Kumar K.V int size = strlen(ancestor); 1128c797b6c6SAneesh Kumar K.V if (!strncmp(path, ancestor, size)) { 1129c797b6c6SAneesh Kumar K.V /* 1130c797b6c6SAneesh Kumar K.V * Now check whether ancestor is a full name or 1131c797b6c6SAneesh Kumar K.V * or directory component and not just part 1132c797b6c6SAneesh Kumar K.V * of a name. 1133c797b6c6SAneesh Kumar K.V */ 1134c797b6c6SAneesh Kumar K.V if (path[size] == '\0' || path[size] == '/') 1135c797b6c6SAneesh Kumar K.V return 1; 1136c797b6c6SAneesh Kumar K.V } 1137c797b6c6SAneesh Kumar K.V return 0; 1138c797b6c6SAneesh Kumar K.V } 1139c797b6c6SAneesh Kumar K.V 1140e277a1b4SG. Campana static int virtio_p9_fix_path(struct p9_fid *fid, char *old_name, char *new_name) 1141c797b6c6SAneesh Kumar K.V { 1142e277a1b4SG. Campana int ret; 1143e277a1b4SG. Campana char *p, tmp_name[PATH_MAX]; 1144c797b6c6SAneesh Kumar K.V size_t rp_sz = strlen(old_name); 1145c797b6c6SAneesh Kumar K.V 1146e277a1b4SG. Campana if (rp_sz == strlen(fid->path)) { 1147c797b6c6SAneesh Kumar K.V /* replace the full name */ 1148e277a1b4SG. Campana p = new_name; 1149e277a1b4SG. Campana } else { 1150c797b6c6SAneesh Kumar K.V /* save the trailing path details */ 1151e277a1b4SG. Campana ret = snprintf(tmp_name, sizeof(tmp_name), "%s%s", new_name, fid->path + rp_sz); 1152e277a1b4SG. Campana if (ret >= (int)sizeof(tmp_name)) 1153e277a1b4SG. Campana return -1; 1154e277a1b4SG. Campana p = tmp_name; 1155e277a1b4SG. Campana } 1156e277a1b4SG. Campana 1157e277a1b4SG. Campana return join_path(fid, p); 1158c797b6c6SAneesh Kumar K.V } 1159c797b6c6SAneesh Kumar K.V 1160e2341580SSasha Levin static void rename_fids(struct p9_dev *p9dev, char *old_name, char *new_name) 1161e2341580SSasha Levin { 1162e2341580SSasha Levin struct rb_node *node = rb_first(&p9dev->fids); 1163e2341580SSasha Levin 1164e2341580SSasha Levin while (node) { 1165e2341580SSasha Levin struct p9_fid *fid = rb_entry(node, struct p9_fid, node); 1166e2341580SSasha Levin 1167e2341580SSasha Levin if (fid->fid != P9_NOFID && virtio_p9_ancestor(fid->path, old_name)) { 1168e277a1b4SG. Campana virtio_p9_fix_path(fid, old_name, new_name); 1169e2341580SSasha Levin } 1170e2341580SSasha Levin node = rb_next(node); 1171e2341580SSasha Levin } 1172e2341580SSasha Levin } 1173e2341580SSasha Levin 1174c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev, 1175c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1176c797b6c6SAneesh Kumar K.V { 1177e2341580SSasha Levin int ret; 1178c797b6c6SAneesh Kumar K.V char *old_name, *new_name; 1179c797b6c6SAneesh Kumar K.V u32 old_dfid_val, new_dfid_val; 1180c797b6c6SAneesh Kumar K.V struct p9_fid *old_dfid, *new_dfid; 1181c797b6c6SAneesh Kumar K.V char old_full_path[PATH_MAX], new_full_path[PATH_MAX]; 1182c797b6c6SAneesh Kumar K.V 1183c797b6c6SAneesh Kumar K.V 1184c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name, 1185c797b6c6SAneesh Kumar K.V &new_dfid_val, &new_name); 1186c797b6c6SAneesh Kumar K.V 118731a6fb8dSSasha Levin old_dfid = get_fid(p9dev, old_dfid_val); 118831a6fb8dSSasha Levin new_dfid = get_fid(p9dev, new_dfid_val); 1189c797b6c6SAneesh Kumar K.V 1190d4727f2bSG. Campana if (get_full_path(old_full_path, sizeof(old_full_path), old_dfid, old_name) != 0) 119132832dd1SG. Campana goto err_out; 119232832dd1SG. Campana 1193d4727f2bSG. Campana if (get_full_path(new_full_path, sizeof(new_full_path), new_dfid, new_name) != 0) 119432832dd1SG. Campana goto err_out; 11959bb99a82SG. Campana 1196c797b6c6SAneesh Kumar K.V ret = rename(old_full_path, new_full_path); 1197c797b6c6SAneesh Kumar K.V if (ret < 0) 1198c797b6c6SAneesh Kumar K.V goto err_out; 1199c797b6c6SAneesh Kumar K.V /* 1200c797b6c6SAneesh Kumar K.V * Now fix path in other fids, if the renamed path is part of 1201c797b6c6SAneesh Kumar K.V * that. 1202c797b6c6SAneesh Kumar K.V */ 1203e2341580SSasha Levin rename_fids(p9dev, old_name, new_name); 1204c797b6c6SAneesh Kumar K.V free(old_name); 1205c797b6c6SAneesh Kumar K.V free(new_name); 1206c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1207c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1208c797b6c6SAneesh Kumar K.V return; 1209c797b6c6SAneesh Kumar K.V err_out: 1210c797b6c6SAneesh Kumar K.V free(old_name); 1211c797b6c6SAneesh Kumar K.V free(new_name); 1212c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1213c797b6c6SAneesh Kumar K.V return; 1214c797b6c6SAneesh Kumar K.V } 1215c797b6c6SAneesh Kumar K.V 1216c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev, 1217c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1218c797b6c6SAneesh Kumar K.V { 1219c797b6c6SAneesh Kumar K.V int ret; 1220c797b6c6SAneesh Kumar K.V char *name; 1221c797b6c6SAneesh Kumar K.V u32 fid_val, flags; 1222c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 1223c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 1224c797b6c6SAneesh Kumar K.V 1225c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags); 122631a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 1227c797b6c6SAneesh Kumar K.V 1228d4727f2bSG. Campana if (get_full_path(full_path, sizeof(full_path), fid, name) != 0) 122932832dd1SG. Campana goto err_out; 12309bb99a82SG. Campana 1231c797b6c6SAneesh Kumar K.V ret = remove(full_path); 1232c797b6c6SAneesh Kumar K.V if (ret < 0) 1233c797b6c6SAneesh Kumar K.V goto err_out; 1234c797b6c6SAneesh Kumar K.V free(name); 1235c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1236c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1237c797b6c6SAneesh Kumar K.V return; 1238c797b6c6SAneesh Kumar K.V err_out: 1239c797b6c6SAneesh Kumar K.V free(name); 1240c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1241c797b6c6SAneesh Kumar K.V return; 1242c797b6c6SAneesh Kumar K.V } 1243c797b6c6SAneesh Kumar K.V 12445cc808aaSSasha Levin static void virtio_p9_flush(struct p9_dev *p9dev, 12455cc808aaSSasha Levin struct p9_pdu *pdu, u32 *outlen) 12465cc808aaSSasha Levin { 12475cc808aaSSasha Levin u16 tag, oldtag; 12485cc808aaSSasha Levin 12495cc808aaSSasha Levin virtio_p9_pdu_readf(pdu, "ww", &tag, &oldtag); 12505cc808aaSSasha Levin virtio_p9_pdu_writef(pdu, "w", tag); 12515cc808aaSSasha Levin *outlen = pdu->write_offset; 12525cc808aaSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 12535cc808aaSSasha Levin 12545cc808aaSSasha Levin return; 12555cc808aaSSasha Levin } 12565cc808aaSSasha Levin 1257c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev, 1258c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1259c797b6c6SAneesh Kumar K.V { 1260c797b6c6SAneesh Kumar K.V return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen); 1261c797b6c6SAneesh Kumar K.V } 1262c797b6c6SAneesh Kumar K.V 1263ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev, 1264af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen); 1265b4422bf3SAneesh Kumar K.V 1266c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */ 1267c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74 1268c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76 1269c797b6c6SAneesh Kumar K.V 1270c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = { 1271c797b6c6SAneesh Kumar K.V [P9_TREADDIR] = virtio_p9_readdir, 1272c797b6c6SAneesh Kumar K.V [P9_TSTATFS] = virtio_p9_statfs, 1273c797b6c6SAneesh Kumar K.V [P9_TGETATTR] = virtio_p9_getattr, 1274c797b6c6SAneesh Kumar K.V [P9_TSETATTR] = virtio_p9_setattr, 1275c797b6c6SAneesh Kumar K.V [P9_TXATTRWALK] = virtio_p9_eopnotsupp, 1276c797b6c6SAneesh Kumar K.V [P9_TXATTRCREATE] = virtio_p9_eopnotsupp, 1277c797b6c6SAneesh Kumar K.V [P9_TMKNOD] = virtio_p9_mknod, 1278c797b6c6SAneesh Kumar K.V [P9_TLOCK] = virtio_p9_lock, 1279c797b6c6SAneesh Kumar K.V [P9_TGETLOCK] = virtio_p9_getlock, 1280c797b6c6SAneesh Kumar K.V [P9_TRENAMEAT] = virtio_p9_renameat, 1281c797b6c6SAneesh Kumar K.V [P9_TREADLINK] = virtio_p9_readlink, 1282c797b6c6SAneesh Kumar K.V [P9_TUNLINKAT] = virtio_p9_unlinkat, 1283c797b6c6SAneesh Kumar K.V [P9_TMKDIR] = virtio_p9_mkdir, 1284b4422bf3SAneesh Kumar K.V [P9_TVERSION] = virtio_p9_version, 1285c797b6c6SAneesh Kumar K.V [P9_TLOPEN] = virtio_p9_open, 1286b4422bf3SAneesh Kumar K.V [P9_TATTACH] = virtio_p9_attach, 1287b4422bf3SAneesh Kumar K.V [P9_TWALK] = virtio_p9_walk, 1288c797b6c6SAneesh Kumar K.V [P9_TCLUNK] = virtio_p9_clunk, 1289c797b6c6SAneesh Kumar K.V [P9_TFSYNC] = virtio_p9_fsync, 1290b4422bf3SAneesh Kumar K.V [P9_TREAD] = virtio_p9_read, 12915cc808aaSSasha Levin [P9_TFLUSH] = virtio_p9_flush, 1292c797b6c6SAneesh Kumar K.V [P9_TLINK] = virtio_p9_link, 1293c797b6c6SAneesh Kumar K.V [P9_TSYMLINK] = virtio_p9_symlink, 1294c797b6c6SAneesh Kumar K.V [P9_TLCREATE] = virtio_p9_create, 1295b4422bf3SAneesh Kumar K.V [P9_TWRITE] = virtio_p9_write, 12966fc5cd9bSSasha Levin [P9_TREMOVE] = virtio_p9_remove, 1297f161f28bSSasha Levin [P9_TRENAME] = virtio_p9_rename, 1298b4422bf3SAneesh Kumar K.V }; 1299b4422bf3SAneesh Kumar K.V 1300af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq) 1301af045e53SAneesh Kumar K.V { 1302af045e53SAneesh Kumar K.V struct p9_pdu *pdu = calloc(1, sizeof(*pdu)); 1303af045e53SAneesh Kumar K.V if (!pdu) 1304af045e53SAneesh Kumar K.V return NULL; 1305af045e53SAneesh Kumar K.V 1306bfc15268SAneesh Kumar K.V /* skip the pdu header p9_msg */ 13075529bcd7SAsias He pdu->read_offset = VIRTIO_9P_HDR_LEN; 13085529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 1309af045e53SAneesh Kumar K.V pdu->queue_head = virt_queue__get_inout_iov(kvm, vq, pdu->in_iov, 1310a8a44649SAsias He pdu->out_iov, &pdu->in_iov_cnt, &pdu->out_iov_cnt); 1311af045e53SAneesh Kumar K.V return pdu; 1312af045e53SAneesh Kumar K.V } 1313af045e53SAneesh Kumar K.V 1314af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu) 1315af045e53SAneesh Kumar K.V { 1316af045e53SAneesh Kumar K.V struct p9_msg *msg; 1317af045e53SAneesh Kumar K.V /* 1318af045e53SAneesh Kumar K.V * we can peek directly into pdu for a u8 1319af045e53SAneesh Kumar K.V * value. The host endianess won't be an issue 1320af045e53SAneesh Kumar K.V */ 1321af045e53SAneesh Kumar K.V msg = pdu->out_iov[0].iov_base; 1322af045e53SAneesh Kumar K.V return msg->cmd; 1323af045e53SAneesh Kumar K.V } 1324af045e53SAneesh Kumar K.V 1325b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job) 13261c7850f9SSasha Levin { 1327af045e53SAneesh Kumar K.V u8 cmd; 1328b4422bf3SAneesh Kumar K.V u32 len = 0; 1329b4422bf3SAneesh Kumar K.V p9_handler *handler; 1330b4422bf3SAneesh Kumar K.V struct p9_dev *p9dev; 1331af045e53SAneesh Kumar K.V struct virt_queue *vq; 1332af045e53SAneesh Kumar K.V struct p9_pdu *p9pdu; 13331c7850f9SSasha Levin 1334b4422bf3SAneesh Kumar K.V vq = job->vq; 1335b4422bf3SAneesh Kumar K.V p9dev = job->p9dev; 13361c7850f9SSasha Levin 1337af045e53SAneesh Kumar K.V p9pdu = virtio_p9_pdu_init(kvm, vq); 1338af045e53SAneesh Kumar K.V cmd = virtio_p9_get_cmd(p9pdu); 1339af045e53SAneesh Kumar K.V 1340c797b6c6SAneesh Kumar K.V if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) || 1341c797b6c6SAneesh Kumar K.V !virtio_9p_dotl_handler[cmd]) 134297b408afSAneesh Kumar K.V handler = virtio_p9_eopnotsupp; 1343dd78d9eaSAneesh Kumar K.V else 1344c797b6c6SAneesh Kumar K.V handler = virtio_9p_dotl_handler[cmd]; 1345c797b6c6SAneesh Kumar K.V 1346af045e53SAneesh Kumar K.V handler(p9dev, p9pdu, &len); 1347af045e53SAneesh Kumar K.V virt_queue__set_used_elem(vq, p9pdu->queue_head, len); 1348af045e53SAneesh Kumar K.V free(p9pdu); 13491c7850f9SSasha Levin return true; 13501c7850f9SSasha Levin } 13511c7850f9SSasha Levin 13521c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param) 13531c7850f9SSasha Levin { 1354b4422bf3SAneesh Kumar K.V struct p9_dev_job *job = (struct p9_dev_job *)param; 1355b4422bf3SAneesh Kumar K.V struct p9_dev *p9dev = job->p9dev; 1356b4422bf3SAneesh Kumar K.V struct virt_queue *vq = job->vq; 13571c7850f9SSasha Levin 13581c7850f9SSasha Levin while (virt_queue__available(vq)) { 1359b4422bf3SAneesh Kumar K.V virtio_p9_do_io_request(kvm, job); 136002eca50cSAsias He p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs); 13611c7850f9SSasha Levin } 13621c7850f9SSasha Levin } 13631c7850f9SSasha Levin 1364c5ae742bSSasha Levin static u8 *get_config(struct kvm *kvm, void *dev) 13651c7850f9SSasha Levin { 1366c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 13671c7850f9SSasha Levin 1368c5ae742bSSasha Levin return ((u8 *)(p9dev->config)); 1369c7838fbdSSasha Levin } 1370c7838fbdSSasha Levin 1371c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev) 1372c7838fbdSSasha Levin { 1373c7838fbdSSasha Levin return 1 << VIRTIO_9P_MOUNT_TAG; 1374c7838fbdSSasha Levin } 1375c7838fbdSSasha Levin 1376c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features) 1377c7838fbdSSasha Levin { 1378c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1379e0ea1859SMarc Zyngier struct virtio_9p_config *conf = p9dev->config; 1380c7838fbdSSasha Levin 1381c7838fbdSSasha Levin p9dev->features = features; 1382e0ea1859SMarc Zyngier conf->tag_len = virtio_host_to_guest_u16(&p9dev->vdev, conf->tag_len); 1383c7838fbdSSasha Levin } 1384c7838fbdSSasha Levin 1385c59ba304SWill Deacon static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align, 1386c59ba304SWill Deacon u32 pfn) 1387c7838fbdSSasha Levin { 1388c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1389b4422bf3SAneesh Kumar K.V struct p9_dev_job *job; 1390b4422bf3SAneesh Kumar K.V struct virt_queue *queue; 1391c7838fbdSSasha Levin void *p; 13921c7850f9SSasha Levin 1393312c62d1SSasha Levin compat__remove_message(compat_id); 1394e59662b3SSasha Levin 1395c7838fbdSSasha Levin queue = &p9dev->vqs[vq]; 1396c7838fbdSSasha Levin queue->pfn = pfn; 1397e7e2950aSSasha Levin p = virtio_get_vq(kvm, queue->pfn, page_size); 1398c7838fbdSSasha Levin job = &p9dev->jobs[vq]; 13991c7850f9SSasha Levin 1400c59ba304SWill Deacon vring_init(&queue->vring, VIRTQUEUE_NUM, p, align); 1401e0ea1859SMarc Zyngier virtio_init_device_vq(&p9dev->vdev, queue); 14021c7850f9SSasha Levin 1403b4422bf3SAneesh Kumar K.V *job = (struct p9_dev_job) { 1404b4422bf3SAneesh Kumar K.V .vq = queue, 1405b4422bf3SAneesh Kumar K.V .p9dev = p9dev, 1406b4422bf3SAneesh Kumar K.V }; 1407df0c7f57SSasha Levin thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job); 140860eb42d5SSasha Levin 1409c7838fbdSSasha Levin return 0; 14101c7850f9SSasha Levin } 14111c7850f9SSasha Levin 1412c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq) 1413c7838fbdSSasha Levin { 1414c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 14151c7850f9SSasha Levin 1416c7838fbdSSasha Levin thread_pool__do_job(&p9dev->jobs[vq].job_id); 1417c7838fbdSSasha Levin 1418c7838fbdSSasha Levin return 0; 1419c7838fbdSSasha Levin } 1420c7838fbdSSasha Levin 1421c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq) 1422c7838fbdSSasha Levin { 1423c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1424c7838fbdSSasha Levin 1425c7838fbdSSasha Levin return p9dev->vqs[vq].pfn; 1426c7838fbdSSasha Levin } 1427c7838fbdSSasha Levin 1428c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq) 1429c7838fbdSSasha Levin { 1430c7838fbdSSasha Levin return VIRTQUEUE_NUM; 1431c7838fbdSSasha Levin } 1432c7838fbdSSasha Levin 14337aba29c1SWill Deacon static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size) 14347aba29c1SWill Deacon { 14357aba29c1SWill Deacon /* FIXME: dynamic */ 14367aba29c1SWill Deacon return size; 14377aba29c1SWill Deacon } 14387aba29c1SWill Deacon 143915542babSAndre Przywara struct virtio_ops p9_dev_virtio_ops = { 1440c7838fbdSSasha Levin .get_config = get_config, 1441c7838fbdSSasha Levin .get_host_features = get_host_features, 1442c7838fbdSSasha Levin .set_guest_features = set_guest_features, 1443c7838fbdSSasha Levin .init_vq = init_vq, 1444c7838fbdSSasha Levin .notify_vq = notify_vq, 1445c7838fbdSSasha Levin .get_pfn_vq = get_pfn_vq, 1446c7838fbdSSasha Levin .get_size_vq = get_size_vq, 14477aba29c1SWill Deacon .set_size_vq = set_size_vq, 1448c7838fbdSSasha Levin }; 14491c47ce69SSasha Levin 1450cac9e8fdSSasha Levin int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) 1451cac9e8fdSSasha Levin { 1452cac9e8fdSSasha Levin char *tag_name; 1453cac9e8fdSSasha Levin char tmp[PATH_MAX]; 1454cac9e8fdSSasha Levin struct kvm *kvm = opt->ptr; 1455cac9e8fdSSasha Levin 1456cac9e8fdSSasha Levin /* 1457cac9e8fdSSasha Levin * 9p dir can be of the form dirname,tag_name or 1458cac9e8fdSSasha Levin * just dirname. In the later case we use the 1459cac9e8fdSSasha Levin * default tag name 1460cac9e8fdSSasha Levin */ 1461cac9e8fdSSasha Levin tag_name = strstr(arg, ","); 1462cac9e8fdSSasha Levin if (tag_name) { 1463cac9e8fdSSasha Levin *tag_name = '\0'; 1464cac9e8fdSSasha Levin tag_name++; 1465cac9e8fdSSasha Levin } 1466cac9e8fdSSasha Levin if (realpath(arg, tmp)) { 1467cac9e8fdSSasha Levin if (virtio_9p__register(kvm, tmp, tag_name) < 0) 1468cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1469cac9e8fdSSasha Levin } else 1470cac9e8fdSSasha Levin die("Failed resolving 9p path"); 1471cac9e8fdSSasha Levin return 0; 1472cac9e8fdSSasha Levin } 1473cac9e8fdSSasha Levin 1474cac9e8fdSSasha Levin int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset) 1475cac9e8fdSSasha Levin { 1476cac9e8fdSSasha Levin char path[PATH_MAX]; 1477cac9e8fdSSasha Levin struct stat st; 1478cac9e8fdSSasha Levin struct kvm *kvm = opt->ptr; 1479cac9e8fdSSasha Levin 1480cac9e8fdSSasha Levin if (stat(arg, &st) == 0 && 1481cac9e8fdSSasha Levin S_ISDIR(st.st_mode)) { 1482cac9e8fdSSasha Levin char tmp[PATH_MAX]; 1483cac9e8fdSSasha Levin 1484cac9e8fdSSasha Levin if (kvm->cfg.using_rootfs) 1485cac9e8fdSSasha Levin die("Please use only one rootfs directory atmost"); 1486cac9e8fdSSasha Levin 1487cac9e8fdSSasha Levin if (realpath(arg, tmp) == 0 || 1488cac9e8fdSSasha Levin virtio_9p__register(kvm, tmp, "/dev/root") < 0) 1489cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1490cac9e8fdSSasha Levin kvm->cfg.using_rootfs = 1; 1491cac9e8fdSSasha Levin return 0; 1492cac9e8fdSSasha Levin } 1493cac9e8fdSSasha Levin 1494cac9e8fdSSasha Levin snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg); 1495cac9e8fdSSasha Levin 1496cac9e8fdSSasha Levin if (stat(path, &st) == 0 && 1497cac9e8fdSSasha Levin S_ISDIR(st.st_mode)) { 1498cac9e8fdSSasha Levin char tmp[PATH_MAX]; 1499cac9e8fdSSasha Levin 1500cac9e8fdSSasha Levin if (kvm->cfg.using_rootfs) 1501cac9e8fdSSasha Levin die("Please use only one rootfs directory atmost"); 1502cac9e8fdSSasha Levin 1503cac9e8fdSSasha Levin if (realpath(path, tmp) == 0 || 1504cac9e8fdSSasha Levin virtio_9p__register(kvm, tmp, "/dev/root") < 0) 1505cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1506cac9e8fdSSasha Levin if (virtio_9p__register(kvm, "/", "hostfs") < 0) 1507cac9e8fdSSasha Levin die("Unable to initialize virtio 9p"); 1508cac9e8fdSSasha Levin kvm_setup_resolv(arg); 1509cac9e8fdSSasha Levin kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 1510cac9e8fdSSasha Levin kvm->cfg.custom_rootfs_name = arg; 1511cac9e8fdSSasha Levin return 0; 1512cac9e8fdSSasha Levin } 1513cac9e8fdSSasha Levin 1514cac9e8fdSSasha Levin return -1; 1515cac9e8fdSSasha Levin } 1516cac9e8fdSSasha Levin 15171c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm) 15181c47ce69SSasha Levin { 15191c47ce69SSasha Levin struct p9_dev *p9dev; 15201c47ce69SSasha Levin 15211c47ce69SSasha Levin list_for_each_entry(p9dev, &devs, list) { 152202eca50cSAsias He virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops, 1523d97dadecSWill Deacon VIRTIO_DEFAULT_TRANS(kvm), PCI_DEVICE_ID_VIRTIO_9P, 1524ae06ce71SWill Deacon VIRTIO_ID_9P, PCI_CLASS_9P); 1525c7838fbdSSasha Levin } 1526c7838fbdSSasha Levin 1527c7838fbdSSasha Levin return 0; 1528c7838fbdSSasha Levin } 152949a8afd1SSasha Levin virtio_dev_init(virtio_9p__init); 1530c7838fbdSSasha Levin 1531c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name) 1532c7838fbdSSasha Levin { 1533c7838fbdSSasha Levin struct p9_dev *p9dev; 153454f6802dSPekka Enberg int err = 0; 15351c7850f9SSasha Levin 1536b4422bf3SAneesh Kumar K.V p9dev = calloc(1, sizeof(*p9dev)); 1537b4422bf3SAneesh Kumar K.V if (!p9dev) 153854f6802dSPekka Enberg return -ENOMEM; 153954f6802dSPekka Enberg 1540b4422bf3SAneesh Kumar K.V if (!tag_name) 15415529bcd7SAsias He tag_name = VIRTIO_9P_DEFAULT_TAG; 154254f6802dSPekka Enberg 1543b4422bf3SAneesh Kumar K.V p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1); 154454f6802dSPekka Enberg if (p9dev->config == NULL) { 154554f6802dSPekka Enberg err = -ENOMEM; 1546b4422bf3SAneesh Kumar K.V goto free_p9dev; 154754f6802dSPekka Enberg } 15481c7850f9SSasha Levin 1549e277a1b4SG. Campana strncpy(p9dev->root_dir, root, sizeof(p9dev->root_dir)); 1550e277a1b4SG. Campana p9dev->root_dir[sizeof(p9dev->root_dir)-1] = '\x00'; 1551e277a1b4SG. Campana 1552b4422bf3SAneesh Kumar K.V p9dev->config->tag_len = strlen(tag_name); 155354f6802dSPekka Enberg if (p9dev->config->tag_len > MAX_TAG_LEN) { 155454f6802dSPekka Enberg err = -EINVAL; 1555b4422bf3SAneesh Kumar K.V goto free_p9dev_config; 155654f6802dSPekka Enberg } 15571c7850f9SSasha Levin 1558c7838fbdSSasha Levin memcpy(&p9dev->config->tag, tag_name, strlen(tag_name)); 15591c7850f9SSasha Levin 1560c7838fbdSSasha Levin list_add(&p9dev->list, &devs); 1561b4422bf3SAneesh Kumar K.V 1562d278197dSAsias He if (compat_id == -1) 156352f34d2cSAsias He compat_id = virtio_compat_add_message("virtio-9p", "CONFIG_NET_9P_VIRTIO"); 1564e59662b3SSasha Levin 156554f6802dSPekka Enberg return err; 156654f6802dSPekka Enberg 1567b4422bf3SAneesh Kumar K.V free_p9dev_config: 1568b4422bf3SAneesh Kumar K.V free(p9dev->config); 1569b4422bf3SAneesh Kumar K.V free_p9dev: 1570b4422bf3SAneesh Kumar K.V free(p9dev); 157154f6802dSPekka Enberg return err; 15721c7850f9SSasha Levin } 1573