Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (84.5 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 e06a765e Harsh Prateek Bora
        if (retval < 0) {
1084 e06a765e Harsh Prateek Bora
            goto out;
1085 e06a765e Harsh Prateek Bora
        }
1086 e06a765e Harsh Prateek Bora
        v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1087 e06a765e Harsh Prateek Bora
    }
1088 ddca7f86 M. Mohan Kumar
    retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1089 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
1090 ddca7f86 M. Mohan Kumar
        goto out;
1091 ddca7f86 M. Mohan Kumar
    }
1092 ddca7f86 M. Mohan Kumar
    retval += offset;
1093 c572f23a Harsh Prateek Bora
    trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1094 c572f23a Harsh Prateek Bora
                              v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1095 c572f23a Harsh Prateek Bora
                              v9stat_dotl.st_gid);
1096 7999f7e1 Aneesh Kumar K.V
out:
1097 7999f7e1 Aneesh Kumar K.V
    put_fid(pdu, fidp);
1098 7999f7e1 Aneesh Kumar K.V
out_nofid:
1099 8db21ce7 Aneesh Kumar K.V
    complete_pdu(s, pdu, retval);
1100 00ede4c2 Sripathi Kodi
}
1101 00ede4c2 Sripathi Kodi
1102 e4027caf Aneesh Kumar K.V
/* Attribute flags */
1103 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MODE       (1 << 0)
1104 e4027caf Aneesh Kumar K.V
#define P9_ATTR_UID        (1 << 1)
1105 e4027caf Aneesh Kumar K.V
#define P9_ATTR_GID        (1 << 2)
1106 e4027caf Aneesh Kumar K.V
#define P9_ATTR_SIZE       (1 << 3)
1107 e4027caf Aneesh Kumar K.V
#define P9_ATTR_ATIME      (1 << 4)
1108 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MTIME      (1 << 5)
1109 e4027caf Aneesh Kumar K.V
#define P9_ATTR_CTIME      (1 << 6)
1110 e4027caf Aneesh Kumar K.V
#define P9_ATTR_ATIME_SET  (1 << 7)
1111 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MTIME_SET  (1 << 8)
1112 e4027caf Aneesh Kumar K.V
1113 e4027caf Aneesh Kumar K.V
#define P9_ATTR_MASK    127
1114 c79ce737 Sripathi Kodi
1115 65c05f9a Aneesh Kumar K.V
static void v9fs_setattr(void *opaque)
1116 c79ce737 Sripathi Kodi
{
1117 65c05f9a Aneesh Kumar K.V
    int err = 0;
1118 65c05f9a Aneesh Kumar K.V
    int32_t fid;
1119 65c05f9a Aneesh Kumar K.V
    V9fsFidState *fidp;
1120 65c05f9a Aneesh Kumar K.V
    size_t offset = 7;
1121 65c05f9a Aneesh Kumar K.V
    V9fsIattr v9iattr;
1122 65c05f9a Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1123 65c05f9a Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1124 c79ce737 Sripathi Kodi
1125 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1126 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1127 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1128 ddca7f86 M. Mohan Kumar
    }
1129 c79ce737 Sripathi Kodi
1130 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1131 65c05f9a Aneesh Kumar K.V
    if (fidp == NULL) {
1132 65c05f9a Aneesh Kumar K.V
        err = -EINVAL;
1133 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1134 c79ce737 Sripathi Kodi
    }
1135 e4027caf Aneesh Kumar K.V
    if (v9iattr.valid & P9_ATTR_MODE) {
1136 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1137 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1138 65c05f9a Aneesh Kumar K.V
            goto out;
1139 c79ce737 Sripathi Kodi
        }
1140 c79ce737 Sripathi Kodi
    }
1141 e4027caf Aneesh Kumar K.V
    if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1142 c79ce737 Sripathi Kodi
        struct timespec times[2];
1143 e4027caf Aneesh Kumar K.V
        if (v9iattr.valid & P9_ATTR_ATIME) {
1144 e4027caf Aneesh Kumar K.V
            if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1145 65c05f9a Aneesh Kumar K.V
                times[0].tv_sec = v9iattr.atime_sec;
1146 65c05f9a Aneesh Kumar K.V
                times[0].tv_nsec = v9iattr.atime_nsec;
1147 c79ce737 Sripathi Kodi
            } else {
1148 c79ce737 Sripathi Kodi
                times[0].tv_nsec = UTIME_NOW;
1149 c79ce737 Sripathi Kodi
            }
1150 c79ce737 Sripathi Kodi
        } else {
1151 c79ce737 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
1152 c79ce737 Sripathi Kodi
        }
1153 e4027caf Aneesh Kumar K.V
        if (v9iattr.valid & P9_ATTR_MTIME) {
1154 e4027caf Aneesh Kumar K.V
            if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1155 65c05f9a Aneesh Kumar K.V
                times[1].tv_sec = v9iattr.mtime_sec;
1156 65c05f9a Aneesh Kumar K.V
                times[1].tv_nsec = v9iattr.mtime_nsec;
1157 c79ce737 Sripathi Kodi
            } else {
1158 c79ce737 Sripathi Kodi
                times[1].tv_nsec = UTIME_NOW;
1159 c79ce737 Sripathi Kodi
            }
1160 c79ce737 Sripathi Kodi
        } else {
1161 c79ce737 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
1162 c79ce737 Sripathi Kodi
        }
1163 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_utimensat(pdu, &fidp->path, times);
1164 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1165 65c05f9a Aneesh Kumar K.V
            goto out;
1166 65c05f9a Aneesh Kumar K.V
        }
1167 c79ce737 Sripathi Kodi
    }
1168 65c05f9a Aneesh Kumar K.V
    /*
1169 65c05f9a Aneesh Kumar K.V
     * If the only valid entry in iattr is ctime we can call
1170 65c05f9a Aneesh Kumar K.V
     * chown(-1,-1) to update the ctime of the file
1171 65c05f9a Aneesh Kumar K.V
     */
1172 e4027caf Aneesh Kumar K.V
    if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1173 e4027caf Aneesh Kumar K.V
        ((v9iattr.valid & P9_ATTR_CTIME)
1174 e4027caf Aneesh Kumar K.V
         && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1175 e4027caf Aneesh Kumar K.V
        if (!(v9iattr.valid & P9_ATTR_UID)) {
1176 65c05f9a Aneesh Kumar K.V
            v9iattr.uid = -1;
1177 65c05f9a Aneesh Kumar K.V
        }
1178 e4027caf Aneesh Kumar K.V
        if (!(v9iattr.valid & P9_ATTR_GID)) {
1179 65c05f9a Aneesh Kumar K.V
            v9iattr.gid = -1;
1180 65c05f9a Aneesh Kumar K.V
        }
1181 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1182 65c05f9a Aneesh Kumar K.V
                            v9iattr.gid);
1183 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1184 65c05f9a Aneesh Kumar K.V
            goto out;
1185 65c05f9a Aneesh Kumar K.V
        }
1186 c79ce737 Sripathi Kodi
    }
1187 e4027caf Aneesh Kumar K.V
    if (v9iattr.valid & (P9_ATTR_SIZE)) {
1188 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1189 65c05f9a Aneesh Kumar K.V
        if (err < 0) {
1190 65c05f9a Aneesh Kumar K.V
            goto out;
1191 65c05f9a Aneesh Kumar K.V
        }
1192 c79ce737 Sripathi Kodi
    }
1193 65c05f9a Aneesh Kumar K.V
    err = offset;
1194 c79ce737 Sripathi Kodi
out:
1195 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1196 84dfb926 Aneesh Kumar K.V
out_nofid:
1197 65c05f9a Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
1198 c79ce737 Sripathi Kodi
}
1199 c79ce737 Sripathi Kodi
1200 3cc19c0c Aneesh Kumar K.V
static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1201 ff5e54c9 Anthony Liguori
{
1202 ff5e54c9 Anthony Liguori
    int i;
1203 ddca7f86 M. Mohan Kumar
    ssize_t err;
1204 3cc19c0c Aneesh Kumar K.V
    size_t offset = 7;
1205 ddca7f86 M. Mohan Kumar
1206 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "w", nwnames);
1207 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1208 ddca7f86 M. Mohan Kumar
        return err;
1209 ddca7f86 M. Mohan Kumar
    }
1210 ddca7f86 M. Mohan Kumar
    offset += err;
1211 3cc19c0c Aneesh Kumar K.V
    for (i = 0; i < nwnames; i++) {
1212 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1213 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1214 ddca7f86 M. Mohan Kumar
            return err;
1215 ddca7f86 M. Mohan Kumar
        }
1216 ddca7f86 M. Mohan Kumar
        offset += err;
1217 ff5e54c9 Anthony Liguori
    }
1218 3cc19c0c Aneesh Kumar K.V
    return offset;
1219 ff5e54c9 Anthony Liguori
}
1220 ff5e54c9 Anthony Liguori
1221 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_walk(void *opaque)
1222 9f107513 Anthony Liguori
{
1223 3cc19c0c Aneesh Kumar K.V
    int name_idx;
1224 3cc19c0c Aneesh Kumar K.V
    V9fsQID *qids = NULL;
1225 3cc19c0c Aneesh Kumar K.V
    int i, err = 0;
1226 2289be19 Aneesh Kumar K.V
    V9fsPath dpath, path;
1227 3cc19c0c Aneesh Kumar K.V
    uint16_t nwnames;
1228 3cc19c0c Aneesh Kumar K.V
    struct stat stbuf;
1229 3cc19c0c Aneesh Kumar K.V
    size_t offset = 7;
1230 3cc19c0c Aneesh Kumar K.V
    int32_t fid, newfid;
1231 3cc19c0c Aneesh Kumar K.V
    V9fsString *wnames = NULL;
1232 3cc19c0c Aneesh Kumar K.V
    V9fsFidState *fidp;
1233 3a93113a Dong Xu Wang
    V9fsFidState *newfidp = NULL;
1234 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
1235 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
1236 ff5e54c9 Anthony Liguori
1237 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1238 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1239 ddca7f86 M. Mohan Kumar
        complete_pdu(s, pdu, err);
1240 ddca7f86 M. Mohan Kumar
        return ;
1241 ddca7f86 M. Mohan Kumar
    }
1242 ddca7f86 M. Mohan Kumar
    offset += err;
1243 ff5e54c9 Anthony Liguori
1244 c572f23a Harsh Prateek Bora
    trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1245 c572f23a Harsh Prateek Bora
1246 3cc19c0c Aneesh Kumar K.V
    if (nwnames && nwnames <= P9_MAXWELEM) {
1247 3cc19c0c Aneesh Kumar K.V
        wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1248 3cc19c0c Aneesh Kumar K.V
        qids   = g_malloc0(sizeof(qids[0]) * nwnames);
1249 3cc19c0c Aneesh Kumar K.V
        for (i = 0; i < nwnames; i++) {
1250 ddca7f86 M. Mohan Kumar
            err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1251 ddca7f86 M. Mohan Kumar
            if (err < 0) {
1252 ddca7f86 M. Mohan Kumar
                goto out_nofid;
1253 ddca7f86 M. Mohan Kumar
            }
1254 ddca7f86 M. Mohan Kumar
            offset += err;
1255 ff5e54c9 Anthony Liguori
        }
1256 3cc19c0c Aneesh Kumar K.V
    } else if (nwnames > P9_MAXWELEM) {
1257 4f8dee2d Harsh Prateek Bora
        err = -EINVAL;
1258 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1259 ff5e54c9 Anthony Liguori
    }
1260 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1261 3cc19c0c Aneesh Kumar K.V
    if (fidp == NULL) {
1262 ff5e54c9 Anthony Liguori
        err = -ENOENT;
1263 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1264 ff5e54c9 Anthony Liguori
    }
1265 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&dpath);
1266 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&path);
1267 2289be19 Aneesh Kumar K.V
    /*
1268 2289be19 Aneesh Kumar K.V
     * Both dpath and path initially poin to fidp.
1269 2289be19 Aneesh Kumar K.V
     * Needed to handle request with nwnames == 0
1270 2289be19 Aneesh Kumar K.V
     */
1271 2289be19 Aneesh Kumar K.V
    v9fs_path_copy(&dpath, &fidp->path);
1272 2289be19 Aneesh Kumar K.V
    v9fs_path_copy(&path, &fidp->path);
1273 2289be19 Aneesh Kumar K.V
    for (name_idx = 0; name_idx < nwnames; name_idx++) {
1274 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);
1275 2289be19 Aneesh Kumar K.V
        if (err < 0) {
1276 2289be19 Aneesh Kumar K.V
            goto out;
1277 2289be19 Aneesh Kumar K.V
        }
1278 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &path, &stbuf);
1279 2289be19 Aneesh Kumar K.V
        if (err < 0) {
1280 2289be19 Aneesh Kumar K.V
            goto out;
1281 2289be19 Aneesh Kumar K.V
        }
1282 2289be19 Aneesh Kumar K.V
        stat_to_qid(&stbuf, &qids[name_idx]);
1283 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&dpath, &path);
1284 2289be19 Aneesh Kumar K.V
    }
1285 ff5e54c9 Anthony Liguori
    if (fid == newfid) {
1286 3cc19c0c Aneesh Kumar K.V
        BUG_ON(fidp->fid_type != P9_FID_NONE);
1287 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
1288 ff5e54c9 Anthony Liguori
    } else {
1289 3cc19c0c Aneesh Kumar K.V
        newfidp = alloc_fid(s, newfid);
1290 3cc19c0c Aneesh Kumar K.V
        if (newfidp == NULL) {
1291 ff5e54c9 Anthony Liguori
            err = -EINVAL;
1292 ff5e54c9 Anthony Liguori
            goto out;
1293 ff5e54c9 Anthony Liguori
        }
1294 3cc19c0c Aneesh Kumar K.V
        newfidp->uid = fidp->uid;
1295 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&newfidp->path, &path);
1296 9f107513 Anthony Liguori
    }
1297 3cc19c0c Aneesh Kumar K.V
    err = v9fs_walk_marshal(pdu, nwnames, qids);
1298 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1299 ff5e54c9 Anthony Liguori
out:
1300 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1301 84dfb926 Aneesh Kumar K.V
    if (newfidp) {
1302 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, newfidp);
1303 84dfb926 Aneesh Kumar K.V
    }
1304 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&dpath);
1305 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&path);
1306 84dfb926 Aneesh Kumar K.V
out_nofid:
1307 3cc19c0c Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
1308 3cc19c0c Aneesh Kumar K.V
    if (nwnames && nwnames <= P9_MAXWELEM) {
1309 3cc19c0c Aneesh Kumar K.V
        for (name_idx = 0; name_idx < nwnames; name_idx++) {
1310 3cc19c0c Aneesh Kumar K.V
            v9fs_string_free(&wnames[name_idx]);
1311 3cc19c0c Aneesh Kumar K.V
        }
1312 3cc19c0c Aneesh Kumar K.V
        g_free(wnames);
1313 3cc19c0c Aneesh Kumar K.V
        g_free(qids);
1314 3cc19c0c Aneesh Kumar K.V
    }
1315 9f107513 Anthony Liguori
}
1316 9f107513 Anthony Liguori
1317 bccacf6c Aneesh Kumar K.V
static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
1318 5e94c103 M. Mohan Kumar
{
1319 5e94c103 M. Mohan Kumar
    struct statfs stbuf;
1320 5e94c103 M. Mohan Kumar
    int32_t iounit = 0;
1321 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1322 5e94c103 M. Mohan Kumar
1323 5e94c103 M. Mohan Kumar
    /*
1324 5e94c103 M. Mohan Kumar
     * iounit should be multiples of f_bsize (host filesystem block size
1325 5e94c103 M. Mohan Kumar
     * and as well as less than (client msize - P9_IOHDRSZ))
1326 5e94c103 M. Mohan Kumar
     */
1327 bccacf6c Aneesh Kumar K.V
    if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1328 5e94c103 M. Mohan Kumar
        iounit = stbuf.f_bsize;
1329 5e94c103 M. Mohan Kumar
        iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1330 5e94c103 M. Mohan Kumar
    }
1331 5e94c103 M. Mohan Kumar
    if (!iounit) {
1332 5e94c103 M. Mohan Kumar
        iounit = s->msize - P9_IOHDRSZ;
1333 5e94c103 M. Mohan Kumar
    }
1334 5e94c103 M. Mohan Kumar
    return iounit;
1335 5e94c103 M. Mohan Kumar
}
1336 5e94c103 M. Mohan Kumar
1337 857bc158 Aneesh Kumar K.V
static void v9fs_open(void *opaque)
1338 5e94c103 M. Mohan Kumar
{
1339 857bc158 Aneesh Kumar K.V
    int flags;
1340 857bc158 Aneesh Kumar K.V
    int32_t fid;
1341 857bc158 Aneesh Kumar K.V
    int32_t mode;
1342 857bc158 Aneesh Kumar K.V
    V9fsQID qid;
1343 7999f7e1 Aneesh Kumar K.V
    int iounit = 0;
1344 857bc158 Aneesh Kumar K.V
    ssize_t err = 0;
1345 857bc158 Aneesh Kumar K.V
    size_t offset = 7;
1346 857bc158 Aneesh Kumar K.V
    struct stat stbuf;
1347 857bc158 Aneesh Kumar K.V
    V9fsFidState *fidp;
1348 857bc158 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1349 857bc158 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1350 5e94c103 M. Mohan Kumar
1351 857bc158 Aneesh Kumar K.V
    if (s->proto_version == V9FS_PROTO_2000L) {
1352 ddca7f86 M. Mohan Kumar
        err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1353 857bc158 Aneesh Kumar K.V
    } else {
1354 67d6fa53 Benjamin Herrenschmidt
        uint8_t modebyte;
1355 67d6fa53 Benjamin Herrenschmidt
        err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1356 67d6fa53 Benjamin Herrenschmidt
        mode = modebyte;
1357 ddca7f86 M. Mohan Kumar
    }
1358 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1359 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1360 857bc158 Aneesh Kumar K.V
    }
1361 c572f23a Harsh Prateek Bora
    trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1362 c572f23a Harsh Prateek Bora
