Statistics
| Branch: | Revision:

root / hw / 9pfs / virtio-9p.c @ ddf5636d

History | View | Annotate | Download (84.8 kB)

1 9f107513 Anthony Liguori
/*
2 9f107513 Anthony Liguori
 * Virtio 9p backend
3 9f107513 Anthony Liguori
 *
4 9f107513 Anthony Liguori
 * Copyright IBM, Corp. 2010
5 9f107513 Anthony Liguori
 *
6 9f107513 Anthony Liguori
 * Authors:
7 9f107513 Anthony Liguori
 *  Anthony Liguori   <aliguori@us.ibm.com>
8 9f107513 Anthony Liguori
 *
9 9f107513 Anthony Liguori
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 9f107513 Anthony Liguori
 * the COPYING file in the top-level directory.
11 9f107513 Anthony Liguori
 *
12 9f107513 Anthony Liguori
 */
13 9f107513 Anthony Liguori
14 0d09e41a Paolo Bonzini
#include "hw/virtio/virtio.h"
15 0d09e41a Paolo Bonzini
#include "hw/i386/pc.h"
16 1de7afc9 Paolo Bonzini
#include "qemu/sockets.h"
17 9f107513 Anthony Liguori
#include "virtio-9p.h"
18 9f107513 Anthony Liguori
#include "fsdev/qemu-fsdev.h"
19 fc22118d Aneesh Kumar K.V
#include "virtio-9p-xattr.h"
20 ff06030f Venkateswararao Jujjuri (JV)
#include "virtio-9p-coth.h"
21 c572f23a Harsh Prateek Bora
#include "trace.h"
22 caf71f86 Paolo Bonzini
#include "migration/migration.h"
23 9f107513 Anthony Liguori
24 7a462745 Aneesh Kumar K.V
int open_fd_hw;
25 7a462745 Aneesh Kumar K.V
int total_open_fd;
26 7a462745 Aneesh Kumar K.V
static int open_fd_rc;
27 9f107513 Anthony Liguori
28 fac4f111 Venkateswararao Jujjuri (JV)
enum {
29 fac4f111 Venkateswararao Jujjuri (JV)
    Oread   = 0x00,
30 fac4f111 Venkateswararao Jujjuri (JV)
    Owrite  = 0x01,
31 fac4f111 Venkateswararao Jujjuri (JV)
    Ordwr   = 0x02,
32 fac4f111 Venkateswararao Jujjuri (JV)
    Oexec   = 0x03,
33 fac4f111 Venkateswararao Jujjuri (JV)
    Oexcl   = 0x04,
34 fac4f111 Venkateswararao Jujjuri (JV)
    Otrunc  = 0x10,
35 fac4f111 Venkateswararao Jujjuri (JV)
    Orexec  = 0x20,
36 fac4f111 Venkateswararao Jujjuri (JV)
    Orclose = 0x40,
37 fac4f111 Venkateswararao Jujjuri (JV)
    Oappend = 0x80,
38 fac4f111 Venkateswararao Jujjuri (JV)
};
39 fac4f111 Venkateswararao Jujjuri (JV)
40 fac4f111 Venkateswararao Jujjuri (JV)
static int omode_to_uflags(int8_t mode)
41 fac4f111 Venkateswararao Jujjuri (JV)
{
42 fac4f111 Venkateswararao Jujjuri (JV)
    int ret = 0;
43 fac4f111 Venkateswararao Jujjuri (JV)
44 fac4f111 Venkateswararao Jujjuri (JV)
    switch (mode & 3) {
45 fac4f111 Venkateswararao Jujjuri (JV)
    case Oread:
46 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_RDONLY;
47 fac4f111 Venkateswararao Jujjuri (JV)
        break;
48 fac4f111 Venkateswararao Jujjuri (JV)
    case Ordwr:
49 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_RDWR;
50 fac4f111 Venkateswararao Jujjuri (JV)
        break;
51 fac4f111 Venkateswararao Jujjuri (JV)
    case Owrite:
52 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_WRONLY;
53 fac4f111 Venkateswararao Jujjuri (JV)
        break;
54 fac4f111 Venkateswararao Jujjuri (JV)
    case Oexec:
55 fac4f111 Venkateswararao Jujjuri (JV)
        ret = O_RDONLY;
56 fac4f111 Venkateswararao Jujjuri (JV)
        break;
57 fac4f111 Venkateswararao Jujjuri (JV)
    }
58 fac4f111 Venkateswararao Jujjuri (JV)
59 fac4f111 Venkateswararao Jujjuri (JV)
    if (mode & Otrunc) {
60 fac4f111 Venkateswararao Jujjuri (JV)
        ret |= O_TRUNC;
61 fac4f111 Venkateswararao Jujjuri (JV)
    }
62 fac4f111 Venkateswararao Jujjuri (JV)
63 fac4f111 Venkateswararao Jujjuri (JV)
    if (mode & Oappend) {
64 fac4f111 Venkateswararao Jujjuri (JV)
        ret |= O_APPEND;
65 fac4f111 Venkateswararao Jujjuri (JV)
    }
66 fac4f111 Venkateswararao Jujjuri (JV)
67 fac4f111 Venkateswararao Jujjuri (JV)
    if (mode & Oexcl) {
68 fac4f111 Venkateswararao Jujjuri (JV)
        ret |= O_EXCL;
69 fac4f111 Venkateswararao Jujjuri (JV)
    }
70 fac4f111 Venkateswararao Jujjuri (JV)
71 fac4f111 Venkateswararao Jujjuri (JV)
    return ret;
72 fac4f111 Venkateswararao Jujjuri (JV)
}
73 fac4f111 Venkateswararao Jujjuri (JV)
74 9844081b M. Mohan Kumar
struct dotl_openflag_map {
75 9844081b M. Mohan Kumar
    int dotl_flag;
76 9844081b M. Mohan Kumar
    int open_flag;
77 9844081b M. Mohan Kumar
};
78 9844081b M. Mohan Kumar
79 9844081b M. Mohan Kumar
static int dotl_to_open_flags(int flags)
80 9844081b M. Mohan Kumar
{
81 9844081b M. Mohan Kumar
    int i;
82 9844081b M. Mohan Kumar
    /*
83 9844081b M. Mohan Kumar
     * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
84 9844081b M. Mohan Kumar
     * and P9_DOTL_NOACCESS
85 9844081b M. Mohan Kumar
     */
86 9844081b M. Mohan Kumar
    int oflags = flags & O_ACCMODE;
87 9844081b M. Mohan Kumar
88 9844081b M. Mohan Kumar
    struct dotl_openflag_map dotl_oflag_map[] = {
89 9844081b M. Mohan Kumar
        { P9_DOTL_CREATE, O_CREAT },
90 9844081b M. Mohan Kumar
        { P9_DOTL_EXCL, O_EXCL },
91 9844081b M. Mohan Kumar
        { P9_DOTL_NOCTTY , O_NOCTTY },
92 9844081b M. Mohan Kumar
        { P9_DOTL_TRUNC, O_TRUNC },
93 9844081b M. Mohan Kumar
        { P9_DOTL_APPEND, O_APPEND },
94 9844081b M. Mohan Kumar
        { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
95 9844081b M. Mohan Kumar
        { P9_DOTL_DSYNC, O_DSYNC },
96 9844081b M. Mohan Kumar
        { P9_DOTL_FASYNC, FASYNC },
97 9844081b M. Mohan Kumar
        { P9_DOTL_DIRECT, O_DIRECT },
98 9844081b M. Mohan Kumar
        { P9_DOTL_LARGEFILE, O_LARGEFILE },
99 9844081b M. Mohan Kumar
        { P9_DOTL_DIRECTORY, O_DIRECTORY },
100 9844081b M. Mohan Kumar
        { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
101 9844081b M. Mohan Kumar
        { P9_DOTL_NOATIME, O_NOATIME },
102 9844081b M. Mohan Kumar
        { P9_DOTL_SYNC, O_SYNC },
103 9844081b M. Mohan Kumar
    };
104 9844081b M. Mohan Kumar
105 9844081b M. Mohan Kumar
    for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
106 9844081b M. Mohan Kumar
        if (flags & dotl_oflag_map[i].dotl_flag) {
107 9844081b M. Mohan Kumar
            oflags |= dotl_oflag_map[i].open_flag;
108 9844081b M. Mohan Kumar
        }
109 9844081b M. Mohan Kumar
    }
110 9844081b M. Mohan Kumar
111 9844081b M. Mohan Kumar
    return oflags;
112 9844081b M. Mohan Kumar
}
113 9844081b M. Mohan Kumar
114 758e8e38 Venkateswararao Jujjuri (JV)
void cred_init(FsCred *credp)
115 131dcb25 Anthony Liguori
{
116 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_uid = -1;
117 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_gid = -1;
118 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_mode = -1;
119 758e8e38 Venkateswararao Jujjuri (JV)
    credp->fc_rdev = -1;
120 131dcb25 Anthony Liguori
}
121 131dcb25 Anthony Liguori
122 d3ab98e6 Aneesh Kumar K.V
static int get_dotl_openflags(V9fsState *s, int oflags)
123 d3ab98e6 Aneesh Kumar K.V
{
124 d3ab98e6 Aneesh Kumar K.V
    int flags;
125 d3ab98e6 Aneesh Kumar K.V
    /*
126 d3ab98e6 Aneesh Kumar K.V
     * Filter the client open flags
127 d3ab98e6 Aneesh Kumar K.V
     */
128 9844081b M. Mohan Kumar
    flags = dotl_to_open_flags(oflags);
129 9844081b M. Mohan Kumar
    flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
130 d3ab98e6 Aneesh Kumar K.V
    /*
131 d3ab98e6 Aneesh Kumar K.V
     * Ignore direct disk access hint until the server supports it.
132 d3ab98e6 Aneesh Kumar K.V
     */
133 d3ab98e6 Aneesh Kumar K.V
    flags &= ~O_DIRECT;
134 d3ab98e6 Aneesh Kumar K.V
    return flags;
135 d3ab98e6 Aneesh Kumar K.V
}
136 d3ab98e6 Aneesh Kumar K.V
137 2289be19 Aneesh Kumar K.V
void v9fs_path_init(V9fsPath *path)
138 2289be19 Aneesh Kumar K.V
{
139 2289be19 Aneesh Kumar K.V
    path->data = NULL;
140 2289be19 Aneesh Kumar K.V
    path->size = 0;
141 2289be19 Aneesh Kumar K.V
}
142 2289be19 Aneesh Kumar K.V
143 2289be19 Aneesh Kumar K.V
void v9fs_path_free(V9fsPath *path)
144 2289be19 Aneesh Kumar K.V
{
145 2289be19 Aneesh Kumar K.V
    g_free(path->data);
146 2289be19 Aneesh Kumar K.V
    path->data = NULL;
147 2289be19 Aneesh Kumar K.V
    path->size = 0;
148 2289be19 Aneesh Kumar K.V
}
149 2289be19 Aneesh Kumar K.V
150 2289be19 Aneesh Kumar K.V
void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
151 2289be19 Aneesh Kumar K.V
{
152 2289be19 Aneesh Kumar K.V
    v9fs_path_free(lhs);
153 2289be19 Aneesh Kumar K.V
    lhs->data = g_malloc(rhs->size);
154 2289be19 Aneesh Kumar K.V
    memcpy(lhs->data, rhs->data, rhs->size);
155 2289be19 Aneesh Kumar K.V
    lhs->size = rhs->size;
156 2289be19 Aneesh Kumar K.V
}
157 2289be19 Aneesh Kumar K.V
158 2289be19 Aneesh Kumar K.V
int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
159 2289be19 Aneesh Kumar K.V
                      const char *name, V9fsPath *path)
160 2289be19 Aneesh Kumar K.V
{
161 2289be19 Aneesh Kumar K.V
    int err;
162 2289be19 Aneesh Kumar K.V
    err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
163 2289be19 Aneesh Kumar K.V
    if (err < 0) {
164 2289be19 Aneesh Kumar K.V
        err = -errno;
165 2289be19 Aneesh Kumar K.V
    }
166 2289be19 Aneesh Kumar K.V
    return err;
167 2289be19 Aneesh Kumar K.V
}
168 2289be19 Aneesh Kumar K.V
169 936532a4 Malahal Naineni
/*
170 936532a4 Malahal Naineni
 * Return TRUE if s1 is an ancestor of s2.
171 936532a4 Malahal Naineni
 *
172 936532a4 Malahal Naineni
 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
173 936532a4 Malahal Naineni
 * As a special case, We treat s1 as ancestor of s2 if they are same!
174 936532a4 Malahal Naineni
 */
175 2289be19 Aneesh Kumar K.V
static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
176 936532a4 Malahal Naineni
{
177 2289be19 Aneesh Kumar K.V
    if (!strncmp(s1->data, s2->data, s1->size - 1)) {
178 2289be19 Aneesh Kumar K.V
        if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
179 936532a4 Malahal Naineni
            return 1;
180 936532a4 Malahal Naineni
        }
181 936532a4 Malahal Naineni
    }
182 936532a4 Malahal Naineni
    return 0;
183 936532a4 Malahal Naineni
}
184 936532a4 Malahal Naineni
185 a03f7874 Anthony Liguori
static size_t v9fs_string_size(V9fsString *str)
186 a03f7874 Anthony Liguori
{
187 a03f7874 Anthony Liguori
    return str->size;
188 a03f7874 Anthony Liguori
}
189 a03f7874 Anthony Liguori
190 b9cb88b0 Aneesh Kumar K.V
/*
191 b9cb88b0 Aneesh Kumar K.V
 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
192 bccacf6c Aneesh Kumar K.V
static int v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
193 b9cb88b0 Aneesh Kumar K.V
{
194 b9cb88b0 Aneesh Kumar K.V
    int err = 1;
195 b9cb88b0 Aneesh Kumar K.V
    if (f->fid_type == P9_FID_FILE) {
196 b9cb88b0 Aneesh Kumar K.V
        if (f->fs.fd == -1) {
197 b9cb88b0 Aneesh Kumar K.V
            do {
198 bccacf6c Aneesh Kumar K.V
                err = v9fs_co_open(pdu, f, f->open_flags);
199 bccacf6c Aneesh Kumar K.V
            } while (err == -EINTR && !pdu->cancelled);
200 b9cb88b0 Aneesh Kumar K.V
        }
201 b9cb88b0 Aneesh Kumar K.V
    } else if (f->fid_type == P9_FID_DIR) {
202 b9cb88b0 Aneesh Kumar K.V
        if (f->fs.dir == NULL) {
203 b9cb88b0 Aneesh Kumar K.V
            do {
204 bccacf6c Aneesh Kumar K.V
                err = v9fs_co_opendir(pdu, f);
205 bccacf6c Aneesh Kumar K.V
            } while (err == -EINTR && !pdu->cancelled);
206 b9cb88b0 Aneesh Kumar K.V
        }
207 b9cb88b0 Aneesh Kumar K.V
    }
208 b9cb88b0 Aneesh Kumar K.V
    return err;
209 b9cb88b0 Aneesh Kumar K.V
}
210 b9cb88b0 Aneesh Kumar K.V
211 bccacf6c Aneesh Kumar K.V
static V9fsFidState *get_fid(V9fsPDU *pdu, int32_t fid)
212 286d5652 Anthony Liguori
{
213 7a462745 Aneesh Kumar K.V
    int err;
214 286d5652 Anthony Liguori
    V9fsFidState *f;
215 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
216 286d5652 Anthony Liguori
217 286d5652 Anthony Liguori
    for (f = s->fid_list; f; f = f->next) {
218 84dfb926 Aneesh Kumar K.V
        BUG_ON(f->clunked);
219 286d5652 Anthony Liguori
        if (f->fid == fid) {
220 7a462745 Aneesh Kumar K.V
            /*
221 7a462745 Aneesh Kumar K.V
             * Update the fid ref upfront so that
222 7a462745 Aneesh Kumar K.V
             * we don't get reclaimed when we yield
223 7a462745 Aneesh Kumar K.V
             * in open later.
224 7a462745 Aneesh Kumar K.V
             */
225 84dfb926 Aneesh Kumar K.V
            f->ref++;
226 7a462745 Aneesh Kumar K.V
            /*
227 7a462745 Aneesh Kumar K.V
             * check whether we need to reopen the
228 7a462745 Aneesh Kumar K.V
             * file. We might have closed the fd
229 7a462745 Aneesh Kumar K.V
             * while trying to free up some file
230 7a462745 Aneesh Kumar K.V
             * descriptors.
231 7a462745 Aneesh Kumar K.V
             */
232 bccacf6c Aneesh Kumar K.V
            err = v9fs_reopen_fid(pdu, f);
233 b9cb88b0 Aneesh Kumar K.V
            if (err < 0) {
234 b9cb88b0 Aneesh Kumar K.V
                f->ref--;
235 b9cb88b0 Aneesh Kumar K.V
                return NULL;
236 b9cb88b0 Aneesh Kumar K.V
            }
237 7a462745 Aneesh Kumar K.V
            /*
238 7a462745 Aneesh Kumar K.V
             * Mark the fid as referenced so that the LRU
239 7a462745 Aneesh Kumar K.V
             * reclaim won't close the file descriptor
240 7a462745 Aneesh Kumar K.V
             */
241 7a462745 Aneesh Kumar K.V
            f->flags |= FID_REFERENCED;
242 286d5652 Anthony Liguori
            return f;
243 286d5652 Anthony Liguori
        }
244 286d5652 Anthony Liguori
    }
245 286d5652 Anthony Liguori
    return NULL;
246 286d5652 Anthony Liguori
}
247 286d5652 Anthony Liguori
248 286d5652 Anthony Liguori
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
249 286d5652 Anthony Liguori
{
250 286d5652 Anthony Liguori
    V9fsFidState *f;
251 286d5652 Anthony Liguori
252 84dfb926 Aneesh Kumar K.V
    for (f = s->fid_list; f; f = f->next) {
253 84dfb926 Aneesh Kumar K.V
        /* If fid is already there return NULL */
254 84dfb926 Aneesh Kumar K.V
        BUG_ON(f->clunked);
255 84dfb926 Aneesh Kumar K.V
        if (f->fid == fid) {
256 84dfb926 Aneesh Kumar K.V
            return NULL;
257 84dfb926 Aneesh Kumar K.V
        }
258 286d5652 Anthony Liguori
    }
259 7267c094 Anthony Liguori
    f = g_malloc0(sizeof(V9fsFidState));
260 286d5652 Anthony Liguori
    f->fid = fid;
261 d62dbb51 Aneesh Kumar K.V
    f->fid_type = P9_FID_NONE;
262 84dfb926 Aneesh Kumar K.V
    f->ref = 1;
263 7a462745 Aneesh Kumar K.V
    /*
264 7a462745 Aneesh Kumar K.V
     * Mark the fid as referenced so that the LRU
265 7a462745 Aneesh Kumar K.V
     * reclaim won't close the file descriptor
266 7a462745 Aneesh Kumar K.V
     */
267 7a462745 Aneesh Kumar K.V
    f->flags |= FID_REFERENCED;
268 286d5652 Anthony Liguori
    f->next = s->fid_list;
269 286d5652 Anthony Liguori
    s->fid_list = f;
270 286d5652 Anthony Liguori
271 286d5652 Anthony Liguori
    return f;
272 286d5652 Anthony Liguori
}
273 286d5652 Anthony Liguori
274 bccacf6c Aneesh Kumar K.V
static int v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
275 10b468bd Aneesh Kumar K.V
{
276 10b468bd Aneesh Kumar K.V
    int retval = 0;
277 10b468bd Aneesh Kumar K.V
278 10b468bd Aneesh Kumar K.V
    if (fidp->fs.xattr.copied_len == -1) {
279 10b468bd Aneesh Kumar K.V
        /* getxattr/listxattr fid */
280 10b468bd Aneesh Kumar K.V
        goto free_value;
281 10b468bd Aneesh Kumar K.V
    }
282 10b468bd Aneesh Kumar K.V
    /*
283 10b468bd Aneesh Kumar K.V
     * if this is fid for setxattr. clunk should
284 10b468bd Aneesh Kumar K.V
     * result in setxattr localcall
285 10b468bd Aneesh Kumar K.V
     */
286 10b468bd Aneesh Kumar K.V
    if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
287 10b468bd Aneesh Kumar K.V
        /* clunk after partial write */
288 10b468bd Aneesh Kumar K.V
        retval = -EINVAL;
289 10b468bd Aneesh Kumar K.V
        goto free_out;
290 10b468bd Aneesh Kumar K.V
    }
291 9ed3ef26 Aneesh Kumar K.V
    if (fidp->fs.xattr.len) {
292 bccacf6c Aneesh Kumar K.V
        retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
293 9ed3ef26 Aneesh Kumar K.V
                                   fidp->fs.xattr.value,
294 9ed3ef26 Aneesh Kumar K.V
                                   fidp->fs.xattr.len,
295 9ed3ef26 Aneesh Kumar K.V
                                   fidp->fs.xattr.flags);
296 9ed3ef26 Aneesh Kumar K.V
    } else {
297 bccacf6c Aneesh Kumar K.V
        retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
298 9ed3ef26 Aneesh Kumar K.V
    }
299 10b468bd Aneesh Kumar K.V
free_out:
300 10b468bd Aneesh Kumar K.V
    v9fs_string_free(&fidp->fs.xattr.name);
301 10b468bd Aneesh Kumar K.V
free_value:
302 10b468bd Aneesh Kumar K.V
    if (fidp->fs.xattr.value) {
303 7267c094 Anthony Liguori
        g_free(fidp->fs.xattr.value);
304 10b468bd Aneesh Kumar K.V
    }
305 10b468bd Aneesh Kumar K.V
    return retval;
306 10b468bd Aneesh Kumar K.V
}
307 10b468bd Aneesh Kumar K.V
308 bccacf6c Aneesh Kumar K.V
static int free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
309 286d5652 Anthony Liguori
{
310 10b468bd Aneesh Kumar K.V
    int retval = 0;
311 84dfb926 Aneesh Kumar K.V
312 84dfb926 Aneesh Kumar K.V
    if (fidp->fid_type == P9_FID_FILE) {
313 7a462745 Aneesh Kumar K.V
        /* If we reclaimed the fd no need to close */
314 7a462745 Aneesh Kumar K.V
        if (fidp->fs.fd != -1) {
315 cc720ddb Aneesh Kumar K.V
            retval = v9fs_co_close(pdu, &fidp->fs);
316 7a462745 Aneesh Kumar K.V
        }
317 84dfb926 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_DIR) {
318 95f65511 Aneesh Kumar K.V
        if (fidp->fs.dir != NULL) {
319 cc720ddb Aneesh Kumar K.V
            retval = v9fs_co_closedir(pdu, &fidp->fs);
320 95f65511 Aneesh Kumar K.V
        }
321 84dfb926 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_XATTR) {
322 bccacf6c Aneesh Kumar K.V
        retval = v9fs_xattr_fid_clunk(pdu, fidp);
323 84dfb926 Aneesh Kumar K.V
    }
324 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&fidp->path);
325 84dfb926 Aneesh Kumar K.V
    g_free(fidp);
326 84dfb926 Aneesh Kumar K.V
    return retval;
327 84dfb926 Aneesh Kumar K.V
}
328 84dfb926 Aneesh Kumar K.V
329 a911a182 Aneesh Kumar K.V
static int put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
330 84dfb926 Aneesh Kumar K.V
{
331 84dfb926 Aneesh Kumar K.V
    BUG_ON(!fidp->ref);
332 84dfb926 Aneesh Kumar K.V
    fidp->ref--;
333 7a462745 Aneesh Kumar K.V
    /*
334 7a462745 Aneesh Kumar K.V
     * Don't free the fid if it is in reclaim list
335 7a462745 Aneesh Kumar K.V
     */
336 84dfb926 Aneesh Kumar K.V
    if (!fidp->ref && fidp->clunked) {
337 e9a0152b Aneesh Kumar K.V
        if (fidp->fid == pdu->s->root_fid) {
338 e9a0152b Aneesh Kumar K.V
            /*
339 e9a0152b Aneesh Kumar K.V
             * if the clunked fid is root fid then we
340 e9a0152b Aneesh Kumar K.V
             * have unmounted the fs on the client side.
341 e9a0152b Aneesh Kumar K.V
             * delete the migration blocker. Ideally, this
342 e9a0152b Aneesh Kumar K.V
             * should be hooked to transport close notification
343 e9a0152b Aneesh Kumar K.V
             */
344 e9a0152b Aneesh Kumar K.V
            if (pdu->s->migration_blocker) {
345 e9a0152b Aneesh Kumar K.V
                migrate_del_blocker(pdu->s->migration_blocker);
346 e9a0152b Aneesh Kumar K.V
                error_free(pdu->s->migration_blocker);
347 e9a0152b Aneesh Kumar K.V
                pdu->s->migration_blocker = NULL;
348 e9a0152b Aneesh Kumar K.V
            }
349 e9a0152b Aneesh Kumar K.V
        }
350 a911a182 Aneesh Kumar K.V
        return free_fid(pdu, fidp);
351 84dfb926 Aneesh Kumar K.V
    }
352 a911a182 Aneesh Kumar K.V
    return 0;
353 84dfb926 Aneesh Kumar K.V
}
354 84dfb926 Aneesh Kumar K.V
355 ce421a19 Aneesh Kumar K.V
static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
356 84dfb926 Aneesh Kumar K.V
{
357 286d5652 Anthony Liguori
    V9fsFidState **fidpp, *fidp;
358 286d5652 Anthony Liguori
359 286d5652 Anthony Liguori
    for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
360 286d5652 Anthony Liguori
        if ((*fidpp)->fid == fid) {
361 286d5652 Anthony Liguori
            break;
362 286d5652 Anthony Liguori
        }
363 286d5652 Anthony Liguori
    }
364 286d5652 Anthony Liguori
    if (*fidpp == NULL) {
365 ce421a19 Aneesh Kumar K.V
        return NULL;
366 286d5652 Anthony Liguori
    }
367 286d5652 Anthony Liguori
    fidp = *fidpp;
368 286d5652 Anthony Liguori
    *fidpp = fidp->next;
369 84dfb926 Aneesh Kumar K.V
    fidp->clunked = 1;
370 ce421a19 Aneesh Kumar K.V
    return fidp;
