Whamcloud - gitweb
LU-2459 osd: add LMA incompat flag check
[fs/lustre-release.git] / lustre / tests / multiop.c
old mode 100755 (executable)
new mode 100644 (file)
index 3bd2128..7a21869
@@ -1,6 +1,39 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
  */
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ */
+
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
 #endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <sys/vfs.h>
+#include <sys/ioctl.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <lustre/liblustreapi.h>
+#include <semaphore.h>
+#include <time.h>
+
+#include <lustre/lustre_idl.h>
+#include <lustre/lustreapi.h>
 
 #define T1 "write data before unlink\n"
 #define T2 "write data after unlink\n"
 char msg[] = "yabba dabba doo, I'm coming for you, I live in a shoe, I don't know what to do.\n'Bigger, bigger,and bigger yet!' cried the Creator.  'You are not yet substantial enough for my boundless intents!'  And ever greater and greater the object became, until all was lost 'neath its momentus bulk.\n";
 char *buf, *buf_align;
 int bufsize = 0;
-#define ALIGN 65535
+sem_t sem;
+#define ALIGN_LEN 65535
 
-char usage[] = 
-"Usage: %s filename command-sequence\n"
+char usage[] =
+"Usage: %s filename command-sequence [path...]\n"
 "    command-sequence items:\n"
 "        c  close\n"
+"        B[num] call setstripe ioctl to create stripes\n"
 "        C[num] create with optional stripes\n"
 "        d  mkdir\n"
 "        D  open(O_DIRECTORY)\n"
-"        L  link\n"
-"        l  symlink\n"
+"        f  statfs\n"
+"       F  print FID\n"
+"        G gid get grouplock\n"
+"        g gid put grouplock\n"
+"        K  link path to filename\n"
+"        L  link filename to path\n"
+"        l  symlink filename to path\n"
 "        m  mknod\n"
 "        M  rw mmap to EOF (must open and stat prior)\n"
-"        N  rename\n"
+"        n  rename path to filename\n"
+"        N  rename filename to path\n"
 "        o  open(O_RDONLY)\n"
 "        O  open(O_CREAT|O_RDWR)\n"
 "        r[num] read [optional length]\n"
@@ -45,6 +92,8 @@ char usage[] =
 "        T[num] ftruncate [optional position, default 0]\n"
 "        u  unlink\n"
 "        U  munmap\n"
+"        v  verbose\n"
+"       V  open a volatile file\n"
 "        w[num] write optional length\n"
 "        W  write entire mmap-ed region\n"
 "        y  fsync\n"
@@ -52,10 +101,19 @@ char usage[] =
 "        z[num] seek [optional position, default 0]\n"
 "        _  wait for signal\n";
 
-static int usr1_received;
 void usr1_handler(int unused)
 {
-        usr1_received = 1;
+        int saved_errno = errno;
+
+        /*
+         * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
+         * that the following functions can be safely called inside a signal
+         * handler:
+         *            sem_post()
+         */
+        sem_post(&sem);
+
+        errno = saved_errno;
 }
 
 static const char *
