Whamcloud - gitweb
LU-5456 hsm: hold inode mutex around ll_setattr_raw()
[fs/lustre-release.git] / lustre / liblustre / tests / echo_test.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/liblustre/tests/echo_test.c
35  *
36  * Lustre Light user test program
37  */
38
39 #include <liblustre.h>
40 #include <obd.h>
41 #include <obd_class.h>
42
43 #define LIBLUSTRE_TEST 1
44 #include "../utils/lctl.c"
45
46 #include "../lutil.h"
47
48 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
49
50 static int liblustre_ioctl(int dev_id, unsigned int opc, void *ptr)
51 {
52         int   rc = -EINVAL;
53         
54         switch (dev_id) {
55         default:
56                 fprintf(stderr, "Unexpected device id %d\n", dev_id);
57                 abort();
58                 break;
59                 
60         case OBD_DEV_ID:
61                 rc = class_handle_ioctl(opc, (unsigned long)ptr);
62                 break;
63         }
64
65         return rc;
66 }
67
68 static char *echo_server_nid = NULL;
69 static char *echo_server_ostname = "obd1";
70 static char *osc_dev_name = "OSC_DEV_NAME";
71 static char *echo_dev_name = "ECHO_CLIENT_DEV_NAME";
72
73 static int connect_echo_client(void)
74 {
75         struct lustre_cfg *lcfg;
76         struct lustre_cfg_bufs bufs;
77         lnet_nid_t nid;
78         char *peer = "ECHO_PEER_NID";
79         class_uuid_t osc_uuid, echo_uuid;
80         struct obd_uuid osc_uuid_str, echo_uuid_str;
81         int err;
82         ENTRY;
83
84         ll_generate_random_uuid(osc_uuid);
85         class_uuid_unparse(osc_uuid, &osc_uuid_str);
86         ll_generate_random_uuid(echo_uuid);
87         class_uuid_unparse(echo_uuid, &echo_uuid_str);
88
89         nid = libcfs_str2nid(echo_server_nid);
90         if (nid == LNET_NID_ANY) {
91                 CERROR("Can't parse NID %s\n", echo_server_nid);
92                 RETURN(-EINVAL);
93         }
94
95         /* add uuid */
96         lustre_cfg_bufs_reset(&bufs, NULL);
97         lustre_cfg_bufs_set_string(&bufs, 1, peer);
98         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
99         lcfg->lcfg_nid = nid;
100         err = class_process_config(lcfg);
101         lustre_cfg_free(lcfg);
102         if (err < 0) {
103                 CERROR("failed add_uuid\n");
104                 RETURN(-EINVAL);
105         }
106
107         /* attach osc */
108         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
109         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_OSC_NAME);
110         lustre_cfg_bufs_set_string(&bufs, 2, osc_uuid_str.uuid);
111         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
112         err = class_process_config(lcfg);
113         lustre_cfg_free(lcfg);
114         if (err < 0) {
115                 CERROR("failed attach osc\n");
116                 RETURN(-EINVAL);
117         }
118
119         /* setup osc */
120         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
121         lustre_cfg_bufs_set_string(&bufs, 1, echo_server_ostname);
122         lustre_cfg_bufs_set_string(&bufs, 2, peer);
123         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
124         err = class_process_config(lcfg);
125         lustre_cfg_free(lcfg);
126         if (err < 0) {
127                 CERROR("failed setup osc\n");
128                 RETURN(-EINVAL);
129         }
130
131         /* attach echo_client */
132         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
133         lustre_cfg_bufs_set_string(&bufs, 1, "echo_client");
134         lustre_cfg_bufs_set_string(&bufs, 2, echo_uuid_str.uuid);
135         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
136         err = class_process_config(lcfg);
137         lustre_cfg_free(lcfg);
138         if (err < 0) {
139                 CERROR("failed attach echo_client\n");
140                 RETURN(-EINVAL);
141         }
142
143         /* setup echo_client */
144         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
145         lustre_cfg_bufs_set_string(&bufs, 1, osc_dev_name);
146         lustre_cfg_bufs_set_string(&bufs, 2, NULL);
147         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
148         err = class_process_config(lcfg);
149         lustre_cfg_free(lcfg);
150         if (err < 0) {
151                 CERROR("failed setup echo_client\n");
152                 RETURN(-EINVAL);
153         }
154
155         RETURN(0);
156 }
157
158 static int disconnect_echo_client(void)
159 {
160         struct lustre_cfg_bufs bufs;
161         struct lustre_cfg *lcfg = NULL;
162         int err;
163         ENTRY;
164
165         /* cleanup echo_client */
166         lustre_cfg_bufs_reset(&bufs, echo_dev_name);
167         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
168         err = class_process_config(lcfg);
169         if (err < 0) {
170                 lustre_cfg_free(lcfg);
171                 CERROR("failed cleanup echo_client\n");
172                 RETURN(-EINVAL);
173         }
174
175         /* detach echo_client */
176         lcfg->lcfg_command = LCFG_DETACH;
177         err = class_process_config(lcfg);
178         lustre_cfg_free(lcfg);
179         if (err < 0) {
180                 CERROR("failed detach echo_client\n");
181                 RETURN(-EINVAL);
182         }
183
184         /* cleanup osc */
185         lustre_cfg_bufs_reset(&bufs, osc_dev_name);
186         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
187         err = class_process_config(lcfg);
188         if (err < 0) {
189                 lustre_cfg_free(lcfg);
190                 CERROR("failed cleanup osc device\n");
191                 RETURN(-EINVAL);
192         }
193
194         /* detach osc */
195         lcfg->lcfg_command = LCFG_DETACH;
196         err = class_process_config(lcfg);
197         lustre_cfg_free(lcfg);
198         if (err < 0) {
199                 CERROR("failed detach osc device\n");
200                 RETURN(-EINVAL);
201         }
202
203         RETURN(0);
204 }
205
206 static void usage(const char *s)
207 {
208         printf("Usage: %s -s ost_host_name [-n ost_name] [-x lctl_options ...]\n", s);
209         printf("    ost_host_name: the host name of echo server\n");
210         printf("    ost_name: ost name, default is \"obd1\"\n");
211         printf("    lctl_options: options to pass to lctl.\n");
212         printf("            (e.g. -x --device 1 test_getattr 10000 -5)\n");
213 }
214
215 extern int time_ptlwait1;
216 extern int time_ptlwait2;
217 extern int time_ptlselect;
218
219 int main(int argc, char **argv) 
220 {
221         int c, rc;
222         int xindex  = -1;  /* index of -x option */
223
224         /* loop until all options are consumed or we hit
225          * a -x option 
226          */
227         while ((c = getopt(argc, argv, "s:n:x:")) != -1 && 
228                xindex == -1) {
229                 switch (c) {
230                 case 's':
231                         echo_server_nid = optarg;
232                         break;
233                 case 'n':
234                         echo_server_ostname = optarg;
235                         break;
236                 case 'x':
237                         xindex = optind-1;
238                         break;
239                 default:
240                         usage(argv[0]);
241                         return 1;
242                 }
243         }
244
245         /*
246          * Only warn with usage() if the -x option isn't specificed
247          * because when using -x this check is not valid.
248          */
249         if (optind != argc && xindex == -1)
250                 usage(argv[0]);
251
252         if (!echo_server_nid) {
253                 usage(argv[0]);
254                 return 1;
255         }
256
257         libcfs_debug = 0;
258         libcfs_subsystem_debug = 0;
259
260         liblustre_init_random();
261
262         if (liblustre_init_current(argv[0]) ||
263             init_obdclass() || init_lib_portals() ||
264             ptlrpc_init() ||
265             lmv_init() ||
266             mdc_init() ||
267             lov_init() ||
268             osc_init() ||
269             echo_client_init()) {
270                 printf("error\n");
271                 return 1;
272         }
273
274         rc = connect_echo_client();
275         if (rc)
276                 return rc;
277
278         set_ioc_handler(liblustre_ioctl);
279
280
281         /*
282          * If the -x option is not specified pass no args to lctl
283          * otherwise pass all the options after the "-x" to lctl
284          *
285          * HACK: in the case when the -x option is specified
286          * lctl sees argv[0] == "-x" and not the real argv[0] seen
287          * in this function.  If that is a problem, a mapping will
288          * have to be done to fix that.  However for normal functioning
289          * it seems to be irrelavant
290          */
291         if( xindex == -1 )
292                 rc = lctl_main(1, &argv[0]);
293         else
294                 rc = lctl_main(argc-xindex+1, &argv[xindex-1]);
295
296         rc |= disconnect_echo_client();
297
298         return rc;
299 }