4 * Copyright IBM, Corp. 2011
7 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 * Not so fast! You might want to read the 9p developer docs first:
16 * https://wiki.qemu.org/Documentation/9p
19 #include "qemu/osdep.h"
20 #include "fsdev/qemu-fsdev.h"
21 #include "qemu/thread.h"
22 #include "qemu/coroutine.h"
23 #include "qemu/main-loop.h"
29 * Intended to be called from bottom-half (e.g. background I/O thread)
32 static int do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
, struct dirent
**dent
)
35 V9fsState
*s
= pdu
->s
;
39 entry
= s
->ops
->readdir(&s
->ctx
, &fidp
->fs
);
40 if (!entry
&& errno
) {
50 * TODO: This will be removed for performance reasons.
51 * Use v9fs_co_readdir_many() instead.
53 int coroutine_fn
v9fs_co_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
58 if (v9fs_request_cancelled(pdu
)) {
61 v9fs_co_run_in_worker({
62 err
= do_readdir(pdu
, fidp
, dent
);
68 * This is solely executed on a background IO thread.
70 * See v9fs_co_readdir_many() (as its only user) below for details.
72 static int do_readdir_many(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
73 struct V9fsDirEnt
**entries
, off_t offset
,
74 int32_t maxsize
, bool dostat
)
76 V9fsState
*s
= pdu
->s
;
82 struct V9fsDirEnt
*e
= NULL
;
87 v9fs_path_init(&path
);
90 * TODO: Here should be a warn_report_once() if lock failed.
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
98 v9fs_readdir_lock(&fidp
->fs
.dir
);
100 /* seek directory to requested initial position */
102 s
->ops
->rewinddir(&s
->ctx
, &fidp
->fs
);
104 s
->ops
->seekdir(&s
->ctx
, &fidp
->fs
, offset
);
107 /* save the directory position */
108 saved_dir_pos
= s
->ops
->telldir(&s
->ctx
, &fidp
->fs
);
109 if (saved_dir_pos
< 0) {
115 /* interrupt loop if request was cancelled by a Tflush request */
116 if (v9fs_request_cancelled(pdu
)) {
121 /* get directory entry from fs driver */
122 err
= do_readdir(pdu
, fidp
, &dent
);
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
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 */
142 /* append next node to result chain */
144 *entries
= e
= g_new0(V9fsDirEnt
, 1);
146 e
= e
->next
= g_new0(V9fsDirEnt
, 1);
148 e
->dent
= qemu_dirent_dup(dent
);
150 /* perform a full stat() for directory entry if requested by caller */
152 err
= s
->ops
->name_to_path(
153 &s
->ctx
, &fidp
->path
, dent
->d_name
, &path
160 err
= s
->ops
->lstat(&s
->ctx
, &path
, &stbuf
);
166 e
->st
= g_new0(struct stat
, 1);
167 memcpy(e
->st
, &stbuf
, sizeof(struct stat
));
171 saved_dir_pos
= qemu_dirent_off(dent
);
174 /* restore (last) saved position */
175 s
->ops
->seekdir(&s
->ctx
, &fidp
->fs
, saved_dir_pos
);
178 v9fs_readdir_unlock(&fidp
->fs
.dir
);
179 v9fs_path_free(&path
);
187 * v9fs_co_readdir_many() - Reads multiple directory entries in one rush.
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
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.
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
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.
217 int coroutine_fn
v9fs_co_readdir_many(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
218 struct V9fsDirEnt
**entries
,
219 off_t offset
, int32_t maxsize
,
224 if (v9fs_request_cancelled(pdu
)) {
227 v9fs_co_run_in_worker({
228 err
= do_readdir_many(pdu
, fidp
, entries
, offset
, maxsize
, dostat
);
233 off_t
v9fs_co_telldir(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
236 V9fsState
*s
= pdu
->s
;
238 if (v9fs_request_cancelled(pdu
)) {
241 v9fs_co_run_in_worker(
243 err
= s
->ops
->telldir(&s
->ctx
, &fidp
->fs
);
251 void coroutine_fn
v9fs_co_seekdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
254 V9fsState
*s
= pdu
->s
;
255 if (v9fs_request_cancelled(pdu
)) {
258 v9fs_co_run_in_worker(
260 s
->ops
->seekdir(&s
->ctx
, &fidp
->fs
, offset
);
264 void coroutine_fn
v9fs_co_rewinddir(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
266 V9fsState
*s
= pdu
->s
;
267 if (v9fs_request_cancelled(pdu
)) {
270 v9fs_co_run_in_worker(
272 s
->ops
->rewinddir(&s
->ctx
, &fidp
->fs
);
276 int coroutine_fn
v9fs_co_mkdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
277 V9fsString
*name
, mode_t mode
, uid_t uid
,
278 gid_t gid
, struct stat
*stbuf
)
283 V9fsState
*s
= pdu
->s
;
285 if (v9fs_request_cancelled(pdu
)) {
292 v9fs_path_read_lock(s
);
293 v9fs_co_run_in_worker(
295 err
= s
->ops
->mkdir(&s
->ctx
, &fidp
->path
, name
->data
, &cred
);
299 v9fs_path_init(&path
);
300 err
= v9fs_name_to_path(s
, &fidp
->path
, name
->data
, &path
);
302 err
= s
->ops
->lstat(&s
->ctx
, &path
, stbuf
);
307 v9fs_path_free(&path
);
314 int coroutine_fn
v9fs_co_opendir(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
317 V9fsState
*s
= pdu
->s
;
319 if (v9fs_request_cancelled(pdu
)) {
322 v9fs_path_read_lock(s
);
323 v9fs_co_run_in_worker(
325 err
= s
->ops
->opendir(&s
->ctx
, &fidp
->path
, &fidp
->fs
);
335 if (total_open_fd
> open_fd_hw
) {
336 v9fs_reclaim_fd(pdu
);
342 int coroutine_fn
v9fs_co_closedir(V9fsPDU
*pdu
, V9fsFidOpenState
*fs
)
345 V9fsState
*s
= pdu
->s
;
347 if (v9fs_request_cancelled(pdu
)) {
350 v9fs_co_run_in_worker(
352 err
= s
->ops
->closedir(&s
->ctx
, fs
);