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