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