1363 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1364 857bc158 Aneesh Kumar K.V
    if (fidp == NULL) {
1365 857bc158 Aneesh Kumar K.V
        err = -ENOENT;
1366 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1367 a6568fe2 Anthony Liguori
    }
1368 857bc158 Aneesh Kumar K.V
    BUG_ON(fidp->fid_type != P9_FID_NONE);
1369 a6568fe2 Anthony Liguori
1370 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1371 857bc158 Aneesh Kumar K.V
    if (err < 0) {
1372 a6568fe2 Anthony Liguori
        goto out;
1373 a6568fe2 Anthony Liguori
    }
1374 857bc158 Aneesh Kumar K.V
    stat_to_qid(&stbuf, &qid);
1375 857bc158 Aneesh Kumar K.V
    if (S_ISDIR(stbuf.st_mode)) {
1376 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_opendir(pdu, fidp);
1377 857bc158 Aneesh Kumar K.V
        if (err < 0) {
1378 857bc158 Aneesh Kumar K.V
            goto out;
1379 857bc158 Aneesh Kumar K.V
        }
1380 857bc158 Aneesh Kumar K.V
        fidp->fid_type = P9_FID_DIR;
1381 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1382 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1383 ddca7f86 M. Mohan Kumar
            goto out;
1384 ddca7f86 M. Mohan Kumar
        }
1385 ddca7f86 M. Mohan Kumar
        err += offset;
1386 a6568fe2 Anthony Liguori
    } else {
1387 771e9d4c M. Mohan Kumar
        if (s->proto_version == V9FS_PROTO_2000L) {
1388 d3ab98e6 Aneesh Kumar K.V
            flags = get_dotl_openflags(s, mode);
1389 771e9d4c M. Mohan Kumar
        } else {
1390 857bc158 Aneesh Kumar K.V
            flags = omode_to_uflags(mode);
1391 771e9d4c M. Mohan Kumar
        }
1392 2c74c2cb M. Mohan Kumar
        if (is_ro_export(&s->ctx)) {
1393 2c74c2cb M. Mohan Kumar
            if (mode & O_WRONLY || mode & O_RDWR ||
1394 2c74c2cb M. Mohan Kumar
                mode & O_APPEND || mode & O_TRUNC) {
1395 2c74c2cb M. Mohan Kumar
                err = -EROFS;
1396 2c74c2cb M. Mohan Kumar
                goto out;
1397 2c74c2cb M. Mohan Kumar
            }
1398 2c74c2cb M. Mohan Kumar
        }
1399 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_open(pdu, fidp, flags);
1400 857bc158 Aneesh Kumar K.V
        if (err < 0) {
1401 857bc158 Aneesh Kumar K.V
            goto out;
1402 857bc158 Aneesh Kumar K.V
        }
1403 857bc158 Aneesh Kumar K.V
        fidp->fid_type = P9_FID_FILE;
1404 7a462745 Aneesh Kumar K.V
        fidp->open_flags = flags;
1405 7a462745 Aneesh Kumar K.V
        if (flags & O_EXCL) {
1406 7a462745 Aneesh Kumar K.V
            /*
1407 7a462745 Aneesh Kumar K.V
             * We let the host file system do O_EXCL check
1408 7a462745 Aneesh Kumar K.V
             * We should not reclaim such fd
1409 7a462745 Aneesh Kumar K.V
             */
1410 7a462745 Aneesh Kumar K.V
            fidp->flags |= FID_NON_RECLAIMABLE;
1411 7a462745 Aneesh Kumar K.V
        }
1412 bccacf6c Aneesh Kumar K.V
        iounit = get_iounit(pdu, &fidp->path);
1413 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1414 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1415 ddca7f86 M. Mohan Kumar
            goto out;
1416 ddca7f86 M. Mohan Kumar
        }
1417 ddca7f86 M. Mohan Kumar
        err += offset;
1418 a6568fe2 Anthony Liguori
    }
1419 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_open_return(pdu->tag, pdu->id,
1420 7999f7e1 Aneesh Kumar K.V
                           qid.type, qid.version, qid.path, iounit);
1421 a6568fe2 Anthony Liguori
out:
1422 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1423 84dfb926 Aneesh Kumar K.V
out_nofid:
1424 a6568fe2 Anthony Liguori
    complete_pdu(s, pdu, err);
1425 a6568fe2 Anthony Liguori
}
1426 a6568fe2 Anthony Liguori
1427 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_lcreate(void *opaque)
1428 c1568af5 Venkateswararao Jujjuri (JV)
{
1429 c1568af5 Venkateswararao Jujjuri (JV)
    int32_t dfid, flags, mode;
1430 c1568af5 Venkateswararao Jujjuri (JV)
    gid_t gid;
1431 c1568af5 Venkateswararao Jujjuri (JV)
    ssize_t err = 0;
1432 36f8981f Venkateswararao Jujjuri
    ssize_t offset = 7;
1433 36f8981f Venkateswararao Jujjuri
    V9fsString name;
1434 36f8981f Venkateswararao Jujjuri
    V9fsFidState *fidp;
1435 36f8981f Venkateswararao Jujjuri
    struct stat stbuf;
1436 36f8981f Venkateswararao Jujjuri
    V9fsQID qid;
1437 36f8981f Venkateswararao Jujjuri
    int32_t iounit;
1438 36f8981f Venkateswararao Jujjuri
    V9fsPDU *pdu = opaque;
1439 c1568af5 Venkateswararao Jujjuri (JV)
1440 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
1441 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1442 ddca7f86 M. Mohan Kumar
                        &name, &flags, &mode, &gid);
1443 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1444 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1445 ddca7f86 M. Mohan Kumar
    }
1446 c572f23a Harsh Prateek Bora
    trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1447 c1568af5 Venkateswararao Jujjuri (JV)
1448 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, dfid);
1449 36f8981f Venkateswararao Jujjuri
    if (fidp == NULL) {
1450 c1568af5 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
1451 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1452 c1568af5 Venkateswararao Jujjuri (JV)
    }
1453 c1568af5 Venkateswararao Jujjuri (JV)
1454 d3ab98e6 Aneesh Kumar K.V
    flags = get_dotl_openflags(pdu->s, flags);
1455 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_open2(pdu, fidp, &name, gid,
1456 02cb7f3a Aneesh Kumar K.V
                        flags | O_CREAT, mode, &stbuf);
1457 36f8981f Venkateswararao Jujjuri
    if (err < 0) {
1458 36f8981f Venkateswararao Jujjuri
        goto out;
1459 36f8981f Venkateswararao Jujjuri
    }
1460 36f8981f Venkateswararao Jujjuri
    fidp->fid_type = P9_FID_FILE;
1461 7a462745 Aneesh Kumar K.V
    fidp->open_flags = flags;
1462 7a462745 Aneesh Kumar K.V
    if (flags & O_EXCL) {
1463 7a462745 Aneesh Kumar K.V
        /*
1464 7a462745 Aneesh Kumar K.V
         * We let the host file system do O_EXCL check
1465 7a462745 Aneesh Kumar K.V
         * We should not reclaim such fd
1466 7a462745 Aneesh Kumar K.V
         */
1467 7a462745 Aneesh Kumar K.V
        fidp->flags |= FID_NON_RECLAIMABLE;
1468 7a462745 Aneesh Kumar K.V
    }
1469 bccacf6c Aneesh Kumar K.V
    iounit =  get_iounit(pdu, &fidp->path);
1470 36f8981f Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
1471 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1472 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1473 ddca7f86 M. Mohan Kumar
        goto out;
1474 ddca7f86 M. Mohan Kumar
    }
1475 ddca7f86 M. Mohan Kumar
    err += offset;
1476 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1477 7999f7e1 Aneesh Kumar K.V
                              qid.type, qid.version, qid.path, iounit);
1478 c1568af5 Venkateswararao Jujjuri (JV)
out:
1479 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1480 84dfb926 Aneesh Kumar K.V
out_nofid:
1481 36f8981f Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
1482 36f8981f Venkateswararao Jujjuri
    v9fs_string_free(&name);
1483 c1568af5 Venkateswararao Jujjuri (JV)
}
1484 c1568af5 Venkateswararao Jujjuri (JV)
1485 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_fsync(void *opaque)
1486 b41e95d3 Venkateswararao Jujjuri (JV)
{
1487 4e9ad444 Aneesh Kumar K.V
    int err;
1488 b41e95d3 Venkateswararao Jujjuri (JV)
    int32_t fid;
1489 4e9ad444 Aneesh Kumar K.V
    int datasync;
1490 b41e95d3 Venkateswararao Jujjuri (JV)
    size_t offset = 7;
1491 b41e95d3 Venkateswararao Jujjuri (JV)
    V9fsFidState *fidp;
1492 4e9ad444 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1493 4e9ad444 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1494 b41e95d3 Venkateswararao Jujjuri (JV)
1495 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1496 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1497 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1498 ddca7f86 M. Mohan Kumar
    }
1499 c572f23a Harsh Prateek Bora
    trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1500 c572f23a Harsh Prateek Bora
1501 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1502 b41e95d3 Venkateswararao Jujjuri (JV)
    if (fidp == NULL) {
1503 b41e95d3 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
1504 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1505 b41e95d3 Venkateswararao Jujjuri (JV)
    }
1506 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_fsync(pdu, fidp, datasync);
1507 4e9ad444 Aneesh Kumar K.V
    if (!err) {
1508 4e9ad444 Aneesh Kumar K.V
        err = offset;
1509 4e9ad444 Aneesh Kumar K.V
    }
1510 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1511 84dfb926 Aneesh Kumar K.V
out_nofid:
1512 4e9ad444 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
1513 b41e95d3 Venkateswararao Jujjuri (JV)
}
1514 b41e95d3 Venkateswararao Jujjuri (JV)
1515 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_clunk(void *opaque)
1516 a6568fe2 Anthony Liguori
{
1517 c540ee51 Aneesh Kumar K.V
    int err;
1518 bbd5697b Anthony Liguori
    int32_t fid;
1519 bbd5697b Anthony Liguori
    size_t offset = 7;
1520 84dfb926 Aneesh Kumar K.V
    V9fsFidState *fidp;
1521 c540ee51 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1522 c540ee51 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1523 bbd5697b Anthony Liguori
1524 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "d", &fid);
1525 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1526 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1527 ddca7f86 M. Mohan Kumar
    }
1528 c572f23a Harsh Prateek Bora
    trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1529 84dfb926 Aneesh Kumar K.V
1530 ce421a19 Aneesh Kumar K.V
    fidp = clunk_fid(s, fid);
1531 84dfb926 Aneesh Kumar K.V
    if (fidp == NULL) {
1532 84dfb926 Aneesh Kumar K.V
        err = -ENOENT;
1533 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1534 84dfb926 Aneesh Kumar K.V
    }
1535 ce421a19 Aneesh Kumar K.V
    /*
1536 ce421a19 Aneesh Kumar K.V
     * Bump the ref so that put_fid will
1537 ce421a19 Aneesh Kumar K.V
     * free the fid.
1538 ce421a19 Aneesh Kumar K.V
     */
1539 ce421a19 Aneesh Kumar K.V
    fidp->ref++;
1540 a911a182 Aneesh Kumar K.V
    err = put_fid(pdu, fidp);
1541 a911a182 Aneesh Kumar K.V
    if (!err) {
1542 a911a182 Aneesh Kumar K.V
        err = offset;
1543 a911a182 Aneesh Kumar K.V
    }
1544 84dfb926 Aneesh Kumar K.V
out_nofid:
1545 bbd5697b Anthony Liguori
    complete_pdu(s, pdu, err);
1546 9f107513 Anthony Liguori
}
1547 9f107513 Anthony Liguori
1548 2f008a8c Aneesh Kumar K.V
static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1549 2f008a8c Aneesh Kumar K.V
                           uint64_t off, uint32_t max_count)
1550 a9231555 Anthony Liguori
{
1551 ddca7f86 M. Mohan Kumar
    ssize_t err;
1552 d208a0e0 Aneesh Kumar K.V
    size_t offset = 7;
1553 d208a0e0 Aneesh Kumar K.V
    int read_count;
1554 d208a0e0 Aneesh Kumar K.V
    int64_t xattr_len;
1555 a9231555 Anthony Liguori
1556 d208a0e0 Aneesh Kumar K.V
    xattr_len = fidp->fs.xattr.len;
1557 d208a0e0 Aneesh Kumar K.V
    read_count = xattr_len - off;
1558 d208a0e0 Aneesh Kumar K.V
    if (read_count > max_count) {
1559 d208a0e0 Aneesh Kumar K.V
        read_count = max_count;
1560 d208a0e0 Aneesh Kumar K.V
    } else if (read_count < 0) {
1561 d208a0e0 Aneesh Kumar K.V
        /*
1562 d208a0e0 Aneesh Kumar K.V
         * read beyond XATTR value
1563 d208a0e0 Aneesh Kumar K.V
         */
1564 d208a0e0 Aneesh Kumar K.V
        read_count = 0;
1565 a9231555 Anthony Liguori
    }
1566 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "d", read_count);
1567 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1568 ddca7f86 M. Mohan Kumar
        return err;
1569 ddca7f86 M. Mohan Kumar
    }
1570 ddca7f86 M. Mohan Kumar
    offset += err;
1571 ddca7f86 M. Mohan Kumar
    err = v9fs_pack(pdu->elem.in_sg, pdu->elem.in_num, offset,
1572 ddca7f86 M. Mohan Kumar
                    ((char *)fidp->fs.xattr.value) + off,
1573 ddca7f86 M. Mohan Kumar
                    read_count);
1574 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1575 ddca7f86 M. Mohan Kumar
        return err;
1576 ddca7f86 M. Mohan Kumar
    }
1577 ddca7f86 M. Mohan Kumar
    offset += err;
1578 d208a0e0 Aneesh Kumar K.V
    return offset;
1579 a9231555 Anthony Liguori
}
1580 a9231555 Anthony Liguori
1581 bccacf6c Aneesh Kumar K.V
static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1582 2f008a8c Aneesh Kumar K.V
                                     V9fsFidState *fidp, uint32_t max_count)
1583 a9231555 Anthony Liguori
{
1584 2289be19 Aneesh Kumar K.V
    V9fsPath path;
1585 d208a0e0 Aneesh Kumar K.V
    V9fsStat v9stat;
1586 d208a0e0 Aneesh Kumar K.V
    int len, err = 0;
1587 d208a0e0 Aneesh Kumar K.V
    int32_t count = 0;
1588 d208a0e0 Aneesh Kumar K.V
    struct stat stbuf;
1589 d208a0e0 Aneesh Kumar K.V
    off_t saved_dir_pos;
1590 5f524c1e Harsh Prateek Bora
    struct dirent *dent, *result;
1591 a9231555 Anthony Liguori
1592 d208a0e0 Aneesh Kumar K.V
    /* save the directory position */
1593 bccacf6c Aneesh Kumar K.V
    saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1594 d208a0e0 Aneesh Kumar K.V
    if (saved_dir_pos < 0) {
1595 d208a0e0 Aneesh Kumar K.V
        return saved_dir_pos;
1596 a9231555 Anthony Liguori
    }
1597 5f524c1e Harsh Prateek Bora
1598 5f524c1e Harsh Prateek Bora
    dent = g_malloc(sizeof(struct dirent));
1599 5f524c1e Harsh Prateek Bora
1600 d208a0e0 Aneesh Kumar K.V
    while (1) {
1601 2289be19 Aneesh Kumar K.V
        v9fs_path_init(&path);
1602 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1603 5f524c1e Harsh Prateek Bora
        if (err || !result) {
1604 d208a0e0 Aneesh Kumar K.V
            break;
1605 d208a0e0 Aneesh Kumar K.V
        }
1606 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1607 d208a0e0 Aneesh Kumar K.V
        if (err < 0) {
1608 d208a0e0 Aneesh Kumar K.V
            goto out;
1609 d208a0e0 Aneesh Kumar K.V
        }
1610 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &path, &stbuf);
1611 2289be19 Aneesh Kumar K.V
        if (err < 0) {
1612 2289be19 Aneesh Kumar K.V
            goto out;
1613 2289be19 Aneesh Kumar K.V
        }
1614 bccacf6c Aneesh Kumar K.V
        err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1615 d208a0e0 Aneesh Kumar K.V
        if (err < 0) {
1616 d208a0e0 Aneesh Kumar K.V
            goto out;
1617 a9231555 Anthony Liguori
        }
1618 d208a0e0 Aneesh Kumar K.V
        /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1619 d208a0e0 Aneesh Kumar K.V
        len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1620 d208a0e0 Aneesh Kumar K.V
        if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1621 d208a0e0 Aneesh Kumar K.V
            /* Ran out of buffer. Set dir back to old position and return */
1622 bccacf6c Aneesh Kumar K.V
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1623 d208a0e0 Aneesh Kumar K.V
            v9fs_stat_free(&v9stat);
1624 2289be19 Aneesh Kumar K.V
            v9fs_path_free(&path);
1625 5f524c1e Harsh Prateek Bora
            g_free(dent);
1626 d208a0e0 Aneesh Kumar K.V
            return count;
1627 d208a0e0 Aneesh Kumar K.V
        }
1628 d208a0e0 Aneesh Kumar K.V
        count += len;
1629 d208a0e0 Aneesh Kumar K.V
        v9fs_stat_free(&v9stat);
1630 2289be19 Aneesh Kumar K.V
        v9fs_path_free(&path);
1631 d208a0e0 Aneesh Kumar K.V
        saved_dir_pos = dent->d_off;
1632 a9231555 Anthony Liguori
    }
1633 a9231555 Anthony Liguori
out:
1634 5f524c1e Harsh Prateek Bora
    g_free(dent);
1635 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&path);
1636 d208a0e0 Aneesh Kumar K.V
    if (err < 0) {
1637 d208a0e0 Aneesh Kumar K.V
        return err;
1638 fa32ef88 Aneesh Kumar K.V
    }
1639 d208a0e0 Aneesh Kumar K.V
    return count;