371 286d5652 Anthony Liguori
}
372 286d5652 Anthony Liguori
373 bccacf6c Aneesh Kumar K.V
void v9fs_reclaim_fd(V9fsPDU *pdu)
374 7a462745 Aneesh Kumar K.V
{
375 7a462745 Aneesh Kumar K.V
    int reclaim_count = 0;
376 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
377 7a462745 Aneesh Kumar K.V
    V9fsFidState *f, *reclaim_list = NULL;
378 7a462745 Aneesh Kumar K.V
379 7a462745 Aneesh Kumar K.V
    for (f = s->fid_list; f; f = f->next) {
380 7a462745 Aneesh Kumar K.V
        /*
381 7a462745 Aneesh Kumar K.V
         * Unlink fids cannot be reclaimed. Check
382 7a462745 Aneesh Kumar K.V
         * for them and skip them. Also skip fids
383 7a462745 Aneesh Kumar K.V
         * currently being operated on.
384 7a462745 Aneesh Kumar K.V
         */
385 7a462745 Aneesh Kumar K.V
        if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
386 7a462745 Aneesh Kumar K.V
            continue;
387 7a462745 Aneesh Kumar K.V
        }
388 7a462745 Aneesh Kumar K.V
        /*
389 7a462745 Aneesh Kumar K.V
         * if it is a recently referenced fid
390 7a462745 Aneesh Kumar K.V
         * we leave the fid untouched and clear the
391 7a462745 Aneesh Kumar K.V
         * reference bit. We come back to it later
392 7a462745 Aneesh Kumar K.V
         * in the next iteration. (a simple LRU without
393 7a462745 Aneesh Kumar K.V
         * moving list elements around)
394 7a462745 Aneesh Kumar K.V
         */
395 7a462745 Aneesh Kumar K.V
        if (f->flags & FID_REFERENCED) {
396 7a462745 Aneesh Kumar K.V
            f->flags &= ~FID_REFERENCED;
397 7a462745 Aneesh Kumar K.V
            continue;
398 7a462745 Aneesh Kumar K.V
        }
399 7a462745 Aneesh Kumar K.V
        /*
400 7a462745 Aneesh Kumar K.V
         * Add fids to reclaim list.
401 7a462745 Aneesh Kumar K.V
         */
402 7a462745 Aneesh Kumar K.V
        if (f->fid_type == P9_FID_FILE) {
403 7a462745 Aneesh Kumar K.V
            if (f->fs.fd != -1) {
404 7a462745 Aneesh Kumar K.V
                /*
405 7a462745 Aneesh Kumar K.V
                 * Up the reference count so that
406 7a462745 Aneesh Kumar K.V
                 * a clunk request won't free this fid
407 7a462745 Aneesh Kumar K.V
                 */
408 7a462745 Aneesh Kumar K.V
                f->ref++;
409 7a462745 Aneesh Kumar K.V
                f->rclm_lst = reclaim_list;
410 7a462745 Aneesh Kumar K.V
                reclaim_list = f;
411 7a462745 Aneesh Kumar K.V
                f->fs_reclaim.fd = f->fs.fd;
412 7a462745 Aneesh Kumar K.V
                f->fs.fd = -1;
413 7a462745 Aneesh Kumar K.V
                reclaim_count++;
414 7a462745 Aneesh Kumar K.V
            }
415 95f65511 Aneesh Kumar K.V
        } else if (f->fid_type == P9_FID_DIR) {
416 95f65511 Aneesh Kumar K.V
            if (f->fs.dir != NULL) {
417 95f65511 Aneesh Kumar K.V
                /*
418 95f65511 Aneesh Kumar K.V
                 * Up the reference count so that
419 95f65511 Aneesh Kumar K.V
                 * a clunk request won't free this fid
420 95f65511 Aneesh Kumar K.V
                 */
421 95f65511 Aneesh Kumar K.V
                f->ref++;
422 95f65511 Aneesh Kumar K.V
                f->rclm_lst = reclaim_list;
423 95f65511 Aneesh Kumar K.V
                reclaim_list = f;
424 95f65511 Aneesh Kumar K.V
                f->fs_reclaim.dir = f->fs.dir;
425 95f65511 Aneesh Kumar K.V
                f->fs.dir = NULL;
426 95f65511 Aneesh Kumar K.V
                reclaim_count++;
427 95f65511 Aneesh Kumar K.V
            }
428 7a462745 Aneesh Kumar K.V
        }
429 7a462745 Aneesh Kumar K.V
        if (reclaim_count >= open_fd_rc) {
430 7a462745 Aneesh Kumar K.V
            break;
431 7a462745 Aneesh Kumar K.V
        }
432 7a462745 Aneesh Kumar K.V
    }
433 7a462745 Aneesh Kumar K.V
    /*
434 7a462745 Aneesh Kumar K.V
     * Now close the fid in reclaim list. Free them if they
435 7a462745 Aneesh Kumar K.V
     * are already clunked.
436 7a462745 Aneesh Kumar K.V
     */
437 7a462745 Aneesh Kumar K.V
    while (reclaim_list) {
438 7a462745 Aneesh Kumar K.V
        f = reclaim_list;
439 7a462745 Aneesh Kumar K.V
        reclaim_list = f->rclm_lst;
440 7a462745 Aneesh Kumar K.V
        if (f->fid_type == P9_FID_FILE) {
441 cc720ddb Aneesh Kumar K.V
            v9fs_co_close(pdu, &f->fs_reclaim);
442 95f65511 Aneesh Kumar K.V
        } else if (f->fid_type == P9_FID_DIR) {
443 cc720ddb Aneesh Kumar K.V
            v9fs_co_closedir(pdu, &f->fs_reclaim);
444 7a462745 Aneesh Kumar K.V
        }
445 7a462745 Aneesh Kumar K.V
        f->rclm_lst = NULL;
446 7a462745 Aneesh Kumar K.V
        /*
447 7a462745 Aneesh Kumar K.V
         * Now drop the fid reference, free it
448 7a462745 Aneesh Kumar K.V
         * if clunked.
449 7a462745 Aneesh Kumar K.V
         */
450 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, f);
451 7a462745 Aneesh Kumar K.V
    }
452 7a462745 Aneesh Kumar K.V
}
453 7a462745 Aneesh Kumar K.V
454 bccacf6c Aneesh Kumar K.V
static int v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
455 7a462745 Aneesh Kumar K.V
{
456 7a462745 Aneesh Kumar K.V
    int err;
457 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
458 7a462745 Aneesh Kumar K.V
    V9fsFidState *fidp, head_fid;
459 7a462745 Aneesh Kumar K.V
460 7a462745 Aneesh Kumar K.V
    head_fid.next = s->fid_list;
461 7a462745 Aneesh Kumar K.V
    for (fidp = s->fid_list; fidp; fidp = fidp->next) {
462 2289be19 Aneesh Kumar K.V
        if (fidp->path.size != path->size) {
463 2289be19 Aneesh Kumar K.V
            continue;
464 2289be19 Aneesh Kumar K.V
        }
465 2289be19 Aneesh Kumar K.V
        if (!memcmp(fidp->path.data, path->data, path->size)) {
466 7a462745 Aneesh Kumar K.V
            /* Mark the fid non reclaimable. */
467 7a462745 Aneesh Kumar K.V
            fidp->flags |= FID_NON_RECLAIMABLE;
468 b9cb88b0 Aneesh Kumar K.V
469 b9cb88b0 Aneesh Kumar K.V
            /* reopen the file/dir if already closed */
470 bccacf6c Aneesh Kumar K.V
            err = v9fs_reopen_fid(pdu, fidp);
471 b9cb88b0 Aneesh Kumar K.V
            if (err < 0) {
472 b9cb88b0 Aneesh Kumar K.V
                return -1;
473 b9cb88b0 Aneesh Kumar K.V
            }
474 b9cb88b0 Aneesh Kumar K.V
            /*
475 b9cb88b0 Aneesh Kumar K.V
             * Go back to head of fid list because
476 b9cb88b0 Aneesh Kumar K.V
             * the list could have got updated when
477 b9cb88b0 Aneesh Kumar K.V
             * switched to the worker thread
478 b9cb88b0 Aneesh Kumar K.V
             */
479 b9cb88b0 Aneesh Kumar K.V
            if (err == 0) {
480 7a462745 Aneesh Kumar K.V
                fidp = &head_fid;
481 7a462745 Aneesh Kumar K.V
            }
482 7a462745 Aneesh Kumar K.V
        }
483 7a462745 Aneesh Kumar K.V
    }
484 7a462745 Aneesh Kumar K.V
    return 0;
485 7a462745 Aneesh Kumar K.V
}
486 7a462745 Aneesh Kumar K.V
487 b41e2992 Deepak C Shetty
static void virtfs_reset(V9fsPDU *pdu)
488 b41e2992 Deepak C Shetty
{
489 b41e2992 Deepak C Shetty
    V9fsState *s = pdu->s;
490 b41e2992 Deepak C Shetty
    V9fsFidState *fidp = NULL;
491 b41e2992 Deepak C Shetty
492 b41e2992 Deepak C Shetty
    /* Free all fids */
493 b41e2992 Deepak C Shetty
    while (s->fid_list) {
494 b41e2992 Deepak C Shetty
        fidp = s->fid_list;
495 b41e2992 Deepak C Shetty
        s->fid_list = fidp->next;
496 b41e2992 Deepak C Shetty
497 b41e2992 Deepak C Shetty
        if (fidp->ref) {
498 b41e2992 Deepak C Shetty
            fidp->clunked = 1;
499 b41e2992 Deepak C Shetty
        } else {
500 b41e2992 Deepak C Shetty
            free_fid(pdu, fidp);
501 b41e2992 Deepak C Shetty
        }
502 b41e2992 Deepak C Shetty
    }
503 b41e2992 Deepak C Shetty
    if (fidp) {
504 b41e2992 Deepak C Shetty
        /* One or more unclunked fids found... */
505 b41e2992 Deepak C Shetty
        error_report("9pfs:%s: One or more uncluncked fids "
506 b41e2992 Deepak C Shetty
                     "found during reset", __func__);
507 b41e2992 Deepak C Shetty
    }
508 b41e2992 Deepak C Shetty
}
509 b41e2992 Deepak C Shetty
510 286d5652 Anthony Liguori
#define P9_QID_TYPE_DIR         0x80
511 286d5652 Anthony Liguori
#define P9_QID_TYPE_SYMLINK     0x02
512 286d5652 Anthony Liguori
513 286d5652 Anthony Liguori
#define P9_STAT_MODE_DIR        0x80000000
514 286d5652 Anthony Liguori
#define P9_STAT_MODE_APPEND     0x40000000
515 286d5652 Anthony Liguori
#define P9_STAT_MODE_EXCL       0x20000000
516 286d5652 Anthony Liguori
#define P9_STAT_MODE_MOUNT      0x10000000
517 286d5652 Anthony Liguori
#define P9_STAT_MODE_AUTH       0x08000000
518 286d5652 Anthony Liguori
#define P9_STAT_MODE_TMP        0x04000000
519 286d5652 Anthony Liguori
#define P9_STAT_MODE_SYMLINK    0x02000000
520 286d5652 Anthony Liguori
#define P9_STAT_MODE_LINK       0x01000000
521 286d5652 Anthony Liguori
#define P9_STAT_MODE_DEVICE     0x00800000
522 286d5652 Anthony Liguori
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
523 286d5652 Anthony Liguori
#define P9_STAT_MODE_SOCKET     0x00100000
524 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETUID     0x00080000
525 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETGID     0x00040000
526 286d5652 Anthony Liguori
#define P9_STAT_MODE_SETVTX     0x00010000
527 286d5652 Anthony Liguori
528 286d5652 Anthony Liguori
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
529 286d5652 Anthony Liguori
                                P9_STAT_MODE_SYMLINK |      \
530 286d5652 Anthony Liguori
                                P9_STAT_MODE_LINK |         \
531 286d5652 Anthony Liguori
                                P9_STAT_MODE_DEVICE |       \
532 286d5652 Anthony Liguori
                                P9_STAT_MODE_NAMED_PIPE |   \
533 286d5652 Anthony Liguori
                                P9_STAT_MODE_SOCKET)
534 286d5652 Anthony Liguori
535 286d5652 Anthony Liguori
/* This is the algorithm from ufs in spfs */
536 286d5652 Anthony Liguori
static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
537 286d5652 Anthony Liguori
{
538 286d5652 Anthony Liguori
    size_t size;
539 286d5652 Anthony Liguori
540 25427ec1 Aneesh Kumar K.V
    memset(&qidp->path, 0, sizeof(qidp->path));
541 286d5652 Anthony Liguori
    size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
542 286d5652 Anthony Liguori
    memcpy(&qidp->path, &stbuf->st_ino, size);
543 286d5652 Anthony Liguori
    qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
544 286d5652 Anthony Liguori
    qidp->type = 0;
545 286d5652 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
546 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_DIR;
547 286d5652 Anthony Liguori
    }
548 286d5652 Anthony Liguori
    if (S_ISLNK(stbuf->st_mode)) {
549 286d5652 Anthony Liguori
        qidp->type |= P9_QID_TYPE_SYMLINK;
550 286d5652 Anthony Liguori
    }
551 286d5652 Anthony Liguori
}
552 286d5652 Anthony Liguori
553 bccacf6c Aneesh Kumar K.V
static int fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, V9fsQID *qidp)
554 286d5652 Anthony Liguori
{
555 286d5652 Anthony Liguori
    struct stat stbuf;
556 286d5652 Anthony Liguori
    int err;
557 286d5652 Anthony Liguori
558 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
559 8c158561 Aneesh Kumar K.V
    if (err < 0) {
560 286d5652 Anthony Liguori
        return err;
561 286d5652 Anthony Liguori
    }
562 286d5652 Anthony Liguori
    stat_to_qid(&stbuf, qidp);
563 286d5652 Anthony Liguori
    return 0;
564 286d5652 Anthony Liguori
}
565 286d5652 Anthony Liguori
566 9f107513 Anthony Liguori
static V9fsPDU *alloc_pdu(V9fsState *s)
567 9f107513 Anthony Liguori
{
568 9f107513 Anthony Liguori
    V9fsPDU *pdu = NULL;
569 9f107513 Anthony Liguori
570 9f107513 Anthony Liguori
    if (!QLIST_EMPTY(&s->free_list)) {
571 bccacf6c Aneesh Kumar K.V
        pdu = QLIST_FIRST(&s->free_list);
572 bccacf6c Aneesh Kumar K.V
        QLIST_REMOVE(pdu, next);
573 bccacf6c Aneesh Kumar K.V
        QLIST_INSERT_HEAD(&s->active_list, pdu, next);
574 9f107513 Anthony Liguori
    }
575 9f107513 Anthony Liguori
    return pdu;
576 9f107513 Anthony Liguori
}
577 9f107513 Anthony Liguori
578 9f107513 Anthony Liguori
static void free_pdu(V9fsState *s, V9fsPDU *pdu)
579 9f107513 Anthony Liguori
{
580 9f107513 Anthony Liguori
    if (pdu) {
581 bccacf6c Aneesh Kumar K.V
        /*
582 bccacf6c Aneesh Kumar K.V
         * Cancelled pdu are added back to the freelist
583 bccacf6c Aneesh Kumar K.V
         * by flush request .
584 bccacf6c Aneesh Kumar K.V
         */
585 bccacf6c Aneesh Kumar K.V
        if (!pdu->cancelled) {
586 bccacf6c Aneesh Kumar K.V
            QLIST_REMOVE(pdu, next);
587 bccacf6c Aneesh Kumar K.V
            QLIST_INSERT_HEAD(&s->free_list, pdu, next);
588 bccacf6c Aneesh Kumar K.V
        }
589 9f107513 Anthony Liguori
    }
590 9f107513 Anthony Liguori
}
591 9f107513 Anthony Liguori
592 ddca7f86 M. Mohan Kumar
/*
593 ddca7f86 M. Mohan Kumar
 * We don't do error checking for pdu_marshal/unmarshal here
594 ddca7f86 M. Mohan Kumar
 * because we always expect to have enough space to encode
595 ddca7f86 M. Mohan Kumar
 * error details
596 ddca7f86 M. Mohan Kumar
 */
597 405a549a Anthony Liguori
static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
598 405a549a Anthony Liguori
{
599 405a549a Anthony Liguori
    int8_t id = pdu->id + 1; /* Response */
600 405a549a Anthony Liguori
601 405a549a Anthony Liguori
    if (len < 0) {
602 405a549a Anthony Liguori
        int err = -len;
603 8f4d1ca5 Arun R Bharadwaj
        len = 7;
604 405a549a Anthony Liguori
605 8f4d1ca5 Arun R Bharadwaj
        if (s->proto_version != V9FS_PROTO_2000L) {
606 8f4d1ca5 Arun R Bharadwaj
            V9fsString str;
607 8f4d1ca5 Arun R Bharadwaj
608 8f4d1ca5 Arun R Bharadwaj
            str.data = strerror(err);
609 8f4d1ca5 Arun R Bharadwaj
            str.size = strlen(str.data);
610 8f4d1ca5 Arun R Bharadwaj
611 8f4d1ca5 Arun R Bharadwaj
            len += pdu_marshal(pdu, len, "s", &str);
612 8f4d1ca5 Arun R Bharadwaj
            id = P9_RERROR;
613 8f4d1ca5 Arun R Bharadwaj
        }
614 405a549a Anthony Liguori
615 cf03eb2c Arun R Bharadwaj
        len += pdu_marshal(pdu, len, "d", err);
616 405a549a Anthony Liguori
617 8f4d1ca5 Arun R Bharadwaj
        if (s->proto_version == V9FS_PROTO_2000L) {
618 8f4d1ca5 Arun R Bharadwaj
            id = P9_RLERROR;
619 8f4d1ca5 Arun R Bharadwaj
        }
620 7999f7e1 Aneesh Kumar K.V
        trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
621 405a549a Anthony Liguori
    }
622 405a549a Anthony Liguori
623 405a549a Anthony Liguori
    /* fill out the header */
624 405a549a Anthony Liguori
    pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
625 405a549a Anthony Liguori
626 405a549a Anthony Liguori
    /* keep these in sync */
627 405a549a Anthony Liguori
    pdu->size = len;
628 405a549a Anthony Liguori
    pdu->id = id;
629 405a549a Anthony Liguori
630 405a549a Anthony Liguori
    /* push onto queue and notify */
631 405a549a Anthony Liguori
    virtqueue_push(s->vq, &pdu->elem, len);
632 405a549a Anthony Liguori
633 405a549a Anthony Liguori
    /* FIXME: we should batch these completions */
634 13daf6ca KONRAD Frederic
    virtio_notify(VIRTIO_DEVICE(s), s->vq);
635 405a549a Anthony Liguori
636 bccacf6c Aneesh Kumar K.V
    /* Now wakeup anybody waiting in flush for this request */
637 bccacf6c Aneesh Kumar K.V
    qemu_co_queue_next(&pdu->complete);
638 bccacf6c Aneesh Kumar K.V
639 405a549a Anthony Liguori
    free_pdu(s, pdu);
640 405a549a Anthony Liguori
}
641 405a549a Anthony Liguori
642 bb9e3216 Anthony Liguori
static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
643 bb9e3216 Anthony Liguori
{
644 bb9e3216 Anthony Liguori
    mode_t ret;
645 bb9e3216 Anthony Liguori
646 bb9e3216 Anthony Liguori
    ret = mode & 0777;
647 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_DIR) {
648 bb9e3216 Anthony Liguori
        ret |= S_IFDIR;
649 bb9e3216 Anthony Liguori
    }
650 bb9e3216 Anthony Liguori
651 cf03eb2c Arun R Bharadwaj
    if (mode & P9_STAT_MODE_SYMLINK) {
652 cf03eb2c Arun R Bharadwaj
        ret |= S_IFLNK;
653 cf03eb2c Arun R Bharadwaj
    }
654 cf03eb2c Arun R Bharadwaj
    if (mode & P9_STAT_MODE_SOCKET) {
655 cf03eb2c Arun R Bharadwaj
        ret |= S_IFSOCK;
656 cf03eb2c Arun R Bharadwaj
    }
657 cf03eb2c Arun R Bharadwaj
    if (mode & P9_STAT_MODE_NAMED_PIPE) {
658 cf03eb2c Arun R Bharadwaj
        ret |= S_IFIFO;
659 cf03eb2c Arun R Bharadwaj
    }
660 cf03eb2c Arun R Bharadwaj
    if (mode & P9_STAT_MODE_DEVICE) {
661 c7e587b7 Aneesh Kumar K.V
        if (extension->size && extension->data[0] == 'c') {
662 cf03eb2c Arun R Bharadwaj
            ret |= S_IFCHR;
663 cf03eb2c Arun R Bharadwaj
        } else {
664 cf03eb2c Arun R Bharadwaj
            ret |= S_IFBLK;
665 bb9e3216 Anthony Liguori
        }
666 bb9e3216 Anthony Liguori
    }
667 bb9e3216 Anthony Liguori
668 bb9e3216 Anthony Liguori
    if (!(ret&~0777)) {
669 bb9e3216 Anthony Liguori
        ret |= S_IFREG;
670 bb9e3216 Anthony Liguori
    }
671 bb9e3216 Anthony Liguori
672 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETUID) {
673 bb9e3216 Anthony Liguori
        ret |= S_ISUID;
674 bb9e3216 Anthony Liguori
    }
675 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETGID) {
676 bb9e3216 Anthony Liguori
        ret |= S_ISGID;
677 bb9e3216 Anthony Liguori
    }
678 bb9e3216 Anthony Liguori
    if (mode & P9_STAT_MODE_SETVTX) {
679 bb9e3216 Anthony Liguori
        ret |= S_ISVTX;
680 bb9e3216 Anthony Liguori
    }
681 bb9e3216 Anthony Liguori
682 bb9e3216 Anthony Liguori
    return ret;
683 bb9e3216 Anthony Liguori
}
684 bb9e3216 Anthony Liguori
685 bb9e3216 Anthony Liguori
static int donttouch_stat(V9fsStat *stat)
686 bb9e3216 Anthony Liguori
{
687 bb9e3216 Anthony Liguori
    if (stat->type == -1 &&
688 bb9e3216 Anthony Liguori
        stat->dev == -1 &&
689 bb9e3216 Anthony Liguori
        stat->qid.type == -1 &&
690 bb9e3216 Anthony Liguori
        stat->qid.version == -1 &&
691 bb9e3216 Anthony Liguori
        stat->qid.path == -1 &&
692 bb9e3216 Anthony Liguori
        stat->mode == -1 &&
693 bb9e3216 Anthony Liguori
        stat->atime == -1 &&
694 bb9e3216 Anthony Liguori
        stat->mtime == -1 &&
695 bb9e3216 Anthony Liguori
        stat->length == -1 &&
696 bb9e3216 Anthony Liguori
        !stat->name.size &&
697 bb9e3216 Anthony Liguori
        !stat->uid.size &&
698 bb9e3216 Anthony Liguori
        !stat->gid.size &&
699 bb9e3216 Anthony Liguori
        !stat->muid.size &&
700 bb9e3216 Anthony Liguori
        stat->n_uid == -1 &&
701 bb9e3216 Anthony Liguori
        stat->n_gid == -1 &&
702 bb9e3216 Anthony Liguori
        stat->n_muid == -1) {
703 bb9e3216 Anthony Liguori
        return 1;
704 bb9e3216 Anthony Liguori
    }
705 bb9e3216 Anthony Liguori
706 bb9e3216 Anthony Liguori
    return 0;
707 bb9e3216 Anthony Liguori
}
708 bb9e3216 Anthony Liguori
709 ddca7f86 M. Mohan Kumar
static void v9fs_stat_init(V9fsStat *stat)
710 ddca7f86 M. Mohan Kumar
{
711 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&stat->name);
712 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&stat->uid);
713 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&stat->gid);
714 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&stat->muid);
715 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&stat->extension);
716 ddca7f86 M. Mohan Kumar
}
717 ddca7f86 M. Mohan Kumar
718 bb9e3216 Anthony Liguori
static void v9fs_stat_free(V9fsStat *stat)
719 bb9e3216 Anthony Liguori
{
720 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->name);
721 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->uid);
722 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->gid);
723 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->muid);
724 bb9e3216 Anthony Liguori
    v9fs_string_free(&stat->extension);
725 bb9e3216 Anthony Liguori
}
726 bb9e3216 Anthony Liguori
727 bb9e3216 Anthony Liguori
static uint32_t stat_to_v9mode(const struct stat *stbuf)
728 bb9e3216 Anthony Liguori
{
729 bb9e3216 Anthony Liguori
    uint32_t mode;
730 bb9e3216 Anthony Liguori
731 bb9e3216 Anthony Liguori
    mode = stbuf->st_mode & 0777;
732 bb9e3216 Anthony Liguori
    if (S_ISDIR(stbuf->st_mode)) {
733 bb9e3216 Anthony Liguori
        mode |= P9_STAT_MODE_DIR;
734 bb9e3216 Anthony Liguori
    }
735 bb9e3216 Anthony Liguori
736 cf03eb2c Arun R Bharadwaj
    if (S_ISLNK(stbuf->st_mode)) {
737 cf03eb2c Arun R Bharadwaj
        mode |= P9_STAT_MODE_SYMLINK;
738 cf03eb2c Arun R Bharadwaj
    }
739 bb9e3216 Anthony Liguori
740 cf03eb2c Arun R Bharadwaj
    if (S_ISSOCK(stbuf->st_mode)) {
741 cf03eb2c Arun R Bharadwaj
        mode |= P9_STAT_MODE_SOCKET;
742 cf03eb2c Arun R Bharadwaj
    }
743 bb9e3216 Anthony Liguori
744 cf03eb2c Arun R Bharadwaj
    if (S_ISFIFO(stbuf->st_mode)) {
745 cf03eb2c Arun R Bharadwaj
        mode |= P9_STAT_MODE_NAMED_PIPE;
746 cf03eb2c Arun R Bharadwaj
    }
747 bb9e3216 Anthony Liguori
748 cf03eb2c Arun R Bharadwaj
    if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
749 cf03eb2c Arun R Bharadwaj
        mode |= P9_STAT_MODE_DEVICE;
750 cf03eb2c Arun R Bharadwaj
    }
751 bb9e3216 Anthony Liguori
752 cf03eb2c Arun R Bharadwaj
    if (stbuf->st_mode & S_ISUID) {
753 cf03eb2c Arun R Bharadwaj
        mode |= P9_STAT_MODE_SETUID;
754 cf03eb2c Arun R Bharadwaj
    }
755 bb9e3216 Anthony Liguori
756 cf03eb2c Arun R Bharadwaj
    if (stbuf->st_mode & S_ISGID) {
757 cf03eb2c Arun R Bharadwaj
        mode |= P9_STAT_MODE_SETGID;
758 cf03eb2c Arun R Bharadwaj
    }
759 bb9e3216 Anthony Liguori
760 cf03eb2c Arun R Bharadwaj
    if (stbuf->st_mode & S_ISVTX) {
761 cf03eb2c Arun R Bharadwaj
        mode |= P9_STAT_MODE_SETVTX;
762 bb9e3216 Anthony Liguori
    }
763 bb9e3216 Anthony Liguori
764 bb9e3216 Anthony Liguori
    return mode;
765 bb9e3216 Anthony Liguori
}
766 bb9e3216 Anthony Liguori
767 bccacf6c Aneesh Kumar K.V
static int stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
768 bb9e3216 Anthony Liguori
                            const struct stat *stbuf,
769 bb9e3216 Anthony Liguori
                            V9fsStat *v9stat)
