From 84dfb926e508aeedd5088a4f029464bf2d5b3791 Mon Sep 17 00:00:00 2001 From: "Aneesh Kumar K.V" Date: Wed, 18 May 2011 17:38:07 +0530 Subject: hw/9pfs: Add reference counting for fid Signed-off-by: Aneesh Kumar K.V --- hw/9pfs/virtio-9p.c | 273 ++++++++++++++++++++++++++++++++++------------------ hw/9pfs/virtio-9p.h | 2 + 2 files changed, 184 insertions(+), 91 deletions(-) diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index ad70768dce..555f456111 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -232,16 +232,17 @@ static size_t v9fs_string_size(V9fsString *str) return str->size; } -static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid) +static V9fsFidState *get_fid(V9fsState *s, int32_t fid) { V9fsFidState *f; for (f = s->fid_list; f; f = f->next) { + BUG_ON(f->clunked); if (f->fid == fid) { + f->ref++; return f; } } - return NULL; } @@ -249,16 +250,17 @@ 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; f->next = s->fid_list; s->fid_list = f; @@ -299,9 +301,33 @@ 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) { + 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); + } + v9fs_string_free(&fidp->path); + g_free(fidp); + return retval; +} + +static void put_fid(V9fsState *s, V9fsFidState *fidp) +{ + BUG_ON(!fidp->ref); + fidp->ref--; + 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 +339,10 @@ static int free_fid(V9fsState *s, int32_t fid) if (*fidpp == NULL) { return -ENOENT; } - fidp = *fidpp; *fidpp = fidp->next; - - 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); - } - v9fs_string_free(&fidp->path); - g_free(fidp); - return retval; + fidp->clunked = 1; + return 0; } #define P9_QID_TYPE_DIR 0x80 @@ -1014,19 +1030,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 +1062,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 +1080,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 +1099,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 +1116,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 +1145,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 +1210,8 @@ static void v9fs_setattr(void *opaque) } err = offset; out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1213,7 +1238,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 +1254,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 +1292,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 +1302,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 +1355,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); @@ -1364,6 +1394,8 @@ static void v9fs_open(void *opaque) err = offset; } out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1385,10 +1417,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); @@ -1415,6 +1447,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 +1465,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 +1484,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 +1601,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 +1657,8 @@ static void v9fs_read(void *opaque) err = -EINVAL; } out: + put_fid(s, fidp); +out_nofid: complete_pdu(s, pdu, err); } @@ -1696,8 +1743,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 +1766,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 +1832,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 +1876,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 +1902,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 +1935,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; @@ -1950,6 +2008,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 +2033,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 +2052,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 +2083,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 +2103,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,10 +2119,10 @@ 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; } err = v9fs_co_remove(pdu->s, &fidp->path); if (!err) { @@ -2066,8 +2130,9 @@ static void v9fs_remove(void *opaque) } /* 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 +2141,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 +2201,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 +2221,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 +2232,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 +2251,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 +2321,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 +2383,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 +2395,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 +2422,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 +2441,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 +2476,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 +2489,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 +2516,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 +2531,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 +2553,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 +2571,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 +2586,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 +2609,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 +2624,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 +2639,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 +2654,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 +2662,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 +2687,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 +2706,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 +2722,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 +2737,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); } diff --git a/hw/9pfs/virtio-9p.h b/hw/9pfs/virtio-9p.h index 1d8c1b1093..e16e5f46a1 100644 --- a/hw/9pfs/virtio-9p.h +++ b/hw/9pfs/virtio-9p.h @@ -203,6 +203,8 @@ struct V9fsFidState V9fsXattr xattr; } fs; uid_t uid; + int ref; + int clunked; V9fsFidState *next; }; -- cgit v1.2.1 From 7a46274529c2a1866e4344b4385739db674d6a35 Mon Sep 17 00:00:00 2001 From: "Aneesh Kumar K.V" Date: Wed, 18 May 2011 15:40:57 +0530 Subject: hw/9pfs: Add file descriptor reclaim support [M. Mohan Kumar removed some unused variables] Signed-off-by: Aneesh Kumar K.V --- hw/9pfs/codir.c | 4 +- hw/9pfs/cofile.c | 19 ++++- hw/9pfs/virtio-9p-coth.h | 4 +- hw/9pfs/virtio-9p-device.c | 1 + hw/9pfs/virtio-9p.c | 187 ++++++++++++++++++++++++++++++++++++++++++++- hw/9pfs/virtio-9p.h | 22 +++++- 6 files changed, 223 insertions(+), 14 deletions(-) diff --git a/hw/9pfs/codir.c b/hw/9pfs/codir.c index 3a257b9027..86adc87adc 100644 --- a/hw/9pfs/codir.c +++ b/hw/9pfs/codir.c @@ -100,12 +100,10 @@ int v9fs_co_opendir(V9fsState *s, V9fsFidState *fidp) return err; } -int v9fs_co_closedir(V9fsState *s, V9fsFidState *fidp) +int v9fs_co_closedir(V9fsState *s, DIR *dir) { int err; - DIR *dir; - dir = fidp->fs.dir; v9fs_co_run_in_worker( { err = s->ops->closedir(&s->ctx, dir); diff --git a/hw/9pfs/cofile.c b/hw/9pfs/cofile.c index e388146422..0caf1e3cee 100644 --- a/hw/9pfs/cofile.c +++ b/hw/9pfs/cofile.c @@ -58,6 +58,12 @@ int v9fs_co_open(V9fsState *s, V9fsFidState *fidp, int flags) err = 0; } }); + if (!err) { + total_open_fd++; + if (total_open_fd > open_fd_hw) { + v9fs_reclaim_fd(s); + } + } return err; } @@ -79,15 +85,19 @@ int v9fs_co_open2(V9fsState *s, V9fsFidState *fidp, char *fullname, gid_t gid, err = -errno; } }); + if (!err) { + total_open_fd++; + if (total_open_fd > open_fd_hw) { + v9fs_reclaim_fd(s); + } + } return err; } -int v9fs_co_close(V9fsState *s, V9fsFidState *fidp) +int v9fs_co_close(V9fsState *s, int fd) { - int fd; int err; - fd = fidp->fs.fd; v9fs_co_run_in_worker( { err = s->ops->close(&s->ctx, fd); @@ -95,6 +105,9 @@ int v9fs_co_close(V9fsState *s, V9fsFidState *fidp) err = -errno; } }); + if (!err) { + total_open_fd--; + } return err; } diff --git a/hw/9pfs/virtio-9p-coth.h b/hw/9pfs/virtio-9p-coth.h index 48defb72ff..b7be9b5f1f 100644 --- a/hw/9pfs/virtio-9p-coth.h +++ b/hw/9pfs/virtio-9p-coth.h @@ -83,8 +83,8 @@ extern int v9fs_co_open2(V9fsState *, V9fsFidState *, char *, gid_t, int, int); extern int v9fs_co_lsetxattr(V9fsState *, V9fsString *, V9fsString *, void *, size_t, int); extern int v9fs_co_lremovexattr(V9fsState *, V9fsString *, V9fsString *); -extern int v9fs_co_closedir(V9fsState *, V9fsFidState *); -extern int v9fs_co_close(V9fsState *, V9fsFidState *); +extern int v9fs_co_closedir(V9fsState *, DIR *); +extern int v9fs_co_close(V9fsState *, int); extern int v9fs_co_fsync(V9fsState *, V9fsFidState *, int); extern int v9fs_co_symlink(V9fsState *, V9fsFidState *, const char *, const char *, gid_t); diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c index 94660024d6..44b9ace0af 100644 --- a/hw/9pfs/virtio-9p-device.c +++ b/hw/9pfs/virtio-9p-device.c @@ -171,6 +171,7 @@ static PCIDeviceInfo virtio_9p_info = { static void virtio_9p_register_devices(void) { pci_qdev_register(&virtio_9p_info); + virtio_9p_set_fd_limit(); } device_init(virtio_9p_register_devices) diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 555f456111..4ba2590566 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, @@ -234,12 +237,41 @@ static size_t v9fs_string_size(V9fsString *str) 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. + */ + 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); + 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; } } @@ -261,6 +293,11 @@ static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) 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; @@ -306,9 +343,12 @@ static int free_fid(V9fsState *s, V9fsFidState *fidp) int retval = 0; if (fidp->fid_type == P9_FID_FILE) { - retval = v9fs_co_close(s, fidp); + /* 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) { - retval = v9fs_co_closedir(s, fidp); + retval = v9fs_co_closedir(s, fidp->fs.dir); } else if (fidp->fid_type == P9_FID_XATTR) { retval = v9fs_xattr_fid_clunk(s, fidp); } @@ -321,6 +361,9 @@ 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); } @@ -345,6 +388,101 @@ static int clunk_fid(V9fsState *s, int32_t fid) return 0; } +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++; + } + } + 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); + } + f->rclm_lst = NULL; + /* + * Now drop the fid reference, free it + * if clunked. + */ + put_fid(s, f); + } +} + +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 if already closed */ + if (fidp->fs.fd == -1) { + do { + err = v9fs_co_open(s, fidp, fidp->open_flags); + } while (err == -EINTR); + 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 + */ + fidp = &head_fid; + } + } + } + return 0; +} + #define P9_QID_TYPE_DIR 0x80 #define P9_QID_TYPE_SYMLINK 0x02 @@ -1389,6 +1527,14 @@ 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; @@ -1432,6 +1578,14 @@ 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); @@ -1993,6 +2147,14 @@ 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) { @@ -2124,11 +2286,19 @@ static void v9fs_remove(void *opaque) err = -EINVAL; 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 */ clunk_fid(pdu->s, fidp->fid); put_fid(pdu->s, fidp); @@ -2823,3 +2993,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; +} diff --git a/hw/9pfs/virtio-9p.h b/hw/9pfs/virtio-9p.h index e16e5f46a1..28fe1ff3a3 100644 --- a/hw/9pfs/virtio-9p.h +++ b/hw/9pfs/virtio-9p.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "hw/virtio.h" #include "fsdev/file-op-9p.h" @@ -101,6 +102,9 @@ enum p9_proto_version { #define P9_NOTAG (u16)(~0) #define P9_NOFID (u32)(~0) #define P9_MAXWELEM 16 + +#define FID_REFERENCED 0x1 +#define FID_NON_RECLAIMABLE 0x2 static inline const char *rpath(FsContext *ctx, const char *path, char *buffer) { snprintf(buffer, PATH_MAX, "%s/%s", ctx->fs_root, path); @@ -198,14 +202,21 @@ struct V9fsFidState int32_t fid; V9fsString path; union { - int fd; - DIR *dir; - V9fsXattr xattr; + int fd; + DIR *dir; + V9fsXattr xattr; } fs; + union { + int fd; + DIR *dir; + } fs_reclaim; + int flags; + int open_flags; uid_t uid; int ref; int clunked; V9fsFidState *next; + V9fsFidState *rclm_lst; }; typedef struct V9fsState @@ -354,6 +365,9 @@ typedef struct V9fsGetlock V9fsString client_id; } V9fsGetlock; +extern int open_fd_hw; +extern int total_open_fd; + size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count, size_t offset, size_t size, int pack); @@ -364,4 +378,6 @@ static inline size_t do_pdu_unpack(void *dst, struct iovec *sg, int sg_count, } extern void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq); +extern void virtio_9p_set_fd_limit(void); +extern void v9fs_reclaim_fd(V9fsState *s); #endif -- cgit v1.2.1 From 9e5b2247eaf56f057402d99285cc76b0d3e8827f Mon Sep 17 00:00:00 2001 From: "Aneesh Kumar K.V" Date: Sun, 22 May 2011 15:33:57 +0530 Subject: hw/9pfs: init fid list properly Signed-off-by: Aneesh Kumar K.V --- hw/9pfs/virtio-9p-device.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c index 44b9ace0af..97f2da5f0e 100644 --- a/hw/9pfs/virtio-9p-device.c +++ b/hw/9pfs/virtio-9p-device.c @@ -130,6 +130,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) s->config_size = sizeof(struct virtio_9p_config) + s->tag_len; s->vdev.get_config = virtio_9p_get_config; + s->fid_list = NULL; if (v9fs_init_worker_threads() < 0) { fprintf(stderr, "worker thread initialization failed\n"); -- cgit v1.2.1 From 9b54ffaab2e7cd09e7da47a9dc9b8f746ebcfe1b Mon Sep 17 00:00:00 2001 From: "Aneesh Kumar K.V" Date: Wed, 18 May 2011 16:59:54 +0530 Subject: hw/9pfs: Use v9fs_do_close instead of close we should use the local abstraction instead of directly calling close. Signed-off-by: Aneesh Kumar K.V --- hw/9pfs/virtio-9p.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 4ba2590566..6529216e7d 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -1592,7 +1592,7 @@ static void v9fs_lcreate(void *opaque) 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; } @@ -2160,7 +2160,7 @@ static void v9fs_create(void *opaque) 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; } -- cgit v1.2.1 From 95f65511c36fe4e2b319de4551f8a5657a63b67f Mon Sep 17 00:00:00 2001 From: "Aneesh Kumar K.V" Date: Wed, 18 May 2011 17:08:34 +0530 Subject: hw/9pfs: Add directory reclaim support Signed-off-by: Aneesh Kumar K.V --- hw/9pfs/codir.c | 9 +++++++++ hw/9pfs/virtio-9p.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/hw/9pfs/codir.c b/hw/9pfs/codir.c index 86adc87adc..f17f927c10 100644 --- a/hw/9pfs/codir.c +++ b/hw/9pfs/codir.c @@ -97,6 +97,12 @@ int v9fs_co_opendir(V9fsState *s, V9fsFidState *fidp) err = 0; } }); + if (!err) { + total_open_fd++; + if (total_open_fd > open_fd_hw) { + v9fs_reclaim_fd(s); + } + } return err; } @@ -111,5 +117,8 @@ int v9fs_co_closedir(V9fsState *s, DIR *dir) err = -errno; } }); + if (!err) { + total_open_fd--; + } return err; } diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 6529216e7d..16f7d4e2d7 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -266,7 +266,17 @@ static V9fsFidState *get_fid(V9fsState *s, int32_t fid) return NULL; } } - } + } else if (f->fid_type == P9_FID_DIR) { + if (f->fs.dir == NULL) { + do { + err = v9fs_co_opendir(s, f); + } while (err == -EINTR); + if (err < 0) { + f->ref--; + return NULL; + } + } + } /* * Mark the fid as referenced so that the LRU * reclaim won't close the file descriptor @@ -348,7 +358,9 @@ static int free_fid(V9fsState *s, V9fsFidState *fidp) retval = v9fs_co_close(s, fidp->fs.fd); } } else if (fidp->fid_type == P9_FID_DIR) { - retval = v9fs_co_closedir(s, fidp->fs.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); } @@ -429,6 +441,19 @@ void v9fs_reclaim_fd(V9fsState *s) 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; @@ -443,6 +468,8 @@ void v9fs_reclaim_fd(V9fsState *s) 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; /* -- cgit v1.2.1 From b9cb88b0565c9a292f79a1c7bd387b8e9978e2a6 Mon Sep 17 00:00:00 2001 From: "Aneesh Kumar K.V" Date: Tue, 2 Aug 2011 11:36:24 +0530 Subject: hw/9pfs: mark directories also as un-reclaimable on unlink Signed-off-by: Aneesh Kumar K.V --- hw/9pfs/virtio-9p.c | 73 ++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 16f7d4e2d7..94b7090e7d 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -235,6 +235,27 @@ static size_t v9fs_string_size(V9fsString *str) return str->size; } +/* + * 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; @@ -249,34 +270,17 @@ static V9fsFidState *get_fid(V9fsState *s, int32_t fid) * 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. */ - 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); - if (err < 0) { - f->ref--; - return NULL; - } - } - } else if (f->fid_type == P9_FID_DIR) { - if (f->fs.dir == NULL) { - do { - err = v9fs_co_opendir(s, f); - } while (err == -EINTR); - if (err < 0) { - f->ref--; - return NULL; - } - } - } + 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 @@ -490,19 +494,18 @@ static int v9fs_mark_fids_unreclaim(V9fsState *s, V9fsString *str) if (!strcmp(fidp->path.data, str->data)) { /* Mark the fid non reclaimable. */ fidp->flags |= FID_NON_RECLAIMABLE; - /* reopen the file if already closed */ - if (fidp->fs.fd == -1) { - do { - err = v9fs_co_open(s, fidp, fidp->open_flags); - } while (err == -EINTR); - 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 - */ + + /* 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; } } -- cgit v1.2.1