btrfs: add test for zone auto reclaim
[xfstests-dev.git] / ltp / fsstress.c
index c5a59d5151b68760691c10a8643b1be5a67b21d8..e7cd0eae304d521af4cee5bf8e17497f5cdfd3c0 100644 (file)
@@ -8,27 +8,29 @@
 #include <setjmp.h>
 #include <sys/uio.h>
 #include <stddef.h>
+#include <stdbool.h>
 #include "global.h"
 
 #ifdef HAVE_BTRFSUTIL_H
 #include <btrfsutil.h>
 #endif
-#ifdef HAVE_ATTR_ATTRIBUTES_H
-#include <attr/attributes.h>
-#endif
 #ifdef HAVE_LINUX_FIEMAP_H
 #include <linux/fiemap.h>
 #endif
-#ifndef HAVE_ATTR_LIST
-#define attr_list(path, buf, size, flags, cursor) (errno = -ENOSYS, -1)
-#endif
 #ifdef HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
 #endif
 #ifdef AIO
 #include <libaio.h>
+#define AIO_ENTRIES    1
 io_context_t   io_ctx;
 #endif
+#ifdef URING
+#include <liburing.h>
+#define URING_ENTRIES  1
+struct io_uring        ring;
+bool have_io_uring;                    /* to indicate runtime availability */
+#endif
 #include <sys/syscall.h>
 #include <sys/xattr.h>
 
@@ -138,6 +140,8 @@ typedef enum {
        OP_TRUNCATE,
        OP_UNLINK,
        OP_UNRESVSP,
+       OP_URING_READ,
+       OP_URING_WRITE,
        OP_WRITE,
        OP_WRITEV,
        OP_LAST
@@ -266,6 +270,8 @@ void        sync_f(int, long);
 void   truncate_f(int, long);
 void   unlink_f(int, long);
 void   unresvsp_f(int, long);
+void   uring_read_f(int, long);
+void   uring_write_f(int, long);
 void   write_f(int, long);
 void   writev_f(int, long);
 char   *xattr_flag_to_string(int);
@@ -334,6 +340,8 @@ opdesc_t    ops[] = {
        { OP_TRUNCATE, "truncate", truncate_f, 2, 1 },
        { OP_UNLINK, "unlink", unlink_f, 1, 1 },
        { OP_UNRESVSP, "unresvsp", unresvsp_f, 1, 1 },
+       { OP_URING_READ, "uring_read", uring_read_f, 1, 0 },
+       { OP_URING_WRITE, "uring_write", uring_write_f, 1, 1 },
        { OP_WRITE, "write", write_f, 4, 1 },
        { OP_WRITEV, "writev", writev_f, 4, 1 },
 }, *ops_end;
@@ -380,9 +388,9 @@ struct print_string flag_str = {0};
 
 void   add_to_flist(int, int, int, int);
 void   append_pathname(pathname_t *, char *);
-int    attr_list_path(pathname_t *, char *, const int, int, attrlist_cursor_t *);
-int    attr_remove_path(pathname_t *, const char *, int);
-int    attr_set_path(pathname_t *, const char *, const char *, const int, int);
+int    attr_list_path(pathname_t *, char *, const int);
+int    attr_remove_path(pathname_t *, const char *);
+int    attr_set_path(pathname_t *, const char *, const char *, const int);
 void   check_cwd(void);
 void   cleanup_flist(void);
 int    creat_path(pathname_t *, mode_t);
@@ -687,10 +695,22 @@ int main(int argc, char **argv)
                        }
                        procid = i;
 #ifdef AIO
-                       if (io_setup(128, &io_ctx) != 0) {
-                               fprintf(stderr, "io_setup failed");
+                       if (io_setup(AIO_ENTRIES, &io_ctx) != 0) {
+                               fprintf(stderr, "io_setup failed\n");
                                exit(1);
                        }
+#endif
+#ifdef URING
+                       have_io_uring = true;
+                       /* If ENOSYS, just ignore uring, other errors are fatal. */
+                       if (io_uring_queue_init(URING_ENTRIES, &ring, 0)) {
+                               if (errno == ENOSYS) {
+                                       have_io_uring = false;
+                               } else {
+                                       fprintf(stderr, "io_uring_queue_init failed\n");
+                                       exit(1);
+                               }
+                       }
 #endif
                        for (i = 0; !loops || (i < loops); i++)
                                doproc();
@@ -700,7 +720,10 @@ int main(int argc, char **argv)
                                return 1;
                        }
 #endif
-
+#ifdef URING
+                       if (have_io_uring)
+                               io_uring_queue_exit(&ring);
+#endif
                        cleanup_flist();
                        free(freq_table);
                        return 0;