1640 fa32ef88 Aneesh Kumar K.V
}
1641 fa32ef88 Aneesh Kumar K.V
1642 302a0d3e Stefan Hajnoczi
/*
1643 302a0d3e Stefan Hajnoczi
 * Create a QEMUIOVector for a sub-region of PDU iovecs
1644 302a0d3e Stefan Hajnoczi
 *
1645 302a0d3e Stefan Hajnoczi
 * @qiov:       uninitialized QEMUIOVector
1646 302a0d3e Stefan Hajnoczi
 * @skip:       number of bytes to skip from beginning of PDU
1647 302a0d3e Stefan Hajnoczi
 * @size:       number of bytes to include
1648 302a0d3e Stefan Hajnoczi
 * @is_write:   true - write, false - read
1649 302a0d3e Stefan Hajnoczi
 *
1650 302a0d3e Stefan Hajnoczi
 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1651 302a0d3e Stefan Hajnoczi
 * with qemu_iovec_destroy().
1652 302a0d3e Stefan Hajnoczi
 */
1653 302a0d3e Stefan Hajnoczi
static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1654 1b093c48 Michael Tokarev
                                    size_t skip, size_t size,
1655 302a0d3e Stefan Hajnoczi
                                    bool is_write)
1656 302a0d3e Stefan Hajnoczi
{
1657 302a0d3e Stefan Hajnoczi
    QEMUIOVector elem;
1658 302a0d3e Stefan Hajnoczi
    struct iovec *iov;
1659 302a0d3e Stefan Hajnoczi
    unsigned int niov;
1660 302a0d3e Stefan Hajnoczi
1661 302a0d3e Stefan Hajnoczi
    if (is_write) {
1662 302a0d3e Stefan Hajnoczi
        iov = pdu->elem.out_sg;
1663 302a0d3e Stefan Hajnoczi
        niov = pdu->elem.out_num;
1664 302a0d3e Stefan Hajnoczi
    } else {
1665 302a0d3e Stefan Hajnoczi
        iov = pdu->elem.in_sg;
1666 302a0d3e Stefan Hajnoczi
        niov = pdu->elem.in_num;
1667 302a0d3e Stefan Hajnoczi
    }
1668 302a0d3e Stefan Hajnoczi
1669 302a0d3e Stefan Hajnoczi
    qemu_iovec_init_external(&elem, iov, niov);
1670 302a0d3e Stefan Hajnoczi
    qemu_iovec_init(qiov, niov);
1671 1b093c48 Michael Tokarev
    qemu_iovec_concat(qiov, &elem, skip, size);
1672 302a0d3e Stefan Hajnoczi
}
1673 302a0d3e Stefan Hajnoczi
1674 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_read(void *opaque)
1675 9f107513 Anthony Liguori
{
1676 a9231555 Anthony Liguori
    int32_t fid;
1677 2f008a8c Aneesh Kumar K.V
    uint64_t off;
1678 a9231555 Anthony Liguori
    ssize_t err = 0;
1679 d208a0e0 Aneesh Kumar K.V
    int32_t count = 0;
1680 d208a0e0 Aneesh Kumar K.V
    size_t offset = 7;
1681 2f008a8c Aneesh Kumar K.V
    uint32_t max_count;
1682 d208a0e0 Aneesh Kumar K.V
    V9fsFidState *fidp;
1683 d208a0e0 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1684 d208a0e0 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1685 a9231555 Anthony Liguori
1686 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1687 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1688 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1689 ddca7f86 M. Mohan Kumar
    }
1690 c572f23a Harsh Prateek Bora
    trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1691 84dfb926 Aneesh Kumar K.V
1692 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1693 d208a0e0 Aneesh Kumar K.V
    if (fidp == NULL) {
1694 a9231555 Anthony Liguori
        err = -EINVAL;
1695 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1696 a9231555 Anthony Liguori
    }
1697 d208a0e0 Aneesh Kumar K.V
    if (fidp->fid_type == P9_FID_DIR) {
1698 a9231555 Anthony Liguori
1699 d208a0e0 Aneesh Kumar K.V
        if (off == 0) {
1700 bccacf6c Aneesh Kumar K.V
            v9fs_co_rewinddir(pdu, fidp);
1701 a9231555 Anthony Liguori
        }
1702 bccacf6c Aneesh Kumar K.V
        count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1703 d208a0e0 Aneesh Kumar K.V
        if (count < 0) {
1704 d208a0e0 Aneesh Kumar K.V
            err = count;
1705 d208a0e0 Aneesh Kumar K.V
            goto out;
1706 56d15a53 Sanchit Garg
        }
1707 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "d", count);
1708 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1709 ddca7f86 M. Mohan Kumar
            goto out;
1710 ddca7f86 M. Mohan Kumar
        }
1711 ddca7f86 M. Mohan Kumar
        err += offset + count;
1712 d208a0e0 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_FILE) {
1713 302a0d3e Stefan Hajnoczi
        QEMUIOVector qiov_full;
1714 302a0d3e Stefan Hajnoczi
        QEMUIOVector qiov;
1715 d208a0e0 Aneesh Kumar K.V
        int32_t len;
1716 d208a0e0 Aneesh Kumar K.V
1717 302a0d3e Stefan Hajnoczi
        v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1718 302a0d3e Stefan Hajnoczi
        qemu_iovec_init(&qiov, qiov_full.niov);
1719 d208a0e0 Aneesh Kumar K.V
        do {
1720 302a0d3e Stefan Hajnoczi
            qemu_iovec_reset(&qiov);
1721 1b093c48 Michael Tokarev
            qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1722 d208a0e0 Aneesh Kumar K.V
            if (0) {
1723 302a0d3e Stefan Hajnoczi
                print_sg(qiov.iov, qiov.niov);
1724 d208a0e0 Aneesh Kumar K.V
            }
1725 d208a0e0 Aneesh Kumar K.V
            /* Loop in case of EINTR */
1726 d208a0e0 Aneesh Kumar K.V
            do {
1727 302a0d3e Stefan Hajnoczi
                len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1728 d208a0e0 Aneesh Kumar K.V
                if (len >= 0) {
1729 d208a0e0 Aneesh Kumar K.V
                    off   += len;
1730 d208a0e0 Aneesh Kumar K.V
                    count += len;
1731 d208a0e0 Aneesh Kumar K.V
                }
1732 bccacf6c Aneesh Kumar K.V
            } while (len == -EINTR && !pdu->cancelled);
1733 d208a0e0 Aneesh Kumar K.V
            if (len < 0) {
1734 d208a0e0 Aneesh Kumar K.V
                /* IO error return the error */
1735 d208a0e0 Aneesh Kumar K.V
                err = len;
1736 d208a0e0 Aneesh Kumar K.V
                goto out;
1737 d208a0e0 Aneesh Kumar K.V
            }
1738 d208a0e0 Aneesh Kumar K.V
        } while (count < max_count && len > 0);
1739 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "d", count);
1740 ddca7f86 M. Mohan Kumar
        if (err < 0) {
1741 ddca7f86 M. Mohan Kumar
            goto out;
1742 ddca7f86 M. Mohan Kumar
        }
1743 ddca7f86 M. Mohan Kumar
        err += offset + count;
1744 302a0d3e Stefan Hajnoczi
        qemu_iovec_destroy(&qiov);
1745 302a0d3e Stefan Hajnoczi
        qemu_iovec_destroy(&qiov_full);
1746 d208a0e0 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_XATTR) {
1747 d208a0e0 Aneesh Kumar K.V
        err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1748 a9231555 Anthony Liguori
    } else {
1749 a9231555 Anthony Liguori
        err = -EINVAL;
1750 9f107513 Anthony Liguori
    }
1751 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1752 a9231555 Anthony Liguori
out:
1753 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1754 84dfb926 Aneesh Kumar K.V
out_nofid:
1755 a9231555 Anthony Liguori
    complete_pdu(s, pdu, err);
1756 9f107513 Anthony Liguori
}
1757 9f107513 Anthony Liguori
1758 5e4eaa79 Aneesh Kumar K.V
static size_t v9fs_readdir_data_size(V9fsString *name)
1759 c18e2f94 Sripathi Kodi
{
1760 5e4eaa79 Aneesh Kumar K.V
    /*
1761 5e4eaa79 Aneesh Kumar K.V
     * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1762 5e4eaa79 Aneesh Kumar K.V
     * size of type (1) + size of name.size (2) + strlen(name.data)
1763 5e4eaa79 Aneesh Kumar K.V
     */
1764 5e4eaa79 Aneesh Kumar K.V
    return 24 + v9fs_string_size(name);
1765 c18e2f94 Sripathi Kodi
}
1766 c18e2f94 Sripathi Kodi
1767 bccacf6c Aneesh Kumar K.V
static int v9fs_do_readdir(V9fsPDU *pdu,
1768 5e4eaa79 Aneesh Kumar K.V
                           V9fsFidState *fidp, int32_t max_count)
1769 c18e2f94 Sripathi Kodi
{
1770 c18e2f94 Sripathi Kodi
    size_t size;
1771 5e4eaa79 Aneesh Kumar K.V
    V9fsQID qid;
1772 5e4eaa79 Aneesh Kumar K.V
    V9fsString name;
1773 5e4eaa79 Aneesh Kumar K.V
    int len, err = 0;
1774 5e4eaa79 Aneesh Kumar K.V
    int32_t count = 0;
1775 5e4eaa79 Aneesh Kumar K.V
    off_t saved_dir_pos;
1776 5f524c1e Harsh Prateek Bora
    struct dirent *dent, *result;
1777 c18e2f94 Sripathi Kodi
1778 5e4eaa79 Aneesh Kumar K.V
    /* save the directory position */
1779 bccacf6c Aneesh Kumar K.V
    saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1780 5e4eaa79 Aneesh Kumar K.V
    if (saved_dir_pos < 0) {
1781 5e4eaa79 Aneesh Kumar K.V
        return saved_dir_pos;
1782 5e4eaa79 Aneesh Kumar K.V
    }
1783 5f524c1e Harsh Prateek Bora
1784 5f524c1e Harsh Prateek Bora
    dent = g_malloc(sizeof(struct dirent));
1785 5f524c1e Harsh Prateek Bora
1786 5e4eaa79 Aneesh Kumar K.V
    while (1) {
1787 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1788 5f524c1e Harsh Prateek Bora
        if (err || !result) {
1789 5e4eaa79 Aneesh Kumar K.V
            break;
1790 5e4eaa79 Aneesh Kumar K.V
        }
1791 5e4eaa79 Aneesh Kumar K.V
        v9fs_string_init(&name);
1792 5e4eaa79 Aneesh Kumar K.V
        v9fs_string_sprintf(&name, "%s", dent->d_name);
1793 5e4eaa79 Aneesh Kumar K.V
        if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1794 c18e2f94 Sripathi Kodi
            /* Ran out of buffer. Set dir back to old position and return */
1795 bccacf6c Aneesh Kumar K.V
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1796 5e4eaa79 Aneesh Kumar K.V
            v9fs_string_free(&name);
1797 5f524c1e Harsh Prateek Bora
            g_free(dent);
1798 5e4eaa79 Aneesh Kumar K.V
            return count;
1799 c18e2f94 Sripathi Kodi
        }
1800 5e4eaa79 Aneesh Kumar K.V
        /*
1801 5e4eaa79 Aneesh Kumar K.V
         * Fill up just the path field of qid because the client uses
1802 c18e2f94 Sripathi Kodi
         * only that. To fill the entire qid structure we will have
1803 c18e2f94 Sripathi Kodi
         * to stat each dirent found, which is expensive
1804 c18e2f94 Sripathi Kodi
         */
1805 5e4eaa79 Aneesh Kumar K.V
        size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1806 5e4eaa79 Aneesh Kumar K.V
        memcpy(&qid.path, &dent->d_ino, size);
1807 c18e2f94 Sripathi Kodi
        /* Fill the other fields with dummy values */
1808 5e4eaa79 Aneesh Kumar K.V
        qid.type = 0;
1809 5e4eaa79 Aneesh Kumar K.V
        qid.version = 0;
1810 c18e2f94 Sripathi Kodi
1811 5e4eaa79 Aneesh Kumar K.V
        /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1812 5e4eaa79 Aneesh Kumar K.V
        len = pdu_marshal(pdu, 11 + count, "Qqbs",
1813 5e4eaa79 Aneesh Kumar K.V
                          &qid, dent->d_off,
1814 5e4eaa79 Aneesh Kumar K.V
                          dent->d_type, &name);
1815 ddca7f86 M. Mohan Kumar
        if (len < 0) {
1816 ddca7f86 M. Mohan Kumar
            v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1817 ddca7f86 M. Mohan Kumar
            v9fs_string_free(&name);
1818 ddca7f86 M. Mohan Kumar
            g_free(dent);
1819 ddca7f86 M. Mohan Kumar
            return len;
1820 ddca7f86 M. Mohan Kumar
        }
1821 5e4eaa79 Aneesh Kumar K.V
        count += len;
1822 5e4eaa79 Aneesh Kumar K.V
        v9fs_string_free(&name);
1823 5e4eaa79 Aneesh Kumar K.V
        saved_dir_pos = dent->d_off;
1824 5e4eaa79 Aneesh Kumar K.V
    }
1825 5f524c1e Harsh Prateek Bora
    g_free(dent);
1826 5e4eaa79 Aneesh Kumar K.V
    if (err < 0) {
1827 5e4eaa79 Aneesh Kumar K.V
        return err;
1828 5e4eaa79 Aneesh Kumar K.V
    }
1829 5e4eaa79 Aneesh Kumar K.V
    return count;
1830 c18e2f94 Sripathi Kodi
}
1831 c18e2f94 Sripathi Kodi
1832 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_readdir(void *opaque)
1833 c18e2f94 Sripathi Kodi
{
1834 c18e2f94 Sripathi Kodi
    int32_t fid;
1835 5e4eaa79 Aneesh Kumar K.V
    V9fsFidState *fidp;
1836 5e4eaa79 Aneesh Kumar K.V
    ssize_t retval = 0;
1837 c18e2f94 Sripathi Kodi
    size_t offset = 7;
1838 2f008a8c Aneesh Kumar K.V
    uint64_t initial_offset;
1839 2f008a8c Aneesh Kumar K.V
    int32_t count;
1840 2f008a8c Aneesh Kumar K.V
    uint32_t max_count;
1841 5e4eaa79 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
1842 5e4eaa79 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
1843 c18e2f94 Sripathi Kodi
1844 ddca7f86 M. Mohan Kumar
    retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1845 ddca7f86 M. Mohan Kumar
                           &initial_offset, &max_count);
1846 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
1847 ddca7f86 M. Mohan Kumar
        goto out_nofid;
1848 ddca7f86 M. Mohan Kumar
    }
1849 c572f23a Harsh Prateek Bora
    trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1850 c572f23a Harsh Prateek Bora
1851 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1852 84dfb926 Aneesh Kumar K.V
    if (fidp == NULL) {
1853 84dfb926 Aneesh Kumar K.V
        retval = -EINVAL;
1854 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1855 84dfb926 Aneesh Kumar K.V
    }
1856 84dfb926 Aneesh Kumar K.V
    if (!fidp->fs.dir) {
1857 5e4eaa79 Aneesh Kumar K.V
        retval = -EINVAL;
1858 c18e2f94 Sripathi Kodi
        goto out;
1859 c18e2f94 Sripathi Kodi
    }
1860 5e4eaa79 Aneesh Kumar K.V
    if (initial_offset == 0) {
1861 bccacf6c Aneesh Kumar K.V
        v9fs_co_rewinddir(pdu, fidp);
1862 c18e2f94 Sripathi Kodi
    } else {
1863 bccacf6c Aneesh Kumar K.V
        v9fs_co_seekdir(pdu, fidp, initial_offset);
1864 c18e2f94 Sripathi Kodi
    }
1865 bccacf6c Aneesh Kumar K.V
    count = v9fs_do_readdir(pdu, fidp, max_count);
1866 5e4eaa79 Aneesh Kumar K.V
    if (count < 0) {
1867 5e4eaa79 Aneesh Kumar K.V
        retval = count;
1868 5e4eaa79 Aneesh Kumar K.V
        goto out;
1869 5e4eaa79 Aneesh Kumar K.V
    }
1870 ddca7f86 M. Mohan Kumar
    retval = pdu_marshal(pdu, offset, "d", count);
1871 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
1872 ddca7f86 M. Mohan Kumar
        goto out;
1873 ddca7f86 M. Mohan Kumar
    }
1874 ddca7f86 M. Mohan Kumar
    retval += count + offset;
1875 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1876 c18e2f94 Sripathi Kodi
out:
1877 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
1878 84dfb926 Aneesh Kumar K.V
out_nofid:
1879 5e4eaa79 Aneesh Kumar K.V
    complete_pdu(s, pdu, retval);
1880 c18e2f94 Sripathi Kodi
}
1881 c18e2f94 Sripathi Kodi
1882 d7a90491 Aneesh Kumar K.V
static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1883 2f008a8c Aneesh Kumar K.V
                            uint64_t off, uint32_t count,
1884 d7a90491 Aneesh Kumar K.V
                            struct iovec *sg, int cnt)
1885 10b468bd Aneesh Kumar K.V
{
1886 10b468bd Aneesh Kumar K.V
    int i, to_copy;
1887 10b468bd Aneesh Kumar K.V
    ssize_t err = 0;
1888 10b468bd Aneesh Kumar K.V
    int write_count;
1889 10b468bd Aneesh Kumar K.V
    int64_t xattr_len;
1890 d7a90491 Aneesh Kumar K.V
    size_t offset = 7;
1891 10b468bd Aneesh Kumar K.V
1892 d7a90491 Aneesh Kumar K.V
1893 d7a90491 Aneesh Kumar K.V
    xattr_len = fidp->fs.xattr.len;
1894 d7a90491 Aneesh Kumar K.V
    write_count = xattr_len - off;
1895 d7a90491 Aneesh Kumar K.V
    if (write_count > count) {
1896 d7a90491 Aneesh Kumar K.V
        write_count = count;
1897 10b468bd Aneesh Kumar K.V
    } else if (write_count < 0) {
1898 10b468bd Aneesh Kumar K.V
        /*
1899 10b468bd Aneesh Kumar K.V
         * write beyond XATTR value len specified in
1900 10b468bd Aneesh Kumar K.V
         * xattrcreate
1901 10b468bd Aneesh Kumar K.V
         */
1902 10b468bd Aneesh Kumar K.V
        err = -ENOSPC;
1903 10b468bd Aneesh Kumar K.V
        goto out;
1904 10b468bd Aneesh Kumar K.V
    }
1905 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "d", write_count);
1906 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1907 ddca7f86 M. Mohan Kumar
        return err;