@@ -70,27 +128,28 @@ pop_arg(int argc, char *argv[])
 }
 
 struct flag_mapping {
-       const char *string;
-       const int  flag;
+       const char *string;
+       const int  flag;
 } flag_table[] = {
-       {"O_RDONLY", O_RDONLY},
-       {"O_WRONLY", O_WRONLY},
-       {"O_RDWR", O_RDWR},
-       {"O_CREAT", O_CREAT},
-       {"O_EXCL", O_EXCL},
-       {"O_NOCTTY", O_NOCTTY},
-       {"O_TRUNC", O_TRUNC},
-       {"O_APPEND", O_APPEND},
-       {"O_NONBLOCK", O_NONBLOCK},
-       {"O_NDELAY", O_NDELAY},
-       {"O_SYNC", O_SYNC},
+       {"O_RDONLY", O_RDONLY},
+       {"O_WRONLY", O_WRONLY},
+       {"O_RDWR", O_RDWR},
+       {"O_CREAT", O_CREAT},
+       {"O_EXCL", O_EXCL},
+       {"O_NOCTTY", O_NOCTTY},
+       {"O_TRUNC", O_TRUNC},
+       {"O_APPEND", O_APPEND},
+       {"O_NONBLOCK", O_NONBLOCK},
+       {"O_NDELAY", O_NDELAY},
+       {"O_SYNC", O_SYNC},
 #ifdef O_DIRECT
-       {"O_DIRECT", O_DIRECT},
+       {"O_DIRECT", O_DIRECT},
 #endif
-       {"O_LARGEFILE", O_LARGEFILE},
-       {"O_DIRECTORY", O_DIRECTORY},
-       {"O_NOFOLLOW", O_NOFOLLOW},
-       {"", -1}
+       {"O_LARGEFILE", O_LARGEFILE},
+       {"O_DIRECTORY", O_DIRECTORY},
+       {"O_NOFOLLOW", O_NOFOLLOW},
+       {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
+       {"", -1}
 };
 
 int get_flags(char *data, int *rflags)
@@ -133,41 +192,51 @@ int get_flags(char *data, int *rflags)
 }
 
 #define POP_ARG() (pop_arg(argc, argv))
-#define min(a,b) ((a)>(b)?(b):(a))
 
 int main(int argc, char **argv)
 {
-        char *fname, *commands;
-        const char *newfile;
-        struct stat st;
-        size_t mmap_len = 0, i;
-        unsigned char *mmap_ptr = NULL, junk = 0;
-        int rc, len, fd = -1;
-        int flags;
-        int save_errno;
-        int verbose = 0;
+       char            *fname, *commands;
+       const char      *newfile;
+       const char      *oldpath;
+       struct stat      st;
+       struct statfs    stfs;
+       size_t           mmap_len = 0, i;
+       unsigned char   *mmap_ptr = NULL, junk = 0;
+       int              rc, len, fd = -1;
+       int              flags;
+       int              save_errno;
+       int              verbose = 0;
+       int              gid = 0;
+       lustre_fid       fid;
+       struct timespec  ts;
+       struct lov_user_md_v3 lum;
 
         if (argc < 3) {
                 fprintf(stderr, usage, argv[0]);
                 exit(1);
         }
 
-        signal(SIGUSR1, usr1_handler);
+        memset(&st, 0, sizeof(st));
+        sem_init(&sem, 0, 0);
+        /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
+        sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
+                  NULL);
 
         fname = argv[1];
 
         for (commands = argv[2]; *commands; commands++) {
                 switch (*commands) {
                 case '_':
-                        if (usr1_received == 0) {
-                                if (verbose) {
-                                        printf("PAUSING\n");
-                                        fflush(stdout);
-                                }
-                                pause();
+                        if (verbose) {
+                                printf("PAUSING\n");
+                                fflush(stdout);
                         }
-                        usr1_received = 0;
-                        signal(SIGUSR1, usr1_handler);
+                       len = atoi(commands+1);
+                       if (len <= 0)
+                               len = 3600; /* 1 hour */
+                       ts.tv_sec = time(NULL) + len;
+                       ts.tv_nsec = 0;
+                        while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
                         break;
                 case 'c':
                         if (close(fd) == -1) {
@@ -177,6 +246,18 @@ int main(int argc, char **argv)
                         }
                         fd = -1;
                         break;
+               case 'B':
+                       lum = (struct lov_user_md_v3) {
+                               .lmm_magic = LOV_USER_MAGIC_V3,
+                               .lmm_stripe_count = atoi(commands + 1),
+                       };
+
+                       if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
+                               save_errno = errno;
+                               perror("LL_IOC_LOV_SETSTRIPE");
+                               exit(save_errno);
+                       }
+                       break;
                 case 'C':
                         len = atoi(commands+1);
                         fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
@@ -202,26 +283,73 @@ int main(int argc, char **argv)
                                 exit(save_errno);
                         }
                         break;
-                case 'l':
-                        newfile = POP_ARG();
-                        if (!newfile)
-                                newfile = fname;
-                        if (symlink(fname, newfile)) {
+                case 'f':
+                        if (statfs(fname, &stfs) == -1) {
                                 save_errno = errno;
-                                perror("symlink()");
+                                perror("statfs()");
                                 exit(save_errno);
                         }
                         break;
-                case 'L':
+               case 'F':
+                       if (fd == -1)
+                               rc = llapi_path2fid(fname, &fid);
+                       else
+                               rc = llapi_fd2fid(fd, &fid);
+                       if (rc != 0)
+                               fprintf(stderr,
+                                       "llapi_path/fd2fid() on %d, rc=%d\n",
+                                       fd, rc);
+                       else
+                               printf(DFID"\n", PFID(&fid));
+                       break;
+                case 'G':
+                        gid = atoi(commands+1);
+                        if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
+                                save_errno = errno;
+                                perror("ioctl(GROUP_LOCK)");
+                                exit(save_errno);
+                        }
+                        break;
+                case 'g':
+                        gid = atoi(commands+1);
+                        if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
+                                save_errno = errno;
+                                perror("ioctl(GROUP_UNLOCK)");
+                                exit(save_errno);
+                        }
+                        break;
+               case 'K':
+                       oldpath = POP_ARG();
+                       if (oldpath == NULL)
+                               oldpath = fname;
+
+                       if (link(oldpath, fname)) {
+                               save_errno = errno;
+                               perror("link()");
+                               exit(save_errno);
+                       }
+                       break;
+                case 'l':
                         newfile = POP_ARG();
                         if (!newfile)
                                 newfile = fname;