770 bb9e3216 Anthony Liguori
{
771 bb9e3216 Anthony Liguori
    int err;
772 bb9e3216 Anthony Liguori
    const char *str;
773 bb9e3216 Anthony Liguori
774 bb9e3216 Anthony Liguori
    memset(v9stat, 0, sizeof(*v9stat));
775 bb9e3216 Anthony Liguori
776 bb9e3216 Anthony Liguori
    stat_to_qid(stbuf, &v9stat->qid);
777 bb9e3216 Anthony Liguori
    v9stat->mode = stat_to_v9mode(stbuf);
778 bb9e3216 Anthony Liguori
    v9stat->atime = stbuf->st_atime;
779 bb9e3216 Anthony Liguori
    v9stat->mtime = stbuf->st_mtime;
780 bb9e3216 Anthony Liguori
    v9stat->length = stbuf->st_size;
781 bb9e3216 Anthony Liguori
782 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->uid);
783 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->gid);
784 bb9e3216 Anthony Liguori
    v9fs_string_null(&v9stat->muid);
785 bb9e3216 Anthony Liguori
786 cf03eb2c Arun R Bharadwaj
    v9stat->n_uid = stbuf->st_uid;
787 cf03eb2c Arun R Bharadwaj
    v9stat->n_gid = stbuf->st_gid;
788 cf03eb2c Arun R Bharadwaj
    v9stat->n_muid = 0;
789 bb9e3216 Anthony Liguori
790 cf03eb2c Arun R Bharadwaj
    v9fs_string_null(&v9stat->extension);
791 bb9e3216 Anthony Liguori
792 cf03eb2c Arun R Bharadwaj
    if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
793 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_readlink(pdu, name, &v9stat->extension);
794 7a5ca31e Venkateswararao Jujjuri
        if (err < 0) {
795 cf03eb2c Arun R Bharadwaj
            return err;
796 bb9e3216 Anthony Liguori
        }
797 cf03eb2c Arun R Bharadwaj
    } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
798 cf03eb2c Arun R Bharadwaj
        v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
799 cf03eb2c Arun R Bharadwaj
                S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
800 cf03eb2c Arun R Bharadwaj
                major(stbuf->st_rdev), minor(stbuf->st_rdev));
801 cf03eb2c Arun R Bharadwaj
    } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
802 c9ba47dc Stefan Weil
        v9fs_string_sprintf(&v9stat->extension, "%s %lu",
803 c9ba47dc Stefan Weil
                "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
804 bb9e3216 Anthony Liguori
    }
805 bb9e3216 Anthony Liguori
806 bb9e3216 Anthony Liguori
    str = strrchr(name->data, '/');
807 bb9e3216 Anthony Liguori
    if (str) {
808 bb9e3216 Anthony Liguori
        str += 1;
809 bb9e3216 Anthony Liguori
    } else {
810 bb9e3216 Anthony Liguori
        str = name->data;
811 bb9e3216 Anthony Liguori
    }
812 bb9e3216 Anthony Liguori
813 bb9e3216 Anthony Liguori
    v9fs_string_sprintf(&v9stat->name, "%s", str);
814 bb9e3216 Anthony Liguori
815 bb9e3216 Anthony Liguori
    v9stat->size = 61 +
816 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->name) +
817 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->uid) +
818 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->gid) +
819 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->muid) +
820 bb9e3216 Anthony Liguori
        v9fs_string_size(&v9stat->extension);
821 bb9e3216 Anthony Liguori
    return 0;
822 bb9e3216 Anthony Liguori
}
823 bb9e3216 Anthony Liguori
824 00ede4c2 Sripathi Kodi
#define P9_STATS_MODE          0x00000001ULL
825 00ede4c2 Sripathi Kodi
#define P9_STATS_NLINK         0x00000002ULL
826 00ede4c2 Sripathi Kodi
#define P9_STATS_UID           0x00000004ULL
827 00ede4c2 Sripathi Kodi
#define P9_STATS_GID           0x00000008ULL
828 00ede4c2 Sripathi Kodi
#define P9_STATS_RDEV          0x00000010ULL
829 00ede4c2 Sripathi Kodi
#define P9_STATS_ATIME         0x00000020ULL
830 00ede4c2 Sripathi Kodi
#define P9_STATS_MTIME         0x00000040ULL
831 00ede4c2 Sripathi Kodi
#define P9_STATS_CTIME         0x00000080ULL
832 00ede4c2 Sripathi Kodi
#define P9_STATS_INO           0x00000100ULL
833 00ede4c2 Sripathi Kodi
#define P9_STATS_SIZE          0x00000200ULL
834 00ede4c2 Sripathi Kodi
#define P9_STATS_BLOCKS        0x00000400ULL
835 00ede4c2 Sripathi Kodi
836 00ede4c2 Sripathi Kodi
#define P9_STATS_BTIME         0x00000800ULL
837 00ede4c2 Sripathi Kodi
#define P9_STATS_GEN           0x00001000ULL
838 00ede4c2 Sripathi Kodi
#define P9_STATS_DATA_VERSION  0x00002000ULL
839 00ede4c2 Sripathi Kodi
840 00ede4c2 Sripathi Kodi
#define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
841 00ede4c2 Sripathi Kodi
#define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
842 00ede4c2 Sripathi Kodi
843 00ede4c2 Sripathi Kodi
844 00ede4c2 Sripathi Kodi
static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
845 8db21ce7 Aneesh Kumar K.V
                                V9fsStatDotl *v9lstat)
846 00ede4c2 Sripathi Kodi
{
847 00ede4c2 Sripathi Kodi
    memset(v9lstat, 0, sizeof(*v9lstat));
848 00ede4c2 Sripathi Kodi
849 00ede4c2 Sripathi Kodi
    v9lstat->st_mode = stbuf->st_mode;
850 00ede4c2 Sripathi Kodi
    v9lstat->st_nlink = stbuf->st_nlink;
851 00ede4c2 Sripathi Kodi
    v9lstat->st_uid = stbuf->st_uid;
852 00ede4c2 Sripathi Kodi
    v9lstat->st_gid = stbuf->st_gid;
853 00ede4c2 Sripathi Kodi
    v9lstat->st_rdev = stbuf->st_rdev;
854 00ede4c2 Sripathi Kodi
    v9lstat->st_size = stbuf->st_size;
855 00ede4c2 Sripathi Kodi
    v9lstat->st_blksize = stbuf->st_blksize;
856 00ede4c2 Sripathi Kodi
    v9lstat->st_blocks = stbuf->st_blocks;
857 00ede4c2 Sripathi Kodi
    v9lstat->st_atime_sec = stbuf->st_atime;
858 00ede4c2 Sripathi Kodi
    v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
859 00ede4c2 Sripathi Kodi
    v9lstat->st_mtime_sec = stbuf->st_mtime;
860 00ede4c2 Sripathi Kodi
    v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
861 00ede4c2 Sripathi Kodi
    v9lstat->st_ctime_sec = stbuf->st_ctime;
862 00ede4c2 Sripathi Kodi
    v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
863 00ede4c2 Sripathi Kodi
    /* Currently we only support BASIC fields in stat */
864 00ede4c2 Sripathi Kodi
    v9lstat->st_result_mask = P9_STATS_BASIC;
865 00ede4c2 Sripathi Kodi
866 00ede4c2 Sripathi Kodi
    stat_to_qid(stbuf, &v9lstat->qid);
867 00ede4c2 Sripathi Kodi
}
868 00ede4c2 Sripathi Kodi
869 1f5a89bf Anthony Liguori
static void print_sg(struct iovec *sg, int cnt)
870 1f5a89bf Anthony Liguori
{
871 1f5a89bf Anthony Liguori
    int i;
872 1f5a89bf Anthony Liguori
873 1f5a89bf Anthony Liguori
    printf("sg[%d]: {", cnt);
874 1f5a89bf Anthony Liguori
    for (i = 0; i < cnt; i++) {
875 1f5a89bf Anthony Liguori
        if (i) {
876 1f5a89bf Anthony Liguori
            printf(", ");
877 1f5a89bf Anthony Liguori
        }
878 1f5a89bf Anthony Liguori
        printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
879 1f5a89bf Anthony Liguori
    }
880 1f5a89bf Anthony Liguori
    printf("}\n");
881 1f5a89bf Anthony Liguori
}
882 1f5a89bf Anthony Liguori
883 2289be19 Aneesh Kumar K.V
/* Will call this only for path name based fid */
884 2289be19 Aneesh Kumar K.V
static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
885 8cf89e00 Anthony Liguori
{
886 2289be19 Aneesh Kumar K.V
    V9fsPath str;
887 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&str);
888 2289be19 Aneesh Kumar K.V
    v9fs_path_copy(&str, dst);
889 2289be19 Aneesh Kumar K.V
    v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len);
890 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&str);
891 2289be19 Aneesh Kumar K.V
    /* +1 to include terminating NULL */
892 2289be19 Aneesh Kumar K.V
    dst->size++;
893 8cf89e00 Anthony Liguori
}
894 8cf89e00 Anthony Liguori
895 2c74c2cb M. Mohan Kumar
static inline bool is_ro_export(FsContext *ctx)
896 2c74c2cb M. Mohan Kumar
{
897 2c74c2cb M. Mohan Kumar
    return ctx->export_flags & V9FS_RDONLY;
898 2c74c2cb M. Mohan Kumar
}
899 2c74c2cb M. Mohan Kumar
900 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_version(void *opaque)
901 9f107513 Anthony Liguori
{
902 ddca7f86 M. Mohan Kumar
    ssize_t err;
903 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
904 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
905 92c1ad03 Anthony Liguori
    V9fsString version;
906 92c1ad03 Anthony Liguori
    size_t offset = 7;
907 92c1ad03 Anthony Liguori
908 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&version);
909 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
910 ddca7f86 M. Mohan Kumar
    if (err < 0) {
911 ddca7f86 M. Mohan Kumar
        offset = err;
912 ddca7f86 M. Mohan Kumar
        goto out;
913 ddca7f86 M. Mohan Kumar
    }
914 c572f23a Harsh Prateek Bora
    trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
915 92c1ad03 Anthony Liguori
916 b41e2992 Deepak C Shetty
    virtfs_reset(pdu);
917 b41e2992 Deepak C Shetty
918 84151514 M. Mohan Kumar
    if (!strcmp(version.data, "9P2000.u")) {
919 84151514 M. Mohan Kumar
        s->proto_version = V9FS_PROTO_2000U;
920 84151514 M. Mohan Kumar
    } else if (!strcmp(version.data, "9P2000.L")) {
921 84151514 M. Mohan Kumar
        s->proto_version = V9FS_PROTO_2000L;
922 84151514 M. Mohan Kumar
    } else {
923 92c1ad03 Anthony Liguori
        v9fs_string_sprintf(&version, "unknown");
924 9f107513 Anthony Liguori
    }
925 92c1ad03 Anthony Liguori
926 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
927 ddca7f86 M. Mohan Kumar
    if (err < 0) {
928 ddca7f86 M. Mohan Kumar
        offset = err;
929 ddca7f86 M. Mohan Kumar
        goto out;
930 ddca7f86 M. Mohan Kumar
    }
931 ddca7f86 M. Mohan Kumar
    offset += err;
932 c572f23a Harsh Prateek Bora
    trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
933 ddca7f86 M. Mohan Kumar
out:
934 92c1ad03 Anthony Liguori
    complete_pdu(s, pdu, offset);
935 92c1ad03 Anthony Liguori
    v9fs_string_free(&version);
936 9f107513 Anthony Liguori
}
937 9f107513 Anthony Liguori
938 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_attach(void *opaque)
939 9f107513 Anthony Liguori
{
940 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
941 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
942 955efc47 Anthony Liguori
    int32_t fid, afid, n_uname;
943 955efc47 Anthony Liguori
    V9fsString uname, aname;
944 955efc47 Anthony Liguori
    V9fsFidState *fidp;
945 955efc47 Anthony Liguori
    size_t offset = 7;
946 8c158561 Aneesh Kumar K.V
    V9fsQID qid;
947 955efc47 Anthony Liguori
    ssize_t err;
948 955efc47 Anthony Liguori
949 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&uname);
950 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&aname);
951 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
952 ddca7f86 M. Mohan Kumar
                        &afid, &uname, &aname, &n_uname);
953 ddca7f86 M. Mohan Kumar
    if (err < 0) {
954 ddca7f86 M. Mohan Kumar
        goto out_nofid;
955 ddca7f86 M. Mohan Kumar
    }
956 c572f23a Harsh Prateek Bora
    trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
957 955efc47 Anthony Liguori
958 955efc47 Anthony Liguori
    fidp = alloc_fid(s, fid);
959 955efc47 Anthony Liguori
    if (fidp == NULL) {
960 955efc47 Anthony Liguori
        err = -EINVAL;
961 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
962 9f107513 Anthony Liguori
    }
963 955efc47 Anthony Liguori
    fidp->uid = n_uname;
964 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
965 2289be19 Aneesh Kumar K.V
    if (err < 0) {
966 2289be19 Aneesh Kumar K.V
        err = -EINVAL;
967 2289be19 Aneesh Kumar K.V
        clunk_fid(s, fid);
968 2289be19 Aneesh Kumar K.V
        goto out;
969 2289be19 Aneesh Kumar K.V
    }
970 bccacf6c Aneesh Kumar K.V
    err = fid_to_qid(pdu, fidp, &qid);
971 8c158561 Aneesh Kumar K.V
    if (err < 0) {
972 955efc47 Anthony Liguori
        err = -EINVAL;
973 84dfb926 Aneesh Kumar K.V
        clunk_fid(s, fid);
974 955efc47 Anthony Liguori
        goto out;
975 955efc47 Anthony Liguori
    }
976 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Q", &qid);
977 ddca7f86 M. Mohan Kumar
    if (err < 0) {
978 ddca7f86 M. Mohan Kumar
        clunk_fid(s, fid);
979 ddca7f86 M. Mohan Kumar
        goto out;
980 ddca7f86 M. Mohan Kumar
    }
981 ddca7f86 M. Mohan Kumar
    err += offset;
982 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_attach_return(pdu->tag, pdu->id,
983 7999f7e1 Aneesh Kumar K.V
                             qid.type, qid.version, qid.path);
984 4cdc0789 Aneesh Kumar K.V
    /*
985 4cdc0789 Aneesh Kumar K.V
     * disable migration if we haven't done already.
986 4cdc0789 Aneesh Kumar K.V
     * attach could get called multiple times for the same export.
987 4cdc0789 Aneesh Kumar K.V
     */
988 4cdc0789 Aneesh Kumar K.V
    if (!s->migration_blocker) {
989 4cdc0789 Aneesh Kumar K.V
        s->root_fid = fid;
990 4cdc0789 Aneesh Kumar K.V
        error_set(&s->migration_blocker, QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
991 4cdc0789 Aneesh Kumar K.V
                  s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
992 4cdc0789 Aneesh Kumar K.V
        migrate_add_blocker(s->migration_blocker);
993 4cdc0789 Aneesh Kumar K.V
    }
994 955efc47 Anthony Liguori
out:
995 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
996 84dfb926 Aneesh Kumar K.V
out_nofid:
997 955efc47 Anthony Liguori
    complete_pdu(s, pdu, err);
998 955efc47 Anthony Liguori
    v9fs_string_free(&uname);
999 955efc47 Anthony Liguori
    v9fs_string_free(&aname);
1000 9f107513 Anthony Liguori
}
1001 9f107513 Anthony Liguori
1002 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_stat(void *opaque)
1003 9f107513 Anthony Liguori
{
1004 4da7d3fa Anthony Liguori
    int32_t fid;
1005 d8e0c29e Aneesh Kumar K.V
    V9fsStat v9stat;
1006 4da7d3fa Anthony Liguori
    ssize_t err = 0;
1007 d8e0c29e Aneesh Kumar K.V
    size_t offset = 7;
1008 d8e0c29e Aneesh Kumar K.V
    struct stat stbuf;
1009 d8e0c29e Aneesh Kumar K.V
    V9fsFidState *fidp;
1010 d8e0c29e Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1011 d8e0c29e Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1012 4da7d3fa Anthony Liguori
1013 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "d", &fid);
1014 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1015 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1016 ddca7f86 M. Mohan Kumar
    }
1017 c572f23a Harsh Prateek Bora
    trace_v9fs_stat(pdu->tag, pdu->id, fid);
1018 84dfb926 Aneesh Kumar K.V
1019 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1020 d8e0c29e Aneesh Kumar K.V
    if (fidp == NULL) {
1021 4da7d3fa Anthony Liguori
        err = -ENOENT;
1022 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1023 9f107513 Anthony Liguori
    }
1024 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1025 d8e0c29e Aneesh Kumar K.V
    if (err < 0) {
1026 d8e0c29e Aneesh Kumar K.V
        goto out;
1027 d8e0c29e Aneesh Kumar K.V
    }
1028 bccacf6c Aneesh Kumar K.V
    err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1029 d8e0c29e Aneesh Kumar K.V
    if (err < 0) {
1030 d8e0c29e Aneesh Kumar K.V
        goto out;
1031 d8e0c29e Aneesh Kumar K.V
    }
1032 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1033 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1034 ddca7f86 M. Mohan Kumar
        v9fs_stat_free(&v9stat);
1035 ddca7f86 M. Mohan Kumar
        goto out;
1036 ddca7f86 M. Mohan Kumar
    }
1037 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1038 7999f7e1 Aneesh Kumar K.V
                           v9stat.atime, v9stat.mtime, v9stat.length);
1039 ddca7f86 M. Mohan Kumar
    err += offset;
1040 d8e0c29e Aneesh Kumar K.V
    v9fs_stat_free(&v9stat);
1041 4da7d3fa Anthony Liguori
out:
1042 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1043 84dfb926 Aneesh Kumar K.V
out_nofid:
1044 d8e0c29e Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
1045 9f107513 Anthony Liguori
}
1046 9f107513 Anthony Liguori
1047 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_getattr(void *opaque)
1048 00ede4c2 Sripathi Kodi
{
1049 00ede4c2 Sripathi Kodi
    int32_t fid;
1050 8db21ce7 Aneesh Kumar K.V
    size_t offset = 7;
1051 8db21ce7 Aneesh Kumar K.V
    ssize_t retval = 0;
1052 8db21ce7 Aneesh Kumar K.V
    struct stat stbuf;
1053 00ede4c2 Sripathi Kodi
    V9fsFidState *fidp;
1054 00ede4c2 Sripathi Kodi
    uint64_t request_mask;
1055 8db21ce7 Aneesh Kumar K.V
    V9fsStatDotl v9stat_dotl;
1056 8db21ce7 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1057 8db21ce7 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1058 00ede4c2 Sripathi Kodi
1059 ddca7f86 M. Mohan Kumar
    retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1060 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
1061 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1062 ddca7f86 M. Mohan Kumar
    }
1063 c572f23a Harsh Prateek Bora
    trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1064 00ede4c2 Sripathi Kodi
1065 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1066 00ede4c2 Sripathi Kodi
    if (fidp == NULL) {
1067 8db21ce7 Aneesh Kumar K.V
        retval = -ENOENT;
1068 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1069 00ede4c2 Sripathi Kodi
    }
1070 8db21ce7 Aneesh Kumar K.V
    /*
1071 8db21ce7 Aneesh Kumar K.V
     * Currently we only support BASIC fields in stat, so there is no
1072 00ede4c2 Sripathi Kodi
     * need to look at request_mask.
1073 00ede4c2 Sripathi Kodi
     */
1074 bccacf6c Aneesh Kumar K.V
    retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1075 8db21ce7 Aneesh Kumar K.V
    if (retval < 0) {
1076 8db21ce7 Aneesh Kumar K.V
        goto out;
1077 8db21ce7 Aneesh Kumar K.V
    }
1078 8db21ce7 Aneesh Kumar K.V
    stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1079 e06a765e Harsh Prateek Bora
1080 e06a765e Harsh Prateek Bora
    /*  fill st_gen if requested and supported by underlying fs */
1081 e06a765e Harsh Prateek Bora
    if (request_mask & P9_STATS_GEN) {
1082 e06a765e Harsh Prateek Bora
        retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1083 f8b7ee38 Kirill A. Shutemov
        switch (retval) {
1084 f8b7ee38 Kirill A. Shutemov
        case 0:
1085 f8b7ee38 Kirill A. Shutemov
            /* we have valid st_gen: update result mask */
1086 f8b7ee38 Kirill A. Shutemov
            v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1087 f8b7ee38 Kirill A. Shutemov
            break;
1088 f8b7ee38 Kirill A. Shutemov
        case -EINTR:
1089 f8b7ee38 Kirill A. Shutemov
            /* request cancelled, e.g. by Tflush */
1090 e06a765e Harsh Prateek Bora
            goto out;
1091 f8b7ee38 Kirill A. Shutemov
        default:
1092 f8b7ee38 Kirill A. Shutemov
            /* failed to get st_gen: not fatal, ignore */
1093 f8b7ee38 Kirill A. Shutemov
            break;
1094 e06a765e Harsh Prateek Bora
        }
1095 e06a765e Harsh Prateek Bora
    }
1096 ddca7f86 M. Mohan Kumar
    retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1097 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
1098 ddca7f86 M. Mohan Kumar
        goto out;
1099 ddca7f86 M. Mohan Kumar
    }
1100 ddca7f86 M. Mohan Kumar
    retval += offset;
1101 c572f23a Harsh Prateek Bora
    trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1102 c572f23a Harsh Prateek Bora
                              v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1103 c572f23a Harsh Prateek Bora
                              v9stat_dotl.st_gid);
1104 7999f7e1 Aneesh Kumar K.V
out:
1105 7999f7e1 Aneesh Kumar K.V
    put_fid(pdu, fidp);
1106 7999f7e1 Aneesh Kumar K.V
out_nofid:
1107 8db21ce7 Aneesh Kumar K.V
    complete_pdu(s, pdu, retval);
1108 00ede4c2 Sripathi Kodi
}
1109 00ede4c2 Sripathi Kodi
1110 e4027caf Aneesh Kumar K.V
/* Attribute flags */
1111 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MODE       (1 << 0)
1112 e4027caf Aneesh Kumar K.V
#define P9_ATTR_UID        (1 << 1)
1113 e4027caf Aneesh Kumar K.V
#define P9_ATTR_GID        (1 << 2)
1114 e4027caf Aneesh Kumar K.V
#define P9_ATTR_SIZE       (1 << 3)
1115 e4027caf Aneesh Kumar K.V
#define P9_ATTR_ATIME      (1 << 4)
1116 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MTIME      (1 << 5)
1117 e4027caf Aneesh Kumar K.V
#define P9_ATTR_CTIME      (1 << 6)
1118 e4027caf Aneesh Kumar K.V
#define P9_ATTR_ATIME_SET  (1 << 7)
1119 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MTIME_SET  (1 << 8)
1120 e4027caf Aneesh Kumar K.V
1121 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MASK    127
1122 c79ce737 Sripathi Kodi
1123 65c05f9a Aneesh Kumar K.V
static void v9fs_setattr(void *opaque)
1124 c79ce737 Sripathi Kodi
{
1125 65c05f9a Aneesh Kumar K.V
    int err = 0;
1126 65c05f9a Aneesh Kumar K.V
    int32_t fid;
1127 65c05f9a Aneesh Kumar K.V
    V9fsFidState *fidp;
1128 65c05f9a Aneesh Kumar K.V
    size_t offset = 7;
1129 65c05f9a Aneesh Kumar K.V
    V9fsIattr v9iattr;
1130 65c05f9a Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1131 65c05f9a Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1132 c79ce737 Sripathi Kodi
1133 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1134 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1135 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1136 ddca7f86 M. Mohan Kumar
    }
1137 c79ce737 Sripathi Kodi
1138 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1139 65c05f9a Aneesh Kumar K.V
    if (fidp == NULL) {
1140 65c05f9a Aneesh Kumar K.V
        err = -EINVAL;
1141 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1142 c79ce737 Sripathi Kodi
    }
1143 e4027caf Aneesh Kumar K.V
    if (v9iattr.valid & P9_ATTR_MODE) {
1144 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1145 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1146 65c05f9a Aneesh Kumar K.V
            goto out;
1147 c79ce737 Sripathi Kodi
        }
1148 c79ce737 Sripathi Kodi
    }
1149 e4027caf Aneesh Kumar K.V
    if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1150 c79ce737 Sripathi Kodi
        struct timespec times[2];
1151 e4027caf Aneesh Kumar K.V
        if (v9iattr.valid & P9_ATTR_ATIME) {
1152 e4027caf Aneesh Kumar K.V
            if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1153 65c05f9a Aneesh Kumar K.V
                times[0].tv_sec = v9iattr.atime_sec;
1154 65c05f9a Aneesh Kumar K.V
                times[0].tv_nsec = v9iattr.atime_nsec;
1155 c79ce737 Sripathi Kodi
            } else {
1156 c79ce737 Sripathi Kodi
                times[0].tv_nsec = UTIME_NOW;
1157 c79ce737 Sripathi Kodi
            }
1158 c79ce737 Sripathi Kodi
        } else {
1159 c79ce737 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
1160 c79ce737 Sripathi Kodi
        }
1161 e4027caf Aneesh Kumar K.V
        if (v9iattr.valid & P9_ATTR_MTIME) {
1162 e4027caf Aneesh Kumar K.V
            if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1163 65c05f9a Aneesh Kumar K.V
                times[1].tv_sec = v9iattr.mtime_sec;
1164 65c05f9a Aneesh Kumar K.V
                times[1].tv_nsec = v9iattr.mtime_nsec;
1165 c79ce737 Sripathi Kodi
            } else {
1166 c79ce737 Sripathi Kodi
                times[1].tv_nsec = UTIME_NOW;
1167 c79ce737 Sripathi Kodi
            }
1168 c79ce737 Sripathi Kodi
        } else {
1169 c79ce737 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
1170 c79ce737 Sripathi Kodi
        }
1171 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_utimensat(pdu, &fidp->path, times);
1172 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1173 65c05f9a Aneesh Kumar K.V
            goto out;
1174 65c05f9a Aneesh Kumar K.V
        }
1175 c79ce737 Sripathi Kodi
    }
1176 65c05f9a Aneesh Kumar K.V
    /*
1177 65c05f9a Aneesh Kumar K.V
     * If the only valid entry in iattr is ctime we can call
1178 65c05f9a Aneesh Kumar K.V
     * chown(-1,-1) to update the ctime of the file
1179 65c05f9a Aneesh Kumar K.V
     */
1180 e4027caf Aneesh Kumar K.V
    if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1181 e4027caf Aneesh Kumar K.V
        ((v9iattr.valid & P9_ATTR_CTIME)
1182 e4027caf Aneesh Kumar K.V
         && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1183 e4027caf Aneesh Kumar K.V
        if (!(v9iattr.valid & P9_ATTR_UID)) {
1184 65c05f9a Aneesh Kumar K.V
            v9iattr.uid = -1;
1185 65c05f9a Aneesh Kumar K.V
        }
1186 e4027caf Aneesh Kumar K.V
        if (!(v9iattr.valid & P9_ATTR_GID)) {
1187 65c05f9a Aneesh Kumar K.V
            v9iattr.gid = -1;
1188 65c05f9a Aneesh Kumar K.V
        }
1189 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1190 65c05f9a Aneesh Kumar K.V
                            v9iattr.gid);