1908 ddca7f86 M. Mohan Kumar
    }
1909 ddca7f86 M. Mohan Kumar
    err += offset;
1910 d7a90491 Aneesh Kumar K.V
    fidp->fs.xattr.copied_len += write_count;
1911 10b468bd Aneesh Kumar K.V
    /*
1912 10b468bd Aneesh Kumar K.V
     * Now copy the content from sg list
1913 10b468bd Aneesh Kumar K.V
     */
1914 d7a90491 Aneesh Kumar K.V
    for (i = 0; i < cnt; i++) {
1915 d7a90491 Aneesh Kumar K.V
        if (write_count > sg[i].iov_len) {
1916 d7a90491 Aneesh Kumar K.V
            to_copy = sg[i].iov_len;
1917 10b468bd Aneesh Kumar K.V
        } else {
1918 10b468bd Aneesh Kumar K.V
            to_copy = write_count;
1919 10b468bd Aneesh Kumar K.V
        }
1920 d7a90491 Aneesh Kumar K.V
        memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
1921 10b468bd Aneesh Kumar K.V
        /* updating vs->off since we are not using below */
1922 d7a90491 Aneesh Kumar K.V
        off += to_copy;
1923 10b468bd Aneesh Kumar K.V
        write_count -= to_copy;
1924 10b468bd Aneesh Kumar K.V
    }
1925 10b468bd Aneesh Kumar K.V
out:
1926 d7a90491 Aneesh Kumar K.V
    return err;
1927 10b468bd Aneesh Kumar K.V
}
1928 10b468bd Aneesh Kumar K.V
1929 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_write(void *opaque)
1930 9f107513 Anthony Liguori
{
1931 d7a90491 Aneesh Kumar K.V
    ssize_t err;
1932 d7a90491 Aneesh Kumar K.V
    int32_t fid;
1933 2f008a8c Aneesh Kumar K.V
    uint64_t off;
1934 2f008a8c Aneesh Kumar K.V
    uint32_t count;
1935 d7a90491 Aneesh Kumar K.V
    int32_t len = 0;
1936 d7a90491 Aneesh Kumar K.V
    int32_t total = 0;
1937 d7a90491 Aneesh Kumar K.V
    size_t offset = 7;
1938 d7a90491 Aneesh Kumar K.V
    V9fsFidState *fidp;
1939 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
1940 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
1941 302a0d3e Stefan Hajnoczi
    QEMUIOVector qiov_full;
1942 302a0d3e Stefan Hajnoczi
    QEMUIOVector qiov;
1943 8449360c Anthony Liguori
1944 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
1945 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1946 ddca7f86 M. Mohan Kumar
        return complete_pdu(s, pdu, err);
1947 ddca7f86 M. Mohan Kumar
    }
1948 ddca7f86 M. Mohan Kumar
    offset += err;
1949 302a0d3e Stefan Hajnoczi
    v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
1950 302a0d3e Stefan Hajnoczi
    trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
1951 84dfb926 Aneesh Kumar K.V
1952 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
1953 d7a90491 Aneesh Kumar K.V
    if (fidp == NULL) {
1954 8449360c Anthony Liguori
        err = -EINVAL;
1955 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
1956 9f107513 Anthony Liguori
    }
1957 d7a90491 Aneesh Kumar K.V
    if (fidp->fid_type == P9_FID_FILE) {
1958 d7a90491 Aneesh Kumar K.V
        if (fidp->fs.fd == -1) {
1959 10b468bd Aneesh Kumar K.V
            err = -EINVAL;
1960 10b468bd Aneesh Kumar K.V
            goto out;
1961 10b468bd Aneesh Kumar K.V
        }
1962 d7a90491 Aneesh Kumar K.V
    } else if (fidp->fid_type == P9_FID_XATTR) {
1963 10b468bd Aneesh Kumar K.V
        /*
1964 10b468bd Aneesh Kumar K.V
         * setxattr operation
1965 10b468bd Aneesh Kumar K.V
         */
1966 302a0d3e Stefan Hajnoczi
        err = v9fs_xattr_write(s, pdu, fidp, off, count,
1967 302a0d3e Stefan Hajnoczi
                               qiov_full.iov, qiov_full.niov);
1968 d7a90491 Aneesh Kumar K.V
        goto out;
1969 10b468bd Aneesh Kumar K.V
    } else {
1970 8449360c Anthony Liguori
        err = -EINVAL;
1971 8449360c Anthony Liguori
        goto out;
1972 8449360c Anthony Liguori
    }
1973 302a0d3e Stefan Hajnoczi
    qemu_iovec_init(&qiov, qiov_full.niov);
1974 d7a90491 Aneesh Kumar K.V
    do {
1975 302a0d3e Stefan Hajnoczi
        qemu_iovec_reset(&qiov);
1976 1b093c48 Michael Tokarev
        qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
1977 d7a90491 Aneesh Kumar K.V
        if (0) {
1978 302a0d3e Stefan Hajnoczi
            print_sg(qiov.iov, qiov.niov);
1979 56d15a53 Sanchit Garg
        }
1980 d7a90491 Aneesh Kumar K.V
        /* Loop in case of EINTR */
1981 d7a90491 Aneesh Kumar K.V
        do {
1982 302a0d3e Stefan Hajnoczi
            len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
1983 d7a90491 Aneesh Kumar K.V
            if (len >= 0) {
1984 d7a90491 Aneesh Kumar K.V
                off   += len;
1985 d7a90491 Aneesh Kumar K.V
                total += len;
1986 d7a90491 Aneesh Kumar K.V
            }
1987 bccacf6c Aneesh Kumar K.V
        } while (len == -EINTR && !pdu->cancelled);
1988 d7a90491 Aneesh Kumar K.V
        if (len < 0) {
1989 d7a90491 Aneesh Kumar K.V
            /* IO error return the error */
1990 d7a90491 Aneesh Kumar K.V
            err = len;
1991 302a0d3e Stefan Hajnoczi
            goto out_qiov;
1992 d7a90491 Aneesh Kumar K.V
        }
1993 d7a90491 Aneesh Kumar K.V
    } while (total < count && len > 0);
1994 302a0d3e Stefan Hajnoczi
1995 302a0d3e Stefan Hajnoczi
    offset = 7;
1996 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "d", total);
1997 ddca7f86 M. Mohan Kumar
    if (err < 0) {
1998 ddca7f86 M. Mohan Kumar
        goto out;
1999 ddca7f86 M. Mohan Kumar
    }
2000 ddca7f86 M. Mohan Kumar
    err += offset;
2001 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2002 302a0d3e Stefan Hajnoczi
out_qiov:
2003 302a0d3e Stefan Hajnoczi
    qemu_iovec_destroy(&qiov);
2004 8449360c Anthony Liguori
out:
2005 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2006 84dfb926 Aneesh Kumar K.V
out_nofid:
2007 302a0d3e Stefan Hajnoczi
    qemu_iovec_destroy(&qiov_full);
2008 d7a90491 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2009 9f107513 Anthony Liguori
}
2010 9f107513 Anthony Liguori
2011 baaa86d9 Venkateswararao Jujjuri
static void v9fs_create(void *opaque)
2012 5e94c103 M. Mohan Kumar
{
2013 baaa86d9 Venkateswararao Jujjuri
    int32_t fid;
2014 baaa86d9 Venkateswararao Jujjuri
    int err = 0;
2015 baaa86d9 Venkateswararao Jujjuri
    size_t offset = 7;
2016 baaa86d9 Venkateswararao Jujjuri
    V9fsFidState *fidp;
2017 baaa86d9 Venkateswararao Jujjuri
    V9fsQID qid;
2018 baaa86d9 Venkateswararao Jujjuri
    int32_t perm;
2019 baaa86d9 Venkateswararao Jujjuri
    int8_t mode;
2020 2289be19 Aneesh Kumar K.V
    V9fsPath path;
2021 baaa86d9 Venkateswararao Jujjuri
    struct stat stbuf;
2022 baaa86d9 Venkateswararao Jujjuri
    V9fsString name;
2023 baaa86d9 Venkateswararao Jujjuri
    V9fsString extension;
2024 baaa86d9 Venkateswararao Jujjuri
    int iounit;
2025 baaa86d9 Venkateswararao Jujjuri
    V9fsPDU *pdu = opaque;
2026 c494dd6f Anthony Liguori
2027 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&path);
2028 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2029 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&extension);
2030 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2031 ddca7f86 M. Mohan Kumar
                        &perm, &mode, &extension);
2032 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2033 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2034 ddca7f86 M. Mohan Kumar
    }
2035 c572f23a Harsh Prateek Bora
    trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2036 c572f23a Harsh Prateek Bora
2037 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2038 baaa86d9 Venkateswararao Jujjuri
    if (fidp == NULL) {
2039 baaa86d9 Venkateswararao Jujjuri
        err = -EINVAL;
2040 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2041 c494dd6f Anthony Liguori
    }
2042 baaa86d9 Venkateswararao Jujjuri
    if (perm & P9_STAT_MODE_DIR) {
2043 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2044 02cb7f3a Aneesh Kumar K.V
                            fidp->uid, -1, &stbuf);
2045 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2046 baaa86d9 Venkateswararao Jujjuri
            goto out;
2047 baaa86d9 Venkateswararao Jujjuri
        }
2048 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2049 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2050 2289be19 Aneesh Kumar K.V
            goto out;
2051 2289be19 Aneesh Kumar K.V
        }
2052 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2053 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_opendir(pdu, fidp);
2054 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2055 baaa86d9 Venkateswararao Jujjuri
            goto out;
2056 baaa86d9 Venkateswararao Jujjuri
        }
2057 baaa86d9 Venkateswararao Jujjuri
        fidp->fid_type = P9_FID_DIR;
2058 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_SYMLINK) {
2059 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_symlink(pdu, fidp, &name,
2060 02cb7f3a Aneesh Kumar K.V
                              extension.data, -1 , &stbuf);
2061 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2062 baaa86d9 Venkateswararao Jujjuri
            goto out;
2063 baaa86d9 Venkateswararao Jujjuri
        }
2064 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2065 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2066 2289be19 Aneesh Kumar K.V
            goto out;
2067 2289be19 Aneesh Kumar K.V
        }
2068 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2069 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_LINK) {
2070 2289be19 Aneesh Kumar K.V
        int32_t ofid = atoi(extension.data);
2071 bccacf6c Aneesh Kumar K.V
        V9fsFidState *ofidp = get_fid(pdu, ofid);
2072 2289be19 Aneesh Kumar K.V
        if (ofidp == NULL) {
2073 baaa86d9 Venkateswararao Jujjuri
            err = -EINVAL;
2074 baaa86d9 Venkateswararao Jujjuri
            goto out;
2075 baaa86d9 Venkateswararao Jujjuri
        }
2076 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_link(pdu, ofidp, fidp, &name);
2077 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, ofidp);
2078 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2079 2289be19 Aneesh Kumar K.V
            goto out;
2080 2289be19 Aneesh Kumar K.V
        }
2081 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2082 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2083 2289be19 Aneesh Kumar K.V
            fidp->fid_type = P9_FID_NONE;
2084 baaa86d9 Venkateswararao Jujjuri
            goto out;
2085 c494dd6f Anthony Liguori
        }
2086 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2087 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2088 02cb7f3a Aneesh Kumar K.V
        if (err < 0) {
2089 02cb7f3a Aneesh Kumar K.V
            fidp->fid_type = P9_FID_NONE;
2090 02cb7f3a Aneesh Kumar K.V
            goto out;
2091 02cb7f3a Aneesh Kumar K.V
        }
2092 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_DEVICE) {
2093 c494dd6f Anthony Liguori
        char ctype;
2094 c494dd6f Anthony Liguori
        uint32_t major, minor;
2095 c494dd6f Anthony Liguori
        mode_t nmode = 0;
2096 c494dd6f Anthony Liguori
2097 baaa86d9 Venkateswararao Jujjuri
        if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2098 c494dd6f Anthony Liguori
            err = -errno;
2099 baaa86d9 Venkateswararao Jujjuri
            goto out;
2100 c494dd6f Anthony Liguori
        }
2101 c494dd6f Anthony Liguori
2102 c494dd6f Anthony Liguori
        switch (ctype) {
2103 c494dd6f Anthony Liguori
        case 'c':
2104 c494dd6f Anthony Liguori
            nmode = S_IFCHR;
2105 c494dd6f Anthony Liguori
            break;
2106 c494dd6f Anthony Liguori
        case 'b':
2107 c494dd6f Anthony Liguori
            nmode = S_IFBLK;
2108 c494dd6f Anthony Liguori
            break;
2109 c494dd6f Anthony Liguori
        default:
2110 c494dd6f Anthony Liguori
            err = -EIO;
2111 baaa86d9 Venkateswararao Jujjuri
            goto out;
2112 baaa86d9 Venkateswararao Jujjuri
        }
2113 c1568af5 Venkateswararao Jujjuri (JV)
2114 baaa86d9 Venkateswararao Jujjuri
        nmode |= perm & 0777;
2115 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2116 02cb7f3a Aneesh Kumar K.V
                            makedev(major, minor), nmode, &stbuf);
2117 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2118 baaa86d9 Venkateswararao Jujjuri
            goto out;
2119 baaa86d9 Venkateswararao Jujjuri
        }
2120 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2121 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2122 2289be19 Aneesh Kumar K.V
            goto out;
2123 2289be19 Aneesh Kumar K.V
        }
2124 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2125 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2126 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2127 02cb7f3a Aneesh Kumar K.V
                            0, S_IFIFO | (perm & 0777), &stbuf);
2128 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2129 baaa86d9 Venkateswararao Jujjuri
            goto out;
2130 baaa86d9 Venkateswararao Jujjuri
        }
2131 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2132 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2133 2289be19 Aneesh Kumar K.V
            goto out;
2134 2289be19 Aneesh Kumar K.V
        }
2135 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2136 baaa86d9 Venkateswararao Jujjuri
    } else if (perm & P9_STAT_MODE_SOCKET) {
2137 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2138 02cb7f3a Aneesh Kumar K.V
                            0, S_IFSOCK | (perm & 0777), &stbuf);
2139 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2140 baaa86d9 Venkateswararao Jujjuri
            goto out;
2141 baaa86d9 Venkateswararao Jujjuri
        }
2142 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2143 2289be19 Aneesh Kumar K.V
        if (err < 0) {
2144 2289be19 Aneesh Kumar K.V
            goto out;
2145 2289be19 Aneesh Kumar K.V
        }
2146 2289be19 Aneesh Kumar K.V
        v9fs_path_copy(&fidp->path, &path);
2147 baaa86d9 Venkateswararao Jujjuri
    } else {
2148 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_open2(pdu, fidp, &name, -1,
2149 02cb7f3a Aneesh Kumar K.V
                            omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2150 baaa86d9 Venkateswararao Jujjuri
        if (err < 0) {
2151 baaa86d9 Venkateswararao Jujjuri
            goto out;
2152 baaa86d9 Venkateswararao Jujjuri
        }
2153 baaa86d9 Venkateswararao Jujjuri
        fidp->fid_type = P9_FID_FILE;
2154 7a462745 Aneesh Kumar K.V
        fidp->open_flags = omode_to_uflags(mode);
2155 7a462745 Aneesh Kumar K.V
        if (fidp->open_flags & O_EXCL) {
2156 7a462745 Aneesh Kumar K.V
            /*
2157 7a462745 Aneesh Kumar K.V
             * We let the host file system do O_EXCL check
2158 7a462745 Aneesh Kumar K.V
             * We should not reclaim such fd
2159 7a462745 Aneesh Kumar K.V
             */
2160 7a462745 Aneesh Kumar K.V
            fidp->flags |= FID_NON_RECLAIMABLE;
2161 7a462745 Aneesh Kumar K.V
        }
2162 c494dd6f Anthony Liguori
    }
2163 bccacf6c Aneesh Kumar K.V
    iounit = get_iounit(pdu, &fidp->path);
2164 baaa86d9 Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
2165 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2166 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2167 ddca7f86 M. Mohan Kumar
        goto out;
2168 ddca7f86 M. Mohan Kumar
    }
2169 ddca7f86 M. Mohan Kumar
    err += offset;
2170 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_create_return(pdu->tag, pdu->id,
2171 7999f7e1 Aneesh Kumar K.V
                             qid.type, qid.version, qid.path, iounit);
2172 c494dd6f Anthony Liguori
out:
2173 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2174 84dfb926 Aneesh Kumar K.V
out_nofid:
2175 baaa86d9 Venkateswararao Jujjuri
   complete_pdu(pdu->s, pdu, err);
2176 baaa86d9 Venkateswararao Jujjuri
   v9fs_string_free(&name);
2177 baaa86d9 Venkateswararao Jujjuri
   v9fs_string_free(&extension);
2178 2289be19 Aneesh Kumar K.V
   v9fs_path_free(&path);
2179 9f107513 Anthony Liguori
}
2180 9f107513 Anthony Liguori
2181 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_symlink(void *opaque)
2182 08c60fc9 Venkateswararao Jujjuri (JV)
{
2183 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2184 3fa2a8d1 Venkateswararao Jujjuri
    V9fsString name;
2185 3fa2a8d1 Venkateswararao Jujjuri
    V9fsString symname;
2186 3fa2a8d1 Venkateswararao Jujjuri
    V9fsFidState *dfidp;
2187 3fa2a8d1 Venkateswararao Jujjuri
    V9fsQID qid;
2188 3fa2a8d1 Venkateswararao Jujjuri
    struct stat stbuf;
2189 08c60fc9 Venkateswararao Jujjuri (JV)
    int32_t dfid;
2190 08c60fc9 Venkateswararao Jujjuri (JV)
    int err = 0;
2191 08c60fc9 Venkateswararao Jujjuri (JV)
    gid_t gid;
2192 3fa2a8d1 Venkateswararao Jujjuri
    size_t offset = 7;
2193 08c60fc9 Venkateswararao Jujjuri (JV)
2194 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2195 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&symname);
2196 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2197 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2198 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2199 ddca7f86 M. Mohan Kumar
    }
