Skip to content

Commit d62dbb5

Browse files
committed
virtio-9p: Add fidtype so that we can do type specific operation
We want to add type specific operation during read/write Signed-off-by: Aneesh Kumar K.V <[email protected]>
1 parent 771e9d4 commit d62dbb5

File tree

2 files changed

+78
-56
lines changed

2 files changed

+78
-56
lines changed

hw/virtio-9p.c

+56-54
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
408408
f = qemu_mallocz(sizeof(V9fsFidState));
409409

410410
f->fid = fid;
411-
f->fd = -1;
412-
f->dir = NULL;
411+
f->fid_type = P9_FID_NONE;
413412

414413
f->next = s->fid_list;
415414
s->fid_list = f;
@@ -434,11 +433,14 @@ static int free_fid(V9fsState *s, int32_t fid)
434433
fidp = *fidpp;
435434
*fidpp = fidp->next;
436435

437-
if (fidp->fd != -1) {
438-
v9fs_do_close(s, fidp->fd);
439-
}
440-
if (fidp->dir) {
441-
v9fs_do_closedir(s, fidp->dir);
436+
if (fidp->fid_type == P9_FID_FILE) {
437+
v9fs_do_close(s, fidp->fs.fd);
438+
} else if (fidp->fid_type == P9_FID_DIR) {
439+
v9fs_do_closedir(s, fidp->fs.dir);
440+
} else if (fidp->fid_type == P9_FID_XATTR) {
441+
if (fidp->fs.xattr.value) {
442+
qemu_free(fidp->fs.xattr.value);
443+
}
442444
}
443445
v9fs_string_free(&fidp->path);
444446
qemu_free(fidp);
@@ -1519,8 +1521,7 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
15191521
/* FIXME: is this really valid? */
15201522
if (fid == newfid) {
15211523

1522-
BUG_ON(vs->fidp->fd != -1);
1523-
BUG_ON(vs->fidp->dir);
1524+
BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
15241525
v9fs_string_init(&vs->path);
15251526
vs->name_idx = 0;
15261527

@@ -1584,11 +1585,11 @@ static int32_t get_iounit(V9fsState *s, V9fsString *name)
15841585

15851586
static void v9fs_open_post_opendir(V9fsState *s, V9fsOpenState *vs, int err)
15861587
{
1587-
if (vs->fidp->dir == NULL) {
1588+
if (vs->fidp->fs.dir == NULL) {
15881589
err = -errno;
15891590
goto out;
15901591
}
1591-
1592+
vs->fidp->fid_type = P9_FID_DIR;
15921593
vs->offset += pdu_marshal(vs->pdu, vs->offset, "Qd", &vs->qid, 0);
15931594
err = vs->offset;
15941595
out:
@@ -1608,11 +1609,11 @@ static void v9fs_open_post_getiounit(V9fsState *s, V9fsOpenState *vs)
16081609

16091610
static void v9fs_open_post_open(V9fsState *s, V9fsOpenState *vs, int err)
16101611
{
1611-
if (vs->fidp->fd == -1) {
1612+
if (vs->fidp->fs.fd == -1) {
16121613
err = -errno;
16131614
goto out;
16141615
}
1615-
1616+
vs->fidp->fid_type = P9_FID_FILE;
16161617
vs->iounit = get_iounit(s, &vs->fidp->path);
16171618
v9fs_open_post_getiounit(s, vs);
16181619
return;
@@ -1642,7 +1643,7 @@ static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
16421643
stat_to_qid(&vs->stbuf, &vs->qid);
16431644

16441645
if (S_ISDIR(vs->stbuf.st_mode)) {
1645-
vs->fidp->dir = v9fs_do_opendir(s, &vs->fidp->path);
1646+
vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fidp->path);
16461647
v9fs_open_post_opendir(s, vs, err);
16471648
} else {
16481649
if (s->proto_version == V9FS_PROTO_2000L) {
@@ -1654,7 +1655,7 @@ static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err)
16541655
} else {
16551656
flags = omode_to_uflags(vs->mode);
16561657
}
1657-
vs->fidp->fd = v9fs_do_open(s, &vs->fidp->path, flags);
1658+
vs->fidp->fs.fd = v9fs_do_open(s, &vs->fidp->path, flags);
16581659
v9fs_open_post_open(s, vs, err);
16591660
}
16601661
return;
@@ -1686,8 +1687,7 @@ static void v9fs_open(V9fsState *s, V9fsPDU *pdu)
16861687
goto out;
16871688
}
16881689

1689-
BUG_ON(vs->fidp->fd != -1);
1690-
BUG_ON(vs->fidp->dir);
1690+
BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
16911691

16921692
err = v9fs_do_lstat(s, &vs->fidp->path, &vs->stbuf);
16931693

@@ -1707,6 +1707,8 @@ static void v9fs_post_lcreate(V9fsState *s, V9fsLcreateState *vs, int err)
17071707
&vs->iounit);
17081708
err = vs->offset;
17091709
} else {
1710+
vs->fidp->fid_type = P9_FID_NONE;
1711+
close(vs->fidp->fs.fd);
17101712
err = -errno;
17111713
}
17121714

@@ -1732,10 +1734,11 @@ static void v9fs_lcreate_post_get_iounit(V9fsState *s, V9fsLcreateState *vs,
17321734
static void v9fs_lcreate_post_do_open2(V9fsState *s, V9fsLcreateState *vs,
17331735
int err)
17341736
{
1735-
if (vs->fidp->fd == -1) {
1737+
if (vs->fidp->fs.fd == -1) {
17361738
err = -errno;
17371739
goto out;
17381740
}
1741+
vs->fidp->fid_type = P9_FID_FILE;
17391742
vs->iounit = get_iounit(s, &vs->fullname);
17401743
v9fs_lcreate_post_get_iounit(s, vs, err);
17411744
return;
@@ -1769,7 +1772,7 @@ static void v9fs_lcreate(V9fsState *s, V9fsPDU *pdu)
17691772
v9fs_string_sprintf(&vs->fullname, "%s/%s", vs->fidp->path.data,
17701773
vs->name.data);
17711774

1772-
vs->fidp->fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
1775+
vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
17731776
gid, flags, mode);
17741777
v9fs_lcreate_post_do_open2(s, vs, err);
17751778
return;
@@ -1833,19 +1836,19 @@ static void v9fs_read_post_dir_lstat(V9fsState *s, V9fsReadState *vs,
18331836
&vs->v9stat);
18341837
if ((vs->len != (vs->v9stat.size + 2)) ||
18351838
((vs->count + vs->len) > vs->max_count)) {
1836-
v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1839+
v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
18371840
v9fs_read_post_seekdir(s, vs, err);
18381841
return;
18391842
}
18401843
vs->count += vs->len;
18411844
v9fs_stat_free(&vs->v9stat);
18421845
v9fs_string_free(&vs->name);
18431846
vs->dir_pos = vs->dent->d_off;
1844-
vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1847+
vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
18451848
v9fs_read_post_readdir(s, vs, err);
18461849
return;
18471850
out:
1848-
v9fs_do_seekdir(s, vs->fidp->dir, vs->dir_pos);
1851+
v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->dir_pos);
18491852
v9fs_read_post_seekdir(s, vs, err);
18501853
return;
18511854

@@ -1873,15 +1876,15 @@ static void v9fs_read_post_readdir(V9fsState *s, V9fsReadState *vs, ssize_t err)
18731876

18741877
static void v9fs_read_post_telldir(V9fsState *s, V9fsReadState *vs, ssize_t err)
18751878
{
1876-
vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
1879+
vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
18771880
v9fs_read_post_readdir(s, vs, err);
18781881
return;
18791882
}
18801883

18811884
static void v9fs_read_post_rewinddir(V9fsState *s, V9fsReadState *vs,
18821885
ssize_t err)
18831886
{
1884-
vs->dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
1887+
vs->dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
18851888
v9fs_read_post_telldir(s, vs, err);
18861889
return;
18871890
}
@@ -1900,7 +1903,7 @@ static void v9fs_read_post_readv(V9fsState *s, V9fsReadState *vs, ssize_t err)
19001903
if (0) {
19011904
print_sg(vs->sg, vs->cnt);
19021905
}
1903-
vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1906+
vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
19041907
} while (vs->len == -1 && errno == EINTR);
19051908
if (vs->len == -1) {
19061909
err = -errno;
@@ -1930,7 +1933,7 @@ static void v9fs_read_post_lseek(V9fsState *s, V9fsReadState *vs, ssize_t err)
19301933
if (0) {
19311934
print_sg(vs->sg, vs->cnt);
19321935
}
1933-
vs->len = v9fs_do_readv(s, vs->fidp->fd, vs->sg, vs->cnt);
1936+
vs->len = v9fs_do_readv(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
19341937
} while (vs->len == -1 && errno == EINTR);
19351938
if (vs->len == -1) {
19361939
err = -errno;
@@ -1964,18 +1967,18 @@ static void v9fs_read(V9fsState *s, V9fsPDU *pdu)
19641967
goto out;
19651968
}
19661969

1967-
if (vs->fidp->dir) {
1970+
if (vs->fidp->fs.dir) {
19681971
vs->max_count = vs->count;
19691972
vs->count = 0;
19701973
if (vs->off == 0) {
1971-
v9fs_do_rewinddir(s, vs->fidp->dir);
1974+
v9fs_do_rewinddir(s, vs->fidp->fs.dir);
19721975
}
19731976
v9fs_read_post_rewinddir(s, vs, err);
19741977
return;
1975-
} else if (vs->fidp->fd != -1) {
1978+
} else if (vs->fidp->fs.fd != -1) {
19761979
vs->sg = vs->iov;
19771980
pdu_marshal(vs->pdu, vs->offset + 4, "v", vs->sg, &vs->cnt);
1978-
err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
1981+
err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
19791982
v9fs_read_post_lseek(s, vs, err);
19801983
return;
19811984
} else {
@@ -2024,7 +2027,7 @@ static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
20242027

20252028
if ((vs->count + V9_READDIR_DATA_SZ) > vs->max_count) {
20262029
/* Ran out of buffer. Set dir back to old position and return */
2027-
v9fs_do_seekdir(s, vs->fidp->dir, vs->saved_dir_pos);
2030+
v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->saved_dir_pos);
20282031
v9fs_readdir_post_seekdir(s, vs);
20292032
return;
20302033
}
@@ -2045,7 +2048,7 @@ static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
20452048
vs->count += len;
20462049
v9fs_string_free(&vs->name);
20472050
vs->saved_dir_pos = vs->dent->d_off;
2048-
vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
2051+
vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
20492052
v9fs_readdir_post_readdir(s, vs);
20502053
return;
20512054
}
@@ -2059,14 +2062,14 @@ static void v9fs_readdir_post_readdir(V9fsState *s, V9fsReadDirState *vs)
20592062

20602063
static void v9fs_readdir_post_telldir(V9fsState *s, V9fsReadDirState *vs)
20612064
{
2062-
vs->dent = v9fs_do_readdir(s, vs->fidp->dir);
2065+
vs->dent = v9fs_do_readdir(s, vs->fidp->fs.dir);
20632066
v9fs_readdir_post_readdir(s, vs);
20642067
return;
20652068
}
20662069

20672070
static void v9fs_readdir_post_setdir(V9fsState *s, V9fsReadDirState *vs)
20682071
{
2069-
vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->dir);
2072+
vs->saved_dir_pos = v9fs_do_telldir(s, vs->fidp->fs.dir);
20702073
v9fs_readdir_post_telldir(s, vs);
20712074
return;
20722075
}
@@ -2087,15 +2090,15 @@ static void v9fs_readdir(V9fsState *s, V9fsPDU *pdu)
20872090
&vs->max_count);
20882091

