Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / client / test_ioctls.c
1
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <limits.h>
9
10 #include <sys/ioctl.h>
11 #include <netinet/in.h>
12 #include <sys/socket.h>
13 #include <netdb.h>
14
15 #include "ioctl.h"
16
17 char new_file_name[PATH_MAX];
18
19 int main(int argc, char **argv)
20 {
21         char *fn;
22         int fd, err;
23         struct ceph_ioctl_layout l;
24         struct ceph_ioctl_dataloc dl;
25
26         if (argc < 3) {
27                 printf("usage: ceph_test_ioctls <filename> <offset>\n");
28                 return 1;
29         }
30         fn = argv[1];
31
32         fd = open(fn, O_CREAT|O_RDWR, 0644);
33         if (fd < 0) {
34                 perror("couldn't open file");
35                 return 1;
36         }
37
38         /* get layout */
39         err = ioctl(fd, CEPH_IOC_GET_LAYOUT, (unsigned long)&l);
40         if (err < 0) {
41                 perror("ioctl IOC_GET_LAYOUT error");
42                 return 1;
43         }
44         printf("layout:\n stripe_unit %lld\n stripe_count %lld\n object_size %lld\n data_pool %lld\n",
45                (long long)l.stripe_unit, (long long)l.stripe_count, (long long)l.object_size, (long long)l.data_pool);
46
47
48         /* set layout */
49         l.stripe_unit = 1048576;
50         l.stripe_count = 2;
51         err = ioctl(fd, CEPH_IOC_SET_LAYOUT, (unsigned long)&l);
52         if (err < 0) {
53                perror("ioctl IOC_SET_LAYOUT error");
54                return 1;
55         }
56         printf("set layout, writing to file\n");
57
58         printf("file %s\n", fn);
59         /* get layout again */
60         err = ioctl(fd, CEPH_IOC_GET_LAYOUT, (unsigned long)&l);
61         if (err < 0) {
62                 perror("ioctl IOC_GET_LAYOUT error");
63                 return 1;
64         }
65         printf("layout:\n stripe_unit %lld\n stripe_count %lld\n object_size %lld\n data_pool %lld\n",
66                (long long)l.stripe_unit, (long long)l.stripe_count, (long long)l.object_size, (long long)l.data_pool);
67
68         /* dataloc */
69         dl.file_offset = atoll(argv[2]);
70         err = ioctl(fd, CEPH_IOC_GET_DATALOC, (unsigned long)&dl);
71         if (err < 0) {
72                 perror("ioctl IOC_GET_DATALOC error");
73                 return 1;
74         }
75
76         printf("dataloc:\n");
77         printf(" file_offset %lld (of object start)\n", (long long)dl.file_offset);
78         printf(" object '%s'\n object_offset %lld\n object_size %lld object_no %lld\n",
79                dl.object_name, (long long)dl.object_offset, (long long)dl.object_size, (long long)dl.object_no);
80         printf(" block_offset %lld\n block_size %lld\n",
81                (long long)dl.block_offset, (long long)dl.block_size);
82
83         char buf[80];
84         getnameinfo((struct sockaddr *)&dl.osd_addr, sizeof(dl.osd_addr), buf, sizeof(buf), 0, 0, NI_NUMERICHOST);
85         printf(" osd%lld %s\n", (long long)dl.osd, buf);
86
87         if (argc < 4)
88           return 0;
89
90         /* set dir default layout */
91         printf("testing dir policy setting\n");
92         fd = open(argv[3], O_RDONLY);
93         if (fd < 0) {
94                 perror("couldn't open dir");
95                 return 1;
96         }
97
98         l.object_size = 1048576;
99         l.stripe_count = 1;
100         err = ioctl(fd, CEPH_IOC_SET_LAYOUT_POLICY, (unsigned long)&l);
101         if (err < 0) {
102                perror("ioctl IOC_SET_LAYOUT_POLICY error");
103                return 1;
104         }
105         printf("set layout, creating file\n");
106
107         snprintf(new_file_name, sizeof(new_file_name),
108                  "%s/testfile", argv[3]);
109         fd = open(new_file_name, O_CREAT | O_RDWR, 0644);
110         if (fd < 0) {
111                 perror("couldn't open file");
112                 return 1;
113         }
114         err = ioctl(fd, CEPH_IOC_GET_LAYOUT, (unsigned long)&l);
115         if (err < 0) {
116                 perror("ioctl IOC_GET_LAYOUT error");
117                 return 1;
118         }
119         printf("layout:\n stripe_unit %lld\n stripe_count %lld\n object_size %lld\n data_pool %lld\n",
120                (long long)l.stripe_unit, (long long)l.stripe_count, (long long)l.object_size, (long long)l.data_pool);
121         return 0;
122 }