-                        if (link(fname, newfile)) {
+                        if (symlink(fname, newfile)) {
                                 save_errno = errno;
                                 perror("symlink()");
                                 exit(save_errno);
                         }
                         break;
+               case 'L':
+                       newfile = POP_ARG();
+                       if (newfile == NULL)
+                               newfile = fname;
+
+                       if (link(fname, newfile)) {
+                               save_errno = errno;
+                               perror("link()");
+                               exit(save_errno);
+                       }
+                       break;
                 case 'm':
                         if (mknod(fname, S_IFREG | 0644, 0) == -1) {
                                 save_errno = errno;
@@ -230,6 +358,11 @@ int main(int argc, char **argv)
                         }
                         break;
                 case 'M':
+                        if (st.st_size == 0) {
+                                fprintf(stderr, "mmap without preceeding stat, or on"
+                                        " zero length file.\n");
+                                exit(-1);
+                        }
                         mmap_len = st.st_size;
                         mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
                                         MAP_SHARED, fd, 0);
@@ -239,6 +372,17 @@ int main(int argc, char **argv)
                                 exit(save_errno);
                         }
                         break;
+               case 'n':
+                       oldpath = POP_ARG();
+                       if (oldpath == NULL)
+                               oldpath = fname;
+
+                       if (rename(oldpath, fname) < 0) {
+                               save_errno = errno;
+                               perror("rename()");
+                               exit(save_errno);
+                       }
+                       break;
                 case 'N':
                         newfile = POP_ARG();
                         if (!newfile)
@@ -260,41 +404,49 @@ int main(int argc, char **argv)
                 case 'o':
                         len = get_flags(commands+1, &flags);
                         commands += len;
-                        fd = open(fname, flags);
+                        if (flags & O_CREAT)
+                                fd = open(fname, flags, 0666);
+                        else
+                                fd = open(fname, flags);
                         if (fd == -1) {
                                 save_errno = errno;
                                 perror("open");
                                 exit(save_errno);
                         }
                         break;
