4 * Copyright IBM, Corp. 2011
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "fsdev/qemu-fsdev.h"
16 #include "qemu/thread.h"
17 #include "qemu/coroutine.h"
18 #include "qemu/main-loop.h"
22 * Intended to be called from bottom-half (e.g. background I/O thread)
25 static int do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, struct dirent **dent)
28 V9fsState *s = pdu->s;
32 entry = s->ops->readdir(&s->ctx, &fidp->fs);
33 if (!entry && errno) {
43 * TODO: This will be removed for performance reasons.
44 * Use v9fs_co_readdir_many() instead.
46 int coroutine_fn v9fs_co_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
51 if (v9fs_request_cancelled(pdu)) {
54 v9fs_co_run_in_worker({
55 err = do_readdir(pdu, fidp, dent);
61 * This is solely executed on a background IO thread.
63 * See v9fs_co_readdir_many() (as its only user) below for details.
65 static int do_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp,
66 struct V9fsDirEnt **entries, off_t offset,
67 int32_t maxsize, bool dostat)
69 V9fsState *s = pdu->s;
75 struct V9fsDirEnt *e = NULL;
80 v9fs_path_init(&path);
83 * TODO: Here should be a warn_report_once() if lock failed.
85 * With a good 9p client we should not get into concurrency here,
86 * because a good client would not use the same fid for concurrent
87 * requests. We do the lock here for safety reasons though. However
88 * the client would then suffer performance issues, so better log that
91 v9fs_readdir_lock(&fidp->fs.dir);
93 /* seek directory to requested initial position */
95 s->ops->rewinddir(&s->ctx, &fidp->fs);
97 s->ops->seekdir(&s->ctx, &fidp->fs, offset);
100 /* save the directory position */
101 saved_dir_pos = s->ops->telldir(&s->ctx, &fidp->fs);
102 if (saved_dir_pos < 0) {
108 /* interrupt loop if request was cancelled by a Tflush request */
109 if (v9fs_request_cancelled(pdu)) {
114 /* get directory entry from fs driver */
115 err = do_readdir(pdu, fidp, &dent);
121 * stop this loop as soon as it would exceed the allowed maximum
122 * response message size for the directory entries collected so far,
123 * because anything beyond that size would need to be discarded by
124 * 9p controller (main thread / top half) anyway
126 v9fs_string_init(&name);
127 v9fs_string_sprintf(&name, "%s", dent->d_name);
128 len = v9fs_readdir_response_size(&name);
129 v9fs_string_free(&name);
130 if (size + len > maxsize) {
131 /* this is not an error case actually */
135 /* append next node to result chain */
137 *entries = e = g_malloc0(sizeof(V9fsDirEnt));
139 e = e->next = g_malloc0(sizeof(V9fsDirEnt));
141 e->dent = g_malloc0(sizeof(struct dirent));
142 memcpy(e->dent, dent, sizeof(struct dirent));
144 /* perform a full stat() for directory entry if requested by caller */
146 err = s->ops->name_to_path(
147 &s->ctx, &fidp->path, dent->d_name, &path
154 err = s->ops->lstat(&s->ctx, &path, &stbuf);
160 e->st = g_malloc0(sizeof(struct stat));
161 memcpy(e->st, &stbuf, sizeof(struct stat));
165 saved_dir_pos = dent->d_off;
168 /* restore (last) saved position */
169 s->ops->seekdir(&s->ctx, &fidp->fs, saved_dir_pos);
172 v9fs_readdir_unlock(&fidp->fs.dir);
173 v9fs_path_free(&path);
181 * @brief Reads multiple directory entries in one rush.
183 * Retrieves the requested (max. amount of) directory entries from the fs
184 * driver. This function must only be called by the main IO thread (top half).
185 * Internally this function call will be dispatched to a background IO thread
186 * (bottom half) where it is eventually executed by the fs driver.
188 * @discussion Acquiring multiple directory entries in one rush from the fs
189 * driver, instead of retrieving each directory entry individually, is very
190 * beneficial from performance point of view. Because for every fs driver
191 * request latency is added, which in practice could lead to overall
192 * latencies of several hundred ms for reading all entries (of just a single
193 * directory) if every directory entry was individually requested from fs
196 * @note You must @b ALWAYS call @c v9fs_free_dirents(entries) after calling
197 * v9fs_co_readdir_many(), both on success and on error cases of this
198 * function, to avoid memory leaks once @p entries are no longer needed.
200 * @param pdu - the causing 9p (T_readdir) client request
201 * @param fidp - already opened directory where readdir shall be performed on
202 * @param entries - output for directory entries (must not be NULL)
203 * @param offset - initial position inside the directory the function shall
204 * seek to before retrieving the directory entries
205 * @param maxsize - maximum result message body size (in bytes)
206 * @param dostat - whether a stat() should be performed and returned for
207 * each directory entry
208 * @returns resulting response message body size (in bytes) on success,
209 * negative error code otherwise
211 int coroutine_fn v9fs_co_readdir_many(V9fsPDU *pdu, V9fsFidState *fidp,
212 struct V9fsDirEnt **entries,
213 off_t offset, int32_t maxsize,
218 if (v9fs_request_cancelled(pdu)) {
221 v9fs_co_run_in_worker({
222 err = do_readdir_many(pdu, fidp, entries, offset, maxsize, dostat);
227 off_t v9fs_co_telldir(V9fsPDU *pdu, V9fsFidState *fidp)
230 V9fsState *s = pdu->s;
232 if (v9fs_request_cancelled(pdu)) {
235 v9fs_co_run_in_worker(
237 err = s->ops->telldir(&s->ctx, &fidp->fs);
245 void coroutine_fn v9fs_co_seekdir(V9fsPDU *pdu, V9fsFidState *fidp,
248 V9fsState *s = pdu->s;
249 if (v9fs_request_cancelled(pdu)) {
252 v9fs_co_run_in_worker(
254 s->ops->seekdir(&s->ctx, &fidp->fs, offset);
258 void coroutine_fn v9fs_co_rewinddir(V9fsPDU *pdu, V9fsFidState *fidp)
260 V9fsState *s = pdu->s;
261 if (v9fs_request_cancelled(pdu)) {
264 v9fs_co_run_in_worker(
266 s->ops->rewinddir(&s->ctx, &fidp->fs);
270 int coroutine_fn v9fs_co_mkdir(V9fsPDU *pdu, V9fsFidState *fidp,
271 V9fsString *name, mode_t mode, uid_t uid,
272 gid_t gid, struct stat *stbuf)
277 V9fsState *s = pdu->s;
279 if (v9fs_request_cancelled(pdu)) {
286 v9fs_path_read_lock(s);
287 v9fs_co_run_in_worker(
289 err = s->ops->mkdir(&s->ctx, &fidp->path, name->data, &cred);
293 v9fs_path_init(&path);
294 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
296 err = s->ops->lstat(&s->ctx, &path, stbuf);
301 v9fs_path_free(&path);
308 int coroutine_fn v9fs_co_opendir(V9fsPDU *pdu, V9fsFidState *fidp)
311 V9fsState *s = pdu->s;
313 if (v9fs_request_cancelled(pdu)) {
316 v9fs_path_read_lock(s);
317 v9fs_co_run_in_worker(
319 err = s->ops->opendir(&s->ctx, &fidp->path, &fidp->fs);
329 if (total_open_fd > open_fd_hw) {
330 v9fs_reclaim_fd(pdu);
336 int coroutine_fn v9fs_co_closedir(V9fsPDU *pdu, V9fsFidOpenState *fs)
339 V9fsState *s = pdu->s;
341 if (v9fs_request_cancelled(pdu)) {
344 v9fs_co_run_in_worker(
346 err = s->ops->closedir(&s->ctx, fs);