1191 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1192 65c05f9a Aneesh Kumar K.V
            goto out;
1193 65c05f9a Aneesh Kumar K.V
        }
1194 c79ce737 Sripathi Kodi
    }
1195 e4027caf Aneesh Kumar K.V
    if (v9iattr.valid & (P9_ATTR_SIZE)) {
1196 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1197 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1198 65c05f9a Aneesh Kumar K.V
            goto out;
1199 65c05f9a Aneesh Kumar K.V
        }
1200 c79ce737 Sripathi Kodi
    }
1201 65c05f9a Aneesh Kumar K.V
    err = offset;
1202 c79ce737 Sripathi Kodi
out:
1203 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1204 84dfb926 Aneesh Kumar K.V
out_nofid:
1205 65c05f9a Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
1206 c79ce737 Sripathi Kodi
}
1207 c79ce737 Sripathi Kodi
1208 3cc19c0c Aneesh Kumar K.V
static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1209 ff5e54c9 Anthony Liguori
{
1210 ff5e54c9 Anthony Liguori
    int i;
1211 ddca7f86 M. Mohan Kumar
    ssize_t err;
1212 3cc19c0c Aneesh Kumar K.V
    size_t offset = 7;
1213 ddca7f86 M. Mohan Kumar
1214 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "w", nwnames);
1215 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1216 ddca7f86 M. Mohan Kumar
        return err;
1217 ddca7f86 M. Mohan Kumar
    }
1218 ddca7f86 M. Mohan Kumar
    offset += err;
1219 3cc19c0c Aneesh Kumar K.V
    for (i = 0; i < nwnames; i++) {
1220 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1221 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1222 ddca7f86 M. Mohan Kumar
            return err;
1223 ddca7f86 M. Mohan Kumar
        }
1224 ddca7f86 M. Mohan Kumar
        offset += err;
1225 ff5e54c9 Anthony Liguori
    }
1226 3cc19c0c Aneesh Kumar K.V
    return offset;
1227 ff5e54c9 Anthony Liguori
}
1228 ff5e54c9 Anthony Liguori
1229 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_walk(void *opaque)
1230 9f107513 Anthony Liguori
{
1231 3cc19c0c Aneesh Kumar K.V
    int name_idx;
1232 3cc19c0c Aneesh Kumar K.V
    V9fsQID *qids = NULL;
1233 3cc19c0c Aneesh Kumar K.V
    int i, err = 0;
1234 2289be19 Aneesh Kumar K.V
    V9fsPath dpath, path;
1235 3cc19c0c Aneesh Kumar K.V
    uint16_t nwnames;
1236 3cc19c0c Aneesh Kumar K.V
    struct stat stbuf;
1237 3cc19c0c Aneesh Kumar K.V
    size_t offset = 7;
1238 3cc19c0c Aneesh Kumar K.V
    int32_t fid, newfid;
1239 3cc19c0c Aneesh Kumar K.V
    V9fsString *wnames = NULL;
1240 3cc19c0c Aneesh Kumar K.V
    V9fsFidState *fidp;
1241 3a93113a Dong Xu Wang
    V9fsFidState *newfidp = NULL;
1242 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
1243 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
1244 ff5e54c9 Anthony Liguori
1245 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1246 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1247 ddca7f86 M. Mohan Kumar
        complete_pdu(s, pdu, err);
1248 ddca7f86 M. Mohan Kumar
        return ;
1249 ddca7f86 M. Mohan Kumar
    }
1250 ddca7f86 M. Mohan Kumar
    offset += err;
1251 ff5e54c9 Anthony Liguori
1252 c572f23a Harsh Prateek Bora
    trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1253 c572f23a Harsh Prateek Bora
1254 3cc19c0c Aneesh Kumar K.V
    if (nwnames && nwnames <= P9_MAXWELEM) {
1255 3cc19c0c Aneesh Kumar K.V
        wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1256 3cc19c0c Aneesh Kumar K.V
        qids   = g_malloc0(sizeof(qids[0]) * nwnames);
1257 3cc19c0c Aneesh Kumar K.V
        for (i = 0; i < nwnames; i++) {
1258 ddca7f86 M. Mohan Kumar
            err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1259 ddca7f86 M. Mohan Kumar
            if (err < 0) {
1260 ddca7f86 M. Mohan Kumar
                goto out_nofid;
1261 ddca7f86 M. Mohan Kumar
            }
1262 ddca7f86 M. Mohan Kumar
            offset += err;
1263 ff5e54c9 Anthony Liguori
        }
1264 3cc19c0c Aneesh Kumar K.V
    } else if (nwnames > P9_MAXWELEM) {
1265 4f8dee2d Harsh Prateek Bora
        err = -EINVAL;
1266 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1267 ff5e54c9 Anthony Liguori
    }
1268 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1269 3cc19c0c Aneesh Kumar K.V
    if (fidp == NULL) {
1270 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1271 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1272 ff5e54c9 Anthony Liguori
    }
1273 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&dpath);
1274 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&path);
1275 2289be19 Aneesh Kumar K.V
    /*
1276 2289be19 Aneesh Kumar K.V
     * Both dpath and path initially poin to fidp.
1277 2289be19 Aneesh Kumar K.V
     * Needed to handle request with nwnames == 0
1278 2289be19 Aneesh Kumar K.V
     */
1279 2289be19 Aneesh Kumar K.V
    v9fs_path_copy(&dpath, &fidp->path);
1280 2289be19 Aneesh Kumar K.V
    v9fs_path_copy(&path, &fidp->path);
1281 2289be19 Aneesh Kumar K.V
    for (name_idx = 0; name_idx < nwnames; name_idx++) {
1282 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);
1283 2289be19 Aneesh Kumar K.V
        if (err < 0) {
1284 2289be19 Aneesh Kumar K.V
            goto out;
1285 2289be19 Aneesh Kumar K.V
        }
1286 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &path, &stbuf);
1287 2289be19 Aneesh Kumar K.V
        if (err < 0) {
1288 2289be19 Aneesh Kumar K.V
            goto out;
1289 2289be19 Aneesh Kumar K.V
        }
1290 2289be19 Aneesh Kumar K.V
        stat_to_qid(&stbuf, &qids[name_idx]);
1291 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&dpath, &path);
1292 2289be19 Aneesh Kumar K.V
    }
1293 ff5e54c9 Anthony Liguori
    if (fid == newfid) {
1294 3cc19c0c Aneesh Kumar K.V
        BUG_ON(fidp->fid_type != P9_FID_NONE);
1295 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
1296 ff5e54c9 Anthony Liguori
    } else {
1297 3cc19c0c Aneesh Kumar K.V
        newfidp = alloc_fid(s, newfid);
1298 3cc19c0c Aneesh Kumar K.V
        if (newfidp == NULL) {
1299 ff5e54c9 Anthony Liguori
            err = -EINVAL;
1300 ff5e54c9 Anthony Liguori
            goto out;
1301 ff5e54c9 Anthony Liguori
        }
1302 3cc19c0c Aneesh Kumar K.V
        newfidp->uid = fidp->uid;
1303 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&newfidp->path, &path);
1304 9f107513 Anthony Liguori
    }
1305 3cc19c0c Aneesh Kumar K.V
    err = v9fs_walk_marshal(pdu, nwnames, qids);
1306 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1307 ff5e54c9 Anthony Liguori
out:
1308 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1309 84dfb926 Aneesh Kumar K.V
    if (newfidp) {
1310 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, newfidp);
1311 84dfb926 Aneesh Kumar K.V
    }
1312 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&dpath);
1313 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&path);
1314 84dfb926 Aneesh Kumar K.V
out_nofid:
1315 3cc19c0c Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
1316 3cc19c0c Aneesh Kumar K.V
    if (nwnames && nwnames <= P9_MAXWELEM) {
1317 3cc19c0c Aneesh Kumar K.V
        for (name_idx = 0; name_idx < nwnames; name_idx++) {
1318 3cc19c0c Aneesh Kumar K.V
            v9fs_string_free(&wnames[name_idx]);
1319 3cc19c0c Aneesh Kumar K.V
        }
1320 3cc19c0c Aneesh Kumar K.V
        g_free(wnames);
1321 3cc19c0c Aneesh Kumar K.V
        g_free(qids);
1322 3cc19c0c Aneesh Kumar K.V
    }
1323 9f107513 Anthony Liguori
}
1324 9f107513 Anthony Liguori
1325 bccacf6c Aneesh Kumar K.V
static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
1326 5e94c103 M. Mohan Kumar
{
1327 5e94c103 M. Mohan Kumar
    struct statfs stbuf;
1328 5e94c103 M. Mohan Kumar
    int32_t iounit = 0;
1329 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1330 5e94c103 M. Mohan Kumar
1331 5e94c103 M. Mohan Kumar
    /*
1332 5e94c103 M. Mohan Kumar
     * iounit should be multiples of f_bsize (host filesystem block size
1333 5e94c103 M. Mohan Kumar
     * and as well as less than (client msize - P9_IOHDRSZ))
1334 5e94c103 M. Mohan Kumar
     */
1335 bccacf6c Aneesh Kumar K.V
    if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1336 5e94c103 M. Mohan Kumar
        iounit = stbuf.f_bsize;
1337 5e94c103 M. Mohan Kumar
        iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1338 5e94c103 M. Mohan Kumar
    }
1339 5e94c103 M. Mohan Kumar
    if (!iounit) {
1340 5e94c103 M. Mohan Kumar
        iounit = s->msize - P9_IOHDRSZ;
1341 5e94c103 M. Mohan Kumar
    }
1342 5e94c103 M. Mohan Kumar
    return iounit;
1343 5e94c103 M. Mohan Kumar
}
1344 5e94c103 M. Mohan Kumar
1345 857bc158 Aneesh Kumar K.V
static void v9fs_open(void *opaque)
1346 5e94c103 M. Mohan Kumar
{
1347 857bc158 Aneesh Kumar K.V
    int flags;
1348 857bc158 Aneesh Kumar K.V
    int32_t fid;
1349 857bc158 Aneesh Kumar K.V
    int32_t mode;
1350 857bc158 Aneesh Kumar K.V
    V9fsQID qid;
1351 7999f7e1 Aneesh Kumar K.V
    int iounit = 0;
1352 857bc158 Aneesh Kumar K.V
    ssize_t err = 0;
1353 857bc158 Aneesh Kumar K.V
    size_t offset = 7;
1354 857bc158 Aneesh Kumar K.V
    struct stat stbuf;
1355 857bc158 Aneesh Kumar K.V
    V9fsFidState *fidp;
1356 857bc158 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1357 857bc158 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1358 5e94c103 M. Mohan Kumar
1359 857bc158 Aneesh Kumar K.V
    if (s->proto_version == V9FS_PROTO_2000L) {
1360 ddca7f86 M. Mohan Kumar
        err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1361 857bc158 Aneesh Kumar K.V
    } else {
1362 67d6fa53 Benjamin Herrenschmidt
        uint8_t modebyte;
1363 67d6fa53 Benjamin Herrenschmidt
        err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1364 67d6fa53 Benjamin Herrenschmidt
        mode = modebyte;
1365 ddca7f86 M. Mohan Kumar
    }
1366 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1367 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1368 857bc158 Aneesh Kumar K.V
    }
1369 c572f23a Harsh Prateek Bora
    trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1370 c572f23a Harsh Prateek Bora
1371 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1372 857bc158 Aneesh Kumar K.V
    if (fidp == NULL) {
1373 857bc158 Aneesh Kumar K.V
        err = -ENOENT;
1374 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1375 a6568fe2 Anthony Liguori
    }
1376 857bc158 Aneesh Kumar K.V
    BUG_ON(fidp->fid_type != P9_FID_NONE);
1377 a6568fe2 Anthony Liguori
1378 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1379 857bc158 Aneesh Kumar K.V
    if (err < 0) {
1380 a6568fe2 Anthony Liguori
        goto out;
1381 a6568fe2 Anthony Liguori
    }
1382 857bc158 Aneesh Kumar K.V
    stat_to_qid(&stbuf, &qid);
1383 857bc158 Aneesh Kumar K.V
    if (S_ISDIR(stbuf.st_mode)) {
1384 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_opendir(pdu, fidp);
1385 857bc158 Aneesh Kumar K.V
        if (err < 0) {
1386 857bc158 Aneesh Kumar K.V
            goto out;
1387 857bc158 Aneesh Kumar K.V
        }
1388 857bc158 Aneesh Kumar K.V
        fidp->fid_type = P9_FID_DIR;
1389 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1390 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1391 ddca7f86 M. Mohan Kumar
            goto out;
1392 ddca7f86 M. Mohan Kumar
        }
1393 ddca7f86 M. Mohan Kumar
        err += offset;
1394 a6568fe2 Anthony Liguori
    } else {
1395 771e9d4c M. Mohan Kumar
        if (s->proto_version == V9FS_PROTO_2000L) {
1396 d3ab98e6 Aneesh Kumar K.V
            flags = get_dotl_openflags(s, mode);
1397 771e9d4c M. Mohan Kumar
        } else {
1398 857bc158 Aneesh Kumar K.V
            flags = omode_to_uflags(mode);
1399 771e9d4c M. Mohan Kumar
        }
1400 2c74c2cb M. Mohan Kumar
        if (is_ro_export(&s->ctx)) {
1401 2c74c2cb M. Mohan Kumar
            if (mode & O_WRONLY || mode & O_RDWR ||
1402 2c74c2cb M. Mohan Kumar
                mode & O_APPEND || mode & O_TRUNC) {
1403 2c74c2cb M. Mohan Kumar
                err = -EROFS;
1404 2c74c2cb M. Mohan Kumar
                goto out;
1405 2c74c2cb M. Mohan Kumar
            }
1406 2c74c2cb M. Mohan Kumar
        }
1407 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_open(pdu, fidp, flags);
1408 857bc158 Aneesh Kumar K.V
        if (err < 0) {
1409 857bc158 Aneesh Kumar K.V
            goto out;
1410 857bc158 Aneesh Kumar K.V
        }
1411 857bc158 Aneesh Kumar K.V
        fidp->fid_type = P9_FID_FILE;
1412 7a462745 Aneesh Kumar K.V
        fidp->open_flags = flags;
1413 7a462745 Aneesh Kumar K.V
        if (flags & O_EXCL) {
1414 7a462745 Aneesh Kumar K.V
            /*
1415 7a462745 Aneesh Kumar K.V
             * We let the host file system do O_EXCL check
1416 7a462745 Aneesh Kumar K.V
             * We should not reclaim such fd
1417 7a462745 Aneesh Kumar K.V
             */
1418 7a462745 Aneesh Kumar K.V
            fidp->flags |= FID_NON_RECLAIMABLE;
1419 7a462745 Aneesh Kumar K.V
        }
1420 bccacf6c Aneesh Kumar K.V
        iounit = get_iounit(pdu, &fidp->path);
1421 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1422 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1423 ddca7f86 M. Mohan Kumar
            goto out;
1424 ddca7f86 M. Mohan Kumar
        }
1425 ddca7f86 M. Mohan Kumar
        err += offset;
1426 a6568fe2 Anthony Liguori
    }
1427 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_open_return(pdu->tag, pdu->id,
1428 7999f7e1 Aneesh Kumar K.V
                           qid.type, qid.version, qid.path, iounit);
1429 a6568fe2 Anthony Liguori
out:
1430 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1431 84dfb926 Aneesh Kumar K.V
out_nofid:
1432 a6568fe2 Anthony Liguori
    complete_pdu(s, pdu, err);
1433 a6568fe2 Anthony Liguori
}
1434 a6568fe2 Anthony Liguori
1435 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_lcreate(void *opaque)
1436 c1568af5 Venkateswararao Jujjuri (JV)
{
1437 c1568af5 Venkateswararao Jujjuri (JV)
    int32_t dfid, flags, mode;
1438 c1568af5 Venkateswararao Jujjuri (JV)
    gid_t gid;
1439 c1568af5 Venkateswararao Jujjuri (JV)
    ssize_t err = 0;
1440 36f8981f Venkateswararao Jujjuri
    ssize_t offset = 7;
1441 36f8981f Venkateswararao Jujjuri
    V9fsString name;
1442 36f8981f Venkateswararao Jujjuri
    V9fsFidState *fidp;
1443 36f8981f Venkateswararao Jujjuri
    struct stat stbuf;
1444 36f8981f Venkateswararao Jujjuri
    V9fsQID qid;
1445 36f8981f Venkateswararao Jujjuri
    int32_t iounit;
1446 36f8981f Venkateswararao Jujjuri
    V9fsPDU *pdu = opaque;
1447 c1568af5 Venkateswararao Jujjuri (JV)
1448 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
1449 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1450 ddca7f86 M. Mohan Kumar
                        &name, &flags, &mode, &gid);
1451 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1452 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1453 ddca7f86 M. Mohan Kumar
    }
1454 c572f23a Harsh Prateek Bora
    trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1455 c1568af5 Venkateswararao Jujjuri (JV)
1456 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, dfid);
1457 36f8981f Venkateswararao Jujjuri
    if (fidp == NULL) {
1458 c1568af5 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
1459 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1460 c1568af5 Venkateswararao Jujjuri (JV)
    }
1461 c1568af5 Venkateswararao Jujjuri (JV)
1462 d3ab98e6 Aneesh Kumar K.V
    flags = get_dotl_openflags(pdu->s, flags);
1463 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_open2(pdu, fidp, &name, gid,
1464 02cb7f3a Aneesh Kumar K.V
                        flags | O_CREAT, mode, &stbuf);
1465 36f8981f Venkateswararao Jujjuri
    if (err < 0) {
1466 36f8981f Venkateswararao Jujjuri
        goto out;
1467 36f8981f Venkateswararao Jujjuri
    }
1468 36f8981f Venkateswararao Jujjuri
    fidp->fid_type = P9_FID_FILE;
1469 7a462745 Aneesh Kumar K.V
    fidp->open_flags = flags;
1470 7a462745 Aneesh Kumar K.V
    if (flags & O_EXCL) {
1471 7a462745 Aneesh Kumar K.V
        /*
1472 7a462745 Aneesh Kumar K.V
         * We let the host file system do O_EXCL check
1473 7a462745 Aneesh Kumar K.V
         * We should not reclaim such fd
1474 7a462745 Aneesh Kumar K.V
         */
1475 7a462745 Aneesh Kumar K.V
        fidp->flags |= FID_NON_RECLAIMABLE;
1476 7a462745 Aneesh Kumar K.V
    }
1477 bccacf6c Aneesh Kumar K.V
    iounit =  get_iounit(pdu, &fidp->path);
1478 36f8981f Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
1479 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1480 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1481 ddca7f86 M. Mohan Kumar
        goto out;
1482 ddca7f86 M. Mohan Kumar
    }
1483 ddca7f86 M. Mohan Kumar
    err += offset;
1484 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1485 7999f7e1 Aneesh Kumar K.V
                              qid.type, qid.version, qid.path, iounit);
1486 c1568af5 Venkateswararao Jujjuri (JV)
out:
1487 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1488 84dfb926 Aneesh Kumar K.V
out_nofid:
1489 36f8981f Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
1490 36f8981f Venkateswararao Jujjuri
    v9fs_string_free(&name);
1491 c1568af5 Venkateswararao Jujjuri (JV)
}
1492 c1568af5 Venkateswararao Jujjuri (JV)
1493 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_fsync(void *opaque)
1494 b41e95d3 Venkateswararao Jujjuri (JV)
{
1495 4e9ad444 Aneesh Kumar K.V
    int err;
1496 b41e95d3 Venkateswararao Jujjuri (JV)
    int32_t fid;
1497 4e9ad444 Aneesh Kumar K.V
    int datasync;
1498 b41e95d3 Venkateswararao Jujjuri (JV)
    size_t offset = 7;
1499 b41e95d3 Venkateswararao Jujjuri (JV)
    V9fsFidState *fidp;
1500 4e9ad444 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1501 4e9ad444 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1502 b41e95d3 Venkateswararao Jujjuri (JV)
1503 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1504 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1505 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1506 ddca7f86 M. Mohan Kumar
    }
1507 c572f23a Harsh Prateek Bora
    trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1508 c572f23a Harsh Prateek Bora
1509 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1510 b41e95d3 Venkateswararao Jujjuri (JV)
    if (fidp == NULL) {
1511 b41e95d3 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
1512 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1513 b41e95d3 Venkateswararao Jujjuri (JV)
    }
1514 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_fsync(pdu, fidp, datasync);
1515 4e9ad444 Aneesh Kumar K.V
    if (!err) {
1516 4e9ad444 Aneesh Kumar K.V
        err = offset;
1517 4e9ad444 Aneesh Kumar K.V
    }
1518 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1519 84dfb926 Aneesh Kumar K.V
out_nofid:
1520 4e9ad444 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
1521 b41e95d3 Venkateswararao Jujjuri (JV)
}
1522 b41e95d3 Venkateswararao Jujjuri (JV)
1523 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_clunk(void *opaque)
1524 a6568fe2 Anthony Liguori
{
1525 c540ee51 Aneesh Kumar K.V
    int err;
1526 bbd5697b Anthony Liguori
    int32_t fid;
1527 bbd5697b Anthony Liguori
    size_t offset = 7;
1528 84dfb926 Aneesh Kumar K.V
    V9fsFidState *fidp;
1529 c540ee51 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1530 c540ee51 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1531 bbd5697b Anthony Liguori
1532 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "d", &fid);
1533 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1534 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1535 ddca7f86 M. Mohan Kumar
    }
1536 c572f23a Harsh Prateek Bora
    trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1537 84dfb926 Aneesh Kumar K.V
1538 ce421a19 Aneesh Kumar K.V
    fidp = clunk_fid(s, fid);
1539 84dfb926 Aneesh Kumar K.V
    if (fidp == NULL) {
1540 84dfb926 Aneesh Kumar K.V
        err = -ENOENT;
1541 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1542 84dfb926 Aneesh Kumar K.V
    }
1543 ce421a19 Aneesh Kumar K.V
    /*
1544 ce421a19 Aneesh Kumar K.V
     * Bump the ref so that put_fid will
1545 ce421a19 Aneesh Kumar K.V
     * free the fid.
1546 ce421a19 Aneesh Kumar K.V
     */
1547 ce421a19 Aneesh Kumar K.V
    fidp->ref++;
1548 a911a182 Aneesh Kumar K.V
    err = put_fid(pdu, fidp);
1549 a911a182 Aneesh Kumar K.V
    if (!err) {
1550 a911a182 Aneesh Kumar K.V
        err = offset;
1551 a911a182 Aneesh Kumar K.V
    }
1552 84dfb926 Aneesh Kumar K.V
out_nofid:
1553 bbd5697b Anthony Liguori
    complete_pdu(s, pdu, err);
1554 9f107513 Anthony Liguori
}
1555 9f107513 Anthony Liguori
1556 2f008a8c Aneesh Kumar K.V
static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1557 2f008a8c Aneesh Kumar K.V
                           uint64_t off, uint32_t max_count)
1558 a9231555 Anthony Liguori
{
1559 ddca7f86 M. Mohan Kumar
    ssize_t err;
1560 d208a0e0 Aneesh Kumar K.V
    size_t offset = 7;
1561 d208a0e0 Aneesh Kumar K.V
    int read_count;
1562 d208a0e0 Aneesh Kumar K.V
    int64_t xattr_len;
1563 a9231555 Anthony Liguori
1564 d208a0e0 Aneesh Kumar K.V
    xattr_len = fidp->fs.xattr.len;
1565 d208a0e0 Aneesh Kumar K.V
    read_count = xattr_len - off;
1566 d208a0e0 Aneesh Kumar K.V
    if (read_count > max_count) {
1567 d208a0e0 Aneesh Kumar K.V
        read_count = max_count;
1568 d208a0e0 Aneesh Kumar K.V
    } else if (read_count < 0) {
1569 d208a0e0 Aneesh Kumar K.V
        /*
1570 d208a0e0 Aneesh Kumar K.V
         * read beyond XATTR value
1571 d208a0e0 Aneesh Kumar K.V
         */
1572 d208a0e0 Aneesh Kumar K.V
        read_count = 0;
1573 a9231555 Anthony Liguori
    }
1574 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "d", read_count);
1575 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1576 ddca7f86 M. Mohan Kumar
        return err;
1577 ddca7f86 M. Mohan Kumar
    }
1578 ddca7f86 M. Mohan Kumar
    offset += err;
1579 ddca7f86 M. Mohan Kumar
    err = v9fs_pack(pdu->elem.in_sg, pdu->elem.in_num, offset,
1580 ddca7f86 M. Mohan Kumar
                    ((char *)fidp->fs.xattr.value) + off,
1581 ddca7f86 M. Mohan Kumar
                    read_count);
1582 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1583 ddca7f86 M. Mohan Kumar
        return err;
1584 ddca7f86 M. Mohan Kumar
    }
1585 ddca7f86 M. Mohan Kumar
    offset += err;
1586 d208a0e0 Aneesh Kumar K.V
    return offset;
1587 a9231555 Anthony Liguori
}
1588 a9231555 Anthony Liguori
1589 bccacf6c Aneesh Kumar K.V
static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1590 2f008a8c Aneesh Kumar K.V
                                     V9fsFidState *fidp, uint32_t max_count)
1591 a9231555 Anthony Liguori
{
1592 2289be19 Aneesh Kumar K.V
    V9fsPath path;
1593 d208a0e0 Aneesh Kumar K.V
    V9fsStat v9stat;
1594 d208a0e0 Aneesh Kumar K.V
    int len, err = 0;
1595 d208a0e0 Aneesh Kumar K.V
    int32_t count = 0;
1596 d208a0e0 Aneesh Kumar K.V
    struct stat stbuf;
1597 d208a0e0 Aneesh Kumar K.V
    off_t saved_dir_pos;
1598 5f524c1e Harsh Prateek Bora
    struct dirent *dent, *result;
1599 a9231555 Anthony Liguori
1600 d208a0e0 Aneesh Kumar K.V
    /* save the directory position */
1601 bccacf6c Aneesh Kumar K.V
    saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1602 d208a0e0 Aneesh Kumar K.V
    if (saved_dir_pos < 0) {
1603 d208a0e0 Aneesh Kumar K.V
        return saved_dir_pos;
1604 a9231555 Anthony Liguori
    }
1605 5f524c1e Harsh Prateek Bora
1606 5f524c1e Harsh Prateek Bora
    dent = g_malloc(sizeof(struct dirent));
1607 5f524c1e Harsh Prateek Bora
1608 d208a0e0 Aneesh Kumar K.V
    while (1) {
1609 2289be19 Aneesh Kumar K.V
        v9fs_path_init(&path);
1610 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1611 5f524c1e Harsh Prateek Bora
        if (err || !result) {
1612 d208a0e0 Aneesh Kumar K.V
            break;
1613 d208a0e0 Aneesh Kumar K.V
        }
1614 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1615 d208a0e0 Aneesh Kumar K.V
        if (err < 0) {
1616 d208a0e0 Aneesh Kumar K.V
            goto out;
1617 d208a0e0 Aneesh Kumar K.V
        }
1618 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &path, &stbuf);
1619 2289be19 Aneesh Kumar K.V
        if (err < 0) {
1620 2289be19 Aneesh Kumar K.V
            goto out;
1621 2289be19 Aneesh Kumar K.V
        }
1622 bccacf6c Aneesh Kumar K.V
        err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1623 d208a0e0 Aneesh Kumar K.V
        if (err < 0) {
1624 d208a0e0 Aneesh Kumar K.V
            goto out;
1625 a9231555 Anthony Liguori
        }
1626 d208a0e0 Aneesh Kumar K.V
        /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1627 d208a0e0 Aneesh Kumar K.V
        len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1628 d208a0e0 Aneesh Kumar K.V
        if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1629 d208a0e0 Aneesh Kumar K.V
            /* Ran out of buffer. Set dir back to old position and return */
1630 bccacf6c Aneesh Kumar K.V
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1631 d208a0e0 Aneesh Kumar K.V
            v9fs_stat_free(&v9stat);
1632 2289be19 Aneesh Kumar K.V
            v9fs_path_free(&path);
1633 5f524c1e Harsh Prateek Bora
            g_free(dent);
1634 d208a0e0 Aneesh Kumar K.V
            return count;
1635 d208a0e0 Aneesh Kumar K.V
        }