2200 c572f23a Harsh Prateek Bora
    trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2201 08c60fc9 Venkateswararao Jujjuri (JV)
2202 bccacf6c Aneesh Kumar K.V
    dfidp = get_fid(pdu, dfid);
2203 3fa2a8d1 Venkateswararao Jujjuri
    if (dfidp == NULL) {
2204 08c60fc9 Venkateswararao Jujjuri (JV)
        err = -EINVAL;
2205 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2206 08c60fc9 Venkateswararao Jujjuri (JV)
    }
2207 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2208 3fa2a8d1 Venkateswararao Jujjuri
    if (err < 0) {
2209 3fa2a8d1 Venkateswararao Jujjuri
        goto out;
2210 3fa2a8d1 Venkateswararao Jujjuri
    }
2211 3fa2a8d1 Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
2212 ddca7f86 M. Mohan Kumar
    err =  pdu_marshal(pdu, offset, "Q", &qid);
2213 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2214 ddca7f86 M. Mohan Kumar
        goto out;
2215 ddca7f86 M. Mohan Kumar
    }
2216 ddca7f86 M. Mohan Kumar
    err += offset;
2217 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_symlink_return(pdu->tag, pdu->id,
2218 7999f7e1 Aneesh Kumar K.V
                              qid.type, qid.version, qid.path);
2219 08c60fc9 Venkateswararao Jujjuri (JV)
out:
2220 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, dfidp);
2221 84dfb926 Aneesh Kumar K.V
out_nofid:
2222 3fa2a8d1 Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
2223 3fa2a8d1 Venkateswararao Jujjuri
    v9fs_string_free(&name);
2224 3fa2a8d1 Venkateswararao Jujjuri
    v9fs_string_free(&symname);
2225 08c60fc9 Venkateswararao Jujjuri (JV)
}
2226 08c60fc9 Venkateswararao Jujjuri (JV)
2227 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_flush(void *opaque)
2228 9f107513 Anthony Liguori
{
2229 ddca7f86 M. Mohan Kumar
    ssize_t err;
2230 bccacf6c Aneesh Kumar K.V
    int16_t tag;
2231 bccacf6c Aneesh Kumar K.V
    size_t offset = 7;
2232 bccacf6c Aneesh Kumar K.V
    V9fsPDU *cancel_pdu;
2233 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2234 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2235 bccacf6c Aneesh Kumar K.V
2236 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "w", &tag);
2237 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2238 ddca7f86 M. Mohan Kumar
        complete_pdu(s, pdu, err);
2239 ddca7f86 M. Mohan Kumar
        return;
2240 ddca7f86 M. Mohan Kumar
    }
2241 c572f23a Harsh Prateek Bora
    trace_v9fs_flush(pdu->tag, pdu->id, tag);
2242 bccacf6c Aneesh Kumar K.V
2243 bccacf6c Aneesh Kumar K.V
    QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2244 bccacf6c Aneesh Kumar K.V
        if (cancel_pdu->tag == tag) {
2245 bccacf6c Aneesh Kumar K.V
            break;
2246 bccacf6c Aneesh Kumar K.V
        }
2247 bccacf6c Aneesh Kumar K.V
    }
2248 bccacf6c Aneesh Kumar K.V
    if (cancel_pdu) {
2249 bccacf6c Aneesh Kumar K.V
        cancel_pdu->cancelled = 1;
2250 bccacf6c Aneesh Kumar K.V
        /*
2251 bccacf6c Aneesh Kumar K.V
         * Wait for pdu to complete.
2252 bccacf6c Aneesh Kumar K.V
         */
2253 bccacf6c Aneesh Kumar K.V
        qemu_co_queue_wait(&cancel_pdu->complete);
2254 bccacf6c Aneesh Kumar K.V
        cancel_pdu->cancelled = 0;
2255 bccacf6c Aneesh Kumar K.V
        free_pdu(pdu->s, cancel_pdu);
2256 bccacf6c Aneesh Kumar K.V
    }
2257 9c5e9d89 Anthony Liguori
    complete_pdu(s, pdu, 7);
2258 9f107513 Anthony Liguori
}
2259 9f107513 Anthony Liguori
2260 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_link(void *opaque)
2261 b2c224be Venkateswararao Jujjuri (JV)
{
2262 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2263 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2264 b2c224be Venkateswararao Jujjuri (JV)
    int32_t dfid, oldfid;
2265 b2c224be Venkateswararao Jujjuri (JV)
    V9fsFidState *dfidp, *oldfidp;
2266 3a93113a Dong Xu Wang
    V9fsString name;
2267 b2c224be Venkateswararao Jujjuri (JV)
    size_t offset = 7;
2268 b2c224be Venkateswararao Jujjuri (JV)
    int err = 0;
2269 b2c224be Venkateswararao Jujjuri (JV)
2270 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2271 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2272 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2273 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2274 ddca7f86 M. Mohan Kumar
    }
2275 c572f23a Harsh Prateek Bora
    trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2276 b2c224be Venkateswararao Jujjuri (JV)
2277 bccacf6c Aneesh Kumar K.V
    dfidp = get_fid(pdu, dfid);
2278 b2c224be Venkateswararao Jujjuri (JV)
    if (dfidp == NULL) {
2279 ffd66876 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
2280 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2281 b2c224be Venkateswararao Jujjuri (JV)
    }
2282 b2c224be Venkateswararao Jujjuri (JV)
2283 bccacf6c Aneesh Kumar K.V
    oldfidp = get_fid(pdu, oldfid);
2284 b2c224be Venkateswararao Jujjuri (JV)
    if (oldfidp == NULL) {
2285 ffd66876 Venkateswararao Jujjuri (JV)
        err = -ENOENT;
2286 b2c224be Venkateswararao Jujjuri (JV)
        goto out;
2287 b2c224be Venkateswararao Jujjuri (JV)
    }
2288 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2289 ffd66876 Venkateswararao Jujjuri (JV)
    if (!err) {
2290 ffd66876 Venkateswararao Jujjuri (JV)
        err = offset;
2291 b2c224be Venkateswararao Jujjuri (JV)
    }
2292 b2c224be Venkateswararao Jujjuri (JV)
out:
2293 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, dfidp);
2294 84dfb926 Aneesh Kumar K.V
out_nofid:
2295 b2c224be Venkateswararao Jujjuri (JV)
    v9fs_string_free(&name);
2296 b2c224be Venkateswararao Jujjuri (JV)
    complete_pdu(s, pdu, err);
2297 b2c224be Venkateswararao Jujjuri (JV)
}
2298 b2c224be Venkateswararao Jujjuri (JV)
2299 532decb7 Aneesh Kumar K.V
/* Only works with path name based fid */
2300 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_remove(void *opaque)
2301 9f107513 Anthony Liguori
{
2302 5bae1900 Anthony Liguori
    int32_t fid;
2303 5bae1900 Anthony Liguori
    int err = 0;
2304 ae1ef571 Venkateswararao Jujjuri
    size_t offset = 7;
2305 ae1ef571 Venkateswararao Jujjuri
    V9fsFidState *fidp;
2306 ae1ef571 Venkateswararao Jujjuri
    V9fsPDU *pdu = opaque;
2307 5bae1900 Anthony Liguori
2308 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "d", &fid);
2309 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2310 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2311 ddca7f86 M. Mohan Kumar
    }
2312 c572f23a Harsh Prateek Bora
    trace_v9fs_remove(pdu->tag, pdu->id, fid);
2313 5bae1900 Anthony Liguori
2314 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2315 ae1ef571 Venkateswararao Jujjuri
    if (fidp == NULL) {
2316 5bae1900 Anthony Liguori
        err = -EINVAL;
2317 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2318 9f107513 Anthony Liguori
    }
2319 532decb7 Aneesh Kumar K.V
    /* if fs driver is not path based, return EOPNOTSUPP */
2320 c98f1d4a Aneesh Kumar K.V
    if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2321 532decb7 Aneesh Kumar K.V
        err = -EOPNOTSUPP;
2322 532decb7 Aneesh Kumar K.V
        goto out_err;
2323 532decb7 Aneesh Kumar K.V
    }
2324 7a462745 Aneesh Kumar K.V
    /*
2325 7a462745 Aneesh Kumar K.V
     * IF the file is unlinked, we cannot reopen
2326 7a462745 Aneesh Kumar K.V
     * the file later. So don't reclaim fd
2327 7a462745 Aneesh Kumar K.V
     */
2328 bccacf6c Aneesh Kumar K.V
    err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2329 7a462745 Aneesh Kumar K.V
    if (err < 0) {
2330 7a462745 Aneesh Kumar K.V
        goto out_err;
2331 7a462745 Aneesh Kumar K.V
    }
2332 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_remove(pdu, &fidp->path);
2333 ae1ef571 Venkateswararao Jujjuri
    if (!err) {
2334 ae1ef571 Venkateswararao Jujjuri
        err = offset;
2335 ae1ef571 Venkateswararao Jujjuri
    }
2336 7a462745 Aneesh Kumar K.V
out_err:
2337 ae1ef571 Venkateswararao Jujjuri
    /* For TREMOVE we need to clunk the fid even on failed remove */
2338 84dfb926 Aneesh Kumar K.V
    clunk_fid(pdu->s, fidp->fid);
2339 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2340 84dfb926 Aneesh Kumar K.V
out_nofid:
2341 ae1ef571 Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
2342 9f107513 Anthony Liguori
}
2343 9f107513 Anthony Liguori
2344 7834cf77 Aneesh Kumar K.V
static void v9fs_unlinkat(void *opaque)
2345 7834cf77 Aneesh Kumar K.V
{
2346 7834cf77 Aneesh Kumar K.V
    int err = 0;
2347 7834cf77 Aneesh Kumar K.V
    V9fsString name;
2348 7834cf77 Aneesh Kumar K.V
    int32_t dfid, flags;
2349 7834cf77 Aneesh Kumar K.V
    size_t offset = 7;
2350 2289be19 Aneesh Kumar K.V
    V9fsPath path;
2351 7834cf77 Aneesh Kumar K.V
    V9fsFidState *dfidp;
2352 7834cf77 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2353 7834cf77 Aneesh Kumar K.V
2354 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2355 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2356 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2357 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2358 ddca7f86 M. Mohan Kumar
    }
2359 bccacf6c Aneesh Kumar K.V
    dfidp = get_fid(pdu, dfid);
2360 7834cf77 Aneesh Kumar K.V
    if (dfidp == NULL) {
2361 7834cf77 Aneesh Kumar K.V
        err = -EINVAL;
2362 7834cf77 Aneesh Kumar K.V
        goto out_nofid;
2363 7834cf77 Aneesh Kumar K.V
    }
2364 7834cf77 Aneesh Kumar K.V
    /*
2365 7834cf77 Aneesh Kumar K.V
     * IF the file is unlinked, we cannot reopen
2366 7834cf77 Aneesh Kumar K.V
     * the file later. So don't reclaim fd
2367 7834cf77 Aneesh Kumar K.V
     */
2368 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&path);
2369 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2370 2289be19 Aneesh Kumar K.V
    if (err < 0) {
2371 2289be19 Aneesh Kumar K.V
        goto out_err;
2372 2289be19 Aneesh Kumar K.V
    }
2373 bccacf6c Aneesh Kumar K.V
    err = v9fs_mark_fids_unreclaim(pdu, &path);
2374 7834cf77 Aneesh Kumar K.V
    if (err < 0) {
2375 7834cf77 Aneesh Kumar K.V
        goto out_err;
2376 7834cf77 Aneesh Kumar K.V
    }
2377 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2378 7834cf77 Aneesh Kumar K.V
    if (!err) {
2379 7834cf77 Aneesh Kumar K.V
        err = offset;
2380 7834cf77 Aneesh Kumar K.V
    }
2381 7834cf77 Aneesh Kumar K.V
out_err:
2382 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, dfidp);
2383 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&path);
2384 7834cf77 Aneesh Kumar K.V
out_nofid:
2385 7834cf77 Aneesh Kumar K.V
    complete_pdu(pdu->s, pdu, err);
2386 7834cf77 Aneesh Kumar K.V
    v9fs_string_free(&name);
2387 7834cf77 Aneesh Kumar K.V
}
2388 7834cf77 Aneesh Kumar K.V
2389 2289be19 Aneesh Kumar K.V
2390 2289be19 Aneesh Kumar K.V
/* Only works with path name based fid */
2391 bccacf6c Aneesh Kumar K.V
static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2392 930b1e17 Aneesh Kumar K.V
                                int32_t newdirfid, V9fsString *name)
2393 8cf89e00 Anthony Liguori
{
2394 930b1e17 Aneesh Kumar K.V
    char *end;
2395 c7b4b0b3 M. Mohan Kumar
    int err = 0;
2396 2289be19 Aneesh Kumar K.V
    V9fsPath new_path;
2397 2289be19 Aneesh Kumar K.V
    V9fsFidState *tfidp;
2398 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2399 84dfb926 Aneesh Kumar K.V
    V9fsFidState *dirfidp = NULL;
2400 c7b4b0b3 M. Mohan Kumar
    char *old_name, *new_name;
2401 8cf89e00 Anthony Liguori
2402 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&new_path);
2403 930b1e17 Aneesh Kumar K.V
    if (newdirfid != -1) {
2404 bccacf6c Aneesh Kumar K.V
        dirfidp = get_fid(pdu, newdirfid);
2405 c7b4b0b3 M. Mohan Kumar
        if (dirfidp == NULL) {
2406 c7b4b0b3 M. Mohan Kumar
            err = -ENOENT;
2407 84dfb926 Aneesh Kumar K.V
            goto out_nofid;
2408 c7b4b0b3 M. Mohan Kumar
        }
2409 d62dbb51 Aneesh Kumar K.V
        BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2410 bccacf6c Aneesh Kumar K.V
        v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2411 c7b4b0b3 M. Mohan Kumar
    } else {
2412 930b1e17 Aneesh Kumar K.V
        old_name = fidp->path.data;
2413 8cf89e00 Anthony Liguori
        end = strrchr(old_name, '/');
2414 8cf89e00 Anthony Liguori
        if (end) {
2415 8cf89e00 Anthony Liguori
            end++;
2416 8cf89e00 Anthony Liguori
        } else {
2417 8cf89e00 Anthony Liguori
            end = old_name;
2418 8cf89e00 Anthony Liguori
        }
2419 7267c094 Anthony Liguori
        new_name = g_malloc0(end - old_name + name->size + 1);
2420 c7b4b0b3 M. Mohan Kumar
        strncat(new_name, old_name, end - old_name);
2421 930b1e17 Aneesh Kumar K.V
        strncat(new_name + (end - old_name), name->data, name->size);
2422 bccacf6c Aneesh Kumar K.V
        v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2423 2289be19 Aneesh Kumar K.V
        g_free(new_name);
2424 c7b4b0b3 M. Mohan Kumar
    }
2425 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2426 2289be19 Aneesh Kumar K.V
    if (err < 0) {
2427 2289be19 Aneesh Kumar K.V
        goto out;
2428 2289be19 Aneesh Kumar K.V
    }
2429 2289be19 Aneesh Kumar K.V
    /*
2430 2289be19 Aneesh Kumar K.V
     * Fixup fid's pointing to the old name to
2431 2289be19 Aneesh Kumar K.V
     * start pointing to the new name
2432 2289be19 Aneesh Kumar K.V
     */
2433 2289be19 Aneesh Kumar K.V
    for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2434 2289be19 Aneesh Kumar K.V
        if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2435 2289be19 Aneesh Kumar K.V
            /* replace the name */
2436 2289be19 Aneesh Kumar K.V
            v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2437 8cf89e00 Anthony Liguori
        }
2438 8cf89e00 Anthony Liguori
    }
2439 c7b4b0b3 M. Mohan Kumar
out:
2440 84dfb926 Aneesh Kumar K.V
    if (dirfidp) {
2441 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, dirfidp);
2442 84dfb926 Aneesh Kumar K.V
    }
2443 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&new_path);
2444 84dfb926 Aneesh Kumar K.V
out_nofid:
2445 c7b4b0b3 M. Mohan Kumar
    return err;
2446 c7b4b0b3 M. Mohan Kumar
}
2447 c7b4b0b3 M. Mohan Kumar
2448 532decb7 Aneesh Kumar K.V
/* Only works with path name based fid */
2449 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_rename(void *opaque)
2450 c7b4b0b3 M. Mohan Kumar
{
2451 c7b4b0b3 M. Mohan Kumar
    int32_t fid;
2452 c7b4b0b3 M. Mohan Kumar
    ssize_t err = 0;
2453 930b1e17 Aneesh Kumar K.V
    size_t offset = 7;
2454 930b1e17 Aneesh Kumar K.V
    V9fsString name;
2455 930b1e17 Aneesh Kumar K.V
    int32_t newdirfid;
2456 930b1e17 Aneesh Kumar K.V
    V9fsFidState *fidp;
2457 930b1e17 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2458 930b1e17 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2459 c7b4b0b3 M. Mohan Kumar
2460 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2461 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2462 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2463 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2464 ddca7f86 M. Mohan Kumar
    }
2465 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2466 930b1e17 Aneesh Kumar K.V
    if (fidp == NULL) {
2467 c7b4b0b3 M. Mohan Kumar
        err = -ENOENT;
2468 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2469 c7b4b0b3 M. Mohan Kumar
    }
2470 930b1e17 Aneesh Kumar K.V
    BUG_ON(fidp->fid_type != P9_FID_NONE);
2471 532decb7 Aneesh Kumar K.V
    /* if fs driver is not path based, return EOPNOTSUPP */
2472 c98f1d4a Aneesh Kumar K.V
    if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2473 532decb7 Aneesh Kumar K.V
        err = -EOPNOTSUPP;
2474 532decb7 Aneesh Kumar K.V
        goto out;
2475 532decb7 Aneesh Kumar K.V
    }
2476 532decb7 Aneesh Kumar K.V
    v9fs_path_write_lock(s);
2477 bccacf6c Aneesh Kumar K.V
    err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2478 532decb7 Aneesh Kumar K.V
    v9fs_path_unlock(s);