@@ -839,28 +862,36 @@ append_pathname(pathname_t *name, char *str)
        name->len += len;
 }
 
+int
+attr_list_count(char *buffer, int buffersize)
+{
+       char *p = buffer;
+       char *end = buffer + buffersize;
+       int count = 0;
+
+       while (p < end && *p != 0) {
+               count++;
+               p += strlen(p) + 1;
+       }
+
+       return count;
+}
+
 int
 attr_list_path(pathname_t *name,
               char *buffer,
-              const int buffersize,
-              int flags,
-              attrlist_cursor_t *cursor)
+              const int buffersize)
 {
        char            buf[NAME_MAX + 1];
        pathname_t      newname;
        int             rval;
 
-       if (flags != ATTR_DONTFOLLOW) {
-               errno = EINVAL;
-               return -1;
-       }
-
-       rval = attr_list(name->path, buffer, buffersize, flags, cursor);
+       rval = llistxattr(name->path, buffer, buffersize);
        if (rval >= 0 || errno != ENAMETOOLONG)
                return rval;
        separate_pathname(name, buf, &newname);
        if (chdir(buf) == 0) {
-               rval = attr_list_path(&newname, buffer, buffersize, flags, cursor);
+               rval = attr_list_path(&newname, buffer, buffersize);
                assert(chdir("..") == 0);
        }
        free_pathname(&newname);
@@ -868,18 +899,18 @@ attr_list_path(pathname_t *name,
 }
 
 int
-attr_remove_path(pathname_t *name, const char *attrname, int flags)
+attr_remove_path(pathname_t *name, const char *attrname)
 {
        char            buf[NAME_MAX + 1];
        pathname_t      newname;
        int             rval;
 
-       rval = attr_remove(name->path, attrname, flags);
+       rval = lremovexattr(name->path, attrname);
        if (rval >= 0 || errno != ENAMETOOLONG)
                return rval;
        separate_pathname(name, buf, &newname);
        if (chdir(buf) == 0) {
-               rval = attr_remove_path(&newname, attrname, flags);
+               rval = attr_remove_path(&newname, attrname);
                assert(chdir("..") == 0);
        }
        free_pathname(&newname);
@@ -888,19 +919,19 @@ attr_remove_path(pathname_t *name, const char *attrname, int flags)
 
 int
 attr_set_path(pathname_t *name, const char *attrname, const char *attrvalue,
-             const int valuelength, int flags)
+             const int valuelength)
 {
        char            buf[NAME_MAX + 1];
        pathname_t      newname;
        int             rval;
 
-       rval = attr_set(name->path, attrname, attrvalue, valuelength, flags);
+       rval = lsetxattr(name->path, attrname, attrvalue, valuelength, 0);
        if (rval >= 0 || errno != ENAMETOOLONG)
                return rval;
        separate_pathname(name, buf, &newname);
        if (chdir(buf) == 0) {
-               rval = attr_set_path(&newname, attrname, attrvalue, valuelength,
-                       flags);
+               rval = attr_set_path(&newname, attrname, attrvalue,
+                                    valuelength);
                assert(chdir("..") == 0);
        }
        free_pathname(&newname);
@@ -2057,11 +2088,11 @@ void
 do_aio_rw(int opno, long r, int flags)
 {
        int64_t         align;
-       char            *buf;
+       char            *buf = NULL;
        struct dioattr  diob;
        int             e;
        pathname_t      f;
-       int             fd;
+       int             fd = -1;
        size_t          len;
        int64_t         lr;
        off64_t         off;
@@ -2078,8 +2109,7 @@ do_aio_rw(int opno, long r, int flags)
        if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
                if (v)
                        printf("%d/%d: do_aio_rw - no filename\n", procid, opno);
-               free_pathname(&f);
-               return;
+               goto aio_out;
        }
        fd = open_path(&f, flags|O_DIRECT);
        e = fd < 0 ? errno : 0;
@@ -2088,25 +2118,20 @@ do_aio_rw(int opno, long r, int flags)
                if (v)
                        printf("%d/%d: do_aio_rw - open %s failed %d\n",
                               procid, opno, f.path, e);
-               free_pathname(&f);
-               return;
+               goto aio_out;
        }
        if (fstat64(fd, &stb) < 0) {
                if (v)
                        printf("%d/%d: do_aio_rw - fstat64 %s failed %d\n",
                               procid, opno, f.path, errno);
-               free_pathname(&f);
-               close(fd);
-               return;
+               goto aio_out;
        }
        inode_info(st, sizeof(st), &stb, v);
        if (!iswrite && stb.st_size == 0) {
                if (v)
                        printf("%d/%d: do_aio_rw - %s%s zero size\n", procid, opno,
                               f.path, st);
-               free_pathname(&f);
-               close(fd);
-               return;
+               goto aio_out;
        }
        if (xfsctl(f.path, fd, XFS_IOC_DIOINFO, &diob) < 0) {
                if (v)
@@ -2129,6 +2154,12 @@ do_aio_rw(int opno, long r, int flags)
        else if (len > diob.d_maxiosz)
                len = diob.d_maxiosz;
        buf = memalign(diob.d_mem, len);
+       if (!buf) {
+               if (v)
+                       printf("%d/%d: do_aio_rw - memalign failed\n",
+                              procid, opno);
+               goto aio_out;
+       }
 
        if (iswrite) {
                off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
@@ -2145,27 +2176,131 @@ do_aio_rw(int opno, long r, int flags)
                if (v)
                        printf("%d/%d: %s - io_submit failed %d\n",
                               procid, opno, iswrite ? "awrite" : "aread", e);
-               free_pathname(&f);
-               close(fd);
-               return;
+               goto aio_out;
        }
        if ((e = io_getevents(io_ctx, 1, 1, &event, NULL)) != 1) {
                if (v)
                        printf("%d/%d: %s - io_getevents failed %d\n",
                               procid, opno, iswrite ? "awrite" : "aread", e);
-               free_pathname(&f);
-               close(fd);
-               return;
+               goto aio_out;
        }
 
        e = event.res != len ? event.res2 : 0;
-       free(buf);
        if (v)
                printf("%d/%d: %s %s%s [%lld,%d] %d\n",
                       procid, opno, iswrite ? "awrite" : "aread",
                       f.path, st, (long long)off, (int)len, e);
+ aio_out:
+       if (buf)
+               free(buf);
+       if (fd != -1)
+               close(fd);
+       free_pathname(&f);
+}
+#endif
+
+#ifdef URING
+void
+do_uring_rw(int opno, long r, int flags)
+{
+       char            *buf = NULL;
+       int             e;
+       pathname_t      f;
+       int             fd = -1;
+       size_t          len;
+       int64_t         lr;
+       off64_t         off;
+       struct stat64   stb;
+       int             v;
+       char            st[1024];
+       struct io_uring_sqe     *sqe;
+       struct io_uring_cqe     *cqe;
+       struct iovec    iovec;
+       int             iswrite = (flags & (O_WRONLY | O_RDWR)) ? 1 : 0;
+
+       if (!have_io_uring)
+               return;
+
+       init_pathname(&f);
+       if (!get_fname(FT_REGFILE, r, &f, NULL, NULL, &v)) {
+               if (v)
+                       printf("%d/%d: do_uring_rw - no filename\n", procid, opno);
+               goto uring_out;
+       }
+       fd = open_path(&f, flags);
+       e = fd < 0 ? errno : 0;
+       check_cwd();
+       if (fd < 0) {
+               if (v)
+                       printf("%d/%d: do_uring_rw - open %s failed %d\n",
+                              procid, opno, f.path, e);
+               goto uring_out;
+       }
+       if (fstat64(fd, &stb) < 0) {
+               if (v)
+                       printf("%d/%d: do_uring_rw - fstat64 %s failed %d\n",
+                              procid, opno, f.path, errno);
+               goto uring_out;
+       }
+       inode_info(st, sizeof(st), &stb, v);
+       if (!iswrite && stb.st_size == 0) {
+               if (v)
+                       printf("%d/%d: do_uring_rw - %s%s zero size\n", procid, opno,
+                              f.path, st);
+               goto uring_out;
+       }
+       sqe = io_uring_get_sqe(&ring);
+       if (!sqe) {
+               if (v)
+                       printf("%d/%d: do_uring_rw - io_uring_get_sqe failed\n",
+                              procid, opno);
+               goto uring_out;
+       }
+       lr = ((int64_t)random() << 32) + random();
+       len = (random() % FILELEN_MAX) + 1;
+       buf = malloc(len);
+       if (!buf) {
+               if (v)
+                       printf("%d/%d: do_uring_rw - malloc failed\n",
+                              procid, opno);
+               goto uring_out;
+       }
+       iovec.iov_base = buf;
+       iovec.iov_len = len;
+       if (iswrite) {
+               off = (off64_t)(lr % MIN(stb.st_size + (1024 * 1024), MAXFSIZE));
+               off %= maxfsize;
+               memset(buf, nameseq & 0xff, len);
+               io_uring_prep_writev(sqe, fd, &iovec, 1, off);
+       } else {
+               off = (off64_t)(lr % stb.st_size);
+               io_uring_prep_readv(sqe, fd, &iovec, 1, off);
+       }
+
+       if ((e = io_uring_submit_and_wait(&ring, 1)) != 1) {
+               if (v)
+                       printf("%d/%d: %s - io_uring_submit failed %d\n", procid, opno,
+                              iswrite ? "uring_write" : "uring_read", e);
+               goto uring_out;
+       }
+       if ((e = io_uring_wait_cqe(&ring, &cqe)) < 0) {
+               if (v)
+                       printf("%d/%d: %s - io_uring_wait_cqe failed %d\n", procid, opno,
+                              iswrite ? "uring_write" : "uring_read", e);
+               goto uring_out;
+       }
+       if (v)
+               printf("%d/%d: %s %s%s [%lld, %d(res=%d)] %d\n",
+                      procid, opno, iswrite ? "uring_write" : "uring_read",
+                      f.path, st, (long long)off, (int)len, cqe->res, e);
+       io_uring_cqe_seen(&ring, cqe);
+
+ uring_out:
+       if (buf)
+               free(buf);
+       if (fd != -1)
+               close(fd);
        free_pathname(&f);
-       close(fd);
 }
 #endif
 
@@ -2180,32 +2315,24 @@ aread_f(int opno, long r)
 void
 attr_remove_f(int opno, long r)
 {
-       attrlist_ent_t          *aep;
-       attrlist_t              *alist;
-       char                    *aname;
-       char                    buf[4096];
-       attrlist_cursor_t       cursor;
+       char                    *bufname;
+       char                    *bufend;
+       char                    *aname = NULL;
+       char                    buf[XATTR_LIST_MAX];
        int                     e;
        int                     ent;
        pathname_t              f;
-       int                     total;
+       int                     total = 0;
        int                     v;
        int                     which;
 
        init_pathname(&f);
        if (!get_fname(FT_ANYm, r, &f, NULL, NULL, &v))
                append_pathname(&f, ".");
-       total = 0;
-       bzero(&cursor, sizeof(cursor));
-       do {
-               bzero(buf, sizeof(buf));
-               e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
-               check_cwd();
-               if (e)
-                       break;
-               alist = (attrlist_t *)buf;
-               total += alist->al_count;
-       } while (alist->al_more);
+       e = attr_list_path(&f, buf, sizeof(buf));
+       check_cwd();
+       if (e > 0)
+               total = attr_list_count(buf, e);
        if (total == 0) {
                if (v)
                        printf("%d/%d: attr_remove - no attrs for %s\n",
@@ -2213,25 +2340,19 @@ attr_remove_f(int opno, long r)
                free_pathname(&f);
                return;
        }
+
        which = (int)(random() % total);
-       bzero(&cursor, sizeof(cursor));
+       bufname = buf;
+       bufend = buf + e;
        ent = 0;
-       aname = NULL;
-       do {
-               bzero(buf, sizeof(buf));
-               e = attr_list_path(&f, buf, sizeof(buf), ATTR_DONTFOLLOW, &cursor);
-               check_cwd();
-               if (e)
-                       break;
-               alist = (attrlist_t *)buf;
-               if (which < ent + alist->al_count) {
-                       aep = (attrlist_ent_t *)
-                               &buf[alist->al_offset[which - ent]];
-                       aname = aep->a_name;
+       while (bufname < bufend) {
+               if (which < ent) {
+                       aname = bufname;
                        break;
                }
-               ent += alist->al_count;
-       } while (alist->al_more);
+               ent++;
+               bufname += strlen(bufname) + 1;
+       }
        if (aname == NULL) {
                if (v)
                        printf(
@@ -2240,7 +2361,10 @@ attr_remove_f(int opno, long r)
                free_pathname(&f);
                return;
        }
-       e = attr_remove_path(&f, aname, ATTR_DONTFOLLOW) < 0 ? errno : 0;
+       if (attr_remove_path(&f, aname) < 0)
+               e = errno;
+       else
+               e = 0;
        check_cwd();
        if (v)
                printf("%d/%d: attr_remove %s %s %d\n",
@@ -2270,8 +2394,10 @@ attr_set_f(int opno, long r)
                len = 1;
        aval = malloc(len);
        memset(aval, nameseq & 0xff, len);
-       e = attr_set_path(&f, aname, aval, len, ATTR_DONTFOLLOW) < 0 ?
-               errno : 0;
+       if (attr_set_path(&f, aname, aval, len) < 0)
+               e = errno;
+       else
+               e = 0;
        check_cwd();
        free(aval);
        if (v)
@@ -5043,6 +5169,22 @@ unresvsp_f(int opno, long r)
        close(fd);
 }
 
+void
+uring_read_f(int opno, long r)
+{
+#ifdef URING
+       do_uring_rw(opno, r, O_RDONLY);
+#endif
+}
+
+void
+uring_write_f(int opno, long r)
+{
+#ifdef URING
+       do_uring_rw(opno, r, O_WRONLY);
+#endif
+}
+
 void
 write_f(int opno, long r)
 {