Whamcloud - gitweb
- landed b_hd_cray_merge3
[fs/lustre-release.git] / lustre / tests / ll_dirstripe_verify.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * ll_dirstripe_verify <dir> <file>:
5  * - to verify if the file has the same lov_user_md setting as the parent dir.
6  * - if dir's offset is set -1, ll_dirstripe_verify <dir> <file1> <file2>
7  *      is used to further verify if file1 and file2's obdidx is continuous.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/ioctl.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <dirent.h>
18
19 #include <liblustre.h>
20 #include <linux/obd.h>
21 #include <linux/lustre_lib.h>
22 #include <lustre/lustre_user.h>
23 #include <linux/obd_lov.h>
24
25 #include <portals/ptlctl.h>
26
27
28 #define MAX_LOV_UUID_COUNT      1000
29
30 int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
31             struct lov_user_md *lum_file2)
32 {
33         int stripe_count;
34         int stripe_size;
35         int stripe_offset;
36         int ost_count;
37         int fd;
38         char buf[32];
39         int i;
40
41         stripe_count = (int)lum_dir->lmm_stripe_count;
42         if (stripe_count == 0) {
43                 fd = open("/proc/fs/lustre/llite/fs0/lov/stripecount", O_RDONLY);
44                 if (fd == -1) {
45                         fprintf(stderr, "open proc file error: %s\n", 
46                                 strerror(errno));
47                         return -1; 
48                 }
49                 if (read(fd, buf, sizeof(buf)) == -1) {
50                         fprintf(stderr, "read proc file error: %s\n", 
51                                 strerror(errno));
52                         close(fd);
53                         return -1;
54                 }
55                 
56                 stripe_count = atoi(buf);
57                 stripe_count = stripe_count ? stripe_count : 1;
58                 close(fd);
59         }
60
61         stripe_size = (int)lum_dir->lmm_stripe_size;
62         if (stripe_size == 0) {
63                 fd = open("/proc/fs/lustre/llite/fs0/lov/stripesize", O_RDONLY);
64                 if (fd == -1) {
65                         fprintf(stderr, "open proc file error: %s\n", 
66                                 strerror(errno)); 
67                         return -1; 
68                 }
69                 if (read(fd, buf, sizeof(buf)) == -1) {
70                         fprintf(stderr, "read proc file error: %s\n", 
71                                 strerror(errno));
72                         close(fd);
73                         return -1;
74                 }
75
76                 stripe_size = atoi(buf);
77                 close(fd);
78         }
79
80         fd = open("/proc/fs/lustre/llite/fs0/lov/numobd", O_RDONLY);
81         if(fd  == -1) {
82                 fprintf(stderr, "open proc file error: %s\n", 
83                         strerror(errno));
84                 return -1;
85         }
86         if (read(fd, buf, sizeof(buf)) == -1) {
87                 fprintf(stderr, "read proc file error: %s\n", 
88                         strerror(errno));
89                 close(fd);
90                 return -1;
91         }
92
93         ost_count = atoi(buf);
94         close(fd);
95
96         if ((lum_file1->lmm_stripe_count != stripe_count) ||
97             (lum_file1->lmm_stripe_size != stripe_size))
98         {
99                 return -1;
100         }
101         
102         stripe_offset = (short int)lum_dir->lmm_stripe_offset;
103         if (stripe_offset != -1) {
104                 for (i = 0; i < stripe_count; i++)
105                         if (lum_file1->lmm_objects[i].l_ost_idx != 
106                             (stripe_offset + i) % ost_count) 
107                                 return -1;
108         } else if (lum_file2 != NULL) {
109                 int next, idx;
110                 next = (lum_file1->lmm_objects[stripe_count-1].l_ost_idx + 1)
111                        % ost_count;
112                 idx = lum_file2->lmm_objects[0].l_ost_idx;
113                 if (idx != next) 
114                         return -1;
115         }
116
117         return 0;        
118 }
119
120 int main(int argc, char **argv)
121 {
122         DIR * dir;
123         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
124         int rc;
125         int lum_size;
126         char *fname;
127
128         if (argc < 3) {
129                 fprintf(stderr, "Usage: %s <dirname> <filename1> [filename2]\n",
130                         argv[0]);
131                 exit(1);
132         }
133
134         dir = opendir(argv[1]);
135         if (dir  == NULL) {
136                 fprintf(stderr, "%s opendir failed\n", argv[1]);
137                 return errno;
138         }
139
140         lum_size = lov_mds_md_size(MAX_LOV_UUID_COUNT);
141         if ((lum_dir = (struct lov_user_md *)malloc(lum_size)) == NULL) {
142                 fprintf(stderr, "unable to allocate memory for ioctl's");
143                 return errno;
144         }        
145
146         rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, lum_dir);
147         if (rc) {
148                 if (errno == ENODATA) {
149                         lum_dir->lmm_stripe_size = 0;
150                         lum_dir->lmm_stripe_count = 0;
151                         lum_dir->lmm_stripe_offset = -1;
152                 } else {
153                         rc = errno;
154                         goto cleanup;
155                 }       
156         }
157
158         if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
159                 fprintf(stderr, "unable to allocate memory for ioctl's");
160                 rc = errno;
161                 goto cleanup;
162         }
163
164         fname = strrchr(argv[2], '/');
165         fname++;
166         strncpy((char *)lum_file1, fname, lum_size);
167         rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file1);
168         if (rc) {
169                 rc = errno;
170                 goto cleanup;
171         }
172
173         if (argc == 4) {
174                 if ((lum_file2 = (struct lov_user_md *)malloc(lum_size)) 
175                     == NULL) {
176                         fprintf(stderr, 
177                                 "unable to allocate memory for ioctl's");
178                         rc = errno;
179                         goto cleanup;
180                 }
181
182                 fname = strrchr(argv[3], '/');
183                 fname++;
184                 strncpy((char *)lum_file2, fname, lum_size);
185                 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file2);
186                 if (rc) {
187                         rc = errno;
188                         goto cleanup;
189                 }
190         }
191
192         rc = compare(lum_dir, lum_file1, lum_file2);
193
194 cleanup:
195         if (lum_dir != NULL)
196                 free(lum_dir);
197         if (lum_file1 != NULL)
198                 free(lum_file1);
199         if (lum_file2 != NULL)
200                 free(lum_file2);
201
202         return rc;
203 }