summaryrefslogtreecommitdiffstats
path: root/tests/bugs/shard/bug-1696136.c
blob: cb650535b0991baf110c574f37c5eb2636c3e95e (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>

enum fallocate_flag {
    TEST_FALLOCATE_NONE,
    TEST_FALLOCATE_KEEP_SIZE,
    TEST_FALLOCATE_ZERO_RANGE,
    TEST_FALLOCATE_PUNCH_HOLE,
    TEST_FALLOCATE_MAX,
};

int
get_fallocate_flag(int opcode)
{
    int ret = 0;

    switch (opcode) {
        case TEST_FALLOCATE_NONE:
            ret = 0;
            break;
        case TEST_FALLOCATE_KEEP_SIZE:
            ret = FALLOC_FL_KEEP_SIZE;
            break;
        case TEST_FALLOCATE_ZERO_RANGE:
            ret = FALLOC_FL_ZERO_RANGE;
            break;
        case TEST_FALLOCATE_PUNCH_HOLE:
            ret = FALLOC_FL_PUNCH_HOLE;
            break;
        default:
            ret = -1;
            break;
    }
    return ret;
}

int
main(int argc, char *argv[])
{
    int ret = 1;
    int opcode = -1;
    off_t offset = 0;
    size_t len = 0;
    glfs_t *fs = NULL;
    glfs_fd_t *fd = NULL;

    if (argc != 8) {
        fprintf(stderr,
                "Syntax: %s <host> <volname> <opcode> <offset> <len> "
                "<file-path> <log-file>\n",
                argv[0]);
        return 1;
    }

    fs = glfs_new(argv[2]);
    if (!fs) {
        fprintf(stderr, "glfs_new: returned NULL\n");
        return 1;
    }

    ret = glfs_set_volfile_server(fs, "tcp", argv[1], 24007);
    if (ret != 0) {
        fprintf(stderr, "glfs_set_volfile_server: returned %d\n", ret);
        goto out;
    }

    ret = glfs_set_logging(fs, argv[7], 7);
    if (ret != 0) {
        fprintf(stderr, "glfs_set_logging: returned %d\n", ret);
        goto out;
    }

    ret = glfs_init(fs);
    if (ret != 0) {
        fprintf(stderr, "glfs_init: returned %d\n", ret);
        goto out;
    }

    opcode = atoi(argv[3]);
    opcode = get_fallocate_flag(opcode);
    if (opcode < 0) {
        fprintf(stderr, "get_fallocate_flag: invalid flag \n");
        goto out;
    }

    /* Note that off_t is signed but size_t isn't. */
    offset = strtol(argv[4], NULL, 10);
    len = strtoul(argv[5], NULL, 10);

    fd = glfs_open(fs, argv[6], O_RDWR);
    if (fd == NULL) {
        fprintf(stderr, "glfs_open: returned NULL\n");
        goto out;
    }

    ret = glfs_fallocate(fd, opcode, offset, len);
    if (ret < 0) {
        fprintf(stderr, "glfs_fallocate: returned %d\n", ret);
        goto out;
    }

    ret = glfs_unlink(fs, argv[6]);
    if (ret < 0) {
        fprintf(stderr, "glfs_unlink: returned %d\n", ret);
        goto out;
    }
    /* Sleep for 3s to give enough time for background deletion to complete
     * during which if the bug exists, the process will crash.
     */
    sleep(3);
    ret = 0;

out:
    if (fd)
        glfs_close(fd);
    glfs_fini(fs);
    return ret;
}