20892092
vs->fidp = lookup_fid(s, fid);
2090-
if (vs->fidp == NULL || !(vs->fidp->dir)) {
2093+
if (vs->fidp == NULL || !(vs->fidp->fs.dir)) {
20912094
err = -EINVAL;
20922095
goto out;
20932096
}
20942097

20952098
if (vs->initial_offset == 0) {
2096-
v9fs_do_rewinddir(s, vs->fidp->dir);
2099+
v9fs_do_rewinddir(s, vs->fidp->fs.dir);
20972100
} else {
2098-
v9fs_do_seekdir(s, vs->fidp->dir, vs->initial_offset);
2101+
v9fs_do_seekdir(s, vs->fidp->fs.dir, vs->initial_offset);
20992102
}
21002103

21012104
v9fs_readdir_post_setdir(s, vs);
@@ -2122,7 +2125,7 @@ static void v9fs_write_post_writev(V9fsState *s, V9fsWriteState *vs,
21222125
if (0) {
21232126
print_sg(vs->sg, vs->cnt);
21242127
}
2125-
vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
2128+
vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
21262129
} while (vs->len == -1 && errno == EINTR);
21272130
if (vs->len == -1) {
21282131
err = -errno;
@@ -2151,7 +2154,7 @@ static void v9fs_write_post_lseek(V9fsState *s, V9fsWriteState *vs, ssize_t err)
21512154
if (0) {
21522155
print_sg(vs->sg, vs->cnt);
21532156
}
2154-
vs->len = v9fs_do_writev(s, vs->fidp->fd, vs->sg, vs->cnt);
2157+
vs->len = v9fs_do_writev(s, vs->fidp->fs.fd, vs->sg, vs->cnt);
21552158
} while (vs->len == -1 && errno == EINTR);
21562159
if (vs->len == -1) {
21572160
err = -errno;
@@ -2188,12 +2191,12 @@ static void v9fs_write(V9fsState *s, V9fsPDU *pdu)
21882191
goto out;
21892192
}
21902193

2191-
if (vs->fidp->fd == -1) {
2194+
if (vs->fidp->fs.fd == -1) {
21922195
err = -EINVAL;
21932196
goto out;
21942197
}
21952198

2196-
err = v9fs_do_lseek(s, vs->fidp->fd, vs->off, SEEK_SET);
2199+
err = v9fs_do_lseek(s, vs->fidp->fs.fd, vs->off, SEEK_SET);
21972200

21982201
v9fs_write_post_lseek(s, vs, err);
21992202
return;
@@ -2245,9 +2248,10 @@ static void v9fs_create_post_perms(V9fsState *s, V9fsCreateState *vs, int err)
22452248
static void v9fs_create_post_opendir(V9fsState *s, V9fsCreateState *vs,
22462249
int err)
22472250
{
2248-
if (!vs->fidp->dir) {
2251+
if (!vs->fidp->fs.dir) {
22492252
err = -errno;
22502253
}
2254+
vs->fidp->fid_type = P9_FID_DIR;
22512255
v9fs_post_create(s, vs, err);
22522256
}
22532257

@@ -2259,7 +2263,7 @@ static void v9fs_create_post_dir_lstat(V9fsState *s, V9fsCreateState *vs,
22592263
goto out;
22602264
}
22612265

2262-
vs->fidp->dir = v9fs_do_opendir(s, &vs->fullname);
2266+
vs->fidp->fs.dir = v9fs_do_opendir(s, &vs->fullname);
22632267
v9fs_create_post_opendir(s, vs, err);
22642268
return;
22652269

@@ -2285,22 +2289,22 @@ static void v9fs_create_post_mkdir(V9fsState *s, V9fsCreateState *vs, int err)
22852289
static void v9fs_create_post_fstat(V9fsState *s, V9fsCreateState *vs, int err)
22862290
{
22872291
if (err) {
2288-
vs->fidp->fd = -1;
2292+
vs->fidp->fid_type = P9_FID_NONE;
2293+
close(vs->fidp->fs.fd);
22892294
err = -errno;
22902295
}
2291-
22922296
v9fs_post_create(s, vs, err);
22932297
return;
22942298
}
22952299

22962300
static void v9fs_create_post_open2(V9fsState *s, V9fsCreateState *vs, int err)
22972301
{
2298-
if (vs->fidp->fd == -1) {
2302+
if (vs->fidp->fs.fd == -1) {
22992303
err = -errno;
23002304
goto out;
23012305
}
2302-
2303-
err = v9fs_do_fstat(s, vs->fidp->fd, &vs->stbuf);
2306+
vs->fidp->fid_type = P9_FID_FILE;
2307+
err = v9fs_do_fstat(s, vs->fidp->fs.fd, &vs->stbuf);
23042308
v9fs_create_post_fstat(s, vs, err);
23052309

23062310
return;
@@ -2371,7 +2375,7 @@ static void v9fs_create_post_lstat(V9fsState *s, V9fsCreateState *vs, int err)
23712375
0, vs->fidp->uid, -1);
23722376
v9fs_post_create(s, vs, err);
23732377
} else {
2374-
vs->fidp->fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
2378+
vs->fidp->fs.fd = v9fs_do_open2(s, vs->fullname.data, vs->fidp->uid,
23752379
-1, omode_to_uflags(vs->mode)|O_CREAT, vs->perm);
23762380

23772381
v9fs_create_post_open2(s, vs, err);
@@ -2615,8 +2619,7 @@ static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)
26152619
goto out;
26162620
}
26172621

2618-
BUG_ON(dirfidp->fd != -1);
2619-
BUG_ON(dirfidp->dir);
2622+
BUG_ON(dirfidp->fid_type != P9_FID_NONE);
26202623

26212624
new_name = qemu_mallocz(dirfidp->path.size + vs->name.size + 2);
26222625

@@ -2727,8 +2730,7 @@ static void v9fs_rename(V9fsState *s, V9fsPDU *pdu)
27272730
goto out;
27282731
}
27292732

2730-
BUG_ON(vs->fidp->fd != -1);
2731-
BUG_ON(vs->fidp->dir);
2733+
BUG_ON(vs->fidp->fid_type != P9_FID_NONE);
27322734

27332735
err = v9fs_complete_rename(s, vs);
27342736
v9fs_rename_post_rename(s, vs, err);
@@ -2855,7 +2857,7 @@ static void v9fs_wstat(V9fsState *s, V9fsPDU *pdu)
28552857

28562858
/* do we need to sync the file? */
28572859
if (donttouch_stat(&vs->v9stat)) {
2858-
err = v9fs_do_fsync(s, vs->fidp->fd);
2860+
err = v9fs_do_fsync(s, vs->fidp->fs.fd);
28592861
v9fs_wstat_post_fsync(s, vs, err);
28602862
return;
28612863
}

0 commit comments

Comments
 (0)