2479 930b1e17 Aneesh Kumar K.V
    if (!err) {
2480 930b1e17 Aneesh Kumar K.V
        err = offset;
2481 930b1e17 Aneesh Kumar K.V
    }
2482 532decb7 Aneesh Kumar K.V
out:
2483 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2484 84dfb926 Aneesh Kumar K.V
out_nofid:
2485 930b1e17 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2486 930b1e17 Aneesh Kumar K.V
    v9fs_string_free(&name);
2487 c7b4b0b3 M. Mohan Kumar
}
2488 c7b4b0b3 M. Mohan Kumar
2489 bccacf6c Aneesh Kumar K.V
static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2490 2289be19 Aneesh Kumar K.V
                               V9fsString *old_name, V9fsPath *newdir,
2491 2289be19 Aneesh Kumar K.V
                               V9fsString *new_name)
2492 2289be19 Aneesh Kumar K.V
{
2493 2289be19 Aneesh Kumar K.V
    V9fsFidState *tfidp;
2494 2289be19 Aneesh Kumar K.V
    V9fsPath oldpath, newpath;
2495 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2496 2289be19 Aneesh Kumar K.V
2497 2289be19 Aneesh Kumar K.V
2498 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&oldpath);
2499 2289be19 Aneesh Kumar K.V
    v9fs_path_init(&newpath);
2500 bccacf6c Aneesh Kumar K.V
    v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2501 bccacf6c Aneesh Kumar K.V
    v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2502 2289be19 Aneesh Kumar K.V
2503 2289be19 Aneesh Kumar K.V
    /*
2504 2289be19 Aneesh Kumar K.V
     * Fixup fid's pointing to the old name to
2505 2289be19 Aneesh Kumar K.V
     * start pointing to the new name
2506 2289be19 Aneesh Kumar K.V
     */
2507 2289be19 Aneesh Kumar K.V
    for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2508 2289be19 Aneesh Kumar K.V
        if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2509 2289be19 Aneesh Kumar K.V
            /* replace the name */
2510 2289be19 Aneesh Kumar K.V
            v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2511 2289be19 Aneesh Kumar K.V
        }
2512 2289be19 Aneesh Kumar K.V
    }
2513 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&oldpath);
2514 2289be19 Aneesh Kumar K.V
    v9fs_path_free(&newpath);
2515 2289be19 Aneesh Kumar K.V
}
2516 2289be19 Aneesh Kumar K.V
2517 bccacf6c Aneesh Kumar K.V
static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2518 89bf6593 Aneesh Kumar K.V
                                  V9fsString *old_name, int32_t newdirfid,
2519 89bf6593 Aneesh Kumar K.V
                                  V9fsString *new_name)
2520 89bf6593 Aneesh Kumar K.V
{
2521 89bf6593 Aneesh Kumar K.V
    int err = 0;
2522 bccacf6c Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2523 89bf6593 Aneesh Kumar K.V
    V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2524 89bf6593 Aneesh Kumar K.V
2525 bccacf6c Aneesh Kumar K.V
    olddirfidp = get_fid(pdu, olddirfid);
2526 89bf6593 Aneesh Kumar K.V
    if (olddirfidp == NULL) {
2527 89bf6593 Aneesh Kumar K.V
        err = -ENOENT;
2528 89bf6593 Aneesh Kumar K.V
        goto out;
2529 89bf6593 Aneesh Kumar K.V
    }
2530 89bf6593 Aneesh Kumar K.V
    if (newdirfid != -1) {
2531 bccacf6c Aneesh Kumar K.V
        newdirfidp = get_fid(pdu, newdirfid);
2532 89bf6593 Aneesh Kumar K.V
        if (newdirfidp == NULL) {
2533 89bf6593 Aneesh Kumar K.V
            err = -ENOENT;
2534 89bf6593 Aneesh Kumar K.V
            goto out;
2535 89bf6593 Aneesh Kumar K.V
        }
2536 89bf6593 Aneesh Kumar K.V
    } else {
2537 bccacf6c Aneesh Kumar K.V
        newdirfidp = get_fid(pdu, olddirfid);
2538 89bf6593 Aneesh Kumar K.V
    }
2539 89bf6593 Aneesh Kumar K.V
2540 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2541 2289be19 Aneesh Kumar K.V
                           &newdirfidp->path, new_name);
2542 2289be19 Aneesh Kumar K.V
    if (err < 0) {
2543 2289be19 Aneesh Kumar K.V
        goto out;
2544 89bf6593 Aneesh Kumar K.V
    }
2545 c98f1d4a Aneesh Kumar K.V
    if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2546 532decb7 Aneesh Kumar K.V
        /* Only for path based fid  we need to do the below fixup */
2547 bccacf6c Aneesh Kumar K.V
        v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2548 532decb7 Aneesh Kumar K.V
                           &newdirfidp->path, new_name);
2549 532decb7 Aneesh Kumar K.V
    }
2550 89bf6593 Aneesh Kumar K.V
out:
2551 89bf6593 Aneesh Kumar K.V
    if (olddirfidp) {
2552 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, olddirfidp);
2553 89bf6593 Aneesh Kumar K.V
    }
2554 89bf6593 Aneesh Kumar K.V
    if (newdirfidp) {
2555 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, newdirfidp);
2556 89bf6593 Aneesh Kumar K.V
    }
2557 89bf6593 Aneesh Kumar K.V
    return err;
2558 89bf6593 Aneesh Kumar K.V
}
2559 89bf6593 Aneesh Kumar K.V
2560 89bf6593 Aneesh Kumar K.V
static void v9fs_renameat(void *opaque)
2561 89bf6593 Aneesh Kumar K.V
{
2562 89bf6593 Aneesh Kumar K.V
    ssize_t err = 0;
2563 89bf6593 Aneesh Kumar K.V
    size_t offset = 7;
2564 89bf6593 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2565 89bf6593 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2566 89bf6593 Aneesh Kumar K.V
    int32_t olddirfid, newdirfid;
2567 89bf6593 Aneesh Kumar K.V
    V9fsString old_name, new_name;
2568 89bf6593 Aneesh Kumar K.V
2569 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&old_name);
2570 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&new_name);
2571 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2572 ddca7f86 M. Mohan Kumar
                        &old_name, &newdirfid, &new_name);
2573 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2574 ddca7f86 M. Mohan Kumar
        goto out_err;
2575 ddca7f86 M. Mohan Kumar
    }
2576 89bf6593 Aneesh Kumar K.V
2577 532decb7 Aneesh Kumar K.V
    v9fs_path_write_lock(s);
2578 bccacf6c Aneesh Kumar K.V
    err = v9fs_complete_renameat(pdu, olddirfid,
2579 bccacf6c Aneesh Kumar K.V
                                 &old_name, newdirfid, &new_name);
2580 532decb7 Aneesh Kumar K.V
    v9fs_path_unlock(s);
2581 89bf6593 Aneesh Kumar K.V
    if (!err) {
2582 89bf6593 Aneesh Kumar K.V
        err = offset;
2583 89bf6593 Aneesh Kumar K.V
    }
2584 ddca7f86 M. Mohan Kumar
2585 ddca7f86 M. Mohan Kumar
out_err:
2586 89bf6593 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2587 89bf6593 Aneesh Kumar K.V
    v9fs_string_free(&old_name);
2588 89bf6593 Aneesh Kumar K.V
    v9fs_string_free(&new_name);
2589 89bf6593 Aneesh Kumar K.V
}
2590 89bf6593 Aneesh Kumar K.V
2591 b81d685e Aneesh Kumar K.V
static void v9fs_wstat(void *opaque)
2592 8cf89e00 Anthony Liguori
{
2593 b81d685e Aneesh Kumar K.V
    int32_t fid;
2594 b81d685e Aneesh Kumar K.V
    int err = 0;
2595 b81d685e Aneesh Kumar K.V
    int16_t unused;
2596 b81d685e Aneesh Kumar K.V
    V9fsStat v9stat;
2597 b81d685e Aneesh Kumar K.V
    size_t offset = 7;
2598 b81d685e Aneesh Kumar K.V
    struct stat stbuf;
2599 b81d685e Aneesh Kumar K.V
    V9fsFidState *fidp;
2600 b81d685e Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2601 b81d685e Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2602 8cf89e00 Anthony Liguori
2603 ddca7f86 M. Mohan Kumar
    v9fs_stat_init(&v9stat);
2604 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2605 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2606 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2607 ddca7f86 M. Mohan Kumar
    }
2608 c572f23a Harsh Prateek Bora
    trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2609 c572f23a Harsh Prateek Bora
                     v9stat.mode, v9stat.atime, v9stat.mtime);
2610 84dfb926 Aneesh Kumar K.V
2611 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2612 b81d685e Aneesh Kumar K.V
    if (fidp == NULL) {
2613 b81d685e Aneesh Kumar K.V
        err = -EINVAL;
2614 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2615 8cf89e00 Anthony Liguori
    }
2616 b81d685e Aneesh Kumar K.V
    /* do we need to sync the file? */
2617 b81d685e Aneesh Kumar K.V
    if (donttouch_stat(&v9stat)) {
2618 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_fsync(pdu, fidp, 0);
2619 8cf89e00 Anthony Liguori
        goto out;
2620 8cf89e00 Anthony Liguori
    }
2621 b81d685e Aneesh Kumar K.V
    if (v9stat.mode != -1) {
2622 b81d685e Aneesh Kumar K.V
        uint32_t v9_mode;
2623 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2624 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2625 b81d685e Aneesh Kumar K.V
            goto out;
2626 b81d685e Aneesh Kumar K.V
        }
2627 b81d685e Aneesh Kumar K.V
        v9_mode = stat_to_v9mode(&stbuf);
2628 b81d685e Aneesh Kumar K.V
        if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2629 b81d685e Aneesh Kumar K.V
            (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2630 b81d685e Aneesh Kumar K.V
            /* Attempting to change the type */
2631 b81d685e Aneesh Kumar K.V
            err = -EIO;
2632 b81d685e Aneesh Kumar K.V
            goto out;
2633 b81d685e Aneesh Kumar K.V
        }
2634 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chmod(pdu, &fidp->path,
2635 b81d685e Aneesh Kumar K.V
                            v9mode_to_mode(v9stat.mode,
2636 b81d685e Aneesh Kumar K.V
                                           &v9stat.extension));
2637 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2638 b81d685e Aneesh Kumar K.V
            goto out;
2639 b81d685e Aneesh Kumar K.V
        }
2640 b81d685e Aneesh Kumar K.V
    }
2641 b81d685e Aneesh Kumar K.V
    if (v9stat.mtime != -1 || v9stat.atime != -1) {
2642 8fc39ae4 Sripathi Kodi
        struct timespec times[2];
2643 b81d685e Aneesh Kumar K.V
        if (v9stat.atime != -1) {
2644 b81d685e Aneesh Kumar K.V
            times[0].tv_sec = v9stat.atime;
2645 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = 0;
2646 8fc39ae4 Sripathi Kodi
        } else {
2647 8fc39ae4 Sripathi Kodi
            times[0].tv_nsec = UTIME_OMIT;
2648 8fc39ae4 Sripathi Kodi
        }
2649 b81d685e Aneesh Kumar K.V
        if (v9stat.mtime != -1) {
2650 b81d685e Aneesh Kumar K.V
            times[1].tv_sec = v9stat.mtime;
2651 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = 0;
2652 8fc39ae4 Sripathi Kodi
        } else {
2653 8fc39ae4 Sripathi Kodi
            times[1].tv_nsec = UTIME_OMIT;
2654 8fc39ae4 Sripathi Kodi
        }
2655 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_utimensat(pdu, &fidp->path, times);
2656 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2657 b81d685e Aneesh Kumar K.V
            goto out;
2658 8cf89e00 Anthony Liguori
        }
2659 8cf89e00 Anthony Liguori
    }
2660 b81d685e Aneesh Kumar K.V
    if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2661 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2662 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2663 8cf89e00 Anthony Liguori
            goto out;
2664 b81d685e Aneesh Kumar K.V
        }
2665 8cf89e00 Anthony Liguori
    }
2666 b81d685e Aneesh Kumar K.V
    if (v9stat.name.size != 0) {
2667 bccacf6c Aneesh Kumar K.V
        err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2668 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2669 b81d685e Aneesh Kumar K.V
            goto out;
2670 b81d685e Aneesh Kumar K.V
        }
2671 8cf89e00 Anthony Liguori
    }
2672 b81d685e Aneesh Kumar K.V
    if (v9stat.length != -1) {
2673 bccacf6c Aneesh Kumar K.V
        err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2674 b81d685e Aneesh Kumar K.V
        if (err < 0) {
2675 b81d685e Aneesh Kumar K.V
            goto out;
2676 b81d685e Aneesh Kumar K.V
        }
2677 8cf89e00 Anthony Liguori
    }
2678 b81d685e Aneesh Kumar K.V
    err = offset;
2679 8cf89e00 Anthony Liguori
out:
2680 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2681 84dfb926 Aneesh Kumar K.V
out_nofid:
2682 b81d685e Aneesh Kumar K.V
    v9fs_stat_free(&v9stat);
2683 b81d685e Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2684 9f107513 Anthony Liguori
}
2685 9f107513 Anthony Liguori
2686 88a4763e Aneesh Kumar K.V
static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2687 88a4763e Aneesh Kumar K.V
{
2688 88a4763e Aneesh Kumar K.V
    uint32_t f_type;
2689 88a4763e Aneesh Kumar K.V
    uint32_t f_bsize;
2690 88a4763e Aneesh Kumar K.V
    uint64_t f_blocks;
2691 88a4763e Aneesh Kumar K.V
    uint64_t f_bfree;
2692 88a4763e Aneesh Kumar K.V
    uint64_t f_bavail;
2693 88a4763e Aneesh Kumar K.V
    uint64_t f_files;
2694 88a4763e Aneesh Kumar K.V
    uint64_t f_ffree;
2695 88a4763e Aneesh Kumar K.V
    uint64_t fsid_val;
2696 88a4763e Aneesh Kumar K.V
    uint32_t f_namelen;
2697 88a4763e Aneesh Kumar K.V
    size_t offset = 7;
2698 5e94c103 M. Mohan Kumar
    int32_t bsize_factor;
2699 5e94c103 M. Mohan Kumar
2700 5e94c103 M. Mohan Kumar
    /*
2701 5e94c103 M. Mohan Kumar
     * compute bsize factor based on host file system block size
2702 5e94c103 M. Mohan Kumar
     * and client msize
2703 5e94c103 M. Mohan Kumar
     */
2704 88a4763e Aneesh Kumar K.V
    bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2705 5e94c103 M. Mohan Kumar
    if (!bsize_factor) {
2706 5e94c103 M. Mohan Kumar
        bsize_factor = 1;
2707 5e94c103 M. Mohan Kumar
    }
2708 88a4763e Aneesh Kumar K.V
    f_type  = stbuf->f_type;
2709 88a4763e Aneesh Kumar K.V
    f_bsize = stbuf->f_bsize;
2710 88a4763e Aneesh Kumar K.V
    f_bsize *= bsize_factor;
2711 5e94c103 M. Mohan Kumar
    /*
2712 5e94c103 M. Mohan Kumar
     * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2713 5e94c103 M. Mohan Kumar
     * adjust(divide) the number of blocks, free blocks and available
2714 5e94c103 M. Mohan Kumar
     * blocks by bsize factor
2715 5e94c103 M. Mohan Kumar
     */
2716 88a4763e Aneesh Kumar K.V
    f_blocks = stbuf->f_blocks/bsize_factor;
2717 88a4763e Aneesh Kumar K.V
    f_bfree  = stbuf->f_bfree/bsize_factor;
2718 88a4763e Aneesh Kumar K.V
    f_bavail = stbuf->f_bavail/bsize_factor;
2719 88a4763e Aneesh Kumar K.V
    f_files  = stbuf->f_files;
2720 88a4763e Aneesh Kumar K.V
    f_ffree  = stbuf->f_ffree;
2721 88a4763e Aneesh Kumar K.V
    fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2722 88a4763e Aneesh Kumar K.V
               (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2723 88a4763e Aneesh Kumar K.V
    f_namelen = stbuf->f_namelen;
2724 be940c87 M. Mohan Kumar
2725 88a4763e Aneesh Kumar K.V
    return pdu_marshal(pdu, offset, "ddqqqqqqd",
2726 88a4763e Aneesh Kumar K.V
                       f_type, f_bsize, f_blocks, f_bfree,
2727 88a4763e Aneesh Kumar K.V
                       f_bavail, f_files, f_ffree,
2728 88a4763e Aneesh Kumar K.V
                       fsid_val, f_namelen);
2729 be940c87 M. Mohan Kumar
}
2730 be940c87 M. Mohan Kumar
2731 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_statfs(void *opaque)
2732 be940c87 M. Mohan Kumar
{
2733 88a4763e Aneesh Kumar K.V
    int32_t fid;
2734 88a4763e Aneesh Kumar K.V
    ssize_t retval = 0;
2735 88a4763e Aneesh Kumar K.V
    size_t offset = 7;
2736 88a4763e Aneesh Kumar K.V
    V9fsFidState *fidp;
2737 88a4763e Aneesh Kumar K.V
    struct statfs stbuf;
2738 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2739 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2740 be940c87 M. Mohan Kumar
2741 ddca7f86 M. Mohan Kumar
    retval = pdu_unmarshal(pdu, offset, "d", &fid);
2742 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
2743 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2744 ddca7f86 M. Mohan Kumar
    }
2745 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2746 88a4763e Aneesh Kumar K.V
    if (fidp == NULL) {
2747 88a4763e Aneesh Kumar K.V
        retval = -ENOENT;
2748 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2749 be940c87 M. Mohan Kumar
    }
2750 bccacf6c Aneesh Kumar K.V
    retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2751 88a4763e Aneesh Kumar K.V
    if (retval < 0) {
2752 88a4763e Aneesh Kumar K.V
        goto out;
2753 88a4763e Aneesh Kumar K.V
    }
2754 ddca7f86 M. Mohan Kumar
    retval = v9fs_fill_statfs(s, pdu, &stbuf);
2755 ddca7f86 M. Mohan Kumar
    if (retval < 0) {
2756 ddca7f86 M. Mohan Kumar
        goto out;
2757 ddca7f86 M. Mohan Kumar
    }
2758 ddca7f86 M. Mohan Kumar
    retval += offset;
2759 be940c87 M. Mohan Kumar
out:
2760 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2761 84dfb926 Aneesh Kumar K.V
out_nofid:
2762 88a4763e Aneesh Kumar K.V
    complete_pdu(s, pdu, retval);
2763 be940c87 M. Mohan Kumar
}
2764 be940c87 M. Mohan Kumar
2765 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_mknod(void *opaque)
2766 5268cecc M. Mohan Kumar
{
2767 1b733fed Aneesh Kumar K.V
2768 1b733fed Aneesh Kumar K.V
    int mode;
2769 1b733fed Aneesh Kumar K.V
    gid_t gid;
2770 5268cecc M. Mohan Kumar
    int32_t fid;
2771 1b733fed Aneesh Kumar K.V
    V9fsQID qid;
2772 5268cecc M. Mohan Kumar
    int err = 0;
2773 5268cecc M. Mohan Kumar
    int major, minor;
2774 1b733fed Aneesh Kumar K.V
    size_t offset = 7;
2775 1b733fed Aneesh Kumar K.V
    V9fsString name;
2776 1b733fed Aneesh Kumar K.V
    struct stat stbuf;
2777 1b733fed Aneesh Kumar K.V
    V9fsFidState *fidp;
2778 1b733fed Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2779 1b733fed Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2780 5268cecc M. Mohan Kumar
2781 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2782 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2783 ddca7f86 M. Mohan Kumar
                        &major, &minor, &gid);
