]>
Commit | Line | Data |
---|---|---|
172198d4 AK |
1 | |
2 | /* | |
3 | * Virtio 9p backend | |
4 | * | |
5 | * Copyright IBM, Corp. 2011 | |
6 | * | |
7 | * Authors: | |
8 | * Aneesh Kumar K.V <[email protected]> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
11 | * the COPYING file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
fbc04127 | 15 | #include "qemu/osdep.h" |
172198d4 | 16 | #include "fsdev/qemu-fsdev.h" |
1de7afc9 | 17 | #include "qemu/thread.h" |
10817bf0 | 18 | #include "qemu/coroutine.h" |
fe52840c | 19 | #include "coth.h" |
172198d4 | 20 | |
e06a765e HPB |
21 | int v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mode, |
22 | V9fsStatDotl *v9stat) | |
23 | { | |
24 | int err = 0; | |
25 | V9fsState *s = pdu->s; | |
26 | ||
27 | if (v9fs_request_cancelled(pdu)) { | |
28 | return -EINTR; | |
29 | } | |
30 | if (s->ctx.exops.get_st_gen) { | |
31 | v9fs_path_read_lock(s); | |
32 | v9fs_co_run_in_worker( | |
33 | { | |
34 | err = s->ctx.exops.get_st_gen(&s->ctx, path, st_mode, | |
35 | &v9stat->st_gen); | |
36 | if (err < 0) { | |
37 | err = -errno; | |
38 | } | |
39 | }); | |
40 | v9fs_path_unlock(s); | |
41 | } | |
42 | return err; | |
43 | } | |
44 | ||
bccacf6c | 45 | int v9fs_co_lstat(V9fsPDU *pdu, V9fsPath *path, struct stat *stbuf) |
172198d4 AK |
46 | { |
47 | int err; | |
bccacf6c | 48 | V9fsState *s = pdu->s; |
172198d4 | 49 | |
bccacf6c AK |
50 | if (v9fs_request_cancelled(pdu)) { |
51 | return -EINTR; | |
52 | } | |
532decb7 | 53 | v9fs_path_read_lock(s); |
172198d4 AK |
54 | v9fs_co_run_in_worker( |
55 | { | |
2289be19 | 56 | err = s->ops->lstat(&s->ctx, path, stbuf); |
172198d4 AK |
57 | if (err < 0) { |
58 | err = -errno; | |
59 | } | |
60 | }); | |
532decb7 | 61 | v9fs_path_unlock(s); |
172198d4 AK |
62 | return err; |
63 | } | |
03feb1e1 | 64 | |
cc720ddb | 65 | int v9fs_co_fstat(V9fsPDU *pdu, V9fsFidState *fidp, struct stat *stbuf) |
03feb1e1 AK |
66 | { |
67 | int err; | |
bccacf6c | 68 | V9fsState *s = pdu->s; |
03feb1e1 | 69 | |
bccacf6c AK |
70 | if (v9fs_request_cancelled(pdu)) { |
71 | return -EINTR; | |
72 | } | |
03feb1e1 AK |
73 | v9fs_co_run_in_worker( |
74 | { | |
8b888272 | 75 | err = s->ops->fstat(&s->ctx, fidp->fid_type, &fidp->fs, stbuf); |
03feb1e1 AK |
76 | if (err < 0) { |
77 | err = -errno; | |
78 | } | |
79 | }); | |
2c30dd74 AK |
80 | /* |
81 | * Some FS driver (local:mapped-file) can't support fetching attributes | |
82 | * using file descriptor. Use Path name in that case. | |
83 | */ | |
84 | if (err == -EOPNOTSUPP) { | |
85 | err = v9fs_co_lstat(pdu, &fidp->path, stbuf); | |
86 | if (err == -ENOENT) { | |
87 | /* | |
88 | * fstat on an unlinked file. Work with partial results | |
89 | * returned from s->ops->fstat | |
90 | */ | |
91 | err = 0; | |
92 | } | |
93 | } | |
03feb1e1 AK |
94 | return err; |
95 | } | |
f6b7f0ab | 96 | |
bccacf6c | 97 | int v9fs_co_open(V9fsPDU *pdu, V9fsFidState *fidp, int flags) |
f6b7f0ab AK |
98 | { |
99 | int err; | |
bccacf6c | 100 | V9fsState *s = pdu->s; |
f6b7f0ab | 101 | |
bccacf6c AK |
102 | if (v9fs_request_cancelled(pdu)) { |
103 | return -EINTR; | |
104 | } | |
532decb7 | 105 | v9fs_path_read_lock(s); |
f6b7f0ab AK |
106 | v9fs_co_run_in_worker( |
107 | { | |
cc720ddb AK |
108 | err = s->ops->open(&s->ctx, &fidp->path, flags, &fidp->fs); |
109 | if (err == -1) { | |
f6b7f0ab AK |
110 | err = -errno; |
111 | } else { | |
112 | err = 0; | |
113 | } | |
114 | }); | |
532decb7 | 115 | v9fs_path_unlock(s); |
7a462745 AK |
116 | if (!err) { |
117 | total_open_fd++; | |
118 | if (total_open_fd > open_fd_hw) { | |
bccacf6c | 119 | v9fs_reclaim_fd(pdu); |
7a462745 AK |
120 | } |
121 | } | |
f6b7f0ab AK |
122 | return err; |
123 | } | |
e4de4232 | 124 | |
bccacf6c | 125 | int v9fs_co_open2(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, gid_t gid, |
02cb7f3a | 126 | int flags, int mode, struct stat *stbuf) |
e4de4232 VJ |
127 | { |
128 | int err; | |
129 | FsCred cred; | |
2289be19 | 130 | V9fsPath path; |
bccacf6c | 131 | V9fsState *s = pdu->s; |
2289be19 | 132 | |
bccacf6c AK |
133 | if (v9fs_request_cancelled(pdu)) { |
134 | return -EINTR; | |
135 | } | |
e4de4232 VJ |
136 | cred_init(&cred); |
137 | cred.fc_mode = mode & 07777; | |
138 | cred.fc_uid = fidp->uid; | |
139 | cred.fc_gid = gid; | |
02cb7f3a AK |
140 | /* |
141 | * Hold the directory fid lock so that directory path name | |
142 | * don't change. Read lock is fine because this fid cannot | |
143 | * be used by any other operation. | |
144 | */ | |
532decb7 | 145 | v9fs_path_read_lock(s); |
e4de4232 VJ |
146 | v9fs_co_run_in_worker( |
147 | { | |
cc720ddb AK |
148 | err = s->ops->open2(&s->ctx, &fidp->path, |
149 | name->data, flags, &cred, &fidp->fs); | |
150 | if (err < 0) { | |
e4de4232 | 151 | err = -errno; |
02cb7f3a | 152 | } else { |
2289be19 AK |
153 | v9fs_path_init(&path); |
154 | err = v9fs_name_to_path(s, &fidp->path, name->data, &path); | |
155 | if (!err) { | |
156 | err = s->ops->lstat(&s->ctx, &path, stbuf); | |
157 | if (err < 0) { | |
158 | err = -errno; | |
cc720ddb | 159 | s->ops->close(&s->ctx, &fidp->fs); |
2289be19 AK |
160 | } else { |
161 | v9fs_path_copy(&fidp->path, &path); | |
162 | } | |
02cb7f3a | 163 | } else { |
cc720ddb | 164 | s->ops->close(&s->ctx, &fidp->fs); |
02cb7f3a | 165 | } |
2289be19 | 166 | v9fs_path_free(&path); |
e4de4232 VJ |
167 | } |
168 | }); | |
532decb7 | 169 | v9fs_path_unlock(s); |
7a462745 AK |
170 | if (!err) { |
171 | total_open_fd++; | |
172 | if (total_open_fd > open_fd_hw) { | |
bccacf6c | 173 | v9fs_reclaim_fd(pdu); |
7a462745 AK |
174 | } |
175 | } | |
e4de4232 VJ |
176 | return err; |
177 | } | |
bed4352c | 178 | |
cc720ddb | 179 | int v9fs_co_close(V9fsPDU *pdu, V9fsFidOpenState *fs) |
bed4352c | 180 | { |
bed4352c | 181 | int err; |
bccacf6c | 182 | V9fsState *s = pdu->s; |
bed4352c | 183 | |
bccacf6c AK |
184 | if (v9fs_request_cancelled(pdu)) { |
185 | return -EINTR; | |
186 | } | |
bed4352c AK |
187 | v9fs_co_run_in_worker( |
188 | { | |
cc720ddb | 189 | err = s->ops->close(&s->ctx, fs); |
bed4352c AK |
190 | if (err < 0) { |
191 | err = -errno; | |
192 | } | |
193 | }); | |
7a462745 AK |
194 | if (!err) { |
195 | total_open_fd--; | |
196 | } | |
bed4352c AK |
197 | return err; |
198 | } | |
4743d1f5 | 199 | |
bccacf6c | 200 | int v9fs_co_fsync(V9fsPDU *pdu, V9fsFidState *fidp, int datasync) |
4743d1f5 | 201 | { |
cc720ddb | 202 | int err; |
bccacf6c | 203 | V9fsState *s = pdu->s; |
4743d1f5 | 204 | |
bccacf6c AK |
205 | if (v9fs_request_cancelled(pdu)) { |
206 | return -EINTR; | |
207 | } | |
4743d1f5 AK |
208 | v9fs_co_run_in_worker( |
209 | { | |
8b888272 | 210 | err = s->ops->fsync(&s->ctx, fidp->fid_type, &fidp->fs, datasync); |
4743d1f5 AK |
211 | if (err < 0) { |
212 | err = -errno; | |
213 | } | |
214 | }); | |
215 | return err; | |
216 | } | |
c6c069b0 | 217 | |
bccacf6c | 218 | int v9fs_co_link(V9fsPDU *pdu, V9fsFidState *oldfid, |
2289be19 | 219 | V9fsFidState *newdirfid, V9fsString *name) |
c6c069b0 VJ |
220 | { |
221 | int err; | |
bccacf6c | 222 | V9fsState *s = pdu->s; |
c6c069b0 | 223 | |
bccacf6c AK |
224 | if (v9fs_request_cancelled(pdu)) { |
225 | return -EINTR; | |
226 | } | |
532decb7 | 227 | v9fs_path_read_lock(s); |
c6c069b0 VJ |
228 | v9fs_co_run_in_worker( |
229 | { | |
2289be19 AK |
230 | err = s->ops->link(&s->ctx, &oldfid->path, |
231 | &newdirfid->path, name->data); | |
c6c069b0 VJ |
232 | if (err < 0) { |
233 | err = -errno; | |
234 | } | |
235 | }); | |
532decb7 | 236 | v9fs_path_unlock(s); |
c6c069b0 VJ |
237 | return err; |
238 | } | |
f6b3c976 | 239 | |
bccacf6c | 240 | int v9fs_co_pwritev(V9fsPDU *pdu, V9fsFidState *fidp, |
f6b3c976 AK |
241 | struct iovec *iov, int iovcnt, int64_t offset) |
242 | { | |
cc720ddb | 243 | int err; |
bccacf6c | 244 | V9fsState *s = pdu->s; |
f6b3c976 | 245 | |
bccacf6c AK |
246 | if (v9fs_request_cancelled(pdu)) { |
247 | return -EINTR; | |
248 | } | |
f6b3c976 AK |
249 | v9fs_co_run_in_worker( |
250 | { | |
cc720ddb | 251 | err = s->ops->pwritev(&s->ctx, &fidp->fs, iov, iovcnt, offset); |
f6b3c976 AK |
252 | if (err < 0) { |
253 | err = -errno; | |
254 | } | |
255 | }); | |
256 | return err; | |
257 | } | |
7eafdcc9 | 258 | |
bccacf6c | 259 | int v9fs_co_preadv(V9fsPDU *pdu, V9fsFidState *fidp, |
7eafdcc9 AK |
260 | struct iovec *iov, int iovcnt, int64_t offset) |
261 | { | |
cc720ddb | 262 | int err; |
bccacf6c | 263 | V9fsState *s = pdu->s; |
7eafdcc9 | 264 | |
bccacf6c AK |
265 | if (v9fs_request_cancelled(pdu)) { |
266 | return -EINTR; | |
267 | } | |
7eafdcc9 AK |
268 | v9fs_co_run_in_worker( |
269 | { | |
cc720ddb | 270 | err = s->ops->preadv(&s->ctx, &fidp->fs, iov, iovcnt, offset); |
7eafdcc9 AK |
271 | if (err < 0) { |
272 | err = -errno; | |
273 | } | |
274 | }); | |
275 | return err; | |
276 | } |