diff options
Diffstat (limited to 'hw/9pfs/virtio-9p.c')
-rw-r--r-- | hw/9pfs/virtio-9p.c | 486 |
1 files changed, 394 insertions, 92 deletions
diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index ad70768dce..94b7090e7d 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -22,6 +22,9 @@ #include "virtio-9p-coth.h" int debug_9p_pdu; +int open_fd_hw; +int total_open_fd; +static int open_fd_rc; enum { Oread = 0x00, @@ -232,16 +235,60 @@ static size_t v9fs_string_size(V9fsString *str) return str->size; } -static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid) +/* + * returns 0 if fid got re-opened, 1 if not, < 0 on error */ +static int v9fs_reopen_fid(V9fsState *s, V9fsFidState *f) +{ + int err = 1; + if (f->fid_type == P9_FID_FILE) { + if (f->fs.fd == -1) { + do { + err = v9fs_co_open(s, f, f->open_flags); + } while (err == -EINTR); + } + } else if (f->fid_type == P9_FID_DIR) { + if (f->fs.dir == NULL) { + do { + err = v9fs_co_opendir(s, f); + } while (err == -EINTR); + } + } + return err; +} + +static V9fsFidState *get_fid(V9fsState *s, int32_t fid) { + int err; V9fsFidState *f; for (f = s->fid_list; f; f = f->next) { + BUG_ON(f->clunked); if (f->fid == fid) { + /* + * Update the fid ref upfront so that + * we don't get reclaimed when we yield + * in open later. + */ + f->ref++; + /* + * check whether we need to reopen the + * file. We might have closed the fd + * while trying to free up some file + * descriptors. + */ + err = v9fs_reopen_fid(s, f); + if (err < 0) { + f->ref--; + return NULL; + } + /* + * Mark the fid as referenced so that the LRU + * reclaim won't close the file descriptor + */ + f->flags |= FID_REFERENCED; return f; } } - return NULL; } @@ -249,16 +296,22 @@ static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) { V9fsFidState *f; - f = lookup_fid(s, fid); - if (f) { - return NULL; + for (f = s->fid_list; f; f = f->next) { + /* If fid is already there return NULL */ + BUG_ON(f->clunked); + if (f->fid == fid) { + return NULL; + } } - f = g_malloc0(sizeof(V9fsFidState)); - f->fid = fid; f->fid_type = P9_FID_NONE; - + f->ref = 1; + /* + * Mark the fid as referenced so that the LRU + * reclaim won't close the file descriptor + */ + f->flags |= FID_REFERENCED; f->next = s->fid_list; s->fid_list = f; @@ -299,9 +352,41 @@ free_value: return retval; } -static int free_fid(V9fsState *s, int32_t fid) +static int free_fid(V9fsState *s, V9fsFidState *fidp) { int retval = 0; + + if (fidp->fid_type == P9_FID_FILE) { + /* If we reclaimed the fd no need to close */ + if (fidp->fs.fd != -1) { + retval = v9fs_co_close(s, fidp->fs.fd); + } + } else if (fidp->fid_type == P9_FID_DIR) { + if (fidp->fs.dir != NULL) { + retval = v9fs_co_closedir(s, fidp->fs.dir); + } + } else if (fidp->fid_type == P9_FID_XATTR) { + retval = v9fs_xattr_fid_clunk(s, fidp); + } + v9fs_string_free(&fidp->path); + g_free(fidp); + return retval; +} + +static void put_fid(V9fsState *s, V9fsFidState *fidp) +{ + BUG_ON(!fidp->ref); + fidp->ref--; + /* + * Don't free the fid if it is in reclaim list + */ + if (!fidp->ref && fidp->clunked) { + free_fid(s, fidp); + } +} + +static int clunk_fid(V9fsState *s, int32_t fid) +{ V9fsFidState **fidpp, *fidp; for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) { @@ -313,20 +398,119 @@ static int free_fid(V9fsState *s, int32_t fid) if (*fidpp == NULL) { return -ENOENT; } - fidp = *fidpp; *fidpp = fidp->next; + fidp->clunked = 1; + return 0; +} - if (fidp->fid_type == P9_FID_FILE) { - retval = v9fs_co_close(s, fidp); - } else if (fidp->fid_type == P9_FID_DIR) { - retval = v9fs_co_closedir(s, fidp); - } else if (fidp->fid_type == P9_FID_XATTR) { - retval = v9fs_xattr_fid_clunk(s, fidp); +void v9fs_reclaim_fd(V9fsState *s) +{ + int reclaim_count = 0; + V9fsFidState *f, *reclaim_list = NULL; + + for (f = s->fid_list; f; f = f->next) { + /* + * Unlink fids cannot be reclaimed. Check + * for them and skip them. Also skip fids + * currently being operated on. + */ + if (f->ref || f->flags & FID_NON_RECLAIMABLE) { + continue; + } + /* + * if it is a recently referenced fid + * we leave the fid untouched and clear the + * reference bit. We come back to it later + * in the next iteration. (a simple LRU without + * moving list elements around) + */ + if (f->flags & FID_REFERENCED) { + f->flags &= ~FID_REFERENCED; + continue; + } + /* + * Add fids to reclaim list. + */ + if (f->fid_type == P9_FID_FILE) { + if (f->fs.fd != -1) { + /* + * Up the reference count so that + * a clunk request won't free this fid + */ + f->ref++; + f->rclm_lst = reclaim_list; + reclaim_list = f; + f->fs_reclaim.fd = f->fs.fd; + f->fs.fd = -1; + reclaim_count++; + } + } else if (f->fid_type == P9_FID_DIR) { + if (f->fs.dir != NULL) { + /* + * Up the reference count so that + * a clunk request won't free this fid + */ + f->ref++; + f->rclm_lst = reclaim_list; + reclaim_list = f; + f->fs_reclaim.dir = f->fs.dir; + f->fs.dir = NULL; + reclaim_count++; + } + } + if (reclaim_count >= open_fd_rc) { + break; + } + } + /* + * Now close the fid in reclaim list. Free them if they + * are already clunked. + */ + while (reclaim_list) { + f = reclaim_list; + reclaim_list = f->rclm_lst; + if (f->fid_type == P9_FID_FILE) { + v9fs_co_close(s, f->fs_reclaim.fd); + } else if (f->fid_type == P9_FID_DIR) { + v9fs_co_closedir(s, f->fs_reclaim.dir); + } + f->rclm_lst = NULL; + /* + * Now drop the fid reference, free it + * if clunked. + */ + put_fid(s, f); } - v9fs_string_free(&fidp->path); - g_free(fidp); - return retval; +} + +static int v9fs_mark_fids_unreclaim(V9fsState *s, V9fsString *str) +{ + int err; + V9fsFidState *fidp, head_fid; + + head_fid.next = s->fid_list; + for (fidp = s->fid_list; fidp; fidp = fidp->next) { + if (!strcmp(fidp->path.data, str->data)) { + /* Mark the fid non reclaimable. */ + fidp->flags |= FID_NON_RECLAIMABLE; + + /* reopen the file/dir if already closed */ + err = v9fs_reopen_fid(s, fidp); + if (err < 0) { + return -1; + } + /* + * Go back to head of fid list because + * the list could have got updated when + * switched to the worker thread + */ + if (err == 0) { + fidp = &head_fid; + } + } + } + return 0; } #define P9_QID_TYPE_DIR 0x80 @@ -1014,19 +1198,21 @@ static void v9fs_attach(void *opaque) fidp = alloc_fid(s, fid); if (fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } fidp->uid = n_uname; v9fs_string_sprintf(&fidp->path, "%s", "/"); err = fid_to_qid(s, fidp, &qid); if (err < 0) { err = -EINVAL; - free_fid(s, fid); + clunk_fid(s, fid); goto out; } offset += pdu_marshal(pdu, offset, "Q", &qid); err = offset; out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); v9fs_string_free(&uname); v9fs_string_free(&aname); @@ -1044,10 +1230,11 @@ static void v9fs_stat(void *opaque) V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "d", &fid); - fidp = lookup_fid(s, fid); + + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } err = v9fs_co_lstat(s, &fidp->path, &stbuf); if (err < 0) { @@ -1061,6 +1248,8 @@ static void v9fs_stat(void *opaque) err = offset; v9fs_stat_free(&v9stat); out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1078,10 +1267,10 @@ static void v9fs_getattr(void *opaque) pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { retval = -ENOENT; - goto out; + goto out_nofid; } /* * Currently we only support BASIC fields in stat, so there is no @@ -1095,6 +1284,8 @@ static void v9fs_getattr(void *opaque) retval = offset; retval += pdu_marshal(pdu, offset, "A", &v9stat_dotl); out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, retval); } @@ -1122,10 +1313,10 @@ static void v9fs_setattr(void *opaque) pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } if (v9iattr.valid & ATTR_MODE) { err = v9fs_co_chmod(s, &fidp->path, v9iattr.mode); @@ -1187,6 +1378,8 @@ static void v9fs_setattr(void *opaque) } err = offset; out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1213,7 +1406,7 @@ static void v9fs_walk(void *opaque) int32_t fid, newfid; V9fsString *wnames = NULL; V9fsFidState *fidp; - V9fsFidState *newfidp; + V9fsFidState *newfidp = NULL;; V9fsPDU *pdu = opaque; V9fsState *s = pdu->s; @@ -1229,12 +1422,12 @@ static void v9fs_walk(void *opaque) } else if (nwnames > P9_MAXWELEM) { err = -EINVAL; - goto out; + goto out_nofid; } - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } if (fid == newfid) { BUG_ON(fidp->fid_type != P9_FID_NONE); @@ -1267,7 +1460,7 @@ static void v9fs_walk(void *opaque) v9fs_string_copy(&newfidp->path, &path); err = v9fs_co_lstat(s, &newfidp->path, &stbuf); if (err < 0) { - free_fid(s, newfidp->fid); + clunk_fid(s, newfidp->fid); v9fs_string_free(&path); goto out; } @@ -1277,6 +1470,11 @@ static void v9fs_walk(void *opaque) } err = v9fs_walk_marshal(pdu, nwnames, qids); out: + put_fid(s, fidp); + if (newfidp) { + put_fid(s, newfidp); + } +out_nofid: complete_pdu(s, pdu, err); if (nwnames && nwnames <= P9_MAXWELEM) { for (name_idx = 0; name_idx < nwnames; name_idx++) { @@ -1325,10 +1523,10 @@ static void v9fs_open(void *opaque) } else { pdu_unmarshal(pdu, offset, "db", &fid, &mode); } - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } BUG_ON(fidp->fid_type != P9_FID_NONE); @@ -1359,11 +1557,21 @@ static void v9fs_open(void *opaque) goto out; } fidp->fid_type = P9_FID_FILE; + fidp->open_flags = flags; + if (flags & O_EXCL) { + /* + * We let the host file system do O_EXCL check + * We should not reclaim such fd + */ + fidp->flags |= FID_NON_RECLAIMABLE; + } iounit = get_iounit(s, &fidp->path); offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit); err = offset; } out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1385,10 +1593,10 @@ static void v9fs_lcreate(void *opaque) pdu_unmarshal(pdu, offset, "dsddd", &dfid, &name, &flags, &mode, &gid); - fidp = lookup_fid(pdu->s, dfid); + fidp = get_fid(pdu->s, dfid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data); @@ -1400,13 +1608,21 @@ static void v9fs_lcreate(void *opaque) goto out; } fidp->fid_type = P9_FID_FILE; + fidp->open_flags = flags; + if (flags & O_EXCL) { + /* + * We let the host file system do O_EXCL check + * We should not reclaim such fd + */ + fidp->flags |= FID_NON_RECLAIMABLE; + } iounit = get_iounit(pdu->s, &fullname); err = v9fs_co_lstat(pdu->s, &fullname, &stbuf); if (err < 0) { fidp->fid_type = P9_FID_NONE; if (fidp->fs.fd > 0) { - close(fidp->fs.fd); + v9fs_co_close(pdu->s, fidp->fs.fd); } goto out; } @@ -1415,6 +1631,8 @@ static void v9fs_lcreate(void *opaque) offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit); err = offset; out: + put_fid(pdu->s, fidp); +out_nofid: complete_pdu(pdu->s, pdu, err); v9fs_string_free(&name); v9fs_string_free(&fullname); @@ -1431,16 +1649,17 @@ static void v9fs_fsync(void *opaque) V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } err = v9fs_co_fsync(s, fidp, datasync); if (!err) { err = offset; } -out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1449,16 +1668,25 @@ static void v9fs_clunk(void *opaque) int err; int32_t fid; size_t offset = 7; + V9fsFidState *fidp; V9fsPDU *pdu = opaque; V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "d", &fid); - err = free_fid(s, fid); + + fidp = get_fid(s, fid); + if (fidp == NULL) { + err = -ENOENT; + goto out_nofid; + } + err = clunk_fid(s, fidp->fid); if (err < 0) { goto out; } err = offset; out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1557,10 +1785,11 @@ static void v9fs_read(void *opaque) V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); - fidp = lookup_fid(s, fid); + + fidp = get_fid(s, fid); if (fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } if (fidp->fid_type == P9_FID_DIR) { @@ -1612,6 +1841,8 @@ static void v9fs_read(void *opaque) err = -EINVAL; } out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1696,8 +1927,12 @@ static void v9fs_readdir(void *opaque) pdu_unmarshal(pdu, offset, "dqd", &fid, &initial_offset, &max_count); - fidp = lookup_fid(s, fid); - if (fidp == NULL || !fidp->fs.dir) { + fidp = get_fid(s, fid); + if (fidp == NULL) { + retval = -EINVAL; + goto out_nofid; + } + if (!fidp->fs.dir) { retval = -EINVAL; goto out; } @@ -1715,6 +1950,8 @@ static void v9fs_readdir(void *opaque) retval += pdu_marshal(pdu, offset, "d", count); retval += count; out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, retval); } @@ -1779,10 +2016,11 @@ static void v9fs_write(void *opaque) V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "dqdv", &fid, &off, &count, sg, &cnt); - fidp = lookup_fid(s, fid); + + fidp = get_fid(s, fid); if (fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } if (fidp->fid_type == P9_FID_FILE) { if (fidp->fs.fd == -1) { @@ -1822,6 +2060,8 @@ static void v9fs_write(void *opaque) offset += pdu_marshal(pdu, offset, "d", total); err = offset; out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1846,10 +2086,10 @@ static void v9fs_create(void *opaque) pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, &perm, &mode, &extension); - fidp = lookup_fid(pdu->s, fid); + fidp = get_fid(pdu->s, fid); if (fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data); @@ -1879,15 +2119,17 @@ static void v9fs_create(void *opaque) } } else if (perm & P9_STAT_MODE_LINK) { int32_t nfid = atoi(extension.data); - V9fsFidState *nfidp = lookup_fid(pdu->s, nfid); + V9fsFidState *nfidp = get_fid(pdu->s, nfid); if (nfidp == NULL) { err = -EINVAL; goto out; } err = v9fs_co_link(pdu->s, &nfidp->path, &fullname); if (err < 0) { + put_fid(pdu->s, nfidp); goto out; } + put_fid(pdu->s, nfidp); } else if (perm & P9_STAT_MODE_DEVICE) { char ctype; uint32_t major, minor; @@ -1935,12 +2177,20 @@ static void v9fs_create(void *opaque) goto out; } fidp->fid_type = P9_FID_FILE; + fidp->open_flags = omode_to_uflags(mode); + if (fidp->open_flags & O_EXCL) { + /* + * We let the host file system do O_EXCL check + * We should not reclaim such fd + */ + fidp->flags |= FID_NON_RECLAIMABLE; + } } err = v9fs_co_lstat(pdu->s, &fullname, &stbuf); if (err < 0) { fidp->fid_type = P9_FID_NONE; if (fidp->fs.fd) { - close(fidp->fs.fd); + v9fs_co_close(pdu->s, fidp->fs.fd); } goto out; } @@ -1950,6 +2200,8 @@ static void v9fs_create(void *opaque) offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit); err = offset; out: + put_fid(pdu->s, fidp); +out_nofid: complete_pdu(pdu->s, pdu, err); v9fs_string_free(&name); v9fs_string_free(&extension); @@ -1973,10 +2225,10 @@ static void v9fs_symlink(void *opaque) v9fs_string_init(&fullname); pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); - dfidp = lookup_fid(pdu->s, dfid); + dfidp = get_fid(pdu->s, dfid); if (dfidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data); @@ -1992,6 +2244,8 @@ static void v9fs_symlink(void *opaque) offset += pdu_marshal(pdu, offset, "Q", &qid); err = offset; out: + put_fid(pdu->s, dfidp); +out_nofid: complete_pdu(pdu->s, pdu, err); v9fs_string_free(&name); v9fs_string_free(&symname); @@ -2021,13 +2275,13 @@ static void v9fs_link(void *opaque) pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); - dfidp = lookup_fid(s, dfid); + dfidp = get_fid(s, dfid); if (dfidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } - oldfidp = lookup_fid(s, oldfid); + oldfidp = get_fid(s, oldfid); if (oldfidp == NULL) { err = -ENOENT; goto out; @@ -2041,6 +2295,8 @@ static void v9fs_link(void *opaque) v9fs_string_free(&fullname); out: + put_fid(s, dfidp); +out_nofid: v9fs_string_free(&name); complete_pdu(s, pdu, err); } @@ -2055,19 +2311,28 @@ static void v9fs_remove(void *opaque) pdu_unmarshal(pdu, offset, "d", &fid); - fidp = lookup_fid(pdu->s, fid); + fidp = get_fid(pdu->s, fid); if (fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; + } + /* + * IF the file is unlinked, we cannot reopen + * the file later. So don't reclaim fd + */ + err = v9fs_mark_fids_unreclaim(pdu->s, &fidp->path); + if (err < 0) { + goto out_err; } err = v9fs_co_remove(pdu->s, &fidp->path); if (!err) { err = offset; } - +out_err: /* For TREMOVE we need to clunk the fid even on failed remove */ - free_fid(pdu->s, fidp->fid); -out: + clunk_fid(pdu->s, fidp->fid); + put_fid(pdu->s, fidp); +out_nofid: complete_pdu(pdu->s, pdu, err); } @@ -2076,14 +2341,14 @@ static int v9fs_complete_rename(V9fsState *s, V9fsFidState *fidp, { char *end; int err = 0; + V9fsFidState *dirfidp = NULL; char *old_name, *new_name; if (newdirfid != -1) { - V9fsFidState *dirfidp; - dirfidp = lookup_fid(s, newdirfid); + dirfidp = get_fid(s, newdirfid); if (dirfidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } BUG_ON(dirfidp->fid_type != P9_FID_NONE); @@ -2136,6 +2401,10 @@ static int v9fs_complete_rename(V9fsState *s, V9fsFidState *fidp, v9fs_string_copy(&fidp->path, name); } out: + if (dirfidp) { + put_fid(s, dirfidp); + } +out_nofid: return err; } @@ -2152,10 +2421,10 @@ static void v9fs_rename(void *opaque) pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } BUG_ON(fidp->fid_type != P9_FID_NONE); @@ -2163,7 +2432,8 @@ static void v9fs_rename(void *opaque) if (!err) { err = offset; } -out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); v9fs_string_free(&name); } @@ -2181,10 +2451,11 @@ static void v9fs_wstat(void *opaque) V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); - fidp = lookup_fid(s, fid); + + fidp = get_fid(s, fid); if (fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } /* do we need to sync the file? */ if (donttouch_stat(&v9stat)) { @@ -2250,6 +2521,8 @@ static void v9fs_wstat(void *opaque) } err = offset; out: + put_fid(s, fidp); +out_nofid: v9fs_stat_free(&v9stat); complete_pdu(s, pdu, err); } @@ -2310,10 +2583,10 @@ static void v9fs_statfs(void *opaque) V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "d", &fid); - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { retval = -ENOENT; - goto out; + goto out_nofid; } retval = v9fs_co_statfs(s, &fidp->path, &stbuf); if (retval < 0) { @@ -2322,6 +2595,8 @@ static void v9fs_statfs(void *opaque) retval = offset; retval += v9fs_fill_statfs(s, pdu, &stbuf); out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, retval); return; } @@ -2347,10 +2622,10 @@ static void v9fs_mknod(void *opaque) pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, &major, &minor, &gid); - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data); err = v9fs_co_mknod(s, &fullname, fidp->uid, gid, @@ -2366,6 +2641,8 @@ static void v9fs_mknod(void *opaque) err = offset; err += pdu_marshal(pdu, offset, "Q", &qid); out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); v9fs_string_free(&fullname); v9fs_string_free(&name); @@ -2399,12 +2676,12 @@ static void v9fs_lock(void *opaque) /* We support only block flag now (that too ignored currently) */ if (flock->flags & ~P9_LOCK_FLAGS_BLOCK) { err = -EINVAL; - goto out; + goto out_nofid; } - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf); if (err < 0) { @@ -2412,6 +2689,8 @@ static void v9fs_lock(void *opaque) } status = P9_LOCK_SUCCESS; out: + put_fid(s, fidp); +out_nofid: err = offset; err += pdu_marshal(pdu, offset, "b", status); complete_pdu(s, pdu, err); @@ -2437,10 +2716,10 @@ static void v9fs_getlock(void *opaque) &glock->start, &glock->length, &glock->proc_id, &glock->client_id); - fidp = lookup_fid(s, fid); + fidp = get_fid(s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf); if (err < 0) { @@ -2452,6 +2731,8 @@ static void v9fs_getlock(void *opaque) &glock->client_id); err = offset; out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); g_free(glock); } @@ -2472,10 +2753,10 @@ static void v9fs_mkdir(void *opaque) v9fs_string_init(&fullname); pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); - fidp = lookup_fid(pdu->s, fid); + fidp = get_fid(pdu->s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data); err = v9fs_co_mkdir(pdu->s, fullname.data, mode, fidp->uid, gid); @@ -2490,6 +2771,8 @@ static void v9fs_mkdir(void *opaque) offset += pdu_marshal(pdu, offset, "Q", &qid); err = offset; out: + put_fid(pdu->s, fidp); +out_nofid: complete_pdu(pdu->s, pdu, err); v9fs_string_free(&fullname); v9fs_string_free(&name); @@ -2503,15 +2786,15 @@ static void v9fs_xattrwalk(void *opaque) size_t offset = 7; int32_t fid, newfid; V9fsFidState *file_fidp; - V9fsFidState *xattr_fidp; + V9fsFidState *xattr_fidp = NULL; V9fsPDU *pdu = opaque; V9fsState *s = pdu->s; pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); - file_fidp = lookup_fid(s, fid); + file_fidp = get_fid(s, fid); if (file_fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } xattr_fidp = alloc_fid(s, newfid); if (xattr_fidp == NULL) { @@ -2526,7 +2809,7 @@ static void v9fs_xattrwalk(void *opaque) size = v9fs_co_llistxattr(s, &xattr_fidp->path, NULL, 0); if (size < 0) { err = size; - free_fid(s, xattr_fidp->fid); + clunk_fid(s, xattr_fidp->fid); goto out; } /* @@ -2541,7 +2824,7 @@ static void v9fs_xattrwalk(void *opaque) xattr_fidp->fs.xattr.value, xattr_fidp->fs.xattr.len); if (err < 0) { - free_fid(s, xattr_fidp->fid); + clunk_fid(s, xattr_fidp->fid); goto out; } } @@ -2556,7 +2839,7 @@ static void v9fs_xattrwalk(void *opaque) &name, NULL, 0); if (size < 0) { err = size; - free_fid(s, xattr_fidp->fid); + clunk_fid(s, xattr_fidp->fid); goto out; } /* @@ -2571,7 +2854,7 @@ static void v9fs_xattrwalk(void *opaque) &name, xattr_fidp->fs.xattr.value, xattr_fidp->fs.xattr.len); if (err < 0) { - free_fid(s, xattr_fidp->fid); + clunk_fid(s, xattr_fidp->fid); goto out; } } @@ -2579,6 +2862,11 @@ static void v9fs_xattrwalk(void *opaque) err = offset; } out: + put_fid(s, file_fidp); + if (xattr_fidp) { + put_fid(s, xattr_fidp); + } +out_nofid: complete_pdu(s, pdu, err); v9fs_string_free(&name); } @@ -2599,10 +2887,10 @@ static void v9fs_xattrcreate(void *opaque) pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); - file_fidp = lookup_fid(s, fid); + file_fidp = get_fid(s, fid); if (file_fidp == NULL) { err = -EINVAL; - goto out; + goto out_nofid; } /* Make the file fid point to xattr */ xattr_fidp = file_fidp; @@ -2618,7 +2906,8 @@ static void v9fs_xattrcreate(void *opaque) xattr_fidp->fs.xattr.value = NULL; } err = offset; -out: + put_fid(s, file_fidp); +out_nofid: complete_pdu(s, pdu, err); v9fs_string_free(&name); } @@ -2633,10 +2922,10 @@ static void v9fs_readlink(void *opaque) V9fsFidState *fidp; pdu_unmarshal(pdu, offset, "d", &fid); - fidp = lookup_fid(pdu->s, fid); + fidp = get_fid(pdu->s, fid); if (fidp == NULL) { err = -ENOENT; - goto out; + goto out_nofid; } v9fs_string_init(&target); @@ -2648,6 +2937,8 @@ static void v9fs_readlink(void *opaque) err = offset; v9fs_string_free(&target); out: + put_fid(pdu->s, fidp); +out_nofid: complete_pdu(pdu->s, pdu, err); } @@ -2732,3 +3023,14 @@ void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) } free_pdu(s, pdu); } + +void virtio_9p_set_fd_limit(void) +{ + struct rlimit rlim; + if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { + fprintf(stderr, "Failed to get the resource limit\n"); + exit(1); + } + open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3); + open_fd_rc = rlim.rlim_cur/2; +} |