summaryrefslogtreecommitdiffstats
path: root/src/ceph/qa/btrfs/test_async_snap.c
blob: 211be95a61c2131b3a2caf14bd036a148d9ad488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>

#include <linux/ioctl.h>
#include <linux/types.h>
#include "../../src/os/btrfs_ioctl.h"

struct btrfs_ioctl_vol_args_v2 va;
struct btrfs_ioctl_vol_args vold;
int max = 4;

void check_return(int r)
{
	if (r < 0) {
		printf("********* failed with %d %s ********\n", errno, strerror(errno));
		exit(1);
	}
}

int main(int argc, char **argv)
{
	int num = 1000;

	if (argc > 1)
		num = atoi(argv[1]);
	printf("will do %d iterations\n", num);

        int cwd = open(".", O_RDONLY);
        printf("cwd = %d\n", cwd);
        while (num-- > 0) {
		if (rand() % 10 == 0) {
			__u64 transid;
			int r;
			printf("sync starting\n");
			r = ioctl(cwd, BTRFS_IOC_START_SYNC, &transid);
			check_return(r);
			printf("sync started, transid %lld, waiting\n", transid);
			r = ioctl(cwd, BTRFS_IOC_WAIT_SYNC, &transid);
			check_return(r);
			printf("sync finished\n");	
		}

                int i = rand() % max;
                struct stat st;
                va.fd = cwd;
                sprintf(va.name, "test.%d", i);
                va.transid = 0;
                int r = stat(va.name, &st);
                if (r < 0) {
			if (rand() % 3 == 0) {
				printf("snap create (sync) %s\n", va.name);
				va.flags = 0;
				r = ioctl(cwd, BTRFS_IOC_SNAP_CREATE_V2, &va);
				check_return(r);
			} else {
				printf("snap create (async) %s\n", va.name);
				va.flags = BTRFS_SUBVOL_CREATE_ASYNC;
				r = ioctl(cwd, BTRFS_IOC_SNAP_CREATE_V2, &va);
				check_return(r);
				printf("snap created, transid %lld\n", va.transid);
				if (rand() % 2 == 0) {
					printf("waiting for async snap create\n");
					r = ioctl(cwd, BTRFS_IOC_WAIT_SYNC, &va.transid);
					check_return(r);
				}
                        }
                } else {
                        printf("snap remove %s\n", va.name);
			vold.fd = va.fd;
			strcpy(vold.name, va.name);
                        r = ioctl(cwd, BTRFS_IOC_SNAP_DESTROY, &vold);
			check_return(r);
                }
        }
	return 0;
}