1636 d208a0e0 Aneesh Kumar K.V
        count += len;
1637 d208a0e0 Aneesh Kumar K.V
        v9fs_stat_free(&v9stat);
1638 2289be19 Aneesh Kumar K.V
        v9fs_path_free(&path);
1639 d208a0e0 Aneesh Kumar K.V
        saved_dir_pos = dent->d_off;
1640 a9231555 Anthony Liguori
    }
1641 a9231555 Anthony Liguori
out:
1642 5f524c1e Harsh Prateek Bora
    g_free(dent);
1643 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&path);
1644 d208a0e0 Aneesh Kumar K.V
    if (err < 0) {
1645 d208a0e0 Aneesh Kumar K.V
        return err;
1646 fa32ef88 Aneesh Kumar K.V
    }
1647 d208a0e0 Aneesh Kumar K.V
    return count;
1648 fa32ef88 Aneesh Kumar K.V
}
1649 fa32ef88 Aneesh Kumar K.V
1650 302a0d3e Stefan Hajnoczi
/*
1651 302a0d3e Stefan Hajnoczi
 * Create a QEMUIOVector for a sub-region of PDU iovecs
1652 302a0d3e Stefan Hajnoczi
 *
1653 302a0d3e Stefan Hajnoczi
 * @qiov:       uninitialized QEMUIOVector
1654 302a0d3e Stefan Hajnoczi
 * @skip:       number of bytes to skip from beginning of PDU
1655 302a0d3e Stefan Hajnoczi
 * @size:       number of bytes to include
1656 302a0d3e Stefan Hajnoczi
 * @is_write:   true - write, false - read
1657 302a0d3e Stefan Hajnoczi
 *
1658 302a0d3e Stefan Hajnoczi
 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1659 302a0d3e Stefan Hajnoczi
 * with qemu_iovec_destroy().
1660 302a0d3e Stefan Hajnoczi
 */
1661 302a0d3e Stefan Hajnoczi
static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1662 1b093c48 Michael Tokarev
                                    size_t skip, size_t size,
1663 302a0d3e Stefan Hajnoczi
                                    bool is_write)
1664 302a0d3e Stefan Hajnoczi
{
1665 302a0d3e Stefan Hajnoczi
    QEMUIOVector elem;
1666 302a0d3e Stefan Hajnoczi
    struct iovec *iov;
1667 302a0d3e Stefan Hajnoczi
    unsigned int niov;
1668 302a0d3e Stefan Hajnoczi
1669 302a0d3e Stefan Hajnoczi
    if (is_write) {
1670 302a0d3e Stefan Hajnoczi
        iov = pdu->elem.out_sg;
1671 302a0d3e Stefan Hajnoczi
        niov = pdu->elem.out_num;
1672 302a0d3e Stefan Hajnoczi
    } else {
1673 302a0d3e Stefan Hajnoczi
        iov = pdu->elem.in_sg;
1674 302a0d3e Stefan Hajnoczi
        niov = pdu->elem.in_num;
1675 302a0d3e Stefan Hajnoczi
    }
1676 302a0d3e Stefan Hajnoczi
1677 302a0d3e Stefan Hajnoczi
    qemu_iovec_init_external(&elem, iov, niov);
1678 302a0d3e Stefan Hajnoczi
    qemu_iovec_init(qiov, niov);
1679 1b093c48 Michael Tokarev
    qemu_iovec_concat(qiov, &elem, skip, size);
1680 302a0d3e Stefan Hajnoczi
}
1681 302a0d3e Stefan Hajnoczi
1682 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_read(void *opaque)
1683 9f107513 Anthony Liguori
{
1684 a9231555 Anthony Liguori
    int32_t fid;
1685 2f008a8c Aneesh Kumar K.V
    uint64_t off;
1686 a9231555 Anthony Liguori
    ssize_t err = 0;
1687 d208a0e0 Aneesh Kumar K.V
    int32_t count = 0;
1688 d208a0e0 Aneesh Kumar K.V
    size_t offset = 7;
1689 2f008a8c Aneesh Kumar K.V
    uint32_t max_count;
1690 d208a0e0 Aneesh Kumar K.V
    V9fsFidState *fidp;
1691 d208a0e0 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1692 d208a0e0 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1693 a9231555 Anthony Liguori
1694 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1695 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1696 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1697 ddca7f86 M. Mohan Kumar
    }
1698 c572f23a Harsh Prateek Bora
    trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1699 84dfb926 Aneesh Kumar K.V
1700 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1701 d208a0e0 Aneesh Kumar K.V
    if (fidp == NULL) {
1702 a9231555 Anthony Liguori
        err = -EINVAL;
1703 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1704 a9231555 Anthony Liguori
    }
1705 d208a0e0 Aneesh Kumar K.V
    if (fidp->fid_type == P9_FID_DIR) {
1706 a9231555 Anthony Liguori
1707 d208a0e0 Aneesh Kumar K.V
        if (off == 0) {
1708 bccacf6c Aneesh Kumar K.V
            v9fs_co_rewinddir(pdu, fidp);
1709 a9231555 Anthony Liguori
        }
1710 bccacf6c Aneesh Kumar K.V
        count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1711 d208a0e0 Aneesh Kumar K.V
        if (count < 0) {
1712 d208a0e0 Aneesh Kumar K.V
            err = count;
1713 d208a0e0 Aneesh Kumar K.V
            goto out;
1714 56d15a53 Sanchit Garg
        }
1715 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "d", count);
1716 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1717 ddca7f86 M. Mohan Kumar
            goto out;
1718 ddca7f86 M. Mohan Kumar
        }
1719 ddca7f86 M. Mohan Kumar
        err += offset + count;
1720 d208a0e0 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_FILE) {
1721 302a0d3e Stefan Hajnoczi
        QEMUIOVector qiov_full;
1722 302a0d3e Stefan Hajnoczi
        QEMUIOVector qiov;
1723 d208a0e0 Aneesh Kumar K.V
        int32_t len;
1724 d208a0e0 Aneesh Kumar K.V
1725 302a0d3e Stefan Hajnoczi
        v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1726 302a0d3e Stefan Hajnoczi
        qemu_iovec_init(&qiov, qiov_full.niov);
1727 d208a0e0 Aneesh Kumar K.V
        do {
1728 302a0d3e Stefan Hajnoczi
            qemu_iovec_reset(&qiov);
1729 1b093c48 Michael Tokarev
            qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1730 d208a0e0 Aneesh Kumar K.V
            if (0) {
1731 302a0d3e Stefan Hajnoczi
                print_sg(qiov.iov, qiov.niov);
1732 d208a0e0 Aneesh Kumar K.V
            }
1733 d208a0e0 Aneesh Kumar K.V
            /* Loop in case of EINTR */
1734 d208a0e0 Aneesh Kumar K.V
            do {
1735 302a0d3e Stefan Hajnoczi
                len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1736 d208a0e0 Aneesh Kumar K.V
                if (len >= 0) {
1737 d208a0e0 Aneesh Kumar K.V
                    off   += len;
1738 d208a0e0 Aneesh Kumar K.V
                    count += len;
1739 d208a0e0 Aneesh Kumar K.V
                }
1740 bccacf6c Aneesh Kumar K.V
            } while (len == -EINTR && !pdu->cancelled);
1741 d208a0e0 Aneesh Kumar K.V
            if (len < 0) {
1742 d208a0e0 Aneesh Kumar K.V
                /* IO error return the error */
1743 d208a0e0 Aneesh Kumar K.V
                err = len;
1744 d208a0e0 Aneesh Kumar K.V
                goto out;
1745 d208a0e0 Aneesh Kumar K.V
            }
1746 d208a0e0 Aneesh Kumar K.V
        } while (count < max_count && len > 0);
1747 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "d", count);
1748 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1749 ddca7f86 M. Mohan Kumar
            goto out;
1750 ddca7f86 M. Mohan Kumar
        }
1751 ddca7f86 M. Mohan Kumar
        err += offset + count;
1752 302a0d3e Stefan Hajnoczi
        qemu_iovec_destroy(&qiov);
1753 302a0d3e Stefan Hajnoczi
        qemu_iovec_destroy(&qiov_full);
1754 d208a0e0 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_XATTR) {
1755 d208a0e0 Aneesh Kumar K.V
        err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1756 a9231555 Anthony Liguori
    } else {
1757 a9231555 Anthony Liguori
        err = -EINVAL;
1758 9f107513 Anthony Liguori
    }
1759 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1760 a9231555 Anthony Liguori
out:
1761 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1762 84dfb926 Aneesh Kumar K.V
out_nofid:
1763 a9231555 Anthony Liguori
    complete_pdu(s, pdu, err);
1764 9f107513 Anthony Liguori
}
1765 9f107513 Anthony Liguori
1766 5e4eaa79 Aneesh Kumar K.V
static size_t v9fs_readdir_data_size(V9fsString *name)
1767 c18e2f94 Sripathi Kodi
{
1768 5e4eaa79 Aneesh Kumar K.V
    /*
1769 5e4eaa79 Aneesh Kumar K.V
     * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1770 5e4eaa79 Aneesh Kumar K.V
     * size of type (1) + size of name.size (2) + strlen(name.data)
1771 5e4eaa79 Aneesh Kumar K.V
     */
1772 5e4eaa79 Aneesh Kumar K.V
    return 24 + v9fs_string_size(name);
1773 c18e2f94 Sripathi Kodi
}
1774 c18e2f94 Sripathi Kodi
1775 bccacf6c Aneesh Kumar K.V
static int v9fs_do_readdir(V9fsPDU *pdu,
1776 5e4eaa79 Aneesh Kumar K.V
                           V9fsFidState *fidp, int32_t max_count)
1777 c18e2f94 Sripathi Kodi
{
1778 c18e2f94 Sripathi Kodi
    size_t size;
1779 5e4eaa79 Aneesh Kumar K.V
    V9fsQID qid;
1780 5e4eaa79 Aneesh Kumar K.V
    V9fsString name;
1781 5e4eaa79 Aneesh Kumar K.V
    int len, err = 0;
1782 5e4eaa79 Aneesh Kumar K.V
    int32_t count = 0;
1783 5e4eaa79 Aneesh Kumar K.V
    off_t saved_dir_pos;
1784 5f524c1e Harsh Prateek Bora
    struct dirent *dent, *result;
1785 c18e2f94 Sripathi Kodi
1786 5e4eaa79 Aneesh Kumar K.V
    /* save the directory position */
1787 bccacf6c Aneesh Kumar K.V
    saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1788 5e4eaa79 Aneesh Kumar K.V
    if (saved_dir_pos < 0) {
1789 5e4eaa79 Aneesh Kumar K.V
        return saved_dir_pos;
1790 5e4eaa79 Aneesh Kumar K.V
    }
1791 5f524c1e Harsh Prateek Bora
1792 5f524c1e Harsh Prateek Bora
    dent = g_malloc(sizeof(struct dirent));
1793 5f524c1e Harsh Prateek Bora
1794 5e4eaa79 Aneesh Kumar K.V
    while (1) {
1795 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1796 5f524c1e Harsh Prateek Bora
        if (err || !result) {
1797 5e4eaa79 Aneesh Kumar K.V
            break;
1798 5e4eaa79 Aneesh Kumar K.V
        }
1799 5e4eaa79 Aneesh Kumar K.V
        v9fs_string_init(&name);
1800 5e4eaa79 Aneesh Kumar K.V
        v9fs_string_sprintf(&name, "%s", dent->d_name);
1801 5e4eaa79 Aneesh Kumar K.V
        if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1802 c18e2f94 Sripathi Kodi
            /* Ran out of buffer. Set dir back to old position and return */
1803 bccacf6c Aneesh Kumar K.V
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1804 5e4eaa79 Aneesh Kumar K.V
            v9fs_string_free(&name);
1805 5f524c1e Harsh Prateek Bora
            g_free(dent);
1806 5e4eaa79 Aneesh Kumar K.V
            return count;
1807 c18e2f94 Sripathi Kodi
        }
1808 5e4eaa79 Aneesh Kumar K.V
        /*
1809 5e4eaa79 Aneesh Kumar K.V
         * Fill up just the path field of qid because the client uses
1810 c18e2f94 Sripathi Kodi
         * only that. To fill the entire qid structure we will have
1811 c18e2f94 Sripathi Kodi
         * to stat each dirent found, which is expensive
1812 c18e2f94 Sripathi Kodi
         */
1813 5e4eaa79 Aneesh Kumar K.V
        size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1814 5e4eaa79 Aneesh Kumar K.V
        memcpy(&qid.path, &dent->d_ino, size);
1815 c18e2f94 Sripathi Kodi
        /* Fill the other fields with dummy values */
1816 5e4eaa79 Aneesh Kumar K.V
        qid.type = 0;
1817 5e4eaa79 Aneesh Kumar K.V
        qid.version = 0;
1818 c18e2f94 Sripathi Kodi
1819 5e4eaa79 Aneesh Kumar K.V
        /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1820 5e4eaa79 Aneesh Kumar K.V
        len = pdu_marshal(pdu, 11 + count, "Qqbs",
1821 5e4eaa79 Aneesh Kumar K.V
                          &qid, dent->d_off,
1822 5e4eaa79 Aneesh Kumar K.V
                          dent->d_type, &name);
1823 ddca7f86 M. Mohan Kumar
        if (len < 0) {
1824 ddca7f86 M. Mohan Kumar
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1825 ddca7f86 M. Mohan Kumar
            v9fs_string_free(&name);
1826 ddca7f86 M. Mohan Kumar
            g_free(dent);
1827 ddca7f86 M. Mohan Kumar
            return len;
1828 ddca7f86 M. Mohan Kumar
        }
1829 5e4eaa79 Aneesh Kumar K.V
        count += len;
1830 5e4eaa79 Aneesh Kumar K.V
        v9fs_string_free(&name);
1831 5e4eaa79 Aneesh Kumar K.V
        saved_dir_pos = dent->d_off;
1832 5e4eaa79 Aneesh Kumar K.V
    }
1833 5f524c1e Harsh Prateek Bora
    g_free(dent);
1834 5e4eaa79 Aneesh Kumar K.V
    if (err < 0) {
1835 5e4eaa79 Aneesh Kumar K.V
        return err;
1836 5e4eaa79 Aneesh Kumar K.V
    }
1837 5e4eaa79 Aneesh Kumar K.V
    return count;
1838 c18e2f94 Sripathi Kodi
}
1839 c18e2f94 Sripathi Kodi
1840 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_readdir(void *opaque)
1841 c18e2f94 Sripathi Kodi
{
1842 c18e2f94 Sripathi Kodi
    int32_t fid;
1843 5e4eaa79 Aneesh Kumar K.V
    V9fsFidState *fidp;
1844 5e4eaa79 Aneesh Kumar K.V
    ssize_t retval = 0;
1845 c18e2f94 Sripathi Kodi
    size_t offset = 7;
1846 2f008a8c Aneesh Kumar K.V
    uint64_t initial_offset;
1847 2f008a8c Aneesh Kumar K.V
    int32_t count;
1848 2f008a8c Aneesh Kumar K.V
    uint32_t max_count;
1849 5e4eaa79 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1850 5e4eaa79 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1851 c18e2f94 Sripathi Kodi
1852 ddca7f86 M. Mohan Kumar
    retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1853 ddca7f86 M. Mohan Kumar
                           &initial_offset, &max_count);
1854 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
1855 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1856 ddca7f86 M. Mohan Kumar
    }
1857 c572f23a Harsh Prateek Bora
    trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1858 c572f23a Harsh Prateek Bora
1859 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1860 84dfb926 Aneesh Kumar K.V
    if (fidp == NULL) {
1861 84dfb926 Aneesh Kumar K.V
        retval = -EINVAL;
1862 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1863 84dfb926 Aneesh Kumar K.V
    }
1864 84dfb926 Aneesh Kumar K.V
    if (!fidp->fs.dir) {
1865 5e4eaa79 Aneesh Kumar K.V
        retval = -EINVAL;
1866 c18e2f94 Sripathi Kodi
        goto out;
1867 c18e2f94 Sripathi Kodi
    }
1868 5e4eaa79 Aneesh Kumar K.V
    if (initial_offset == 0) {
1869 bccacf6c Aneesh Kumar K.V
        v9fs_co_rewinddir(pdu, fidp);
1870 c18e2f94 Sripathi Kodi
    } else {
1871 bccacf6c Aneesh Kumar K.V
        v9fs_co_seekdir(pdu, fidp, initial_offset);
1872 c18e2f94 Sripathi Kodi
    }
1873 bccacf6c Aneesh Kumar K.V
    count = v9fs_do_readdir(pdu, fidp, max_count);
1874 5e4eaa79 Aneesh Kumar K.V
    if (count < 0) {
1875 5e4eaa79 Aneesh Kumar K.V
        retval = count;
1876 5e4eaa79 Aneesh Kumar K.V
        goto out;
1877 5e4eaa79 Aneesh Kumar K.V
    }
1878 ddca7f86 M. Mohan Kumar
    retval = pdu_marshal(pdu, offset, "d", count);
1879 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
1880 ddca7f86 M. Mohan Kumar
        goto out;
1881 ddca7f86 M. Mohan Kumar
    }
1882 ddca7f86 M. Mohan Kumar
    retval += count + offset;
1883 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1884 c18e2f94 Sripathi Kodi
out:
1885 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1886 84dfb926 Aneesh Kumar K.V
out_nofid:
1887 5e4eaa79 Aneesh Kumar K.V
    complete_pdu(s, pdu, retval);
1888 c18e2f94 Sripathi Kodi
}
1889 c18e2f94 Sripathi Kodi
1890 d7a90491 Aneesh Kumar K.V
static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1891 2f008a8c Aneesh Kumar K.V
                            uint64_t off, uint32_t count,
1892 d7a90491 Aneesh Kumar K.V
                            struct iovec *sg, int cnt)
1893 10b468bd Aneesh Kumar K.V
{
1894 10b468bd Aneesh Kumar K.V
    int i, to_copy;
1895 10b468bd Aneesh Kumar K.V
    ssize_t err = 0;
1896 10b468bd Aneesh Kumar K.V
    int write_count;
1897 10b468bd Aneesh Kumar K.V
    int64_t xattr_len;
1898 d7a90491 Aneesh Kumar K.V
    size_t offset = 7;
1899 10b468bd Aneesh Kumar K.V
1900 d7a90491 Aneesh Kumar K.V
1901 d7a90491 Aneesh Kumar K.V
    xattr_len = fidp->fs.xattr.len;
1902 d7a90491 Aneesh Kumar K.V
    write_count = xattr_len - off;
1903 d7a90491 Aneesh Kumar K.V
    if (write_count > count) {
1904 d7a90491 Aneesh Kumar K.V
        write_count = count;
1905 10b468bd Aneesh Kumar K.V
    } else if (write_count < 0) {
1906 10b468bd Aneesh Kumar K.V
        /*
1907 10b468bd Aneesh Kumar K.V
         * write beyond XATTR value len specified in
1908 10b468bd Aneesh Kumar K.V
         * xattrcreate
1909 10b468bd Aneesh Kumar K.V
         */
1910 10b468bd Aneesh Kumar K.V
        err = -ENOSPC;
1911 10b468bd Aneesh Kumar K.V
        goto out;
1912 10b468bd Aneesh Kumar K.V
    }
1913 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "d", write_count);
1914 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1915 ddca7f86 M. Mohan Kumar
        return err;
1916 ddca7f86 M. Mohan Kumar
    }
1917 ddca7f86 M. Mohan Kumar
    err += offset;
1918 d7a90491 Aneesh Kumar K.V
    fidp->fs.xattr.copied_len += write_count;
1919 10b468bd Aneesh Kumar K.V
    /*
1920 10b468bd Aneesh Kumar K.V
     * Now copy the content from sg list
1921 10b468bd Aneesh Kumar K.V
     */
1922 d7a90491 Aneesh Kumar K.V
    for (i = 0; i < cnt; i++) {
1923 d7a90491 Aneesh Kumar K.V
        if (write_count > sg[i].iov_len) {
1924 d7a90491 Aneesh Kumar K.V
            to_copy = sg[i].iov_len;
1925 10b468bd Aneesh Kumar K.V
        } else {
1926 10b468bd Aneesh Kumar K.V
            to_copy = write_count;
1927 10b468bd Aneesh Kumar K.V
        }
1928 d7a90491 Aneesh Kumar K.V
        memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
1929 10b468bd Aneesh Kumar K.V
        /* updating vs->off since we are not using below */
1930 d7a90491 Aneesh Kumar K.V
        off += to_copy;
1931 10b468bd Aneesh Kumar K.V
        write_count -= to_copy;
1932 10b468bd Aneesh Kumar K.V
    }
1933 10b468bd Aneesh Kumar K.V
out:
1934 d7a90491 Aneesh Kumar K.V
    return err;
1935 10b468bd Aneesh Kumar K.V
}
1936 10b468bd Aneesh Kumar K.V
1937 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_write(void *opaque)
1938 9f107513 Anthony Liguori
{
1939 d7a90491 Aneesh Kumar K.V
    ssize_t err;
1940 d7a90491 Aneesh Kumar K.V
    int32_t fid;
1941 2f008a8c Aneesh Kumar K.V
    uint64_t off;
1942 2f008a8c Aneesh Kumar K.V
    uint32_t count;
1943 d7a90491 Aneesh Kumar K.V
    int32_t len = 0;
1944 d7a90491 Aneesh Kumar K.V
    int32_t total = 0;
1945 d7a90491 Aneesh Kumar K.V
    size_t offset = 7;
1946 d7a90491 Aneesh Kumar K.V
    V9fsFidState *fidp;
1947 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
1948 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
1949 302a0d3e Stefan Hajnoczi
    QEMUIOVector qiov_full;
1950 302a0d3e Stefan Hajnoczi
    QEMUIOVector qiov;
1951 8449360c Anthony Liguori
1952 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
1953 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1954 ddca7f86 M. Mohan Kumar
        return complete_pdu(s, pdu, err);
1955 ddca7f86 M. Mohan Kumar
    }
1956 ddca7f86 M. Mohan Kumar
    offset += err;
1957 302a0d3e Stefan Hajnoczi
    v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
1958 302a0d3e Stefan Hajnoczi
    trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
1959 84dfb926 Aneesh Kumar K.V
1960 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1961 d7a90491 Aneesh Kumar K.V
    if (fidp == NULL) {
1962 8449360c Anthony Liguori
        err = -EINVAL;
1963 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1964 9f107513 Anthony Liguori
    }
1965 d7a90491 Aneesh Kumar K.V
    if (fidp->fid_type == P9_FID_FILE) {
1966 d7a90491 Aneesh Kumar K.V
        if (fidp->fs.fd == -1) {
1967 10b468bd Aneesh Kumar K.V
            err = -EINVAL;
1968 10b468bd Aneesh Kumar K.V
            goto out;
1969 10b468bd Aneesh Kumar K.V
        }
1970 d7a90491 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_XATTR) {
1971 10b468bd Aneesh Kumar K.V
        /*
1972 10b468bd Aneesh Kumar K.V
         * setxattr operation
1973 10b468bd Aneesh Kumar K.V
         */
1974 302a0d3e Stefan Hajnoczi
        err = v9fs_xattr_write(s, pdu, fidp, off, count,
1975 302a0d3e Stefan Hajnoczi
                               qiov_full.iov, qiov_full.niov);
1976 d7a90491 Aneesh Kumar K.V
        goto out;
1977 10b468bd Aneesh Kumar K.V
    } else {
1978 8449360c Anthony Liguori
        err = -EINVAL;
1979 8449360c Anthony Liguori
        goto out;
1980 8449360c Anthony Liguori
    }
1981 302a0d3e Stefan Hajnoczi
    qemu_iovec_init(&qiov, qiov_full.niov);
1982 d7a90491 Aneesh Kumar K.V
    do {
1983 302a0d3e Stefan Hajnoczi
        qemu_iovec_reset(&qiov);
1984 1b093c48 Michael Tokarev
        qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
1985 d7a90491 Aneesh Kumar K.V
        if (0) {
1986 302a0d3e Stefan Hajnoczi
            print_sg(qiov.iov, qiov.niov);
1987 56d15a53 Sanchit Garg
        }
1988 d7a90491 Aneesh Kumar K.V
        /* Loop in case of EINTR */
1989 d7a90491 Aneesh Kumar K.V
        do {
1990 302a0d3e Stefan Hajnoczi
            len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
1991 d7a90491 Aneesh Kumar K.V
            if (len >= 0) {
1992 d7a90491 Aneesh Kumar K.V
                off   += len;
1993 d7a90491 Aneesh Kumar K.V
                total += len;
1994 d7a90491 Aneesh Kumar K.V
            }
1995 bccacf6c Aneesh Kumar K.V
        } while (len == -EINTR && !pdu->cancelled);
1996 d7a90491 Aneesh Kumar K.V
        if (len < 0) {
1997 d7a90491 Aneesh Kumar K.V
            /* IO error return the error */
1998 d7a90491 Aneesh Kumar K.V
            err = len;
1999 302a0d3e Stefan Hajnoczi
            goto out_qiov;
2000 d7a90491 Aneesh Kumar K.V
        }
2001 d7a90491 Aneesh Kumar K.V
    } while (total < count && len > 0);
2002 302a0d3e Stefan Hajnoczi
2003 302a0d3e Stefan Hajnoczi
    offset = 7;
2004 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "d", total);
2005 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2006 ddca7f86 M. Mohan Kumar
        goto out;
2007 ddca7f86 M. Mohan Kumar
    }
2008 ddca7f86 M. Mohan Kumar
    err += offset;
2009 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2010 302a0d3e Stefan Hajnoczi
out_qiov:
2011 302a0d3e Stefan Hajnoczi
    qemu_iovec_destroy(&qiov);