2784 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2785 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2786 ddca7f86 M. Mohan Kumar
    }
2787 c572f23a Harsh Prateek Bora
    trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2788 5268cecc M. Mohan Kumar
2789 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2790 5268cecc M. Mohan Kumar
    if (fidp == NULL) {
2791 5268cecc M. Mohan Kumar
        err = -ENOENT;
2792 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2793 5268cecc M. Mohan Kumar
    }
2794 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
2795 02cb7f3a Aneesh Kumar K.V
                        makedev(major, minor), mode, &stbuf);
2796 1b733fed Aneesh Kumar K.V
    if (err < 0) {
2797 1b733fed Aneesh Kumar K.V
        goto out;
2798 1b733fed Aneesh Kumar K.V
    }
2799 1b733fed Aneesh Kumar K.V
    stat_to_qid(&stbuf, &qid);
2800 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Q", &qid);
2801 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2802 ddca7f86 M. Mohan Kumar
        goto out;
2803 ddca7f86 M. Mohan Kumar
    }
2804 ddca7f86 M. Mohan Kumar
    err += offset;
2805 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_mknod_return(pdu->tag, pdu->id,
2806 7999f7e1 Aneesh Kumar K.V
                            qid.type, qid.version, qid.path);
2807 5268cecc M. Mohan Kumar
out:
2808 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2809 84dfb926 Aneesh Kumar K.V
out_nofid:
2810 1b733fed Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2811 1b733fed Aneesh Kumar K.V
    v9fs_string_free(&name);
2812 5268cecc M. Mohan Kumar
}
2813 5268cecc M. Mohan Kumar
2814 82cc3ee8 M. Mohan Kumar
/*
2815 82cc3ee8 M. Mohan Kumar
 * Implement posix byte range locking code
2816 82cc3ee8 M. Mohan Kumar
 * Server side handling of locking code is very simple, because 9p server in
2817 82cc3ee8 M. Mohan Kumar
 * QEMU can handle only one client. And most of the lock handling
2818 82cc3ee8 M. Mohan Kumar
 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2819 82cc3ee8 M. Mohan Kumar
 * do any thing in * qemu 9p server side lock code path.
2820 82cc3ee8 M. Mohan Kumar
 * So when a TLOCK request comes, always return success
2821 82cc3ee8 M. Mohan Kumar
 */
2822 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_lock(void *opaque)
2823 82cc3ee8 M. Mohan Kumar
{
2824 0c27bf2a Aneesh Kumar K.V
    int8_t status;
2825 ddca7f86 M. Mohan Kumar
    V9fsFlock flock;
2826 0c27bf2a Aneesh Kumar K.V
    size_t offset = 7;
2827 0c27bf2a Aneesh Kumar K.V
    struct stat stbuf;
2828 0c27bf2a Aneesh Kumar K.V
    V9fsFidState *fidp;
2829 0c27bf2a Aneesh Kumar K.V
    int32_t fid, err = 0;
2830 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2831 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2832 82cc3ee8 M. Mohan Kumar
2833 ddca7f86 M. Mohan Kumar
    status = P9_LOCK_ERROR;
2834 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&flock.client_id);
2835 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
2836 ddca7f86 M. Mohan Kumar
                        &flock.flags, &flock.start, &flock.length,
2837 ddca7f86 M. Mohan Kumar
                        &flock.proc_id, &flock.client_id);
2838 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2839 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2840 ddca7f86 M. Mohan Kumar
    }
2841 c572f23a Harsh Prateek Bora
    trace_v9fs_lock(pdu->tag, pdu->id, fid,
2842 ddca7f86 M. Mohan Kumar
                    flock.type, flock.start, flock.length);
2843 c572f23a Harsh Prateek Bora
2844 82cc3ee8 M. Mohan Kumar
2845 82cc3ee8 M. Mohan Kumar
    /* We support only block flag now (that too ignored currently) */
2846 ddca7f86 M. Mohan Kumar
    if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
2847 82cc3ee8 M. Mohan Kumar
        err = -EINVAL;
2848 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2849 82cc3ee8 M. Mohan Kumar
    }
2850 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2851 0c27bf2a Aneesh Kumar K.V
    if (fidp == NULL) {
2852 82cc3ee8 M. Mohan Kumar
        err = -ENOENT;
2853 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2854 82cc3ee8 M. Mohan Kumar
    }
2855 cc720ddb Aneesh Kumar K.V
    err = v9fs_co_fstat(pdu, fidp, &stbuf);
2856 82cc3ee8 M. Mohan Kumar
    if (err < 0) {
2857 82cc3ee8 M. Mohan Kumar
        goto out;
2858 82cc3ee8 M. Mohan Kumar
    }
2859 0c27bf2a Aneesh Kumar K.V
    status = P9_LOCK_SUCCESS;
2860 82cc3ee8 M. Mohan Kumar
out:
2861 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2862 84dfb926 Aneesh Kumar K.V
out_nofid:
2863 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "b", status);
2864 ddca7f86 M. Mohan Kumar
    if (err > 0) {
2865 ddca7f86 M. Mohan Kumar
        err += offset;
2866 ddca7f86 M. Mohan Kumar
    }
2867 c572f23a Harsh Prateek Bora
    trace_v9fs_lock_return(pdu->tag, pdu->id, status);
2868 0c27bf2a Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2869 ddca7f86 M. Mohan Kumar
    v9fs_string_free(&flock.client_id);
2870 82cc3ee8 M. Mohan Kumar
}
2871 82cc3ee8 M. Mohan Kumar
2872 8f354003 M. Mohan Kumar
/*
2873 8f354003 M. Mohan Kumar
 * When a TGETLOCK request comes, always return success because all lock
2874 8f354003 M. Mohan Kumar
 * handling is done by client's VFS layer.
2875 8f354003 M. Mohan Kumar
 */
2876 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_getlock(void *opaque)
2877 8f354003 M. Mohan Kumar
{
2878 e4e414a4 Aneesh Kumar K.V
    size_t offset = 7;
2879 e4e414a4 Aneesh Kumar K.V
    struct stat stbuf;
2880 e4e414a4 Aneesh Kumar K.V
    V9fsFidState *fidp;
2881 ddca7f86 M. Mohan Kumar
    V9fsGetlock glock;
2882 e4e414a4 Aneesh Kumar K.V
    int32_t fid, err = 0;
2883 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2884 ff06030f Venkateswararao Jujjuri (JV)
    V9fsState *s = pdu->s;
2885 8f354003 M. Mohan Kumar
2886 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&glock.client_id);
2887 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
2888 ddca7f86 M. Mohan Kumar
                        &glock.start, &glock.length, &glock.proc_id,
2889 ddca7f86 M. Mohan Kumar
                        &glock.client_id);
2890 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2891 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2892 ddca7f86 M. Mohan Kumar
    }
2893 c572f23a Harsh Prateek Bora
    trace_v9fs_getlock(pdu->tag, pdu->id, fid,
2894 ddca7f86 M. Mohan Kumar
                       glock.type, glock.start, glock.length);
2895 c572f23a Harsh Prateek Bora
2896 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2897 e4e414a4 Aneesh Kumar K.V
    if (fidp == NULL) {
2898 8f354003 M. Mohan Kumar
        err = -ENOENT;
2899 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2900 8f354003 M. Mohan Kumar
    }
2901 cc720ddb Aneesh Kumar K.V
    err = v9fs_co_fstat(pdu, fidp, &stbuf);
2902 8f354003 M. Mohan Kumar
    if (err < 0) {
2903 8f354003 M. Mohan Kumar
        goto out;
2904 8f354003 M. Mohan Kumar
    }
2905 ddca7f86 M. Mohan Kumar
    glock.type = P9_LOCK_TYPE_UNLCK;
2906 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "bqqds", glock.type,
2907 ddca7f86 M. Mohan Kumar
                          glock.start, glock.length, glock.proc_id,
2908 ddca7f86 M. Mohan Kumar
                          &glock.client_id);
2909 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2910 ddca7f86 M. Mohan Kumar
        goto out;
2911 ddca7f86 M. Mohan Kumar
    }
2912 ddca7f86 M. Mohan Kumar
    err += offset;
2913 ddca7f86 M. Mohan Kumar
    trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
2914 ddca7f86 M. Mohan Kumar
                              glock.length, glock.proc_id);
2915 8f354003 M. Mohan Kumar
out:
2916 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2917 84dfb926 Aneesh Kumar K.V
out_nofid:
2918 e4e414a4 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
2919 ddca7f86 M. Mohan Kumar
    v9fs_string_free(&glock.client_id);
2920 8f354003 M. Mohan Kumar
}
2921 8f354003 M. Mohan Kumar
2922 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_mkdir(void *opaque)
2923 b67592ea M. Mohan Kumar
{
2924 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
2925 e84861f7 Venkateswararao Jujjuri
    size_t offset = 7;
2926 b67592ea M. Mohan Kumar
    int32_t fid;
2927 e84861f7 Venkateswararao Jujjuri
    struct stat stbuf;
2928 e84861f7 Venkateswararao Jujjuri
    V9fsQID qid;
2929 02cb7f3a Aneesh Kumar K.V
    V9fsString name;
2930 b67592ea M. Mohan Kumar
    V9fsFidState *fidp;
2931 b67592ea M. Mohan Kumar
    gid_t gid;
2932 b67592ea M. Mohan Kumar
    int mode;
2933 e84861f7 Venkateswararao Jujjuri
    int err = 0;
2934 b67592ea M. Mohan Kumar
2935 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2936 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
2937 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2938 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2939 ddca7f86 M. Mohan Kumar
    }
2940 c572f23a Harsh Prateek Bora
    trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
2941 c572f23a Harsh Prateek Bora
2942 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
2943 b67592ea M. Mohan Kumar
    if (fidp == NULL) {
2944 b67592ea M. Mohan Kumar
        err = -ENOENT;
2945 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2946 b67592ea M. Mohan Kumar
    }
2947 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
2948 e84861f7 Venkateswararao Jujjuri
    if (err < 0) {
2949 e84861f7 Venkateswararao Jujjuri
        goto out;
2950 e84861f7 Venkateswararao Jujjuri
    }
2951 e84861f7 Venkateswararao Jujjuri
    stat_to_qid(&stbuf, &qid);
2952 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "Q", &qid);
2953 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2954 ddca7f86 M. Mohan Kumar
        goto out;
2955 ddca7f86 M. Mohan Kumar
    }
2956 ddca7f86 M. Mohan Kumar
    err += offset;
2957 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_mkdir_return(pdu->tag, pdu->id,
2958 7999f7e1 Aneesh Kumar K.V
                            qid.type, qid.version, qid.path, err);
2959 b67592ea M. Mohan Kumar
out:
2960 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
2961 84dfb926 Aneesh Kumar K.V
out_nofid:
2962 e84861f7 Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
2963 e84861f7 Venkateswararao Jujjuri
    v9fs_string_free(&name);
2964 b67592ea M. Mohan Kumar
}
2965 b67592ea M. Mohan Kumar
2966 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_xattrwalk(void *opaque)
2967 fa32ef88 Aneesh Kumar K.V
{
2968 670185a6 Aneesh Kumar K.V
    int64_t size;
2969 670185a6 Aneesh Kumar K.V
    V9fsString name;
2970 fa32ef88 Aneesh Kumar K.V
    ssize_t err = 0;
2971 670185a6 Aneesh Kumar K.V
    size_t offset = 7;
2972 fa32ef88 Aneesh Kumar K.V
    int32_t fid, newfid;
2973 670185a6 Aneesh Kumar K.V
    V9fsFidState *file_fidp;
2974 84dfb926 Aneesh Kumar K.V
    V9fsFidState *xattr_fidp = NULL;
2975 670185a6 Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
2976 670185a6 Aneesh Kumar K.V
    V9fsState *s = pdu->s;
2977 fa32ef88 Aneesh Kumar K.V
2978 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
2979 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
2980 ddca7f86 M. Mohan Kumar
    if (err < 0) {
2981 ddca7f86 M. Mohan Kumar
        goto out_nofid;
2982 ddca7f86 M. Mohan Kumar
    }
2983 c572f23a Harsh Prateek Bora
    trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
2984 c572f23a Harsh Prateek Bora
2985 bccacf6c Aneesh Kumar K.V
    file_fidp = get_fid(pdu, fid);
2986 670185a6 Aneesh Kumar K.V
    if (file_fidp == NULL) {
2987 fa32ef88 Aneesh Kumar K.V
        err = -ENOENT;
2988 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
2989 fa32ef88 Aneesh Kumar K.V
    }
2990 670185a6 Aneesh Kumar K.V
    xattr_fidp = alloc_fid(s, newfid);
2991 670185a6 Aneesh Kumar K.V
    if (xattr_fidp == NULL) {
2992 fa32ef88 Aneesh Kumar K.V
        err = -EINVAL;
2993 fa32ef88 Aneesh Kumar K.V
        goto out;
2994 fa32ef88 Aneesh Kumar K.V
    }
2995 2289be19 Aneesh Kumar K.V
    v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
2996 ddca7f86 M. Mohan Kumar
    if (name.data == NULL) {
2997 fa32ef88 Aneesh Kumar K.V
        /*
2998 fa32ef88 Aneesh Kumar K.V
         * listxattr request. Get the size first
2999 fa32ef88 Aneesh Kumar K.V
         */
3000 bccacf6c Aneesh Kumar K.V
        size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3001 670185a6 Aneesh Kumar K.V
        if (size < 0) {
3002 670185a6 Aneesh Kumar K.V
            err = size;
3003 84dfb926 Aneesh Kumar K.V
            clunk_fid(s, xattr_fidp->fid);
3004 670185a6 Aneesh Kumar K.V
            goto out;
3005 fa32ef88 Aneesh Kumar K.V
        }
3006 670185a6 Aneesh Kumar K.V
        /*
3007 670185a6 Aneesh Kumar K.V
         * Read the xattr value
3008 670185a6 Aneesh Kumar K.V
         */
3009 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.len = size;
3010 670185a6 Aneesh Kumar K.V
        xattr_fidp->fid_type = P9_FID_XATTR;
3011 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.copied_len = -1;
3012 670185a6 Aneesh Kumar K.V
        if (size) {
3013 7267c094 Anthony Liguori
            xattr_fidp->fs.xattr.value = g_malloc(size);
3014 bccacf6c Aneesh Kumar K.V
            err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3015 670185a6 Aneesh Kumar K.V
                                     xattr_fidp->fs.xattr.value,
3016 670185a6 Aneesh Kumar K.V
                                     xattr_fidp->fs.xattr.len);
3017 670185a6 Aneesh Kumar K.V
            if (err < 0) {
3018 84dfb926 Aneesh Kumar K.V
                clunk_fid(s, xattr_fidp->fid);
3019 670185a6 Aneesh Kumar K.V
                goto out;
3020 670185a6 Aneesh Kumar K.V
            }
3021 670185a6 Aneesh Kumar K.V
        }
3022 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "q", size);
3023 ddca7f86 M. Mohan Kumar
        if (err < 0) {
3024 ddca7f86 M. Mohan Kumar
            goto out;
3025 ddca7f86 M. Mohan Kumar
        }
3026 ddca7f86 M. Mohan Kumar
        err += offset;
3027 fa32ef88 Aneesh Kumar K.V
    } else {
3028 fa32ef88 Aneesh Kumar K.V
        /*
3029 fa32ef88 Aneesh Kumar K.V
         * specific xattr fid. We check for xattr
3030 fa32ef88 Aneesh Kumar K.V
         * presence also collect the xattr size
3031 fa32ef88 Aneesh Kumar K.V
         */
3032 bccacf6c Aneesh Kumar K.V
        size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3033 670185a6 Aneesh Kumar K.V
                                 &name, NULL, 0);
3034 670185a6 Aneesh Kumar K.V
        if (size < 0) {
3035 670185a6 Aneesh Kumar K.V
            err = size;
3036 84dfb926 Aneesh Kumar K.V
            clunk_fid(s, xattr_fidp->fid);
3037 670185a6 Aneesh Kumar K.V
            goto out;
3038 fa32ef88 Aneesh Kumar K.V
        }
3039 670185a6 Aneesh Kumar K.V
        /*
3040 670185a6 Aneesh Kumar K.V
         * Read the xattr value
3041 670185a6 Aneesh Kumar K.V
         */
3042 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.len = size;
3043 670185a6 Aneesh Kumar K.V
        xattr_fidp->fid_type = P9_FID_XATTR;
3044 670185a6 Aneesh Kumar K.V
        xattr_fidp->fs.xattr.copied_len = -1;
