Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / btrfs / test_async_snap.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include <sys/ioctl.h>
9 #include <string.h>
10
11 #include <linux/ioctl.h>
12 #include <linux/types.h>
13 #include "../../src/os/btrfs_ioctl.h"
14
15 struct btrfs_ioctl_vol_args_v2 va;
16 struct btrfs_ioctl_vol_args vold;
17 int max = 4;
18
19 void check_return(int r)
20 {
21         if (r < 0) {
22                 printf("********* failed with %d %s ********\n", errno, strerror(errno));
23                 exit(1);
24         }
25 }
26
27 int main(int argc, char **argv)
28 {
29         int num = 1000;
30
31         if (argc > 1)
32                 num = atoi(argv[1]);
33         printf("will do %d iterations\n", num);
34
35         int cwd = open(".", O_RDONLY);
36         printf("cwd = %d\n", cwd);
37         while (num-- > 0) {
38                 if (rand() % 10 == 0) {
39                         __u64 transid;
40                         int r;
41                         printf("sync starting\n");
42                         r = ioctl(cwd, BTRFS_IOC_START_SYNC, &transid);
43                         check_return(r);
44                         printf("sync started, transid %lld, waiting\n", transid);
45                         r = ioctl(cwd, BTRFS_IOC_WAIT_SYNC, &transid);
46                         check_return(r);
47                         printf("sync finished\n");      
48                 }
49
50                 int i = rand() % max;
51                 struct stat st;
52                 va.fd = cwd;
53                 sprintf(va.name, "test.%d", i);
54                 va.transid = 0;
55                 int r = stat(va.name, &st);
56                 if (r < 0) {
57                         if (rand() % 3 == 0) {
58                                 printf("snap create (sync) %s\n", va.name);
59                                 va.flags = 0;
60                                 r = ioctl(cwd, BTRFS_IOC_SNAP_CREATE_V2, &va);
61                                 check_return(r);
62                         } else {
63                                 printf("snap create (async) %s\n", va.name);
64                                 va.flags = BTRFS_SUBVOL_CREATE_ASYNC;
65                                 r = ioctl(cwd, BTRFS_IOC_SNAP_CREATE_V2, &va);
66                                 check_return(r);
67                                 printf("snap created, transid %lld\n", va.transid);
68                                 if (rand() % 2 == 0) {
69                                         printf("waiting for async snap create\n");
70                                         r = ioctl(cwd, BTRFS_IOC_WAIT_SYNC, &va.transid);
71                                         check_return(r);
72                                 }
73                         }
74                 } else {
75                         printf("snap remove %s\n", va.name);
76                         vold.fd = va.fd;
77                         strcpy(vold.name, va.name);
78                         r = ioctl(cwd, BTRFS_IOC_SNAP_DESTROY, &vold);
79                         check_return(r);
80                 }
81         }
82         return 0;
83 }