2012 8449360c Anthony Liguori
out:
2013 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2014 84dfb926 Aneesh Kumar K.V
out_nofid:
2015 302a0d3e Stefan Hajnoczi
    qemu_iovec_destroy(&qiov_full);
2016 d7a90491 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2017 9f107513 Anthony Liguori
}
2018 9f107513 Anthony Liguori
2019 baaa86d9 Venkateswararao Jujjuri
static void v9fs_create(void *opaque)
2020 5e94c103 M. Mohan Kumar
{
2021 baaa86d9 Venkateswararao Jujjuri
    int32_t fid;
2022 baaa86d9 Venkateswararao Jujjuri
    int err = 0;
2023 baaa86d9 Venkateswararao Jujjuri
    size_t offset = 7;
2024 baaa86d9 Venkateswararao Jujjuri
    V9fsFidState *fidp;
2025 baaa86d9 Venkateswararao Jujjuri
    V9fsQID qid;
2026 baaa86d9 Venkateswararao Jujjuri
    int32_t perm;
2027 baaa86d9 Venkateswararao Jujjuri
    int8_t mode;
2028 2289be19 Aneesh Kumar K.V
    V9fsPath path;
2029 baaa86d9 Venkateswararao Jujjuri
    struct stat stbuf;
2030 baaa86d9 Venkateswararao Jujjuri
    V9fsString name;
2031 baaa86d9 Venkateswararao Jujjuri
    V9fsString extension;
2032 baaa86d9 Venkateswararao Jujjuri
    int iounit;
2033 baaa86d9 Venkateswararao Jujjuri
    V9fsPDU *pdu = opaque;
2034 c494dd6f Anthony Liguori
2035 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&path);
2036 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2037 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&extension);
2038 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2039 ddca7f86 M. Mohan Kumar
                        &perm, &mode, &extension);
2040 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2041 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2042 ddca7f86 M. Mohan Kumar
    }
2043 c572f23a Harsh Prateek Bora
    trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2044 c572f23a Harsh Prateek Bora
2045 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2046 baaa86d9 Venkateswararao Jujjuri
    if (fidp == NULL) {
2047 baaa86d9 Venkateswararao Jujjuri
        err = -EINVAL;
2048 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2049 c494dd6f Anthony Liguori
    }
2050 baaa86d9 Venkateswararao Jujjuri
    if (perm & P9_STAT_MODE_DIR) {
2051 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2052 02cb7f3a Aneesh Kumar K.V
                            fidp->uid, -1, &stbuf);
2053 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2054 baaa86d9 Venkateswararao Jujjuri
            goto out;
2055 baaa86d9 Venkateswararao Jujjuri
        }
2056 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2057 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2058 2289be19 Aneesh Kumar K.V
            goto out;
2059 2289be19 Aneesh Kumar K.V
        }
2060 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2061 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_opendir(pdu, fidp);
2062 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2063 baaa86d9 Venkateswararao Jujjuri
            goto out;
2064 baaa86d9 Venkateswararao Jujjuri
        }
2065 baaa86d9 Venkateswararao Jujjuri
        fidp->fid_type = P9_FID_DIR;
2066 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_SYMLINK) {
2067 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_symlink(pdu, fidp, &name,
2068 02cb7f3a Aneesh Kumar K.V
                              extension.data, -1 , &stbuf);
2069 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2070 baaa86d9 Venkateswararao Jujjuri
            goto out;
2071 baaa86d9 Venkateswararao Jujjuri
        }
2072 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2073 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2074 2289be19 Aneesh Kumar K.V
            goto out;
2075 2289be19 Aneesh Kumar K.V
        }
2076 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2077 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_LINK) {
2078 2289be19 Aneesh Kumar K.V
        int32_t ofid = atoi(extension.data);
2079 bccacf6c Aneesh Kumar K.V
        V9fsFidState *ofidp = get_fid(pdu, ofid);
2080 2289be19 Aneesh Kumar K.V
        if (ofidp == NULL) {
2081 baaa86d9 Venkateswararao Jujjuri
            err = -EINVAL;
2082 baaa86d9 Venkateswararao Jujjuri
            goto out;
2083 baaa86d9 Venkateswararao Jujjuri
        }
2084 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_link(pdu, ofidp, fidp, &name);
2085 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, ofidp);
2086 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2087 2289be19 Aneesh Kumar K.V
            goto out;
2088 2289be19 Aneesh Kumar K.V
        }
2089 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2090 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2091 2289be19 Aneesh Kumar K.V
            fidp->fid_type = P9_FID_NONE;
2092 baaa86d9 Venkateswararao Jujjuri
            goto out;
2093 c494dd6f Anthony Liguori
        }
2094 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2095 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2096 02cb7f3a Aneesh Kumar K.V
        if (err < 0) {
2097 02cb7f3a Aneesh Kumar K.V
            fidp->fid_type = P9_FID_NONE;
2098 02cb7f3a Aneesh Kumar K.V
            goto out;
2099 02cb7f3a Aneesh Kumar K.V
        }
2100 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_DEVICE) {
2101 c494dd6f Anthony Liguori
        char ctype;
2102 c494dd6f Anthony Liguori
        uint32_t major, minor;
2103 c494dd6f Anthony Liguori
        mode_t nmode = 0;
2104 c494dd6f Anthony Liguori
2105 baaa86d9 Venkateswararao Jujjuri
        if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2106 c494dd6f Anthony Liguori
            err = -errno;
2107 baaa86d9 Venkateswararao Jujjuri
            goto out;
2108 c494dd6f Anthony Liguori
        }
2109 c494dd6f Anthony Liguori
2110 c494dd6f Anthony Liguori
        switch (ctype) {
2111 c494dd6f Anthony Liguori
        case 'c':
2112 c494dd6f Anthony Liguori
            nmode = S_IFCHR;
2113 c494dd6f Anthony Liguori
            break;
2114 c494dd6f Anthony Liguori
        case 'b':
2115 c494dd6f Anthony Liguori
            nmode = S_IFBLK;
2116 c494dd6f Anthony Liguori
            break;
2117 c494dd6f Anthony Liguori
        default:
2118 c494dd6f Anthony Liguori
            err = -EIO;
2119 baaa86d9 Venkateswararao Jujjuri
            goto out;
2120 baaa86d9 Venkateswararao Jujjuri
        }
2121 c1568af5 Venkateswararao Jujjuri (JV)
2122 baaa86d9 Venkateswararao Jujjuri
        nmode |= perm & 0777;
2123 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2124 02cb7f3a Aneesh Kumar K.V
                            makedev(major, minor), nmode, &stbuf);
2125 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2126 baaa86d9 Venkateswararao Jujjuri
            goto out;
2127 baaa86d9 Venkateswararao Jujjuri
        }
2128 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2129 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2130 2289be19 Aneesh Kumar K.V
            goto out;
2131 2289be19 Aneesh Kumar K.V
        }
2132 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2133 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2134 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2135 02cb7f3a Aneesh Kumar K.V
                            0, S_IFIFO | (perm & 0777), &stbuf);
2136 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2137 baaa86d9 Venkateswararao Jujjuri
            goto out;
2138 baaa86d9 Venkateswararao Jujjuri
        }
2139 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2140 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2141 2289be19 Aneesh Kumar K.V
            goto out;
2142 2289be19 Aneesh Kumar K.V
        }
2143 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2144 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_SOCKET) {
2145 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2146 02cb7f3a Aneesh Kumar K.V
                            0, S_IFSOCK | (perm & 0777), &stbuf);
2147 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2148 baaa86d9 Venkateswararao Jujjuri
            goto out;
2149 baaa86d9 Venkateswararao Jujjuri
        }
2150 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2151 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2152 2289be19 Aneesh Kumar K.V
            goto out;
2153 2289be19 Aneesh Kumar K.V
        }
2154 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2155 baaa86d9 Venkateswararao Jujjuri
    } else {
2156 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_open2(pdu, fidp, &name, -1,
2157 02cb7f3a Aneesh Kumar K.V
                            omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2158 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2159 baaa86d9 Venkateswararao Jujjuri
            goto out;
2160 baaa86d9 Venkateswararao Jujjuri
        }
2161 baaa86d9 Venkateswararao Jujjuri
        fidp->fid_type = P9_FID_FILE;
2162 7a462745 Aneesh Kumar K.V
        fidp->open_flags = omode_to_uflags(mode);
2163 7a462745 Aneesh Kumar K.V
        if (fidp->open_flags & O_EXCL) {
2164 7a462745 Aneesh Kumar K.V
            /*
2165 7a462745 Aneesh Kumar K.V
             * We let the host file system do O_EXCL check
2166 7a462745 Aneesh Kumar K.V
             * We should not reclaim such fd
2167 7a462745 Aneesh Kumar K.V
             */
2168 7a462745 Aneesh Kumar K.V
            fidp->flags |= FID_NON_RECLAIMABLE;
2169 7a462745 Aneesh Kumar K.V
        }
2170 c494dd6f Anthony Liguori
    }
2171 bccacf6c Aneesh Kumar K.V
    iounit = get_iounit(pdu, &fidp->path);
2172 baaa86d9 Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
2173 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2174 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2175 ddca7f86 M. Mohan Kumar
        goto out;
2176 ddca7f86 M. Mohan Kumar
    }
2177 ddca7f86 M. Mohan Kumar
    err += offset;
2178 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_create_return(pdu->tag, pdu->id,
2179 7999f7e1 Aneesh Kumar K.V
                             qid.type, qid.version, qid.path, iounit);
2180 c494dd6f Anthony Liguori
out:
2181 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2182 84dfb926 Aneesh Kumar K.V
out_nofid:
2183 baaa86d9 Venkateswararao Jujjuri
   complete_pdu(pdu->s, pdu, err);
2184 baaa86d9 Venkateswararao Jujjuri
   v9fs_string_free(&name);
2185 baaa86d9 Venkateswararao Jujjuri
   v9fs_string_free(&extension);
2186 2289be19 Aneesh Kumar K.V
   v9fs_path_free(&path);
2187 9f107513 Anthony Liguori
}
2188 9f107513 Anthony Liguori
2189 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_symlink(void *opaque)
2190 08c60fc9 Venkateswararao Jujjuri (JV)
{
2191 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2192 3fa2a8d1 Venkateswararao Jujjuri
    V9fsString name;
2193 3fa2a8d1 Venkateswararao Jujjuri
    V9fsString symname;
2194 3fa2a8d1 Venkateswararao Jujjuri
    V9fsFidState *dfidp;
2195 3fa2a8d1 Venkateswararao Jujjuri
    V9fsQID qid;
2196 3fa2a8d1 Venkateswararao Jujjuri
    struct stat stbuf;
2197 08c60fc9 Venkateswararao Jujjuri (JV)
    int32_t dfid;
2198 08c60fc9 Venkateswararao Jujjuri (JV)
    int err = 0;
2199 08c60fc9 Venkateswararao Jujjuri (JV)
    gid_t gid;
2200 3fa2a8d1 Venkateswararao Jujjuri
    size_t offset = 7;
2201 08c60fc9 Venkateswararao Jujjuri (JV)
2202 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2203 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&symname);
2204 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2205 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2206 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2207 ddca7f86 M. Mohan Kumar
    }
2208 c572f23a Harsh Prateek Bora
    trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2209 08c60fc9 Venkateswararao Jujjuri (JV)
2210 bccacf6c Aneesh Kumar K.V
    dfidp = get_fid(pdu, dfid);
2211 3fa2a8d1 Venkateswararao Jujjuri
    if (dfidp == NULL) {
2212 08c60fc9 Venkateswararao Jujjuri (JV)
        err = -EINVAL;
2213 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2214 08c60fc9 Venkateswararao Jujjuri (JV)
    }
2215 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2216 3fa2a8d1 Venkateswararao Jujjuri
    if (err < 0) {
2217 3fa2a8d1 Venkateswararao Jujjuri
        goto out;
2218 3fa2a8d1 Venkateswararao Jujjuri
    }
2219 3fa2a8d1 Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
2220 ddca7f86 M. Mohan Kumar
    err =  pdu_marshal(pdu, offset, "Q", &qid);
2221 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2222 ddca7f86 M. Mohan Kumar
        goto out;
2223 ddca7f86 M. Mohan Kumar
    }
2224 ddca7f86 M. Mohan Kumar
    err += offset;
2225 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_symlink_return(pdu->tag, pdu->id,
2226 7999f7e1 Aneesh Kumar K.V
                              qid.type, qid.version, qid.path);
2227 08c60fc9 Venkateswararao Jujjuri (JV)
out:
2228 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, dfidp);
2229 84dfb926 Aneesh Kumar K.V
out_nofid:
2230 3fa2a8d1 Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
2231 3fa2a8d1 Venkateswararao Jujjuri
    v9fs_string_free(&name);
2232 3fa2a8d1 Venkateswararao Jujjuri
    v9fs_string_free(&symname);
2233 08c60fc9 Venkateswararao Jujjuri (JV)
}
2234 08c60fc9 Venkateswararao Jujjuri (JV)
2235 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_flush(void *opaque)
2236 9f107513 Anthony Liguori
{
2237 ddca7f86 M. Mohan Kumar
    ssize_t err;
2238 bccacf6c Aneesh Kumar K.V
    int16_t tag;
2239 bccacf6c Aneesh Kumar K.V
    size_t offset = 7;
2240 bccacf6c Aneesh Kumar K.V
    V9fsPDU *cancel_pdu;
2241 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2242 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2243 bccacf6c Aneesh Kumar K.V
2244 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "w", &tag);
2245 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2246 ddca7f86 M. Mohan Kumar
        complete_pdu(s, pdu, err);
2247 ddca7f86 M. Mohan Kumar
        return;
2248 ddca7f86 M. Mohan Kumar
    }
2249 c572f23a Harsh Prateek Bora
    trace_v9fs_flush(pdu->tag, pdu->id, tag);
2250 bccacf6c Aneesh Kumar K.V
2251 bccacf6c Aneesh Kumar K.V
    QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2252 bccacf6c Aneesh Kumar K.V
        if (cancel_pdu->tag == tag) {
2253 bccacf6c Aneesh Kumar K.V
            break;
2254 bccacf6c Aneesh Kumar K.V
        }
2255 bccacf6c Aneesh Kumar K.V
    }
2256 bccacf6c Aneesh Kumar K.V
    if (cancel_pdu) {
2257 bccacf6c Aneesh Kumar K.V
        cancel_pdu->cancelled = 1;
2258 bccacf6c Aneesh Kumar K.V
        /*
2259 bccacf6c Aneesh Kumar K.V
         * Wait for pdu to complete.
2260 bccacf6c Aneesh Kumar K.V
         */
2261 bccacf6c Aneesh Kumar K.V
        qemu_co_queue_wait(&cancel_pdu->complete);
2262 bccacf6c Aneesh Kumar K.V
        cancel_pdu->cancelled = 0;
2263 bccacf6c Aneesh Kumar K.V
        free_pdu(pdu->s, cancel_pdu);
2264 bccacf6c Aneesh Kumar K.V
    }
2265 9c5e9d89 Anthony Liguori
    complete_pdu(s, pdu, 7);
2266 9f107513 Anthony Liguori
}
2267 9f107513 Anthony Liguori
2268 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_link(void *opaque)
2269 b2c224be Venkateswararao Jujjuri (JV)
{
2270 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2271 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2272 b2c224be Venkateswararao Jujjuri (JV)
    int32_t dfid, oldfid;
2273 b2c224be Venkateswararao Jujjuri (JV)
    V9fsFidState *dfidp, *oldfidp;
2274 3a93113a Dong Xu Wang
    V9fsString name;
2275 b2c224be Venkateswararao Jujjuri (JV)
    size_t offset = 7;
2276 b2c224be Venkateswararao Jujjuri (JV)
    int err = 0;
2277 b2c224be Venkateswararao Jujjuri (JV)
2278 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2279 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2280 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2281 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2282 ddca7f86 M. Mohan Kumar
    }
2283 c572f23a Harsh Prateek Bora
    trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2284 b2c224be Venkateswararao Jujjuri (JV)
2285 bccacf6c Aneesh Kumar K.V
    dfidp = get_fid(pdu, dfid);
2286 b2c224be Venkateswararao Jujjuri (JV)
    if (dfidp == NULL) {
2287 ffd66876 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
2288 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2289 b2c224be Venkateswararao Jujjuri (JV)
    }
2290 b2c224be Venkateswararao Jujjuri (JV)
2291 bccacf6c Aneesh Kumar K.V
    oldfidp = get_fid(pdu, oldfid);
2292 b2c224be Venkateswararao Jujjuri (JV)
    if (oldfidp == NULL) {
2293 ffd66876 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
2294 b2c224be Venkateswararao Jujjuri (JV)
        goto out;
2295 b2c224be Venkateswararao Jujjuri (JV)
    }
2296 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2297 ffd66876 Venkateswararao Jujjuri (JV)
    if (!err) {
2298 ffd66876 Venkateswararao Jujjuri (JV)
        err = offset;
2299 b2c224be Venkateswararao Jujjuri (JV)
    }
2300 b2c224be Venkateswararao Jujjuri (JV)
out:
2301 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, dfidp);
2302 84dfb926 Aneesh Kumar K.V
out_nofid:
2303 b2c224be Venkateswararao Jujjuri (JV)
    v9fs_string_free(&name);
2304 b2c224be Venkateswararao Jujjuri (JV)
    complete_pdu(s, pdu, err);
2305 b2c224be Venkateswararao Jujjuri (JV)
}
2306 b2c224be Venkateswararao Jujjuri (JV)
2307 532decb7 Aneesh Kumar K.V
/* Only works with path name based fid */
2308 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_remove(void *opaque)
2309 9f107513 Anthony Liguori
{
2310 5bae1900 Anthony Liguori
    int32_t fid;
2311 5bae1900 Anthony Liguori
    int err = 0;
2312 ae1ef571 Venkateswararao Jujjuri
    size_t offset = 7;
2313 ae1ef571 Venkateswararao Jujjuri
    V9fsFidState *fidp;
2314 ae1ef571 Venkateswararao Jujjuri
    V9fsPDU *pdu = opaque;
2315 5bae1900 Anthony Liguori
2316 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "d", &fid);
2317 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2318 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2319 ddca7f86 M. Mohan Kumar
    }
2320 c572f23a Harsh Prateek Bora
    trace_v9fs_remove(pdu->tag, pdu->id, fid);
2321 5bae1900 Anthony Liguori
2322 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2323 ae1ef571 Venkateswararao Jujjuri
    if (fidp == NULL) {
2324 5bae1900 Anthony Liguori
        err = -EINVAL;
2325 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2326 9f107513 Anthony Liguori
    }
2327 532decb7 Aneesh Kumar K.V
    /* if fs driver is not path based, return EOPNOTSUPP */
2328 c98f1d4a Aneesh Kumar K.V
    if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2329 532decb7 Aneesh Kumar K.V
        err = -EOPNOTSUPP;
2330 532decb7 Aneesh Kumar K.V
        goto out_err;
2331 532decb7 Aneesh Kumar K.V
    }
2332 7a462745 Aneesh Kumar K.V
    /*
2333 7a462745 Aneesh Kumar K.V
     * IF the file is unlinked, we cannot reopen
2334 7a462745 Aneesh Kumar K.V
     * the file later. So don't reclaim fd
2335 7a462745 Aneesh Kumar K.V
     */
2336 bccacf6c Aneesh Kumar K.V
    err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2337 7a462745 Aneesh Kumar K.V
    if (err < 0) {
2338 7a462745 Aneesh Kumar K.V
        goto out_err;
2339 7a462745 Aneesh Kumar K.V
    }
2340 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_remove(pdu, &fidp->path);
2341 ae1ef571 Venkateswararao Jujjuri
    if (!err) {
2342 ae1ef571 Venkateswararao Jujjuri
        err = offset;
2343 ae1ef571 Venkateswararao Jujjuri
    }
2344 7a462745 Aneesh Kumar K.V
out_err:
2345 ae1ef571 Venkateswararao Jujjuri
    /* For TREMOVE we need to clunk the fid even on failed remove */
2346 84dfb926 Aneesh Kumar K.V
    clunk_fid(pdu->s, fidp->fid);
2347 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2348 84dfb926 Aneesh Kumar K.V
out_nofid:
2349 ae1ef571 Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
2350 9f107513 Anthony Liguori
}
2351 9f107513 Anthony Liguori
2352 7834cf77 Aneesh Kumar K.V
static void v9fs_unlinkat(void *opaque)
2353 7834cf77 Aneesh Kumar K.V
{
2354 7834cf77 Aneesh Kumar K.V
    int err = 0;
2355 7834cf77 Aneesh Kumar K.V
    V9fsString name;
2356 7834cf77 Aneesh Kumar K.V
    int32_t dfid, flags;
2357 7834cf77 Aneesh Kumar K.V
    size_t offset = 7;
2358 2289be19 Aneesh Kumar K.V
    V9fsPath path;
2359 7834cf77 Aneesh Kumar K.V
    V9fsFidState *dfidp;
2360 7834cf77 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2361 7834cf77 Aneesh Kumar K.V
2362 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2363 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2364 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2365 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2366 ddca7f86 M. Mohan Kumar
    }
2367 bccacf6c Aneesh Kumar K.V
    dfidp = get_fid(pdu, dfid);
2368 7834cf77 Aneesh Kumar K.V
    if (dfidp == NULL) {
2369 7834cf77 Aneesh Kumar K.V
        err = -EINVAL;
2370 7834cf77 Aneesh Kumar K.V
        goto out_nofid;
2371 7834cf77 Aneesh Kumar K.V
    }
2372 7834cf77 Aneesh Kumar K.V
    /*
2373 7834cf77 Aneesh Kumar K.V
     * IF the file is unlinked, we cannot reopen
2374 7834cf77 Aneesh Kumar K.V
     * the file later. So don't reclaim fd
2375 7834cf77 Aneesh Kumar K.V
     */
2376 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&path);
2377 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2378 2289be19 Aneesh Kumar K.V
    if (err < 0) {
2379 2289be19 Aneesh Kumar K.V
        goto out_err;
2380 2289be19 Aneesh Kumar K.V
    }
2381 bccacf6c Aneesh Kumar K.V
    err = v9fs_mark_fids_unreclaim(pdu, &path);
2382 7834cf77 Aneesh Kumar K.V
    if (err < 0) {
2383 7834cf77 Aneesh Kumar K.V
        goto out_err;
2384 7834cf77 Aneesh Kumar K.V
    }
2385 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2386 7834cf77 Aneesh Kumar K.V
    if (!err) {
2387 7834cf77 Aneesh Kumar K.V
        err = offset;
2388 7834cf77 Aneesh Kumar K.V
    }
2389 7834cf77 Aneesh Kumar K.V
out_err:
2390 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, dfidp);
2391 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&path);
2392 7834cf77 Aneesh Kumar K.V
out_nofid:
2393 7834cf77 Aneesh Kumar K.V
    complete_pdu(pdu->s, pdu, err);
2394 7834cf77 Aneesh Kumar K.V
    v9fs_string_free(&name);
2395 7834cf77 Aneesh Kumar K.V
}
2396 7834cf77 Aneesh Kumar K.V
2397 2289be19 Aneesh Kumar K.V
2398 2289be19 Aneesh Kumar K.V
/* Only works with path name based fid */
2399 bccacf6c Aneesh Kumar K.V
static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2400 930b1e17 Aneesh Kumar K.V
                                int32_t newdirfid, V9fsString *name)
2401 8cf89e00 Anthony Liguori
{
2402 930b1e17 Aneesh Kumar K.V
    char *end;
2403 c7b4b0b3 M. Mohan Kumar
    int err = 0;
2404 2289be19 Aneesh Kumar K.V
    V9fsPath new_path;
2405 2289be19 Aneesh Kumar K.V
    V9fsFidState *tfidp;
2406 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2407 84dfb926 Aneesh Kumar K.V
    V9fsFidState *dirfidp = NULL;
2408 c7b4b0b3 M. Mohan Kumar
    char *old_name, *new_name;
2409 8cf89e00 Anthony Liguori
2410 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&new_path);
2411 930b1e17 Aneesh Kumar K.V
    if (newdirfid != -1) {
2412 bccacf6c Aneesh Kumar K.V
        dirfidp = get_fid(pdu, newdirfid);
2413 c7b4b0b3 M. Mohan Kumar
        if (dirfidp == NULL) {
2414 c7b4b0b3 M. Mohan Kumar
            err = -ENOENT;
2415 84dfb926 Aneesh Kumar K.V
            goto out_nofid;
2416 c7b4b0b3 M. Mohan Kumar
        }
2417 d62dbb51 Aneesh Kumar K.V
        BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2418 bccacf6c Aneesh Kumar K.V
        v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2419 c7b4b0b3 M. Mohan Kumar
    } else {
2420 930b1e17 Aneesh Kumar K.V
        old_name = fidp->path.data;
2421 8cf89e00 Anthony Liguori
        end = strrchr(old_name, '/');
2422 8cf89e00 Anthony Liguori
        if (end) {
2423 8cf89e00 Anthony Liguori
            end++;
2424 8cf89e00 Anthony Liguori
        } else {
2425 8cf89e00 Anthony Liguori
            end = old_name;
2426 8cf89e00 Anthony Liguori
        }
2427 7267c094 Anthony Liguori
        new_name = g_malloc0(end - old_name + name->size + 1);
2428 c7b4b0b3 M. Mohan Kumar
        strncat(new_name, old_name, end - old_name);
2429 930b1e17 Aneesh Kumar K.V
        strncat(new_name + (end - old_name), name->data, name->size);
2430 bccacf6c Aneesh Kumar K.V
        v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2431 2289be19 Aneesh Kumar K.V
        g_free(new_name);
2432 c7b4b0b3 M. Mohan Kumar
    }
2433 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2434 2289be19 Aneesh Kumar K.V
    if (err < 0) {
2435 2289be19 Aneesh Kumar K.V
        goto out;
2436 2289be19 Aneesh Kumar K.V
    }
2437 2289be19 Aneesh Kumar K.V
    /*
2438 2289be19 Aneesh Kumar K.V
     * Fixup fid's pointing to the old name to
2439 2289be19 Aneesh Kumar K.V
     * start pointing to the new name
2440 2289be19 Aneesh Kumar K.V
     */
2441 2289be19 Aneesh Kumar K.V
    for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2442 2289be19 Aneesh Kumar K.V
        if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2443 2289be19 Aneesh Kumar K.V
            /* replace the name */
2444 2289be19 Aneesh Kumar K.V
            v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2445 8cf89e00 Anthony Liguori
        }
2446 8cf89e00 Anthony Liguori
    }
2447 c7b4b0b3 M. Mohan Kumar
out:
2448 84dfb926 Aneesh Kumar K.V
    if (dirfidp) {
2449 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, dirfidp);
2450 84dfb926 Aneesh Kumar K.V
    }
2451 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&new_path);
2452 84dfb926 Aneesh Kumar K.V
out_nofid:
2453 c7b4b0b3 M. Mohan Kumar
    return err;