-                case 'r':
-                        len = atoi(commands+1);
-                        if (len <= 0)
-                                len = 1;
-                        if (bufsize < len) {
-                                buf = realloc(buf, len + ALIGN);
-                                if (buf == NULL) {
-                                        save_errno = errno;
-                                        perror("allocating buf for read\n");
-                                        exit(save_errno);
-                                }
-                                bufsize = len;
-                                buf_align = (char *)((long)(buf + ALIGN) &
-                                                     ~ALIGN);
-                        }
-                        while (len > 0) {
-                                rc = read(fd, buf_align, len);
-                                if (rc == -1) {
-                                        save_errno = errno;
-                                        perror("read");
-                                        exit(save_errno);
-                                }
-                                if (rc < len)
-                                        fprintf(stderr, "short read: %u/%u\n",
-                                                rc, len);
-                                len -= rc;
-                        }
-                        break;
+               case 'r':
+                       len = atoi(commands+1);
+                       if (len <= 0)
+                               len = 1;
+                       if (bufsize < len) {
+                               buf = realloc(buf, len + ALIGN_LEN);
+                               if (buf == NULL) {
+                                       save_errno = errno;
+                                       perror("allocating buf for read\n");
+                                       exit(save_errno);
+                               }
+                               bufsize = len;
+                               buf_align = (char *)((long)(buf + ALIGN_LEN) &
+                                                    ~ALIGN_LEN);
+                       }
+                       while (len > 0) {
+                               rc = read(fd, buf_align, len);
+                               if (rc == -1) {
+                                       save_errno = errno;
+                                       perror("read");
+                                       exit(save_errno);
+                               }
+                               if (rc < len) {
+                                       fprintf(stderr, "short read: %u/%u\n",
+                                               rc, len);
+                                       if (rc == 0)
+                                               exit(ENODATA);
+                               }
+                               len -= rc;
+                               if (verbose >= 2)
+                                       printf("%.*s\n", rc, buf_align);
+                       }
+                       break;
                 case 'R':
                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
                                 junk += mmap_ptr[i];
@@ -344,37 +496,46 @@ int main(int argc, char **argv)
                         }
                         break;
                 case 'v':
-                        verbose = 1;
-                        break;
-                case 'w':
-                        len = atoi(commands+1);
-                        if (len <= 0)
-                                len = 1;
-                        if (bufsize < len) {
-                                buf = realloc(buf, len + ALIGN);
-                                if (buf == NULL) {
-                                        save_errno = errno;
-                                        perror("allocating buf for write\n");
-                                        exit(save_errno);
-                                }
-                                bufsize = len;
-                                buf_align = (char *)((long)(buf + ALIGN) &
-                                                     ~ALIGN);
-                                strncpy(buf_align, msg, bufsize);
-                        }
-                        while (len > 0) {
-                                rc = write(fd, buf_align, len);
-                                if (rc == -1) {
-                                        save_errno = errno;
-                                        perror("write");
-                                        exit(save_errno);
-                                }
-                                if (rc < len)
-                                        fprintf(stderr, "short write: %u/%u\n",
-                                                rc, len);
-                                len -= rc;
-                        }
+                        verbose++;
                         break;
+               case 'V':
+                       len = get_flags(commands + 1, &flags);
+                       commands += len;
+                       fd = llapi_create_volatile(fname, flags);
+                       if (fd < 0) {
+                               perror("llapi_create_volatile");
+                               exit(fd);
+                       }
+                       break;
+               case 'w':
+                       len = atoi(commands+1);
+                       if (len <= 0)
+                               len = 1;
+                       if (bufsize < len) {
+                               buf = realloc(buf, len + ALIGN_LEN);
+                               if (buf == NULL) {
+                                       save_errno = errno;
+                                       perror("allocating buf for write\n");
+                                       exit(save_errno);
+                               }
+                               bufsize = len;
+                               buf_align = (char *)((long)(buf + ALIGN_LEN) &
+                                                    ~ALIGN_LEN);
+                               strncpy(buf_align, msg, bufsize);
+                       }
+                       while (len > 0) {
+                               rc = write(fd, buf_align, len);
+                               if (rc == -1) {
+                                       save_errno = errno;
+                                       perror("write");
+                                       exit(save_errno);
+                               }
+                               if (rc < len)
+                                       fprintf(stderr, "short write: %u/%u\n",
+                                               rc, len);
+                               len -= rc;
+                       }
+                       break;
                 case 'W':
                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
                                 mmap_ptr[i] += junk++;
@@ -400,6 +561,7 @@ int main(int argc, char **argv)
                                 exit(save_errno);
                         }
                         break;
+               case '-':
                 case '0':
                 case '1':
                 case '2':