Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Virtio 9p backend |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2010 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
Christian Schoenebeck | 6f56908 | 2021-05-06 15:12:23 +0200 | [diff] [blame] | 14 | /* |
| 15 | * Not so fast! You might want to read the 9p developer docs first: |
| 16 | * https://wiki.qemu.org/Documentation/9p |
| 17 | */ |
| 18 | |
Peter Maydell | fbc0412 | 2016-01-26 18:17:10 +0000 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
Greg Kurz | e3e83f2 | 2016-09-16 08:56:15 +0200 | [diff] [blame] | 20 | #include <glib/gprintf.h> |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 21 | #include "hw/virtio/virtio.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 22 | #include "qapi/error.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 23 | #include "qemu/error-report.h" |
Michael S. Tsirkin | cd4bfbb | 2015-07-23 20:57:53 +0300 | [diff] [blame] | 24 | #include "qemu/iov.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 25 | #include "qemu/main-loop.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 26 | #include "qemu/sockets.h" |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 27 | #include "virtio-9p.h" |
| 28 | #include "fsdev/qemu-fsdev.h" |
Wei Liu | 267ae09 | 2015-11-18 18:31:52 +0000 | [diff] [blame] | 29 | #include "9p-xattr.h" |
Wei Liu | fe52840 | 2015-11-18 17:57:30 +0000 | [diff] [blame] | 30 | #include "coth.h" |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 31 | #include "trace.h" |
Juan Quintela | 795c40b | 2017-04-06 12:00:28 +0200 | [diff] [blame] | 32 | #include "migration/blocker.h" |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 33 | #include "qemu/xxhash.h" |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 34 | #include <math.h> |
Dan Robertson | 03556ea | 2020-05-25 10:38:03 +0200 | [diff] [blame] | 35 | #include <linux/limits.h> |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 36 | |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 37 | int open_fd_hw; |
| 38 | int total_open_fd; |
| 39 | static int open_fd_rc; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 40 | |
Venkateswararao Jujjuri (JV) | fac4f11 | 2010-06-01 13:30:51 -0700 | [diff] [blame] | 41 | enum { |
| 42 | Oread = 0x00, |
| 43 | Owrite = 0x01, |
| 44 | Ordwr = 0x02, |
| 45 | Oexec = 0x03, |
| 46 | Oexcl = 0x04, |
| 47 | Otrunc = 0x10, |
| 48 | Orexec = 0x20, |
| 49 | Orclose = 0x40, |
| 50 | Oappend = 0x80, |
| 51 | }; |
| 52 | |
Christian Schoenebeck | cc82fde | 2021-10-01 16:27:46 +0200 | [diff] [blame] | 53 | P9ARRAY_DEFINE_TYPE(V9fsPath, v9fs_path_free); |
| 54 | |
Greg Kurz | 7567359 | 2018-01-08 11:18:22 +0100 | [diff] [blame] | 55 | static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) |
Wei Liu | 0e2082d | 2015-12-02 14:22:04 +0000 | [diff] [blame] | 56 | { |
| 57 | ssize_t ret; |
| 58 | va_list ap; |
| 59 | |
| 60 | va_start(ap, fmt); |
Stefano Stabellini | ea83441 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 61 | ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap); |
Wei Liu | 0e2082d | 2015-12-02 14:22:04 +0000 | [diff] [blame] | 62 | va_end(ap); |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | |
Greg Kurz | 7567359 | 2018-01-08 11:18:22 +0100 | [diff] [blame] | 67 | static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) |
Wei Liu | 0e2082d | 2015-12-02 14:22:04 +0000 | [diff] [blame] | 68 | { |
| 69 | ssize_t ret; |
| 70 | va_list ap; |
| 71 | |
| 72 | va_start(ap, fmt); |
Stefano Stabellini | ea83441 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 73 | ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap); |
Wei Liu | 0e2082d | 2015-12-02 14:22:04 +0000 | [diff] [blame] | 74 | va_end(ap); |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
Venkateswararao Jujjuri (JV) | fac4f11 | 2010-06-01 13:30:51 -0700 | [diff] [blame] | 79 | static int omode_to_uflags(int8_t mode) |
| 80 | { |
| 81 | int ret = 0; |
| 82 | |
| 83 | switch (mode & 3) { |
| 84 | case Oread: |
| 85 | ret = O_RDONLY; |
| 86 | break; |
| 87 | case Ordwr: |
| 88 | ret = O_RDWR; |
| 89 | break; |
| 90 | case Owrite: |
| 91 | ret = O_WRONLY; |
| 92 | break; |
| 93 | case Oexec: |
| 94 | ret = O_RDONLY; |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | if (mode & Otrunc) { |
| 99 | ret |= O_TRUNC; |
| 100 | } |
| 101 | |
| 102 | if (mode & Oappend) { |
| 103 | ret |= O_APPEND; |
| 104 | } |
| 105 | |
| 106 | if (mode & Oexcl) { |
| 107 | ret |= O_EXCL; |
| 108 | } |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
Greg Kurz | 8e71b96 | 2018-01-08 11:18:22 +0100 | [diff] [blame] | 113 | typedef struct DotlOpenflagMap { |
M. Mohan Kumar | 9844081 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 114 | int dotl_flag; |
| 115 | int open_flag; |
Greg Kurz | 8e71b96 | 2018-01-08 11:18:22 +0100 | [diff] [blame] | 116 | } DotlOpenflagMap; |
M. Mohan Kumar | 9844081 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 117 | |
| 118 | static int dotl_to_open_flags(int flags) |
| 119 | { |
| 120 | int i; |
| 121 | /* |
| 122 | * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY |
| 123 | * and P9_DOTL_NOACCESS |
| 124 | */ |
| 125 | int oflags = flags & O_ACCMODE; |
| 126 | |
Greg Kurz | 8e71b96 | 2018-01-08 11:18:22 +0100 | [diff] [blame] | 127 | DotlOpenflagMap dotl_oflag_map[] = { |
M. Mohan Kumar | 9844081 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 128 | { P9_DOTL_CREATE, O_CREAT }, |
| 129 | { P9_DOTL_EXCL, O_EXCL }, |
| 130 | { P9_DOTL_NOCTTY , O_NOCTTY }, |
| 131 | { P9_DOTL_TRUNC, O_TRUNC }, |
| 132 | { P9_DOTL_APPEND, O_APPEND }, |
| 133 | { P9_DOTL_NONBLOCK, O_NONBLOCK } , |
| 134 | { P9_DOTL_DSYNC, O_DSYNC }, |
| 135 | { P9_DOTL_FASYNC, FASYNC }, |
| 136 | { P9_DOTL_DIRECT, O_DIRECT }, |
| 137 | { P9_DOTL_LARGEFILE, O_LARGEFILE }, |
| 138 | { P9_DOTL_DIRECTORY, O_DIRECTORY }, |
| 139 | { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, |
| 140 | { P9_DOTL_NOATIME, O_NOATIME }, |
| 141 | { P9_DOTL_SYNC, O_SYNC }, |
| 142 | }; |
| 143 | |
| 144 | for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { |
| 145 | if (flags & dotl_oflag_map[i].dotl_flag) { |
| 146 | oflags |= dotl_oflag_map[i].open_flag; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return oflags; |
| 151 | } |
| 152 | |
Venkateswararao Jujjuri (JV) | 758e8e3 | 2010-06-14 13:34:41 -0700 | [diff] [blame] | 153 | void cred_init(FsCred *credp) |
| 154 | { |
| 155 | credp->fc_uid = -1; |
| 156 | credp->fc_gid = -1; |
| 157 | credp->fc_mode = -1; |
| 158 | credp->fc_rdev = -1; |
| 159 | } |
| 160 | |
Aneesh Kumar K.V | d3ab98e | 2011-10-12 19:11:23 +0530 | [diff] [blame] | 161 | static int get_dotl_openflags(V9fsState *s, int oflags) |
| 162 | { |
| 163 | int flags; |
| 164 | /* |
| 165 | * Filter the client open flags |
| 166 | */ |
M. Mohan Kumar | 9844081 | 2011-10-12 19:11:24 +0530 | [diff] [blame] | 167 | flags = dotl_to_open_flags(oflags); |
| 168 | flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); |
Aneesh Kumar K.V | d3ab98e | 2011-10-12 19:11:23 +0530 | [diff] [blame] | 169 | /* |
| 170 | * Ignore direct disk access hint until the server supports it. |
| 171 | */ |
| 172 | flags &= ~O_DIRECT; |
| 173 | return flags; |
| 174 | } |
| 175 | |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 176 | void v9fs_path_init(V9fsPath *path) |
| 177 | { |
| 178 | path->data = NULL; |
| 179 | path->size = 0; |
| 180 | } |
| 181 | |
| 182 | void v9fs_path_free(V9fsPath *path) |
| 183 | { |
| 184 | g_free(path->data); |
| 185 | path->data = NULL; |
| 186 | path->size = 0; |
| 187 | } |
| 188 | |
Greg Kurz | e3e83f2 | 2016-09-16 08:56:15 +0200 | [diff] [blame] | 189 | |
| 190 | void GCC_FMT_ATTR(2, 3) |
| 191 | v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) |
| 192 | { |
| 193 | va_list ap; |
| 194 | |
| 195 | v9fs_path_free(path); |
| 196 | |
| 197 | va_start(ap, fmt); |
| 198 | /* Bump the size for including terminating NULL */ |
| 199 | path->size = g_vasprintf(&path->data, fmt, ap) + 1; |
| 200 | va_end(ap); |
| 201 | } |
| 202 | |
Marc-André Lureau | e446a1e | 2018-02-19 18:27:15 +0100 | [diff] [blame] | 203 | void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 204 | { |
Marc-André Lureau | e446a1e | 2018-02-19 18:27:15 +0100 | [diff] [blame] | 205 | v9fs_path_free(dst); |
| 206 | dst->size = src->size; |
| 207 | dst->data = g_memdup(src->data, src->size); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, |
| 211 | const char *name, V9fsPath *path) |
| 212 | { |
| 213 | int err; |
| 214 | err = s->ops->name_to_path(&s->ctx, dirpath, name, path); |
| 215 | if (err < 0) { |
| 216 | err = -errno; |
| 217 | } |
| 218 | return err; |
| 219 | } |
| 220 | |
Malahal Naineni | 936532a | 2011-06-01 12:35:11 +0530 | [diff] [blame] | 221 | /* |
| 222 | * Return TRUE if s1 is an ancestor of s2. |
| 223 | * |
| 224 | * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". |
| 225 | * As a special case, We treat s1 as ancestor of s2 if they are same! |
| 226 | */ |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 227 | static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2) |
Malahal Naineni | 936532a | 2011-06-01 12:35:11 +0530 | [diff] [blame] | 228 | { |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 229 | if (!strncmp(s1->data, s2->data, s1->size - 1)) { |
| 230 | if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') { |
Malahal Naineni | 936532a | 2011-06-01 12:35:11 +0530 | [diff] [blame] | 231 | return 1; |
| 232 | } |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
Anthony Liguori | a03f787 | 2010-04-29 17:44:46 +0530 | [diff] [blame] | 237 | static size_t v9fs_string_size(V9fsString *str) |
| 238 | { |
| 239 | return str->size; |
| 240 | } |
| 241 | |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 242 | /* |
| 243 | * returns 0 if fid got re-opened, 1 if not, < 0 on error */ |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 244 | static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f) |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 245 | { |
| 246 | int err = 1; |
| 247 | if (f->fid_type == P9_FID_FILE) { |
| 248 | if (f->fs.fd == -1) { |
| 249 | do { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 250 | err = v9fs_co_open(pdu, f, f->open_flags); |
| 251 | } while (err == -EINTR && !pdu->cancelled); |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 252 | } |
| 253 | } else if (f->fid_type == P9_FID_DIR) { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 254 | if (f->fs.dir.stream == NULL) { |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 255 | do { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 256 | err = v9fs_co_opendir(pdu, f); |
| 257 | } while (err == -EINTR && !pdu->cancelled); |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | return err; |
| 261 | } |
| 262 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 263 | static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid) |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 264 | { |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 265 | int err; |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 266 | V9fsFidState *f; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 267 | V9fsState *s = pdu->s; |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 268 | |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 269 | QSIMPLEQ_FOREACH(f, &s->fid_list, next) { |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 270 | BUG_ON(f->clunked); |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 271 | if (f->fid == fid) { |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 272 | /* |
| 273 | * Update the fid ref upfront so that |
| 274 | * we don't get reclaimed when we yield |
| 275 | * in open later. |
| 276 | */ |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 277 | f->ref++; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 278 | /* |
| 279 | * check whether we need to reopen the |
| 280 | * file. We might have closed the fd |
| 281 | * while trying to free up some file |
| 282 | * descriptors. |
| 283 | */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 284 | err = v9fs_reopen_fid(pdu, f); |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 285 | if (err < 0) { |
| 286 | f->ref--; |
| 287 | return NULL; |
| 288 | } |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 289 | /* |
| 290 | * Mark the fid as referenced so that the LRU |
| 291 | * reclaim won't close the file descriptor |
| 292 | */ |
| 293 | f->flags |= FID_REFERENCED; |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 294 | return f; |
| 295 | } |
| 296 | } |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) |
| 301 | { |
| 302 | V9fsFidState *f; |
| 303 | |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 304 | QSIMPLEQ_FOREACH(f, &s->fid_list, next) { |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 305 | /* If fid is already there return NULL */ |
| 306 | BUG_ON(f->clunked); |
| 307 | if (f->fid == fid) { |
| 308 | return NULL; |
| 309 | } |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 310 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 311 | f = g_malloc0(sizeof(V9fsFidState)); |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 312 | f->fid = fid; |
Aneesh Kumar K.V | d62dbb5 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 313 | f->fid_type = P9_FID_NONE; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 314 | f->ref = 1; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 315 | /* |
| 316 | * Mark the fid as referenced so that the LRU |
| 317 | * reclaim won't close the file descriptor |
| 318 | */ |
| 319 | f->flags |= FID_REFERENCED; |
Greg Kurz | 20b7f45 | 2021-01-21 19:15:10 +0100 | [diff] [blame] | 320 | QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next); |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 321 | |
Christian Schoenebeck | d2c5cf7 | 2020-07-29 10:39:12 +0200 | [diff] [blame] | 322 | v9fs_readdir_init(s->proto_version, &f->fs.dir); |
| 323 | v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir); |
Greg Kurz | 7cde47d | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 324 | |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 325 | return f; |
| 326 | } |
| 327 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 328 | static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp) |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 329 | { |
| 330 | int retval = 0; |
| 331 | |
Li Qiang | dd28fbb | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 332 | if (fidp->fs.xattr.xattrwalk_fid) { |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 333 | /* getxattr/listxattr fid */ |
| 334 | goto free_value; |
| 335 | } |
| 336 | /* |
| 337 | * if this is fid for setxattr. clunk should |
| 338 | * result in setxattr localcall |
| 339 | */ |
| 340 | if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { |
| 341 | /* clunk after partial write */ |
| 342 | retval = -EINVAL; |
| 343 | goto free_out; |
| 344 | } |
Aneesh Kumar K.V | 9ed3ef2 | 2010-08-26 11:15:23 +0530 | [diff] [blame] | 345 | if (fidp->fs.xattr.len) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 346 | retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name, |
Aneesh Kumar K.V | 9ed3ef2 | 2010-08-26 11:15:23 +0530 | [diff] [blame] | 347 | fidp->fs.xattr.value, |
| 348 | fidp->fs.xattr.len, |
| 349 | fidp->fs.xattr.flags); |
| 350 | } else { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 351 | retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name); |
Aneesh Kumar K.V | 9ed3ef2 | 2010-08-26 11:15:23 +0530 | [diff] [blame] | 352 | } |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 353 | free_out: |
| 354 | v9fs_string_free(&fidp->fs.xattr.name); |
| 355 | free_value: |
Markus Armbruster | 9e28840 | 2014-06-06 18:43:29 +0200 | [diff] [blame] | 356 | g_free(fidp->fs.xattr.value); |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 357 | return retval; |
| 358 | } |
| 359 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 360 | static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp) |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 361 | { |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 362 | int retval = 0; |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 363 | |
Aneesh Kumar K.V | d62dbb5 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 364 | if (fidp->fid_type == P9_FID_FILE) { |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 365 | /* If we reclaimed the fd no need to close */ |
| 366 | if (fidp->fs.fd != -1) { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 367 | retval = v9fs_co_close(pdu, &fidp->fs); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 368 | } |
Aneesh Kumar K.V | d62dbb5 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 369 | } else if (fidp->fid_type == P9_FID_DIR) { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 370 | if (fidp->fs.dir.stream != NULL) { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 371 | retval = v9fs_co_closedir(pdu, &fidp->fs); |
Aneesh Kumar K.V | 95f6551 | 2011-05-18 17:08:34 +0530 | [diff] [blame] | 372 | } |
Aneesh Kumar K.V | d62dbb5 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 373 | } else if (fidp->fid_type == P9_FID_XATTR) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 374 | retval = v9fs_xattr_fid_clunk(pdu, fidp); |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 375 | } |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 376 | v9fs_path_free(&fidp->path); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 377 | g_free(fidp); |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 378 | return retval; |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 379 | } |
| 380 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 381 | static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp) |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 382 | { |
| 383 | BUG_ON(!fidp->ref); |
| 384 | fidp->ref--; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 385 | /* |
| 386 | * Don't free the fid if it is in reclaim list |
| 387 | */ |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 388 | if (!fidp->ref && fidp->clunked) { |
Aneesh Kumar K.V | e9a0152 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 389 | if (fidp->fid == pdu->s->root_fid) { |
| 390 | /* |
| 391 | * if the clunked fid is root fid then we |
| 392 | * have unmounted the fs on the client side. |
| 393 | * delete the migration blocker. Ideally, this |
| 394 | * should be hooked to transport close notification |
| 395 | */ |
| 396 | if (pdu->s->migration_blocker) { |
| 397 | migrate_del_blocker(pdu->s->migration_blocker); |
| 398 | error_free(pdu->s->migration_blocker); |
| 399 | pdu->s->migration_blocker = NULL; |
| 400 | } |
| 401 | } |
Aneesh Kumar K.V | a911a18 | 2013-02-05 11:27:46 +0530 | [diff] [blame] | 402 | return free_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 403 | } |
Aneesh Kumar K.V | a911a18 | 2013-02-05 11:27:46 +0530 | [diff] [blame] | 404 | return 0; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 405 | } |
| 406 | |
Aneesh Kumar K.V | ce421a1 | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 407 | static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid) |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 408 | { |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 409 | V9fsFidState *fidp; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 410 | |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 411 | QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) { |
| 412 | if (fidp->fid == fid) { |
| 413 | QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); |
| 414 | fidp->clunked = true; |
| 415 | return fidp; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 416 | } |
| 417 | } |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 418 | return NULL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 419 | } |
| 420 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 421 | void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 422 | { |
| 423 | int reclaim_count = 0; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 424 | V9fsState *s = pdu->s; |
Greg Kurz | 81f9766 | 2021-01-22 15:35:14 +0100 | [diff] [blame] | 425 | V9fsFidState *f; |
| 426 | QSLIST_HEAD(, V9fsFidState) reclaim_list = |
| 427 | QSLIST_HEAD_INITIALIZER(reclaim_list); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 428 | |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 429 | QSIMPLEQ_FOREACH(f, &s->fid_list, next) { |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 430 | /* |
| 431 | * Unlink fids cannot be reclaimed. Check |
| 432 | * for them and skip them. Also skip fids |
| 433 | * currently being operated on. |
| 434 | */ |
| 435 | if (f->ref || f->flags & FID_NON_RECLAIMABLE) { |
| 436 | continue; |
| 437 | } |
| 438 | /* |
| 439 | * if it is a recently referenced fid |
| 440 | * we leave the fid untouched and clear the |
| 441 | * reference bit. We come back to it later |
| 442 | * in the next iteration. (a simple LRU without |
| 443 | * moving list elements around) |
| 444 | */ |
| 445 | if (f->flags & FID_REFERENCED) { |
| 446 | f->flags &= ~FID_REFERENCED; |
| 447 | continue; |
| 448 | } |
| 449 | /* |
| 450 | * Add fids to reclaim list. |
| 451 | */ |
| 452 | if (f->fid_type == P9_FID_FILE) { |
| 453 | if (f->fs.fd != -1) { |
| 454 | /* |
| 455 | * Up the reference count so that |
| 456 | * a clunk request won't free this fid |
| 457 | */ |
| 458 | f->ref++; |
Greg Kurz | 81f9766 | 2021-01-22 15:35:14 +0100 | [diff] [blame] | 459 | QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 460 | f->fs_reclaim.fd = f->fs.fd; |
| 461 | f->fs.fd = -1; |
| 462 | reclaim_count++; |
| 463 | } |
Aneesh Kumar K.V | 95f6551 | 2011-05-18 17:08:34 +0530 | [diff] [blame] | 464 | } else if (f->fid_type == P9_FID_DIR) { |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 465 | if (f->fs.dir.stream != NULL) { |
Aneesh Kumar K.V | 95f6551 | 2011-05-18 17:08:34 +0530 | [diff] [blame] | 466 | /* |
| 467 | * Up the reference count so that |
| 468 | * a clunk request won't free this fid |
| 469 | */ |
| 470 | f->ref++; |
Greg Kurz | 81f9766 | 2021-01-22 15:35:14 +0100 | [diff] [blame] | 471 | QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 472 | f->fs_reclaim.dir.stream = f->fs.dir.stream; |
| 473 | f->fs.dir.stream = NULL; |
Aneesh Kumar K.V | 95f6551 | 2011-05-18 17:08:34 +0530 | [diff] [blame] | 474 | reclaim_count++; |
| 475 | } |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 476 | } |
| 477 | if (reclaim_count >= open_fd_rc) { |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | /* |
| 482 | * Now close the fid in reclaim list. Free them if they |
| 483 | * are already clunked. |
| 484 | */ |
Greg Kurz | 81f9766 | 2021-01-22 15:35:14 +0100 | [diff] [blame] | 485 | while (!QSLIST_EMPTY(&reclaim_list)) { |
| 486 | f = QSLIST_FIRST(&reclaim_list); |
| 487 | QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 488 | if (f->fid_type == P9_FID_FILE) { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 489 | v9fs_co_close(pdu, &f->fs_reclaim); |
Aneesh Kumar K.V | 95f6551 | 2011-05-18 17:08:34 +0530 | [diff] [blame] | 490 | } else if (f->fid_type == P9_FID_DIR) { |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 491 | v9fs_co_closedir(pdu, &f->fs_reclaim); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 492 | } |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 493 | /* |
| 494 | * Now drop the fid reference, free it |
| 495 | * if clunked. |
| 496 | */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 497 | put_fid(pdu, f); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 501 | static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path) |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 502 | { |
| 503 | int err; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 504 | V9fsState *s = pdu->s; |
Greg Kurz | 20b7f45 | 2021-01-21 19:15:10 +0100 | [diff] [blame] | 505 | V9fsFidState *fidp, *fidp_next; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 506 | |
Greg Kurz | 20b7f45 | 2021-01-21 19:15:10 +0100 | [diff] [blame] | 507 | fidp = QSIMPLEQ_FIRST(&s->fid_list); |
| 508 | if (!fidp) { |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * v9fs_reopen_fid() can yield : a reference on the fid must be held |
| 514 | * to ensure its pointer remains valid and we can safely pass it to |
| 515 | * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so |
| 516 | * we must keep a reference on the next fid as well. So the logic here |
| 517 | * is to get a reference on a fid and only put it back during the next |
| 518 | * iteration after we could get a reference on the next fid. Start with |
| 519 | * the first one. |
| 520 | */ |
| 521 | for (fidp->ref++; fidp; fidp = fidp_next) { |
| 522 | if (fidp->path.size == path->size && |
| 523 | !memcmp(fidp->path.data, path->data, path->size)) { |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 524 | /* Mark the fid non reclaimable. */ |
| 525 | fidp->flags |= FID_NON_RECLAIMABLE; |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 526 | |
| 527 | /* reopen the file/dir if already closed */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 528 | err = v9fs_reopen_fid(pdu, fidp); |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 529 | if (err < 0) { |
Greg Kurz | 20b7f45 | 2021-01-21 19:15:10 +0100 | [diff] [blame] | 530 | put_fid(pdu, fidp); |
Greg Kurz | 267fcad | 2017-11-06 18:05:35 +0100 | [diff] [blame] | 531 | return err; |
Aneesh Kumar K.V | b9cb88b | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 532 | } |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 533 | } |
Greg Kurz | 20b7f45 | 2021-01-21 19:15:10 +0100 | [diff] [blame] | 534 | |
| 535 | fidp_next = QSIMPLEQ_NEXT(fidp, next); |
| 536 | |
| 537 | if (fidp_next) { |
| 538 | /* |
| 539 | * Ensure the next fid survives a potential clunk request during |
| 540 | * put_fid() below and v9fs_reopen_fid() in the next iteration. |
| 541 | */ |
| 542 | fidp_next->ref++; |
| 543 | } |
| 544 | |
| 545 | /* We're done with this fid */ |
| 546 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 547 | } |
Greg Kurz | 20b7f45 | 2021-01-21 19:15:10 +0100 | [diff] [blame] | 548 | |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 552 | static void coroutine_fn virtfs_reset(V9fsPDU *pdu) |
Deepak C Shetty | b41e299 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 553 | { |
| 554 | V9fsState *s = pdu->s; |
Greg Kurz | 79decce | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 555 | V9fsFidState *fidp; |
Deepak C Shetty | b41e299 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 556 | |
| 557 | /* Free all fids */ |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 558 | while (!QSIMPLEQ_EMPTY(&s->fid_list)) { |
Greg Kurz | 6d54af0 | 2017-04-04 18:06:01 +0200 | [diff] [blame] | 559 | /* Get fid */ |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 560 | fidp = QSIMPLEQ_FIRST(&s->fid_list); |
Greg Kurz | 6d54af0 | 2017-04-04 18:06:01 +0200 | [diff] [blame] | 561 | fidp->ref++; |
Deepak C Shetty | b41e299 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 562 | |
Greg Kurz | 6d54af0 | 2017-04-04 18:06:01 +0200 | [diff] [blame] | 563 | /* Clunk fid */ |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 564 | QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); |
Greg Kurz | 2e53160 | 2021-01-18 15:22:58 +0100 | [diff] [blame] | 565 | fidp->clunked = true; |
Greg Kurz | 6d54af0 | 2017-04-04 18:06:01 +0200 | [diff] [blame] | 566 | |
| 567 | put_fid(pdu, fidp); |
Deepak C Shetty | b41e299 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 568 | } |
Deepak C Shetty | b41e299 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 569 | } |
| 570 | |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 571 | #define P9_QID_TYPE_DIR 0x80 |
| 572 | #define P9_QID_TYPE_SYMLINK 0x02 |
| 573 | |
| 574 | #define P9_STAT_MODE_DIR 0x80000000 |
| 575 | #define P9_STAT_MODE_APPEND 0x40000000 |
| 576 | #define P9_STAT_MODE_EXCL 0x20000000 |
| 577 | #define P9_STAT_MODE_MOUNT 0x10000000 |
| 578 | #define P9_STAT_MODE_AUTH 0x08000000 |
| 579 | #define P9_STAT_MODE_TMP 0x04000000 |
| 580 | #define P9_STAT_MODE_SYMLINK 0x02000000 |
| 581 | #define P9_STAT_MODE_LINK 0x01000000 |
| 582 | #define P9_STAT_MODE_DEVICE 0x00800000 |
| 583 | #define P9_STAT_MODE_NAMED_PIPE 0x00200000 |
| 584 | #define P9_STAT_MODE_SOCKET 0x00100000 |
| 585 | #define P9_STAT_MODE_SETUID 0x00080000 |
| 586 | #define P9_STAT_MODE_SETGID 0x00040000 |
| 587 | #define P9_STAT_MODE_SETVTX 0x00010000 |
| 588 | |
| 589 | #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ |
| 590 | P9_STAT_MODE_SYMLINK | \ |
| 591 | P9_STAT_MODE_LINK | \ |
| 592 | P9_STAT_MODE_DEVICE | \ |
| 593 | P9_STAT_MODE_NAMED_PIPE | \ |
| 594 | P9_STAT_MODE_SOCKET) |
| 595 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 596 | /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */ |
| 597 | static inline uint8_t mirror8bit(uint8_t byte) |
| 598 | { |
| 599 | return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023; |
| 600 | } |
| 601 | |
| 602 | /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */ |
| 603 | static inline uint64_t mirror64bit(uint64_t value) |
| 604 | { |
| 605 | return ((uint64_t)mirror8bit(value & 0xff) << 56) | |
| 606 | ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) | |
| 607 | ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) | |
| 608 | ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) | |
| 609 | ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) | |
| 610 | ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) | |
| 611 | ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) | |
| 612 | ((uint64_t)mirror8bit((value >> 56) & 0xff)); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * @brief Parameter k for the Exponential Golomb algorihm to be used. |
| 617 | * |
| 618 | * The smaller this value, the smaller the minimum bit count for the Exp. |
| 619 | * Golomb generated affixes will be (at lowest index) however for the |
| 620 | * price of having higher maximum bit count of generated affixes (at highest |
| 621 | * index). Likewise increasing this parameter yields in smaller maximum bit |
| 622 | * count for the price of having higher minimum bit count. |
| 623 | * |
| 624 | * In practice that means: a good value for k depends on the expected amount |
| 625 | * of devices to be exposed by one export. For a small amount of devices k |
| 626 | * should be small, for a large amount of devices k might be increased |
| 627 | * instead. The default of k=0 should be fine for most users though. |
| 628 | * |
| 629 | * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of |
| 630 | * k should not change as long as guest is still running! Because that would |
| 631 | * cause completely different inode numbers to be generated on guest. |
| 632 | */ |
| 633 | #define EXP_GOLOMB_K 0 |
| 634 | |
| 635 | /** |
| 636 | * @brief Exponential Golomb algorithm for arbitrary k (including k=0). |
| 637 | * |
| 638 | * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!) |
| 639 | * with growing length and with the mathematical property of being |
| 640 | * "prefix-free". The latter means the generated prefixes can be prepended |
| 641 | * in front of arbitrary numbers and the resulting concatenated numbers are |
| 642 | * guaranteed to be always unique. |
| 643 | * |
| 644 | * This is a minor adjustment to the original Exp. Golomb algorithm in the |
| 645 | * sense that lowest allowed index (@param n) starts with 1, not with zero. |
| 646 | * |
| 647 | * @param n - natural number (or index) of the prefix to be generated |
| 648 | * (1, 2, 3, ...) |
| 649 | * @param k - parameter k of Exp. Golomb algorithm to be used |
| 650 | * (see comment on EXP_GOLOMB_K macro for details about k) |
| 651 | */ |
| 652 | static VariLenAffix expGolombEncode(uint64_t n, int k) |
| 653 | { |
| 654 | const uint64_t value = n + (1 << k) - 1; |
| 655 | const int bits = (int) log2(value) + 1; |
| 656 | return (VariLenAffix) { |
| 657 | .type = AffixType_Prefix, |
| 658 | .value = value, |
| 659 | .bits = bits + MAX((bits - 1 - k), 0) |
| 660 | }; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * @brief Converts a suffix into a prefix, or a prefix into a suffix. |
| 665 | * |
| 666 | * Simply mirror all bits of the affix value, for the purpose to preserve |
| 667 | * respectively the mathematical "prefix-free" or "suffix-free" property |
| 668 | * after the conversion. |
| 669 | * |
| 670 | * If a passed prefix is suitable to create unique numbers, then the |
| 671 | * returned suffix is suitable to create unique numbers as well (and vice |
| 672 | * versa). |
| 673 | */ |
| 674 | static VariLenAffix invertAffix(const VariLenAffix *affix) |
| 675 | { |
| 676 | return (VariLenAffix) { |
| 677 | .type = |
| 678 | (affix->type == AffixType_Suffix) ? |
| 679 | AffixType_Prefix : AffixType_Suffix, |
| 680 | .value = |
| 681 | mirror64bit(affix->value) >> |
| 682 | ((sizeof(affix->value) * 8) - affix->bits), |
| 683 | .bits = affix->bits |
| 684 | }; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * @brief Generates suffix numbers with "suffix-free" property. |
| 689 | * |
| 690 | * This is just a wrapper function on top of the Exp. Golomb algorithm. |
| 691 | * |
| 692 | * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes, |
| 693 | * this function converts the Exp. Golomb prefixes into appropriate suffixes |
| 694 | * which are still suitable for generating unique numbers. |
| 695 | * |
| 696 | * @param n - natural number (or index) of the suffix to be generated |
| 697 | * (1, 2, 3, ...) |
| 698 | */ |
| 699 | static VariLenAffix affixForIndex(uint64_t index) |
| 700 | { |
| 701 | VariLenAffix prefix; |
| 702 | prefix = expGolombEncode(index, EXP_GOLOMB_K); |
| 703 | return invertAffix(&prefix); /* convert prefix to suffix */ |
| 704 | } |
| 705 | |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 706 | /* creative abuse of tb_hash_func7, which is based on xxhash */ |
| 707 | static uint32_t qpp_hash(QppEntry e) |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 708 | { |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 709 | return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0); |
| 710 | } |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 711 | |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 712 | static uint32_t qpf_hash(QpfEntry e) |
| 713 | { |
| 714 | return qemu_xxhash7(e.ino, e.dev, 0, 0, 0); |
| 715 | } |
| 716 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 717 | static bool qpd_cmp_func(const void *obj, const void *userp) |
| 718 | { |
| 719 | const QpdEntry *e1 = obj, *e2 = userp; |
| 720 | return e1->dev == e2->dev; |
| 721 | } |
| 722 | |
| 723 | static bool qpp_cmp_func(const void *obj, const void *userp) |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 724 | { |
| 725 | const QppEntry *e1 = obj, *e2 = userp; |
| 726 | return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix; |
| 727 | } |
| 728 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 729 | static bool qpf_cmp_func(const void *obj, const void *userp) |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 730 | { |
| 731 | const QpfEntry *e1 = obj, *e2 = userp; |
| 732 | return e1->dev == e2->dev && e1->ino == e2->ino; |
| 733 | } |
| 734 | |
| 735 | static void qp_table_remove(void *p, uint32_t h, void *up) |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 736 | { |
| 737 | g_free(p); |
| 738 | } |
| 739 | |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 740 | static void qp_table_destroy(struct qht *ht) |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 741 | { |
| 742 | if (!ht || !ht->map) { |
| 743 | return; |
| 744 | } |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 745 | qht_iter(ht, qp_table_remove, NULL); |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 746 | qht_destroy(ht); |
| 747 | } |
| 748 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 749 | static void qpd_table_init(struct qht *ht) |
| 750 | { |
| 751 | qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE); |
| 752 | } |
| 753 | |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 754 | static void qpp_table_init(struct qht *ht) |
| 755 | { |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 756 | qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE); |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 759 | static void qpf_table_init(struct qht *ht) |
| 760 | { |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 761 | qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE); |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 764 | /* |
| 765 | * Returns how many (high end) bits of inode numbers of the passed fs |
| 766 | * device shall be used (in combination with the device number) to |
| 767 | * generate hash values for qpp_table entries. |
| 768 | * |
| 769 | * This function is required if variable length suffixes are used for inode |
| 770 | * number mapping on guest level. Since a device may end up having multiple |
| 771 | * entries in qpp_table, each entry most probably with a different suffix |
| 772 | * length, we thus need this function in conjunction with qpd_table to |
| 773 | * "agree" about a fix amount of bits (per device) to be always used for |
| 774 | * generating hash values for the purpose of accessing qpp_table in order |
| 775 | * get consistent behaviour when accessing qpp_table. |
| 776 | */ |
| 777 | static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev) |
| 778 | { |
| 779 | QpdEntry lookup = { |
| 780 | .dev = dev |
| 781 | }, *val; |
| 782 | uint32_t hash = dev; |
| 783 | VariLenAffix affix; |
| 784 | |
| 785 | val = qht_lookup(&pdu->s->qpd_table, &lookup, hash); |
| 786 | if (!val) { |
| 787 | val = g_malloc0(sizeof(QpdEntry)); |
| 788 | *val = lookup; |
| 789 | affix = affixForIndex(pdu->s->qp_affix_next); |
| 790 | val->prefix_bits = affix.bits; |
| 791 | qht_insert(&pdu->s->qpd_table, val, hash, NULL); |
| 792 | pdu->s->qp_ndevices++; |
| 793 | } |
| 794 | return val->prefix_bits; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * @brief Slow / full mapping host inode nr -> guest inode nr. |
| 799 | * |
| 800 | * This function performs a slower and much more costly remapping of an |
| 801 | * original file inode number on host to an appropriate different inode |
| 802 | * number on guest. For every (dev, inode) combination on host a new |
| 803 | * sequential number is generated, cached and exposed as inode number on |
| 804 | * guest. |
| 805 | * |
| 806 | * This is just a "last resort" fallback solution if the much faster/cheaper |
| 807 | * qid_path_suffixmap() failed. In practice this slow / full mapping is not |
| 808 | * expected ever to be used at all though. |
| 809 | * |
| 810 | * @see qid_path_suffixmap() for details |
| 811 | * |
| 812 | */ |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 813 | static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, |
| 814 | uint64_t *path) |
| 815 | { |
| 816 | QpfEntry lookup = { |
| 817 | .dev = stbuf->st_dev, |
| 818 | .ino = stbuf->st_ino |
| 819 | }, *val; |
| 820 | uint32_t hash = qpf_hash(lookup); |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 821 | VariLenAffix affix; |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 822 | |
| 823 | val = qht_lookup(&pdu->s->qpf_table, &lookup, hash); |
| 824 | |
| 825 | if (!val) { |
| 826 | if (pdu->s->qp_fullpath_next == 0) { |
| 827 | /* no more files can be mapped :'( */ |
| 828 | error_report_once( |
| 829 | "9p: No more prefixes available for remapping inodes from " |
| 830 | "host to guest." |
| 831 | ); |
| 832 | return -ENFILE; |
| 833 | } |
| 834 | |
| 835 | val = g_malloc0(sizeof(QppEntry)); |
| 836 | *val = lookup; |
| 837 | |
| 838 | /* new unique inode and device combo */ |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 839 | affix = affixForIndex( |
| 840 | 1ULL << (sizeof(pdu->s->qp_affix_next) * 8) |
| 841 | ); |
| 842 | val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value; |
| 843 | pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1); |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 844 | qht_insert(&pdu->s->qpf_table, val, hash, NULL); |
| 845 | } |
| 846 | |
| 847 | *path = val->path; |
| 848 | return 0; |
| 849 | } |
| 850 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 851 | /** |
| 852 | * @brief Quick mapping host inode nr -> guest inode nr. |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 853 | * |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 854 | * This function performs quick remapping of an original file inode number |
| 855 | * on host to an appropriate different inode number on guest. This remapping |
| 856 | * of inodes is required to avoid inode nr collisions on guest which would |
| 857 | * happen if the 9p export contains more than 1 exported file system (or |
| 858 | * more than 1 file system data set), because unlike on host level where the |
| 859 | * files would have different device nrs, all files exported by 9p would |
| 860 | * share the same device nr on guest (the device nr of the virtual 9p device |
| 861 | * that is). |
| 862 | * |
| 863 | * Inode remapping is performed by chopping off high end bits of the original |
| 864 | * inode number from host, shifting the result upwards and then assigning a |
| 865 | * generated suffix number for the low end bits, where the same suffix number |
| 866 | * will be shared by all inodes with the same device id AND the same high end |
| 867 | * bits that have been chopped off. That approach utilizes the fact that inode |
| 868 | * numbers very likely share the same high end bits (i.e. due to their common |
| 869 | * sequential generation by file systems) and hence we only have to generate |
| 870 | * and track a very limited amount of suffixes in practice due to that. |
| 871 | * |
| 872 | * We generate variable size suffixes for that purpose. The 1st generated |
| 873 | * suffix will only have 1 bit and hence we only need to chop off 1 bit from |
| 874 | * the original inode number. The subsequent suffixes being generated will |
| 875 | * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being |
| 876 | * generated will have 3 bits and hence we have to chop off 3 bits from their |
| 877 | * original inodes, and so on. That approach of using variable length suffixes |
| 878 | * (i.e. over fixed size ones) utilizes the fact that in practice only a very |
| 879 | * limited amount of devices are shared by the same export (e.g. typically |
| 880 | * less than 2 dozen devices per 9p export), so in practice we need to chop |
| 881 | * off less bits than with fixed size prefixes and yet are flexible to add |
| 882 | * new devices at runtime below host's export directory at any time without |
| 883 | * having to reboot guest nor requiring to reconfigure guest for that. And due |
| 884 | * to the very limited amount of original high end bits that we chop off that |
| 885 | * way, the total amount of suffixes we need to generate is less than by using |
| 886 | * fixed size prefixes and hence it also improves performance of the inode |
| 887 | * remapping algorithm, and finally has the nice side effect that the inode |
| 888 | * numbers on guest will be much smaller & human friendly. ;-) |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 889 | */ |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 890 | static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf, |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 891 | uint64_t *path) |
| 892 | { |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 893 | const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev); |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 894 | QppEntry lookup = { |
| 895 | .dev = stbuf->st_dev, |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 896 | .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits)) |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 897 | }, *val; |
| 898 | uint32_t hash = qpp_hash(lookup); |
| 899 | |
| 900 | val = qht_lookup(&pdu->s->qpp_table, &lookup, hash); |
| 901 | |
| 902 | if (!val) { |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 903 | if (pdu->s->qp_affix_next == 0) { |
| 904 | /* we ran out of affixes */ |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 905 | warn_report_once( |
| 906 | "9p: Potential degraded performance of inode remapping" |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 907 | ); |
| 908 | return -ENFILE; |
| 909 | } |
| 910 | |
| 911 | val = g_malloc0(sizeof(QppEntry)); |
| 912 | *val = lookup; |
| 913 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 914 | /* new unique inode affix and device combo */ |
| 915 | val->qp_affix_index = pdu->s->qp_affix_next++; |
| 916 | val->qp_affix = affixForIndex(val->qp_affix_index); |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 917 | qht_insert(&pdu->s->qpp_table, val, hash, NULL); |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 918 | } |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 919 | /* assuming generated affix to be suffix type, not prefix */ |
| 920 | *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value; |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp) |
| 925 | { |
| 926 | int err; |
| 927 | size_t size; |
| 928 | |
| 929 | if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { |
| 930 | /* map inode+device to qid path (fast path) */ |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 931 | err = qid_path_suffixmap(pdu, stbuf, &qidp->path); |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 932 | if (err == -ENFILE) { |
| 933 | /* fast path didn't work, fall back to full map */ |
| 934 | err = qid_path_fullmap(pdu, stbuf, &qidp->path); |
| 935 | } |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 936 | if (err) { |
| 937 | return err; |
| 938 | } |
| 939 | } else { |
| 940 | if (pdu->s->dev_id != stbuf->st_dev) { |
| 941 | if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) { |
| 942 | error_report_once( |
| 943 | "9p: Multiple devices detected in same VirtFS export. " |
| 944 | "Access of guest to additional devices is (partly) " |
| 945 | "denied due to virtfs option 'multidevs=forbid' being " |
| 946 | "effective." |
| 947 | ); |
| 948 | return -ENODEV; |
| 949 | } else { |
| 950 | warn_report_once( |
| 951 | "9p: Multiple devices detected in same VirtFS export, " |
| 952 | "which might lead to file ID collisions and severe " |
| 953 | "misbehaviours on guest! You should either use a " |
| 954 | "separate export for each device shared from host or " |
| 955 | "use virtfs option 'multidevs=remap'!" |
| 956 | ); |
| 957 | } |
| 958 | } |
| 959 | memset(&qidp->path, 0, sizeof(qidp->path)); |
| 960 | size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); |
| 961 | memcpy(&qidp->path, &stbuf->st_ino, size); |
| 962 | } |
| 963 | |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 964 | qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); |
| 965 | qidp->type = 0; |
| 966 | if (S_ISDIR(stbuf->st_mode)) { |
| 967 | qidp->type |= P9_QID_TYPE_DIR; |
| 968 | } |
| 969 | if (S_ISLNK(stbuf->st_mode)) { |
| 970 | qidp->type |= P9_QID_TYPE_SYMLINK; |
| 971 | } |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 972 | |
| 973 | return 0; |
Anthony Liguori | 286d565 | 2010-04-29 17:44:48 +0530 | [diff] [blame] | 974 | } |
| 975 | |
Wei Liu | 4b311c5 | 2016-01-07 18:35:12 +0000 | [diff] [blame] | 976 | V9fsPDU *pdu_alloc(V9fsState *s) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 977 | { |
| 978 | V9fsPDU *pdu = NULL; |
| 979 | |
| 980 | if (!QLIST_EMPTY(&s->free_list)) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 981 | pdu = QLIST_FIRST(&s->free_list); |
| 982 | QLIST_REMOVE(pdu, next); |
| 983 | QLIST_INSERT_HEAD(&s->active_list, pdu, next); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 984 | } |
| 985 | return pdu; |
| 986 | } |
| 987 | |
Wei Liu | 4b311c5 | 2016-01-07 18:35:12 +0000 | [diff] [blame] | 988 | void pdu_free(V9fsPDU *pdu) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 989 | { |
Greg Kurz | 6868a42 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 990 | V9fsState *s = pdu->s; |
Greg Kurz | f74e27b | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 991 | |
| 992 | g_assert(!pdu->cancelled); |
| 993 | QLIST_REMOVE(pdu, next); |
| 994 | QLIST_INSERT_HEAD(&s->free_list, pdu, next); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 995 | } |
| 996 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 997 | static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len) |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 998 | { |
| 999 | int8_t id = pdu->id + 1; /* Response */ |
Wei Liu | ad38ce9 | 2015-12-02 12:06:28 +0000 | [diff] [blame] | 1000 | V9fsState *s = pdu->s; |
Greg Kurz | 06a37db | 2017-06-29 15:11:51 +0200 | [diff] [blame] | 1001 | int ret; |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1002 | |
Keno Fischer | fc78d5e | 2018-02-01 21:21:27 +0100 | [diff] [blame] | 1003 | /* |
| 1004 | * The 9p spec requires that successfully cancelled pdus receive no reply. |
| 1005 | * Sending a reply would confuse clients because they would |
| 1006 | * assume that any EINTR is the actual result of the operation, |
| 1007 | * rather than a consequence of the cancellation. However, if |
| 1008 | * the operation completed (succesfully or with an error other |
| 1009 | * than caused be cancellation), we do send out that reply, both |
| 1010 | * for efficiency and to avoid confusing the rest of the state machine |
| 1011 | * that assumes passing a non-error here will mean a successful |
| 1012 | * transmission of the reply. |
| 1013 | */ |
| 1014 | bool discard = pdu->cancelled && len == -EINTR; |
| 1015 | if (discard) { |
| 1016 | trace_v9fs_rcancel(pdu->tag, pdu->id); |
| 1017 | pdu->size = 0; |
| 1018 | goto out_notify; |
| 1019 | } |
| 1020 | |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1021 | if (len < 0) { |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1022 | int err = -len; |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1023 | len = 7; |
Arun R Bharadwaj | 8f4d1ca | 2010-07-28 14:10:22 +0530 | [diff] [blame] | 1024 | |
| 1025 | if (s->proto_version != V9FS_PROTO_2000L) { |
| 1026 | V9fsString str; |
| 1027 | |
| 1028 | str.data = strerror(err); |
| 1029 | str.size = strlen(str.data); |
| 1030 | |
Greg Kurz | 06a37db | 2017-06-29 15:11:51 +0200 | [diff] [blame] | 1031 | ret = pdu_marshal(pdu, len, "s", &str); |
| 1032 | if (ret < 0) { |
| 1033 | goto out_notify; |
| 1034 | } |
| 1035 | len += ret; |
Arun R Bharadwaj | 8f4d1ca | 2010-07-28 14:10:22 +0530 | [diff] [blame] | 1036 | id = P9_RERROR; |
| 1037 | } |
| 1038 | |
Greg Kurz | 06a37db | 2017-06-29 15:11:51 +0200 | [diff] [blame] | 1039 | ret = pdu_marshal(pdu, len, "d", err); |
| 1040 | if (ret < 0) { |
| 1041 | goto out_notify; |
| 1042 | } |
| 1043 | len += ret; |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1044 | |
Arun R Bharadwaj | 8f4d1ca | 2010-07-28 14:10:22 +0530 | [diff] [blame] | 1045 | if (s->proto_version == V9FS_PROTO_2000L) { |
| 1046 | id = P9_RLERROR; |
| 1047 | } |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 1048 | trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */ |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* fill out the header */ |
Greg Kurz | 06a37db | 2017-06-29 15:11:51 +0200 | [diff] [blame] | 1052 | if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { |
| 1053 | goto out_notify; |
| 1054 | } |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1055 | |
| 1056 | /* keep these in sync */ |
| 1057 | pdu->size = len; |
| 1058 | pdu->id = id; |
| 1059 | |
Greg Kurz | 06a37db | 2017-06-29 15:11:51 +0200 | [diff] [blame] | 1060 | out_notify: |
Greg Kurz | a17d865 | 2017-05-25 10:30:13 +0200 | [diff] [blame] | 1061 | pdu->s->transport->push_and_notify(pdu); |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1062 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1063 | /* Now wakeup anybody waiting in flush for this request */ |
Greg Kurz | f74e27b | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1064 | if (!qemu_co_queue_next(&pdu->complete)) { |
| 1065 | pdu_free(pdu); |
| 1066 | } |
Anthony Liguori | 405a549 | 2010-04-29 17:44:45 +0530 | [diff] [blame] | 1067 | } |
| 1068 | |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1069 | static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) |
| 1070 | { |
| 1071 | mode_t ret; |
| 1072 | |
| 1073 | ret = mode & 0777; |
| 1074 | if (mode & P9_STAT_MODE_DIR) { |
| 1075 | ret |= S_IFDIR; |
| 1076 | } |
| 1077 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1078 | if (mode & P9_STAT_MODE_SYMLINK) { |
| 1079 | ret |= S_IFLNK; |
| 1080 | } |
| 1081 | if (mode & P9_STAT_MODE_SOCKET) { |
| 1082 | ret |= S_IFSOCK; |
| 1083 | } |
| 1084 | if (mode & P9_STAT_MODE_NAMED_PIPE) { |
| 1085 | ret |= S_IFIFO; |
| 1086 | } |
| 1087 | if (mode & P9_STAT_MODE_DEVICE) { |
Aneesh Kumar K.V | c7e587b | 2013-05-20 11:29:52 +0530 | [diff] [blame] | 1088 | if (extension->size && extension->data[0] == 'c') { |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1089 | ret |= S_IFCHR; |
| 1090 | } else { |
| 1091 | ret |= S_IFBLK; |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Xinhao Zhang | 0101173 | 2020-10-30 12:35:13 +0800 | [diff] [blame] | 1095 | if (!(ret & ~0777)) { |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1096 | ret |= S_IFREG; |
| 1097 | } |
| 1098 | |
| 1099 | if (mode & P9_STAT_MODE_SETUID) { |
| 1100 | ret |= S_ISUID; |
| 1101 | } |
| 1102 | if (mode & P9_STAT_MODE_SETGID) { |
| 1103 | ret |= S_ISGID; |
| 1104 | } |
| 1105 | if (mode & P9_STAT_MODE_SETVTX) { |
| 1106 | ret |= S_ISVTX; |
| 1107 | } |
| 1108 | |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
| 1112 | static int donttouch_stat(V9fsStat *stat) |
| 1113 | { |
| 1114 | if (stat->type == -1 && |
| 1115 | stat->dev == -1 && |
Antonios Motakis | 8703283 | 2019-10-10 11:36:04 +0200 | [diff] [blame] | 1116 | stat->qid.type == 0xff && |
| 1117 | stat->qid.version == (uint32_t) -1 && |
| 1118 | stat->qid.path == (uint64_t) -1 && |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1119 | stat->mode == -1 && |
| 1120 | stat->atime == -1 && |
| 1121 | stat->mtime == -1 && |
| 1122 | stat->length == -1 && |
| 1123 | !stat->name.size && |
| 1124 | !stat->uid.size && |
| 1125 | !stat->gid.size && |
| 1126 | !stat->muid.size && |
| 1127 | stat->n_uid == -1 && |
| 1128 | stat->n_gid == -1 && |
| 1129 | stat->n_muid == -1) { |
| 1130 | return 1; |
| 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1136 | static void v9fs_stat_init(V9fsStat *stat) |
| 1137 | { |
| 1138 | v9fs_string_init(&stat->name); |
| 1139 | v9fs_string_init(&stat->uid); |
| 1140 | v9fs_string_init(&stat->gid); |
| 1141 | v9fs_string_init(&stat->muid); |
| 1142 | v9fs_string_init(&stat->extension); |
| 1143 | } |
| 1144 | |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1145 | static void v9fs_stat_free(V9fsStat *stat) |
| 1146 | { |
| 1147 | v9fs_string_free(&stat->name); |
| 1148 | v9fs_string_free(&stat->uid); |
| 1149 | v9fs_string_free(&stat->gid); |
| 1150 | v9fs_string_free(&stat->muid); |
| 1151 | v9fs_string_free(&stat->extension); |
| 1152 | } |
| 1153 | |
| 1154 | static uint32_t stat_to_v9mode(const struct stat *stbuf) |
| 1155 | { |
| 1156 | uint32_t mode; |
| 1157 | |
| 1158 | mode = stbuf->st_mode & 0777; |
| 1159 | if (S_ISDIR(stbuf->st_mode)) { |
| 1160 | mode |= P9_STAT_MODE_DIR; |
| 1161 | } |
| 1162 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1163 | if (S_ISLNK(stbuf->st_mode)) { |
| 1164 | mode |= P9_STAT_MODE_SYMLINK; |
| 1165 | } |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1166 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1167 | if (S_ISSOCK(stbuf->st_mode)) { |
| 1168 | mode |= P9_STAT_MODE_SOCKET; |
| 1169 | } |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1170 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1171 | if (S_ISFIFO(stbuf->st_mode)) { |
| 1172 | mode |= P9_STAT_MODE_NAMED_PIPE; |
| 1173 | } |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1174 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1175 | if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { |
| 1176 | mode |= P9_STAT_MODE_DEVICE; |
| 1177 | } |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1178 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1179 | if (stbuf->st_mode & S_ISUID) { |
| 1180 | mode |= P9_STAT_MODE_SETUID; |
| 1181 | } |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1182 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1183 | if (stbuf->st_mode & S_ISGID) { |
| 1184 | mode |= P9_STAT_MODE_SETGID; |
| 1185 | } |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1186 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1187 | if (stbuf->st_mode & S_ISVTX) { |
| 1188 | mode |= P9_STAT_MODE_SETVTX; |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | return mode; |
| 1192 | } |
| 1193 | |
Jan Dakinevich | 6069537 | 2017-09-20 08:48:51 +0200 | [diff] [blame] | 1194 | static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, |
| 1195 | const char *basename, |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1196 | const struct stat *stbuf, |
| 1197 | V9fsStat *v9stat) |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1198 | { |
| 1199 | int err; |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1200 | |
| 1201 | memset(v9stat, 0, sizeof(*v9stat)); |
| 1202 | |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 1203 | err = stat_to_qid(pdu, stbuf, &v9stat->qid); |
| 1204 | if (err < 0) { |
| 1205 | return err; |
| 1206 | } |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1207 | v9stat->mode = stat_to_v9mode(stbuf); |
| 1208 | v9stat->atime = stbuf->st_atime; |
| 1209 | v9stat->mtime = stbuf->st_mtime; |
| 1210 | v9stat->length = stbuf->st_size; |
| 1211 | |
Greg Kurz | abdf008 | 2016-09-16 08:56:15 +0200 | [diff] [blame] | 1212 | v9fs_string_free(&v9stat->uid); |
| 1213 | v9fs_string_free(&v9stat->gid); |
| 1214 | v9fs_string_free(&v9stat->muid); |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1215 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1216 | v9stat->n_uid = stbuf->st_uid; |
| 1217 | v9stat->n_gid = stbuf->st_gid; |
| 1218 | v9stat->n_muid = 0; |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1219 | |
Greg Kurz | abdf008 | 2016-09-16 08:56:15 +0200 | [diff] [blame] | 1220 | v9fs_string_free(&v9stat->extension); |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1221 | |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1222 | if (v9stat->mode & P9_STAT_MODE_SYMLINK) { |
Jan Dakinevich | 6069537 | 2017-09-20 08:48:51 +0200 | [diff] [blame] | 1223 | err = v9fs_co_readlink(pdu, path, &v9stat->extension); |
Venkateswararao Jujjuri | 7a5ca31 | 2011-08-08 23:36:41 +0530 | [diff] [blame] | 1224 | if (err < 0) { |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1225 | return err; |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1226 | } |
Arun R Bharadwaj | cf03eb2 | 2010-07-28 13:55:05 +0530 | [diff] [blame] | 1227 | } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { |
| 1228 | v9fs_string_sprintf(&v9stat->extension, "%c %u %u", |
| 1229 | S_ISCHR(stbuf->st_mode) ? 'c' : 'b', |
| 1230 | major(stbuf->st_rdev), minor(stbuf->st_rdev)); |
| 1231 | } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { |
Stefan Weil | c9ba47d | 2010-09-27 18:45:47 +0200 | [diff] [blame] | 1232 | v9fs_string_sprintf(&v9stat->extension, "%s %lu", |
| 1233 | "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink); |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1234 | } |
| 1235 | |
Jan Dakinevich | 6069537 | 2017-09-20 08:48:51 +0200 | [diff] [blame] | 1236 | v9fs_string_sprintf(&v9stat->name, "%s", basename); |
Anthony Liguori | bb9e321 | 2010-04-29 17:44:49 +0530 | [diff] [blame] | 1237 | |
| 1238 | v9stat->size = 61 + |
| 1239 | v9fs_string_size(&v9stat->name) + |
| 1240 | v9fs_string_size(&v9stat->uid) + |
| 1241 | v9fs_string_size(&v9stat->gid) + |
| 1242 | v9fs_string_size(&v9stat->muid) + |
| 1243 | v9fs_string_size(&v9stat->extension); |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1247 | #define P9_STATS_MODE 0x00000001ULL |
| 1248 | #define P9_STATS_NLINK 0x00000002ULL |
| 1249 | #define P9_STATS_UID 0x00000004ULL |
| 1250 | #define P9_STATS_GID 0x00000008ULL |
| 1251 | #define P9_STATS_RDEV 0x00000010ULL |
| 1252 | #define P9_STATS_ATIME 0x00000020ULL |
| 1253 | #define P9_STATS_MTIME 0x00000040ULL |
| 1254 | #define P9_STATS_CTIME 0x00000080ULL |
| 1255 | #define P9_STATS_INO 0x00000100ULL |
| 1256 | #define P9_STATS_SIZE 0x00000200ULL |
| 1257 | #define P9_STATS_BLOCKS 0x00000400ULL |
| 1258 | |
| 1259 | #define P9_STATS_BTIME 0x00000800ULL |
| 1260 | #define P9_STATS_GEN 0x00001000ULL |
| 1261 | #define P9_STATS_DATA_VERSION 0x00002000ULL |
| 1262 | |
| 1263 | #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ |
| 1264 | #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ |
| 1265 | |
| 1266 | |
Christian Schoenebeck | b565bcc | 2021-09-27 17:45:00 +0200 | [diff] [blame] | 1267 | /** |
| 1268 | * Convert host filesystem's block size into an appropriate block size for |
| 1269 | * 9p client (guest OS side). The value returned suggests an "optimum" block |
| 1270 | * size for 9p I/O, i.e. to maximize performance. |
| 1271 | * |
| 1272 | * @pdu: 9p client request |
| 1273 | * @blksize: host filesystem's block size |
| 1274 | */ |
| 1275 | static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize) |
Christian Schoenebeck | 669ced0 | 2021-09-22 15:13:31 +0200 | [diff] [blame] | 1276 | { |
| 1277 | int32_t iounit = 0; |
| 1278 | V9fsState *s = pdu->s; |
| 1279 | |
| 1280 | /* |
Christian Schoenebeck | b565bcc | 2021-09-27 17:45:00 +0200 | [diff] [blame] | 1281 | * iounit should be multiples of blksize (host filesystem block size) |
Christian Schoenebeck | 669ced0 | 2021-09-22 15:13:31 +0200 | [diff] [blame] | 1282 | * as well as less than (client msize - P9_IOHDRSZ) |
| 1283 | */ |
Christian Schoenebeck | b565bcc | 2021-09-27 17:45:00 +0200 | [diff] [blame] | 1284 | if (blksize) { |
Christian Schoenebeck | 04a7f9e | 2021-09-27 17:50:36 +0200 | [diff] [blame] | 1285 | iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, blksize); |
Christian Schoenebeck | 669ced0 | 2021-09-22 15:13:31 +0200 | [diff] [blame] | 1286 | } |
| 1287 | if (!iounit) { |
| 1288 | iounit = s->msize - P9_IOHDRSZ; |
| 1289 | } |
| 1290 | return iounit; |
| 1291 | } |
| 1292 | |
Christian Schoenebeck | b565bcc | 2021-09-27 17:45:00 +0200 | [diff] [blame] | 1293 | static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf) |
| 1294 | { |
| 1295 | return blksize_to_iounit(pdu, stbuf->st_blksize); |
| 1296 | } |
| 1297 | |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 1298 | static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, |
Aneesh Kumar K.V | 8db21ce | 2011-05-18 16:04:33 -0700 | [diff] [blame] | 1299 | V9fsStatDotl *v9lstat) |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1300 | { |
| 1301 | memset(v9lstat, 0, sizeof(*v9lstat)); |
| 1302 | |
| 1303 | v9lstat->st_mode = stbuf->st_mode; |
| 1304 | v9lstat->st_nlink = stbuf->st_nlink; |
| 1305 | v9lstat->st_uid = stbuf->st_uid; |
| 1306 | v9lstat->st_gid = stbuf->st_gid; |
| 1307 | v9lstat->st_rdev = stbuf->st_rdev; |
| 1308 | v9lstat->st_size = stbuf->st_size; |
Christian Schoenebeck | 669ced0 | 2021-09-22 15:13:31 +0200 | [diff] [blame] | 1309 | v9lstat->st_blksize = stat_to_iounit(pdu, stbuf); |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1310 | v9lstat->st_blocks = stbuf->st_blocks; |
| 1311 | v9lstat->st_atime_sec = stbuf->st_atime; |
| 1312 | v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; |
| 1313 | v9lstat->st_mtime_sec = stbuf->st_mtime; |
| 1314 | v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; |
| 1315 | v9lstat->st_ctime_sec = stbuf->st_ctime; |
| 1316 | v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; |
| 1317 | /* Currently we only support BASIC fields in stat */ |
| 1318 | v9lstat->st_result_mask = P9_STATS_BASIC; |
| 1319 | |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 1320 | return stat_to_qid(pdu, stbuf, &v9lstat->qid); |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1321 | } |
| 1322 | |
Anthony Liguori | 1f5a89b | 2010-04-29 17:44:50 +0530 | [diff] [blame] | 1323 | static void print_sg(struct iovec *sg, int cnt) |
| 1324 | { |
| 1325 | int i; |
| 1326 | |
| 1327 | printf("sg[%d]: {", cnt); |
| 1328 | for (i = 0; i < cnt; i++) { |
| 1329 | if (i) { |
| 1330 | printf(", "); |
| 1331 | } |
| 1332 | printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); |
| 1333 | } |
| 1334 | printf("}\n"); |
| 1335 | } |
| 1336 | |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1337 | /* Will call this only for path name based fid */ |
| 1338 | static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 1339 | { |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1340 | V9fsPath str; |
| 1341 | v9fs_path_init(&str); |
| 1342 | v9fs_path_copy(&str, dst); |
Greg Kurz | e3e83f2 | 2016-09-16 08:56:15 +0200 | [diff] [blame] | 1343 | v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1344 | v9fs_path_free(&str); |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 1345 | } |
| 1346 | |
M. Mohan Kumar | 2c74c2c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 1347 | static inline bool is_ro_export(FsContext *ctx) |
| 1348 | { |
| 1349 | return ctx->export_flags & V9FS_RDONLY; |
| 1350 | } |
| 1351 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1352 | static void coroutine_fn v9fs_version(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1353 | { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1354 | ssize_t err; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 1355 | V9fsPDU *pdu = opaque; |
| 1356 | V9fsState *s = pdu->s; |
Anthony Liguori | 92c1ad0 | 2010-04-29 17:44:51 +0530 | [diff] [blame] | 1357 | V9fsString version; |
| 1358 | size_t offset = 7; |
| 1359 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1360 | v9fs_string_init(&version); |
| 1361 | err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); |
| 1362 | if (err < 0) { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1363 | goto out; |
| 1364 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1365 | trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data); |
Anthony Liguori | 92c1ad0 | 2010-04-29 17:44:51 +0530 | [diff] [blame] | 1366 | |
Deepak C Shetty | b41e299 | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 1367 | virtfs_reset(pdu); |
| 1368 | |
M. Mohan Kumar | 8415151 | 2010-05-27 13:57:29 +0530 | [diff] [blame] | 1369 | if (!strcmp(version.data, "9P2000.u")) { |
| 1370 | s->proto_version = V9FS_PROTO_2000U; |
| 1371 | } else if (!strcmp(version.data, "9P2000.L")) { |
| 1372 | s->proto_version = V9FS_PROTO_2000L; |
| 1373 | } else { |
Anthony Liguori | 92c1ad0 | 2010-04-29 17:44:51 +0530 | [diff] [blame] | 1374 | v9fs_string_sprintf(&version, "unknown"); |
Christian Schoenebeck | e16453a | 2020-02-08 09:24:19 +0100 | [diff] [blame] | 1375 | /* skip min. msize check, reporting invalid version has priority */ |
| 1376 | goto marshal; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1377 | } |
Anthony Liguori | 92c1ad0 | 2010-04-29 17:44:51 +0530 | [diff] [blame] | 1378 | |
Christian Schoenebeck | e16453a | 2020-02-08 09:24:19 +0100 | [diff] [blame] | 1379 | if (s->msize < P9_MIN_MSIZE) { |
| 1380 | err = -EMSGSIZE; |
| 1381 | error_report( |
| 1382 | "9pfs: Client requested msize < minimum msize (" |
| 1383 | stringify(P9_MIN_MSIZE) ") supported by this server." |
| 1384 | ); |
| 1385 | goto out; |
| 1386 | } |
| 1387 | |
Christian Schoenebeck | 62777d8 | 2020-09-03 16:20:21 +0200 | [diff] [blame] | 1388 | /* 8192 is the default msize of Linux clients */ |
Christian Schoenebeck | c418f93 | 2020-09-06 18:50:32 +0200 | [diff] [blame] | 1389 | if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) { |
Christian Schoenebeck | 62777d8 | 2020-09-03 16:20:21 +0200 | [diff] [blame] | 1390 | warn_report_once( |
| 1391 | "9p: degraded performance: a reasonable high msize should be " |
| 1392 | "chosen on client/guest side (chosen msize is <= 8192). See " |
| 1393 | "https://wiki.qemu.org/Documentation/9psetup#msize for details." |
| 1394 | ); |
| 1395 | } |
| 1396 | |
Christian Schoenebeck | e16453a | 2020-02-08 09:24:19 +0100 | [diff] [blame] | 1397 | marshal: |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1398 | err = pdu_marshal(pdu, offset, "ds", s->msize, &version); |
| 1399 | if (err < 0) { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1400 | goto out; |
| 1401 | } |
Philippe Mathieu-Daudé | 403a905 | 2017-08-09 16:32:46 +0200 | [diff] [blame] | 1402 | err += offset; |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1403 | trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1404 | out: |
Philippe Mathieu-Daudé | 403a905 | 2017-08-09 16:32:46 +0200 | [diff] [blame] | 1405 | pdu_complete(pdu, err); |
Anthony Liguori | 92c1ad0 | 2010-04-29 17:44:51 +0530 | [diff] [blame] | 1406 | v9fs_string_free(&version); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1407 | } |
| 1408 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1409 | static void coroutine_fn v9fs_attach(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1410 | { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 1411 | V9fsPDU *pdu = opaque; |
| 1412 | V9fsState *s = pdu->s; |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1413 | int32_t fid, afid, n_uname; |
| 1414 | V9fsString uname, aname; |
| 1415 | V9fsFidState *fidp; |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1416 | size_t offset = 7; |
Aneesh Kumar K.V | 8c15856 | 2011-05-08 13:15:29 +0530 | [diff] [blame] | 1417 | V9fsQID qid; |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1418 | ssize_t err; |
Christian Schoenebeck | 1102437 | 2021-06-04 19:52:18 +0200 | [diff] [blame] | 1419 | struct stat stbuf; |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1420 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1421 | v9fs_string_init(&uname); |
| 1422 | v9fs_string_init(&aname); |
| 1423 | err = pdu_unmarshal(pdu, offset, "ddssd", &fid, |
| 1424 | &afid, &uname, &aname, &n_uname); |
| 1425 | if (err < 0) { |
| 1426 | goto out_nofid; |
| 1427 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1428 | trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1429 | |
| 1430 | fidp = alloc_fid(s, fid); |
| 1431 | if (fidp == NULL) { |
| 1432 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1433 | goto out_nofid; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1434 | } |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1435 | fidp->uid = n_uname; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1436 | err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1437 | if (err < 0) { |
| 1438 | err = -EINVAL; |
| 1439 | clunk_fid(s, fid); |
| 1440 | goto out; |
| 1441 | } |
Christian Schoenebeck | 1102437 | 2021-06-04 19:52:18 +0200 | [diff] [blame] | 1442 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
| 1443 | if (err < 0) { |
| 1444 | err = -EINVAL; |
| 1445 | clunk_fid(s, fid); |
| 1446 | goto out; |
| 1447 | } |
| 1448 | err = stat_to_qid(pdu, &stbuf, &qid); |
Aneesh Kumar K.V | 8c15856 | 2011-05-08 13:15:29 +0530 | [diff] [blame] | 1449 | if (err < 0) { |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1450 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1451 | clunk_fid(s, fid); |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1452 | goto out; |
| 1453 | } |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1454 | |
| 1455 | /* |
| 1456 | * disable migration if we haven't done already. |
| 1457 | * attach could get called multiple times for the same export. |
| 1458 | */ |
| 1459 | if (!s->migration_blocker) { |
| 1460 | error_setg(&s->migration_blocker, |
| 1461 | "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", |
| 1462 | s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); |
Markus Armbruster | 9261ef5 | 2020-06-30 11:03:28 +0200 | [diff] [blame] | 1463 | err = migrate_add_blocker(s->migration_blocker, NULL); |
| 1464 | if (err < 0) { |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1465 | error_free(s->migration_blocker); |
| 1466 | s->migration_blocker = NULL; |
| 1467 | clunk_fid(s, fid); |
| 1468 | goto out; |
| 1469 | } |
| 1470 | s->root_fid = fid; |
| 1471 | } |
| 1472 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1473 | err = pdu_marshal(pdu, offset, "Q", &qid); |
| 1474 | if (err < 0) { |
| 1475 | clunk_fid(s, fid); |
| 1476 | goto out; |
| 1477 | } |
| 1478 | err += offset; |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1479 | |
Christian Schoenebeck | 1102437 | 2021-06-04 19:52:18 +0200 | [diff] [blame] | 1480 | memcpy(&s->root_st, &stbuf, sizeof(stbuf)); |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 1481 | trace_v9fs_attach_return(pdu->tag, pdu->id, |
| 1482 | qid.type, qid.version, qid.path); |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1483 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1484 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1485 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 1486 | pdu_complete(pdu, err); |
Anthony Liguori | 955efc4 | 2010-04-29 17:44:52 +0530 | [diff] [blame] | 1487 | v9fs_string_free(&uname); |
| 1488 | v9fs_string_free(&aname); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1489 | } |
| 1490 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1491 | static void coroutine_fn v9fs_stat(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1492 | { |
Aneesh Kumar K.V | d8e0c29 | 2011-05-07 18:29:24 +0530 | [diff] [blame] | 1493 | int32_t fid; |
| 1494 | V9fsStat v9stat; |
| 1495 | ssize_t err = 0; |
| 1496 | size_t offset = 7; |
| 1497 | struct stat stbuf; |
| 1498 | V9fsFidState *fidp; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 1499 | V9fsPDU *pdu = opaque; |
Jan Dakinevich | 6069537 | 2017-09-20 08:48:51 +0200 | [diff] [blame] | 1500 | char *basename; |
Anthony Liguori | 4da7d3f | 2010-04-29 17:44:53 +0530 | [diff] [blame] | 1501 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1502 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
| 1503 | if (err < 0) { |
| 1504 | goto out_nofid; |
| 1505 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1506 | trace_v9fs_stat(pdu->tag, pdu->id, fid); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1507 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1508 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | d8e0c29 | 2011-05-07 18:29:24 +0530 | [diff] [blame] | 1509 | if (fidp == NULL) { |
Anthony Liguori | 4da7d3f | 2010-04-29 17:44:53 +0530 | [diff] [blame] | 1510 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1511 | goto out_nofid; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1512 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1513 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
Aneesh Kumar K.V | d8e0c29 | 2011-05-07 18:29:24 +0530 | [diff] [blame] | 1514 | if (err < 0) { |
| 1515 | goto out; |
| 1516 | } |
Jan Dakinevich | 6069537 | 2017-09-20 08:48:51 +0200 | [diff] [blame] | 1517 | basename = g_path_get_basename(fidp->path.data); |
| 1518 | err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat); |
| 1519 | g_free(basename); |
Aneesh Kumar K.V | d8e0c29 | 2011-05-07 18:29:24 +0530 | [diff] [blame] | 1520 | if (err < 0) { |
| 1521 | goto out; |
| 1522 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1523 | err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); |
| 1524 | if (err < 0) { |
| 1525 | v9fs_stat_free(&v9stat); |
| 1526 | goto out; |
| 1527 | } |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 1528 | trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, |
| 1529 | v9stat.atime, v9stat.mtime, v9stat.length); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1530 | err += offset; |
Aneesh Kumar K.V | d8e0c29 | 2011-05-07 18:29:24 +0530 | [diff] [blame] | 1531 | v9fs_stat_free(&v9stat); |
Anthony Liguori | 4da7d3f | 2010-04-29 17:44:53 +0530 | [diff] [blame] | 1532 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1533 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1534 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 1535 | pdu_complete(pdu, err); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1536 | } |
| 1537 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1538 | static void coroutine_fn v9fs_getattr(void *opaque) |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1539 | { |
| 1540 | int32_t fid; |
Aneesh Kumar K.V | 8db21ce | 2011-05-18 16:04:33 -0700 | [diff] [blame] | 1541 | size_t offset = 7; |
| 1542 | ssize_t retval = 0; |
| 1543 | struct stat stbuf; |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1544 | V9fsFidState *fidp; |
| 1545 | uint64_t request_mask; |
Aneesh Kumar K.V | 8db21ce | 2011-05-18 16:04:33 -0700 | [diff] [blame] | 1546 | V9fsStatDotl v9stat_dotl; |
| 1547 | V9fsPDU *pdu = opaque; |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1548 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1549 | retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); |
| 1550 | if (retval < 0) { |
| 1551 | goto out_nofid; |
| 1552 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1553 | trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1554 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1555 | fidp = get_fid(pdu, fid); |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1556 | if (fidp == NULL) { |
Aneesh Kumar K.V | 8db21ce | 2011-05-18 16:04:33 -0700 | [diff] [blame] | 1557 | retval = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1558 | goto out_nofid; |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1559 | } |
Aneesh Kumar K.V | 8db21ce | 2011-05-18 16:04:33 -0700 | [diff] [blame] | 1560 | /* |
| 1561 | * Currently we only support BASIC fields in stat, so there is no |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1562 | * need to look at request_mask. |
| 1563 | */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1564 | retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
Aneesh Kumar K.V | 8db21ce | 2011-05-18 16:04:33 -0700 | [diff] [blame] | 1565 | if (retval < 0) { |
| 1566 | goto out; |
| 1567 | } |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 1568 | retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl); |
| 1569 | if (retval < 0) { |
| 1570 | goto out; |
| 1571 | } |
Harsh Prateek Bora | e06a765 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1572 | |
| 1573 | /* fill st_gen if requested and supported by underlying fs */ |
| 1574 | if (request_mask & P9_STATS_GEN) { |
| 1575 | retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); |
Kirill A. Shutemov | f8b7ee3 | 2014-01-28 17:08:27 +0200 | [diff] [blame] | 1576 | switch (retval) { |
| 1577 | case 0: |
| 1578 | /* we have valid st_gen: update result mask */ |
| 1579 | v9stat_dotl.st_result_mask |= P9_STATS_GEN; |
| 1580 | break; |
| 1581 | case -EINTR: |
| 1582 | /* request cancelled, e.g. by Tflush */ |
Harsh Prateek Bora | e06a765 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1583 | goto out; |
Kirill A. Shutemov | f8b7ee3 | 2014-01-28 17:08:27 +0200 | [diff] [blame] | 1584 | default: |
| 1585 | /* failed to get st_gen: not fatal, ignore */ |
| 1586 | break; |
Harsh Prateek Bora | e06a765 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1587 | } |
Harsh Prateek Bora | e06a765 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1588 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1589 | retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); |
| 1590 | if (retval < 0) { |
| 1591 | goto out; |
| 1592 | } |
| 1593 | retval += offset; |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1594 | trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, |
| 1595 | v9stat_dotl.st_mode, v9stat_dotl.st_uid, |
| 1596 | v9stat_dotl.st_gid); |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 1597 | out: |
| 1598 | put_fid(pdu, fidp); |
| 1599 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 1600 | pdu_complete(pdu, retval); |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 1601 | } |
| 1602 | |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1603 | /* Attribute flags */ |
| 1604 | #define P9_ATTR_MODE (1 << 0) |
| 1605 | #define P9_ATTR_UID (1 << 1) |
| 1606 | #define P9_ATTR_GID (1 << 2) |
| 1607 | #define P9_ATTR_SIZE (1 << 3) |
| 1608 | #define P9_ATTR_ATIME (1 << 4) |
| 1609 | #define P9_ATTR_MTIME (1 << 5) |
| 1610 | #define P9_ATTR_CTIME (1 << 6) |
| 1611 | #define P9_ATTR_ATIME_SET (1 << 7) |
| 1612 | #define P9_ATTR_MTIME_SET (1 << 8) |
| 1613 | |
| 1614 | #define P9_ATTR_MASK 127 |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1615 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1616 | static void coroutine_fn v9fs_setattr(void *opaque) |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1617 | { |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1618 | int err = 0; |
| 1619 | int32_t fid; |
| 1620 | V9fsFidState *fidp; |
| 1621 | size_t offset = 7; |
| 1622 | V9fsIattr v9iattr; |
| 1623 | V9fsPDU *pdu = opaque; |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1624 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1625 | err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); |
| 1626 | if (err < 0) { |
| 1627 | goto out_nofid; |
| 1628 | } |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1629 | |
Greg Kurz | 8f9c64b | 2018-05-02 08:59:24 +0200 | [diff] [blame] | 1630 | trace_v9fs_setattr(pdu->tag, pdu->id, fid, |
| 1631 | v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid, |
| 1632 | v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec); |
| 1633 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1634 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1635 | if (fidp == NULL) { |
| 1636 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1637 | goto out_nofid; |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1638 | } |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1639 | if (v9iattr.valid & P9_ATTR_MODE) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1640 | err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1641 | if (err < 0) { |
| 1642 | goto out; |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1643 | } |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1644 | } |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1645 | if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1646 | struct timespec times[2]; |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1647 | if (v9iattr.valid & P9_ATTR_ATIME) { |
| 1648 | if (v9iattr.valid & P9_ATTR_ATIME_SET) { |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1649 | times[0].tv_sec = v9iattr.atime_sec; |
| 1650 | times[0].tv_nsec = v9iattr.atime_nsec; |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1651 | } else { |
| 1652 | times[0].tv_nsec = UTIME_NOW; |
| 1653 | } |
| 1654 | } else { |
| 1655 | times[0].tv_nsec = UTIME_OMIT; |
| 1656 | } |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1657 | if (v9iattr.valid & P9_ATTR_MTIME) { |
| 1658 | if (v9iattr.valid & P9_ATTR_MTIME_SET) { |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1659 | times[1].tv_sec = v9iattr.mtime_sec; |
| 1660 | times[1].tv_nsec = v9iattr.mtime_nsec; |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1661 | } else { |
| 1662 | times[1].tv_nsec = UTIME_NOW; |
| 1663 | } |
| 1664 | } else { |
| 1665 | times[1].tv_nsec = UTIME_OMIT; |
| 1666 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1667 | err = v9fs_co_utimensat(pdu, &fidp->path, times); |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1668 | if (err < 0) { |
| 1669 | goto out; |
| 1670 | } |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1671 | } |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1672 | /* |
| 1673 | * If the only valid entry in iattr is ctime we can call |
| 1674 | * chown(-1,-1) to update the ctime of the file |
| 1675 | */ |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1676 | if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || |
| 1677 | ((v9iattr.valid & P9_ATTR_CTIME) |
| 1678 | && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { |
| 1679 | if (!(v9iattr.valid & P9_ATTR_UID)) { |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1680 | v9iattr.uid = -1; |
| 1681 | } |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1682 | if (!(v9iattr.valid & P9_ATTR_GID)) { |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1683 | v9iattr.gid = -1; |
| 1684 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1685 | err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1686 | v9iattr.gid); |
| 1687 | if (err < 0) { |
| 1688 | goto out; |
| 1689 | } |
| 1690 | } |
Aneesh Kumar K.V | e4027ca | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 1691 | if (v9iattr.valid & (P9_ATTR_SIZE)) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1692 | err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); |
Aneesh Kumar K.V | 65c05f9 | 2011-05-18 16:05:10 -0700 | [diff] [blame] | 1693 | if (err < 0) { |
| 1694 | goto out; |
| 1695 | } |
| 1696 | } |
| 1697 | err = offset; |
Greg Kurz | 8f9c64b | 2018-05-02 08:59:24 +0200 | [diff] [blame] | 1698 | trace_v9fs_setattr_return(pdu->tag, pdu->id); |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1699 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1700 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1701 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 1702 | pdu_complete(pdu, err); |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 1703 | } |
| 1704 | |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1705 | static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1706 | { |
| 1707 | int i; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1708 | ssize_t err; |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1709 | size_t offset = 7; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1710 | |
| 1711 | err = pdu_marshal(pdu, offset, "w", nwnames); |
| 1712 | if (err < 0) { |
| 1713 | return err; |
| 1714 | } |
| 1715 | offset += err; |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1716 | for (i = 0; i < nwnames; i++) { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1717 | err = pdu_marshal(pdu, offset, "Q", &qids[i]); |
| 1718 | if (err < 0) { |
| 1719 | return err; |
| 1720 | } |
| 1721 | offset += err; |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1722 | } |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1723 | return offset; |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1724 | } |
| 1725 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 1726 | static bool name_is_illegal(const char *name) |
| 1727 | { |
| 1728 | return !*name || strchr(name, '/') != NULL; |
| 1729 | } |
| 1730 | |
Christian Schoenebeck | f22cad4 | 2021-06-04 19:57:21 +0200 | [diff] [blame] | 1731 | static bool same_stat_id(const struct stat *a, const struct stat *b) |
Greg Kurz | 56f101e | 2016-08-30 17:02:27 +0200 | [diff] [blame] | 1732 | { |
Christian Schoenebeck | f22cad4 | 2021-06-04 19:57:21 +0200 | [diff] [blame] | 1733 | return a->st_dev == b->st_dev && a->st_ino == b->st_ino; |
Greg Kurz | 56f101e | 2016-08-30 17:02:27 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1736 | static void coroutine_fn v9fs_walk(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1737 | { |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1738 | int name_idx; |
Christian Schoenebeck | 869605b | 2021-08-17 15:46:50 +0200 | [diff] [blame] | 1739 | g_autofree V9fsQID *qids = NULL; |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1740 | int i, err = 0; |
Christian Schoenebeck | 7e98578 | 2021-10-01 16:27:59 +0200 | [diff] [blame] | 1741 | V9fsPath dpath, path; |
| 1742 | P9ARRAY_REF(V9fsPath) pathes = NULL; |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1743 | uint16_t nwnames; |
Christian Schoenebeck | 869605b | 2021-08-17 15:46:50 +0200 | [diff] [blame] | 1744 | struct stat stbuf, fidst; |
| 1745 | g_autofree struct stat *stbufs = NULL; |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1746 | size_t offset = 7; |
| 1747 | int32_t fid, newfid; |
Christian Schoenebeck | 7e98578 | 2021-10-01 16:27:59 +0200 | [diff] [blame] | 1748 | P9ARRAY_REF(V9fsString) wnames = NULL; |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1749 | V9fsFidState *fidp; |
Dong Xu Wang | 3a93113 | 2011-11-29 16:52:38 +0800 | [diff] [blame] | 1750 | V9fsFidState *newfidp = NULL; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 1751 | V9fsPDU *pdu = opaque; |
| 1752 | V9fsState *s = pdu->s; |
Greg Kurz | 56f101e | 2016-08-30 17:02:27 +0200 | [diff] [blame] | 1753 | V9fsQID qid; |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1754 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1755 | err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames); |
| 1756 | if (err < 0) { |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 1757 | pdu_complete(pdu, err); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1758 | return ; |
| 1759 | } |
| 1760 | offset += err; |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1761 | |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1762 | trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames); |
| 1763 | |
Christian Schoenebeck | 232a4d2 | 2021-05-16 17:55:34 +0200 | [diff] [blame] | 1764 | if (nwnames > P9_MAXWELEM) { |
| 1765 | err = -EINVAL; |
| 1766 | goto out_nofid; |
| 1767 | } |
| 1768 | if (nwnames) { |
Christian Schoenebeck | 7e98578 | 2021-10-01 16:27:59 +0200 | [diff] [blame] | 1769 | P9ARRAY_NEW(V9fsString, wnames, nwnames); |
Greg Kurz | 1923923 | 2018-12-12 14:18:10 +0100 | [diff] [blame] | 1770 | qids = g_new0(V9fsQID, nwnames); |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1771 | stbufs = g_new0(struct stat, nwnames); |
Christian Schoenebeck | 7e98578 | 2021-10-01 16:27:59 +0200 | [diff] [blame] | 1772 | P9ARRAY_NEW(V9fsPath, pathes, nwnames); |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1773 | for (i = 0; i < nwnames; i++) { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1774 | err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); |
| 1775 | if (err < 0) { |
| 1776 | goto out_nofid; |
| 1777 | } |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 1778 | if (name_is_illegal(wnames[i].data)) { |
| 1779 | err = -ENOENT; |
| 1780 | goto out_nofid; |
| 1781 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1782 | offset += err; |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1783 | } |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1784 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1785 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1786 | if (fidp == NULL) { |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1787 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1788 | goto out_nofid; |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1789 | } |
Greg Kurz | 56f101e | 2016-08-30 17:02:27 +0200 | [diff] [blame] | 1790 | |
Greg Kurz | 13fd08e | 2016-09-16 11:44:49 +0200 | [diff] [blame] | 1791 | v9fs_path_init(&dpath); |
| 1792 | v9fs_path_init(&path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1793 | /* |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1794 | * Both dpath and path initially point to fidp. |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1795 | * Needed to handle request with nwnames == 0 |
| 1796 | */ |
| 1797 | v9fs_path_copy(&dpath, &fidp->path); |
| 1798 | v9fs_path_copy(&path, &fidp->path); |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1799 | |
| 1800 | /* |
| 1801 | * To keep latency (i.e. overall execution time for processing this |
| 1802 | * Twalk client request) as small as possible, run all the required fs |
| 1803 | * driver code altogether inside the following block. |
| 1804 | */ |
| 1805 | v9fs_co_run_in_worker({ |
| 1806 | if (v9fs_request_cancelled(pdu)) { |
| 1807 | err = -EINTR; |
| 1808 | break; |
| 1809 | } |
| 1810 | err = s->ops->lstat(&s->ctx, &dpath, &fidst); |
| 1811 | if (err < 0) { |
| 1812 | err = -errno; |
| 1813 | break; |
| 1814 | } |
| 1815 | stbuf = fidst; |
| 1816 | for (name_idx = 0; name_idx < nwnames; name_idx++) { |
| 1817 | if (v9fs_request_cancelled(pdu)) { |
| 1818 | err = -EINTR; |
| 1819 | break; |
| 1820 | } |
| 1821 | if (!same_stat_id(&pdu->s->root_st, &stbuf) || |
| 1822 | strcmp("..", wnames[name_idx].data)) |
| 1823 | { |
| 1824 | err = s->ops->name_to_path(&s->ctx, &dpath, |
Christian Schoenebeck | 97b1d8f | 2021-08-17 14:38:24 +0200 | [diff] [blame] | 1825 | wnames[name_idx].data, |
| 1826 | &pathes[name_idx]); |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1827 | if (err < 0) { |
| 1828 | err = -errno; |
| 1829 | break; |
| 1830 | } |
| 1831 | if (v9fs_request_cancelled(pdu)) { |
| 1832 | err = -EINTR; |
| 1833 | break; |
| 1834 | } |
Christian Schoenebeck | 97b1d8f | 2021-08-17 14:38:24 +0200 | [diff] [blame] | 1835 | err = s->ops->lstat(&s->ctx, &pathes[name_idx], &stbuf); |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1836 | if (err < 0) { |
| 1837 | err = -errno; |
| 1838 | break; |
| 1839 | } |
| 1840 | stbufs[name_idx] = stbuf; |
Christian Schoenebeck | 97b1d8f | 2021-08-17 14:38:24 +0200 | [diff] [blame] | 1841 | v9fs_path_copy(&dpath, &pathes[name_idx]); |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | }); |
| 1845 | /* |
| 1846 | * Handle all the rest of this Twalk request on main thread ... |
| 1847 | */ |
| 1848 | if (err < 0) { |
| 1849 | goto out; |
| 1850 | } |
| 1851 | |
| 1852 | err = stat_to_qid(pdu, &fidst, &qid); |
| 1853 | if (err < 0) { |
| 1854 | goto out; |
| 1855 | } |
| 1856 | stbuf = fidst; |
| 1857 | |
| 1858 | /* reset dpath and path */ |
| 1859 | v9fs_path_copy(&dpath, &fidp->path); |
| 1860 | v9fs_path_copy(&path, &fidp->path); |
| 1861 | |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1862 | for (name_idx = 0; name_idx < nwnames; name_idx++) { |
Christian Schoenebeck | f22cad4 | 2021-06-04 19:57:21 +0200 | [diff] [blame] | 1863 | if (!same_stat_id(&pdu->s->root_st, &stbuf) || |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1864 | strcmp("..", wnames[name_idx].data)) |
| 1865 | { |
| 1866 | stbuf = stbufs[name_idx]; |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 1867 | err = stat_to_qid(pdu, &stbuf, &qid); |
| 1868 | if (err < 0) { |
| 1869 | goto out; |
| 1870 | } |
Christian Schoenebeck | 8d6cb10 | 2021-07-02 17:16:32 +0200 | [diff] [blame] | 1871 | v9fs_path_copy(&path, &pathes[name_idx]); |
Greg Kurz | 56f101e | 2016-08-30 17:02:27 +0200 | [diff] [blame] | 1872 | v9fs_path_copy(&dpath, &path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1873 | } |
Greg Kurz | 56f101e | 2016-08-30 17:02:27 +0200 | [diff] [blame] | 1874 | memcpy(&qids[name_idx], &qid, sizeof(qid)); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1875 | } |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1876 | if (fid == newfid) { |
Greg Kurz | 49dd946 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 1877 | if (fidp->fid_type != P9_FID_NONE) { |
| 1878 | err = -EINVAL; |
| 1879 | goto out; |
| 1880 | } |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 1881 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1882 | v9fs_path_copy(&fidp->path, &path); |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 1883 | v9fs_path_unlock(s); |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1884 | } else { |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1885 | newfidp = alloc_fid(s, newfid); |
| 1886 | if (newfidp == NULL) { |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1887 | err = -EINVAL; |
| 1888 | goto out; |
| 1889 | } |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1890 | newfidp->uid = fidp->uid; |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1891 | v9fs_path_copy(&newfidp->path, &path); |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1892 | } |
Aneesh Kumar K.V | 3cc19c0 | 2011-05-07 19:41:55 +0530 | [diff] [blame] | 1893 | err = v9fs_walk_marshal(pdu, nwnames, qids); |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 1894 | trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids); |
Anthony Liguori | ff5e54c | 2010-04-29 17:44:54 +0530 | [diff] [blame] | 1895 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1896 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1897 | if (newfidp) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1898 | put_fid(pdu, newfidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1899 | } |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 1900 | v9fs_path_free(&dpath); |
| 1901 | v9fs_path_free(&path); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1902 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 1903 | pdu_complete(pdu, err); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 1904 | } |
| 1905 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1906 | static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 1907 | { |
| 1908 | struct statfs stbuf; |
Christian Schoenebeck | b565bcc | 2021-09-27 17:45:00 +0200 | [diff] [blame] | 1909 | int err = v9fs_co_statfs(pdu, path, &stbuf); |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 1910 | |
Christian Schoenebeck | b565bcc | 2021-09-27 17:45:00 +0200 | [diff] [blame] | 1911 | return blksize_to_iounit(pdu, (err >= 0) ? stbuf.f_bsize : 0); |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 1912 | } |
| 1913 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 1914 | static void coroutine_fn v9fs_open(void *opaque) |
Anthony Liguori | a6568fe | 2010-04-29 17:44:55 +0530 | [diff] [blame] | 1915 | { |
M. Mohan Kumar | 771e9d4 | 2010-06-22 19:47:04 +0530 | [diff] [blame] | 1916 | int flags; |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1917 | int32_t fid; |
| 1918 | int32_t mode; |
| 1919 | V9fsQID qid; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 1920 | int iounit = 0; |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1921 | ssize_t err = 0; |
| 1922 | size_t offset = 7; |
| 1923 | struct stat stbuf; |
| 1924 | V9fsFidState *fidp; |
| 1925 | V9fsPDU *pdu = opaque; |
| 1926 | V9fsState *s = pdu->s; |
M. Mohan Kumar | 771e9d4 | 2010-06-22 19:47:04 +0530 | [diff] [blame] | 1927 | |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1928 | if (s->proto_version == V9FS_PROTO_2000L) { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1929 | err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1930 | } else { |
Benjamin Herrenschmidt | 67d6fa5 | 2012-02-24 11:23:30 +1100 | [diff] [blame] | 1931 | uint8_t modebyte; |
| 1932 | err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); |
| 1933 | mode = modebyte; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1934 | } |
| 1935 | if (err < 0) { |
| 1936 | goto out_nofid; |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1937 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 1938 | trace_v9fs_open(pdu->tag, pdu->id, fid, mode); |
| 1939 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1940 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1941 | if (fidp == NULL) { |
| 1942 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 1943 | goto out_nofid; |
Anthony Liguori | a6568fe | 2010-04-29 17:44:55 +0530 | [diff] [blame] | 1944 | } |
Greg Kurz | 49dd946 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 1945 | if (fidp->fid_type != P9_FID_NONE) { |
| 1946 | err = -EINVAL; |
| 1947 | goto out; |
| 1948 | } |
Anthony Liguori | a6568fe | 2010-04-29 17:44:55 +0530 | [diff] [blame] | 1949 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1950 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1951 | if (err < 0) { |
| 1952 | goto out; |
| 1953 | } |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 1954 | err = stat_to_qid(pdu, &stbuf, &qid); |
| 1955 | if (err < 0) { |
| 1956 | goto out; |
| 1957 | } |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1958 | if (S_ISDIR(stbuf.st_mode)) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1959 | err = v9fs_co_opendir(pdu, fidp); |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1960 | if (err < 0) { |
| 1961 | goto out; |
| 1962 | } |
| 1963 | fidp->fid_type = P9_FID_DIR; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1964 | err = pdu_marshal(pdu, offset, "Qd", &qid, 0); |
| 1965 | if (err < 0) { |
| 1966 | goto out; |
| 1967 | } |
| 1968 | err += offset; |
Anthony Liguori | a6568fe | 2010-04-29 17:44:55 +0530 | [diff] [blame] | 1969 | } else { |
M. Mohan Kumar | 771e9d4 | 2010-06-22 19:47:04 +0530 | [diff] [blame] | 1970 | if (s->proto_version == V9FS_PROTO_2000L) { |
Aneesh Kumar K.V | d3ab98e | 2011-10-12 19:11:23 +0530 | [diff] [blame] | 1971 | flags = get_dotl_openflags(s, mode); |
M. Mohan Kumar | 771e9d4 | 2010-06-22 19:47:04 +0530 | [diff] [blame] | 1972 | } else { |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1973 | flags = omode_to_uflags(mode); |
M. Mohan Kumar | 771e9d4 | 2010-06-22 19:47:04 +0530 | [diff] [blame] | 1974 | } |
M. Mohan Kumar | 2c74c2c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 1975 | if (is_ro_export(&s->ctx)) { |
| 1976 | if (mode & O_WRONLY || mode & O_RDWR || |
| 1977 | mode & O_APPEND || mode & O_TRUNC) { |
| 1978 | err = -EROFS; |
| 1979 | goto out; |
| 1980 | } |
M. Mohan Kumar | 2c74c2c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 1981 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1982 | err = v9fs_co_open(pdu, fidp, flags); |
Aneesh Kumar K.V | 857bc15 | 2011-05-07 17:36:36 +0530 | [diff] [blame] | 1983 | if (err < 0) { |
| 1984 | goto out; |
| 1985 | } |
| 1986 | fidp->fid_type = P9_FID_FILE; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 1987 | fidp->open_flags = flags; |
| 1988 | if (flags & O_EXCL) { |
| 1989 | /* |
| 1990 | * We let the host file system do O_EXCL check |
| 1991 | * We should not reclaim such fd |
| 1992 | */ |
| 1993 | fidp->flags |= FID_NON_RECLAIMABLE; |
| 1994 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 1995 | iounit = get_iounit(pdu, &fidp->path); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 1996 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); |
| 1997 | if (err < 0) { |
| 1998 | goto out; |
| 1999 | } |
| 2000 | err += offset; |
Anthony Liguori | a6568fe | 2010-04-29 17:44:55 +0530 | [diff] [blame] | 2001 | } |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 2002 | trace_v9fs_open_return(pdu->tag, pdu->id, |
| 2003 | qid.type, qid.version, qid.path, iounit); |
Anthony Liguori | a6568fe | 2010-04-29 17:44:55 +0530 | [diff] [blame] | 2004 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2005 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2006 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2007 | pdu_complete(pdu, err); |
Anthony Liguori | a6568fe | 2010-04-29 17:44:55 +0530 | [diff] [blame] | 2008 | } |
| 2009 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2010 | static void coroutine_fn v9fs_lcreate(void *opaque) |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2011 | { |
| 2012 | int32_t dfid, flags, mode; |
| 2013 | gid_t gid; |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2014 | ssize_t err = 0; |
Venkateswararao Jujjuri | 36f8981 | 2011-08-08 23:54:08 +0530 | [diff] [blame] | 2015 | ssize_t offset = 7; |
Venkateswararao Jujjuri | 36f8981 | 2011-08-08 23:54:08 +0530 | [diff] [blame] | 2016 | V9fsString name; |
| 2017 | V9fsFidState *fidp; |
| 2018 | struct stat stbuf; |
| 2019 | V9fsQID qid; |
| 2020 | int32_t iounit; |
| 2021 | V9fsPDU *pdu = opaque; |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2022 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2023 | v9fs_string_init(&name); |
| 2024 | err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, |
| 2025 | &name, &flags, &mode, &gid); |
| 2026 | if (err < 0) { |
| 2027 | goto out_nofid; |
| 2028 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2029 | trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2030 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 2031 | if (name_is_illegal(name.data)) { |
| 2032 | err = -ENOENT; |
| 2033 | goto out_nofid; |
| 2034 | } |
| 2035 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 2036 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
| 2037 | err = -EEXIST; |
| 2038 | goto out_nofid; |
| 2039 | } |
| 2040 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2041 | fidp = get_fid(pdu, dfid); |
Venkateswararao Jujjuri | 36f8981 | 2011-08-08 23:54:08 +0530 | [diff] [blame] | 2042 | if (fidp == NULL) { |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2043 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2044 | goto out_nofid; |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2045 | } |
Li Qiang | d63fb19 | 2017-03-27 21:13:19 +0200 | [diff] [blame] | 2046 | if (fidp->fid_type != P9_FID_NONE) { |
| 2047 | err = -EINVAL; |
| 2048 | goto out; |
| 2049 | } |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2050 | |
Aneesh Kumar K.V | d3ab98e | 2011-10-12 19:11:23 +0530 | [diff] [blame] | 2051 | flags = get_dotl_openflags(pdu->s, flags); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2052 | err = v9fs_co_open2(pdu, fidp, &name, gid, |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 2053 | flags | O_CREAT, mode, &stbuf); |
Venkateswararao Jujjuri | 36f8981 | 2011-08-08 23:54:08 +0530 | [diff] [blame] | 2054 | if (err < 0) { |
| 2055 | goto out; |
| 2056 | } |
| 2057 | fidp->fid_type = P9_FID_FILE; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 2058 | fidp->open_flags = flags; |
| 2059 | if (flags & O_EXCL) { |
| 2060 | /* |
| 2061 | * We let the host file system do O_EXCL check |
| 2062 | * We should not reclaim such fd |
| 2063 | */ |
| 2064 | fidp->flags |= FID_NON_RECLAIMABLE; |
| 2065 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2066 | iounit = get_iounit(pdu, &fidp->path); |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 2067 | err = stat_to_qid(pdu, &stbuf, &qid); |
| 2068 | if (err < 0) { |
| 2069 | goto out; |
| 2070 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2071 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); |
| 2072 | if (err < 0) { |
| 2073 | goto out; |
| 2074 | } |
| 2075 | err += offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 2076 | trace_v9fs_lcreate_return(pdu->tag, pdu->id, |
| 2077 | qid.type, qid.version, qid.path, iounit); |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2078 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2079 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2080 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2081 | pdu_complete(pdu, err); |
Venkateswararao Jujjuri | 36f8981 | 2011-08-08 23:54:08 +0530 | [diff] [blame] | 2082 | v9fs_string_free(&name); |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Greg Kurz | a1bf8b7 | 2016-11-25 12:54:21 +0100 | [diff] [blame] | 2085 | static void coroutine_fn v9fs_fsync(void *opaque) |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 2086 | { |
Aneesh Kumar K.V | 4e9ad44 | 2011-05-07 21:25:51 +0530 | [diff] [blame] | 2087 | int err; |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 2088 | int32_t fid; |
Aneesh Kumar K.V | 4e9ad44 | 2011-05-07 21:25:51 +0530 | [diff] [blame] | 2089 | int datasync; |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 2090 | size_t offset = 7; |
| 2091 | V9fsFidState *fidp; |
Aneesh Kumar K.V | 4e9ad44 | 2011-05-07 21:25:51 +0530 | [diff] [blame] | 2092 | V9fsPDU *pdu = opaque; |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 2093 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2094 | err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); |
| 2095 | if (err < 0) { |
| 2096 | goto out_nofid; |
| 2097 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2098 | trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); |
| 2099 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2100 | fidp = get_fid(pdu, fid); |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 2101 | if (fidp == NULL) { |
| 2102 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2103 | goto out_nofid; |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 2104 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2105 | err = v9fs_co_fsync(pdu, fidp, datasync); |
Aneesh Kumar K.V | 4e9ad44 | 2011-05-07 21:25:51 +0530 | [diff] [blame] | 2106 | if (!err) { |
| 2107 | err = offset; |
| 2108 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2109 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2110 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2111 | pdu_complete(pdu, err); |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2114 | static void coroutine_fn v9fs_clunk(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2115 | { |
Aneesh Kumar K.V | c540ee5 | 2011-05-07 20:36:38 +0530 | [diff] [blame] | 2116 | int err; |
Anthony Liguori | bbd5697 | 2010-04-29 17:44:57 +0530 | [diff] [blame] | 2117 | int32_t fid; |
| 2118 | size_t offset = 7; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2119 | V9fsFidState *fidp; |
Aneesh Kumar K.V | c540ee5 | 2011-05-07 20:36:38 +0530 | [diff] [blame] | 2120 | V9fsPDU *pdu = opaque; |
| 2121 | V9fsState *s = pdu->s; |
Anthony Liguori | bbd5697 | 2010-04-29 17:44:57 +0530 | [diff] [blame] | 2122 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2123 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
| 2124 | if (err < 0) { |
| 2125 | goto out_nofid; |
| 2126 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2127 | trace_v9fs_clunk(pdu->tag, pdu->id, fid); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2128 | |
Aneesh Kumar K.V | ce421a1 | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 2129 | fidp = clunk_fid(s, fid); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2130 | if (fidp == NULL) { |
| 2131 | err = -ENOENT; |
| 2132 | goto out_nofid; |
| 2133 | } |
Aneesh Kumar K.V | ce421a1 | 2011-08-02 11:36:24 +0530 | [diff] [blame] | 2134 | /* |
| 2135 | * Bump the ref so that put_fid will |
| 2136 | * free the fid. |
| 2137 | */ |
| 2138 | fidp->ref++; |
Aneesh Kumar K.V | a911a18 | 2013-02-05 11:27:46 +0530 | [diff] [blame] | 2139 | err = put_fid(pdu, fidp); |
| 2140 | if (!err) { |
| 2141 | err = offset; |
| 2142 | } |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2143 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2144 | pdu_complete(pdu, err); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2145 | } |
| 2146 | |
Stefano Stabellini | bcb8998 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2147 | /* |
| 2148 | * Create a QEMUIOVector for a sub-region of PDU iovecs |
| 2149 | * |
| 2150 | * @qiov: uninitialized QEMUIOVector |
| 2151 | * @skip: number of bytes to skip from beginning of PDU |
| 2152 | * @size: number of bytes to include |
| 2153 | * @is_write: true - write, false - read |
| 2154 | * |
| 2155 | * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up |
| 2156 | * with qemu_iovec_destroy(). |
| 2157 | */ |
| 2158 | static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2159 | size_t skip, size_t size, |
Stefano Stabellini | bcb8998 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2160 | bool is_write) |
| 2161 | { |
| 2162 | QEMUIOVector elem; |
| 2163 | struct iovec *iov; |
| 2164 | unsigned int niov; |
| 2165 | |
Stefano Stabellini | 88da0b0 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2166 | if (is_write) { |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2167 | pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip); |
Stefano Stabellini | 88da0b0 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2168 | } else { |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2169 | pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip); |
Stefano Stabellini | 88da0b0 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2170 | } |
Stefano Stabellini | bcb8998 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2171 | |
| 2172 | qemu_iovec_init_external(&elem, iov, niov); |
| 2173 | qemu_iovec_init(qiov, niov); |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2174 | qemu_iovec_concat(qiov, &elem, skip, size); |
Stefano Stabellini | bcb8998 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Aneesh Kumar K.V | 2f008a8 | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 2177 | static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, |
| 2178 | uint64_t off, uint32_t max_count) |
Anthony Liguori | a923155 | 2010-04-29 17:44:56 +0530 | [diff] [blame] | 2179 | { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2180 | ssize_t err; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2181 | size_t offset = 7; |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2182 | uint64_t read_count; |
Stefano Stabellini | bcb8998 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2183 | QEMUIOVector qiov_full; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 2184 | |
Li Qiang | 7e55d65 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 2185 | if (fidp->fs.xattr.len < off) { |
| 2186 | read_count = 0; |
Greg Kurz | 16724a1 | 2020-01-20 15:11:39 +0100 | [diff] [blame] | 2187 | } else { |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2188 | read_count = fidp->fs.xattr.len - off; |
| 2189 | } |
| 2190 | if (read_count > max_count) { |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2191 | read_count = max_count; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 2192 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2193 | err = pdu_marshal(pdu, offset, "d", read_count); |
| 2194 | if (err < 0) { |
| 2195 | return err; |
| 2196 | } |
| 2197 | offset += err; |
Wei Liu | 00588a0 | 2016-01-11 09:29:37 +0000 | [diff] [blame] | 2198 | |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2199 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false); |
Greg Kurz | fa0eb5c | 2017-01-25 00:23:49 +0100 | [diff] [blame] | 2200 | err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0, |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2201 | ((char *)fidp->fs.xattr.value) + off, |
| 2202 | read_count); |
Stefano Stabellini | bcb8998 | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 2203 | qemu_iovec_destroy(&qiov_full); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2204 | if (err < 0) { |
| 2205 | return err; |
| 2206 | } |
| 2207 | offset += err; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2208 | return offset; |
| 2209 | } |
| 2210 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2211 | static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, |
| 2212 | V9fsFidState *fidp, |
| 2213 | uint32_t max_count) |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2214 | { |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2215 | V9fsPath path; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2216 | V9fsStat v9stat; |
| 2217 | int len, err = 0; |
| 2218 | int32_t count = 0; |
| 2219 | struct stat stbuf; |
| 2220 | off_t saved_dir_pos; |
Greg Kurz | 635324e | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2221 | struct dirent *dent; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2222 | |
| 2223 | /* save the directory position */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2224 | saved_dir_pos = v9fs_co_telldir(pdu, fidp); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2225 | if (saved_dir_pos < 0) { |
| 2226 | return saved_dir_pos; |
| 2227 | } |
Harsh Prateek Bora | 5f524c1 | 2011-05-18 17:23:00 +0530 | [diff] [blame] | 2228 | |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2229 | while (1) { |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2230 | v9fs_path_init(&path); |
Greg Kurz | 7cde47d | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2231 | |
| 2232 | v9fs_readdir_lock(&fidp->fs.dir); |
| 2233 | |
Greg Kurz | 635324e | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2234 | err = v9fs_co_readdir(pdu, fidp, &dent); |
| 2235 | if (err || !dent) { |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2236 | break; |
| 2237 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2238 | err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2239 | if (err < 0) { |
Greg Kurz | 8762a46 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2240 | break; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2241 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2242 | err = v9fs_co_lstat(pdu, &path, &stbuf); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2243 | if (err < 0) { |
Greg Kurz | 8762a46 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2244 | break; |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2245 | } |
Jan Dakinevich | 6069537 | 2017-09-20 08:48:51 +0200 | [diff] [blame] | 2246 | err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2247 | if (err < 0) { |
Greg Kurz | 8762a46 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2248 | break; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2249 | } |
Jan Dakinevich | 772a736 | 2017-09-20 08:48:52 +0200 | [diff] [blame] | 2250 | if ((count + v9stat.size + 2) > max_count) { |
| 2251 | v9fs_readdir_unlock(&fidp->fs.dir); |
Greg Kurz | 7cde47d | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2252 | |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2253 | /* Ran out of buffer. Set dir back to old position and return */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2254 | v9fs_co_seekdir(pdu, fidp, saved_dir_pos); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2255 | v9fs_stat_free(&v9stat); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2256 | v9fs_path_free(&path); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2257 | return count; |
| 2258 | } |
Jan Dakinevich | 772a736 | 2017-09-20 08:48:52 +0200 | [diff] [blame] | 2259 | |
| 2260 | /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ |
| 2261 | len = pdu_marshal(pdu, 11 + count, "S", &v9stat); |
| 2262 | |
| 2263 | v9fs_readdir_unlock(&fidp->fs.dir); |
| 2264 | |
| 2265 | if (len < 0) { |
| 2266 | v9fs_co_seekdir(pdu, fidp, saved_dir_pos); |
| 2267 | v9fs_stat_free(&v9stat); |
| 2268 | v9fs_path_free(&path); |
| 2269 | return len; |
| 2270 | } |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2271 | count += len; |
| 2272 | v9fs_stat_free(&v9stat); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2273 | v9fs_path_free(&path); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2274 | saved_dir_pos = dent->d_off; |
| 2275 | } |
Greg Kurz | 8762a46 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2276 | |
Greg Kurz | 7cde47d | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2277 | v9fs_readdir_unlock(&fidp->fs.dir); |
| 2278 | |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2279 | v9fs_path_free(&path); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2280 | if (err < 0) { |
| 2281 | return err; |
| 2282 | } |
| 2283 | return count; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 2284 | } |
| 2285 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2286 | static void coroutine_fn v9fs_read(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2287 | { |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2288 | int32_t fid; |
Aneesh Kumar K.V | 2f008a8 | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 2289 | uint64_t off; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2290 | ssize_t err = 0; |
| 2291 | int32_t count = 0; |
| 2292 | size_t offset = 7; |
Aneesh Kumar K.V | 2f008a8 | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 2293 | uint32_t max_count; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2294 | V9fsFidState *fidp; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 2295 | V9fsPDU *pdu = opaque; |
| 2296 | V9fsState *s = pdu->s; |
Anthony Liguori | a923155 | 2010-04-29 17:44:56 +0530 | [diff] [blame] | 2297 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2298 | err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); |
| 2299 | if (err < 0) { |
| 2300 | goto out_nofid; |
| 2301 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2302 | trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2303 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2304 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2305 | if (fidp == NULL) { |
Anthony Liguori | a923155 | 2010-04-29 17:44:56 +0530 | [diff] [blame] | 2306 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2307 | goto out_nofid; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2308 | } |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2309 | if (fidp->fid_type == P9_FID_DIR) { |
Christian Schoenebeck | d2c5cf7 | 2020-07-29 10:39:12 +0200 | [diff] [blame] | 2310 | if (s->proto_version != V9FS_PROTO_2000U) { |
| 2311 | warn_report_once( |
| 2312 | "9p: bad client: T_read request on directory only expected " |
| 2313 | "with 9P2000.u protocol version" |
| 2314 | ); |
| 2315 | err = -EOPNOTSUPP; |
| 2316 | goto out; |
| 2317 | } |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2318 | if (off == 0) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2319 | v9fs_co_rewinddir(pdu, fidp); |
Anthony Liguori | a923155 | 2010-04-29 17:44:56 +0530 | [diff] [blame] | 2320 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2321 | count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2322 | if (count < 0) { |
| 2323 | err = count; |
| 2324 | goto out; |
| 2325 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2326 | err = pdu_marshal(pdu, offset, "d", count); |
| 2327 | if (err < 0) { |
| 2328 | goto out; |
| 2329 | } |
| 2330 | err += offset + count; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2331 | } else if (fidp->fid_type == P9_FID_FILE) { |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2332 | QEMUIOVector qiov_full; |
| 2333 | QEMUIOVector qiov; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2334 | int32_t len; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2335 | |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2336 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2337 | qemu_iovec_init(&qiov, qiov_full.niov); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2338 | do { |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2339 | qemu_iovec_reset(&qiov); |
Michael Tokarev | 1b093c4 | 2012-03-12 21:28:06 +0400 | [diff] [blame] | 2340 | qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2341 | if (0) { |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2342 | print_sg(qiov.iov, qiov.niov); |
Sanchit Garg | 56d15a5 | 2010-10-08 11:30:16 +0530 | [diff] [blame] | 2343 | } |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2344 | /* Loop in case of EINTR */ |
| 2345 | do { |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2346 | len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2347 | if (len >= 0) { |
| 2348 | off += len; |
| 2349 | count += len; |
| 2350 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2351 | } while (len == -EINTR && !pdu->cancelled); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2352 | if (len < 0) { |
| 2353 | /* IO error return the error */ |
| 2354 | err = len; |
Li Qiang | e95c9a4 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2355 | goto out_free_iovec; |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2356 | } |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2357 | } while (count < max_count && len > 0); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2358 | err = pdu_marshal(pdu, offset, "d", count); |
| 2359 | if (err < 0) { |
Li Qiang | e95c9a4 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2360 | goto out_free_iovec; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2361 | } |
| 2362 | err += offset + count; |
Li Qiang | e95c9a4 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2363 | out_free_iovec: |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2364 | qemu_iovec_destroy(&qiov); |
| 2365 | qemu_iovec_destroy(&qiov_full); |
Aneesh Kumar K.V | d208a0e | 2011-05-20 13:46:31 -0700 | [diff] [blame] | 2366 | } else if (fidp->fid_type == P9_FID_XATTR) { |
| 2367 | err = v9fs_xattr_read(s, pdu, fidp, off, max_count); |
Anthony Liguori | a923155 | 2010-04-29 17:44:56 +0530 | [diff] [blame] | 2368 | } else { |
| 2369 | err = -EINVAL; |
| 2370 | } |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 2371 | trace_v9fs_read_return(pdu->tag, pdu->id, count, err); |
Anthony Liguori | a923155 | 2010-04-29 17:44:56 +0530 | [diff] [blame] | 2372 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2373 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2374 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2375 | pdu_complete(pdu, err); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2376 | } |
| 2377 | |
Christian Schoenebeck | 29c9d2c | 2020-07-29 10:11:15 +0200 | [diff] [blame] | 2378 | /** |
| 2379 | * Returns size required in Rreaddir response for the passed dirent @p name. |
| 2380 | * |
| 2381 | * @param name - directory entry's name (i.e. file name, directory name) |
| 2382 | * @returns required size in bytes |
| 2383 | */ |
| 2384 | size_t v9fs_readdir_response_size(V9fsString *name) |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2385 | { |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2386 | /* |
| 2387 | * Size of each dirent on the wire: size of qid (13) + size of offset (8) |
| 2388 | * size of type (1) + size of name.size (2) + strlen(name.data) |
| 2389 | */ |
| 2390 | return 24 + v9fs_string_size(name); |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2391 | } |
| 2392 | |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2393 | static void v9fs_free_dirents(struct V9fsDirEnt *e) |
| 2394 | { |
| 2395 | struct V9fsDirEnt *next = NULL; |
| 2396 | |
| 2397 | for (; e; e = next) { |
| 2398 | next = e->next; |
| 2399 | g_free(e->dent); |
| 2400 | g_free(e->st); |
| 2401 | g_free(e); |
| 2402 | } |
| 2403 | } |
| 2404 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2405 | static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2406 | off_t offset, int32_t max_count) |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2407 | { |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2408 | size_t size; |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2409 | V9fsQID qid; |
| 2410 | V9fsString name; |
| 2411 | int len, err = 0; |
| 2412 | int32_t count = 0; |
Greg Kurz | 635324e | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2413 | struct dirent *dent; |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2414 | struct stat *st; |
| 2415 | struct V9fsDirEnt *entries = NULL; |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2416 | |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2417 | /* |
| 2418 | * inode remapping requires the device id, which in turn might be |
| 2419 | * different for different directory entries, so if inode remapping is |
| 2420 | * enabled we have to make a full stat for each directory entry |
| 2421 | */ |
| 2422 | const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES; |
| 2423 | |
| 2424 | /* |
| 2425 | * Fetch all required directory entries altogether on a background IO |
| 2426 | * thread from fs driver. We don't want to do that for each entry |
| 2427 | * individually, because hopping between threads (this main IO thread |
| 2428 | * and background IO driver thread) would sum up to huge latencies. |
| 2429 | */ |
| 2430 | count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count, |
| 2431 | dostat); |
| 2432 | if (count < 0) { |
| 2433 | err = count; |
| 2434 | count = 0; |
| 2435 | goto out; |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2436 | } |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2437 | count = 0; |
Harsh Prateek Bora | 5f524c1 | 2011-05-18 17:23:00 +0530 | [diff] [blame] | 2438 | |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2439 | for (struct V9fsDirEnt *e = entries; e; e = e->next) { |
| 2440 | dent = e->dent; |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 2441 | |
| 2442 | if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2443 | st = e->st; |
| 2444 | /* e->st should never be NULL, but just to be sure */ |
| 2445 | if (!st) { |
| 2446 | err = -1; |
| 2447 | break; |
| 2448 | } |
| 2449 | |
| 2450 | /* remap inode */ |
| 2451 | err = stat_to_qid(pdu, st, &qid); |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 2452 | if (err < 0) { |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2453 | break; |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 2454 | } |
| 2455 | } else { |
| 2456 | /* |
| 2457 | * Fill up just the path field of qid because the client uses |
| 2458 | * only that. To fill the entire qid structure we will have |
| 2459 | * to stat each dirent found, which is expensive. For the |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2460 | * latter reason we don't call stat_to_qid() here. Only drawback |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 2461 | * is that no multi-device export detection of stat_to_qid() |
| 2462 | * would be done and provided as error to the user here. But |
| 2463 | * user would get that error anyway when accessing those |
| 2464 | * files/dirs through other ways. |
| 2465 | */ |
| 2466 | size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); |
| 2467 | memcpy(&qid.path, &dent->d_ino, size); |
| 2468 | /* Fill the other fields with dummy values */ |
| 2469 | qid.type = 0; |
| 2470 | qid.version = 0; |
| 2471 | } |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2472 | |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2473 | v9fs_string_init(&name); |
| 2474 | v9fs_string_sprintf(&name, "%s", dent->d_name); |
| 2475 | |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2476 | /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ |
| 2477 | len = pdu_marshal(pdu, 11 + count, "Qqbs", |
| 2478 | &qid, dent->d_off, |
| 2479 | dent->d_type, &name); |
Greg Kurz | 7cde47d | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2480 | |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2481 | v9fs_string_free(&name); |
Greg Kurz | 7cde47d | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2482 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2483 | if (len < 0) { |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2484 | err = len; |
| 2485 | break; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2486 | } |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2487 | |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2488 | count += len; |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2489 | } |
Greg Kurz | 7cde47d | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2490 | |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2491 | out: |
| 2492 | v9fs_free_dirents(entries); |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2493 | if (err < 0) { |
| 2494 | return err; |
| 2495 | } |
| 2496 | return count; |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2497 | } |
| 2498 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2499 | static void coroutine_fn v9fs_readdir(void *opaque) |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2500 | { |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2501 | int32_t fid; |
| 2502 | V9fsFidState *fidp; |
| 2503 | ssize_t retval = 0; |
| 2504 | size_t offset = 7; |
Aneesh Kumar K.V | 2f008a8 | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 2505 | uint64_t initial_offset; |
| 2506 | int32_t count; |
| 2507 | uint32_t max_count; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 2508 | V9fsPDU *pdu = opaque; |
Christian Schoenebeck | d36a5c2 | 2020-02-08 09:24:19 +0100 | [diff] [blame] | 2509 | V9fsState *s = pdu->s; |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2510 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2511 | retval = pdu_unmarshal(pdu, offset, "dqd", &fid, |
| 2512 | &initial_offset, &max_count); |
| 2513 | if (retval < 0) { |
| 2514 | goto out_nofid; |
| 2515 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2516 | trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); |
| 2517 | |
Christian Schoenebeck | d36a5c2 | 2020-02-08 09:24:19 +0100 | [diff] [blame] | 2518 | /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */ |
| 2519 | if (max_count > s->msize - 11) { |
| 2520 | max_count = s->msize - 11; |
| 2521 | warn_report_once( |
| 2522 | "9p: bad client: T_readdir with count > msize - 11" |
| 2523 | ); |
| 2524 | } |
| 2525 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2526 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2527 | if (fidp == NULL) { |
| 2528 | retval = -EINVAL; |
| 2529 | goto out_nofid; |
| 2530 | } |
Greg Kurz | f314ea4 | 2016-06-06 11:52:34 +0200 | [diff] [blame] | 2531 | if (!fidp->fs.dir.stream) { |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2532 | retval = -EINVAL; |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2533 | goto out; |
| 2534 | } |
Christian Schoenebeck | d2c5cf7 | 2020-07-29 10:39:12 +0200 | [diff] [blame] | 2535 | if (s->proto_version != V9FS_PROTO_2000L) { |
| 2536 | warn_report_once( |
| 2537 | "9p: bad client: T_readdir request only expected with 9P2000.L " |
| 2538 | "protocol version" |
| 2539 | ); |
| 2540 | retval = -EOPNOTSUPP; |
| 2541 | goto out; |
| 2542 | } |
Christian Schoenebeck | 0c4356b | 2020-07-29 10:13:05 +0200 | [diff] [blame] | 2543 | count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count); |
Aneesh Kumar K.V | 5e4eaa7 | 2011-05-18 15:57:17 -0700 | [diff] [blame] | 2544 | if (count < 0) { |
| 2545 | retval = count; |
| 2546 | goto out; |
| 2547 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2548 | retval = pdu_marshal(pdu, offset, "d", count); |
| 2549 | if (retval < 0) { |
| 2550 | goto out; |
| 2551 | } |
| 2552 | retval += count + offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 2553 | trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2554 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2555 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2556 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2557 | pdu_complete(pdu, retval); |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 2558 | } |
| 2559 | |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2560 | static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, |
Aneesh Kumar K.V | 2f008a8 | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 2561 | uint64_t off, uint32_t count, |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2562 | struct iovec *sg, int cnt) |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2563 | { |
| 2564 | int i, to_copy; |
| 2565 | ssize_t err = 0; |
Li Qiang | 7e55d65 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 2566 | uint64_t write_count; |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2567 | size_t offset = 7; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2568 | |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2569 | |
Li Qiang | 7e55d65 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 2570 | if (fidp->fs.xattr.len < off) { |
Daniel Henrique Barboza | b858e80 | 2020-01-20 15:11:39 +0100 | [diff] [blame] | 2571 | return -ENOSPC; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2572 | } |
Li Qiang | 7e55d65 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 2573 | write_count = fidp->fs.xattr.len - off; |
| 2574 | if (write_count > count) { |
| 2575 | write_count = count; |
| 2576 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2577 | err = pdu_marshal(pdu, offset, "d", write_count); |
| 2578 | if (err < 0) { |
| 2579 | return err; |
| 2580 | } |
| 2581 | err += offset; |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2582 | fidp->fs.xattr.copied_len += write_count; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2583 | /* |
| 2584 | * Now copy the content from sg list |
| 2585 | */ |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2586 | for (i = 0; i < cnt; i++) { |
| 2587 | if (write_count > sg[i].iov_len) { |
| 2588 | to_copy = sg[i].iov_len; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2589 | } else { |
| 2590 | to_copy = write_count; |
| 2591 | } |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2592 | memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2593 | /* updating vs->off since we are not using below */ |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2594 | off += to_copy; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2595 | write_count -= to_copy; |
| 2596 | } |
Daniel Henrique Barboza | b858e80 | 2020-01-20 15:11:39 +0100 | [diff] [blame] | 2597 | |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2598 | return err; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2599 | } |
| 2600 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2601 | static void coroutine_fn v9fs_write(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2602 | { |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2603 | ssize_t err; |
| 2604 | int32_t fid; |
Aneesh Kumar K.V | 2f008a8 | 2011-12-21 12:37:23 +0530 | [diff] [blame] | 2605 | uint64_t off; |
| 2606 | uint32_t count; |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2607 | int32_t len = 0; |
| 2608 | int32_t total = 0; |
| 2609 | size_t offset = 7; |
| 2610 | V9fsFidState *fidp; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 2611 | V9fsPDU *pdu = opaque; |
| 2612 | V9fsState *s = pdu->s; |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2613 | QEMUIOVector qiov_full; |
| 2614 | QEMUIOVector qiov; |
Anthony Liguori | 8449360 | 2010-04-29 17:44:58 +0530 | [diff] [blame] | 2615 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2616 | err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); |
| 2617 | if (err < 0) { |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2618 | pdu_complete(pdu, err); |
Stefan Weil | 0289a41 | 2015-03-08 19:17:54 +0100 | [diff] [blame] | 2619 | return; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2620 | } |
| 2621 | offset += err; |
Stefano Stabellini | cf45183 | 2020-05-21 12:26:25 -0700 | [diff] [blame] | 2622 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2623 | trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2624 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2625 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2626 | if (fidp == NULL) { |
Anthony Liguori | 8449360 | 2010-04-29 17:44:58 +0530 | [diff] [blame] | 2627 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2628 | goto out_nofid; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2629 | } |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2630 | if (fidp->fid_type == P9_FID_FILE) { |
| 2631 | if (fidp->fs.fd == -1) { |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2632 | err = -EINVAL; |
| 2633 | goto out; |
| 2634 | } |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2635 | } else if (fidp->fid_type == P9_FID_XATTR) { |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2636 | /* |
| 2637 | * setxattr operation |
| 2638 | */ |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2639 | err = v9fs_xattr_write(s, pdu, fidp, off, count, |
| 2640 | qiov_full.iov, qiov_full.niov); |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2641 | goto out; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 2642 | } else { |
Anthony Liguori | 8449360 | 2010-04-29 17:44:58 +0530 | [diff] [blame] | 2643 | err = -EINVAL; |
| 2644 | goto out; |
| 2645 | } |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2646 | qemu_iovec_init(&qiov, qiov_full.niov); |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2647 | do { |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2648 | qemu_iovec_reset(&qiov); |
Michael Tokarev | 1b093c4 | 2012-03-12 21:28:06 +0400 | [diff] [blame] | 2649 | qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2650 | if (0) { |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2651 | print_sg(qiov.iov, qiov.niov); |
Sanchit Garg | 56d15a5 | 2010-10-08 11:30:16 +0530 | [diff] [blame] | 2652 | } |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2653 | /* Loop in case of EINTR */ |
| 2654 | do { |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2655 | len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2656 | if (len >= 0) { |
| 2657 | off += len; |
| 2658 | total += len; |
| 2659 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2660 | } while (len == -EINTR && !pdu->cancelled); |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2661 | if (len < 0) { |
| 2662 | /* IO error return the error */ |
| 2663 | err = len; |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2664 | goto out_qiov; |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2665 | } |
Aneesh Kumar K.V | d7a9049 | 2011-05-08 12:29:31 +0530 | [diff] [blame] | 2666 | } while (total < count && len > 0); |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2667 | |
| 2668 | offset = 7; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2669 | err = pdu_marshal(pdu, offset, "d", total); |
| 2670 | if (err < 0) { |
Li Qiang | fdfcc9a | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2671 | goto out_qiov; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2672 | } |
| 2673 | err += offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 2674 | trace_v9fs_write_return(pdu->tag, pdu->id, total, err); |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2675 | out_qiov: |
| 2676 | qemu_iovec_destroy(&qiov); |
Anthony Liguori | 8449360 | 2010-04-29 17:44:58 +0530 | [diff] [blame] | 2677 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2678 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2679 | out_nofid: |
Stefan Hajnoczi | 302a0d3 | 2011-12-21 12:37:22 +0530 | [diff] [blame] | 2680 | qemu_iovec_destroy(&qiov_full); |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2681 | pdu_complete(pdu, err); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2682 | } |
| 2683 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2684 | static void coroutine_fn v9fs_create(void *opaque) |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 2685 | { |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2686 | int32_t fid; |
| 2687 | int err = 0; |
| 2688 | size_t offset = 7; |
| 2689 | V9fsFidState *fidp; |
| 2690 | V9fsQID qid; |
| 2691 | int32_t perm; |
| 2692 | int8_t mode; |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2693 | V9fsPath path; |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2694 | struct stat stbuf; |
| 2695 | V9fsString name; |
| 2696 | V9fsString extension; |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2697 | int iounit; |
| 2698 | V9fsPDU *pdu = opaque; |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2699 | V9fsState *s = pdu->s; |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 2700 | |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2701 | v9fs_path_init(&path); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2702 | v9fs_string_init(&name); |
| 2703 | v9fs_string_init(&extension); |
| 2704 | err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, |
| 2705 | &perm, &mode, &extension); |
| 2706 | if (err < 0) { |
| 2707 | goto out_nofid; |
| 2708 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2709 | trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); |
| 2710 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 2711 | if (name_is_illegal(name.data)) { |
| 2712 | err = -ENOENT; |
| 2713 | goto out_nofid; |
| 2714 | } |
| 2715 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 2716 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
| 2717 | err = -EEXIST; |
| 2718 | goto out_nofid; |
| 2719 | } |
| 2720 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2721 | fidp = get_fid(pdu, fid); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2722 | if (fidp == NULL) { |
| 2723 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2724 | goto out_nofid; |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2725 | } |
Li Qiang | d63fb19 | 2017-03-27 21:13:19 +0200 | [diff] [blame] | 2726 | if (fidp->fid_type != P9_FID_NONE) { |
| 2727 | err = -EINVAL; |
| 2728 | goto out; |
| 2729 | } |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2730 | if (perm & P9_STAT_MODE_DIR) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2731 | err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 2732 | fidp->uid, -1, &stbuf); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2733 | if (err < 0) { |
| 2734 | goto out; |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2735 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2736 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2737 | if (err < 0) { |
| 2738 | goto out; |
| 2739 | } |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2740 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2741 | v9fs_path_copy(&fidp->path, &path); |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2742 | v9fs_path_unlock(s); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2743 | err = v9fs_co_opendir(pdu, fidp); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2744 | if (err < 0) { |
| 2745 | goto out; |
| 2746 | } |
| 2747 | fidp->fid_type = P9_FID_DIR; |
| 2748 | } else if (perm & P9_STAT_MODE_SYMLINK) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2749 | err = v9fs_co_symlink(pdu, fidp, &name, |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 2750 | extension.data, -1 , &stbuf); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2751 | if (err < 0) { |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2752 | goto out; |
| 2753 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2754 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2755 | if (err < 0) { |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2756 | goto out; |
| 2757 | } |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2758 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2759 | v9fs_path_copy(&fidp->path, &path); |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2760 | v9fs_path_unlock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2761 | } else if (perm & P9_STAT_MODE_LINK) { |
| 2762 | int32_t ofid = atoi(extension.data); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2763 | V9fsFidState *ofidp = get_fid(pdu, ofid); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2764 | if (ofidp == NULL) { |
| 2765 | err = -EINVAL; |
| 2766 | goto out; |
| 2767 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2768 | err = v9fs_co_link(pdu, ofidp, fidp, &name); |
| 2769 | put_fid(pdu, ofidp); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2770 | if (err < 0) { |
| 2771 | goto out; |
| 2772 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2773 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2774 | if (err < 0) { |
| 2775 | fidp->fid_type = P9_FID_NONE; |
| 2776 | goto out; |
| 2777 | } |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2778 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2779 | v9fs_path_copy(&fidp->path, &path); |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2780 | v9fs_path_unlock(s); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2781 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 2782 | if (err < 0) { |
| 2783 | fidp->fid_type = P9_FID_NONE; |
| 2784 | goto out; |
| 2785 | } |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2786 | } else if (perm & P9_STAT_MODE_DEVICE) { |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2787 | char ctype; |
| 2788 | uint32_t major, minor; |
| 2789 | mode_t nmode = 0; |
| 2790 | |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2791 | if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2792 | err = -errno; |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2793 | goto out; |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | switch (ctype) { |
| 2797 | case 'c': |
| 2798 | nmode = S_IFCHR; |
| 2799 | break; |
| 2800 | case 'b': |
| 2801 | nmode = S_IFBLK; |
| 2802 | break; |
| 2803 | default: |
| 2804 | err = -EIO; |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2805 | goto out; |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2806 | } |
| 2807 | |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2808 | nmode |= perm & 0777; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2809 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 2810 | makedev(major, minor), nmode, &stbuf); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2811 | if (err < 0) { |
| 2812 | goto out; |
| 2813 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2814 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2815 | if (err < 0) { |
| 2816 | goto out; |
| 2817 | } |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2818 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2819 | v9fs_path_copy(&fidp->path, &path); |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2820 | v9fs_path_unlock(s); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2821 | } else if (perm & P9_STAT_MODE_NAMED_PIPE) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2822 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 2823 | 0, S_IFIFO | (perm & 0777), &stbuf); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2824 | if (err < 0) { |
| 2825 | goto out; |
| 2826 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2827 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2828 | if (err < 0) { |
| 2829 | goto out; |
| 2830 | } |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2831 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2832 | v9fs_path_copy(&fidp->path, &path); |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2833 | v9fs_path_unlock(s); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2834 | } else if (perm & P9_STAT_MODE_SOCKET) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2835 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 2836 | 0, S_IFSOCK | (perm & 0777), &stbuf); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2837 | if (err < 0) { |
| 2838 | goto out; |
| 2839 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2840 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2841 | if (err < 0) { |
| 2842 | goto out; |
| 2843 | } |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2844 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2845 | v9fs_path_copy(&fidp->path, &path); |
Greg Kurz | 5b3c77a | 2018-11-20 13:00:35 +0100 | [diff] [blame] | 2846 | v9fs_path_unlock(s); |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2847 | } else { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2848 | err = v9fs_co_open2(pdu, fidp, &name, -1, |
Xinhao Zhang | 0101173 | 2020-10-30 12:35:13 +0800 | [diff] [blame] | 2849 | omode_to_uflags(mode) | O_CREAT, perm, &stbuf); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2850 | if (err < 0) { |
| 2851 | goto out; |
| 2852 | } |
| 2853 | fidp->fid_type = P9_FID_FILE; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 2854 | fidp->open_flags = omode_to_uflags(mode); |
| 2855 | if (fidp->open_flags & O_EXCL) { |
| 2856 | /* |
| 2857 | * We let the host file system do O_EXCL check |
| 2858 | * We should not reclaim such fd |
| 2859 | */ |
| 2860 | fidp->flags |= FID_NON_RECLAIMABLE; |
| 2861 | } |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2862 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2863 | iounit = get_iounit(pdu, &fidp->path); |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 2864 | err = stat_to_qid(pdu, &stbuf, &qid); |
| 2865 | if (err < 0) { |
| 2866 | goto out; |
| 2867 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2868 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); |
| 2869 | if (err < 0) { |
| 2870 | goto out; |
| 2871 | } |
| 2872 | err += offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 2873 | trace_v9fs_create_return(pdu->tag, pdu->id, |
| 2874 | qid.type, qid.version, qid.path, iounit); |
Anthony Liguori | c494dd6 | 2010-04-29 17:44:59 +0530 | [diff] [blame] | 2875 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2876 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2877 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2878 | pdu_complete(pdu, err); |
Venkateswararao Jujjuri | baaa86d | 2011-08-08 23:56:50 +0530 | [diff] [blame] | 2879 | v9fs_string_free(&name); |
| 2880 | v9fs_string_free(&extension); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 2881 | v9fs_path_free(&path); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2882 | } |
| 2883 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2884 | static void coroutine_fn v9fs_symlink(void *opaque) |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2885 | { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 2886 | V9fsPDU *pdu = opaque; |
Venkateswararao Jujjuri | 3fa2a8d | 2011-08-09 00:00:01 +0530 | [diff] [blame] | 2887 | V9fsString name; |
| 2888 | V9fsString symname; |
Venkateswararao Jujjuri | 3fa2a8d | 2011-08-09 00:00:01 +0530 | [diff] [blame] | 2889 | V9fsFidState *dfidp; |
| 2890 | V9fsQID qid; |
| 2891 | struct stat stbuf; |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2892 | int32_t dfid; |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2893 | int err = 0; |
| 2894 | gid_t gid; |
Venkateswararao Jujjuri | 3fa2a8d | 2011-08-09 00:00:01 +0530 | [diff] [blame] | 2895 | size_t offset = 7; |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2896 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2897 | v9fs_string_init(&name); |
| 2898 | v9fs_string_init(&symname); |
| 2899 | err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); |
| 2900 | if (err < 0) { |
| 2901 | goto out_nofid; |
| 2902 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2903 | trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2904 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 2905 | if (name_is_illegal(name.data)) { |
| 2906 | err = -ENOENT; |
| 2907 | goto out_nofid; |
| 2908 | } |
| 2909 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 2910 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
| 2911 | err = -EEXIST; |
| 2912 | goto out_nofid; |
| 2913 | } |
| 2914 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2915 | dfidp = get_fid(pdu, dfid); |
Venkateswararao Jujjuri | 3fa2a8d | 2011-08-09 00:00:01 +0530 | [diff] [blame] | 2916 | if (dfidp == NULL) { |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2917 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2918 | goto out_nofid; |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2919 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2920 | err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); |
Venkateswararao Jujjuri | 3fa2a8d | 2011-08-09 00:00:01 +0530 | [diff] [blame] | 2921 | if (err < 0) { |
| 2922 | goto out; |
| 2923 | } |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 2924 | err = stat_to_qid(pdu, &stbuf, &qid); |
| 2925 | if (err < 0) { |
| 2926 | goto out; |
| 2927 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2928 | err = pdu_marshal(pdu, offset, "Q", &qid); |
| 2929 | if (err < 0) { |
| 2930 | goto out; |
| 2931 | } |
| 2932 | err += offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 2933 | trace_v9fs_symlink_return(pdu->tag, pdu->id, |
| 2934 | qid.type, qid.version, qid.path); |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2935 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2936 | put_fid(pdu, dfidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 2937 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2938 | pdu_complete(pdu, err); |
Venkateswararao Jujjuri | 3fa2a8d | 2011-08-09 00:00:01 +0530 | [diff] [blame] | 2939 | v9fs_string_free(&name); |
| 2940 | v9fs_string_free(&symname); |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
Greg Kurz | a1bf8b7 | 2016-11-25 12:54:21 +0100 | [diff] [blame] | 2943 | static void coroutine_fn v9fs_flush(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2944 | { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2945 | ssize_t err; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2946 | int16_t tag; |
| 2947 | size_t offset = 7; |
Greg Kurz | d5f2af7 | 2017-03-21 09:12:47 +0100 | [diff] [blame] | 2948 | V9fsPDU *cancel_pdu = NULL; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 2949 | V9fsPDU *pdu = opaque; |
| 2950 | V9fsState *s = pdu->s; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2951 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2952 | err = pdu_unmarshal(pdu, offset, "w", &tag); |
| 2953 | if (err < 0) { |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2954 | pdu_complete(pdu, err); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2955 | return; |
| 2956 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2957 | trace_v9fs_flush(pdu->tag, pdu->id, tag); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2958 | |
Greg Kurz | d5f2af7 | 2017-03-21 09:12:47 +0100 | [diff] [blame] | 2959 | if (pdu->tag == tag) { |
Alistair Francis | 3dc6f86 | 2017-07-12 06:57:41 -0700 | [diff] [blame] | 2960 | warn_report("the guest sent a self-referencing 9P flush request"); |
Greg Kurz | d5f2af7 | 2017-03-21 09:12:47 +0100 | [diff] [blame] | 2961 | } else { |
| 2962 | QLIST_FOREACH(cancel_pdu, &s->active_list, next) { |
| 2963 | if (cancel_pdu->tag == tag) { |
| 2964 | break; |
| 2965 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2966 | } |
| 2967 | } |
| 2968 | if (cancel_pdu) { |
| 2969 | cancel_pdu->cancelled = 1; |
| 2970 | /* |
| 2971 | * Wait for pdu to complete. |
| 2972 | */ |
Paolo Bonzini | 1ace7ce | 2017-02-13 19:12:43 +0100 | [diff] [blame] | 2973 | qemu_co_queue_wait(&cancel_pdu->complete, NULL); |
Greg Kurz | 18adde8 | 2017-04-04 18:06:01 +0200 | [diff] [blame] | 2974 | if (!qemu_co_queue_next(&cancel_pdu->complete)) { |
| 2975 | cancel_pdu->cancelled = 0; |
| 2976 | pdu_free(cancel_pdu); |
| 2977 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 2978 | } |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 2979 | pdu_complete(pdu, 7); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 2980 | } |
| 2981 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 2982 | static void coroutine_fn v9fs_link(void *opaque) |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 2983 | { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 2984 | V9fsPDU *pdu = opaque; |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 2985 | int32_t dfid, oldfid; |
| 2986 | V9fsFidState *dfidp, *oldfidp; |
Dong Xu Wang | 3a93113 | 2011-11-29 16:52:38 +0800 | [diff] [blame] | 2987 | V9fsString name; |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 2988 | size_t offset = 7; |
| 2989 | int err = 0; |
| 2990 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 2991 | v9fs_string_init(&name); |
| 2992 | err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); |
| 2993 | if (err < 0) { |
| 2994 | goto out_nofid; |
| 2995 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 2996 | trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 2997 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 2998 | if (name_is_illegal(name.data)) { |
| 2999 | err = -ENOENT; |
| 3000 | goto out_nofid; |
| 3001 | } |
| 3002 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 3003 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
| 3004 | err = -EEXIST; |
| 3005 | goto out_nofid; |
| 3006 | } |
| 3007 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3008 | dfidp = get_fid(pdu, dfid); |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3009 | if (dfidp == NULL) { |
Venkateswararao Jujjuri (JV) | ffd6687 | 2011-05-09 11:47:28 -0700 | [diff] [blame] | 3010 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3011 | goto out_nofid; |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3012 | } |
| 3013 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3014 | oldfidp = get_fid(pdu, oldfid); |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3015 | if (oldfidp == NULL) { |
Venkateswararao Jujjuri (JV) | ffd6687 | 2011-05-09 11:47:28 -0700 | [diff] [blame] | 3016 | err = -ENOENT; |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3017 | goto out; |
| 3018 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3019 | err = v9fs_co_link(pdu, oldfidp, dfidp, &name); |
Venkateswararao Jujjuri (JV) | ffd6687 | 2011-05-09 11:47:28 -0700 | [diff] [blame] | 3020 | if (!err) { |
| 3021 | err = offset; |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3022 | } |
Li Qiang | 4c15867 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3023 | put_fid(pdu, oldfidp); |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3024 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3025 | put_fid(pdu, dfidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3026 | out_nofid: |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3027 | v9fs_string_free(&name); |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3028 | pdu_complete(pdu, err); |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3031 | /* Only works with path name based fid */ |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3032 | static void coroutine_fn v9fs_remove(void *opaque) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 3033 | { |
Anthony Liguori | 5bae190 | 2010-04-29 17:45:01 +0530 | [diff] [blame] | 3034 | int32_t fid; |
Anthony Liguori | 5bae190 | 2010-04-29 17:45:01 +0530 | [diff] [blame] | 3035 | int err = 0; |
Venkateswararao Jujjuri | ae1ef57 | 2011-08-08 23:50:20 +0530 | [diff] [blame] | 3036 | size_t offset = 7; |
| 3037 | V9fsFidState *fidp; |
| 3038 | V9fsPDU *pdu = opaque; |
Anthony Liguori | 5bae190 | 2010-04-29 17:45:01 +0530 | [diff] [blame] | 3039 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3040 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
| 3041 | if (err < 0) { |
| 3042 | goto out_nofid; |
| 3043 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3044 | trace_v9fs_remove(pdu->tag, pdu->id, fid); |
Anthony Liguori | 5bae190 | 2010-04-29 17:45:01 +0530 | [diff] [blame] | 3045 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3046 | fidp = get_fid(pdu, fid); |
Venkateswararao Jujjuri | ae1ef57 | 2011-08-08 23:50:20 +0530 | [diff] [blame] | 3047 | if (fidp == NULL) { |
Anthony Liguori | 5bae190 | 2010-04-29 17:45:01 +0530 | [diff] [blame] | 3048 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3049 | goto out_nofid; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 3050 | } |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3051 | /* if fs driver is not path based, return EOPNOTSUPP */ |
Aneesh Kumar K.V | c98f1d4 | 2011-10-12 20:59:18 +0530 | [diff] [blame] | 3052 | if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3053 | err = -EOPNOTSUPP; |
| 3054 | goto out_err; |
| 3055 | } |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 3056 | /* |
| 3057 | * IF the file is unlinked, we cannot reopen |
| 3058 | * the file later. So don't reclaim fd |
| 3059 | */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3060 | err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 3061 | if (err < 0) { |
| 3062 | goto out_err; |
| 3063 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3064 | err = v9fs_co_remove(pdu, &fidp->path); |
Venkateswararao Jujjuri | ae1ef57 | 2011-08-08 23:50:20 +0530 | [diff] [blame] | 3065 | if (!err) { |
| 3066 | err = offset; |
| 3067 | } |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 3068 | out_err: |
Venkateswararao Jujjuri | ae1ef57 | 2011-08-08 23:50:20 +0530 | [diff] [blame] | 3069 | /* For TREMOVE we need to clunk the fid even on failed remove */ |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3070 | clunk_fid(pdu->s, fidp->fid); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3071 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3072 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3073 | pdu_complete(pdu, err); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 3074 | } |
| 3075 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3076 | static void coroutine_fn v9fs_unlinkat(void *opaque) |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3077 | { |
| 3078 | int err = 0; |
| 3079 | V9fsString name; |
Keno Fischer | 67e8734 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3080 | int32_t dfid, flags, rflags = 0; |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3081 | size_t offset = 7; |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3082 | V9fsPath path; |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3083 | V9fsFidState *dfidp; |
| 3084 | V9fsPDU *pdu = opaque; |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3085 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3086 | v9fs_string_init(&name); |
| 3087 | err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); |
| 3088 | if (err < 0) { |
| 3089 | goto out_nofid; |
| 3090 | } |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 3091 | |
| 3092 | if (name_is_illegal(name.data)) { |
| 3093 | err = -ENOENT; |
| 3094 | goto out_nofid; |
| 3095 | } |
| 3096 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 3097 | if (!strcmp(".", name.data)) { |
| 3098 | err = -EINVAL; |
| 3099 | goto out_nofid; |
| 3100 | } |
| 3101 | |
| 3102 | if (!strcmp("..", name.data)) { |
| 3103 | err = -ENOTEMPTY; |
| 3104 | goto out_nofid; |
| 3105 | } |
| 3106 | |
Keno Fischer | 67e8734 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3107 | if (flags & ~P9_DOTL_AT_REMOVEDIR) { |
| 3108 | err = -EINVAL; |
| 3109 | goto out_nofid; |
| 3110 | } |
| 3111 | |
| 3112 | if (flags & P9_DOTL_AT_REMOVEDIR) { |
| 3113 | rflags |= AT_REMOVEDIR; |
| 3114 | } |
| 3115 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3116 | dfidp = get_fid(pdu, dfid); |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3117 | if (dfidp == NULL) { |
| 3118 | err = -EINVAL; |
| 3119 | goto out_nofid; |
| 3120 | } |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3121 | /* |
| 3122 | * IF the file is unlinked, we cannot reopen |
| 3123 | * the file later. So don't reclaim fd |
| 3124 | */ |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3125 | v9fs_path_init(&path); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3126 | err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3127 | if (err < 0) { |
| 3128 | goto out_err; |
| 3129 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3130 | err = v9fs_mark_fids_unreclaim(pdu, &path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3131 | if (err < 0) { |
| 3132 | goto out_err; |
| 3133 | } |
Keno Fischer | 67e8734 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3134 | err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags); |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3135 | if (!err) { |
| 3136 | err = offset; |
| 3137 | } |
| 3138 | out_err: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3139 | put_fid(pdu, dfidp); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3140 | v9fs_path_free(&path); |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3141 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3142 | pdu_complete(pdu, err); |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 3143 | v9fs_string_free(&name); |
| 3144 | } |
| 3145 | |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3146 | |
| 3147 | /* Only works with path name based fid */ |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3148 | static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, |
| 3149 | int32_t newdirfid, |
| 3150 | V9fsString *name) |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3151 | { |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3152 | int err = 0; |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3153 | V9fsPath new_path; |
| 3154 | V9fsFidState *tfidp; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3155 | V9fsState *s = pdu->s; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3156 | V9fsFidState *dirfidp = NULL; |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3157 | |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3158 | v9fs_path_init(&new_path); |
Aneesh Kumar K.V | 930b1e1 | 2011-05-07 16:01:33 +0530 | [diff] [blame] | 3159 | if (newdirfid != -1) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3160 | dirfidp = get_fid(pdu, newdirfid); |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3161 | if (dirfidp == NULL) { |
Daniel Henrique Barboza | b858e80 | 2020-01-20 15:11:39 +0100 | [diff] [blame] | 3162 | return -ENOENT; |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3163 | } |
Greg Kurz | 49dd946 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3164 | if (fidp->fid_type != P9_FID_NONE) { |
| 3165 | err = -EINVAL; |
| 3166 | goto out; |
| 3167 | } |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3168 | err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); |
| 3169 | if (err < 0) { |
| 3170 | goto out; |
| 3171 | } |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3172 | } else { |
Jan Dakinevich | 4d8bc73 | 2017-09-20 08:48:52 +0200 | [diff] [blame] | 3173 | char *dir_name = g_path_get_dirname(fidp->path.data); |
| 3174 | V9fsPath dir_path; |
| 3175 | |
| 3176 | v9fs_path_init(&dir_path); |
| 3177 | v9fs_path_sprintf(&dir_path, "%s", dir_name); |
| 3178 | g_free(dir_name); |
| 3179 | |
| 3180 | err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path); |
| 3181 | v9fs_path_free(&dir_path); |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3182 | if (err < 0) { |
| 3183 | goto out; |
| 3184 | } |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3185 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3186 | err = v9fs_co_rename(pdu, &fidp->path, &new_path); |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3187 | if (err < 0) { |
| 3188 | goto out; |
| 3189 | } |
| 3190 | /* |
| 3191 | * Fixup fid's pointing to the old name to |
| 3192 | * start pointing to the new name |
| 3193 | */ |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 3194 | QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3195 | if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { |
| 3196 | /* replace the name */ |
| 3197 | v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3198 | } |
| 3199 | } |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3200 | out: |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3201 | if (dirfidp) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3202 | put_fid(pdu, dirfidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3203 | } |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3204 | v9fs_path_free(&new_path); |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3205 | return err; |
| 3206 | } |
| 3207 | |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3208 | /* Only works with path name based fid */ |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3209 | static void coroutine_fn v9fs_rename(void *opaque) |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3210 | { |
Aneesh Kumar K.V | 930b1e1 | 2011-05-07 16:01:33 +0530 | [diff] [blame] | 3211 | int32_t fid; |
| 3212 | ssize_t err = 0; |
| 3213 | size_t offset = 7; |
| 3214 | V9fsString name; |
| 3215 | int32_t newdirfid; |
| 3216 | V9fsFidState *fidp; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3217 | V9fsPDU *pdu = opaque; |
| 3218 | V9fsState *s = pdu->s; |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3219 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3220 | v9fs_string_init(&name); |
| 3221 | err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); |
| 3222 | if (err < 0) { |
| 3223 | goto out_nofid; |
| 3224 | } |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 3225 | |
| 3226 | if (name_is_illegal(name.data)) { |
| 3227 | err = -ENOENT; |
| 3228 | goto out_nofid; |
| 3229 | } |
| 3230 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 3231 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
| 3232 | err = -EISDIR; |
| 3233 | goto out_nofid; |
| 3234 | } |
| 3235 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3236 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 930b1e1 | 2011-05-07 16:01:33 +0530 | [diff] [blame] | 3237 | if (fidp == NULL) { |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3238 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3239 | goto out_nofid; |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3240 | } |
Greg Kurz | 49dd946 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3241 | if (fidp->fid_type != P9_FID_NONE) { |
| 3242 | err = -EINVAL; |
| 3243 | goto out; |
| 3244 | } |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3245 | /* if fs driver is not path based, return EOPNOTSUPP */ |
Aneesh Kumar K.V | c98f1d4 | 2011-10-12 20:59:18 +0530 | [diff] [blame] | 3246 | if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3247 | err = -EOPNOTSUPP; |
| 3248 | goto out; |
| 3249 | } |
| 3250 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3251 | err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3252 | v9fs_path_unlock(s); |
Aneesh Kumar K.V | 930b1e1 | 2011-05-07 16:01:33 +0530 | [diff] [blame] | 3253 | if (!err) { |
| 3254 | err = offset; |
| 3255 | } |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3256 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3257 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3258 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3259 | pdu_complete(pdu, err); |
Aneesh Kumar K.V | 930b1e1 | 2011-05-07 16:01:33 +0530 | [diff] [blame] | 3260 | v9fs_string_free(&name); |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 3261 | } |
| 3262 | |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3263 | static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, |
| 3264 | V9fsString *old_name, |
| 3265 | V9fsPath *newdir, |
| 3266 | V9fsString *new_name) |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3267 | { |
| 3268 | V9fsFidState *tfidp; |
| 3269 | V9fsPath oldpath, newpath; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3270 | V9fsState *s = pdu->s; |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3271 | int err; |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3272 | |
| 3273 | v9fs_path_init(&oldpath); |
| 3274 | v9fs_path_init(&newpath); |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3275 | err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); |
| 3276 | if (err < 0) { |
| 3277 | goto out; |
| 3278 | } |
| 3279 | err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); |
| 3280 | if (err < 0) { |
| 3281 | goto out; |
| 3282 | } |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3283 | |
| 3284 | /* |
| 3285 | * Fixup fid's pointing to the old name to |
| 3286 | * start pointing to the new name |
| 3287 | */ |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 3288 | QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3289 | if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { |
| 3290 | /* replace the name */ |
| 3291 | v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); |
| 3292 | } |
| 3293 | } |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3294 | out: |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3295 | v9fs_path_free(&oldpath); |
| 3296 | v9fs_path_free(&newpath); |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3297 | return err; |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3298 | } |
| 3299 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3300 | static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, |
| 3301 | V9fsString *old_name, |
| 3302 | int32_t newdirfid, |
| 3303 | V9fsString *new_name) |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3304 | { |
| 3305 | int err = 0; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3306 | V9fsState *s = pdu->s; |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3307 | V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; |
| 3308 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3309 | olddirfidp = get_fid(pdu, olddirfid); |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3310 | if (olddirfidp == NULL) { |
| 3311 | err = -ENOENT; |
| 3312 | goto out; |
| 3313 | } |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3314 | if (newdirfid != -1) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3315 | newdirfidp = get_fid(pdu, newdirfid); |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3316 | if (newdirfidp == NULL) { |
| 3317 | err = -ENOENT; |
| 3318 | goto out; |
| 3319 | } |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3320 | } else { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3321 | newdirfidp = get_fid(pdu, olddirfid); |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3322 | } |
| 3323 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3324 | err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3325 | &newdirfidp->path, new_name); |
| 3326 | if (err < 0) { |
| 3327 | goto out; |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3328 | } |
Aneesh Kumar K.V | c98f1d4 | 2011-10-12 20:59:18 +0530 | [diff] [blame] | 3329 | if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3330 | /* Only for path based fid we need to do the below fixup */ |
Greg Kurz | 4fa6200 | 2017-05-25 10:30:14 +0200 | [diff] [blame] | 3331 | err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, |
| 3332 | &newdirfidp->path, new_name); |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3333 | } |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3334 | out: |
| 3335 | if (olddirfidp) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3336 | put_fid(pdu, olddirfidp); |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3337 | } |
| 3338 | if (newdirfidp) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3339 | put_fid(pdu, newdirfidp); |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3340 | } |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3341 | return err; |
| 3342 | } |
| 3343 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3344 | static void coroutine_fn v9fs_renameat(void *opaque) |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3345 | { |
| 3346 | ssize_t err = 0; |
| 3347 | size_t offset = 7; |
| 3348 | V9fsPDU *pdu = opaque; |
| 3349 | V9fsState *s = pdu->s; |
| 3350 | int32_t olddirfid, newdirfid; |
| 3351 | V9fsString old_name, new_name; |
| 3352 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3353 | v9fs_string_init(&old_name); |
| 3354 | v9fs_string_init(&new_name); |
| 3355 | err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, |
| 3356 | &old_name, &newdirfid, &new_name); |
| 3357 | if (err < 0) { |
| 3358 | goto out_err; |
| 3359 | } |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3360 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 3361 | if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { |
| 3362 | err = -ENOENT; |
| 3363 | goto out_err; |
| 3364 | } |
| 3365 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 3366 | if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || |
| 3367 | !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { |
| 3368 | err = -EISDIR; |
| 3369 | goto out_err; |
| 3370 | } |
| 3371 | |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3372 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3373 | err = v9fs_complete_renameat(pdu, olddirfid, |
| 3374 | &old_name, newdirfid, &new_name); |
Aneesh Kumar K.V | 532decb | 2011-08-02 11:35:54 +0530 | [diff] [blame] | 3375 | v9fs_path_unlock(s); |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3376 | if (!err) { |
| 3377 | err = offset; |
| 3378 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3379 | |
| 3380 | out_err: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3381 | pdu_complete(pdu, err); |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 3382 | v9fs_string_free(&old_name); |
| 3383 | v9fs_string_free(&new_name); |
| 3384 | } |
| 3385 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3386 | static void coroutine_fn v9fs_wstat(void *opaque) |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3387 | { |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3388 | int32_t fid; |
| 3389 | int err = 0; |
| 3390 | int16_t unused; |
| 3391 | V9fsStat v9stat; |
| 3392 | size_t offset = 7; |
| 3393 | struct stat stbuf; |
| 3394 | V9fsFidState *fidp; |
| 3395 | V9fsPDU *pdu = opaque; |
Greg Kurz | 1d20398 | 2018-11-23 13:28:03 +0100 | [diff] [blame] | 3396 | V9fsState *s = pdu->s; |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3397 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3398 | v9fs_stat_init(&v9stat); |
| 3399 | err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); |
| 3400 | if (err < 0) { |
| 3401 | goto out_nofid; |
| 3402 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3403 | trace_v9fs_wstat(pdu->tag, pdu->id, fid, |
| 3404 | v9stat.mode, v9stat.atime, v9stat.mtime); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3405 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3406 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3407 | if (fidp == NULL) { |
| 3408 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3409 | goto out_nofid; |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3410 | } |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3411 | /* do we need to sync the file? */ |
| 3412 | if (donttouch_stat(&v9stat)) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3413 | err = v9fs_co_fsync(pdu, fidp, 0); |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3414 | goto out; |
| 3415 | } |
| 3416 | if (v9stat.mode != -1) { |
| 3417 | uint32_t v9_mode; |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3418 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3419 | if (err < 0) { |
| 3420 | goto out; |
| 3421 | } |
| 3422 | v9_mode = stat_to_v9mode(&stbuf); |
| 3423 | if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != |
| 3424 | (v9_mode & P9_STAT_MODE_TYPE_BITS)) { |
| 3425 | /* Attempting to change the type */ |
| 3426 | err = -EIO; |
| 3427 | goto out; |
| 3428 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3429 | err = v9fs_co_chmod(pdu, &fidp->path, |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3430 | v9mode_to_mode(v9stat.mode, |
| 3431 | &v9stat.extension)); |
| 3432 | if (err < 0) { |
| 3433 | goto out; |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3434 | } |
| 3435 | } |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3436 | if (v9stat.mtime != -1 || v9stat.atime != -1) { |
Sripathi Kodi | 8fc39ae | 2010-06-09 19:14:38 +0530 | [diff] [blame] | 3437 | struct timespec times[2]; |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3438 | if (v9stat.atime != -1) { |
| 3439 | times[0].tv_sec = v9stat.atime; |
Sripathi Kodi | 8fc39ae | 2010-06-09 19:14:38 +0530 | [diff] [blame] | 3440 | times[0].tv_nsec = 0; |
| 3441 | } else { |
| 3442 | times[0].tv_nsec = UTIME_OMIT; |
| 3443 | } |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3444 | if (v9stat.mtime != -1) { |
| 3445 | times[1].tv_sec = v9stat.mtime; |
Sripathi Kodi | 8fc39ae | 2010-06-09 19:14:38 +0530 | [diff] [blame] | 3446 | times[1].tv_nsec = 0; |
| 3447 | } else { |
| 3448 | times[1].tv_nsec = UTIME_OMIT; |
| 3449 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3450 | err = v9fs_co_utimensat(pdu, &fidp->path, times); |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3451 | if (err < 0) { |
| 3452 | goto out; |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3453 | } |
| 3454 | } |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3455 | if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3456 | err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3457 | if (err < 0) { |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3458 | goto out; |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3459 | } |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3460 | } |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3461 | if (v9stat.name.size != 0) { |
Greg Kurz | 1d20398 | 2018-11-23 13:28:03 +0100 | [diff] [blame] | 3462 | v9fs_path_write_lock(s); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3463 | err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); |
Greg Kurz | 1d20398 | 2018-11-23 13:28:03 +0100 | [diff] [blame] | 3464 | v9fs_path_unlock(s); |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3465 | if (err < 0) { |
| 3466 | goto out; |
| 3467 | } |
| 3468 | } |
| 3469 | if (v9stat.length != -1) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3470 | err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3471 | if (err < 0) { |
| 3472 | goto out; |
| 3473 | } |
| 3474 | } |
| 3475 | err = offset; |
Anthony Liguori | 8cf89e0 | 2010-04-29 17:45:00 +0530 | [diff] [blame] | 3476 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3477 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3478 | out_nofid: |
Aneesh Kumar K.V | b81d685 | 2011-05-08 13:06:04 +0530 | [diff] [blame] | 3479 | v9fs_stat_free(&v9stat); |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3480 | pdu_complete(pdu, err); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 3481 | } |
| 3482 | |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3483 | static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3484 | { |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3485 | uint32_t f_type; |
| 3486 | uint32_t f_bsize; |
| 3487 | uint64_t f_blocks; |
| 3488 | uint64_t f_bfree; |
| 3489 | uint64_t f_bavail; |
| 3490 | uint64_t f_files; |
| 3491 | uint64_t f_ffree; |
| 3492 | uint64_t fsid_val; |
| 3493 | uint32_t f_namelen; |
| 3494 | size_t offset = 7; |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 3495 | int32_t bsize_factor; |
| 3496 | |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 3497 | /* |
| 3498 | * compute bsize factor based on host file system block size |
| 3499 | * and client msize |
| 3500 | */ |
Xinhao Zhang | 0101173 | 2020-10-30 12:35:13 +0800 | [diff] [blame] | 3501 | bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize; |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 3502 | if (!bsize_factor) { |
| 3503 | bsize_factor = 1; |
| 3504 | } |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3505 | f_type = stbuf->f_type; |
| 3506 | f_bsize = stbuf->f_bsize; |
| 3507 | f_bsize *= bsize_factor; |
M. Mohan Kumar | 5e94c10 | 2010-06-09 19:14:28 +0530 | [diff] [blame] | 3508 | /* |
| 3509 | * f_bsize is adjusted(multiplied) by bsize factor, so we need to |
| 3510 | * adjust(divide) the number of blocks, free blocks and available |
| 3511 | * blocks by bsize factor |
| 3512 | */ |
Xinhao Zhang | 0101173 | 2020-10-30 12:35:13 +0800 | [diff] [blame] | 3513 | f_blocks = stbuf->f_blocks / bsize_factor; |
| 3514 | f_bfree = stbuf->f_bfree / bsize_factor; |
| 3515 | f_bavail = stbuf->f_bavail / bsize_factor; |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3516 | f_files = stbuf->f_files; |
| 3517 | f_ffree = stbuf->f_ffree; |
| 3518 | fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | |
| 3519 | (unsigned long long)stbuf->f_fsid.__val[1] << 32; |
| 3520 | f_namelen = stbuf->f_namelen; |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3521 | |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3522 | return pdu_marshal(pdu, offset, "ddqqqqqqd", |
| 3523 | f_type, f_bsize, f_blocks, f_bfree, |
| 3524 | f_bavail, f_files, f_ffree, |
| 3525 | fsid_val, f_namelen); |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3526 | } |
| 3527 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3528 | static void coroutine_fn v9fs_statfs(void *opaque) |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3529 | { |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3530 | int32_t fid; |
| 3531 | ssize_t retval = 0; |
| 3532 | size_t offset = 7; |
| 3533 | V9fsFidState *fidp; |
| 3534 | struct statfs stbuf; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3535 | V9fsPDU *pdu = opaque; |
| 3536 | V9fsState *s = pdu->s; |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3537 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3538 | retval = pdu_unmarshal(pdu, offset, "d", &fid); |
| 3539 | if (retval < 0) { |
| 3540 | goto out_nofid; |
| 3541 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3542 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3543 | if (fidp == NULL) { |
| 3544 | retval = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3545 | goto out_nofid; |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3546 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3547 | retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); |
Aneesh Kumar K.V | 88a4763 | 2011-05-18 16:04:01 -0700 | [diff] [blame] | 3548 | if (retval < 0) { |
| 3549 | goto out; |
| 3550 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3551 | retval = v9fs_fill_statfs(s, pdu, &stbuf); |
| 3552 | if (retval < 0) { |
| 3553 | goto out; |
| 3554 | } |
| 3555 | retval += offset; |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3556 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3557 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3558 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3559 | pdu_complete(pdu, retval); |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3560 | } |
| 3561 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3562 | static void coroutine_fn v9fs_mknod(void *opaque) |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 3563 | { |
Aneesh Kumar K.V | 1b733fe | 2011-05-18 16:06:51 -0700 | [diff] [blame] | 3564 | |
| 3565 | int mode; |
| 3566 | gid_t gid; |
| 3567 | int32_t fid; |
| 3568 | V9fsQID qid; |
| 3569 | int err = 0; |
| 3570 | int major, minor; |
| 3571 | size_t offset = 7; |
| 3572 | V9fsString name; |
| 3573 | struct stat stbuf; |
Aneesh Kumar K.V | 1b733fe | 2011-05-18 16:06:51 -0700 | [diff] [blame] | 3574 | V9fsFidState *fidp; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3575 | V9fsPDU *pdu = opaque; |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 3576 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3577 | v9fs_string_init(&name); |
| 3578 | err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, |
| 3579 | &major, &minor, &gid); |
| 3580 | if (err < 0) { |
| 3581 | goto out_nofid; |
| 3582 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3583 | trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 3584 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 3585 | if (name_is_illegal(name.data)) { |
| 3586 | err = -ENOENT; |
| 3587 | goto out_nofid; |
| 3588 | } |
| 3589 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 3590 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
| 3591 | err = -EEXIST; |
| 3592 | goto out_nofid; |
| 3593 | } |
| 3594 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3595 | fidp = get_fid(pdu, fid); |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 3596 | if (fidp == NULL) { |
| 3597 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3598 | goto out_nofid; |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 3599 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3600 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 3601 | makedev(major, minor), mode, &stbuf); |
Aneesh Kumar K.V | 1b733fe | 2011-05-18 16:06:51 -0700 | [diff] [blame] | 3602 | if (err < 0) { |
| 3603 | goto out; |
| 3604 | } |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 3605 | err = stat_to_qid(pdu, &stbuf, &qid); |
| 3606 | if (err < 0) { |
| 3607 | goto out; |
| 3608 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3609 | err = pdu_marshal(pdu, offset, "Q", &qid); |
| 3610 | if (err < 0) { |
| 3611 | goto out; |
| 3612 | } |
| 3613 | err += offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 3614 | trace_v9fs_mknod_return(pdu->tag, pdu->id, |
| 3615 | qid.type, qid.version, qid.path); |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 3616 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3617 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3618 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3619 | pdu_complete(pdu, err); |
Aneesh Kumar K.V | 1b733fe | 2011-05-18 16:06:51 -0700 | [diff] [blame] | 3620 | v9fs_string_free(&name); |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 3621 | } |
| 3622 | |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3623 | /* |
| 3624 | * Implement posix byte range locking code |
| 3625 | * Server side handling of locking code is very simple, because 9p server in |
| 3626 | * QEMU can handle only one client. And most of the lock handling |
| 3627 | * (like conflict, merging) etc is done by the VFS layer itself, so no need to |
| 3628 | * do any thing in * qemu 9p server side lock code path. |
| 3629 | * So when a TLOCK request comes, always return success |
| 3630 | */ |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3631 | static void coroutine_fn v9fs_lock(void *opaque) |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3632 | { |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3633 | V9fsFlock flock; |
Aneesh Kumar K.V | 0c27bf2 | 2011-08-22 09:14:04 +0530 | [diff] [blame] | 3634 | size_t offset = 7; |
| 3635 | struct stat stbuf; |
| 3636 | V9fsFidState *fidp; |
| 3637 | int32_t fid, err = 0; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3638 | V9fsPDU *pdu = opaque; |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3639 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3640 | v9fs_string_init(&flock.client_id); |
| 3641 | err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, |
| 3642 | &flock.flags, &flock.start, &flock.length, |
| 3643 | &flock.proc_id, &flock.client_id); |
| 3644 | if (err < 0) { |
| 3645 | goto out_nofid; |
| 3646 | } |
| 3647 | trace_v9fs_lock(pdu->tag, pdu->id, fid, |
| 3648 | flock.type, flock.start, flock.length); |
| 3649 | |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3650 | |
| 3651 | /* We support only block flag now (that too ignored currently) */ |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3652 | if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3653 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3654 | goto out_nofid; |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3655 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3656 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 0c27bf2 | 2011-08-22 09:14:04 +0530 | [diff] [blame] | 3657 | if (fidp == NULL) { |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3658 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3659 | goto out_nofid; |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3660 | } |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 3661 | err = v9fs_co_fstat(pdu, fidp, &stbuf); |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3662 | if (err < 0) { |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3663 | goto out; |
| 3664 | } |
Paolo Bonzini | 4bae2b3 | 2017-02-28 10:31:46 +0100 | [diff] [blame] | 3665 | err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS); |
| 3666 | if (err < 0) { |
| 3667 | goto out; |
| 3668 | } |
| 3669 | err += offset; |
| 3670 | trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS); |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3671 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3672 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3673 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3674 | pdu_complete(pdu, err); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3675 | v9fs_string_free(&flock.client_id); |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 3676 | } |
| 3677 | |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3678 | /* |
| 3679 | * When a TGETLOCK request comes, always return success because all lock |
| 3680 | * handling is done by client's VFS layer. |
| 3681 | */ |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3682 | static void coroutine_fn v9fs_getlock(void *opaque) |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3683 | { |
Aneesh Kumar K.V | e4e414a | 2011-05-07 17:21:38 +0530 | [diff] [blame] | 3684 | size_t offset = 7; |
| 3685 | struct stat stbuf; |
| 3686 | V9fsFidState *fidp; |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3687 | V9fsGetlock glock; |
Aneesh Kumar K.V | e4e414a | 2011-05-07 17:21:38 +0530 | [diff] [blame] | 3688 | int32_t fid, err = 0; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3689 | V9fsPDU *pdu = opaque; |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3690 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3691 | v9fs_string_init(&glock.client_id); |
| 3692 | err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, |
| 3693 | &glock.start, &glock.length, &glock.proc_id, |
| 3694 | &glock.client_id); |
| 3695 | if (err < 0) { |
| 3696 | goto out_nofid; |
| 3697 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3698 | trace_v9fs_getlock(pdu->tag, pdu->id, fid, |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3699 | glock.type, glock.start, glock.length); |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3700 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3701 | fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | e4e414a | 2011-05-07 17:21:38 +0530 | [diff] [blame] | 3702 | if (fidp == NULL) { |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3703 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3704 | goto out_nofid; |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3705 | } |
Aneesh Kumar K.V | cc720dd | 2011-10-25 12:10:40 +0530 | [diff] [blame] | 3706 | err = v9fs_co_fstat(pdu, fidp, &stbuf); |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3707 | if (err < 0) { |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3708 | goto out; |
| 3709 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3710 | glock.type = P9_LOCK_TYPE_UNLCK; |
| 3711 | err = pdu_marshal(pdu, offset, "bqqds", glock.type, |
| 3712 | glock.start, glock.length, glock.proc_id, |
| 3713 | &glock.client_id); |
| 3714 | if (err < 0) { |
| 3715 | goto out; |
| 3716 | } |
| 3717 | err += offset; |
| 3718 | trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, |
| 3719 | glock.length, glock.proc_id); |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3720 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3721 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3722 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3723 | pdu_complete(pdu, err); |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3724 | v9fs_string_free(&glock.client_id); |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 3725 | } |
| 3726 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3727 | static void coroutine_fn v9fs_mkdir(void *opaque) |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3728 | { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3729 | V9fsPDU *pdu = opaque; |
Venkateswararao Jujjuri | e84861f | 2011-08-08 23:46:14 +0530 | [diff] [blame] | 3730 | size_t offset = 7; |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3731 | int32_t fid; |
Venkateswararao Jujjuri | e84861f | 2011-08-08 23:46:14 +0530 | [diff] [blame] | 3732 | struct stat stbuf; |
Venkateswararao Jujjuri | e84861f | 2011-08-08 23:46:14 +0530 | [diff] [blame] | 3733 | V9fsQID qid; |
Aneesh Kumar K.V | 02cb7f3 | 2011-05-24 15:10:56 +0530 | [diff] [blame] | 3734 | V9fsString name; |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3735 | V9fsFidState *fidp; |
| 3736 | gid_t gid; |
| 3737 | int mode; |
Venkateswararao Jujjuri | e84861f | 2011-08-08 23:46:14 +0530 | [diff] [blame] | 3738 | int err = 0; |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3739 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3740 | v9fs_string_init(&name); |
| 3741 | err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); |
| 3742 | if (err < 0) { |
| 3743 | goto out_nofid; |
| 3744 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3745 | trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); |
| 3746 | |
Greg Kurz | fff39a7 | 2016-08-30 19:11:05 +0200 | [diff] [blame] | 3747 | if (name_is_illegal(name.data)) { |
| 3748 | err = -ENOENT; |
| 3749 | goto out_nofid; |
| 3750 | } |
| 3751 | |
Greg Kurz | 805b5d9 | 2016-08-30 19:13:11 +0200 | [diff] [blame] | 3752 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
| 3753 | err = -EEXIST; |
| 3754 | goto out_nofid; |
| 3755 | } |
| 3756 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3757 | fidp = get_fid(pdu, fid); |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3758 | if (fidp == NULL) { |
| 3759 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3760 | goto out_nofid; |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3761 | } |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3762 | err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); |
Venkateswararao Jujjuri | e84861f | 2011-08-08 23:46:14 +0530 | [diff] [blame] | 3763 | if (err < 0) { |
| 3764 | goto out; |
| 3765 | } |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 3766 | err = stat_to_qid(pdu, &stbuf, &qid); |
| 3767 | if (err < 0) { |
| 3768 | goto out; |
| 3769 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3770 | err = pdu_marshal(pdu, offset, "Q", &qid); |
| 3771 | if (err < 0) { |
| 3772 | goto out; |
| 3773 | } |
| 3774 | err += offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 3775 | trace_v9fs_mkdir_return(pdu->tag, pdu->id, |
| 3776 | qid.type, qid.version, qid.path, err); |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3777 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3778 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3779 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3780 | pdu_complete(pdu, err); |
Venkateswararao Jujjuri | e84861f | 2011-08-08 23:46:14 +0530 | [diff] [blame] | 3781 | v9fs_string_free(&name); |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 3782 | } |
| 3783 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3784 | static void coroutine_fn v9fs_xattrwalk(void *opaque) |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3785 | { |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3786 | int64_t size; |
| 3787 | V9fsString name; |
| 3788 | ssize_t err = 0; |
| 3789 | size_t offset = 7; |
| 3790 | int32_t fid, newfid; |
| 3791 | V9fsFidState *file_fidp; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3792 | V9fsFidState *xattr_fidp = NULL; |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3793 | V9fsPDU *pdu = opaque; |
| 3794 | V9fsState *s = pdu->s; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3795 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3796 | v9fs_string_init(&name); |
| 3797 | err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); |
| 3798 | if (err < 0) { |
| 3799 | goto out_nofid; |
| 3800 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3801 | trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); |
| 3802 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3803 | file_fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3804 | if (file_fidp == NULL) { |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3805 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3806 | goto out_nofid; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3807 | } |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3808 | xattr_fidp = alloc_fid(s, newfid); |
| 3809 | if (xattr_fidp == NULL) { |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3810 | err = -EINVAL; |
| 3811 | goto out; |
| 3812 | } |
Aneesh Kumar K.V | 2289be1 | 2011-09-09 15:14:18 +0530 | [diff] [blame] | 3813 | v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); |
Li Qiang | ba42ebb | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3814 | if (!v9fs_string_size(&name)) { |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3815 | /* |
| 3816 | * listxattr request. Get the size first |
| 3817 | */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3818 | size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3819 | if (size < 0) { |
| 3820 | err = size; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3821 | clunk_fid(s, xattr_fidp->fid); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3822 | goto out; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3823 | } |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3824 | /* |
| 3825 | * Read the xattr value |
| 3826 | */ |
| 3827 | xattr_fidp->fs.xattr.len = size; |
| 3828 | xattr_fidp->fid_type = P9_FID_XATTR; |
Li Qiang | dd28fbb | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3829 | xattr_fidp->fs.xattr.xattrwalk_fid = true; |
Keno Fischer | a647502 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3830 | xattr_fidp->fs.xattr.value = g_malloc0(size); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3831 | if (size) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3832 | err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3833 | xattr_fidp->fs.xattr.value, |
| 3834 | xattr_fidp->fs.xattr.len); |
| 3835 | if (err < 0) { |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3836 | clunk_fid(s, xattr_fidp->fid); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3837 | goto out; |
| 3838 | } |
| 3839 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3840 | err = pdu_marshal(pdu, offset, "q", size); |
| 3841 | if (err < 0) { |
| 3842 | goto out; |
| 3843 | } |
| 3844 | err += offset; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3845 | } else { |
| 3846 | /* |
| 3847 | * specific xattr fid. We check for xattr |
| 3848 | * presence also collect the xattr size |
| 3849 | */ |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3850 | size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3851 | &name, NULL, 0); |
| 3852 | if (size < 0) { |
| 3853 | err = size; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3854 | clunk_fid(s, xattr_fidp->fid); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3855 | goto out; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3856 | } |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3857 | /* |
| 3858 | * Read the xattr value |
| 3859 | */ |
| 3860 | xattr_fidp->fs.xattr.len = size; |
| 3861 | xattr_fidp->fid_type = P9_FID_XATTR; |
Li Qiang | dd28fbb | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3862 | xattr_fidp->fs.xattr.xattrwalk_fid = true; |
Keno Fischer | a647502 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3863 | xattr_fidp->fs.xattr.value = g_malloc0(size); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3864 | if (size) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3865 | err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3866 | &name, xattr_fidp->fs.xattr.value, |
| 3867 | xattr_fidp->fs.xattr.len); |
| 3868 | if (err < 0) { |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3869 | clunk_fid(s, xattr_fidp->fid); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3870 | goto out; |
| 3871 | } |
| 3872 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3873 | err = pdu_marshal(pdu, offset, "q", size); |
| 3874 | if (err < 0) { |
| 3875 | goto out; |
| 3876 | } |
| 3877 | err += offset; |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3878 | } |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 3879 | trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3880 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3881 | put_fid(pdu, file_fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3882 | if (xattr_fidp) { |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3883 | put_fid(pdu, xattr_fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3884 | } |
| 3885 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3886 | pdu_complete(pdu, err); |
Aneesh Kumar K.V | 670185a | 2011-05-18 16:05:48 -0700 | [diff] [blame] | 3887 | v9fs_string_free(&name); |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3888 | } |
| 3889 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3890 | static void coroutine_fn v9fs_xattrcreate(void *opaque) |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3891 | { |
Keno Fischer | aca6897 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3892 | int flags, rflags = 0; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3893 | int32_t fid; |
Greg Kurz | 3b79ef2 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3894 | uint64_t size; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3895 | ssize_t err = 0; |
Aneesh Kumar K.V | f10ff58 | 2011-05-18 16:06:26 -0700 | [diff] [blame] | 3896 | V9fsString name; |
| 3897 | size_t offset = 7; |
| 3898 | V9fsFidState *file_fidp; |
| 3899 | V9fsFidState *xattr_fidp; |
| 3900 | V9fsPDU *pdu = opaque; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3901 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3902 | v9fs_string_init(&name); |
| 3903 | err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); |
| 3904 | if (err < 0) { |
| 3905 | goto out_nofid; |
| 3906 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3907 | trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3908 | |
Keno Fischer | aca6897 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3909 | if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) { |
| 3910 | err = -EINVAL; |
| 3911 | goto out_nofid; |
| 3912 | } |
| 3913 | |
| 3914 | if (flags & P9_XATTR_CREATE) { |
| 3915 | rflags |= XATTR_CREATE; |
| 3916 | } |
| 3917 | |
| 3918 | if (flags & P9_XATTR_REPLACE) { |
| 3919 | rflags |= XATTR_REPLACE; |
| 3920 | } |
| 3921 | |
Greg Kurz | 3b79ef2 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3922 | if (size > XATTR_SIZE_MAX) { |
| 3923 | err = -E2BIG; |
| 3924 | goto out_nofid; |
| 3925 | } |
| 3926 | |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3927 | file_fidp = get_fid(pdu, fid); |
Aneesh Kumar K.V | f10ff58 | 2011-05-18 16:06:26 -0700 | [diff] [blame] | 3928 | if (file_fidp == NULL) { |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3929 | err = -EINVAL; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3930 | goto out_nofid; |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3931 | } |
Greg Kurz | dd654e0 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3932 | if (file_fidp->fid_type != P9_FID_NONE) { |
| 3933 | err = -EINVAL; |
| 3934 | goto out_put_fid; |
| 3935 | } |
| 3936 | |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3937 | /* Make the file fid point to xattr */ |
Aneesh Kumar K.V | f10ff58 | 2011-05-18 16:06:26 -0700 | [diff] [blame] | 3938 | xattr_fidp = file_fidp; |
| 3939 | xattr_fidp->fid_type = P9_FID_XATTR; |
| 3940 | xattr_fidp->fs.xattr.copied_len = 0; |
Li Qiang | dd28fbb | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3941 | xattr_fidp->fs.xattr.xattrwalk_fid = false; |
Aneesh Kumar K.V | f10ff58 | 2011-05-18 16:06:26 -0700 | [diff] [blame] | 3942 | xattr_fidp->fs.xattr.len = size; |
Keno Fischer | aca6897 | 2018-06-07 12:17:22 +0200 | [diff] [blame] | 3943 | xattr_fidp->fs.xattr.flags = rflags; |
Aneesh Kumar K.V | f10ff58 | 2011-05-18 16:06:26 -0700 | [diff] [blame] | 3944 | v9fs_string_init(&xattr_fidp->fs.xattr.name); |
| 3945 | v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); |
Li Qiang | eb68760 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3946 | xattr_fidp->fs.xattr.value = g_malloc0(size); |
Aneesh Kumar K.V | f10ff58 | 2011-05-18 16:06:26 -0700 | [diff] [blame] | 3947 | err = offset; |
Greg Kurz | dd654e0 | 2016-11-01 12:00:40 +0100 | [diff] [blame] | 3948 | out_put_fid: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3949 | put_fid(pdu, file_fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3950 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3951 | pdu_complete(pdu, err); |
Aneesh Kumar K.V | f10ff58 | 2011-05-18 16:06:26 -0700 | [diff] [blame] | 3952 | v9fs_string_free(&name); |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 3953 | } |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3954 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 3955 | static void coroutine_fn v9fs_readlink(void *opaque) |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 3956 | { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3957 | V9fsPDU *pdu = opaque; |
Venkateswararao Jujjuri | 7a5ca31 | 2011-08-08 23:36:41 +0530 | [diff] [blame] | 3958 | size_t offset = 7; |
| 3959 | V9fsString target; |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 3960 | int32_t fid; |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 3961 | int err = 0; |
| 3962 | V9fsFidState *fidp; |
| 3963 | |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3964 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
| 3965 | if (err < 0) { |
| 3966 | goto out_nofid; |
| 3967 | } |
Harsh Prateek Bora | c572f23 | 2011-10-12 19:11:25 +0530 | [diff] [blame] | 3968 | trace_v9fs_readlink(pdu->tag, pdu->id, fid); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3969 | fidp = get_fid(pdu, fid); |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 3970 | if (fidp == NULL) { |
| 3971 | err = -ENOENT; |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3972 | goto out_nofid; |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 3973 | } |
| 3974 | |
Venkateswararao Jujjuri | 7a5ca31 | 2011-08-08 23:36:41 +0530 | [diff] [blame] | 3975 | v9fs_string_init(&target); |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3976 | err = v9fs_co_readlink(pdu, &fidp->path, &target); |
Venkateswararao Jujjuri | 7a5ca31 | 2011-08-08 23:36:41 +0530 | [diff] [blame] | 3977 | if (err < 0) { |
| 3978 | goto out; |
| 3979 | } |
M. Mohan Kumar | ddca7f8 | 2011-12-14 13:49:13 +0530 | [diff] [blame] | 3980 | err = pdu_marshal(pdu, offset, "s", &target); |
| 3981 | if (err < 0) { |
| 3982 | v9fs_string_free(&target); |
| 3983 | goto out; |
| 3984 | } |
| 3985 | err += offset; |
Aneesh Kumar K.V | 7999f7e | 2011-10-24 15:09:49 +0530 | [diff] [blame] | 3986 | trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); |
Venkateswararao Jujjuri | 7a5ca31 | 2011-08-08 23:36:41 +0530 | [diff] [blame] | 3987 | v9fs_string_free(&target); |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 3988 | out: |
Aneesh Kumar K.V | bccacf6 | 2011-08-02 11:36:17 +0530 | [diff] [blame] | 3989 | put_fid(pdu, fidp); |
Aneesh Kumar K.V | 84dfb92 | 2011-05-18 17:38:07 +0530 | [diff] [blame] | 3990 | out_nofid: |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 3991 | pdu_complete(pdu, err); |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 3992 | } |
| 3993 | |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 3994 | static CoroutineEntry *pdu_co_handlers[] = { |
Sripathi Kodi | c18e2f9 | 2010-06-09 14:57:57 +0530 | [diff] [blame] | 3995 | [P9_TREADDIR] = v9fs_readdir, |
M. Mohan Kumar | be940c8 | 2010-05-10 12:11:03 +0530 | [diff] [blame] | 3996 | [P9_TSTATFS] = v9fs_statfs, |
Sripathi Kodi | 00ede4c | 2010-07-20 11:44:41 +0530 | [diff] [blame] | 3997 | [P9_TGETATTR] = v9fs_getattr, |
Sripathi Kodi | c79ce73 | 2010-06-17 18:18:47 +0530 | [diff] [blame] | 3998 | [P9_TSETATTR] = v9fs_setattr, |
Aneesh Kumar K.V | fa32ef8 | 2010-09-02 11:09:06 +0530 | [diff] [blame] | 3999 | [P9_TXATTRWALK] = v9fs_xattrwalk, |
Aneesh Kumar K.V | 10b468b | 2010-09-02 11:09:07 +0530 | [diff] [blame] | 4000 | [P9_TXATTRCREATE] = v9fs_xattrcreate, |
M. Mohan Kumar | 5268cec | 2010-06-22 12:24:09 +0530 | [diff] [blame] | 4001 | [P9_TMKNOD] = v9fs_mknod, |
M. Mohan Kumar | c7b4b0b | 2010-06-22 12:29:41 +0530 | [diff] [blame] | 4002 | [P9_TRENAME] = v9fs_rename, |
M. Mohan Kumar | 82cc3ee | 2010-09-08 02:19:32 +0530 | [diff] [blame] | 4003 | [P9_TLOCK] = v9fs_lock, |
M. Mohan Kumar | 8f35400 | 2010-09-08 02:36:52 +0530 | [diff] [blame] | 4004 | [P9_TGETLOCK] = v9fs_getlock, |
Aneesh Kumar K.V | 89bf659 | 2011-05-23 23:24:41 +0530 | [diff] [blame] | 4005 | [P9_TRENAMEAT] = v9fs_renameat, |
M. Mohan Kumar | df0973a | 2010-09-14 15:08:25 +0530 | [diff] [blame] | 4006 | [P9_TREADLINK] = v9fs_readlink, |
Aneesh Kumar K.V | 7834cf7 | 2011-09-09 15:07:01 +0530 | [diff] [blame] | 4007 | [P9_TUNLINKAT] = v9fs_unlinkat, |
M. Mohan Kumar | b67592e | 2010-06-22 12:25:22 +0530 | [diff] [blame] | 4008 | [P9_TMKDIR] = v9fs_mkdir, |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4009 | [P9_TVERSION] = v9fs_version, |
M. Mohan Kumar | 771e9d4 | 2010-06-22 19:47:04 +0530 | [diff] [blame] | 4010 | [P9_TLOPEN] = v9fs_open, |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4011 | [P9_TATTACH] = v9fs_attach, |
| 4012 | [P9_TSTAT] = v9fs_stat, |
| 4013 | [P9_TWALK] = v9fs_walk, |
| 4014 | [P9_TCLUNK] = v9fs_clunk, |
Venkateswararao Jujjuri (JV) | b41e95d | 2010-09-22 17:18:33 -0700 | [diff] [blame] | 4015 | [P9_TFSYNC] = v9fs_fsync, |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4016 | [P9_TOPEN] = v9fs_open, |
| 4017 | [P9_TREAD] = v9fs_read, |
| 4018 | #if 0 |
| 4019 | [P9_TAUTH] = v9fs_auth, |
| 4020 | #endif |
| 4021 | [P9_TFLUSH] = v9fs_flush, |
Venkateswararao Jujjuri (JV) | b2c224b | 2010-06-09 11:21:15 -0700 | [diff] [blame] | 4022 | [P9_TLINK] = v9fs_link, |
Venkateswararao Jujjuri (JV) | 08c60fc | 2010-06-09 14:02:08 -0700 | [diff] [blame] | 4023 | [P9_TSYMLINK] = v9fs_symlink, |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4024 | [P9_TCREATE] = v9fs_create, |
Venkateswararao Jujjuri (JV) | c1568af | 2010-06-17 18:27:24 -0700 | [diff] [blame] | 4025 | [P9_TLCREATE] = v9fs_lcreate, |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4026 | [P9_TWRITE] = v9fs_write, |
| 4027 | [P9_TWSTAT] = v9fs_wstat, |
| 4028 | [P9_TREMOVE] = v9fs_remove, |
| 4029 | }; |
| 4030 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 4031 | static void coroutine_fn v9fs_op_not_supp(void *opaque) |
Aneesh Kumar K.V | 5c3234c | 2011-06-01 12:35:14 +0530 | [diff] [blame] | 4032 | { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 4033 | V9fsPDU *pdu = opaque; |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 4034 | pdu_complete(pdu, -EOPNOTSUPP); |
Aneesh Kumar K.V | 5c3234c | 2011-06-01 12:35:14 +0530 | [diff] [blame] | 4035 | } |
| 4036 | |
Greg Kurz | 8440e22 | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 4037 | static void coroutine_fn v9fs_fs_ro(void *opaque) |
M. Mohan Kumar | 2c74c2c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 4038 | { |
| 4039 | V9fsPDU *pdu = opaque; |
Wei Liu | dc295f8 | 2015-12-02 15:00:14 +0000 | [diff] [blame] | 4040 | pdu_complete(pdu, -EROFS); |
M. Mohan Kumar | 2c74c2c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | static inline bool is_read_only_op(V9fsPDU *pdu) |
| 4044 | { |
| 4045 | switch (pdu->id) { |
| 4046 | case P9_TREADDIR: |
| 4047 | case P9_TSTATFS: |
| 4048 | case P9_TGETATTR: |
| 4049 | case P9_TXATTRWALK: |
| 4050 | case P9_TLOCK: |
| 4051 | case P9_TGETLOCK: |
| 4052 | case P9_TREADLINK: |
| 4053 | case P9_TVERSION: |
| 4054 | case P9_TLOPEN: |
| 4055 | case P9_TATTACH: |
| 4056 | case P9_TSTAT: |
| 4057 | case P9_TWALK: |
| 4058 | case P9_TCLUNK: |
| 4059 | case P9_TFSYNC: |
| 4060 | case P9_TOPEN: |
| 4061 | case P9_TREAD: |
| 4062 | case P9_TAUTH: |
| 4063 | case P9_TFLUSH: |
| 4064 | return 1; |
| 4065 | default: |
| 4066 | return 0; |
| 4067 | } |
| 4068 | } |
| 4069 | |
Greg Kurz | 506f327 | 2017-05-25 10:30:13 +0200 | [diff] [blame] | 4070 | void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr) |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4071 | { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 4072 | Coroutine *co; |
| 4073 | CoroutineEntry *handler; |
Wei Liu | ad38ce9 | 2015-12-02 12:06:28 +0000 | [diff] [blame] | 4074 | V9fsState *s = pdu->s; |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4075 | |
Greg Kurz | 506f327 | 2017-05-25 10:30:13 +0200 | [diff] [blame] | 4076 | pdu->size = le32_to_cpu(hdr->size_le); |
| 4077 | pdu->id = hdr->id; |
| 4078 | pdu->tag = le16_to_cpu(hdr->tag_le); |
| 4079 | |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 4080 | if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || |
| 4081 | (pdu_co_handlers[pdu->id] == NULL)) { |
Aneesh Kumar K.V | 5c3234c | 2011-06-01 12:35:14 +0530 | [diff] [blame] | 4082 | handler = v9fs_op_not_supp; |
Greg Kurz | d147123 | 2018-01-08 11:18:22 +0100 | [diff] [blame] | 4083 | } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { |
| 4084 | handler = v9fs_fs_ro; |
Aneesh Kumar K.V | 5c3234c | 2011-06-01 12:35:14 +0530 | [diff] [blame] | 4085 | } else { |
Venkateswararao Jujjuri (JV) | ff06030 | 2011-05-18 14:18:05 -0700 | [diff] [blame] | 4086 | handler = pdu_co_handlers[pdu->id]; |
Aneesh Kumar K.V | 5c3234c | 2011-06-01 12:35:14 +0530 | [diff] [blame] | 4087 | } |
M. Mohan Kumar | 2c74c2c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 4088 | |
Greg Kurz | 506f327 | 2017-05-25 10:30:13 +0200 | [diff] [blame] | 4089 | qemu_co_queue_init(&pdu->complete); |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 4090 | co = qemu_coroutine_create(handler, pdu); |
| 4091 | qemu_coroutine_enter(co); |
Anthony Liguori | 9f10751 | 2010-04-29 17:44:44 +0530 | [diff] [blame] | 4092 | } |
| 4093 | |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4094 | /* Returns 0 on success, 1 on failure. */ |
Greg Kurz | 066eb00 | 2018-02-01 21:21:27 +0100 | [diff] [blame] | 4095 | int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, |
| 4096 | Error **errp) |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4097 | { |
Vladimir Sementsov-Ogievskiy | 92c4512 | 2020-07-07 18:50:35 +0200 | [diff] [blame] | 4098 | ERRP_GUARD(); |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4099 | int i, len; |
| 4100 | struct stat stat; |
| 4101 | FsDriverEntry *fse; |
| 4102 | V9fsPath path; |
| 4103 | int rc = 1; |
| 4104 | |
Greg Kurz | 066eb00 | 2018-02-01 21:21:27 +0100 | [diff] [blame] | 4105 | assert(!s->transport); |
| 4106 | s->transport = t; |
| 4107 | |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4108 | /* initialize pdu allocator */ |
| 4109 | QLIST_INIT(&s->free_list); |
| 4110 | QLIST_INIT(&s->active_list); |
Greg Kurz | 0d78289 | 2017-01-13 18:18:20 +0100 | [diff] [blame] | 4111 | for (i = 0; i < MAX_REQ; i++) { |
Stefano Stabellini | 583f21f | 2017-01-03 17:28:44 +0100 | [diff] [blame] | 4112 | QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); |
| 4113 | s->pdus[i].s = s; |
| 4114 | s->pdus[i].idx = i; |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | v9fs_path_init(&path); |
| 4118 | |
| 4119 | fse = get_fsdev_fsentry(s->fsconf.fsdev_id); |
| 4120 | |
| 4121 | if (!fse) { |
| 4122 | /* We don't have a fsdev identified by fsdev_id */ |
| 4123 | error_setg(errp, "9pfs device couldn't find fsdev with the " |
| 4124 | "id = %s", |
| 4125 | s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); |
| 4126 | goto out; |
| 4127 | } |
| 4128 | |
| 4129 | if (!s->fsconf.tag) { |
| 4130 | /* we haven't specified a mount_tag */ |
| 4131 | error_setg(errp, "fsdev with id %s needs mount_tag arguments", |
| 4132 | s->fsconf.fsdev_id); |
| 4133 | goto out; |
| 4134 | } |
| 4135 | |
| 4136 | s->ctx.export_flags = fse->export_flags; |
| 4137 | s->ctx.fs_root = g_strdup(fse->path); |
| 4138 | s->ctx.exops.get_st_gen = NULL; |
| 4139 | len = strlen(s->fsconf.tag); |
| 4140 | if (len > MAX_TAG_LEN - 1) { |
| 4141 | error_setg(errp, "mount tag '%s' (%d bytes) is longer than " |
| 4142 | "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); |
| 4143 | goto out; |
| 4144 | } |
| 4145 | |
| 4146 | s->tag = g_strdup(s->fsconf.tag); |
| 4147 | s->ctx.uid = -1; |
| 4148 | |
| 4149 | s->ops = fse->ops; |
| 4150 | |
Tobias Schramm | b96feb2 | 2017-06-29 15:11:50 +0200 | [diff] [blame] | 4151 | s->ctx.fmode = fse->fmode; |
| 4152 | s->ctx.dmode = fse->dmode; |
| 4153 | |
Greg Kurz | feabd6c | 2021-01-18 15:22:59 +0100 | [diff] [blame] | 4154 | QSIMPLEQ_INIT(&s->fid_list); |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4155 | qemu_co_rwlock_init(&s->rename_lock); |
| 4156 | |
Greg Kurz | 65603a8 | 2018-01-08 11:18:23 +0100 | [diff] [blame] | 4157 | if (s->ops->init(&s->ctx, errp) < 0) { |
| 4158 | error_prepend(errp, "cannot initialize fsdev '%s': ", |
| 4159 | s->fsconf.fsdev_id); |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4160 | goto out; |
| 4161 | } |
| 4162 | |
| 4163 | /* |
| 4164 | * Check details of export path, We need to use fs driver |
| 4165 | * call back to do that. Since we are in the init path, we don't |
| 4166 | * use co-routines here. |
| 4167 | */ |
| 4168 | if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { |
| 4169 | error_setg(errp, |
| 4170 | "error in converting name to path %s", strerror(errno)); |
| 4171 | goto out; |
| 4172 | } |
| 4173 | if (s->ops->lstat(&s->ctx, &path, &stat)) { |
| 4174 | error_setg(errp, "share path %s does not exist", fse->path); |
| 4175 | goto out; |
| 4176 | } else if (!S_ISDIR(stat.st_mode)) { |
| 4177 | error_setg(errp, "share path %s is not a directory", fse->path); |
| 4178 | goto out; |
| 4179 | } |
Pradeep Jagadeesh | b8bbdb8 | 2017-02-28 10:31:46 +0100 | [diff] [blame] | 4180 | |
Antonios Motakis | 3b5ee9e | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 4181 | s->dev_id = stat.st_dev; |
| 4182 | |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 4183 | /* init inode remapping : */ |
| 4184 | /* hash table for variable length inode suffixes */ |
| 4185 | qpd_table_init(&s->qpd_table); |
| 4186 | /* hash table for slow/full inode remapping (most users won't need it) */ |
| 4187 | qpf_table_init(&s->qpf_table); |
| 4188 | /* hash table for quick inode remapping */ |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 4189 | qpp_table_init(&s->qpp_table); |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 4190 | s->qp_ndevices = 0; |
| 4191 | s->qp_affix_next = 1; /* reserve 0 to detect overflow */ |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 4192 | s->qp_fullpath_next = 1; |
Antonios Motakis | 1a6ed33 | 2019-10-10 11:36:05 +0200 | [diff] [blame] | 4193 | |
Pradeep Jagadeesh | b8bbdb8 | 2017-02-28 10:31:46 +0100 | [diff] [blame] | 4194 | s->ctx.fst = &fse->fst; |
| 4195 | fsdev_throttle_init(s->ctx.fst); |
| 4196 | |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4197 | rc = 0; |
| 4198 | out: |
| 4199 | if (rc) { |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 4200 | v9fs_device_unrealize_common(s); |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4201 | } |
Greg Kurz | c0da0cb | 2019-10-10 11:36:04 +0200 | [diff] [blame] | 4202 | v9fs_path_free(&path); |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4203 | return rc; |
| 4204 | } |
| 4205 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 4206 | void v9fs_device_unrealize_common(V9fsState *s) |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4207 | { |
Greg Kurz | c0da0cb | 2019-10-10 11:36:04 +0200 | [diff] [blame] | 4208 | if (s->ops && s->ops->cleanup) { |
Li Qiang | 702dbcc | 2016-11-23 13:53:34 +0100 | [diff] [blame] | 4209 | s->ops->cleanup(&s->ctx); |
| 4210 | } |
Greg Kurz | c0da0cb | 2019-10-10 11:36:04 +0200 | [diff] [blame] | 4211 | if (s->ctx.fst) { |
| 4212 | fsdev_throttle_cleanup(s->ctx.fst); |
| 4213 | } |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4214 | g_free(s->tag); |
Christian Schoenebeck | 6b6aa82 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 4215 | qp_table_destroy(&s->qpd_table); |
Antonios Motakis | f3fe4a2 | 2019-10-07 17:02:45 +0200 | [diff] [blame] | 4216 | qp_table_destroy(&s->qpp_table); |
| 4217 | qp_table_destroy(&s->qpf_table); |
Li Qiang | 4774718 | 2016-11-23 13:53:34 +0100 | [diff] [blame] | 4218 | g_free(s->ctx.fs_root); |
Wei Liu | 2a0c56a | 2015-12-03 11:55:49 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Greg Kurz | 0e44a0f | 2016-10-17 14:13:58 +0200 | [diff] [blame] | 4221 | typedef struct VirtfsCoResetData { |
| 4222 | V9fsPDU pdu; |
| 4223 | bool done; |
| 4224 | } VirtfsCoResetData; |
| 4225 | |
| 4226 | static void coroutine_fn virtfs_co_reset(void *opaque) |
| 4227 | { |
| 4228 | VirtfsCoResetData *data = opaque; |
| 4229 | |
| 4230 | virtfs_reset(&data->pdu); |
| 4231 | data->done = true; |
| 4232 | } |
| 4233 | |
| 4234 | void v9fs_reset(V9fsState *s) |
| 4235 | { |
| 4236 | VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; |
| 4237 | Coroutine *co; |
| 4238 | |
| 4239 | while (!QLIST_EMPTY(&s->active_list)) { |
| 4240 | aio_poll(qemu_get_aio_context(), true); |
| 4241 | } |
| 4242 | |
| 4243 | co = qemu_coroutine_create(virtfs_co_reset, &data); |
| 4244 | qemu_coroutine_enter(co); |
| 4245 | |
| 4246 | while (!data.done) { |
| 4247 | aio_poll(qemu_get_aio_context(), true); |
| 4248 | } |
| 4249 | } |
| 4250 | |
Wei Liu | 72a1897 | 2016-01-07 18:40:09 +0000 | [diff] [blame] | 4251 | static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 4252 | { |
| 4253 | struct rlimit rlim; |
| 4254 | if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { |
Greg Kurz | 63325b1 | 2016-01-22 15:12:17 +0100 | [diff] [blame] | 4255 | error_report("Failed to get the resource limit"); |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 4256 | exit(1); |
| 4257 | } |
Xinhao Zhang | 0101173 | 2020-10-30 12:35:13 +0800 | [diff] [blame] | 4258 | open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3); |
| 4259 | open_fd_rc = rlim.rlim_cur / 2; |
Aneesh Kumar K.V | 7a46274 | 2011-05-18 15:40:57 +0530 | [diff] [blame] | 4260 | } |