2454 c7b4b0b3 M. Mohan Kumar
}
2455 c7b4b0b3 M. Mohan Kumar
2456 532decb7 Aneesh Kumar K.V
/* Only works with path name based fid */
2457 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_rename(void *opaque)
2458 c7b4b0b3 M. Mohan Kumar
{
2459 c7b4b0b3 M. Mohan Kumar
    int32_t fid;
2460 c7b4b0b3 M. Mohan Kumar
    ssize_t err = 0;
2461 930b1e17 Aneesh Kumar K.V
    size_t offset = 7;
2462 930b1e17 Aneesh Kumar K.V
    V9fsString name;
2463 930b1e17 Aneesh Kumar K.V
    int32_t newdirfid;
2464 930b1e17 Aneesh Kumar K.V
    V9fsFidState *fidp;
2465 930b1e17 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2466 930b1e17 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2467 c7b4b0b3 M. Mohan Kumar
2468 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2469 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2470 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2471 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2472 ddca7f86 M. Mohan Kumar
    }
2473 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2474 930b1e17 Aneesh Kumar K.V
    if (fidp == NULL) {
2475 c7b4b0b3 M. Mohan Kumar
        err = -ENOENT;
2476 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2477 c7b4b0b3 M. Mohan Kumar
    }
2478 930b1e17 Aneesh Kumar K.V
    BUG_ON(fidp->fid_type != P9_FID_NONE);
2479 532decb7 Aneesh Kumar K.V
    /* if fs driver is not path based, return EOPNOTSUPP */
2480 c98f1d4a Aneesh Kumar K.V
    if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2481 532decb7 Aneesh Kumar K.V
        err = -EOPNOTSUPP;
2482 532decb7 Aneesh Kumar K.V
        goto out;
2483 532decb7 Aneesh Kumar K.V
    }
2484 532decb7 Aneesh Kumar K.V
    v9fs_path_write_lock(s);
2485 bccacf6c Aneesh Kumar K.V
    err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2486 532decb7 Aneesh Kumar K.V
    v9fs_path_unlock(s);
2487 930b1e17 Aneesh Kumar K.V
    if (!err) {
2488 930b1e17 Aneesh Kumar K.V
        err = offset;
2489 930b1e17 Aneesh Kumar K.V
    }
2490 532decb7 Aneesh Kumar K.V
out:
2491 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2492 84dfb926 Aneesh Kumar K.V
out_nofid:
2493 930b1e17 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2494 930b1e17 Aneesh Kumar K.V
    v9fs_string_free(&name);
2495 c7b4b0b3 M. Mohan Kumar
}
2496 c7b4b0b3 M. Mohan Kumar
2497 bccacf6c Aneesh Kumar K.V
static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2498 2289be19 Aneesh Kumar K.V
                               V9fsString *old_name, V9fsPath *newdir,
2499 2289be19 Aneesh Kumar K.V
                               V9fsString *new_name)
2500 2289be19 Aneesh Kumar K.V
{
2501 2289be19 Aneesh Kumar K.V
    V9fsFidState *tfidp;
2502 2289be19 Aneesh Kumar K.V
    V9fsPath oldpath, newpath;
2503 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2504 2289be19 Aneesh Kumar K.V
2505 2289be19 Aneesh Kumar K.V
2506 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&oldpath);
2507 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&newpath);
2508 bccacf6c Aneesh Kumar K.V
    v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2509 bccacf6c Aneesh Kumar K.V
    v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2510 2289be19 Aneesh Kumar K.V
2511 2289be19 Aneesh Kumar K.V
    /*
2512 2289be19 Aneesh Kumar K.V
     * Fixup fid's pointing to the old name to
2513 2289be19 Aneesh Kumar K.V
     * start pointing to the new name
2514 2289be19 Aneesh Kumar K.V
     */
2515 2289be19 Aneesh Kumar K.V
    for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2516 2289be19 Aneesh Kumar K.V
        if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2517 2289be19 Aneesh Kumar K.V
            /* replace the name */
2518 2289be19 Aneesh Kumar K.V
            v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2519 2289be19 Aneesh Kumar K.V
        }
2520 2289be19 Aneesh Kumar K.V
    }
2521 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&oldpath);
2522 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&newpath);
2523 2289be19 Aneesh Kumar K.V
}
2524 2289be19 Aneesh Kumar K.V
2525 bccacf6c Aneesh Kumar K.V
static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2526 89bf6593 Aneesh Kumar K.V
                                  V9fsString *old_name, int32_t newdirfid,
2527 89bf6593 Aneesh Kumar K.V
                                  V9fsString *new_name)
2528 89bf6593 Aneesh Kumar K.V
{
2529 89bf6593 Aneesh Kumar K.V
    int err = 0;
2530 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2531 89bf6593 Aneesh Kumar K.V
    V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2532 89bf6593 Aneesh Kumar K.V
2533 bccacf6c Aneesh Kumar K.V
    olddirfidp = get_fid(pdu, olddirfid);
2534 89bf6593 Aneesh Kumar K.V
    if (olddirfidp == NULL) {
2535 89bf6593 Aneesh Kumar K.V
        err = -ENOENT;
2536 89bf6593 Aneesh Kumar K.V
        goto out;
2537 89bf6593 Aneesh Kumar K.V
    }
2538 89bf6593 Aneesh Kumar K.V
    if (newdirfid != -1) {
2539 bccacf6c Aneesh Kumar K.V
        newdirfidp = get_fid(pdu, newdirfid);
2540 89bf6593 Aneesh Kumar K.V
        if (newdirfidp == NULL) {
2541 89bf6593 Aneesh Kumar K.V
            err = -ENOENT;
2542 89bf6593 Aneesh Kumar K.V
            goto out;
2543 89bf6593 Aneesh Kumar K.V
        }
2544 89bf6593 Aneesh Kumar K.V
    } else {
2545 bccacf6c Aneesh Kumar K.V
        newdirfidp = get_fid(pdu, olddirfid);
2546 89bf6593 Aneesh Kumar K.V
    }
2547 89bf6593 Aneesh Kumar K.V
2548 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2549 2289be19 Aneesh Kumar K.V
                           &newdirfidp->path, new_name);
2550 2289be19 Aneesh Kumar K.V
    if (err < 0) {
2551 2289be19 Aneesh Kumar K.V
        goto out;
2552 89bf6593 Aneesh Kumar K.V
    }
2553 c98f1d4a Aneesh Kumar K.V
    if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2554 532decb7 Aneesh Kumar K.V
        /* Only for path based fid  we need to do the below fixup */
2555 bccacf6c Aneesh Kumar K.V
        v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2556 532decb7 Aneesh Kumar K.V
                           &newdirfidp->path, new_name);
2557 532decb7 Aneesh Kumar K.V
    }
2558 89bf6593 Aneesh Kumar K.V
out:
2559 89bf6593 Aneesh Kumar K.V
    if (olddirfidp) {
2560 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, olddirfidp);
2561 89bf6593 Aneesh Kumar K.V
    }
2562 89bf6593 Aneesh Kumar K.V
    if (newdirfidp) {
2563 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, newdirfidp);
2564 89bf6593 Aneesh Kumar K.V
    }
2565 89bf6593 Aneesh Kumar K.V
    return err;
2566 89bf6593 Aneesh Kumar K.V
}
2567 89bf6593 Aneesh Kumar K.V
2568 89bf6593 Aneesh Kumar K.V
static void v9fs_renameat(void *opaque)
2569 89bf6593 Aneesh Kumar K.V
{
2570 89bf6593 Aneesh Kumar K.V
    ssize_t err = 0;
2571 89bf6593 Aneesh Kumar K.V
    size_t offset = 7;
2572 89bf6593 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2573 89bf6593 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2574 89bf6593 Aneesh Kumar K.V
    int32_t olddirfid, newdirfid;
2575 89bf6593 Aneesh Kumar K.V
    V9fsString old_name, new_name;
2576 89bf6593 Aneesh Kumar K.V
2577 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&old_name);
2578 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&new_name);
2579 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2580 ddca7f86 M. Mohan Kumar
                        &old_name, &newdirfid, &new_name);
2581 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2582 ddca7f86 M. Mohan Kumar
        goto out_err;
2583 ddca7f86 M. Mohan Kumar
    }
2584 89bf6593 Aneesh Kumar K.V
2585 532decb7 Aneesh Kumar K.V
    v9fs_path_write_lock(s);
2586 bccacf6c Aneesh Kumar K.V
    err = v9fs_complete_renameat(pdu, olddirfid,
2587 bccacf6c Aneesh Kumar K.V
                                 &old_name, newdirfid, &new_name);
2588 532decb7 Aneesh Kumar K.V
    v9fs_path_unlock(s);
2589 89bf6593 Aneesh Kumar K.V
    if (!err) {
2590 89bf6593 Aneesh Kumar K.V
        err = offset;
2591 89bf6593 Aneesh Kumar K.V
    }
2592 ddca7f86 M. Mohan Kumar
2593 ddca7f86 M. Mohan Kumar
out_err:
2594 89bf6593 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2595 89bf6593 Aneesh Kumar K.V
    v9fs_string_free(&old_name);
2596 89bf6593 Aneesh Kumar K.V
    v9fs_string_free(&new_name);
2597 89bf6593 Aneesh Kumar K.V
}
2598 89bf6593 Aneesh Kumar K.V
2599 b81d685e Aneesh Kumar K.V
static void v9fs_wstat(void *opaque)
2600 8cf89e00 Anthony Liguori
{
2601 b81d685e Aneesh Kumar K.V
    int32_t fid;
2602 b81d685e Aneesh Kumar K.V
    int err = 0;
2603 b81d685e Aneesh Kumar K.V
    int16_t unused;
2604 b81d685e Aneesh Kumar K.V
    V9fsStat v9stat;
2605 b81d685e Aneesh Kumar K.V
    size_t offset = 7;
2606 b81d685e Aneesh Kumar K.V
    struct stat stbuf;
2607 b81d685e Aneesh Kumar K.V
    V9fsFidState *fidp;
2608 b81d685e Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2609 b81d685e Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2610 8cf89e00 Anthony Liguori
2611 ddca7f86 M. Mohan Kumar
    v9fs_stat_init(&v9stat);
2612 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2613 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2614 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2615 ddca7f86 M. Mohan Kumar
    }
2616 c572f23a Harsh Prateek Bora
    trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2617 c572f23a Harsh Prateek Bora
                     v9stat.mode, v9stat.atime, v9stat.mtime);
2618 84dfb926 Aneesh Kumar K.V
2619 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2620 b81d685e Aneesh Kumar K.V
    if (fidp == NULL) {
2621 b81d685e Aneesh Kumar K.V
        err = -EINVAL;
2622 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2623 8cf89e00 Anthony Liguori
    }
2624 b81d685e Aneesh Kumar K.V
    /* do we need to sync the file? */
2625 b81d685e Aneesh Kumar K.V
    if (donttouch_stat(&v9stat)) {
2626 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_fsync(pdu, fidp, 0);
2627 8cf89e00 Anthony Liguori
        goto out;
2628 8cf89e00 Anthony Liguori
    }
2629 b81d685e Aneesh Kumar K.V
    if (v9stat.mode != -1) {
2630 b81d685e Aneesh Kumar K.V
        uint32_t v9_mode;
2631 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2632 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2633 b81d685e Aneesh Kumar K.V
            goto out;
2634 b81d685e Aneesh Kumar K.V
        }
2635 b81d685e Aneesh Kumar K.V
        v9_mode = stat_to_v9mode(&stbuf);
2636 b81d685e Aneesh Kumar K.V
        if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2637 b81d685e Aneesh Kumar K.V
            (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2638 b81d685e Aneesh Kumar K.V
            /* Attempting to change the type */
2639 b81d685e Aneesh Kumar K.V
            err = -EIO;
2640 b81d685e Aneesh Kumar K.V
            goto out;
2641 b81d685e Aneesh Kumar K.V
        }
2642 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chmod(pdu, &fidp->path,
2643 b81d685e Aneesh Kumar K.V
                            v9mode_to_mode(v9stat.mode,
2644 b81d685e Aneesh Kumar K.V
                                           &v9stat.extension));
2645 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2646 b81d685e Aneesh Kumar K.V
            goto out;
2647 b81d685e Aneesh Kumar K.V
        }
2648 b81d685e Aneesh Kumar K.V
    }
2649 b81d685e Aneesh Kumar K.V
    if (v9stat.mtime != -1 || v9stat.atime != -1) {
2650 8fc39ae4 Sripathi Kodi
        struct timespec times[2];
2651 b81d685e Aneesh Kumar K.V
        if (v9stat.atime != -1) {
2652 b81d685e Aneesh Kumar K.V
            times[0].tv_sec = v9stat.atime;
2653 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = 0;
2654 8fc39ae4 Sripathi Kodi
        } else {
2655 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
2656 8fc39ae4 Sripathi Kodi
        }
2657 b81d685e Aneesh Kumar K.V
        if (v9stat.mtime != -1) {
2658 b81d685e Aneesh Kumar K.V
            times[1].tv_sec = v9stat.mtime;
2659 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = 0;
2660 8fc39ae4 Sripathi Kodi
        } else {
2661 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
2662 8fc39ae4 Sripathi Kodi
        }
2663 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_utimensat(pdu, &fidp->path, times);
2664 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2665 b81d685e Aneesh Kumar K.V
            goto out;
2666 8cf89e00 Anthony Liguori
        }
2667 8cf89e00 Anthony Liguori
    }
2668 b81d685e Aneesh Kumar K.V
    if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2669 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2670 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2671 8cf89e00 Anthony Liguori
            goto out;
2672 b81d685e Aneesh Kumar K.V
        }
2673 8cf89e00 Anthony Liguori
    }
2674 b81d685e Aneesh Kumar K.V
    if (v9stat.name.size != 0) {
2675 bccacf6c Aneesh Kumar K.V
        err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2676 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2677 b81d685e Aneesh Kumar K.V
            goto out;
2678 b81d685e Aneesh Kumar K.V
        }
2679 8cf89e00 Anthony Liguori
    }
2680 b81d685e Aneesh Kumar K.V
    if (v9stat.length != -1) {
2681 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2682 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2683 b81d685e Aneesh Kumar K.V
            goto out;
2684 b81d685e Aneesh Kumar K.V
        }
2685 8cf89e00 Anthony Liguori
    }
2686 b81d685e Aneesh Kumar K.V
    err = offset;
2687 8cf89e00 Anthony Liguori
out:
2688 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2689 84dfb926 Aneesh Kumar K.V
out_nofid:
2690 b81d685e Aneesh Kumar K.V
    v9fs_stat_free(&v9stat);
2691 b81d685e Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2692 9f107513 Anthony Liguori
}
2693 9f107513 Anthony Liguori
2694 88a4763e Aneesh Kumar K.V
static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2695 88a4763e Aneesh Kumar K.V
{
2696 88a4763e Aneesh Kumar K.V
    uint32_t f_type;
2697 88a4763e Aneesh Kumar K.V
    uint32_t f_bsize;
2698 88a4763e Aneesh Kumar K.V
    uint64_t f_blocks;
2699 88a4763e Aneesh Kumar K.V
    uint64_t f_bfree;
2700 88a4763e Aneesh Kumar K.V
    uint64_t f_bavail;
2701 88a4763e Aneesh Kumar K.V
    uint64_t f_files;
2702 88a4763e Aneesh Kumar K.V
    uint64_t f_ffree;
2703 88a4763e Aneesh Kumar K.V
    uint64_t fsid_val;
2704 88a4763e Aneesh Kumar K.V
    uint32_t f_namelen;
2705 88a4763e Aneesh Kumar K.V
    size_t offset = 7;
2706 5e94c103 M. Mohan Kumar
    int32_t bsize_factor;
2707 5e94c103 M. Mohan Kumar
2708 5e94c103 M. Mohan Kumar
    /*
2709 5e94c103 M. Mohan Kumar
     * compute bsize factor based on host file system block size
2710 5e94c103 M. Mohan Kumar
     * and client msize
2711 5e94c103 M. Mohan Kumar
     */
2712 88a4763e Aneesh Kumar K.V
    bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2713 5e94c103 M. Mohan Kumar
    if (!bsize_factor) {
2714 5e94c103 M. Mohan Kumar
        bsize_factor = 1;
2715 5e94c103 M. Mohan Kumar
    }
2716 88a4763e Aneesh Kumar K.V
    f_type  = stbuf->f_type;
2717 88a4763e Aneesh Kumar K.V
    f_bsize = stbuf->f_bsize;
2718 88a4763e Aneesh Kumar K.V
    f_bsize *= bsize_factor;
2719 5e94c103 M. Mohan Kumar
    /*
2720 5e94c103 M. Mohan Kumar
     * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2721 5e94c103 M. Mohan Kumar
     * adjust(divide) the number of blocks, free blocks and available
2722 5e94c103 M. Mohan Kumar
     * blocks by bsize factor
2723 5e94c103 M. Mohan Kumar
     */
2724 88a4763e Aneesh Kumar K.V
    f_blocks = stbuf->f_blocks/bsize_factor;
2725 88a4763e Aneesh Kumar K.V
    f_bfree  = stbuf->f_bfree/bsize_factor;
2726 88a4763e Aneesh Kumar K.V
    f_bavail = stbuf->f_bavail/bsize_factor;
2727 88a4763e Aneesh Kumar K.V
    f_files  = stbuf->f_files;
2728 88a4763e Aneesh Kumar K.V
    f_ffree  = stbuf->f_ffree;
2729 88a4763e Aneesh Kumar K.V
    fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2730 88a4763e Aneesh Kumar K.V
               (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2731 88a4763e Aneesh Kumar K.V
    f_namelen = stbuf->f_namelen;
2732 be940c87 M. Mohan Kumar
2733 88a4763e Aneesh Kumar K.V
    return pdu_marshal(pdu, offset, "ddqqqqqqd",
2734 88a4763e Aneesh Kumar K.V
                       f_type, f_bsize, f_blocks, f_bfree,
2735 88a4763e Aneesh Kumar K.V
                       f_bavail, f_files, f_ffree,
2736 88a4763e Aneesh Kumar K.V
                       fsid_val, f_namelen);
2737 be940c87 M. Mohan Kumar
}
2738 be940c87 M. Mohan Kumar
2739 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_statfs(void *opaque)
2740 be940c87 M. Mohan Kumar
{
2741 88a4763e Aneesh Kumar K.V
    int32_t fid;
2742 88a4763e Aneesh Kumar K.V
    ssize_t retval = 0;
2743 88a4763e Aneesh Kumar K.V
    size_t offset = 7;
2744 88a4763e Aneesh Kumar K.V
    V9fsFidState *fidp;
2745 88a4763e Aneesh Kumar K.V
    struct statfs stbuf;
2746 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2747 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2748 be940c87 M. Mohan Kumar
2749 ddca7f86 M. Mohan Kumar
    retval = pdu_unmarshal(pdu, offset, "d", &fid);
2750 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
2751 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2752 ddca7f86 M. Mohan Kumar
    }
2753 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2754 88a4763e Aneesh Kumar K.V
    if (fidp == NULL) {
2755 88a4763e Aneesh Kumar K.V
        retval = -ENOENT;
2756 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2757 be940c87 M. Mohan Kumar
    }
2758 bccacf6c Aneesh Kumar K.V
    retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2759 88a4763e Aneesh Kumar K.V
    if (retval < 0) {
2760 88a4763e Aneesh Kumar K.V
        goto out;
2761 88a4763e Aneesh Kumar K.V
    }
2762 ddca7f86 M. Mohan Kumar
    retval = v9fs_fill_statfs(s, pdu, &stbuf);
2763 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
2764 ddca7f86 M. Mohan Kumar
        goto out;
2765 ddca7f86 M. Mohan Kumar
    }
2766 ddca7f86 M. Mohan Kumar
    retval += offset;
2767 be940c87 M. Mohan Kumar
out:
2768 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2769 84dfb926 Aneesh Kumar K.V
out_nofid:
2770 88a4763e Aneesh Kumar K.V
    complete_pdu(s, pdu, retval);
2771 be940c87 M. Mohan Kumar
}
2772 be940c87 M. Mohan Kumar
2773 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_mknod(void *opaque)
2774 5268cecc M. Mohan Kumar
{
2775 1b733fed Aneesh Kumar K.V
2776 1b733fed Aneesh Kumar K.V
    int mode;
2777 1b733fed Aneesh Kumar K.V
    gid_t gid;
2778 5268cecc M. Mohan Kumar
    int32_t fid;
2779 1b733fed Aneesh Kumar K.V
    V9fsQID qid;
2780 5268cecc M. Mohan Kumar
    int err = 0;
2781 5268cecc M. Mohan Kumar
    int major, minor;
2782 1b733fed Aneesh Kumar K.V
    size_t offset = 7;
2783 1b733fed Aneesh Kumar K.V
    V9fsString name;
2784 1b733fed Aneesh Kumar K.V
    struct stat stbuf;
2785 1b733fed Aneesh Kumar K.V
    V9fsFidState *fidp;
2786 1b733fed Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2787 1b733fed Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2788 5268cecc M. Mohan Kumar
2789 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2790 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2791 ddca7f86 M. Mohan Kumar
                        &major, &minor, &gid);
2792 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2793 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2794 ddca7f86 M. Mohan Kumar
    }
2795 c572f23a Harsh Prateek Bora
    trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2796 5268cecc M. Mohan Kumar
2797 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2798 5268cecc M. Mohan Kumar
    if (fidp == NULL) {
2799 5268cecc M. Mohan Kumar
        err = -ENOENT;
2800 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2801 5268cecc M. Mohan Kumar
    }
2802 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
2803 02cb7f3a Aneesh Kumar K.V
                        makedev(major, minor), mode, &stbuf);
2804 1b733fed Aneesh Kumar K.V
    if (err < 0) {
2805 1b733fed Aneesh Kumar K.V
        goto out;
2806 1b733fed Aneesh Kumar K.V
    }
2807 1b733fed Aneesh Kumar K.V
    stat_to_qid(&stbuf, &qid);
2808 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Q", &qid);
2809 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2810 ddca7f86 M. Mohan Kumar
        goto out;
2811 ddca7f86 M. Mohan Kumar
    }
2812 ddca7f86 M. Mohan Kumar
    err += offset;
2813 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_mknod_return(pdu->tag, pdu->id,
2814 7999f7e1 Aneesh Kumar K.V
                            qid.type, qid.version, qid.path);
2815 5268cecc M. Mohan Kumar
out:
2816 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2817 84dfb926 Aneesh Kumar K.V
out_nofid:
2818 1b733fed Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2819 1b733fed Aneesh Kumar K.V
    v9fs_string_free(&name);
2820 5268cecc M. Mohan Kumar
}
2821 5268cecc M. Mohan Kumar
2822 82cc3ee8 M. Mohan Kumar
/*
2823 82cc3ee8 M. Mohan Kumar
 * Implement posix byte range locking code
2824 82cc3ee8 M. Mohan Kumar
 * Server side handling of locking code is very simple, because 9p server in
2825 82cc3ee8 M. Mohan Kumar
 * QEMU can handle only one client. And most of the lock handling
2826 82cc3ee8 M. Mohan Kumar
 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2827 82cc3ee8 M. Mohan Kumar
 * do any thing in * qemu 9p server side lock code path.
2828 82cc3ee8 M. Mohan Kumar
 * So when a TLOCK request comes, always return success
2829 82cc3ee8 M. Mohan Kumar
 */
2830 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_lock(void *opaque)
2831 82cc3ee8 M. Mohan Kumar
{
2832 0c27bf2a Aneesh Kumar K.V
    int8_t status;
2833 ddca7f86 M. Mohan Kumar
    V9fsFlock flock;
2834 0c27bf2a Aneesh Kumar K.V
    size_t offset = 7;
2835 0c27bf2a Aneesh Kumar K.V
    struct stat stbuf;
2836 0c27bf2a Aneesh Kumar K.V
    V9fsFidState *fidp;
2837 0c27bf2a Aneesh Kumar K.V
    int32_t fid, err = 0;
2838 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2839 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2840 82cc3ee8 M. Mohan Kumar
2841 ddca7f86 M. Mohan Kumar
    status = P9_LOCK_ERROR;
2842 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&flock.client_id);
2843 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
2844 ddca7f86 M. Mohan Kumar
                        &flock.flags, &flock.start, &flock.length,
2845 ddca7f86 M. Mohan Kumar
                        &flock.proc_id, &flock.client_id);
2846 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2847 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2848 ddca7f86 M. Mohan Kumar
    }
2849 c572f23a Harsh Prateek Bora
    trace_v9fs_lock(pdu->tag, pdu->id, fid,
2850 ddca7f86 M. Mohan Kumar
                    flock.type, flock.start, flock.length);
2851 c572f23a Harsh Prateek Bora
2852 82cc3ee8 M. Mohan Kumar
2853 82cc3ee8 M. Mohan Kumar
    /* We support only block flag now (that too ignored currently) */
2854 ddca7f86 M. Mohan Kumar
    if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
2855 82cc3ee8 M. Mohan Kumar
        err = -EINVAL;
2856 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2857 82cc3ee8 M. Mohan Kumar
    }
2858 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2859 0c27bf2a Aneesh Kumar K.V
    if (fidp == NULL) {
2860 82cc3ee8 M. Mohan Kumar
        err = -ENOENT;
2861 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2862 82cc3ee8 M. Mohan Kumar
    }
2863 cc720ddb Aneesh Kumar K.V
    err = v9fs_co_fstat(pdu, fidp, &stbuf);
2864 82cc3ee8 M. Mohan Kumar
    if (err < 0) {
2865 82cc3ee8 M. Mohan Kumar
        goto out;
2866 82cc3ee8 M. Mohan Kumar
    }
2867 0c27bf2a Aneesh Kumar K.V
    status = P9_LOCK_SUCCESS;
2868 82cc3ee8 M. Mohan Kumar
out:
2869 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2870 84dfb926 Aneesh Kumar K.V
out_nofid:
2871 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "b", status);
2872 ddca7f86 M. Mohan Kumar
    if (err > 0) {
2873 ddca7f86 M. Mohan Kumar
        err += offset;
2874 ddca7f86 M. Mohan Kumar
    }
2875 c572f23a Harsh Prateek Bora
    trace_v9fs_lock_return(pdu->tag, pdu->id, status);
2876 0c27bf2a Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2877 ddca7f86 M. Mohan Kumar
    v9fs_string_free(&flock.client_id);
2878 82cc3ee8 M. Mohan Kumar
}
2879 82cc3ee8 M. Mohan Kumar
2880 8f354003 M. Mohan Kumar
/*
2881 8f354003 M. Mohan Kumar
 * When a TGETLOCK request comes, always return success because all lock
2882 8f354003 M. Mohan Kumar
 * handling is done by client's VFS layer.
2883 8f354003 M. Mohan Kumar
 */
