Whamcloud - gitweb
like a bad penny
[fs/lustre-release.git] / lustre / tests / lovstripe.c
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6 #include <string.h>
7 #include <errno.h>
8
9
10 /****************** Custom includes ********************/
11 #include <linux/lustre_lite.h>
12 #include <linux/lustre_idl.h>
13
14
15 /******************  Functions ******************/
16 int write_file(char *name, struct lov_mds_md *striping, int bufsize,
17                char *buf1, char *buf2);
18
19
20 /************************  Main **********************/
21
22 #define STRIPE_SIZE 128 * 1024
23
24 int main(int argc, char *argv[])
25 {
26         struct lov_mds_md a_striping;
27         long bufsize = sizeof(long) * STRIPE_SIZE;
28         char *rbuf, *wbuf;
29         int data, *dp;
30         int result;
31
32         rbuf = malloc(bufsize);
33         wbuf = malloc(bufsize);
34         if (!rbuf || !wbuf) {
35                 fprintf(stderr, "%s: unable to allocate buffers\n", argv[0]);
36                 return 1;
37         }
38
39         /* Initialize to an easily-verified pattern */
40         for (data = 0, dp = (int *)wbuf; data < STRIPE_SIZE; data++, dp++)
41                 *dp = data;
42
43         /*  Init defaults on striping info  */
44         a_striping.lmm_magic = LOV_MAGIC;
45         a_striping.lmm_stripe_size = STRIPE_SIZE;
46         a_striping.lmm_stripe_pattern = 0;
47
48         /*  Write file for OST1 only  */
49         /*       Start at OST 0, and use only 1 OST  */
50         a_striping.lmm_stripe_offset = 0;
51         a_striping.lmm_stripe_count = 1;
52
53         result = write_file("/mnt/lustre/ost1", &a_striping, bufsize,
54                             wbuf, rbuf);
55
56         if (result < 0)
57                 goto out;
58
59         /*  Write file for OST2 only  */
60         /*       Start at OST 1, and use only 1 OST  */
61         a_striping.lmm_stripe_offset = 1;
62         a_striping.lmm_stripe_count = 1;
63
64         result = write_file("/mnt/lustre/ost2", &a_striping, bufsize,
65                             wbuf, rbuf);
66
67         if (result < 0)
68                 goto out;
69
70         /*  Write file across both OST1 and OST2  */
71         /*       Start at OST 0, and use only 2 OSTs  */
72         a_striping.lmm_stripe_offset = 0;
73         a_striping.lmm_stripe_count = 2;
74
75         result = write_file("/mnt/lustre/ost1and2", &a_striping, bufsize,
76                             wbuf, rbuf);
77
78         if (result < 0)
79                 goto out;
80
81 out:
82         free(rbuf);
83         free(wbuf);
84         return result;
85 }
86
87
88 int write_file(char *name, struct lov_mds_md *striping, int bufsize,
89                char *wbuf, char *rbuf)
90 {
91         int fd, result;
92
93         printf("opening %s\n", name);
94         fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
95         if (fd < 0) {
96                 fprintf(stderr, "\nUnable to open '%s': %s\n",
97                          name, strerror(errno));
98                 return -errno;
99         }
100
101         printf("setting stripe data on %s\n", name);
102         result = ioctl(fd, LL_IOC_LOV_SETSTRIPE, striping);
103         if (result < 0) {
104                 fprintf(stderr, "\nError on ioctl for '%s' (%d): %s\n",
105                         name, fd, strerror(errno));
106                 close(fd);
107                 return -errno;
108         }
109
110         /*  Write bogus data  */
111         printf("writing data to %s\n", name);
112         result = write(fd, wbuf, bufsize);
113         if (result < 0) {
114                 fprintf(stderr, "\nerror: writing data to '%s' (%d): %s\n",
115                         name, fd, strerror(errno));
116                 close(fd);
117                 return -errno;
118         }
119
120         if (result != bufsize) {
121                 fprintf(stderr, "\nerror: short write to '%s' (%d): %d != %d\n",
122                         name, fd, result, bufsize);
123                 close(fd);
124                 return -1;
125         }
126
127         /*  Seek to beginning again */
128         printf("seeking in %s\n", name);
129         result = lseek(fd, 0, SEEK_SET);
130         if (result < 0) {
131                 fprintf(stderr, "\nerror: seeking to beginning '%s' (%d): %s\n",
132                         name, fd, strerror(errno));
133                 close(fd);
134                 return -errno;
135         }
136
137         /*  Read bogus data back  */
138         printf("reading data from %s\n", name);
139         result = read(fd, rbuf, bufsize);
140         if (result < 0) {
141                 fprintf(stderr, "\nerror: reading data from '%s' (%d): %s\n",
142                         name, fd, strerror(errno));
143                 close(fd);
144                 return -errno;
145         }
146
147         if (result != bufsize) {
148                 fprintf(stderr,"\nerror: short read from '%s' (%d): %d != %d\n",
149                         name, fd, result, bufsize);
150                 close(fd);
151                 return -1;
152         }
153
154         if (memcmp(wbuf, rbuf, bufsize)) {
155                 fprintf(stderr, "\nerror: comparing data in '%s' (%d): %s\n",
156                         name, fd, strerror(errno));
157                 close(fd);
158                 return -1;
159         }
160
161         close(fd);
162
163         return 0;
164 }