btrfs: add test for multiple fsync with adjacent preallocated extents
[xfstests-dev.git] / ltp / fsstress.c
index 22df5e38b5dc90b9387cc5ee7397383285fbe9f2..b4ddf5e2ef35eadf578d4e38649c4641da9fa7bf 100644 (file)
 #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
@@ -35,6 +29,7 @@ io_context_t  io_ctx;
 #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>
@@ -393,7 +388,7 @@ 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_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);
@@ -706,9 +701,15 @@ int main(int argc, char **argv)
                        }
 #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)) {
-                               fprintf(stderr, "io_uring_queue_init failed\n");
-                               exit(1);
+                               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++)
@@ -720,7 +721,8 @@ int main(int argc, char **argv)
                        }
 #endif
 #ifdef URING
-                       io_uring_queue_exit(&ring);
+                       if (have_io_uring)
+                               io_uring_queue_exit(&ring);
 #endif
                        cleanup_flist();
                        free(freq_table);
@@ -860,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);
@@ -2130,7 +2140,7 @@ do_aio_rw(int opno, long r, int flags)
                        " fallback to stat()\n",
                                procid, opno, f.path, st, errno);
                diob.d_mem = diob.d_miniosz = stb.st_blksize;
-               diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
+               diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
        }
        dio_env = getenv("XFS_DIO_MIN");
        if (dio_env)
@@ -2208,6 +2218,9 @@ do_uring_rw(int opno, long r, int flags)
        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)
@@ -2302,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",
@@ -2335,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(
@@ -2609,7 +2608,7 @@ clonerange_f(
 
        /* Calculate offsets */
        len = (random() % FILELEN_MAX) + 1;
-       len &= ~(stat1.st_blksize - 1);
+       len = rounddown_64(len, stat1.st_blksize);
        if (len == 0)
                len = stat1.st_blksize;
        if (len > stat1.st_size)
@@ -2621,7 +2620,7 @@ clonerange_f(
        else
                off1 = (off64_t)(lr % MIN(stat1.st_size - len, MAXFSIZE));
        off1 %= maxfsize;
-       off1 &= ~(stat1.st_blksize - 1);
+       off1 = rounddown_64(off1, stat1.st_blksize);
 
        /*
         * If srcfile == destfile, randomly generate destination ranges
@@ -2632,7 +2631,7 @@ clonerange_f(
                lr = ((int64_t)random() << 32) + random();
                off2 = (off64_t)(lr % max_off2);
                off2 %= maxfsize;
-               off2 &= ~(stat2.st_blksize - 1);
+               off2 = rounddown_64(off2, stat2.st_blksize);
        } while (stat1.st_ino == stat2.st_ino && llabs(off2 - off1) < len);
 
        /* Clone data blocks */
@@ -2969,7 +2968,7 @@ deduperange_f(
 
        /* Never try to dedupe more than half of the src file. */
        len = (random() % FILELEN_MAX) + 1;
-       len &= ~(stat[0].st_blksize - 1);
+       len = rounddown_64(len, stat[0].st_blksize);
        if (len == 0)
                len = stat[0].st_blksize / 2;
        if (len > stat[0].st_size / 2)
@@ -2982,7 +2981,7 @@ deduperange_f(
        else
                off[0] = (off64_t)(lr % MIN(stat[0].st_size - len, MAXFSIZE));
        off[0] %= maxfsize;
-       off[0] &= ~(stat[0].st_blksize - 1);
+       off[0] = rounddown_64(off[0], stat[0].st_blksize);
 
        /*
         * If srcfile == destfile[i], randomly generate destination ranges
@@ -2998,7 +2997,7 @@ deduperange_f(
                        else
                                off[i] = (off64_t)(lr % MIN(stat[i].st_size - len, MAXFSIZE));
                        off[i] %= maxfsize;
-                       off[i] &= ~(stat[i].st_blksize - 1);
+                       off[i] = rounddown_64(off[i], stat[i].st_blksize);
                } while (stat[0].st_ino == stat[i].st_ino &&
                         llabs(off[i] - off[0]) < len &&
                         tries++ < 10);
@@ -3407,7 +3406,7 @@ dread_f(int opno, long r)
                        " fallback to stat()\n",
                                procid, opno, f.path, st, errno);
                diob.d_mem = diob.d_miniosz = stb.st_blksize;
-               diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
+               diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
        }
 
        dio_env = getenv("XFS_DIO_MIN");
@@ -3484,7 +3483,7 @@ dwrite_f(int opno, long r)
                                " %s%s return %d, fallback to stat()\n",
                               procid, opno, f.path, st, errno);
                diob.d_mem = diob.d_miniosz = stb.st_blksize;
-               diob.d_maxiosz = INT_MAX & ~(diob.d_miniosz - 1);
+               diob.d_maxiosz = rounddown_64(INT_MAX, diob.d_miniosz);
        }
 
        dio_env = getenv("XFS_DIO_MIN");
@@ -3580,8 +3579,8 @@ do_fallocate(int opno, long r, int mode)
         */
        if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) &&
                (opno % 2)) {
-               off = ((off + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
-               len = ((len + stb.st_blksize - 1) & ~(stb.st_blksize - 1));
+               off = roundup_64(off, stb.st_blksize);
+               len = roundup_64(len, stb.st_blksize);
        }
        mode |= FALLOC_FL_KEEP_SIZE & random();
        e = fallocate(fd, mode, (loff_t)off, (loff_t)len) < 0 ? errno : 0;
@@ -4187,7 +4186,7 @@ do_mmap(int opno, long r, int prot)
 
        lr = ((int64_t)random() << 32) + random();
        off = (off64_t)(lr % stb.st_size);
-       off &= (off64_t)(~(sysconf(_SC_PAGE_SIZE) - 1));
+       off = rounddown_64(off, sysconf(_SC_PAGE_SIZE));
        len = (size_t)(random() % MIN(stb.st_size - off, FILELEN_MAX)) + 1;
 
        flags = (random() % 2) ? MAP_SHARED : MAP_PRIVATE;