Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 : */
4 : #include "spdk/stdinc.h"
5 : #include "spdk/event.h"
6 : #include "spdk/log.h"
7 : #include "spdk/string.h"
8 : #include "spdk/config.h"
9 : #include "spdk/util.h"
10 : #include "spdk/thread.h"
11 : #include "aio_mgr.h"
12 : #include "fsdev_aio.h"
13 :
14 : #define IO_STATUS_ASYNC INT_MIN
15 :
16 : #ifndef UNUSED
17 : #define UNUSED(x) (void)(x)
18 : #endif
19 :
20 : /* See https://libfuse.github.io/doxygen/structfuse__conn__info.html */
21 : #define MAX_BACKGROUND (100)
22 : #define TIME_GRAN (1)
23 : #define MAX_AIOS 256
24 : #define DEFAULT_WRITEBACK_CACHE true
25 : #define DEFAULT_MAX_WRITE 0x00020000
26 : #define DEFAULT_XATTR_ENABLED false
27 : #define DEFAULT_TIMEOUT_MS 0 /* to prevent the attribute caching */
28 :
29 : #ifdef SPDK_CONFIG_HAVE_STRUCT_STAT_ST_ATIM
30 : /* Linux */
31 : #define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atim.tv_nsec)
32 : #define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctim.tv_nsec)
33 : #define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtim.tv_nsec)
34 : #define ST_ATIM_NSEC_SET(stbuf, val) (stbuf)->st_atim.tv_nsec = (val)
35 : #define ST_CTIM_NSEC_SET(stbuf, val) (stbuf)->st_ctim.tv_nsec = (val)
36 : #define ST_MTIM_NSEC_SET(stbuf, val) (stbuf)->st_mtim.tv_nsec = (val)
37 : #elif defined(SPDK_CONFIG_HAVE_STRUCT_STAT_ST_ATIMESPEC)
38 : /* FreeBSD */
39 : #define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atimespec.tv_nsec)
40 : #define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctimespec.tv_nsec)
41 : #define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtimespec.tv_nsec)
42 : #define ST_ATIM_NSEC_SET(stbuf, val) (stbuf)->st_atimespec.tv_nsec = (val)
43 : #define ST_CTIM_NSEC_SET(stbuf, val) (stbuf)->st_ctimespec.tv_nsec = (val)
44 : #define ST_MTIM_NSEC_SET(stbuf, val) (stbuf)->st_mtimespec.tv_nsec = (val)
45 : #else
46 : #define ST_ATIM_NSEC(stbuf) 0
47 : #define ST_CTIM_NSEC(stbuf) 0
48 : #define ST_MTIM_NSEC(stbuf) 0
49 : #define ST_ATIM_NSEC_SET(stbuf, val) do { } while (0)
50 : #define ST_CTIM_NSEC_SET(stbuf, val) do { } while (0)
51 : #define ST_MTIM_NSEC_SET(stbuf, val) do { } while (0)
52 : #endif
53 :
54 : struct lo_cred {
55 : uid_t euid;
56 : gid_t egid;
57 : };
58 :
59 : /** Inode number type */
60 : typedef uint64_t spdk_ino_t;
61 :
62 : struct lo_key {
63 : ino_t ino;
64 : dev_t dev;
65 : };
66 :
67 : struct spdk_fsdev_file_handle {
68 : int fd;
69 : struct {
70 : DIR *dp;
71 : struct dirent *entry;
72 : off_t offset;
73 : } dir;
74 : struct spdk_fsdev_file_object *fobject;
75 : TAILQ_ENTRY(spdk_fsdev_file_handle) link;
76 : };
77 :
78 : #define FOBJECT_FMT "ino=%" PRIu64 " dev=%" PRIu64
79 : #define FOBJECT_ARGS(fo) ((uint64_t)(fo)->key.ino), ((uint64_t)(fo)->key.dev)
80 : struct spdk_fsdev_file_object {
81 : uint32_t is_symlink : 1;
82 : uint32_t is_dir : 1;
83 : uint32_t reserved : 30;
84 : int fd;
85 : char *fd_str;
86 : struct lo_key key;
87 : uint64_t refcount;
88 : struct spdk_fsdev_file_object *parent_fobject;
89 : TAILQ_ENTRY(spdk_fsdev_file_object) link;
90 : TAILQ_HEAD(, spdk_fsdev_file_object) leafs;
91 : TAILQ_HEAD(, spdk_fsdev_file_handle) handles;
92 : struct spdk_spinlock lock;
93 : char name[];
94 : };
95 :
96 : struct aio_fsdev {
97 : struct spdk_fsdev fsdev;
98 : char *root_path;
99 : int proc_self_fd;
100 : pthread_mutex_t mutex;
101 : struct spdk_fsdev_file_object *root;
102 : TAILQ_ENTRY(aio_fsdev) tailq;
103 : bool xattr_enabled;
104 : };
105 :
106 : struct aio_fsdev_io {
107 : struct spdk_aio_mgr_io *aio;
108 : struct aio_io_channel *ch;
109 : TAILQ_ENTRY(aio_fsdev_io) link;
110 : };
111 :
112 : struct aio_io_channel {
113 : struct spdk_poller *poller;
114 : struct spdk_aio_mgr *mgr;
115 : TAILQ_HEAD(, aio_fsdev_io) ios_in_progress;
116 : };
117 :
118 : static TAILQ_HEAD(, aio_fsdev) g_aio_fsdev_head = TAILQ_HEAD_INITIALIZER(
119 : g_aio_fsdev_head);
120 :
121 : static inline struct aio_fsdev *
122 0 : fsdev_to_aio_fsdev(struct spdk_fsdev *fsdev)
123 : {
124 0 : return SPDK_CONTAINEROF(fsdev, struct aio_fsdev, fsdev);
125 : }
126 :
127 : static inline struct spdk_fsdev_io *
128 0 : aio_to_fsdev_io(const struct aio_fsdev_io *aio_io)
129 : {
130 0 : return SPDK_CONTAINEROF(aio_io, struct spdk_fsdev_io, driver_ctx);
131 : }
132 :
133 : static inline struct aio_fsdev_io *
134 0 : fsdev_to_aio_io(const struct spdk_fsdev_io *fsdev_io)
135 : {
136 0 : return (struct aio_fsdev_io *)fsdev_io->driver_ctx;
137 : }
138 :
139 : static inline bool
140 0 : fsdev_aio_is_valid_fobject(struct aio_fsdev *vfsdev, struct spdk_fsdev_file_object *fobject)
141 : {
142 0 : return fobject != NULL;
143 : }
144 :
145 : static inline bool
146 0 : fsdev_aio_is_valid_fhandle(struct aio_fsdev *vfsdev, struct spdk_fsdev_file_handle *fhandle)
147 : {
148 0 : return fhandle != NULL;
149 : }
150 :
151 : static int
152 0 : is_dot_or_dotdot(const char *name)
153 : {
154 0 : return name[0] == '.' && (name[1] == '\0' ||
155 0 : (name[1] == '.' && name[2] == '\0'));
156 : }
157 :
158 : /* Is `path` a single path component that is not "." or ".."? */
159 : static int
160 0 : is_safe_path_component(const char *path)
161 : {
162 0 : if (strchr(path, '/')) {
163 0 : return 0;
164 : }
165 :
166 0 : return !is_dot_or_dotdot(path);
167 : }
168 :
169 : static struct spdk_fsdev_file_object *
170 0 : lo_find_leaf_unsafe(struct spdk_fsdev_file_object *fobject, ino_t ino, dev_t dev)
171 : {
172 : struct spdk_fsdev_file_object *leaf_fobject;
173 :
174 0 : TAILQ_FOREACH(leaf_fobject, &fobject->leafs, link) {
175 0 : if (leaf_fobject->key.ino == ino && leaf_fobject->key.dev == dev) {
176 0 : return leaf_fobject;
177 : }
178 : }
179 :
180 0 : return NULL;
181 : }
182 :
183 : /* This function returns:
184 : * 1 if the refcount is still non zero
185 : * a negative error number if the refcount became zero, the file object was deleted but the defered underlying file deletion failed
186 : * 0 if the refcount became zero, the file object was deleted and eithr the underlying file deletion wasn't defered or succeeded
187 : */
188 : static int
189 0 : file_object_unref(struct spdk_fsdev_file_object *fobject, uint32_t count)
190 : {
191 0 : int res = 0;
192 :
193 0 : spdk_spin_lock(&fobject->lock);
194 0 : assert(fobject->refcount >= count);
195 0 : fobject->refcount -= count;
196 0 : spdk_spin_unlock(&fobject->lock);
197 :
198 0 : if (!fobject->refcount) {
199 0 : struct spdk_fsdev_file_object *parent_fobject = fobject->parent_fobject;
200 :
201 0 : if (parent_fobject) {
202 0 : spdk_spin_lock(&parent_fobject->lock);
203 0 : TAILQ_REMOVE(&parent_fobject->leafs, fobject, link);
204 0 : spdk_spin_unlock(&parent_fobject->lock);
205 0 : file_object_unref(parent_fobject, 1); /* unref by the leaf */
206 : }
207 :
208 0 : spdk_spin_destroy(&fobject->lock);
209 0 : close(fobject->fd);
210 0 : free(fobject->fd_str);
211 0 : free(fobject);
212 : }
213 :
214 0 : return res;
215 : }
216 :
217 : static void
218 0 : file_object_ref(struct spdk_fsdev_file_object *fobject)
219 : {
220 0 : spdk_spin_lock(&fobject->lock);
221 0 : fobject->refcount++;
222 0 : spdk_spin_unlock(&fobject->lock);
223 0 : }
224 :
225 : static struct spdk_fsdev_file_object *
226 0 : file_object_create_unsafe(struct spdk_fsdev_file_object *parent_fobject, int fd, ino_t ino,
227 : dev_t dev, mode_t mode)
228 : {
229 : struct spdk_fsdev_file_object *fobject;
230 :
231 0 : fobject = calloc(1, sizeof(*fobject));
232 0 : if (!fobject) {
233 0 : SPDK_ERRLOG("Cannot alloc fobject\n");
234 0 : return NULL;
235 : }
236 :
237 0 : fobject->fd_str = spdk_sprintf_alloc("%d", fd);
238 0 : if (!fobject->fd_str) {
239 0 : SPDK_ERRLOG("Cannot alloc fd_str\n");
240 0 : free(fobject);
241 0 : return NULL;
242 : }
243 :
244 0 : fobject->fd = fd;
245 0 : fobject->key.ino = ino;
246 0 : fobject->key.dev = dev;
247 0 : fobject->refcount = 1;
248 0 : fobject->is_symlink = S_ISLNK(mode) ? 1 : 0;
249 0 : fobject->is_dir = S_ISDIR(mode) ? 1 : 0;
250 :
251 0 : TAILQ_INIT(&fobject->handles);
252 0 : TAILQ_INIT(&fobject->leafs);
253 0 : spdk_spin_init(&fobject->lock);
254 :
255 0 : if (parent_fobject) {
256 0 : fobject->parent_fobject = parent_fobject;
257 0 : TAILQ_INSERT_TAIL(&parent_fobject->leafs, fobject, link);
258 0 : parent_fobject->refcount++;
259 : }
260 :
261 0 : return fobject;
262 : }
263 :
264 : static struct spdk_fsdev_file_handle *
265 0 : file_handle_create(struct spdk_fsdev_file_object *fobject, int fd)
266 : {
267 : struct spdk_fsdev_file_handle *fhandle;
268 :
269 0 : fhandle = calloc(1, sizeof(*fhandle));
270 0 : if (!fhandle) {
271 0 : SPDK_ERRLOG("Cannot alloc fhandle\n");
272 0 : return NULL;
273 : }
274 :
275 0 : fhandle->fobject = fobject;
276 0 : fhandle->fd = fd;
277 :
278 0 : spdk_spin_lock(&fobject->lock);
279 0 : fobject->refcount++;
280 0 : TAILQ_INSERT_TAIL(&fobject->handles, fhandle, link);
281 0 : spdk_spin_unlock(&fobject->lock);
282 :
283 0 : return fhandle;
284 : }
285 :
286 : static void
287 0 : file_handle_delete(struct spdk_fsdev_file_handle *fhandle)
288 : {
289 0 : struct spdk_fsdev_file_object *fobject = fhandle->fobject;
290 :
291 0 : spdk_spin_lock(&fobject->lock);
292 0 : fobject->refcount--;
293 0 : TAILQ_REMOVE(&fobject->handles, fhandle, link);
294 0 : spdk_spin_unlock(&fobject->lock);
295 :
296 0 : if (fhandle->dir.dp) {
297 0 : closedir(fhandle->dir.dp);
298 : }
299 :
300 0 : close(fhandle->fd);
301 0 : free(fhandle);
302 0 : }
303 :
304 : static int
305 0 : file_object_fill_attr(struct spdk_fsdev_file_object *fobject, struct spdk_fsdev_file_attr *attr)
306 : {
307 0 : struct stat stbuf;
308 : int res;
309 :
310 0 : res = fstatat(fobject->fd, "", &stbuf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
311 0 : if (res == -1) {
312 0 : res = -errno;
313 0 : SPDK_ERRLOG("fstatat() failed with %d\n", res);
314 0 : return res;
315 : }
316 :
317 0 : memset(attr, 0, sizeof(*attr));
318 :
319 0 : attr->ino = stbuf.st_ino;
320 0 : attr->size = stbuf.st_size;
321 0 : attr->blocks = stbuf.st_blocks;
322 0 : attr->atime = stbuf.st_atime;
323 0 : attr->mtime = stbuf.st_mtime;
324 0 : attr->ctime = stbuf.st_ctime;
325 0 : attr->atimensec = ST_ATIM_NSEC(&stbuf);
326 0 : attr->mtimensec = ST_MTIM_NSEC(&stbuf);
327 0 : attr->ctimensec = ST_CTIM_NSEC(&stbuf);
328 0 : attr->mode = stbuf.st_mode;
329 0 : attr->nlink = stbuf.st_nlink;
330 0 : attr->uid = stbuf.st_uid;
331 0 : attr->gid = stbuf.st_gid;
332 0 : attr->rdev = stbuf.st_rdev;
333 0 : attr->blksize = stbuf.st_blksize;
334 0 : attr->valid_ms = DEFAULT_TIMEOUT_MS;
335 :
336 0 : return 0;
337 : }
338 :
339 : static int
340 0 : utimensat_empty(struct aio_fsdev *vfsdev, struct spdk_fsdev_file_object *fobject,
341 : const struct timespec *tv)
342 : {
343 : int res;
344 :
345 0 : if (fobject->is_symlink) {
346 0 : res = utimensat(fobject->fd, "", tv, AT_EMPTY_PATH);
347 0 : if (res == -1 && errno == EINVAL) {
348 : /* Sorry, no race free way to set times on symlink. */
349 0 : errno = EPERM;
350 : }
351 : } else {
352 0 : res = utimensat(vfsdev->proc_self_fd, fobject->fd_str, tv, 0);
353 : }
354 :
355 0 : return res;
356 : }
357 :
358 : static int
359 0 : lo_getattr(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
360 : {
361 : int res;
362 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
363 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.getattr.fobject;
364 :
365 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
366 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
367 0 : return -EINVAL;
368 : }
369 :
370 0 : res = file_object_fill_attr(fobject, &fsdev_io->u_out.getattr.attr);
371 0 : if (res) {
372 0 : SPDK_ERRLOG("Cannot fill attr for " FOBJECT_FMT " (err=%d)\n", FOBJECT_ARGS(fobject), res);
373 0 : return res;
374 : }
375 :
376 0 : SPDK_DEBUGLOG(fsdev_aio, "GETATTR succeeded for " FOBJECT_FMT "\n", FOBJECT_ARGS(fobject));
377 0 : return 0;
378 : }
379 :
380 : static int
381 0 : lo_opendir(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
382 : {
383 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
384 : int error;
385 : int fd;
386 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.opendir.fobject;
387 0 : uint32_t flags = fsdev_io->u_in.opendir.flags;
388 0 : struct spdk_fsdev_file_handle *fhandle = NULL;
389 :
390 : UNUSED(flags);
391 :
392 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
393 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
394 0 : return -EINVAL;
395 : }
396 :
397 0 : fd = openat(fobject->fd, ".", O_RDONLY);
398 0 : if (fd == -1) {
399 0 : error = -errno;
400 0 : SPDK_ERRLOG("openat failed for " FOBJECT_FMT " (err=%d)\n", FOBJECT_ARGS(fobject), error);
401 0 : goto out_err;
402 : }
403 :
404 0 : fhandle = file_handle_create(fobject, fd);
405 0 : if (fhandle == NULL) {
406 0 : error = -ENOMEM;
407 0 : SPDK_ERRLOG("file_handle_create failed for " FOBJECT_FMT " (err=%d)\n", FOBJECT_ARGS(fobject),
408 : error);
409 0 : goto out_err;
410 : }
411 :
412 0 : fhandle->dir.dp = fdopendir(fd);
413 0 : if (fhandle->dir.dp == NULL) {
414 0 : error = -errno;
415 0 : SPDK_ERRLOG("fdopendir failed for " FOBJECT_FMT " (err=%d)\n", FOBJECT_ARGS(fobject), error);
416 0 : goto out_err;
417 : }
418 :
419 0 : fhandle->dir.offset = 0;
420 0 : fhandle->dir.entry = NULL;
421 :
422 0 : SPDK_DEBUGLOG(fsdev_aio, "OPENDIR succeeded for " FOBJECT_FMT " (fh=%p)\n",
423 : FOBJECT_ARGS(fobject), fhandle);
424 :
425 0 : fsdev_io->u_out.opendir.fhandle = fhandle;
426 :
427 0 : return 0;
428 :
429 0 : out_err:
430 0 : if (fhandle) {
431 0 : file_handle_delete(fhandle);
432 0 : } else if (fd != -1) {
433 0 : close(fd);
434 : }
435 :
436 0 : return error;
437 : }
438 :
439 : static int
440 0 : lo_releasedir(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
441 : {
442 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
443 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.releasedir.fobject;
444 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.releasedir.fhandle;
445 :
446 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
447 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
448 0 : return -EINVAL;
449 : }
450 :
451 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
452 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
453 0 : return -EINVAL;
454 : }
455 :
456 0 : SPDK_DEBUGLOG(fsdev_aio, "RELEASEDIR succeeded for " FOBJECT_FMT " (fh=%p)\n",
457 : FOBJECT_ARGS(fobject), fhandle);
458 :
459 0 : file_handle_delete(fhandle);
460 :
461 0 : return 0;
462 : }
463 :
464 : static int
465 0 : lo_do_lookup(struct aio_fsdev *vfsdev, struct spdk_fsdev_file_object *parent_fobject,
466 : const char *name, struct spdk_fsdev_file_object **pfobject,
467 : struct spdk_fsdev_file_attr *attr)
468 : {
469 : int newfd;
470 : int res;
471 0 : struct stat stat;
472 : struct spdk_fsdev_file_object *fobject;
473 :
474 : /* Do not allow escaping root directory */
475 0 : if (parent_fobject == vfsdev->root && strcmp(name, "..") == 0) {
476 0 : name = ".";
477 : }
478 :
479 0 : newfd = openat(parent_fobject->fd, name, O_PATH | O_NOFOLLOW);
480 0 : if (newfd == -1) {
481 0 : res = -errno;
482 0 : SPDK_DEBUGLOG(fsdev_aio, "openat( " FOBJECT_FMT " %s) failed with %d\n",
483 : FOBJECT_ARGS(parent_fobject), name, res);
484 0 : return res;
485 : }
486 :
487 0 : res = fstatat(newfd, "", &stat, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
488 0 : if (res == -1) {
489 0 : res = -errno;
490 0 : SPDK_ERRLOG("fstatat(%s) failed with %d\n", name, res);
491 0 : close(newfd);
492 0 : return res;
493 : }
494 :
495 0 : spdk_spin_lock(&parent_fobject->lock);
496 0 : fobject = lo_find_leaf_unsafe(parent_fobject, stat.st_ino, stat.st_dev);
497 0 : if (fobject) {
498 0 : close(newfd);
499 0 : newfd = -1;
500 0 : file_object_ref(fobject); /* reference by a lo_do_lookup caller */
501 : } else {
502 0 : fobject = file_object_create_unsafe(parent_fobject, newfd, stat.st_ino, stat.st_dev, stat.st_mode);
503 : }
504 0 : spdk_spin_unlock(&parent_fobject->lock);
505 :
506 0 : if (!fobject) {
507 0 : SPDK_ERRLOG("Cannot create file object\n");
508 0 : close(newfd);
509 0 : return -ENOMEM;
510 : }
511 :
512 0 : if (attr) {
513 0 : res = file_object_fill_attr(fobject, attr);
514 0 : if (res) {
515 0 : SPDK_ERRLOG("fill_attr(%s) failed with %d\n", name, res);
516 0 : file_object_unref(fobject, 1);
517 0 : if (newfd != -1) {
518 0 : close(newfd);
519 : }
520 0 : return res;
521 : }
522 : }
523 :
524 0 : *pfobject = fobject;
525 :
526 0 : SPDK_DEBUGLOG(fsdev_aio, "lookup(%s) in dir " FOBJECT_FMT ": " FOBJECT_FMT " fd=%d\n",
527 : name, FOBJECT_ARGS(parent_fobject), FOBJECT_ARGS(fobject), fobject->fd);
528 0 : return 0;
529 : }
530 :
531 : static int
532 0 : lo_lookup(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
533 : {
534 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
535 : int err;
536 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.lookup.parent_fobject;
537 0 : char *name = fsdev_io->u_in.lookup.name;
538 :
539 0 : if (!parent_fobject) {
540 0 : err = file_object_fill_attr(vfsdev->root, &fsdev_io->u_out.lookup.attr);
541 0 : if (err) {
542 0 : SPDK_DEBUGLOG(fsdev_aio, "file_object_fill_attr(root) failed with err=%d\n", err);
543 0 : return err;
544 : }
545 :
546 0 : file_object_ref(vfsdev->root);
547 0 : fsdev_io->u_out.lookup.fobject = vfsdev->root;
548 0 : return 0;
549 : }
550 :
551 0 : SPDK_DEBUGLOG(fsdev_aio, " name %s\n", name);
552 :
553 : /* Don't use is_safe_path_component(), allow "." and ".." for NFS export
554 : * support.
555 : */
556 0 : if (strchr(name, '/')) {
557 0 : return -EINVAL;
558 : }
559 :
560 0 : err = lo_do_lookup(vfsdev, parent_fobject, name, &fsdev_io->u_out.lookup.fobject,
561 : &fsdev_io->u_out.lookup.attr);
562 0 : if (err) {
563 0 : SPDK_DEBUGLOG(fsdev_aio, "lo_do_lookup(%s) failed with err=%d\n", name, err);
564 0 : return err;
565 : }
566 :
567 0 : return 0;
568 : }
569 :
570 : /*
571 : * Change to uid/gid of caller so that file is created with ownership of caller.
572 : */
573 : static int
574 0 : lo_change_cred(const struct lo_cred *new, struct lo_cred *old)
575 : {
576 : int res;
577 :
578 0 : old->euid = geteuid();
579 0 : old->egid = getegid();
580 :
581 0 : res = syscall(SYS_setresgid, -1, new->egid, -1);
582 0 : if (res == -1) {
583 0 : return -errno;
584 : }
585 :
586 0 : res = syscall(SYS_setresuid, -1, new->euid, -1);
587 0 : if (res == -1) {
588 0 : int errno_save = -errno;
589 :
590 0 : syscall(SYS_setresgid, -1, old->egid, -1);
591 0 : return errno_save;
592 : }
593 :
594 0 : return 0;
595 : }
596 :
597 : /* Regain Privileges */
598 : static void
599 0 : lo_restore_cred(struct lo_cred *old)
600 : {
601 : int res;
602 :
603 0 : res = syscall(SYS_setresuid, -1, old->euid, -1);
604 0 : if (res == -1) {
605 0 : SPDK_ERRLOG("seteuid(%u)", old->euid);
606 : }
607 :
608 0 : res = syscall(SYS_setresgid, -1, old->egid, -1);
609 0 : if (res == -1) {
610 0 : SPDK_ERRLOG("setegid(%u)", old->egid);
611 : }
612 0 : }
613 :
614 : static int
615 0 : lo_readdir(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
616 : {
617 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
618 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.readdir.fobject;
619 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.readdir.fhandle;
620 0 : uint64_t offset = fsdev_io->u_in.readdir.offset;
621 :
622 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
623 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
624 0 : return -EINVAL;
625 : }
626 :
627 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
628 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
629 0 : return -EINVAL;
630 : }
631 :
632 0 : if (((off_t)offset) != fhandle->dir.offset) {
633 0 : seekdir(fhandle->dir.dp, offset);
634 0 : fhandle->dir.entry = NULL;
635 0 : fhandle->dir.offset = offset;
636 : }
637 :
638 0 : while (1) {
639 : off_t nextoff;
640 : const char *name;
641 : int res;
642 :
643 0 : if (!fhandle->dir.entry) {
644 0 : errno = 0;
645 0 : fhandle->dir.entry = readdir(fhandle->dir.dp);
646 0 : if (!fhandle->dir.entry) {
647 0 : if (errno) { /* Error */
648 0 : res = -errno;
649 0 : SPDK_ERRLOG("readdir failed with err=%d", res);
650 0 : return res;
651 : } else { /* End of stream */
652 0 : break;
653 : }
654 : }
655 : }
656 :
657 0 : nextoff = fhandle->dir.entry->d_off;
658 0 : name = fhandle->dir.entry->d_name;
659 :
660 : /* Hide root's parent directory */
661 0 : if (fobject == vfsdev->root && strcmp(name, "..") == 0) {
662 0 : goto skip_entry;
663 : }
664 :
665 0 : if (is_dot_or_dotdot(name)) {
666 0 : fsdev_io->u_out.readdir.fobject = NULL;
667 0 : memset(&fsdev_io->u_out.readdir.attr, 0, sizeof(fsdev_io->u_out.readdir.attr));
668 0 : fsdev_io->u_out.readdir.attr.ino = fhandle->dir.entry->d_ino;
669 0 : fsdev_io->u_out.readdir.attr.mode = DT_DIR << 12;
670 0 : goto skip_lookup;
671 : }
672 :
673 0 : res = lo_do_lookup(vfsdev, fobject, name, &fsdev_io->u_out.readdir.fobject,
674 : &fsdev_io->u_out.readdir.attr);
675 0 : if (res) {
676 0 : SPDK_DEBUGLOG(fsdev_aio, "lo_do_lookup(%s) failed with err=%d\n", name, res);
677 0 : return res;
678 : }
679 :
680 0 : skip_lookup:
681 0 : fsdev_io->u_out.readdir.name = name;
682 0 : fsdev_io->u_out.readdir.offset = nextoff;
683 :
684 0 : res = fsdev_io->u_in.readdir.entry_cb_fn(fsdev_io, fsdev_io->internal.cb_arg);
685 0 : if (res) {
686 0 : if (fsdev_io->u_out.readdir.fobject) {
687 0 : file_object_unref(fsdev_io->u_out.readdir.fobject, 1);
688 : }
689 0 : break;
690 : }
691 :
692 0 : skip_entry:
693 0 : fhandle->dir.entry = NULL;
694 0 : fhandle->dir.offset = nextoff;
695 : }
696 :
697 0 : SPDK_DEBUGLOG(fsdev_aio, "READDIR succeeded for " FOBJECT_FMT " (fh=%p, offset=%" PRIu64 ")\n",
698 : FOBJECT_ARGS(fobject), fhandle, offset);
699 0 : return 0;
700 : }
701 :
702 : static int
703 0 : lo_forget(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
704 : {
705 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
706 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.readdir.fobject;
707 0 : uint64_t nlookup = fsdev_io->u_in.forget.nlookup;
708 :
709 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
710 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
711 0 : return -EINVAL;
712 : }
713 :
714 0 : file_object_unref(fobject, nlookup);
715 :
716 0 : return 0;
717 : }
718 :
719 : static uint32_t
720 0 : update_open_flags(struct aio_fsdev *vfsdev, uint32_t flags)
721 : {
722 : /*
723 : * With writeback cache, kernel may send read requests even
724 : * when userspace opened write-only
725 : */
726 0 : if (vfsdev->fsdev.opts.writeback_cache_enabled && (flags & O_ACCMODE) == O_WRONLY) {
727 0 : flags &= ~O_ACCMODE;
728 0 : flags |= O_RDWR;
729 : }
730 :
731 : /*
732 : * With writeback cache, O_APPEND is handled by the kernel.
733 : * This breaks atomicity (since the file may change in the
734 : * underlying filesystem, so that the kernel's idea of the
735 : * end of the file isn't accurate anymore). In this example,
736 : * we just accept that. A more rigorous filesystem may want
737 : * to return an error here
738 : */
739 0 : if (vfsdev->fsdev.opts.writeback_cache_enabled && (flags & O_APPEND)) {
740 0 : flags &= ~O_APPEND;
741 : }
742 :
743 : /*
744 : * O_DIRECT in guest should not necessarily mean bypassing page
745 : * cache on host as well. If somebody needs that behavior, it
746 : * probably should be a configuration knob in daemon.
747 : */
748 0 : flags &= ~O_DIRECT;
749 :
750 0 : return flags;
751 : }
752 :
753 : static int
754 0 : lo_open(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
755 : {
756 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
757 : int fd, saverr;
758 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.open.fobject;
759 0 : uint32_t flags = fsdev_io->u_in.open.flags;
760 : struct spdk_fsdev_file_handle *fhandle;
761 :
762 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
763 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
764 0 : return -EINVAL;
765 : }
766 :
767 0 : flags = update_open_flags(vfsdev, flags);
768 :
769 0 : fd = openat(vfsdev->proc_self_fd, fobject->fd_str, flags & ~O_NOFOLLOW);
770 0 : if (fd == -1) {
771 0 : saverr = -errno;
772 0 : SPDK_ERRLOG("openat(%d, %s, 0x%08" PRIx32 ") failed with err=%d\n",
773 : vfsdev->proc_self_fd, fobject->fd_str, flags, saverr);
774 0 : return saverr;
775 : }
776 :
777 0 : fhandle = file_handle_create(fobject, fd);
778 0 : if (!fhandle) {
779 0 : SPDK_ERRLOG("cannot create a file handle (fd=%d)\n", fd);
780 0 : close(fd);
781 0 : return -ENOMEM;
782 : }
783 :
784 0 : fsdev_io->u_out.open.fhandle = fhandle;
785 :
786 0 : SPDK_DEBUGLOG(fsdev_aio, "OPEN succeeded for " FOBJECT_FMT " (fh=%p, fd=%d)\n",
787 : FOBJECT_ARGS(fobject), fhandle, fd);
788 :
789 0 : return 0;
790 : }
791 :
792 : static int
793 0 : lo_flush(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
794 : {
795 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
796 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.flush.fobject;
797 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.flush.fhandle;
798 : int res, saverr;
799 :
800 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
801 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
802 0 : return -EINVAL;
803 : }
804 :
805 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
806 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
807 0 : return -EINVAL;
808 : }
809 :
810 0 : res = close(dup(fhandle->fd));
811 0 : if (res) {
812 0 : saverr = -errno;
813 0 : SPDK_ERRLOG("close(dup(%d)) failed for " FOBJECT_FMT " (fh=%p, err=%d)\n",
814 : fhandle->fd, FOBJECT_ARGS(fobject), fhandle, saverr);
815 0 : return saverr;
816 : }
817 :
818 0 : SPDK_DEBUGLOG(fsdev_aio, "FLUSH succeeded for " FOBJECT_FMT " (fh=%p)\n", FOBJECT_ARGS(fobject),
819 : fhandle);
820 :
821 0 : return 0;
822 : }
823 :
824 : static int
825 0 : lo_setattr(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
826 : {
827 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
828 : int saverr;
829 : int res;
830 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.setattr.fobject;
831 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.setattr.fhandle;
832 0 : uint32_t to_set = fsdev_io->u_in.setattr.to_set;
833 0 : struct spdk_fsdev_file_attr *attr = &fsdev_io->u_in.setattr.attr;
834 :
835 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
836 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
837 0 : return -EINVAL;
838 : }
839 :
840 0 : if (to_set & FSDEV_SET_ATTR_MODE) {
841 0 : if (fhandle) {
842 0 : res = fchmod(fhandle->fd, attr->mode);
843 : } else {
844 0 : res = fchmodat(vfsdev->proc_self_fd, fobject->fd_str, attr->mode, 0);
845 : }
846 0 : if (res == -1) {
847 0 : saverr = -errno;
848 0 : SPDK_ERRLOG("fchmod failed for " FOBJECT_FMT "\n", FOBJECT_ARGS(fobject));
849 0 : return saverr;
850 : }
851 : }
852 :
853 0 : if (to_set & (FSDEV_SET_ATTR_UID | FSDEV_SET_ATTR_GID)) {
854 0 : uid_t uid = (to_set & FSDEV_SET_ATTR_UID) ? attr->uid : (uid_t) -1;
855 0 : gid_t gid = (to_set & FSDEV_SET_ATTR_GID) ? attr->gid : (gid_t) -1;
856 :
857 0 : res = fchownat(fobject->fd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
858 0 : if (res == -1) {
859 0 : saverr = -errno;
860 0 : SPDK_ERRLOG("fchownat failed for " FOBJECT_FMT "\n", FOBJECT_ARGS(fobject));
861 0 : return saverr;
862 : }
863 : }
864 :
865 0 : if (to_set & FSDEV_SET_ATTR_SIZE) {
866 : int truncfd;
867 :
868 0 : if (fhandle) {
869 0 : truncfd = fhandle->fd;
870 : } else {
871 0 : truncfd = openat(vfsdev->proc_self_fd, fobject->fd_str, O_RDWR);
872 0 : if (truncfd < 0) {
873 0 : saverr = -errno;
874 0 : SPDK_ERRLOG("openat failed for " FOBJECT_FMT "\n", FOBJECT_ARGS(fobject));
875 0 : return saverr;
876 : }
877 : }
878 :
879 0 : res = ftruncate(truncfd, attr->size);
880 0 : if (!fhandle) {
881 0 : saverr = -errno;
882 0 : close(truncfd);
883 0 : errno = saverr;
884 : }
885 0 : if (res == -1) {
886 0 : saverr = -errno;
887 0 : SPDK_ERRLOG("ftruncate failed for " FOBJECT_FMT " (size=%" PRIu64 ")\n", FOBJECT_ARGS(fobject),
888 : attr->size);
889 0 : return saverr;
890 : }
891 : }
892 :
893 0 : if (to_set & (FSDEV_SET_ATTR_ATIME | FSDEV_SET_ATTR_MTIME)) {
894 0 : struct timespec tv[2];
895 :
896 0 : tv[0].tv_sec = 0;
897 0 : tv[1].tv_sec = 0;
898 0 : tv[0].tv_nsec = UTIME_OMIT;
899 0 : tv[1].tv_nsec = UTIME_OMIT;
900 :
901 0 : if (to_set & FSDEV_SET_ATTR_ATIME_NOW) {
902 0 : tv[0].tv_nsec = UTIME_NOW;
903 0 : } else if (to_set & FSDEV_SET_ATTR_ATIME) {
904 0 : tv[0].tv_sec = attr->atime;
905 0 : tv[0].tv_nsec = attr->atimensec;
906 : }
907 :
908 0 : if (to_set & FSDEV_SET_ATTR_MTIME_NOW) {
909 0 : tv[1].tv_nsec = UTIME_NOW;
910 0 : } else if (to_set & FSDEV_SET_ATTR_MTIME) {
911 0 : tv[1].tv_sec = attr->mtime;
912 0 : tv[1].tv_nsec = attr->mtimensec;
913 : }
914 :
915 0 : if (fhandle) {
916 0 : res = futimens(fhandle->fd, tv);
917 : } else {
918 0 : res = utimensat_empty(vfsdev, fobject, tv);
919 : }
920 0 : if (res == -1) {
921 0 : saverr = -errno;
922 0 : SPDK_ERRLOG("futimens/utimensat_empty failed for " FOBJECT_FMT "\n",
923 : FOBJECT_ARGS(fobject));
924 0 : return saverr;
925 : }
926 : }
927 :
928 0 : res = file_object_fill_attr(fobject, &fsdev_io->u_out.setattr.attr);
929 0 : if (res) {
930 0 : SPDK_ERRLOG("file_object_fill_attr failed for " FOBJECT_FMT "\n",
931 : FOBJECT_ARGS(fobject));
932 0 : return res;
933 : }
934 :
935 0 : SPDK_DEBUGLOG(fsdev_aio, "SETATTR succeeded for " FOBJECT_FMT "\n",
936 : FOBJECT_ARGS(fobject));
937 :
938 0 : return 0;
939 : }
940 :
941 : static int
942 0 : lo_create(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
943 : {
944 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
945 : int fd;
946 : int err;
947 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.create.parent_fobject;
948 0 : const char *name = fsdev_io->u_in.create.name;
949 0 : uint32_t mode = fsdev_io->u_in.create.mode;
950 0 : uint32_t flags = fsdev_io->u_in.create.flags;
951 0 : uint32_t umask = fsdev_io->u_in.create.umask;
952 0 : struct lo_cred old_cred, new_cred = {
953 0 : .euid = fsdev_io->u_in.create.euid,
954 0 : .egid = fsdev_io->u_in.create.egid,
955 : };
956 0 : struct spdk_fsdev_file_object *fobject;
957 : struct spdk_fsdev_file_handle *fhandle;
958 0 : struct spdk_fsdev_file_attr *attr = &fsdev_io->u_out.create.attr;
959 :
960 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, parent_fobject)) {
961 0 : SPDK_ERRLOG("Invalid parent_fobject: %p\n", parent_fobject);
962 0 : return -EINVAL;
963 : }
964 :
965 : UNUSED(umask);
966 :
967 0 : if (!is_safe_path_component(name)) {
968 0 : SPDK_ERRLOG("CREATE: %s not a safe component\n", name);
969 0 : return -EINVAL;
970 : }
971 :
972 0 : err = lo_change_cred(&new_cred, &old_cred);
973 0 : if (err) {
974 0 : SPDK_ERRLOG("CREATE: cannot change credentials\n");
975 0 : return err;
976 : }
977 :
978 0 : flags = update_open_flags(vfsdev, flags);
979 :
980 0 : fd = openat(parent_fobject->fd, name, (flags | O_CREAT) & ~O_NOFOLLOW, mode);
981 0 : err = fd == -1 ? -errno : 0;
982 0 : lo_restore_cred(&old_cred);
983 :
984 0 : if (err) {
985 0 : SPDK_ERRLOG("CREATE: openat failed with %d\n", err);
986 0 : return err;
987 : }
988 :
989 0 : err = lo_do_lookup(vfsdev, parent_fobject, name, &fobject, attr);
990 0 : if (err) {
991 0 : SPDK_ERRLOG("CREATE: lookup failed with %d\n", err);
992 0 : return err;
993 : }
994 :
995 0 : fhandle = file_handle_create(fobject, fd);
996 0 : if (!fhandle) {
997 0 : SPDK_ERRLOG("cannot create a file handle (fd=%d)\n", fd);
998 0 : close(fd);
999 0 : file_object_unref(fobject, 1);
1000 0 : return -ENOMEM;
1001 : }
1002 :
1003 0 : SPDK_DEBUGLOG(fsdev_aio, "CREATE: succeeded (name=%s " FOBJECT_FMT " fh=%p)\n",
1004 : name, FOBJECT_ARGS(fobject), fhandle);
1005 :
1006 0 : fsdev_io->u_out.create.fobject = fobject;
1007 0 : fsdev_io->u_out.create.fhandle = fhandle;
1008 :
1009 0 : return 0;
1010 : }
1011 :
1012 : static int
1013 0 : lo_release(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1014 : {
1015 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1016 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.release.fobject;
1017 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.release.fhandle;
1018 :
1019 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1020 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1021 0 : return -EINVAL;
1022 : }
1023 :
1024 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
1025 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
1026 0 : return -EINVAL;
1027 : }
1028 :
1029 0 : SPDK_DEBUGLOG(fsdev_aio, "RELEASE succeeded for " FOBJECT_FMT " fh=%p)\n",
1030 : FOBJECT_ARGS(fobject), fhandle);
1031 :
1032 0 : file_handle_delete(fhandle);
1033 :
1034 0 : return 0;
1035 : }
1036 :
1037 : static void
1038 0 : lo_read_cb(void *ctx, uint32_t data_size, int error)
1039 : {
1040 0 : struct spdk_fsdev_io *fsdev_io = ctx;
1041 0 : struct aio_fsdev_io *vfsdev_io = fsdev_to_aio_io(fsdev_io);
1042 :
1043 0 : if (vfsdev_io->aio) {
1044 0 : TAILQ_REMOVE(&vfsdev_io->ch->ios_in_progress, vfsdev_io, link);
1045 : }
1046 :
1047 0 : fsdev_io->u_out.read.data_size = data_size;
1048 :
1049 0 : spdk_fsdev_io_complete(fsdev_io, error);
1050 0 : }
1051 :
1052 : static int
1053 0 : lo_read(struct spdk_io_channel *_ch, struct spdk_fsdev_io *fsdev_io)
1054 : {
1055 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1056 0 : struct aio_io_channel *ch = spdk_io_channel_get_ctx(_ch);
1057 0 : struct aio_fsdev_io *vfsdev_io = fsdev_to_aio_io(fsdev_io);
1058 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.read.fobject;
1059 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.read.fhandle;
1060 0 : size_t size = fsdev_io->u_in.read.size;
1061 0 : uint64_t offs = fsdev_io->u_in.read.offs;
1062 0 : uint32_t flags = fsdev_io->u_in.read.flags;
1063 0 : struct iovec *outvec = fsdev_io->u_in.read.iov;
1064 0 : uint32_t outcnt = fsdev_io->u_in.read.iovcnt;
1065 :
1066 : /* we don't suport the memory domains at the moment */
1067 0 : assert(!fsdev_io->u_in.read.opts || !fsdev_io->u_in.read.opts->memory_domain);
1068 :
1069 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1070 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1071 0 : return -EINVAL;
1072 : }
1073 :
1074 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
1075 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
1076 0 : return -EINVAL;
1077 : }
1078 :
1079 : UNUSED(flags);
1080 :
1081 0 : if (!outcnt || !outvec) {
1082 0 : SPDK_ERRLOG("bad outvec: iov=%p outcnt=%" PRIu32 "\n", outvec, outcnt);
1083 0 : return -EINVAL;
1084 : }
1085 :
1086 0 : vfsdev_io->aio = spdk_aio_mgr_read(ch->mgr, lo_read_cb, fsdev_io, fhandle->fd, offs, size, outvec,
1087 : outcnt);
1088 0 : if (vfsdev_io->aio) {
1089 0 : vfsdev_io->ch = ch;
1090 0 : TAILQ_INSERT_TAIL(&ch->ios_in_progress, vfsdev_io, link);
1091 : }
1092 :
1093 0 : return IO_STATUS_ASYNC;
1094 : }
1095 :
1096 : static void
1097 0 : lo_write_cb(void *ctx, uint32_t data_size, int error)
1098 : {
1099 0 : struct spdk_fsdev_io *fsdev_io = ctx;
1100 0 : struct aio_fsdev_io *vfsdev_io = fsdev_to_aio_io(fsdev_io);
1101 :
1102 0 : if (vfsdev_io->aio) {
1103 0 : TAILQ_REMOVE(&vfsdev_io->ch->ios_in_progress, vfsdev_io, link);
1104 : }
1105 :
1106 0 : fsdev_io->u_out.write.data_size = data_size;
1107 :
1108 0 : spdk_fsdev_io_complete(fsdev_io, error);
1109 0 : }
1110 :
1111 : static int
1112 0 : lo_write(struct spdk_io_channel *_ch, struct spdk_fsdev_io *fsdev_io)
1113 : {
1114 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1115 0 : struct aio_io_channel *ch = spdk_io_channel_get_ctx(_ch);
1116 0 : struct aio_fsdev_io *vfsdev_io = fsdev_to_aio_io(fsdev_io);
1117 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.write.fobject;
1118 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.write.fhandle;
1119 0 : size_t size = fsdev_io->u_in.write.size;
1120 0 : uint64_t offs = fsdev_io->u_in.write.offs;
1121 0 : uint32_t flags = fsdev_io->u_in.write.flags;
1122 0 : const struct iovec *invec = fsdev_io->u_in.write.iov;
1123 0 : uint32_t incnt = fsdev_io->u_in.write.iovcnt;
1124 :
1125 : /* we don't suport the memory domains at the moment */
1126 0 : assert(!fsdev_io->u_in.write.opts || !fsdev_io->u_in.write.opts->memory_domain);
1127 :
1128 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1129 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1130 0 : return -EINVAL;
1131 : }
1132 :
1133 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
1134 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
1135 0 : return -EINVAL;
1136 : }
1137 :
1138 : UNUSED(flags);
1139 :
1140 0 : if (!incnt || !invec) { /* there should be at least one iovec with data */
1141 0 : SPDK_ERRLOG("bad invec: iov=%p cnt=%" PRIu32 "\n", invec, incnt);
1142 0 : return -EINVAL;
1143 : }
1144 :
1145 0 : vfsdev_io->aio = spdk_aio_mgr_write(ch->mgr, lo_write_cb, fsdev_io,
1146 : fhandle->fd, offs, size, invec, incnt);
1147 0 : if (vfsdev_io->aio) {
1148 0 : vfsdev_io->ch = ch;
1149 0 : TAILQ_INSERT_TAIL(&ch->ios_in_progress, vfsdev_io, link);
1150 : }
1151 :
1152 0 : return IO_STATUS_ASYNC;
1153 : }
1154 :
1155 : static int
1156 0 : lo_readlink(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1157 : {
1158 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1159 : int res;
1160 : char *buf;
1161 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.readlink.fobject;
1162 :
1163 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1164 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1165 0 : return -EINVAL;
1166 : }
1167 :
1168 0 : buf = malloc(PATH_MAX + 1);
1169 0 : if (!buf) {
1170 0 : SPDK_ERRLOG("malloc(%zu) failed\n", (size_t)(PATH_MAX + 1));
1171 0 : return -ENOMEM;
1172 : }
1173 :
1174 0 : res = readlinkat(fobject->fd, "", buf, PATH_MAX + 1);
1175 0 : if (res == -1) {
1176 0 : int saverr = -errno;
1177 0 : SPDK_ERRLOG("readlinkat failed for " FOBJECT_FMT " with %d\n",
1178 : FOBJECT_ARGS(fobject), saverr);
1179 0 : free(buf);
1180 0 : return saverr;
1181 : }
1182 :
1183 0 : if (((uint32_t)res) == PATH_MAX + 1) {
1184 0 : SPDK_ERRLOG("buffer is too short\n");
1185 0 : free(buf);
1186 0 : return -ENAMETOOLONG;
1187 : }
1188 :
1189 0 : buf[res] = 0;
1190 0 : fsdev_io->u_out.readlink.linkname = buf;
1191 :
1192 0 : return 0;
1193 : }
1194 :
1195 : static int
1196 0 : lo_statfs(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1197 : {
1198 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1199 : int res;
1200 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.statfs.fobject;
1201 0 : struct statvfs stbuf;
1202 :
1203 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1204 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1205 0 : return -EINVAL;
1206 : }
1207 :
1208 0 : res = fstatvfs(fobject->fd, &stbuf);
1209 0 : if (res == -1) {
1210 0 : int saverr = -errno;
1211 0 : SPDK_ERRLOG("fstatvfs failed with %d\n", saverr);
1212 0 : return saverr;
1213 : }
1214 :
1215 0 : fsdev_io->u_out.statfs.statfs.blocks = stbuf.f_blocks;
1216 0 : fsdev_io->u_out.statfs.statfs.bfree = stbuf.f_bfree;
1217 0 : fsdev_io->u_out.statfs.statfs.bavail = stbuf.f_bavail;
1218 0 : fsdev_io->u_out.statfs.statfs.files = stbuf.f_files;
1219 0 : fsdev_io->u_out.statfs.statfs.ffree = stbuf.f_ffree;
1220 0 : fsdev_io->u_out.statfs.statfs.bsize = stbuf.f_bsize;
1221 0 : fsdev_io->u_out.statfs.statfs.namelen = stbuf.f_namemax;
1222 0 : fsdev_io->u_out.statfs.statfs.frsize = stbuf.f_frsize;
1223 :
1224 0 : return 0;
1225 : }
1226 :
1227 : static int
1228 0 : lo_mknod_symlink(struct spdk_fsdev_io *fsdev_io, struct spdk_fsdev_file_object *parent_fobject,
1229 : const char *name, mode_t mode, dev_t rdev, const char *link, uid_t euid, gid_t egid,
1230 : struct spdk_fsdev_file_object **pfobject, struct spdk_fsdev_file_attr *attr)
1231 : {
1232 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1233 : int res;
1234 : int saverr;
1235 0 : struct lo_cred old_cred, new_cred = {
1236 : .euid = euid,
1237 : .egid = egid,
1238 : };
1239 :
1240 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, parent_fobject)) {
1241 0 : SPDK_ERRLOG("Invalid parent_fobject: %p\n", parent_fobject);
1242 0 : return -EINVAL;
1243 : }
1244 :
1245 0 : if (!is_safe_path_component(name)) {
1246 0 : SPDK_ERRLOG("%s isn'h safe\n", name);
1247 0 : return -EINVAL;
1248 : }
1249 :
1250 0 : res = lo_change_cred(&new_cred, &old_cred);
1251 0 : if (res) {
1252 0 : SPDK_ERRLOG("cannot change cred (err=%d)\n", res);
1253 0 : return res;
1254 : }
1255 :
1256 0 : if (S_ISDIR(mode)) {
1257 0 : res = mkdirat(parent_fobject->fd, name, mode);
1258 0 : } else if (S_ISLNK(mode)) {
1259 0 : if (link) {
1260 0 : res = symlinkat(link, parent_fobject->fd, name);
1261 : } else {
1262 0 : SPDK_ERRLOG("NULL link pointer\n");
1263 0 : errno = EINVAL;
1264 : }
1265 : } else {
1266 0 : res = mknodat(parent_fobject->fd, name, mode, rdev);
1267 : }
1268 0 : saverr = -errno;
1269 :
1270 0 : lo_restore_cred(&old_cred);
1271 :
1272 0 : if (res == -1) {
1273 0 : SPDK_ERRLOG("cannot mkdirat/symlinkat/mknodat (err=%d)\n", saverr);
1274 0 : return saverr;
1275 : }
1276 :
1277 0 : res = lo_do_lookup(vfsdev, parent_fobject, name, pfobject, attr);
1278 0 : if (res) {
1279 0 : SPDK_ERRLOG("lookup failed (err=%d)\n", res);
1280 0 : return res;
1281 : }
1282 :
1283 0 : SPDK_DEBUGLOG(fsdev_aio, "lo_mknod_symlink(" FOBJECT_FMT "/%s -> " FOBJECT_FMT "\n",
1284 : FOBJECT_ARGS(parent_fobject), name, FOBJECT_ARGS(*pfobject));
1285 :
1286 0 : return 0;
1287 : }
1288 :
1289 : static int
1290 0 : lo_mknod(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1291 : {
1292 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.mknod.parent_fobject;
1293 0 : char *name = fsdev_io->u_in.mknod.name;
1294 0 : mode_t mode = fsdev_io->u_in.mknod.mode;
1295 0 : dev_t rdev = fsdev_io->u_in.mknod.rdev;
1296 0 : uid_t euid = fsdev_io->u_in.mknod.euid;
1297 0 : gid_t egid = fsdev_io->u_in.mknod.egid;
1298 :
1299 0 : return lo_mknod_symlink(fsdev_io, parent_fobject, name, mode, rdev, NULL, euid, egid,
1300 : &fsdev_io->u_out.mknod.fobject, &fsdev_io->u_out.mknod.attr);
1301 : }
1302 :
1303 : static int
1304 0 : lo_mkdir(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1305 : {
1306 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.mkdir.parent_fobject;
1307 0 : char *name = fsdev_io->u_in.mkdir.name;
1308 0 : mode_t mode = fsdev_io->u_in.mkdir.mode;
1309 0 : uid_t euid = fsdev_io->u_in.mkdir.euid;
1310 0 : gid_t egid = fsdev_io->u_in.mkdir.egid;
1311 :
1312 0 : return lo_mknod_symlink(fsdev_io, parent_fobject, name, S_IFDIR | mode, 0, NULL, euid, egid,
1313 : &fsdev_io->u_out.mkdir.fobject, &fsdev_io->u_out.mkdir.attr);
1314 : }
1315 :
1316 : static int
1317 0 : lo_symlink(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1318 : {
1319 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.symlink.parent_fobject;
1320 0 : char *target = fsdev_io->u_in.symlink.target;
1321 0 : char *linkpath = fsdev_io->u_in.symlink.linkpath;
1322 0 : uid_t euid = fsdev_io->u_in.symlink.euid;
1323 0 : gid_t egid = fsdev_io->u_in.symlink.egid;
1324 :
1325 0 : return lo_mknod_symlink(fsdev_io, parent_fobject, target, S_IFLNK, 0, linkpath, euid, egid,
1326 : &fsdev_io->u_out.symlink.fobject, &fsdev_io->u_out.symlink.attr);
1327 : }
1328 :
1329 : static int
1330 0 : lo_do_unlink(struct aio_fsdev *vfsdev, struct spdk_fsdev_file_object *parent_fobject,
1331 : const char *name, bool is_dir)
1332 : {
1333 : /* fobject must be initialized to avoid a scan-build false positive */
1334 0 : struct spdk_fsdev_file_object *fobject = NULL;
1335 : int res;
1336 :
1337 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, parent_fobject)) {
1338 0 : SPDK_ERRLOG("Invalid parent_fobject: %p\n", parent_fobject);
1339 0 : return -EINVAL;
1340 : }
1341 :
1342 0 : if (!is_safe_path_component(name)) {
1343 0 : SPDK_ERRLOG("%s isn't safe\n", name);
1344 0 : return -EINVAL;
1345 : }
1346 :
1347 0 : res = lo_do_lookup(vfsdev, parent_fobject, name, &fobject, NULL);
1348 0 : if (res) {
1349 0 : SPDK_ERRLOG("can't find '%s' under " FOBJECT_FMT "\n", name, FOBJECT_ARGS(parent_fobject));
1350 0 : return -EIO;
1351 : }
1352 :
1353 0 : res = unlinkat(parent_fobject->fd, name, is_dir ? AT_REMOVEDIR : 0);
1354 0 : if (res) {
1355 0 : res = -errno;
1356 0 : SPDK_WARNLOG("unlinkat(" FOBJECT_FMT " %s) failed (err=%d)\n",
1357 : FOBJECT_ARGS(parent_fobject), name, res);
1358 : }
1359 :
1360 0 : file_object_unref(fobject, 1);
1361 0 : return res;
1362 : }
1363 :
1364 : static int
1365 0 : lo_unlink(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1366 : {
1367 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1368 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.unlink.parent_fobject;
1369 0 : char *name = fsdev_io->u_in.unlink.name;
1370 :
1371 0 : return lo_do_unlink(vfsdev, parent_fobject, name, false);
1372 : }
1373 :
1374 : static int
1375 0 : lo_rmdir(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1376 : {
1377 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1378 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.rmdir.parent_fobject;
1379 0 : char *name = fsdev_io->u_in.rmdir.name;
1380 :
1381 0 : return lo_do_unlink(vfsdev, parent_fobject, name, true);
1382 : }
1383 :
1384 : static int
1385 0 : lo_rename(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1386 : {
1387 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1388 : int res, saverr;
1389 : /* old_fobject must be initialized to avoid a scan-build false positive */
1390 0 : struct spdk_fsdev_file_object *old_fobject = NULL;
1391 0 : struct spdk_fsdev_file_object *parent_fobject = fsdev_io->u_in.rename.parent_fobject;
1392 0 : char *name = fsdev_io->u_in.rename.name;
1393 0 : struct spdk_fsdev_file_object *new_parent_fobject = fsdev_io->u_in.rename.new_parent_fobject;
1394 0 : char *new_name = fsdev_io->u_in.rename.new_name;
1395 0 : uint32_t flags = fsdev_io->u_in.rename.flags;
1396 :
1397 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, parent_fobject)) {
1398 0 : SPDK_ERRLOG("Invalid parent_fobject: %p\n", parent_fobject);
1399 0 : return -EINVAL;
1400 : }
1401 :
1402 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, new_parent_fobject)) {
1403 0 : SPDK_ERRLOG("Invalid new_parent_fobject: %p\n", new_parent_fobject);
1404 0 : return -EINVAL;
1405 : }
1406 :
1407 0 : if (!is_safe_path_component(name)) {
1408 0 : SPDK_ERRLOG("name '%s' isn't safe\n", name);
1409 0 : return -EINVAL;
1410 : }
1411 :
1412 0 : if (!is_safe_path_component(new_name)) {
1413 0 : SPDK_ERRLOG("newname '%s' isn't safe\n", new_name);
1414 0 : return -EINVAL;
1415 : }
1416 :
1417 0 : res = lo_do_lookup(vfsdev, parent_fobject, name, &old_fobject, NULL);
1418 0 : if (res) {
1419 0 : SPDK_ERRLOG("can't find '%s' under " FOBJECT_FMT "\n", name, FOBJECT_ARGS(parent_fobject));
1420 0 : return -EIO;
1421 : }
1422 :
1423 0 : saverr = 0;
1424 0 : if (flags) {
1425 : #ifndef SYS_renameat2
1426 : SPDK_ERRLOG("flags are not supported\n");
1427 : return -ENOTSUP;
1428 : #else
1429 0 : res = syscall(SYS_renameat2, parent_fobject->fd, name, new_parent_fobject->fd,
1430 : new_name, flags);
1431 0 : if (res == -1 && errno == ENOSYS) {
1432 0 : SPDK_ERRLOG("SYS_renameat2 returned ENOSYS\n");
1433 0 : saverr = -EINVAL;
1434 0 : } else if (res == -1) {
1435 0 : saverr = -errno;
1436 0 : SPDK_ERRLOG("SYS_renameat2 failed (err=%d))\n", saverr);
1437 : }
1438 : #endif
1439 : } else {
1440 0 : res = renameat(parent_fobject->fd, name, new_parent_fobject->fd, new_name);
1441 0 : if (res == -1) {
1442 0 : saverr = -errno;
1443 0 : SPDK_ERRLOG("renameat failed (err=%d)\n", saverr);
1444 : }
1445 : }
1446 :
1447 0 : file_object_unref(old_fobject, 1);
1448 :
1449 0 : return saverr;
1450 : }
1451 :
1452 : static int
1453 0 : linkat_empty_nofollow(struct aio_fsdev *vfsdev, struct spdk_fsdev_file_object *fobject, int dfd,
1454 : const char *name)
1455 : {
1456 : int res;
1457 :
1458 0 : if (fobject->is_symlink) {
1459 0 : res = linkat(fobject->fd, "", dfd, name, AT_EMPTY_PATH);
1460 0 : if (res == -1 && (errno == ENOENT || errno == EINVAL)) {
1461 : /* Sorry, no race free way to hard-link a symlink. */
1462 0 : errno = EPERM;
1463 : }
1464 : } else {
1465 0 : res = linkat(vfsdev->proc_self_fd, fobject->fd_str, dfd, name, AT_SYMLINK_FOLLOW);
1466 : }
1467 :
1468 0 : return res;
1469 : }
1470 :
1471 : static int
1472 0 : lo_link(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1473 : {
1474 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1475 : int res;
1476 : int saverr;
1477 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.link.fobject;
1478 0 : struct spdk_fsdev_file_object *new_parent_fobject = fsdev_io->u_in.link.new_parent_fobject;
1479 0 : char *name = fsdev_io->u_in.link.name;
1480 :
1481 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1482 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1483 0 : return -EINVAL;
1484 : }
1485 :
1486 0 : if (!is_safe_path_component(name)) {
1487 0 : SPDK_ERRLOG("%s is not a safe component\n", name);
1488 0 : return -EINVAL;
1489 : }
1490 :
1491 0 : res = linkat_empty_nofollow(vfsdev, fobject, new_parent_fobject->fd, name);
1492 0 : if (res == -1) {
1493 0 : saverr = -errno;
1494 0 : SPDK_ERRLOG("linkat_empty_nofollow failed " FOBJECT_FMT " -> " FOBJECT_FMT " name=%s (err=%d)\n",
1495 : FOBJECT_ARGS(fobject), FOBJECT_ARGS(new_parent_fobject), name, saverr);
1496 0 : return saverr;
1497 : }
1498 :
1499 0 : res = lo_do_lookup(vfsdev, new_parent_fobject, name, &fsdev_io->u_out.link.fobject,
1500 : &fsdev_io->u_out.link.attr);
1501 0 : if (res) {
1502 0 : SPDK_ERRLOG("lookup failed (err=%d)\n", res);
1503 0 : return res;
1504 : }
1505 :
1506 0 : SPDK_DEBUGLOG(fsdev_aio, "LINK succeeded for " FOBJECT_FMT " -> " FOBJECT_FMT " name=%s\n",
1507 : FOBJECT_ARGS(fobject), FOBJECT_ARGS(fsdev_io->u_out.link.fobject), name);
1508 :
1509 0 : return 0;
1510 : }
1511 :
1512 : static int
1513 0 : lo_fsync(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1514 : {
1515 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1516 : int res, saverr, fd;
1517 0 : char *buf;
1518 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.fsync.fobject;
1519 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.fsync.fhandle;
1520 0 : bool datasync = fsdev_io->u_in.fsync.datasync;
1521 :
1522 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1523 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1524 0 : return -EINVAL;
1525 : }
1526 :
1527 0 : if (!fhandle) {
1528 0 : res = asprintf(&buf, "%i", fobject->fd);
1529 0 : if (res == -1) {
1530 0 : saverr = -errno;
1531 0 : SPDK_ERRLOG("asprintf failed (errno=%d)\n", saverr);
1532 0 : return saverr;
1533 : }
1534 :
1535 0 : fd = openat(vfsdev->proc_self_fd, buf, O_RDWR);
1536 0 : saverr = -errno;
1537 0 : free(buf);
1538 0 : if (fd == -1) {
1539 0 : SPDK_ERRLOG("openat failed (errno=%d)\n", saverr);
1540 0 : return saverr;
1541 : }
1542 : } else {
1543 0 : fd = fhandle->fd;
1544 : }
1545 :
1546 0 : if (datasync) {
1547 0 : res = fdatasync(fd);
1548 : } else {
1549 0 : res = fsync(fd);
1550 : }
1551 :
1552 0 : saverr = -errno;
1553 0 : if (!fhandle) {
1554 0 : close(fd);
1555 : }
1556 :
1557 0 : if (res == -1) {
1558 0 : SPDK_ERRLOG("fdatasync/fsync failed for " FOBJECT_FMT " fh=%p (err=%d)\n",
1559 : FOBJECT_ARGS(fobject), fhandle, saverr);
1560 0 : return saverr;
1561 : }
1562 :
1563 0 : SPDK_DEBUGLOG(fsdev_aio, "FSYNC succeeded for " FOBJECT_FMT " fh=%p\n",
1564 : FOBJECT_ARGS(fobject), fhandle);
1565 :
1566 0 : return 0;
1567 : }
1568 :
1569 : static int
1570 0 : lo_setxattr(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1571 : {
1572 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1573 : ssize_t ret;
1574 : int saverr;
1575 0 : int fd = -1;
1576 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.setxattr.fobject;
1577 0 : char *name = fsdev_io->u_in.setxattr.name;
1578 0 : char *value = fsdev_io->u_in.setxattr.value;
1579 0 : uint32_t size = fsdev_io->u_in.setxattr.size;
1580 0 : uint32_t flags = fsdev_io->u_in.setxattr.flags;
1581 :
1582 0 : if (!vfsdev->xattr_enabled) {
1583 0 : SPDK_INFOLOG(fsdev_aio, "xattr is disabled by config\n");
1584 0 : return -ENOSYS;
1585 : }
1586 :
1587 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1588 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1589 0 : return -EINVAL;
1590 : }
1591 :
1592 0 : if (fobject->is_symlink) {
1593 : /* Sorry, no race free way to removexattr on symlink. */
1594 0 : SPDK_ERRLOG("cannot set xattr for symlink\n");
1595 0 : return -EPERM;
1596 : }
1597 :
1598 0 : fd = openat(vfsdev->proc_self_fd, fobject->fd_str, O_RDWR);
1599 0 : if (fd < 0) {
1600 0 : saverr = -errno;
1601 0 : SPDK_ERRLOG("openat failed with errno=%d\n", saverr);
1602 0 : return saverr;
1603 : }
1604 :
1605 0 : ret = fsetxattr(fd, name, value, size, flags);
1606 0 : saverr = -errno;
1607 0 : close(fd);
1608 0 : if (ret == -1) {
1609 0 : if (saverr == -ENOTSUP) {
1610 0 : SPDK_INFOLOG(fsdev_aio, "flistxattr: extended attributes are not supported or disabled\n");
1611 : } else {
1612 0 : SPDK_ERRLOG("flistxattr failed with errno=%d\n", saverr);
1613 : }
1614 0 : return saverr;
1615 : }
1616 :
1617 0 : SPDK_DEBUGLOG(fsdev_aio,
1618 : "SETXATTR succeeded for " FOBJECT_FMT " name=%s value=%s size=%" PRIu32 "flags=0x%x" PRIx32 "\n",
1619 : FOBJECT_ARGS(fobject), name, value, size, flags);
1620 :
1621 0 : return 0;
1622 : }
1623 :
1624 : static int
1625 0 : lo_getxattr(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1626 : {
1627 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1628 : ssize_t ret;
1629 : int saverr;
1630 0 : int fd = -1;
1631 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.getxattr.fobject;
1632 0 : char *name = fsdev_io->u_in.getxattr.name;
1633 0 : void *buffer = fsdev_io->u_in.getxattr.buffer;
1634 0 : size_t size = fsdev_io->u_in.getxattr.size;
1635 :
1636 0 : if (!vfsdev->xattr_enabled) {
1637 0 : SPDK_INFOLOG(fsdev_aio, "xattr is disabled by config\n");
1638 0 : return -ENOSYS;
1639 : }
1640 :
1641 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1642 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1643 0 : return -EINVAL;
1644 : }
1645 :
1646 0 : if (fobject->is_symlink) {
1647 : /* Sorry, no race free way to getxattr on symlink. */
1648 0 : SPDK_ERRLOG("cannot get xattr for symlink\n");
1649 0 : return -EPERM;
1650 : }
1651 :
1652 0 : fd = openat(vfsdev->proc_self_fd, fobject->fd_str, O_RDWR);
1653 0 : if (fd < 0) {
1654 0 : saverr = -errno;
1655 0 : SPDK_ERRLOG("openat failed with errno=%d\n", saverr);
1656 0 : return saverr;
1657 : }
1658 :
1659 0 : ret = fgetxattr(fd, name, buffer, size);
1660 0 : saverr = -errno;
1661 0 : close(fd);
1662 0 : if (ret == -1) {
1663 0 : if (saverr == -ENODATA) {
1664 0 : SPDK_INFOLOG(fsdev_aio, "fgetxattr: no extended attribute '%s' found\n", name);
1665 0 : } else if (saverr == -ENOTSUP) {
1666 0 : SPDK_INFOLOG(fsdev_aio, "fgetxattr: extended attributes are not supported or disabled\n");
1667 : } else {
1668 0 : SPDK_ERRLOG("fgetxattr failed with errno=%d\n", saverr);
1669 : }
1670 0 : return saverr;
1671 : }
1672 :
1673 0 : fsdev_io->u_out.getxattr.value_size = ret;
1674 :
1675 0 : SPDK_DEBUGLOG(fsdev_aio,
1676 : "GETXATTR succeeded for " FOBJECT_FMT " name=%s value=%s value_size=%zd\n",
1677 : FOBJECT_ARGS(fobject), name, (char *)buffer, ret);
1678 :
1679 0 : return 0;
1680 : }
1681 :
1682 : static int
1683 0 : lo_listxattr(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1684 : {
1685 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1686 : ssize_t ret;
1687 : int saverr;
1688 0 : int fd = -1;
1689 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.listxattr.fobject;
1690 0 : char *buffer = fsdev_io->u_in.listxattr.buffer;
1691 0 : size_t size = fsdev_io->u_in.listxattr.size;
1692 :
1693 0 : if (!vfsdev->xattr_enabled) {
1694 0 : SPDK_INFOLOG(fsdev_aio, "xattr is disabled by config\n");
1695 0 : return -ENOSYS;
1696 : }
1697 :
1698 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1699 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1700 0 : return -EINVAL;
1701 : }
1702 :
1703 0 : if (fobject->is_symlink) {
1704 : /* Sorry, no race free way to listxattr on symlink. */
1705 0 : SPDK_ERRLOG("cannot list xattr for symlink\n");
1706 0 : return -EPERM;
1707 : }
1708 :
1709 0 : fd = openat(vfsdev->proc_self_fd, fobject->fd_str, O_RDONLY);
1710 0 : if (fd < 0) {
1711 0 : saverr = -errno;
1712 0 : SPDK_ERRLOG("openat failed with errno=%d\n", saverr);
1713 0 : return saverr;
1714 : }
1715 :
1716 0 : ret = flistxattr(fd, buffer, size);
1717 0 : saverr = -errno;
1718 0 : close(fd);
1719 0 : if (ret == -1) {
1720 0 : if (saverr == -ENOTSUP) {
1721 0 : SPDK_INFOLOG(fsdev_aio, "flistxattr: extended attributes are not supported or disabled\n");
1722 : } else {
1723 0 : SPDK_ERRLOG("flistxattr failed with errno=%d\n", saverr);
1724 : }
1725 0 : return saverr;
1726 : }
1727 :
1728 0 : fsdev_io->u_out.listxattr.data_size = ret;
1729 0 : fsdev_io->u_out.listxattr.size_only = (size == 0);
1730 :
1731 0 : SPDK_DEBUGLOG(fsdev_aio, "LISTXATTR succeeded for " FOBJECT_FMT " data_size=%zu\n",
1732 : FOBJECT_ARGS(fobject), ret);
1733 :
1734 0 : return 0;
1735 : }
1736 :
1737 : static int
1738 0 : lo_removexattr(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1739 : {
1740 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1741 : ssize_t ret;
1742 : int saverr;
1743 0 : int fd = -1;
1744 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.removexattr.fobject;
1745 0 : char *name = fsdev_io->u_in.removexattr.name;
1746 :
1747 0 : if (!vfsdev->xattr_enabled) {
1748 0 : SPDK_INFOLOG(fsdev_aio, "xattr is disabled by config\n");
1749 0 : return -ENOSYS;
1750 : }
1751 :
1752 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1753 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1754 0 : return -EINVAL;
1755 : }
1756 :
1757 0 : if (fobject->is_symlink) {
1758 : /* Sorry, no race free way to setxattr on symlink. */
1759 0 : SPDK_ERRLOG("cannot list xattr for symlink\n");
1760 0 : return -EPERM;
1761 : }
1762 :
1763 0 : fd = openat(vfsdev->proc_self_fd, fobject->fd_str, O_RDONLY);
1764 0 : if (fd < 0) {
1765 0 : saverr = -errno;
1766 0 : SPDK_ERRLOG("openat failed with errno=%d\n", saverr);
1767 0 : return saverr;
1768 : }
1769 :
1770 0 : ret = fremovexattr(fd, name);
1771 0 : saverr = -errno;
1772 0 : close(fd);
1773 0 : if (ret == -1) {
1774 0 : if (saverr == -ENODATA) {
1775 0 : SPDK_INFOLOG(fsdev_aio, "fremovexattr: no extended attribute '%s' found\n", name);
1776 0 : } else if (saverr == -ENOTSUP) {
1777 0 : SPDK_INFOLOG(fsdev_aio, "fremovexattr: extended attributes are not supported or disabled\n");
1778 : } else {
1779 0 : SPDK_ERRLOG("fremovexattr failed with errno=%d\n", saverr);
1780 : }
1781 0 : return saverr;
1782 : }
1783 :
1784 0 : SPDK_DEBUGLOG(fsdev_aio, "REMOVEXATTR succeeded for " FOBJECT_FMT " name=%s\n",
1785 : FOBJECT_ARGS(fobject), name);
1786 :
1787 0 : return 0;
1788 : }
1789 :
1790 : static int
1791 0 : lo_fsyncdir(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1792 : {
1793 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1794 : int res;
1795 0 : int saverr = 0;
1796 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.fsyncdir.fobject;
1797 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.fsyncdir.fhandle;
1798 0 : bool datasync = fsdev_io->u_in.fsyncdir.datasync;
1799 :
1800 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1801 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1802 0 : return -EINVAL;
1803 : }
1804 :
1805 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
1806 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
1807 0 : return -EINVAL;
1808 : }
1809 :
1810 0 : if (datasync) {
1811 0 : res = fdatasync(fhandle->fd);
1812 : } else {
1813 0 : res = fsync(fhandle->fd);
1814 : }
1815 :
1816 0 : if (res == -1) {
1817 0 : saverr = -errno;
1818 0 : SPDK_ERRLOG("%s failed for fh=%p with err=%d\n",
1819 : datasync ? "fdatasync" : "fsync", fhandle, saverr);
1820 0 : return saverr;
1821 : }
1822 :
1823 0 : SPDK_DEBUGLOG(fsdev_aio, "FSYNCDIR succeeded for " FOBJECT_FMT " fh=%p datasync=%d\n",
1824 : FOBJECT_ARGS(fobject), fhandle, datasync);
1825 :
1826 0 : return 0;
1827 : }
1828 :
1829 : static int
1830 0 : lo_flock(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1831 : {
1832 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1833 : int res;
1834 0 : int saverr = 0;
1835 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.flock.fobject;
1836 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.flock.fhandle;
1837 0 : int operation = fsdev_io->u_in.flock.operation;
1838 :
1839 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1840 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1841 0 : return -EINVAL;
1842 : }
1843 :
1844 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
1845 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
1846 0 : return -EINVAL;
1847 : }
1848 :
1849 0 : res = flock(fhandle->fd, operation | LOCK_NB);
1850 0 : if (res == -1) {
1851 0 : saverr = -errno;
1852 0 : SPDK_ERRLOG("flock failed for fh=%p with err=%d\n", fhandle, saverr);
1853 0 : return saverr;
1854 : }
1855 :
1856 0 : SPDK_DEBUGLOG(fsdev_aio, "FLOCK succeeded for " FOBJECT_FMT " fh=%p operation=%d\n",
1857 : FOBJECT_ARGS(fobject), fhandle, operation);
1858 :
1859 0 : return 0;
1860 : }
1861 :
1862 : static int
1863 0 : lo_fallocate(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1864 : {
1865 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1866 : int err;
1867 0 : struct spdk_fsdev_file_object *fobject = fsdev_io->u_in.fallocate.fobject;
1868 0 : struct spdk_fsdev_file_handle *fhandle = fsdev_io->u_in.fallocate.fhandle;
1869 0 : uint32_t mode = fsdev_io->u_in.fallocate.mode;
1870 0 : uint64_t offset = fsdev_io->u_in.fallocate.offset;
1871 0 : uint64_t length = fsdev_io->u_in.fallocate.length;
1872 :
1873 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject)) {
1874 0 : SPDK_ERRLOG("Invalid fobject: %p\n", fobject);
1875 0 : return -EINVAL;
1876 : }
1877 :
1878 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle)) {
1879 0 : SPDK_ERRLOG("Invalid fhandle: %p\n", fhandle);
1880 0 : return -EINVAL;
1881 : }
1882 :
1883 0 : if (mode) {
1884 0 : SPDK_ERRLOG("non-zero mode is not suppored\n");
1885 0 : return -EOPNOTSUPP;
1886 : }
1887 :
1888 0 : err = posix_fallocate(fhandle->fd, offset, length);
1889 0 : if (err) {
1890 0 : SPDK_ERRLOG("posix_fallocate failed for fh=%p with err=%d\n",
1891 : fhandle, err);
1892 : }
1893 :
1894 0 : SPDK_DEBUGLOG(fsdev_aio,
1895 : "FALLOCATE returns %d for " FOBJECT_FMT " fh=%p offset=%" PRIu64 " length=%" PRIu64 "\n",
1896 : err, FOBJECT_ARGS(fobject), fhandle, offset, length);
1897 0 : return err;
1898 : }
1899 :
1900 : static int
1901 0 : lo_copy_file_range(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
1902 : {
1903 : #ifdef SPDK_CONFIG_COPY_FILE_RANGE
1904 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev_io->fsdev);
1905 : ssize_t res;
1906 0 : int saverr = 0;
1907 0 : struct spdk_fsdev_file_object *fobject_in = fsdev_io->u_in.copy_file_range.fobject_in;
1908 0 : struct spdk_fsdev_file_handle *fhandle_in = fsdev_io->u_in.copy_file_range.fhandle_in;
1909 0 : off_t off_in = fsdev_io->u_in.copy_file_range.off_in;
1910 0 : struct spdk_fsdev_file_object *fobject_out = fsdev_io->u_in.copy_file_range.fobject_out;
1911 0 : struct spdk_fsdev_file_handle *fhandle_out = fsdev_io->u_in.copy_file_range.fhandle_out;
1912 0 : off_t off_out = fsdev_io->u_in.copy_file_range.off_out;
1913 0 : size_t len = fsdev_io->u_in.copy_file_range.len;
1914 0 : uint32_t flags = fsdev_io->u_in.copy_file_range.flags;
1915 :
1916 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject_in)) {
1917 0 : SPDK_ERRLOG("Invalid fobject_in: %p\n", fobject_in);
1918 0 : return -EINVAL;
1919 : }
1920 :
1921 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle_in)) {
1922 0 : SPDK_ERRLOG("Invalid fhandle_in: %p\n", fhandle_in);
1923 0 : return -EINVAL;
1924 : }
1925 :
1926 0 : if (!fsdev_aio_is_valid_fobject(vfsdev, fobject_out)) {
1927 0 : SPDK_ERRLOG("Invalid fobject_out: %p\n", fobject_out);
1928 0 : return -EINVAL;
1929 : }
1930 :
1931 0 : if (!fsdev_aio_is_valid_fhandle(vfsdev, fhandle_out)) {
1932 0 : SPDK_ERRLOG("Invalid fhandle_out: %p\n", fhandle_out);
1933 0 : return -EINVAL;
1934 : }
1935 :
1936 0 : res = copy_file_range(fhandle_in->fd, &off_in, fhandle_out->fd, &off_out, len, flags);
1937 0 : if (res < 0) {
1938 0 : saverr = -errno;
1939 0 : SPDK_ERRLOG("copy_file_range failed with err=%d\n", saverr);
1940 0 : return saverr;
1941 : }
1942 :
1943 0 : SPDK_DEBUGLOG(fsdev_aio,
1944 : "COPY_FILE_RANGE succeeded for " FOBJECT_FMT " fh=%p offset=%" PRIu64 " -> " FOBJECT_FMT
1945 : " fh=%p offset=%" PRIu64 " (len-%zu flags=0x%" PRIx32 ")\n",
1946 : FOBJECT_ARGS(fobject_in), fhandle_in, (uint64_t)off_in, FOBJECT_ARGS(fobject_out), fhandle_out,
1947 : (uint64_t)off_out, len, flags);
1948 :
1949 0 : return 0;
1950 : #else
1951 : return -ENOSYS;
1952 : #endif
1953 : }
1954 :
1955 : static int
1956 0 : lo_abort(struct spdk_io_channel *_ch, struct spdk_fsdev_io *fsdev_io)
1957 : {
1958 0 : struct aio_io_channel *ch = spdk_io_channel_get_ctx(_ch);
1959 : struct aio_fsdev_io *vfsdev_io;
1960 0 : uint64_t unique_to_abort = fsdev_io->u_in.abort.unique_to_abort;
1961 :
1962 0 : TAILQ_FOREACH(vfsdev_io, &ch->ios_in_progress, link) {
1963 0 : struct spdk_fsdev_io *_fsdev_io = aio_to_fsdev_io(vfsdev_io);
1964 0 : if (spdk_fsdev_io_get_unique(_fsdev_io) == unique_to_abort) {
1965 0 : spdk_aio_mgr_cancel(ch->mgr, vfsdev_io->aio);
1966 0 : return 0;
1967 : }
1968 : }
1969 :
1970 0 : return 0;
1971 : }
1972 :
1973 : static int
1974 0 : aio_io_poll(void *arg)
1975 : {
1976 0 : struct aio_io_channel *ch = arg;
1977 :
1978 0 : spdk_aio_mgr_poll(ch->mgr);
1979 :
1980 0 : return SPDK_POLLER_IDLE;
1981 : }
1982 :
1983 : static int
1984 0 : aio_fsdev_create_cb(void *io_device, void *ctx_buf)
1985 : {
1986 0 : struct aio_io_channel *ch = ctx_buf;
1987 0 : struct spdk_thread *thread = spdk_get_thread();
1988 :
1989 0 : ch->mgr = spdk_aio_mgr_create(MAX_AIOS);
1990 0 : if (!ch->mgr) {
1991 0 : SPDK_ERRLOG("aoi manager init for failed (thread=%s)\n", spdk_thread_get_name(thread));
1992 0 : return -ENOMEM;
1993 : }
1994 :
1995 0 : ch->poller = SPDK_POLLER_REGISTER(aio_io_poll, ch, 0);
1996 0 : TAILQ_INIT(&ch->ios_in_progress);
1997 :
1998 0 : SPDK_DEBUGLOG(fsdev_aio, "Created aio fsdev IO channel: thread %s, thread id %" PRIu64
1999 : "\n",
2000 : spdk_thread_get_name(thread), spdk_thread_get_id(thread));
2001 0 : return 0;
2002 : }
2003 :
2004 : static void
2005 0 : aio_fsdev_destroy_cb(void *io_device, void *ctx_buf)
2006 : {
2007 0 : struct aio_io_channel *ch = ctx_buf;
2008 0 : struct spdk_thread *thread = spdk_get_thread();
2009 :
2010 : UNUSED(thread);
2011 :
2012 0 : spdk_poller_unregister(&ch->poller);
2013 0 : spdk_aio_mgr_delete(ch->mgr);
2014 :
2015 0 : SPDK_DEBUGLOG(fsdev_aio, "Destroyed aio fsdev IO channel: thread %s, thread id %" PRIu64
2016 : "\n",
2017 : spdk_thread_get_name(thread), spdk_thread_get_id(thread));
2018 0 : }
2019 :
2020 : static int
2021 0 : fsdev_aio_initialize(void)
2022 : {
2023 : /*
2024 : * We need to pick some unique address as our "io device" - so just use the
2025 : * address of the global tailq.
2026 : */
2027 0 : spdk_io_device_register(&g_aio_fsdev_head,
2028 : aio_fsdev_create_cb, aio_fsdev_destroy_cb,
2029 : sizeof(struct aio_io_channel), "aio_fsdev");
2030 :
2031 0 : return 0;
2032 : }
2033 :
2034 : static void
2035 0 : _fsdev_aio_finish_cb(void *arg)
2036 : {
2037 : /* @todo: handle async module fini */
2038 : /* spdk_fsdev_module_fini_done(); */
2039 0 : }
2040 :
2041 : static void
2042 0 : fsdev_aio_finish(void)
2043 : {
2044 0 : spdk_io_device_unregister(&g_aio_fsdev_head, _fsdev_aio_finish_cb);
2045 0 : }
2046 :
2047 : static int
2048 0 : fsdev_aio_get_ctx_size(void)
2049 : {
2050 0 : return sizeof(struct aio_fsdev_io);
2051 : }
2052 :
2053 : static struct spdk_fsdev_module aio_fsdev_module = {
2054 : .name = "aio",
2055 : .module_init = fsdev_aio_initialize,
2056 : .module_fini = fsdev_aio_finish,
2057 : .get_ctx_size = fsdev_aio_get_ctx_size,
2058 : };
2059 :
2060 0 : SPDK_FSDEV_MODULE_REGISTER(aio, &aio_fsdev_module);
2061 :
2062 : static void
2063 0 : fsdev_aio_free(struct aio_fsdev *vfsdev)
2064 : {
2065 0 : if (vfsdev->proc_self_fd != -1) {
2066 0 : close(vfsdev->proc_self_fd);
2067 : }
2068 :
2069 0 : if (vfsdev->root) {
2070 0 : int destroyed = file_object_unref(vfsdev->root, 1);
2071 0 : assert(destroyed == 0);
2072 : UNUSED(destroyed);
2073 :
2074 : }
2075 :
2076 0 : free(vfsdev->fsdev.name);
2077 0 : free(vfsdev->root_path);
2078 :
2079 0 : free(vfsdev);
2080 0 : }
2081 :
2082 : static void
2083 0 : fsdev_free_leafs(struct spdk_fsdev_file_object *fobject)
2084 : {
2085 0 : while (!TAILQ_EMPTY(&fobject->handles)) {
2086 0 : struct spdk_fsdev_file_handle *fhandle = TAILQ_FIRST(&fobject->handles);
2087 0 : file_handle_delete(fhandle);
2088 : #ifdef __clang_analyzer__
2089 : /*
2090 : * scan-build fails to comprehend that file_handle_delete() removes the fhandle
2091 : * from the queue, so it thinks it's remained accessible and throws the "Use of
2092 : * memory after it is freed" error here.
2093 : * The loop below "teaches" the scan-build that the freed fhandle is not on the
2094 : * list anymore and supresses the error in this way.
2095 : */
2096 : struct spdk_fsdev_file_handle *tmp;
2097 : TAILQ_FOREACH(tmp, &fobject->handles, link) {
2098 : assert(tmp != fhandle);
2099 : }
2100 : #endif
2101 : }
2102 :
2103 0 : while (!TAILQ_EMPTY(&fobject->leafs)) {
2104 0 : struct spdk_fsdev_file_object *leaf_fobject = TAILQ_FIRST(&fobject->leafs);
2105 0 : fsdev_free_leafs(leaf_fobject);
2106 : }
2107 :
2108 0 : if (fobject->refcount) {
2109 : /* if still referenced - zero refcount */
2110 0 : int res = file_object_unref(fobject, fobject->refcount);
2111 0 : assert(res == 0);
2112 : UNUSED(res);
2113 : }
2114 0 : }
2115 :
2116 : static int
2117 0 : fsdev_aio_destruct(void *ctx)
2118 : {
2119 0 : struct aio_fsdev *vfsdev = ctx;
2120 :
2121 0 : TAILQ_REMOVE(&g_aio_fsdev_head, vfsdev, tailq);
2122 :
2123 0 : fsdev_free_leafs(vfsdev->root);
2124 0 : vfsdev->root = NULL;
2125 :
2126 0 : pthread_mutex_destroy(&vfsdev->mutex);
2127 :
2128 0 : fsdev_aio_free(vfsdev);
2129 0 : return 0;
2130 : }
2131 :
2132 : typedef int (*fsdev_op_handler_func)(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io);
2133 :
2134 : static fsdev_op_handler_func handlers[] = {
2135 : [SPDK_FSDEV_IO_LOOKUP] = lo_lookup,
2136 : [SPDK_FSDEV_IO_FORGET] = lo_forget,
2137 : [SPDK_FSDEV_IO_GETATTR] = lo_getattr,
2138 : [SPDK_FSDEV_IO_SETATTR] = lo_setattr,
2139 : [SPDK_FSDEV_IO_READLINK] = lo_readlink,
2140 : [SPDK_FSDEV_IO_SYMLINK] = lo_symlink,
2141 : [SPDK_FSDEV_IO_MKNOD] = lo_mknod,
2142 : [SPDK_FSDEV_IO_MKDIR] = lo_mkdir,
2143 : [SPDK_FSDEV_IO_UNLINK] = lo_unlink,
2144 : [SPDK_FSDEV_IO_RMDIR] = lo_rmdir,
2145 : [SPDK_FSDEV_IO_RENAME] = lo_rename,
2146 : [SPDK_FSDEV_IO_LINK] = lo_link,
2147 : [SPDK_FSDEV_IO_OPEN] = lo_open,
2148 : [SPDK_FSDEV_IO_READ] = lo_read,
2149 : [SPDK_FSDEV_IO_WRITE] = lo_write,
2150 : [SPDK_FSDEV_IO_STATFS] = lo_statfs,
2151 : [SPDK_FSDEV_IO_RELEASE] = lo_release,
2152 : [SPDK_FSDEV_IO_FSYNC] = lo_fsync,
2153 : [SPDK_FSDEV_IO_SETXATTR] = lo_setxattr,
2154 : [SPDK_FSDEV_IO_GETXATTR] = lo_getxattr,
2155 : [SPDK_FSDEV_IO_LISTXATTR] = lo_listxattr,
2156 : [SPDK_FSDEV_IO_REMOVEXATTR] = lo_removexattr,
2157 : [SPDK_FSDEV_IO_FLUSH] = lo_flush,
2158 : [SPDK_FSDEV_IO_OPENDIR] = lo_opendir,
2159 : [SPDK_FSDEV_IO_READDIR] = lo_readdir,
2160 : [SPDK_FSDEV_IO_RELEASEDIR] = lo_releasedir,
2161 : [SPDK_FSDEV_IO_FSYNCDIR] = lo_fsyncdir,
2162 : [SPDK_FSDEV_IO_FLOCK] = lo_flock,
2163 : [SPDK_FSDEV_IO_CREATE] = lo_create,
2164 : [SPDK_FSDEV_IO_ABORT] = lo_abort,
2165 : [SPDK_FSDEV_IO_FALLOCATE] = lo_fallocate,
2166 : [SPDK_FSDEV_IO_COPY_FILE_RANGE] = lo_copy_file_range,
2167 : };
2168 :
2169 : static void
2170 0 : fsdev_aio_submit_request(struct spdk_io_channel *ch, struct spdk_fsdev_io *fsdev_io)
2171 : {
2172 : int status;
2173 0 : enum spdk_fsdev_io_type type = spdk_fsdev_io_get_type(fsdev_io);
2174 :
2175 0 : assert(type >= 0 && type < __SPDK_FSDEV_IO_LAST);
2176 :
2177 0 : status = handlers[type](ch, fsdev_io);
2178 0 : if (status != IO_STATUS_ASYNC) {
2179 0 : spdk_fsdev_io_complete(fsdev_io, status);
2180 : }
2181 0 : }
2182 :
2183 : static struct spdk_io_channel *
2184 0 : fsdev_aio_get_io_channel(void *ctx)
2185 : {
2186 0 : return spdk_get_io_channel(&g_aio_fsdev_head);
2187 : }
2188 :
2189 : static int
2190 0 : fsdev_aio_negotiate_opts(void *ctx, struct spdk_fsdev_open_opts *opts)
2191 : {
2192 0 : struct aio_fsdev *vfsdev = ctx;
2193 :
2194 0 : assert(opts != 0);
2195 0 : assert(opts->opts_size != 0);
2196 :
2197 : UNUSED(vfsdev);
2198 :
2199 0 : if (opts->opts_size > offsetof(struct spdk_fsdev_open_opts, max_write)) {
2200 : /* Set the value the aio fsdev was created with */
2201 0 : opts->max_write = vfsdev->fsdev.opts.max_write;
2202 : }
2203 :
2204 0 : if (opts->opts_size > offsetof(struct spdk_fsdev_open_opts, writeback_cache_enabled)) {
2205 0 : if (vfsdev->fsdev.opts.writeback_cache_enabled) {
2206 : /* The writeback_cache_enabled was enabled upon creation => we follow the opts */
2207 0 : vfsdev->fsdev.opts.writeback_cache_enabled = opts->writeback_cache_enabled;
2208 : } else {
2209 : /* The writeback_cache_enabled was disabled upon creation => we reflect it in the opts */
2210 0 : opts->writeback_cache_enabled = false;
2211 : }
2212 : }
2213 :
2214 : /* The AIO doesn't apply any additional restrictions, so we just accept the requested opts */
2215 0 : SPDK_DEBUGLOG(fsdev_aio,
2216 : "aio filesystem %s: opts updated: max_write=%" PRIu32 ", writeback_cache=%" PRIu8 "\n",
2217 : vfsdev->fsdev.name, vfsdev->fsdev.opts.max_write, vfsdev->fsdev.opts.writeback_cache_enabled);
2218 :
2219 0 : return 0;
2220 : }
2221 :
2222 : static void
2223 0 : fsdev_aio_write_config_json(struct spdk_fsdev *fsdev, struct spdk_json_write_ctx *w)
2224 : {
2225 0 : struct aio_fsdev *vfsdev = fsdev_to_aio_fsdev(fsdev);
2226 :
2227 0 : spdk_json_write_object_begin(w);
2228 0 : spdk_json_write_named_string(w, "method", "fsdev_aio_create");
2229 0 : spdk_json_write_named_object_begin(w, "params");
2230 0 : spdk_json_write_named_string(w, "name", spdk_fsdev_get_name(&vfsdev->fsdev));
2231 0 : spdk_json_write_named_string(w, "root_path", vfsdev->root_path);
2232 0 : spdk_json_write_named_bool(w, "enable_xattr", vfsdev->xattr_enabled);
2233 0 : spdk_json_write_named_bool(w, "enable_writeback_cache",
2234 0 : !!vfsdev->fsdev.opts.writeback_cache_enabled);
2235 0 : spdk_json_write_named_uint32(w, "max_write", vfsdev->fsdev.opts.max_write);
2236 0 : spdk_json_write_object_end(w); /* params */
2237 0 : spdk_json_write_object_end(w);
2238 0 : }
2239 :
2240 : static const struct spdk_fsdev_fn_table aio_fn_table = {
2241 : .destruct = fsdev_aio_destruct,
2242 : .submit_request = fsdev_aio_submit_request,
2243 : .get_io_channel = fsdev_aio_get_io_channel,
2244 : .negotiate_opts = fsdev_aio_negotiate_opts,
2245 : .write_config_json = fsdev_aio_write_config_json,
2246 : };
2247 :
2248 : static int
2249 0 : setup_root(struct aio_fsdev *vfsdev)
2250 : {
2251 : int fd, res;
2252 0 : struct stat stat;
2253 :
2254 0 : fd = open(vfsdev->root_path, O_PATH);
2255 0 : if (fd == -1) {
2256 0 : res = -errno;
2257 0 : SPDK_ERRLOG("Cannot open root %s (err=%d)\n", vfsdev->root_path, res);
2258 0 : return res;
2259 : }
2260 :
2261 0 : res = fstatat(fd, "", &stat, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
2262 0 : if (res == -1) {
2263 0 : res = -errno;
2264 0 : SPDK_ERRLOG("Cannot get root fstatat of %s (err=%d)\n", vfsdev->root_path, res);
2265 0 : close(fd);
2266 0 : return res;
2267 : }
2268 :
2269 0 : vfsdev->root = file_object_create_unsafe(NULL, fd, stat.st_ino, stat.st_dev, stat.st_mode);
2270 0 : if (!vfsdev->root) {
2271 0 : SPDK_ERRLOG("Cannot alloc root\n");
2272 0 : close(fd);
2273 0 : return -ENOMEM;
2274 : }
2275 :
2276 0 : SPDK_INFOLOG(fsdev_aio, "root (%s) fd=%d\n", vfsdev->root_path, fd);
2277 0 : return 0;
2278 : }
2279 :
2280 : static int
2281 0 : setup_proc_self_fd(struct aio_fsdev *vfsdev)
2282 : {
2283 0 : vfsdev->proc_self_fd = open("/proc/self/fd", O_PATH);
2284 0 : if (vfsdev->proc_self_fd == -1) {
2285 0 : int saverr = -errno;
2286 0 : SPDK_ERRLOG("Failed to open procfs fd dir with %d\n", saverr);
2287 0 : return saverr;
2288 : }
2289 :
2290 0 : SPDK_DEBUGLOG(fsdev_aio, "procfs fd dir opened (fd=%d)\n", vfsdev->proc_self_fd);
2291 0 : return 0;
2292 : }
2293 :
2294 : void
2295 0 : spdk_fsdev_aio_get_default_opts(struct spdk_fsdev_aio_opts *opts)
2296 : {
2297 0 : assert(opts);
2298 :
2299 0 : memset(opts, 0, sizeof(*opts));
2300 :
2301 0 : opts->xattr_enabled = DEFAULT_XATTR_ENABLED;
2302 0 : opts->writeback_cache_enabled = DEFAULT_WRITEBACK_CACHE;
2303 0 : opts->max_write = DEFAULT_MAX_WRITE;
2304 0 : }
2305 :
2306 : int
2307 0 : spdk_fsdev_aio_create(struct spdk_fsdev **fsdev, const char *name, const char *root_path,
2308 : const struct spdk_fsdev_aio_opts *opts)
2309 : {
2310 : struct aio_fsdev *vfsdev;
2311 : int rc;
2312 :
2313 0 : vfsdev = calloc(1, sizeof(*vfsdev));
2314 0 : if (!vfsdev) {
2315 0 : SPDK_ERRLOG("Could not allocate aio_fsdev\n");
2316 0 : return -ENOMEM;
2317 : }
2318 :
2319 0 : vfsdev->proc_self_fd = -1;
2320 :
2321 0 : vfsdev->fsdev.name = strdup(name);
2322 0 : if (!vfsdev->fsdev.name) {
2323 0 : SPDK_ERRLOG("Could not strdup fsdev name: %s\n", name);
2324 0 : fsdev_aio_free(vfsdev);
2325 0 : return -ENOMEM;
2326 : }
2327 :
2328 0 : vfsdev->root_path = strdup(root_path);
2329 0 : if (!vfsdev->root_path) {
2330 0 : SPDK_ERRLOG("Could not strdup root path: %s\n", root_path);
2331 0 : fsdev_aio_free(vfsdev);
2332 0 : return -ENOMEM;
2333 : }
2334 :
2335 0 : rc = setup_root(vfsdev);
2336 0 : if (rc) {
2337 0 : SPDK_ERRLOG("Could not setup root: %s (err=%d)\n", root_path, rc);
2338 0 : fsdev_aio_free(vfsdev);
2339 0 : return rc;
2340 : }
2341 :
2342 0 : rc = setup_proc_self_fd(vfsdev);
2343 0 : if (rc) {
2344 0 : SPDK_ERRLOG("Could not setup proc_self_fd (err=%d)\n", rc);
2345 0 : fsdev_aio_free(vfsdev);
2346 0 : return rc;
2347 : }
2348 :
2349 0 : if (opts->xattr_enabled) {
2350 0 : SPDK_ERRLOG("Extended attributes can only be enabled in Linux\n");
2351 0 : fsdev_aio_free(vfsdev);
2352 0 : return rc;
2353 : }
2354 :
2355 0 : vfsdev->xattr_enabled = opts->xattr_enabled;
2356 0 : vfsdev->fsdev.ctxt = vfsdev;
2357 0 : vfsdev->fsdev.fn_table = &aio_fn_table;
2358 0 : vfsdev->fsdev.module = &aio_fsdev_module;
2359 :
2360 0 : pthread_mutex_init(&vfsdev->mutex, NULL);
2361 :
2362 0 : rc = spdk_fsdev_register(&vfsdev->fsdev);
2363 0 : if (rc) {
2364 0 : fsdev_aio_free(vfsdev);
2365 0 : return rc;
2366 : }
2367 :
2368 0 : vfsdev->fsdev.opts.writeback_cache_enabled = opts->writeback_cache_enabled;
2369 0 : vfsdev->fsdev.opts.max_write = opts->max_write;
2370 :
2371 0 : *fsdev = &(vfsdev->fsdev);
2372 0 : TAILQ_INSERT_TAIL(&g_aio_fsdev_head, vfsdev, tailq);
2373 0 : SPDK_DEBUGLOG(fsdev_aio, "Created aio filesystem %s (xattr_enabled=%" PRIu8 " writeback_cache=%"
2374 : PRIu8 " max_write=%" PRIu32 ")\n",
2375 : vfsdev->fsdev.name, vfsdev->xattr_enabled, vfsdev->fsdev.opts.writeback_cache_enabled,
2376 : vfsdev->fsdev.opts.max_write);
2377 0 : return rc;
2378 : }
2379 : void
2380 0 : spdk_fsdev_aio_delete(const char *name,
2381 : spdk_delete_aio_fsdev_complete cb_fn, void *cb_arg)
2382 : {
2383 : int rc;
2384 :
2385 0 : rc = spdk_fsdev_unregister_by_name(name, &aio_fsdev_module, cb_fn, cb_arg);
2386 0 : if (rc != 0) {
2387 0 : cb_fn(cb_arg, rc);
2388 : }
2389 :
2390 0 : SPDK_DEBUGLOG(fsdev_aio, "Deleted aio filesystem %s\n", name);
2391 0 : }
2392 :
2393 0 : SPDK_LOG_REGISTER_COMPONENT(fsdev_aio)
|