summaryrefslogtreecommitdiffstats
path: root/src/client/test_ioctls.c
blob: 23fa835c54e3ee011e2165461c4532fd6c77d068 (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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>

#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>

#include "ioctl.h"

char new_file_name[PATH_MAX];

int main(int argc, char **argv)
{
	char *fn;
	int fd, err;
	struct ceph_ioctl_layout l;
	struct ceph_ioctl_dataloc dl;

	if (argc < 3) {
		printf("usage: ceph_test_ioctls <filename> <offset>\n");
		return 1;
	}
	fn = argv[1];

	fd = open(fn, O_CREAT|O_RDWR, 0644);
	if (fd < 0) {
		perror("couldn't open file");
		return 1;
	}

	/* get layout */
        err = ioctl(fd, CEPH_IOC_GET_LAYOUT, (unsigned long)&l);
        if (err < 0) {
                perror("ioctl IOC_GET_LAYOUT error");
                return 1;
        }
        printf("layout:\n stripe_unit %lld\n stripe_count %lld\n object_size %lld\n data_pool %lld\n",
               (long long)l.stripe_unit, (long long)l.stripe_count, (long long)l.object_size, (long long)l.data_pool);


        /* set layout */
        l.stripe_unit = 1048576;
        l.stripe_count = 2;
        err = ioctl(fd, CEPH_IOC_SET_LAYOUT, (unsigned long)&l);
        if (err < 0) {
               perror("ioctl IOC_SET_LAYOUT error");
               return 1;
        }
        printf("set layout, writing to file\n");

	printf("file %s\n", fn);
	/* get layout again */
	err = ioctl(fd, CEPH_IOC_GET_LAYOUT, (unsigned long)&l);
	if (err < 0) {
		perror("ioctl IOC_GET_LAYOUT error");
		return 1;
	}
	printf("layout:\n stripe_unit %lld\n stripe_count %lld\n object_size %lld\n data_pool %lld\n",
	       (long long)l.stripe_unit, (long long)l.stripe_count, (long long)l.object_size, (long long)l.data_pool);

	/* dataloc */
	dl.file_offset = atoll(argv[2]);
	err = ioctl(fd, CEPH_IOC_GET_DATALOC, (unsigned long)&dl);
	if (err < 0) {
		perror("ioctl IOC_GET_DATALOC error");
		return 1;
	}

	printf("dataloc:\n");
	printf(" file_offset %lld (of object start)\n", (long long)dl.file_offset);
	printf(" object '%s'\n object_offset %lld\n object_size %lld object_no %lld\n",
	       dl.object_name, (long long)dl.object_offset, (long long)dl.object_size, (long long)dl.object_no);
	printf(" block_offset %lld\n block_size %lld\n",
	       (long long)dl.block_offset, (long long)dl.block_size);

	char buf[80];
	getnameinfo((struct sockaddr *)&dl.osd_addr, sizeof(dl.osd_addr), buf, sizeof(buf), 0, 0, NI_NUMERICHOST);
	printf(" osd%lld %s\n", (long long)dl.osd, buf);

	if (argc < 4)
	  return 0;

	/* set dir default layout */
	printf("testing dir policy setting\n");
	fd = open(argv[3], O_RDONLY);
        if (fd < 0) {
                perror("couldn't open dir");
                return 1;
        }

        l.object_size = 1048576;
        l.stripe_count = 1;
        err = ioctl(fd, CEPH_IOC_SET_LAYOUT_POLICY, (unsigned long)&l);
        if (err < 0) {
               perror("ioctl IOC_SET_LAYOUT_POLICY error");
               return 1;
        }
        printf("set layout, creating file\n");

        snprintf(new_file_name, sizeof(new_file_name),
		 "%s/testfile", argv[3]);
        fd = open(new_file_name, O_CREAT | O_RDWR, 0644);
        if (fd < 0) {
                perror("couldn't open file");
                return 1;
        }
        err = ioctl(fd, CEPH_IOC_GET_LAYOUT, (unsigned long)&l);
        if (err < 0) {
                perror("ioctl IOC_GET_LAYOUT error");
                return 1;
        }
        printf("layout:\n stripe_unit %lld\n stripe_count %lld\n object_size %lld\n data_pool %lld\n",
               (long long)l.stripe_unit, (long long)l.stripe_count, (long long)l.object_size, (long long)l.data_pool);
        return 0;
}