3045 670185a6 Aneesh Kumar K.V
        if (size) {
3046 7267c094 Anthony Liguori
            xattr_fidp->fs.xattr.value = g_malloc(size);
3047 bccacf6c Aneesh Kumar K.V
            err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3048 670185a6 Aneesh Kumar K.V
                                    &name, xattr_fidp->fs.xattr.value,
3049 670185a6 Aneesh Kumar K.V
                                    xattr_fidp->fs.xattr.len);
3050 670185a6 Aneesh Kumar K.V
            if (err < 0) {
3051 84dfb926 Aneesh Kumar K.V
                clunk_fid(s, xattr_fidp->fid);
3052 670185a6 Aneesh Kumar K.V
                goto out;
3053 670185a6 Aneesh Kumar K.V
            }
3054 670185a6 Aneesh Kumar K.V
        }
3055 ddca7f86 M. Mohan Kumar
        err = pdu_marshal(pdu, offset, "q", size);
3056 ddca7f86 M. Mohan Kumar
        if (err < 0) {
3057 ddca7f86 M. Mohan Kumar
            goto out;
3058 ddca7f86 M. Mohan Kumar
        }
3059 ddca7f86 M. Mohan Kumar
        err += offset;
3060 fa32ef88 Aneesh Kumar K.V
    }
3061 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3062 fa32ef88 Aneesh Kumar K.V
out:
3063 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, file_fidp);
3064 84dfb926 Aneesh Kumar K.V
    if (xattr_fidp) {
3065 bccacf6c Aneesh Kumar K.V
        put_fid(pdu, xattr_fidp);
3066 84dfb926 Aneesh Kumar K.V
    }
3067 84dfb926 Aneesh Kumar K.V
out_nofid:
3068 670185a6 Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
3069 670185a6 Aneesh Kumar K.V
    v9fs_string_free(&name);
3070 fa32ef88 Aneesh Kumar K.V
}
3071 fa32ef88 Aneesh Kumar K.V
3072 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_xattrcreate(void *opaque)
3073 10b468bd Aneesh Kumar K.V
{
3074 10b468bd Aneesh Kumar K.V
    int flags;
3075 10b468bd Aneesh Kumar K.V
    int32_t fid;
3076 f10ff58d Aneesh Kumar K.V
    int64_t size;
3077 10b468bd Aneesh Kumar K.V
    ssize_t err = 0;
3078 f10ff58d Aneesh Kumar K.V
    V9fsString name;
3079 f10ff58d Aneesh Kumar K.V
    size_t offset = 7;
3080 f10ff58d Aneesh Kumar K.V
    V9fsFidState *file_fidp;
3081 f10ff58d Aneesh Kumar K.V
    V9fsFidState *xattr_fidp;
3082 f10ff58d Aneesh Kumar K.V
    V9fsPDU *pdu = opaque;
3083 f10ff58d Aneesh Kumar K.V
    V9fsState *s = pdu->s;
3084 10b468bd Aneesh Kumar K.V
3085 ddca7f86 M. Mohan Kumar
    v9fs_string_init(&name);
3086 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3087 ddca7f86 M. Mohan Kumar
    if (err < 0) {
3088 ddca7f86 M. Mohan Kumar
        goto out_nofid;
3089 ddca7f86 M. Mohan Kumar
    }
3090 c572f23a Harsh Prateek Bora
    trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3091 10b468bd Aneesh Kumar K.V
3092 bccacf6c Aneesh Kumar K.V
    file_fidp = get_fid(pdu, fid);
3093 f10ff58d Aneesh Kumar K.V
    if (file_fidp == NULL) {
3094 10b468bd Aneesh Kumar K.V
        err = -EINVAL;
3095 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
3096 10b468bd Aneesh Kumar K.V
    }
3097 10b468bd Aneesh Kumar K.V
    /* Make the file fid point to xattr */
3098 f10ff58d Aneesh Kumar K.V
    xattr_fidp = file_fidp;
3099 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fid_type = P9_FID_XATTR;
3100 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fs.xattr.copied_len = 0;
3101 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fs.xattr.len = size;
3102 f10ff58d Aneesh Kumar K.V
    xattr_fidp->fs.xattr.flags = flags;
3103 f10ff58d Aneesh Kumar K.V
    v9fs_string_init(&xattr_fidp->fs.xattr.name);
3104 f10ff58d Aneesh Kumar K.V
    v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3105 6528499f Markus Armbruster
    xattr_fidp->fs.xattr.value = g_malloc(size);
3106 f10ff58d Aneesh Kumar K.V
    err = offset;
3107 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, file_fidp);
3108 84dfb926 Aneesh Kumar K.V
out_nofid:
3109 f10ff58d Aneesh Kumar K.V
    complete_pdu(s, pdu, err);
3110 f10ff58d Aneesh Kumar K.V
    v9fs_string_free(&name);
3111 10b468bd Aneesh Kumar K.V
}
3112 fa32ef88 Aneesh Kumar K.V
3113 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_readlink(void *opaque)
3114 df0973a4 M. Mohan Kumar
{
3115 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
3116 7a5ca31e Venkateswararao Jujjuri
    size_t offset = 7;
3117 7a5ca31e Venkateswararao Jujjuri
    V9fsString target;
3118 df0973a4 M. Mohan Kumar
    int32_t fid;
3119 df0973a4 M. Mohan Kumar
    int err = 0;
3120 df0973a4 M. Mohan Kumar
    V9fsFidState *fidp;
3121 df0973a4 M. Mohan Kumar
3122 ddca7f86 M. Mohan Kumar
    err = pdu_unmarshal(pdu, offset, "d", &fid);
3123 ddca7f86 M. Mohan Kumar
    if (err < 0) {
3124 ddca7f86 M. Mohan Kumar
        goto out_nofid;
3125 ddca7f86 M. Mohan Kumar
    }
3126 c572f23a Harsh Prateek Bora
    trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3127 bccacf6c Aneesh Kumar K.V
    fidp = get_fid(pdu, fid);
3128 df0973a4 M. Mohan Kumar
    if (fidp == NULL) {
3129 df0973a4 M. Mohan Kumar
        err = -ENOENT;
3130 84dfb926 Aneesh Kumar K.V
        goto out_nofid;
3131 df0973a4 M. Mohan Kumar
    }
3132 df0973a4 M. Mohan Kumar
3133 7a5ca31e Venkateswararao Jujjuri
    v9fs_string_init(&target);
3134 bccacf6c Aneesh Kumar K.V
    err = v9fs_co_readlink(pdu, &fidp->path, &target);
3135 7a5ca31e Venkateswararao Jujjuri
    if (err < 0) {
3136 7a5ca31e Venkateswararao Jujjuri
        goto out;
3137 7a5ca31e Venkateswararao Jujjuri
    }
3138 ddca7f86 M. Mohan Kumar
    err = pdu_marshal(pdu, offset, "s", &target);
3139 ddca7f86 M. Mohan Kumar
    if (err < 0) {
3140 ddca7f86 M. Mohan Kumar
        v9fs_string_free(&target);
3141 ddca7f86 M. Mohan Kumar
        goto out;
3142 ddca7f86 M. Mohan Kumar
    }
3143 ddca7f86 M. Mohan Kumar
    err += offset;
3144 7999f7e1 Aneesh Kumar K.V
    trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3145 7a5ca31e Venkateswararao Jujjuri
    v9fs_string_free(&target);
3146 df0973a4 M. Mohan Kumar
out:
3147 bccacf6c Aneesh Kumar K.V
    put_fid(pdu, fidp);
3148 84dfb926 Aneesh Kumar K.V
out_nofid:
3149 7a5ca31e Venkateswararao Jujjuri
    complete_pdu(pdu->s, pdu, err);
3150 df0973a4 M. Mohan Kumar
}
3151 df0973a4 M. Mohan Kumar
3152 ff06030f Venkateswararao Jujjuri (JV)
static CoroutineEntry *pdu_co_handlers[] = {
3153 c18e2f94 Sripathi Kodi
    [P9_TREADDIR] = v9fs_readdir,
3154 be940c87 M. Mohan Kumar
    [P9_TSTATFS] = v9fs_statfs,
3155 00ede4c2 Sripathi Kodi
    [P9_TGETATTR] = v9fs_getattr,
3156 c79ce737 Sripathi Kodi
    [P9_TSETATTR] = v9fs_setattr,
3157 fa32ef88 Aneesh Kumar K.V
    [P9_TXATTRWALK] = v9fs_xattrwalk,
3158 10b468bd Aneesh Kumar K.V
    [P9_TXATTRCREATE] = v9fs_xattrcreate,
3159 5268cecc M. Mohan Kumar
    [P9_TMKNOD] = v9fs_mknod,
3160 c7b4b0b3 M. Mohan Kumar
    [P9_TRENAME] = v9fs_rename,
3161 82cc3ee8 M. Mohan Kumar
    [P9_TLOCK] = v9fs_lock,
3162 8f354003 M. Mohan Kumar
    [P9_TGETLOCK] = v9fs_getlock,
3163 89bf6593 Aneesh Kumar K.V
    [P9_TRENAMEAT] = v9fs_renameat,
3164 df0973a4 M. Mohan Kumar
    [P9_TREADLINK] = v9fs_readlink,
3165 7834cf77 Aneesh Kumar K.V
    [P9_TUNLINKAT] = v9fs_unlinkat,
3166 b67592ea M. Mohan Kumar
    [P9_TMKDIR] = v9fs_mkdir,
3167 9f107513 Anthony Liguori
    [P9_TVERSION] = v9fs_version,
3168 771e9d4c M. Mohan Kumar
    [P9_TLOPEN] = v9fs_open,
3169 9f107513 Anthony Liguori
    [P9_TATTACH] = v9fs_attach,
3170 9f107513 Anthony Liguori
    [P9_TSTAT] = v9fs_stat,
3171 9f107513 Anthony Liguori
    [P9_TWALK] = v9fs_walk,
3172 9f107513 Anthony Liguori
    [P9_TCLUNK] = v9fs_clunk,
3173 b41e95d3 Venkateswararao Jujjuri (JV)
    [P9_TFSYNC] = v9fs_fsync,
3174 9f107513 Anthony Liguori
    [P9_TOPEN] = v9fs_open,
3175 9f107513 Anthony Liguori
    [P9_TREAD] = v9fs_read,
3176 9f107513 Anthony Liguori
#if 0
3177 9f107513 Anthony Liguori
    [P9_TAUTH] = v9fs_auth,
3178 9f107513 Anthony Liguori
#endif
3179 9f107513 Anthony Liguori
    [P9_TFLUSH] = v9fs_flush,
3180 b2c224be Venkateswararao Jujjuri (JV)
    [P9_TLINK] = v9fs_link,
3181 08c60fc9 Venkateswararao Jujjuri (JV)
    [P9_TSYMLINK] = v9fs_symlink,
3182 9f107513 Anthony Liguori
    [P9_TCREATE] = v9fs_create,
3183 c1568af5 Venkateswararao Jujjuri (JV)
    [P9_TLCREATE] = v9fs_lcreate,
3184 9f107513 Anthony Liguori
    [P9_TWRITE] = v9fs_write,
3185 9f107513 Anthony Liguori
    [P9_TWSTAT] = v9fs_wstat,
3186 9f107513 Anthony Liguori
    [P9_TREMOVE] = v9fs_remove,
3187 9f107513 Anthony Liguori
};
3188 9f107513 Anthony Liguori
3189 ff06030f Venkateswararao Jujjuri (JV)
static void v9fs_op_not_supp(void *opaque)
3190 5c3234c6 Aneesh Kumar K.V
{
3191 ff06030f Venkateswararao Jujjuri (JV)
    V9fsPDU *pdu = opaque;
3192 ff06030f Venkateswararao Jujjuri (JV)
    complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
3193 5c3234c6 Aneesh Kumar K.V
}
3194 5c3234c6 Aneesh Kumar K.V
3195 2c74c2cb M. Mohan Kumar
static void v9fs_fs_ro(void *opaque)
3196 2c74c2cb M. Mohan Kumar
{
3197 2c74c2cb M. Mohan Kumar
    V9fsPDU *pdu = opaque;
3198 2c74c2cb M. Mohan Kumar
    complete_pdu(pdu->s, pdu, -EROFS);
3199 2c74c2cb M. Mohan Kumar
}
3200 2c74c2cb M. Mohan Kumar
3201 2c74c2cb M. Mohan Kumar
static inline bool is_read_only_op(V9fsPDU *pdu)
3202 2c74c2cb M. Mohan Kumar
{
3203 2c74c2cb M. Mohan Kumar
    switch (pdu->id) {
3204 2c74c2cb M. Mohan Kumar
    case P9_TREADDIR:
3205 2c74c2cb M. Mohan Kumar
    case P9_TSTATFS:
3206 2c74c2cb M. Mohan Kumar
    case P9_TGETATTR:
3207 2c74c2cb M. Mohan Kumar
    case P9_TXATTRWALK:
3208 2c74c2cb M. Mohan Kumar
    case P9_TLOCK:
3209 2c74c2cb M. Mohan Kumar
    case P9_TGETLOCK:
3210 2c74c2cb M. Mohan Kumar
    case P9_TREADLINK:
3211 2c74c2cb M. Mohan Kumar
    case P9_TVERSION:
3212 2c74c2cb M. Mohan Kumar
    case P9_TLOPEN:
3213 2c74c2cb M. Mohan Kumar
    case P9_TATTACH:
3214 2c74c2cb M. Mohan Kumar
    case P9_TSTAT:
3215 2c74c2cb M. Mohan Kumar
    case P9_TWALK:
3216 2c74c2cb M. Mohan Kumar
    case P9_TCLUNK:
3217 2c74c2cb M. Mohan Kumar
    case P9_TFSYNC:
3218 2c74c2cb M. Mohan Kumar
    case P9_TOPEN:
3219 2c74c2cb M. Mohan Kumar
    case P9_TREAD:
3220 2c74c2cb M. Mohan Kumar
    case P9_TAUTH:
3221 2c74c2cb M. Mohan Kumar
    case P9_TFLUSH:
3222 2c74c2cb M. Mohan Kumar
        return 1;
3223 2c74c2cb M. Mohan Kumar
    default:
3224 2c74c2cb M. Mohan Kumar
        return 0;
3225 2c74c2cb M. Mohan Kumar
    }
3226 2c74c2cb M. Mohan Kumar
}
3227 2c74c2cb M. Mohan Kumar
3228 9f107513 Anthony Liguori
static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3229 9f107513 Anthony Liguori
{
3230 ff06030f Venkateswararao Jujjuri (JV)
    Coroutine *co;
3231 ff06030f Venkateswararao Jujjuri (JV)
    CoroutineEntry *handler;
3232 9f107513 Anthony Liguori
3233 ff06030f Venkateswararao Jujjuri (JV)
    if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3234 ff06030f Venkateswararao Jujjuri (JV)
        (pdu_co_handlers[pdu->id] == NULL)) {
3235 5c3234c6 Aneesh Kumar K.V
        handler = v9fs_op_not_supp;
3236 5c3234c6 Aneesh Kumar K.V
    } else {
3237 ff06030f Venkateswararao Jujjuri (JV)
        handler = pdu_co_handlers[pdu->id];
3238 5c3234c6 Aneesh Kumar K.V
    }
3239 2c74c2cb M. Mohan Kumar
3240 2c74c2cb M. Mohan Kumar
    if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3241 2c74c2cb M. Mohan Kumar
        handler = v9fs_fs_ro;
3242 2c74c2cb M. Mohan Kumar
    }
3243 ff06030f Venkateswararao Jujjuri (JV)
    co = qemu_coroutine_create(handler);
3244 ff06030f Venkateswararao Jujjuri (JV)
    qemu_coroutine_enter(co, pdu);
3245 9f107513 Anthony Liguori
}
3246 9f107513 Anthony Liguori
3247 f4f61d27 Aneesh Kumar K.V
void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3248 9f107513 Anthony Liguori
{
3249 9f107513 Anthony Liguori
    V9fsState *s = (V9fsState *)vdev;
3250 9f107513 Anthony Liguori
    V9fsPDU *pdu;
3251 9f107513 Anthony Liguori
    ssize_t len;
3252 9f107513 Anthony Liguori
3253 9f107513 Anthony Liguori
    while ((pdu = alloc_pdu(s)) &&
3254 9f107513 Anthony Liguori
            (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3255 9f107513 Anthony Liguori
        uint8_t *ptr;
3256 ff06030f Venkateswararao Jujjuri (JV)
        pdu->s = s;
3257 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3258 9f107513 Anthony Liguori
        BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3259 9f107513 Anthony Liguori
3260 9f107513 Anthony Liguori
        ptr = pdu->elem.out_sg[0].iov_base;
3261 9f107513 Anthony Liguori
3262 67d6fa53 Benjamin Herrenschmidt
        pdu->size = le32_to_cpu(*(uint32_t *)ptr);
3263 9f107513 Anthony Liguori
        pdu->id = ptr[4];
3264 67d6fa53 Benjamin Herrenschmidt
        pdu->tag = le16_to_cpu(*(uint16_t *)(ptr + 5));
3265 bccacf6c Aneesh Kumar K.V
        qemu_co_queue_init(&pdu->complete);
3266 9f107513 Anthony Liguori
        submit_pdu(s, pdu);
3267 9f107513 Anthony Liguori
    }
3268 9f107513 Anthony Liguori
    free_pdu(s, pdu);
3269 9f107513 Anthony Liguori
}
3270 7a462745 Aneesh Kumar K.V
3271 60653b28 Paolo Bonzini
static void __attribute__((__constructor__)) virtio_9p_set_fd_limit(void)
3272 7a462745 Aneesh Kumar K.V
{
3273 7a462745 Aneesh Kumar K.V
    struct rlimit rlim;
3274 7a462745 Aneesh Kumar K.V
    if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3275 7a462745 Aneesh Kumar K.V
        fprintf(stderr, "Failed to get the resource limit\n");
3276 7a462745 Aneesh Kumar K.V
        exit(1);
3277 7a462745 Aneesh Kumar K.V
    }
3278 7a462745 Aneesh Kumar K.V
    open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3279 7a462745 Aneesh Kumar K.V
    open_fd_rc = rlim.rlim_cur/2;
3280 7a462745 Aneesh Kumar K.V
}