Whamcloud - gitweb
804a7a02e581bd15c6c344442690f88ecd70d12c
[fs/lustre-release.git] / lnet / selftest / conctl.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/selftest/conctl.c
37  *
38  * IOC handle in kernel
39  *
40  * Author: Liang Zhen <liangzhen@clusterfs.com>
41  */
42
43 #include <libcfs/libcfs.h>
44 #include <lnet/lib-lnet.h>
45 #include <lnet/lnetst.h>
46 #include "console.h"
47
48 static int
49 lst_session_new_ioctl(lstio_session_new_args_t *args)
50 {
51         char      *name;
52         int        rc;
53
54         if (args->lstio_ses_idp   == NULL || /* address for output sid */
55             args->lstio_ses_key   == 0 || /* no key is specified */
56             args->lstio_ses_namep == NULL || /* session name */
57             args->lstio_ses_nmlen <= 0 ||
58             args->lstio_ses_nmlen > LST_NAME_SIZE)
59                 return -EINVAL;
60
61         LIBCFS_ALLOC(name, args->lstio_ses_nmlen + 1);
62         if (name == NULL)
63                 return -ENOMEM;
64
65         if (copy_from_user(name, args->lstio_ses_namep,
66                            args->lstio_ses_nmlen)) {
67                 LIBCFS_FREE(name, args->lstio_ses_nmlen + 1);
68                 return -EFAULT;
69         }
70
71         name[args->lstio_ses_nmlen] = 0;
72
73         rc = lstcon_session_new(name,
74                                 args->lstio_ses_key,
75                                 args->lstio_ses_feats,
76                                 args->lstio_ses_force,
77                                 args->lstio_ses_timeout,
78                                 args->lstio_ses_idp);
79
80         LIBCFS_FREE(name, args->lstio_ses_nmlen + 1);
81         return rc;
82 }
83
84 static int
85 lst_session_end_ioctl(lstio_session_end_args_t *args)
86 {
87         if (args->lstio_ses_key != console_session.ses_key)
88                 return -EACCES;
89
90         return lstcon_session_end();
91 }
92
93 static int
94 lst_session_info_ioctl(lstio_session_info_args_t *args)
95 {
96         /* no checking of key */
97
98         if (args->lstio_ses_idp   == NULL || /* address for ouput sid */
99             args->lstio_ses_keyp  == NULL || /* address for ouput key */
100             args->lstio_ses_featp  == NULL || /* address for ouput features */
101             args->lstio_ses_ndinfo == NULL || /* address for output ndinfo */
102             args->lstio_ses_namep == NULL || /* address for ouput name */
103             args->lstio_ses_nmlen <= 0 ||
104             args->lstio_ses_nmlen > LST_NAME_SIZE)
105                 return -EINVAL;
106
107         return lstcon_session_info(args->lstio_ses_idp,
108                                    args->lstio_ses_keyp,
109                                    args->lstio_ses_featp,
110                                    args->lstio_ses_ndinfo,
111                                    args->lstio_ses_namep,
112                                    args->lstio_ses_nmlen);
113 }
114
115 static int
116 lst_debug_ioctl(lstio_debug_args_t *args)
117 {
118         char   *name   = NULL;
119         int     client = 1;
120         int     rc;
121
122         if (args->lstio_dbg_key != console_session.ses_key)
123                 return -EACCES;
124
125         if (args->lstio_dbg_resultp == NULL)
126                 return -EINVAL;
127
128         if (args->lstio_dbg_namep != NULL && /* name of batch/group */
129             (args->lstio_dbg_nmlen <= 0 ||
130              args->lstio_dbg_nmlen > LST_NAME_SIZE))
131                 return -EINVAL;
132
133         if (args->lstio_dbg_namep != NULL) {
134                 LIBCFS_ALLOC(name, args->lstio_dbg_nmlen + 1);
135                 if (name == NULL)
136                         return -ENOMEM;
137
138                 if (copy_from_user(name, args->lstio_dbg_namep,
139                                        args->lstio_dbg_nmlen)) {
140                         LIBCFS_FREE(name, args->lstio_dbg_nmlen + 1);
141
142                         return -EFAULT;
143                 }
144
145                 name[args->lstio_dbg_nmlen] = 0;
146         }
147
148         rc = -EINVAL;
149
150         switch (args->lstio_dbg_type) {
151         case LST_OPC_SESSION:
152                 rc = lstcon_session_debug(args->lstio_dbg_timeout,
153                                           args->lstio_dbg_resultp);
154                 break;
155
156         case LST_OPC_BATCHSRV:
157                 client = 0;
158         case LST_OPC_BATCHCLI:
159                 if (name == NULL)
160                         goto out;
161
162                 rc = lstcon_batch_debug(args->lstio_dbg_timeout,
163                                         name, client, args->lstio_dbg_resultp);
164                 break;
165
166         case LST_OPC_GROUP:
167                 if (name == NULL)
168                         goto out;
169
170                 rc = lstcon_group_debug(args->lstio_dbg_timeout,
171                                         name, args->lstio_dbg_resultp);
172                 break;
173
174         case LST_OPC_NODES:
175                 if (args->lstio_dbg_count <= 0 ||
176                     args->lstio_dbg_idsp == NULL)
177                         goto out;
178
179                 rc = lstcon_nodes_debug(args->lstio_dbg_timeout,
180                                         args->lstio_dbg_count,
181                                         args->lstio_dbg_idsp,
182                                         args->lstio_dbg_resultp);
183                 break;
184
185         default:
186                 break;
187         }
188
189 out:
190         if (name != NULL)
191                 LIBCFS_FREE(name, args->lstio_dbg_nmlen + 1);
192
193         return rc;
194 }
195
196 static int
197 lst_group_add_ioctl(lstio_group_add_args_t *args)
198 {
199         char           *name;
200         int             rc;
201
202         if (args->lstio_grp_key != console_session.ses_key)
203                 return -EACCES;
204
205         if (args->lstio_grp_namep == NULL||
206             args->lstio_grp_nmlen <= 0 ||
207             args->lstio_grp_nmlen > LST_NAME_SIZE)
208                 return -EINVAL;
209
210         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
211         if (name == NULL)
212                 return -ENOMEM;
213
214         if (copy_from_user(name, args->lstio_grp_namep,
215                            args->lstio_grp_nmlen)) {
216                 LIBCFS_FREE(name, args->lstio_grp_nmlen);
217                 return -EFAULT;
218         }
219
220         name[args->lstio_grp_nmlen] = 0;
221
222         rc = lstcon_group_add(name);
223
224         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
225
226         return rc;
227 }
228
229 static int
230 lst_group_del_ioctl(lstio_group_del_args_t *args)
231 {
232         int     rc;
233         char   *name;
234
235         if (args->lstio_grp_key != console_session.ses_key)
236                 return -EACCES;
237
238         if (args->lstio_grp_namep == NULL ||
239             args->lstio_grp_nmlen <= 0 ||
240             args->lstio_grp_nmlen > LST_NAME_SIZE)
241                 return -EINVAL;
242
243         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
244         if (name == NULL)
245                 return -ENOMEM;
246
247         if (copy_from_user(name, args->lstio_grp_namep,
248                            args->lstio_grp_nmlen)) {
249                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
250                 return -EFAULT;
251         }
252
253         name[args->lstio_grp_nmlen] = 0;
254
255         rc = lstcon_group_del(name);
256
257         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
258
259         return rc;
260 }
261
262 static int
263 lst_group_update_ioctl(lstio_group_update_args_t *args)
264 {
265         int     rc;
266         char   *name;
267
268         if (args->lstio_grp_key != console_session.ses_key)
269                 return -EACCES;
270
271         if (args->lstio_grp_resultp == NULL ||
272             args->lstio_grp_namep == NULL ||
273             args->lstio_grp_nmlen <= 0 ||
274             args->lstio_grp_nmlen > LST_NAME_SIZE)
275                 return -EINVAL;
276
277         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
278         if (name == NULL)
279                 return -ENOMEM;
280
281         if (copy_from_user(name, args->lstio_grp_namep,
282                            args->lstio_grp_nmlen)) {
283                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
284                 return -EFAULT;
285         }
286
287         name[args->lstio_grp_nmlen] = 0;
288
289         switch (args->lstio_grp_opc) {
290         case LST_GROUP_CLEAN:
291                 rc = lstcon_group_clean(name, args->lstio_grp_args);
292                 break;
293
294         case LST_GROUP_REFRESH:
295                 rc = lstcon_group_refresh(name, args->lstio_grp_resultp);
296                 break;
297
298         case LST_GROUP_RMND:
299                 if (args->lstio_grp_count  <= 0 ||
300                     args->lstio_grp_idsp == NULL) {
301                         rc = -EINVAL;
302                         break;
303                 }
304                 rc = lstcon_nodes_remove(name, args->lstio_grp_count,
305                                          args->lstio_grp_idsp,
306                                          args->lstio_grp_resultp);
307                 break;
308
309         default:
310                 rc = -EINVAL;
311                 break;
312         }
313
314         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
315
316         return rc;
317 }
318
319 static int
320 lst_nodes_add_ioctl(lstio_group_nodes_args_t *args)
321 {
322         unsigned feats;
323         int     rc;
324         char   *name;
325
326         if (args->lstio_grp_key != console_session.ses_key)
327                 return -EACCES;
328
329         if (args->lstio_grp_idsp == NULL || /* array of ids */
330             args->lstio_grp_count <= 0 ||
331             args->lstio_grp_resultp == NULL ||
332             args->lstio_grp_featp == NULL ||
333             args->lstio_grp_namep == NULL ||
334             args->lstio_grp_nmlen <= 0 ||
335             args->lstio_grp_nmlen > LST_NAME_SIZE)
336                 return -EINVAL;
337
338         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
339         if (name == NULL)
340                 return -ENOMEM;
341
342         if (copy_from_user(name, args->lstio_grp_namep,
343                                args->lstio_grp_nmlen)) {
344                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
345
346                 return -EFAULT;
347         }
348
349         name[args->lstio_grp_nmlen] = 0;
350
351         rc = lstcon_nodes_add(name, args->lstio_grp_count,
352                               args->lstio_grp_idsp, &feats,
353                               args->lstio_grp_resultp);
354
355         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
356         if (rc == 0 &&
357             copy_to_user(args->lstio_grp_featp, &feats, sizeof(feats))) {
358                 return -EINVAL;
359         }
360
361         return rc;
362 }
363
364 static int
365 lst_group_list_ioctl(lstio_group_list_args_t *args)
366 {
367         if (args->lstio_grp_key != console_session.ses_key)
368                 return -EACCES;
369
370         if (args->lstio_grp_idx   < 0 ||
371             args->lstio_grp_namep == NULL ||
372             args->lstio_grp_nmlen <= 0 ||
373             args->lstio_grp_nmlen > LST_NAME_SIZE)
374                 return -EINVAL;
375
376         return lstcon_group_list(args->lstio_grp_idx,
377                               args->lstio_grp_nmlen,
378                               args->lstio_grp_namep);
379 }
380
381 static int
382 lst_group_info_ioctl(lstio_group_info_args_t *args)
383 {
384         char           *name;
385         int             ndent;
386         int             index;
387         int             rc;
388
389         if (args->lstio_grp_key != console_session.ses_key)
390                 return -EACCES;
391
392         if (args->lstio_grp_namep == NULL ||
393             args->lstio_grp_nmlen <= 0 ||
394             args->lstio_grp_nmlen > LST_NAME_SIZE)
395                 return -EINVAL;
396
397         if (args->lstio_grp_entp  == NULL && /* output: group entry */
398             args->lstio_grp_dentsp == NULL)  /* output: node entry */
399                 return -EINVAL;
400
401         if (args->lstio_grp_dentsp != NULL) { /* have node entry */
402                 if (args->lstio_grp_idxp == NULL || /* node index */
403                     args->lstio_grp_ndentp == NULL) /* # of node entry */
404                         return -EINVAL;
405
406                 if (copy_from_user(&ndent, args->lstio_grp_ndentp,
407                                    sizeof(ndent)) ||
408                     copy_from_user(&index, args->lstio_grp_idxp,
409                                    sizeof(index)))
410                         return -EFAULT;
411
412                 if (ndent <= 0 || index < 0)
413                         return -EINVAL;
414         }
415
416         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
417         if (name == NULL)
418                 return -ENOMEM;
419
420         if (copy_from_user(name, args->lstio_grp_namep,
421                            args->lstio_grp_nmlen)) {
422                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
423                 return -EFAULT;
424         }
425
426         name[args->lstio_grp_nmlen] = 0;
427
428         rc = lstcon_group_info(name, args->lstio_grp_entp,
429                                &index, &ndent, args->lstio_grp_dentsp);
430
431         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
432
433         if (rc != 0)
434                 return rc;
435
436         if (args->lstio_grp_dentsp != NULL &&
437             (copy_to_user(args->lstio_grp_idxp, &index, sizeof(index)) ||
438              copy_to_user(args->lstio_grp_ndentp, &ndent, sizeof(ndent))))
439                 return -EFAULT;
440
441         return 0;
442 }
443
444 static int
445 lst_batch_add_ioctl(lstio_batch_add_args_t *args)
446 {
447         int             rc;
448         char           *name;
449
450         if (args->lstio_bat_key != console_session.ses_key)
451                 return -EACCES;
452
453         if (args->lstio_bat_namep == NULL ||
454             args->lstio_bat_nmlen <= 0 ||
455             args->lstio_bat_nmlen > LST_NAME_SIZE)
456                 return -EINVAL;
457
458         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
459         if (name == NULL)
460                 return -ENOMEM;
461
462         if (copy_from_user(name, args->lstio_bat_namep,
463                            args->lstio_bat_nmlen)) {
464                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
465                 return -EFAULT;
466         }
467
468         name[args->lstio_bat_nmlen] = 0;
469
470         rc = lstcon_batch_add(name);
471
472         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
473
474         return rc;
475 }
476
477 static int
478 lst_batch_run_ioctl(lstio_batch_run_args_t *args)
479 {
480         int             rc;
481         char           *name;
482
483         if (args->lstio_bat_key != console_session.ses_key)
484                 return -EACCES;
485
486         if (args->lstio_bat_namep == NULL ||
487             args->lstio_bat_nmlen <= 0 ||
488             args->lstio_bat_nmlen > LST_NAME_SIZE)
489                 return -EINVAL;
490
491         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
492         if (name == NULL)
493                 return -ENOMEM;
494
495         if (copy_from_user(name, args->lstio_bat_namep,
496                            args->lstio_bat_nmlen)) {
497                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
498                 return -EFAULT;
499         }
500
501         name[args->lstio_bat_nmlen] = 0;
502
503         rc = lstcon_batch_run(name, args->lstio_bat_timeout,
504                               args->lstio_bat_resultp);
505
506         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
507
508         return rc;
509 }
510
511 static int
512 lst_batch_stop_ioctl(lstio_batch_stop_args_t *args)
513 {
514         int             rc;
515         char           *name;
516
517         if (args->lstio_bat_key != console_session.ses_key)
518                 return -EACCES;
519
520         if (args->lstio_bat_resultp == NULL ||
521             args->lstio_bat_namep == NULL ||
522             args->lstio_bat_nmlen <= 0 ||
523             args->lstio_bat_nmlen > LST_NAME_SIZE)
524                 return -EINVAL;
525
526         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
527         if (name == NULL)
528                 return -ENOMEM;
529
530         if (copy_from_user(name, args->lstio_bat_namep,
531                            args->lstio_bat_nmlen)) {
532                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
533                 return -EFAULT;
534         }
535
536         name[args->lstio_bat_nmlen] = 0;
537
538         rc = lstcon_batch_stop(name, args->lstio_bat_force,
539                                args->lstio_bat_resultp);
540
541         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
542
543         return rc;
544 }
545
546 static int
547 lst_batch_query_ioctl(lstio_batch_query_args_t *args)
548 {
549         char   *name;
550         int     rc;
551
552         if (args->lstio_bat_key != console_session.ses_key)
553                 return -EACCES;
554
555         if (args->lstio_bat_resultp == NULL ||
556             args->lstio_bat_namep == NULL ||
557             args->lstio_bat_nmlen <= 0 ||
558             args->lstio_bat_nmlen > LST_NAME_SIZE)
559                 return -EINVAL;
560
561         if (args->lstio_bat_testidx < 0)
562                 return -EINVAL;
563
564         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
565         if (name == NULL)
566                 return -ENOMEM;
567
568         if (copy_from_user(name, args->lstio_bat_namep,
569                            args->lstio_bat_nmlen)) {
570                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
571                 return -EFAULT;
572         }
573
574         name[args->lstio_bat_nmlen] = 0;
575
576         rc = lstcon_test_batch_query(name,
577                                      args->lstio_bat_testidx,
578                                      args->lstio_bat_client,
579                                      args->lstio_bat_timeout,
580                                      args->lstio_bat_resultp);
581
582         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
583
584         return rc;
585 }
586
587 static int
588 lst_batch_list_ioctl(lstio_batch_list_args_t *args)
589 {
590         if (args->lstio_bat_key != console_session.ses_key)
591                 return -EACCES;
592
593         if (args->lstio_bat_idx   < 0 ||
594             args->lstio_bat_namep == NULL ||
595             args->lstio_bat_nmlen <= 0 ||
596             args->lstio_bat_nmlen > LST_NAME_SIZE)
597                 return -EINVAL;
598
599         return lstcon_batch_list(args->lstio_bat_idx,
600                               args->lstio_bat_nmlen,
601                               args->lstio_bat_namep);
602 }
603
604 static int
605 lst_batch_info_ioctl(lstio_batch_info_args_t *args)
606 {
607         char           *name;
608         int             rc;
609         int             index;
610         int             ndent;
611
612         if (args->lstio_bat_key != console_session.ses_key)
613                 return -EACCES;
614
615         if (args->lstio_bat_namep == NULL || /* batch name */
616             args->lstio_bat_nmlen <= 0 ||
617             args->lstio_bat_nmlen > LST_NAME_SIZE)
618                 return -EINVAL;
619
620         if (args->lstio_bat_entp == NULL && /* output: batch entry */
621             args->lstio_bat_dentsp == NULL) /* output: node entry */
622                 return -EINVAL;
623
624         if (args->lstio_bat_dentsp != NULL) { /* have node entry */
625                 if (args->lstio_bat_idxp == NULL || /* node index */
626                     args->lstio_bat_ndentp == NULL) /* # of node entry */
627                         return -EINVAL;
628
629                 if (copy_from_user(&index, args->lstio_bat_idxp,
630                                        sizeof(index)) ||
631                     copy_from_user(&ndent, args->lstio_bat_ndentp,
632                                        sizeof(ndent)))
633                         return -EFAULT;
634
635                 if (ndent <= 0 || index < 0)
636                         return -EINVAL;
637         }
638
639         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
640         if (name == NULL)
641                 return -ENOMEM;
642
643         if (copy_from_user(name, args->lstio_bat_namep,
644                            args->lstio_bat_nmlen)) {
645                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
646                 return -EFAULT;
647         }
648
649         name[args->lstio_bat_nmlen] = 0;
650
651         rc = lstcon_batch_info(name,
652                             args->lstio_bat_entp, args->lstio_bat_server,
653                             args->lstio_bat_testidx, &index, &ndent,
654                             args->lstio_bat_dentsp);
655
656         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
657
658         if (rc != 0)
659                 return rc;
660
661         if (args->lstio_bat_dentsp != NULL &&
662             (copy_to_user(args->lstio_bat_idxp, &index, sizeof(index)) ||
663              copy_to_user(args->lstio_bat_ndentp, &ndent, sizeof(ndent))))
664                 rc = -EFAULT;
665
666         return rc;
667 }
668
669 static int
670 lst_stat_query_ioctl(lstio_stat_args_t *args)
671 {
672         int             rc;
673         char           *name = NULL;
674
675         /* TODO: not finished */
676         if (args->lstio_sta_key != console_session.ses_key)
677                 return -EACCES;
678
679         if (args->lstio_sta_resultp == NULL)
680                 return -EINVAL;
681
682         if (args->lstio_sta_idsp != NULL) {
683                 if (args->lstio_sta_count <= 0)
684                         return -EINVAL;
685
686                 rc = lstcon_nodes_stat(args->lstio_sta_count,
687                                        args->lstio_sta_idsp,
688                                        args->lstio_sta_timeout,
689                                        args->lstio_sta_resultp);
690         } else if (args->lstio_sta_namep != NULL) {
691                 if (args->lstio_sta_nmlen <= 0 ||
692                     args->lstio_sta_nmlen > LST_NAME_SIZE)
693                         return -EINVAL;
694
695                 LIBCFS_ALLOC(name, args->lstio_sta_nmlen + 1);
696                 if (name == NULL)
697                         return -ENOMEM;
698
699                 rc = copy_from_user(name, args->lstio_sta_namep,
700                                     args->lstio_sta_nmlen);
701                 if (rc == 0)
702                         rc = lstcon_group_stat(name, args->lstio_sta_timeout,
703                                                args->lstio_sta_resultp);
704                 else
705                         rc = -EFAULT;
706
707         } else {
708                 rc = -EINVAL;
709         }
710
711         if (name != NULL)
712                 LIBCFS_FREE(name, args->lstio_sta_nmlen + 1);
713         return rc;
714 }
715
716 static int lst_test_add_ioctl(lstio_test_args_t *args)
717 {
718         char            *batch_name;
719         char            *src_name = NULL;
720         char            *dst_name = NULL;
721         void            *param = NULL;
722         int             ret = 0;
723         int             rc = -ENOMEM;
724
725         if (args->lstio_tes_resultp == NULL ||
726             args->lstio_tes_retp == NULL ||
727             args->lstio_tes_bat_name == NULL || /* no specified batch */
728             args->lstio_tes_bat_nmlen <= 0 ||
729             args->lstio_tes_bat_nmlen > LST_NAME_SIZE ||
730             args->lstio_tes_sgrp_name == NULL || /* no source group */
731             args->lstio_tes_sgrp_nmlen <= 0 ||
732             args->lstio_tes_sgrp_nmlen > LST_NAME_SIZE ||
733             args->lstio_tes_dgrp_name == NULL || /* no target group */
734             args->lstio_tes_dgrp_nmlen <= 0 ||
735             args->lstio_tes_dgrp_nmlen > LST_NAME_SIZE)
736                 return -EINVAL;
737
738         if (args->lstio_tes_loop == 0 || /* negative is infinite */
739             args->lstio_tes_concur <= 0 ||
740             args->lstio_tes_dist <= 0 ||
741             args->lstio_tes_span <= 0)
742                 return -EINVAL;
743
744         /* have parameter, check if parameter length is valid */
745         if (args->lstio_tes_param != NULL &&
746             (args->lstio_tes_param_len <= 0 ||
747              args->lstio_tes_param_len >
748              PAGE_CACHE_SIZE - sizeof(lstcon_test_t)))
749                 return -EINVAL;
750
751         LIBCFS_ALLOC(batch_name, args->lstio_tes_bat_nmlen + 1);
752         if (batch_name == NULL)
753                 return rc;
754
755         LIBCFS_ALLOC(src_name, args->lstio_tes_sgrp_nmlen + 1);
756         if (src_name == NULL)
757                 goto out;
758
759         LIBCFS_ALLOC(dst_name, args->lstio_tes_dgrp_nmlen + 1);
760         if (dst_name == NULL)
761                 goto out;
762
763         if (args->lstio_tes_param != NULL) {
764                 LIBCFS_ALLOC(param, args->lstio_tes_param_len);
765                 if (param == NULL)
766                         goto out;
767                 if (copy_from_user(param, args->lstio_tes_param,
768                                    args->lstio_tes_param_len)) {
769                         rc = -EFAULT;
770                         goto out;
771                 }
772         }
773
774         rc = -EFAULT;
775         if (copy_from_user(batch_name, args->lstio_tes_bat_name,
776                            args->lstio_tes_bat_nmlen) ||
777             copy_from_user(src_name, args->lstio_tes_sgrp_name,
778                            args->lstio_tes_sgrp_nmlen) ||
779             copy_from_user(dst_name, args->lstio_tes_dgrp_name,
780                            args->lstio_tes_dgrp_nmlen))
781                 goto out;
782
783         rc = lstcon_test_add(batch_name,
784                             args->lstio_tes_type,
785                             args->lstio_tes_loop,
786                             args->lstio_tes_concur,
787                             args->lstio_tes_dist, args->lstio_tes_span,
788                             src_name, dst_name, param,
789                             args->lstio_tes_param_len,
790                             &ret, args->lstio_tes_resultp);
791
792         if (ret != 0)
793                 rc = (copy_to_user(args->lstio_tes_retp, &ret,
794                                        sizeof(ret))) ? -EFAULT : 0;
795 out:
796         if (batch_name != NULL)
797                 LIBCFS_FREE(batch_name, args->lstio_tes_bat_nmlen + 1);
798
799         if (src_name != NULL)
800                 LIBCFS_FREE(src_name, args->lstio_tes_sgrp_nmlen + 1);
801
802         if (dst_name != NULL)
803                 LIBCFS_FREE(dst_name, args->lstio_tes_dgrp_nmlen + 1);
804
805         if (param != NULL)
806                 LIBCFS_FREE(param, args->lstio_tes_param_len);
807
808         return rc;
809 }
810
811 int
812 lstcon_ioctl_entry(unsigned int cmd, struct libcfs_ioctl_hdr *hdr)
813 {
814         char   *buf;
815         struct libcfs_ioctl_data *data;
816         int     opc;
817         int     rc;
818
819         if (cmd != IOC_LIBCFS_LNETST)
820                 return -EINVAL;
821
822         data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
823
824         opc = data->ioc_u32[0];
825
826         if (data->ioc_plen1 > PAGE_CACHE_SIZE)
827                 return -EINVAL;
828
829         LIBCFS_ALLOC(buf, data->ioc_plen1);
830         if (buf == NULL)
831                 return -ENOMEM;
832
833         /* copy in parameter */
834         if (copy_from_user(buf, data->ioc_pbuf1, data->ioc_plen1)) {
835                 LIBCFS_FREE(buf, data->ioc_plen1);
836                 return -EFAULT;
837         }
838
839         mutex_lock(&console_session.ses_mutex);
840
841         console_session.ses_laststamp = cfs_time_current_sec();
842
843         if (console_session.ses_shutdown) {
844                 rc = -ESHUTDOWN;
845                 goto out;
846         }
847
848         if (console_session.ses_expired)
849                 lstcon_session_end();
850
851         if (opc != LSTIO_SESSION_NEW &&
852             console_session.ses_state == LST_SESSION_NONE) {
853                 CDEBUG(D_NET, "LST no active session\n");
854                 rc = -ESRCH;
855                 goto out;
856         }
857
858         memset(&console_session.ses_trans_stat, 0, sizeof(lstcon_trans_stat_t));
859
860         switch (opc) {
861         case LSTIO_SESSION_NEW:
862                 rc = lst_session_new_ioctl((lstio_session_new_args_t *)buf);
863                 break;
864         case LSTIO_SESSION_END:
865                 rc = lst_session_end_ioctl((lstio_session_end_args_t *)buf);
866                 break;
867         case LSTIO_SESSION_INFO:
868                 rc = lst_session_info_ioctl((lstio_session_info_args_t *)buf);
869                 break;
870         case LSTIO_DEBUG:
871                 rc = lst_debug_ioctl((lstio_debug_args_t *)buf);
872                 break;
873         case LSTIO_GROUP_ADD:
874                 rc = lst_group_add_ioctl((lstio_group_add_args_t *)buf);
875                 break;
876         case LSTIO_GROUP_DEL:
877                 rc = lst_group_del_ioctl((lstio_group_del_args_t *)buf);
878                 break;
879         case LSTIO_GROUP_UPDATE:
880                 rc = lst_group_update_ioctl((lstio_group_update_args_t *)buf);
881                 break;
882         case LSTIO_NODES_ADD:
883                 rc = lst_nodes_add_ioctl((lstio_group_nodes_args_t *)buf);
884                 break;
885         case LSTIO_GROUP_LIST:
886                 rc = lst_group_list_ioctl((lstio_group_list_args_t *)buf);
887                 break;
888         case LSTIO_GROUP_INFO:
889                 rc = lst_group_info_ioctl((lstio_group_info_args_t *)buf);
890                 break;
891         case LSTIO_BATCH_ADD:
892                 rc = lst_batch_add_ioctl((lstio_batch_add_args_t *)buf);
893                 break;
894         case LSTIO_BATCH_START:
895                 rc = lst_batch_run_ioctl((lstio_batch_run_args_t *)buf);
896                 break;
897         case LSTIO_BATCH_STOP:
898                 rc = lst_batch_stop_ioctl((lstio_batch_stop_args_t *)buf);
899                 break;
900         case LSTIO_BATCH_QUERY:
901                 rc = lst_batch_query_ioctl((lstio_batch_query_args_t *)buf);
902                 break;
903         case LSTIO_BATCH_LIST:
904                 rc = lst_batch_list_ioctl((lstio_batch_list_args_t *)buf);
905                 break;
906         case LSTIO_BATCH_INFO:
907                 rc = lst_batch_info_ioctl((lstio_batch_info_args_t *)buf);
908                 break;
909         case LSTIO_TEST_ADD:
910                 rc = lst_test_add_ioctl((lstio_test_args_t *)buf);
911                 break;
912         case LSTIO_STAT_QUERY:
913                 rc = lst_stat_query_ioctl((lstio_stat_args_t *)buf);
914                 break;
915         default:
916                 rc = -EINVAL;
917         }
918
919         if (copy_to_user(data->ioc_pbuf2, &console_session.ses_trans_stat,
920                          sizeof(lstcon_trans_stat_t)))
921                 rc = -EFAULT;
922 out:
923         mutex_unlock(&console_session.ses_mutex);
924
925         LIBCFS_FREE(buf, data->ioc_plen1);
926
927         return rc;
928 }