2884 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_getlock(void *opaque)
2885 8f354003 M. Mohan Kumar
{
2886 e4e414a4 Aneesh Kumar K.V
    size_t offset = 7;
2887 e4e414a4 Aneesh Kumar K.V
    struct stat stbuf;
2888 e4e414a4 Aneesh Kumar K.V
    V9fsFidState *fidp;
2889 ddca7f86 M. Mohan Kumar
    V9fsGetlock glock;
2890 e4e414a4 Aneesh Kumar K.V
    int32_t fid, err = 0;
2891 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2892 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2893 8f354003 M. Mohan Kumar
2894 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&glock.client_id);
2895 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
2896 ddca7f86 M. Mohan Kumar
                        &glock.start, &glock.length, &glock.proc_id,
2897 ddca7f86 M. Mohan Kumar
                        &glock.client_id);
2898 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2899 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2900 ddca7f86 M. Mohan Kumar
    }
2901 c572f23a Harsh Prateek Bora
    trace_v9fs_getlock(pdu->tag, pdu->id, fid,
2902 ddca7f86 M. Mohan Kumar
                       glock.type, glock.start, glock.length);
2903 c572f23a Harsh Prateek Bora
2904 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2905 e4e414a4 Aneesh Kumar K.V
    if (fidp == NULL) {
2906 8f354003 M. Mohan Kumar
        err = -ENOENT;
2907 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2908 8f354003 M. Mohan Kumar
    }
2909 cc720ddb Aneesh Kumar K.V
    err = v9fs_co_fstat(pdu, fidp, &stbuf);
2910 8f354003 M. Mohan Kumar
    if (err < 0) {
2911 8f354003 M. Mohan Kumar
        goto out;
2912 8f354003 M. Mohan Kumar
    }
2913 ddca7f86 M. Mohan Kumar
    glock.type = P9_LOCK_TYPE_UNLCK;
2914 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "bqqds", glock.type,
2915 ddca7f86 M. Mohan Kumar
                          glock.start, glock.length, glock.proc_id,
2916 ddca7f86 M. Mohan Kumar
                          &glock.client_id);
2917 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2918 ddca7f86 M. Mohan Kumar
        goto out;
2919 ddca7f86 M. Mohan Kumar
    }
2920 ddca7f86 M. Mohan Kumar
    err += offset;
2921 ddca7f86 M. Mohan Kumar
    trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
2922 ddca7f86 M. Mohan Kumar
                              glock.length, glock.proc_id);
2923 8f354003 M. Mohan Kumar
out:
2924 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2925 84dfb926 Aneesh Kumar K.V
out_nofid:
2926 e4e414a4 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2927 ddca7f86 M. Mohan Kumar
    v9fs_string_free(&glock.client_id);
2928 8f354003 M. Mohan Kumar
}
2929 8f354003 M. Mohan Kumar
2930 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_mkdir(void *opaque)
2931 b67592ea M. Mohan Kumar
{
2932 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2933 e84861f7 Venkateswararao Jujjuri
    size_t offset = 7;
2934 b67592ea M. Mohan Kumar
    int32_t fid;
2935 e84861f7 Venkateswararao Jujjuri
    struct stat stbuf;
2936 e84861f7 Venkateswararao Jujjuri
    V9fsQID qid;
2937 02cb7f3a Aneesh Kumar K.V
    V9fsString name;
2938 b67592ea M. Mohan Kumar
    V9fsFidState *fidp;
2939 b67592ea M. Mohan Kumar
    gid_t gid;
2940 b67592ea M. Mohan Kumar
    int mode;
2941 e84861f7 Venkateswararao Jujjuri
    int err = 0;
2942 b67592ea M. Mohan Kumar
2943 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2944 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
2945 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2946 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2947 ddca7f86 M. Mohan Kumar
    }
2948 c572f23a Harsh Prateek Bora
    trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
2949 c572f23a Harsh Prateek Bora
2950 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2951 b67592ea M. Mohan Kumar
    if (fidp == NULL) {
2952 b67592ea M. Mohan Kumar
        err = -ENOENT;
2953 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2954 b67592ea M. Mohan Kumar
    }
2955 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
2956 e84861f7 Venkateswararao Jujjuri
    if (err < 0) {
2957 e84861f7 Venkateswararao Jujjuri
        goto out;
2958 e84861f7 Venkateswararao Jujjuri
    }
2959 e84861f7 Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
2960 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Q", &qid);
2961 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2962 ddca7f86 M. Mohan Kumar
        goto out;
2963 ddca7f86 M. Mohan Kumar
    }
2964 ddca7f86 M. Mohan Kumar
    err += offset;
2965 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_mkdir_return(pdu->tag, pdu->id,
2966 7999f7e1 Aneesh Kumar K.V
                            qid.type, qid.version, qid.path, err);
2967 b67592ea M. Mohan Kumar
out:
2968 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2969 84dfb926 Aneesh Kumar K.V
out_nofid:
2970 e84861f7 Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
2971 e84861f7 Venkateswararao Jujjuri
    v9fs_string_free(&name);
2972 b67592ea M. Mohan Kumar
}
2973 b67592ea M. Mohan Kumar
2974 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_xattrwalk(void *opaque)
2975 fa32ef88 Aneesh Kumar K.V
{
2976 670185a6 Aneesh Kumar K.V
    int64_t size;
2977 670185a6 Aneesh Kumar K.V
    V9fsString name;
2978 fa32ef88 Aneesh Kumar K.V
    ssize_t err = 0;
2979 670185a6 Aneesh Kumar K.V
    size_t offset = 7;
2980 fa32ef88 Aneesh Kumar K.V
    int32_t fid, newfid;
2981 670185a6 Aneesh Kumar K.V
    V9fsFidState *file_fidp;
2982 84dfb926 Aneesh Kumar K.V
    V9fsFidState *xattr_fidp = NULL;
2983 670185a6 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2984 670185a6 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2985 fa32ef88 Aneesh Kumar K.V
2986 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2987 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
2988 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2989 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2990 ddca7f86 M. Mohan Kumar
    }
2991 c572f23a Harsh Prateek Bora
    trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
2992 c572f23a Harsh Prateek Bora
2993 bccacf6c Aneesh Kumar K.V
    file_fidp = get_fid(pdu, fid);
2994 670185a6 Aneesh Kumar K.V
    if (file_fidp == NULL) {
2995 fa32ef88 Aneesh Kumar K.V
        err = -ENOENT;
2996 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2997 fa32ef88 Aneesh Kumar K.V
    }
2998 670185a6 Aneesh Kumar K.V
    xattr_fidp = alloc_fid(s, newfid);
2999 670185a6 Aneesh Kumar K.V
    if (xattr_fidp == NULL) {
3000 fa32ef88 Aneesh Kumar K.V
        err = -EINVAL;
3001 fa32ef88 Aneesh Kumar K.V
        goto out;
3002 fa32ef88 Aneesh Kumar K.V
    }
3003 2289be19 Aneesh Kumar K.V
    v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3004 ddca7f86 M. Mohan Kumar
    if (name.data == NULL) {
3005 fa32ef88 Aneesh Kumar K.V
        /*
3006 fa32ef88 Aneesh Kumar K.V
         * listxattr request. Get the size first
3007 fa32ef88 Aneesh Kumar K.V
         */
3008 bccacf6c Aneesh Kumar K.V
        size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3009 670185a6 Aneesh Kumar K.V
        if (size < 0) {
3010 670185a6 Aneesh Kumar K.V
            err = size;
3011 84dfb926 Aneesh Kumar K.V
            clunk_fid(s, xattr_fidp->fid);
3012 670185a6 Aneesh Kumar K.V
            goto out;
3013 fa32ef88 Aneesh Kumar K.V
        }
3014 670185a6 Aneesh Kumar K.V
        /*
3015 670185a6 Aneesh Kumar K.V
         * Read the xattr value
3016 670185a6 Aneesh Kumar K.V
         */
3017 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.len = size;
3018 670185a6 Aneesh Kumar K.V
        xattr_fidp->fid_type = P9_FID_XATTR;
3019 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.copied_len = -1;
3020 670185a6 Aneesh Kumar K.V
        if (size) {
3021 7267c094 Anthony Liguori
            xattr_fidp->fs.xattr.value = g_malloc(size);
3022 bccacf6c Aneesh Kumar K.V
            err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3023 670185a6 Aneesh Kumar K.V
                                     xattr_fidp->fs.xattr.value,
3024 670185a6 Aneesh Kumar K.V
                                     xattr_fidp->fs.xattr.len);
3025 670185a6 Aneesh Kumar K.V
            if (err < 0) {
3026 84dfb926 Aneesh Kumar K.V
                clunk_fid(s, xattr_fidp->fid);
3027 670185a6 Aneesh Kumar K.V
                goto out;
3028 670185a6 Aneesh Kumar K.V
            }
3029 670185a6 Aneesh Kumar K.V
        }
3030 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "q", size);
3031 ddca7f86 M. Mohan Kumar
        if (err < 0) {
3032 ddca7f86 M. Mohan Kumar
            goto out;
3033 ddca7f86 M. Mohan Kumar
        }
3034 ddca7f86 M. Mohan Kumar
        err += offset;
3035 fa32ef88 Aneesh Kumar K.V
    } else {
3036 fa32ef88 Aneesh Kumar K.V
        /*
3037 fa32ef88 Aneesh Kumar K.V
         * specific xattr fid. We check for xattr
3038 fa32ef88 Aneesh Kumar K.V
         * presence also collect the xattr size
3039 fa32ef88 Aneesh Kumar K.V
         */
3040 bccacf6c Aneesh Kumar K.V
        size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3041 670185a6 Aneesh Kumar K.V
                                 &name, NULL, 0);
3042 670185a6 Aneesh Kumar K.V
        if (size < 0) {
3043 670185a6 Aneesh Kumar K.V
            err = size;
3044 84dfb926 Aneesh Kumar K.V
            clunk_fid(s, xattr_fidp->fid);
3045 670185a6 Aneesh Kumar K.V
            goto out;
3046 fa32ef88 Aneesh Kumar K.V
        }
3047 670185a6 Aneesh Kumar K.V
        /*
3048 670185a6 Aneesh Kumar K.V
         * Read the xattr value
3049 670185a6 Aneesh Kumar K.V
         */
3050 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.len = size;
3051 670185a6 Aneesh Kumar K.V
        xattr_fidp->fid_type = P9_FID_XATTR;
3052 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.copied_len = -1;
3053 670185a6 Aneesh Kumar K.V
        if (size) {
3054 7267c094 Anthony Liguori
            xattr_fidp->fs.xattr.value = g_malloc(size);
3055 bccacf6c Aneesh Kumar K.V
            err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3056 670185a6 Aneesh Kumar K.V
                                    &name, xattr_fidp->fs.xattr.value,
3057 670185a6 Aneesh Kumar K.V
                                    xattr_fidp->fs.xattr.len);
3058 670185a6 Aneesh Kumar K.V
            if (err < 0) {
3059 84dfb926 Aneesh Kumar K.V
                clunk_fid(s, xattr_fidp->fid);
3060 670185a6 Aneesh Kumar K.V
                goto out;
3061 670185a6 Aneesh Kumar K.V
            }
3062 670185a6 Aneesh Kumar K.V
        }
3063 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "q", size);
3064 ddca7f86 M. Mohan Kumar
        if (err < 0) {
3065 ddca7f86 M. Mohan Kumar
            goto out;
3066 ddca7f86 M. Mohan Kumar
        }
3067 ddca7f86 M. Mohan Kumar
        err += offset;
3068 fa32ef88 Aneesh Kumar K.V
    }
3069 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3070 fa32ef88 Aneesh Kumar K.V
out:
3071 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, file_fidp);
3072 84dfb926 Aneesh Kumar K.V
    if (xattr_fidp) {
3073 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, xattr_fidp);
3074 84dfb926 Aneesh Kumar K.V
    }
3075 84dfb926 Aneesh Kumar K.V
out_nofid:
3076 670185a6 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
3077 670185a6 Aneesh Kumar K.V
    v9fs_string_free(&name);
3078 fa32ef88 Aneesh Kumar K.V
}
3079 fa32ef88 Aneesh Kumar K.V
3080 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_xattrcreate(void *opaque)
3081 10b468bd Aneesh Kumar K.V
{
3082 10b468bd Aneesh Kumar K.V
    int flags;
3083 10b468bd Aneesh Kumar K.V
    int32_t fid;
3084 f10ff58d Aneesh Kumar K.V
    int64_t size;
3085 10b468bd Aneesh Kumar K.V
    ssize_t err = 0;
3086 f10ff58d Aneesh Kumar K.V
    V9fsString name;
3087 f10ff58d Aneesh Kumar K.V
    size_t offset = 7;
3088 f10ff58d Aneesh Kumar K.V
    V9fsFidState *file_fidp;
3089 f10ff58d Aneesh Kumar K.V
    V9fsFidState *xattr_fidp;
3090 f10ff58d Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
3091 f10ff58d Aneesh Kumar K.V
    V9fsState *s = pdu->s;
3092 10b468bd Aneesh Kumar K.V
3093 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
3094 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3095 ddca7f86 M. Mohan Kumar
    if (err < 0) {
3096 ddca7f86 M. Mohan Kumar
        goto out_nofid;
3097 ddca7f86 M. Mohan Kumar
    }
3098 c572f23a Harsh Prateek Bora
    trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3099 10b468bd Aneesh Kumar K.V
3100 bccacf6c Aneesh Kumar K.V
    file_fidp = get_fid(pdu, fid);
3101 f10ff58d Aneesh Kumar K.V
    if (file_fidp == NULL) {
3102 10b468bd Aneesh Kumar K.V
        err = -EINVAL;
3103 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
3104 10b468bd Aneesh Kumar K.V
    }
3105 10b468bd Aneesh Kumar K.V
    /* Make the file fid point to xattr */
3106 f10ff58d Aneesh Kumar K.V
    xattr_fidp = file_fidp;
3107 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fid_type = P9_FID_XATTR;
3108 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fs.xattr.copied_len = 0;
3109 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fs.xattr.len = size;
3110 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fs.xattr.flags = flags;
3111 f10ff58d Aneesh Kumar K.V
    v9fs_string_init(&xattr_fidp->fs.xattr.name);
3112 f10ff58d Aneesh Kumar K.V
    v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3113 6528499f Markus Armbruster
    xattr_fidp->fs.xattr.value = g_malloc(size);
3114 f10ff58d Aneesh Kumar K.V
    err = offset;
3115 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, file_fidp);
3116 84dfb926 Aneesh Kumar K.V
out_nofid:
3117 f10ff58d Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
3118 f10ff58d Aneesh Kumar K.V
    v9fs_string_free(&name);
3119 10b468bd Aneesh Kumar K.V
}
3120 fa32ef88 Aneesh Kumar K.V
3121 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_readlink(void *opaque)
3122 df0973a4 M. Mohan Kumar
{
3123 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
3124 7a5ca31e Venkateswararao Jujjuri
    size_t offset = 7;
3125 7a5ca31e Venkateswararao Jujjuri
    V9fsString target;
3126 df0973a4 M. Mohan Kumar
    int32_t fid;
3127 df0973a4 M. Mohan Kumar
    int err = 0;
3128 df0973a4 M. Mohan Kumar
    V9fsFidState *fidp;
3129 df0973a4 M. Mohan Kumar
3130 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "d", &fid);
3131 ddca7f86 M. Mohan Kumar
    if (err < 0) {
3132 ddca7f86 M. Mohan Kumar
        goto out_nofid;
3133 ddca7f86 M. Mohan Kumar
    }
3134 c572f23a Harsh Prateek Bora
    trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3135 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
3136 df0973a4 M. Mohan Kumar
    if (fidp == NULL) {
3137 df0973a4 M. Mohan Kumar
        err = -ENOENT;
3138 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
3139 df0973a4 M. Mohan Kumar
    }
3140 df0973a4 M. Mohan Kumar
3141 7a5ca31e Venkateswararao Jujjuri
    v9fs_string_init(&target);
3142 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_readlink(pdu, &fidp->path, &target);
3143 7a5ca31e Venkateswararao Jujjuri
    if (err < 0) {
3144 7a5ca31e Venkateswararao Jujjuri
        goto out;
3145 7a5ca31e Venkateswararao Jujjuri
    }
3146 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "s", &target);
3147 ddca7f86 M. Mohan Kumar
    if (err < 0) {
3148 ddca7f86 M. Mohan Kumar
        v9fs_string_free(&target);
3149 ddca7f86 M. Mohan Kumar
        goto out;
3150 ddca7f86 M. Mohan Kumar
    }
3151 ddca7f86 M. Mohan Kumar
    err += offset;
3152 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3153 7a5ca31e Venkateswararao Jujjuri
    v9fs_string_free(&target);
3154 df0973a4 M. Mohan Kumar
out:
3155 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
3156 84dfb926 Aneesh Kumar K.V
out_nofid:
3157 7a5ca31e Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
3158 df0973a4 M. Mohan Kumar
}
3159 df0973a4 M. Mohan Kumar
3160 ff06030f Venkateswararao Jujjuri (JV)
static CoroutineEntry *pdu_co_handlers[] = {
3161 c18e2f94 Sripathi Kodi
    [P9_TREADDIR] = v9fs_readdir,
3162 be940c87 M. Mohan Kumar
    [P9_TSTATFS] = v9fs_statfs,
3163 00ede4c2 Sripathi Kodi
    [P9_TGETATTR] = v9fs_getattr,
3164 c79ce737 Sripathi Kodi
    [P9_TSETATTR] = v9fs_setattr,
3165 fa32ef88 Aneesh Kumar K.V
    [P9_TXATTRWALK] = v9fs_xattrwalk,
3166 10b468bd Aneesh Kumar K.V
    [P9_TXATTRCREATE] = v9fs_xattrcreate,
3167 5268cecc M. Mohan Kumar
    [P9_TMKNOD] = v9fs_mknod,
3168 c7b4b0b3 M. Mohan Kumar
    [P9_TRENAME] = v9fs_rename,
3169 82cc3ee8 M. Mohan Kumar
    [P9_TLOCK] = v9fs_lock,
3170 8f354003 M. Mohan Kumar
    [P9_TGETLOCK] = v9fs_getlock,
3171 89bf6593 Aneesh Kumar K.V
    [P9_TRENAMEAT] = v9fs_renameat,
3172 df0973a4 M. Mohan Kumar
    [P9_TREADLINK] = v9fs_readlink,
3173 7834cf77 Aneesh Kumar K.V
    [P9_TUNLINKAT] = v9fs_unlinkat,
3174 b67592ea M. Mohan Kumar
    [P9_TMKDIR] = v9fs_mkdir,
3175 9f107513 Anthony Liguori
    [P9_TVERSION] = v9fs_version,
3176 771e9d4c M. Mohan Kumar
    [P9_TLOPEN] = v9fs_open,
3177 9f107513 Anthony Liguori
    [P9_TATTACH] = v9fs_attach,
3178 9f107513 Anthony Liguori
    [P9_TSTAT] = v9fs_stat,
3179 9f107513 Anthony Liguori
    [P9_TWALK] = v9fs_walk,
3180 9f107513 Anthony Liguori
    [P9_TCLUNK] = v9fs_clunk,
3181 b41e95d3 Venkateswararao Jujjuri (JV)
    [P9_TFSYNC] = v9fs_fsync,
3182 9f107513 Anthony Liguori
    [P9_TOPEN] = v9fs_open,
3183 9f107513 Anthony Liguori
    [P9_TREAD] = v9fs_read,
3184 9f107513 Anthony Liguori
#if 0
3185 9f107513 Anthony Liguori
    [P9_TAUTH] = v9fs_auth,
3186 9f107513 Anthony Liguori
#endif
3187 9f107513 Anthony Liguori
    [P9_TFLUSH] = v9fs_flush,
3188 b2c224be Venkateswararao Jujjuri (JV)
    [P9_TLINK] = v9fs_link,
3189 08c60fc9 Venkateswararao Jujjuri (JV)
    [P9_TSYMLINK] = v9fs_symlink,
3190 9f107513 Anthony Liguori
    [P9_TCREATE] = v9fs_create,
3191 c1568af5 Venkateswararao Jujjuri (JV)
    [P9_TLCREATE] = v9fs_lcreate,
3192 9f107513 Anthony Liguori
    [P9_TWRITE] = v9fs_write,
3193 9f107513 Anthony Liguori
    [P9_TWSTAT] = v9fs_wstat,
3194 9f107513 Anthony Liguori
    [P9_TREMOVE] = v9fs_remove,
3195 9f107513 Anthony Liguori
};
3196 9f107513 Anthony Liguori
3197 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_op_not_supp(void *opaque)
3198 5c3234c6 Aneesh Kumar K.V
{
3199 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
3200 ff06030f Venkateswararao Jujjuri (JV)
    complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
3201 5c3234c6 Aneesh Kumar K.V
}
3202 5c3234c6 Aneesh Kumar K.V
3203 2c74c2cb M. Mohan Kumar
static void v9fs_fs_ro(void *opaque)
3204 2c74c2cb M. Mohan Kumar
{
3205 2c74c2cb M. Mohan Kumar
    V9fsPDU *pdu = opaque;
3206 2c74c2cb M. Mohan Kumar
    complete_pdu(pdu->s, pdu, -EROFS);
3207 2c74c2cb M. Mohan Kumar
}
3208 2c74c2cb M. Mohan Kumar
3209 2c74c2cb M. Mohan Kumar
static inline bool is_read_only_op(V9fsPDU *pdu)
3210 2c74c2cb M. Mohan Kumar
{
3211 2c74c2cb M. Mohan Kumar
    switch (pdu->id) {
3212 2c74c2cb M. Mohan Kumar
    case P9_TREADDIR:
3213 2c74c2cb M. Mohan Kumar
    case P9_TSTATFS:
3214 2c74c2cb M. Mohan Kumar
    case P9_TGETATTR:
3215 2c74c2cb M. Mohan Kumar
    case P9_TXATTRWALK:
3216 2c74c2cb M. Mohan Kumar
    case P9_TLOCK:
3217 2c74c2cb M. Mohan Kumar
    case P9_TGETLOCK:
3218 2c74c2cb M. Mohan Kumar
    case P9_TREADLINK:
3219 2c74c2cb M. Mohan Kumar
    case P9_TVERSION:
3220 2c74c2cb M. Mohan Kumar
    case P9_TLOPEN:
3221 2c74c2cb M. Mohan Kumar
    case P9_TATTACH:
3222 2c74c2cb M. Mohan Kumar
    case P9_TSTAT:
3223 2c74c2cb M. Mohan Kumar
    case P9_TWALK:
3224 2c74c2cb M. Mohan Kumar
    case P9_TCLUNK:
3225 2c74c2cb M. Mohan Kumar
    case P9_TFSYNC:
3226 2c74c2cb M. Mohan Kumar
    case P9_TOPEN:
3227 2c74c2cb M. Mohan Kumar
    case P9_TREAD:
3228 2c74c2cb M. Mohan Kumar
    case P9_TAUTH:
3229 2c74c2cb M. Mohan Kumar
    case P9_TFLUSH:
3230 2c74c2cb M. Mohan Kumar
        return 1;
3231 2c74c2cb M. Mohan Kumar
    default:
3232 2c74c2cb M. Mohan Kumar
        return 0;
3233 2c74c2cb M. Mohan Kumar
    }
3234 2c74c2cb M. Mohan Kumar
}
3235 2c74c2cb M. Mohan Kumar
3236 9f107513 Anthony Liguori
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3237 9f107513 Anthony Liguori
{
3238 ff06030f Venkateswararao Jujjuri (JV)
    Coroutine *co;
3239 ff06030f Venkateswararao Jujjuri (JV)
    CoroutineEntry *handler;
3240 9f107513 Anthony Liguori
3241 ff06030f Venkateswararao Jujjuri (JV)
    if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3242 ff06030f Venkateswararao Jujjuri (JV)
        (pdu_co_handlers[pdu->id] == NULL)) {
3243 5c3234c6 Aneesh Kumar K.V
        handler = v9fs_op_not_supp;
3244 5c3234c6 Aneesh Kumar K.V
    } else {
3245 ff06030f Venkateswararao Jujjuri (JV)
        handler = pdu_co_handlers[pdu->id];
3246 5c3234c6 Aneesh Kumar K.V
    }
3247 2c74c2cb M. Mohan Kumar
3248 2c74c2cb M. Mohan Kumar
    if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3249 2c74c2cb M. Mohan Kumar
        handler = v9fs_fs_ro;
3250 2c74c2cb M. Mohan Kumar
    }
3251 ff06030f Venkateswararao Jujjuri (JV)
    co = qemu_coroutine_create(handler);
3252 ff06030f Venkateswararao Jujjuri (JV)
    qemu_coroutine_enter(co, pdu);
3253 9f107513 Anthony Liguori
}
3254 9f107513 Anthony Liguori
3255 f4f61d27 Aneesh Kumar K.V
void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3256 9f107513 Anthony Liguori
{
3257 9f107513 Anthony Liguori
    V9fsState *s = (V9fsState *)vdev;
3258 9f107513 Anthony Liguori
    V9fsPDU *pdu;
3259 9f107513 Anthony Liguori
    ssize_t len;
3260 9f107513 Anthony Liguori
3261 9f107513 Anthony Liguori
    while ((pdu = alloc_pdu(s)) &&
3262 9f107513 Anthony Liguori
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3263 9f107513 Anthony Liguori
        uint8_t *ptr;
3264 ff06030f Venkateswararao Jujjuri (JV)
        pdu->s = s;
3265 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3266 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3267 9f107513 Anthony Liguori
3268 9f107513 Anthony Liguori
        ptr = pdu->elem.out_sg[0].iov_base;
3269 9f107513 Anthony Liguori
3270 67d6fa53 Benjamin Herrenschmidt
        pdu->size = le32_to_cpu(*(uint32_t *)ptr);
3271 9f107513 Anthony Liguori
        pdu->id = ptr[4];
3272 67d6fa53 Benjamin Herrenschmidt
        pdu->tag = le16_to_cpu(*(uint16_t *)(ptr + 5));
3273 bccacf6c Aneesh Kumar K.V
        qemu_co_queue_init(&pdu->complete);
3274 9f107513 Anthony Liguori
        submit_pdu(s, pdu);
3275 9f107513 Anthony Liguori
    }
3276 9f107513 Anthony Liguori
    free_pdu(s, pdu);
3277 9f107513 Anthony Liguori
}
3278 7a462745 Aneesh Kumar K.V
3279 60653b28 Paolo Bonzini
static void __attribute__((__constructor__)) virtio_9p_set_fd_limit(void)
3280 7a462745 Aneesh Kumar K.V
{
3281 7a462745 Aneesh Kumar K.V
    struct rlimit rlim;
3282 7a462745 Aneesh Kumar K.V
    if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3283 7a462745 Aneesh Kumar K.V
        fprintf(stderr, "Failed to get the resource limit\n");
3284 7a462745 Aneesh Kumar K.V
        exit(1);
3285 7a462745 Aneesh Kumar K.V
    }
3286 7a462745 Aneesh Kumar K.V
    open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3287 7a462745 Aneesh Kumar K.V
    open_fd_rc = rlim.rlim_cur/2;
3288 7a462745 Aneesh Kumar K.V
}