Whamcloud - gitweb
land b_smallfix 20040407_1414:
[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/lov/lov1/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/lov/lov1/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/lov/lov1/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                 return -1;
99         
100         stripe_offset = (short int)lum_dir->lmm_stripe_offset;
101         if (stripe_offset != -1) {
102                 for (i = 0; i < stripe_count; i++)
103                         if (lum_file1->lmm_objects[i].l_ost_idx != 
104                             (stripe_offset + i) % ost_count) 
105                                 return -1;
106         } else if (lum_file2 != NULL) {
107                 int next, idx;
108                 next = (lum_file1->lmm_objects[stripe_count-1].l_ost_idx + 1)
109                        % ost_count;
110                 idx = lum_file2->lmm_objects[0].l_ost_idx;
111                 if (idx != next) 
112                         return -1;
113         }
114
115         return 0;        
116 }
117
118 int main(int argc, char **argv)
119 {
120         DIR * dir;
121         struct lov_user_md *lum_dir, *lum_file1 = NULL, *lum_file2 = NULL;
122         int rc;
123         int lum_size;
124         char *fname;
125
126         if (argc < 3) {
127                 fprintf(stderr, "Usage: %s <dirname> <filename1> [filename2]\n",
128                         argv[0]);
129                 exit(1);
130         }
131
132         dir = opendir(argv[1]);
133         if (dir  == NULL) {
134                 fprintf(stderr, "%s opendir failed\n", argv[1]);
135                 return errno;
136         }
137
138         lum_size = lov_mds_md_size(MAX_LOV_UUID_COUNT);
139         if ((lum_dir = (struct lov_user_md *)malloc(lum_size)) == NULL) {
140                 fprintf(stderr, "unable to allocate memory for ioctl's");
141                 return errno;
142         }        
143
144         rc = ioctl(dirfd(dir), LL_IOC_LOV_GETSTRIPE, lum_dir);
145         if (rc) {
146                 if (errno == ENODATA) {
147                         lum_dir->lmm_stripe_size = 0;
148                         lum_dir->lmm_stripe_count = 0;
149                         lum_dir->lmm_stripe_offset = -1;
150                 } else {
151                         rc = errno;
152                         goto cleanup;
153                 }       
154         }
155
156         if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
157                 fprintf(stderr, "unable to allocate memory for ioctl's");
158                 rc = errno;
159                 goto cleanup;
160         }
161
162         fname = strrchr(argv[2], '/');
163         fname++;
164         strncpy((char *)lum_file1, fname, lum_size);
165         rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file1);
166         if (rc) {
167                 rc = errno;
168                 goto cleanup;
169         }
170
171         if (argc == 4) {
172                 if ((lum_file2 = (struct lov_user_md *)malloc(lum_size)) 
173                     == NULL) {
174                         fprintf(stderr, 
175                                 "unable to allocate memory for ioctl's");
176                         rc = errno;
177                         goto cleanup;
178                 }
179
180                 fname = strrchr(argv[3], '/');
181                 fname++;
182                 strncpy((char *)lum_file2, fname, lum_size);
183                 rc = ioctl(dirfd(dir), IOC_MDC_GETSTRIPE, lum_file2);
184                 if (rc) {
185                         rc = errno;
186                         goto cleanup;
187                 }
188         }
189
190         rc = compare(lum_dir, lum_file1, lum_file2);
191
192 cleanup:
193         if (lum_dir != NULL)
194                 free(lum_dir);
195         if (lum_file1 != NULL)
196                 free(lum_file1);
197         if (lum_file2 != NULL)
198                 free(lum_file2);
199
200         return rc;
201 }