master
c 368 lines 10.1 KB
Raw
1 /*
2 * 9p backend
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14 /*
15 * Not so fast! You might want to read the 9p developer docs first:
16 * https://wiki.qemu.org/Documentation/9p
17 */
18
19 #include "qemu/osdep.h"
20 #include "fsdev/qemu-fsdev.h"
21 #include "qemu/thread.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/error-report.h"
24 #include "coth.h"
25 #include "9p-xattr.h"
26 #include "9p-util.h"
27
28 /*
29 * Intended to be called from bottom-half (e.g. background I/O thread)
30 * context.
31 */
32 static int do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, struct dirent **dent)
33 {
34 int err = 0;
35 V9fsState *s = pdu->s;
36 struct dirent *entry;
37
38 errno = 0;
39 entry = s->ops->readdir(&s->ctx, &fidp->fs);
40 if (!entry && errno) {
41 *dent = NULL;
42 err = -errno;
43 } else {
44 *dent = entry;
45 }
46 return err;
47 }
48
49 /*
50 * TODO: This will be removed for performance reasons.
51 * Use v9fs_co_readdir_many() instead.
52 */
53 int coroutine_fn v9fs_co_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
54 struct dirent **dent)
55 {
56 int err;
57
58 if (v9fs_request_cancelled(pdu)) {
59 return -EINTR;
60 }
61 v9fs_co_run_in_worker({
62 err = do_readdir(pdu, fidp, dent);
63 });
64 return err;
65 }
66
67 /*
68 * This is solely executed on a background IO thread.
69 *
70 * See v9fs_co_readdir_many() (as its only user) below for details.
71 */
72 static int coroutine_fn
73 do_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp, struct V9fsDirEnt **entries,
74 off_t offset, int32_t maxsize, bool dostat)
75 {
76 V9fsState *s = pdu->s;
77 V9fsString name;
78 int len, err = 0;
79 int32_t size = 0;
80 off_t saved_dir_pos;
81 struct dirent *dent;
82 struct V9fsDirEnt *e = NULL;
83 V9fsPath path;
84 struct stat stbuf;
85
86 *entries = NULL;
87 v9fs_path_init(&path);
88
89 /*
90 * TODO: Here should be a warn_report_once() if lock failed.
91 *
92 * With a good 9p client we should not get into concurrency here,
93 * because a good client would not use the same fid for concurrent
94 * requests. We do the lock here for safety reasons though. However
95 * the client would then suffer performance issues, so better log that
96 * issue here.
97 */
98 v9fs_readdir_lock(&fidp->fs.dir);
99
100 /* seek directory to requested initial position */
101 if (offset == 0) {
102 s->ops->rewinddir(&s->ctx, &fidp->fs);
103 } else {
104 s->ops->seekdir(&s->ctx, &fidp->fs, offset);
105 }
106
107 /* save the directory position */
108 saved_dir_pos = s->ops->telldir(&s->ctx, &fidp->fs);
109 if (saved_dir_pos < 0) {
110 err = saved_dir_pos;
111 goto out;
112 }
113
114 while (true) {
115 /* interrupt loop if request was cancelled by a Tflush request */
116 if (v9fs_request_cancelled(pdu)) {
117 err = -EINTR;
118 break;
119 }
120
121 /* get directory entry from fs driver */
122 err = do_readdir(pdu, fidp, &dent);
123 if (err || !dent) {
124 break;
125 }
126
127 /*
128 * stop this loop as soon as it would exceed the allowed maximum
129 * response message size for the directory entries collected so far,
130 * because anything beyond that size would need to be discarded by
131 * 9p controller (main thread / top half) anyway
132 */
133 v9fs_string_init(&name);
134 v9fs_string_sprintf(&name, "%s", dent->d_name);
135 len = v9fs_readdir_response_size(&name);
136 v9fs_string_free(&name);
137 if (size + len > maxsize) {
138 /* this is not an error case actually */
139 break;
140 }
141
142 /* append next node to result chain */
143 if (!e) {
144 *entries = e = g_new0(V9fsDirEnt, 1);
145 } else {
146 e = e->next = g_new0(V9fsDirEnt, 1);
147 }
148 e->dent = qemu_dirent_dup(dent);
149
150 /* perform a full stat() for directory entry if requested by caller */
151 if (dostat) {
152 err = s->ops->name_to_path(
153 &s->ctx, &fidp->path, dent->d_name, &path
154 );
155 if (err < 0) {
156 err = -errno;
157 break;
158 }
159
160 err = s->ops->lstat(&s->ctx, &path, &stbuf);
161 if (err < 0) {
162 err = -errno;
163 break;
164 }
165
166 e->st = g_new0(struct stat, 1);
167 memcpy(e->st, &stbuf, sizeof(struct stat));
168 }
169
170 size += len;
171 saved_dir_pos = qemu_dirent_off(dent);
172 }
173
174 /* restore (last) saved position */
175 s->ops->seekdir(&s->ctx, &fidp->fs, saved_dir_pos);
176
177 out:
178 v9fs_readdir_unlock(&fidp->fs.dir);
179 v9fs_path_free(&path);
180 if (err < 0) {
181 return err;
182 }
183 return size;
184 }
185
186 /**
187 * v9fs_co_readdir_many() - Reads multiple directory entries in one rush.
188 *
189 * @pdu: the causing 9p (T_readdir) client request
190 * @fidp: already opened directory where readdir shall be performed on
191 * @entries: output for directory entries (must not be NULL)
192 * @offset: initial position inside the directory the function shall
193 * seek to before retrieving the directory entries
194 * @maxsize: maximum result message body size (in bytes)
195 * @dostat: whether a stat() should be performed and returned for
196 * each directory entry
197 * Return: resulting response message body size (in bytes) on success,
198 * negative error code otherwise
199 *
200 * Retrieves the requested (max. amount of) directory entries from the fs
201 * driver. This function must only be called by the main IO thread (top half).
202 * Internally this function call will be dispatched to a background IO thread
203 * (bottom half) where it is eventually executed by the fs driver.
204 *
205 * Acquiring multiple directory entries in one rush from the fs
206 * driver, instead of retrieving each directory entry individually, is very
207 * beneficial from performance point of view. Because for every fs driver
208 * request latency is added, which in practice could lead to overall
209 * latencies of several hundred ms for reading all entries (of just a single
210 * directory) if every directory entry was individually requested from fs
211 * driver.
212 *
213 * NOTE: You must ALWAYS call v9fs_free_dirents(entries) after calling
214 * v9fs_co_readdir_many(), both on success and on error cases of this
215 * function, to avoid memory leaks once @entries are no longer needed.
216 */
217 int coroutine_fn v9fs_co_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp,
218 struct V9fsDirEnt **entries,
219 off_t offset, int32_t maxsize,
220 bool dostat)
221 {
222 int err = 0;
223 V9fsState *s = pdu->s;
224
225 if (v9fs_request_cancelled(pdu)) {
226 return -EINTR;
227 }
228 v9fs_path_read_lock(s);
229 v9fs_co_run_in_worker({
230 err = do_readdir_many(pdu, fidp, entries, offset, maxsize, dostat);
231 });
232 v9fs_path_unlock(s);
233 return err;
234 }
235
236 off_t v9fs_co_telldir(V9fsPDU *pdu, V9fsFidState *fidp)
237 {
238 off_t err;
239 V9fsState *s = pdu->s;
240
241 if (v9fs_request_cancelled(pdu)) {
242 return -EINTR;
243 }
244 v9fs_co_run_in_worker(
245 {
246 err = s->ops->telldir(&s->ctx, &fidp->fs);
247 if (err < 0) {
248 err = -errno;
249 }
250 });
251 return err;
252 }
253
254 void coroutine_fn v9fs_co_seekdir(V9fsPDU *pdu, V9fsFidState *fidp,
255 off_t offset)
256 {
257 V9fsState *s = pdu->s;
258 if (v9fs_request_cancelled(pdu)) {
259 return;
260 }
261 v9fs_co_run_in_worker(
262 {
263 s->ops->seekdir(&s->ctx, &fidp->fs, offset);
264 });
265 }
266
267 void coroutine_fn v9fs_co_rewinddir(V9fsPDU *pdu, V9fsFidState *fidp)
268 {
269 V9fsState *s = pdu->s;
270 if (v9fs_request_cancelled(pdu)) {
271 return;
272 }
273 v9fs_co_run_in_worker(
274 {
275 s->ops->rewinddir(&s->ctx, &fidp->fs);
276 });
277 }
278
279 int coroutine_fn v9fs_co_mkdir(V9fsPDU *pdu, V9fsFidState *fidp,
280 V9fsString *name, mode_t mode, uid_t uid,
281 gid_t gid, struct stat *stbuf)
282 {
283 int err;
284 FsCred cred;
285 V9fsPath path;
286 V9fsState *s = pdu->s;
287
288 if (v9fs_request_cancelled(pdu)) {
289 return -EINTR;
290 }
291 cred_init(&cred);
292 cred.fc_mode = mode;
293 cred.fc_uid = uid;
294 cred.fc_gid = gid;
295 v9fs_path_read_lock(s);
296 v9fs_co_run_in_worker(
297 {
298 err = s->ops->mkdir(&s->ctx, &fidp->path, name->data, &cred);
299 if (err < 0) {
300 err = -errno;
301 } else {
302 v9fs_path_init(&path);
303 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
304 if (!err) {
305 err = s->ops->lstat(&s->ctx, &path, stbuf);
306 if (err < 0) {
307 err = -errno;
308 }
309 }
310 v9fs_path_free(&path);
311 }
312 });
313 v9fs_path_unlock(s);
314 return err;
315 }
316
317 int coroutine_fn v9fs_co_opendir(V9fsPDU *pdu, V9fsFidState *fidp)
318 {
319 int err;
320 V9fsState *s = pdu->s;
321
322 if (v9fs_request_cancelled(pdu)) {
323 return -EINTR;
324 }
325 v9fs_path_read_lock(s);
326 v9fs_co_run_in_worker(
327 {
328 err = s->ops->opendir(&s->ctx, &fidp->path, &fidp->fs);
329 if (err < 0) {
330 err = -errno;
331 } else {
332 err = 0;
333 }
334 });
335 v9fs_path_unlock(s);
336 if (!err) {
337 total_open_fd++;
338 if (total_open_fd > open_fd_hw) {
339 v9fs_reclaim_fd(pdu);
340 }
341 }
342 return err;
343 }
344
345 int coroutine_fn v9fs_co_closedir(V9fsPDU *pdu, V9fsFidOpenState *fs)
346 {
347 int err;
348 V9fsState *s = pdu->s;
349
350 if (v9fs_request_cancelled(pdu)) {
351 return -EINTR;
352 }
353 v9fs_co_run_in_worker(
354 {
355 err = s->ops->closedir(&s->ctx, fs);
356 if (err < 0) {
357 err = -errno;
358 }
359 });
360 /* 'man 2 close' suggests to ignore close() errors except of EBADF */
361 if (unlikely(err && errno == EBADF)) {
362 /* unexpected case as we should have checked for a valid file handle */
363 error_report("9pfs: WARNING: v9fs_co_closedir() failed with EBADF");
364 } else {
365 total_open_fd--;
366 }
367 return err;
368 }