11c7850f9SSasha Levin #include "kvm/virtio-pci-dev.h" 21c7850f9SSasha Levin #include "kvm/ioport.h" 31c7850f9SSasha Levin #include "kvm/util.h" 41c7850f9SSasha Levin #include "kvm/threadpool.h" 5bfc15268SAneesh Kumar K.V #include "kvm/irq.h" 6bfc15268SAneesh Kumar K.V #include "kvm/virtio-9p.h" 7e59662b3SSasha Levin #include "kvm/guest_compat.h" 81c7850f9SSasha Levin 9bfc15268SAneesh Kumar K.V #include <stdio.h> 10bfc15268SAneesh Kumar K.V #include <stdlib.h> 111c7850f9SSasha Levin #include <fcntl.h> 121c7850f9SSasha Levin #include <sys/stat.h> 13bfc15268SAneesh Kumar K.V #include <unistd.h> 14bfc15268SAneesh Kumar K.V #include <string.h> 15bfc15268SAneesh Kumar K.V #include <errno.h> 16c797b6c6SAneesh Kumar K.V #include <sys/vfs.h> 171c7850f9SSasha Levin 182daa28d4SAneesh Kumar K.V #include <linux/virtio_ring.h> 192daa28d4SAneesh Kumar K.V #include <linux/virtio_9p.h> 202daa28d4SAneesh Kumar K.V #include <net/9p/9p.h> 212daa28d4SAneesh Kumar K.V 22c7838fbdSSasha Levin static LIST_HEAD(devs); 23312c62d1SSasha Levin static int compat_id = -1; 24c7838fbdSSasha Levin 25e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid); 26e2341580SSasha Levin static struct p9_fid *find_or_create_fid(struct p9_dev *dev, u32 fid) 27e2341580SSasha Levin { 28e2341580SSasha Levin struct rb_node *node = dev->fids.rb_node; 29e2341580SSasha Levin struct p9_fid *pfid = NULL; 30e2341580SSasha Levin 31e2341580SSasha Levin while (node) { 32e2341580SSasha Levin struct p9_fid *cur = rb_entry(node, struct p9_fid, node); 33e2341580SSasha Levin 34e2341580SSasha Levin if (fid < cur->fid) { 35e2341580SSasha Levin node = node->rb_left; 36e2341580SSasha Levin } else if (fid > cur->fid) { 37e2341580SSasha Levin node = node->rb_right; 38e2341580SSasha Levin } else { 39e2341580SSasha Levin return cur; 40e2341580SSasha Levin } 41e2341580SSasha Levin } 42e2341580SSasha Levin 43e2341580SSasha Levin pfid = calloc(sizeof(*pfid), 1); 44e2341580SSasha Levin if (!pfid) 45e2341580SSasha Levin return NULL; 46e2341580SSasha Levin 47e2341580SSasha Levin pfid->fid = fid; 48e2341580SSasha Levin strcpy(pfid->abs_path, dev->root_dir); 49e2341580SSasha Levin pfid->path = pfid->abs_path + strlen(dev->root_dir); 50e2341580SSasha Levin 51e2341580SSasha Levin insert_new_fid(dev, pfid); 52e2341580SSasha Levin 53e2341580SSasha Levin return pfid; 54e2341580SSasha Levin } 55e2341580SSasha Levin 56e2341580SSasha Levin static int insert_new_fid(struct p9_dev *dev, struct p9_fid *fid) 57e2341580SSasha Levin { 58e2341580SSasha Levin struct rb_node **node = &(dev->fids.rb_node), *parent = NULL; 59e2341580SSasha Levin 60e2341580SSasha Levin while (*node) { 61e2341580SSasha Levin int result = fid->fid - rb_entry(*node, struct p9_fid, node)->fid; 62e2341580SSasha Levin 63e2341580SSasha Levin parent = *node; 64e2341580SSasha Levin if (result < 0) 65e2341580SSasha Levin node = &((*node)->rb_left); 66e2341580SSasha Levin else if (result > 0) 67e2341580SSasha Levin node = &((*node)->rb_right); 68e2341580SSasha Levin else 69e2341580SSasha Levin return -EEXIST; 70e2341580SSasha Levin } 71e2341580SSasha Levin 72e2341580SSasha Levin rb_link_node(&fid->node, parent, node); 73e2341580SSasha Levin rb_insert_color(&fid->node, &dev->fids); 74e2341580SSasha Levin return 0; 75e2341580SSasha Levin } 76e2341580SSasha Levin 7731a6fb8dSSasha Levin static struct p9_fid *get_fid(struct p9_dev *p9dev, int fid) 7831a6fb8dSSasha Levin { 79e2341580SSasha Levin struct p9_fid *new; 8031a6fb8dSSasha Levin 81e2341580SSasha Levin new = find_or_create_fid(p9dev, fid); 82e2341580SSasha Levin 83e2341580SSasha Levin return new; 8431a6fb8dSSasha Levin } 8531a6fb8dSSasha Levin 861c7850f9SSasha Levin /* Warning: Immediately use value returned from this function */ 87b4422bf3SAneesh Kumar K.V static const char *rel_to_abs(struct p9_dev *p9dev, 88b4422bf3SAneesh Kumar K.V const char *path, char *abs_path) 891c7850f9SSasha Levin { 90b4422bf3SAneesh Kumar K.V sprintf(abs_path, "%s/%s", p9dev->root_dir, path); 911c7850f9SSasha Levin 921c7850f9SSasha Levin return abs_path; 931c7850f9SSasha Levin } 941c7850f9SSasha 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 1535529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 154c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", err); 155bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 156eee1ba8eSAneesh Kumar K.V 157c797b6c6SAneesh Kumar K.V /* read the tag from input */ 158bfc15268SAneesh Kumar K.V pdu->read_offset = sizeof(u32) + sizeof(u8); 159bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "w", &tag); 160bfc15268SAneesh Kumar K.V 161c797b6c6SAneesh Kumar K.V /* Update the header */ 162bfc15268SAneesh Kumar K.V pdu->write_offset = 0; 163c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "dbw", *outlen, P9_RLERROR, tag); 164eee1ba8eSAneesh Kumar K.V } 165eee1ba8eSAneesh Kumar K.V 166ead43b01SAneesh Kumar K.V static void virtio_p9_version(struct p9_dev *p9dev, 167af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1681c7850f9SSasha Levin { 169c797b6c6SAneesh Kumar K.V u32 msize; 170c797b6c6SAneesh Kumar K.V char *version; 171c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ds", &msize, &version); 172c797b6c6SAneesh Kumar K.V /* 173c797b6c6SAneesh Kumar K.V * reply with the same msize the client sent us 174c797b6c6SAneesh Kumar K.V * Error out if the request is not for 9P2000.L 175c797b6c6SAneesh Kumar K.V */ 1765529bcd7SAsias He if (!strcmp(version, VIRTIO_9P_VERSION_DOTL)) 177c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ds", msize, version); 178c797b6c6SAneesh Kumar K.V else 179c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ds", msize, "unknown"); 1801c7850f9SSasha Levin 181bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 182bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 183c797b6c6SAneesh Kumar K.V free(version); 184ead43b01SAneesh Kumar K.V return; 1851c7850f9SSasha Levin } 1861c7850f9SSasha Levin 187ead43b01SAneesh Kumar K.V static void virtio_p9_clunk(struct p9_dev *p9dev, 188af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1891c7850f9SSasha Levin { 190bfc15268SAneesh Kumar K.V u32 fid; 1911c7850f9SSasha Levin 192bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid); 193bfc15268SAneesh Kumar K.V close_fid(p9dev, fid); 1941c7850f9SSasha Levin 195bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 196bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 197ead43b01SAneesh Kumar K.V return; 1981c7850f9SSasha Levin } 1991c7850f9SSasha Levin 200c797b6c6SAneesh Kumar K.V /* 201c797b6c6SAneesh Kumar K.V * FIXME!! Need to map to protocol independent value. Upstream 202c797b6c6SAneesh Kumar K.V * 9p also have the same BUG 203c797b6c6SAneesh Kumar K.V */ 204c797b6c6SAneesh Kumar K.V static int virtio_p9_openflags(int flags) 205c797b6c6SAneesh Kumar K.V { 206c797b6c6SAneesh Kumar K.V flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT | O_DIRECT); 207c797b6c6SAneesh Kumar K.V flags |= O_NOFOLLOW; 208c797b6c6SAneesh Kumar K.V return flags; 209c797b6c6SAneesh Kumar K.V } 210c797b6c6SAneesh Kumar K.V 21132585666SSasha Levin static bool is_dir(struct p9_fid *fid) 21232585666SSasha Levin { 21332585666SSasha Levin struct stat st; 21432585666SSasha Levin 21532585666SSasha Levin stat(fid->abs_path, &st); 21632585666SSasha Levin 21732585666SSasha Levin return S_ISDIR(st.st_mode); 21832585666SSasha Levin } 21932585666SSasha Levin 220ead43b01SAneesh Kumar K.V static void virtio_p9_open(struct p9_dev *p9dev, 221af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 2221c7850f9SSasha Levin { 223c797b6c6SAneesh Kumar K.V u32 fid, flags; 2241c7850f9SSasha Levin struct stat st; 225bfc15268SAneesh Kumar K.V struct p9_qid qid; 226bfc15268SAneesh Kumar K.V struct p9_fid *new_fid; 227bfc15268SAneesh Kumar K.V 228c797b6c6SAneesh Kumar K.V 229c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dd", &fid, &flags); 23031a6fb8dSSasha Levin new_fid = get_fid(p9dev, fid); 2311c7850f9SSasha Levin 23230204a77SAneesh Kumar K.V if (lstat(new_fid->abs_path, &st) < 0) 233eee1ba8eSAneesh Kumar K.V goto err_out; 2341c7850f9SSasha Levin 235c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 2361c7850f9SSasha Levin 23732585666SSasha Levin if (is_dir(new_fid)) { 2381c7850f9SSasha Levin new_fid->dir = opendir(new_fid->abs_path); 239eee1ba8eSAneesh Kumar K.V if (!new_fid->dir) 240eee1ba8eSAneesh Kumar K.V goto err_out; 241eee1ba8eSAneesh Kumar K.V } else { 242eee1ba8eSAneesh Kumar K.V new_fid->fd = open(new_fid->abs_path, 243c797b6c6SAneesh Kumar K.V virtio_p9_openflags(flags)); 244eee1ba8eSAneesh Kumar K.V if (new_fid->fd < 0) 245eee1ba8eSAneesh Kumar K.V goto err_out; 246eee1ba8eSAneesh Kumar K.V } 247c797b6c6SAneesh Kumar K.V /* FIXME!! need ot send proper iounit */ 248bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 249bfc15268SAneesh Kumar K.V 250bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 251bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 252ead43b01SAneesh Kumar K.V return; 253eee1ba8eSAneesh Kumar K.V err_out: 254eee1ba8eSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 255ead43b01SAneesh Kumar K.V return; 2561c7850f9SSasha Levin } 2571c7850f9SSasha Levin 258ead43b01SAneesh Kumar K.V static void virtio_p9_create(struct p9_dev *p9dev, 259af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 2601c7850f9SSasha Levin { 261c797b6c6SAneesh Kumar K.V int fd, ret; 262bfc15268SAneesh Kumar K.V char *name; 263af045e53SAneesh Kumar K.V struct stat st; 264bfc15268SAneesh Kumar K.V struct p9_qid qid; 265c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 2664bc9734aSAneesh Kumar K.V char full_path[PATH_MAX]; 267c797b6c6SAneesh Kumar K.V u32 dfid_val, flags, mode, gid; 268af045e53SAneesh Kumar K.V 269c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsddd", &dfid_val, 270c797b6c6SAneesh Kumar K.V &name, &flags, &mode, &gid); 27131a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 2721c7850f9SSasha Levin 273c797b6c6SAneesh Kumar K.V flags = virtio_p9_openflags(flags); 2745f900f6dSSasha Levin 275c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 276c797b6c6SAneesh Kumar K.V fd = open(full_path, flags | O_CREAT, mode); 2774bc9734aSAneesh Kumar K.V if (fd < 0) 2784bc9734aSAneesh Kumar K.V goto err_out; 279c797b6c6SAneesh Kumar K.V dfid->fd = fd; 280c797b6c6SAneesh Kumar K.V 2814bc9734aSAneesh Kumar K.V if (lstat(full_path, &st) < 0) 2826c8ca053SAneesh Kumar K.V goto err_out; 2831c7850f9SSasha Levin 284c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 285c797b6c6SAneesh Kumar K.V if (ret < 0) 286c797b6c6SAneesh Kumar K.V goto err_out; 287c797b6c6SAneesh Kumar K.V 288c797b6c6SAneesh Kumar K.V ret = lchown(full_path, dfid->uid, gid); 289c797b6c6SAneesh Kumar K.V if (ret < 0) 290c797b6c6SAneesh Kumar K.V goto err_out; 291c797b6c6SAneesh Kumar K.V 292c797b6c6SAneesh Kumar K.V sprintf(dfid->path, "%s/%s", dfid->path, name); 293c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 294bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 295bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 296bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 2975f900f6dSSasha Levin free(name); 2986c8ca053SAneesh Kumar K.V return; 2996c8ca053SAneesh Kumar K.V err_out: 3005f900f6dSSasha Levin free(name); 301c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 302c797b6c6SAneesh Kumar K.V return; 303c797b6c6SAneesh Kumar K.V } 304c797b6c6SAneesh Kumar K.V 305c797b6c6SAneesh Kumar K.V static void virtio_p9_mkdir(struct p9_dev *p9dev, 306c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 307c797b6c6SAneesh Kumar K.V { 308c797b6c6SAneesh Kumar K.V int ret; 309c797b6c6SAneesh Kumar K.V char *name; 310c797b6c6SAneesh Kumar K.V struct stat st; 311c797b6c6SAneesh Kumar K.V struct p9_qid qid; 312c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 313c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 314c797b6c6SAneesh Kumar K.V u32 dfid_val, mode, gid; 315c797b6c6SAneesh Kumar K.V 316c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsdd", &dfid_val, 317c797b6c6SAneesh Kumar K.V &name, &mode, &gid); 31831a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 319c797b6c6SAneesh Kumar K.V 320c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 321c797b6c6SAneesh Kumar K.V ret = mkdir(full_path, mode); 322c797b6c6SAneesh Kumar K.V if (ret < 0) 323c797b6c6SAneesh Kumar K.V goto err_out; 324c797b6c6SAneesh Kumar K.V 325c797b6c6SAneesh Kumar K.V if (lstat(full_path, &st) < 0) 326c797b6c6SAneesh Kumar K.V goto err_out; 327c797b6c6SAneesh Kumar K.V 328c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 329c797b6c6SAneesh Kumar K.V if (ret < 0) 330c797b6c6SAneesh Kumar K.V goto err_out; 331c797b6c6SAneesh Kumar K.V 332c797b6c6SAneesh Kumar K.V ret = lchown(full_path, dfid->uid, gid); 333c797b6c6SAneesh Kumar K.V if (ret < 0) 334c797b6c6SAneesh Kumar K.V goto err_out; 335c797b6c6SAneesh Kumar K.V 336c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 337c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qd", &qid, 0); 338c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 339c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 340c797b6c6SAneesh Kumar K.V free(name); 341c797b6c6SAneesh Kumar K.V return; 342c797b6c6SAneesh Kumar K.V err_out: 343c797b6c6SAneesh Kumar K.V free(name); 344c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 345ead43b01SAneesh Kumar K.V return; 3461c7850f9SSasha Levin } 3471c7850f9SSasha Levin 348ead43b01SAneesh Kumar K.V static void virtio_p9_walk(struct p9_dev *p9dev, 349af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 3501c7850f9SSasha Levin { 351af045e53SAneesh Kumar K.V u8 i; 352bfc15268SAneesh Kumar K.V u16 nwqid; 353bfc15268SAneesh Kumar K.V u16 nwname; 354bfc15268SAneesh Kumar K.V struct p9_qid wqid; 355e2341580SSasha Levin struct p9_fid *new_fid, *old_fid; 356c797b6c6SAneesh Kumar K.V u32 fid_val, newfid_val; 357c797b6c6SAneesh Kumar K.V 3581c7850f9SSasha Levin 359bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ddw", &fid_val, &newfid_val, &nwname); 36031a6fb8dSSasha Levin new_fid = get_fid(p9dev, newfid_val); 3611c7850f9SSasha Levin 362bfc15268SAneesh Kumar K.V nwqid = 0; 363bfc15268SAneesh Kumar K.V if (nwname) { 36431a6fb8dSSasha Levin struct p9_fid *fid = get_fid(p9dev, fid_val); 365bfc15268SAneesh Kumar K.V 366baac79a5SAneesh Kumar K.V strcpy(new_fid->path, fid->path); 367bfc15268SAneesh Kumar K.V /* skip the space for count */ 368bfc15268SAneesh Kumar K.V pdu->write_offset += sizeof(u16); 369bfc15268SAneesh Kumar K.V for (i = 0; i < nwname; i++) { 370bfc15268SAneesh Kumar K.V struct stat st; 3711c7850f9SSasha Levin char tmp[PATH_MAX] = {0}; 3721c7850f9SSasha Levin char full_path[PATH_MAX]; 373e55ed135SPekka Enberg char *str; 374bfc15268SAneesh Kumar K.V 375bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "s", &str); 3761c7850f9SSasha Levin 3771c7850f9SSasha Levin /* Format the new path we're 'walk'ing into */ 378baac79a5SAneesh Kumar K.V sprintf(tmp, "%s/%s", new_fid->path, str); 379e55ed135SPekka Enberg 380e55ed135SPekka Enberg free(str); 381e55ed135SPekka Enberg 382c797b6c6SAneesh Kumar K.V if (lstat(rel_to_abs(p9dev, tmp, full_path), &st) < 0) 3836c8ca053SAneesh Kumar K.V goto err_out; 3841c7850f9SSasha Levin 385c797b6c6SAneesh Kumar K.V stat2qid(&st, &wqid); 3861c7850f9SSasha Levin strcpy(new_fid->path, tmp); 387c797b6c6SAneesh Kumar K.V new_fid->uid = fid->uid; 388bfc15268SAneesh Kumar K.V nwqid++; 389bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &wqid); 3901c7850f9SSasha Levin } 3911c7850f9SSasha Levin } else { 392bfc15268SAneesh Kumar K.V /* 393bfc15268SAneesh Kumar K.V * update write_offset so our outlen get correct value 394bfc15268SAneesh Kumar K.V */ 395bfc15268SAneesh Kumar K.V pdu->write_offset += sizeof(u16); 396e2341580SSasha Levin old_fid = get_fid(p9dev, fid_val); 397e2341580SSasha Levin strcpy(new_fid->path, old_fid->path); 398e2341580SSasha Levin new_fid->uid = old_fid->uid; 3991c7850f9SSasha Levin } 400bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 4015529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 402bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", nwqid); 403bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 4046c8ca053SAneesh Kumar K.V return; 4056c8ca053SAneesh Kumar K.V err_out: 4066c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 407ead43b01SAneesh Kumar K.V return; 4081c7850f9SSasha Levin } 4091c7850f9SSasha Levin 410ead43b01SAneesh Kumar K.V static void virtio_p9_attach(struct p9_dev *p9dev, 411af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 4121c7850f9SSasha Levin { 413bfc15268SAneesh Kumar K.V char *uname; 414bfc15268SAneesh Kumar K.V char *aname; 4151c7850f9SSasha Levin struct stat st; 416bfc15268SAneesh Kumar K.V struct p9_qid qid; 4171c7850f9SSasha Levin struct p9_fid *fid; 418c797b6c6SAneesh Kumar K.V u32 fid_val, afid, uid; 419bfc15268SAneesh Kumar K.V 420c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "ddssd", &fid_val, &afid, 421c797b6c6SAneesh Kumar K.V &uname, &aname, &uid); 4221c7850f9SSasha Levin 42339257180SPekka Enberg free(uname); 42439257180SPekka Enberg free(aname); 42539257180SPekka Enberg 42630204a77SAneesh Kumar K.V if (lstat(p9dev->root_dir, &st) < 0) 4276c8ca053SAneesh Kumar K.V goto err_out; 4281c7850f9SSasha Levin 429c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 4301c7850f9SSasha Levin 43131a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 432c797b6c6SAneesh Kumar K.V fid->uid = uid; 4331c7850f9SSasha Levin strcpy(fid->path, "/"); 4341c7850f9SSasha Levin 435bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 436bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 437bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 4386c8ca053SAneesh Kumar K.V return; 4396c8ca053SAneesh Kumar K.V err_out: 4406c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 441ead43b01SAneesh Kumar K.V return; 4421c7850f9SSasha Levin } 4431c7850f9SSasha Levin 444c797b6c6SAneesh Kumar K.V static void virtio_p9_fill_stat(struct p9_dev *p9dev, 445c797b6c6SAneesh Kumar K.V struct stat *st, struct p9_stat_dotl *statl) 4465f900f6dSSasha Levin { 447c797b6c6SAneesh Kumar K.V memset(statl, 0, sizeof(*statl)); 448c797b6c6SAneesh Kumar K.V statl->st_mode = st->st_mode; 449c797b6c6SAneesh Kumar K.V statl->st_nlink = st->st_nlink; 450c797b6c6SAneesh Kumar K.V statl->st_uid = st->st_uid; 451c797b6c6SAneesh Kumar K.V statl->st_gid = st->st_gid; 452c797b6c6SAneesh Kumar K.V statl->st_rdev = st->st_rdev; 453c797b6c6SAneesh Kumar K.V statl->st_size = st->st_size; 454c797b6c6SAneesh Kumar K.V statl->st_blksize = st->st_blksize; 455c797b6c6SAneesh Kumar K.V statl->st_blocks = st->st_blocks; 456c797b6c6SAneesh Kumar K.V statl->st_atime_sec = st->st_atime; 457c797b6c6SAneesh Kumar K.V statl->st_atime_nsec = st->st_atim.tv_nsec; 458c797b6c6SAneesh Kumar K.V statl->st_mtime_sec = st->st_mtime; 459c797b6c6SAneesh Kumar K.V statl->st_mtime_nsec = st->st_mtim.tv_nsec; 460c797b6c6SAneesh Kumar K.V statl->st_ctime_sec = st->st_ctime; 461c797b6c6SAneesh Kumar K.V statl->st_ctime_nsec = st->st_ctim.tv_nsec; 462c797b6c6SAneesh Kumar K.V /* Currently we only support BASIC fields in stat */ 463c797b6c6SAneesh Kumar K.V statl->st_result_mask = P9_STATS_BASIC; 464c797b6c6SAneesh Kumar K.V stat2qid(st, &statl->qid); 4651c7850f9SSasha Levin } 4661c7850f9SSasha Levin 467ead43b01SAneesh Kumar K.V static void virtio_p9_read(struct p9_dev *p9dev, 468af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 4691c7850f9SSasha Levin { 470bfc15268SAneesh Kumar K.V u64 offset; 471bfc15268SAneesh Kumar K.V u32 fid_val; 472c797b6c6SAneesh Kumar K.V u16 iov_cnt; 473c797b6c6SAneesh Kumar K.V void *iov_base; 474c797b6c6SAneesh Kumar K.V size_t iov_len; 475bfc15268SAneesh Kumar K.V u32 count, rcount; 476bfc15268SAneesh Kumar K.V struct p9_fid *fid; 477c797b6c6SAneesh Kumar K.V 4781c7850f9SSasha Levin 479bfc15268SAneesh Kumar K.V rcount = 0; 480bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 48131a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 48250c479e0SAneesh Kumar K.V 48350c479e0SAneesh Kumar K.V iov_base = pdu->in_iov[0].iov_base; 48450c479e0SAneesh Kumar K.V iov_len = pdu->in_iov[0].iov_len; 48550c479e0SAneesh Kumar K.V iov_cnt = pdu->in_iov_cnt; 4865529bcd7SAsias He pdu->in_iov[0].iov_base += VIRTIO_9P_HDR_LEN + sizeof(u32); 4875529bcd7SAsias He pdu->in_iov[0].iov_len -= VIRTIO_9P_HDR_LEN + sizeof(u32); 4886b163a87SAneesh Kumar K.V pdu->in_iov_cnt = virtio_p9_update_iov_cnt(pdu->in_iov, 489bfc15268SAneesh Kumar K.V count, 4906b163a87SAneesh Kumar K.V pdu->in_iov_cnt); 491bfc15268SAneesh Kumar K.V rcount = preadv(fid->fd, pdu->in_iov, 492bfc15268SAneesh Kumar K.V pdu->in_iov_cnt, offset); 493bfc15268SAneesh Kumar K.V if (rcount > count) 494bfc15268SAneesh Kumar K.V rcount = count; 495bfc15268SAneesh Kumar K.V /* 496bfc15268SAneesh Kumar K.V * Update the iov_base back, so that rest of 497bfc15268SAneesh Kumar K.V * pdu_writef works correctly. 498bfc15268SAneesh Kumar K.V */ 49950c479e0SAneesh Kumar K.V pdu->in_iov[0].iov_base = iov_base; 50050c479e0SAneesh Kumar K.V pdu->in_iov[0].iov_len = iov_len; 50150c479e0SAneesh Kumar K.V pdu->in_iov_cnt = iov_cnt; 502c797b6c6SAneesh Kumar K.V 5035529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 504bfc15268SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", rcount); 505bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset + rcount; 506bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 507ead43b01SAneesh Kumar K.V return; 5081c7850f9SSasha Levin } 5091c7850f9SSasha Levin 510c797b6c6SAneesh Kumar K.V static int virtio_p9_dentry_size(struct dirent *dent) 511c797b6c6SAneesh Kumar K.V { 512c797b6c6SAneesh Kumar K.V /* 513c797b6c6SAneesh Kumar K.V * Size of each dirent: 514c797b6c6SAneesh Kumar K.V * qid(13) + offset(8) + type(1) + name_len(2) + name 515c797b6c6SAneesh Kumar K.V */ 516c797b6c6SAneesh Kumar K.V return 24 + strlen(dent->d_name); 517c797b6c6SAneesh Kumar K.V } 518c797b6c6SAneesh Kumar K.V 519c797b6c6SAneesh Kumar K.V static void virtio_p9_readdir(struct p9_dev *p9dev, 520c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 521c797b6c6SAneesh Kumar K.V { 522c797b6c6SAneesh Kumar K.V u32 fid_val; 523c797b6c6SAneesh Kumar K.V u32 count, rcount; 524c797b6c6SAneesh Kumar K.V struct stat st; 525c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 526c797b6c6SAneesh Kumar K.V struct dirent *dent; 527c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 528c797b6c6SAneesh Kumar K.V u64 offset, old_offset; 529c797b6c6SAneesh Kumar K.V 530c797b6c6SAneesh Kumar K.V rcount = 0; 531c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 53231a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 533c797b6c6SAneesh Kumar K.V 53432585666SSasha Levin if (!is_dir(fid)) { 53569bb4278SSasha Levin errno = EINVAL; 536c797b6c6SAneesh Kumar K.V goto err_out; 537c797b6c6SAneesh Kumar K.V } 538c797b6c6SAneesh Kumar K.V 539c797b6c6SAneesh Kumar K.V /* Move the offset specified */ 540c797b6c6SAneesh Kumar K.V seekdir(fid->dir, offset); 541c797b6c6SAneesh Kumar K.V 542c797b6c6SAneesh Kumar K.V old_offset = offset; 543c797b6c6SAneesh Kumar K.V /* If reading a dir, fill the buffer with p9_stat entries */ 544c797b6c6SAneesh Kumar K.V dent = readdir(fid->dir); 545c797b6c6SAneesh Kumar K.V 546c797b6c6SAneesh Kumar K.V /* Skip the space for writing count */ 547c797b6c6SAneesh Kumar K.V pdu->write_offset += sizeof(u32); 548c797b6c6SAneesh Kumar K.V while (dent) { 549c797b6c6SAneesh Kumar K.V u32 read; 550c797b6c6SAneesh Kumar K.V struct p9_qid qid; 551c797b6c6SAneesh Kumar K.V 552c797b6c6SAneesh Kumar K.V if ((rcount + virtio_p9_dentry_size(dent)) > count) { 553c797b6c6SAneesh Kumar K.V /* seek to the previous offset and return */ 554c797b6c6SAneesh Kumar K.V seekdir(fid->dir, old_offset); 555c797b6c6SAneesh Kumar K.V break; 556c797b6c6SAneesh Kumar K.V } 557c797b6c6SAneesh Kumar K.V old_offset = dent->d_off; 558c797b6c6SAneesh Kumar K.V lstat(rel_to_abs(p9dev, dent->d_name, full_path), &st); 559c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 560c797b6c6SAneesh Kumar K.V read = pdu->write_offset; 561c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Qqbs", &qid, dent->d_off, 562c797b6c6SAneesh Kumar K.V dent->d_type, dent->d_name); 563c797b6c6SAneesh Kumar K.V rcount += pdu->write_offset - read; 564c797b6c6SAneesh Kumar K.V dent = readdir(fid->dir); 565c797b6c6SAneesh Kumar K.V } 566c797b6c6SAneesh Kumar K.V 5675529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 568c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", rcount); 569c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset + rcount; 570c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 571c797b6c6SAneesh Kumar K.V return; 572c797b6c6SAneesh Kumar K.V err_out: 573c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 574c797b6c6SAneesh Kumar K.V return; 575c797b6c6SAneesh Kumar K.V } 576c797b6c6SAneesh Kumar K.V 577c797b6c6SAneesh Kumar K.V 578c797b6c6SAneesh Kumar K.V static void virtio_p9_getattr(struct p9_dev *p9dev, 579af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 5801c7850f9SSasha Levin { 581bfc15268SAneesh Kumar K.V u32 fid_val; 582af045e53SAneesh Kumar K.V struct stat st; 583c797b6c6SAneesh Kumar K.V u64 request_mask; 584bfc15268SAneesh Kumar K.V struct p9_fid *fid; 585c797b6c6SAneesh Kumar K.V struct p9_stat_dotl statl; 5861c7850f9SSasha Levin 587c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dq", &fid_val, &request_mask); 58831a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 58930204a77SAneesh Kumar K.V if (lstat(fid->abs_path, &st) < 0) 5906c8ca053SAneesh Kumar K.V goto err_out; 5911c7850f9SSasha Levin 592c797b6c6SAneesh Kumar K.V virtio_p9_fill_stat(p9dev, &st, &statl); 593c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "A", &statl); 594bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 595bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 596ead43b01SAneesh Kumar K.V return; 5976c8ca053SAneesh Kumar K.V err_out: 5986c8ca053SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 5996c8ca053SAneesh Kumar K.V return; 6001c7850f9SSasha Levin } 6011c7850f9SSasha Levin 602c797b6c6SAneesh Kumar K.V /* FIXME!! from linux/fs.h */ 603c797b6c6SAneesh Kumar K.V /* 604c797b6c6SAneesh Kumar K.V * Attribute flags. These should be or-ed together to figure out what 605c797b6c6SAneesh Kumar K.V * has been changed! 606c797b6c6SAneesh Kumar K.V */ 607c797b6c6SAneesh Kumar K.V #define ATTR_MODE (1 << 0) 608c797b6c6SAneesh Kumar K.V #define ATTR_UID (1 << 1) 609c797b6c6SAneesh Kumar K.V #define ATTR_GID (1 << 2) 610c797b6c6SAneesh Kumar K.V #define ATTR_SIZE (1 << 3) 611c797b6c6SAneesh Kumar K.V #define ATTR_ATIME (1 << 4) 612c797b6c6SAneesh Kumar K.V #define ATTR_MTIME (1 << 5) 613c797b6c6SAneesh Kumar K.V #define ATTR_CTIME (1 << 6) 614c797b6c6SAneesh Kumar K.V #define ATTR_ATIME_SET (1 << 7) 615c797b6c6SAneesh Kumar K.V #define ATTR_MTIME_SET (1 << 8) 616c797b6c6SAneesh Kumar K.V #define ATTR_FORCE (1 << 9) /* Not a change, but a change it */ 617c797b6c6SAneesh Kumar K.V #define ATTR_ATTR_FLAG (1 << 10) 618c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SUID (1 << 11) 619c797b6c6SAneesh Kumar K.V #define ATTR_KILL_SGID (1 << 12) 620c797b6c6SAneesh Kumar K.V #define ATTR_FILE (1 << 13) 621c797b6c6SAneesh Kumar K.V #define ATTR_KILL_PRIV (1 << 14) 622c797b6c6SAneesh Kumar K.V #define ATTR_OPEN (1 << 15) /* Truncating from open(O_TRUNC) */ 623c797b6c6SAneesh Kumar K.V #define ATTR_TIMES_SET (1 << 16) 624c797b6c6SAneesh Kumar K.V 625c797b6c6SAneesh Kumar K.V #define ATTR_MASK 127 626c797b6c6SAneesh Kumar K.V 627c797b6c6SAneesh Kumar K.V static void virtio_p9_setattr(struct p9_dev *p9dev, 628af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 6291c7850f9SSasha Levin { 630c797b6c6SAneesh Kumar K.V int ret = 0; 631bfc15268SAneesh Kumar K.V u32 fid_val; 632bfc15268SAneesh Kumar K.V struct p9_fid *fid; 633c797b6c6SAneesh Kumar K.V struct p9_iattr_dotl p9attr; 6341c7850f9SSasha Levin 635c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dI", &fid_val, &p9attr); 63631a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 6371c7850f9SSasha Levin 638c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MODE) { 639c797b6c6SAneesh Kumar K.V ret = chmod(fid->abs_path, p9attr.mode); 640c797b6c6SAneesh Kumar K.V if (ret < 0) 641c797b6c6SAneesh Kumar K.V goto err_out; 642c797b6c6SAneesh Kumar K.V } 643c797b6c6SAneesh Kumar K.V if (p9attr.valid & (ATTR_ATIME | ATTR_MTIME)) { 644c797b6c6SAneesh Kumar K.V struct timespec times[2]; 645c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_ATIME) { 646c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_ATIME_SET) { 647c797b6c6SAneesh Kumar K.V times[0].tv_sec = p9attr.atime_sec; 648c797b6c6SAneesh Kumar K.V times[0].tv_nsec = p9attr.atime_nsec; 649c797b6c6SAneesh Kumar K.V } else { 650c797b6c6SAneesh Kumar K.V times[0].tv_nsec = UTIME_NOW; 651c797b6c6SAneesh Kumar K.V } 652c797b6c6SAneesh Kumar K.V } else { 653c797b6c6SAneesh Kumar K.V times[0].tv_nsec = UTIME_OMIT; 654c797b6c6SAneesh Kumar K.V } 655c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MTIME) { 656c797b6c6SAneesh Kumar K.V if (p9attr.valid & ATTR_MTIME_SET) { 657c797b6c6SAneesh Kumar K.V times[1].tv_sec = p9attr.mtime_sec; 658c797b6c6SAneesh Kumar K.V times[1].tv_nsec = p9attr.mtime_nsec; 659c797b6c6SAneesh Kumar K.V } else { 660c797b6c6SAneesh Kumar K.V times[1].tv_nsec = UTIME_NOW; 661c797b6c6SAneesh Kumar K.V } 662c797b6c6SAneesh Kumar K.V } else 663c797b6c6SAneesh Kumar K.V times[1].tv_nsec = UTIME_OMIT; 664c797b6c6SAneesh Kumar K.V 665c797b6c6SAneesh Kumar K.V ret = utimensat(-1, fid->abs_path, times, AT_SYMLINK_NOFOLLOW); 666c797b6c6SAneesh Kumar K.V if (ret < 0) 667c797b6c6SAneesh Kumar K.V goto err_out; 668c797b6c6SAneesh Kumar K.V } 669c797b6c6SAneesh Kumar K.V /* 670c797b6c6SAneesh Kumar K.V * If the only valid entry in iattr is ctime we can call 671c797b6c6SAneesh Kumar K.V * chown(-1,-1) to update the ctime of the file 672c797b6c6SAneesh Kumar K.V */ 673c797b6c6SAneesh Kumar K.V if ((p9attr.valid & (ATTR_UID | ATTR_GID)) || 674c797b6c6SAneesh Kumar K.V ((p9attr.valid & ATTR_CTIME) 675c797b6c6SAneesh Kumar K.V && !((p9attr.valid & ATTR_MASK) & ~ATTR_CTIME))) { 676c797b6c6SAneesh Kumar K.V if (!(p9attr.valid & ATTR_UID)) 677c797b6c6SAneesh Kumar K.V p9attr.uid = -1; 678c797b6c6SAneesh Kumar K.V 679c797b6c6SAneesh Kumar K.V if (!(p9attr.valid & ATTR_GID)) 680c797b6c6SAneesh Kumar K.V p9attr.gid = -1; 681c797b6c6SAneesh Kumar K.V 682c797b6c6SAneesh Kumar K.V ret = lchown(fid->abs_path, p9attr.uid, p9attr.gid); 683c797b6c6SAneesh Kumar K.V if (ret < 0) 684c797b6c6SAneesh Kumar K.V goto err_out; 685c797b6c6SAneesh Kumar K.V } 686c797b6c6SAneesh Kumar K.V if (p9attr.valid & (ATTR_SIZE)) { 687c797b6c6SAneesh Kumar K.V ret = truncate(fid->abs_path, p9attr.size); 688c797b6c6SAneesh Kumar K.V if (ret < 0) 689c797b6c6SAneesh Kumar K.V goto err_out; 690c797b6c6SAneesh Kumar K.V } 6915529bcd7SAsias He *outlen = VIRTIO_9P_HDR_LEN; 692bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 693ead43b01SAneesh Kumar K.V return; 6944bc9734aSAneesh Kumar K.V err_out: 6954bc9734aSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 6964bc9734aSAneesh Kumar K.V return; 6971c7850f9SSasha Levin } 6981c7850f9SSasha Levin 699ead43b01SAneesh Kumar K.V static void virtio_p9_write(struct p9_dev *p9dev, 700af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 7011c7850f9SSasha Levin { 7024bc9734aSAneesh Kumar K.V 703bfc15268SAneesh Kumar K.V u64 offset; 704bfc15268SAneesh Kumar K.V u32 fid_val; 7054bc9734aSAneesh Kumar K.V u32 count; 7064bc9734aSAneesh Kumar K.V ssize_t res; 70750c479e0SAneesh Kumar K.V u16 iov_cnt; 70850c479e0SAneesh Kumar K.V void *iov_base; 70950c479e0SAneesh Kumar K.V size_t iov_len; 710bfc15268SAneesh Kumar K.V struct p9_fid *fid; 711b064b05aSAneesh Kumar K.V /* u32 fid + u64 offset + u32 count */ 712b064b05aSAneesh Kumar K.V int twrite_size = sizeof(u32) + sizeof(u64) + sizeof(u32); 7131c7850f9SSasha Levin 714bfc15268SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dqd", &fid_val, &offset, &count); 71531a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 716af045e53SAneesh Kumar K.V 71750c479e0SAneesh Kumar K.V iov_base = pdu->out_iov[0].iov_base; 71850c479e0SAneesh Kumar K.V iov_len = pdu->out_iov[0].iov_len; 71950c479e0SAneesh Kumar K.V iov_cnt = pdu->out_iov_cnt; 72050c479e0SAneesh Kumar K.V 721bfc15268SAneesh Kumar K.V /* Adjust the iovec to skip the header and meta data */ 722b064b05aSAneesh Kumar K.V pdu->out_iov[0].iov_base += (sizeof(struct p9_msg) + twrite_size); 723b064b05aSAneesh Kumar K.V pdu->out_iov[0].iov_len -= (sizeof(struct p9_msg) + twrite_size); 724bfc15268SAneesh Kumar K.V pdu->out_iov_cnt = virtio_p9_update_iov_cnt(pdu->out_iov, count, 7256b163a87SAneesh Kumar K.V pdu->out_iov_cnt); 7264bc9734aSAneesh Kumar K.V res = pwritev(fid->fd, pdu->out_iov, pdu->out_iov_cnt, offset); 72750c479e0SAneesh Kumar K.V /* 72850c479e0SAneesh Kumar K.V * Update the iov_base back, so that rest of 72950c479e0SAneesh Kumar K.V * pdu_readf works correctly. 73050c479e0SAneesh Kumar K.V */ 73150c479e0SAneesh Kumar K.V pdu->out_iov[0].iov_base = iov_base; 73250c479e0SAneesh Kumar K.V pdu->out_iov[0].iov_len = iov_len; 73350c479e0SAneesh Kumar K.V pdu->out_iov_cnt = iov_cnt; 734c797b6c6SAneesh Kumar K.V 7354bc9734aSAneesh Kumar K.V if (res < 0) 7364bc9734aSAneesh Kumar K.V goto err_out; 7374bc9734aSAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", res); 738bfc15268SAneesh Kumar K.V *outlen = pdu->write_offset; 739bfc15268SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 740ead43b01SAneesh Kumar K.V return; 7414bc9734aSAneesh Kumar K.V err_out: 7424bc9734aSAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 7434bc9734aSAneesh Kumar K.V return; 7441c7850f9SSasha Levin } 7451c7850f9SSasha Levin 7466fc5cd9bSSasha Levin static void virtio_p9_remove(struct p9_dev *p9dev, 7476fc5cd9bSSasha Levin struct p9_pdu *pdu, u32 *outlen) 7486fc5cd9bSSasha Levin { 7496fc5cd9bSSasha Levin int ret; 7506fc5cd9bSSasha Levin u32 fid_val; 7516fc5cd9bSSasha Levin struct p9_fid *fid; 7526fc5cd9bSSasha Levin 7536fc5cd9bSSasha Levin virtio_p9_pdu_readf(pdu, "d", &fid_val); 75431a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 7556fc5cd9bSSasha Levin 7569b604a9cSSasha Levin ret = remove(fid->abs_path); 7576fc5cd9bSSasha Levin if (ret < 0) 7586fc5cd9bSSasha Levin goto err_out; 7596fc5cd9bSSasha Levin *outlen = pdu->write_offset; 7606fc5cd9bSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 7616fc5cd9bSSasha Levin return; 7626fc5cd9bSSasha Levin 7636fc5cd9bSSasha Levin err_out: 7646fc5cd9bSSasha Levin virtio_p9_error_reply(p9dev, pdu, errno, outlen); 7656fc5cd9bSSasha Levin return; 7666fc5cd9bSSasha Levin } 7676fc5cd9bSSasha Levin 768f161f28bSSasha Levin static void virtio_p9_rename(struct p9_dev *p9dev, 769f161f28bSSasha Levin struct p9_pdu *pdu, u32 *outlen) 770f161f28bSSasha Levin { 771f161f28bSSasha Levin int ret; 772f161f28bSSasha Levin u32 fid_val, new_fid_val; 773f161f28bSSasha Levin struct p9_fid *fid, *new_fid; 774f161f28bSSasha Levin char full_path[PATH_MAX], *new_name; 775f161f28bSSasha Levin 776f161f28bSSasha Levin virtio_p9_pdu_readf(pdu, "dds", &fid_val, &new_fid_val, &new_name); 77731a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 77831a6fb8dSSasha Levin new_fid = get_fid(p9dev, new_fid_val); 779f161f28bSSasha Levin 780f161f28bSSasha Levin sprintf(full_path, "%s/%s", new_fid->abs_path, new_name); 781f161f28bSSasha Levin ret = rename(fid->abs_path, full_path); 782f161f28bSSasha Levin if (ret < 0) 783f161f28bSSasha Levin goto err_out; 784f161f28bSSasha Levin *outlen = pdu->write_offset; 785f161f28bSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 786f161f28bSSasha Levin return; 787f161f28bSSasha Levin 788f161f28bSSasha Levin err_out: 789f161f28bSSasha Levin virtio_p9_error_reply(p9dev, pdu, errno, outlen); 790f161f28bSSasha Levin return; 791f161f28bSSasha Levin } 792f161f28bSSasha Levin 793c797b6c6SAneesh Kumar K.V static void virtio_p9_readlink(struct p9_dev *p9dev, 794c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 795c797b6c6SAneesh Kumar K.V { 796c797b6c6SAneesh Kumar K.V int ret; 797c797b6c6SAneesh Kumar K.V u32 fid_val; 798c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 799c797b6c6SAneesh Kumar K.V char target_path[PATH_MAX]; 800c797b6c6SAneesh Kumar K.V 801c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid_val); 80231a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 803c797b6c6SAneesh Kumar K.V 804c797b6c6SAneesh Kumar K.V memset(target_path, 0, PATH_MAX); 805c797b6c6SAneesh Kumar K.V ret = readlink(fid->abs_path, target_path, PATH_MAX - 1); 806c797b6c6SAneesh Kumar K.V if (ret < 0) 807c797b6c6SAneesh Kumar K.V goto err_out; 808c797b6c6SAneesh Kumar K.V 809c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "s", target_path); 810c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 811c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 812c797b6c6SAneesh Kumar K.V return; 813c797b6c6SAneesh Kumar K.V err_out: 814c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 815c797b6c6SAneesh Kumar K.V return; 816c797b6c6SAneesh Kumar K.V } 817c797b6c6SAneesh Kumar K.V 818c797b6c6SAneesh Kumar K.V static void virtio_p9_statfs(struct p9_dev *p9dev, 819c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 820c797b6c6SAneesh Kumar K.V { 821c797b6c6SAneesh Kumar K.V int ret; 822c797b6c6SAneesh Kumar K.V u64 fsid; 823c797b6c6SAneesh Kumar K.V u32 fid_val; 824c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 825c797b6c6SAneesh Kumar K.V struct statfs stat_buf; 826c797b6c6SAneesh Kumar K.V 827c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "d", &fid_val); 82831a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 829c797b6c6SAneesh Kumar K.V 830c797b6c6SAneesh Kumar K.V ret = statfs(fid->abs_path, &stat_buf); 831c797b6c6SAneesh Kumar K.V if (ret < 0) 832c797b6c6SAneesh Kumar K.V goto err_out; 833c797b6c6SAneesh Kumar K.V /* FIXME!! f_blocks needs update based on client msize */ 834c797b6c6SAneesh Kumar K.V fsid = (unsigned int) stat_buf.f_fsid.__val[0] | 835c797b6c6SAneesh Kumar K.V (unsigned long long)stat_buf.f_fsid.__val[1] << 32; 836c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "ddqqqqqqd", stat_buf.f_type, 837c797b6c6SAneesh Kumar K.V stat_buf.f_bsize, stat_buf.f_blocks, 838c797b6c6SAneesh Kumar K.V stat_buf.f_bfree, stat_buf.f_bavail, 839c797b6c6SAneesh Kumar K.V stat_buf.f_files, stat_buf.f_ffree, 840c797b6c6SAneesh Kumar K.V fsid, stat_buf.f_namelen); 841c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 842c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 843c797b6c6SAneesh Kumar K.V return; 844c797b6c6SAneesh Kumar K.V err_out: 845c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 846c797b6c6SAneesh Kumar K.V return; 847c797b6c6SAneesh Kumar K.V } 848c797b6c6SAneesh Kumar K.V 849c797b6c6SAneesh Kumar K.V static void virtio_p9_mknod(struct p9_dev *p9dev, 850c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 851c797b6c6SAneesh Kumar K.V { 852c797b6c6SAneesh Kumar K.V int ret; 853c797b6c6SAneesh Kumar K.V char *name; 854c797b6c6SAneesh Kumar K.V struct stat st; 855c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 856c797b6c6SAneesh Kumar K.V struct p9_qid qid; 857c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 858c797b6c6SAneesh Kumar K.V u32 fid_val, mode, major, minor, gid; 859c797b6c6SAneesh Kumar K.V 860c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsdddd", &fid_val, &name, &mode, 861c797b6c6SAneesh Kumar K.V &major, &minor, &gid); 862c797b6c6SAneesh Kumar K.V 86331a6fb8dSSasha Levin dfid = get_fid(p9dev, fid_val); 864c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 865c797b6c6SAneesh Kumar K.V ret = mknod(full_path, mode, makedev(major, minor)); 866c797b6c6SAneesh Kumar K.V if (ret < 0) 867c797b6c6SAneesh Kumar K.V goto err_out; 868c797b6c6SAneesh Kumar K.V 869c797b6c6SAneesh Kumar K.V if (lstat(full_path, &st) < 0) 870c797b6c6SAneesh Kumar K.V goto err_out; 871c797b6c6SAneesh Kumar K.V 872c797b6c6SAneesh Kumar K.V ret = chmod(full_path, mode & 0777); 873c797b6c6SAneesh Kumar K.V if (ret < 0) 874c797b6c6SAneesh Kumar K.V goto err_out; 875c797b6c6SAneesh Kumar K.V 876c797b6c6SAneesh Kumar K.V ret = lchown(full_path, dfid->uid, gid); 877c797b6c6SAneesh Kumar K.V if (ret < 0) 878c797b6c6SAneesh Kumar K.V goto err_out; 879c797b6c6SAneesh Kumar K.V 880c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 881c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 882c797b6c6SAneesh Kumar K.V free(name); 883c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 884c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 885c797b6c6SAneesh Kumar K.V return; 886c797b6c6SAneesh Kumar K.V err_out: 887c797b6c6SAneesh Kumar K.V free(name); 888c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 889c797b6c6SAneesh Kumar K.V return; 890c797b6c6SAneesh Kumar K.V } 891c797b6c6SAneesh Kumar K.V 892c797b6c6SAneesh Kumar K.V static void virtio_p9_fsync(struct p9_dev *p9dev, 893c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 894c797b6c6SAneesh Kumar K.V { 895c797b6c6SAneesh Kumar K.V int ret; 896c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 897c797b6c6SAneesh Kumar K.V u32 fid_val, datasync; 898c797b6c6SAneesh Kumar K.V 899c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dd", &fid_val, &datasync); 90031a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 901c797b6c6SAneesh Kumar K.V 902c797b6c6SAneesh Kumar K.V if (datasync) 903c797b6c6SAneesh Kumar K.V ret = fdatasync(fid->fd); 904c797b6c6SAneesh Kumar K.V else 905c797b6c6SAneesh Kumar K.V ret = fsync(fid->fd); 906c797b6c6SAneesh Kumar K.V if (ret < 0) 907c797b6c6SAneesh Kumar K.V goto err_out; 908c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 909c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 910c797b6c6SAneesh Kumar K.V return; 911c797b6c6SAneesh Kumar K.V err_out: 912c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 913c797b6c6SAneesh Kumar K.V return; 914c797b6c6SAneesh Kumar K.V } 915c797b6c6SAneesh Kumar K.V 916c797b6c6SAneesh Kumar K.V static void virtio_p9_symlink(struct p9_dev *p9dev, 917c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 918c797b6c6SAneesh Kumar K.V { 919c797b6c6SAneesh Kumar K.V int ret; 920c797b6c6SAneesh Kumar K.V struct stat st; 921c797b6c6SAneesh Kumar K.V u32 fid_val, gid; 922c797b6c6SAneesh Kumar K.V struct p9_qid qid; 923c797b6c6SAneesh Kumar K.V struct p9_fid *dfid; 924c797b6c6SAneesh Kumar K.V char new_name[PATH_MAX]; 925c797b6c6SAneesh Kumar K.V char *old_path, *name; 926c797b6c6SAneesh Kumar K.V 927c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dssd", &fid_val, &name, &old_path, &gid); 928c797b6c6SAneesh Kumar K.V 92931a6fb8dSSasha Levin dfid = get_fid(p9dev, fid_val); 930c797b6c6SAneesh Kumar K.V sprintf(new_name, "%s/%s", dfid->abs_path, name); 931c797b6c6SAneesh Kumar K.V ret = symlink(old_path, new_name); 932c797b6c6SAneesh Kumar K.V if (ret < 0) 933c797b6c6SAneesh Kumar K.V goto err_out; 934c797b6c6SAneesh Kumar K.V 935c797b6c6SAneesh Kumar K.V if (lstat(new_name, &st) < 0) 936c797b6c6SAneesh Kumar K.V goto err_out; 937c797b6c6SAneesh Kumar K.V 938c797b6c6SAneesh Kumar K.V ret = lchown(new_name, dfid->uid, gid); 939c797b6c6SAneesh Kumar K.V if (ret < 0) 940c797b6c6SAneesh Kumar K.V goto err_out; 941c797b6c6SAneesh Kumar K.V 942c797b6c6SAneesh Kumar K.V stat2qid(&st, &qid); 943c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "Q", &qid); 944c797b6c6SAneesh Kumar K.V free(name); 945c797b6c6SAneesh Kumar K.V free(old_path); 946c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 947c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 948c797b6c6SAneesh Kumar K.V return; 949c797b6c6SAneesh Kumar K.V err_out: 950c797b6c6SAneesh Kumar K.V free(name); 951c797b6c6SAneesh Kumar K.V free(old_path); 952c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 953c797b6c6SAneesh Kumar K.V return; 954c797b6c6SAneesh Kumar K.V } 955c797b6c6SAneesh Kumar K.V 956c797b6c6SAneesh Kumar K.V static void virtio_p9_link(struct p9_dev *p9dev, 957c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 958c797b6c6SAneesh Kumar K.V { 959c797b6c6SAneesh Kumar K.V int ret; 960c797b6c6SAneesh Kumar K.V char *name; 961c797b6c6SAneesh Kumar K.V u32 fid_val, dfid_val; 962c797b6c6SAneesh Kumar K.V struct p9_fid *dfid, *fid; 963c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 964c797b6c6SAneesh Kumar K.V 965c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dds", &dfid_val, &fid_val, &name); 966c797b6c6SAneesh Kumar K.V 96731a6fb8dSSasha Levin dfid = get_fid(p9dev, dfid_val); 96831a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 969c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", dfid->abs_path, name); 970c797b6c6SAneesh Kumar K.V ret = link(fid->abs_path, full_path); 971c797b6c6SAneesh Kumar K.V if (ret < 0) 972c797b6c6SAneesh Kumar K.V goto err_out; 973c797b6c6SAneesh Kumar K.V free(name); 974c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 975c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 976c797b6c6SAneesh Kumar K.V return; 977c797b6c6SAneesh Kumar K.V err_out: 978c797b6c6SAneesh Kumar K.V free(name); 979c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 980c797b6c6SAneesh Kumar K.V return; 981c797b6c6SAneesh Kumar K.V 982c797b6c6SAneesh Kumar K.V } 983c797b6c6SAneesh Kumar K.V 984c797b6c6SAneesh Kumar K.V static void virtio_p9_lock(struct p9_dev *p9dev, 985c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 986c797b6c6SAneesh Kumar K.V { 987c797b6c6SAneesh Kumar K.V u8 ret; 988c797b6c6SAneesh Kumar K.V u32 fid_val; 989c797b6c6SAneesh Kumar K.V struct p9_flock flock; 990c797b6c6SAneesh Kumar K.V 991c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dbdqqds", &fid_val, &flock.type, 992c797b6c6SAneesh Kumar K.V &flock.flags, &flock.start, &flock.length, 993c797b6c6SAneesh Kumar K.V &flock.proc_id, &flock.client_id); 994c797b6c6SAneesh Kumar K.V 995c797b6c6SAneesh Kumar K.V /* Just return success */ 996c797b6c6SAneesh Kumar K.V ret = P9_LOCK_SUCCESS; 997c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "d", ret); 998c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 999c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1000c797b6c6SAneesh Kumar K.V free(flock.client_id); 1001c797b6c6SAneesh Kumar K.V return; 1002c797b6c6SAneesh Kumar K.V } 1003c797b6c6SAneesh Kumar K.V 1004c797b6c6SAneesh Kumar K.V static void virtio_p9_getlock(struct p9_dev *p9dev, 1005c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1006c797b6c6SAneesh Kumar K.V { 1007c797b6c6SAneesh Kumar K.V u32 fid_val; 1008c797b6c6SAneesh Kumar K.V struct p9_getlock glock; 1009c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dbqqds", &fid_val, &glock.type, 1010c797b6c6SAneesh Kumar K.V &glock.start, &glock.length, &glock.proc_id, 1011c797b6c6SAneesh Kumar K.V &glock.client_id); 1012c797b6c6SAneesh Kumar K.V 1013c797b6c6SAneesh Kumar K.V /* Just return success */ 1014c797b6c6SAneesh Kumar K.V glock.type = F_UNLCK; 1015c797b6c6SAneesh Kumar K.V virtio_p9_pdu_writef(pdu, "bqqds", glock.type, 1016c797b6c6SAneesh Kumar K.V glock.start, glock.length, glock.proc_id, 1017c797b6c6SAneesh Kumar K.V glock.client_id); 1018c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1019c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1020c797b6c6SAneesh Kumar K.V free(glock.client_id); 1021c797b6c6SAneesh Kumar K.V return; 1022c797b6c6SAneesh Kumar K.V } 1023c797b6c6SAneesh Kumar K.V 1024c797b6c6SAneesh Kumar K.V static int virtio_p9_ancestor(char *path, char *ancestor) 1025c797b6c6SAneesh Kumar K.V { 1026c797b6c6SAneesh Kumar K.V int size = strlen(ancestor); 1027c797b6c6SAneesh Kumar K.V if (!strncmp(path, ancestor, size)) { 1028c797b6c6SAneesh Kumar K.V /* 1029c797b6c6SAneesh Kumar K.V * Now check whether ancestor is a full name or 1030c797b6c6SAneesh Kumar K.V * or directory component and not just part 1031c797b6c6SAneesh Kumar K.V * of a name. 1032c797b6c6SAneesh Kumar K.V */ 1033c797b6c6SAneesh Kumar K.V if (path[size] == '\0' || path[size] == '/') 1034c797b6c6SAneesh Kumar K.V return 1; 1035c797b6c6SAneesh Kumar K.V } 1036c797b6c6SAneesh Kumar K.V return 0; 1037c797b6c6SAneesh Kumar K.V } 1038c797b6c6SAneesh Kumar K.V 1039c797b6c6SAneesh Kumar K.V static void virtio_p9_fix_path(char *fid_path, char *old_name, char *new_name) 1040c797b6c6SAneesh Kumar K.V { 1041c797b6c6SAneesh Kumar K.V char tmp_name[PATH_MAX]; 1042c797b6c6SAneesh Kumar K.V size_t rp_sz = strlen(old_name); 1043c797b6c6SAneesh Kumar K.V 1044c797b6c6SAneesh Kumar K.V if (rp_sz == strlen(fid_path)) { 1045c797b6c6SAneesh Kumar K.V /* replace the full name */ 1046c797b6c6SAneesh Kumar K.V strcpy(fid_path, new_name); 1047c797b6c6SAneesh Kumar K.V return; 1048c797b6c6SAneesh Kumar K.V } 1049c797b6c6SAneesh Kumar K.V /* save the trailing path details */ 1050c797b6c6SAneesh Kumar K.V strcpy(tmp_name, fid_path + rp_sz); 1051c797b6c6SAneesh Kumar K.V sprintf(fid_path, "%s%s", new_name, tmp_name); 1052c797b6c6SAneesh Kumar K.V return; 1053c797b6c6SAneesh Kumar K.V } 1054c797b6c6SAneesh Kumar K.V 1055e2341580SSasha Levin static void rename_fids(struct p9_dev *p9dev, char *old_name, char *new_name) 1056e2341580SSasha Levin { 1057e2341580SSasha Levin struct rb_node *node = rb_first(&p9dev->fids); 1058e2341580SSasha Levin 1059e2341580SSasha Levin while (node) { 1060e2341580SSasha Levin struct p9_fid *fid = rb_entry(node, struct p9_fid, node); 1061e2341580SSasha Levin 1062e2341580SSasha Levin if (fid->fid != P9_NOFID && virtio_p9_ancestor(fid->path, old_name)) { 1063e2341580SSasha Levin virtio_p9_fix_path(fid->path, old_name, new_name); 1064e2341580SSasha Levin } 1065e2341580SSasha Levin node = rb_next(node); 1066e2341580SSasha Levin } 1067e2341580SSasha Levin } 1068e2341580SSasha Levin 1069c797b6c6SAneesh Kumar K.V static void virtio_p9_renameat(struct p9_dev *p9dev, 1070c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1071c797b6c6SAneesh Kumar K.V { 1072e2341580SSasha Levin int ret; 1073c797b6c6SAneesh Kumar K.V char *old_name, *new_name; 1074c797b6c6SAneesh Kumar K.V u32 old_dfid_val, new_dfid_val; 1075c797b6c6SAneesh Kumar K.V struct p9_fid *old_dfid, *new_dfid; 1076c797b6c6SAneesh Kumar K.V char old_full_path[PATH_MAX], new_full_path[PATH_MAX]; 1077c797b6c6SAneesh Kumar K.V 1078c797b6c6SAneesh Kumar K.V 1079c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsds", &old_dfid_val, &old_name, 1080c797b6c6SAneesh Kumar K.V &new_dfid_val, &new_name); 1081c797b6c6SAneesh Kumar K.V 108231a6fb8dSSasha Levin old_dfid = get_fid(p9dev, old_dfid_val); 108331a6fb8dSSasha Levin new_dfid = get_fid(p9dev, new_dfid_val); 1084c797b6c6SAneesh Kumar K.V 1085c797b6c6SAneesh Kumar K.V sprintf(old_full_path, "%s/%s", old_dfid->abs_path, old_name); 1086c797b6c6SAneesh Kumar K.V sprintf(new_full_path, "%s/%s", new_dfid->abs_path, new_name); 1087c797b6c6SAneesh Kumar K.V ret = rename(old_full_path, new_full_path); 1088c797b6c6SAneesh Kumar K.V if (ret < 0) 1089c797b6c6SAneesh Kumar K.V goto err_out; 1090c797b6c6SAneesh Kumar K.V /* 1091c797b6c6SAneesh Kumar K.V * Now fix path in other fids, if the renamed path is part of 1092c797b6c6SAneesh Kumar K.V * that. 1093c797b6c6SAneesh Kumar K.V */ 1094e2341580SSasha Levin rename_fids(p9dev, old_name, new_name); 1095c797b6c6SAneesh Kumar K.V free(old_name); 1096c797b6c6SAneesh Kumar K.V free(new_name); 1097c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1098c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1099c797b6c6SAneesh Kumar K.V return; 1100c797b6c6SAneesh Kumar K.V err_out: 1101c797b6c6SAneesh Kumar K.V free(old_name); 1102c797b6c6SAneesh Kumar K.V free(new_name); 1103c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1104c797b6c6SAneesh Kumar K.V return; 1105c797b6c6SAneesh Kumar K.V } 1106c797b6c6SAneesh Kumar K.V 1107c797b6c6SAneesh Kumar K.V static void virtio_p9_unlinkat(struct p9_dev *p9dev, 1108c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1109c797b6c6SAneesh Kumar K.V { 1110c797b6c6SAneesh Kumar K.V int ret; 1111c797b6c6SAneesh Kumar K.V char *name; 1112c797b6c6SAneesh Kumar K.V u32 fid_val, flags; 1113c797b6c6SAneesh Kumar K.V struct p9_fid *fid; 1114c797b6c6SAneesh Kumar K.V char full_path[PATH_MAX]; 1115c797b6c6SAneesh Kumar K.V 1116c797b6c6SAneesh Kumar K.V virtio_p9_pdu_readf(pdu, "dsd", &fid_val, &name, &flags); 111731a6fb8dSSasha Levin fid = get_fid(p9dev, fid_val); 1118c797b6c6SAneesh Kumar K.V 1119c797b6c6SAneesh Kumar K.V sprintf(full_path, "%s/%s", fid->abs_path, name); 1120c797b6c6SAneesh Kumar K.V ret = remove(full_path); 1121c797b6c6SAneesh Kumar K.V if (ret < 0) 1122c797b6c6SAneesh Kumar K.V goto err_out; 1123c797b6c6SAneesh Kumar K.V free(name); 1124c797b6c6SAneesh Kumar K.V *outlen = pdu->write_offset; 1125c797b6c6SAneesh Kumar K.V virtio_p9_set_reply_header(pdu, *outlen); 1126c797b6c6SAneesh Kumar K.V return; 1127c797b6c6SAneesh Kumar K.V err_out: 1128c797b6c6SAneesh Kumar K.V free(name); 1129c797b6c6SAneesh Kumar K.V virtio_p9_error_reply(p9dev, pdu, errno, outlen); 1130c797b6c6SAneesh Kumar K.V return; 1131c797b6c6SAneesh Kumar K.V } 1132c797b6c6SAneesh Kumar K.V 11335cc808aaSSasha Levin static void virtio_p9_flush(struct p9_dev *p9dev, 11345cc808aaSSasha Levin struct p9_pdu *pdu, u32 *outlen) 11355cc808aaSSasha Levin { 11365cc808aaSSasha Levin u16 tag, oldtag; 11375cc808aaSSasha Levin 11385cc808aaSSasha Levin virtio_p9_pdu_readf(pdu, "ww", &tag, &oldtag); 11395cc808aaSSasha Levin virtio_p9_pdu_writef(pdu, "w", tag); 11405cc808aaSSasha Levin *outlen = pdu->write_offset; 11415cc808aaSSasha Levin virtio_p9_set_reply_header(pdu, *outlen); 11425cc808aaSSasha Levin 11435cc808aaSSasha Levin return; 11445cc808aaSSasha Levin } 11455cc808aaSSasha Levin 1146c797b6c6SAneesh Kumar K.V static void virtio_p9_eopnotsupp(struct p9_dev *p9dev, 1147c797b6c6SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen) 1148c797b6c6SAneesh Kumar K.V { 1149c797b6c6SAneesh Kumar K.V return virtio_p9_error_reply(p9dev, pdu, EOPNOTSUPP, outlen); 1150c797b6c6SAneesh Kumar K.V } 1151c797b6c6SAneesh Kumar K.V 1152ead43b01SAneesh Kumar K.V typedef void p9_handler(struct p9_dev *p9dev, 1153af045e53SAneesh Kumar K.V struct p9_pdu *pdu, u32 *outlen); 1154b4422bf3SAneesh Kumar K.V 1155c797b6c6SAneesh Kumar K.V /* FIXME should be removed when merging with latest linus tree */ 1156c797b6c6SAneesh Kumar K.V #define P9_TRENAMEAT 74 1157c797b6c6SAneesh Kumar K.V #define P9_TUNLINKAT 76 1158c797b6c6SAneesh Kumar K.V 1159c797b6c6SAneesh Kumar K.V static p9_handler *virtio_9p_dotl_handler [] = { 1160c797b6c6SAneesh Kumar K.V [P9_TREADDIR] = virtio_p9_readdir, 1161c797b6c6SAneesh Kumar K.V [P9_TSTATFS] = virtio_p9_statfs, 1162c797b6c6SAneesh Kumar K.V [P9_TGETATTR] = virtio_p9_getattr, 1163c797b6c6SAneesh Kumar K.V [P9_TSETATTR] = virtio_p9_setattr, 1164c797b6c6SAneesh Kumar K.V [P9_TXATTRWALK] = virtio_p9_eopnotsupp, 1165c797b6c6SAneesh Kumar K.V [P9_TXATTRCREATE] = virtio_p9_eopnotsupp, 1166c797b6c6SAneesh Kumar K.V [P9_TMKNOD] = virtio_p9_mknod, 1167c797b6c6SAneesh Kumar K.V [P9_TLOCK] = virtio_p9_lock, 1168c797b6c6SAneesh Kumar K.V [P9_TGETLOCK] = virtio_p9_getlock, 1169c797b6c6SAneesh Kumar K.V [P9_TRENAMEAT] = virtio_p9_renameat, 1170c797b6c6SAneesh Kumar K.V [P9_TREADLINK] = virtio_p9_readlink, 1171c797b6c6SAneesh Kumar K.V [P9_TUNLINKAT] = virtio_p9_unlinkat, 1172c797b6c6SAneesh Kumar K.V [P9_TMKDIR] = virtio_p9_mkdir, 1173b4422bf3SAneesh Kumar K.V [P9_TVERSION] = virtio_p9_version, 1174c797b6c6SAneesh Kumar K.V [P9_TLOPEN] = virtio_p9_open, 1175b4422bf3SAneesh Kumar K.V [P9_TATTACH] = virtio_p9_attach, 1176b4422bf3SAneesh Kumar K.V [P9_TWALK] = virtio_p9_walk, 1177c797b6c6SAneesh Kumar K.V [P9_TCLUNK] = virtio_p9_clunk, 1178c797b6c6SAneesh Kumar K.V [P9_TFSYNC] = virtio_p9_fsync, 1179b4422bf3SAneesh Kumar K.V [P9_TREAD] = virtio_p9_read, 11805cc808aaSSasha Levin [P9_TFLUSH] = virtio_p9_flush, 1181c797b6c6SAneesh Kumar K.V [P9_TLINK] = virtio_p9_link, 1182c797b6c6SAneesh Kumar K.V [P9_TSYMLINK] = virtio_p9_symlink, 1183c797b6c6SAneesh Kumar K.V [P9_TLCREATE] = virtio_p9_create, 1184b4422bf3SAneesh Kumar K.V [P9_TWRITE] = virtio_p9_write, 11856fc5cd9bSSasha Levin [P9_TREMOVE] = virtio_p9_remove, 1186f161f28bSSasha Levin [P9_TRENAME] = virtio_p9_rename, 1187b4422bf3SAneesh Kumar K.V }; 1188b4422bf3SAneesh Kumar K.V 1189af045e53SAneesh Kumar K.V static struct p9_pdu *virtio_p9_pdu_init(struct kvm *kvm, struct virt_queue *vq) 1190af045e53SAneesh Kumar K.V { 1191af045e53SAneesh Kumar K.V struct p9_pdu *pdu = calloc(1, sizeof(*pdu)); 1192af045e53SAneesh Kumar K.V if (!pdu) 1193af045e53SAneesh Kumar K.V return NULL; 1194af045e53SAneesh Kumar K.V 1195bfc15268SAneesh Kumar K.V /* skip the pdu header p9_msg */ 11965529bcd7SAsias He pdu->read_offset = VIRTIO_9P_HDR_LEN; 11975529bcd7SAsias He pdu->write_offset = VIRTIO_9P_HDR_LEN; 1198af045e53SAneesh Kumar K.V pdu->queue_head = virt_queue__get_inout_iov(kvm, vq, pdu->in_iov, 1199a8a44649SAsias He pdu->out_iov, &pdu->in_iov_cnt, &pdu->out_iov_cnt); 1200af045e53SAneesh Kumar K.V return pdu; 1201af045e53SAneesh Kumar K.V } 1202af045e53SAneesh Kumar K.V 1203af045e53SAneesh Kumar K.V static u8 virtio_p9_get_cmd(struct p9_pdu *pdu) 1204af045e53SAneesh Kumar K.V { 1205af045e53SAneesh Kumar K.V struct p9_msg *msg; 1206af045e53SAneesh Kumar K.V /* 1207af045e53SAneesh Kumar K.V * we can peek directly into pdu for a u8 1208af045e53SAneesh Kumar K.V * value. The host endianess won't be an issue 1209af045e53SAneesh Kumar K.V */ 1210af045e53SAneesh Kumar K.V msg = pdu->out_iov[0].iov_base; 1211af045e53SAneesh Kumar K.V return msg->cmd; 1212af045e53SAneesh Kumar K.V } 1213af045e53SAneesh Kumar K.V 1214b4422bf3SAneesh Kumar K.V static bool virtio_p9_do_io_request(struct kvm *kvm, struct p9_dev_job *job) 12151c7850f9SSasha Levin { 1216af045e53SAneesh Kumar K.V u8 cmd; 1217b4422bf3SAneesh Kumar K.V u32 len = 0; 1218b4422bf3SAneesh Kumar K.V p9_handler *handler; 1219b4422bf3SAneesh Kumar K.V struct p9_dev *p9dev; 1220af045e53SAneesh Kumar K.V struct virt_queue *vq; 1221af045e53SAneesh Kumar K.V struct p9_pdu *p9pdu; 12221c7850f9SSasha Levin 1223b4422bf3SAneesh Kumar K.V vq = job->vq; 1224b4422bf3SAneesh Kumar K.V p9dev = job->p9dev; 12251c7850f9SSasha Levin 1226af045e53SAneesh Kumar K.V p9pdu = virtio_p9_pdu_init(kvm, vq); 1227af045e53SAneesh Kumar K.V cmd = virtio_p9_get_cmd(p9pdu); 1228af045e53SAneesh Kumar K.V 1229c797b6c6SAneesh Kumar K.V if ((cmd >= ARRAY_SIZE(virtio_9p_dotl_handler)) || 1230c797b6c6SAneesh Kumar K.V !virtio_9p_dotl_handler[cmd]) 123197b408afSAneesh Kumar K.V handler = virtio_p9_eopnotsupp; 1232dd78d9eaSAneesh Kumar K.V else 1233c797b6c6SAneesh Kumar K.V handler = virtio_9p_dotl_handler[cmd]; 1234c797b6c6SAneesh Kumar K.V 1235af045e53SAneesh Kumar K.V handler(p9dev, p9pdu, &len); 1236af045e53SAneesh Kumar K.V virt_queue__set_used_elem(vq, p9pdu->queue_head, len); 1237af045e53SAneesh Kumar K.V free(p9pdu); 12381c7850f9SSasha Levin return true; 12391c7850f9SSasha Levin } 12401c7850f9SSasha Levin 12411c7850f9SSasha Levin static void virtio_p9_do_io(struct kvm *kvm, void *param) 12421c7850f9SSasha Levin { 1243b4422bf3SAneesh Kumar K.V struct p9_dev_job *job = (struct p9_dev_job *)param; 1244b4422bf3SAneesh Kumar K.V struct p9_dev *p9dev = job->p9dev; 1245b4422bf3SAneesh Kumar K.V struct virt_queue *vq = job->vq; 12461c7850f9SSasha Levin 12471c7850f9SSasha Levin while (virt_queue__available(vq)) { 1248b4422bf3SAneesh Kumar K.V virtio_p9_do_io_request(kvm, job); 124902eca50cSAsias He p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs); 12501c7850f9SSasha Levin } 12511c7850f9SSasha Levin } 12521c7850f9SSasha Levin 1253*c5ae742bSSasha Levin static u8 *get_config(struct kvm *kvm, void *dev) 12541c7850f9SSasha Levin { 1255c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 12561c7850f9SSasha Levin 1257*c5ae742bSSasha Levin return ((u8 *)(p9dev->config)); 1258c7838fbdSSasha Levin } 1259c7838fbdSSasha Levin 1260c7838fbdSSasha Levin static u32 get_host_features(struct kvm *kvm, void *dev) 1261c7838fbdSSasha Levin { 1262c7838fbdSSasha Levin return 1 << VIRTIO_9P_MOUNT_TAG; 1263c7838fbdSSasha Levin } 1264c7838fbdSSasha Levin 1265c7838fbdSSasha Levin static void set_guest_features(struct kvm *kvm, void *dev, u32 features) 1266c7838fbdSSasha Levin { 1267c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1268c7838fbdSSasha Levin 1269c7838fbdSSasha Levin p9dev->features = features; 1270c7838fbdSSasha Levin } 1271c7838fbdSSasha Levin 1272c7838fbdSSasha Levin static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn) 1273c7838fbdSSasha Levin { 1274c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1275b4422bf3SAneesh Kumar K.V struct p9_dev_job *job; 1276b4422bf3SAneesh Kumar K.V struct virt_queue *queue; 1277c7838fbdSSasha Levin void *p; 12781c7850f9SSasha Levin 1279312c62d1SSasha Levin compat__remove_message(compat_id); 1280e59662b3SSasha Levin 1281c7838fbdSSasha Levin queue = &p9dev->vqs[vq]; 1282c7838fbdSSasha Levin queue->pfn = pfn; 12831c7850f9SSasha Levin p = guest_pfn_to_host(kvm, queue->pfn); 1284c7838fbdSSasha Levin job = &p9dev->jobs[vq]; 12851c7850f9SSasha Levin 1286c7838fbdSSasha Levin vring_init(&queue->vring, VIRTQUEUE_NUM, p, VIRTIO_PCI_VRING_ALIGN); 12871c7850f9SSasha Levin 1288b4422bf3SAneesh Kumar K.V *job = (struct p9_dev_job) { 1289b4422bf3SAneesh Kumar K.V .vq = queue, 1290b4422bf3SAneesh Kumar K.V .p9dev = p9dev, 1291b4422bf3SAneesh Kumar K.V }; 1292df0c7f57SSasha Levin thread_pool__init_job(&job->job_id, kvm, virtio_p9_do_io, job); 129360eb42d5SSasha Levin 1294c7838fbdSSasha Levin return 0; 12951c7850f9SSasha Levin } 12961c7850f9SSasha Levin 1297c7838fbdSSasha Levin static int notify_vq(struct kvm *kvm, void *dev, u32 vq) 1298c7838fbdSSasha Levin { 1299c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 13001c7850f9SSasha Levin 1301c7838fbdSSasha Levin thread_pool__do_job(&p9dev->jobs[vq].job_id); 1302c7838fbdSSasha Levin 1303c7838fbdSSasha Levin return 0; 1304c7838fbdSSasha Levin } 1305c7838fbdSSasha Levin 1306c7838fbdSSasha Levin static int get_pfn_vq(struct kvm *kvm, void *dev, u32 vq) 1307c7838fbdSSasha Levin { 1308c7838fbdSSasha Levin struct p9_dev *p9dev = dev; 1309c7838fbdSSasha Levin 1310c7838fbdSSasha Levin return p9dev->vqs[vq].pfn; 1311c7838fbdSSasha Levin } 1312c7838fbdSSasha Levin 1313c7838fbdSSasha Levin static int get_size_vq(struct kvm *kvm, void *dev, u32 vq) 1314c7838fbdSSasha Levin { 1315c7838fbdSSasha Levin return VIRTQUEUE_NUM; 1316c7838fbdSSasha Levin } 1317c7838fbdSSasha Levin 13181c47ce69SSasha Levin struct virtio_ops p9_dev_virtio_ops = (struct virtio_ops) { 1319c7838fbdSSasha Levin .get_config = get_config, 1320c7838fbdSSasha Levin .get_host_features = get_host_features, 1321c7838fbdSSasha Levin .set_guest_features = set_guest_features, 1322c7838fbdSSasha Levin .init_vq = init_vq, 1323c7838fbdSSasha Levin .notify_vq = notify_vq, 1324c7838fbdSSasha Levin .get_pfn_vq = get_pfn_vq, 1325c7838fbdSSasha Levin .get_size_vq = get_size_vq, 1326c7838fbdSSasha Levin }; 13271c47ce69SSasha Levin 13281c47ce69SSasha Levin int virtio_9p__init(struct kvm *kvm) 13291c47ce69SSasha Levin { 13301c47ce69SSasha Levin struct p9_dev *p9dev; 13311c47ce69SSasha Levin 13321c47ce69SSasha Levin list_for_each_entry(p9dev, &devs, list) { 133302eca50cSAsias He virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops, 13345529bcd7SAsias He VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_9P, VIRTIO_ID_9P, PCI_CLASS_9P); 1335c7838fbdSSasha Levin } 1336c7838fbdSSasha Levin 1337c7838fbdSSasha Levin return 0; 1338c7838fbdSSasha Levin } 1339c7838fbdSSasha Levin 1340c7838fbdSSasha Levin int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name) 1341c7838fbdSSasha Levin { 1342c7838fbdSSasha Levin struct p9_dev *p9dev; 134354f6802dSPekka Enberg int err = 0; 13441c7850f9SSasha Levin 1345b4422bf3SAneesh Kumar K.V p9dev = calloc(1, sizeof(*p9dev)); 1346b4422bf3SAneesh Kumar K.V if (!p9dev) 134754f6802dSPekka Enberg return -ENOMEM; 134854f6802dSPekka Enberg 1349b4422bf3SAneesh Kumar K.V if (!tag_name) 13505529bcd7SAsias He tag_name = VIRTIO_9P_DEFAULT_TAG; 135154f6802dSPekka Enberg 1352b4422bf3SAneesh Kumar K.V p9dev->config = calloc(1, sizeof(*p9dev->config) + strlen(tag_name) + 1); 135354f6802dSPekka Enberg if (p9dev->config == NULL) { 135454f6802dSPekka Enberg err = -ENOMEM; 1355b4422bf3SAneesh Kumar K.V goto free_p9dev; 135654f6802dSPekka Enberg } 13571c7850f9SSasha Levin 1358b4422bf3SAneesh Kumar K.V strcpy(p9dev->root_dir, root); 1359b4422bf3SAneesh Kumar K.V p9dev->config->tag_len = strlen(tag_name); 136054f6802dSPekka Enberg if (p9dev->config->tag_len > MAX_TAG_LEN) { 136154f6802dSPekka Enberg err = -EINVAL; 1362b4422bf3SAneesh Kumar K.V goto free_p9dev_config; 136354f6802dSPekka Enberg } 13641c7850f9SSasha Levin 1365c7838fbdSSasha Levin memcpy(&p9dev->config->tag, tag_name, strlen(tag_name)); 13661c7850f9SSasha Levin 1367c7838fbdSSasha Levin list_add(&p9dev->list, &devs); 1368b4422bf3SAneesh Kumar K.V 1369d278197dSAsias He if (compat_id == -1) 137052f34d2cSAsias He compat_id = virtio_compat_add_message("virtio-9p", "CONFIG_NET_9P_VIRTIO"); 1371e59662b3SSasha Levin 137254f6802dSPekka Enberg return err; 137354f6802dSPekka Enberg 1374b4422bf3SAneesh Kumar K.V free_p9dev_config: 1375b4422bf3SAneesh Kumar K.V free(p9dev->config); 1376b4422bf3SAneesh Kumar K.V free_p9dev: 1377b4422bf3SAneesh Kumar K.V free(p9dev); 137854f6802dSPekka Enberg return err; 13791c7850f9SSasha Levin } 1380